vim-patch:8.1.1924: using empty string for current buffer is unexpected

Problem:    Using empty string for current buffer is unexpected.
Solution:   Make the argument optional for bufname() and bufnr().
a8eee21e75
This commit is contained in:
Jan Edmund Lazo 2019-08-24 20:48:35 -04:00
parent 4fedef51b0
commit 66c06dad62
No known key found for this signature in database
GPG Key ID: 64915E6E9F735B15
4 changed files with 34 additions and 22 deletions

View File

@ -2023,8 +2023,8 @@ bufexists({expr}) Number |TRUE| if buffer {expr} exists
buflisted({expr}) Number |TRUE| if buffer {expr} is listed buflisted({expr}) Number |TRUE| if buffer {expr} is listed
bufload({expr}) Number load buffer {expr} if not loaded yet bufload({expr}) Number load buffer {expr} if not loaded yet
bufloaded({expr}) Number |TRUE| if buffer {expr} is loaded bufloaded({expr}) Number |TRUE| if buffer {expr} is loaded
bufname({expr}) String Name of the buffer {expr} bufname([{expr}]) String Name of the buffer {expr}
bufnr({expr} [, {create}]) Number Number of the buffer {expr} bufnr([{expr} [, {create}]]) Number Number of the buffer {expr}
bufwinid({expr}) Number |window-ID| of buffer {expr} bufwinid({expr}) Number |window-ID| of buffer {expr}
bufwinnr({expr}) Number window number of buffer {expr} bufwinnr({expr}) Number window number of buffer {expr}
byte2line({byte}) Number line number at byte count {byte} byte2line({byte}) Number line number at byte count {byte}
@ -2759,9 +2759,10 @@ bufloaded({expr}) *bufloaded()*
{expr} exists and is loaded (shown in a window or hidden). {expr} exists and is loaded (shown in a window or hidden).
The {expr} argument is used like with |bufexists()|. The {expr} argument is used like with |bufexists()|.
bufname({expr}) *bufname()* bufname([{expr}]) *bufname()*
The result is the name of a buffer, as it is displayed by the The result is the name of a buffer, as it is displayed by the
":ls" command. ":ls" command.
+ If {expr} is omitted the current buffer is used.
If {expr} is a Number, that buffer number's name is given. If {expr} is a Number, that buffer number's name is given.
Number zero is the alternate buffer for the current window. Number zero is the alternate buffer for the current window.
If {expr} is a String, it is used as a |file-pattern| to match If {expr} is a String, it is used as a |file-pattern| to match
@ -2788,7 +2789,7 @@ bufname({expr}) *bufname()*
bufname("file2") name of buffer where "file2" matches. bufname("file2") name of buffer where "file2" matches.
*bufnr()* *bufnr()*
bufnr({expr} [, {create}]) bufnr([{expr} [, {create}]])
The result is the number of a buffer, as it is displayed by The result is the number of a buffer, as it is displayed by
the ":ls" command. For the use of {expr}, see |bufname()| the ":ls" command. For the use of {expr}, see |bufname()|
above. above.
@ -2796,7 +2797,7 @@ bufnr({expr} [, {create}])
{create} argument is present and not zero, a new, unlisted, {create} argument is present and not zero, a new, unlisted,
buffer is created and its number is returned. buffer is created and its number is returned.
bufnr("$") is the last buffer: > bufnr("$") is the last buffer: >
:let last_buffer = bufnr("$") :let last_buffer = bufnr("$")
< The result is a Number, which is the highest buffer number < The result is a Number, which is the highest buffer number
of existing buffers. Note that not all buffers with a smaller of existing buffers. Note that not all buffers with a smaller
number necessarily exist, because ":bwipeout" may have removed number necessarily exist, because ":bwipeout" may have removed

View File

@ -7353,14 +7353,19 @@ static buf_T * get_buf_arg(typval_T *arg)
*/ */
static void f_bufname(typval_T *argvars, typval_T *rettv, FunPtr fptr) static void f_bufname(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{ {
const buf_T *buf;
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL; rettv->vval.v_string = NULL;
if (!tv_check_str_or_nr(&argvars[0])) { if (argvars[0].v_type == VAR_UNKNOWN) {
return; buf = curbuf;
} else {
if (!tv_check_str_or_nr(&argvars[0])) {
return;
}
emsg_off++;
buf = tv_get_buf(&argvars[0], false);
emsg_off--;
} }
emsg_off++;
const buf_T *const buf = tv_get_buf(&argvars[0], false);
emsg_off--;
if (buf != NULL && buf->b_fname != NULL) { if (buf != NULL && buf->b_fname != NULL) {
rettv->vval.v_string = (char_u *)xstrdup((char *)buf->b_fname); rettv->vval.v_string = (char_u *)xstrdup((char *)buf->b_fname);
} }
@ -7371,15 +7376,21 @@ static void f_bufname(typval_T *argvars, typval_T *rettv, FunPtr fptr)
*/ */
static void f_bufnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) static void f_bufnr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{ {
const buf_T *buf;
bool error = false; bool error = false;
rettv->vval.v_number = -1; rettv->vval.v_number = -1;
if (!tv_check_str_or_nr(&argvars[0])) {
return; if (argvars[0].v_type == VAR_UNKNOWN) {
buf = curbuf;
} else {
if (!tv_check_str_or_nr(&argvars[0])) {
return;
}
emsg_off++;
buf = tv_get_buf(&argvars[0], false);
emsg_off--;
} }
emsg_off++;
const buf_T *buf = tv_get_buf(&argvars[0], false);
emsg_off--;
// If the buffer isn't found and the second argument is not zero create a // If the buffer isn't found and the second argument is not zero create a
// new buffer. // new buffer.

View File

@ -44,13 +44,13 @@ return {
bufadd={args=1}, bufadd={args=1},
bufexists={args=1}, bufexists={args=1},
buffer_exists={args=1, func='f_bufexists'}, -- obsolete buffer_exists={args=1, func='f_bufexists'}, -- obsolete
buffer_name={args=1, func='f_bufname'}, -- obsolete buffer_name={args={0, 1}, func='f_bufname'}, -- obsolete
buffer_number={args=1, func='f_bufnr'}, -- obsolete buffer_number={args={0, 1}, func='f_bufnr'}, -- obsolete
buflisted={args=1}, buflisted={args=1},
bufload={args=1}, bufload={args=1},
bufloaded={args=1}, bufloaded={args=1},
bufname={args=1}, bufname={args={0, 1}},
bufnr={args={1, 2}}, bufnr={args={0, 2}},
bufwinid={args=1}, bufwinid={args=1},
bufwinnr={args=1}, bufwinnr={args=1},
byte2line={args=1}, byte2line={args=1},

View File

@ -379,10 +379,10 @@ func Test_argedit()
" make sure to use a new buffer number for x when it is loaded " make sure to use a new buffer number for x when it is loaded
bw! x bw! x
new new
let a = bufnr('') let a = bufnr()
argedit x argedit x
call assert_equal(a, bufnr('')) call assert_equal(a, bufnr())
call assert_equal('x', bufname('')) call assert_equal('x', bufname())
%argd %argd
bw! x bw! x
endfunc endfunc