diff options
author | James Booth <boothj5@gmail.com> | 2016-07-05 21:46:36 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2016-07-05 21:46:36 +0100 |
commit | 71178b3696cceae72c93d2e904ae41a811dec87d (patch) | |
tree | 4640007204d07e2cac8de011f5c20d8ecee2da27 /src | |
parent | fd218ac3e443650a7b310d4f8cde758f08c0ed8c (diff) | |
download | profani-tty-71178b3696cceae72c93d2e904ae41a811dec87d.tar.gz |
Store plugin completers by plugin name
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/api.c | 6 | ||||
-rw-r--r-- | src/plugins/autocompleters.c | 109 | ||||
-rw-r--r-- | src/plugins/autocompleters.h | 6 | ||||
-rw-r--r-- | src/tools/autocomplete.c | 18 | ||||
-rw-r--r-- | src/tools/autocomplete.h | 2 |
5 files changed, 96 insertions, 45 deletions
diff --git a/src/plugins/api.c b/src/plugins/api.c index ecd1749e..c8e6a166 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -163,19 +163,19 @@ api_register_timed(const char *const plugin_name, void *callback, int interval_s void api_completer_add(const char *const plugin_name, const char *key, char **items) { - autocompleters_add(key, items); + autocompleters_add(plugin_name, key, items); } void api_completer_remove(const char *const plugin_name, const char *key, char **items) { - autocompleters_remove(key, items); + autocompleters_remove(plugin_name, key, items); } void api_completer_clear(const char *const plugin_name, const char *key) { - autocompleters_clear(key); + autocompleters_clear(plugin_name, key); } void diff --git a/src/plugins/autocompleters.c b/src/plugins/autocompleters.c index 36ffea4a..ab4a70b7 100644 --- a/src/plugins/autocompleters.c +++ b/src/plugins/autocompleters.c @@ -38,56 +38,71 @@ #include "tools/autocomplete.h" -static GHashTable *autocompleters; +static GHashTable *plugin_to_acs; + +static void +_free_autocompleters(GHashTable *key_to_ac) +{ + g_hash_table_destroy(key_to_ac); +} void autocompleters_init(void) { - autocompleters = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)autocomplete_free); + plugin_to_acs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_free_autocompleters); } void -autocompleters_add(const char *key, char **items) +autocompleters_add(const char *const plugin_name, const char *key, char **items) { - if (g_hash_table_contains(autocompleters, key)) { - Autocomplete existing_ac = g_hash_table_lookup(autocompleters, key); - - int i = 0; - for (i = 0; i < g_strv_length(items); i++) { - autocomplete_add(existing_ac, items[i]); + GHashTable *key_to_ac = g_hash_table_lookup(plugin_to_acs, plugin_name); + if (key_to_ac) { + if (g_hash_table_contains(key_to_ac, key)) { + Autocomplete existing_ac = g_hash_table_lookup(key_to_ac, key); + autocomplete_add_all(existing_ac, items); + } else { + Autocomplete new_ac = autocomplete_new(); + autocomplete_add_all(new_ac, items); + g_hash_table_insert(key_to_ac, strdup(key), new_ac); } } else { + key_to_ac = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)autocomplete_free); Autocomplete new_ac = autocomplete_new(); - int i = 0; - for (i = 0; i < g_strv_length(items); i++) { - autocomplete_add(new_ac, items[i]); - } - g_hash_table_insert(autocompleters, strdup(key), new_ac); + autocomplete_add_all(new_ac, items); + g_hash_table_insert(key_to_ac, strdup(key), new_ac); + g_hash_table_insert(plugin_to_acs, strdup(plugin_name), key_to_ac); } } void -autocompleters_remove(const char *key, char **items) +autocompleters_remove(const char *const plugin_name, const char *key, char **items) { - if (!g_hash_table_contains(autocompleters, key)) { + GHashTable *key_to_ac = g_hash_table_lookup(plugin_to_acs, plugin_name); + if (!key_to_ac) { return; } - Autocomplete ac = g_hash_table_lookup(autocompleters, key); - int i = 0; - for (i = 0; i < g_strv_length(items); i++) { - autocomplete_remove(ac, items[i]); + if (!g_hash_table_contains(key_to_ac, key)) { + return; } + + Autocomplete ac = g_hash_table_lookup(key_to_ac, key); + autocomplete_remove_all(ac, items); } void -autocompleters_clear(const char *key) +autocompleters_clear(const char *const plugin_name, const char *key) { - if (!g_hash_table_contains(autocompleters, key)) { + GHashTable *key_to_ac = g_hash_table_lookup(plugin_to_acs, plugin_name); + if (!key_to_ac) { return; } - Autocomplete ac = g_hash_table_lookup(autocompleters, key); + if (!g_hash_table_contains(key_to_ac, key)) { + return; + } + + Autocomplete ac = g_hash_table_lookup(key_to_ac, key); autocomplete_clear(ac); } @@ -96,17 +111,26 @@ autocompleters_complete(const char * const input) { char *result = NULL; - GList *keys = g_hash_table_get_keys(autocompleters); - GList *curr = keys; - while (curr) { - result = autocomplete_param_with_ac(input, curr->data, g_hash_table_lookup(autocompleters, curr->data), TRUE); - if (result) { - g_list_free(keys); - return result; + GList *ac_hashes = g_hash_table_get_values(plugin_to_acs); + GList *curr_hash = ac_hashes; + while (curr_hash) { + GHashTable *key_to_ac = curr_hash->data; + + GList *keys = g_hash_table_get_keys(key_to_ac); + GList *curr = keys; + while (curr) { + result = autocomplete_param_with_ac(input, curr->data, g_hash_table_lookup(key_to_ac, curr->data), TRUE); + if (result) { + g_list_free(keys); + return result; + } + curr = g_list_next(curr); } - curr = g_list_next(curr); + g_list_free(keys); + + curr_hash = g_list_next(curr_hash); } - g_list_free(keys); + g_list_free(ac_hashes); return NULL; } @@ -114,17 +138,24 @@ autocompleters_complete(const char * const input) void autocompleters_reset(void) { - GList *acs = g_hash_table_get_values(autocompleters); - GList *curr = acs; - while (curr) { - autocomplete_reset(curr->data); - curr = g_list_next(curr); + GList *ac_hashes = g_hash_table_get_values(plugin_to_acs); + GList *curr_hash = ac_hashes; + while (curr_hash) { + GList *acs = g_hash_table_get_values(curr_hash->data); + GList *curr = acs; + while (curr) { + autocomplete_reset(curr->data); + curr = g_list_next(curr); + } + + g_list_free(acs); + curr_hash = g_list_next(curr_hash); } - g_list_free(acs); + g_list_free(ac_hashes); } void autocompleters_destroy(void) { - g_hash_table_destroy(autocompleters); + g_hash_table_destroy(plugin_to_acs); } diff --git a/src/plugins/autocompleters.h b/src/plugins/autocompleters.h index a04dfe2f..1350ba7c 100644 --- a/src/plugins/autocompleters.h +++ b/src/plugins/autocompleters.h @@ -38,9 +38,9 @@ #include <glib.h> void autocompleters_init(void); -void autocompleters_add(const char *key, char **items); -void autocompleters_remove(const char *key, char **items); -void autocompleters_clear(const char *key); +void autocompleters_add(const char *const plugin_name, const char *key, char **items); +void autocompleters_remove(const char *const plugin_name, const char *key, char **items); +void autocompleters_clear(const char *const plugin_name, const char *key); char* autocompleters_complete(const char * const input); void autocompleters_reset(void); void autocompleters_destroy(void); diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c index 2484a6e1..258f02da 100644 --- a/src/tools/autocomplete.c +++ b/src/tools/autocomplete.c @@ -118,6 +118,15 @@ autocomplete_add(Autocomplete ac, const char *item) } void +autocomplete_add_all(Autocomplete ac, char **items) +{ + int i = 0; + for (i = 0; i < g_strv_length(items); i++) { + autocomplete_add(ac, items[i]); + } +} + +void autocomplete_remove(Autocomplete ac, const char *const item) { if (ac) { @@ -139,6 +148,15 @@ autocomplete_remove(Autocomplete ac, const char *const item) return; } +void +autocomplete_remove_all(Autocomplete ac, char **items) +{ + int i = 0; + for (i = 0; i < g_strv_length(items); i++) { + autocomplete_remove(ac, items[i]); + } +} + GSList* autocomplete_create_list(Autocomplete ac) { diff --git a/src/tools/autocomplete.h b/src/tools/autocomplete.h index 9c2f8fc2..12b60718 100644 --- a/src/tools/autocomplete.h +++ b/src/tools/autocomplete.h @@ -50,7 +50,9 @@ void autocomplete_clear(Autocomplete ac); void autocomplete_free(Autocomplete ac); void autocomplete_add(Autocomplete ac, const char *item); +void autocomplete_add_all(Autocomplete ac, char **items); void autocomplete_remove(Autocomplete ac, const char *const item); +void autocomplete_remove_all(Autocomplete ac, char **items); // find the next item prefixed with search string gchar* autocomplete_complete(Autocomplete ac, const gchar *search_str, gboolean quote); |