From 6ae1f47d8eab6229c96f4ff0ec2a51b83aa1ab75 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Fri, 6 Sep 2013 22:41:52 +0200 Subject: [PATCH] Added completion for bookmark tags. This allows to get already used bookmark tags for new bookmarks ':bma ' or ':bookmark-add '. --- doc/vimb.1 | 4 ++++ src/bookmark.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/bookmark.h | 1 + src/completion.c | 2 ++ src/main.c | 2 ++ src/main.h | 15 ++++++++------- 6 files changed, 63 insertions(+), 7 deletions(-) diff --git a/doc/vimb.1 b/doc/vimb.1 index 319d0a9..8a9f6c8 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -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 diff --git a/src/bookmark.c b/src/bookmark.c index 2fecaa7..3cbd281 100644 --- a/src/bookmark.c +++ b/src/bookmark.c @@ -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. diff --git a/src/bookmark.h b/src/bookmark.h index a346905..d7a8e56 100644 --- a/src/bookmark.h +++ b/src/bookmark.h @@ -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); diff --git a/src/completion.c b/src/completion.c index 663fb07..1f8aa14 100644 --- a/src/completion.c +++ b/src/completion.c @@ -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) { diff --git a/src/main.c b/src/main.c index 1d98781..24d1863 100644 --- a/src/main.c +++ b/src/main.c @@ -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}, diff --git a/src/main.h b/src/main.h index f856909..3cbdce8 100644 --- a/src/main.h +++ b/src/main.h @@ -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 { -- 2.20.1