1
0
forked from aniani/vim

patch 8.1.1987: 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-09-04 22:29:14 +02:00
parent a5a7882ea4
commit a0d1fef4eb
8 changed files with 99 additions and 41 deletions

View File

@@ -1,4 +1,4 @@
*eval.txt* For Vim version 8.1. Last change: 2019 Aug 31
*eval.txt* For Vim version 8.1. Last change: 2019 Sep 04
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -2547,7 +2547,7 @@ keys({dict}) List keys in {dict}
len({expr}) Number the length of {expr}
libcall({lib}, {func}, {arg}) String call {func} in library {lib} with {arg}
libcallnr({lib}, {func}, {arg}) Number idem, but return a Number
line({expr}) Number line nr of cursor, last line or mark
line({expr} [, {winid}]) Number line nr of cursor, last line or mark
line2byte({lnum}) Number byte count of line {lnum}
lispindent({lnum}) Number Lisp indent for line {lnum}
list2str({list} [, {utf8}]) String turn numbers in {list} into a String
@@ -5688,8 +5688,8 @@ glob2regpat({expr}) *glob2regpat()*
GetExpr()->glob2regpat()
< *globpath()*
globpath({path}, {expr} [, {nosuf} [, {list} [, {alllinks}]]])
Perform glob() on all directories in {path} and concatenate
the results. Example: >
Perform glob() for {expr} on all directories in {path} and
concatenate the results. Example: >
:echo globpath(&rtp, "syntax/c.vim")
<
{path} is a comma-separated list of directory names. Each
@@ -5790,7 +5790,9 @@ hasmapto({what} [, {mode} [, {abbr}]]) *hasmapto()*
If no matching mapping is found 0 is returned.
The following characters are recognized in {mode}:
n Normal mode
v Visual mode
v Visual and Select mode
x Visual mode
s Select mode
o Operator-pending mode
i Insert mode
l Language-Argument ("r", "f", "t", etc.)
@@ -6376,8 +6378,9 @@ libcallnr({libname}, {funcname}, {argument})
the argument to the called function: >
GetValue()->libcallnr("libc.so", "printf")
<
*line()*
line({expr}) The result is a Number, which is the line number of the file
line({expr} [, {winid}]) *line()*
The result is a Number, which is the line number of the file
position given with {expr}. The accepted positions are:
. the cursor position
$ the last line in the current buffer
@@ -6395,8 +6398,11 @@ line({expr}) The result is a Number, which is the line number of the file
then applies to another buffer.
To get the column number use |col()|. To get both use
|getpos()|.
With the optional {winid} argument the values are obtained for
that window instead of the current window.
Examples: >
line(".") line number of the cursor
line(".", winid) idem, in window "winid"
line("'t") line number of mark t
line("'" . marker) line number of mark marker
<
@@ -7546,6 +7552,9 @@ range({expr} [, {max} [, {stride}]]) *range()*
range(2, -2, -1) " [2, 1, 0, -1, -2]
range(0) " []
range(2, 0) " error!
<
Can also be used as a |method|: >
GetExpr()->range()
<
*readdir()*
readdir({directory} [, {expr}])
@@ -7575,6 +7584,9 @@ readdir({directory} [, {expr}])
\ {x : s:tree(a:dir . '/' . x)} : x})}
endfunction
echo s:tree(".")
<
Can also be used as a |method|: >
GetDirName()->readdir()
<
*readfile()*
readfile({fname} [, {type} [, {max}]])
@@ -7610,6 +7622,9 @@ readfile({fname} [, {type} [, {max}]])
the result is an empty list.
Also see |writefile()|.
Can also be used as a |method|: >
GetFileName()->readfile()
reg_executing() *reg_executing()*
Returns the single letter name of the register being executed.
Returns an empty string when no register is being executed.
@@ -7631,6 +7646,10 @@ reltime([{start} [, {end}]]) *reltime()*
and {end}.
The {start} and {end} arguments must be values returned by
reltime().
Can also be used as a |method|: >
GetStart()->reltime()
<
{only available when compiled with the |+reltime| feature}
reltimefloat({time}) *reltimefloat()*
@@ -7641,7 +7660,11 @@ reltimefloat({time}) *reltimefloat()*
let seconds = reltimefloat(reltime(start))
< See the note of reltimestr() about overhead.
Also see |profiling|.
{only available when compiled with the |+reltime| feature}
Can also be used as a |method|: >
reltime(start)->reltimefloat()
< {only available when compiled with the |+reltime| feature}
reltimestr({time}) *reltimestr()*
Return a String that represents the time value of {time}.
@@ -7656,7 +7679,11 @@ reltimestr({time}) *reltimestr()*
can use split() to remove it. >
echo split(reltimestr(reltime(start)))[0]
< Also see |profiling|.
{only available when compiled with the |+reltime| feature}
Can also be used as a |method|: >
reltime(start)->reltimestr()
< {only available when compiled with the |+reltime| feature}
*remote_expr()* *E449*
remote_expr({server}, {string} [, {idvar} [, {timeout}]])
@@ -7685,6 +7712,8 @@ remote_expr({server}, {string} [, {idvar} [, {timeout}]])
:echo remote_expr("gvim", "2+2")
:echo remote_expr("gvim1", "b:current_syntax")
<
Can also be used as a |method|: >
ServerName()->remote_expr(expr)
remote_foreground({server}) *remote_foreground()*
Move the Vim server with the name {server} to the foreground.
@@ -7696,7 +7725,11 @@ remote_foreground({server}) *remote_foreground()*
Note: This does not restore the window if it was minimized,
like foreground() does.
This function is not available in the |sandbox|.
{only in the Win32, Athena, Motif and GTK GUI versions and the
Can also be used as a |method|: >
ServerName()->remote_foreground()
< {only in the Win32, Athena, Motif and GTK GUI versions and the
Win32 console version}
@@ -7714,6 +7747,9 @@ remote_peek({serverid} [, {retvar}]) *remote_peek()*
:let repl = ""
:echo "PEEK: ".remote_peek(id, "repl").": ".repl
< Can also be used as a |method|: >
ServerId()->remote_peek()
remote_read({serverid}, [{timeout}]) *remote_read()*
Return the oldest available reply from {serverid} and consume
it. Unless a {timeout} in seconds is given, it blocks until a
@@ -7723,6 +7759,9 @@ remote_read({serverid}, [{timeout}]) *remote_read()*
{only available when compiled with the |+clientserver| feature}
Example: >
:echo remote_read(id)
< Can also be used as a |method|: >
ServerId()->remote_read()
<
*remote_send()* *E241*
remote_send({server}, {string} [, {idvar}])
@@ -7746,12 +7785,19 @@ remote_send({server}, {string} [, {idvar}])
\ echo remote_read(expand("<amatch>"))
:echo remote_send("gvim", ":sleep 10 | echo ".
\ 'server2client(expand("<client>"), "HELLO")<CR>')
<
Can also be used as a |method|: >
ServerName()->remote_send(keys)
<
*remote_startserver()* *E941* *E942*
remote_startserver({name})
Become the server {name}. This fails if already running as a
server, when |v:servername| is not empty.
{only available when compiled with the |+clientserver| feature}
Can also be used as a |method|: >
ServerName()->remote_startserver()
< {only available when compiled with the |+clientserver| feature}
remove({list}, {idx} [, {end}]) *remove()*
Without {end}: Remove the item at {idx} from |List| {list} and
@@ -7795,6 +7841,9 @@ rename({from}, {to}) *rename()*
NOTE: If {to} exists it is overwritten without warning.
This function is not available in the |sandbox|.
Can also be used as a |method|: >
GetOldName()->rename(newname)
repeat({expr}, {count}) *repeat()*
Repeat {expr} {count} times and return the concatenated
result. Example: >
@@ -7824,6 +7873,8 @@ resolve({filename}) *resolve()* *E655*
current directory (provided the result is still a relative
path name) and also keeps a trailing path separator.
Can also be used as a |method|: >
GetName()->resolve()
reverse({object}) *reverse()*
Reverse the order of items in {object} in-place.
@@ -7861,7 +7912,11 @@ rubyeval({expr}) *rubyeval()*
Hashes are represented as Vim |Dictionary| type.
Other objects are represented as strings resulted from their
"Object#to_s" method.
{only available when compiled with the |+ruby| feature}
Can also be used as a |method|: >
GetRubyExpr()->rubyeval()
< {only available when compiled with the |+ruby| feature}
screenattr({row}, {col}) *screenattr()*
Like |screenchar()|, but return the attribute. This is a rather
@@ -10231,6 +10286,7 @@ macunix Synonym for osxdarwin
menu Compiled with support for |:menu|.
mksession Compiled with support for |:mksession|.
modify_fname Compiled with file name modifiers. |filename-modifiers|
(always true)
mouse Compiled with support mouse.
mouse_dec Compiled with support for Dec terminal mouse.
mouse_gpm Compiled with support for gpm (Linux console mouse)

View File

@@ -684,32 +684,32 @@ static funcentry_T global_functions[] =
#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
{"pyxeval", 1, 1, FEARG_1, f_pyxeval},
#endif
{"range", 1, 3, 0, f_range},
{"readdir", 1, 2, 0, f_readdir},
{"readfile", 1, 3, 0, f_readfile},
{"range", 1, 3, FEARG_1, f_range},
{"readdir", 1, 2, FEARG_1, f_readdir},
{"readfile", 1, 3, FEARG_1, f_readfile},
{"reg_executing", 0, 0, 0, f_reg_executing},
{"reg_recording", 0, 0, 0, f_reg_recording},
{"reltime", 0, 2, 0, f_reltime},
{"reltime", 0, 2, FEARG_1, f_reltime},
#ifdef FEAT_FLOAT
{"reltimefloat", 1, 1, 0, f_reltimefloat},
{"reltimefloat", 1, 1, FEARG_1, f_reltimefloat},
#endif
{"reltimestr", 1, 1, 0, f_reltimestr},
{"remote_expr", 2, 4, 0, f_remote_expr},
{"remote_foreground", 1, 1, 0, f_remote_foreground},
{"remote_peek", 1, 2, 0, f_remote_peek},
{"remote_read", 1, 2, 0, f_remote_read},
{"remote_send", 2, 3, 0, f_remote_send},
{"remote_startserver", 1, 1, 0, f_remote_startserver},
{"reltimestr", 1, 1, FEARG_1, f_reltimestr},
{"remote_expr", 2, 4, FEARG_1, f_remote_expr},
{"remote_foreground", 1, 1, FEARG_1, f_remote_foreground},
{"remote_peek", 1, 2, FEARG_1, f_remote_peek},
{"remote_read", 1, 2, FEARG_1, f_remote_read},
{"remote_send", 2, 3, FEARG_1, f_remote_send},
{"remote_startserver", 1, 1, FEARG_1, f_remote_startserver},
{"remove", 2, 3, FEARG_1, f_remove},
{"rename", 2, 2, 0, f_rename},
{"rename", 2, 2, FEARG_1, f_rename},
{"repeat", 2, 2, FEARG_1, f_repeat},
{"resolve", 1, 1, 0, f_resolve},
{"resolve", 1, 1, FEARG_1, f_resolve},
{"reverse", 1, 1, FEARG_1, f_reverse},
#ifdef FEAT_FLOAT
{"round", 1, 1, FEARG_1, f_round},
#endif
#ifdef FEAT_RUBY
{"rubyeval", 1, 1, 0, f_rubyeval},
{"rubyeval", 1, 1, FEARG_1, f_rubyeval},
#endif
{"screenattr", 2, 2, 0, f_screenattr},
{"screenchar", 2, 2, 0, f_screenchar},

View File

@@ -34,7 +34,7 @@ func Test_client_server()
" When using valgrind it takes much longer.
call WaitForAssert({-> assert_match(name, serverlist())})
call remote_foreground(name)
eval name->remote_foreground()
call remote_send(name, ":let testvar = 'yes'\<CR>")
call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"')
@@ -53,7 +53,7 @@ func Test_client_server()
endif
" Wait for the server to be up and answering requests.
sleep 100m
call WaitForAssert({-> assert_true(remote_expr(name, "v:version", "", 1) != "")})
call WaitForAssert({-> assert_true(name->remote_expr("v:version", "", 1) != "")})
call remote_send(name, ":let testvar = 'maybe'\<CR>")
call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))})
@@ -63,7 +63,7 @@ func Test_client_server()
" Expression evaluated locally.
if v:servername == ''
call remote_startserver('MYSELF')
eval 'MYSELF'->remote_startserver()
" May get MYSELF1 when running the test again.
call assert_match('MYSELF', v:servername)
endif
@@ -71,11 +71,11 @@ func Test_client_server()
call assert_equal('myself', remote_expr(v:servername, 'testvar'))
call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
call assert_equal('got it', remote_read(g:myserverid, 2))
call assert_equal('got it', g:myserverid->remote_read(2))
call remote_send(name, ":call server2client(expand('<client>'), 'another')\<CR>", 'g:myserverid')
let peek_result = 'nothing'
let r = remote_peek(g:myserverid, 'peek_result')
let r = g:myserverid->remote_peek('peek_result')
" unpredictable whether the result is already available.
if r > 0
call assert_equal('another', peek_result)
@@ -89,7 +89,7 @@ func Test_client_server()
call assert_equal('another', g:peek_result)
call assert_equal('another', remote_read(g:myserverid, 2))
call remote_send(name, ":qa!\<CR>")
eval name->remote_send(":qa!\<CR>")
try
call WaitForAssert({-> assert_equal("dead", job_status(job))})
finally

View File

@@ -75,7 +75,7 @@ func Test_readfile_binary()
call setline(1, ['one', 'two', 'three'])
setlocal ff=dos
write XReadfile
let lines = readfile('XReadfile')
let lines = 'XReadfile'->readfile()
call assert_equal(['one', 'two', 'three'], lines)
let lines = readfile('XReadfile', '', 2)
call assert_equal(['one', 'two'], lines)

View File

@@ -253,7 +253,7 @@ func Test_resolve_unix()
call delete('Xlink')
silent !ln -s -f Xlink2/ Xlink1
call assert_equal('Xlink2', resolve('Xlink1'))
call assert_equal('Xlink2', 'Xlink1'->resolve())
call assert_equal('Xlink2/', resolve('Xlink1/'))
call delete('Xlink1')
@@ -1413,7 +1413,7 @@ func Test_func_range_with_edit()
" is invalid in that buffer.
call writefile(['just one line'], 'Xfuncrange2')
new
call setline(1, range(10))
call setline(1, 10->range())
write Xfuncrange1
call assert_fails('5,8call EditAnotherFile()', 'E16:')
@@ -1544,7 +1544,7 @@ func Test_readdir()
call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
" Only results containing "f"
let files = readdir('Xdir', { x -> stridx(x, 'f') !=- 1 })
let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
call assert_equal(['foo.txt'], sort(files))
" Only .txt files

View File

@@ -8,11 +8,11 @@ func Test_reltime()
let now = reltime()
sleep 10m
let later = reltime()
let elapsed = reltime(now)
let elapsed = now->reltime()
call assert_true(reltimestr(elapsed) =~ '0\.0')
call assert_true(reltimestr(elapsed) != '0.0')
call assert_true(elapsed->reltimestr() != '0.0')
call assert_true(reltimefloat(elapsed) < 0.1)
call assert_true(reltimefloat(elapsed) > 0.0)
call assert_true(elapsed->reltimefloat() > 0.0)
let same = reltime(now, now)
call assert_equal('0.000', split(reltimestr(same))[0][:4])

View File

@@ -25,7 +25,7 @@ func Test_rename_file_ignore_case()
set fileignorecase
call writefile(['foo'], 'Xrename')
call assert_equal(0, rename('Xrename', 'XRENAME'))
call assert_equal(0, 'Xrename'->rename('XRENAME'))
call assert_equal(['foo'], readfile('XRENAME'))

View File

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