From 6b461d4259c611182166585a9473081b42d2477b Mon Sep 17 00:00:00 2001 From: h-east Date: Fri, 4 Jul 2025 06:22:02 +0900 Subject: [PATCH 1/2] Revert "patch 9.1.1505: not possible to return completion type for :ex command" This reverts commit 5d2354fc07b642f8835bef27e6dd46ee705726b5. --- runtime/doc/builtin.txt | 15 +++++---- runtime/doc/usr_41.txt | 5 +-- runtime/doc/version9.txt | 4 +-- src/evalfunc.c | 2 +- src/ex_getln.c | 59 +++++++++++------------------------- src/testdir/test_cmdline.vim | 10 ------ 6 files changed, 29 insertions(+), 66 deletions(-) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 8f9c7baa21..00730a7c3e 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -1,4 +1,4 @@ -*builtin.txt* For Vim version 9.1. Last change: 2025 Jul 03 +*builtin.txt* For Vim version 9.1. Last change: 2025 Jun 28 VIM REFERENCE MANUAL by Bram Moolenaar @@ -238,8 +238,8 @@ getcharsearch() Dict last character search getcharstr([{expr} [, {opts}]]) String get one character from the user getcmdcomplpat() String return the completion pattern of the current command-line completion -getcmdcompltype({pat}) String return the type of command-line - completion +getcmdcompltype() String return the type of the current + command-line completion getcmdline() String return the current command-line input getcmdpos() Number return cursor position in command-line getcmdprompt() String return the current command-line prompt @@ -4201,11 +4201,10 @@ getcmdcomplpat() *getcmdcomplpat()* Return type: |String| -getcmdcompltype([{pat}]) *getcmdcompltype()* - Return the type of command-line completion using {pat}. - If {pat} is omited, only works when the command line is being - edited, thus requires use of |c_CTRL-\_e| or |c_CTRL-R_=|. - +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()|, |getcmdline()|, |getcmdprompt()|, |getcmdcomplpat()| and |setcmdline()|. diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index 9b8cdc2bea..ffa6fcb7ae 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -1,4 +1,4 @@ -*usr_41.txt* For Vim version 9.1. Last change: 2025 Jul 03 +*usr_41.txt* For Vim version 9.1. Last change: 2025 Apr 27 VIM USER MANUAL - by Bram Moolenaar @@ -1100,7 +1100,8 @@ Buffers, windows and the argument list: Command line: *command-line-functions* getcmdcomplpat() get completion pattern of the current command line - getcmdcompltype() get the type of the command line completion + getcmdcompltype() get the type of the current command line + completion getcmdline() get the current command line input getcmdprompt() get the current command line prompt getcmdpos() get position of the cursor in the command line diff --git a/runtime/doc/version9.txt b/runtime/doc/version9.txt index 75d14011ab..97b9679e26 100644 --- a/runtime/doc/version9.txt +++ b/runtime/doc/version9.txt @@ -1,4 +1,4 @@ -*version9.txt* For Vim version 9.1. Last change: 2025 Jul 03 +*version9.txt* For Vim version 9.1. Last change: 2025 Jun 28 VIM REFERENCE MANUAL by Bram Moolenaar @@ -41607,8 +41607,6 @@ Completion: ~ - allow to complete directories from 'cdpath' for |:cd| and similar commands, add the "cd_in_path" completion type for e.g. |:command-complete| and |getcompletion()| -- |getcompletion()| now accepts a pat and returns the completion type for the - {pat} argument - allow to complete shell commands and files using the new shellcmdline completion type using |:command-complete| and |getcmdcomplpat()| - allow to specify additional attributes in the completion menu (allows to diff --git a/src/evalfunc.c b/src/evalfunc.c index 6ba698d566..a31ad1341f 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -2264,7 +2264,7 @@ static funcentry_T global_functions[] = ret_string, f_getcharstr}, {"getcmdcomplpat", 0, 0, 0, NULL, ret_string, f_getcmdcomplpat}, - {"getcmdcompltype", 0, 1, FEARG_1, NULL, + {"getcmdcompltype", 0, 0, 0, NULL, ret_string, f_getcmdcompltype}, {"getcmdline", 0, 0, 0, NULL, ret_string, f_getcmdline}, diff --git a/src/ex_getln.c b/src/ex_getln.c index 044f3894c9..324f492db1 100644 --- a/src/ex_getln.c +++ b/src/ex_getln.c @@ -4306,16 +4306,25 @@ get_cmdline_completion_pattern(void) * Get the current command-line completion type. */ static char_u * -get_cmdline_completion(expand_T *xpc) +get_cmdline_completion(void) { + cmdline_info_T *p; + char_u *buffer; int xp_context; - xp_context = xpc->xp_context; + if (cmdline_star > 0) + return NULL; + + p = get_ccline_ptr(); + if (p == NULL || p->xpc == NULL) + return NULL; + + xp_context = p->xpc->xp_context; if (xp_context == EXPAND_NOTHING) { - set_expand_context(xpc); - xp_context = xpc->xp_context; - xpc->xp_context = EXPAND_NOTHING; + set_expand_context(p->xpc); + xp_context = p->xpc->xp_context; + p->xpc->xp_context = EXPAND_NOTHING; } if (xp_context == EXPAND_UNSUCCESSFUL) return NULL; @@ -4326,12 +4335,10 @@ get_cmdline_completion(expand_T *xpc) if (xp_context == EXPAND_USER_LIST || xp_context == EXPAND_USER_DEFINED) { - char_u *buffer; - - buffer = alloc(STRLEN(cmd_compl) + STRLEN(xpc->xp_arg) + 2); + buffer = alloc(STRLEN(cmd_compl) + STRLEN(p->xpc->xp_arg) + 2); if (buffer == NULL) return NULL; - sprintf((char *)buffer, "%s,%s", cmd_compl, xpc->xp_arg); + sprintf((char *)buffer, "%s,%s", cmd_compl, p->xpc->xp_arg); return buffer; } @@ -4354,40 +4361,8 @@ f_getcmdcomplpat(typval_T *argvars UNUSED, typval_T *rettv) void f_getcmdcompltype(typval_T *argvars UNUSED, typval_T *rettv) { - if (check_for_opt_string_arg(argvars, 0) == FAIL) - return; - rettv->v_type = VAR_STRING; - - if (argvars[0].v_type != VAR_UNKNOWN) - { - char_u *pat; - expand_T xpc; - int cmdline_len; - - pat = tv_get_string(&argvars[0]); - ExpandInit(&xpc); - - cmdline_len = (int)STRLEN(pat); - set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE); - xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); - xpc.xp_col = cmdline_len; - - rettv->v_type = VAR_STRING; - rettv->vval.v_string = get_cmdline_completion(&xpc); - - ExpandCleanup(&xpc); - } - else - { - cmdline_info_T *p; - - p = get_ccline_ptr(); - if (cmdline_star > 0 || p == NULL || p->xpc == NULL) - return; - - rettv->vval.v_string = get_cmdline_completion(p->xpc); - } + rettv->vval.v_string = get_cmdline_completion(); } /* diff --git a/src/testdir/test_cmdline.vim b/src/testdir/test_cmdline.vim index 9ebeb0f44a..59c25db75f 100644 --- a/src/testdir/test_cmdline.vim +++ b/src/testdir/test_cmdline.vim @@ -4589,14 +4589,4 @@ func Test_range_complete() set wildcharm=0 endfunc -func Test_getcmdcompltype_with_pat() - call assert_fails('call getcmdcompltype({})', 'E1174:') - call assert_equal(getcmdcompltype(''), 'command') - call assert_equal(getcmdcompltype('dummy '), '') - call assert_equal(getcmdcompltype('cd '), 'dir_in_path') - call assert_equal(getcmdcompltype('let v:n'), 'var') - call assert_equal(getcmdcompltype('call tag'), 'function') - call assert_equal(getcmdcompltype('help '), 'help') -endfunc - " vim: shiftwidth=2 sts=2 expandtab From ec8c74e8480d77f655ca1136ff530395d457a37a Mon Sep 17 00:00:00 2001 From: h-east Date: Fri, 4 Jul 2025 07:42:32 +0900 Subject: [PATCH 2/2] Revert 9.1.1505 and instead adding the getcompletiontype() function Co-authored-by: Shougo Matsushita Signed-off-by: Shougo Matsushita --- runtime/doc/builtin.txt | 16 ++++++++++++++- runtime/doc/tags | 1 + runtime/doc/usr_41.txt | 4 +++- src/cmdexpand.c | 29 +++++++++++++++++++++++++++ src/evalfunc.c | 2 ++ src/ex_getln.c | 39 ++++++++++++++++++------------------ src/proto/cmdexpand.pro | 1 + src/proto/ex_getln.pro | 1 + src/testdir/test_cmdline.vim | 11 ++++++++++ 9 files changed, 83 insertions(+), 21 deletions(-) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 00730a7c3e..d14de01e8d 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -1,4 +1,4 @@ -*builtin.txt* For Vim version 9.1. Last change: 2025 Jun 28 +*builtin.txt* For Vim version 9.1. Last change: 2025 Jul 04 VIM REFERENCE MANUAL by Bram Moolenaar @@ -249,6 +249,8 @@ getcmdtype() String return current command-line type getcmdwintype() String return current command-line window type getcompletion({pat}, {type} [, {filtered}]) List list of cmdline completion matches +getcompletiontype({pat}) String return the type of the command-line + completion using {pat} getcurpos([{winnr}]) List position of the cursor getcursorcharpos([{winnr}]) List character position of the cursor getcwd([{winnr} [, {tabnr}]]) String get the current working directory @@ -4210,6 +4212,9 @@ getcmdcompltype() *getcmdcompltype()* |getcmdprompt()|, |getcmdcomplpat()| and |setcmdline()|. Returns an empty string when completion is not defined. + To get the type of the command-line completion for the + specified string, use |getcompletiontype()|. + Return type: |String| @@ -4368,6 +4373,15 @@ getcompletion({pat}, {type} [, {filtered}]) *getcompletion()* < Return type: list +getcompletiontype({pat}) *getcompletiontype()* + Return the type of the command-line completion using {pat}. + When no corresponding completion type is found, an empty + string is returned. + To get the current command-line completion type, use + |getcmdcompltype()|. + + Return type: |String| + *getcurpos()* getcurpos([{winid}]) Get the position of the cursor. This is like getpos('.'), but diff --git a/runtime/doc/tags b/runtime/doc/tags index 6b363dbaa9..3f83210b49 100644 --- a/runtime/doc/tags +++ b/runtime/doc/tags @@ -7976,6 +7976,7 @@ getcmdscreenpos() builtin.txt /*getcmdscreenpos()* getcmdtype() builtin.txt /*getcmdtype()* getcmdwintype() builtin.txt /*getcmdwintype()* getcompletion() builtin.txt /*getcompletion()* +getcompletiontype() builtin.txt /*getcompletiontype()* getcurpos() builtin.txt /*getcurpos()* getcursorcharpos() builtin.txt /*getcursorcharpos()* getcwd() builtin.txt /*getcwd()* diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index ffa6fcb7ae..8dc081b37c 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -1,4 +1,4 @@ -*usr_41.txt* For Vim version 9.1. Last change: 2025 Apr 27 +*usr_41.txt* For Vim version 9.1. Last change: 2025 Jul 04 VIM USER MANUAL - by Bram Moolenaar @@ -1112,6 +1112,8 @@ Command line: *command-line-functions* getcmdtype() return the current command-line type getcmdwintype() return the current command-line window type getcompletion() list of command-line completion matches + getcompletiontype() get the type of the command-line completion + for specified string fullcommand() get full command name cmdcomplete_info() get command-line completion information diff --git a/src/cmdexpand.c b/src/cmdexpand.c index f7a5f5cc63..b1c9ed67ab 100644 --- a/src/cmdexpand.c +++ b/src/cmdexpand.c @@ -4556,6 +4556,35 @@ f_getcompletion(typval_T *argvars, typval_T *rettv) ExpandCleanup(&xpc); } +/* + * "getcompletiontype()" function + */ + void +f_getcompletiontype(typval_T *argvars, typval_T *rettv) +{ + char_u *pat; + expand_T xpc; + int cmdline_len; + + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + + if (check_for_string_arg(argvars, 0) == FAIL) + return; + + pat = tv_get_string(&argvars[0]); + ExpandInit(&xpc); + + cmdline_len = (int)STRLEN(pat); + set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE); + xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); + xpc.xp_col = cmdline_len; + + rettv->vval.v_string = get_cmdline_completion(&xpc); + + ExpandCleanup(&xpc); +} + /* * "cmdcomplete_info()" function */ diff --git a/src/evalfunc.c b/src/evalfunc.c index a31ad1341f..ec21087e25 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -2280,6 +2280,8 @@ static funcentry_T global_functions[] = ret_string, f_getcmdwintype}, {"getcompletion", 2, 3, FEARG_1, arg3_string_string_bool, ret_list_string, f_getcompletion}, + {"getcompletiontype", 1, 1, FEARG_1, arg1_string, + ret_string, f_getcompletiontype}, {"getcurpos", 0, 1, FEARG_1, arg1_number, ret_list_number, f_getcurpos}, {"getcursorcharpos", 0, 1, FEARG_1, arg1_number, diff --git a/src/ex_getln.c b/src/ex_getln.c index 324f492db1..f2a51ea2c8 100644 --- a/src/ex_getln.c +++ b/src/ex_getln.c @@ -4303,28 +4303,19 @@ get_cmdline_completion_pattern(void) } /* - * Get the current command-line completion type. + * Get the command-line completion type. */ - static char_u * -get_cmdline_completion(void) + char_u * +get_cmdline_completion(expand_T *xpc) { - cmdline_info_T *p; - char_u *buffer; int xp_context; - if (cmdline_star > 0) - return NULL; - - p = get_ccline_ptr(); - if (p == NULL || p->xpc == NULL) - return NULL; - - xp_context = p->xpc->xp_context; + xp_context = xpc->xp_context; if (xp_context == EXPAND_NOTHING) { - set_expand_context(p->xpc); - xp_context = p->xpc->xp_context; - p->xpc->xp_context = EXPAND_NOTHING; + set_expand_context(xpc); + xp_context = xpc->xp_context; + xpc->xp_context = EXPAND_NOTHING; } if (xp_context == EXPAND_UNSUCCESSFUL) return NULL; @@ -4335,10 +4326,12 @@ get_cmdline_completion(void) if (xp_context == EXPAND_USER_LIST || xp_context == EXPAND_USER_DEFINED) { - buffer = alloc(STRLEN(cmd_compl) + STRLEN(p->xpc->xp_arg) + 2); + char_u *buffer; + + buffer = alloc(STRLEN(cmd_compl) + STRLEN(xpc->xp_arg) + 2); if (buffer == NULL) return NULL; - sprintf((char *)buffer, "%s,%s", cmd_compl, p->xpc->xp_arg); + sprintf((char *)buffer, "%s,%s", cmd_compl, xpc->xp_arg); return buffer; } @@ -4361,8 +4354,16 @@ f_getcmdcomplpat(typval_T *argvars UNUSED, typval_T *rettv) void f_getcmdcompltype(typval_T *argvars UNUSED, typval_T *rettv) { + cmdline_info_T *p; + rettv->v_type = VAR_STRING; - rettv->vval.v_string = get_cmdline_completion(); + rettv->vval.v_string = NULL; + + p = get_ccline_ptr(); + if (cmdline_star > 0 || p == NULL || p->xpc == NULL) + return; + + rettv->vval.v_string = get_cmdline_completion(p->xpc); } /* diff --git a/src/proto/cmdexpand.pro b/src/proto/cmdexpand.pro index 92e9dcb024..2b2cc23e5a 100644 --- a/src/proto/cmdexpand.pro +++ b/src/proto/cmdexpand.pro @@ -24,5 +24,6 @@ int wildmenu_translate_key(cmdline_info_T *cclp, int key, expand_T *xp, int did_ int wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp); void wildmenu_cleanup(cmdline_info_T *cclp); void f_getcompletion(typval_T *argvars, typval_T *rettv); +void f_getcompletiontype(typval_T *argvars, typval_T *rettv); void f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv); /* vim: set ft=c : */ diff --git a/src/proto/ex_getln.pro b/src/proto/ex_getln.pro index 6c93ac755a..9087c837df 100644 --- a/src/proto/ex_getln.pro +++ b/src/proto/ex_getln.pro @@ -30,6 +30,7 @@ char_u *vim_strsave_fnameescape(char_u *fname, int what); 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); +char_u *get_cmdline_completion(expand_T *xpc); void f_getcmdcomplpat(typval_T *argvars, typval_T *rettv); void f_getcmdcompltype(typval_T *argvars, typval_T *rettv); void f_getcmdline(typval_T *argvars, typval_T *rettv); diff --git a/src/testdir/test_cmdline.vim b/src/testdir/test_cmdline.vim index 59c25db75f..844f58945c 100644 --- a/src/testdir/test_cmdline.vim +++ b/src/testdir/test_cmdline.vim @@ -877,6 +877,17 @@ func Test_getcompletion() call assert_fails('call getcompletion("abc", [])', 'E1174:') endfunc +func Test_getcompletiontype() + call assert_fails('call getcompletiontype()', 'E119:') + call assert_fails('call getcompletiontype({})', 'E1174:') + call assert_equal(getcompletiontype(''), 'command') + call assert_equal(getcompletiontype('dummy '), '') + call assert_equal(getcompletiontype('cd '), 'dir_in_path') + call assert_equal(getcompletiontype('let v:n'), 'var') + call assert_equal(getcompletiontype('call tag'), 'function') + call assert_equal(getcompletiontype('help '), 'help') +endfunc + func Test_multibyte_expression() " Get a dialog in the GUI CheckNotGui