To: vim_dev@googlegroups.com Subject: Patch 8.1.1984 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.1.1984 Problem: More functions can be used as methods. Solution: Make various functions usable as a method. Files: runtime/doc/eval.txt, src/evalfunc.c, src/testdir/test_functions.vim, src/testdir/test_perl.vim, src/testdir/test_prompt_buffer.vim, src/testdir/test_python2.vim, src/testdir/test_python3.vim, src/testdir/test_pyx2.vim *** ../vim-8.1.1983/runtime/doc/eval.txt 2019-09-01 20:16:48.003438501 +0200 --- runtime/doc/eval.txt 2019-09-04 20:04:33.130867681 +0200 *************** *** 7112,7117 **** --- 7126,7134 ---- below it, zero is returned. See also |prevnonblank()|. + Can also be used as a |method|: > + GetLnum()->nextnonblank() + nr2char({expr} [, {utf8}]) *nr2char()* Return a string with a single character, which has the number value {expr}. Examples: > *************** *** 7130,7135 **** --- 7147,7154 ---- let str = join(map(list, {_, val -> nr2char(val)}), '') < Result: "ABC" + Can also be used as a |method|: > + GetNumber()->nr2char() or({expr}, {expr}) *or()* Bitwise OR on the two arguments. The arguments are converted *************** *** 7149,7154 **** --- 7168,7176 ---- < ~/.v/a/myfile.vim ~ It doesn't matter if the path exists or not. + Can also be used as a |method|: > + GetDirectories()->pathshorten() + perleval({expr}) *perleval()* Evaluate Perl expression {expr} in scalar context and return its result converted to Vim data structures. If value can't be *************** *** 7158,7164 **** Example: > :echo perleval('[1 .. 4]') < [1, 2, 3, 4] ! {only available when compiled with the |+perl| feature} popup_ functions are documented here: |popup-functions|. --- 7180,7190 ---- Example: > :echo perleval('[1 .. 4]') < [1, 2, 3, 4] ! ! Can also be used as a |method|: > ! GetExpr()->perleval() ! ! < {only available when compiled with the |+perl| feature} popup_ functions are documented here: |popup-functions|. *************** *** 7188,7193 **** --- 7214,7221 ---- above it, zero is returned. Also see |nextnonblank()|. + Can also be used as a |method|: > + GetLnum()->prevnonblank() printf({fmt}, {expr1} ...) *printf()* Return a String with {fmt}, where "%" items are replaced by *************** *** 7416,7421 **** --- 7444,7453 ---- endif endfunc + < Can also be used as a |method|: > + GetBuffer()->prompt_setcallback(callback) + + prompt_setinterrupt({buf}, {expr}) *prompt_setinterrupt()* Set a callback for buffer {buf} to {expr}. When {expr} is an empty string the callback is removed. This has only effect if *************** *** 7425,7430 **** --- 7457,7465 ---- mode. Without setting a callback Vim will exit Insert mode, as in any buffer. + Can also be used as a |method|: > + GetBuffer()->prompt_setinterrupt(callback) + prompt_setprompt({buf}, {text}) *prompt_setprompt()* Set prompt for buffer {buf} to {text}. You most likely want {text} to end in a space. *************** *** 7432,7437 **** --- 7467,7475 ---- "prompt". Example: > call prompt_setprompt(bufnr(), 'command: ') < + Can also be used as a |method|: > + GetBuffer()->prompt_setprompt('command: ') + prop_ functions are documented here: |text-prop-functions|. pum_getpos() *pum_getpos()* *************** *** 7463,7469 **** Lists are represented as Vim |List| type. Dictionaries are represented as Vim |Dictionary| type with keys converted to strings. ! {only available when compiled with the |+python3| feature} *E858* *E859* pyeval({expr}) *pyeval()* --- 7501,7511 ---- Lists are represented as Vim |List| type. Dictionaries are represented as Vim |Dictionary| type with keys converted to strings. ! ! Can also be used as a |method|: > ! GetExpr()->py3eval() ! ! < {only available when compiled with the |+python3| feature} *E858* *E859* pyeval({expr}) *pyeval()* *************** *** 7474,7487 **** Lists are represented as Vim |List| type. Dictionaries are represented as Vim |Dictionary| type, non-string keys result in error. ! {only available when compiled with the |+python| feature} pyxeval({expr}) *pyxeval()* Evaluate Python expression {expr} and return its result converted to Vim data structures. Uses Python 2 or 3, see |python_x| and 'pyxversion'. See also: |pyeval()|, |py3eval()| ! {only available when compiled with the |+python| or the |+python3| feature} *E726* *E727* --- 7516,7537 ---- Lists are represented as Vim |List| type. Dictionaries are represented as Vim |Dictionary| type, non-string keys result in error. ! ! Can also be used as a |method|: > ! GetExpr()->pyeval() ! ! < {only available when compiled with the |+python| feature} pyxeval({expr}) *pyxeval()* Evaluate Python expression {expr} and return its result converted to Vim data structures. Uses Python 2 or 3, see |python_x| and 'pyxversion'. See also: |pyeval()|, |py3eval()| ! ! Can also be used as a |method|: > ! GetExpr()->pyxeval() ! ! < {only available when compiled with the |+python| or the |+python3| feature} *E726* *E727* *** ../vim-8.1.1983/src/evalfunc.c 2019-09-04 18:15:16.178573707 +0200 --- src/evalfunc.c 2019-09-04 20:01:12.483714617 +0200 *************** *** 625,636 **** #ifdef FEAT_MZSCHEME {"mzeval", 1, 1, FEARG_1, f_mzeval}, #endif ! {"nextnonblank", 1, 1, 0, f_nextnonblank}, ! {"nr2char", 1, 2, 0, f_nr2char}, {"or", 2, 2, FEARG_1, f_or}, ! {"pathshorten", 1, 1, 0, f_pathshorten}, #ifdef FEAT_PERL ! {"perleval", 1, 1, 0, f_perleval}, #endif #ifdef FEAT_TEXT_PROP {"popup_atcursor", 2, 2, FEARG_1, f_popup_atcursor}, --- 625,636 ---- #ifdef FEAT_MZSCHEME {"mzeval", 1, 1, FEARG_1, f_mzeval}, #endif ! {"nextnonblank", 1, 1, FEARG_1, f_nextnonblank}, ! {"nr2char", 1, 2, FEARG_1, f_nr2char}, {"or", 2, 2, FEARG_1, f_or}, ! {"pathshorten", 1, 1, FEARG_1, f_pathshorten}, #ifdef FEAT_PERL ! {"perleval", 1, 1, FEARG_1, f_perleval}, #endif #ifdef FEAT_TEXT_PROP {"popup_atcursor", 2, 2, FEARG_1, f_popup_atcursor}, *************** *** 657,668 **** #ifdef FEAT_FLOAT {"pow", 2, 2, FEARG_1, f_pow}, #endif ! {"prevnonblank", 1, 1, 0, f_prevnonblank}, {"printf", 1, 19, FEARG_2, f_printf}, #ifdef FEAT_JOB_CHANNEL ! {"prompt_setcallback", 2, 2, 0, f_prompt_setcallback}, ! {"prompt_setinterrupt", 2, 2, 0, f_prompt_setinterrupt}, ! {"prompt_setprompt", 2, 2, 0, f_prompt_setprompt}, #endif #ifdef FEAT_TEXT_PROP {"prop_add", 3, 3, 0, f_prop_add}, --- 657,668 ---- #ifdef FEAT_FLOAT {"pow", 2, 2, FEARG_1, f_pow}, #endif ! {"prevnonblank", 1, 1, FEARG_1, f_prevnonblank}, {"printf", 1, 19, FEARG_2, f_printf}, #ifdef FEAT_JOB_CHANNEL ! {"prompt_setcallback", 2, 2, FEARG_1, f_prompt_setcallback}, ! {"prompt_setinterrupt", 2, 2, FEARG_1, f_prompt_setinterrupt}, ! {"prompt_setprompt", 2, 2, FEARG_1, f_prompt_setprompt}, #endif #ifdef FEAT_TEXT_PROP {"prop_add", 3, 3, 0, f_prop_add}, *************** *** 678,690 **** {"pum_getpos", 0, 0, 0, f_pum_getpos}, {"pumvisible", 0, 0, 0, f_pumvisible}, #ifdef FEAT_PYTHON3 ! {"py3eval", 1, 1, 0, f_py3eval}, #endif #ifdef FEAT_PYTHON ! {"pyeval", 1, 1, 0, f_pyeval}, #endif #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) ! {"pyxeval", 1, 1, 0, f_pyxeval}, #endif {"range", 1, 3, 0, f_range}, {"readdir", 1, 2, 0, f_readdir}, --- 678,690 ---- {"pum_getpos", 0, 0, 0, f_pum_getpos}, {"pumvisible", 0, 0, 0, f_pumvisible}, #ifdef FEAT_PYTHON3 ! {"py3eval", 1, 1, FEARG_1, f_py3eval}, #endif #ifdef FEAT_PYTHON ! {"pyeval", 1, 1, FEARG_1, f_pyeval}, #endif #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}, *** ../vim-8.1.1983/src/testdir/test_functions.vim 2019-09-03 23:36:57.677559719 +0200 --- src/testdir/test_functions.vim 2019-09-04 19:47:44.879427462 +0200 *************** *** 400,409 **** func Test_pathshorten() call assert_equal('', pathshorten('')) call assert_equal('foo', pathshorten('foo')) ! call assert_equal('/foo', pathshorten('/foo')) call assert_equal('f/', pathshorten('foo/')) call assert_equal('f/bar', pathshorten('foo/bar')) ! 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')) --- 400,409 ---- func Test_pathshorten() call assert_equal('', pathshorten('')) call assert_equal('foo', pathshorten('foo')) ! call assert_equal('/foo', '/foo'->pathshorten()) call assert_equal('f/', pathshorten('foo/')) call assert_equal('f/bar', pathshorten('foo/bar')) ! call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten()) 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')) *************** *** 847,867 **** call assert_equal(0, nextnonblank(-1)) call assert_equal(0, nextnonblank(0)) call assert_equal(1, nextnonblank(1)) ! call assert_equal(4, nextnonblank(2)) call assert_equal(4, nextnonblank(3)) call assert_equal(4, nextnonblank(4)) call assert_equal(6, nextnonblank(5)) call assert_equal(6, nextnonblank(6)) call assert_equal(7, nextnonblank(7)) ! call assert_equal(0, nextnonblank(8)) call assert_equal(0, prevnonblank(-1)) call assert_equal(0, prevnonblank(0)) ! call assert_equal(1, prevnonblank(1)) call assert_equal(1, prevnonblank(2)) call assert_equal(1, prevnonblank(3)) call assert_equal(4, prevnonblank(4)) ! call assert_equal(4, prevnonblank(5)) call assert_equal(6, prevnonblank(6)) call assert_equal(7, prevnonblank(7)) call assert_equal(0, prevnonblank(8)) --- 847,867 ---- call assert_equal(0, nextnonblank(-1)) call assert_equal(0, nextnonblank(0)) call assert_equal(1, nextnonblank(1)) ! call assert_equal(4, 2->nextnonblank()) call assert_equal(4, nextnonblank(3)) call assert_equal(4, nextnonblank(4)) call assert_equal(6, nextnonblank(5)) call assert_equal(6, nextnonblank(6)) call assert_equal(7, nextnonblank(7)) ! call assert_equal(0, 8->nextnonblank()) call assert_equal(0, prevnonblank(-1)) call assert_equal(0, prevnonblank(0)) ! call assert_equal(1, 1->prevnonblank()) call assert_equal(1, prevnonblank(2)) call assert_equal(1, prevnonblank(3)) call assert_equal(4, prevnonblank(4)) ! call assert_equal(4, 5->prevnonblank()) call assert_equal(6, prevnonblank(6)) call assert_equal(7, prevnonblank(7)) call assert_equal(0, prevnonblank(8)) *************** *** 1220,1226 **** call assert_equal("a", trim("a", "")) call assert_equal("", trim("", "a")) ! let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '') call assert_equal("x", trim(chars . "x" . chars)) endfunc --- 1220,1226 ---- call assert_equal("a", trim("a", "")) call assert_equal("", trim("", "a")) ! let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '') call assert_equal("x", trim(chars . "x" . chars)) endfunc *** ../vim-8.1.1983/src/testdir/test_perl.vim 2019-06-15 17:57:43.972724036 +0200 --- src/testdir/test_perl.vim 2019-09-04 19:46:20.711703021 +0200 *************** *** 30,36 **** funct Test_VIM_Blob() call assert_equal('0z', perleval('VIM::Blob("")')) ! call assert_equal('0z31326162', perleval('VIM::Blob("12ab")')) call assert_equal('0z00010203', perleval('VIM::Blob("\x00\x01\x02\x03")')) call assert_equal('0z8081FEFF', perleval('VIM::Blob("\x80\x81\xfe\xff")')) endfunc --- 30,36 ---- funct Test_VIM_Blob() call assert_equal('0z', perleval('VIM::Blob("")')) ! call assert_equal('0z31326162', 'VIM::Blob("12ab")'->perleval()) call assert_equal('0z00010203', perleval('VIM::Blob("\x00\x01\x02\x03")')) call assert_equal('0z8081FEFF', perleval('VIM::Blob("\x80\x81\xfe\xff")')) endfunc *** ../vim-8.1.1983/src/testdir/test_prompt_buffer.vim 2019-06-20 03:45:31.175536929 +0200 --- src/testdir/test_prompt_buffer.vim 2019-09-04 20:00:18.167950718 +0200 *************** *** 48,53 **** --- 48,54 ---- \ 'new', \ 'set buftype=prompt', \ 'call prompt_setcallback(bufnr(""), function("TextEntered"))', + \ 'eval bufnr("")->prompt_setprompt("cmd: ")', \ 'startinsert', \ ], a:name) endfunc *************** *** 60,69 **** call WriteScript(scriptName) let buf = RunVimInTerminal('-S ' . scriptName, {}) ! call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))}) call term_sendkeys(buf, "hello\") ! call WaitForAssert({-> assert_equal('% hello', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))}) call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))}) --- 61,70 ---- call WriteScript(scriptName) let buf = RunVimInTerminal('-S ' . scriptName, {}) ! call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))}) call term_sendkeys(buf, "hello\") ! call WaitForAssert({-> assert_equal('cmd: hello', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('Command: "hello"', term_getline(buf, 2))}) call WaitForAssert({-> assert_equal('Result: "hello"', term_getline(buf, 3))}) *************** *** 82,100 **** call WriteScript(scriptName) let buf = RunVimInTerminal('-S ' . scriptName, {}) ! call WaitForAssert({-> assert_equal('%', term_getline(buf, 1))}) let bs = "\" call term_sendkeys(buf, "hello" . bs . bs) ! call WaitForAssert({-> assert_equal('% hel', term_getline(buf, 1))}) let left = "\" call term_sendkeys(buf, left . left . left . bs . '-') ! call WaitForAssert({-> assert_equal('% -hel', term_getline(buf, 1))}) let end = "\" call term_sendkeys(buf, end . "x") ! call WaitForAssert({-> assert_equal('% -helx', term_getline(buf, 1))}) call term_sendkeys(buf, "\exit\") call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))}) --- 83,101 ---- call WriteScript(scriptName) let buf = RunVimInTerminal('-S ' . scriptName, {}) ! call WaitForAssert({-> assert_equal('cmd:', term_getline(buf, 1))}) let bs = "\" call term_sendkeys(buf, "hello" . bs . bs) ! call WaitForAssert({-> assert_equal('cmd: hel', term_getline(buf, 1))}) let left = "\" call term_sendkeys(buf, left . left . left . bs . '-') ! call WaitForAssert({-> assert_equal('cmd: -hel', term_getline(buf, 1))}) let end = "\" call term_sendkeys(buf, end . "x") ! call WaitForAssert({-> assert_equal('cmd: -helx', term_getline(buf, 1))}) call term_sendkeys(buf, "\exit\") call WaitForAssert({-> assert_equal('other buffer', term_getline(buf, 1))}) *************** *** 113,120 **** new set buftype=prompt ! call prompt_setcallback(bufnr(''), function('MyPromptCallback', [{}])) ! call prompt_setinterrupt(bufnr(''), function('MyPromptInterrupt', [{}])) call test_garbagecollect_now() " Must not crash call feedkeys("\\", 'xt') --- 114,121 ---- new set buftype=prompt ! eval bufnr('')->prompt_setcallback(function('MyPromptCallback', [{}])) ! eval bufnr('')->prompt_setinterrupt(function('MyPromptInterrupt', [{}])) call test_garbagecollect_now() " Must not crash call feedkeys("\\", 'xt') *** ../vim-8.1.1983/src/testdir/test_python2.vim 2019-06-15 17:57:43.972724036 +0200 --- src/testdir/test_python2.vim 2019-09-04 20:02:06.183484325 +0200 *************** *** 54,60 **** try py f = vim.Function('\x80\xfdR' + vim.eval('s:foo()')) ! call assert_equal(name, pyeval('f.name')) catch call assert_false(v:exception) endtry --- 54,60 ---- try py f = vim.Function('\x80\xfdR' + vim.eval('s:foo()')) ! call assert_equal(name, 'f.name'->pyeval()) catch call assert_false(v:exception) endtry *** ../vim-8.1.1983/src/testdir/test_python3.vim 2019-06-15 17:57:43.972724036 +0200 --- src/testdir/test_python3.vim 2019-09-04 20:01:01.159763565 +0200 *************** *** 54,60 **** try py3 f = vim.Function(b'\x80\xfdR' + vim.eval('s:foo()').encode()) ! call assert_equal(name, py3eval('f.name')) catch call assert_false(v:exception) endtry --- 54,60 ---- try py3 f = vim.Function(b'\x80\xfdR' + vim.eval('s:foo()').encode()) ! call assert_equal(name, 'f.name'->py3eval()) catch call assert_false(v:exception) endtry *** ../vim-8.1.1983/src/testdir/test_pyx2.vim 2019-06-15 17:57:43.972724036 +0200 --- src/testdir/test_pyx2.vim 2019-09-04 20:03:06.459229142 +0200 *************** *** 34,40 **** func Test_pyxeval() pyx import sys ! call assert_match(s:py2pattern, split(pyxeval('sys.version'))[0]) endfunc --- 34,40 ---- func Test_pyxeval() pyx import sys ! call assert_match(s:py2pattern, split('sys.version'->pyxeval())[0]) endfunc *** ../vim-8.1.1983/src/version.c 2019-09-04 18:53:08.485096735 +0200 --- src/version.c 2019-09-04 20:04:51.790790572 +0200 *************** *** 759,760 **** --- 759,762 ---- { /* Add new patch number below this line */ + /**/ + 1984, /**/ -- A bad peace is better than a good war. - Yiddish Proverb /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///