Skip to content

Commit 191929b

Browse files
committed
patch 8.2.1489: Vim9: error when setting an option with setbufvar()
Problem: Vim9: error when setting an option with setbufvar(). Solution: Do not get a number from a string value. (closes #6740)
1 parent 9dc1917 commit 191929b

3 files changed

Lines changed: 34 additions & 21 deletions

File tree

src/evalvars.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,6 +3292,24 @@ getwinvar(
32923292
--emsg_off;
32933293
}
32943294

3295+
/*
3296+
* Set option "varname" to the value of "varp" for the current buffer/window.
3297+
*/
3298+
static void
3299+
set_option_from_tv(char_u *varname, typval_T *varp)
3300+
{
3301+
long numval = 0;
3302+
char_u *strval;
3303+
char_u nbuf[NUMBUFLEN];
3304+
int error = FALSE;
3305+
3306+
if (!in_vim9script() || varp->v_type != VAR_STRING)
3307+
numval = (long)tv_get_number_chk(varp, &error);
3308+
strval = tv_get_string_buf_chk(varp, nbuf);
3309+
if (!error && strval != NULL)
3310+
set_option_value(varname, numval, strval, OPT_LOCAL);
3311+
}
3312+
32953313
/*
32963314
* "setwinvar()" and "settabwinvar()" functions
32973315
*/
@@ -3304,7 +3322,6 @@ setwinvar(typval_T *argvars, int off)
33043322
int need_switch_win;
33053323
char_u *varname, *winvarname;
33063324
typval_T *varp;
3307-
char_u nbuf[NUMBUFLEN];
33083325
tabpage_T *tp = NULL;
33093326

33103327
if (check_secure())
@@ -3325,17 +3342,7 @@ setwinvar(typval_T *argvars, int off)
33253342
|| switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
33263343
{
33273344
if (*varname == '&')
3328-
{
3329-
long numval;
3330-
char_u *strval;
3331-
int error = FALSE;
3332-
3333-
++varname;
3334-
numval = (long)tv_get_number_chk(varp, &error);
3335-
strval = tv_get_string_buf_chk(varp, nbuf);
3336-
if (!error && strval != NULL)
3337-
set_option_value(varname, numval, strval, OPT_LOCAL);
3338-
}
3345+
set_option_from_tv(varname + 1, varp);
33393346
else
33403347
{
33413348
winvarname = alloc(STRLEN(varname) + 3);
@@ -3759,7 +3766,6 @@ f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
37593766
buf_T *buf;
37603767
char_u *varname, *bufvarname;
37613768
typval_T *varp;
3762-
char_u nbuf[NUMBUFLEN];
37633769

37643770
if (check_secure())
37653771
return;
@@ -3772,19 +3778,12 @@ f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
37723778
{
37733779
if (*varname == '&')
37743780
{
3775-
long numval;
3776-
char_u *strval;
3777-
int error = FALSE;
37783781
aco_save_T aco;
37793782

37803783
// set curbuf to be our buf, temporarily
37813784
aucmd_prepbuf(&aco, buf);
37823785

3783-
++varname;
3784-
numval = (long)tv_get_number_chk(varp, &error);
3785-
strval = tv_get_string_buf_chk(varp, nbuf);
3786-
if (!error && strval != NULL)
3787-
set_option_value(varname, numval, strval, OPT_LOCAL);
3786+
set_option_from_tv(varname + 1, varp);
37883787

37893788
// reset notion of buffer
37903789
aucmd_restbuf(&aco);

src/testdir/test_vim9_func.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,18 @@ def Test_readdir()
13911391
eval expand('.')->readdirex({e -> e.name[0] !=# '.'})
13921392
enddef
13931393

1394+
def Test_setbufvar()
1395+
setbufvar(bufnr('%'), '&syntax', 'vim')
1396+
assert_equal('vim', &syntax)
1397+
setbufvar(bufnr('%'), '&ts', 16)
1398+
assert_equal(16, &ts)
1399+
settabwinvar(1, 1, '&syntax', 'vam')
1400+
assert_equal('vam', &syntax)
1401+
settabwinvar(1, 1, '&ts', 15)
1402+
assert_equal(15, &ts)
1403+
setlocal ts=8
1404+
enddef
1405+
13941406
def Fibonacci(n: number): number
13951407
if n < 2
13961408
return n

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+
1489,
757759
/**/
758760
1488,
759761
/**/

0 commit comments

Comments
 (0)