about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/tools/autocomplete.c29
-rw-r--r--src/tools/autocomplete.h3
-rw-r--r--src/ui/window_list.c4
3 files changed, 26 insertions, 10 deletions
diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c
index 6c960ba2..ca5fe772 100644
--- a/src/tools/autocomplete.c
+++ b/src/tools/autocomplete.c
@@ -134,6 +134,23 @@ autocomplete_update(Autocomplete ac, char **items)
 }
 
 void
+autocomplete_add_reverse(Autocomplete ac, const char *item)
+{
+    if (ac) {
+        char *item_cpy;
+        GList *curr = g_list_find_custom(ac->items, item, (GCompareFunc)strcmp);
+
+        // if item already exists
+        if (curr) {
+            return;
+        }
+
+        item_cpy = strdup(item);
+        ac->items = g_list_prepend(ac->items, item_cpy);
+    }
+}
+
+void
 autocomplete_add(Autocomplete ac, const char *item)
 {
     if (ac) {
@@ -148,8 +165,6 @@ autocomplete_add(Autocomplete ac, const char *item)
         item_cpy = strdup(item);
         ac->items = g_list_insert_sorted(ac->items, item_cpy, (GCompareFunc)strcmp);
     }
-
-    return;
 }
 
 void
@@ -385,14 +400,14 @@ autocomplete_param_no_with_func(const char *const input, char *command, int arg_
     return NULL;
 }
 
-/* remove the first message if we have more than max */
+/* remove the last message if we have more than max */
 void
-autocomplete_remove_older_than_max(Autocomplete ac, int maxsize)
+autocomplete_remove_older_than_max_reverse(Autocomplete ac, int maxsize)
 {
     if (autocomplete_length(ac) > maxsize) {
-        GList *first = g_list_nth(ac->items, 0);
-        if (first) {
-            ac->items = g_list_delete_link(ac->items, first);
+        GList *last = g_list_last(ac->items);
+        if (last) {
+            ac->items = g_list_delete_link(ac->items, last);
         }
     }
 }
diff --git a/src/tools/autocomplete.h b/src/tools/autocomplete.h
index 10bbbf61..6f4fe9c7 100644
--- a/src/tools/autocomplete.h
+++ b/src/tools/autocomplete.h
@@ -55,6 +55,7 @@ void autocomplete_add_all(Autocomplete ac, char **items);
 void autocomplete_update(Autocomplete ac, char **items);
 void autocomplete_remove(Autocomplete ac, const char *const item);
 void autocomplete_remove_all(Autocomplete ac, char **items);
+void autocomplete_add_reverse(Autocomplete ac, const char *item);
 
 // find the next item prefixed with search string
 gchar* autocomplete_complete(Autocomplete ac, const gchar *search_str, gboolean quote, gboolean previous);
@@ -75,5 +76,5 @@ void autocomplete_reset(Autocomplete ac);
 
 gboolean autocomplete_contains(Autocomplete ac, const char *value);
 
-void autocomplete_remove_older_than_max(Autocomplete ac, int maxsize);
+void autocomplete_remove_older_than_max_reverse(Autocomplete ac, int maxsize);
 #endif
diff --git a/src/ui/window_list.c b/src/ui/window_list.c
index e506a957..01b5177f 100644
--- a/src/ui/window_list.c
+++ b/src/ui/window_list.c
@@ -1163,9 +1163,9 @@ wins_add_urls_ac(const ProfWin *const win, const ProfMessage *const message)
     {
         gchar *word = g_match_info_fetch (match_info, 0);
 
-        autocomplete_add(win->urls_ac, word);
+        autocomplete_add_reverse(win->urls_ac, word);
         // for people who run profanity a long time, we don't want to waste a lot of memory
-        autocomplete_remove_older_than_max(win->urls_ac, 20);
+        autocomplete_remove_older_than_max_reverse(win->urls_ac, 20);
 
         g_free (word);
         g_match_info_next (match_info, NULL);
an class="n">GList* commands = plugins_get_command_names(); assert_true(commands == NULL); callbacks_close(); g_list_free(commands); } void returns_commands(void** state) { callbacks_init(); PluginCommand* command1 = malloc(sizeof(PluginCommand)); command1->command_name = strdup("command1"); callbacks_add_command("plugin1", command1); PluginCommand* command2 = malloc(sizeof(PluginCommand)); command2->command_name = strdup("command2"); callbacks_add_command("plugin1", command2); PluginCommand* command3 = malloc(sizeof(PluginCommand)); command3->command_name = strdup("command3"); callbacks_add_command("plugin2", command3); GList* names = plugins_get_command_names(); assert_true(g_list_length(names) == 3); gboolean foundCommand1 = FALSE; gboolean foundCommand2 = FALSE; gboolean foundCommand3 = FALSE; GList* curr = names; while (curr) { if (g_strcmp0(curr->data, "command1") == 0) { foundCommand1 = TRUE; } if (g_strcmp0(curr->data, "command2") == 0) { foundCommand2 = TRUE; } if (g_strcmp0(curr->data, "command3") == 0) { foundCommand3 = TRUE; } curr = g_list_next(curr); } assert_true(foundCommand1 && foundCommand2 && foundCommand3); g_list_free(names); // TODO: why does this make the test fail? // callbacks_close(); }