Skip to content

Commit e99e844

Browse files
committed
patch 7.4.2103
Problem: Can't have "augroup END" right after ":au!". Solution: Check for the bar character before the command argument.
1 parent 45a2495 commit e99e844

4 files changed

Lines changed: 108 additions & 54 deletions

File tree

runtime/doc/autocmd.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ effects. Be careful not to destroy your text.
5252
==============================================================================
5353
2. Defining autocommands *autocmd-define*
5454

55-
Note: The ":autocmd" command cannot be followed by another command, since any
56-
'|' is considered part of the command.
57-
5855
*:au* *:autocmd*
5956
:au[tocmd] [group] {event} {pat} [nested] {cmd}
6057
Add {cmd} to the list of commands that Vim will
@@ -67,6 +64,12 @@ Note: The ":autocmd" command cannot be followed by another command, since any
6764
The special pattern <buffer> or <buffer=N> defines a buffer-local autocommand.
6865
See |autocmd-buflocal|.
6966

67+
Note: The ":autocmd" command can only be followed by another command when the
68+
'|' appears before {cmd}. This works: >
69+
:augroup mine | au! BufRead | augroup END
70+
But this sees "augroup" as part of the defined command: >
71+
:augroup mine | au BufRead * set tw=70 | augroup END
72+
7073
Note that special characters (e.g., "%", "<cword>") in the ":autocmd"
7174
arguments are not expanded when the autocommand is defined. These will be
7275
expanded when the Event is recognized, and the {cmd} is executed. The only

src/fileio.c

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8100,8 +8100,8 @@ event_name2nr(char_u *start, char_u **end)
81008100
int i;
81018101
int len;
81028102

8103-
/* the event name ends with end of line, a blank or a comma */
8104-
for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p)
8103+
/* the event name ends with end of line, '|', a blank or a comma */
8104+
for (p = start; *p && !vim_iswhite(*p) && *p != ',' && *p != '|'; ++p)
81058105
;
81068106
for (i = 0; event_names[i].name != NULL; ++i)
81078107
{
@@ -8153,7 +8153,7 @@ find_end_event(
81538153
}
81548154
else
81558155
{
8156-
for (pat = arg; *pat && !vim_iswhite(*pat); pat = p)
8156+
for (pat = arg; *pat && *pat != '|' && !vim_iswhite(*pat); pat = p)
81578157
{
81588158
if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS)
81598159
{
@@ -8286,8 +8286,9 @@ au_event_restore(char_u *old_ei)
82868286
* Mostly a {group} argument can optionally appear before <event>.
82878287
*/
82888288
void
8289-
do_autocmd(char_u *arg, int forceit)
8289+
do_autocmd(char_u *arg_in, int forceit)
82908290
{
8291+
char_u *arg = arg_in;
82918292
char_u *pat;
82928293
char_u *envpat = NULL;
82938294
char_u *cmd;
@@ -8296,12 +8297,20 @@ do_autocmd(char_u *arg, int forceit)
82968297
int nested = FALSE;
82978298
int group;
82988299

8299-
/*
8300-
* Check for a legal group name. If not, use AUGROUP_ALL.
8301-
*/
8302-
group = au_get_grouparg(&arg);
8303-
if (arg == NULL) /* out of memory */
8304-
return;
8300+
if (*arg == '|')
8301+
{
8302+
arg = (char_u *)"";
8303+
group = AUGROUP_ALL; /* no argument, use all groups */
8304+
}
8305+
else
8306+
{
8307+
/*
8308+
* Check for a legal group name. If not, use AUGROUP_ALL.
8309+
*/
8310+
group = au_get_grouparg(&arg);
8311+
if (arg == NULL) /* out of memory */
8312+
return;
8313+
}
83058314

83068315
/*
83078316
* Scan over the events.
@@ -8311,53 +8320,61 @@ do_autocmd(char_u *arg, int forceit)
83118320
if (pat == NULL)
83128321
return;
83138322

8314-
/*
8315-
* Scan over the pattern. Put a NUL at the end.
8316-
*/
83178323
pat = skipwhite(pat);
8318-
cmd = pat;
8319-
while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\'))
8320-
cmd++;
8321-
if (*cmd)
8322-
*cmd++ = NUL;
8323-
8324-
/* Expand environment variables in the pattern. Set 'shellslash', we want
8325-
* forward slashes here. */
8326-
if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL)
8324+
if (*pat == '|')
83278325
{
8326+
pat = (char_u *)"";
8327+
cmd = (char_u *)"";
8328+
}
8329+
else
8330+
{
8331+
/*
8332+
* Scan over the pattern. Put a NUL at the end.
8333+
*/
8334+
cmd = pat;
8335+
while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\'))
8336+
cmd++;
8337+
if (*cmd)
8338+
*cmd++ = NUL;
8339+
8340+
/* Expand environment variables in the pattern. Set 'shellslash', we want
8341+
* forward slashes here. */
8342+
if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL)
8343+
{
83288344
#ifdef BACKSLASH_IN_FILENAME
8329-
int p_ssl_save = p_ssl;
8345+
int p_ssl_save = p_ssl;
83308346

8331-
p_ssl = TRUE;
8347+
p_ssl = TRUE;
83328348
#endif
8333-
envpat = expand_env_save(pat);
8349+
envpat = expand_env_save(pat);
83348350
#ifdef BACKSLASH_IN_FILENAME
8335-
p_ssl = p_ssl_save;
8351+
p_ssl = p_ssl_save;
83368352
#endif
8337-
if (envpat != NULL)
8338-
pat = envpat;
8339-
}
8353+
if (envpat != NULL)
8354+
pat = envpat;
8355+
}
83408356

8341-
/*
8342-
* Check for "nested" flag.
8343-
*/
8344-
cmd = skipwhite(cmd);
8345-
if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6]))
8346-
{
8347-
nested = TRUE;
8348-
cmd = skipwhite(cmd + 6);
8349-
}
8357+
/*
8358+
* Check for "nested" flag.
8359+
*/
8360+
cmd = skipwhite(cmd);
8361+
if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6]))
8362+
{
8363+
nested = TRUE;
8364+
cmd = skipwhite(cmd + 6);
8365+
}
83508366

8351-
/*
8352-
* Find the start of the commands.
8353-
* Expand <sfile> in it.
8354-
*/
8355-
if (*cmd != NUL)
8356-
{
8357-
cmd = expand_sfile(cmd);
8358-
if (cmd == NULL) /* some error */
8359-
return;
8360-
need_free = TRUE;
8367+
/*
8368+
* Find the start of the commands.
8369+
* Expand <sfile> in it.
8370+
*/
8371+
if (*cmd != NUL)
8372+
{
8373+
cmd = expand_sfile(cmd);
8374+
if (cmd == NULL) /* some error */
8375+
return;
8376+
need_free = TRUE;
8377+
}
83618378
}
83628379

83638380
/*
@@ -8374,7 +8391,7 @@ do_autocmd(char_u *arg, int forceit)
83748391
*/
83758392
last_event = (event_T)-1; /* for listing the event name */
83768393
last_group = AUGROUP_ERROR; /* for listing the group name */
8377-
if (*arg == '*' || *arg == NUL)
8394+
if (*arg == '*' || *arg == NUL || *arg == '|')
83788395
{
83798396
for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
83808397
event = (event_T)((int)event + 1))
@@ -8384,7 +8401,7 @@ do_autocmd(char_u *arg, int forceit)
83848401
}
83858402
else
83868403
{
8387-
while (*arg && !vim_iswhite(*arg))
8404+
while (*arg && *arg != '|' && !vim_iswhite(*arg))
83888405
if (do_autocmd_event(event_name2nr(arg, &arg), pat,
83898406
nested, cmd, forceit, group) == FAIL)
83908407
break;
@@ -8409,7 +8426,8 @@ au_get_grouparg(char_u **argp)
84098426
char_u *arg = *argp;
84108427
int group = AUGROUP_ALL;
84118428

8412-
p = skiptowhite(arg);
8429+
for (p = arg; *p && !vim_iswhite(*p) && *p != '|'; ++p)
8430+
;
84138431
if (p > arg)
84148432
{
84158433
group_name = vim_strnsave(arg, (int)(p - arg));

src/testdir/test_autocmd.vim

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ if has('timers')
1919
call timer_start(100, 'ExitInsertMode')
2020
call feedkeys('a', 'x!')
2121
call assert_equal(1, g:triggered)
22+
au! CursorHoldI
2223
endfunc
2324

2425
func Test_cursorhold_insert_ctrl_x()
@@ -29,6 +30,7 @@ if has('timers')
2930
" CursorHoldI does not trigger after CTRL-X
3031
call feedkeys("a\<C-X>", 'x!')
3132
call assert_equal(0, g:triggered)
33+
au! CursorHoldI
3234
endfunc
3335
endif
3436

@@ -58,6 +60,7 @@ function Test_bufunload()
5860
bwipeout
5961
call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
6062

63+
au! test_bufunload_group
6164
augroup! test_bufunload_group
6265
endfunc
6366

@@ -120,3 +123,31 @@ func Test_win_tab_autocmd()
120123
augroup END
121124
unlet g:record
122125
endfunc
126+
127+
func s:AddAnAutocmd()
128+
augroup vimBarTest
129+
au BufReadCmd * echo 'hello'
130+
augroup END
131+
call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
132+
endfunc
133+
134+
func Test_early_bar()
135+
" test that a bar is recognized before the {event}
136+
call s:AddAnAutocmd()
137+
augroup vimBarTest | au! | augroup END
138+
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
139+
140+
call s:AddAnAutocmd()
141+
augroup vimBarTest| au!| augroup END
142+
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
143+
144+
" test that a bar is recognized after the {event}
145+
call s:AddAnAutocmd()
146+
augroup vimBarTest| au!BufReadCmd| augroup END
147+
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
148+
149+
" test that a bar is recognized after the {group}
150+
call s:AddAnAutocmd()
151+
au! vimBarTest|echo 'hello'
152+
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
153+
endfunc

src/version.c

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

759759
static int included_patches[] =
760760
{ /* Add new patch number below this line */
761+
/**/
762+
2103,
761763
/**/
762764
2102,
763765
/**/

0 commit comments

Comments
 (0)