From: Daniel Carl Date: Mon, 8 Sep 2014 20:35:45 +0000 (+0200) Subject: Added completion for :autocmd and :augroup (#100). X-Git-Url: https://git.owens.tech/wrapped.html/wrapped.html/git?a=commitdiff_plain;h=ef4486dcb37b3777b43150675e87e89daea11c50;p=vimb.git Added completion for :autocmd and :augroup (#100). --- diff --git a/src/autocmd.c b/src/autocmd.c index 9ef8827..ea030fb 100644 --- a/src/autocmd.c +++ b/src/autocmd.c @@ -23,6 +23,7 @@ #include "ascii.h" #include "ex.h" #include "util.h" +#include "completion.h" typedef struct { guint bits; /* the bits identify the events the command applies to */ @@ -286,6 +287,59 @@ gboolean autocmd_run(AuEvent event, const char *uri, const char *group) return true; } +gboolean autocmd_fill_group_completion(GtkListStore *store, const char *input) +{ + GSList *lg; + gboolean found = false; + GtkTreeIter iter; + + if (!input || !*input) { + for (lg = groups; lg; lg = lg->next) { + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, ((AuGroup*)lg->data)->name, -1); + found = true; + } + } else { + for (lg = groups; lg; lg = lg->next) { + char *value = ((AuGroup*)lg->data)->name; + if (g_str_has_prefix(value, input)) { + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, value, -1); + found = true; + } + } + } + + return found; +} + +gboolean autocmd_fill_event_completion(GtkListStore *store, const char *input) +{ + int i; + const char *value; + gboolean found = false; + GtkTreeIter iter; + + if (!input || !*input) { + for (i = 0; i < LENGTH(events); i++) { + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, events[i].name, -1); + found = true; + } + } else { + for (i = 0; i < LENGTH(events); i++) { + value = events[i].name; + if (g_str_has_prefix(value, input)) { + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, value, -1); + found = true; + } + } + } + + return found; +} + /** * Get the augroup by it's name. */ diff --git a/src/autocmd.h b/src/autocmd.h index 0d437f0..fe3bc04 100644 --- a/src/autocmd.h +++ b/src/autocmd.h @@ -43,6 +43,8 @@ void autocmd_cleanup(void); gboolean autocmd_augroup(char *name, gboolean delete); gboolean autocmd_add(char *name, gboolean delete); gboolean autocmd_run(AuEvent event, const char *uri, const char *group); +gboolean autocmd_fill_group_completion(GtkListStore *store, const char *input); +gboolean autocmd_fill_event_completion(GtkListStore *store, const char *input); #endif /* end of include guard: _AUTOCMD_H */ #endif diff --git a/src/ex.c b/src/ex.c index 6647bf1..7d14324 100644 --- a/src/ex.c +++ b/src/ex.c @@ -1028,29 +1028,32 @@ static gboolean complete(short direction) skip_whitespace(&in); parse_count(&in, arg); - /* packup the current pointer so that we can restore the input pointer - * if tha command name parsing fails */ + /* Backup the current pointer so that we can restore the input pointer + * if tha command name parsing fails. */ before_cmdname = in; - if (parse_command_name(&in, arg) && VB_IS_SPACE(*in)) { + /* Do ex command specific completion if the comman is recognized and + * there is a space after the command and the optional '!' bang. */ + if (parse_command_name(&in, arg) && parse_bang(&in, arg) && VB_IS_SPACE(*in)) { const char *token; - /* get only the last word of input string for the completion for - * bookmark tag completion */ + + /* Get only the last word of input string for the completion for + * bookmark tag completion. */ if (arg->code == EX_BMA) { - /* find the end of the input and search for the next - * whitespace toward the beginning */ + /* Find the end of the input and search for the next + * whitespace toward the beginning. */ token = strrchr(in, '\0'); while (token >= in && !VB_IS_SPACE(*token)) { token--; } } else { - /* use all input except ot the command itself for the - * completion */ + /* Use all input except of the command and it's possible bang + * itself for the completion. */ token = in; } - /* save the string prefix that will not be part of completion like - * the ':open ' if ':open something' is completed. this means that + /* Save the string prefix that will not be part of completion like + * the ':open ' if ':open something' is completed. This means that * the completion will only the none prefix part of the input */ OVERWRITE_NSTRING(excomp.prefix, input, token - input + 1); @@ -1067,7 +1070,7 @@ static gboolean complete(short direction) break; case EX_SET: - sort = true; + sort = true; found = setting_fill_completion(store, token); break; @@ -1086,6 +1089,18 @@ static gboolean complete(short direction) found = handler_fill_completion(store, token); break; +#ifdef FEATURE_AUTOCMD + case EX_AUTOCMD: + sort = true; + found = autocmd_fill_event_completion(store, token); + break; + + case EX_AUGROUP: + sort = true; + found = autocmd_fill_group_completion(store, token); + break; +#endif + default: break; } @@ -1093,8 +1108,8 @@ static gboolean complete(short direction) /* restore the 'in' pointer after try to parse command name */ in = before_cmdname; - /* backup the parsed data so we can access them in - * completion_select function */ + /* Backup the parsed data so we can access them in + * completion_select function. */ excomp.count = arg->count; if (ex_fill_completion(store, in)) {