mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
Simplified autocompleter
This commit is contained in:
parent
34238ad6a4
commit
a85ba5e2cb
@ -29,59 +29,25 @@ struct p_autocomplete_t {
|
|||||||
GSList *items;
|
GSList *items;
|
||||||
GSList *last_found;
|
GSList *last_found;
|
||||||
gchar *search_str;
|
gchar *search_str;
|
||||||
PStrFunc str_func;
|
|
||||||
PCopyFunc copy_func;
|
|
||||||
PEqualDeepFunc equal_deep_func;
|
|
||||||
GDestroyNotify free_func;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static gchar * _search_from(PAutocomplete ac, GSList *curr);
|
static gchar * _search_from(PAutocomplete ac, GSList *curr);
|
||||||
static const char *_str_func_default(const char *orig);
|
|
||||||
static const char *_copy_func_default(const char *orig);
|
|
||||||
static int _deep_equals_func_default(const char *o1, const char *o2);
|
|
||||||
|
|
||||||
PAutocomplete
|
PAutocomplete
|
||||||
p_autocomplete_new(void)
|
p_autocomplete_new(void)
|
||||||
{
|
|
||||||
return p_obj_autocomplete_new(NULL, NULL, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
PAutocomplete
|
|
||||||
p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
|
|
||||||
PEqualDeepFunc equal_deep_func, GDestroyNotify free_func)
|
|
||||||
{
|
{
|
||||||
PAutocomplete new = malloc(sizeof(struct p_autocomplete_t));
|
PAutocomplete new = malloc(sizeof(struct p_autocomplete_t));
|
||||||
new->items = NULL;
|
new->items = NULL;
|
||||||
new->last_found = NULL;
|
new->last_found = NULL;
|
||||||
new->search_str = NULL;
|
new->search_str = NULL;
|
||||||
|
|
||||||
if (str_func)
|
|
||||||
new->str_func = str_func;
|
|
||||||
else
|
|
||||||
new->str_func = (PStrFunc)_str_func_default;
|
|
||||||
|
|
||||||
if (copy_func)
|
|
||||||
new->copy_func = copy_func;
|
|
||||||
else
|
|
||||||
new->copy_func = (PCopyFunc)_copy_func_default;
|
|
||||||
|
|
||||||
if (free_func)
|
|
||||||
new->free_func = free_func;
|
|
||||||
else
|
|
||||||
new->free_func = (GDestroyNotify)free;
|
|
||||||
|
|
||||||
if (equal_deep_func)
|
|
||||||
new->equal_deep_func = equal_deep_func;
|
|
||||||
else
|
|
||||||
new->equal_deep_func = (PEqualDeepFunc)_deep_equals_func_default;
|
|
||||||
|
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
p_autocomplete_clear(PAutocomplete ac)
|
p_autocomplete_clear(PAutocomplete ac)
|
||||||
{
|
{
|
||||||
g_slist_free_full(ac->items, ac->free_func);
|
g_slist_free_full(ac->items, free);
|
||||||
ac->items = NULL;
|
ac->items = NULL;
|
||||||
|
|
||||||
p_autocomplete_reset(ac);
|
p_autocomplete_reset(ac);
|
||||||
@ -109,16 +75,16 @@ p_autocomplete_add(PAutocomplete ac, void *item)
|
|||||||
while(curr) {
|
while(curr) {
|
||||||
|
|
||||||
// insert
|
// insert
|
||||||
if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) > 0) {
|
if (g_strcmp0(curr->data, item) > 0) {
|
||||||
ac->items = g_slist_insert_before(ac->items,
|
ac->items = g_slist_insert_before(ac->items,
|
||||||
curr, item);
|
curr, item);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
||||||
// update
|
// update
|
||||||
} else if (g_strcmp0(ac->str_func(curr->data), ac->str_func(item)) == 0) {
|
} else if (g_strcmp0(curr->data, item) == 0) {
|
||||||
// only update if data different
|
// only update if data different
|
||||||
if (!ac->equal_deep_func(curr->data, item)) {
|
if (strcmp(curr->data, item) != 0) {
|
||||||
ac->free_func(curr->data);
|
free(curr->data);
|
||||||
curr->data = item;
|
curr->data = item;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
} else {
|
} else {
|
||||||
@ -141,7 +107,7 @@ p_autocomplete_remove(PAutocomplete ac, const char * const item)
|
|||||||
{
|
{
|
||||||
// reset last found if it points to the item to be removed
|
// reset last found if it points to the item to be removed
|
||||||
if (ac->last_found != NULL)
|
if (ac->last_found != NULL)
|
||||||
if (g_strcmp0(ac->str_func(ac->last_found->data), item) == 0)
|
if (g_strcmp0(ac->last_found->data, item) == 0)
|
||||||
ac->last_found = NULL;
|
ac->last_found = NULL;
|
||||||
|
|
||||||
if (!ac->items) {
|
if (!ac->items) {
|
||||||
@ -150,10 +116,10 @@ p_autocomplete_remove(PAutocomplete ac, const char * const item)
|
|||||||
GSList *curr = ac->items;
|
GSList *curr = ac->items;
|
||||||
|
|
||||||
while(curr) {
|
while(curr) {
|
||||||
if (g_strcmp0(ac->str_func(curr->data), item) == 0) {
|
if (g_strcmp0(curr->data, item) == 0) {
|
||||||
void *current_item = curr->data;
|
void *current_item = curr->data;
|
||||||
ac->items = g_slist_remove(ac->items, curr->data);
|
ac->items = g_slist_remove(ac->items, curr->data);
|
||||||
ac->free_func(current_item);
|
free(current_item);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
@ -172,7 +138,7 @@ p_autocomplete_get_list(PAutocomplete ac)
|
|||||||
GSList *curr = ac->items;
|
GSList *curr = ac->items;
|
||||||
|
|
||||||
while(curr) {
|
while(curr) {
|
||||||
copy = g_slist_append(copy, ac->copy_func(curr->data));
|
copy = g_slist_append(copy, strdup(curr->data));
|
||||||
curr = g_slist_next(curr);
|
curr = g_slist_next(curr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,17 +187,17 @@ _search_from(PAutocomplete ac, GSList *curr)
|
|||||||
while(curr) {
|
while(curr) {
|
||||||
|
|
||||||
// match found
|
// match found
|
||||||
if (strncmp(ac->str_func(curr->data),
|
if (strncmp(curr->data,
|
||||||
ac->search_str,
|
ac->search_str,
|
||||||
strlen(ac->search_str)) == 0) {
|
strlen(ac->search_str)) == 0) {
|
||||||
gchar *result =
|
gchar *result =
|
||||||
(gchar *) malloc((strlen(ac->str_func(curr->data)) + 1) * sizeof(gchar));
|
(gchar *) malloc((strlen(curr->data) + 1) * sizeof(gchar));
|
||||||
|
|
||||||
// set pointer to last found
|
// set pointer to last found
|
||||||
ac->last_found = curr;
|
ac->last_found = curr;
|
||||||
|
|
||||||
// return the string, must be free'd by caller
|
// return the string, must be free'd by caller
|
||||||
strcpy(result, ac->str_func(curr->data));
|
strcpy(result, curr->data);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,21 +206,3 @@ _search_from(PAutocomplete ac, GSList *curr)
|
|||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
|
||||||
_str_func_default(const char *orig)
|
|
||||||
{
|
|
||||||
return orig;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *
|
|
||||||
_copy_func_default(const char *orig)
|
|
||||||
{
|
|
||||||
return strdup(orig);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
_deep_equals_func_default(const char *o1, const char *o2)
|
|
||||||
{
|
|
||||||
return (strcmp(o1, o2) == 0);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user