To: vim_dev@googlegroups.com Subject: Patch 8.2.4903 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.4903 Problem: Cannot get the current cmdline completion type and position. Solution: Add getcmdcompltype() and getcmdscreenpos(). (Shougo Matsushita, closes #10344) Files: runtime/doc/builtin.txt, runtime/doc/usr_41.txt, src/cmdexpand.c, src/proto/cmdexpand.pro, src/evalfunc.c, src/ex_getln.c, src/proto/ex_getln.pro, src/usercmd.c, src/proto/usercmd.pro, src/testdir/test_cmdline.vim *** ../vim-8.2.4902/runtime/doc/builtin.txt 2022-05-04 15:40:16.028317668 +0100 --- runtime/doc/builtin.txt 2022-05-07 12:33:28.250945445 +0100 *************** *** 213,220 **** --- 213,224 ---- getcharpos({expr}) List position of cursor, mark, etc. getcharsearch() Dict last character search getcharstr([expr]) String get one character from the user + getcmdcompltype() String return the type of the current + command-line completion getcmdline() String return the current command-line getcmdpos() Number return cursor position in command-line + getcmdscreenpos() Number return cursor screen position in + command-line getcmdtype() String return current command-line type getcmdwintype() String return current command-line window type getcompletion({pat}, {type} [, {filtered}]) *************** *** 3193,3198 **** --- 3212,3224 ---- Otherwise this works like |getchar()|, except that a number result is converted to a string. + getcmdcompltype() *getcmdcompltype()* + Return the type of the current command-line completion. + Only works when the command line is being edited, thus + requires use of |c_CTRL-\_e| or |c_CTRL-R_=|. + See |command-completion| for the return string. + Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|. + Returns an empty string when completion is not defined. getcmdline() *getcmdline()* Return the current command-line. Only works when the command *************** *** 3212,3217 **** --- 3238,3252 ---- Returns 0 otherwise. Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|. + getcmdscreenpos() *getcmdscreenpos()* + Return the screen position of the cursor in the command line + as a byte count. The first column is 1. + Instead of |getcmdpos()|, it adds the prompt position. + Only works when editing the command line, thus requires use of + |c_CTRL-\_e| or |c_CTRL-R_=| or an expression mapping. + Returns 0 otherwise. + Also see |getcmdpos()|, |setcmdpos()|. + getcmdtype() *getcmdtype()* Return the current command-line type. Possible return values are: *** ../vim-8.2.4902/runtime/doc/usr_41.txt 2022-04-28 15:26:29.214947839 +0100 --- runtime/doc/usr_41.txt 2022-05-07 12:33:28.250945445 +0100 *************** *** 885,892 **** --- 976,987 ---- swapname() get the swap file path of a buffer Command line: *command-line-functions* + getcmdcompltype() get the type of the current command line + completion getcmdline() get the current command line getcmdpos() get position of the cursor in the command line + getcmdscreenpos() get screen position of the cursor in the + command line setcmdpos() set position of the cursor in the command line getcmdtype() return the current command-line type getcmdwintype() return the current command-line window type *** ../vim-8.2.4902/src/cmdexpand.c 2022-04-11 19:38:15.915471119 +0100 --- src/cmdexpand.c 2022-05-07 12:33:28.254945438 +0100 *************** *** 15,21 **** static int cmd_showtail; // Only show path tail in lists ? - static void set_expand_context(expand_T *xp); static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches, char_u *((*func)(expand_T *, int)), int escaped); --- 15,20 ---- *************** *** 1230,1236 **** * EXPAND_ENV_VARS Complete environment variable names * EXPAND_USER Complete user names */ ! static void set_expand_context(expand_T *xp) { cmdline_info_T *ccline = get_cmdline_info(); --- 1229,1235 ---- * EXPAND_ENV_VARS Complete environment variable names * EXPAND_USER Complete user names */ ! void set_expand_context(expand_T *xp) { cmdline_info_T *ccline = get_cmdline_info(); *** ../vim-8.2.4902/src/proto/cmdexpand.pro 2022-02-24 13:28:36.570222354 +0000 --- src/proto/cmdexpand.pro 2022-05-07 12:33:28.254945438 +0100 *************** *** 13,18 **** --- 13,19 ---- char_u *sm_gettail(char_u *s); char_u *addstar(char_u *fname, int len, int context); void set_cmd_context(expand_T *xp, char_u *str, int len, int col, int use_ccline); + void set_expand_context(expand_T *xp); int expand_cmdline(expand_T *xp, char_u *str, int col, int *matchcount, char_u ***matches); void globpath(char_u *path, char_u *file, garray_T *ga, int expand_options); int wildmenu_translate_key(cmdline_info_T *cclp, int key, expand_T *xp, int did_wild_list); *** ../vim-8.2.4902/src/evalfunc.c 2022-05-05 13:52:59.412192108 +0100 --- src/evalfunc.c 2022-05-07 12:33:28.254945438 +0100 *************** *** 1850,1859 **** --- 1850,1863 ---- ret_dict_any, f_getcharsearch}, {"getcharstr", 0, 1, 0, arg1_bool, ret_string, f_getcharstr}, + {"getcmdcompltype", 0, 0, 0, NULL, + ret_string, f_getcmdcompltype}, {"getcmdline", 0, 0, 0, NULL, ret_string, f_getcmdline}, {"getcmdpos", 0, 0, 0, NULL, ret_number, f_getcmdpos}, + {"getcmdscreenpos", 0, 0, 0, NULL, + ret_number, f_getcmdscreenpos}, {"getcmdtype", 0, 0, 0, NULL, ret_string, f_getcmdtype}, {"getcmdwintype", 0, 0, 0, NULL, *** ../vim-8.2.4902/src/ex_getln.c 2022-05-07 10:49:07.020917189 +0100 --- src/ex_getln.c 2022-05-07 12:46:11.706290543 +0100 *************** *** 4119,4124 **** --- 4119,4160 ---- } /* + * Get the current command-line completion type. + */ + static char_u * + get_cmdline_completion(void) + { + cmdline_info_T *p; + + if (cmdline_star > 0) + return NULL; + + p = get_ccline_ptr(); + if (p && p->xpc != NULL) + { + char_u *cmd_compl; + + set_expand_context(p->xpc); + + cmd_compl = cmdcomplete_type_to_str(p->xpc->xp_context); + if (cmd_compl != NULL) + return vim_strnsave(cmd_compl, strlen((char *)cmd_compl)); + } + + return NULL; + } + + /* + * "getcmdcompltype()" function + */ + void + f_getcmdcompltype(typval_T *argvars UNUSED, typval_T *rettv) + { + rettv->v_type = VAR_STRING; + rettv->vval.v_string = get_cmdline_completion(); + } + + /* * "getcmdline()" function */ void *************** *** 4142,4147 **** --- 4178,4205 ---- } /* + * Get the command line cursor screen position. + */ + static int + get_cmdline_screen_pos(void) + { + cmdline_info_T *p = get_ccline_ptr(); + + if (p == NULL) + return -1; + return p->cmdspos; + } + + /* + * "getcmdscreenpos()" function + */ + void + f_getcmdscreenpos(typval_T *argvars UNUSED, typval_T *rettv) + { + rettv->vval.v_number = get_cmdline_screen_pos() + 1; + } + + /* * Set the command line byte position to "pos". Zero is the first position. * Only works when the command line is being edited. * Returns 1 when failed, 0 when OK. *** ../vim-8.2.4902/src/proto/ex_getln.pro 2022-01-08 18:43:36.877446896 +0000 --- src/proto/ex_getln.pro 2022-05-07 12:33:28.254945438 +0100 *************** *** 30,38 **** --- 30,40 ---- void escape_fname(char_u **pp); void tilde_replace(char_u *orig_pat, int num_files, char_u **files); cmdline_info_T *get_cmdline_info(void); + void f_getcmdcompltype(typval_T *argvars, typval_T *rettv); void f_getcmdline(typval_T *argvars, typval_T *rettv); void f_getcmdpos(typval_T *argvars, typval_T *rettv); void f_setcmdpos(typval_T *argvars, typval_T *rettv); + void f_getcmdscreenpos(typval_T *argvars, typval_T *rettv); void f_getcmdtype(typval_T *argvars, typval_T *rettv); int get_cmdline_firstc(void); int get_list_range(char_u **str, int *num1, int *num2); *** ../vim-8.2.4902/src/usercmd.c 2022-03-24 11:22:07.215294108 +0000 --- src/usercmd.c 2022-05-07 12:41:40.370497915 +0100 *************** *** 443,448 **** --- 443,467 ---- } #ifdef FEAT_EVAL + /* + * Get the name of completion type "expand" as a string. + */ + char_u * + cmdcomplete_type_to_str(int expand) + { + int i; + + for (i = 0; command_complete[i].expand != 0; i++) + if (command_complete[i].expand == expand) + return (char_u *)command_complete[i].name; + + return NULL; + } + + /* + * Get the index of completion type "complete_str". + * Returns EXPAND_NOTHING if no match found. + */ int cmdcomplete_str_to_type(char_u *complete_str) { *** ../vim-8.2.4902/src/proto/usercmd.pro 2022-02-18 13:56:34.630118479 +0000 --- src/proto/usercmd.pro 2022-05-07 12:41:43.726495229 +0100 *************** *** 1,7 **** /* usercmd.c */ char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *complp); - char_u *set_context_in_user_cmdarg(char_u *cmd, char_u *arg, long argt, int context, expand_T *xp, int forceit); char_u *set_context_in_user_cmd(expand_T *xp, char_u *arg_in); char_u *expand_user_command_name(int idx); char_u *get_user_commands(expand_T *xp, int idx); char_u *get_user_command_name(int idx, int cmdidx); --- 1,7 ---- /* usercmd.c */ char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *complp); char_u *set_context_in_user_cmd(expand_T *xp, char_u *arg_in); + char_u *set_context_in_user_cmdarg(char_u *cmd, char_u *arg, long argt, int context, expand_T *xp, int forceit); char_u *expand_user_command_name(int idx); char_u *get_user_commands(expand_T *xp, int idx); char_u *get_user_command_name(int idx, int cmdidx); *************** *** 9,14 **** --- 9,15 ---- char_u *get_user_cmd_flags(expand_T *xp, int idx); char_u *get_user_cmd_nargs(expand_T *xp, int idx); char_u *get_user_cmd_complete(expand_T *xp, int idx); + char_u *cmdcomplete_type_to_str(int expand); int cmdcomplete_str_to_type(char_u *complete_str); char *uc_fun_cmd(void); int parse_compl_arg(char_u *value, int vallen, int *complp, long *argt, char_u **compl_arg); *** ../vim-8.2.4902/src/testdir/test_cmdline.vim 2022-05-07 10:49:07.024917180 +0100 --- src/testdir/test_cmdline.vim 2022-05-07 12:33:28.254945438 +0100 *************** *** 3380,3383 **** --- 3380,3395 ---- endfor endfunc + func Check_completion() + call assert_equal('let a', getcmdline()) + call assert_equal(6, getcmdpos()) + call assert_equal(7, getcmdscreenpos()) + call assert_equal('var', getcmdcompltype()) + return '' + endfunc + + func Test_screenpos_and_completion() + call feedkeys(":let a\=Check_completion()\\", "xt") + endfunc + " vim: shiftwidth=2 sts=2 expandtab *** ../vim-8.2.4902/src/version.c 2022-05-07 12:24:57.943638388 +0100 --- src/version.c 2022-05-07 12:36:20.850773441 +0100 *************** *** 748,749 **** --- 748,751 ---- { /* Add new patch number below this line */ + /**/ + 4903, /**/ -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///