]> granicus.if.org Git - vim/commitdiff
patch 8.2.1489: Vim9: error when setting an option with setbufvar() v8.2.1489
authorBram Moolenaar <Bram@vim.org>
Wed, 19 Aug 2020 19:20:49 +0000 (21:20 +0200)
committerBram Moolenaar <Bram@vim.org>
Wed, 19 Aug 2020 19:20:49 +0000 (21:20 +0200)
Problem:    Vim9: error when setting an option with setbufvar().
Solution:   Do not get a number from a string value. (closes #6740)

src/evalvars.c
src/testdir/test_vim9_func.vim
src/version.c

index f59dde9c69365314e33f002198bf8b15873c9ad1..bff6086e53781a2e0a5fbf2229dad7e5b4cb1620 100644 (file)
@@ -3292,6 +3292,24 @@ getwinvar(
     --emsg_off;
 }
 
+/*
+ * Set option "varname" to the value of "varp" for the current buffer/window.
+ */
+    static void
+set_option_from_tv(char_u *varname, typval_T *varp)
+{
+    long       numval = 0;
+    char_u     *strval;
+    char_u     nbuf[NUMBUFLEN];
+    int                error = FALSE;
+
+    if (!in_vim9script() || varp->v_type != VAR_STRING)
+       numval = (long)tv_get_number_chk(varp, &error);
+    strval = tv_get_string_buf_chk(varp, nbuf);
+    if (!error && strval != NULL)
+       set_option_value(varname, numval, strval, OPT_LOCAL);
+}
+
 /*
  * "setwinvar()" and "settabwinvar()" functions
  */
@@ -3304,7 +3322,6 @@ setwinvar(typval_T *argvars, int off)
     int                need_switch_win;
     char_u     *varname, *winvarname;
     typval_T   *varp;
-    char_u     nbuf[NUMBUFLEN];
     tabpage_T  *tp = NULL;
 
     if (check_secure())
@@ -3325,17 +3342,7 @@ setwinvar(typval_T *argvars, int off)
               || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
        {
            if (*varname == '&')
-           {
-               long    numval;
-               char_u  *strval;
-               int             error = FALSE;
-
-               ++varname;
-               numval = (long)tv_get_number_chk(varp, &error);
-               strval = tv_get_string_buf_chk(varp, nbuf);
-               if (!error && strval != NULL)
-                   set_option_value(varname, numval, strval, OPT_LOCAL);
-           }
+               set_option_from_tv(varname + 1, varp);
            else
            {
                winvarname = alloc(STRLEN(varname) + 3);
@@ -3759,7 +3766,6 @@ f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
     buf_T      *buf;
     char_u     *varname, *bufvarname;
     typval_T   *varp;
-    char_u     nbuf[NUMBUFLEN];
 
     if (check_secure())
        return;
@@ -3772,19 +3778,12 @@ f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
     {
        if (*varname == '&')
        {
-           long        numval;
-           char_u      *strval;
-           int         error = FALSE;
            aco_save_T  aco;
 
            // set curbuf to be our buf, temporarily
            aucmd_prepbuf(&aco, buf);
 
-           ++varname;
-           numval = (long)tv_get_number_chk(varp, &error);
-           strval = tv_get_string_buf_chk(varp, nbuf);
-           if (!error && strval != NULL)
-               set_option_value(varname, numval, strval, OPT_LOCAL);
+           set_option_from_tv(varname + 1, varp);
 
            // reset notion of buffer
            aucmd_restbuf(&aco);
index ad59844d541d80f34fba490679cf36f4aa7add0f..1f0fd1c8a3d72e3820a229ab530fed7c0e67e8f6 100644 (file)
@@ -1391,6 +1391,18 @@ def Test_readdir()
    eval expand('.')->readdirex({e -> e.name[0] !=# '.'})
 enddef
 
+def Test_setbufvar()
+   setbufvar(bufnr('%'), '&syntax', 'vim')
+   assert_equal('vim', &syntax)
+   setbufvar(bufnr('%'), '&ts', 16)
+   assert_equal(16, &ts)
+   settabwinvar(1, 1, '&syntax', 'vam')
+   assert_equal('vam', &syntax)
+   settabwinvar(1, 1, '&ts', 15)
+   assert_equal(15, &ts)
+   setlocal ts=8
+enddef
+
 def Fibonacci(n: number): number
   if n < 2
     return n
index d6d197303a148f60bd59536014769116a8bb710f..b281a4728b7d10faac62e7d121ba59d065bf75ba 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1489,
 /**/
     1488,
 /**/