summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2019-08-24 22:14:58 +0200
committerBram Moolenaar <Bram@vim.org>2019-08-24 22:14:58 +0200
commita8eee21e75324d199acb1663cb5009e03014a13a (patch)
tree7214b321e35fa081b9be19cd5373731bca478ca8
parent4119309d70c7fc32637e77f2e84c185ad1768892 (diff)
downloadvim-git-a8eee21e75324d199acb1663cb5009e03014a13a.tar.gz
patch 8.1.1924: using empty string for current buffer is unexpectedv8.1.1924
Problem: Using empty string for current buffer is unexpected. Solution: Make the argument optional for bufname() and bufnr().
-rw-r--r--runtime/doc/eval.txt15
-rw-r--r--src/evalfunc.c38
-rw-r--r--src/testdir/test_arglist.vim6
-rw-r--r--src/version.c2
4 files changed, 37 insertions, 24 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index 12e4d0920..9a00dd329 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -2335,8 +2335,8 @@ bufexists({expr}) Number |TRUE| if buffer {expr} exists
buflisted({expr}) Number |TRUE| if buffer {expr} is listed
bufload({expr}) Number load buffer {expr} if not loaded yet
bufloaded({expr}) Number |TRUE| if buffer {expr} is loaded
-bufname({expr}) String Name of the buffer {expr}
-bufnr({expr} [, {create}]) Number Number of the buffer {expr}
+bufname([{expr}]) String Name of the buffer {expr}
+bufnr([{expr} [, {create}]]) Number Number of the buffer {expr}
bufwinid({expr}) Number window ID of buffer {expr}
bufwinnr({expr}) Number window number of buffer {expr}
byte2line({byte}) Number line number at byte count {byte}
@@ -3217,9 +3217,10 @@ bufloaded({expr}) *bufloaded()*
Can also be used as a |method|: >
let loaded = 'somename'->bufloaded()
-bufname({expr}) *bufname()*
+bufname([{expr}]) *bufname()*
The result is the name of a buffer, as it is displayed by the
":ls" command.
+ If {expr} is omitted the current buffer is used.
If {expr} is a Number, that buffer number's name is given.
Number zero is the alternate buffer for the current window.
If {expr} is a String, it is used as a |file-pattern| to match
@@ -3251,7 +3252,7 @@ bufname({expr}) *bufname()*
Obsolete name: buffer_name().
*bufnr()*
-bufnr({expr} [, {create}])
+bufnr([{expr} [, {create}]])
The result is the number of a buffer, as it is displayed by
the ":ls" command. For the use of {expr}, see |bufname()|
above.
@@ -3259,7 +3260,7 @@ bufnr({expr} [, {create}])
{create} argument is present and not zero, a new, unlisted,
buffer is created and its number is returned.
bufnr("$") is the last buffer: >
- :let last_buffer = bufnr("$")
+ :let last_buffer = bufnr("$")
< The result is a Number, which is the highest buffer number
of existing buffers. Note that not all buffers with a smaller
number necessarily exist, because ":bwipeout" may have removed
@@ -7201,7 +7202,7 @@ prompt_setcallback({buf}, {expr}) *prompt_setcallback()*
that was entered at the prompt. This can be an empty string
if the user only typed Enter.
Example: >
- call prompt_setcallback(bufnr(''), function('s:TextEntered'))
+ call prompt_setcallback(bufnr(), function('s:TextEntered'))
func s:TextEntered(text)
if a:text == 'exit' || a:text == 'quit'
stopinsert
@@ -7227,7 +7228,7 @@ prompt_setprompt({buf}, {text}) *prompt_setprompt()*
{text} to end in a space.
The result is only visible if {buf} has 'buftype' set to
"prompt". Example: >
- call prompt_setprompt(bufnr(''), 'command: ')
+ call prompt_setprompt(bufnr(), 'command: ')
<
prop_ functions are documented here: |text-prop-functions|.
diff --git a/src/evalfunc.c b/src/evalfunc.c
index c5ec21dd6..07e5993de 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -457,13 +457,13 @@ static funcentry_T global_functions[] =
{"bufadd", 1, 1, FEARG_1, f_bufadd},
{"bufexists", 1, 1, FEARG_1, f_bufexists},
{"buffer_exists", 1, 1, FEARG_1, f_bufexists}, // obsolete
- {"buffer_name", 1, 1, 0, f_bufname}, // obsolete
- {"buffer_number", 1, 1, 0, f_bufnr}, // obsolete
+ {"buffer_name", 0, 1, FEARG_1, f_bufname}, // obsolete
+ {"buffer_number", 0, 1, FEARG_1, f_bufnr}, // obsolete
{"buflisted", 1, 1, FEARG_1, f_buflisted},
{"bufload", 1, 1, FEARG_1, f_bufload},
{"bufloaded", 1, 1, FEARG_1, f_bufloaded},
- {"bufname", 1, 1, FEARG_1, f_bufname},
- {"bufnr", 1, 2, FEARG_1, f_bufnr},
+ {"bufname", 0, 1, FEARG_1, f_bufname},
+ {"bufnr", 0, 2, FEARG_1, f_bufnr},
{"bufwinid", 1, 1, FEARG_1, f_bufwinid},
{"bufwinnr", 1, 1, FEARG_1, f_bufwinnr},
{"byte2line", 1, 1, FEARG_1, f_byte2line},
@@ -1820,15 +1820,20 @@ f_bufname(typval_T *argvars, typval_T *rettv)
{
buf_T *buf;
- (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
- ++emsg_off;
- buf = tv_get_buf(&argvars[0], FALSE);
+ if (argvars[0].v_type == VAR_UNKNOWN)
+ buf = curbuf;
+ else
+ {
+ (void)tv_get_number(&argvars[0]); // issue errmsg if type error
+ ++emsg_off;
+ buf = tv_get_buf(&argvars[0], FALSE);
+ --emsg_off;
+ }
rettv->v_type = VAR_STRING;
if (buf != NULL && buf->b_fname != NULL)
rettv->vval.v_string = vim_strsave(buf->b_fname);
else
rettv->vval.v_string = NULL;
- --emsg_off;
}
/*
@@ -1841,13 +1846,18 @@ f_bufnr(typval_T *argvars, typval_T *rettv)
int error = FALSE;
char_u *name;
- (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
- ++emsg_off;
- buf = tv_get_buf(&argvars[0], FALSE);
- --emsg_off;
+ if (argvars[0].v_type == VAR_UNKNOWN)
+ buf = curbuf;
+ else
+ {
+ (void)tv_get_number(&argvars[0]); // issue errmsg if type error
+ ++emsg_off;
+ buf = tv_get_buf(&argvars[0], FALSE);
+ --emsg_off;
+ }
- /* If the buffer isn't found and the second argument is not zero create a
- * new buffer. */
+ // If the buffer isn't found and the second argument is not zero create a
+ // new buffer.
if (buf == NULL
&& argvars[1].v_type != VAR_UNKNOWN
&& tv_get_number_chk(&argvars[1], &error) != 0
diff --git a/src/testdir/test_arglist.vim b/src/testdir/test_arglist.vim
index a382a1a54..daed3d421 100644
--- a/src/testdir/test_arglist.vim
+++ b/src/testdir/test_arglist.vim
@@ -398,10 +398,10 @@ func Test_argedit()
" make sure to use a new buffer number for x when it is loaded
bw! x
new
- let a = bufnr('')
+ let a = bufnr()
argedit x
- call assert_equal(a, bufnr(''))
- call assert_equal('x', bufname(''))
+ call assert_equal(a, bufnr())
+ call assert_equal('x', bufname())
%argd
bw! x
endfunc
diff --git a/src/version.c b/src/version.c
index 8ae3dda68..cab79da1f 100644
--- a/src/version.c
+++ b/src/version.c
@@ -762,6 +762,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1924,
+/**/
1923,
/**/
1922,