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

ba9551d131

Co-authored-by: Girish Palya <girishji@gmail.com>
This commit is contained in:
zeertzjq
2025-09-08 11:40:12 +08:00
committed by GitHub
parent da78772328
commit 29f30ad91c
7 changed files with 32 additions and 3 deletions

View File

@@ -400,6 +400,8 @@ CmdlineLeave Before leaving the command-line (including
non-interactive use of ":" in a mapping: use
|<Cmd>| instead to avoid this).
<afile> expands to the |cmdline-char|.
Sets the |v:char| to the key that exited the
command-line (e.g. <CR>, <CTRL-C>, <Esc>).
Sets these |v:event| keys:
abort (mutable)
cmdlevel
@@ -417,6 +419,7 @@ CmdlineLeavePre Just before leaving the command line, and
not when using |<Cmd>|. Also triggered when
abandoning the command line by typing CTRL-C
or <Esc>. <afile> 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

View File

@@ -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.

View File

@@ -21,7 +21,8 @@ v:argv
v:char
Argument for evaluating 'formatexpr' and used for the typed
character when using <expr> in an abbreviation |:map-<expr>|.
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

View File

@@ -14,7 +14,8 @@ vim.v.argv = ...
--- Argument for evaluating 'formatexpr' and used for the typed
--- character when using <expr> in an abbreviation `:map-<expr>`.
--- 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 = ...

View File

@@ -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)) {

View File

@@ -14,7 +14,8 @@ M.vars = {
desc = [=[
Argument for evaluating 'formatexpr' and used for the typed
character when using <expr> in an abbreviation |:map-<expr>|.
It is also used by the |InsertCharPre| and |InsertEnter| events.
It is also used by the |InsertCharPre|, |InsertEnter|,
|CmdlineLeave| and |CmdlineLeavePre| events.
]=],
},
charconvert_from = {

View File

@@ -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 ["\<C-C>", "\<Esc>", "\<CR>"]
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