# HG changeset patch # User Bram Moolenaar # Date 1598994903 -7200 # Node ID 71f886a48ef5d1e93f841c3857dd8ea1aa04a5a9 # Parent dd2ca50d7e0a880ea5beb0cbf0c1f6df9bc49196 patch 8.2.1562: Vim9: error when using "%" where a buffer is expected Commit: https://github.com/vim/vim/commit/3767e3a3302d745349eff8cfe45411f03e13de43 Author: Bram Moolenaar Date: Tue Sep 1 23:06:01 2020 +0200 patch 8.2.1562: Vim9: error when using "%" where a buffer is expected Problem: Vim9: error when using "%" where a buffer is expected. Solution: Add tv_get_buf_from_arg(). (closes https://github.com/vim/vim/issues/6814) diff --git a/src/evalbuffer.c b/src/evalbuffer.c --- a/src/evalbuffer.c +++ b/src/evalbuffer.c @@ -364,16 +364,7 @@ f_bufname(typval_T *argvars, typval_T *r 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 *ret 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 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 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 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 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) diff --git a/src/proto/typval.pro b/src/proto/typval.pro --- a/src/proto/typval.pro +++ b/src/proto/typval.pro @@ -29,4 +29,5 @@ int eval_env_var(char_u **arg, typval_T 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 : */ diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim --- a/src/testdir/test_vim9_func.vim +++ b/src/testdir/test_vim9_func.vim @@ -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 diff --git a/src/typval.c b/src/typval.c --- a/src/typval.c +++ b/src/typval.c @@ -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 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -755,6 +755,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1562, +/**/ 1561, /**/ 1560,