summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2017-10-19 18:35:51 +0200
committerBram Moolenaar <Bram@vim.org>2017-10-19 18:35:51 +0200
commitfafcf0dd59fd9c4ef743bb333ae40d1d322b6079 (patch)
tree15b885e7fe89b2cdcf70c2eb33456db7f1f79b77
parentff930cad8a9100eeb04256aab1a14de993c1d7e9 (diff)
downloadvim-git-fafcf0dd59fd9c4ef743bb333ae40d1d322b6079.tar.gz
patch 8.0.1206: no autocmd for entering or leaving the command linev8.0.1206
Problem: No autocmd for entering or leaving the command line. Solution: Add CmdlineEnter and CmdlineLeave.
-rw-r--r--runtime/doc/autocmd.txt12
-rw-r--r--src/ex_getln.c36
-rw-r--r--src/fileio.c2
-rw-r--r--src/testdir/test_autocmd.vim22
-rw-r--r--src/version.c2
-rw-r--r--src/vim.h2
6 files changed, 69 insertions, 7 deletions
diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt
index b4b064c26..da35c279c 100644
--- a/runtime/doc/autocmd.txt
+++ b/runtime/doc/autocmd.txt
@@ -492,6 +492,18 @@ CmdUndefined When a user command is used but it isn't
command is defined. An alternative is to
always define the user command and have it
invoke an autoloaded function. See |autoload|.
+ *CmdlineEnter*
+CmdlineEnter After moving the cursor to the command line,
+ where the user can type a command or search
+ string.
+ <afile> is set to a single character,
+ indicating the type of command-line.
+ |cmdwin-char|
+ *CmdlineLeave*
+CmdlineLeave Before leaving the command line.
+ <afile> is set to a single character,
+ indicating the type of command-line.
+ |cmdwin-char|
*CmdwinEnter*
CmdwinEnter After entering the command-line window.
Useful for setting options specifically for
diff --git a/src/ex_getln.c b/src/ex_getln.c
index 9f7dad99a..ceeeaf89f 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -145,6 +145,19 @@ sort_func_compare(const void *s1, const void *s2);
static void set_search_match(pos_T *t);
#endif
+
+#ifdef FEAT_AUTOCMD
+ static void
+trigger_cmd_autocmd(int typechar, int evt)
+{
+ char_u typestr[2];
+
+ typestr[0] = typechar;
+ typestr[1] = NUL;
+ apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
+}
+#endif
+
/*
* getcmdline() - accept a command line starting with firstc.
*
@@ -222,6 +235,9 @@ getcmdline(
* custom status line may invoke ":normal". */
struct cmdline_info save_ccline;
#endif
+#ifdef FEAT_AUTOCMD
+ int cmdline_type;
+#endif
#ifdef FEAT_EVAL
if (firstc == -1)
@@ -349,6 +365,12 @@ getcmdline(
* terminal mode set to cooked. Need to set raw mode here then. */
settmode(TMODE_RAW);
+#ifdef FEAT_AUTOCMD
+ /* Trigger CmdlineEnter autocommands. */
+ cmdline_type = firstc == NUL ? '-' : firstc;
+ trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
+#endif
+
#ifdef FEAT_CMDHIST
init_history();
hiscnt = hislen; /* set hiscnt to impossible history value */
@@ -2085,6 +2107,11 @@ returncmd:
if (some_key_typed)
need_wait_return = FALSE;
+#ifdef FEAT_AUTOCMD
+ /* Trigger CmdlineLeave autocommands. */
+ trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
+#endif
+
State = save_State;
#ifdef USE_IM_CONTROL
if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
@@ -6834,9 +6861,6 @@ open_cmdwin(void)
linenr_T lnum;
int histtype;
garray_T winsizes;
-#ifdef FEAT_AUTOCMD
- char_u typestr[2];
-#endif
int save_restart_edit = restart_edit;
int save_State = State;
int save_exmode = exmode_active;
@@ -6965,9 +6989,7 @@ open_cmdwin(void)
# ifdef FEAT_AUTOCMD
/* Trigger CmdwinEnter autocommands. */
- typestr[0] = cmdwin_type;
- typestr[1] = NUL;
- apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
+ trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
if (restart_edit != 0) /* autocmd with ":startinsert" */
stuffcharReadbuff(K_NOP);
# endif
@@ -6990,7 +7012,7 @@ open_cmdwin(void)
# endif
/* Trigger CmdwinLeave autocommands. */
- apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
+ trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
# ifdef FEAT_FOLDING
/* Restore KeyTyped in case it is modified by autocommands */
diff --git a/src/fileio.c b/src/fileio.c
index 0d1ac2d07..575515613 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -7731,6 +7731,8 @@ static struct event_name
{"BufWritePost", EVENT_BUFWRITEPOST},
{"BufWritePre", EVENT_BUFWRITEPRE},
{"BufWriteCmd", EVENT_BUFWRITECMD},
+ {"CmdlineEnter", EVENT_CMDLINEENTER},
+ {"CmdlineLeave", EVENT_CMDLINELEAVE},
{"CmdwinEnter", EVENT_CMDWINENTER},
{"CmdwinLeave", EVENT_CMDWINLEAVE},
{"CmdUndefined", EVENT_CMDUNDEFINED},
diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim
index f5a228995..6af0820eb 100644
--- a/src/testdir/test_autocmd.vim
+++ b/src/testdir/test_autocmd.vim
@@ -793,3 +793,25 @@ func Test_QuitPre()
bwipe Xfoo
bwipe Xbar
endfunc
+
+func Test_Cmdline()
+ au! CmdlineEnter : let g:entered = expand('<afile>')
+ au! CmdlineLeave : let g:left = expand('<afile>')
+ let g:entered = 0
+ let g:left = 0
+ call feedkeys(":echo 'hello'\<CR>", 'xt')
+ call assert_equal(':', g:entered)
+ call assert_equal(':', g:left)
+ au! CmdlineEnter
+ au! CmdlineLeave
+
+ au! CmdlineEnter / let g:entered = expand('<afile>')
+ au! CmdlineLeave / let g:left = expand('<afile>')
+ let g:entered = 0
+ let g:left = 0
+ call feedkeys("/hello<CR>", 'xt')
+ call assert_equal('/', g:entered)
+ call assert_equal('/', g:left)
+ au! CmdlineEnter
+ au! CmdlineLeave
+endfunc
diff --git a/src/version.c b/src/version.c
index 5a6c72180..b0f8c5f51 100644
--- a/src/version.c
+++ b/src/version.c
@@ -762,6 +762,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1206,
+/**/
1205,
/**/
1204,
diff --git a/src/vim.h b/src/vim.h
index 11b7b0838..ed12b072f 100644
--- a/src/vim.h
+++ b/src/vim.h
@@ -1295,6 +1295,8 @@ enum auto_event
EVENT_BUFWRITEPOST, /* after writing a buffer */
EVENT_BUFWRITEPRE, /* before writing a buffer */
EVENT_BUFWRITECMD, /* write buffer using command */
+ EVENT_CMDLINEENTER, /* after entering the command line */
+ EVENT_CMDLINELEAVE, /* before leaving the command line */
EVENT_CMDWINENTER, /* after entering the cmdline window */
EVENT_CMDWINLEAVE, /* before leaving the cmdline window */
EVENT_COLORSCHEME, /* after loading a colorscheme */