Mercurial > vim
changeset 17982:2029737e6a22 v8.1.1987
patch 8.1.1987: more functions can be used as methods
Commit: https://github.com/vim/vim/commit/a0d1fef4ebb693696464c5e22e33269f724b8e0e
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Sep 4 22:29:14 2019 +0200
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.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 04 Sep 2019 22:30:03 +0200 |
parents | cf10d0482ad1 |
children | 9814aa081796 |
files | runtime/doc/eval.txt src/evalfunc.c src/testdir/test_clientserver.vim src/testdir/test_eval_stuff.vim src/testdir/test_functions.vim src/testdir/test_reltime.vim src/testdir/test_rename.vim src/version.c |
diffstat | 8 files changed, 100 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -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}]]) 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}, {argume 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, whi 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 < @@ -7547,6 +7553,9 @@ range({expr} [, {max} [, {stride}]]) range(0) " [] range(2, 0) " error! < + Can also be used as a |method|: > + GetExpr()->range() +< *readdir()* readdir({directory} [, {expr}]) Return a list with file and directory names in {directory}. @@ -7576,6 +7585,9 @@ readdir({directory} [, {expr}]) endfunction echo s:tree(".") < + Can also be used as a |method|: > + GetDirName()->readdir() +< *readfile()* readfile({fname} [, {type} [, {max}]]) Read file {fname} and return a |List|, each line of the file @@ -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 :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_f 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}]) *r :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}]) * {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}]) @@ -7747,11 +7786,18 @@ remote_send({server}, {string} [, {idvar :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()* *E65 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)
--- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -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}, -#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}, + {"reltimefloat", 1, 1, FEARG_1, f_reltimefloat}, +#endif + {"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},
--- a/src/testdir/test_clientserver.vim +++ b/src/testdir/test_clientserver.vim @@ -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
--- a/src/testdir/test_eval_stuff.vim +++ b/src/testdir/test_eval_stuff.vim @@ -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)
--- a/src/testdir/test_functions.vim +++ b/src/testdir/test_functions.vim @@ -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
--- a/src/testdir/test_reltime.vim +++ b/src/testdir/test_reltime.vim @@ -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])
--- a/src/testdir/test_rename.vim +++ b/src/testdir/test_rename.vim @@ -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'))