blob: 212a15f3146f31bd4f16b1f91a81db8461440820 [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 Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020028 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020029 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010042 " ConPTY works on anonymous pipe.
43 if !has('conpty')
44 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020045 call assert_match('^\\\\.\\pipe\\', ''->term_gettty())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010046 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020047 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020048 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020049 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020051 call assert_match('%aR[^\n]*running]', execute('ls R'))
52 call assert_notmatch('%[^\n]*running]', execute('ls F'))
53 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020055 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +020056 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020057 call assert_equal('n', mode())
58 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020059 call assert_match('%aF[^\n]*finished]', execute('ls F'))
60 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020062
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 " closing window wipes out the terminal buffer a with finished job
64 close
65 call assert_equal("", bufname(buf))
66
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020067 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020068 unlet g:job
69endfunc
70
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020071func Test_terminal_TerminalWinOpen()
72 au TerminalWinOpen * let b:done = 'yes'
73 let buf = Run_shell_in_terminal({})
74 call assert_equal('yes', b:done)
75 call StopShellInTerminal(buf)
76 " closing window wipes out the terminal buffer with the finished job
77 close
78
79 if has("unix")
80 terminal ++hidden ++open sleep 1
81 sleep 1
82 call assert_fails("echo b:done", 'E121:')
83 endif
84
85 au! TerminalWinOpen
86endfunc
87
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020088func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020089 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020090 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020091 call term_wait(buf)
92
93 setlocal modifiable
94 exe "normal Axxx\<Esc>"
95 call assert_fails(buf . 'bwipe', 'E517')
96 undo
97
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020098 exe buf . 'bwipe'
99 unlet g:job
100endfunc
101
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100102func Test_terminal_paste_register()
103 let @" = "text to paste"
104
105 let buf = Run_shell_in_terminal({})
106 " Wait for the shell to display a prompt
107 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
108
109 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
110 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200111 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100112
113 exe buf . 'bwipe!'
114 unlet g:job
115endfunc
116
Bram Moolenaar94053a52017-08-01 21:44:33 +0200117func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200118 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200119 call assert_fails(buf . 'bwipe', 'E517')
120 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200121 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200122 call assert_equal("", bufname(buf))
123
124 unlet g:job
125endfunc
126
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200127func Test_terminal_split_quit()
128 let buf = Run_shell_in_terminal({})
129 call term_wait(buf)
130 split
131 quit!
132 call term_wait(buf)
133 sleep 50m
134 call assert_equal('run', job_status(g:job))
135
136 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200137 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200138
139 exe buf . 'bwipe'
140 unlet g:job
141endfunc
142
Bram Moolenaar94053a52017-08-01 21:44:33 +0200143func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200144 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200145 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200146 quit
147 for nr in range(1, winnr('$'))
148 call assert_notequal(winbufnr(nr), buf)
149 endfor
150 call assert_true(bufloaded(buf))
151 call assert_true(buflisted(buf))
152
153 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200154 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200155 exe buf . 'bwipe'
156
157 unlet g:job
158endfunc
159
Bram Moolenaar1e115362019-01-09 23:01:02 +0100160func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200161 exe g:buf . 'bwipe!'
162 let g:buf = 0
163endfunc
164
Bram Moolenaar9d189612017-09-09 18:11:00 +0200165func Get_cat_123_cmd()
166 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100167 if !has('conpty')
168 return 'cmd /c "cls && color 2 && echo 123"'
169 else
170 " When clearing twice, extra sequence is not output.
171 return 'cmd /c "cls && cls && color 2 && echo 123"'
172 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200173 else
174 call writefile(["\<Esc>[32m123"], 'Xtext')
175 return "cat Xtext"
176 endif
177endfunc
178
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200179func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200180 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200181 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
182 let g:job = term_getjob(g:buf)
183
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200184 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
185 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200186 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100187 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200188 call delete('Xtext')
189endfunc
190
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200192 let l = term_scrape(a:buf, 0)
193 call assert_true(len(l) == 0)
194 let l = term_scrape(a:buf, 999)
195 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200196 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200197 call assert_true(len(l) > 0)
198 call assert_equal('1', l[0].chars)
199 call assert_equal('2', l[1].chars)
200 call assert_equal('3', l[2].chars)
201 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200202 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
203 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100204 if has('win32')
205 " On Windows 'background' always defaults to dark, even though the terminal
206 " may use a light background. Therefore accept both white and black.
207 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200208 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100209 if &background == 'light'
210 call assert_equal('#ffffff', l[0].bg)
211 else
212 call assert_equal('#000000', l[0].bg)
213 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200214 endif
215
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200216 let l = term_getline(a:buf, -1)
217 call assert_equal('', l)
218 let l = term_getline(a:buf, 0)
219 call assert_equal('', l)
220 let l = term_getline(a:buf, 999)
221 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200222 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200223 call assert_equal('123', l)
224endfunc
225
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200226func Test_terminal_scrape_123()
227 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200228 let buf = term_start(cmd)
229
230 let termlist = term_list()
231 call assert_equal(1, len(termlist))
232 call assert_equal(buf, termlist[0])
233
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200234 " Nothing happens with invalid buffer number
235 call term_wait(1234)
236
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200237 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200238 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200239 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200240 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200241 call Check_123(buf)
242
243 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100244 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200245 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200246 call term_wait(buf)
247 call Check_123(buf)
248
249 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200250 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200251endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200252
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200253func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200254 call writefile(["léttrs"], 'Xtext')
255 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200256 " Run cmd with UTF-8 codepage to make the type command print the expected
257 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100258 let buf = term_start("cmd /K chcp 65001")
259 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200260 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100261 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200262 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let buf = term_start("cat Xtext")
264 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200265 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100267 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
268 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200269 call assert_true(len(l) >= 7)
270 call assert_equal('l', l[0].chars)
271 call assert_equal('é', l[1].chars)
272 call assert_equal(1, l[1].width)
273 call assert_equal('t', l[2].chars)
274 call assert_equal('t', l[3].chars)
275 call assert_equal('ま', l[4].chars)
276 call assert_equal(2, l[4].width)
277 call assert_equal('r', l[5].chars)
278 call assert_equal('s', l[6].chars)
279
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100280 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200281 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100282 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200283
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100284 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200285 call delete('Xtext')
286endfunc
287
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200288func Test_terminal_scroll()
289 call writefile(range(1, 200), 'Xtext')
290 if has('win32')
291 let cmd = 'cmd /c "type Xtext"'
292 else
293 let cmd = "cat Xtext"
294 endif
295 let buf = term_start(cmd)
296
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100297 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200298 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200299 call term_wait(buf)
300 if has('win32')
301 " TODO: this should not be needed
302 sleep 100m
303 endif
304
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200305 let scrolled = buf->term_getscrolled()
306 call assert_equal(scrolled, term_getscrolled(buf))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200307 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200308 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200309 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200310 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200311 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200312 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200313
314 exe buf . 'bwipe'
315 call delete('Xtext')
316endfunc
317
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200318func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200319 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200320 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200321 call writefile(range(150), 'Xtext')
322 if has('win32')
323 call term_sendkeys(buf, "type Xtext\<CR>")
324 else
325 call term_sendkeys(buf, "cat Xtext\<CR>")
326 endif
327 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200328 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200329 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200330 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200331 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200332
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200333 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200334 call term_wait(buf)
335 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200336 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100337 call delete('Xtext')
338endfunc
339
340func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200341 " tail -f only works on Unix
342 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100343
344 call writefile(range(50), 'Xtext')
345 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100346 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100347 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100348 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100349 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
350 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100351 \ 'call feedkeys("\<C-W>N", "xt")',
352 \ ], 'XTest_postponed')
353 let buf = RunVimInTerminal('-S XTest_postponed', {})
354 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar96baf022019-02-14 23:49:38 +0100355 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100356
357 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200358 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100359 call term_sendkeys(buf, "k")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100360 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100361
362 " Back to Terminal-Job mode, text will scroll and show the extra line.
363 call term_sendkeys(buf, "a")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100364 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100365
366 call term_wait(buf)
367 call term_sendkeys(buf, "\<C-C>")
368 call term_wait(buf)
369 call term_sendkeys(buf, "exit\<CR>")
370 call term_wait(buf)
371 call term_sendkeys(buf, ":q\<CR>")
372 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 Moolenaare9f6fd22017-09-10 14:25:49 +0200566 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200567 return
568 endif
569 call mkdir('Xdir')
570 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200571 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200572
573 exe buf . 'bwipe'
574 call delete('Xdir', 'rf')
575endfunc
576
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200577func Test_terminal_cwd_failure()
578 " Case 1: Provided directory is not actually a directory. Attempt to make
579 " the file executable as well.
580 call writefile([], 'Xfile')
581 call setfperm('Xfile', 'rwx------')
582 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
583 call delete('Xfile')
584
585 " Case 2: Directory does not exist.
586 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
587
588 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100589 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200590 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100591 call mkdir('XdirNoAccess', '', '0600')
592 " return early if the directory permissions could not be set properly
593 if getfperm('XdirNoAccess')[2] == 'x'
594 call delete('XdirNoAccess', 'rf')
595 return
596 endif
597 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
598 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200599 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200600endfunc
601
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100602func Test_terminal_servername()
603 if !has('clientserver')
604 return
605 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200606 call s:test_environment("VIM_SERVERNAME", v:servername)
607endfunc
608
609func Test_terminal_version()
610 call s:test_environment("VIM_TERMINAL", string(v:version))
611endfunc
612
613func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100614 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100615 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200616 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100617 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200618 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100619 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200620 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100621 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100622 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200623 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200624 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100625
Bram Moolenaar012eb662018-03-13 17:55:27 +0100626 exe buf . 'bwipe'
627 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100628endfunc
629
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200630func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100631 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200632 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200633 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100634 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100635 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100636 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100637 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100638 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200639 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200640 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200641 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200642
Bram Moolenaar012eb662018-03-13 17:55:27 +0100643 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200644endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200645
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200646func Test_terminal_list_args()
647 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
648 call assert_fails(buf . 'bwipe', 'E517')
649 exe buf . 'bwipe!'
650 call assert_equal("", bufname(buf))
651endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200652
653func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100654 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100655 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200656 " The shell or something else has a problem dealing with more than 1000
657 " characters at the same time.
658 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100659 " NPFS is used in Windows, nonblocking mode does not work properly.
660 elseif has('win32')
661 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200662 else
663 let len = 5000
664 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200665
666 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100667 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200668 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100669 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200670
671 " On MS-Windows there is an extra empty line below "done". Find "done" in
672 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100673 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100674 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100675 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200676 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100677 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200678 endif
679 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200680
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100681 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200682 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100683 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200684 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200685 bwipe
686endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200687
688func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200689 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200690 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200691 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200692 if has('win32')
693 " TODO: enable once writing to stdin works on MS-Windows
694 return
695 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200696 new
697 call setline(1, ['one', 'two', 'three'])
698 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200699 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200700 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200701 call assert_equal(['3', '3', '14'], nrs)
702 bwipe
703
Bram Moolenaardada6d22017-09-02 17:18:35 +0200704 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200705 call setline(1, ['one', 'two', 'three', 'four'])
706 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200707 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200708 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200709 call assert_equal(['2', '2', '10'], nrs)
710 bwipe
711
Bram Moolenaardada6d22017-09-02 17:18:35 +0200712 if executable('python')
713 new
714 call setline(1, ['print("hello")'])
715 1term ++eof=exit() python
716 " MS-Windows echoes the input, Unix doesn't.
717 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
718 if getline(1) =~ 'hello'
719 call assert_equal('hello', getline(1))
720 else
721 call assert_equal('hello', getline(line('$') - 1))
722 endif
723 bwipe
724
725 if has('win32')
726 new
727 call setline(1, ['print("hello")'])
728 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200729 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200730 call assert_equal('hello', getline(line('$') - 1))
731 bwipe
732 endif
733 endif
734
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200735 bwipe!
736endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200737
738func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200739 let buf = term_start('NONE', {})
740 call assert_notequal(0, buf)
741
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200742 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200743 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100744 if has('gui_running') && !has('win32')
745 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200746 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100747 else
748 " Otherwise using a job works on all systems.
749 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200750 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200751 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200752
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200753 bwipe!
754endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200755
756func Test_terminal_special_chars()
757 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200758 CheckUnix
759
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200760 call mkdir('Xdir with spaces')
761 call writefile(['x'], 'Xdir with spaces/quoted"file')
762 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200763 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200764 call term_wait('')
765
766 call delete('Xdir with spaces', 'rf')
767 bwipe
768endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200769
770func Test_terminal_wrong_options()
771 call assert_fails('call term_start(&shell, {
772 \ "in_io": "file",
773 \ "in_name": "xxx",
774 \ "out_io": "file",
775 \ "out_name": "xxx",
776 \ "err_io": "file",
777 \ "err_name": "xxx"
778 \ })', 'E474:')
779 call assert_fails('call term_start(&shell, {
780 \ "out_buf": bufnr("%")
781 \ })', 'E474:')
782 call assert_fails('call term_start(&shell, {
783 \ "err_buf": bufnr("%")
784 \ })', 'E474:')
785endfunc
786
787func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200788 let cmd = Get_cat_123_cmd()
789 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
790 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100791 " ConPTY may precede escape sequence. There are things that are not so.
792 if !has('conpty')
793 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
794 call assert_match('123', readfile('Xfile')[0])
795 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200796 let g:job = term_getjob(buf)
797 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
798 call delete('Xfile')
799 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200800
801 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200802 call writefile(['one line'], 'Xfile')
803 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
804 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200805 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200806 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200807 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200808 bwipe
809 call delete('Xfile')
810 endif
811endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200812
813func TerminalTmap(remap)
814 let buf = Run_shell_in_terminal({})
815 call assert_equal('t', mode())
816
817 if a:remap
818 tmap 123 456
819 else
820 tnoremap 123 456
821 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100822 " don't use abcde, it's an existing command
823 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200824 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100825 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200826 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200827 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200828 let lnum = term_getcursor(buf)[0]
829 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100830 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200831 else
832 call assert_match('456', term_getline(buf, lnum))
833 endif
834
835 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200836 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200837 call term_wait(buf)
838
839 tunmap 123
840 tunmap 456
841 call assert_equal('', maparg('123', 't'))
842 close
843 unlet g:job
844endfunc
845
846func Test_terminal_tmap()
847 call TerminalTmap(1)
848 call TerminalTmap(0)
849endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200850
851func Test_terminal_wall()
852 let buf = Run_shell_in_terminal({})
853 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200854 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200855 call term_wait(buf)
856 exe buf . 'bwipe'
857 unlet g:job
858endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200859
Bram Moolenaar7a760922018-02-19 23:10:02 +0100860func Test_terminal_wqall()
861 let buf = Run_shell_in_terminal({})
862 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200863 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100864 call term_wait(buf)
865 exe buf . 'bwipe'
866 unlet g:job
867endfunc
868
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200869func Test_terminal_composing_unicode()
870 let save_enc = &encoding
871 set encoding=utf-8
872
873 if has('win32')
874 let cmd = "cmd /K chcp 65001"
875 let lnum = [3, 6, 9]
876 else
877 let cmd = &shell
878 let lnum = [1, 3, 5]
879 endif
880
881 enew
882 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100883 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200884 call term_wait(buf, 50)
885
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200886 if has('win32')
887 call assert_equal('cmd', job_info(g:job).cmd[0])
888 else
889 call assert_equal(&shell, job_info(g:job).cmd[0])
890 endif
891
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200892 " ascii + composing
893 let txt = "a\u0308bc"
894 call term_sendkeys(buf, "echo " . txt . "\r")
895 call term_wait(buf, 50)
896 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
897 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
898 let l = term_scrape(buf, lnum[0] + 1)
899 call assert_equal("a\u0308", l[0].chars)
900 call assert_equal("b", l[1].chars)
901 call assert_equal("c", l[2].chars)
902
903 " multibyte + composing
904 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
905 call term_sendkeys(buf, "echo " . txt . "\r")
906 call term_wait(buf, 50)
907 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
908 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
909 let l = term_scrape(buf, lnum[1] + 1)
910 call assert_equal("\u304b\u3099", l[0].chars)
911 call assert_equal("\u304e", l[1].chars)
912 call assert_equal("\u304f\u3099", l[2].chars)
913 call assert_equal("\u3052", l[3].chars)
914 call assert_equal("\u3053\u3099", l[4].chars)
915
916 " \u00a0 + composing
917 let txt = "abc\u00a0\u0308"
918 call term_sendkeys(buf, "echo " . txt . "\r")
919 call term_wait(buf, 50)
920 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
921 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
922 let l = term_scrape(buf, lnum[2] + 1)
923 call assert_equal("\u00a0\u0308", l[3].chars)
924
925 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200926 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200927 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100928 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200929 let &encoding = save_enc
930endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100931
932func Test_terminal_aucmd_on_close()
933 fun Nop()
934 let s:called = 1
935 endfun
936
937 aug repro
938 au!
939 au BufWinLeave * call Nop()
940 aug END
941
942 let [cmd, waittime] = s:get_sleep_cmd()
943
944 call assert_equal(1, winnr('$'))
945 new
946 call setline(1, ['one', 'two'])
947 exe 'term ++close ' . cmd
948 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200949 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100950 call assert_equal(1, s:called)
951 bwipe!
952
953 unlet s:called
954 au! repro
955 delfunc Nop
956endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100957
958func Test_terminal_term_start_empty_command()
959 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
960 call assert_fails(cmd, 'E474')
961 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
962 call assert_fails(cmd, 'E474')
963 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
964 call assert_fails(cmd, 'E474')
965 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
966 call assert_fails(cmd, 'E474')
967endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100968
969func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200970 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100971
972 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200973 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100974
Bram Moolenaar086eb872018-03-25 21:24:12 +0200975 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200976 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100977
Bram Moolenaar086eb872018-03-25 21:24:12 +0200978 " Request the cursor position.
979 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100980
981 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200982 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100983
Bram Moolenaar086eb872018-03-25 21:24:12 +0200984 " End "cat" gently.
985 call term_sendkeys(buf, "\<CR>\<C-D>")
986
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200987 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100988 exe buf . 'bwipe'
989 unlet g:job
990endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100991
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100992" Run Vim, start a terminal in that Vim with the kill argument,
993" :qall works.
994func Run_terminal_qall_kill(line1, line2)
995 " 1. Open a terminal window and wait for the prompt to appear
996 " 2. set kill using term_setkill()
997 " 3. make Vim exit, it will kill the shell
998 let after = [
999 \ a:line1,
1000 \ 'let buf = bufnr("%")',
1001 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1002 \ ' sleep 10m',
1003 \ 'endwhile',
1004 \ a:line2,
1005 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1006 \ 'qall',
1007 \ ]
1008 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001009 return
1010 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001011 call assert_equal("done", readfile("Xdone")[0])
1012 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001013endfunc
1014
1015" Run Vim in a terminal, then start a terminal in that Vim with a kill
1016" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001017func Test_terminal_qall_kill_arg()
1018 call Run_terminal_qall_kill('term ++kill=kill', '')
1019endfunc
1020
1021" Run Vim, start a terminal in that Vim, set the kill argument with
1022" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001023func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001024 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001025endfunc
1026
1027" Run Vim, start a terminal in that Vim without the kill argument,
1028" check that :qall does not exit, :qall! does.
1029func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001030 let after =<< trim [CODE]
1031 term
1032 let buf = bufnr("%")
1033 while term_getline(buf, 1) =~ "^\\s*$"
1034 sleep 10m
1035 endwhile
1036 set nomore
1037 au VimLeavePre * call writefile(["too early"], "Xdone")
1038 qall
1039 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1040 cquit
1041 [CODE]
1042
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001043 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001044 return
1045 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001046 call assert_equal("done", readfile("Xdone")[0])
1047 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001048endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001049
1050" Run Vim in a terminal, then start a terminal in that Vim without a kill
1051" argument, check that :confirm qall works.
1052func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001053 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001054 let buf = RunVimInTerminal('', {})
1055
1056 " Open a terminal window and wait for the prompt to appear
1057 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001058 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1059 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001060
1061 " make Vim exit, it will prompt to kill the shell
1062 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001063 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001064 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001065 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001066
1067 " close the terminal window where Vim was running
1068 quit
1069endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001070
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001071" Run Vim in a terminal, then start a terminal window with a shell and check
1072" that Vim exits if it is closed.
1073func Test_terminal_exit()
1074 CheckRunVimInTerminal
1075
1076 let lines =<< trim END
1077 let winid = win_getid()
1078 help
1079 term
1080 let termid = win_getid()
1081 call win_gotoid(winid)
1082 close
1083 call win_gotoid(termid)
1084 END
1085 call writefile(lines, 'XtermExit')
1086 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1087 let job = term_getjob(buf)
1088 call WaitForAssert({-> assert_equal("run", job_status(job))})
1089
1090 " quit the shell, it will make Vim exit
1091 call term_sendkeys(buf, "exit\<CR>")
1092 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1093
1094 call delete('XtermExit')
1095endfunc
1096
Bram Moolenaar012eb662018-03-13 17:55:27 +01001097func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001098 augroup repro
1099 au!
1100 au TerminalOpen * let s:called += 1
1101 augroup END
1102
1103 let s:called = 0
1104
1105 " Open a terminal window with :terminal
1106 terminal
1107 call assert_equal(1, s:called)
1108 bwipe!
1109
1110 " Open a terminal window with term_start()
1111 call term_start(&shell)
1112 call assert_equal(2, s:called)
1113 bwipe!
1114
1115 " Open a hidden terminal buffer with :terminal
1116 terminal ++hidden
1117 call assert_equal(3, s:called)
1118 for buf in term_list()
1119 exe buf . "bwipe!"
1120 endfor
1121
1122 " Open a hidden terminal buffer with term_start()
1123 let buf = term_start(&shell, {'hidden': 1})
1124 call assert_equal(4, s:called)
1125 exe buf . "bwipe!"
1126
1127 unlet s:called
1128 au! repro
1129endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001130
1131func Check_dump01(off)
1132 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1133 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001134 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001135endfunc
1136
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001137func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001138 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001139 let save_enc = &encoding
1140 set encoding=utf-8
1141 call assert_equal(1, winnr('$'))
1142
1143 let text = " a\u0300 e\u0302 o\u0308"
1144 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001145 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001146 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001147 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001148 let dumpline = readfile('Xdump')[0]
1149 call assert_match('|à| |ê| |ö', dumpline)
1150
1151 call StopVimInTerminal(buf)
1152 call delete('Xcomposing')
1153 call delete('Xdump')
1154 let &encoding = save_enc
1155endfunc
1156
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001157" just testing basic functionality.
1158func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001159 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001160 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001161 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001162 call assert_equal(2, winnr('$'))
1163 call assert_equal(20, line('$'))
1164 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001165
1166 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001167 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001168 call assert_equal(buf, buf2)
1169 call assert_notequal('one two three four five', trim(getline(1)))
1170
1171 " Load the first dump again in the same window
1172 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1173 call assert_equal(buf, buf2)
1174 call Check_dump01(0)
1175
1176 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1177 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1178 new
1179 let closedbuf = winbufnr('')
1180 quit
1181 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1182
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001183 quit
1184endfunc
1185
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001186func Test_terminal_dumpload_dump()
1187 CheckRunVimInTerminal
1188
1189 let lines =<< trim END
1190 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1191 END
1192 call writefile(lines, 'XtermDumpload')
1193 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1194 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1195
1196 call StopVimInTerminal(buf)
1197 call delete('XtermDumpload')
1198endfunc
1199
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001200func Test_terminal_dumpdiff()
1201 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001202 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001203 call assert_equal(2, winnr('$'))
1204 call assert_equal(62, line('$'))
1205 call Check_dump01(0)
1206 call Check_dump01(42)
1207 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1208 quit
1209endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001210
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001211func Test_terminal_dumpdiff_swap()
1212 call assert_equal(1, winnr('$'))
1213 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1214 call assert_equal(2, winnr('$'))
1215 call assert_equal(62, line('$'))
1216 call assert_match('Test_popup_command_01.dump', getline(21))
1217 call assert_match('Test_popup_command_03.dump', getline(42))
1218 call assert_match('Undo', getline(3))
1219 call assert_match('three four five', getline(45))
1220
1221 normal s
1222 call assert_match('Test_popup_command_03.dump', getline(21))
1223 call assert_match('Test_popup_command_01.dump', getline(42))
1224 call assert_match('three four five', getline(3))
1225 call assert_match('Undo', getline(45))
1226 quit
1227endfunc
1228
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001229func Test_terminal_dumpdiff_options()
1230 set laststatus=0
1231 call assert_equal(1, winnr('$'))
1232 let height = winheight(0)
1233 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1234 call assert_equal(2, winnr('$'))
1235 call assert_equal(height, winheight(winnr()))
1236 call assert_equal(33, winwidth(winnr()))
1237 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1238 quit
1239
1240 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001241 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1242 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001243 call assert_equal(&columns, winwidth(0))
1244 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001245 call assert_equal('something else', bufname('%'))
1246 quit
1247
1248 call assert_equal(1, winnr('$'))
1249 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1250 call assert_equal(1, winnr('$'))
1251 bwipe
1252
1253 set laststatus&
1254endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001255
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001256func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001257 call assert_equal(1, winnr('$'))
1258
1259 " Use the title termcap entries to output the escape sequence.
1260 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001261 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001262 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001263 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001264 \ 'redraw',
1265 \ "set t_ts=",
1266 \ ], 'Xscript')
1267 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001268 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001269 call assert_equal('Xtextfile', expand('%:t'))
1270 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001271 return buf
1272endfunc
1273
1274func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001275 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001276 let buf = Api_drop_common('')
1277 call assert_equal(0, &bin)
1278 call assert_equal('', &fenc)
1279
1280 call StopVimInTerminal(buf)
1281 call delete('Xscript')
1282 bwipe Xtextfile
1283endfunc
1284
1285func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001286 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001287 let buf = Api_drop_common(',{"bin":1}')
1288 call assert_equal(1, &bin)
1289
1290 call StopVimInTerminal(buf)
1291 call delete('Xscript')
1292 bwipe Xtextfile
1293endfunc
1294
1295func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001296 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001297 let buf = Api_drop_common(',{"binary":1}')
1298 call assert_equal(1, &bin)
1299
1300 call StopVimInTerminal(buf)
1301 call delete('Xscript')
1302 bwipe Xtextfile
1303endfunc
1304
1305func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001306 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001307 set binary
1308 let buf = Api_drop_common(',{"nobin":1}')
1309 call assert_equal(0, &bin)
1310
1311 call StopVimInTerminal(buf)
1312 call delete('Xscript')
1313 bwipe Xtextfile
1314 set nobinary
1315endfunc
1316
1317func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001318 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001319 set binary
1320 let buf = Api_drop_common(',{"nobinary":1}')
1321 call assert_equal(0, &bin)
1322
1323 call StopVimInTerminal(buf)
1324 call delete('Xscript')
1325 bwipe Xtextfile
1326 set nobinary
1327endfunc
1328
1329func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001330 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001331 let buf = Api_drop_common(',{"ff":"dos"}')
1332 call assert_equal("dos", &ff)
1333
1334 call StopVimInTerminal(buf)
1335 call delete('Xscript')
1336 bwipe Xtextfile
1337endfunc
1338
1339func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001340 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001341 let buf = Api_drop_common(',{"fileformat":"dos"}')
1342 call assert_equal("dos", &ff)
1343
1344 call StopVimInTerminal(buf)
1345 call delete('Xscript')
1346 bwipe Xtextfile
1347endfunc
1348
1349func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001350 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001351 let buf = Api_drop_common(',{"enc":"utf-16"}')
1352 call assert_equal("utf-16", &fenc)
1353
1354 call StopVimInTerminal(buf)
1355 call delete('Xscript')
1356 bwipe Xtextfile
1357endfunc
1358
1359func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001360 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001361 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1362 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001363
1364 call StopVimInTerminal(buf)
1365 call delete('Xscript')
1366 bwipe Xtextfile
1367endfunc
1368
1369func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001370 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001371 let firstwinid = win_getid()
1372 split Xtextfile
1373 let textfile_winid = win_getid()
1374 call assert_equal(2, winnr('$'))
1375 call win_gotoid(firstwinid)
1376
1377 " Use the title termcap entries to output the escape sequence.
1378 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001379 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001380 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1381 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1382 \ 'redraw',
1383 \ "set t_ts=",
1384 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001385 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001386 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001387 call assert_equal(textfile_winid, win_getid())
1388
1389 call StopVimInTerminal(buf)
1390 call delete('Xscript')
1391 bwipe Xtextfile
1392endfunc
1393
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001394func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001395 let g:called_bufnum = a:bufnum
1396 let g:called_arg = a:arg
1397endfunc
1398
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001399func WriteApiCall(funcname)
1400 " Use the title termcap entries to output the escape sequence.
1401 call writefile([
1402 \ 'set title',
1403 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1404 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1405 \ 'redraw',
1406 \ "set t_ts=",
1407 \ ], 'Xscript')
1408endfunc
1409
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001410func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001411 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001412
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001413 unlet! g:called_bufnum
1414 unlet! g:called_arg
1415
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001416 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001417
1418 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001419 let buf = RunVimInTerminal('-S Xscript', {})
1420 call WaitFor({-> exists('g:called_bufnum')})
1421 call assert_equal(buf, g:called_bufnum)
1422 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001423 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001424
1425 unlet! g:called_bufnum
1426 unlet! g:called_arg
1427
1428 " Enable explicitly
1429 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1430 call WaitFor({-> exists('g:called_bufnum')})
1431 call assert_equal(buf, g:called_bufnum)
1432 call assert_equal(['hello', 123], g:called_arg)
1433 call StopVimInTerminal(buf)
1434
1435 unlet! g:called_bufnum
1436 unlet! g:called_arg
1437
1438 func! ApiCall_TryThis(bufnum, arg)
1439 let g:called_bufnum2 = a:bufnum
1440 let g:called_arg2 = a:arg
1441 endfunc
1442
1443 call WriteApiCall('ApiCall_TryThis')
1444
1445 " Use prefix match
1446 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1447 call WaitFor({-> exists('g:called_bufnum2')})
1448 call assert_equal(buf, g:called_bufnum2)
1449 call assert_equal(['hello', 123], g:called_arg2)
1450 call StopVimInTerminal(buf)
1451
1452 unlet! g:called_bufnum2
1453 unlet! g:called_arg2
1454
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001455 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001456 delfunction! ApiCall_TryThis
1457 unlet! g:called_bufnum2
1458 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001459endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001460
1461func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001462 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001463
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001464 func! TryThis(bufnum, arg)
1465 let g:called_bufnum3 = a:bufnum
1466 let g:called_arg3 = a:arg
1467 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001468
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001469 call WriteApiCall('TryThis')
1470
1471 unlet! g:called_bufnum3
1472 unlet! g:called_arg3
1473
1474 " Not permitted
1475 call ch_logfile('Xlog', 'w')
1476 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1477 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1478 call assert_false(exists('g:called_bufnum3'))
1479 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001480 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001481
1482 " No match
1483 call ch_logfile('Xlog', 'w')
1484 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1485 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1486 call assert_false(exists('g:called_bufnum3'))
1487 call assert_false(exists('g:called_arg3'))
1488 call StopVimInTerminal(buf)
1489
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001490 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001491 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001492 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001493 delfunction! TryThis
1494 unlet! g:called_bufnum3
1495 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001496endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001497
Bram Moolenaara997b452018-04-17 23:24:06 +02001498let s:caught_e937 = 0
1499
1500func Tapi_Delete(bufnum, arg)
1501 try
1502 execute 'bdelete!' a:bufnum
1503 catch /E937:/
1504 let s:caught_e937 = 1
1505 endtry
1506endfunc
1507
1508func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001509 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001510
1511 call WriteApiCall('Tapi_Delete')
1512 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001513 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001514
1515 call StopVimInTerminal(buf)
1516 call delete('Xscript')
1517 call ch_logfile('', '')
1518endfunc
1519
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001520func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001521 if !exists('*term_getansicolors')
1522 throw 'Skipped: term_getansicolors() not supported'
1523 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001524 let colors = [
1525 \ '#000000', '#e00000',
1526 \ '#00e000', '#e0e000',
1527 \ '#0000e0', '#e000e0',
1528 \ '#00e0e0', '#e0e0e0',
1529 \ '#808080', '#ff4040',
1530 \ '#40ff40', '#ffff40',
1531 \ '#4040ff', '#ff40ff',
1532 \ '#40ffff', '#ffffff',
1533 \]
1534
1535 let buf = Run_shell_in_terminal({})
1536 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001537 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001538 call term_wait(buf)
1539
1540 exe buf . 'bwipe'
1541endfunc
1542
1543let s:test_colors = [
1544 \ '#616e64', '#0d0a79',
1545 \ '#6d610d', '#0a7373',
1546 \ '#690d0a', '#6d696e',
1547 \ '#0d0a6f', '#616e0d',
1548 \ '#0a6479', '#6d0d0a',
1549 \ '#617373', '#0d0a69',
1550 \ '#6d690d', '#0a6e6f',
1551 \ '#610d0a', '#6e6479',
1552 \]
1553
1554func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001555 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001556 if !exists('*term_getansicolors')
1557 throw 'Skipped: term_getansicolors() not supported'
1558 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001559 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1560 let buf = Run_shell_in_terminal({})
1561 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001562 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001563 call term_wait(buf)
1564
1565 exe buf . 'bwipe'
1566 unlet g:terminal_ansi_colors
1567endfunc
1568
1569func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001570 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001571 if !exists('*term_getansicolors')
1572 throw 'Skipped: term_getansicolors() not supported'
1573 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001574 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1575 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1576 call assert_equal(s:test_colors, term_getansicolors(buf))
1577
1578 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001579 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001580
1581 let colors = [
1582 \ 'ivory', 'AliceBlue',
1583 \ 'grey67', 'dark goldenrod',
1584 \ 'SteelBlue3', 'PaleVioletRed4',
1585 \ 'MediumPurple2', 'yellow2',
1586 \ 'RosyBrown3', 'OrangeRed2',
1587 \ 'white smoke', 'navy blue',
1588 \ 'grey47', 'gray97',
1589 \ 'MistyRose2', 'DodgerBlue4',
1590 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001591 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001592
1593 let colors[4] = 'Invalid'
1594 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1595
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001596 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001597 call term_wait(buf)
1598 exe buf . 'bwipe'
1599endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001600
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001601func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001602 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001603
1604 " Use all the ANSI colors.
1605 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001606 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001607 \ 'hi Tblack ctermfg=0 ctermbg=8',
1608 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1609 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1610 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1611 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1612 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1613 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1614 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1615 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1616 \ 'hi Tred ctermfg=9 ctermbg=1',
1617 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1618 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1619 \ 'hi Tblue ctermfg=12 ctermbg=4',
1620 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1621 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1622 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001623 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1624 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1625 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001626 \ '',
1627 \ 'call matchadd("Tblack", "A")',
1628 \ 'call matchadd("Tdarkred", "B")',
1629 \ 'call matchadd("Tdarkgreen", "C")',
1630 \ 'call matchadd("Tbrown", "D")',
1631 \ 'call matchadd("Tdarkblue", "E")',
1632 \ 'call matchadd("Tdarkmagenta", "F")',
1633 \ 'call matchadd("Tdarkcyan", "G")',
1634 \ 'call matchadd("Tlightgrey", "H")',
1635 \ 'call matchadd("Tdarkgrey", "I")',
1636 \ 'call matchadd("Tred", "J")',
1637 \ 'call matchadd("Tgreen", "K")',
1638 \ 'call matchadd("Tyellow", "L")',
1639 \ 'call matchadd("Tblue", "M")',
1640 \ 'call matchadd("Tmagenta", "N")',
1641 \ 'call matchadd("Tcyan", "O")',
1642 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001643 \ 'call matchadd("TdarkredBold", "X")',
1644 \ 'call matchadd("TgreenBold", "Y")',
1645 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001646 \ 'redraw',
1647 \ ], 'Xcolorscript')
1648 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1649 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1650
1651 call term_sendkeys(buf, ":q\<CR>")
1652 call StopVimInTerminal(buf)
1653 call delete('Xcolorscript')
1654endfunc
1655
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001656func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001657 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001658 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001659 let text = []
1660 for n in range(10)
1661 call add(text, repeat(n, 50))
1662 endfor
1663 call writefile(text, 'Xwinsize')
1664 let buf = RunVimInTerminal('Xwinsize', {})
1665 let win = bufwinid(buf)
1666 call assert_equal([6, 40], term_getsize(buf))
1667 call assert_equal(6, winheight(win))
1668 call assert_equal(40, winwidth(win))
1669
1670 " resizing the window doesn't resize the terminal.
1671 resize 10
1672 vertical resize 60
1673 call assert_equal([6, 40], term_getsize(buf))
1674 call assert_equal(10, winheight(win))
1675 call assert_equal(60, winwidth(win))
1676
1677 call StopVimInTerminal(buf)
1678 call delete('Xwinsize')
1679
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001680 call assert_fails('set termwinsize=40', 'E474')
1681 call assert_fails('set termwinsize=10+40', 'E474')
1682 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001683
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001684 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001685endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001686
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001687func Test_terminal_termwinsize_option_zero()
1688 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001689 let buf = Run_shell_in_terminal({})
1690 let win = bufwinid(buf)
1691 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001692 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001693 call term_wait(buf)
1694 exe buf . 'bwipe'
1695
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001696 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001697 let buf = Run_shell_in_terminal({})
1698 let win = bufwinid(buf)
1699 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001700 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001701 call term_wait(buf)
1702 exe buf . 'bwipe'
1703
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001704 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001705 let buf = Run_shell_in_terminal({})
1706 let win = bufwinid(buf)
1707 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001708 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001709 call term_wait(buf)
1710 exe buf . 'bwipe'
1711
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001712 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001713endfunc
1714
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001715func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001716 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001717 vsplit
1718 let buf = Run_shell_in_terminal({})
1719 let win = bufwinid(buf)
1720 call assert_inrange(10, 1000, winheight(win))
1721 call assert_inrange(50, 1000, winwidth(win))
1722 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1723
1724 resize 15
1725 vertical resize 60
1726 redraw
1727 call assert_equal([15, 60], term_getsize(buf))
1728 call assert_equal(15, winheight(win))
1729 call assert_equal(60, winwidth(win))
1730
1731 resize 7
1732 vertical resize 30
1733 redraw
1734 call assert_equal([10, 50], term_getsize(buf))
1735 call assert_equal(7, winheight(win))
1736 call assert_equal(30, winwidth(win))
1737
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001738 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001739 call term_wait(buf)
1740 exe buf . 'bwipe'
1741
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001742 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001743 let buf = Run_shell_in_terminal({})
1744 let win = bufwinid(buf)
1745 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001746 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001747 call term_wait(buf)
1748 exe buf . 'bwipe'
1749
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001750 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001751endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001752
1753func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001754 " make three tabpages, terminal in the middle
1755 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001756 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001757 tabnew
1758 tabprev
1759 call assert_equal(1, winnr('$'))
1760 call assert_equal(2, tabpagenr())
1761 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001762
1763 let buf = Run_shell_in_terminal({})
1764 let termwin = bufwinid(buf)
1765 set termwinkey=<C-L>
1766 call feedkeys("\<C-L>w", 'tx')
1767 call assert_equal(thiswin, win_getid())
1768 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001769 call assert_equal(termwin, win_getid())
1770
Bram Moolenaar997d4242019-09-14 21:23:40 +02001771 if has('langmap')
1772 set langmap=xjyk
1773 call feedkeys("\<C-L>x", 'tx')
1774 call assert_equal(thiswin, win_getid())
1775 call feedkeys("\<C-W>y", 'tx')
1776 call assert_equal(termwin, win_getid())
1777 set langmap=
1778 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001779
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001780 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001781 call assert_equal(3, tabpagenr())
1782 tabprev
1783 call assert_equal(2, tabpagenr())
1784 call assert_equal(termwin, win_getid())
1785
1786 call feedkeys("\<C-L>gT", "xt")
1787 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001788 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001789 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001790 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001791
1792 let job = term_getjob(buf)
1793 call feedkeys("\<C-L>\<C-C>", 'tx')
1794 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001795
1796 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001797 tabnext
1798 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001799 tabprev
1800 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001801endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001802
1803func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001804 CheckUnix
1805
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001806 call writefile([
1807 \ '#!/bin/sh',
1808 \ 'echo "this is standard error" >&2',
1809 \ 'echo "this is standard out" >&1',
1810 \ ], 'Xechoerrout.sh')
1811 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1812
1813 let outfile = 'Xtermstdout'
1814 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001815
1816 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1817 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001818 call assert_equal('this is standard error', term_getline(buf, 1))
1819
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001820 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001821 exe buf . 'bwipe'
1822 call delete('Xechoerrout.sh')
1823 call delete(outfile)
1824endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001825
1826func Test_terminwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001827 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001828
1829 " Let the terminal output more than 'termwinscroll' lines, some at the start
1830 " will be dropped.
1831 exe 'set termwinscroll=' . &lines
1832 let buf = term_start('/bin/sh')
1833 for i in range(1, &lines)
1834 call feedkeys("echo " . i . "\<CR>", 'xt')
1835 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1836 endfor
1837 " Go to Terminal-Normal mode to update the buffer.
1838 call feedkeys("\<C-W>N", 'xt')
1839 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1840
1841 " Every "echo nr" must only appear once
1842 let lines = getline(1, line('$'))
1843 for i in range(&lines - len(lines) / 2 + 2, &lines)
1844 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1845 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1846 endfor
1847
1848 exe buf . 'bwipe!'
1849endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001850
Bram Moolenaar875cf872018-07-08 20:49:07 +02001851" Resizing the terminal window caused an ml_get error.
1852" TODO: This does not reproduce the original problem.
1853func Test_terminal_resize()
1854 set statusline=x
1855 terminal
1856 call assert_equal(2, winnr('$'))
1857
1858 " Fill the terminal with text.
1859 if has('win32')
1860 call feedkeys("dir\<CR>", 'xt')
1861 else
1862 call feedkeys("ls\<CR>", 'xt')
1863 endif
1864 " Go to Terminal-Normal mode for a moment.
1865 call feedkeys("\<C-W>N", 'xt')
1866 " Open a new window
1867 call feedkeys("i\<C-W>n", 'xt')
1868 call assert_equal(3, winnr('$'))
1869 redraw
1870
1871 close
1872 call assert_equal(2, winnr('$'))
1873 call feedkeys("exit\<CR>", 'xt')
1874 set statusline&
1875endfunc
1876
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001877" must be nearly the last, we can't go back from GUI to terminal
1878func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001879 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001880
1881 " Ignore the "failed to create input context" error.
1882 call test_ignore_error('E285:')
1883
1884 gui -f
1885
1886 call assert_equal(1, winnr('$'))
1887 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001888 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001889 call term_wait(buf)
1890
1891 " closing window wipes out the terminal buffer a with finished job
1892 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1893 call assert_equal("", bufname(buf))
1894
1895 unlet g:job
1896endfunc
1897
1898func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001899 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001900 set guioptions+=!
1901
1902 let filename = 'Xtestscript'
1903 if has('win32')
1904 let filename .= '.bat'
1905 let prefix = ''
1906 let contents = ['@echo off', 'exit %1']
1907 else
1908 let filename .= '.sh'
1909 let prefix = './'
1910 let contents = ['#!/bin/sh', 'exit $1']
1911 endif
1912 call writefile(contents, filename)
1913 call setfperm(filename, 'rwxrwx---')
1914
1915 " Check if v:shell_error is equal to the exit status.
1916 let exitval = 0
1917 execute printf(':!%s%s %d', prefix, filename, exitval)
1918 call assert_equal(exitval, v:shell_error)
1919
1920 let exitval = 9
1921 execute printf(':!%s%s %d', prefix, filename, exitval)
1922 call assert_equal(exitval, v:shell_error)
1923
1924 set guioptions&
1925 call delete(filename)
1926endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001927
1928func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001929 CheckUnix
1930
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001931 term ++hidden cat
1932 let bnr = bufnr('$')
1933 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1934 exe 'sbuf ' . bnr
1935 call assert_equal('terminal', &buftype)
1936 call term_sendkeys(bnr, "asdf\<CR>")
1937 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1938 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001939 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001940 bwipe!
1941endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001942
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001943func Test_terminal_switch_mode()
1944 term
1945 let bnr = bufnr('$')
1946 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1947 call feedkeys("\<C-W>N", 'xt')
1948 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1949 call feedkeys("A", 'xt')
1950 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1951 call feedkeys("\<C-W>N", 'xt')
1952 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1953 call feedkeys("I", 'xt')
1954 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1955 call feedkeys("\<C-W>Nv", 'xt')
1956 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1957 call feedkeys("I", 'xt')
1958 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1959 call feedkeys("\<C-W>Nv", 'xt')
1960 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1961 call feedkeys("A", 'xt')
1962 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1963 bwipe!
1964endfunc
1965
Bram Moolenaara3817732019-10-16 16:31:44 +02001966func Test_terminal_normal_mode()
1967 CheckRunVimInTerminal
1968
1969 " Run Vim in a terminal and open a terminal window to run Vim in.
1970 let lines =<< trim END
1971 call setline(1, range(11111, 11122))
1972 3
1973 END
1974 call writefile(lines, 'XtermNormal')
1975 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
1976 call term_wait(buf)
1977
1978 call term_sendkeys(buf, "\<C-W>N")
1979 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
1980 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
1981
1982 call term_sendkeys(buf, ":set culopt=number\r")
1983 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
1984
1985 call term_sendkeys(buf, ":set culopt=line\r")
1986 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
1987
1988 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
1989 call StopVimInTerminal(buf)
1990 call delete('XtermNormal')
1991endfunc
1992
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001993func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001994 CheckUnix
1995
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001996 call assert_equal(1, winnr('$'))
1997 term ++hidden ++close ls
1998 let bnr = bufnr('$')
1999 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2000 call WaitForAssert({-> assert_false(bufexists(bnr))})
2001 call assert_equal(1, winnr('$'))
2002endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002003
2004func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002005 " This test does not pass through ConPTY.
2006 if has('conpty')
2007 return
2008 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002009 let contents = [
2010 \ [ 'One', '', 'X' ],
2011 \ [ 'Two', '', '' ],
2012 \ [ 'Three' ] + repeat([''], 30)
2013 \ ]
2014
2015 for c in contents
2016 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002017 if has('win32')
2018 term cmd /c type Xfile
2019 else
2020 term cat Xfile
2021 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002022 let bnr = bufnr('$')
2023 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2024 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002025 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002026 call assert_equal(c, getline(1, line('$')))
2027 quit
2028 endfor
2029
2030 call delete('Xfile')
2031endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002032
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002033func Test_terminal_no_job()
2034 let term = term_start('false', {'term_finish': 'close'})
2035 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2036endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002037
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002038func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002039 CheckUnix
2040
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002041 let buf = Run_shell_in_terminal({})
2042
2043 " Wait for the shell to display a prompt.
2044 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2045
2046 " Hide the cursor.
2047 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2048 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2049
2050 " Show the cursor.
2051 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002052 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002053
2054 " Change color of cursor.
2055 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2056 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2057 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2058 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2059 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2060
2061 " Make cursor a blinking block.
2062 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2063 call WaitForAssert({-> assert_equal([1, 1],
2064 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2065
2066 " Make cursor a steady block.
2067 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2068 call WaitForAssert({-> assert_equal([0, 1],
2069 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2070
2071 " Make cursor a blinking underline.
2072 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2073 call WaitForAssert({-> assert_equal([1, 2],
2074 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2075
2076 " Make cursor a steady underline.
2077 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2078 call WaitForAssert({-> assert_equal([0, 2],
2079 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2080
2081 " Make cursor a blinking vertical bar.
2082 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2083 call WaitForAssert({-> assert_equal([1, 3],
2084 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2085
2086 " Make cursor a steady vertical bar.
2087 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2088 call WaitForAssert({-> assert_equal([0, 3],
2089 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2090
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002091 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002092endfunc
2093
Bram Moolenaar1e115362019-01-09 23:01:02 +01002094func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002095 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002096 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002097 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002098 call assert_equal('', term_gettitle(bufnr('$') + 1))
2099
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002100 if !has('title') || &title == 0 || empty(&t_ts)
2101 throw "Skipped: can't get/set title"
2102 endif
2103
Bram Moolenaar1e115362019-01-09 23:01:02 +01002104 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002105 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002106 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2107 call term_sendkeys(term, ":e Xfoo\r")
2108 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002109 else
2110 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002111 call term_sendkeys(term, ":e Xfoo\r")
2112 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002113 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002114
Bram Moolenaar1e115362019-01-09 23:01:02 +01002115 call term_sendkeys(term, ":set titlestring=foo\r")
2116 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2117
2118 exe term . 'bwipe!'
2119endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002120
2121" When drawing the statusline the cursor position may not have been updated
2122" yet.
2123" 1. create a terminal, make it show 2 lines
2124" 2. 0.5 sec later: leave terminal window, execute "i"
2125" 3. 0.5 sec later: clear terminal window, now it's 1 line
2126" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2127" 4. 0.5 sec later: should be done, clean up
2128func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002129 CheckUnix
2130
Bram Moolenaar10772302019-01-20 18:25:54 +01002131 set statusline=x
2132 terminal
2133 let tbuf = bufnr('')
2134 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2135 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2136 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2137 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2138
2139 sleep 2
2140 exe tbuf . 'bwipe!'
2141 au! BufLeave
2142 set statusline=
2143endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002144
2145func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002146 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002147
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002148 " split, go to the bottom-right window
2149 split
2150 wincmd j
2151 set splitright
2152
2153 call writefile([
2154 \ 'echo getwinpos()',
2155 \ ], 'XTest_getwinpos')
2156 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2157 call term_wait(buf)
2158
2159 " Find the output of getwinpos() in the bottom line.
2160 let rows = term_getsize(buf)[0]
2161 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2162 let line = term_getline(buf, rows)
2163 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2164 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2165
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002166 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002167 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002168 " In the GUI it can be more, let's assume a 20 x 14 cell.
2169 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002170 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002171 let winpos = 50->getwinpos()
2172 call assert_equal(xroot, winpos[0])
2173 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002174 let [winrow, wincol] = win_screenpos('.')
2175 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2176 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2177 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2178 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002179
2180 call term_wait(buf)
2181 call term_sendkeys(buf, ":q\<CR>")
2182 call StopVimInTerminal(buf)
2183 call delete('XTest_getwinpos')
2184 exe buf . 'bwipe!'
2185 set splitright&
2186 only!
2187endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002188
2189func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002190 " somehow doesn't work on MS-Windows
2191 CheckUnix
2192 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002193
2194 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002195 call writefile(["\<Esc>[?1047h"], 'Xtext')
2196 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002197 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2198
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002199 call writefile(["\<Esc>[?1047l"], 'Xtext')
2200 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002201 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002202
2203 call term_sendkeys(buf, "exit\r")
2204 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002205 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002206endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002207
2208func Test_terminal_setapi_and_call()
2209 if !CanRunVimInTerminal()
2210 return
2211 endif
2212
2213 call WriteApiCall('Tapi_TryThis')
2214 call ch_logfile('Xlog', 'w')
2215
2216 unlet! g:called_bufnum
2217 unlet! g:called_arg
2218
2219 let buf = RunVimInTerminal('-S Xscript', {'term_api': 0})
2220 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2221 call assert_false(exists('g:called_bufnum'))
2222 call assert_false(exists('g:called_arg'))
2223
2224 call term_setapi(buf, 'Tapi_TryThis')
2225 call term_sendkeys(buf, ":set notitle\<CR>")
2226 call term_sendkeys(buf, ":source Xscript\<CR>")
2227 call WaitFor({-> exists('g:called_bufnum')})
2228 call assert_equal(buf, g:called_bufnum)
2229 call assert_equal(['hello', 123], g:called_arg)
2230 call StopVimInTerminal(buf)
2231
2232 call delete('Xscript')
2233 call ch_logfile('')
2234 call delete('Xlog')
2235 unlet! g:called_bufnum
2236 unlet! g:called_arg
2237endfunc