0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 8.1.1984: 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 20:05:59 +02:00
parent ea781459b9
commit 3f4f3d8e7e
9 changed files with 73 additions and 34 deletions

View File

@@ -7120,6 +7120,9 @@ nextnonblank({lnum}) *nextnonblank()*
below it, zero is returned. below it, zero is returned.
See also |prevnonblank()|. See also |prevnonblank()|.
Can also be used as a |method|: >
GetLnum()->nextnonblank()
nr2char({expr} [, {utf8}]) *nr2char()* nr2char({expr} [, {utf8}]) *nr2char()*
Return a string with a single character, which has the number Return a string with a single character, which has the number
value {expr}. Examples: > value {expr}. Examples: >
@@ -7138,6 +7141,8 @@ nr2char({expr} [, {utf8}]) *nr2char()*
let str = join(map(list, {_, val -> nr2char(val)}), '') let str = join(map(list, {_, val -> nr2char(val)}), '')
< Result: "ABC" < Result: "ABC"
Can also be used as a |method|: >
GetNumber()->nr2char()
or({expr}, {expr}) *or()* or({expr}, {expr}) *or()*
Bitwise OR on the two arguments. The arguments are converted Bitwise OR on the two arguments. The arguments are converted
@@ -7157,6 +7162,9 @@ pathshorten({expr}) *pathshorten()*
< ~/.v/a/myfile.vim ~ < ~/.v/a/myfile.vim ~
It doesn't matter if the path exists or not. It doesn't matter if the path exists or not.
Can also be used as a |method|: >
GetDirectories()->pathshorten()
perleval({expr}) *perleval()* perleval({expr}) *perleval()*
Evaluate Perl expression {expr} in scalar context and return Evaluate Perl expression {expr} in scalar context and return
its result converted to Vim data structures. If value can't be its result converted to Vim data structures. If value can't be
@@ -7166,7 +7174,11 @@ perleval({expr}) *perleval()*
Example: > Example: >
:echo perleval('[1 .. 4]') :echo perleval('[1 .. 4]')
< [1, 2, 3, 4] < [1, 2, 3, 4]
{only available when compiled with the |+perl| feature}
Can also be used as a |method|: >
GetExpr()->perleval()
< {only available when compiled with the |+perl| feature}
popup_ functions are documented here: |popup-functions|. popup_ functions are documented here: |popup-functions|.
@@ -7196,6 +7208,8 @@ prevnonblank({lnum}) *prevnonblank()*
above it, zero is returned. above it, zero is returned.
Also see |nextnonblank()|. Also see |nextnonblank()|.
Can also be used as a |method|: >
GetLnum()->prevnonblank()
printf({fmt}, {expr1} ...) *printf()* printf({fmt}, {expr1} ...) *printf()*
Return a String with {fmt}, where "%" items are replaced by Return a String with {fmt}, where "%" items are replaced by
@@ -7424,6 +7438,10 @@ prompt_setcallback({buf}, {expr}) *prompt_setcallback()*
endif endif
endfunc endfunc
< Can also be used as a |method|: >
GetBuffer()->prompt_setcallback(callback)
prompt_setinterrupt({buf}, {expr}) *prompt_setinterrupt()* prompt_setinterrupt({buf}, {expr}) *prompt_setinterrupt()*
Set a callback for buffer {buf} to {expr}. When {expr} is an Set a callback for buffer {buf} to {expr}. When {expr} is an
empty string the callback is removed. This has only effect if empty string the callback is removed. This has only effect if
@@ -7433,6 +7451,9 @@ prompt_setinterrupt({buf}, {expr}) *prompt_setinterrupt()*
mode. Without setting a callback Vim will exit Insert mode, mode. Without setting a callback Vim will exit Insert mode,
as in any buffer. as in any buffer.
Can also be used as a |method|: >
GetBuffer()->prompt_setinterrupt(callback)
prompt_setprompt({buf}, {text}) *prompt_setprompt()* prompt_setprompt({buf}, {text}) *prompt_setprompt()*
Set prompt for buffer {buf} to {text}. You most likely want Set prompt for buffer {buf} to {text}. You most likely want
{text} to end in a space. {text} to end in a space.
@@ -7440,6 +7461,9 @@ prompt_setprompt({buf}, {text}) *prompt_setprompt()*
"prompt". Example: > "prompt". Example: >
call prompt_setprompt(bufnr(), 'command: ') call prompt_setprompt(bufnr(), 'command: ')
< <
Can also be used as a |method|: >
GetBuffer()->prompt_setprompt('command: ')
prop_ functions are documented here: |text-prop-functions|. prop_ functions are documented here: |text-prop-functions|.
pum_getpos() *pum_getpos()* pum_getpos() *pum_getpos()*
@@ -7471,7 +7495,11 @@ py3eval({expr}) *py3eval()*
Lists are represented as Vim |List| type. Lists are represented as Vim |List| type.
Dictionaries are represented as Vim |Dictionary| type with Dictionaries are represented as Vim |Dictionary| type with
keys converted to strings. keys converted to strings.
{only available when compiled with the |+python3| feature}
Can also be used as a |method|: >
GetExpr()->py3eval()
< {only available when compiled with the |+python3| feature}
*E858* *E859* *E858* *E859*
pyeval({expr}) *pyeval()* pyeval({expr}) *pyeval()*
@@ -7482,14 +7510,22 @@ pyeval({expr}) *pyeval()*
Lists are represented as Vim |List| type. Lists are represented as Vim |List| type.
Dictionaries are represented as Vim |Dictionary| type, Dictionaries are represented as Vim |Dictionary| type,
non-string keys result in error. non-string keys result in error.
{only available when compiled with the |+python| feature}
Can also be used as a |method|: >
GetExpr()->pyeval()
< {only available when compiled with the |+python| feature}
pyxeval({expr}) *pyxeval()* pyxeval({expr}) *pyxeval()*
Evaluate Python expression {expr} and return its result Evaluate Python expression {expr} and return its result
converted to Vim data structures. converted to Vim data structures.
Uses Python 2 or 3, see |python_x| and 'pyxversion'. Uses Python 2 or 3, see |python_x| and 'pyxversion'.
See also: |pyeval()|, |py3eval()| See also: |pyeval()|, |py3eval()|
{only available when compiled with the |+python| or the
Can also be used as a |method|: >
GetExpr()->pyxeval()
< {only available when compiled with the |+python| or the
|+python3| feature} |+python3| feature}
*E726* *E727* *E726* *E727*

View File

@@ -625,12 +625,12 @@ static funcentry_T global_functions[] =
#ifdef FEAT_MZSCHEME #ifdef FEAT_MZSCHEME
{"mzeval", 1, 1, FEARG_1, f_mzeval}, {"mzeval", 1, 1, FEARG_1, f_mzeval},
#endif #endif
{"nextnonblank", 1, 1, 0, f_nextnonblank}, {"nextnonblank", 1, 1, FEARG_1, f_nextnonblank},
{"nr2char", 1, 2, 0, f_nr2char}, {"nr2char", 1, 2, FEARG_1, f_nr2char},
{"or", 2, 2, FEARG_1, f_or}, {"or", 2, 2, FEARG_1, f_or},
{"pathshorten", 1, 1, 0, f_pathshorten}, {"pathshorten", 1, 1, FEARG_1, f_pathshorten},
#ifdef FEAT_PERL #ifdef FEAT_PERL
{"perleval", 1, 1, 0, f_perleval}, {"perleval", 1, 1, FEARG_1, f_perleval},
#endif #endif
#ifdef FEAT_TEXT_PROP #ifdef FEAT_TEXT_PROP
{"popup_atcursor", 2, 2, FEARG_1, f_popup_atcursor}, {"popup_atcursor", 2, 2, FEARG_1, f_popup_atcursor},
@@ -657,12 +657,12 @@ static funcentry_T global_functions[] =
#ifdef FEAT_FLOAT #ifdef FEAT_FLOAT
{"pow", 2, 2, FEARG_1, f_pow}, {"pow", 2, 2, FEARG_1, f_pow},
#endif #endif
{"prevnonblank", 1, 1, 0, f_prevnonblank}, {"prevnonblank", 1, 1, FEARG_1, f_prevnonblank},
{"printf", 1, 19, FEARG_2, f_printf}, {"printf", 1, 19, FEARG_2, f_printf},
#ifdef FEAT_JOB_CHANNEL #ifdef FEAT_JOB_CHANNEL
{"prompt_setcallback", 2, 2, 0, f_prompt_setcallback}, {"prompt_setcallback", 2, 2, FEARG_1, f_prompt_setcallback},
{"prompt_setinterrupt", 2, 2, 0, f_prompt_setinterrupt}, {"prompt_setinterrupt", 2, 2, FEARG_1, f_prompt_setinterrupt},
{"prompt_setprompt", 2, 2, 0, f_prompt_setprompt}, {"prompt_setprompt", 2, 2, FEARG_1, f_prompt_setprompt},
#endif #endif
#ifdef FEAT_TEXT_PROP #ifdef FEAT_TEXT_PROP
{"prop_add", 3, 3, 0, f_prop_add}, {"prop_add", 3, 3, 0, f_prop_add},
@@ -678,13 +678,13 @@ static funcentry_T global_functions[] =
{"pum_getpos", 0, 0, 0, f_pum_getpos}, {"pum_getpos", 0, 0, 0, f_pum_getpos},
{"pumvisible", 0, 0, 0, f_pumvisible}, {"pumvisible", 0, 0, 0, f_pumvisible},
#ifdef FEAT_PYTHON3 #ifdef FEAT_PYTHON3
{"py3eval", 1, 1, 0, f_py3eval}, {"py3eval", 1, 1, FEARG_1, f_py3eval},
#endif #endif
#ifdef FEAT_PYTHON #ifdef FEAT_PYTHON
{"pyeval", 1, 1, 0, f_pyeval}, {"pyeval", 1, 1, FEARG_1, f_pyeval},
#endif #endif
#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
{"pyxeval", 1, 1, 0, f_pyxeval}, {"pyxeval", 1, 1, FEARG_1, f_pyxeval},
#endif #endif
{"range", 1, 3, 0, f_range}, {"range", 1, 3, 0, f_range},
{"readdir", 1, 2, 0, f_readdir}, {"readdir", 1, 2, 0, f_readdir},

View File

@@ -400,10 +400,10 @@ endfunc
func Test_pathshorten() func Test_pathshorten()
call assert_equal('', pathshorten('')) call assert_equal('', pathshorten(''))
call assert_equal('foo', pathshorten('foo')) call assert_equal('foo', pathshorten('foo'))
call assert_equal('/foo', pathshorten('/foo')) call assert_equal('/foo', '/foo'->pathshorten())
call assert_equal('f/', pathshorten('foo/')) call assert_equal('f/', pathshorten('foo/'))
call assert_equal('f/bar', pathshorten('foo/bar')) call assert_equal('f/bar', pathshorten('foo/bar'))
call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar')) call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar')) call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
call assert_equal('.f/bar', pathshorten('.foo/bar')) call assert_equal('.f/bar', pathshorten('.foo/bar'))
call assert_equal('~f/bar', pathshorten('~foo/bar')) call assert_equal('~f/bar', pathshorten('~foo/bar'))
@@ -847,21 +847,21 @@ Test
call assert_equal(0, nextnonblank(-1)) call assert_equal(0, nextnonblank(-1))
call assert_equal(0, nextnonblank(0)) call assert_equal(0, nextnonblank(0))
call assert_equal(1, nextnonblank(1)) call assert_equal(1, nextnonblank(1))
call assert_equal(4, nextnonblank(2)) call assert_equal(4, 2->nextnonblank())
call assert_equal(4, nextnonblank(3)) call assert_equal(4, nextnonblank(3))
call assert_equal(4, nextnonblank(4)) call assert_equal(4, nextnonblank(4))
call assert_equal(6, nextnonblank(5)) call assert_equal(6, nextnonblank(5))
call assert_equal(6, nextnonblank(6)) call assert_equal(6, nextnonblank(6))
call assert_equal(7, nextnonblank(7)) call assert_equal(7, nextnonblank(7))
call assert_equal(0, nextnonblank(8)) call assert_equal(0, 8->nextnonblank())
call assert_equal(0, prevnonblank(-1)) call assert_equal(0, prevnonblank(-1))
call assert_equal(0, prevnonblank(0)) call assert_equal(0, prevnonblank(0))
call assert_equal(1, prevnonblank(1)) call assert_equal(1, 1->prevnonblank())
call assert_equal(1, prevnonblank(2)) call assert_equal(1, prevnonblank(2))
call assert_equal(1, prevnonblank(3)) call assert_equal(1, prevnonblank(3))
call assert_equal(4, prevnonblank(4)) call assert_equal(4, prevnonblank(4))
call assert_equal(4, prevnonblank(5)) call assert_equal(4, 5->prevnonblank())
call assert_equal(6, prevnonblank(6)) call assert_equal(6, prevnonblank(6))
call assert_equal(7, prevnonblank(7)) call assert_equal(7, prevnonblank(7))
call assert_equal(0, prevnonblank(8)) call assert_equal(0, prevnonblank(8))
@@ -1220,7 +1220,7 @@ func Test_trim()
call assert_equal("a", trim("a", "")) call assert_equal("a", trim("a", ""))
call assert_equal("", trim("", "a")) call assert_equal("", trim("", "a"))
let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '') let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
call assert_equal("x", trim(chars . "x" . chars)) call assert_equal("x", trim(chars . "x" . chars))
endfunc endfunc

View File

@@ -30,7 +30,7 @@ endfunc
funct Test_VIM_Blob() funct Test_VIM_Blob()
call assert_equal('0z', perleval('VIM::Blob("")')) call assert_equal('0z', perleval('VIM::Blob("")'))
call assert_equal('0z31326162', perleval('VIM::Blob("12ab")')) call assert_equal('0z31326162', 'VIM::Blob("12ab")'->perleval())
call assert_equal('0z00010203', perleval('VIM::Blob("\x00\x01\x02\x03")')) call assert_equal('0z00010203', perleval('VIM::Blob("\x00\x01\x02\x03")'))
call assert_equal('0z8081FEFF', perleval('VIM::Blob("\x80\x81\xfe\xff")')) call assert_equal('0z8081FEFF', perleval('VIM::Blob("\x80\x81\xfe\xff")'))
endfunc endfunc

View File

@@ -48,6 +48,7 @@ func WriteScript(name)
\ 'new', \ 'new',
\ 'set buftype=prompt', \ 'set buftype=prompt',
\ 'call prompt_setcallback(bufnr(""), function("TextEntered"))', \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))',
\ 'eval bufnr("")->prompt_setprompt("cmd: ")',
\ 'startinsert', \ 'startinsert',
\ ], a:name) \ ], a:name)
endfunc endfunc
@@ -60,10 +61,10 @@ func Test_prompt_basic()
call WriteScript(scriptName) call WriteScript(scriptName)
let buf = RunVimInTerminal('-S ' . scriptName, {}) let buf = RunVimInTerminal('-S ' . scriptName, {})
call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
call term_sendkeys(buf, "hello\<CR>") call term_sendkeys(buf, "hello\<CR>")
call WaitForAssert({-> assert_equal('% hello', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))})
call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))}) call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))})
call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))}) call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))})
@@ -82,19 +83,19 @@ func Test_prompt_editing()
call WriteScript(scriptName) call WriteScript(scriptName)
let buf = RunVimInTerminal('-S ' . scriptName, {}) let buf = RunVimInTerminal('-S ' . scriptName, {})
call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))})
let bs = "\<BS>" let bs = "\<BS>"
call term_sendkeys(buf, "hello" . bs . bs) call term_sendkeys(buf, "hello" . bs . bs)
call WaitForAssert({-> assert_equal('% hel', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))})
let left = "\<Left>" let left = "\<Left>"
call term_sendkeys(buf, left . left . left . bs . '-') call term_sendkeys(buf, left . left . left . bs . '-')
call WaitForAssert({-> assert_equal('% -hel', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))})
let end = "\<End>" let end = "\<End>"
call term_sendkeys(buf, end . "x") call term_sendkeys(buf, end . "x")
call WaitForAssert({-> assert_equal('% -helx', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('cmd: -helx', term_getline(buf, 1))})
call term_sendkeys(buf, "\<C-U>exit\<CR>") call term_sendkeys(buf, "\<C-U>exit\<CR>")
call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))})
@@ -113,8 +114,8 @@ func Test_prompt_garbage_collect()
new new
set buftype=prompt set buftype=prompt
call prompt_setcallback(bufnr(''), function('MyPromptCallback', [{}])) eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}]))
call prompt_setinterrupt(bufnr(''), function('MyPromptInterrupt', [{}])) eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}]))
call test_garbagecollect_now() call test_garbagecollect_now()
" Must not crash " Must not crash
call feedkeys("\<CR>\<C-C>", 'xt') call feedkeys("\<CR>\<C-C>", 'xt')

View File

@@ -54,7 +54,7 @@ func Test_vim_function()
try try
py f = vim.Function('\x80\xfdR' + vim.eval('s:foo()')) py f = vim.Function('\x80\xfdR' + vim.eval('s:foo()'))
call assert_equal(name, pyeval('f.name')) call assert_equal(name, 'f.name'->pyeval())
catch catch
call assert_false(v:exception) call assert_false(v:exception)
endtry endtry

View File

@@ -54,7 +54,7 @@ func Test_vim_function()
try try
py3 f = vim.Function(b'\x80\xfdR' + vim.eval('s:foo()').encode()) py3 f = vim.Function(b'\x80\xfdR' + vim.eval('s:foo()').encode())
call assert_equal(name, py3eval('f.name')) call assert_equal(name, 'f.name'->py3eval())
catch catch
call assert_false(v:exception) call assert_false(v:exception)
endtry endtry

View File

@@ -34,7 +34,7 @@ endfunc
func Test_pyxeval() func Test_pyxeval()
pyx import sys pyx import sys
call assert_match(s:py2pattern, split(pyxeval('sys.version'))[0]) call assert_match(s:py2pattern, split('sys.version'->pyxeval())[0])
endfunc endfunc

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 */
/**/
1984,
/**/ /**/
1983, 1983,
/**/ /**/