Skip to content

Commit fafcf0d

Browse files
committed
patch 8.0.1206: no autocmd for entering or leaving the command line
Problem: No autocmd for entering or leaving the command line. Solution: Add CmdlineEnter and CmdlineLeave.
1 parent ff930ca commit fafcf0d

File tree

6 files changed

+69
-7
lines changed

6 files changed

+69
-7
lines changed

runtime/doc/autocmd.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,18 @@ CmdUndefined When a user command is used but it isn't
492492
command is defined. An alternative is to
493493
always define the user command and have it
494494
invoke an autoloaded function. See |autoload|.
495+
*CmdlineEnter*
496+
CmdlineEnter After moving the cursor to the command line,
497+
where the user can type a command or search
498+
string.
499+
<afile> is set to a single character,
500+
indicating the type of command-line.
501+
|cmdwin-char|
502+
*CmdlineLeave*
503+
CmdlineLeave Before leaving the command line.
504+
<afile> is set to a single character,
505+
indicating the type of command-line.
506+
|cmdwin-char|
495507
*CmdwinEnter*
496508
CmdwinEnter After entering the command-line window.
497509
Useful for setting options specifically for

src/ex_getln.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ sort_func_compare(const void *s1, const void *s2);
145145
static void set_search_match(pos_T *t);
146146
#endif
147147

148+
149+
#ifdef FEAT_AUTOCMD
150+
static void
151+
trigger_cmd_autocmd(int typechar, int evt)
152+
{
153+
char_u typestr[2];
154+
155+
typestr[0] = typechar;
156+
typestr[1] = NUL;
157+
apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158+
}
159+
#endif
160+
148161
/*
149162
* getcmdline() - accept a command line starting with firstc.
150163
*
@@ -222,6 +235,9 @@ getcmdline(
222235
* custom status line may invoke ":normal". */
223236
struct cmdline_info save_ccline;
224237
#endif
238+
#ifdef FEAT_AUTOCMD
239+
int cmdline_type;
240+
#endif
225241

226242
#ifdef FEAT_EVAL
227243
if (firstc == -1)
@@ -349,6 +365,12 @@ getcmdline(
349365
* terminal mode set to cooked. Need to set raw mode here then. */
350366
settmode(TMODE_RAW);
351367

368+
#ifdef FEAT_AUTOCMD
369+
/* Trigger CmdlineEnter autocommands. */
370+
cmdline_type = firstc == NUL ? '-' : firstc;
371+
trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
372+
#endif
373+
352374
#ifdef FEAT_CMDHIST
353375
init_history();
354376
hiscnt = hislen; /* set hiscnt to impossible history value */
@@ -2085,6 +2107,11 @@ getcmdline(
20852107
if (some_key_typed)
20862108
need_wait_return = FALSE;
20872109

2110+
#ifdef FEAT_AUTOCMD
2111+
/* Trigger CmdlineLeave autocommands. */
2112+
trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
2113+
#endif
2114+
20882115
State = save_State;
20892116
#ifdef USE_IM_CONTROL
20902117
if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
@@ -6834,9 +6861,6 @@ open_cmdwin(void)
68346861
linenr_T lnum;
68356862
int histtype;
68366863
garray_T winsizes;
6837-
#ifdef FEAT_AUTOCMD
6838-
char_u typestr[2];
6839-
#endif
68406864
int save_restart_edit = restart_edit;
68416865
int save_State = State;
68426866
int save_exmode = exmode_active;
@@ -6965,9 +6989,7 @@ open_cmdwin(void)
69656989

69666990
# ifdef FEAT_AUTOCMD
69676991
/* Trigger CmdwinEnter autocommands. */
6968-
typestr[0] = cmdwin_type;
6969-
typestr[1] = NUL;
6970-
apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
6992+
trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
69716993
if (restart_edit != 0) /* autocmd with ":startinsert" */
69726994
stuffcharReadbuff(K_NOP);
69736995
# endif
@@ -6990,7 +7012,7 @@ open_cmdwin(void)
69907012
# endif
69917013

69927014
/* Trigger CmdwinLeave autocommands. */
6993-
apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
7015+
trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
69947016

69957017
# ifdef FEAT_FOLDING
69967018
/* Restore KeyTyped in case it is modified by autocommands */

src/fileio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7731,6 +7731,8 @@ static struct event_name
77317731
{"BufWritePost", EVENT_BUFWRITEPOST},
77327732
{"BufWritePre", EVENT_BUFWRITEPRE},
77337733
{"BufWriteCmd", EVENT_BUFWRITECMD},
7734+
{"CmdlineEnter", EVENT_CMDLINEENTER},
7735+
{"CmdlineLeave", EVENT_CMDLINELEAVE},
77347736
{"CmdwinEnter", EVENT_CMDWINENTER},
77357737
{"CmdwinLeave", EVENT_CMDWINLEAVE},
77367738
{"CmdUndefined", EVENT_CMDUNDEFINED},

src/testdir/test_autocmd.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,3 +793,25 @@ func Test_QuitPre()
793793
bwipe Xfoo
794794
bwipe Xbar
795795
endfunc
796+
797+
func Test_Cmdline()
798+
au! CmdlineEnter : let g:entered = expand('<afile>')
799+
au! CmdlineLeave : let g:left = expand('<afile>')
800+
let g:entered = 0
801+
let g:left = 0
802+
call feedkeys(":echo 'hello'\<CR>", 'xt')
803+
call assert_equal(':', g:entered)
804+
call assert_equal(':', g:left)
805+
au! CmdlineEnter
806+
au! CmdlineLeave
807+
808+
au! CmdlineEnter / let g:entered = expand('<afile>')
809+
au! CmdlineLeave / let g:left = expand('<afile>')
810+
let g:entered = 0
811+
let g:left = 0
812+
call feedkeys("/hello<CR>", 'xt')
813+
call assert_equal('/', g:entered)
814+
call assert_equal('/', g:left)
815+
au! CmdlineEnter
816+
au! CmdlineLeave
817+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ static char *(features[]) =
761761

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1206,
764766
/**/
765767
1205,
766768
/**/

src/vim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,8 @@ enum auto_event
12951295
EVENT_BUFWRITEPOST, /* after writing a buffer */
12961296
EVENT_BUFWRITEPRE, /* before writing a buffer */
12971297
EVENT_BUFWRITECMD, /* write buffer using command */
1298+
EVENT_CMDLINEENTER, /* after entering the command line */
1299+
EVENT_CMDLINELEAVE, /* before leaving the command line */
12981300
EVENT_CMDWINENTER, /* after entering the cmdline window */
12991301
EVENT_CMDWINLEAVE, /* before leaving the cmdline window */
13001302
EVENT_COLORSCHEME, /* after loading a colorscheme */

0 commit comments

Comments
 (0)