1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Renamed autocomplete function to get list

This commit is contained in:
James Booth 2014-09-26 00:06:50 +01:00
parent 0afdbfaf03
commit 0b2a1a1bc7
6 changed files with 39 additions and 27 deletions

View File

@ -102,13 +102,13 @@ muc_invite_count(void)
GSList *
muc_get_invites(void)
{
return autocomplete_get_list(invite_ac);
return autocomplete_create_list(invite_ac);
}
gboolean
muc_invites_include(const char * const room)
{
GSList *invites = autocomplete_get_list(invite_ac);
GSList *invites = autocomplete_create_list(invite_ac);
GSList *curr = invites;
while (curr != NULL) {
if (strcmp(curr->data, room) == 0) {

View File

@ -339,7 +339,7 @@ roster_get_group(const char * const group)
GSList *
roster_get_groups(void)
{
return autocomplete_get_list(groups_ac);
return autocomplete_create_list(groups_ac);
}
char *

View File

@ -62,7 +62,7 @@ autocomplete_new(void)
void
autocomplete_clear(Autocomplete ac)
{
if (ac != NULL) {
if (ac) {
g_slist_free_full(ac->items, free);
ac->items = NULL;
@ -87,9 +87,9 @@ autocomplete_free(Autocomplete ac)
gint
autocomplete_length(Autocomplete ac)
{
if (ac == NULL) {
if (!ac) {
return 0;
} else if (ac->items == NULL) {
} else if (!ac->items) {
return 0;
} else {
return g_slist_length(ac->items);
@ -99,25 +99,26 @@ autocomplete_length(Autocomplete ac)
void
autocomplete_add(Autocomplete ac, const char *item)
{
if (ac != NULL) {
if (ac) {
char *item_cpy;
GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);
// if item already exists
if (curr != NULL) {
if (curr) {
return;
}
item_cpy = strdup(item);
ac->items = g_slist_insert_sorted(ac->items, item_cpy, (GCompareFunc)strcmp);
}
return;
}
void
autocomplete_remove(Autocomplete ac, const char * const item)
{
if (ac != NULL) {
if (ac) {
GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);
if (!curr) {
@ -137,7 +138,7 @@ autocomplete_remove(Autocomplete ac, const char * const item)
}
GSList *
autocomplete_get_list(Autocomplete ac)
autocomplete_create_list(Autocomplete ac)
{
GSList *copy = NULL;
GSList *curr = ac->items;
@ -171,36 +172,43 @@ autocomplete_complete(Autocomplete ac, gchar *search_str, gboolean quote)
gchar *found = NULL;
// no autocomplete to search
if (ac == NULL)
if (!ac) {
return NULL;
}
// no items to search
if (!ac->items)
if (!ac->items) {
return NULL;
}
// first search attempt
if (ac->last_found == NULL) {
if (ac->search_str != NULL) {
if (!ac->last_found) {
if (ac->search_str) {
FREE_SET_NULL(ac->search_str);
}
ac->search_str = strdup(search_str);
found = _search_from(ac, ac->items, quote);
return found;
// subsequent search attempt
} else {
// search from here+1 to end
found = _search_from(ac, g_slist_next(ac->last_found), quote);
if (found != NULL)
if (found) {
return found;
}
// search from beginning
found = _search_from(ac, ac->items, quote);
if (found != NULL)
if (found) {
return found;
}
// we found nothing, reset search
autocomplete_reset(ac);
return NULL;
}
}
@ -224,7 +232,7 @@ autocomplete_param_with_func(char *input, int *size, char *command,
inp_cpy[(*size) - len] = '\0';
char *found = func(inp_cpy);
if (found != NULL) {
if (found) {
auto_msg = g_string_new(command_cpy);
g_string_append(auto_msg, found);
free(found);
@ -254,7 +262,7 @@ autocomplete_param_with_ac(char *input, int *size, char *command,
inp_cpy[(*size) - len] = '\0';
char *found = autocomplete_complete(ac, inp_cpy, quote);
if (found != NULL) {
if (found) {
auto_msg = g_string_new(command_cpy);
g_string_append(auto_msg, found);
free(found);
@ -292,9 +300,9 @@ autocomplete_param_no_with_func(char *input, int *size, char *command,
gchar *comp_str = g_strdup(&inp_cpy[strlen(start_str)]);
// autocomplete param
if (comp_str != NULL) {
if (comp_str) {
char *found = func(comp_str);
if (found != NULL) {
if (found) {
result_str = g_string_new("");
g_string_append(result_str, start_str);
g_string_append(result_str, found);

View File

@ -55,7 +55,7 @@ void autocomplete_remove(Autocomplete ac, const char * const item);
// find the next item prefixed with search string
gchar * autocomplete_complete(Autocomplete ac, gchar *search_str, gboolean quote);
GSList * autocomplete_get_list(Autocomplete ac);
GSList * autocomplete_create_list(Autocomplete ac);
gint autocomplete_length(Autocomplete ac);
char * autocomplete_param_with_func(char *input, int *size, char *command,

View File

@ -142,7 +142,7 @@ _presence_subscription(const char * const jid, const jabber_subscr_t action)
static GSList *
_presence_get_subscription_requests(void)
{
return autocomplete_get_list(sub_requests_ac);
return autocomplete_create_list(sub_requests_ac);
}
static gint
@ -167,7 +167,7 @@ static gboolean
_presence_sub_request_exists(const char * const bare_jid)
{
gboolean result = FALSE;
GSList *requests_p = autocomplete_get_list(sub_requests_ac);
GSList *requests_p = autocomplete_create_list(sub_requests_ac);
GSList *requests = requests_p;
while (requests != NULL) {

View File

@ -31,11 +31,12 @@ void find_after_create(void **state)
void get_after_create_returns_null(void **state)
{
Autocomplete ac = autocomplete_new();
GSList *result = autocomplete_get_list(ac);
GSList *result = autocomplete_create_list(ac);
assert_null(result);
autocomplete_clear(ac);
g_slist_free_full(result, g_free);
}
void add_one_and_complete(void **state)
@ -79,11 +80,12 @@ void add_two_adds_two(void **state)
Autocomplete ac = autocomplete_new();
autocomplete_add(ac, "Hello");
autocomplete_add(ac, "Help");
GSList *result = autocomplete_get_list(ac);
GSList *result = autocomplete_create_list(ac);
assert_int_equal(2, g_slist_length(result));
autocomplete_clear(ac);
g_slist_free_full(result, g_free);
}
void add_two_same_adds_one(void **state)
@ -91,11 +93,12 @@ void add_two_same_adds_one(void **state)
Autocomplete ac = autocomplete_new();
autocomplete_add(ac, "Hello");
autocomplete_add(ac, "Hello");
GSList *result = autocomplete_get_list(ac);
GSList *result = autocomplete_create_list(ac);
assert_int_equal(1, g_slist_length(result));
autocomplete_clear(ac);
g_slist_free_full(result, g_free);
}
void add_two_same_updates(void **state)
@ -103,7 +106,7 @@ void add_two_same_updates(void **state)
Autocomplete ac = autocomplete_new();
autocomplete_add(ac, "Hello");
autocomplete_add(ac, "Hello");
GSList *result = autocomplete_get_list(ac);
GSList *result = autocomplete_create_list(ac);
GSList *first = g_slist_nth(result, 0);
@ -112,4 +115,5 @@ void add_two_same_updates(void **state)
assert_string_equal("Hello", str);
autocomplete_clear(ac);
g_slist_free_full(result, g_free);
}