blob: 353bd54d012b5457d32d407d69f5187604d8c2dc [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarea5d6fa2017-08-18 21:07:11 +02003if !has('terminal')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004 finish
5endif
6
7source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01008source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
11
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 if has('win32')
16 let buf = term_start([&shell,'/k'], a:options)
17 else
18 let buf = term_start(&shell, a:options)
19 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar35422f42017-08-05 16:33:56 +020028 let string = string({'job': term_getjob(buf)})
29 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010042 " ConPTY works on anonymous pipe.
43 if !has('conpty')
44 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
45 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
46 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020047 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020048 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020049 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020051 call assert_match('%aR[^\n]*running]', execute('ls R'))
52 call assert_notmatch('%[^\n]*running]', execute('ls F'))
53 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054
Bram Moolenaar94053a52017-08-01 21:44:33 +020055 call Stop_shell_in_terminal(buf)
56 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020057 call assert_equal('n', mode())
58 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020059 call assert_match('%aF[^\n]*finished]', execute('ls F'))
60 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020062
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 " closing window wipes out the terminal buffer a with finished job
64 close
65 call assert_equal("", bufname(buf))
66
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020067 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020068 unlet g:job
69endfunc
70
71func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020073 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020074 call term_wait(buf)
75
76 setlocal modifiable
77 exe "normal Axxx\<Esc>"
78 call assert_fails(buf . 'bwipe', 'E517')
79 undo
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081 exe buf . 'bwipe'
82 unlet g:job
83endfunc
84
Bram Moolenaar94053a52017-08-01 21:44:33 +020085func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020086 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020087 call assert_fails(buf . 'bwipe', 'E517')
88 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +020089 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +020090 call assert_equal("", bufname(buf))
91
92 unlet g:job
93endfunc
94
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020095func Test_terminal_split_quit()
96 let buf = Run_shell_in_terminal({})
97 call term_wait(buf)
98 split
99 quit!
100 call term_wait(buf)
101 sleep 50m
102 call assert_equal('run', job_status(g:job))
103
104 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200105 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200106
107 exe buf . 'bwipe'
108 unlet g:job
109endfunc
110
Bram Moolenaar94053a52017-08-01 21:44:33 +0200111func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200112 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200113 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200114 quit
115 for nr in range(1, winnr('$'))
116 call assert_notequal(winbufnr(nr), buf)
117 endfor
118 call assert_true(bufloaded(buf))
119 call assert_true(buflisted(buf))
120
121 exe 'split ' . buf . 'buf'
122 call Stop_shell_in_terminal(buf)
123 exe buf . 'bwipe'
124
125 unlet g:job
126endfunc
127
Bram Moolenaar1e115362019-01-09 23:01:02 +0100128func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200129 exe g:buf . 'bwipe!'
130 let g:buf = 0
131endfunc
132
Bram Moolenaar9d189612017-09-09 18:11:00 +0200133func Get_cat_123_cmd()
134 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100135 if !has('conpty')
136 return 'cmd /c "cls && color 2 && echo 123"'
137 else
138 " When clearing twice, extra sequence is not output.
139 return 'cmd /c "cls && cls && color 2 && echo 123"'
140 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200141 else
142 call writefile(["\<Esc>[32m123"], 'Xtext')
143 return "cat Xtext"
144 endif
145endfunc
146
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200147func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200148 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200149 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
150 let g:job = term_getjob(g:buf)
151
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200152 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
153 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200154 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100155 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200156 call delete('Xtext')
157endfunc
158
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200159func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200160 let l = term_scrape(a:buf, 0)
161 call assert_true(len(l) == 0)
162 let l = term_scrape(a:buf, 999)
163 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200164 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200165 call assert_true(len(l) > 0)
166 call assert_equal('1', l[0].chars)
167 call assert_equal('2', l[1].chars)
168 call assert_equal('3', l[2].chars)
169 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar81df6352018-12-22 18:25:30 +0100170 if has('win32')
171 " On Windows 'background' always defaults to dark, even though the terminal
172 " may use a light background. Therefore accept both white and black.
173 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100175 if &background == 'light'
176 call assert_equal('#ffffff', l[0].bg)
177 else
178 call assert_equal('#000000', l[0].bg)
179 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 endif
181
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200182 let l = term_getline(a:buf, -1)
183 call assert_equal('', l)
184 let l = term_getline(a:buf, 0)
185 call assert_equal('', l)
186 let l = term_getline(a:buf, 999)
187 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200188 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200189 call assert_equal('123', l)
190endfunc
191
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200192func Test_terminal_scrape_123()
193 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200194 let buf = term_start(cmd)
195
196 let termlist = term_list()
197 call assert_equal(1, len(termlist))
198 call assert_equal(buf, termlist[0])
199
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200200 " Nothing happens with invalid buffer number
201 call term_wait(1234)
202
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200203 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200204 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200205 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200206 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200207 call Check_123(buf)
208
209 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100210 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200211 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200212 call term_wait(buf)
213 call Check_123(buf)
214
215 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200216 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200217endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200218
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200219func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220 call writefile(["léttまrs"], 'Xtext')
221 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200222 " Run cmd with UTF-8 codepage to make the type command print the expected
223 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100224 let buf = term_start("cmd /K chcp 65001")
225 call term_sendkeys(buf, "type Xtext\<CR>")
226 call term_sendkeys(buf, "exit\<CR>")
227 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200228 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100229 let buf = term_start("cat Xtext")
230 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200231 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200232
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100233 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
234 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200235 call assert_true(len(l) >= 7)
236 call assert_equal('l', l[0].chars)
237 call assert_equal('é', l[1].chars)
238 call assert_equal(1, l[1].width)
239 call assert_equal('t', l[2].chars)
240 call assert_equal('t', l[3].chars)
241 call assert_equal('ま', l[4].chars)
242 call assert_equal(2, l[4].width)
243 call assert_equal('r', l[5].chars)
244 call assert_equal('s', l[6].chars)
245
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200247 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100248 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200249
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100250 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200251 call delete('Xtext')
252endfunc
253
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200254func Test_terminal_scroll()
255 call writefile(range(1, 200), 'Xtext')
256 if has('win32')
257 let cmd = 'cmd /c "type Xtext"'
258 else
259 let cmd = "cat Xtext"
260 endif
261 let buf = term_start(cmd)
262
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200265 call term_wait(buf)
266 if has('win32')
267 " TODO: this should not be needed
268 sleep 100m
269 endif
270
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200271 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200272 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200273 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200274 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200275 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200276 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200277 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200278
279 exe buf . 'bwipe'
280 call delete('Xtext')
281endfunc
282
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200283func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200284 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200285 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200286 call writefile(range(150), 'Xtext')
287 if has('win32')
288 call term_sendkeys(buf, "type Xtext\<CR>")
289 else
290 call term_sendkeys(buf, "cat Xtext\<CR>")
291 endif
292 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200293 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200294 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200295 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200296 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200297
298 call Stop_shell_in_terminal(buf)
299 call term_wait(buf)
300 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200301 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100302 call delete('Xtext')
303endfunc
304
305func Test_terminal_postponed_scrollback()
306 if !has('unix')
307 " tail -f only works on Unix
308 return
309 endif
310
311 call writefile(range(50), 'Xtext')
312 call writefile([
313 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100314 \ 'sleep 400m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100315 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100316 \ 'sleep 200m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100317 \ 'call feedkeys("\<C-W>N", "xt")',
318 \ ], 'XTest_postponed')
319 let buf = RunVimInTerminal('-S XTest_postponed', {})
320 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100321 call term_wait(buf)
Bram Moolenaar96baf022019-02-14 23:49:38 +0100322 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100323
324 silent !echo 'one more line' >>Xtext
325 " Sceen will not change, move cursor to get a different dump
326 call term_sendkeys(buf, "k")
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100327 call term_wait(buf)
Bram Moolenaar96baf022019-02-14 23:49:38 +0100328 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100329
330 " Back to Terminal-Job mode, text will scroll and show the extra line.
331 call term_sendkeys(buf, "a")
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100332 call term_wait(buf)
Bram Moolenaar96baf022019-02-14 23:49:38 +0100333 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100334
335 call term_wait(buf)
336 call term_sendkeys(buf, "\<C-C>")
337 call term_wait(buf)
338 call term_sendkeys(buf, "exit\<CR>")
339 call term_wait(buf)
340 call term_sendkeys(buf, ":q\<CR>")
341 call StopVimInTerminal(buf)
342 call delete('XTest_postponed')
343 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200344endfunc
345
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100346" Run diff on two dumps with different size.
347func Test_terminal_dumpdiff_size()
348 call assert_equal(1, winnr('$'))
349 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
350 call assert_equal(2, winnr('$'))
351 call assert_match('Test_incsearch_search_01.dump', getline(10))
352 call assert_match(' +++++$', getline(11))
353 call assert_match('Test_popup_command_01.dump', getline(31))
354 call assert_equal(repeat('+', 75), getline(30))
355 quit
356endfunc
357
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200358func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200359 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200360
Bram Moolenaarb2412082017-08-20 18:09:14 +0200361 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200362 let size = term_getsize('')
363 bwipe!
364 call assert_equal(5, size[0])
365
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200366 call term_start(cmd, {'term_rows': 6})
367 let size = term_getsize('')
368 bwipe!
369 call assert_equal(6, size[0])
370
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200371 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200372 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200373 call assert_equal([5, 33], term_getsize(''))
374
375 call term_setsize('', 6, 0)
376 call assert_equal([6, 33], term_getsize(''))
377
378 call term_setsize('', 0, 35)
379 call assert_equal([6, 35], term_getsize(''))
380
381 call term_setsize('', 7, 30)
382 call assert_equal([7, 30], term_getsize(''))
383
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200384 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200385 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200386
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200387 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
388 let size = term_getsize('')
389 bwipe!
390 call assert_equal([6, 36], size)
391
Bram Moolenaarb2412082017-08-20 18:09:14 +0200392 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200393 let size = term_getsize('')
394 bwipe!
395 call assert_equal(20, size[1])
396
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200397 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
398 let size = term_getsize('')
399 bwipe!
400 call assert_equal(26, size[1])
401
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200402 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200403 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200404 let size = term_getsize('')
405 bwipe!
406 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200407
408 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
409 let size = term_getsize('')
410 bwipe!
411 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200412
413 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200414endfunc
415
416func Test_terminal_curwin()
417 let cmd = Get_cat_123_cmd()
418 call assert_equal(1, winnr('$'))
419
420 split dummy
421 exe 'terminal ++curwin ' . cmd
422 call assert_equal(2, winnr('$'))
423 bwipe!
424
425 split dummy
426 call term_start(cmd, {'curwin': 1})
427 call assert_equal(2, winnr('$'))
428 bwipe!
429
430 split dummy
431 call setline(1, 'change')
432 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
433 call assert_equal(2, winnr('$'))
434 exe 'terminal! ++curwin ' . cmd
435 call assert_equal(2, winnr('$'))
436 bwipe!
437
438 split dummy
439 call setline(1, 'change')
440 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
441 call assert_equal(2, winnr('$'))
442 bwipe!
443
444 split dummy
445 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200446 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200447endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200448
Bram Moolenaarff546792017-11-21 14:47:57 +0100449func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200450 if s:python != ''
451 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200452 " 500 was not enough for Travis
453 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200454 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200455 echo 'This will take five seconds...'
456 let waittime = 2000
457 if has('win32')
458 let cmd = $windir . '\system32\timeout.exe 1'
459 else
460 let cmd = 'sleep 1'
461 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100463 return [cmd, waittime]
464endfunc
465
466func Test_terminal_finish_open_close()
467 call assert_equal(1, winnr('$'))
468
469 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200470
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100471 " shell terminal closes automatically
472 terminal
473 let buf = bufnr('%')
474 call assert_equal(2, winnr('$'))
475 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200476 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100477 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200478 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100479
480 " shell terminal that does not close automatically
481 terminal ++noclose
482 let buf = bufnr('%')
483 call assert_equal(2, winnr('$'))
484 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200485 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100486 call Stop_shell_in_terminal(buf)
487 call assert_equal(2, winnr('$'))
488 quit
489 call assert_equal(1, winnr('$'))
490
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200491 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200492 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200493 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200494 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200495
496 call term_start(cmd, {'term_finish': 'close'})
497 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200498 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200499 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200500 call assert_equal(1, winnr('$'))
501
502 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200503 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200504 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200505 bwipe
506
507 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200508 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200509 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200510 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200511
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200512 exe 'terminal ++hidden ++open ' . cmd
513 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200514 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200515 bwipe
516
517 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
518 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200519 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200520 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200521
522 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
523 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
524 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
525 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
526
527 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200528 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200529 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200530 call assert_equal(4, winheight(0))
531 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200532endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200533
534func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200535 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200536 return
537 endif
538 call mkdir('Xdir')
539 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200540 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200541
542 exe buf . 'bwipe'
543 call delete('Xdir', 'rf')
544endfunc
545
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200546func Test_terminal_cwd_failure()
547 " Case 1: Provided directory is not actually a directory. Attempt to make
548 " the file executable as well.
549 call writefile([], 'Xfile')
550 call setfperm('Xfile', 'rwx------')
551 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
552 call delete('Xfile')
553
554 " Case 2: Directory does not exist.
555 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
556
557 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100558 " Skip this for root, it will be accessible anyway.
559 if $USER != 'root'
560 call mkdir('XdirNoAccess', '', '0600')
561 " return early if the directory permissions could not be set properly
562 if getfperm('XdirNoAccess')[2] == 'x'
563 call delete('XdirNoAccess', 'rf')
564 return
565 endif
566 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
567 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200568 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200569endfunc
570
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100571func Test_terminal_servername()
572 if !has('clientserver')
573 return
574 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200575 call s:test_environment("VIM_SERVERNAME", v:servername)
576endfunc
577
578func Test_terminal_version()
579 call s:test_environment("VIM_TERMINAL", string(v:version))
580endfunc
581
582func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100583 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100584 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200585 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100586 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200587 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100588 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200589 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100590 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100591 call term_wait(buf)
592 call Stop_shell_in_terminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200593 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100594
Bram Moolenaar012eb662018-03-13 17:55:27 +0100595 exe buf . 'bwipe'
596 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100597endfunc
598
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200599func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100600 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200601 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200602 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100603 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100604 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100605 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100606 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100607 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100608 call term_wait(buf)
609 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200610 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200611
Bram Moolenaar012eb662018-03-13 17:55:27 +0100612 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200613endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200614
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200615func Test_terminal_list_args()
616 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
617 call assert_fails(buf . 'bwipe', 'E517')
618 exe buf . 'bwipe!'
619 call assert_equal("", bufname(buf))
620endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200621
622func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100623 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100624 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200625 " The shell or something else has a problem dealing with more than 1000
626 " characters at the same time.
627 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100628 " NPFS is used in Windows, nonblocking mode does not work properly.
629 elseif has('win32')
630 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200631 else
632 let len = 5000
633 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200634
635 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100636 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200637 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100638 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200639
640 " On MS-Windows there is an extra empty line below "done". Find "done" in
641 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100642 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100643 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100644 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200645 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100646 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200647 endif
648 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200649
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100650 let g:job = term_getjob(buf)
651 call Stop_shell_in_terminal(buf)
652 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200653 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200654 bwipe
655endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200656
657func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200658 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200659 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200660 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200661 if has('win32')
662 " TODO: enable once writing to stdin works on MS-Windows
663 return
664 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200665 new
666 call setline(1, ['one', 'two', 'three'])
667 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200668 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200669 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200670 call assert_equal(['3', '3', '14'], nrs)
671 bwipe
672
Bram Moolenaardada6d22017-09-02 17:18:35 +0200673 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200674 call setline(1, ['one', 'two', 'three', 'four'])
675 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200676 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200677 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200678 call assert_equal(['2', '2', '10'], nrs)
679 bwipe
680
Bram Moolenaardada6d22017-09-02 17:18:35 +0200681 if executable('python')
682 new
683 call setline(1, ['print("hello")'])
684 1term ++eof=exit() python
685 " MS-Windows echoes the input, Unix doesn't.
686 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
687 if getline(1) =~ 'hello'
688 call assert_equal('hello', getline(1))
689 else
690 call assert_equal('hello', getline(line('$') - 1))
691 endif
692 bwipe
693
694 if has('win32')
695 new
696 call setline(1, ['print("hello")'])
697 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200698 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200699 call assert_equal('hello', getline(line('$') - 1))
700 bwipe
701 endif
702 endif
703
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200704 bwipe!
705endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200706
707func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200708 let buf = term_start('NONE', {})
709 call assert_notequal(0, buf)
710
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200711 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200712 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100713 if has('gui_running') && !has('win32')
714 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200715 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100716 else
717 " Otherwise using a job works on all systems.
718 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200719 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200720 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200721
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200722 bwipe!
723endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200724
725func Test_terminal_special_chars()
726 " this file name only works on Unix
727 if !has('unix')
728 return
729 endif
730 call mkdir('Xdir with spaces')
731 call writefile(['x'], 'Xdir with spaces/quoted"file')
732 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200733 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200734 call term_wait('')
735
736 call delete('Xdir with spaces', 'rf')
737 bwipe
738endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200739
740func Test_terminal_wrong_options()
741 call assert_fails('call term_start(&shell, {
742 \ "in_io": "file",
743 \ "in_name": "xxx",
744 \ "out_io": "file",
745 \ "out_name": "xxx",
746 \ "err_io": "file",
747 \ "err_name": "xxx"
748 \ })', 'E474:')
749 call assert_fails('call term_start(&shell, {
750 \ "out_buf": bufnr("%")
751 \ })', 'E474:')
752 call assert_fails('call term_start(&shell, {
753 \ "err_buf": bufnr("%")
754 \ })', 'E474:')
755endfunc
756
757func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200758 let cmd = Get_cat_123_cmd()
759 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
760 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100761 " ConPTY may precede escape sequence. There are things that are not so.
762 if !has('conpty')
763 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
764 call assert_match('123', readfile('Xfile')[0])
765 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200766 let g:job = term_getjob(buf)
767 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
768 call delete('Xfile')
769 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200770
771 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200772 call writefile(['one line'], 'Xfile')
773 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
774 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200775 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200776 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200777 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200778 bwipe
779 call delete('Xfile')
780 endif
781endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200782
783func TerminalTmap(remap)
784 let buf = Run_shell_in_terminal({})
785 call assert_equal('t', mode())
786
787 if a:remap
788 tmap 123 456
789 else
790 tnoremap 123 456
791 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100792 " don't use abcde, it's an existing command
793 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200794 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100795 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200796 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200797 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200798 let lnum = term_getcursor(buf)[0]
799 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100800 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200801 else
802 call assert_match('456', term_getline(buf, lnum))
803 endif
804
805 call term_sendkeys(buf, "\r")
806 call Stop_shell_in_terminal(buf)
807 call term_wait(buf)
808
809 tunmap 123
810 tunmap 456
811 call assert_equal('', maparg('123', 't'))
812 close
813 unlet g:job
814endfunc
815
816func Test_terminal_tmap()
817 call TerminalTmap(1)
818 call TerminalTmap(0)
819endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200820
821func Test_terminal_wall()
822 let buf = Run_shell_in_terminal({})
823 wall
824 call Stop_shell_in_terminal(buf)
825 call term_wait(buf)
826 exe buf . 'bwipe'
827 unlet g:job
828endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200829
Bram Moolenaar7a760922018-02-19 23:10:02 +0100830func Test_terminal_wqall()
831 let buf = Run_shell_in_terminal({})
832 call assert_fails('wqall', 'E948')
833 call Stop_shell_in_terminal(buf)
834 call term_wait(buf)
835 exe buf . 'bwipe'
836 unlet g:job
837endfunc
838
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200839func Test_terminal_composing_unicode()
840 let save_enc = &encoding
841 set encoding=utf-8
842
843 if has('win32')
844 let cmd = "cmd /K chcp 65001"
845 let lnum = [3, 6, 9]
846 else
847 let cmd = &shell
848 let lnum = [1, 3, 5]
849 endif
850
851 enew
852 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100853 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200854 call term_wait(buf, 50)
855
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200856 if has('win32')
857 call assert_equal('cmd', job_info(g:job).cmd[0])
858 else
859 call assert_equal(&shell, job_info(g:job).cmd[0])
860 endif
861
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200862 " ascii + composing
863 let txt = "a\u0308bc"
864 call term_sendkeys(buf, "echo " . txt . "\r")
865 call term_wait(buf, 50)
866 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
867 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
868 let l = term_scrape(buf, lnum[0] + 1)
869 call assert_equal("a\u0308", l[0].chars)
870 call assert_equal("b", l[1].chars)
871 call assert_equal("c", l[2].chars)
872
873 " multibyte + composing
874 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
875 call term_sendkeys(buf, "echo " . txt . "\r")
876 call term_wait(buf, 50)
877 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
878 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
879 let l = term_scrape(buf, lnum[1] + 1)
880 call assert_equal("\u304b\u3099", l[0].chars)
881 call assert_equal("\u304e", l[1].chars)
882 call assert_equal("\u304f\u3099", l[2].chars)
883 call assert_equal("\u3052", l[3].chars)
884 call assert_equal("\u3053\u3099", l[4].chars)
885
886 " \u00a0 + composing
887 let txt = "abc\u00a0\u0308"
888 call term_sendkeys(buf, "echo " . txt . "\r")
889 call term_wait(buf, 50)
890 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
891 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
892 let l = term_scrape(buf, lnum[2] + 1)
893 call assert_equal("\u00a0\u0308", l[3].chars)
894
895 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200896 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200897 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100898 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200899 let &encoding = save_enc
900endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100901
902func Test_terminal_aucmd_on_close()
903 fun Nop()
904 let s:called = 1
905 endfun
906
907 aug repro
908 au!
909 au BufWinLeave * call Nop()
910 aug END
911
912 let [cmd, waittime] = s:get_sleep_cmd()
913
914 call assert_equal(1, winnr('$'))
915 new
916 call setline(1, ['one', 'two'])
917 exe 'term ++close ' . cmd
918 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200919 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100920 call assert_equal(1, s:called)
921 bwipe!
922
923 unlet s:called
924 au! repro
925 delfunc Nop
926endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100927
928func Test_terminal_term_start_empty_command()
929 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
930 call assert_fails(cmd, 'E474')
931 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
932 call assert_fails(cmd, 'E474')
933 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
934 call assert_fails(cmd, 'E474')
935 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
936 call assert_fails(cmd, 'E474')
937endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100938
939func Test_terminal_response_to_control_sequence()
940 if !has('unix')
941 return
942 endif
943
944 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200945 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100946
Bram Moolenaar086eb872018-03-25 21:24:12 +0200947 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200948 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100949
Bram Moolenaar086eb872018-03-25 21:24:12 +0200950 " Request the cursor position.
951 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100952
953 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200954 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100955
Bram Moolenaar086eb872018-03-25 21:24:12 +0200956 " End "cat" gently.
957 call term_sendkeys(buf, "\<CR>\<C-D>")
958
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100959 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100960 exe buf . 'bwipe'
961 unlet g:job
962endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100963
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100964" Run Vim, start a terminal in that Vim with the kill argument,
965" :qall works.
966func Run_terminal_qall_kill(line1, line2)
967 " 1. Open a terminal window and wait for the prompt to appear
968 " 2. set kill using term_setkill()
969 " 3. make Vim exit, it will kill the shell
970 let after = [
971 \ a:line1,
972 \ 'let buf = bufnr("%")',
973 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
974 \ ' sleep 10m',
975 \ 'endwhile',
976 \ a:line2,
977 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
978 \ 'qall',
979 \ ]
980 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100981 return
982 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100983 call assert_equal("done", readfile("Xdone")[0])
984 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100985endfunc
986
987" Run Vim in a terminal, then start a terminal in that Vim with a kill
988" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100989func Test_terminal_qall_kill_arg()
990 call Run_terminal_qall_kill('term ++kill=kill', '')
991endfunc
992
993" Run Vim, start a terminal in that Vim, set the kill argument with
994" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100995func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100996 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
997endfunc
998
999" Run Vim, start a terminal in that Vim without the kill argument,
1000" check that :qall does not exit, :qall! does.
1001func Test_terminal_qall_exit()
1002 let after = [
1003 \ 'term',
1004 \ 'let buf = bufnr("%")',
1005 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1006 \ ' sleep 10m',
1007 \ 'endwhile',
1008 \ 'set nomore',
1009 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
1010 \ 'qall',
1011 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
1012 \ 'cquit',
1013 \ ]
1014 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001015 return
1016 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001017 call assert_equal("done", readfile("Xdone")[0])
1018 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001019endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001020
1021" Run Vim in a terminal, then start a terminal in that Vim without a kill
1022" argument, check that :confirm qall works.
1023func Test_terminal_qall_prompt()
1024 if !CanRunVimInTerminal()
1025 return
1026 endif
1027 let buf = RunVimInTerminal('', {})
1028
1029 " Open a terminal window and wait for the prompt to appear
1030 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001031 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1032 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001033
1034 " make Vim exit, it will prompt to kill the shell
1035 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001036 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001037 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001038 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001039
1040 " close the terminal window where Vim was running
1041 quit
1042endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001043
Bram Moolenaar012eb662018-03-13 17:55:27 +01001044func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001045 augroup repro
1046 au!
1047 au TerminalOpen * let s:called += 1
1048 augroup END
1049
1050 let s:called = 0
1051
1052 " Open a terminal window with :terminal
1053 terminal
1054 call assert_equal(1, s:called)
1055 bwipe!
1056
1057 " Open a terminal window with term_start()
1058 call term_start(&shell)
1059 call assert_equal(2, s:called)
1060 bwipe!
1061
1062 " Open a hidden terminal buffer with :terminal
1063 terminal ++hidden
1064 call assert_equal(3, s:called)
1065 for buf in term_list()
1066 exe buf . "bwipe!"
1067 endfor
1068
1069 " Open a hidden terminal buffer with term_start()
1070 let buf = term_start(&shell, {'hidden': 1})
1071 call assert_equal(4, s:called)
1072 exe buf . "bwipe!"
1073
1074 unlet s:called
1075 au! repro
1076endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001077
1078func Check_dump01(off)
1079 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1080 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001081 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001082endfunc
1083
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001084func Test_terminal_dumpwrite_composing()
1085 if !CanRunVimInTerminal()
1086 return
1087 endif
1088 let save_enc = &encoding
1089 set encoding=utf-8
1090 call assert_equal(1, winnr('$'))
1091
1092 let text = " a\u0300 e\u0302 o\u0308"
1093 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001094 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001095 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001096 call term_dumpwrite(buf, 'Xdump')
1097 let dumpline = readfile('Xdump')[0]
1098 call assert_match('|à| |ê| |ö', dumpline)
1099
1100 call StopVimInTerminal(buf)
1101 call delete('Xcomposing')
1102 call delete('Xdump')
1103 let &encoding = save_enc
1104endfunc
1105
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001106" just testing basic functionality.
1107func Test_terminal_dumpload()
1108 call assert_equal(1, winnr('$'))
1109 call term_dumpload('dumps/Test_popup_command_01.dump')
1110 call assert_equal(2, winnr('$'))
1111 call assert_equal(20, line('$'))
1112 call Check_dump01(0)
1113 quit
1114endfunc
1115
1116func Test_terminal_dumpdiff()
1117 call assert_equal(1, winnr('$'))
1118 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1119 call assert_equal(2, winnr('$'))
1120 call assert_equal(62, line('$'))
1121 call Check_dump01(0)
1122 call Check_dump01(42)
1123 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1124 quit
1125endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001126
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001127func Test_terminal_dumpdiff_swap()
1128 call assert_equal(1, winnr('$'))
1129 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1130 call assert_equal(2, winnr('$'))
1131 call assert_equal(62, line('$'))
1132 call assert_match('Test_popup_command_01.dump', getline(21))
1133 call assert_match('Test_popup_command_03.dump', getline(42))
1134 call assert_match('Undo', getline(3))
1135 call assert_match('three four five', getline(45))
1136
1137 normal s
1138 call assert_match('Test_popup_command_03.dump', getline(21))
1139 call assert_match('Test_popup_command_01.dump', getline(42))
1140 call assert_match('three four five', getline(3))
1141 call assert_match('Undo', getline(45))
1142 quit
1143endfunc
1144
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001145func Test_terminal_dumpdiff_options()
1146 set laststatus=0
1147 call assert_equal(1, winnr('$'))
1148 let height = winheight(0)
1149 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1150 call assert_equal(2, winnr('$'))
1151 call assert_equal(height, winheight(winnr()))
1152 call assert_equal(33, winwidth(winnr()))
1153 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1154 quit
1155
1156 call assert_equal(1, winnr('$'))
1157 let width = winwidth(0)
1158 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1159 call assert_equal(2, winnr('$'))
1160 call assert_equal(width, winwidth(winnr()))
1161 call assert_equal(13, winheight(winnr()))
1162 call assert_equal('something else', bufname('%'))
1163 quit
1164
1165 call assert_equal(1, winnr('$'))
1166 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1167 call assert_equal(1, winnr('$'))
1168 bwipe
1169
1170 set laststatus&
1171endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001172
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001173func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001174 call assert_equal(1, winnr('$'))
1175
1176 " Use the title termcap entries to output the escape sequence.
1177 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001178 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001179 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001180 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001181 \ 'redraw',
1182 \ "set t_ts=",
1183 \ ], 'Xscript')
1184 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001185 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001186 call assert_equal('Xtextfile', expand('%:t'))
1187 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001188 return buf
1189endfunc
1190
1191func Test_terminal_api_drop_newwin()
1192 if !CanRunVimInTerminal()
1193 return
1194 endif
1195 let buf = Api_drop_common('')
1196 call assert_equal(0, &bin)
1197 call assert_equal('', &fenc)
1198
1199 call StopVimInTerminal(buf)
1200 call delete('Xscript')
1201 bwipe Xtextfile
1202endfunc
1203
1204func Test_terminal_api_drop_newwin_bin()
1205 if !CanRunVimInTerminal()
1206 return
1207 endif
1208 let buf = Api_drop_common(',{"bin":1}')
1209 call assert_equal(1, &bin)
1210
1211 call StopVimInTerminal(buf)
1212 call delete('Xscript')
1213 bwipe Xtextfile
1214endfunc
1215
1216func Test_terminal_api_drop_newwin_binary()
1217 if !CanRunVimInTerminal()
1218 return
1219 endif
1220 let buf = Api_drop_common(',{"binary":1}')
1221 call assert_equal(1, &bin)
1222
1223 call StopVimInTerminal(buf)
1224 call delete('Xscript')
1225 bwipe Xtextfile
1226endfunc
1227
1228func Test_terminal_api_drop_newwin_nobin()
1229 if !CanRunVimInTerminal()
1230 return
1231 endif
1232 set binary
1233 let buf = Api_drop_common(',{"nobin":1}')
1234 call assert_equal(0, &bin)
1235
1236 call StopVimInTerminal(buf)
1237 call delete('Xscript')
1238 bwipe Xtextfile
1239 set nobinary
1240endfunc
1241
1242func Test_terminal_api_drop_newwin_nobinary()
1243 if !CanRunVimInTerminal()
1244 return
1245 endif
1246 set binary
1247 let buf = Api_drop_common(',{"nobinary":1}')
1248 call assert_equal(0, &bin)
1249
1250 call StopVimInTerminal(buf)
1251 call delete('Xscript')
1252 bwipe Xtextfile
1253 set nobinary
1254endfunc
1255
1256func Test_terminal_api_drop_newwin_ff()
1257 if !CanRunVimInTerminal()
1258 return
1259 endif
1260 let buf = Api_drop_common(',{"ff":"dos"}')
1261 call assert_equal("dos", &ff)
1262
1263 call StopVimInTerminal(buf)
1264 call delete('Xscript')
1265 bwipe Xtextfile
1266endfunc
1267
1268func Test_terminal_api_drop_newwin_fileformat()
1269 if !CanRunVimInTerminal()
1270 return
1271 endif
1272 let buf = Api_drop_common(',{"fileformat":"dos"}')
1273 call assert_equal("dos", &ff)
1274
1275 call StopVimInTerminal(buf)
1276 call delete('Xscript')
1277 bwipe Xtextfile
1278endfunc
1279
1280func Test_terminal_api_drop_newwin_enc()
1281 if !CanRunVimInTerminal()
1282 return
1283 endif
1284 let buf = Api_drop_common(',{"enc":"utf-16"}')
1285 call assert_equal("utf-16", &fenc)
1286
1287 call StopVimInTerminal(buf)
1288 call delete('Xscript')
1289 bwipe Xtextfile
1290endfunc
1291
1292func Test_terminal_api_drop_newwin_encoding()
1293 if !CanRunVimInTerminal()
1294 return
1295 endif
1296 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1297 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001298
1299 call StopVimInTerminal(buf)
1300 call delete('Xscript')
1301 bwipe Xtextfile
1302endfunc
1303
1304func Test_terminal_api_drop_oldwin()
1305 if !CanRunVimInTerminal()
1306 return
1307 endif
1308 let firstwinid = win_getid()
1309 split Xtextfile
1310 let textfile_winid = win_getid()
1311 call assert_equal(2, winnr('$'))
1312 call win_gotoid(firstwinid)
1313
1314 " Use the title termcap entries to output the escape sequence.
1315 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001316 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001317 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1318 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1319 \ 'redraw',
1320 \ "set t_ts=",
1321 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001322 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001323 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001324 call assert_equal(textfile_winid, win_getid())
1325
1326 call StopVimInTerminal(buf)
1327 call delete('Xscript')
1328 bwipe Xtextfile
1329endfunc
1330
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001331func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001332 let g:called_bufnum = a:bufnum
1333 let g:called_arg = a:arg
1334endfunc
1335
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001336func WriteApiCall(funcname)
1337 " Use the title termcap entries to output the escape sequence.
1338 call writefile([
1339 \ 'set title',
1340 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1341 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1342 \ 'redraw',
1343 \ "set t_ts=",
1344 \ ], 'Xscript')
1345endfunc
1346
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001347func Test_terminal_api_call()
1348 if !CanRunVimInTerminal()
1349 return
1350 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001351
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001352 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001353 let buf = RunVimInTerminal('-S Xscript', {})
1354 call WaitFor({-> exists('g:called_bufnum')})
1355 call assert_equal(buf, g:called_bufnum)
1356 call assert_equal(['hello', 123], g:called_arg)
1357
1358 call StopVimInTerminal(buf)
1359 call delete('Xscript')
1360 unlet g:called_bufnum
1361 unlet g:called_arg
1362endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001363
1364func Test_terminal_api_call_fails()
1365 if !CanRunVimInTerminal()
1366 return
1367 endif
1368
1369 call WriteApiCall('TryThis')
1370 call ch_logfile('Xlog', 'w')
1371 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001372 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001373
1374 call StopVimInTerminal(buf)
1375 call delete('Xscript')
1376 call ch_logfile('', '')
1377 call delete('Xlog')
1378endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001379
Bram Moolenaara997b452018-04-17 23:24:06 +02001380let s:caught_e937 = 0
1381
1382func Tapi_Delete(bufnum, arg)
1383 try
1384 execute 'bdelete!' a:bufnum
1385 catch /E937:/
1386 let s:caught_e937 = 1
1387 endtry
1388endfunc
1389
1390func Test_terminal_api_call_fail_delete()
1391 if !CanRunVimInTerminal()
1392 return
1393 endif
1394
1395 call WriteApiCall('Tapi_Delete')
1396 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001397 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001398
1399 call StopVimInTerminal(buf)
1400 call delete('Xscript')
1401 call ch_logfile('', '')
1402endfunc
1403
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001404func Test_terminal_ansicolors_default()
1405 let colors = [
1406 \ '#000000', '#e00000',
1407 \ '#00e000', '#e0e000',
1408 \ '#0000e0', '#e000e0',
1409 \ '#00e0e0', '#e0e0e0',
1410 \ '#808080', '#ff4040',
1411 \ '#40ff40', '#ffff40',
1412 \ '#4040ff', '#ff40ff',
1413 \ '#40ffff', '#ffffff',
1414 \]
1415
1416 let buf = Run_shell_in_terminal({})
1417 call assert_equal(colors, term_getansicolors(buf))
1418 call Stop_shell_in_terminal(buf)
1419 call term_wait(buf)
1420
1421 exe buf . 'bwipe'
1422endfunc
1423
1424let s:test_colors = [
1425 \ '#616e64', '#0d0a79',
1426 \ '#6d610d', '#0a7373',
1427 \ '#690d0a', '#6d696e',
1428 \ '#0d0a6f', '#616e0d',
1429 \ '#0a6479', '#6d0d0a',
1430 \ '#617373', '#0d0a69',
1431 \ '#6d690d', '#0a6e6f',
1432 \ '#610d0a', '#6e6479',
1433 \]
1434
1435func Test_terminal_ansicolors_global()
1436 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1437 let buf = Run_shell_in_terminal({})
1438 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1439 call Stop_shell_in_terminal(buf)
1440 call term_wait(buf)
1441
1442 exe buf . 'bwipe'
1443 unlet g:terminal_ansi_colors
1444endfunc
1445
1446func Test_terminal_ansicolors_func()
1447 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1448 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1449 call assert_equal(s:test_colors, term_getansicolors(buf))
1450
1451 call term_setansicolors(buf, g:terminal_ansi_colors)
1452 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1453
1454 let colors = [
1455 \ 'ivory', 'AliceBlue',
1456 \ 'grey67', 'dark goldenrod',
1457 \ 'SteelBlue3', 'PaleVioletRed4',
1458 \ 'MediumPurple2', 'yellow2',
1459 \ 'RosyBrown3', 'OrangeRed2',
1460 \ 'white smoke', 'navy blue',
1461 \ 'grey47', 'gray97',
1462 \ 'MistyRose2', 'DodgerBlue4',
1463 \]
1464 call term_setansicolors(buf, colors)
1465
1466 let colors[4] = 'Invalid'
1467 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1468
1469 call Stop_shell_in_terminal(buf)
1470 call term_wait(buf)
1471 exe buf . 'bwipe'
1472endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001473
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001474func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001475 if !CanRunVimInTerminal()
1476 return
1477 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001478 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001479 let text = []
1480 for n in range(10)
1481 call add(text, repeat(n, 50))
1482 endfor
1483 call writefile(text, 'Xwinsize')
1484 let buf = RunVimInTerminal('Xwinsize', {})
1485 let win = bufwinid(buf)
1486 call assert_equal([6, 40], term_getsize(buf))
1487 call assert_equal(6, winheight(win))
1488 call assert_equal(40, winwidth(win))
1489
1490 " resizing the window doesn't resize the terminal.
1491 resize 10
1492 vertical resize 60
1493 call assert_equal([6, 40], term_getsize(buf))
1494 call assert_equal(10, winheight(win))
1495 call assert_equal(60, winwidth(win))
1496
1497 call StopVimInTerminal(buf)
1498 call delete('Xwinsize')
1499
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001500 call assert_fails('set termwinsize=40', 'E474')
1501 call assert_fails('set termwinsize=10+40', 'E474')
1502 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001503
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001504 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001505endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001506
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001507func Test_terminal_termwinsize_option_zero()
1508 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001509 let buf = Run_shell_in_terminal({})
1510 let win = bufwinid(buf)
1511 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1512 call Stop_shell_in_terminal(buf)
1513 call term_wait(buf)
1514 exe buf . 'bwipe'
1515
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001516 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001517 let buf = Run_shell_in_terminal({})
1518 let win = bufwinid(buf)
1519 call assert_equal([7, winwidth(win)], term_getsize(buf))
1520 call Stop_shell_in_terminal(buf)
1521 call term_wait(buf)
1522 exe buf . 'bwipe'
1523
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001524 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001525 let buf = Run_shell_in_terminal({})
1526 let win = bufwinid(buf)
1527 call assert_equal([winheight(win), 33], term_getsize(buf))
1528 call Stop_shell_in_terminal(buf)
1529 call term_wait(buf)
1530 exe buf . 'bwipe'
1531
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001532 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001533endfunc
1534
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001535func Test_terminal_termwinsize_mininmum()
1536 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001537 vsplit
1538 let buf = Run_shell_in_terminal({})
1539 let win = bufwinid(buf)
1540 call assert_inrange(10, 1000, winheight(win))
1541 call assert_inrange(50, 1000, winwidth(win))
1542 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1543
1544 resize 15
1545 vertical resize 60
1546 redraw
1547 call assert_equal([15, 60], term_getsize(buf))
1548 call assert_equal(15, winheight(win))
1549 call assert_equal(60, winwidth(win))
1550
1551 resize 7
1552 vertical resize 30
1553 redraw
1554 call assert_equal([10, 50], term_getsize(buf))
1555 call assert_equal(7, winheight(win))
1556 call assert_equal(30, winwidth(win))
1557
1558 call Stop_shell_in_terminal(buf)
1559 call term_wait(buf)
1560 exe buf . 'bwipe'
1561
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001562 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001563 let buf = Run_shell_in_terminal({})
1564 let win = bufwinid(buf)
1565 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1566 call Stop_shell_in_terminal(buf)
1567 call term_wait(buf)
1568 exe buf . 'bwipe'
1569
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001570 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001571endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001572
1573func Test_terminal_termwinkey()
1574 call assert_equal(1, winnr('$'))
1575 let thiswin = win_getid()
1576
1577 let buf = Run_shell_in_terminal({})
1578 let termwin = bufwinid(buf)
1579 set termwinkey=<C-L>
1580 call feedkeys("\<C-L>w", 'tx')
1581 call assert_equal(thiswin, win_getid())
1582 call feedkeys("\<C-W>w", 'tx')
1583
1584 let job = term_getjob(buf)
1585 call feedkeys("\<C-L>\<C-C>", 'tx')
1586 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001587
1588 set termwinkey&
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001589endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001590
1591func Test_terminal_out_err()
1592 if !has('unix')
1593 return
1594 endif
1595 call writefile([
1596 \ '#!/bin/sh',
1597 \ 'echo "this is standard error" >&2',
1598 \ 'echo "this is standard out" >&1',
1599 \ ], 'Xechoerrout.sh')
1600 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1601
1602 let outfile = 'Xtermstdout'
1603 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001604
1605 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1606 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001607 call assert_equal('this is standard error', term_getline(buf, 1))
1608
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001609 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001610 exe buf . 'bwipe'
1611 call delete('Xechoerrout.sh')
1612 call delete(outfile)
1613endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001614
1615func Test_terminwinscroll()
1616 if !has('unix')
1617 return
1618 endif
1619
1620 " Let the terminal output more than 'termwinscroll' lines, some at the start
1621 " will be dropped.
1622 exe 'set termwinscroll=' . &lines
1623 let buf = term_start('/bin/sh')
1624 for i in range(1, &lines)
1625 call feedkeys("echo " . i . "\<CR>", 'xt')
1626 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1627 endfor
1628 " Go to Terminal-Normal mode to update the buffer.
1629 call feedkeys("\<C-W>N", 'xt')
1630 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1631
1632 " Every "echo nr" must only appear once
1633 let lines = getline(1, line('$'))
1634 for i in range(&lines - len(lines) / 2 + 2, &lines)
1635 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1636 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1637 endfor
1638
1639 exe buf . 'bwipe!'
1640endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001641
Bram Moolenaar875cf872018-07-08 20:49:07 +02001642" Resizing the terminal window caused an ml_get error.
1643" TODO: This does not reproduce the original problem.
1644func Test_terminal_resize()
1645 set statusline=x
1646 terminal
1647 call assert_equal(2, winnr('$'))
1648
1649 " Fill the terminal with text.
1650 if has('win32')
1651 call feedkeys("dir\<CR>", 'xt')
1652 else
1653 call feedkeys("ls\<CR>", 'xt')
1654 endif
1655 " Go to Terminal-Normal mode for a moment.
1656 call feedkeys("\<C-W>N", 'xt')
1657 " Open a new window
1658 call feedkeys("i\<C-W>n", 'xt')
1659 call assert_equal(3, winnr('$'))
1660 redraw
1661
1662 close
1663 call assert_equal(2, winnr('$'))
1664 call feedkeys("exit\<CR>", 'xt')
1665 set statusline&
1666endfunc
1667
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001668" must be nearly the last, we can't go back from GUI to terminal
1669func Test_zz1_terminal_in_gui()
1670 if !CanRunGui()
1671 return
1672 endif
1673
1674 " Ignore the "failed to create input context" error.
1675 call test_ignore_error('E285:')
1676
1677 gui -f
1678
1679 call assert_equal(1, winnr('$'))
1680 let buf = Run_shell_in_terminal({'term_finish': 'close'})
1681 call Stop_shell_in_terminal(buf)
1682 call term_wait(buf)
1683
1684 " closing window wipes out the terminal buffer a with finished job
1685 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1686 call assert_equal("", bufname(buf))
1687
1688 unlet g:job
1689endfunc
1690
1691func Test_zz2_terminal_guioptions_bang()
1692 if !has('gui_running')
1693 return
1694 endif
1695 set guioptions+=!
1696
1697 let filename = 'Xtestscript'
1698 if has('win32')
1699 let filename .= '.bat'
1700 let prefix = ''
1701 let contents = ['@echo off', 'exit %1']
1702 else
1703 let filename .= '.sh'
1704 let prefix = './'
1705 let contents = ['#!/bin/sh', 'exit $1']
1706 endif
1707 call writefile(contents, filename)
1708 call setfperm(filename, 'rwxrwx---')
1709
1710 " Check if v:shell_error is equal to the exit status.
1711 let exitval = 0
1712 execute printf(':!%s%s %d', prefix, filename, exitval)
1713 call assert_equal(exitval, v:shell_error)
1714
1715 let exitval = 9
1716 execute printf(':!%s%s %d', prefix, filename, exitval)
1717 call assert_equal(exitval, v:shell_error)
1718
1719 set guioptions&
1720 call delete(filename)
1721endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001722
1723func Test_terminal_hidden()
1724 if !has('unix')
1725 return
1726 endif
1727 term ++hidden cat
1728 let bnr = bufnr('$')
1729 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1730 exe 'sbuf ' . bnr
1731 call assert_equal('terminal', &buftype)
1732 call term_sendkeys(bnr, "asdf\<CR>")
1733 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1734 call term_sendkeys(bnr, "\<C-D>")
1735 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1736 bwipe!
1737endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001738
1739func Test_terminal_hidden_and_close()
1740 if !has('unix')
1741 return
1742 endif
1743 call assert_equal(1, winnr('$'))
1744 term ++hidden ++close ls
1745 let bnr = bufnr('$')
1746 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1747 call WaitForAssert({-> assert_false(bufexists(bnr))})
1748 call assert_equal(1, winnr('$'))
1749endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001750
1751func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001752 " This test does not pass through ConPTY.
1753 if has('conpty')
1754 return
1755 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001756 let contents = [
1757 \ [ 'One', '', 'X' ],
1758 \ [ 'Two', '', '' ],
1759 \ [ 'Three' ] + repeat([''], 30)
1760 \ ]
1761
1762 for c in contents
1763 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001764 if has('win32')
1765 term cmd /c type Xfile
1766 else
1767 term cat Xfile
1768 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001769 let bnr = bufnr('$')
1770 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1771 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001772 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001773 call assert_equal(c, getline(1, line('$')))
1774 quit
1775 endfor
1776
1777 call delete('Xfile')
1778endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001779
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001780func Test_terminal_no_job()
1781 let term = term_start('false', {'term_finish': 'close'})
1782 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1783endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001784
1785func Test_term_gettitle()
1786 if !has('title') || empty(&t_ts)
1787 return
1788 endif
1789 " TODO: this fails on Travis
1790 return
1791
1792 " term_gettitle() returns an empty string for a non-terminal buffer
1793 " or for a non-existing buffer.
1794 call assert_equal('', term_gettitle(bufnr('%')))
1795 call assert_equal('', term_gettitle(bufnr('$') + 1))
1796
1797 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
1798 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
1799
1800 call term_sendkeys(term, ":e Xfoo\r")
1801 call WaitForAssert({-> assert_match('Xfoo (.*[/\\]testdir) - VIM', term_gettitle(term)) })
1802
1803 call term_sendkeys(term, ":set titlestring=foo\r")
1804 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
1805
1806 exe term . 'bwipe!'
1807endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01001808
1809" When drawing the statusline the cursor position may not have been updated
1810" yet.
1811" 1. create a terminal, make it show 2 lines
1812" 2. 0.5 sec later: leave terminal window, execute "i"
1813" 3. 0.5 sec later: clear terminal window, now it's 1 line
1814" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1815" 4. 0.5 sec later: should be done, clean up
1816func Test_terminal_statusline()
1817 if !has('unix')
1818 return
1819 endif
1820 set statusline=x
1821 terminal
1822 let tbuf = bufnr('')
1823 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1824 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1825 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1826 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1827
1828 sleep 2
1829 exe tbuf . 'bwipe!'
1830 au! BufLeave
1831 set statusline=
1832endfunc