vim-patch:8.1.1113: making an autocommand trigger once is not so easy

Problem:    Making an autocommand trigger once is not so easy.
Solution:   Add the ++once argument.  Also add ++nested as an alias for
            "nested". (Justin M. Keyes, closes vim/vim#4100)
eb93f3f0e2
This commit is contained in:
Justin M. Keyes 2019-04-06 23:28:05 +02:00
parent 052ced4954
commit 3cd9422c4c
3 changed files with 90 additions and 6 deletions

View File

@ -6061,17 +6061,30 @@ void do_autocmd(char_u *arg_in, int forceit)
for (size_t i = 0; i < 2; i++) {
if (*cmd != NUL) {
// Check for "++once" flag.
if (!once && STRNCMP(cmd, "++once", 6) == 0 && ascii_iswhite(cmd[6])) {
if (STRNCMP(cmd, "++once", 6) == 0 && ascii_iswhite(cmd[6])) {
if (once) {
EMSG2(_(e_duparg2), "++once");
}
once = true;
cmd = skipwhite(cmd + 6);
}
// Check for "++nested" flag.
if (!nested
&& ((STRNCMP(cmd, "++nested", 8) == 0 && ascii_iswhite(cmd[8]))
// Deprecated form (without "++").
|| (STRNCMP(cmd, "nested", 6) == 0 && ascii_iswhite(cmd[6])))) {
if ((STRNCMP(cmd, "++nested", 8) == 0 && ascii_iswhite(cmd[8]))) {
if (nested) {
EMSG2(_(e_duparg2), "++nested");
}
nested = true;
cmd = skipwhite(cmd + ('+' == cmd[0] ? 8 : 6));
cmd = skipwhite(cmd + 8);
}
// Check for the old (deprecated) "nested" flag.
if (STRNCMP(cmd, "nested", 6) == 0 && ascii_iswhite(cmd[6])) {
if (nested) {
EMSG2(_(e_duparg2), "nested");
}
nested = true;
cmd = skipwhite(cmd + 6);
}
}
}

View File

@ -931,6 +931,7 @@ EXTERN char_u e_interr[] INIT(= N_("Interrupted"));
EXTERN char_u e_invaddr[] INIT(= N_("E14: Invalid address"));
EXTERN char_u e_invarg[] INIT(= N_("E474: Invalid argument"));
EXTERN char_u e_invarg2[] INIT(= N_("E475: Invalid argument: %s"));
EXTERN char_u e_duparg2[] INIT(= N_("E983: Duplicate argument: %s"));
EXTERN char_u e_invexpr2[] INIT(= N_("E15: Invalid expression: %s"));
EXTERN char_u e_invrange[] INIT(= N_("E16: Invalid range"));
EXTERN char_u e_invcmd[] INIT(= N_("E476: Invalid command"));

View File

@ -1308,3 +1308,73 @@ func Test_Changed_FirstTime()
call delete('Xchanged.txt')
bwipe!
endfunc
func Test_autocmd_nested()
let g:did_nested = 0
augroup Testing
au WinNew * edit somefile
au BufNew * let g:did_nested = 1
augroup END
split
call assert_equal(0, g:did_nested)
close
bwipe! somefile
" old nested argument still works
augroup Testing
au!
au WinNew * nested edit somefile
au BufNew * let g:did_nested = 1
augroup END
split
call assert_equal(1, g:did_nested)
close
bwipe! somefile
" New ++nested argument works
augroup Testing
au!
au WinNew * ++nested edit somefile
au BufNew * let g:did_nested = 1
augroup END
split
call assert_equal(1, g:did_nested)
close
bwipe! somefile
augroup Testing
au!
augroup END
call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
call assert_fails('au WinNew * nested nested echo bad', 'E983:')
endfunc
func Test_autocmd_once()
" Without ++once WinNew triggers twice
let g:did_split = 0
augroup Testing
au WinNew * let g:did_split += 1
augroup END
split
split
call assert_equal(2, g:did_split)
call assert_true(exists('#WinNew'))
close
close
" With ++once WinNew triggers once
let g:did_split = 0
augroup Testing
au!
au WinNew * ++once let g:did_split += 1
augroup END
split
split
call assert_equal(1, g:did_split)
call assert_false(exists('#WinNew'))
close
close
call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
endfunc