]> granicus.if.org Git - vim/commitdiff
patch 8.2.1562: Vim9: error when using "%" where a buffer is expected v8.2.1562
authorBram Moolenaar <Bram@vim.org>
Tue, 1 Sep 2020 21:06:01 +0000 (23:06 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 1 Sep 2020 21:06:01 +0000 (23:06 +0200)
Problem:    Vim9: error when using "%" where a buffer is expected.
Solution:   Add tv_get_buf_from_arg(). (closes #6814)

src/evalbuffer.c
src/proto/typval.pro
src/testdir/test_vim9_func.vim
src/typval.c
src/version.c

index 5cf884a80cea668a53fe6b44308a65d2c34978d1..300441551b60504af0a00ca69cc5b478f06e5d98 100644 (file)
@@ -364,16 +364,7 @@ f_bufname(typval_T *argvars, typval_T *rettv)
     if (tv->v_type == VAR_UNKNOWN)
        buf = curbuf;
     else
-    {
-       ++emsg_off;
-       buf = tv_get_buf(tv, FALSE);
-       --emsg_off;
-       if (buf == NULL
-               && tv->v_type != VAR_NUMBER
-               && tv->v_type != VAR_STRING)
-           // issue errmsg for type error
-           (void)tv_get_number(tv);
-    }
+       buf = tv_get_buf_from_arg(tv);
     rettv->v_type = VAR_STRING;
     if (buf != NULL && buf->b_fname != NULL)
        rettv->vval.v_string = vim_strsave(buf->b_fname);
@@ -394,13 +385,7 @@ f_bufnr(typval_T *argvars, typval_T *rettv)
     if (argvars[0].v_type == VAR_UNKNOWN)
        buf = curbuf;
     else
-    {
-       if (argvars[0].v_type != VAR_STRING)
-           (void)tv_get_number(&argvars[0]);    // issue errmsg if type error
-       ++emsg_off;
-       buf = tv_get_buf(&argvars[0], FALSE);
-       --emsg_off;
-    }
+       buf = tv_get_buf_from_arg(&argvars[0]);
 
     // If the buffer isn't found and the second argument is not zero create a
     // new buffer.
@@ -425,9 +410,7 @@ buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
     int                winnr = 0;
     buf_T      *buf;
 
-    (void)tv_get_number(&argvars[0]);      // issue errmsg if type error
-    ++emsg_off;
-    buf = tv_get_buf(&argvars[0], TRUE);
+    buf = tv_get_buf_from_arg(&argvars[0]);
     FOR_ALL_WINDOWS(wp)
     {
        ++winnr;
@@ -435,7 +418,6 @@ buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
            break;
     }
     rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
-    --emsg_off;
 }
 
 /*
@@ -662,10 +644,7 @@ f_getbufinfo(typval_T *argvars, typval_T *rettv)
     else if (argvars[0].v_type != VAR_UNKNOWN)
     {
        // Information about one buffer.  Argument specifies the buffer
-       (void)tv_get_number(&argvars[0]);   // issue errmsg if type error
-       ++emsg_off;
-       argbuf = tv_get_buf(&argvars[0], FALSE);
-       --emsg_off;
+       argbuf = tv_get_buf_from_arg(&argvars[0]);
        if (argbuf == NULL)
            return;
     }
@@ -752,10 +731,7 @@ f_getbufline(typval_T *argvars, typval_T *rettv)
     linenr_T   end;
     buf_T      *buf;
 
-    (void)tv_get_number(&argvars[0]);      // issue errmsg if type error
-    ++emsg_off;
-    buf = tv_get_buf(&argvars[0], FALSE);
-    --emsg_off;
+    buf = tv_get_buf_from_arg(&argvars[0]);
 
     lnum = tv_get_lnum_buf(&argvars[1], buf);
     if (argvars[2].v_type == VAR_UNKNOWN)
index 25ad3e6737612e758ae63ad504c56892683e2410..9c57226da2c06d8b517331736737d792b3cfbbe5 100644 (file)
@@ -29,4 +29,5 @@ int eval_env_var(char_u **arg, typval_T *rettv, int evaluate);
 linenr_T tv_get_lnum(typval_T *argvars);
 linenr_T tv_get_lnum_buf(typval_T *argvars, buf_T *buf);
 buf_T *tv_get_buf(typval_T *tv, int curtab_only);
+buf_T *tv_get_buf_from_arg(typval_T *tv);
 /* vim: set ft=c : */
index 282d8dcec249ab53ea1a3040967c1f1094d740c7..0575836443bc28946cd5ebc48acbba15dcc1808d 100644 (file)
@@ -1443,6 +1443,11 @@ def Test_bufname()
   close
 enddef
 
+def Test_gebufinfo()
+  let bufinfo = getbufinfo(bufnr())
+  assert_equal(bufinfo, getbufinfo('%'))
+enddef
+
 def Fibonacci(n: number): number
   if n < 2
     return n
index d1732f2b7c0ca26f16783a01d1203223788b356c..c7cfd411672011ee169a2bd73c33c8233f9d8a75 100644 (file)
@@ -1562,4 +1562,23 @@ tv_get_buf(typval_T *tv, int curtab_only)
     return buf;
 }
 
+/*
+ * Like tv_get_buf() but give an error message is the type is wrong.
+ */
+    buf_T *
+tv_get_buf_from_arg(typval_T *tv)
+{
+    buf_T *buf;
+
+    ++emsg_off;
+    buf = tv_get_buf(tv, FALSE);
+    --emsg_off;
+    if (buf == NULL
+           && tv->v_type != VAR_NUMBER
+           && tv->v_type != VAR_STRING)
+       // issue errmsg for type error
+       (void)tv_get_number(tv);
+    return buf;
+}
+
 #endif // FEAT_EVAL
index 80c39982d312bd63d19c5441734186d853b16b15..f2d89f67287490c625f2c532a3451bc2ca6fab6e 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1562,
 /**/
     1561,
 /**/