Added completion for bookmark tags.
authorDaniel Carl <danielcarl@gmx.de>
Fri, 6 Sep 2013 20:41:52 +0000 (22:41 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Fri, 6 Sep 2013 20:41:52 +0000 (22:41 +0200)
This allows to get already used bookmark tags for new bookmarks ':bma <tab>'
or ':bookmark-add <tab>'.

doc/vimb.1
src/bookmark.c
src/bookmark.h
src/completion.c
src/main.c
src/main.h

index 319d0a9..8a9f6c8 100644 (file)
@@ -460,6 +460,10 @@ Example:
 ":open \fB!\fPfoo ba" will match all bookmark that have the tags "foo" or
 "foot" and tags starting with "ba" like "ball".
 .TP
+.B boomark tags
+The boomark tag completion allows to insert already used bookmars for the
+`:bma ` or `:bookmark-add ` commands.
+.TP
 .B search
 The search completion allow to get a filtered list of already done searches.
 This completion starts by `/` or `?` in inputbox and performs a prefix
index 2fecaa7..3cbd281 100644 (file)
@@ -149,6 +149,52 @@ gboolean bookmark_fill_completion(GtkListStore *store, const char *input)
     return found;
 }
 
+gboolean bookmark_fill_tag_completion(GtkListStore *store, const char *input)
+{
+    gboolean found = false;
+    unsigned int len, i;
+    GtkTreeIter iter;
+    GList *src = NULL, *tags = NULL, *l;
+    Bookmark *bm;
+
+    /* get all distinct tags from bookmark file */
+    src = load(vb.files[FILES_BOOKMARK]);
+    for (GList *l = src; l; l = l->next) {
+        bm = (Bookmark*)l->data;
+        len = (bm->tags) ? g_strv_length(bm->tags) : 0;
+        for (i = 0; i < len; i++) {
+            char *tag = bm->tags[i];
+            /* add tag only if it isn't already in the list */
+            if (!g_list_find_custom(tags, tag, (GCompareFunc)strcmp)) {
+                tags = g_list_prepend(tags, tag);
+            }
+        }
+    }
+
+    /* generate the completion with the found tags */
+    if (!input || *input == '\0') {
+        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;
+            }
+        }
+    }
+    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 */
+    g_list_free(tags);
+
+    return found;
+}
+
 #ifdef FEATURE_QUEUE
 /**
  * Push a uri to the end of the queue.
index a346905..d7a8e56 100644 (file)
@@ -23,6 +23,7 @@
 gboolean bookmark_add(const char *uri, const char *title, const char *tags);
 gboolean bookmark_remove(const char *uri);
 gboolean bookmark_fill_completion(GtkListStore *store, const char *input);
+gboolean bookmark_fill_tag_completion(GtkListStore *store, const char *input);
 #ifdef FEATURE_QUEUE
 gboolean bookmark_queue_push(const char *uri);
 gboolean bookmark_queue_unshift(const char *uri);
index 663fb07..1f8aa14 100644 (file)
@@ -94,6 +94,8 @@ gboolean completion_complete(gboolean back)
         res = command_fill_completion(store, command);
     } else if (type == VB_INPUT_SEARCH_FORWARD || type == VB_INPUT_SEARCH_BACKWARD) {
         res = history_fill_completion(store, HISTORY_SEARCH, suffix);
+    } else if (type == VB_INPUT_BOOKMARK_ADD) {
+        res = bookmark_fill_tag_completion(store, suffix);
     }
 
     if (!res) {
index 1d98781..24d1863 100644 (file)
@@ -419,6 +419,8 @@ VbInputType vb_get_input_parts(const char* input, unsigned int use,
         {VB_INPUT_OPEN, ":open ", 6},
         {VB_INPUT_TABOPEN, ":tabopen ", 9},
         {VB_INPUT_SET, ":set ", 5},
+        {VB_INPUT_BOOKMARK_ADD, ":bma ", 5},
+        {VB_INPUT_BOOKMARK_ADD, ":bookmark-add ", 14},
         {VB_INPUT_COMMAND, ":", 1},
         {VB_INPUT_SEARCH_FORWARD, "/", 1},
         {VB_INPUT_SEARCH_BACKWARD, "?", 1},
index f856909..3cbdce8 100644 (file)
@@ -120,13 +120,14 @@ typedef enum _vb_mode {
 
 typedef enum {
     VB_INPUT_UNKNOWN,
-    VB_INPUT_SET              = 1<<0,
-    VB_INPUT_OPEN             = 1<<1,
-    VB_INPUT_TABOPEN          = 1<<2,
-    VB_INPUT_COMMAND          = 1<<3,
-    VB_INPUT_SEARCH_FORWARD   = 1<<4,
-    VB_INPUT_SEARCH_BACKWARD  = 1<<5,
-    VB_INPUT_ALL              = VB_INPUT_OPEN | VB_INPUT_TABOPEN | VB_INPUT_SET | VB_INPUT_COMMAND | VB_INPUT_SEARCH_FORWARD | VB_INPUT_SEARCH_BACKWARD,
+    VB_INPUT_SET              = 0x01,
+    VB_INPUT_OPEN             = 0x02,
+    VB_INPUT_TABOPEN          = 0x04,
+    VB_INPUT_COMMAND          = 0x08,
+    VB_INPUT_SEARCH_FORWARD   = 0x10,
+    VB_INPUT_SEARCH_BACKWARD  = 0x20,
+    VB_INPUT_BOOKMARK_ADD     = 0x40,
+    VB_INPUT_ALL              = 0xff, /* map to match all input types */
 } VbInputType;
 
 enum {