Skip to content

Commit 2e80095

Browse files
committed
patch 8.2.1518: Vim9: cannot assign to local option
Problem: Vim9: cannot assign to local option. Solution: Skip over "&l:" and "&g:". (closes #6749)
1 parent 6c53fca commit 2e80095

6 files changed

Lines changed: 51 additions & 18 deletions

File tree

src/ex_docmd.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,6 +3242,27 @@ append_command(char_u *cmd)
32423242
*d = NUL;
32433243
}
32443244

3245+
/*
3246+
* If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3247+
* the name. Otherwise just return "start".
3248+
*/
3249+
char_u *
3250+
skip_option_env_lead(char_u *start)
3251+
{
3252+
char_u *name = start;
3253+
3254+
if (*start == '&')
3255+
{
3256+
if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3257+
name += 3;
3258+
else
3259+
name += 1;
3260+
}
3261+
else if (*start == '$')
3262+
name += 1;
3263+
return name;
3264+
}
3265+
32453266
/*
32463267
* Find an Ex command by its name, either built-in or user.
32473268
* Start of the name can be found at eap->cmd.
@@ -3273,9 +3294,7 @@ find_ex_command(
32733294
p = eap->cmd;
32743295
if (lookup != NULL)
32753296
{
3276-
// Skip over first char for "&opt = val", "$ENV = val" and "@r = val".
3277-
char_u *pskip = (*eap->cmd == '&' || *eap->cmd == '$')
3278-
? eap->cmd + 1 : eap->cmd;
3297+
char_u *pskip = skip_option_env_lead(eap->cmd);
32793298

32803299
if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
32813300
|| ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))

src/proto/ex_docmd.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ int parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only);
1010
void undo_cmdmod(exarg_T *eap, int save_msg_scroll);
1111
int parse_cmd_address(exarg_T *eap, char **errormsg, int silent);
1212
int checkforcmd(char_u **pp, char *cmd, int len);
13+
char_u *skip_option_env_lead(char_u *start);
1314
char_u *find_ex_command(exarg_T *eap, int *full, void *(*lookup)(char_u *, size_t, cctx_T *), cctx_T *cctx);
1415
int modifier_len(char_u *cmd);
1516
int cmd_exists(char_u *name);

src/testdir/test_vim9_script.vim

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,21 @@ def Test_assignment()
110110
endif
111111

112112
lines =<< trim END
113-
vim9script
114113
&ts = 6
115114
&ts += 3
116115
assert_equal(9, &ts)
116+
117+
&l:ts = 6
118+
assert_equal(6, &ts)
119+
&l:ts += 2
120+
assert_equal(8, &ts)
121+
122+
&g:ts = 6
123+
assert_equal(6, &g:ts)
124+
&g:ts += 2
125+
assert_equal(8, &g:ts)
117126
END
118-
CheckScriptSuccess(lines)
127+
CheckDefAndScriptSuccess(lines)
119128

120129
CheckDefFailure(['&notex += 3'], 'E113:')
121130
CheckDefFailure(['&ts ..= "xxx"'], 'E1019:')
@@ -163,19 +172,15 @@ def Test_assignment()
163172
call CheckDefFailure(['$SOME_ENV_VAR += "more"'], 'E1051:')
164173
call CheckDefFailure(['$SOME_ENV_VAR += 123'], 'E1012:')
165174

166-
@a = 'areg'
167-
@a ..= 'add'
168-
assert_equal('aregadd', @a)
169-
call CheckDefFailure(['@a += "more"'], 'E1051:')
170-
call CheckDefFailure(['@a += 123'], 'E1012:')
171-
172175
lines =<< trim END
173-
vim9script
174176
@c = 'areg'
175177
@c ..= 'add'
176178
assert_equal('aregadd', @c)
177179
END
178-
call CheckScriptSuccess(lines)
180+
CheckDefAndScriptSuccess(lines)
181+
182+
call CheckDefFailure(['@a += "more"'], 'E1051:')
183+
call CheckDefFailure(['@a += 123'], 'E1012:')
179184

180185
v:errmsg = 'none'
181186
v:errmsg ..= 'again'

src/testdir/vim9.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def CheckScriptSuccess(lines: list<string>)
4141
delete('Xdef')
4242
enddef
4343

44+
def CheckDefAndScriptSuccess(lines: list<string>)
45+
CheckDefSuccess(lines)
46+
CheckScriptSuccess(['vim9script'] + lines)
47+
enddef
48+
4449
" Check that a command fails both when used in a :def function and when used
4550
" in Vim9 script.
4651
def CheckScriptAndDefFailure(lines: list<string>, error: string, lnum = -3)

src/version.c

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

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1518,
757759
/**/
758760
1517,
759761
/**/

src/vim9compile.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,8 +4550,8 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
45504550
p = var_start + 2;
45514551
else
45524552
{
4553-
p = (*var_start == '&' || *var_start == '$')
4554-
? var_start + 1 : var_start;
4553+
// skip over the leading "&", "&l:", "&g:" and "$"
4554+
p = skip_option_env_lead(var_start);
45554555
p = to_name_end(p, TRUE);
45564556
}
45574557

@@ -4595,8 +4595,8 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
45954595
}
45964596
cc = *p;
45974597
*p = NUL;
4598-
opt_type = get_option_value(var_start + 1, &numval,
4599-
NULL, opt_flags);
4598+
opt_type = get_option_value(skip_option_env_lead(var_start),
4599+
&numval, NULL, opt_flags);
46004600
*p = cc;
46014601
if (opt_type == -3)
46024602
{
@@ -5131,7 +5131,8 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
51315131
switch (dest)
51325132
{
51335133
case dest_option:
5134-
generate_STOREOPT(cctx, name + 1, opt_flags);
5134+
generate_STOREOPT(cctx, skip_option_env_lead(name),
5135+
opt_flags);
51355136
break;
51365137
case dest_global:
51375138
// include g: with the name, easier to execute that way

0 commit comments

Comments
 (0)