1
0
forked from aniani/vim

patch 8.1.1915: 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-23 22:31:37 +02:00
parent d019039ccd
commit 1a3a89168d
12 changed files with 84 additions and 31 deletions

View File

@ -3410,6 +3410,9 @@ char2nr({expr} [, {utf8}]) *char2nr()*
let list = map(split(str, '\zs'), {_, val -> char2nr(val)}) let list = map(split(str, '\zs'), {_, val -> char2nr(val)})
< Result: [65, 66, 67] < Result: [65, 66, 67]
Can also be used as a |method|: >
GetChar()->char2nr()
chdir({dir}) *chdir()* chdir({dir}) *chdir()*
Change the current working directory to {dir}. The scope of Change the current working directory to {dir}. The scope of
the directory change depends on the directory of the current the directory change depends on the directory of the current
@ -3430,6 +3433,9 @@ chdir({dir}) *chdir()*
" ... do some work " ... do some work
call chdir(save_dir) call chdir(save_dir)
endif endif
< Can also be used as a |method|: >
GetDir()->chdir()
< <
cindent({lnum}) *cindent()* cindent({lnum}) *cindent()*
Get the amount of indent for line {lnum} according the C Get the amount of indent for line {lnum} according the C
@ -3440,12 +3446,18 @@ cindent({lnum}) *cindent()*
feature, -1 is returned. feature, -1 is returned.
See |C-indenting|. See |C-indenting|.
Can also be used as a |method|: >
GetLnum()->cindent()
clearmatches([{win}]) *clearmatches()* clearmatches([{win}]) *clearmatches()*
Clears all matches previously defined for the current window Clears all matches previously defined for the current window
by |matchadd()| and the |:match| commands. by |matchadd()| and the |:match| commands.
If {win} is specified, use the window with this number or If {win} is specified, use the window with this number or
window ID instead of the current window. window ID instead of the current window.
Can also be used as a |method|: >
GetWin()->clearmatches()
<
*col()* *col()*
col({expr}) The result is a Number, which is the byte index of the column col({expr}) The result is a Number, which is the byte index of the column
position given with {expr}. The accepted positions are: position given with {expr}. The accepted positions are:
@ -3481,6 +3493,9 @@ col({expr}) The result is a Number, which is the byte index of the column
\<C-O>:set ve=all<CR> \<C-O>:set ve=all<CR>
\<C-O>:echo col(".") . "\n" <Bar> \<C-O>:echo col(".") . "\n" <Bar>
\let &ve = save_ve<CR> \let &ve = save_ve<CR>
< Can also be used as a |method|: >
GetPos()->col()
< <
complete({startcol}, {matches}) *complete()* *E785* complete({startcol}, {matches}) *complete()* *E785*
@ -3512,6 +3527,10 @@ complete({startcol}, {matches}) *complete()* *E785*
< This isn't very useful, but it shows how it works. Note that < This isn't very useful, but it shows how it works. Note that
an empty string is returned to avoid a zero being inserted. an empty string is returned to avoid a zero being inserted.
Can also be used as a |method|, the second argument is passed
in: >
GetMatches()->complete(col('.'))
complete_add({expr}) *complete_add()* complete_add({expr}) *complete_add()*
Add {expr} to the list of matches. Only to be used by the Add {expr} to the list of matches. Only to be used by the
function specified with the 'completefunc' option. function specified with the 'completefunc' option.
@ -3521,6 +3540,9 @@ complete_add({expr}) *complete_add()*
See |complete-functions| for an explanation of {expr}. It is See |complete-functions| for an explanation of {expr}. It is
the same as one item in the list that 'omnifunc' would return. the same as one item in the list that 'omnifunc' would return.
Can also be used as a |method|: >
GetMoreMatches()->complete_add()
complete_check() *complete_check()* complete_check() *complete_check()*
Check for a key typed while looking for completion matches. Check for a key typed while looking for completion matches.
This is to be used when looking for matches takes some time. This is to be used when looking for matches takes some time.
@ -3581,6 +3603,9 @@ complete_info([{what}])
call complete_info(['mode']) call complete_info(['mode'])
" Get only 'mode' and 'pum_visible' " Get only 'mode' and 'pum_visible'
call complete_info(['mode', 'pum_visible']) call complete_info(['mode', 'pum_visible'])
< Can also be used as a |method|: >
GetItems()->complete_info()
< <
*confirm()* *confirm()*
confirm({msg} [, {choices} [, {default} [, {type}]]]) confirm({msg} [, {choices} [, {default} [, {type}]]])
@ -3636,6 +3661,9 @@ confirm({msg} [, {choices} [, {default} [, {type}]]])
don't fit, a vertical layout is used anyway. For some systems don't fit, a vertical layout is used anyway. For some systems
the horizontal layout is always used. the horizontal layout is always used.
Can also be used as a |method|in: >
BuildMessage()->confirm("&Yes\n&No")
*copy()* *copy()*
copy({expr}) Make a copy of {expr}. For Numbers and Strings this isn't copy({expr}) Make a copy of {expr}. For Numbers and Strings this isn't
different from using {expr} directly. different from using {expr} directly.
@ -3765,12 +3793,18 @@ cursor({list})
position within a <Tab> or after the last character. position within a <Tab> or after the last character.
Returns 0 when the position could be set, -1 otherwise. Returns 0 when the position could be set, -1 otherwise.
Can also be used as a |method|: >
GetCursorPos()->cursor()
debugbreak({pid}) *debugbreak()* debugbreak({pid}) *debugbreak()*
Specifically used to interrupt a program being debugged. It Specifically used to interrupt a program being debugged. It
will cause process {pid} to get a SIGTRAP. Behavior for other will cause process {pid} to get a SIGTRAP. Behavior for other
processes is undefined. See |terminal-debugger|. processes is undefined. See |terminal-debugger|.
{only available on MS-Windows} {only available on MS-Windows}
Can also be used as a |method|: >
GetPid()->debugbreak()
deepcopy({expr} [, {noref}]) *deepcopy()* *E698* deepcopy({expr} [, {noref}]) *deepcopy()* *E698*
Make a copy of {expr}. For Numbers and Strings this isn't Make a copy of {expr}. For Numbers and Strings this isn't
different from using {expr} directly. different from using {expr} directly.
@ -3792,6 +3826,9 @@ deepcopy({expr} [, {noref}]) *deepcopy()* *E698*
{noref} set to 1 will fail. {noref} set to 1 will fail.
Also see |copy()|. Also see |copy()|.
Can also be used as a |method|: >
GetObject()->deepcopy()
delete({fname} [, {flags}]) *delete()* delete({fname} [, {flags}]) *delete()*
Without {flags} or with {flags} empty: Deletes the file by the Without {flags} or with {flags} empty: Deletes the file by the
name {fname}. This also works when {fname} is a symbolic link. name {fname}. This also works when {fname} is a symbolic link.
@ -3813,6 +3850,9 @@ delete({fname} [, {flags}]) *delete()*
To delete a line from the buffer use |:delete| or To delete a line from the buffer use |:delete| or
|deletebufline()|. |deletebufline()|.
Can also be used as a |method|: >
GetName()->delete()
deletebufline({expr}, {first} [, {last}]) *deletebufline()* deletebufline({expr}, {first} [, {last}]) *deletebufline()*
Delete lines {first} to {last} (inclusive) from buffer {expr}. Delete lines {first} to {last} (inclusive) from buffer {expr}.
If {last} is omitted then delete line {first} only. If {last} is omitted then delete line {first} only.
@ -3824,6 +3864,9 @@ deletebufline({expr}, {first} [, {last}]) *deletebufline()*
when using |line()| this refers to the current buffer. Use "$" when using |line()| this refers to the current buffer. Use "$"
to refer to the last line in buffer {expr}. to refer to the last line in buffer {expr}.
Can also be used as a |method|: >
GetBuffer()->deletebufline(1)
*did_filetype()* *did_filetype()*
did_filetype() Returns |TRUE| when autocommands are being executed and the did_filetype() Returns |TRUE| when autocommands are being executed and the
FileType event has been triggered at least once. Can be used FileType event has been triggered at least once. Can be used
@ -3845,6 +3888,9 @@ diff_filler({lnum}) *diff_filler()*
line, "'m" mark m, etc. line, "'m" mark m, etc.
Returns 0 if the current window is not in diff mode. Returns 0 if the current window is not in diff mode.
Can also be used as a |method|: >
GetLnum()->diff_filler()
diff_hlID({lnum}, {col}) *diff_hlID()* diff_hlID({lnum}, {col}) *diff_hlID()*
Returns the highlight ID for diff mode at line {lnum} column Returns the highlight ID for diff mode at line {lnum} column
{col} (byte index). When the current line does not have a {col} (byte index). When the current line does not have a
@ -3856,6 +3902,8 @@ diff_hlID({lnum}, {col}) *diff_hlID()*
The highlight ID can be used with |synIDattr()| to obtain The highlight ID can be used with |synIDattr()| to obtain
syntax information about the highlighting. syntax information about the highlighting.
Can also be used as a |method|: >
GetLnum()->diff_hlID(col)
environ() *environ()* environ() *environ()*
Return all of environment variables as dictionary. You can Return all of environment variables as dictionary. You can
check if an environment variable exists like this: > check if an environment variable exists like this: >

View File

@ -494,16 +494,16 @@ static funcentry_T global_functions[] =
{"ch_status", 1, 2, FEARG_1, f_ch_status}, {"ch_status", 1, 2, FEARG_1, f_ch_status},
#endif #endif
{"changenr", 0, 0, 0, f_changenr}, {"changenr", 0, 0, 0, f_changenr},
{"char2nr", 1, 2, 0, f_char2nr}, {"char2nr", 1, 2, FEARG_1, f_char2nr},
{"chdir", 1, 1, 0, f_chdir}, {"chdir", 1, 1, FEARG_1, f_chdir},
{"cindent", 1, 1, 0, f_cindent}, {"cindent", 1, 1, FEARG_1, f_cindent},
{"clearmatches", 0, 1, 0, f_clearmatches}, {"clearmatches", 0, 1, FEARG_1, f_clearmatches},
{"col", 1, 1, 0, f_col}, {"col", 1, 1, FEARG_1, f_col},
{"complete", 2, 2, 0, f_complete}, {"complete", 2, 2, FEARG_2, f_complete},
{"complete_add", 1, 1, 0, f_complete_add}, {"complete_add", 1, 1, FEARG_1, f_complete_add},
{"complete_check", 0, 0, 0, f_complete_check}, {"complete_check", 0, 0, 0, f_complete_check},
{"complete_info", 0, 1, 0, f_complete_info}, {"complete_info", 0, 1, FEARG_1, f_complete_info},
{"confirm", 1, 4, 0, f_confirm}, {"confirm", 1, 4, FEARG_1, f_confirm},
{"copy", 1, 1, FEARG_1, f_copy}, {"copy", 1, 1, FEARG_1, f_copy},
#ifdef FEAT_FLOAT #ifdef FEAT_FLOAT
{"cos", 1, 1, FEARG_1, f_cos}, {"cos", 1, 1, FEARG_1, f_cos},
@ -511,16 +511,16 @@ static funcentry_T global_functions[] =
#endif #endif
{"count", 2, 4, FEARG_1, f_count}, {"count", 2, 4, FEARG_1, f_count},
{"cscope_connection",0,3, 0, f_cscope_connection}, {"cscope_connection",0,3, 0, f_cscope_connection},
{"cursor", 1, 3, 0, f_cursor}, {"cursor", 1, 3, FEARG_1, f_cursor},
#ifdef MSWIN #ifdef MSWIN
{"debugbreak", 1, 1, 0, f_debugbreak}, {"debugbreak", 1, 1, FEARG_1, f_debugbreak},
#endif #endif
{"deepcopy", 1, 2, 0, f_deepcopy}, {"deepcopy", 1, 2, FEARG_1, f_deepcopy},
{"delete", 1, 2, 0, f_delete}, {"delete", 1, 2, FEARG_1, f_delete},
{"deletebufline", 2, 3, 0, f_deletebufline}, {"deletebufline", 2, 3, FEARG_1, f_deletebufline},
{"did_filetype", 0, 0, 0, f_did_filetype}, {"did_filetype", 0, 0, 0, f_did_filetype},
{"diff_filler", 1, 1, 0, f_diff_filler}, {"diff_filler", 1, 1, FEARG_1, f_diff_filler},
{"diff_hlID", 2, 2, 0, f_diff_hlID}, {"diff_hlID", 2, 2, FEARG_1, f_diff_hlID},
{"empty", 1, 1, FEARG_1, f_empty}, {"empty", 1, 1, FEARG_1, f_empty},
{"environ", 0, 0, 0, f_environ}, {"environ", 0, 0, 0, f_environ},
{"escape", 2, 2, 0, f_escape}, {"escape", 2, 2, 0, f_escape},

View File

@ -132,7 +132,7 @@ func Test_deletebufline()
call assert_equal(0, deletebufline(b, 2, 8)) call assert_equal(0, deletebufline(b, 2, 8))
call assert_equal(['aaa'], getbufline(b, 1, 2)) call assert_equal(['aaa'], getbufline(b, 1, 2))
exe "bd!" b exe "bd!" b
call assert_equal(1, deletebufline(b, 1)) call assert_equal(1, b->deletebufline(1))
call assert_equal(1, deletebufline(-1, 1)) call assert_equal(1, deletebufline(-1, 1))

View File

@ -85,7 +85,7 @@ func Test_chdir_func()
call assert_equal('y', fnamemodify(getcwd(1, 2), ':t')) call assert_equal('y', fnamemodify(getcwd(1, 2), ':t'))
call assert_equal('z', fnamemodify(getcwd(3, 2), ':t')) call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))
tabnext | wincmd t tabnext | wincmd t
call chdir('..') eval '..'->chdir()
call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t')) call assert_equal('Xdir', fnamemodify(getcwd(1, 2), ':t'))
call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t')) call assert_equal('Xdir', fnamemodify(getcwd(2, 2), ':t'))
call assert_equal('z', fnamemodify(getcwd(3, 2), ':t')) call assert_equal('z', fnamemodify(getcwd(3, 2), ':t'))

View File

@ -121,9 +121,9 @@ func Test_cindent_func()
new new
setlocal cindent setlocal cindent
call setline(1, ['int main(void)', '{', 'return 0;', '}']) call setline(1, ['int main(void)', '{', 'return 0;', '}'])
call assert_equal(cindent(0), -1) call assert_equal(-1, cindent(0))
call assert_equal(cindent(3), &sw) call assert_equal(&sw, 3->cindent())
call assert_equal(cindent(line('$')+1), -1) call assert_equal(-1, cindent(line('$')+1))
bwipe! bwipe!
endfunc endfunc

View File

@ -22,7 +22,7 @@ func Test_move_cursor()
call cursor(3, 0) call cursor(3, 0)
call assert_equal([3, 1, 0, 1], getcurpos()[1:]) call assert_equal([3, 1, 0, 1], getcurpos()[1:])
" below last line goes to last line " below last line goes to last line
call cursor(9, 1) eval [9, 1]->cursor()
call assert_equal([4, 1, 0, 1], getcurpos()[1:]) call assert_equal([4, 1, 0, 1], getcurpos()[1:])
call setline(1, ["\<TAB>"]) call setline(1, ["\<TAB>"])

View File

@ -674,7 +674,7 @@ func Test_diff_hlID()
call diff_hlID(1, 2)->synIDattr("name")->assert_equal("DiffText") call diff_hlID(1, 2)->synIDattr("name")->assert_equal("DiffText")
call diff_hlID(2, 1)->synIDattr("name")->assert_equal("") call diff_hlID(2, 1)->synIDattr("name")->assert_equal("")
call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd") call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd")
call diff_hlID(4, 1)->synIDattr("name")->assert_equal("") eval 4->diff_hlID(1)->synIDattr("name")->assert_equal("")
wincmd w wincmd w
call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange") call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
@ -693,7 +693,7 @@ func Test_diff_filler()
diffthis diffthis
redraw redraw
call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'diff_filler(v:val)')) call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'v:val->diff_filler()'))
wincmd w wincmd w
call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)')) call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))

View File

@ -1078,7 +1078,7 @@ func Test_col()
call assert_equal(7, col('$')) call assert_equal(7, col('$'))
call assert_equal(4, col("'x")) call assert_equal(4, col("'x"))
call assert_equal(6, col("'Y")) call assert_equal(6, col("'Y"))
call assert_equal(2, col([1, 2])) call assert_equal(2, [1, 2]->col())
call assert_equal(7, col([1, '$'])) call assert_equal(7, col([1, '$']))
call assert_equal(0, col('')) call assert_equal(0, col(''))
@ -1413,7 +1413,7 @@ func Test_confirm()
call assert_equal(1, a) call assert_equal(1, a)
call feedkeys('y', 'L') call feedkeys('y', 'L')
let a = confirm('Are you sure?', "&Yes\n&No") let a = 'Are you sure?'->confirm("&Yes\n&No")
call assert_equal(1, a) call assert_equal(1, a)
call feedkeys('n', 'L') call feedkeys('n', 'L')
@ -1514,7 +1514,7 @@ func Test_readdir()
let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1}) let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
call assert_equal(1, len(files)) call assert_equal(1, len(files))
call delete('Xdir', 'rf') eval 'Xdir'->delete('rf')
endfunc endfunc
func Test_delete_rf() func Test_delete_rf()
@ -1548,6 +1548,7 @@ endfunc
func Test_char2nr() func Test_char2nr()
call assert_equal(12354, char2nr('あ', 1)) call assert_equal(12354, char2nr('あ', 1))
call assert_equal(120, 'x'->char2nr())
endfunc endfunc
func Test_eventhandler() func Test_eventhandler()

View File

@ -215,7 +215,7 @@ func Test_matchaddpos_otherwin()
\] \]
call assert_equal(expect, savematches) call assert_equal(expect, savematches)
call clearmatches(winid) eval winid->clearmatches()
call assert_equal([], getmatches(winid)) call assert_equal([], getmatches(winid))
call setmatches(savematches, winid) call setmatches(savematches, winid)

View File

@ -8,6 +8,7 @@ func Test_list_method()
eval l->assert_notequal([3, 2, 1]) eval l->assert_notequal([3, 2, 1])
eval l->assert_notequal([3, 2, 1], 'wrong') eval l->assert_notequal([3, 2, 1], 'wrong')
call assert_equal(l, l->copy()) call assert_equal(l, l->copy())
call assert_equal(l, l->deepcopy())
call assert_equal(1, l->count(2)) call assert_equal(1, l->count(2))
call assert_false(l->empty()) call assert_false(l->empty())
call assert_true([]->empty()) call assert_true([]->empty())
@ -38,6 +39,7 @@ func Test_dict_method()
let d = #{one: 1, two: 2, three: 3} let d = #{one: 1, two: 2, three: 3}
call assert_equal(d, d->copy()) call assert_equal(d, d->copy())
call assert_equal(d, d->deepcopy())
call assert_equal(1, d->count(2)) call assert_equal(1, d->count(2))
call assert_false(d->empty()) call assert_false(d->empty())
call assert_true({}->empty()) call assert_true({}->empty())

View File

@ -250,7 +250,7 @@ endfunc
func Test_noinsert_complete() func Test_noinsert_complete()
func! s:complTest1() abort func! s:complTest1() abort
call complete(1, ['source', 'soundfold']) eval ['source', 'soundfold']->complete(1)
return '' return ''
endfunc endfunc
@ -403,7 +403,7 @@ func DummyCompleteFour(findstart, base)
return 0 return 0
else else
call complete_add('four1') call complete_add('four1')
call complete_add('four2') eval 'four2'->complete_add()
call complete_check() call complete_check()
call complete_add('four3') call complete_add('four3')
call complete_add('four4') call complete_add('four4')
@ -993,7 +993,7 @@ func GetCompleteInfo()
if empty(g:compl_what) if empty(g:compl_what)
let g:compl_info = complete_info() let g:compl_info = complete_info()
else else
let g:compl_info = complete_info(g:compl_what) let g:compl_info = g:compl_what->complete_info()
endif endif
return '' return ''
endfunc endfunc

View File

@ -761,6 +761,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 */
/**/
1915,
/**/ /**/
1914, 1914,
/**/ /**/