]> granicus.if.org Git - vim/commitdiff
patch 7.4.2011 v7.4.2011
authorBram Moolenaar <Bram@vim.org>
Sat, 9 Jul 2016 16:50:29 +0000 (18:50 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 9 Jul 2016 16:50:29 +0000 (18:50 +0200)
Problem:    It is not easy to get a list of command arguments.
Solution:   Add getcompletion(). (Yegappan Lakshmanan)

runtime/doc/eval.txt
src/eval.c
src/ex_docmd.c
src/proto/ex_docmd.pro
src/testdir/test_cmdline.vim
src/version.c

index 7f948eee1ef5471482193c0b7ce1d4d6df539f4e..438e28eec6aaf1507f46b7f5bde3ae653e6ad084 100644 (file)
@@ -2008,6 +2008,7 @@ getcmdline()                      String  return the current command-line
 getcmdpos()                    Number  return cursor position in command-line
 getcmdtype()                   String  return current command-line type
 getcmdwintype()                        String  return current command-line window type
+getcompletion({pat}, {type})   List    list of cmdline completion matches
 getcurpos()                    List    position of the cursor
 getcwd([{winnr} [, {tabnr}]])  String  get the current working directory
 getfontname([{name}])          String  name of font being used
@@ -4044,6 +4045,49 @@ getcmdwintype()                                          *getcmdwintype()*
                values are the same as |getcmdtype()|. Returns an empty string
                when not in the command-line window.
 
+getcompletion({pat}, {type})                   *getcompletion()*
+               Return a list of command-line completion matches. {type}
+               specifies what for.  The following completion types are
+               supported:
+
+               augroup         autocmd groups
+               buffer          buffer names
+               behave          :behave suboptions
+               color           color schemes
+               command         Ex command (and arguments)
+               compiler        compilers
+               cscope          |:cscope| suboptions
+               dir             directory names
+               environment     environment variable names
+               event           autocommand events
+               expression      Vim expression
+               file            file and directory names
+               file_in_path    file and directory names in |'path'|
+               filetype        filetype names |'filetype'|
+               function        function name
+               help            help subjects
+               highlight       highlight groups
+               history         :history suboptions
+               locale          locale names (as output of locale -a)
+               mapping         mapping name
+               menu            menus
+               option          options
+               shellcmd        Shell command
+               sign            |:sign| suboptions
+               syntax          syntax file names |'syntax'|
+               syntime         |:syntime| suboptions
+               tag             tags
+               tag_listfiles   tags, file names
+               user            user names
+               var             user variables
+
+               If {pat} is an empty string, then all the matches are returned.
+               Otherwise only items matching {pat} are returned. See
+               |wildcards| for the use of special characters in {pat}.
+
+               If there are no matches, an empty list is returned.  An
+               invalid value for {type} produces an error.
+
                                                        *getcurpos()*
 getcurpos()    Get the position of the cursor.  This is like getpos('.'), but
                includes an extra item in the list:
index 3ab8ea4956b0c19f3fb6376e961da2f68543b414..6e15a55df6364d7b7555cbe4bd8132fd7e7c30d1 100644 (file)
@@ -593,6 +593,9 @@ static void f_getchar(typval_T *argvars, typval_T *rettv);
 static void f_getcharmod(typval_T *argvars, typval_T *rettv);
 static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
 static void f_getcmdline(typval_T *argvars, typval_T *rettv);
+#if defined(FEAT_CMDL_COMPL)
+static void f_getcompletion(typval_T *argvars, typval_T *rettv);
+#endif
 static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
 static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
 static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
@@ -8606,6 +8609,9 @@ static struct fst
     {"getcmdpos",      0, 0, f_getcmdpos},
     {"getcmdtype",     0, 0, f_getcmdtype},
     {"getcmdwintype",  0, 0, f_getcmdwintype},
+#if defined(FEAT_CMDL_COMPL)
+    {"getcompletion",  2, 2, f_getcompletion},
+#endif
     {"getcurpos",      0, 0, f_getcurpos},
     {"getcwd",         0, 2, f_getcwd},
     {"getfontname",    0, 1, f_getfontname},
@@ -13083,6 +13089,55 @@ f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
 #endif
 }
 
+#if defined(FEAT_CMDL_COMPL)
+/*
+ * "getcompletion()" function
+ */
+    static void
+f_getcompletion(typval_T *argvars, typval_T *rettv)
+{
+    char_u     *pat;
+    expand_T   xpc;
+    int                options = WILD_KEEP_ALL | WILD_SILENT | WILD_USE_NL
+                                         | WILD_LIST_NOTFOUND | WILD_NO_BEEP;
+
+    if (p_wic)
+       options |= WILD_ICASE;
+
+    ExpandInit(&xpc);
+    xpc.xp_pattern = get_tv_string(&argvars[0]);
+    xpc.xp_pattern_len = STRLEN(xpc.xp_pattern);
+    xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
+    if (xpc.xp_context == EXPAND_NOTHING)
+    {
+       if (argvars[1].v_type == VAR_STRING)
+           EMSG2(_(e_invarg2), argvars[1].vval.v_string);
+       else
+           EMSG(_(e_invarg));
+       return;
+    }
+
+    if (xpc.xp_context == EXPAND_MENUS)
+    {
+       set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
+       xpc.xp_pattern_len = STRLEN(xpc.xp_pattern);
+    }
+
+    pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
+    if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
+    {
+       int i;
+
+       ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
+
+       for (i = 0; i < xpc.xp_numfiles; i++)
+           list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
+    }
+    vim_free(pat);
+    ExpandCleanup(&xpc);
+}
+#endif
+
 /*
  * "getcwd()" function
  */
index f8b50ed1d1ebfef92eb80bbff28380a3e1398e3e..07fdaa94a08183748735dd8d150752b61affe25d 100644 (file)
@@ -7049,6 +7049,18 @@ parse_compl_arg(
 # endif
     return OK;
 }
+
+    int
+cmdcomplete_str_to_type(char_u *complete_str)
+{
+    int i;
+
+    for (i = 0; command_complete[i].expand != 0; ++i)
+       if (STRCMP(complete_str, command_complete[i].name) == 0)
+           return command_complete[i].expand;
+
+    return EXPAND_NOTHING;
+}
 #endif
 
     static void
index 6ff1588e9eef8ddbaf7f0ac0b18e49bafe3be289..b92ce80d135aa3c8cfdf0f5344ac0ead7033c11c 100644 (file)
@@ -25,6 +25,7 @@ char_u *get_user_cmd_nargs(expand_T *xp, int idx);
 char_u *get_user_cmd_complete(expand_T *xp, int idx);
 int parse_addr_type_arg(char_u *value, int vallen, long *argt, int *addr_type_arg);
 int parse_compl_arg(char_u *value, int vallen, int *complp, long *argt, char_u **compl_arg);
+int cmdcomplete_str_to_type(char_u *complete_str);
 void not_exiting(void);
 void tabpage_close(int forceit);
 void tabpage_close_other(tabpage_T *tp, int forceit);
index 69dc9cdc05508932e4877886f203c3fda4fabed7..30d366b1e6eaf132b8d673fb1de60e4242ace1f0 100644 (file)
@@ -24,3 +24,19 @@ func Test_complete_wildmenu()
   call delete('Xtestfile2')
   set nowildmenu
 endfunc
+
+func Test_getcompletion()
+  let groupcount = len(getcompletion('', 'event'))
+  call assert_true(groupcount > 0)
+  let matchcount = len(getcompletion('File', 'event'))
+  call assert_true(matchcount > 0)
+  call assert_true(groupcount > matchcount)
+
+  source $VIMRUNTIME/menu.vim
+  let matchcount = len(getcompletion('', 'menu'))
+  call assert_true(matchcount > 0)
+  let matchcount = len(getcompletion('ToolBar.', 'menu'))
+  call assert_true(matchcount > 0)
+
+  call assert_fails('call getcompletion("", "burp")', 'E475:')
+endfunc
index b0969bdf7e38abc7ffddc61f106513d7ca9270e2..33cfc57523009b883e84ef4da39c2a2ddcbc9887 100644 (file)
@@ -758,6 +758,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2011,
 /**/
     2010,
 /**/