# HG changeset patch # User Bram Moolenaar # Date 1567969203 -7200 # Node ID 049a7481d7379f4b14cb1cab17f56afbff17c7ae # Parent 403b2abc914795278d4daa6afc83fea72feadbfe patch 8.1.2012: more functions can be used as methods Commit: https://github.com/vim/vim/commit/7ee80f766130f22598c928415c92ec612a8f18cf Author: Bram Moolenaar Date: Sun Sep 8 20:55:06 2019 +0200 patch 8.1.2012: more functions can be used as methods Problem: More functions can be used as methods. Solution: Make terminal functions usable as a method. Fix term_getattr(). diff --git a/runtime/doc/terminal.txt b/runtime/doc/terminal.txt --- a/runtime/doc/terminal.txt +++ b/runtime/doc/terminal.txt @@ -479,6 +479,9 @@ term_dumpdiff({filename}, {filename} [, Using the "s" key the top and bottom parts are swapped. This makes it easy to spot a difference. + Can also be used as a |method|: > + GetFilename()->term_dumpdiff(otherfile) +< *term_dumpload()* term_dumpload({filename} [, {options}]) Open a new window displaying the contents of {filename} @@ -488,6 +491,9 @@ term_dumpload({filename} [, {options}]) For {options} see |term_dumpdiff()|. + Can also be used as a |method|: > + GetFilename()-> term_dumpload() +< *term_dumpwrite()* term_dumpwrite({buf}, {filename} [, {options}]) Dump the contents of the terminal screen of {buf} in the file @@ -502,11 +508,18 @@ term_dumpwrite({buf}, {filename} [, {opt "rows" maximum number of rows to dump "columns" maximum number of columns to dump + Can also be used as a |method|, the base is used for the file + name: > + GetFilename()-> term_dumpwrite(bufnr) + term_getaltscreen({buf}) *term_getaltscreen()* Returns 1 if the terminal of {buf} is using the alternate screen. {buf} is used as with |term_getsize()|. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_getaltscreen() + term_getansicolors({buf}) *term_getansicolors()* Get the ANSI color palette in use by terminal {buf}. @@ -517,8 +530,12 @@ term_getansicolors({buf}) *term_getan {buf} is used as with |term_getsize()|. If the buffer does not exist or is not a terminal window, an empty list is returned. - {only available when compiled with the |+terminal| feature and - with GUI enabled and/or the |+termguicolors| feature} + + Can also be used as a |method|: > + GetBufnr()->term_getansicolors() + +< {only available when compiled with GUI enabled and/or the + |+termguicolors| feature} term_getattr({attr}, {what}) *term_getattr()* Given {attr}, a value returned by term_scrape() in the "attr" @@ -528,7 +545,10 @@ term_getattr({attr}, {what}) *term_ge underline strike reverse - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetAttr()->term_getattr() + term_getcursor({buf}) *term_getcursor()* Get the cursor position of terminal {buf}. Returns a list with @@ -550,13 +570,18 @@ term_getcursor({buf}) *term_getcurso {buf} must be the buffer number of a terminal window. If the buffer does not exist or is not a terminal window, an empty list is returned. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_getcursor() term_getjob({buf}) *term_getjob()* Get the Job associated with terminal window {buf}. {buf} is used as with |term_getsize()|. Returns |v:null| when there is no job. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_getjob() + term_getline({buf}, {row}) *term_getline()* Get a line of text from the terminal window of {buf}. @@ -567,7 +592,10 @@ term_getline({buf}, {row}) *term_getl returned. To get attributes of each character use |term_scrape()|. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_getline(row) + term_getscrolled({buf}) *term_getscrolled()* Return the number of lines that scrolled to above the top of @@ -579,7 +607,10 @@ term_getscrolled({buf}) *term_getscr < (if that line exists). {buf} is used as with |term_getsize()|. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_getscrolled() + term_getsize({buf}) *term_getsize()* Get the size of terminal {buf}. Returns a list with two @@ -589,7 +620,10 @@ term_getsize({buf}) *term_getsize()* {buf} must be the buffer number of a terminal window. Use an empty string for the current buffer. If the buffer does not exist or is not a terminal window, an empty list is returned. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_getsize() + term_getstatus({buf}) *term_getstatus()* Get the status of terminal {buf}. This returns a comma @@ -602,7 +636,10 @@ term_getstatus({buf}) *term_getstatu {buf} must be the buffer number of a terminal window. If the buffer does not exist or is not a terminal window, an empty string is returned. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_getstatus() + term_gettitle({buf}) *term_gettitle()* Get the title of terminal {buf}. This is the title that the @@ -611,7 +648,10 @@ term_gettitle({buf}) *term_gettitle( {buf} must be the buffer number of a terminal window. If the buffer does not exist or is not a terminal window, an empty string is returned. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_gettitle() + term_gettty({buf} [, {input}]) *term_gettty()* Get the name of the controlling terminal associated with @@ -620,12 +660,15 @@ term_gettty({buf} [, {input}]) *term_ When {input} is omitted or 0, return the name for writing (stdout). When {input} is 1 return the name for reading (stdin). On UNIX, both return same name. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_gettty() + term_list() *term_list()* Return a list with the buffer numbers of all buffers for terminal windows. - {only available when compiled with the |+terminal| feature} + term_scrape({buf}, {row}) *term_scrape()* Get the contents of {row} of terminal screen of {buf}. @@ -642,7 +685,10 @@ term_scrape({buf}, {row}) *term_scrap "attr" attributes of the cell, use |term_getattr()| to get the individual flags "width" cell width: 1 or 2 - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_scrape(row) + term_sendkeys({buf}, {keys}) *term_sendkeys()* Send keystrokes {keys} to terminal {buf}. @@ -650,7 +696,10 @@ term_sendkeys({buf}, {keys}) *term_se {keys} are translated as key sequences. For example, "\" means the character CTRL-X. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_sendkeys(keys) + term_setansicolors({buf}, {colors}) *term_setansicolors()* Set the ANSI color palette used by terminal {buf}. @@ -680,8 +729,12 @@ term_setansicolors({buf}, {colors}) *t 'termguicolors' is set. When not using GUI colors (GUI mode or 'termguicolors'), the terminal window always uses the 16 ANSI colors of the underlying terminal. - {only available when compiled with the |+terminal| feature and - with GUI enabled and/or the |+termguicolors| feature} + + Can also be used as a |method|: > + GetBufnr()->term_setansicolors(colors) + +< {only available with GUI enabled and/or the |+termguicolors| + feature} term_setkill({buf}, {how}) *term_setkill()* When exiting Vim or trying to close the terminal window in @@ -695,6 +748,10 @@ term_setkill({buf}, {how}) *term_setk After sending the signal Vim will wait for up to a second to check that the job actually stopped. + Can also be used as a |method|: > + GetBufnr()->term_setkill(how) + + term_setrestore({buf}, {command}) *term_setrestore()* Set the command to write in a session file to restore the job in this terminal. The line written in the session file is: > @@ -703,7 +760,10 @@ term_setrestore({buf}, {command}) *ter Use an empty {command} to run 'shell'. Use "NONE" to not restore this window. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_setrestore(command) + term_setsize({buf}, {rows}, {cols}) *term_setsize()* *E955* Set the size of terminal {buf}. The size of the window @@ -714,7 +774,10 @@ term_setsize({buf}, {rows}, {cols}) *te {buf} must be the buffer number of a terminal window. Use an empty string for the current buffer. If the buffer does not exist or is not a terminal window, an error is given. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_setsize(rows, cols) + term_start({cmd} [, {options}]) *term_start()* Open a terminal window and run {cmd} in it. @@ -781,14 +844,20 @@ term_start({cmd} [, {options}]) *term_ "tty_type" (MS-Windows only): Specify which pty to use. See 'termwintype' for the values. - {only available when compiled with the |+terminal| feature} + Can also be used as a |method|: > + GetCommand()->term_start() + +< {only available when compiled with the |+terminal| feature} + term_wait({buf} [, {time}]) *term_wait()* Wait for pending updates of {buf} to be handled. {buf} is used as with |term_getsize()|. {time} is how long to wait for updates to arrive in msec. If not set then 10 msec will be used. - {only available when compiled with the |+terminal| feature} + + Can also be used as a |method|: > + GetBufnr()->term_wait() ============================================================================== 3. Terminal communication *terminal-communication* diff --git a/src/evalfunc.c b/src/evalfunc.c --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -765,33 +765,33 @@ static funcentry_T global_functions[] = #endif {"tempname", 0, 0, 0, f_tempname}, #ifdef FEAT_TERMINAL - {"term_dumpdiff", 2, 3, 0, f_term_dumpdiff}, - {"term_dumpload", 1, 2, 0, f_term_dumpload}, - {"term_dumpwrite", 2, 3, 0, f_term_dumpwrite}, - {"term_getaltscreen", 1, 1, 0, f_term_getaltscreen}, + {"term_dumpdiff", 2, 3, FEARG_1, f_term_dumpdiff}, + {"term_dumpload", 1, 2, FEARG_1, f_term_dumpload}, + {"term_dumpwrite", 2, 3, FEARG_2, f_term_dumpwrite}, + {"term_getaltscreen", 1, 1, FEARG_1, f_term_getaltscreen}, # if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) - {"term_getansicolors", 1, 1, 0, f_term_getansicolors}, + {"term_getansicolors", 1, 1, FEARG_1, f_term_getansicolors}, # endif - {"term_getattr", 2, 2, 0, f_term_getattr}, - {"term_getcursor", 1, 1, 0, f_term_getcursor}, - {"term_getjob", 1, 1, 0, f_term_getjob}, - {"term_getline", 2, 2, 0, f_term_getline}, - {"term_getscrolled", 1, 1, 0, f_term_getscrolled}, - {"term_getsize", 1, 1, 0, f_term_getsize}, - {"term_getstatus", 1, 1, 0, f_term_getstatus}, - {"term_gettitle", 1, 1, 0, f_term_gettitle}, - {"term_gettty", 1, 2, 0, f_term_gettty}, + {"term_getattr", 2, 2, FEARG_1, f_term_getattr}, + {"term_getcursor", 1, 1, FEARG_1, f_term_getcursor}, + {"term_getjob", 1, 1, FEARG_1, f_term_getjob}, + {"term_getline", 2, 2, FEARG_1, f_term_getline}, + {"term_getscrolled", 1, 1, FEARG_1, f_term_getscrolled}, + {"term_getsize", 1, 1, FEARG_1, f_term_getsize}, + {"term_getstatus", 1, 1, FEARG_1, f_term_getstatus}, + {"term_gettitle", 1, 1, FEARG_1, f_term_gettitle}, + {"term_gettty", 1, 2, FEARG_1, f_term_gettty}, {"term_list", 0, 0, 0, f_term_list}, - {"term_scrape", 2, 2, 0, f_term_scrape}, - {"term_sendkeys", 2, 2, 0, f_term_sendkeys}, + {"term_scrape", 2, 2, FEARG_1, f_term_scrape}, + {"term_sendkeys", 2, 2, FEARG_1, f_term_sendkeys}, # if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) - {"term_setansicolors", 2, 2, 0, f_term_setansicolors}, + {"term_setansicolors", 2, 2, FEARG_1, f_term_setansicolors}, # endif - {"term_setkill", 2, 2, 0, f_term_setkill}, - {"term_setrestore", 2, 2, 0, f_term_setrestore}, - {"term_setsize", 3, 3, 0, f_term_setsize}, - {"term_start", 1, 2, 0, f_term_start}, - {"term_wait", 1, 2, 0, f_term_wait}, + {"term_setkill", 2, 2, FEARG_1, f_term_setkill}, + {"term_setrestore", 2, 2, FEARG_1, f_term_setrestore}, + {"term_setsize", 3, 3, FEARG_1, f_term_setsize}, + {"term_start", 1, 2, FEARG_1, f_term_start}, + {"term_wait", 1, 2, FEARG_1, f_term_wait}, #endif {"test_alloc_fail", 3, 3, FEARG_1, f_test_alloc_fail}, {"test_autochdir", 0, 0, 0, f_test_autochdir}, diff --git a/src/terminal.c b/src/terminal.c --- a/src/terminal.c +++ b/src/terminal.c @@ -5055,6 +5055,8 @@ f_term_getattr(typval_T *argvars, typval if (name == NULL) return; + if (attr > HL_ALL) + attr = syn_attr2attr(attr); for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i) if (STRCMP(name, attrs[i].name) == 0) { diff --git a/src/testdir/test_mksession.vim b/src/testdir/test_mksession.vim --- a/src/testdir/test_mksession.vim +++ b/src/testdir/test_mksession.vim @@ -408,7 +408,7 @@ endfunc func Test_mksession_terminal_restore_other() terminal - call term_setrestore(bufnr('%'), 'other') + eval bufnr('%')->term_setrestore('other') mksession! Xtest_mks.out let lines = readfile('Xtest_mks.out') let term_cmd = '' diff --git a/src/testdir/test_terminal.vim b/src/testdir/test_terminal.vim --- a/src/testdir/test_terminal.vim +++ b/src/testdir/test_terminal.vim @@ -25,7 +25,7 @@ func Run_shell_in_terminal(options) let g:job = term_getjob(buf) call assert_equal(v:t_job, type(g:job)) - let string = string({'job': term_getjob(buf)}) + let string = string({'job': buf->term_getjob()}) call assert_match("{'job': 'process \\d\\+ run'}", string) return buf @@ -42,7 +42,7 @@ func Test_terminal_basic() " ConPTY works on anonymous pipe. if !has('conpty') call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out) - call assert_match('^\\\\.\\pipe\\', term_gettty('')) + call assert_match('^\\\\.\\pipe\\', ''->term_gettty()) endif endif call assert_equal('t', mode()) @@ -91,7 +91,7 @@ func Test_terminal_paste_register() call feedkeys("echo \\"\" \\"=37 + 5\\", 'xt') call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))}) - call WaitForAssert({-> assert_equal('text to paste 42', getline(2))}) + call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())}) exe buf . 'bwipe!' unlet g:job @@ -176,12 +176,14 @@ func Check_123(buf) call assert_true(len(l) == 0) let l = term_scrape(a:buf, 999) call assert_true(len(l) == 0) - let l = term_scrape(a:buf, 1) + let l = a:buf->term_scrape(1) call assert_true(len(l) > 0) call assert_equal('1', l[0].chars) call assert_equal('2', l[1].chars) call assert_equal('3', l[2].chars) call assert_equal('#00e000', l[0].fg) + call assert_equal(0, term_getattr(l[0].attr, 'bold')) + call assert_equal(0, l[0].attr->term_getattr('italic')) if has('win32') " On Windows 'background' always defaults to dark, even though the terminal " may use a light background. Therefore accept both white and black. @@ -238,7 +240,7 @@ func Test_terminal_scrape_multibyte() " multibyte characters. let buf = term_start("cmd /K chcp 65001") call term_sendkeys(buf, "type Xtext\") - call term_sendkeys(buf, "exit\") + eval buf->term_sendkeys("exit\") let line = 4 else let buf = term_start("cat Xtext") @@ -283,7 +285,8 @@ func Test_terminal_scroll() sleep 100m endif - let scrolled = term_getscrolled(buf) + let scrolled = buf->term_getscrolled() + call assert_equal(scrolled, term_getscrolled(buf)) call assert_equal('1', getline(1)) call assert_equal('1', term_getline(buf, 1 - scrolled)) call assert_equal('49', getline(49)) @@ -383,12 +386,12 @@ func Test_terminal_size() vsplit exe 'terminal ++rows=5 ++cols=33 ' . cmd - call assert_equal([5, 33], term_getsize('')) + call assert_equal([5, 33], ''->term_getsize()) call term_setsize('', 6, 0) call assert_equal([6, 33], term_getsize('')) - call term_setsize('', 0, 35) + eval ''->term_setsize(0, 35) call assert_equal([6, 35], term_getsize('')) call term_setsize('', 7, 30) @@ -407,7 +410,7 @@ func Test_terminal_size() bwipe! call assert_equal(20, size[1]) - call term_start(cmd, {'vertical': 1, 'term_cols': 26}) + eval cmd->term_start({'vertical': 1, 'term_cols': 26}) let size = term_getsize('') bwipe! call assert_equal(26, size[1]) @@ -618,7 +621,7 @@ func Test_terminal_env() else call term_sendkeys(buf, "echo $TESTENV\r") endif - call term_wait(buf) + eval buf->term_wait() call StopShellInTerminal(buf) call WaitForAssert({-> assert_equal('correct', getline(2))}) @@ -1006,7 +1009,7 @@ endfunc " Run Vim, start a terminal in that Vim, set the kill argument with " term_setkill(), check that :qall works. func Test_terminal_qall_kill_func() - call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")') + call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")') endfunc " Run Vim, start a terminal in that Vim without the kill argument, @@ -1107,7 +1110,7 @@ func Test_terminal_dumpwrite_composing() call writefile([text], 'Xcomposing') let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {}) call WaitForAssert({-> assert_match(text, term_getline(buf, 1))}) - call term_dumpwrite(buf, 'Xdump') + eval 'Xdump'->term_dumpwrite(buf) let dumpline = readfile('Xdump')[0] call assert_match('|à| |ê| |ö', dumpline) @@ -1127,7 +1130,7 @@ func Test_terminal_dumpload() call Check_dump01(0) " Load another dump in the same window - let buf2 = term_dumpload('dumps/Test_diff_01.dump', {'bufnr': buf}) + let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf}) call assert_equal(buf, buf2) call assert_notequal('one two three four five', trim(getline(1))) @@ -1148,7 +1151,7 @@ endfunc func Test_terminal_dumpdiff() call assert_equal(1, winnr('$')) - call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump') + eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump') call assert_equal(2, winnr('$')) call assert_equal(62, line('$')) call Check_dump01(0) @@ -1490,7 +1493,7 @@ func Test_terminal_ansicolors_func() call assert_equal(s:test_colors, term_getansicolors(buf)) call term_setansicolors(buf, g:terminal_ansi_colors) - call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf)) + call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors()) let colors = [ \ 'ivory', 'AliceBlue', @@ -1502,7 +1505,7 @@ func Test_terminal_ansicolors_func() \ 'grey47', 'gray97', \ 'MistyRose2', 'DodgerBlue4', \] - call term_setansicolors(buf, colors) + eval buf->term_setansicolors(colors) let colors[4] = 'Invalid' call assert_fails('call term_setansicolors(buf, colors)', 'E474:') @@ -1856,7 +1859,7 @@ func Test_terminal_hidden() call term_sendkeys(bnr, "asdf\") call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))}) call term_sendkeys(bnr, "\") - call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))}) + call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())}) bwipe! endfunc @@ -1944,7 +1947,7 @@ func Test_term_getcursor() " Show the cursor. call term_sendkeys(buf, "echo -e '\\033[?25h'\r") - call WaitForAssert({-> assert_equal(1, term_getcursor(buf)[2].visible)}) + call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)}) " Change color of cursor. call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)}) @@ -1989,7 +1992,7 @@ endfunc func Test_term_gettitle() " term_gettitle() returns an empty string for a non-terminal buffer " and for a non-existing buffer. - call assert_equal('', term_gettitle(bufnr('%'))) + call assert_equal('', bufnr('%')->term_gettitle()) call assert_equal('', term_gettitle(bufnr('$') + 1)) if !has('title') || &title == 0 || empty(&t_ts) @@ -2083,3 +2086,19 @@ func Test_terminal_getwinpos() set splitright& only! endfunc + +func Test_terminal_altscreen() + CheckUnix + + let buf = term_start(&shell, {}) + + call term_sendkeys(buf, 'echo "\e[?1047h"' .. "\r") + call term_wait(buf) + call assert_equal(1, term_getaltscreen(buf)) + call term_sendkeys(buf, 'echo "\e[?1047l"' .. "\r") + call term_wait(buf) + call assert_equal(0, buf->term_getaltscreen()) + + call term_sendkeys(buf, "exit\r") + exe buf . "bwipe!" +endfunc diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -758,6 +758,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2012, +/**/ 2011, /**/ 2010,