Added completion for :shortcut-remove.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 26 May 2014 13:13:44 +0000 (15:13 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 26 May 2014 13:13:44 +0000 (15:13 +0200)
Simplified some of the completions to avoid duplicate code.

src/bookmark.c
src/ex.c
src/handlers.c
src/setting.c
src/shortcut.c
src/shortcut.h
src/util.c
src/util.h

index c5028cc..69151ae 100644 (file)
@@ -151,10 +151,9 @@ gboolean bookmark_fill_completion(GtkListStore *store, const char *input)
 
 gboolean bookmark_fill_tag_completion(GtkListStore *store, const char *input)
 {
-    gboolean found = false;
+    gboolean found;
     unsigned int len, i;
-    GtkTreeIter iter;
-    GList *src = NULL, *tags = NULL, *l;
+    GList *src = NULL, *tags = NULL;
     Bookmark *bm;
 
     /* get all distinct tags from bookmark file */
@@ -172,21 +171,7 @@ gboolean bookmark_fill_tag_completion(GtkListStore *store, const char *input)
     }
 
     /* generate the completion with the found tags */
-    if (!input || !*input) {
-        for (l = tags; l; l = l->next) {
-            gtk_list_store_append(store, &iter);
-            gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1);
-            found = true;
-        }
-    } else {
-        for (l = tags; l; l = l->next) {
-            if (g_str_has_prefix(l->data, input)) {
-                gtk_list_store_append(store, &iter);
-                gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1);
-                found = true;
-            }
-        }
-    }
+    found = util_fill_completion(store, input, tags);
     g_list_free_full(src, (GDestroyNotify)free_bookmark);
     /* we don't need to free the values, because they where already removed by
      * freeing the src list - we never allocated new momory for them */
index 5450e8a..121bc3d 100644 (file)
--- a/src/ex.c
+++ b/src/ex.c
@@ -1024,6 +1024,11 @@ static gboolean complete(short direction)
                     found = bookmark_fill_tag_completion(store, token);
                     break;
 
+                case EX_SCR:
+                    sort  = true;
+                    found = shortcut_fill_completion(store, token);
+                    break;
+
                 case EX_HANDREM:
                     sort  = true;
                     found = handler_fill_completion(store, token);
index 5b9c23e..2aed472 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "main.h"
 #include "handlers.h"
-#include "completion.h"
 #include "util.h"
 
 extern VbCore vb;
@@ -72,26 +71,8 @@ gboolean handle_uri(const char *uri)
 
 gboolean handler_fill_completion(GtkListStore *store, const char *input)
 {
-    gboolean found = false;
-    GtkTreeIter iter;
     GList *src = g_hash_table_get_keys(handlers);
-
-    if (!input || !*input) {
-        for (GList *l = src; l; l = l->next) {
-            gtk_list_store_append(store, &iter);
-            gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1);
-            found = true;
-        }
-    } else {
-        for (GList *l = src; l; l = l->next) {
-            char *value = (char*)l->data;
-            if (g_str_has_prefix(value, input)) {
-                gtk_list_store_append(store, &iter);
-                gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1);
-                found = true;
-            }
-        }
-    }
+    gboolean found = util_fill_completion(store, input, src);
     g_list_free(src);
 
     return found;
index 75b8fff..deb281e 100644 (file)
@@ -241,26 +241,8 @@ gboolean setting_run(char *name, const char *param)
 
 gboolean setting_fill_completion(GtkListStore *store, const char *input)
 {
-    gboolean found = false;
-    GtkTreeIter iter;
     GList *src = g_hash_table_get_keys(settings);
-
-    if (!input || !*input) {
-        for (GList *l = src; l; l = l->next) {
-            gtk_list_store_append(store, &iter);
-            gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1);
-            found = true;
-        }
-    } else {
-        for (GList *l = src; l; l = l->next) {
-            char *value = (char*)l->data;
-            if (g_str_has_prefix(value, input)) {
-                gtk_list_store_append(store, &iter);
-                gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1);
-                found = true;
-            }
-        }
-    }
+    gboolean found = util_fill_completion(store, input, src);
     g_list_free(src);
 
     return found;
index 1d746e9..0ba1f8c 100644 (file)
@@ -104,6 +104,15 @@ char *shortcut_get_uri(const char *string)
     return uri;
 }
 
+gboolean shortcut_fill_completion(GtkListStore *store, const char *input)
+{
+    GList *src = g_hash_table_get_keys(shortcuts);
+    gboolean found = util_fill_completion(store, input, src);
+    g_list_free(src);
+
+    return found;
+}
+
 /**
  * Retrieves th highest placesholder number used in given string.
  * If no placeholder is found -1 is returned.
index 672365e..466f08f 100644 (file)
@@ -26,5 +26,6 @@ gboolean shortcut_add(const char *key, const char *uri);
 gboolean shortcut_remove(const char *key);
 gboolean shortcut_set_default(const char *key);
 char *shortcut_get_uri(const char *key);
+gboolean shortcut_fill_completion(GtkListStore *store, const char *input);
 
 #endif /* end of include guard: _SHORTCUT_H */
index f4ecfe3..64b2fd2 100644 (file)
@@ -24,6 +24,7 @@
 #include "main.h"
 #include "util.h"
 #include "ascii.h"
+#include "completion.h"
 
 char *util_get_config_dir(void)
 {
@@ -415,3 +416,31 @@ char *util_expand(const char *src)
 
     return result;
 }
+
+/**
+ * Fills the given list store by matching data of also given src list.
+ */
+gboolean util_fill_completion(GtkListStore *store, const char *input, GList *src)
+{
+    gboolean found = false;
+    GtkTreeIter iter;
+
+    if (!input || !*input) {
+        for (GList *l = src; l; l = l->next) {
+            gtk_list_store_append(store, &iter);
+            gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1);
+            found = true;
+        }
+    } else {
+        for (GList *l = src; l; l = l->next) {
+            char *value = (char*)l->data;
+            if (g_str_has_prefix(value, input)) {
+                gtk_list_store_append(store, &iter);
+                gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, l->data, -1);
+                found = true;
+            }
+        }
+    }
+
+    return found;
+}
index a4b51fb..ba74fb2 100644 (file)
@@ -41,5 +41,6 @@ char *util_str_replace(const char* search, const char* replace, const char* stri
 gboolean util_create_tmp_file(const char *content, char **file);
 char *util_build_path(const char *path, const char *dir);
 char *util_expand(const char *src);
+gboolean util_fill_completion(GtkListStore *store, const char *input, GList *src);
 
 #endif /* end of include guard: _UTIL_H */