From 29f30ad91c596e1684a5b3cb3125aee315655512 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 8 Sep 2025 11:40:12 +0800 Subject: [PATCH] vim-patch:9.1.1679: unclear what key causes CmdlineLeave autocommand (#35677) Problem: unclear what key causes CmdlineLeave autocommand Solution: Set |v:char| to the key (Girish Palya). related: vim/vim#17806 closes: vim/vim#18063 https://github.com/vim/vim/commit/ba9551d131d608b71155bacc0c4a65264f1f5f7c Co-authored-by: Girish Palya --- runtime/doc/autocmd.txt | 3 +++ runtime/doc/news.txt | 1 + runtime/doc/vvars.txt | 3 ++- runtime/lua/vim/_meta/vvars.lua | 3 ++- src/nvim/ex_getln.c | 3 +++ src/nvim/vvars.lua | 3 ++- test/old/testdir/test_cmdline.vim | 19 +++++++++++++++++++ 7 files changed, 32 insertions(+), 3 deletions(-) diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt index 3a340d40eb..b0e9530f31 100644 --- a/runtime/doc/autocmd.txt +++ b/runtime/doc/autocmd.txt @@ -400,6 +400,8 @@ CmdlineLeave Before leaving the command-line (including non-interactive use of ":" in a mapping: use || instead to avoid this). expands to the |cmdline-char|. + Sets the |v:char| to the key that exited the + command-line (e.g. , , ). Sets these |v:event| keys: abort (mutable) cmdlevel @@ -417,6 +419,7 @@ CmdlineLeavePre Just before leaving the command line, and not when using ||. Also triggered when abandoning the command line by typing CTRL-C or . is set to |cmdline-char|. + Sets |v:char| as with |CmdlineLeave|. *CmdwinEnter* CmdwinEnter After entering the command-line window. Useful for setting options specifically for diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 0e870b261f..9b62b45817 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -196,6 +196,7 @@ EDITOR EVENTS +• |CmdlineLeave| sets |v:char| to the character that stops the Cmdline mode. • |CmdlineLeavePre| triggered before preparing to leave the command line. • New `append` paremeter for |ui-messages| `msg_show` event. • New `msg_id` paremeter for |ui-messages| `msg_show` event. diff --git a/runtime/doc/vvars.txt b/runtime/doc/vvars.txt index 0248fb1f69..3e8dbd752f 100644 --- a/runtime/doc/vvars.txt +++ b/runtime/doc/vvars.txt @@ -21,7 +21,8 @@ v:argv v:char Argument for evaluating 'formatexpr' and used for the typed character when using in an abbreviation |:map-|. - It is also used by the |InsertCharPre| and |InsertEnter| events. + It is also used by the |InsertCharPre|, |InsertEnter|, + |CmdlineLeave| and |CmdlineLeavePre| events. *v:charconvert_from* *charconvert_from-variable* v:charconvert_from diff --git a/runtime/lua/vim/_meta/vvars.lua b/runtime/lua/vim/_meta/vvars.lua index a5c6c6b984..dd4aa8d80a 100644 --- a/runtime/lua/vim/_meta/vvars.lua +++ b/runtime/lua/vim/_meta/vvars.lua @@ -14,7 +14,8 @@ vim.v.argv = ... --- Argument for evaluating 'formatexpr' and used for the typed --- character when using in an abbreviation `:map-`. ---- It is also used by the `InsertCharPre` and `InsertEnter` events. +--- It is also used by the `InsertCharPre`, `InsertEnter`, +--- `CmdlineLeave` and `CmdlineLeavePre` events. --- @type string vim.v.char = ... diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index dbb1dbc9b8..f17dcd5ae0 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -897,6 +897,7 @@ static uint8_t *command_line_enter(int firstc, int count, int indent, bool clear // Trigger CmdlineLeavePre autocommands if not already triggered. if (!s->event_cmdlineleavepre_triggered) { + set_vim_var_char(s->c); // Set v:char trigger_cmd_autocmd(s->cmdline_type, EVENT_CMDLINELEAVEPRE); } @@ -910,6 +911,7 @@ static uint8_t *command_line_enter(int firstc, int count, int indent, bool clear // not readonly: tv_dict_add_bool(dict, S_LEN("abort"), s->gotesc ? kBoolVarTrue : kBoolVarFalse); + set_vim_var_char(s->c); // Set v:char TRY_WRAP(&err, { apply_autocmds(EVENT_CMDLINELEAVE, firstcbuf, firstcbuf, false, curbuf); // error printed below, to avoid redraw issues @@ -1361,6 +1363,7 @@ static int command_line_execute(VimState *state, int key) // Trigger CmdlineLeavePre autocommand if ((KeyTyped && (s->c == '\n' || s->c == '\r' || s->c == K_KENTER || s->c == ESC)) || s->c == Ctrl_C) { + set_vim_var_char(s->c); // Set v:char trigger_cmd_autocmd(s->cmdline_type, EVENT_CMDLINELEAVEPRE); s->event_cmdlineleavepre_triggered = true; if ((s->c == ESC || s->c == Ctrl_C) && (wim_flags[0] & kOptWimFlagList)) { diff --git a/src/nvim/vvars.lua b/src/nvim/vvars.lua index 67fa5bd87e..37b7c8022b 100644 --- a/src/nvim/vvars.lua +++ b/src/nvim/vvars.lua @@ -14,7 +14,8 @@ M.vars = { desc = [=[ Argument for evaluating 'formatexpr' and used for the typed character when using in an abbreviation |:map-|. - It is also used by the |InsertCharPre| and |InsertEnter| events. + It is also used by the |InsertCharPre|, |InsertEnter|, + |CmdlineLeave| and |CmdlineLeavePre| events. ]=], }, charconvert_from = { diff --git a/test/old/testdir/test_cmdline.vim b/test/old/testdir/test_cmdline.vim index 08df73f3f9..41c9eb9398 100644 --- a/test/old/testdir/test_cmdline.vim +++ b/test/old/testdir/test_cmdline.vim @@ -4967,4 +4967,23 @@ func Test_long_line_noselect() call StopVimInTerminal(buf) endfunc +func Test_CmdlineLeave_vchar_keys() + func OnLeave() + let g:leave_key = v:char + endfunction + + new + for event in ["CmdlineLeavePre", "CmdlineLeave"] + exec "autocmd" event "* :call OnLeave()" + for key in ["\", "\", "\"] + call feedkeys($":echo{key}", 'tx') + call assert_equal(key, g:leave_key) + endfor + exec "autocmd!" event + endfor + bwipe! + delfunc OnLeave + unlet g:leave_key +endfunc + " vim: shiftwidth=2 sts=2 expandtab