Skip to content

Commit 29f30ad

Browse files
zeertzjqgirishji
andauthored
vim-patch:9.1.1679: unclear what key causes CmdlineLeave autocommand (neovim#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 vim/vim@ba9551d Co-authored-by: Girish Palya <[email protected]>
1 parent da78772 commit 29f30ad

File tree

7 files changed

+32
-3
lines changed

7 files changed

+32
-3
lines changed

runtime/doc/autocmd.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ CmdlineLeave Before leaving the command-line (including
400400
non-interactive use of ":" in a mapping: use
401401
|<Cmd>| instead to avoid this).
402402
<afile> expands to the |cmdline-char|.
403+
Sets the |v:char| to the key that exited the
404+
command-line (e.g. <CR>, <CTRL-C>, <Esc>).
403405
Sets these |v:event| keys:
404406
abort (mutable)
405407
cmdlevel
@@ -417,6 +419,7 @@ CmdlineLeavePre Just before leaving the command line, and
417419
not when using |<Cmd>|. Also triggered when
418420
abandoning the command line by typing CTRL-C
419421
or <Esc>. <afile> is set to |cmdline-char|.
422+
Sets |v:char| as with |CmdlineLeave|.
420423
*CmdwinEnter*
421424
CmdwinEnter After entering the command-line window.
422425
Useful for setting options specifically for

runtime/doc/news.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ EDITOR
196196

197197
EVENTS
198198

199+
|CmdlineLeave| sets |v:char| to the character that stops the Cmdline mode.
199200
|CmdlineLeavePre| triggered before preparing to leave the command line.
200201
• New `append` paremeter for |ui-messages| `msg_show` event.
201202
• New `msg_id` paremeter for |ui-messages| `msg_show` event.

runtime/doc/vvars.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ v:argv
2121
v:char
2222
Argument for evaluating 'formatexpr' and used for the typed
2323
character when using <expr> in an abbreviation |:map-<expr>|.
24-
It is also used by the |InsertCharPre| and |InsertEnter| events.
24+
It is also used by the |InsertCharPre|, |InsertEnter|,
25+
|CmdlineLeave| and |CmdlineLeavePre| events.
2526

2627
*v:charconvert_from* *charconvert_from-variable*
2728
v:charconvert_from

runtime/lua/vim/_meta/vvars.lua

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nvim/ex_getln.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ static uint8_t *command_line_enter(int firstc, int count, int indent, bool clear
897897

898898
// Trigger CmdlineLeavePre autocommands if not already triggered.
899899
if (!s->event_cmdlineleavepre_triggered) {
900+
set_vim_var_char(s->c); // Set v:char
900901
trigger_cmd_autocmd(s->cmdline_type, EVENT_CMDLINELEAVEPRE);
901902
}
902903

@@ -910,6 +911,7 @@ static uint8_t *command_line_enter(int firstc, int count, int indent, bool clear
910911
// not readonly:
911912
tv_dict_add_bool(dict, S_LEN("abort"),
912913
s->gotesc ? kBoolVarTrue : kBoolVarFalse);
914+
set_vim_var_char(s->c); // Set v:char
913915
TRY_WRAP(&err, {
914916
apply_autocmds(EVENT_CMDLINELEAVE, firstcbuf, firstcbuf, false, curbuf);
915917
// error printed below, to avoid redraw issues
@@ -1361,6 +1363,7 @@ static int command_line_execute(VimState *state, int key)
13611363
// Trigger CmdlineLeavePre autocommand
13621364
if ((KeyTyped && (s->c == '\n' || s->c == '\r' || s->c == K_KENTER || s->c == ESC))
13631365
|| s->c == Ctrl_C) {
1366+
set_vim_var_char(s->c); // Set v:char
13641367
trigger_cmd_autocmd(s->cmdline_type, EVENT_CMDLINELEAVEPRE);
13651368
s->event_cmdlineleavepre_triggered = true;
13661369
if ((s->c == ESC || s->c == Ctrl_C) && (wim_flags[0] & kOptWimFlagList)) {

src/nvim/vvars.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ M.vars = {
1414
desc = [=[
1515
Argument for evaluating 'formatexpr' and used for the typed
1616
character when using <expr> in an abbreviation |:map-<expr>|.
17-
It is also used by the |InsertCharPre| and |InsertEnter| events.
17+
It is also used by the |InsertCharPre|, |InsertEnter|,
18+
|CmdlineLeave| and |CmdlineLeavePre| events.
1819
]=],
1920
},
2021
charconvert_from = {

test/old/testdir/test_cmdline.vim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4967,4 +4967,23 @@ func Test_long_line_noselect()
49674967
call StopVimInTerminal(buf)
49684968
endfunc
49694969

4970+
func Test_CmdlineLeave_vchar_keys()
4971+
func OnLeave()
4972+
let g:leave_key = v:char
4973+
endfunction
4974+
4975+
new
4976+
for event in ["CmdlineLeavePre", "CmdlineLeave"]
4977+
exec "autocmd" event "* :call OnLeave()"
4978+
for key in ["\<C-C>", "\<Esc>", "\<CR>"]
4979+
call feedkeys($":echo{key}", 'tx')
4980+
call assert_equal(key, g:leave_key)
4981+
endfor
4982+
exec "autocmd!" event
4983+
endfor
4984+
bwipe!
4985+
delfunc OnLeave
4986+
unlet g:leave_key
4987+
endfunc
4988+
49704989
" vim: shiftwidth=2 sts=2 expandtab

0 commit comments

Comments
 (0)