From c2fb1fc700db28cb554be9da8e79443b5d3a5fe9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 24 Sep 2024 06:51:02 +0800 Subject: [PATCH 1/2] vim-patch:9.1.0741: No way to get prompt for input()/confirm() Problem: No way to get prompt for input()/confirm() Solution: add getcmdprompt() function (Shougo Matsushita) (Shougo Matsushita) closes: vim/vim#15667 https://github.com/vim/vim/commit/6908428560a0d6ae27bf7af6fcb6dc362e31926c Co-authored-by: Shougo Matsushita --- runtime/doc/builtin.txt | 26 ++++++++++++++++--------- runtime/doc/usr_41.txt | 3 ++- runtime/lua/vim/_meta/vimfn.lua | 28 ++++++++++++++++++--------- src/nvim/eval.lua | 32 ++++++++++++++++++++++--------- src/nvim/eval/funcs.c | 2 ++ src/nvim/ex_getln.c | 24 +++++++++++++++++++++++ test/old/testdir/test_cmdline.vim | 16 +++++++++++++++- 7 files changed, 102 insertions(+), 29 deletions(-) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 0e3e09ea76..d76cf96762 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -2361,18 +2361,18 @@ getcmdcompltype() *getcmdcompltype()* 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()| and - |setcmdline()|. + Also see |getcmdtype()|, |setcmdpos()|, |getcmdline()|, + |getcmdprompt()| and |setcmdline()|. Returns an empty string when completion is not defined. getcmdline() *getcmdline()* - Return the current command-line. Only works when the command - line is being edited, thus requires use of |c_CTRL-\_e| or - |c_CTRL-R_=|. + Return the current command-line input. Only works when the + command line is being edited, thus requires use of + |c_CTRL-\_e| or |c_CTRL-R_=|. Example: >vim cmap eescape(getcmdline(), ' \') -< Also see |getcmdtype()|, |getcmdpos()|, |setcmdpos()| and - |setcmdline()|. +< Also see |getcmdtype()|, |getcmdpos()|, |setcmdpos()|, + |getcmdprompt()| and |setcmdline()|. Returns an empty string when entering a password or using |inputsecret()|. @@ -2382,8 +2382,16 @@ getcmdpos() *getcmdpos()* 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 |getcmdtype()|, |setcmdpos()|, |getcmdline()| and - |setcmdline()|. + Also see |getcmdtype()|, |setcmdpos()|, |getcmdline()|, + |getcmdprompt()| and |setcmdline()|. + +getcmdprompt() *getcmdprompt()* + Return the current command-line prompt when using functions + like |input()| or |confirm()|. + Only works when the command line is being edited, thus + requires use of |c_CTRL-\_e| or |c_CTRL-R_=|. + Also see |getcmdtype()|, |getcmdline()|, |getcmdpos()|, + |setcmdpos()| and |setcmdline()|. getcmdscreenpos() *getcmdscreenpos()* Return the screen position of the cursor in the command line diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index 08ef9ac886..8c7ed875cf 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -908,7 +908,8 @@ Buffers, windows and the argument list: Command line: *command-line-functions* getcmdcompltype() get the type of the current command line completion - getcmdline() get the current command line + getcmdline() get the current command line input + getcmdprompt() get the current command line prompt getcmdpos() get position of the cursor in the command line getcmdscreenpos() get screen position of the cursor in the command line diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index 70a7503aac..649805f447 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -2883,20 +2883,20 @@ function vim.fn.getcharstr(expr) end --- 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()| and ---- |setcmdline()|. +--- Also see |getcmdtype()|, |setcmdpos()|, |getcmdline()|, +--- |getcmdprompt()| and |setcmdline()|. --- Returns an empty string when completion is not defined. --- --- @return string function vim.fn.getcmdcompltype() end ---- Return the current command-line. Only works when the command ---- line is being edited, thus requires use of |c_CTRL-\_e| or ---- |c_CTRL-R_=|. +--- Return the current command-line input. Only works when the +--- command line is being edited, thus requires use of +--- |c_CTRL-\_e| or |c_CTRL-R_=|. --- Example: >vim --- cmap eescape(getcmdline(), ' \') ---- vim cmap eescape(getcmdline(), ' \') - vval.v_number = p != NULL ? p->cmdpos + 1 : 0; } +static char current_prompt[CMDBUFFSIZE + 1] = ""; + +/// Get current command line prompt. +static char *get_prompt(void) +{ + return current_prompt; +} + +/// Set current command line prompt. +void set_prompt(const char *str) +{ + xstrlcpy(current_prompt, str, sizeof(current_prompt)); +} + +/// "getcmdprompt()" function +void f_getcmdprompt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + CmdlineInfo *p = get_ccline_ptr(); + rettv->v_type = VAR_STRING; + rettv->vval.v_string = p != NULL ? xstrdup(get_prompt()) : NULL; +} + /// "getcmdscreenpos()" function void f_getcmdscreenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { @@ -4730,6 +4752,8 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const const bool cmd_silent_save = cmd_silent; cmd_silent = false; // Want to see the prompt. + set_prompt(prompt); + // Only the part of the message after the last NL is considered as // prompt for the command line, unlsess cmdline is externalized const char *p = prompt; diff --git a/test/old/testdir/test_cmdline.vim b/test/old/testdir/test_cmdline.vim index d00b5bbf46..ba71f4b785 100644 --- a/test/old/testdir/test_cmdline.vim +++ b/test/old/testdir/test_cmdline.vim @@ -1534,7 +1534,7 @@ endfunc set cpo& -func Test_getcmdtype() +func Test_getcmdtype_getcmdprompt() call feedkeys(":MyCmd a\=Check_cmdline(':')\\", "xt") let cmdtype = '' @@ -1558,6 +1558,20 @@ func Test_getcmdtype() cunmap call assert_equal('', getcmdline()) + + call assert_equal('', getcmdprompt()) + augroup test_CmdlineEnter + autocmd! + autocmd CmdlineEnter * let g:cmdprompt=getcmdprompt() + augroup END + call feedkeys(":call input('Answer?')\a\\", "xt") + call assert_equal('Answer?', g:cmdprompt) + call assert_equal('', getcmdprompt()) + + augroup test_CmdlineEnter + au! + augroup END + augroup! test_CmdlineEnter endfunc func Test_getcmdwintype() From 65b6cd1b3a43024f6c2d9d6f31b8ae3a69dabb56 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 26 Sep 2024 22:15:54 +0800 Subject: [PATCH 2/2] vim-patch:9.1.0742: getcmdprompt() implementation can be improved Problem: getcmdprompt() implementation can be improved Solution: Improve and simplify it (h-east) closes: vim/vim#15743 https://github.com/vim/vim/commit/25876a6cdd439054d0b3f920ccca0a435481de15 Co-authored-by: h-east --- src/nvim/eval/funcs.c | 2 -- src/nvim/ex_getln.c | 19 ++----------------- test/old/testdir/test_cmdline.vim | 11 +++++++++++ 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 4a96c9a3f3..e8b9288717 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -813,8 +813,6 @@ static void f_confirm(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) const char *message = tv_get_string_chk(&argvars[0]); if (message == NULL) { error = true; - } else { - set_prompt(message); } if (argvars[1].v_type != VAR_UNKNOWN) { buttons = tv_get_string_buf_chk(&argvars[1], buf); diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 7eb4fe67a1..69fcdac095 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -4132,26 +4132,13 @@ void f_getcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) rettv->vval.v_number = p != NULL ? p->cmdpos + 1 : 0; } -static char current_prompt[CMDBUFFSIZE + 1] = ""; - -/// Get current command line prompt. -static char *get_prompt(void) -{ - return current_prompt; -} - -/// Set current command line prompt. -void set_prompt(const char *str) -{ - xstrlcpy(current_prompt, str, sizeof(current_prompt)); -} - /// "getcmdprompt()" function void f_getcmdprompt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { CmdlineInfo *p = get_ccline_ptr(); rettv->v_type = VAR_STRING; - rettv->vval.v_string = p != NULL ? xstrdup(get_prompt()) : NULL; + rettv->vval.v_string = p != NULL && p->cmdprompt != NULL + ? xstrdup(p->cmdprompt) : NULL; } /// "getcmdscreenpos()" function @@ -4752,8 +4739,6 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const const bool cmd_silent_save = cmd_silent; cmd_silent = false; // Want to see the prompt. - set_prompt(prompt); - // Only the part of the message after the last NL is considered as // prompt for the command line, unlsess cmdline is externalized const char *p = prompt; diff --git a/test/old/testdir/test_cmdline.vim b/test/old/testdir/test_cmdline.vim index ba71f4b785..d8a217fd05 100644 --- a/test/old/testdir/test_cmdline.vim +++ b/test/old/testdir/test_cmdline.vim @@ -1567,6 +1567,17 @@ func Test_getcmdtype_getcmdprompt() call feedkeys(":call input('Answer?')\a\\", "xt") call assert_equal('Answer?', g:cmdprompt) call assert_equal('', getcmdprompt()) + call feedkeys(":\\", "xt") + call assert_equal('', g:cmdprompt) + call assert_equal('', getcmdprompt()) + + let str = "C" .. repeat("c", 1023) .. "xyz" + call feedkeys(":call input('" .. str .. "')\\\", "xt") + call assert_equal(str, g:cmdprompt) + + call feedkeys(':call input("Msg1\nMessage2\nAns?")' .. "\b\\", "xt") + call assert_equal('Ans?', g:cmdprompt) + call assert_equal('', getcmdprompt()) augroup test_CmdlineEnter au!