Skip to content

Commit f9f24ce

Browse files
committed
patch 8.1.1953: more functions can be used as a method
Problem: More functions can be used as a method. Solution: Allow more functions to be used as a method.
1 parent 5d69fdb commit f9f24ce

13 files changed

Lines changed: 106 additions & 35 deletions

runtime/doc/eval.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5774,6 +5774,9 @@ haslocaldir([{winnr} [, {tabnr}]]) *haslocaldir()*
57745774
" tab page m
57755775
:echo haslocaldir(-1, m)
57765776
<
5777+
Can also be used as a |method|: >
5778+
GetWinnr()->haslocaldir()
5779+
57775780
hasmapto({what} [, {mode} [, {abbr}]]) *hasmapto()*
57785781
The result is a Number, which is 1 if there is a mapping that
57795782
contains {what} in somewhere in the rhs (what it is mapped to)
@@ -5802,6 +5805,9 @@ hasmapto({what} [, {mode} [, {abbr}]]) *hasmapto()*
58025805
< This installs the mapping to "\ABCdoit" only if there isn't
58035806
already a mapping to "\ABCdoit".
58045807

5808+
Can also be used as a |method|: >
5809+
GetRHS()->hasmapto()
5810+
58055811
histadd({history}, {item}) *histadd()*
58065812
Add the String {item} to the history {history} which can be
58075813
one of: *hist-names*
@@ -5823,6 +5829,10 @@ histadd({history}, {item}) *histadd()*
58235829
:let date=input("Enter date: ")
58245830
< This function is not available in the |sandbox|.
58255831

5832+
Can also be used as a |method|, the base is used for the
5833+
second argument: >
5834+
GetPattern()->histadd('search')
5835+
58265836
histdel({history} [, {item}]) *histdel()*
58275837
Clear {history}, i.e. delete all its entries. See |hist-names|
58285838
for the possible values of {history}.
@@ -5854,6 +5864,9 @@ histdel({history} [, {item}]) *histdel()*
58545864
the "n" command and 'hlsearch': >
58555865
:call histdel("search", -1)
58565866
:let @/ = histget("search", -1)
5867+
<
5868+
Can also be used as a |method|: >
5869+
GetHistory()->histdel()
58575870
58585871
histget({history} [, {index}]) *histget()*
58595872
The result is a String, the entry with Number {index} from
@@ -5870,13 +5883,19 @@ histget({history} [, {index}]) *histget()*
58705883
the {num}th entry from the output of |:history|. >
58715884
:command -nargs=1 H execute histget("cmd", 0+<args>)
58725885
<
5886+
Can also be used as a |method|: >
5887+
GetHistory()->histget()
5888+
58735889
histnr({history}) *histnr()*
58745890
The result is the Number of the current entry in {history}.
58755891
See |hist-names| for the possible values of {history}.
58765892
If an error occurred, -1 is returned.
58775893

58785894
Example: >
58795895
:let inp_index = histnr("expr")
5896+
5897+
< Can also be used as a |method|: >
5898+
GetHistory()->histnr()
58805899
<
58815900
hlexists({name}) *hlexists()*
58825901
The result is a Number, which is non-zero if a highlight group
@@ -5887,6 +5906,9 @@ hlexists({name}) *hlexists()*
58875906
*highlight_exists()*
58885907
Obsolete name: highlight_exists().
58895908

5909+
Can also be used as a |method|: >
5910+
GetName()->hlexists()
5911+
<
58905912
*hlID()*
58915913
hlID({name}) The result is a Number, which is the ID of the highlight group
58925914
with name {name}. When the highlight group doesn't exist,
@@ -5898,6 +5920,9 @@ hlID({name}) The result is a Number, which is the ID of the highlight group
58985920
< *highlightID()*
58995921
Obsolete name: highlightID().
59005922

5923+
Can also be used as a |method|: >
5924+
GetName()->hlID()
5925+
59015926
hostname() *hostname()*
59025927
The result is a String, which is the name of the machine on
59035928
which Vim is currently running. Machine names greater than
@@ -5922,13 +5947,18 @@ iconv({expr}, {from}, {to}) *iconv()*
59225947
from/to UCS-2 is automatically changed to use UTF-8. You
59235948
cannot use UCS-2 in a string anyway, because of the NUL bytes.
59245949

5950+
Can also be used as a |method|: >
5951+
GetText()->iconv('latin1', 'utf-8')
5952+
<
59255953
*indent()*
59265954
indent({lnum}) The result is a Number, which is indent of line {lnum} in the
59275955
current buffer. The indent is counted in spaces, the value
59285956
of 'tabstop' is relevant. {lnum} is used just like in
59295957
|getline()|.
59305958
When {lnum} is invalid -1 is returned.
59315959

5960+
Can also be used as a |method|: >
5961+
GetLnum()->indent()
59325962
59335963
index({object}, {expr} [, {start} [, {ic}]]) *index()*
59345964
If {object} is a |List| return the lowest index where the item
@@ -5949,6 +5979,8 @@ index({object}, {expr} [, {start} [, {ic}]]) *index()*
59495979
:let idx = index(words, "the")
59505980
:if index(numbers, 123) >= 0
59515981
5982+
< Can also be used as a |method|: >
5983+
GetObject()->index(what)
59525984
59535985
input({prompt} [, {text} [, {completion}]]) *input()*
59545986
The result is a String, which is whatever the user typed on
@@ -5995,6 +6027,9 @@ input({prompt} [, {text} [, {completion}]]) *input()*
59956027
: call inputrestore()
59966028
:endfunction
59976029
6030+
< Can also be used as a |method|: >
6031+
GetPrompt()->input()
6032+
59986033
inputdialog({prompt} [, {text} [, {cancelreturn}]]) *inputdialog()*
59996034
Like |input()|, but when the GUI is running and text dialogs
60006035
are supported, a dialog window pops up to input the text.
@@ -6009,6 +6044,9 @@ inputdialog({prompt} [, {text} [, {cancelreturn}]]) *inputdialog()*
60096044
<Esc> works like pressing the Cancel button.
60106045
NOTE: Command-line completion is not supported.
60116046

6047+
Can also be used as a |method|: >
6048+
GetPrompt()->inputdialog()
6049+
60126050
inputlist({textlist}) *inputlist()*
60136051
{textlist} must be a |List| of strings. This |List| is
60146052
displayed, one string per line. The user will be prompted to
@@ -6025,6 +6063,9 @@ inputlist({textlist}) *inputlist()*
60256063
let color = inputlist(['Select color:', '1. red',
60266064
\ '2. green', '3. blue'])
60276065
6066+
< Can also be used as a |method|: >
6067+
GetChoices()->inputlist()
6068+
60286069
inputrestore() *inputrestore()*
60296070
Restore typeahead that was saved with a previous |inputsave()|.
60306071
Should be called the same number of times inputsave() is
@@ -6050,6 +6091,9 @@ inputsecret({prompt} [, {text}]) *inputsecret()*
60506091
typed on the command-line in response to the issued prompt.
60516092
NOTE: Command-line completion is not supported.
60526093

6094+
Can also be used as a |method|: >
6095+
GetPrompt()->inputsecret()
6096+
60536097
insert({object}, {item} [, {idx}]) *insert()*
60546098
When {object} is a |List| or a |Blob| insert {item} at the start
60556099
of it.
@@ -6083,6 +6127,9 @@ isdirectory({directory}) *isdirectory()*
60836127
exist, or isn't a directory, the result is |FALSE|. {directory}
60846128
is any expression, which is used as a String.
60856129

6130+
Can also be used as a |method|: >
6131+
GetName()->isdirectory()
6132+
60866133
isinf({expr}) *isinf()*
60876134
Return 1 if {expr} is a positive infinity, or -1 a negative
60886135
infinity, otherwise 0. >
@@ -6109,6 +6156,9 @@ islocked({expr}) *islocked()* *E786*
61096156
< When {expr} is a variable that does not exist you get an error
61106157
message. Use |exists()| to check for existence.
61116158

6159+
Can also be used as a |method|: >
6160+
GetName()->islocked()
6161+
61126162
isnan({expr}) *isnan()*
61136163
Return |TRUE| if {expr} is a float with value NaN. >
61146164
echo isnan(0.0 / 0.0)

src/evalfunc.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -595,33 +595,33 @@ static funcentry_T global_functions[] =
595595
{"globpath", 2, 5, FEARG_2, f_globpath},
596596
{"has", 1, 1, 0, f_has},
597597
{"has_key", 2, 2, FEARG_1, f_has_key},
598-
{"haslocaldir", 0, 2, 0, f_haslocaldir},
599-
{"hasmapto", 1, 3, 0, f_hasmapto},
600-
{"highlightID", 1, 1, 0, f_hlID}, // obsolete
601-
{"highlight_exists",1, 1, 0, f_hlexists}, // obsolete
602-
{"histadd", 2, 2, 0, f_histadd},
603-
{"histdel", 1, 2, 0, f_histdel},
604-
{"histget", 1, 2, 0, f_histget},
605-
{"histnr", 1, 1, 0, f_histnr},
606-
{"hlID", 1, 1, 0, f_hlID},
607-
{"hlexists", 1, 1, 0, f_hlexists},
598+
{"haslocaldir", 0, 2, FEARG_1, f_haslocaldir},
599+
{"hasmapto", 1, 3, FEARG_1, f_hasmapto},
600+
{"highlightID", 1, 1, FEARG_1, f_hlID}, // obsolete
601+
{"highlight_exists",1, 1, FEARG_1, f_hlexists}, // obsolete
602+
{"histadd", 2, 2, FEARG_2, f_histadd},
603+
{"histdel", 1, 2, FEARG_1, f_histdel},
604+
{"histget", 1, 2, FEARG_1, f_histget},
605+
{"histnr", 1, 1, FEARG_1, f_histnr},
606+
{"hlID", 1, 1, FEARG_1, f_hlID},
607+
{"hlexists", 1, 1, FEARG_1, f_hlexists},
608608
{"hostname", 0, 0, 0, f_hostname},
609-
{"iconv", 3, 3, 0, f_iconv},
610-
{"indent", 1, 1, 0, f_indent},
609+
{"iconv", 3, 3, FEARG_1, f_iconv},
610+
{"indent", 1, 1, FEARG_1, f_indent},
611611
{"index", 2, 4, FEARG_1, f_index},
612-
{"input", 1, 3, 0, f_input},
613-
{"inputdialog", 1, 3, 0, f_inputdialog},
614-
{"inputlist", 1, 1, 0, f_inputlist},
612+
{"input", 1, 3, FEARG_1, f_input},
613+
{"inputdialog", 1, 3, FEARG_1, f_inputdialog},
614+
{"inputlist", 1, 1, FEARG_1, f_inputlist},
615615
{"inputrestore", 0, 0, 0, f_inputrestore},
616616
{"inputsave", 0, 0, 0, f_inputsave},
617-
{"inputsecret", 1, 2, 0, f_inputsecret},
617+
{"inputsecret", 1, 2, FEARG_1, f_inputsecret},
618618
{"insert", 2, 3, FEARG_1, f_insert},
619619
{"invert", 1, 1, FEARG_1, f_invert},
620-
{"isdirectory", 1, 1, 0, f_isdirectory},
620+
{"isdirectory", 1, 1, FEARG_1, f_isdirectory},
621621
#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
622622
{"isinf", 1, 1, FEARG_1, f_isinf},
623623
#endif
624-
{"islocked", 1, 1, 0, f_islocked},
624+
{"islocked", 1, 1, FEARG_1, f_islocked},
625625
#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
626626
{"isnan", 1, 1, FEARG_1, f_isnan},
627627
#endif

src/testdir/test_blob.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func Test_blob_index()
278278
call assert_equal(2, index(0zDEADBEEF, 0xBE))
279279
call assert_equal(-1, index(0zDEADBEEF, 0))
280280
call assert_equal(2, index(0z11111111, 0x11, 2))
281-
call assert_equal(3, index(0z11110111, 0x11, 2))
281+
call assert_equal(3, 0z11110111->index(0x11, 2))
282282
call assert_equal(2, index(0z11111111, 0x11, -2))
283283
call assert_equal(3, index(0z11110111, 0x11, -2))
284284

src/testdir/test_breakindent.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func Test_breakindent11()
411411
" test strdisplaywidth()
412412
call s:test_windows('setl cpo-=n sbr=>> nu nuw=4 nolist briopt= ts=4')
413413
let text = getline(2)
414-
let width = strlen(text[1:])+indent(2)+strlen(&sbr)*3 " text wraps 3 times
414+
let width = strlen(text[1:]) + indent(2) + strlen(&sbr) * 3 " text wraps 3 times
415415
call assert_equal(width, strdisplaywidth(text))
416416
call s:close_windows('set sbr=')
417417
endfunc
@@ -423,7 +423,7 @@ func Test_breakindent11_vartabs()
423423
" test strdisplaywidth()
424424
call s:test_windows('setl cpo-=n sbr=>> nu nuw=4 nolist briopt= ts=4 vts=4')
425425
let text = getline(2)
426-
let width = strlen(text[1:])+indent(2)+strlen(&sbr)*3 " text wraps 3 times
426+
let width = strlen(text[1:]) + 2->indent() + strlen(&sbr) * 3 " text wraps 3 times
427427
call assert_equal(width, strdisplaywidth(text))
428428
call s:close_windows('set sbr= vts&')
429429
endfunc

src/testdir/test_delete.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Test_recursive_delete()
3232
call assert_equal(['a', 'b'], readfile('Xdir1/Xfile'))
3333
call assert_true(isdirectory('Xdir1/subdir'))
3434
call assert_equal(['a', 'b'], readfile('Xdir1/subdir/Xfile'))
35-
call assert_true(isdirectory('Xdir1/empty'))
35+
call assert_true('Xdir1/empty'->isdirectory())
3636
call assert_equal(0, delete('Xdir1', 'rf'))
3737
call assert_false(isdirectory('Xdir1'))
3838
call assert_equal(-1, delete('Xdir1', 'd'))

src/testdir/test_functions.vim

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ endfunc
10691069

10701070
func Test_hlexists()
10711071
call assert_equal(0, hlexists('does_not_exist'))
1072-
call assert_equal(0, hlexists('Number'))
1072+
call assert_equal(0, 'Number'->hlexists())
10731073
call assert_equal(0, highlight_exists('does_not_exist'))
10741074
call assert_equal(0, highlight_exists('Number'))
10751075
syntax on
@@ -1102,7 +1102,7 @@ endfunc
11021102
func Test_inputlist()
11031103
call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
11041104
call assert_equal(1, c)
1105-
call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
1105+
call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
11061106
call assert_equal(2, c)
11071107
call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
11081108
call assert_equal(3, c)
@@ -1279,7 +1279,7 @@ func Test_reg_executing_and_recording()
12791279
let g:regs = []
12801280
func TestFunc() abort
12811281
let g:regs += [reg_executing()]
1282-
let g:typed = input('?')
1282+
let g:typed = '?'->input()
12831283
let g:regs += [reg_executing()]
12841284
endfunc
12851285
call feedkeys("@qy\<CR>", 'xt')
@@ -1295,6 +1295,25 @@ func Test_reg_executing_and_recording()
12951295
unlet s:reg_stat
12961296
endfunc
12971297

1298+
func Test_inputsecret()
1299+
map W :call TestFunc()<CR>
1300+
let @q = "W"
1301+
let g:typed1 = ''
1302+
let g:typed2 = ''
1303+
let g:regs = []
1304+
func TestFunc() abort
1305+
let g:typed1 = '?'->inputsecret()
1306+
let g:typed2 = inputsecret('password: ')
1307+
endfunc
1308+
call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1309+
call assert_equal("something", g:typed1)
1310+
call assert_equal("else", g:typed2)
1311+
delfunc TestFunc
1312+
unmap W
1313+
unlet g:typed1
1314+
unlet g:typed2
1315+
endfunc
1316+
12981317
func Test_libcall_libcallnr()
12991318
if !has('libcall')
13001319
return

src/testdir/test_getcwd.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func GetCwdInfo(win, tab)
1717
let lflag = haslocaldir(a:win)
1818
else
1919
let dirname = fnamemodify(getcwd(a:win, a:tab), mod)
20-
let lflag = haslocaldir(a:win, a:tab)
20+
let lflag = a:win->haslocaldir(a:tab)
2121
endif
2222
return bufname . ' ' . dirname . ' ' . lflag
2323
endfunc

src/testdir/test_history.vim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function History_Tests(hist)
1212
call assert_equal(-1, histnr(a:hist))
1313
call assert_equal('', histget(a:hist))
1414

15-
call assert_true(histadd(a:hist, 'ls'))
15+
call assert_true('ls'->histadd(a:hist))
1616
call assert_true(histadd(a:hist, 'buffers'))
1717
call assert_equal('buffers', histget(a:hist))
1818
call assert_equal('ls', histget(a:hist, -2))
@@ -21,14 +21,14 @@ function History_Tests(hist)
2121
call assert_equal('', histget(a:hist, -5))
2222
call assert_equal(2, histnr(a:hist))
2323
call assert_true(histdel(a:hist, 2))
24-
call assert_false(histdel(a:hist, 7))
24+
call assert_false(a:hist->histdel(7))
2525
call assert_equal(1, histnr(a:hist))
2626
call assert_equal('ls', histget(a:hist, -1))
2727

2828
call assert_true(histadd(a:hist, 'buffers'))
2929
call assert_true(histadd(a:hist, 'ls'))
30-
call assert_equal('ls', histget(a:hist, -1))
31-
call assert_equal(4, histnr(a:hist))
30+
call assert_equal('ls', a:hist->histget(-1))
31+
call assert_equal(4, a:hist->histnr())
3232

3333
let a=execute('history ' . a:hist)
3434
call assert_match("^\n # \\S* history\n 3 buffers\n> 4 ls$", a)

src/testdir/test_listdict.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func Test_lockvar_script_autoload()
565565
set rtp+=./sautest
566566
lockvar g:footest#x
567567
unlockvar g:footest#x
568-
call assert_equal(-1, islocked('g:footest#x'))
568+
call assert_equal(-1, 'g:footest#x'->islocked())
569569
call assert_equal(0, exists('g:footest#x'))
570570
call assert_equal(1, g:footest#x)
571571
let &rtp = old_rtp

src/testdir/test_syn_attr.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
func Test_missing_attr()
44
hi Mine term=bold cterm=italic
55
call assert_equal('Mine', synIDattr(hlID("Mine"), "name"))
6-
call assert_equal('', synIDattr(hlID("Mine"), "bg", 'term'))
6+
call assert_equal('', synIDattr("Mine"->hlID(), "bg", 'term'))
77
call assert_equal('1', synIDattr(hlID("Mine"), "bold", 'term'))
88
call assert_equal('1', synIDattr(hlID("Mine"), "italic", 'cterm'))
99
hi Mine term=reverse cterm=inverse
@@ -12,7 +12,7 @@ func Test_missing_attr()
1212
hi Mine term=underline cterm=standout gui=undercurl
1313
call assert_equal('1', synIDattr(hlID("Mine"), "underline", 'term'))
1414
call assert_equal('1', synIDattr(hlID("Mine"), "standout", 'cterm'))
15-
call assert_equal('1', synIDattr(hlID("Mine"), "undercurl", 'gui'))
15+
call assert_equal('1', synIDattr("Mine"->hlID(), "undercurl", 'gui'))
1616
hi Mine term=NONE cterm=NONE gui=NONE
1717
call assert_equal('', synIDattr(hlID("Mine"), "bold", 'term'))
1818
call assert_equal('', synIDattr(hlID("Mine"), "italic", 'cterm'))

0 commit comments

Comments
 (0)