1
0
forked from aniani/vim

patch 8.2.3268: cannot use a block with :autocmd like with :command

Problem:    Cannot use a block with :autocmd like with :command.
Solution:   Add support for a {} block after :autocmd. (closes #8620)
This commit is contained in:
Bram Moolenaar
2021-08-01 14:52:32 +02:00
parent 6db660bed9
commit 73b8b0ae3a
10 changed files with 100 additions and 50 deletions

View File

@@ -114,9 +114,6 @@ static struct
{ADDR_NONE, NULL, NULL}
};
#define UC_BUFFER 1 // -buffer: local to current buffer
#define UC_VIM9 2 // {} argument: Vim9 syntax.
/*
* Search for a user command that matches "eap->cmd".
* Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
@@ -974,6 +971,49 @@ fail:
return FAIL;
}
/*
* If "p" starts with "{" then read a block of commands until "}".
* Used for ":command" and ":autocmd".
*/
char_u *
may_get_cmd_block(exarg_T *eap, char_u *p, char_u **tofree, int *flags)
{
char_u *retp = p;
if (*p == '{' && ends_excmd2(eap->arg, skipwhite(p + 1))
&& eap->getline != NULL)
{
garray_T ga;
char_u *line = NULL;
ga_init2(&ga, sizeof(char_u *), 10);
if (ga_add_string(&ga, p) == FAIL)
return retp;
// Read lines between '{' and '}'. Does not support nesting or
// here-doc constructs.
for (;;)
{
vim_free(line);
if ((line = eap->getline(':', eap->cookie,
0, GETLINE_CONCAT_CONTBAR)) == NULL)
{
emsg(_(e_missing_rcurly));
break;
}
if (ga_add_string(&ga, line) == FAIL)
break;
if (*skipwhite(line) == '}')
break;
}
vim_free(line);
retp = *tofree = ga_concat_strings(&ga, "\n");
ga_clear_strings(&ga);
*flags |= UC_VIM9;
}
return retp;
}
/*
* ":command ..." implementation
*/
@@ -1043,38 +1083,7 @@ ex_command(exarg_T *eap)
{
char_u *tofree = NULL;
if (*p == '{' && ends_excmd2(eap->arg, skipwhite(p + 1))
&& eap->getline != NULL)
{
garray_T ga;
char_u *line = NULL;
ga_init2(&ga, sizeof(char_u *), 10);
if (ga_add_string(&ga, p) == FAIL)
return;
// Read lines between '{' and '}'. Does not support nesting or
// here-doc constructs.
//
for (;;)
{
vim_free(line);
if ((line = eap->getline(':', eap->cookie,
0, GETLINE_CONCAT_CONTBAR)) == NULL)
{
emsg(_(e_missing_rcurly));
break;
}
if (ga_add_string(&ga, line) == FAIL)
break;
if (*skipwhite(line) == '}')
break;
}
vim_free(line);
p = tofree = ga_concat_strings(&ga, "\n");
ga_clear_strings(&ga);
flags |= UC_VIM9;
}
p = may_get_cmd_block(eap, p, &tofree, &flags);
uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
addr_type_arg, eap->forceit);