about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-05-29 11:26:18 +0200
committerMichael Vetter <jubalh@iodoru.org>2020-05-29 11:26:18 +0200
commitbfaf737efad0766e62e8c9100a7c96ca97346e51 (patch)
treea39e3277a5805dbe848b92ab8d714b9d4dc2d364 /src
parente19bf0e30321981ca46ff3370f479679ebc7e240 (diff)
downloadprofani-tty-bfaf737efad0766e62e8c9100a7c96ca97346e51.tar.gz
urlopen: get last URL first
Fix https://github.com/profanity-im/profanity/issues/1348
Diffstat (limited to 'src')
-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);