blob: eb83c6b893fe0b9c69238f57932af61e13309109 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02005
6source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01007source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02008
Bram Moolenaarb81bc772017-08-11 22:45:01 +02009let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020010let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020011
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 Moolenaar373a8762020-03-19 19:44:32 +010020 let g:test_is_flaky = 1
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020021
22 let termlist = term_list()
23 call assert_equal(1, len(termlist))
24 call assert_equal(buf, termlist[0])
25
26 let g:job = term_getjob(buf)
27 call assert_equal(v:t_job, type(g:job))
28
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020029 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020030 call assert_match("{'job': 'process \\d\\+ run'}", string)
31
Bram Moolenaar94053a52017-08-01 21:44:33 +020032 return buf
33endfunc
34
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020036 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020037 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020038
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020039 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020040 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020041 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020042 call assert_match('%aR[^\n]*running]', execute('ls R'))
43 call assert_notmatch('%[^\n]*running]', execute('ls F'))
44 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020045
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020046 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +020047 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020048 call assert_equal('n', mode())
49 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020050 call assert_match('%aF[^\n]*finished]', execute('ls F'))
51 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
52 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020053
Bram Moolenaar94053a52017-08-01 21:44:33 +020054 " closing window wipes out the terminal buffer a with finished job
55 close
56 call assert_equal("", bufname(buf))
57
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020058 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020059 unlet g:job
60endfunc
61
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020062func Test_terminal_TerminalWinOpen()
63 au TerminalWinOpen * let b:done = 'yes'
64 let buf = Run_shell_in_terminal({})
65 call assert_equal('yes', b:done)
66 call StopShellInTerminal(buf)
67 " closing window wipes out the terminal buffer with the finished job
68 close
69
70 if has("unix")
71 terminal ++hidden ++open sleep 1
72 sleep 1
73 call assert_fails("echo b:done", 'E121:')
74 endif
75
76 au! TerminalWinOpen
77endfunc
78
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020079func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020080 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020081 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020082 call term_wait(buf)
83
84 setlocal modifiable
85 exe "normal Axxx\<Esc>"
86 call assert_fails(buf . 'bwipe', 'E517')
87 undo
88
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020089 exe buf . 'bwipe'
90 unlet g:job
91endfunc
92
Bram Moolenaar5b868a82019-02-25 06:11:53 +010093func Test_terminal_paste_register()
94 let @" = "text to paste"
95
96 let buf = Run_shell_in_terminal({})
97 " Wait for the shell to display a prompt
98 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
99
100 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
101 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200102 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100103
104 exe buf . 'bwipe!'
105 unlet g:job
106endfunc
107
Bram Moolenaar94053a52017-08-01 21:44:33 +0200108func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200109 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200110 call assert_fails(buf . 'bwipe', 'E517')
111 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200112 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200113 call assert_equal("", bufname(buf))
114
115 unlet g:job
116endfunc
117
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200118func Test_terminal_split_quit()
119 let buf = Run_shell_in_terminal({})
120 call term_wait(buf)
121 split
122 quit!
123 call term_wait(buf)
124 sleep 50m
125 call assert_equal('run', job_status(g:job))
126
127 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200128 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200129
130 exe buf . 'bwipe'
131 unlet g:job
132endfunc
133
Bram Moolenaar94053a52017-08-01 21:44:33 +0200134func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200135 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200136 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200137 quit
138 for nr in range(1, winnr('$'))
139 call assert_notequal(winbufnr(nr), buf)
140 endfor
141 call assert_true(bufloaded(buf))
142 call assert_true(buflisted(buf))
143
144 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200145 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200146 exe buf . 'bwipe'
147
148 unlet g:job
149endfunc
150
Bram Moolenaar1e115362019-01-09 23:01:02 +0100151func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200152 exe g:buf . 'bwipe!'
153 let g:buf = 0
154endfunc
155
Bram Moolenaar9d189612017-09-09 18:11:00 +0200156func Get_cat_123_cmd()
157 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100158 if !has('conpty')
159 return 'cmd /c "cls && color 2 && echo 123"'
160 else
161 " When clearing twice, extra sequence is not output.
162 return 'cmd /c "cls && cls && color 2 && echo 123"'
163 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200164 else
165 call writefile(["\<Esc>[32m123"], 'Xtext')
166 return "cat Xtext"
167 endif
168endfunc
169
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200170func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200171 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200172 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
173 let g:job = term_getjob(g:buf)
174
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200175 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
176 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200177 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100178 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200179 call delete('Xtext')
180endfunc
181
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200182func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200183 let l = term_scrape(a:buf, 0)
184 call assert_true(len(l) == 0)
185 let l = term_scrape(a:buf, 999)
186 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200187 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200188 call assert_true(len(l) > 0)
189 call assert_equal('1', l[0].chars)
190 call assert_equal('2', l[1].chars)
191 call assert_equal('3', l[2].chars)
192 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200193 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
194 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100195 if has('win32')
196 " On Windows 'background' always defaults to dark, even though the terminal
197 " may use a light background. Therefore accept both white and black.
198 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200199 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100200 if &background == 'light'
201 call assert_equal('#ffffff', l[0].bg)
202 else
203 call assert_equal('#000000', l[0].bg)
204 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200205 endif
206
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200207 let l = term_getline(a:buf, -1)
208 call assert_equal('', l)
209 let l = term_getline(a:buf, 0)
210 call assert_equal('', l)
211 let l = term_getline(a:buf, 999)
212 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200213 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200214 call assert_equal('123', l)
215endfunc
216
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200217func Test_terminal_scrape_123()
218 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200219 let buf = term_start(cmd)
220
221 let termlist = term_list()
222 call assert_equal(1, len(termlist))
223 call assert_equal(buf, termlist[0])
224
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200225 " Nothing happens with invalid buffer number
226 call term_wait(1234)
227
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200228 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200229 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200230 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200231 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200232 call Check_123(buf)
233
234 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100235 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200236 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200237 call term_wait(buf)
238 call Check_123(buf)
239
240 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200241 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200242endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200243
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200244func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200245 call writefile(["léttまrs"], 'Xtext')
246 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200247 " Run cmd with UTF-8 codepage to make the type command print the expected
248 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100249 let buf = term_start("cmd /K chcp 65001")
250 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200251 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100252 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200253 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100254 let buf = term_start("cat Xtext")
255 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200256 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200257
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100258 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
259 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200260 call assert_true(len(l) >= 7)
261 call assert_equal('l', l[0].chars)
262 call assert_equal('é', l[1].chars)
263 call assert_equal(1, l[1].width)
264 call assert_equal('t', l[2].chars)
265 call assert_equal('t', l[3].chars)
266 call assert_equal('ま', l[4].chars)
267 call assert_equal(2, l[4].width)
268 call assert_equal('r', l[5].chars)
269 call assert_equal('s', l[6].chars)
270
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100271 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200272 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100273 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200274
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100275 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200276 call delete('Xtext')
277endfunc
278
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200279func Test_terminal_scroll()
280 call writefile(range(1, 200), 'Xtext')
281 if has('win32')
282 let cmd = 'cmd /c "type Xtext"'
283 else
284 let cmd = "cat Xtext"
285 endif
286 let buf = term_start(cmd)
287
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100288 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200289 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200290 call term_wait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200291
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100292 " wait until the scrolling stops
293 while 1
294 let scrolled = buf->term_getscrolled()
295 sleep 20m
296 if scrolled == buf->term_getscrolled()
297 break
298 endif
299 endwhile
300
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200301 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200302 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200303 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200304 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200305 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200306 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200307
308 exe buf . 'bwipe'
309 call delete('Xtext')
310endfunc
311
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200312func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200313 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200314 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200315 call writefile(range(150), 'Xtext')
316 if has('win32')
317 call term_sendkeys(buf, "type Xtext\<CR>")
318 else
319 call term_sendkeys(buf, "cat Xtext\<CR>")
320 endif
321 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200322 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200323 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200324 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200325 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200326
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200327 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200328 call term_wait(buf)
329 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200330 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100331 call delete('Xtext')
332endfunc
333
334func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200335 " tail -f only works on Unix
336 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100337
338 call writefile(range(50), 'Xtext')
339 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100340 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100341 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100342 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100343 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
344 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100345 \ 'call feedkeys("\<C-W>N", "xt")',
346 \ ], 'XTest_postponed')
347 let buf = RunVimInTerminal('-S XTest_postponed', {})
348 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100349 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100350
351 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200352 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100353 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100354 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100355
356 " Back to Terminal-Job mode, text will scroll and show the extra line.
357 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100358 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100359
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100360 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100361 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100362 call term_wait(buf, 50)
363 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100364 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100365 call term_wait(buf, 100)
366 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100367 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
368
369 " check type of term_sendkeys() return value
370 echo type(tsk_ret)
371
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100372 call StopVimInTerminal(buf)
373 call delete('XTest_postponed')
374 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200375endfunc
376
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100377" Run diff on two dumps with different size.
378func Test_terminal_dumpdiff_size()
379 call assert_equal(1, winnr('$'))
380 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
381 call assert_equal(2, winnr('$'))
382 call assert_match('Test_incsearch_search_01.dump', getline(10))
383 call assert_match(' +++++$', getline(11))
384 call assert_match('Test_popup_command_01.dump', getline(31))
385 call assert_equal(repeat('+', 75), getline(30))
386 quit
387endfunc
388
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200389func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200390 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200391
Bram Moolenaarb2412082017-08-20 18:09:14 +0200392 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200393 let size = term_getsize('')
394 bwipe!
395 call assert_equal(5, size[0])
396
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200397 call term_start(cmd, {'term_rows': 6})
398 let size = term_getsize('')
399 bwipe!
400 call assert_equal(6, size[0])
401
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200402 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200403 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200404 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200405
406 call term_setsize('', 6, 0)
407 call assert_equal([6, 33], term_getsize(''))
408
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200409 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200410 call assert_equal([6, 35], term_getsize(''))
411
412 call term_setsize('', 7, 30)
413 call assert_equal([7, 30], term_getsize(''))
414
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200415 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200416 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200417
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200418 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
419 let size = term_getsize('')
420 bwipe!
421 call assert_equal([6, 36], size)
422
Bram Moolenaarb2412082017-08-20 18:09:14 +0200423 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200424 let size = term_getsize('')
425 bwipe!
426 call assert_equal(20, size[1])
427
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200428 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200429 let size = term_getsize('')
430 bwipe!
431 call assert_equal(26, size[1])
432
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200433 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200434 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200435 let size = term_getsize('')
436 bwipe!
437 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200438
439 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
440 let size = term_getsize('')
441 bwipe!
442 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200443
444 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200445endfunc
446
447func Test_terminal_curwin()
448 let cmd = Get_cat_123_cmd()
449 call assert_equal(1, winnr('$'))
450
451 split dummy
452 exe 'terminal ++curwin ' . cmd
453 call assert_equal(2, winnr('$'))
454 bwipe!
455
456 split dummy
457 call term_start(cmd, {'curwin': 1})
458 call assert_equal(2, winnr('$'))
459 bwipe!
460
461 split dummy
462 call setline(1, 'change')
463 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
464 call assert_equal(2, winnr('$'))
465 exe 'terminal! ++curwin ' . cmd
466 call assert_equal(2, winnr('$'))
467 bwipe!
468
469 split dummy
470 call setline(1, 'change')
471 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
472 call assert_equal(2, winnr('$'))
473 bwipe!
474
475 split dummy
476 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200477 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200478endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200479
Bram Moolenaarff546792017-11-21 14:47:57 +0100480func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200481 if s:python != ''
482 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200483 " 500 was not enough for Travis
484 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200485 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200486 echo 'This will take five seconds...'
487 let waittime = 2000
488 if has('win32')
489 let cmd = $windir . '\system32\timeout.exe 1'
490 else
491 let cmd = 'sleep 1'
492 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200493 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100494 return [cmd, waittime]
495endfunc
496
497func Test_terminal_finish_open_close()
498 call assert_equal(1, winnr('$'))
499
500 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200501
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100502 " shell terminal closes automatically
503 terminal
504 let buf = bufnr('%')
505 call assert_equal(2, winnr('$'))
506 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200507 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200508 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200509 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100510
511 " shell terminal that does not close automatically
512 terminal ++noclose
513 let buf = bufnr('%')
514 call assert_equal(2, winnr('$'))
515 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200516 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200517 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100518 call assert_equal(2, winnr('$'))
519 quit
520 call assert_equal(1, winnr('$'))
521
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200522 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200523 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200524 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200525 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200526
527 call term_start(cmd, {'term_finish': 'close'})
528 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200529 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200530 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200531 call assert_equal(1, winnr('$'))
532
533 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200534 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200535 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200536 bwipe
537
538 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200539 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200540 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200541 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200542
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200543 exe 'terminal ++hidden ++open ' . cmd
544 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200545 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200546 bwipe
547
548 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
549 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200550 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200551 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200552
553 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
554 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
555 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
556 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
557
558 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200559 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200560 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200561 call assert_equal(4, winheight(0))
562 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200563endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200564
565func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100566 if has('win32')
567 let cmd = 'cmd /c cd'
568 else
569 CheckExecutable pwd
570 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200571 endif
572 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100573 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200574 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200575
576 exe buf . 'bwipe'
577 call delete('Xdir', 'rf')
578endfunc
579
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200580func Test_terminal_cwd_failure()
581 " Case 1: Provided directory is not actually a directory. Attempt to make
582 " the file executable as well.
583 call writefile([], 'Xfile')
584 call setfperm('Xfile', 'rwx------')
585 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
586 call delete('Xfile')
587
588 " Case 2: Directory does not exist.
589 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
590
591 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100592 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200593 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100594 call mkdir('XdirNoAccess', '', '0600')
595 " return early if the directory permissions could not be set properly
596 if getfperm('XdirNoAccess')[2] == 'x'
597 call delete('XdirNoAccess', 'rf')
598 return
599 endif
600 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
601 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200602 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200603endfunc
604
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100605func Test_terminal_servername()
606 if !has('clientserver')
607 return
608 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200609 call s:test_environment("VIM_SERVERNAME", v:servername)
610endfunc
611
612func Test_terminal_version()
613 call s:test_environment("VIM_TERMINAL", string(v:version))
614endfunc
615
616func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100617 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100618 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200619 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100620 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200621 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100622 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200623 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100624 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100625 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200626 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200627 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100628
Bram Moolenaar012eb662018-03-13 17:55:27 +0100629 exe buf . 'bwipe'
630 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100631endfunc
632
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200633func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100634 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200635 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200636 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100637 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100638 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100639 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100640 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100641 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200642 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200643 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200644 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200645
Bram Moolenaar012eb662018-03-13 17:55:27 +0100646 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200647endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200648
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200649func Test_terminal_list_args()
650 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
651 call assert_fails(buf . 'bwipe', 'E517')
652 exe buf . 'bwipe!'
653 call assert_equal("", bufname(buf))
654endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200655
656func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100657 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100658 let wait_time = 5000
659 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100660 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200661 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100662 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200663 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100664 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100665 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100666 " NPFS is used in Windows, nonblocking mode does not work properly.
667 elseif has('win32')
668 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200669 else
670 let len = 5000
671 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200672
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100673 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100674 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100675 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200676 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100677 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200678
679 " On MS-Windows there is an extra empty line below "done". Find "done" in
680 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100681 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100682 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100683 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200684 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100685 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200686 endif
687 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200688
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100689 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200690 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100691 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200692 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200693 bwipe
694endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200695
696func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100697 " TODO: enable once writing to stdin works on MS-Windows
698 CheckNotMSWindows
699 CheckExecutable wc
700
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200701 call setline(1, ['one', 'two', 'three'])
702 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200703 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200704 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200705 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100706 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200707
708 call setline(1, ['one', 'two', 'three', 'four'])
709 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200710 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200711 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200712 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100713 %bwipe!
714endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200715
Bram Moolenaar21109272020-01-30 16:27:20 +0100716func Test_terminal_eof_arg()
717 CheckExecutable python
Bram Moolenaardada6d22017-09-02 17:18:35 +0200718
Bram Moolenaar21109272020-01-30 16:27:20 +0100719 call setline(1, ['print("hello")'])
720 1term ++eof=exit(123) python
721 " MS-Windows echoes the input, Unix doesn't.
722 if has('win32')
723 call WaitFor({-> getline('$') =~ 'exit(123)'})
724 call assert_equal('hello', getline(line('$') - 1))
725 else
726 call WaitFor({-> getline('$') =~ 'hello'})
727 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200728 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100729 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
730 %bwipe!
731endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200732
Bram Moolenaar21109272020-01-30 16:27:20 +0100733func Test_terminal_eof_arg_win32_ctrl_z()
734 CheckMSWindows
735 CheckExecutable python
736
737 call setline(1, ['print("hello")'])
738 1term ++eof=<C-Z> python
739 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
740 call assert_match('\^Z', getline(line('$') - 1))
741 %bwipe!
742endfunc
743
744func Test_terminal_duplicate_eof_arg()
745 CheckExecutable python
746
747 " Check the last specified ++eof arg is used and should not memory leak.
748 new
749 call setline(1, ['print("hello")'])
750 1term ++eof=<C-Z> ++eof=exit(123) python
751 " MS-Windows echoes the input, Unix doesn't.
752 if has('win32')
753 call WaitFor({-> getline('$') =~ 'exit(123)'})
754 call assert_equal('hello', getline(line('$') - 1))
755 else
756 call WaitFor({-> getline('$') =~ 'hello'})
757 call assert_equal('hello', getline('$'))
758 endif
759 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
760 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200761endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200762
763func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200764 let buf = term_start('NONE', {})
765 call assert_notequal(0, buf)
766
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200767 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200768 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100769 if has('gui_running') && !has('win32')
770 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200771 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100772 else
773 " Otherwise using a job works on all systems.
774 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200775 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200776 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200777
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200778 bwipe!
779endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200780
781func Test_terminal_special_chars()
782 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200783 CheckUnix
784
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200785 call mkdir('Xdir with spaces')
786 call writefile(['x'], 'Xdir with spaces/quoted"file')
787 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200788 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100789 " make sure the job has finished
790 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200791
792 call delete('Xdir with spaces', 'rf')
793 bwipe
794endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200795
796func Test_terminal_wrong_options()
797 call assert_fails('call term_start(&shell, {
798 \ "in_io": "file",
799 \ "in_name": "xxx",
800 \ "out_io": "file",
801 \ "out_name": "xxx",
802 \ "err_io": "file",
803 \ "err_name": "xxx"
804 \ })', 'E474:')
805 call assert_fails('call term_start(&shell, {
806 \ "out_buf": bufnr("%")
807 \ })', 'E474:')
808 call assert_fails('call term_start(&shell, {
809 \ "err_buf": bufnr("%")
810 \ })', 'E474:')
811endfunc
812
813func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200814 let cmd = Get_cat_123_cmd()
815 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
816 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100817 " ConPTY may precede escape sequence. There are things that are not so.
818 if !has('conpty')
819 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
820 call assert_match('123', readfile('Xfile')[0])
821 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200822 let g:job = term_getjob(buf)
823 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
824 call delete('Xfile')
825 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200826
827 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200828 call writefile(['one line'], 'Xfile')
829 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
830 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200831 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200832 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200833 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200834 bwipe
835 call delete('Xfile')
836 endif
837endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200838
839func TerminalTmap(remap)
840 let buf = Run_shell_in_terminal({})
841 call assert_equal('t', mode())
842
843 if a:remap
844 tmap 123 456
845 else
846 tnoremap 123 456
847 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100848 " don't use abcde, it's an existing command
849 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200850 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100851 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200852 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200853 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200854 let lnum = term_getcursor(buf)[0]
855 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100856 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200857 else
858 call assert_match('456', term_getline(buf, lnum))
859 endif
860
861 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200862 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200863 call term_wait(buf)
864
865 tunmap 123
866 tunmap 456
867 call assert_equal('', maparg('123', 't'))
868 close
869 unlet g:job
870endfunc
871
872func Test_terminal_tmap()
873 call TerminalTmap(1)
874 call TerminalTmap(0)
875endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200876
877func Test_terminal_wall()
878 let buf = Run_shell_in_terminal({})
879 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200880 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200881 call term_wait(buf)
882 exe buf . 'bwipe'
883 unlet g:job
884endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200885
Bram Moolenaar7a760922018-02-19 23:10:02 +0100886func Test_terminal_wqall()
887 let buf = Run_shell_in_terminal({})
888 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200889 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100890 call term_wait(buf)
891 exe buf . 'bwipe'
892 unlet g:job
893endfunc
894
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200895func Test_terminal_composing_unicode()
Bram Moolenaar9134f1e2019-11-29 20:26:13 +0100896 CheckNotBSD
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200897 let save_enc = &encoding
898 set encoding=utf-8
899
900 if has('win32')
901 let cmd = "cmd /K chcp 65001"
902 let lnum = [3, 6, 9]
903 else
904 let cmd = &shell
905 let lnum = [1, 3, 5]
906 endif
907
908 enew
909 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100910 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200911 call term_wait(buf, 50)
912
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200913 if has('win32')
914 call assert_equal('cmd', job_info(g:job).cmd[0])
915 else
916 call assert_equal(&shell, job_info(g:job).cmd[0])
917 endif
918
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200919 " ascii + composing
920 let txt = "a\u0308bc"
921 call term_sendkeys(buf, "echo " . txt . "\r")
922 call term_wait(buf, 50)
923 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
924 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
925 let l = term_scrape(buf, lnum[0] + 1)
926 call assert_equal("a\u0308", l[0].chars)
927 call assert_equal("b", l[1].chars)
928 call assert_equal("c", l[2].chars)
929
930 " multibyte + composing
931 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
932 call term_sendkeys(buf, "echo " . txt . "\r")
933 call term_wait(buf, 50)
934 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
935 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
936 let l = term_scrape(buf, lnum[1] + 1)
937 call assert_equal("\u304b\u3099", l[0].chars)
938 call assert_equal("\u304e", l[1].chars)
939 call assert_equal("\u304f\u3099", l[2].chars)
940 call assert_equal("\u3052", l[3].chars)
941 call assert_equal("\u3053\u3099", l[4].chars)
942
943 " \u00a0 + composing
944 let txt = "abc\u00a0\u0308"
945 call term_sendkeys(buf, "echo " . txt . "\r")
946 call term_wait(buf, 50)
947 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
948 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
949 let l = term_scrape(buf, lnum[2] + 1)
950 call assert_equal("\u00a0\u0308", l[3].chars)
951
952 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200953 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200954 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100955 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200956 let &encoding = save_enc
957endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100958
959func Test_terminal_aucmd_on_close()
960 fun Nop()
961 let s:called = 1
962 endfun
963
964 aug repro
965 au!
966 au BufWinLeave * call Nop()
967 aug END
968
969 let [cmd, waittime] = s:get_sleep_cmd()
970
971 call assert_equal(1, winnr('$'))
972 new
973 call setline(1, ['one', 'two'])
974 exe 'term ++close ' . cmd
975 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200976 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100977 call assert_equal(1, s:called)
978 bwipe!
979
980 unlet s:called
981 au! repro
982 delfunc Nop
983endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100984
985func Test_terminal_term_start_empty_command()
986 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
987 call assert_fails(cmd, 'E474')
988 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
989 call assert_fails(cmd, 'E474')
990 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
991 call assert_fails(cmd, 'E474')
992 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
993 call assert_fails(cmd, 'E474')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200994 let cmd = "call term_start('', {'term_name' : []})"
995 call assert_fails(cmd, 'E475')
996 let cmd = "call term_start('', {'term_finish' : 'axby'})"
997 call assert_fails(cmd, 'E475')
998 let cmd = "call term_start('', {'eof_chars' : []})"
999 call assert_fails(cmd, 'E475:')
1000 let cmd = "call term_start('', {'term_kill' : []})"
1001 call assert_fails(cmd, 'E475:')
1002 let cmd = "call term_start('', {'tty_type' : []})"
1003 call assert_fails(cmd, 'E475:')
1004 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1005 call assert_fails(cmd, 'E475:')
1006 let cmd = "call term_start('', {'term_highlight' : []})"
1007 call assert_fails(cmd, 'E475:')
1008 if has('gui')
1009 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1010 call assert_fails(cmd, 'E475:')
1011 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1012 call assert_fails(cmd, 'E730:')
1013 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
1014 call assert_fails(cmd, 'E475:')
1015 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001016endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001017
1018func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001019 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001020
1021 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001022 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001023
Bram Moolenaar086eb872018-03-25 21:24:12 +02001024 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001025 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001026
Bram Moolenaar086eb872018-03-25 21:24:12 +02001027 " Request the cursor position.
1028 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001029
1030 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001031 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001032
Bram Moolenaar086eb872018-03-25 21:24:12 +02001033 " End "cat" gently.
1034 call term_sendkeys(buf, "\<CR>\<C-D>")
1035
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001036 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001037 exe buf . 'bwipe'
1038 unlet g:job
1039endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001040
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001041" Run Vim, start a terminal in that Vim with the kill argument,
1042" :qall works.
1043func Run_terminal_qall_kill(line1, line2)
1044 " 1. Open a terminal window and wait for the prompt to appear
1045 " 2. set kill using term_setkill()
1046 " 3. make Vim exit, it will kill the shell
1047 let after = [
1048 \ a:line1,
1049 \ 'let buf = bufnr("%")',
1050 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1051 \ ' sleep 10m',
1052 \ 'endwhile',
1053 \ a:line2,
1054 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1055 \ 'qall',
1056 \ ]
1057 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001058 return
1059 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001060 call assert_equal("done", readfile("Xdone")[0])
1061 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001062endfunc
1063
1064" Run Vim in a terminal, then start a terminal in that Vim with a kill
1065" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001066func Test_terminal_qall_kill_arg()
1067 call Run_terminal_qall_kill('term ++kill=kill', '')
1068endfunc
1069
1070" Run Vim, start a terminal in that Vim, set the kill argument with
1071" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001072func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001073 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001074endfunc
1075
1076" Run Vim, start a terminal in that Vim without the kill argument,
1077" check that :qall does not exit, :qall! does.
1078func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001079 let after =<< trim [CODE]
1080 term
1081 let buf = bufnr("%")
1082 while term_getline(buf, 1) =~ "^\\s*$"
1083 sleep 10m
1084 endwhile
1085 set nomore
1086 au VimLeavePre * call writefile(["too early"], "Xdone")
1087 qall
1088 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1089 cquit
1090 [CODE]
1091
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001092 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001093 return
1094 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001095 call assert_equal("done", readfile("Xdone")[0])
1096 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001097endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001098
1099" Run Vim in a terminal, then start a terminal in that Vim without a kill
1100" argument, check that :confirm qall works.
1101func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001102 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001103 let buf = RunVimInTerminal('', {})
1104
1105 " Open a terminal window and wait for the prompt to appear
1106 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001107 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1108 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001109
1110 " make Vim exit, it will prompt to kill the shell
1111 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001112 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001113 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001114 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001115
1116 " close the terminal window where Vim was running
1117 quit
1118endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001119
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001120" Run Vim in a terminal, then start a terminal window with a shell and check
1121" that Vim exits if it is closed.
1122func Test_terminal_exit()
1123 CheckRunVimInTerminal
1124
1125 let lines =<< trim END
1126 let winid = win_getid()
1127 help
1128 term
1129 let termid = win_getid()
1130 call win_gotoid(winid)
1131 close
1132 call win_gotoid(termid)
1133 END
1134 call writefile(lines, 'XtermExit')
1135 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1136 let job = term_getjob(buf)
1137 call WaitForAssert({-> assert_equal("run", job_status(job))})
1138
1139 " quit the shell, it will make Vim exit
1140 call term_sendkeys(buf, "exit\<CR>")
1141 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1142
1143 call delete('XtermExit')
1144endfunc
1145
Bram Moolenaar012eb662018-03-13 17:55:27 +01001146func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001147 augroup repro
1148 au!
1149 au TerminalOpen * let s:called += 1
1150 augroup END
1151
1152 let s:called = 0
1153
1154 " Open a terminal window with :terminal
1155 terminal
1156 call assert_equal(1, s:called)
1157 bwipe!
1158
1159 " Open a terminal window with term_start()
1160 call term_start(&shell)
1161 call assert_equal(2, s:called)
1162 bwipe!
1163
1164 " Open a hidden terminal buffer with :terminal
1165 terminal ++hidden
1166 call assert_equal(3, s:called)
1167 for buf in term_list()
1168 exe buf . "bwipe!"
1169 endfor
1170
1171 " Open a hidden terminal buffer with term_start()
1172 let buf = term_start(&shell, {'hidden': 1})
1173 call assert_equal(4, s:called)
1174 exe buf . "bwipe!"
1175
1176 unlet s:called
1177 au! repro
1178endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001179
1180func Check_dump01(off)
1181 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1182 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001183 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001184endfunc
1185
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001186func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001187 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001188 let save_enc = &encoding
1189 set encoding=utf-8
1190 call assert_equal(1, winnr('$'))
1191
1192 let text = " a\u0300 e\u0302 o\u0308"
1193 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001194 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001195 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001196 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001197 let dumpline = readfile('Xdump')[0]
1198 call assert_match('|à| |ê| |ö', dumpline)
1199
1200 call StopVimInTerminal(buf)
1201 call delete('Xcomposing')
1202 call delete('Xdump')
1203 let &encoding = save_enc
1204endfunc
1205
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001206" just testing basic functionality.
1207func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001208 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001209 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001210 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001211 call assert_equal(2, winnr('$'))
1212 call assert_equal(20, line('$'))
1213 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001214
1215 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001216 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001217 call assert_equal(buf, buf2)
1218 call assert_notequal('one two three four five', trim(getline(1)))
1219
1220 " Load the first dump again in the same window
1221 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1222 call assert_equal(buf, buf2)
1223 call Check_dump01(0)
1224
1225 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1226 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1227 new
1228 let closedbuf = winbufnr('')
1229 quit
1230 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1231
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001232 quit
1233endfunc
1234
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001235func Test_terminal_dumpload_dump()
1236 CheckRunVimInTerminal
1237
1238 let lines =<< trim END
1239 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1240 END
1241 call writefile(lines, 'XtermDumpload')
1242 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1243 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1244
1245 call StopVimInTerminal(buf)
1246 call delete('XtermDumpload')
1247endfunc
1248
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001249func Test_terminal_dumpdiff()
1250 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001251 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001252 call assert_equal(2, winnr('$'))
1253 call assert_equal(62, line('$'))
1254 call Check_dump01(0)
1255 call Check_dump01(42)
1256 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1257 quit
1258endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001259
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001260func Test_terminal_dumpdiff_swap()
1261 call assert_equal(1, winnr('$'))
1262 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1263 call assert_equal(2, winnr('$'))
1264 call assert_equal(62, line('$'))
1265 call assert_match('Test_popup_command_01.dump', getline(21))
1266 call assert_match('Test_popup_command_03.dump', getline(42))
1267 call assert_match('Undo', getline(3))
1268 call assert_match('three four five', getline(45))
1269
1270 normal s
1271 call assert_match('Test_popup_command_03.dump', getline(21))
1272 call assert_match('Test_popup_command_01.dump', getline(42))
1273 call assert_match('three four five', getline(3))
1274 call assert_match('Undo', getline(45))
1275 quit
1276endfunc
1277
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001278func Test_terminal_dumpdiff_options()
1279 set laststatus=0
1280 call assert_equal(1, winnr('$'))
1281 let height = winheight(0)
1282 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1283 call assert_equal(2, winnr('$'))
1284 call assert_equal(height, winheight(winnr()))
1285 call assert_equal(33, winwidth(winnr()))
1286 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1287 quit
1288
1289 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001290 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1291 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001292 call assert_equal(&columns, winwidth(0))
1293 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001294 call assert_equal('something else', bufname('%'))
1295 quit
1296
1297 call assert_equal(1, winnr('$'))
1298 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1299 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001300 call assert_fails("call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'bufnr': -1})", 'E475:')
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001301 bwipe
1302
1303 set laststatus&
1304endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001305
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001306func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001307 call assert_equal(1, winnr('$'))
1308
1309 " Use the title termcap entries to output the escape sequence.
1310 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001311 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001312 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001313 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001314 \ 'redraw',
1315 \ "set t_ts=",
1316 \ ], 'Xscript')
1317 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001318 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001319 call assert_equal('Xtextfile', expand('%:t'))
1320 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001321 return buf
1322endfunc
1323
1324func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001325 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001326 let buf = Api_drop_common('')
1327 call assert_equal(0, &bin)
1328 call assert_equal('', &fenc)
1329
1330 call StopVimInTerminal(buf)
1331 call delete('Xscript')
1332 bwipe Xtextfile
1333endfunc
1334
1335func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001336 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001337 let buf = Api_drop_common(',{"bin":1}')
1338 call assert_equal(1, &bin)
1339
1340 call StopVimInTerminal(buf)
1341 call delete('Xscript')
1342 bwipe Xtextfile
1343endfunc
1344
1345func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001346 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001347 let buf = Api_drop_common(',{"binary":1}')
1348 call assert_equal(1, &bin)
1349
1350 call StopVimInTerminal(buf)
1351 call delete('Xscript')
1352 bwipe Xtextfile
1353endfunc
1354
1355func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001356 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001357 set binary
1358 let buf = Api_drop_common(',{"nobin":1}')
1359 call assert_equal(0, &bin)
1360
1361 call StopVimInTerminal(buf)
1362 call delete('Xscript')
1363 bwipe Xtextfile
1364 set nobinary
1365endfunc
1366
1367func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001368 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001369 set binary
1370 let buf = Api_drop_common(',{"nobinary":1}')
1371 call assert_equal(0, &bin)
1372
1373 call StopVimInTerminal(buf)
1374 call delete('Xscript')
1375 bwipe Xtextfile
1376 set nobinary
1377endfunc
1378
1379func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001380 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001381 let buf = Api_drop_common(',{"ff":"dos"}')
1382 call assert_equal("dos", &ff)
1383
1384 call StopVimInTerminal(buf)
1385 call delete('Xscript')
1386 bwipe Xtextfile
1387endfunc
1388
1389func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001390 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001391 let buf = Api_drop_common(',{"fileformat":"dos"}')
1392 call assert_equal("dos", &ff)
1393
1394 call StopVimInTerminal(buf)
1395 call delete('Xscript')
1396 bwipe Xtextfile
1397endfunc
1398
1399func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001400 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001401 let buf = Api_drop_common(',{"enc":"utf-16"}')
1402 call assert_equal("utf-16", &fenc)
1403
1404 call StopVimInTerminal(buf)
1405 call delete('Xscript')
1406 bwipe Xtextfile
1407endfunc
1408
1409func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001410 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001411 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1412 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001413
1414 call StopVimInTerminal(buf)
1415 call delete('Xscript')
1416 bwipe Xtextfile
1417endfunc
1418
1419func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001420 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001421 let firstwinid = win_getid()
1422 split Xtextfile
1423 let textfile_winid = win_getid()
1424 call assert_equal(2, winnr('$'))
1425 call win_gotoid(firstwinid)
1426
1427 " Use the title termcap entries to output the escape sequence.
1428 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001429 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001430 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1431 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1432 \ 'redraw',
1433 \ "set t_ts=",
1434 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001435 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001436 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001437 call assert_equal(textfile_winid, win_getid())
1438
1439 call StopVimInTerminal(buf)
1440 call delete('Xscript')
1441 bwipe Xtextfile
1442endfunc
1443
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001444func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001445 let g:called_bufnum = a:bufnum
1446 let g:called_arg = a:arg
1447endfunc
1448
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001449func WriteApiCall(funcname)
1450 " Use the title termcap entries to output the escape sequence.
1451 call writefile([
1452 \ 'set title',
1453 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1454 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1455 \ 'redraw',
1456 \ "set t_ts=",
1457 \ ], 'Xscript')
1458endfunc
1459
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001460func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001461 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001462
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001463 unlet! g:called_bufnum
1464 unlet! g:called_arg
1465
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001466 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001467
1468 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001469 let buf = RunVimInTerminal('-S Xscript', {})
1470 call WaitFor({-> exists('g:called_bufnum')})
1471 call assert_equal(buf, g:called_bufnum)
1472 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001473 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001474
1475 unlet! g:called_bufnum
1476 unlet! g:called_arg
1477
1478 " Enable explicitly
1479 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1480 call WaitFor({-> exists('g:called_bufnum')})
1481 call assert_equal(buf, g:called_bufnum)
1482 call assert_equal(['hello', 123], g:called_arg)
1483 call StopVimInTerminal(buf)
1484
1485 unlet! g:called_bufnum
1486 unlet! g:called_arg
1487
1488 func! ApiCall_TryThis(bufnum, arg)
1489 let g:called_bufnum2 = a:bufnum
1490 let g:called_arg2 = a:arg
1491 endfunc
1492
1493 call WriteApiCall('ApiCall_TryThis')
1494
1495 " Use prefix match
1496 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1497 call WaitFor({-> exists('g:called_bufnum2')})
1498 call assert_equal(buf, g:called_bufnum2)
1499 call assert_equal(['hello', 123], g:called_arg2)
1500 call StopVimInTerminal(buf)
1501
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001502 call assert_fails("call term_start('ls', {'term_api' : []})", 'E475:')
1503
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001504 unlet! g:called_bufnum2
1505 unlet! g:called_arg2
1506
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001507 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001508 delfunction! ApiCall_TryThis
1509 unlet! g:called_bufnum2
1510 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001511endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001512
1513func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001514 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001515
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001516 func! TryThis(bufnum, arg)
1517 let g:called_bufnum3 = a:bufnum
1518 let g:called_arg3 = a:arg
1519 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001520
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001521 call WriteApiCall('TryThis')
1522
1523 unlet! g:called_bufnum3
1524 unlet! g:called_arg3
1525
1526 " Not permitted
1527 call ch_logfile('Xlog', 'w')
1528 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1529 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1530 call assert_false(exists('g:called_bufnum3'))
1531 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001532 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001533
1534 " No match
1535 call ch_logfile('Xlog', 'w')
1536 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1537 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1538 call assert_false(exists('g:called_bufnum3'))
1539 call assert_false(exists('g:called_arg3'))
1540 call StopVimInTerminal(buf)
1541
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001542 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001543 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001544 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001545 delfunction! TryThis
1546 unlet! g:called_bufnum3
1547 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001548endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001549
Bram Moolenaara997b452018-04-17 23:24:06 +02001550let s:caught_e937 = 0
1551
1552func Tapi_Delete(bufnum, arg)
1553 try
1554 execute 'bdelete!' a:bufnum
1555 catch /E937:/
1556 let s:caught_e937 = 1
1557 endtry
1558endfunc
1559
1560func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001561 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001562
1563 call WriteApiCall('Tapi_Delete')
1564 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001565 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001566
1567 call StopVimInTerminal(buf)
1568 call delete('Xscript')
1569 call ch_logfile('', '')
1570endfunc
1571
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001572func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001573 if !exists('*term_getansicolors')
1574 throw 'Skipped: term_getansicolors() not supported'
1575 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001576 let colors = [
1577 \ '#000000', '#e00000',
1578 \ '#00e000', '#e0e000',
1579 \ '#0000e0', '#e000e0',
1580 \ '#00e0e0', '#e0e0e0',
1581 \ '#808080', '#ff4040',
1582 \ '#40ff40', '#ffff40',
1583 \ '#4040ff', '#ff40ff',
1584 \ '#40ffff', '#ffffff',
1585 \]
1586
1587 let buf = Run_shell_in_terminal({})
1588 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001589 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001590 call term_wait(buf)
1591
1592 exe buf . 'bwipe'
1593endfunc
1594
1595let s:test_colors = [
1596 \ '#616e64', '#0d0a79',
1597 \ '#6d610d', '#0a7373',
1598 \ '#690d0a', '#6d696e',
1599 \ '#0d0a6f', '#616e0d',
1600 \ '#0a6479', '#6d0d0a',
1601 \ '#617373', '#0d0a69',
1602 \ '#6d690d', '#0a6e6f',
1603 \ '#610d0a', '#6e6479',
1604 \]
1605
1606func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001607 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001608 if !exists('*term_getansicolors')
1609 throw 'Skipped: term_getansicolors() not supported'
1610 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001611 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1612 let buf = Run_shell_in_terminal({})
1613 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001614 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001615 call term_wait(buf)
1616
1617 exe buf . 'bwipe'
1618 unlet g:terminal_ansi_colors
1619endfunc
1620
1621func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001622 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001623 if !exists('*term_getansicolors')
1624 throw 'Skipped: term_getansicolors() not supported'
1625 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001626 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1627 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1628 call assert_equal(s:test_colors, term_getansicolors(buf))
1629
1630 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001631 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001632
1633 let colors = [
1634 \ 'ivory', 'AliceBlue',
1635 \ 'grey67', 'dark goldenrod',
1636 \ 'SteelBlue3', 'PaleVioletRed4',
1637 \ 'MediumPurple2', 'yellow2',
1638 \ 'RosyBrown3', 'OrangeRed2',
1639 \ 'white smoke', 'navy blue',
1640 \ 'grey47', 'gray97',
1641 \ 'MistyRose2', 'DodgerBlue4',
1642 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001643 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001644
1645 let colors[4] = 'Invalid'
1646 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1647
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001648 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001649 call term_wait(buf)
1650 exe buf . 'bwipe'
1651endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001652
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001653func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001654 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001655
1656 " Use all the ANSI colors.
1657 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001658 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001659 \ 'hi Tblack ctermfg=0 ctermbg=8',
1660 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1661 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1662 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1663 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1664 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1665 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1666 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1667 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1668 \ 'hi Tred ctermfg=9 ctermbg=1',
1669 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1670 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1671 \ 'hi Tblue ctermfg=12 ctermbg=4',
1672 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1673 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1674 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001675 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1676 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1677 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001678 \ '',
1679 \ 'call matchadd("Tblack", "A")',
1680 \ 'call matchadd("Tdarkred", "B")',
1681 \ 'call matchadd("Tdarkgreen", "C")',
1682 \ 'call matchadd("Tbrown", "D")',
1683 \ 'call matchadd("Tdarkblue", "E")',
1684 \ 'call matchadd("Tdarkmagenta", "F")',
1685 \ 'call matchadd("Tdarkcyan", "G")',
1686 \ 'call matchadd("Tlightgrey", "H")',
1687 \ 'call matchadd("Tdarkgrey", "I")',
1688 \ 'call matchadd("Tred", "J")',
1689 \ 'call matchadd("Tgreen", "K")',
1690 \ 'call matchadd("Tyellow", "L")',
1691 \ 'call matchadd("Tblue", "M")',
1692 \ 'call matchadd("Tmagenta", "N")',
1693 \ 'call matchadd("Tcyan", "O")',
1694 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001695 \ 'call matchadd("TdarkredBold", "X")',
1696 \ 'call matchadd("TgreenBold", "Y")',
1697 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001698 \ 'redraw',
1699 \ ], 'Xcolorscript')
1700 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1701 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1702
1703 call term_sendkeys(buf, ":q\<CR>")
1704 call StopVimInTerminal(buf)
1705 call delete('Xcolorscript')
1706endfunc
1707
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001708func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001709 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001710 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001711 let text = []
1712 for n in range(10)
1713 call add(text, repeat(n, 50))
1714 endfor
1715 call writefile(text, 'Xwinsize')
1716 let buf = RunVimInTerminal('Xwinsize', {})
1717 let win = bufwinid(buf)
1718 call assert_equal([6, 40], term_getsize(buf))
1719 call assert_equal(6, winheight(win))
1720 call assert_equal(40, winwidth(win))
1721
1722 " resizing the window doesn't resize the terminal.
1723 resize 10
1724 vertical resize 60
1725 call assert_equal([6, 40], term_getsize(buf))
1726 call assert_equal(10, winheight(win))
1727 call assert_equal(60, winwidth(win))
1728
1729 call StopVimInTerminal(buf)
1730 call delete('Xwinsize')
1731
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001732 call assert_fails('set termwinsize=40', 'E474')
1733 call assert_fails('set termwinsize=10+40', 'E474')
1734 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001735
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001736 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001737endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001738
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001739func Test_terminal_termwinsize_option_zero()
1740 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001741 let buf = Run_shell_in_terminal({})
1742 let win = bufwinid(buf)
1743 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001744 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001745 call term_wait(buf)
1746 exe buf . 'bwipe'
1747
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001748 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001749 let buf = Run_shell_in_terminal({})
1750 let win = bufwinid(buf)
1751 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001752 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001753 call term_wait(buf)
1754 exe buf . 'bwipe'
1755
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001756 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001757 let buf = Run_shell_in_terminal({})
1758 let win = bufwinid(buf)
1759 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001760 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001761 call term_wait(buf)
1762 exe buf . 'bwipe'
1763
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001764 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001765endfunc
1766
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001767func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001768 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001769 vsplit
1770 let buf = Run_shell_in_terminal({})
1771 let win = bufwinid(buf)
1772 call assert_inrange(10, 1000, winheight(win))
1773 call assert_inrange(50, 1000, winwidth(win))
1774 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1775
1776 resize 15
1777 vertical resize 60
1778 redraw
1779 call assert_equal([15, 60], term_getsize(buf))
1780 call assert_equal(15, winheight(win))
1781 call assert_equal(60, winwidth(win))
1782
1783 resize 7
1784 vertical resize 30
1785 redraw
1786 call assert_equal([10, 50], term_getsize(buf))
1787 call assert_equal(7, winheight(win))
1788 call assert_equal(30, winwidth(win))
1789
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001790 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001791 call term_wait(buf)
1792 exe buf . 'bwipe'
1793
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001794 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001795 let buf = Run_shell_in_terminal({})
1796 let win = bufwinid(buf)
1797 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001798 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001799 call term_wait(buf)
1800 exe buf . 'bwipe'
1801
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001802 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001803endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001804
1805func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001806 " make three tabpages, terminal in the middle
1807 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001808 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001809 tabnew
1810 tabprev
1811 call assert_equal(1, winnr('$'))
1812 call assert_equal(2, tabpagenr())
1813 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001814
1815 let buf = Run_shell_in_terminal({})
1816 let termwin = bufwinid(buf)
1817 set termwinkey=<C-L>
1818 call feedkeys("\<C-L>w", 'tx')
1819 call assert_equal(thiswin, win_getid())
1820 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001821 call assert_equal(termwin, win_getid())
1822
Bram Moolenaar997d4242019-09-14 21:23:40 +02001823 if has('langmap')
1824 set langmap=xjyk
1825 call feedkeys("\<C-L>x", 'tx')
1826 call assert_equal(thiswin, win_getid())
1827 call feedkeys("\<C-W>y", 'tx')
1828 call assert_equal(termwin, win_getid())
1829 set langmap=
1830 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001831
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001832 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001833 call assert_equal(3, tabpagenr())
1834 tabprev
1835 call assert_equal(2, tabpagenr())
1836 call assert_equal(termwin, win_getid())
1837
1838 call feedkeys("\<C-L>gT", "xt")
1839 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001840 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001841 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001842 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001843
1844 let job = term_getjob(buf)
1845 call feedkeys("\<C-L>\<C-C>", 'tx')
1846 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001847
1848 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001849 tabnext
1850 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001851 tabprev
1852 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001853endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001854
1855func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001856 CheckUnix
1857
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001858 call writefile([
1859 \ '#!/bin/sh',
1860 \ 'echo "this is standard error" >&2',
1861 \ 'echo "this is standard out" >&1',
1862 \ ], 'Xechoerrout.sh')
1863 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1864
1865 let outfile = 'Xtermstdout'
1866 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001867
1868 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1869 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001870 call assert_equal('this is standard error', term_getline(buf, 1))
1871
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001872 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001873 exe buf . 'bwipe'
1874 call delete('Xechoerrout.sh')
1875 call delete(outfile)
1876endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001877
Bram Moolenaare219f732019-11-30 15:34:08 +01001878func Test_termwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001879 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001880
1881 " Let the terminal output more than 'termwinscroll' lines, some at the start
1882 " will be dropped.
1883 exe 'set termwinscroll=' . &lines
1884 let buf = term_start('/bin/sh')
1885 for i in range(1, &lines)
1886 call feedkeys("echo " . i . "\<CR>", 'xt')
1887 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1888 endfor
1889 " Go to Terminal-Normal mode to update the buffer.
1890 call feedkeys("\<C-W>N", 'xt')
1891 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1892
1893 " Every "echo nr" must only appear once
1894 let lines = getline(1, line('$'))
1895 for i in range(&lines - len(lines) / 2 + 2, &lines)
1896 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1897 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1898 endfor
1899
1900 exe buf . 'bwipe!'
1901endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001902
Bram Moolenaar875cf872018-07-08 20:49:07 +02001903" Resizing the terminal window caused an ml_get error.
1904" TODO: This does not reproduce the original problem.
1905func Test_terminal_resize()
1906 set statusline=x
1907 terminal
1908 call assert_equal(2, winnr('$'))
1909
1910 " Fill the terminal with text.
1911 if has('win32')
1912 call feedkeys("dir\<CR>", 'xt')
1913 else
1914 call feedkeys("ls\<CR>", 'xt')
1915 endif
1916 " Go to Terminal-Normal mode for a moment.
1917 call feedkeys("\<C-W>N", 'xt')
1918 " Open a new window
1919 call feedkeys("i\<C-W>n", 'xt')
1920 call assert_equal(3, winnr('$'))
1921 redraw
1922
1923 close
1924 call assert_equal(2, winnr('$'))
1925 call feedkeys("exit\<CR>", 'xt')
1926 set statusline&
1927endfunc
1928
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001929" must be nearly the last, we can't go back from GUI to terminal
1930func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001931 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001932
1933 " Ignore the "failed to create input context" error.
1934 call test_ignore_error('E285:')
1935
1936 gui -f
1937
1938 call assert_equal(1, winnr('$'))
1939 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001940 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001941 call term_wait(buf)
1942
1943 " closing window wipes out the terminal buffer a with finished job
1944 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1945 call assert_equal("", bufname(buf))
1946
1947 unlet g:job
1948endfunc
1949
1950func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001951 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001952 set guioptions+=!
1953
1954 let filename = 'Xtestscript'
1955 if has('win32')
1956 let filename .= '.bat'
1957 let prefix = ''
1958 let contents = ['@echo off', 'exit %1']
1959 else
1960 let filename .= '.sh'
1961 let prefix = './'
1962 let contents = ['#!/bin/sh', 'exit $1']
1963 endif
1964 call writefile(contents, filename)
1965 call setfperm(filename, 'rwxrwx---')
1966
1967 " Check if v:shell_error is equal to the exit status.
1968 let exitval = 0
1969 execute printf(':!%s%s %d', prefix, filename, exitval)
1970 call assert_equal(exitval, v:shell_error)
1971
1972 let exitval = 9
1973 execute printf(':!%s%s %d', prefix, filename, exitval)
1974 call assert_equal(exitval, v:shell_error)
1975
1976 set guioptions&
1977 call delete(filename)
1978endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001979
1980func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001981 CheckUnix
1982
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001983 term ++hidden cat
1984 let bnr = bufnr('$')
1985 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1986 exe 'sbuf ' . bnr
1987 call assert_equal('terminal', &buftype)
1988 call term_sendkeys(bnr, "asdf\<CR>")
1989 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1990 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001991 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001992 bwipe!
1993endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001994
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001995func Test_terminal_switch_mode()
1996 term
1997 let bnr = bufnr('$')
1998 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1999 call feedkeys("\<C-W>N", 'xt')
2000 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2001 call feedkeys("A", 'xt')
2002 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2003 call feedkeys("\<C-W>N", 'xt')
2004 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2005 call feedkeys("I", 'xt')
2006 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2007 call feedkeys("\<C-W>Nv", 'xt')
2008 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2009 call feedkeys("I", 'xt')
2010 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2011 call feedkeys("\<C-W>Nv", 'xt')
2012 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2013 call feedkeys("A", 'xt')
2014 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2015 bwipe!
2016endfunc
2017
Bram Moolenaara3817732019-10-16 16:31:44 +02002018func Test_terminal_normal_mode()
2019 CheckRunVimInTerminal
2020
2021 " Run Vim in a terminal and open a terminal window to run Vim in.
2022 let lines =<< trim END
2023 call setline(1, range(11111, 11122))
2024 3
2025 END
2026 call writefile(lines, 'XtermNormal')
2027 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
2028 call term_wait(buf)
2029
2030 call term_sendkeys(buf, "\<C-W>N")
2031 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
2032 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
2033
2034 call term_sendkeys(buf, ":set culopt=number\r")
2035 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
2036
2037 call term_sendkeys(buf, ":set culopt=line\r")
2038 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
2039
2040 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
2041 call StopVimInTerminal(buf)
2042 call delete('XtermNormal')
2043endfunc
2044
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002045func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002046 CheckUnix
2047
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002048 call assert_equal(1, winnr('$'))
2049 term ++hidden ++close ls
2050 let bnr = bufnr('$')
2051 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2052 call WaitForAssert({-> assert_false(bufexists(bnr))})
2053 call assert_equal(1, winnr('$'))
2054endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002055
2056func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002057 " This test does not pass through ConPTY.
2058 if has('conpty')
2059 return
2060 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002061 let contents = [
2062 \ [ 'One', '', 'X' ],
2063 \ [ 'Two', '', '' ],
2064 \ [ 'Three' ] + repeat([''], 30)
2065 \ ]
2066
2067 for c in contents
2068 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002069 if has('win32')
2070 term cmd /c type Xfile
2071 else
2072 term cat Xfile
2073 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002074 let bnr = bufnr('$')
2075 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2076 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002077 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002078 call assert_equal(c, getline(1, line('$')))
2079 quit
2080 endfor
2081
2082 call delete('Xfile')
2083endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002084
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002085func Test_terminal_no_job()
Bram Moolenaar077ff432019-10-28 00:42:21 +01002086 if has('win32')
2087 let cmd = 'cmd /c ""'
2088 else
2089 CheckExecutable false
2090 let cmd = 'false'
2091 endif
2092 let term = term_start(cmd, {'term_finish': 'close'})
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002093 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2094endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002095
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002096func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002097 CheckUnix
2098
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002099 let buf = Run_shell_in_terminal({})
2100
2101 " Wait for the shell to display a prompt.
2102 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2103
2104 " Hide the cursor.
2105 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2106 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2107
2108 " Show the cursor.
2109 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002110 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002111
2112 " Change color of cursor.
2113 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2114 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2115 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2116 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2117 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2118
2119 " Make cursor a blinking block.
2120 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2121 call WaitForAssert({-> assert_equal([1, 1],
2122 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2123
2124 " Make cursor a steady block.
2125 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2126 call WaitForAssert({-> assert_equal([0, 1],
2127 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2128
2129 " Make cursor a blinking underline.
2130 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2131 call WaitForAssert({-> assert_equal([1, 2],
2132 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2133
2134 " Make cursor a steady underline.
2135 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2136 call WaitForAssert({-> assert_equal([0, 2],
2137 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2138
2139 " Make cursor a blinking vertical bar.
2140 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2141 call WaitForAssert({-> assert_equal([1, 3],
2142 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2143
2144 " Make cursor a steady vertical bar.
2145 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2146 call WaitForAssert({-> assert_equal([0, 3],
2147 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2148
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002149 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002150endfunc
2151
Bram Moolenaar1e115362019-01-09 23:01:02 +01002152func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002153 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002154 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002155 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002156 call assert_equal('', term_gettitle(bufnr('$') + 1))
2157
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002158 if !has('title') || &title == 0 || empty(&t_ts)
2159 throw "Skipped: can't get/set title"
2160 endif
2161
Bram Moolenaar1e115362019-01-09 23:01:02 +01002162 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002163 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002164 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2165 call term_sendkeys(term, ":e Xfoo\r")
2166 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002167 else
2168 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002169 call term_sendkeys(term, ":e Xfoo\r")
2170 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002171 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002172
Bram Moolenaar1e115362019-01-09 23:01:02 +01002173 call term_sendkeys(term, ":set titlestring=foo\r")
2174 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2175
2176 exe term . 'bwipe!'
2177endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002178
Bram Moolenaar01603a92020-04-03 12:56:17 +02002179func Test_term_gettty()
2180 let buf = Run_shell_in_terminal({})
2181 let gettty = term_gettty(buf)
2182
2183 if has('unix') && executable('tty')
2184 " Find tty using the tty shell command.
2185 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2186 call term_sendkeys(buf, "tty\r")
2187 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
2188 let tty = term_getline(buf, 2)
2189 call assert_equal(tty, gettty)
2190 endif
2191
2192 let gettty0 = term_gettty(buf, 0)
2193 let gettty1 = term_gettty(buf, 1)
2194
2195 call assert_equal(gettty, gettty0)
2196 call assert_equal(job_info(g:job).tty_out, gettty0)
2197 call assert_equal(job_info(g:job).tty_in, gettty1)
2198
2199 if has('unix')
2200 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
2201 " are identical according to :help term_gettty()
2202 call assert_equal(gettty0, gettty1)
2203 call assert_match('^/dev/', gettty)
2204 else
2205 " ConPTY works on anonymous pipe.
2206 if !has('conpty')
2207 call assert_match('^\\\\.\\pipe\\', gettty0)
2208 call assert_match('^\\\\.\\pipe\\', gettty1)
2209 endif
2210 endif
2211
2212 call assert_fails('call term_gettty(buf, 2)', 'E475:')
2213 call assert_fails('call term_gettty(buf, -1)', 'E475:')
2214
2215 call assert_equal('', term_gettty(buf + 1))
2216
2217 call StopShellInTerminal(buf)
2218 call term_wait(buf)
2219 exe buf . 'bwipe'
2220endfunc
2221
Bram Moolenaar10772302019-01-20 18:25:54 +01002222" When drawing the statusline the cursor position may not have been updated
2223" yet.
2224" 1. create a terminal, make it show 2 lines
2225" 2. 0.5 sec later: leave terminal window, execute "i"
2226" 3. 0.5 sec later: clear terminal window, now it's 1 line
2227" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2228" 4. 0.5 sec later: should be done, clean up
2229func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002230 CheckUnix
2231
Bram Moolenaar10772302019-01-20 18:25:54 +01002232 set statusline=x
2233 terminal
2234 let tbuf = bufnr('')
2235 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2236 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2237 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2238 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2239
2240 sleep 2
2241 exe tbuf . 'bwipe!'
2242 au! BufLeave
2243 set statusline=
2244endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002245
2246func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002247 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002248
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002249 " split, go to the bottom-right window
2250 split
2251 wincmd j
2252 set splitright
2253
2254 call writefile([
2255 \ 'echo getwinpos()',
2256 \ ], 'XTest_getwinpos')
2257 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2258 call term_wait(buf)
2259
2260 " Find the output of getwinpos() in the bottom line.
2261 let rows = term_getsize(buf)[0]
2262 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2263 let line = term_getline(buf, rows)
2264 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2265 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2266
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002267 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002268 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002269 " In the GUI it can be more, let's assume a 20 x 14 cell.
2270 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002271 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002272 let winpos = 50->getwinpos()
2273 call assert_equal(xroot, winpos[0])
2274 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002275 let [winrow, wincol] = win_screenpos('.')
2276 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2277 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2278 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2279 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002280
2281 call term_wait(buf)
2282 call term_sendkeys(buf, ":q\<CR>")
2283 call StopVimInTerminal(buf)
2284 call delete('XTest_getwinpos')
2285 exe buf . 'bwipe!'
2286 set splitright&
2287 only!
2288endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002289
2290func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002291 " somehow doesn't work on MS-Windows
2292 CheckUnix
2293 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002294
2295 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002296 call writefile(["\<Esc>[?1047h"], 'Xtext')
2297 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002298 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2299
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002300 call writefile(["\<Esc>[?1047l"], 'Xtext')
2301 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002302 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002303
2304 call term_sendkeys(buf, "exit\r")
2305 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002306 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002307endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002308
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002309func Test_terminal_shell_option()
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002310 if has('unix')
2311 " exec is a shell builtin command, should fail without a shell.
2312 term exec ls runtest.vim
2313 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
2314 bwipe!
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002315
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002316 term ++shell exec ls runtest.vim
2317 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2318 bwipe!
2319 elseif has('win32')
2320 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar36ec6f62019-11-05 22:38:47 +01002321 try
2322 term dir /b runtest.vim
2323 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
2324 catch /CreateProcess/
2325 " ignore
2326 endtry
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002327 bwipe!
2328
2329 term ++shell dir /b runtest.vim
2330 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2331 bwipe!
2332 endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002333endfunc
2334
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002335func Test_terminal_setapi_and_call()
Bram Moolenaar21109272020-01-30 16:27:20 +01002336 CheckRunVimInTerminal
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002337
2338 call WriteApiCall('Tapi_TryThis')
2339 call ch_logfile('Xlog', 'w')
2340
2341 unlet! g:called_bufnum
2342 unlet! g:called_arg
2343
Bram Moolenaar21109272020-01-30 16:27:20 +01002344 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002345 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2346 call assert_false(exists('g:called_bufnum'))
2347 call assert_false(exists('g:called_arg'))
2348
Bram Moolenaar21109272020-01-30 16:27:20 +01002349 eval buf->term_setapi('Tapi_')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002350 call term_sendkeys(buf, ":set notitle\<CR>")
2351 call term_sendkeys(buf, ":source Xscript\<CR>")
2352 call WaitFor({-> exists('g:called_bufnum')})
2353 call assert_equal(buf, g:called_bufnum)
2354 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar21109272020-01-30 16:27:20 +01002355
2356 call StopVimInTerminal(buf)
2357
2358 call delete('Xscript')
2359 call ch_logfile('')
2360 call delete('Xlog')
2361 unlet! g:called_bufnum
2362 unlet! g:called_arg
2363endfunc
2364
2365func Test_terminal_api_arg()
2366 CheckRunVimInTerminal
2367
2368 call WriteApiCall('Tapi_TryThis')
2369 call ch_logfile('Xlog', 'w')
2370
2371 unlet! g:called_bufnum
2372 unlet! g:called_arg
2373
2374 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2375 let buf = bufnr('%')
2376 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2377 call assert_false(exists('g:called_bufnum'))
2378 call assert_false(exists('g:called_arg'))
2379
2380 call StopVimInTerminal(buf)
2381
2382 call ch_logfile('Xlog', 'w')
2383
2384 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2385 let buf = bufnr('%')
2386 call WaitFor({-> exists('g:called_bufnum')})
2387 call assert_equal(buf, g:called_bufnum)
2388 call assert_equal(['hello', 123], g:called_arg)
2389
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002390 call StopVimInTerminal(buf)
2391
2392 call delete('Xscript')
2393 call ch_logfile('')
2394 call delete('Xlog')
2395 unlet! g:called_bufnum
2396 unlet! g:called_arg
2397endfunc
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002398
Bram Moolenaar9b9be002020-03-22 14:41:22 +01002399func Test_terminal_invalid_arg()
2400 call assert_fails('terminal ++xyz', 'E181:')
2401endfunc
2402
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002403func Test_terminal_in_popup()
2404 CheckRunVimInTerminal
2405
2406 let text =<< trim END
2407 some text
2408 to edit
2409 in a popup window
2410 END
2411 call writefile(text, 'Xtext')
Bram Moolenaar3180fe62020-02-02 13:47:06 +01002412 let cmd = GetVimCommandCleanTerm()
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002413 let lines = [
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01002414 \ 'set t_u7=',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002415 \ 'call setline(1, range(20))',
2416 \ 'hi PopTerm ctermbg=grey',
2417 \ 'func OpenTerm(setColor)',
Bram Moolenaar353c3512020-03-15 14:19:26 +01002418 \ " set noruler",
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002419 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
Bram Moolenaar57c33952020-02-29 16:09:16 +01002420 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002421 \ ' if a:setColor',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002422 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002423 \ ' endif',
2424 \ 'endfunc',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002425 \ 'func HidePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002426 \ ' call popup_hide(g:winid)',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002427 \ 'endfunc',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002428 \ 'func ClosePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002429 \ ' call popup_close(g:winid)',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002430 \ 'endfunc',
2431 \ 'func ReopenPopup()',
2432 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
2433 \ 'endfunc',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002434 \ ]
2435 call writefile(lines, 'XtermPopup')
2436 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002437 call term_wait(buf, 200)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002438 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002439 call term_wait(buf, 200)
Bram Moolenaarc2adc002020-02-14 17:05:18 +01002440 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002441 call term_wait(buf, 200)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002442 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002443 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
2444
2445 call term_sendkeys(buf, ":q\<CR>")
2446 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
2447
2448 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002449 call term_wait(buf, 300)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002450 call term_sendkeys(buf, ":set hlsearch\<CR>")
2451 call term_sendkeys(buf, "/edit\<CR>")
2452 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
2453
Bram Moolenaar1af5ce02020-02-06 11:54:35 +01002454 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
2455 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
2456 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar67021882020-02-07 22:20:53 +01002457 call term_wait(buf, 100)
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002458
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002459 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
2460 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
2461
2462 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
2463 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002464
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002465 " Go to terminal-Normal mode and visually select text.
2466 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
2467 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
2468
2469 " Back to job mode, redraws
2470 call term_sendkeys(buf, "A")
2471 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
2472
2473 call term_wait(buf, 100)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002474 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002475 call term_wait(buf, 200) " wait for terminal to vanish
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002476
2477 call StopVimInTerminal(buf)
Bram Moolenaar19398262020-03-14 15:28:08 +01002478 call delete('Xtext')
2479 call delete('XtermPopup')
2480endfunc
2481
2482" Check a terminal in popup window uses the default mininum size.
2483func Test_terminal_in_popup_min_size()
2484 CheckRunVimInTerminal
2485
2486 let text =<< trim END
2487 another text
2488 to show
2489 in a popup window
2490 END
2491 call writefile(text, 'Xtext')
2492 let lines = [
2493 \ 'set t_u7=',
2494 \ 'call setline(1, range(20))',
Bram Moolenaar19398262020-03-14 15:28:08 +01002495 \ 'func OpenTerm()',
2496 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
2497 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2498 \ 'endfunc',
2499 \ ]
2500 call writefile(lines, 'XtermPopup')
2501 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar353c3512020-03-15 14:19:26 +01002502 call term_wait(buf, 200)
2503 call term_sendkeys(buf, ":set noruler\<CR>")
Bram Moolenaar19398262020-03-14 15:28:08 +01002504 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
2505 call term_wait(buf, 100)
2506 call term_sendkeys(buf, ":\<CR>")
2507 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
2508
2509 call term_wait(buf, 100)
2510 call term_sendkeys(buf, ":q\<CR>")
2511 call term_wait(buf, 100) " wait for terminal to vanish
2512 call StopVimInTerminal(buf)
2513 call delete('Xtext')
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002514 call delete('XtermPopup')
2515endfunc
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002516
Bram Moolenaar83d47902020-03-26 20:34:00 +01002517" Check a terminal in popup window with different colors
2518func Terminal_in_popup_colored(group_name, highlight_cmd, highlight_opt)
2519 CheckRunVimInTerminal
2520 CheckUnix
2521
2522 let lines = [
2523 \ 'set t_u7=',
2524 \ 'call setline(1, range(20))',
2525 \ 'func OpenTerm()',
2526 \ " let s:buf = term_start('cat', #{hidden: 1, "
2527 \ .. a:highlight_opt .. "})",
2528 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2529 \ 'endfunc',
2530 \ a:highlight_cmd,
2531 \ ]
2532 call writefile(lines, 'XtermPopup')
2533 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
2534 call term_wait(buf, 200)
2535 call term_sendkeys(buf, ":set noruler\<CR>")
2536 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
2537 call term_wait(buf, 100)
2538 call term_sendkeys(buf, "hello\<CR>")
2539 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
2540
2541 call term_sendkeys(buf, "\<C-D>")
2542 call term_wait(buf, 100)
2543 call term_sendkeys(buf, ":q\<CR>")
2544 call term_wait(buf, 100) " wait for terminal to vanish
2545 call StopVimInTerminal(buf)
2546 call delete('XtermPopup')
2547endfunc
2548
2549func Test_terminal_in_popup_colored_Terminal()
2550 call Terminal_in_popup_colored("Terminal", "highlight Terminal ctermfg=blue ctermbg=yellow", "")
2551endfunc
2552
2553func Test_terminal_in_popup_colored_group()
2554 call Terminal_in_popup_colored("MyTermCol", "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue", "term_highlight: 'MyTermCol',")
2555endfunc
2556
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002557func Test_double_popup_terminal()
2558 let buf1 = term_start(&shell, #{hidden: 1})
2559 let win1 = popup_create(buf1, {})
2560 let buf2 = term_start(&shell, #{hidden: 1})
2561 let win2 = popup_create(buf2, {})
2562 call popup_close(win1)
2563 call popup_close(win2)
2564 exe buf1 .. 'bwipe!'
2565 exe buf2 .. 'bwipe!'
2566endfunc
2567
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002568func Test_issue_5607()
2569 let wincount = winnr('$')
2570 exe 'terminal' &shell &shellcmdflag 'exit'
2571 let job = term_getjob(bufnr())
2572 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2573
2574 let old_wincolor = &wincolor
2575 try
2576 set wincolor=
2577 finally
2578 let &wincolor = old_wincolor
2579 bw!
2580 endtry
2581endfunc
Bram Moolenaare010c722020-02-24 21:37:54 +01002582
2583func Test_hidden_terminal()
2584 let buf = term_start(&shell, #{hidden: 1})
2585 call assert_equal('', bufname('^$'))
2586 call StopShellInTerminal(buf)
2587endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002588
2589func Test_term_nasty_callback()
2590 func OpenTerms()
2591 set hidden
2592 let g:buf0 = term_start('sh', #{hidden: 1})
2593 call popup_create(g:buf0, {})
2594 let g:buf1 = term_start('sh', #{hidden: 1, term_finish: 'close'})
2595 call popup_create(g:buf1, {})
2596 let g:buf2 = term_start(['sh', '-c'], #{curwin: 1, exit_cb: function('TermExit')})
Bram Moolenaar52ea92b2020-03-29 17:50:48 +02002597 call term_wait(g:buf2, 100)
Bram Moolenaarcee52202020-03-11 14:19:58 +01002598 call popup_close(win_getid())
2599 endfunc
2600 func TermExit(...)
Bram Moolenaar52ea92b2020-03-29 17:50:48 +02002601 let altbuf = bufnr('#')
2602 call term_sendkeys(altbuf, "exit\<CR>")
2603 call term_wait(altbuf)
Bram Moolenaarcee52202020-03-11 14:19:58 +01002604 call popup_close(win_getid())
Bram Moolenaarab505b12020-03-23 19:28:44 +01002605 endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002606 call OpenTerms()
2607
2608 call term_sendkeys(g:buf0, "exit\<CR>")
Bram Moolenaar52ea92b2020-03-29 17:50:48 +02002609 call term_wait(g:buf0, 100)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01002610 exe g:buf0 .. 'bwipe!'
Bram Moolenaarcee52202020-03-11 14:19:58 +01002611 set hidden&
2612endfunc
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002613
2614" vim: shiftwidth=2 sts=2 expandtab