1
0
forked from aniani/vim

patch 8.1.1888: more functions can be used as methods

Problem:    More functions can be used as methods.
Solution:   Make various functions usable as a method.
This commit is contained in:
Bram Moolenaar
2019-08-18 23:01:56 +02:00
parent 0a52df50a0
commit 073e4b92e6
9 changed files with 79 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
*eval.txt* For Vim version 8.1. Last change: 2019 Aug 17 *eval.txt* For Vim version 8.1. Last change: 2019 Aug 18
VIM REFERENCE MANUAL by Bram Moolenaar VIM REFERENCE MANUAL by Bram Moolenaar
@@ -2937,6 +2937,8 @@ and({expr}, {expr}) *and()*
to a number. A List, Dict or Float argument causes an error. to a number. A List, Dict or Float argument causes an error.
Example: > Example: >
:let flag = and(bits, 0x80) :let flag = and(bits, 0x80)
< Can also be used as a |method|: >
:let flag = bits->and(0x80)
append({lnum}, {text}) *append()* append({lnum}, {text}) *append()*
@@ -3088,6 +3090,8 @@ balloon_show({expr}) *balloon_show()*
func BalloonCallback(result) func BalloonCallback(result)
call balloon_show(a:result) call balloon_show(a:result)
endfunc endfunc
< Can also be used as a |method|: >
GetText()->balloon_show()
< <
The intended use is that fetching the content of the balloon The intended use is that fetching the content of the balloon
is initiated from 'balloonexpr'. It will invoke an is initiated from 'balloonexpr'. It will invoke an
@@ -3105,7 +3109,10 @@ balloon_split({msg}) *balloon_split()*
splits are made for the current window size and optimize to splits are made for the current window size and optimize to
show debugger output. show debugger output.
Returns a |List| with the split lines. Returns a |List| with the split lines.
{only available when compiled with the |+balloon_eval_term| Can also be used as a |method|: >
GetText()->balloon_split()->balloon_show()
< {only available when compiled with the |+balloon_eval_term|
feature} feature}
*browse()* *browse()*
@@ -3117,8 +3124,8 @@ browse({save}, {title}, {initdir}, {default})
{title} title for the requester {title} title for the requester
{initdir} directory to start browsing in {initdir} directory to start browsing in
{default} default file name {default} default file name
When the "Cancel" button is hit, something went wrong, or An empty string is returned when the "Cancel" button is hit,
browsing is not possible, an empty string is returned. something went wrong, or browsing is not possible.
*browsedir()* *browsedir()*
browsedir({title}, {initdir}) browsedir({title}, {initdir})
@@ -3144,6 +3151,8 @@ bufadd({name}) *bufadd()*
let bufnr = bufadd('someName') let bufnr = bufadd('someName')
call bufload(bufnr) call bufload(bufnr)
call setbufline(bufnr, 1, ['some', 'text']) call setbufline(bufnr, 1, ['some', 'text'])
< Can also be used as a |method|: >
let bufnr = 'somename'->bufadd()
bufexists({expr}) *bufexists()* bufexists({expr}) *bufexists()*
The result is a Number, which is |TRUE| if a buffer called The result is a Number, which is |TRUE| if a buffer called
@@ -3166,14 +3175,20 @@ bufexists({expr}) *bufexists()*
for MS-Windows 8.3 names in the form "c:\DOCUME~1" for MS-Windows 8.3 names in the form "c:\DOCUME~1"
Use "bufexists(0)" to test for the existence of an alternate Use "bufexists(0)" to test for the existence of an alternate
file name. file name.
*buffer_exists()*
Obsolete name: buffer_exists(). Can also be used as a |method|: >
let exists = 'somename'->bufexists()
<
Obsolete name: buffer_exists(). *buffer_exists()*
buflisted({expr}) *buflisted()* buflisted({expr}) *buflisted()*
The result is a Number, which is |TRUE| if a buffer called The result is a Number, which is |TRUE| if a buffer called
{expr} exists and is listed (has the 'buflisted' option set). {expr} exists and is listed (has the 'buflisted' option set).
The {expr} argument is used like with |bufexists()|. The {expr} argument is used like with |bufexists()|.
Can also be used as a |method|: >
let listed = 'somename'->buflisted()
bufload({expr}) *bufload()* bufload({expr}) *bufload()*
Ensure the buffer {expr} is loaded. When the buffer name Ensure the buffer {expr} is loaded. When the buffer name
refers to an existing file then the file is read. Otherwise refers to an existing file then the file is read. Otherwise
@@ -3183,11 +3198,17 @@ bufload({expr}) *bufload()*
there will be no dialog, the buffer will be loaded anyway. there will be no dialog, the buffer will be loaded anyway.
The {expr} argument is used like with |bufexists()|. The {expr} argument is used like with |bufexists()|.
Can also be used as a |method|: >
eval 'somename'->bufload()
bufloaded({expr}) *bufloaded()* bufloaded({expr}) *bufloaded()*
The result is a Number, which is |TRUE| if a buffer called The result is a Number, which is |TRUE| if a buffer called
{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()|.
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 The result is the name of a buffer, as it is displayed by the
":ls" command. ":ls" command.
@@ -3209,6 +3230,9 @@ bufname({expr}) *bufname()*
If the {expr} is a String, but you want to use it as a buffer If the {expr} is a String, but you want to use it as a buffer
number, force it to be a Number by adding zero to it: > number, force it to be a Number by adding zero to it: >
:echo bufname("3" + 0) :echo bufname("3" + 0)
< Can also be used as a |method|: >
echo bufnr->bufname()
< If the buffer doesn't exist, or doesn't have a name, an empty < If the buffer doesn't exist, or doesn't have a name, an empty
string is returned. > string is returned. >
bufname("#") alternate buffer name bufname("#") alternate buffer name
@@ -3232,8 +3256,11 @@ bufnr({expr} [, {create}])
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
them. Use bufexists() to test for the existence of a buffer. them. Use bufexists() to test for the existence of a buffer.
*buffer_number()*
Obsolete name: buffer_number(). Can also be used as a |method|: >
echo bufref->bufnr()
<
Obsolete name: buffer_number(). *buffer_number()*
*last_buffer_nr()* *last_buffer_nr()*
Obsolete name for bufnr("$"): last_buffer_nr(). Obsolete name for bufnr("$"): last_buffer_nr().
@@ -5834,6 +5861,8 @@ invert({expr}) *invert()*
Bitwise invert. The argument is converted to a number. A Bitwise invert. The argument is converted to a number. A
List, Dict or Float argument causes an error. Example: > List, Dict or Float argument causes an error. Example: >
:let bits = invert(bits) :let bits = invert(bits)
< Can also be used as a |method|: >
:let bits = bits->invert()
isdirectory({directory}) *isdirectory()* isdirectory({directory}) *isdirectory()*
The result is a Number, which is |TRUE| when a directory The result is a Number, which is |TRUE| when a directory
@@ -6762,11 +6791,14 @@ nr2char({expr} [, {utf8}]) *nr2char()*
let str = join(map(list, {_, val -> nr2char(val)}), '') let str = join(map(list, {_, val -> nr2char(val)}), '')
< Result: "ABC" < Result: "ABC"
or({expr}, {expr}) *or()* or({expr}, {expr}) *or()*
Bitwise OR on the two arguments. The arguments are converted Bitwise OR on the two arguments. The arguments are converted
to a number. A List, Dict or Float argument causes an error. to a number. A List, Dict or Float argument causes an error.
Example: > Example: >
:let bits = or(bits, 0x80) :let bits = or(bits, 0x80)
< Can also be used as a |method|: >
:let bits = bits->or(0x80)
pathshorten({expr}) *pathshorten()* pathshorten({expr}) *pathshorten()*
@@ -9689,6 +9721,8 @@ xor({expr}, {expr}) *xor()*
to a number. A List, Dict or Float argument causes an error. to a number. A List, Dict or Float argument causes an error.
Example: > Example: >
:let bits = xor(bits, 0x80) :let bits = xor(bits, 0x80)
< Can also be used as a |method|: >
:let bits = bits->xor(0x80)
< <

View File

@@ -419,7 +419,7 @@ static funcentry_T global_functions[] =
{"acos", 1, 1, FEARG_1, f_acos}, // WJMc {"acos", 1, 1, FEARG_1, f_acos}, // WJMc
#endif #endif
{"add", 2, 2, FEARG_1, f_add}, {"add", 2, 2, FEARG_1, f_add},
{"and", 2, 2, 0, f_and}, {"and", 2, 2, FEARG_1, f_and},
{"append", 2, 2, FEARG_LAST, f_append}, {"append", 2, 2, FEARG_LAST, f_append},
{"appendbufline", 3, 3, FEARG_LAST, f_appendbufline}, {"appendbufline", 3, 3, FEARG_LAST, f_appendbufline},
{"argc", 0, 1, 0, f_argc}, {"argc", 0, 1, 0, f_argc},
@@ -447,23 +447,23 @@ static funcentry_T global_functions[] =
#endif #endif
#ifdef FEAT_BEVAL #ifdef FEAT_BEVAL
{"balloon_gettext", 0, 0, 0, f_balloon_gettext}, {"balloon_gettext", 0, 0, 0, f_balloon_gettext},
{"balloon_show", 1, 1, 0, f_balloon_show}, {"balloon_show", 1, 1, FEARG_1, f_balloon_show},
# if defined(FEAT_BEVAL_TERM) # if defined(FEAT_BEVAL_TERM)
{"balloon_split", 1, 1, 0, f_balloon_split}, {"balloon_split", 1, 1, FEARG_1, f_balloon_split},
# endif # endif
#endif #endif
{"browse", 4, 4, 0, f_browse}, {"browse", 4, 4, 0, f_browse},
{"browsedir", 2, 2, 0, f_browsedir}, {"browsedir", 2, 2, 0, f_browsedir},
{"bufadd", 1, 1, 0, f_bufadd}, {"bufadd", 1, 1, FEARG_1, f_bufadd},
{"bufexists", 1, 1, 0, f_bufexists}, {"bufexists", 1, 1, FEARG_1, f_bufexists},
{"buffer_exists", 1, 1, 0, f_bufexists}, // obsolete {"buffer_exists", 1, 1, FEARG_1, f_bufexists}, // obsolete
{"buffer_name", 1, 1, 0, f_bufname}, // obsolete {"buffer_name", 1, 1, 0, f_bufname}, // obsolete
{"buffer_number", 1, 1, 0, f_bufnr}, // obsolete {"buffer_number", 1, 1, 0, f_bufnr}, // obsolete
{"buflisted", 1, 1, 0, f_buflisted}, {"buflisted", 1, 1, FEARG_1, f_buflisted},
{"bufload", 1, 1, 0, f_bufload}, {"bufload", 1, 1, FEARG_1, f_bufload},
{"bufloaded", 1, 1, 0, f_bufloaded}, {"bufloaded", 1, 1, FEARG_1, f_bufloaded},
{"bufname", 1, 1, 0, f_bufname}, {"bufname", 1, 1, FEARG_1, f_bufname},
{"bufnr", 1, 2, 0, f_bufnr}, {"bufnr", 1, 2, FEARG_1, f_bufnr},
{"bufwinid", 1, 1, 0, f_bufwinid}, {"bufwinid", 1, 1, 0, f_bufwinid},
{"bufwinnr", 1, 1, 0, f_bufwinnr}, {"bufwinnr", 1, 1, 0, f_bufwinnr},
{"byte2line", 1, 1, 0, f_byte2line}, {"byte2line", 1, 1, 0, f_byte2line},
@@ -626,7 +626,7 @@ static funcentry_T global_functions[] =
{"inputsave", 0, 0, 0, f_inputsave}, {"inputsave", 0, 0, 0, f_inputsave},
{"inputsecret", 1, 2, 0, f_inputsecret}, {"inputsecret", 1, 2, 0, f_inputsecret},
{"insert", 2, 3, FEARG_1, f_insert}, {"insert", 2, 3, FEARG_1, f_insert},
{"invert", 1, 1, 0, f_invert}, {"invert", 1, 1, FEARG_1, f_invert},
{"isdirectory", 1, 1, 0, f_isdirectory}, {"isdirectory", 1, 1, 0, f_isdirectory},
#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
{"isinf", 1, 1, FEARG_1, f_isinf}, {"isinf", 1, 1, FEARG_1, f_isinf},
@@ -690,7 +690,7 @@ static funcentry_T global_functions[] =
#endif #endif
{"nextnonblank", 1, 1, 0, f_nextnonblank}, {"nextnonblank", 1, 1, 0, f_nextnonblank},
{"nr2char", 1, 2, 0, f_nr2char}, {"nr2char", 1, 2, 0, f_nr2char},
{"or", 2, 2, 0, f_or}, {"or", 2, 2, FEARG_1, f_or},
{"pathshorten", 1, 1, 0, f_pathshorten}, {"pathshorten", 1, 1, 0, f_pathshorten},
#ifdef FEAT_PERL #ifdef FEAT_PERL
{"perleval", 1, 1, 0, f_perleval}, {"perleval", 1, 1, 0, f_perleval},
@@ -976,7 +976,7 @@ static funcentry_T global_functions[] =
{"winwidth", 1, 1, 0, f_winwidth}, {"winwidth", 1, 1, 0, f_winwidth},
{"wordcount", 0, 0, 0, f_wordcount}, {"wordcount", 0, 0, 0, f_wordcount},
{"writefile", 2, 3, 0, f_writefile}, {"writefile", 2, 3, 0, f_writefile},
{"xor", 2, 2, 0, f_xor}, {"xor", 2, 2, FEARG_1, f_xor},
}; };
/* /*

View File

@@ -90,8 +90,8 @@ func Test_argadd_empty_curbuf()
call assert_equal('', bufname('%')) call assert_equal('', bufname('%'))
call assert_equal(1, line('$')) call assert_equal(1, line('$'))
rew rew
call assert_notequal(curbuf, bufnr('%')) call assert_notequal(curbuf, '%'->bufnr())
call assert_equal('Xargadd', bufname('%')) call assert_equal('Xargadd', '%'->bufname())
call assert_equal(2, line('$')) call assert_equal(2, line('$'))
call delete('Xargadd') call delete('Xargadd')

View File

@@ -12,7 +12,7 @@ func Test_balloon_show_gui()
call balloon_show('') call balloon_show('')
let msg = 'that that' let msg = 'that that'
call balloon_show(msg) eval msg->balloon_show()
call assert_equal(msg, balloon_gettext()) call assert_equal(msg, balloon_gettext())
sleep 10m sleep 10m
call balloon_show('') call balloon_show('')

View File

@@ -1537,12 +1537,12 @@ func Test_bufadd_bufload()
let curbuf = bufnr('') let curbuf = bufnr('')
call writefile(['some', 'text'], 'XotherName') call writefile(['some', 'text'], 'XotherName')
let buf = bufadd('XotherName') let buf = 'XotherName'->bufadd()
call assert_notequal(0, buf) call assert_notequal(0, buf)
call assert_equal(1, bufexists('XotherName')) eval 'XotherName'->bufexists()->assert_equal(1)
call assert_equal(0, getbufvar(buf, '&buflisted')) call assert_equal(0, getbufvar(buf, '&buflisted'))
call assert_equal(0, bufloaded(buf)) call assert_equal(0, bufloaded(buf))
call bufload(buf) eval buf->bufload()
call assert_equal(1, bufloaded(buf)) call assert_equal(1, bufloaded(buf))
call assert_equal(['some', 'text'], getbufline(buf, 1, '$')) call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
call assert_equal(curbuf, bufnr('')) call assert_equal(curbuf, bufnr(''))

View File

@@ -37,7 +37,7 @@ function Test_hide()
" :hide as a command " :hide as a command
hide hide
call assert_equal([orig_bname, orig_winnr], [bufname(''), winnr('$')]) call assert_equal([orig_bname, orig_winnr], [bufname(''), winnr('$')])
call assert_equal([1, 1], [buflisted('Xf1'), bufloaded('Xf1')]) call assert_equal([1, 1], ['Xf1'->buflisted(), 'Xf1'->bufloaded()])
bwipeout! Xf1 bwipeout! Xf1
new Xf1 new Xf1

View File

@@ -758,9 +758,8 @@ func Test_popup_and_previewwindow_dump()
endfunc endfunc
func Test_balloon_split() func Test_balloon_split()
if !exists('*balloon_split') CheckFunction balloon_split
return
endif
call assert_equal([ call assert_equal([
\ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"', \ 'tempname: 0x555555e380a0 "/home/mool/.viminfz.tmp"',
\ ], balloon_split( \ ], balloon_split(
@@ -771,13 +770,14 @@ func Test_balloon_split()
\ ], balloon_split( \ ], balloon_split(
\ 'one two three four one two three four one two three four')) \ 'one two three four one two three four one two three four'))
call assert_equal([ eval 'struct = {one = 1, two = 2, three = 3}'
\ 'struct = {', \ ->balloon_split()
\ ' one = 1,', \ ->assert_equal([
\ ' two = 2,', \ 'struct = {',
\ ' three = 3}', \ ' one = 1,',
\ ], balloon_split( \ ' two = 2,',
\ 'struct = {one = 1, two = 2, three = 3}')) \ ' three = 3}',
\ ])
call assert_equal([ call assert_equal([
\ 'struct = {', \ 'struct = {',

View File

@@ -1330,6 +1330,7 @@ func Test_bitwise_functions()
" and " and
call assert_equal(127, and(127, 127)) call assert_equal(127, and(127, 127))
call assert_equal(16, and(127, 16)) call assert_equal(16, and(127, 16))
eval 127->and(16)->assert_equal(16)
call assert_equal(0, and(127, 128)) call assert_equal(0, and(127, 128))
call assert_fails("call and(1.0, 1)", 'E805:') call assert_fails("call and(1.0, 1)", 'E805:')
call assert_fails("call and([], 1)", 'E745:') call assert_fails("call and([], 1)", 'E745:')
@@ -1340,6 +1341,7 @@ func Test_bitwise_functions()
" or " or
call assert_equal(23, or(16, 7)) call assert_equal(23, or(16, 7))
call assert_equal(15, or(8, 7)) call assert_equal(15, or(8, 7))
eval 8->or(7)->assert_equal(15)
call assert_equal(123, or(0, 123)) call assert_equal(123, or(0, 123))
call assert_fails("call or(1.0, 1)", 'E805:') call assert_fails("call or(1.0, 1)", 'E805:')
call assert_fails("call or([], 1)", 'E745:') call assert_fails("call or([], 1)", 'E745:')
@@ -1350,6 +1352,7 @@ func Test_bitwise_functions()
" xor " xor
call assert_equal(0, xor(127, 127)) call assert_equal(0, xor(127, 127))
call assert_equal(111, xor(127, 16)) call assert_equal(111, xor(127, 16))
eval 127->xor(16)->assert_equal(111)
call assert_equal(255, xor(127, 128)) call assert_equal(255, xor(127, 128))
call assert_fails("call xor(1.0, 1)", 'E805:') call assert_fails("call xor(1.0, 1)", 'E805:')
call assert_fails("call xor([], 1)", 'E745:') call assert_fails("call xor([], 1)", 'E745:')
@@ -1359,6 +1362,7 @@ func Test_bitwise_functions()
call assert_fails("call xor(1, {})", 'E728:') call assert_fails("call xor(1, {})", 'E728:')
" invert " invert
call assert_equal(65408, and(invert(127), 65535)) call assert_equal(65408, and(invert(127), 65535))
eval 127->invert()->and(65535)->assert_equal(65408)
call assert_equal(65519, and(invert(16), 65535)) call assert_equal(65519, and(invert(16), 65535))
call assert_equal(65407, and(invert(128), 65535)) call assert_equal(65407, and(invert(128), 65535))
call assert_fails("call invert(1.0)", 'E805:') call assert_fails("call invert(1.0)", 'E805:')

View File

@@ -765,6 +765,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1888,
/**/ /**/
1887, 1887,
/**/ /**/