To: vim_dev@googlegroups.com Subject: Patch 7.4.2011 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 7.4.2011 Problem: It is not easy to get a list of command arguments. Solution: Add getcompletion(). (Yegappan Lakshmanan) Files: runtime/doc/eval.txt, src/eval.c, src/ex_docmd.c, src/proto/ex_docmd.pro, src/testdir/test_cmdline.vim *** ../vim-7.4.2010/runtime/doc/eval.txt 2016-07-09 17:05:49.211222309 +0200 --- runtime/doc/eval.txt 2016-07-09 18:47:19.184624302 +0200 *************** *** 1989,1994 **** --- 2008,2014 ---- 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 *************** *** 3987,3992 **** --- 4045,4093 ---- 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: *** ../vim-7.4.2010/src/eval.c 2016-07-09 17:39:23.869279327 +0200 --- src/eval.c 2016-07-09 18:47:39.332324234 +0200 *************** *** 593,598 **** --- 593,601 ---- 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,8611 **** --- 8609,8617 ---- {"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,13088 **** --- 13089,13143 ---- #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 */ *** ../vim-7.4.2010/src/ex_docmd.c 2016-07-09 17:05:49.207222368 +0200 --- src/ex_docmd.c 2016-07-09 18:15:18.185188793 +0200 *************** *** 7049,7054 **** --- 7049,7066 ---- # 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 *** ../vim-7.4.2010/src/proto/ex_docmd.pro 2016-01-31 20:24:09.970066843 +0100 --- src/proto/ex_docmd.pro 2016-07-09 18:15:18.185188793 +0200 *************** *** 25,30 **** --- 25,31 ---- 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); *** ../vim-7.4.2010/src/testdir/test_cmdline.vim 2016-06-11 23:22:22.895097595 +0200 --- src/testdir/test_cmdline.vim 2016-07-09 18:47:52.876122524 +0200 *************** *** 24,26 **** --- 24,42 ---- 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 *** ../vim-7.4.2010/src/version.c 2016-07-09 17:55:24.902980302 +0200 --- src/version.c 2016-07-09 18:12:23.671787020 +0200 *************** *** 760,761 **** --- 760,763 ---- { /* Add new patch number below this line */ + /**/ + 2011, /**/ -- Hear about the guy who played a blank tape at full blast? The mime next door went nuts. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///