0
0
mirror of https://github.com/vim/vim.git synced 2025-09-27 04:14:06 -04:00

patch 8.1.1993: 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-06 21:34:30 +02:00
parent 16b58ae9f3
commit 196b466443
16 changed files with 111 additions and 30 deletions

View File

@@ -5833,7 +5833,7 @@ histadd({history}, {item}) *histadd()*
Can also be used as a |method|, the base is used for the Can also be used as a |method|, the base is used for the
second argument: > second argument: >
GetPattern()->histadd('search') GetHistory()->histadd('search')
histdel({history} [, {item}]) *histdel()* histdel({history} [, {item}]) *histdel()*
Clear {history}, i.e. delete all its entries. See |hist-names| Clear {history}, i.e. delete all its entries. See |hist-names|
@@ -7923,6 +7923,9 @@ screenattr({row}, {col}) *screenattr()*
arbitrary number that can only be used to compare to the arbitrary number that can only be used to compare to the
attribute at other positions. attribute at other positions.
Can also be used as a |method|: >
GetRow()->screenattr(col)
screenchar({row}, {col}) *screenchar()* screenchar({row}, {col}) *screenchar()*
The result is a Number, which is the character at position The result is a Number, which is the character at position
[row, col] on the screen. This works for every possible [row, col] on the screen. This works for every possible
@@ -7933,6 +7936,9 @@ screenchar({row}, {col}) *screenchar()*
This is mainly to be used for testing. This is mainly to be used for testing.
Returns -1 when row or col is out of range. Returns -1 when row or col is out of range.
Can also be used as a |method|: >
GetRow()->screenchar(col)
screenchars({row}, {col}) *screenchars()* screenchars({row}, {col}) *screenchars()*
The result is a List of Numbers. The first number is the same The result is a List of Numbers. The first number is the same
as what |screenchar()| returns. Further numbers are as what |screenchar()| returns. Further numbers are
@@ -7940,6 +7946,9 @@ screenchars({row}, {col}) *screenchars()*
This is mainly to be used for testing. This is mainly to be used for testing.
Returns an empty List when row or col is out of range. Returns an empty List when row or col is out of range.
Can also be used as a |method|: >
GetRow()->screenchars(col)
screencol() *screencol()* screencol() *screencol()*
The result is a Number, which is the current screen column of The result is a Number, which is the current screen column of
the cursor. The leftmost column has number 1. the cursor. The leftmost column has number 1.
@@ -7970,6 +7979,9 @@ screenpos({winid}, {lnum}, {col}) *screenpos()*
a Tab it would be the same as "endcol", while for a double a Tab it would be the same as "endcol", while for a double
width character it would be the same as "col". width character it would be the same as "col".
Can also be used as a |method|: >
GetWinid()->screenpos(lnum, col)
screenrow() *screenrow()* screenrow() *screenrow()*
The result is a Number, which is the current screen row of the The result is a Number, which is the current screen row of the
cursor. The top line has number one. cursor. The top line has number one.
@@ -7986,6 +7998,9 @@ screenstring({row}, {col}) *screenstring()*
This is mainly to be used for testing. This is mainly to be used for testing.
Returns an empty String when row or col is out of range. Returns an empty String when row or col is out of range.
Can also be used as a |method|: >
GetRow()->screenstring(col)
search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *search()* search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *search()*
Search for regexp pattern {pattern}. The search starts at the Search for regexp pattern {pattern}. The search starts at the
cursor position (you can use |cursor()| to set it). cursor position (you can use |cursor()| to set it).
@@ -8072,6 +8087,8 @@ search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *search()*
without the 'e' flag if the cursor is on the "f" of "if". without the 'e' flag if the cursor is on the "f" of "if".
The 'n' flag tells the function not to move the cursor. The 'n' flag tells the function not to move the cursor.
Can also be used as a |method|: >
GetPattern()->search()
searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()* searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()*
Search for the declaration of {name}. Search for the declaration of {name}.
@@ -8090,6 +8107,9 @@ searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()*
if searchdecl('myvar') == 0 if searchdecl('myvar') == 0
echo getline('.') echo getline('.')
endif endif
<
Can also be used as a |method|: >
GetName()->searchdecl()
< <
*searchpair()* *searchpair()*
searchpair({start}, {middle}, {end} [, {flags} [, {skip} searchpair({start}, {middle}, {end} [, {flags} [, {skip}
@@ -8205,6 +8225,9 @@ searchpos({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *searchpos()*
< In this example "submatch" is 2 when a lowercase letter is < In this example "submatch" is 2 when a lowercase letter is
found |/\l|, 3 when an uppercase letter is found |/\u|. found |/\l|, 3 when an uppercase letter is found |/\u|.
Can also be used as a |method|: >
GetPattern()->searchpos()
server2client({clientid}, {string}) *server2client()* server2client({clientid}, {string}) *server2client()*
Send a reply string to {clientid}. The most recent {clientid} Send a reply string to {clientid}. The most recent {clientid}
that sent a string can be retrieved with expand("<client>"). that sent a string can be retrieved with expand("<client>").
@@ -8216,6 +8239,9 @@ server2client({clientid}, {string}) *server2client()*
See also |clientserver|. See also |clientserver|.
Example: > Example: >
:echo server2client(expand("<client>"), "HELLO") :echo server2client(expand("<client>"), "HELLO")
< Can also be used as a |method|: >
GetClientId()->server2client(string)
< <
serverlist() *serverlist()* serverlist() *serverlist()*
Return a list of available server names, one per line. Return a list of available server names, one per line.
@@ -8239,6 +8265,9 @@ setbufline({expr}, {lnum}, {text}) *setbufline()*
{lnum} is not valid then 1 is returned. On success 0 is {lnum} is not valid then 1 is returned. On success 0 is
returned. returned.
Can also be used as a |method|: >
GetText()->setbufline(buf, lnum)
setbufvar({expr}, {varname}, {val}) *setbufvar()* setbufvar({expr}, {varname}, {val}) *setbufvar()*
Set option or local variable {varname} in buffer {expr} to Set option or local variable {varname} in buffer {expr} to
{val}. {val}.
@@ -8252,6 +8281,9 @@ setbufvar({expr}, {varname}, {val}) *setbufvar()*
:call setbufvar("todo", "myvar", "foobar") :call setbufvar("todo", "myvar", "foobar")
< This function is not available in the |sandbox|. < This function is not available in the |sandbox|.
Can also be used as a |method|: >
GetValue()->setbufvar(buf, varname)
setcharsearch({dict}) *setcharsearch()* setcharsearch({dict}) *setcharsearch()*
Set the current character search information to {dict}, Set the current character search information to {dict},
which contains one or more of the following entries: which contains one or more of the following entries:
@@ -8272,6 +8304,9 @@ setcharsearch({dict}) *setcharsearch()*
:call setcharsearch(prevsearch) :call setcharsearch(prevsearch)
< Also see |getcharsearch()|. < Also see |getcharsearch()|.
Can also be used as a |method|: >
SavedSearch()->setcharsearch()
setcmdpos({pos}) *setcmdpos()* setcmdpos({pos}) *setcmdpos()*
Set the cursor position in the command line to byte position Set the cursor position in the command line to byte position
{pos}. The first position is 1. {pos}. The first position is 1.
@@ -8287,11 +8322,17 @@ setcmdpos({pos}) *setcmdpos()*
Returns 0 when successful, 1 when not editing the command Returns 0 when successful, 1 when not editing the command
line. line.
Can also be used as a |method|: >
GetPos()->setcmdpos()
setenv({name}, {val}) *setenv()* setenv({name}, {val}) *setenv()*
Set environment variable {name} to {val}. Set environment variable {name} to {val}.
When {val} is |v:null| the environment variable is deleted. When {val} is |v:null| the environment variable is deleted.
See also |expr-env|. See also |expr-env|.
Can also be used as a |method|, passing the value as the base: >
GetPath()->setenv('PATH')
setfperm({fname}, {mode}) *setfperm()* *chmod* setfperm({fname}, {mode}) *setfperm()* *chmod*
Set the file permissions for {fname} to {mode}. Set the file permissions for {fname} to {mode}.
{mode} must be a string with 9 characters. It is of the form {mode} must be a string with 9 characters. It is of the form
@@ -8338,6 +8379,9 @@ setline({lnum}, {text}) *setline()*
< Note: The '[ and '] marks are not set. < Note: The '[ and '] marks are not set.
Can also be used as a |method|, passing the text as the base: >
GetText()->setline(lnum)
setloclist({nr}, {list} [, {action} [, {what}]]) *setloclist()* setloclist({nr}, {list} [, {action} [, {what}]]) *setloclist()*
Create or replace or add to the location list for window {nr}. Create or replace or add to the location list for window {nr}.
{nr} can be the window number or the |window-ID|. {nr} can be the window number or the |window-ID|.

View File

@@ -700,27 +700,27 @@ static funcentry_T global_functions[] =
#ifdef FEAT_RUBY #ifdef FEAT_RUBY
{"rubyeval", 1, 1, FEARG_1, f_rubyeval}, {"rubyeval", 1, 1, FEARG_1, f_rubyeval},
#endif #endif
{"screenattr", 2, 2, 0, f_screenattr}, {"screenattr", 2, 2, FEARG_1, f_screenattr},
{"screenchar", 2, 2, 0, f_screenchar}, {"screenchar", 2, 2, FEARG_1, f_screenchar},
{"screenchars", 2, 2, 0, f_screenchars}, {"screenchars", 2, 2, FEARG_1, f_screenchars},
{"screencol", 0, 0, 0, f_screencol}, {"screencol", 0, 0, 0, f_screencol},
{"screenpos", 3, 3, 0, f_screenpos}, {"screenpos", 3, 3, FEARG_1, f_screenpos},
{"screenrow", 0, 0, 0, f_screenrow}, {"screenrow", 0, 0, 0, f_screenrow},
{"screenstring", 2, 2, 0, f_screenstring}, {"screenstring", 2, 2, FEARG_1, f_screenstring},
{"search", 1, 4, 0, f_search}, {"search", 1, 4, FEARG_1, f_search},
{"searchdecl", 1, 3, 0, f_searchdecl}, {"searchdecl", 1, 3, FEARG_1, f_searchdecl},
{"searchpair", 3, 7, 0, f_searchpair}, {"searchpair", 3, 7, 0, f_searchpair},
{"searchpairpos", 3, 7, 0, f_searchpairpos}, {"searchpairpos", 3, 7, 0, f_searchpairpos},
{"searchpos", 1, 4, 0, f_searchpos}, {"searchpos", 1, 4, FEARG_1, f_searchpos},
{"server2client", 2, 2, 0, f_server2client}, {"server2client", 2, 2, FEARG_1, f_server2client},
{"serverlist", 0, 0, 0, f_serverlist}, {"serverlist", 0, 0, 0, f_serverlist},
{"setbufline", 3, 3, 0, f_setbufline}, {"setbufline", 3, 3, FEARG_3, f_setbufline},
{"setbufvar", 3, 3, 0, f_setbufvar}, {"setbufvar", 3, 3, FEARG_3, f_setbufvar},
{"setcharsearch", 1, 1, 0, f_setcharsearch}, {"setcharsearch", 1, 1, FEARG_1, f_setcharsearch},
{"setcmdpos", 1, 1, 0, f_setcmdpos}, {"setcmdpos", 1, 1, FEARG_1, f_setcmdpos},
{"setenv", 2, 2, 0, f_setenv}, {"setenv", 2, 2, FEARG_2, f_setenv},
{"setfperm", 2, 2, FEARG_1, f_setfperm}, {"setfperm", 2, 2, FEARG_1, f_setfperm},
{"setline", 2, 2, 0, f_setline}, {"setline", 2, 2, FEARG_2, f_setline},
{"setloclist", 2, 4, 0, f_setloclist}, {"setloclist", 2, 4, 0, f_setloclist},
{"setmatches", 1, 2, 0, f_setmatches}, {"setmatches", 1, 2, 0, f_setmatches},
{"setpos", 2, 2, 0, f_setpos}, {"setpos", 2, 2, 0, f_setpos},

View File

@@ -20,7 +20,7 @@ func Test_setbufline_getbufline()
let b = bufnr('%') let b = bufnr('%')
wincmd w wincmd w
call assert_equal(1, setbufline(b, 5, ['x'])) call assert_equal(1, setbufline(b, 5, ['x']))
call assert_equal(1, setbufline(bufnr('$') + 1, 1, ['x'])) call assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
call assert_equal(0, setbufline(b, 4, ['d', 'e'])) call assert_equal(0, setbufline(b, 4, ['d', 'e']))
call assert_equal(['c'], b->getbufline(3)) call assert_equal(['c'], b->getbufline(3))
call assert_equal(['d'], getbufline(b, 4)) call assert_equal(['d'], getbufline(b, 4))

View File

@@ -20,7 +20,7 @@ func Test_charsearch()
" check that setcharsearch() changes the settings. " check that setcharsearch() changes the settings.
3 3
normal! ylfep normal! ylfep
call setcharsearch({'char': 'k'}) eval {'char': 'k'}->setcharsearch()
normal! ;p normal! ;p
call setcharsearch({'forward': 0}) call setcharsearch({'forward': 0})
normal! $;p normal! $;p

View File

@@ -73,7 +73,7 @@ func Test_client_server()
call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid') call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
call assert_equal('got it', g:myserverid->remote_read(2)) call assert_equal('got it', g:myserverid->remote_read(2))
call remote_send(name, ":call server2client(expand('<client>'), 'another')\<CR>", 'g:myserverid') call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid')
let peek_result = 'nothing' let peek_result = 'nothing'
let r = g:myserverid->remote_peek('peek_result') let r = g:myserverid->remote_peek('peek_result')
" unpredictable whether the result is already available. " unpredictable whether the result is already available.

View File

@@ -672,7 +672,7 @@ func Test_setcmdpos()
call assert_equal('"12ab', @:) call assert_equal('"12ab', @:)
" setcmdpos() returns 1 when not editing the command line. " setcmdpos() returns 1 when not editing the command line.
call assert_equal(1, setcmdpos(3)) call assert_equal(1, 3->setcmdpos())
endfunc endfunc
func Test_cmdline_overstrike() func Test_cmdline_overstrike()

View File

@@ -87,7 +87,7 @@ func Test_screenpos()
call assert_equal({'row': winrow, call assert_equal({'row': winrow,
\ 'col': wincol + 13, \ 'col': wincol + 13,
\ 'curscol': wincol + 13, \ 'curscol': wincol + 13,
\ 'endcol': wincol + 13}, screenpos(winid, 1, 7)) \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
call assert_equal({'row': winrow + 2, call assert_equal({'row': winrow + 2,
\ 'col': wincol + 1, \ 'col': wincol + 1,
\ 'curscol': wincol + 1, \ 'curscol': wincol + 1,

View File

@@ -330,7 +330,7 @@ func Test_diffoff()
call setline(1, ['One', '', 'Two', 'Three']) call setline(1, ['One', '', 'Two', 'Three'])
diffthis diffthis
redraw redraw
call assert_notequal(normattr, screenattr(1, 1)) call assert_notequal(normattr, 1->screenattr(1))
diffoff! diffoff!
redraw redraw
call assert_equal(normattr, screenattr(1, 1)) call assert_equal(normattr, screenattr(1, 1))

View File

@@ -18,7 +18,7 @@ endfunc
func Test_setenv() func Test_setenv()
unlet! $TESTENV unlet! $TESTENV
call setenv('TEST ENV', 'foo') eval 'foo'->setenv('TEST ENV')
call assert_equal('foo', getenv('TEST ENV')) call assert_equal('foo', getenv('TEST ENV'))
call setenv('TEST ENV', v:null) call setenv('TEST ENV', v:null)
call assert_equal(v:null, getenv('TEST ENV')) call assert_equal(v:null, getenv('TEST ENV'))

View File

@@ -1137,7 +1137,7 @@ func Test_setbufvar_options()
wincmd h wincmd h
let wh = winheight('.') let wh = winheight('.')
let dummy_buf = bufnr('dummy_buf2', v:true) let dummy_buf = bufnr('dummy_buf2', v:true)
call setbufvar(dummy_buf, '&buftype', 'nofile') eval 'nofile'->setbufvar(dummy_buf, '&buftype')
execute 'belowright vertical split #' . dummy_buf execute 'belowright vertical split #' . dummy_buf
call assert_equal(wh, winheight('.')) call assert_equal(wh, winheight('.'))
@@ -1413,7 +1413,7 @@ func Test_func_range_with_edit()
" is invalid in that buffer. " is invalid in that buffer.
call writefile(['just one line'], 'Xfuncrange2') call writefile(['just one line'], 'Xfuncrange2')
new new
call setline(1, 10->range()) eval 10->range()->setline(1)
write Xfuncrange1 write Xfuncrange1
call assert_fails('5,8call EditAnotherFile()', 'E16:') call assert_fails('5,8call EditAnotherFile()', 'E16:')

View File

@@ -10,7 +10,7 @@ endif
func s:screenline(lnum) abort func s:screenline(lnum) abort
let line = [] let line = []
for c in range(1, winwidth(0)) for c in range(1, winwidth(0))
call add(line, nr2char(screenchar(a:lnum, c))) call add(line, nr2char(a:lnum->screenchar(c)))
endfor endfor
return s:trim(join(line, '')) return s:trim(join(line, ''))
endfunc endfunc

View File

@@ -833,7 +833,7 @@ func Test_popup_time()
sleep 700m sleep 700m
redraw redraw
let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
call assert_equal('hello', line) call assert_equal('hello', line)
call popup_create('on the command line', #{ call popup_create('on the command line', #{

View File

@@ -1280,7 +1280,7 @@ func Test_search_match_at_curpos()
normal gg normal gg
call search('foobar', 'c') eval 'foobar'->search('c')
call assert_equal([1, 1], [line('.'), col('.')]) call assert_equal([1, 1], [line('.'), col('.')])
normal j normal j
@@ -1318,3 +1318,38 @@ func Test_search_display_pattern()
set norl set norl
endif endif
endfunc endfunc
func Test_searchdecl()
let lines =<< trim END
int global;
func()
{
int global;
if (cond) {
int local;
}
int local;
// comment
}
END
new
call setline(1, lines)
10
call assert_equal(0, searchdecl('local', 0, 0))
call assert_equal(7, getcurpos()[1])
10
call assert_equal(0, 'local'->searchdecl(0, 1))
call assert_equal(9, getcurpos()[1])
10
call assert_equal(0, searchdecl('global'))
call assert_equal(5, getcurpos()[1])
10
call assert_equal(0, searchdecl('global', 1))
call assert_equal(1, getcurpos()[1])
bwipe!
endfunc

View File

@@ -7,7 +7,7 @@ func Test_searchpos()
call cursor(1, 1) call cursor(1, 1)
call assert_equal([1, 1, 2], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW')) call assert_equal([1, 1, 2], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW'))
call cursor(1, 2) call cursor(1, 2)
call assert_equal([2, 1, 1], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW')) call assert_equal([2, 1, 1], '\%(\([a-z]\)\|\_.\)\{-}xyz'->searchpos('pcW'))
set cpo-=c set cpo-=c
call cursor(1, 2) call cursor(1, 2)
call assert_equal([1, 2, 2], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW')) call assert_equal([1, 2, 2], searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW'))

View File

@@ -112,7 +112,7 @@ func Test_screenchar_utf8()
call setline(1, ["ABC\u0308"]) call setline(1, ["ABC\u0308"])
redraw redraw
call assert_equal([0x0041], screenchars(1, 1)) call assert_equal([0x0041], screenchars(1, 1))
call assert_equal([0x0042], screenchars(1, 2)) call assert_equal([0x0042], 1->screenchars(2))
call assert_equal([0x0043, 0x0308], screenchars(1, 3)) call assert_equal([0x0043, 0x0308], screenchars(1, 3))
call assert_equal("A", screenstring(1, 1)) call assert_equal("A", screenstring(1, 1))
call assert_equal("B", screenstring(1, 2)) call assert_equal("B", screenstring(1, 2))

View File

@@ -757,6 +757,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 */
/**/
1993,
/**/ /**/
1992, 1992,
/**/ /**/