blob: f506a39f68d9b414f3c9987cff568b261bea2dae [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
71func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020073 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020074 call term_wait(buf)
75
76 setlocal modifiable
77 exe "normal Axxx\<Esc>"
78 call assert_fails(buf . 'bwipe', 'E517')
79 undo
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081 exe buf . 'bwipe'
82 unlet g:job
83endfunc
84
Bram Moolenaar5b868a82019-02-25 06:11:53 +010085func Test_terminal_paste_register()
86 let @" = "text to paste"
87
88 let buf = Run_shell_in_terminal({})
89 " Wait for the shell to display a prompt
90 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
91
92 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
93 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020094 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010095
96 exe buf . 'bwipe!'
97 unlet g:job
98endfunc
99
Bram Moolenaar94053a52017-08-01 21:44:33 +0200100func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200101 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200102 call assert_fails(buf . 'bwipe', 'E517')
103 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200104 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200105 call assert_equal("", bufname(buf))
106
107 unlet g:job
108endfunc
109
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200110func Test_terminal_split_quit()
111 let buf = Run_shell_in_terminal({})
112 call term_wait(buf)
113 split
114 quit!
115 call term_wait(buf)
116 sleep 50m
117 call assert_equal('run', job_status(g:job))
118
119 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200120 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200121
122 exe buf . 'bwipe'
123 unlet g:job
124endfunc
125
Bram Moolenaar94053a52017-08-01 21:44:33 +0200126func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200127 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200128 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200129 quit
130 for nr in range(1, winnr('$'))
131 call assert_notequal(winbufnr(nr), buf)
132 endfor
133 call assert_true(bufloaded(buf))
134 call assert_true(buflisted(buf))
135
136 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200137 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200138 exe buf . 'bwipe'
139
140 unlet g:job
141endfunc
142
Bram Moolenaar1e115362019-01-09 23:01:02 +0100143func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200144 exe g:buf . 'bwipe!'
145 let g:buf = 0
146endfunc
147
Bram Moolenaar9d189612017-09-09 18:11:00 +0200148func Get_cat_123_cmd()
149 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100150 if !has('conpty')
151 return 'cmd /c "cls && color 2 && echo 123"'
152 else
153 " When clearing twice, extra sequence is not output.
154 return 'cmd /c "cls && cls && color 2 && echo 123"'
155 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200156 else
157 call writefile(["\<Esc>[32m123"], 'Xtext')
158 return "cat Xtext"
159 endif
160endfunc
161
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200163 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200164 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
165 let g:job = term_getjob(g:buf)
166
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200167 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
168 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200169 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100170 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200171 call delete('Xtext')
172endfunc
173
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200175 let l = term_scrape(a:buf, 0)
176 call assert_true(len(l) == 0)
177 let l = term_scrape(a:buf, 999)
178 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200179 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 call assert_true(len(l) > 0)
181 call assert_equal('1', l[0].chars)
182 call assert_equal('2', l[1].chars)
183 call assert_equal('3', l[2].chars)
184 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200185 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
186 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100187 if has('win32')
188 " On Windows 'background' always defaults to dark, even though the terminal
189 " may use a light background. Therefore accept both white and black.
190 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100192 if &background == 'light'
193 call assert_equal('#ffffff', l[0].bg)
194 else
195 call assert_equal('#000000', l[0].bg)
196 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200197 endif
198
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200199 let l = term_getline(a:buf, -1)
200 call assert_equal('', l)
201 let l = term_getline(a:buf, 0)
202 call assert_equal('', l)
203 let l = term_getline(a:buf, 999)
204 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200205 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200206 call assert_equal('123', l)
207endfunc
208
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200209func Test_terminal_scrape_123()
210 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200211 let buf = term_start(cmd)
212
213 let termlist = term_list()
214 call assert_equal(1, len(termlist))
215 call assert_equal(buf, termlist[0])
216
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200217 " Nothing happens with invalid buffer number
218 call term_wait(1234)
219
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200220 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200221 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200222 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200223 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224 call Check_123(buf)
225
226 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100227 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200228 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200229 call term_wait(buf)
230 call Check_123(buf)
231
232 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200233 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200234endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200235
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200237 call writefile(["léttまrs"], 'Xtext')
238 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200239 " Run cmd with UTF-8 codepage to make the type command print the expected
240 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 let buf = term_start("cmd /K chcp 65001")
242 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200243 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100244 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200245 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let buf = term_start("cat Xtext")
247 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200248 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200249
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100250 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
251 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200252 call assert_true(len(l) >= 7)
253 call assert_equal('l', l[0].chars)
254 call assert_equal('é', l[1].chars)
255 call assert_equal(1, l[1].width)
256 call assert_equal('t', l[2].chars)
257 call assert_equal('t', l[3].chars)
258 call assert_equal('ま', l[4].chars)
259 call assert_equal(2, l[4].width)
260 call assert_equal('r', l[5].chars)
261 call assert_equal('s', l[6].chars)
262
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100265 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100267 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200268 call delete('Xtext')
269endfunc
270
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200271func Test_terminal_scroll()
272 call writefile(range(1, 200), 'Xtext')
273 if has('win32')
274 let cmd = 'cmd /c "type Xtext"'
275 else
276 let cmd = "cat Xtext"
277 endif
278 let buf = term_start(cmd)
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 Moolenaarf8d57a52017-08-07 20:38:42 +0200282 call term_wait(buf)
283 if has('win32')
284 " TODO: this should not be needed
285 sleep 100m
286 endif
287
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200288 let scrolled = buf->term_getscrolled()
289 call assert_equal(scrolled, term_getscrolled(buf))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200290 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200291 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200292 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200293 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200294 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200295 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200296
297 exe buf . 'bwipe'
298 call delete('Xtext')
299endfunc
300
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200301func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200302 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200303 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200304 call writefile(range(150), 'Xtext')
305 if has('win32')
306 call term_sendkeys(buf, "type Xtext\<CR>")
307 else
308 call term_sendkeys(buf, "cat Xtext\<CR>")
309 endif
310 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200311 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200312 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200313 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200314 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200315
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200316 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200317 call term_wait(buf)
318 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200319 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100320 call delete('Xtext')
321endfunc
322
323func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200324 " tail -f only works on Unix
325 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100326
327 call writefile(range(50), 'Xtext')
328 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100329 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100330 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100331 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100332 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
333 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100334 \ 'call feedkeys("\<C-W>N", "xt")',
335 \ ], 'XTest_postponed')
336 let buf = RunVimInTerminal('-S XTest_postponed', {})
337 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar96baf022019-02-14 23:49:38 +0100338 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100339
340 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200341 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100342 call term_sendkeys(buf, "k")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100343 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100344
345 " Back to Terminal-Job mode, text will scroll and show the extra line.
346 call term_sendkeys(buf, "a")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100347 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100348
349 call term_wait(buf)
350 call term_sendkeys(buf, "\<C-C>")
351 call term_wait(buf)
352 call term_sendkeys(buf, "exit\<CR>")
353 call term_wait(buf)
354 call term_sendkeys(buf, ":q\<CR>")
355 call StopVimInTerminal(buf)
356 call delete('XTest_postponed')
357 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200358endfunc
359
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100360" Run diff on two dumps with different size.
361func Test_terminal_dumpdiff_size()
362 call assert_equal(1, winnr('$'))
363 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
364 call assert_equal(2, winnr('$'))
365 call assert_match('Test_incsearch_search_01.dump', getline(10))
366 call assert_match(' +++++$', getline(11))
367 call assert_match('Test_popup_command_01.dump', getline(31))
368 call assert_equal(repeat('+', 75), getline(30))
369 quit
370endfunc
371
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200372func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200373 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200374
Bram Moolenaarb2412082017-08-20 18:09:14 +0200375 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200376 let size = term_getsize('')
377 bwipe!
378 call assert_equal(5, size[0])
379
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200380 call term_start(cmd, {'term_rows': 6})
381 let size = term_getsize('')
382 bwipe!
383 call assert_equal(6, size[0])
384
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200385 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200386 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200387 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200388
389 call term_setsize('', 6, 0)
390 call assert_equal([6, 33], term_getsize(''))
391
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200392 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200393 call assert_equal([6, 35], term_getsize(''))
394
395 call term_setsize('', 7, 30)
396 call assert_equal([7, 30], term_getsize(''))
397
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200398 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200399 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200400
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200401 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
402 let size = term_getsize('')
403 bwipe!
404 call assert_equal([6, 36], size)
405
Bram Moolenaarb2412082017-08-20 18:09:14 +0200406 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200407 let size = term_getsize('')
408 bwipe!
409 call assert_equal(20, size[1])
410
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200411 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200412 let size = term_getsize('')
413 bwipe!
414 call assert_equal(26, size[1])
415
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200416 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200417 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200418 let size = term_getsize('')
419 bwipe!
420 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200421
422 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
423 let size = term_getsize('')
424 bwipe!
425 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200426
427 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200428endfunc
429
430func Test_terminal_curwin()
431 let cmd = Get_cat_123_cmd()
432 call assert_equal(1, winnr('$'))
433
434 split dummy
435 exe 'terminal ++curwin ' . cmd
436 call assert_equal(2, winnr('$'))
437 bwipe!
438
439 split dummy
440 call term_start(cmd, {'curwin': 1})
441 call assert_equal(2, winnr('$'))
442 bwipe!
443
444 split dummy
445 call setline(1, 'change')
446 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
447 call assert_equal(2, winnr('$'))
448 exe 'terminal! ++curwin ' . cmd
449 call assert_equal(2, winnr('$'))
450 bwipe!
451
452 split dummy
453 call setline(1, 'change')
454 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
455 call assert_equal(2, winnr('$'))
456 bwipe!
457
458 split dummy
459 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200460 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200461endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462
Bram Moolenaarff546792017-11-21 14:47:57 +0100463func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200464 if s:python != ''
465 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200466 " 500 was not enough for Travis
467 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200468 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200469 echo 'This will take five seconds...'
470 let waittime = 2000
471 if has('win32')
472 let cmd = $windir . '\system32\timeout.exe 1'
473 else
474 let cmd = 'sleep 1'
475 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200476 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100477 return [cmd, waittime]
478endfunc
479
480func Test_terminal_finish_open_close()
481 call assert_equal(1, winnr('$'))
482
483 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200484
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100485 " shell terminal closes automatically
486 terminal
487 let buf = bufnr('%')
488 call assert_equal(2, winnr('$'))
489 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200490 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200491 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200492 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100493
494 " shell terminal that does not close automatically
495 terminal ++noclose
496 let buf = bufnr('%')
497 call assert_equal(2, winnr('$'))
498 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200499 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200500 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100501 call assert_equal(2, winnr('$'))
502 quit
503 call assert_equal(1, winnr('$'))
504
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200505 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200506 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200507 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200508 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200509
510 call term_start(cmd, {'term_finish': 'close'})
511 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200512 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200513 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200514 call assert_equal(1, winnr('$'))
515
516 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200517 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200518 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200519 bwipe
520
521 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200522 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200523 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200524 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200525
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200526 exe 'terminal ++hidden ++open ' . cmd
527 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200528 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200529 bwipe
530
531 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
532 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200533 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200534 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200535
536 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
537 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
538 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
539 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
540
541 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200542 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200543 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200544 call assert_equal(4, winheight(0))
545 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200546endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200547
548func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200549 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200550 return
551 endif
552 call mkdir('Xdir')
553 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200554 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200555
556 exe buf . 'bwipe'
557 call delete('Xdir', 'rf')
558endfunc
559
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200560func Test_terminal_cwd_failure()
561 " Case 1: Provided directory is not actually a directory. Attempt to make
562 " the file executable as well.
563 call writefile([], 'Xfile')
564 call setfperm('Xfile', 'rwx------')
565 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
566 call delete('Xfile')
567
568 " Case 2: Directory does not exist.
569 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
570
571 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100572 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200573 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100574 call mkdir('XdirNoAccess', '', '0600')
575 " return early if the directory permissions could not be set properly
576 if getfperm('XdirNoAccess')[2] == 'x'
577 call delete('XdirNoAccess', 'rf')
578 return
579 endif
580 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
581 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200582 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200583endfunc
584
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100585func Test_terminal_servername()
586 if !has('clientserver')
587 return
588 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200589 call s:test_environment("VIM_SERVERNAME", v:servername)
590endfunc
591
592func Test_terminal_version()
593 call s:test_environment("VIM_TERMINAL", string(v:version))
594endfunc
595
596func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100597 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100598 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200599 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100600 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200601 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100602 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200603 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100604 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100605 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200606 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200607 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100608
Bram Moolenaar012eb662018-03-13 17:55:27 +0100609 exe buf . 'bwipe'
610 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100611endfunc
612
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200613func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100614 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200615 " 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 Moolenaarba6febd2017-10-30 21:56:23 +0100617 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100618 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100619 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100620 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100621 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200622 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200623 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200624 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200625
Bram Moolenaar012eb662018-03-13 17:55:27 +0100626 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200627endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200628
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200629func Test_terminal_list_args()
630 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
631 call assert_fails(buf . 'bwipe', 'E517')
632 exe buf . 'bwipe!'
633 call assert_equal("", bufname(buf))
634endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200635
636func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100637 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100638 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200639 " The shell or something else has a problem dealing with more than 1000
640 " characters at the same time.
641 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100642 " NPFS is used in Windows, nonblocking mode does not work properly.
643 elseif has('win32')
644 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200645 else
646 let len = 5000
647 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200648
649 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100650 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200651 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100652 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200653
654 " On MS-Windows there is an extra empty line below "done". Find "done" in
655 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100656 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100657 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100658 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200659 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100660 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200661 endif
662 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200663
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100664 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200665 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100666 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200667 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200668 bwipe
669endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200670
671func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200672 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200673 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200674 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200675 if has('win32')
676 " TODO: enable once writing to stdin works on MS-Windows
677 return
678 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200679 new
680 call setline(1, ['one', 'two', 'three'])
681 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200682 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200683 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200684 call assert_equal(['3', '3', '14'], nrs)
685 bwipe
686
Bram Moolenaardada6d22017-09-02 17:18:35 +0200687 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200688 call setline(1, ['one', 'two', 'three', 'four'])
689 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200690 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200691 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200692 call assert_equal(['2', '2', '10'], nrs)
693 bwipe
694
Bram Moolenaardada6d22017-09-02 17:18:35 +0200695 if executable('python')
696 new
697 call setline(1, ['print("hello")'])
698 1term ++eof=exit() python
699 " MS-Windows echoes the input, Unix doesn't.
700 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
701 if getline(1) =~ 'hello'
702 call assert_equal('hello', getline(1))
703 else
704 call assert_equal('hello', getline(line('$') - 1))
705 endif
706 bwipe
707
708 if has('win32')
709 new
710 call setline(1, ['print("hello")'])
711 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200712 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200713 call assert_equal('hello', getline(line('$') - 1))
714 bwipe
715 endif
716 endif
717
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200718 bwipe!
719endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200720
721func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200722 let buf = term_start('NONE', {})
723 call assert_notequal(0, buf)
724
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200725 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200726 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100727 if has('gui_running') && !has('win32')
728 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200729 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100730 else
731 " Otherwise using a job works on all systems.
732 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200733 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200734 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200735
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200736 bwipe!
737endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200738
739func Test_terminal_special_chars()
740 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200741 CheckUnix
742
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200743 call mkdir('Xdir with spaces')
744 call writefile(['x'], 'Xdir with spaces/quoted"file')
745 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200746 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200747 call term_wait('')
748
749 call delete('Xdir with spaces', 'rf')
750 bwipe
751endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200752
753func Test_terminal_wrong_options()
754 call assert_fails('call term_start(&shell, {
755 \ "in_io": "file",
756 \ "in_name": "xxx",
757 \ "out_io": "file",
758 \ "out_name": "xxx",
759 \ "err_io": "file",
760 \ "err_name": "xxx"
761 \ })', 'E474:')
762 call assert_fails('call term_start(&shell, {
763 \ "out_buf": bufnr("%")
764 \ })', 'E474:')
765 call assert_fails('call term_start(&shell, {
766 \ "err_buf": bufnr("%")
767 \ })', 'E474:')
768endfunc
769
770func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200771 let cmd = Get_cat_123_cmd()
772 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
773 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100774 " ConPTY may precede escape sequence. There are things that are not so.
775 if !has('conpty')
776 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
777 call assert_match('123', readfile('Xfile')[0])
778 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200779 let g:job = term_getjob(buf)
780 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
781 call delete('Xfile')
782 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200783
784 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200785 call writefile(['one line'], 'Xfile')
786 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
787 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200788 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200789 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200790 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200791 bwipe
792 call delete('Xfile')
793 endif
794endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200795
796func TerminalTmap(remap)
797 let buf = Run_shell_in_terminal({})
798 call assert_equal('t', mode())
799
800 if a:remap
801 tmap 123 456
802 else
803 tnoremap 123 456
804 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100805 " don't use abcde, it's an existing command
806 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200807 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100808 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200809 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200810 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200811 let lnum = term_getcursor(buf)[0]
812 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100813 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200814 else
815 call assert_match('456', term_getline(buf, lnum))
816 endif
817
818 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200819 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200820 call term_wait(buf)
821
822 tunmap 123
823 tunmap 456
824 call assert_equal('', maparg('123', 't'))
825 close
826 unlet g:job
827endfunc
828
829func Test_terminal_tmap()
830 call TerminalTmap(1)
831 call TerminalTmap(0)
832endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200833
834func Test_terminal_wall()
835 let buf = Run_shell_in_terminal({})
836 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200837 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200838 call term_wait(buf)
839 exe buf . 'bwipe'
840 unlet g:job
841endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200842
Bram Moolenaar7a760922018-02-19 23:10:02 +0100843func Test_terminal_wqall()
844 let buf = Run_shell_in_terminal({})
845 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200846 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100847 call term_wait(buf)
848 exe buf . 'bwipe'
849 unlet g:job
850endfunc
851
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200852func Test_terminal_composing_unicode()
853 let save_enc = &encoding
854 set encoding=utf-8
855
856 if has('win32')
857 let cmd = "cmd /K chcp 65001"
858 let lnum = [3, 6, 9]
859 else
860 let cmd = &shell
861 let lnum = [1, 3, 5]
862 endif
863
864 enew
865 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100866 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200867 call term_wait(buf, 50)
868
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200869 if has('win32')
870 call assert_equal('cmd', job_info(g:job).cmd[0])
871 else
872 call assert_equal(&shell, job_info(g:job).cmd[0])
873 endif
874
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200875 " ascii + composing
876 let txt = "a\u0308bc"
877 call term_sendkeys(buf, "echo " . txt . "\r")
878 call term_wait(buf, 50)
879 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
880 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
881 let l = term_scrape(buf, lnum[0] + 1)
882 call assert_equal("a\u0308", l[0].chars)
883 call assert_equal("b", l[1].chars)
884 call assert_equal("c", l[2].chars)
885
886 " multibyte + composing
887 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
888 call term_sendkeys(buf, "echo " . txt . "\r")
889 call term_wait(buf, 50)
890 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
891 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
892 let l = term_scrape(buf, lnum[1] + 1)
893 call assert_equal("\u304b\u3099", l[0].chars)
894 call assert_equal("\u304e", l[1].chars)
895 call assert_equal("\u304f\u3099", l[2].chars)
896 call assert_equal("\u3052", l[3].chars)
897 call assert_equal("\u3053\u3099", l[4].chars)
898
899 " \u00a0 + composing
900 let txt = "abc\u00a0\u0308"
901 call term_sendkeys(buf, "echo " . txt . "\r")
902 call term_wait(buf, 50)
903 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
904 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
905 let l = term_scrape(buf, lnum[2] + 1)
906 call assert_equal("\u00a0\u0308", l[3].chars)
907
908 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200909 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200910 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100911 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200912 let &encoding = save_enc
913endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100914
915func Test_terminal_aucmd_on_close()
916 fun Nop()
917 let s:called = 1
918 endfun
919
920 aug repro
921 au!
922 au BufWinLeave * call Nop()
923 aug END
924
925 let [cmd, waittime] = s:get_sleep_cmd()
926
927 call assert_equal(1, winnr('$'))
928 new
929 call setline(1, ['one', 'two'])
930 exe 'term ++close ' . cmd
931 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200932 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100933 call assert_equal(1, s:called)
934 bwipe!
935
936 unlet s:called
937 au! repro
938 delfunc Nop
939endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100940
941func Test_terminal_term_start_empty_command()
942 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
943 call assert_fails(cmd, 'E474')
944 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
945 call assert_fails(cmd, 'E474')
946 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
947 call assert_fails(cmd, 'E474')
948 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
949 call assert_fails(cmd, 'E474')
950endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100951
952func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200953 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100954
955 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200956 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100957
Bram Moolenaar086eb872018-03-25 21:24:12 +0200958 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200959 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100960
Bram Moolenaar086eb872018-03-25 21:24:12 +0200961 " Request the cursor position.
962 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100963
964 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200965 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100966
Bram Moolenaar086eb872018-03-25 21:24:12 +0200967 " End "cat" gently.
968 call term_sendkeys(buf, "\<CR>\<C-D>")
969
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200970 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100971 exe buf . 'bwipe'
972 unlet g:job
973endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100974
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100975" Run Vim, start a terminal in that Vim with the kill argument,
976" :qall works.
977func Run_terminal_qall_kill(line1, line2)
978 " 1. Open a terminal window and wait for the prompt to appear
979 " 2. set kill using term_setkill()
980 " 3. make Vim exit, it will kill the shell
981 let after = [
982 \ a:line1,
983 \ 'let buf = bufnr("%")',
984 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
985 \ ' sleep 10m',
986 \ 'endwhile',
987 \ a:line2,
988 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
989 \ 'qall',
990 \ ]
991 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100992 return
993 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100994 call assert_equal("done", readfile("Xdone")[0])
995 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100996endfunc
997
998" Run Vim in a terminal, then start a terminal in that Vim with a kill
999" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001000func Test_terminal_qall_kill_arg()
1001 call Run_terminal_qall_kill('term ++kill=kill', '')
1002endfunc
1003
1004" Run Vim, start a terminal in that Vim, set the kill argument with
1005" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001006func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001007 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001008endfunc
1009
1010" Run Vim, start a terminal in that Vim without the kill argument,
1011" check that :qall does not exit, :qall! does.
1012func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001013 let after =<< trim [CODE]
1014 term
1015 let buf = bufnr("%")
1016 while term_getline(buf, 1) =~ "^\\s*$"
1017 sleep 10m
1018 endwhile
1019 set nomore
1020 au VimLeavePre * call writefile(["too early"], "Xdone")
1021 qall
1022 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1023 cquit
1024 [CODE]
1025
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001026 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001027 return
1028 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001029 call assert_equal("done", readfile("Xdone")[0])
1030 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001031endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001032
1033" Run Vim in a terminal, then start a terminal in that Vim without a kill
1034" argument, check that :confirm qall works.
1035func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001036 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001037 let buf = RunVimInTerminal('', {})
1038
1039 " Open a terminal window and wait for the prompt to appear
1040 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001041 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1042 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001043
1044 " make Vim exit, it will prompt to kill the shell
1045 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001046 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001047 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001048 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001049
1050 " close the terminal window where Vim was running
1051 quit
1052endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001053
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001054" Run Vim in a terminal, then start a terminal window with a shell and check
1055" that Vim exits if it is closed.
1056func Test_terminal_exit()
1057 CheckRunVimInTerminal
1058
1059 let lines =<< trim END
1060 let winid = win_getid()
1061 help
1062 term
1063 let termid = win_getid()
1064 call win_gotoid(winid)
1065 close
1066 call win_gotoid(termid)
1067 END
1068 call writefile(lines, 'XtermExit')
1069 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1070 let job = term_getjob(buf)
1071 call WaitForAssert({-> assert_equal("run", job_status(job))})
1072
1073 " quit the shell, it will make Vim exit
1074 call term_sendkeys(buf, "exit\<CR>")
1075 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1076
1077 call delete('XtermExit')
1078endfunc
1079
Bram Moolenaar012eb662018-03-13 17:55:27 +01001080func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001081 augroup repro
1082 au!
1083 au TerminalOpen * let s:called += 1
1084 augroup END
1085
1086 let s:called = 0
1087
1088 " Open a terminal window with :terminal
1089 terminal
1090 call assert_equal(1, s:called)
1091 bwipe!
1092
1093 " Open a terminal window with term_start()
1094 call term_start(&shell)
1095 call assert_equal(2, s:called)
1096 bwipe!
1097
1098 " Open a hidden terminal buffer with :terminal
1099 terminal ++hidden
1100 call assert_equal(3, s:called)
1101 for buf in term_list()
1102 exe buf . "bwipe!"
1103 endfor
1104
1105 " Open a hidden terminal buffer with term_start()
1106 let buf = term_start(&shell, {'hidden': 1})
1107 call assert_equal(4, s:called)
1108 exe buf . "bwipe!"
1109
1110 unlet s:called
1111 au! repro
1112endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001113
1114func Check_dump01(off)
1115 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1116 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001117 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001118endfunc
1119
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001120func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001121 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001122 let save_enc = &encoding
1123 set encoding=utf-8
1124 call assert_equal(1, winnr('$'))
1125
1126 let text = " a\u0300 e\u0302 o\u0308"
1127 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001128 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001129 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001130 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001131 let dumpline = readfile('Xdump')[0]
1132 call assert_match('|à| |ê| |ö', dumpline)
1133
1134 call StopVimInTerminal(buf)
1135 call delete('Xcomposing')
1136 call delete('Xdump')
1137 let &encoding = save_enc
1138endfunc
1139
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001140" just testing basic functionality.
1141func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001142 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001143 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001144 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001145 call assert_equal(2, winnr('$'))
1146 call assert_equal(20, line('$'))
1147 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001148
1149 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001150 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001151 call assert_equal(buf, buf2)
1152 call assert_notequal('one two three four five', trim(getline(1)))
1153
1154 " Load the first dump again in the same window
1155 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1156 call assert_equal(buf, buf2)
1157 call Check_dump01(0)
1158
1159 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1160 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1161 new
1162 let closedbuf = winbufnr('')
1163 quit
1164 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1165
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001166 quit
1167endfunc
1168
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001169func Test_terminal_dumpload_dump()
1170 CheckRunVimInTerminal
1171
1172 let lines =<< trim END
1173 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1174 END
1175 call writefile(lines, 'XtermDumpload')
1176 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1177 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1178
1179 call StopVimInTerminal(buf)
1180 call delete('XtermDumpload')
1181endfunc
1182
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001183func Test_terminal_dumpdiff()
1184 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001185 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001186 call assert_equal(2, winnr('$'))
1187 call assert_equal(62, line('$'))
1188 call Check_dump01(0)
1189 call Check_dump01(42)
1190 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1191 quit
1192endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001193
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001194func Test_terminal_dumpdiff_swap()
1195 call assert_equal(1, winnr('$'))
1196 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1197 call assert_equal(2, winnr('$'))
1198 call assert_equal(62, line('$'))
1199 call assert_match('Test_popup_command_01.dump', getline(21))
1200 call assert_match('Test_popup_command_03.dump', getline(42))
1201 call assert_match('Undo', getline(3))
1202 call assert_match('three four five', getline(45))
1203
1204 normal s
1205 call assert_match('Test_popup_command_03.dump', getline(21))
1206 call assert_match('Test_popup_command_01.dump', getline(42))
1207 call assert_match('three four five', getline(3))
1208 call assert_match('Undo', getline(45))
1209 quit
1210endfunc
1211
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001212func Test_terminal_dumpdiff_options()
1213 set laststatus=0
1214 call assert_equal(1, winnr('$'))
1215 let height = winheight(0)
1216 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1217 call assert_equal(2, winnr('$'))
1218 call assert_equal(height, winheight(winnr()))
1219 call assert_equal(33, winwidth(winnr()))
1220 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1221 quit
1222
1223 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001224 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1225 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001226 call assert_equal(&columns, winwidth(0))
1227 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001228 call assert_equal('something else', bufname('%'))
1229 quit
1230
1231 call assert_equal(1, winnr('$'))
1232 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1233 call assert_equal(1, winnr('$'))
1234 bwipe
1235
1236 set laststatus&
1237endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001238
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001239func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001240 call assert_equal(1, winnr('$'))
1241
1242 " Use the title termcap entries to output the escape sequence.
1243 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001244 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001245 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001246 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001247 \ 'redraw',
1248 \ "set t_ts=",
1249 \ ], 'Xscript')
1250 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001251 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001252 call assert_equal('Xtextfile', expand('%:t'))
1253 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001254 return buf
1255endfunc
1256
1257func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001258 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001259 let buf = Api_drop_common('')
1260 call assert_equal(0, &bin)
1261 call assert_equal('', &fenc)
1262
1263 call StopVimInTerminal(buf)
1264 call delete('Xscript')
1265 bwipe Xtextfile
1266endfunc
1267
1268func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001269 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001270 let buf = Api_drop_common(',{"bin":1}')
1271 call assert_equal(1, &bin)
1272
1273 call StopVimInTerminal(buf)
1274 call delete('Xscript')
1275 bwipe Xtextfile
1276endfunc
1277
1278func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001279 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001280 let buf = Api_drop_common(',{"binary":1}')
1281 call assert_equal(1, &bin)
1282
1283 call StopVimInTerminal(buf)
1284 call delete('Xscript')
1285 bwipe Xtextfile
1286endfunc
1287
1288func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001289 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001290 set binary
1291 let buf = Api_drop_common(',{"nobin":1}')
1292 call assert_equal(0, &bin)
1293
1294 call StopVimInTerminal(buf)
1295 call delete('Xscript')
1296 bwipe Xtextfile
1297 set nobinary
1298endfunc
1299
1300func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001301 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001302 set binary
1303 let buf = Api_drop_common(',{"nobinary":1}')
1304 call assert_equal(0, &bin)
1305
1306 call StopVimInTerminal(buf)
1307 call delete('Xscript')
1308 bwipe Xtextfile
1309 set nobinary
1310endfunc
1311
1312func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001313 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001314 let buf = Api_drop_common(',{"ff":"dos"}')
1315 call assert_equal("dos", &ff)
1316
1317 call StopVimInTerminal(buf)
1318 call delete('Xscript')
1319 bwipe Xtextfile
1320endfunc
1321
1322func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001323 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001324 let buf = Api_drop_common(',{"fileformat":"dos"}')
1325 call assert_equal("dos", &ff)
1326
1327 call StopVimInTerminal(buf)
1328 call delete('Xscript')
1329 bwipe Xtextfile
1330endfunc
1331
1332func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001333 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001334 let buf = Api_drop_common(',{"enc":"utf-16"}')
1335 call assert_equal("utf-16", &fenc)
1336
1337 call StopVimInTerminal(buf)
1338 call delete('Xscript')
1339 bwipe Xtextfile
1340endfunc
1341
1342func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001343 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001344 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1345 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001346
1347 call StopVimInTerminal(buf)
1348 call delete('Xscript')
1349 bwipe Xtextfile
1350endfunc
1351
1352func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001353 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001354 let firstwinid = win_getid()
1355 split Xtextfile
1356 let textfile_winid = win_getid()
1357 call assert_equal(2, winnr('$'))
1358 call win_gotoid(firstwinid)
1359
1360 " Use the title termcap entries to output the escape sequence.
1361 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001362 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001363 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1364 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1365 \ 'redraw',
1366 \ "set t_ts=",
1367 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001368 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001369 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001370 call assert_equal(textfile_winid, win_getid())
1371
1372 call StopVimInTerminal(buf)
1373 call delete('Xscript')
1374 bwipe Xtextfile
1375endfunc
1376
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001377func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001378 let g:called_bufnum = a:bufnum
1379 let g:called_arg = a:arg
1380endfunc
1381
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001382func WriteApiCall(funcname)
1383 " Use the title termcap entries to output the escape sequence.
1384 call writefile([
1385 \ 'set title',
1386 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1387 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1388 \ 'redraw',
1389 \ "set t_ts=",
1390 \ ], 'Xscript')
1391endfunc
1392
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001393func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001394 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001395
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001396 unlet! g:called_bufnum
1397 unlet! g:called_arg
1398
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001399 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001400
1401 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001402 let buf = RunVimInTerminal('-S Xscript', {})
1403 call WaitFor({-> exists('g:called_bufnum')})
1404 call assert_equal(buf, g:called_bufnum)
1405 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001406 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001407
1408 unlet! g:called_bufnum
1409 unlet! g:called_arg
1410
1411 " Enable explicitly
1412 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1413 call WaitFor({-> exists('g:called_bufnum')})
1414 call assert_equal(buf, g:called_bufnum)
1415 call assert_equal(['hello', 123], g:called_arg)
1416 call StopVimInTerminal(buf)
1417
1418 unlet! g:called_bufnum
1419 unlet! g:called_arg
1420
1421 func! ApiCall_TryThis(bufnum, arg)
1422 let g:called_bufnum2 = a:bufnum
1423 let g:called_arg2 = a:arg
1424 endfunc
1425
1426 call WriteApiCall('ApiCall_TryThis')
1427
1428 " Use prefix match
1429 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1430 call WaitFor({-> exists('g:called_bufnum2')})
1431 call assert_equal(buf, g:called_bufnum2)
1432 call assert_equal(['hello', 123], g:called_arg2)
1433 call StopVimInTerminal(buf)
1434
1435 unlet! g:called_bufnum2
1436 unlet! g:called_arg2
1437
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001438 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001439 delfunction! ApiCall_TryThis
1440 unlet! g:called_bufnum2
1441 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001442endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001443
1444func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001445 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001446
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001447 func! TryThis(bufnum, arg)
1448 let g:called_bufnum3 = a:bufnum
1449 let g:called_arg3 = a:arg
1450 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001451
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001452 call WriteApiCall('TryThis')
1453
1454 unlet! g:called_bufnum3
1455 unlet! g:called_arg3
1456
1457 " Not permitted
1458 call ch_logfile('Xlog', 'w')
1459 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1460 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1461 call assert_false(exists('g:called_bufnum3'))
1462 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001463 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001464
1465 " No match
1466 call ch_logfile('Xlog', 'w')
1467 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1468 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1469 call assert_false(exists('g:called_bufnum3'))
1470 call assert_false(exists('g:called_arg3'))
1471 call StopVimInTerminal(buf)
1472
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001473 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001474 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001475 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001476 delfunction! TryThis
1477 unlet! g:called_bufnum3
1478 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001479endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001480
Bram Moolenaara997b452018-04-17 23:24:06 +02001481let s:caught_e937 = 0
1482
1483func Tapi_Delete(bufnum, arg)
1484 try
1485 execute 'bdelete!' a:bufnum
1486 catch /E937:/
1487 let s:caught_e937 = 1
1488 endtry
1489endfunc
1490
1491func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001492 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001493
1494 call WriteApiCall('Tapi_Delete')
1495 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001496 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001497
1498 call StopVimInTerminal(buf)
1499 call delete('Xscript')
1500 call ch_logfile('', '')
1501endfunc
1502
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001503func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001504 if !exists('*term_getansicolors')
1505 throw 'Skipped: term_getansicolors() not supported'
1506 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001507 let colors = [
1508 \ '#000000', '#e00000',
1509 \ '#00e000', '#e0e000',
1510 \ '#0000e0', '#e000e0',
1511 \ '#00e0e0', '#e0e0e0',
1512 \ '#808080', '#ff4040',
1513 \ '#40ff40', '#ffff40',
1514 \ '#4040ff', '#ff40ff',
1515 \ '#40ffff', '#ffffff',
1516 \]
1517
1518 let buf = Run_shell_in_terminal({})
1519 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001520 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001521 call term_wait(buf)
1522
1523 exe buf . 'bwipe'
1524endfunc
1525
1526let s:test_colors = [
1527 \ '#616e64', '#0d0a79',
1528 \ '#6d610d', '#0a7373',
1529 \ '#690d0a', '#6d696e',
1530 \ '#0d0a6f', '#616e0d',
1531 \ '#0a6479', '#6d0d0a',
1532 \ '#617373', '#0d0a69',
1533 \ '#6d690d', '#0a6e6f',
1534 \ '#610d0a', '#6e6479',
1535 \]
1536
1537func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001538 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001539 if !exists('*term_getansicolors')
1540 throw 'Skipped: term_getansicolors() not supported'
1541 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001542 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1543 let buf = Run_shell_in_terminal({})
1544 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001545 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001546 call term_wait(buf)
1547
1548 exe buf . 'bwipe'
1549 unlet g:terminal_ansi_colors
1550endfunc
1551
1552func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001553 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001554 if !exists('*term_getansicolors')
1555 throw 'Skipped: term_getansicolors() not supported'
1556 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001557 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1558 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1559 call assert_equal(s:test_colors, term_getansicolors(buf))
1560
1561 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001562 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001563
1564 let colors = [
1565 \ 'ivory', 'AliceBlue',
1566 \ 'grey67', 'dark goldenrod',
1567 \ 'SteelBlue3', 'PaleVioletRed4',
1568 \ 'MediumPurple2', 'yellow2',
1569 \ 'RosyBrown3', 'OrangeRed2',
1570 \ 'white smoke', 'navy blue',
1571 \ 'grey47', 'gray97',
1572 \ 'MistyRose2', 'DodgerBlue4',
1573 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001574 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001575
1576 let colors[4] = 'Invalid'
1577 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1578
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001579 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001580 call term_wait(buf)
1581 exe buf . 'bwipe'
1582endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001583
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001584func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001585 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001586
1587 " Use all the ANSI colors.
1588 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001589 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001590 \ 'hi Tblack ctermfg=0 ctermbg=8',
1591 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1592 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1593 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1594 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1595 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1596 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1597 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1598 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1599 \ 'hi Tred ctermfg=9 ctermbg=1',
1600 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1601 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1602 \ 'hi Tblue ctermfg=12 ctermbg=4',
1603 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1604 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1605 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001606 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1607 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1608 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001609 \ '',
1610 \ 'call matchadd("Tblack", "A")',
1611 \ 'call matchadd("Tdarkred", "B")',
1612 \ 'call matchadd("Tdarkgreen", "C")',
1613 \ 'call matchadd("Tbrown", "D")',
1614 \ 'call matchadd("Tdarkblue", "E")',
1615 \ 'call matchadd("Tdarkmagenta", "F")',
1616 \ 'call matchadd("Tdarkcyan", "G")',
1617 \ 'call matchadd("Tlightgrey", "H")',
1618 \ 'call matchadd("Tdarkgrey", "I")',
1619 \ 'call matchadd("Tred", "J")',
1620 \ 'call matchadd("Tgreen", "K")',
1621 \ 'call matchadd("Tyellow", "L")',
1622 \ 'call matchadd("Tblue", "M")',
1623 \ 'call matchadd("Tmagenta", "N")',
1624 \ 'call matchadd("Tcyan", "O")',
1625 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001626 \ 'call matchadd("TdarkredBold", "X")',
1627 \ 'call matchadd("TgreenBold", "Y")',
1628 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001629 \ 'redraw',
1630 \ ], 'Xcolorscript')
1631 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1632 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1633
1634 call term_sendkeys(buf, ":q\<CR>")
1635 call StopVimInTerminal(buf)
1636 call delete('Xcolorscript')
1637endfunc
1638
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001639func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001640 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001641 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001642 let text = []
1643 for n in range(10)
1644 call add(text, repeat(n, 50))
1645 endfor
1646 call writefile(text, 'Xwinsize')
1647 let buf = RunVimInTerminal('Xwinsize', {})
1648 let win = bufwinid(buf)
1649 call assert_equal([6, 40], term_getsize(buf))
1650 call assert_equal(6, winheight(win))
1651 call assert_equal(40, winwidth(win))
1652
1653 " resizing the window doesn't resize the terminal.
1654 resize 10
1655 vertical resize 60
1656 call assert_equal([6, 40], term_getsize(buf))
1657 call assert_equal(10, winheight(win))
1658 call assert_equal(60, winwidth(win))
1659
1660 call StopVimInTerminal(buf)
1661 call delete('Xwinsize')
1662
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001663 call assert_fails('set termwinsize=40', 'E474')
1664 call assert_fails('set termwinsize=10+40', 'E474')
1665 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001666
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001667 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001668endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001669
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001670func Test_terminal_termwinsize_option_zero()
1671 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001672 let buf = Run_shell_in_terminal({})
1673 let win = bufwinid(buf)
1674 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001675 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001676 call term_wait(buf)
1677 exe buf . 'bwipe'
1678
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001679 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001680 let buf = Run_shell_in_terminal({})
1681 let win = bufwinid(buf)
1682 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001683 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001684 call term_wait(buf)
1685 exe buf . 'bwipe'
1686
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001687 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001688 let buf = Run_shell_in_terminal({})
1689 let win = bufwinid(buf)
1690 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001691 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001692 call term_wait(buf)
1693 exe buf . 'bwipe'
1694
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001695 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001696endfunc
1697
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001698func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001699 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001700 vsplit
1701 let buf = Run_shell_in_terminal({})
1702 let win = bufwinid(buf)
1703 call assert_inrange(10, 1000, winheight(win))
1704 call assert_inrange(50, 1000, winwidth(win))
1705 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1706
1707 resize 15
1708 vertical resize 60
1709 redraw
1710 call assert_equal([15, 60], term_getsize(buf))
1711 call assert_equal(15, winheight(win))
1712 call assert_equal(60, winwidth(win))
1713
1714 resize 7
1715 vertical resize 30
1716 redraw
1717 call assert_equal([10, 50], term_getsize(buf))
1718 call assert_equal(7, winheight(win))
1719 call assert_equal(30, winwidth(win))
1720
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001721 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001722 call term_wait(buf)
1723 exe buf . 'bwipe'
1724
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001725 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001726 let buf = Run_shell_in_terminal({})
1727 let win = bufwinid(buf)
1728 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001729 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001730 call term_wait(buf)
1731 exe buf . 'bwipe'
1732
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001733 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001734endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001735
1736func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001737 " make three tabpages, terminal in the middle
1738 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001739 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001740 tabnew
1741 tabprev
1742 call assert_equal(1, winnr('$'))
1743 call assert_equal(2, tabpagenr())
1744 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001745
1746 let buf = Run_shell_in_terminal({})
1747 let termwin = bufwinid(buf)
1748 set termwinkey=<C-L>
1749 call feedkeys("\<C-L>w", 'tx')
1750 call assert_equal(thiswin, win_getid())
1751 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001752 call assert_equal(termwin, win_getid())
1753
Bram Moolenaar997d4242019-09-14 21:23:40 +02001754 if has('langmap')
1755 set langmap=xjyk
1756 call feedkeys("\<C-L>x", 'tx')
1757 call assert_equal(thiswin, win_getid())
1758 call feedkeys("\<C-W>y", 'tx')
1759 call assert_equal(termwin, win_getid())
1760 set langmap=
1761 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001762
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001763 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001764 call assert_equal(3, tabpagenr())
1765 tabprev
1766 call assert_equal(2, tabpagenr())
1767 call assert_equal(termwin, win_getid())
1768
1769 call feedkeys("\<C-L>gT", "xt")
1770 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001771 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001772 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001773 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001774
1775 let job = term_getjob(buf)
1776 call feedkeys("\<C-L>\<C-C>", 'tx')
1777 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001778
1779 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001780 tabnext
1781 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001782 tabprev
1783 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001784endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001785
1786func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001787 CheckUnix
1788
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001789 call writefile([
1790 \ '#!/bin/sh',
1791 \ 'echo "this is standard error" >&2',
1792 \ 'echo "this is standard out" >&1',
1793 \ ], 'Xechoerrout.sh')
1794 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1795
1796 let outfile = 'Xtermstdout'
1797 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001798
1799 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1800 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001801 call assert_equal('this is standard error', term_getline(buf, 1))
1802
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001803 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001804 exe buf . 'bwipe'
1805 call delete('Xechoerrout.sh')
1806 call delete(outfile)
1807endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001808
1809func Test_terminwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001810 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001811
1812 " Let the terminal output more than 'termwinscroll' lines, some at the start
1813 " will be dropped.
1814 exe 'set termwinscroll=' . &lines
1815 let buf = term_start('/bin/sh')
1816 for i in range(1, &lines)
1817 call feedkeys("echo " . i . "\<CR>", 'xt')
1818 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1819 endfor
1820 " Go to Terminal-Normal mode to update the buffer.
1821 call feedkeys("\<C-W>N", 'xt')
1822 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1823
1824 " Every "echo nr" must only appear once
1825 let lines = getline(1, line('$'))
1826 for i in range(&lines - len(lines) / 2 + 2, &lines)
1827 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1828 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1829 endfor
1830
1831 exe buf . 'bwipe!'
1832endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001833
Bram Moolenaar875cf872018-07-08 20:49:07 +02001834" Resizing the terminal window caused an ml_get error.
1835" TODO: This does not reproduce the original problem.
1836func Test_terminal_resize()
1837 set statusline=x
1838 terminal
1839 call assert_equal(2, winnr('$'))
1840
1841 " Fill the terminal with text.
1842 if has('win32')
1843 call feedkeys("dir\<CR>", 'xt')
1844 else
1845 call feedkeys("ls\<CR>", 'xt')
1846 endif
1847 " Go to Terminal-Normal mode for a moment.
1848 call feedkeys("\<C-W>N", 'xt')
1849 " Open a new window
1850 call feedkeys("i\<C-W>n", 'xt')
1851 call assert_equal(3, winnr('$'))
1852 redraw
1853
1854 close
1855 call assert_equal(2, winnr('$'))
1856 call feedkeys("exit\<CR>", 'xt')
1857 set statusline&
1858endfunc
1859
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001860" must be nearly the last, we can't go back from GUI to terminal
1861func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001862 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001863
1864 " Ignore the "failed to create input context" error.
1865 call test_ignore_error('E285:')
1866
1867 gui -f
1868
1869 call assert_equal(1, winnr('$'))
1870 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001871 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001872 call term_wait(buf)
1873
1874 " closing window wipes out the terminal buffer a with finished job
1875 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1876 call assert_equal("", bufname(buf))
1877
1878 unlet g:job
1879endfunc
1880
1881func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001882 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001883 set guioptions+=!
1884
1885 let filename = 'Xtestscript'
1886 if has('win32')
1887 let filename .= '.bat'
1888 let prefix = ''
1889 let contents = ['@echo off', 'exit %1']
1890 else
1891 let filename .= '.sh'
1892 let prefix = './'
1893 let contents = ['#!/bin/sh', 'exit $1']
1894 endif
1895 call writefile(contents, filename)
1896 call setfperm(filename, 'rwxrwx---')
1897
1898 " Check if v:shell_error is equal to the exit status.
1899 let exitval = 0
1900 execute printf(':!%s%s %d', prefix, filename, exitval)
1901 call assert_equal(exitval, v:shell_error)
1902
1903 let exitval = 9
1904 execute printf(':!%s%s %d', prefix, filename, exitval)
1905 call assert_equal(exitval, v:shell_error)
1906
1907 set guioptions&
1908 call delete(filename)
1909endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001910
1911func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001912 CheckUnix
1913
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001914 term ++hidden cat
1915 let bnr = bufnr('$')
1916 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1917 exe 'sbuf ' . bnr
1918 call assert_equal('terminal', &buftype)
1919 call term_sendkeys(bnr, "asdf\<CR>")
1920 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1921 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001922 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001923 bwipe!
1924endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001925
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001926func Test_terminal_switch_mode()
1927 term
1928 let bnr = bufnr('$')
1929 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1930 call feedkeys("\<C-W>N", 'xt')
1931 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1932 call feedkeys("A", 'xt')
1933 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1934 call feedkeys("\<C-W>N", 'xt')
1935 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1936 call feedkeys("I", 'xt')
1937 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1938 call feedkeys("\<C-W>Nv", 'xt')
1939 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1940 call feedkeys("I", 'xt')
1941 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1942 call feedkeys("\<C-W>Nv", 'xt')
1943 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1944 call feedkeys("A", 'xt')
1945 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1946 bwipe!
1947endfunc
1948
Bram Moolenaara3817732019-10-16 16:31:44 +02001949func Test_terminal_normal_mode()
1950 CheckRunVimInTerminal
1951
1952 " Run Vim in a terminal and open a terminal window to run Vim in.
1953 let lines =<< trim END
1954 call setline(1, range(11111, 11122))
1955 3
1956 END
1957 call writefile(lines, 'XtermNormal')
1958 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
1959 call term_wait(buf)
1960
1961 call term_sendkeys(buf, "\<C-W>N")
1962 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
1963 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
1964
1965 call term_sendkeys(buf, ":set culopt=number\r")
1966 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
1967
1968 call term_sendkeys(buf, ":set culopt=line\r")
1969 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
1970
1971 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
1972 call StopVimInTerminal(buf)
1973 call delete('XtermNormal')
1974endfunc
1975
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001976func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001977 CheckUnix
1978
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001979 call assert_equal(1, winnr('$'))
1980 term ++hidden ++close ls
1981 let bnr = bufnr('$')
1982 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1983 call WaitForAssert({-> assert_false(bufexists(bnr))})
1984 call assert_equal(1, winnr('$'))
1985endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001986
1987func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001988 " This test does not pass through ConPTY.
1989 if has('conpty')
1990 return
1991 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001992 let contents = [
1993 \ [ 'One', '', 'X' ],
1994 \ [ 'Two', '', '' ],
1995 \ [ 'Three' ] + repeat([''], 30)
1996 \ ]
1997
1998 for c in contents
1999 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002000 if has('win32')
2001 term cmd /c type Xfile
2002 else
2003 term cat Xfile
2004 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002005 let bnr = bufnr('$')
2006 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2007 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002008 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002009 call assert_equal(c, getline(1, line('$')))
2010 quit
2011 endfor
2012
2013 call delete('Xfile')
2014endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002015
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002016func Test_terminal_no_job()
2017 let term = term_start('false', {'term_finish': 'close'})
2018 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2019endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002020
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002021func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002022 CheckUnix
2023
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002024 let buf = Run_shell_in_terminal({})
2025
2026 " Wait for the shell to display a prompt.
2027 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2028
2029 " Hide the cursor.
2030 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2031 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2032
2033 " Show the cursor.
2034 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002035 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002036
2037 " Change color of cursor.
2038 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2039 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2040 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2041 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2042 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2043
2044 " Make cursor a blinking block.
2045 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2046 call WaitForAssert({-> assert_equal([1, 1],
2047 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2048
2049 " Make cursor a steady block.
2050 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2051 call WaitForAssert({-> assert_equal([0, 1],
2052 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2053
2054 " Make cursor a blinking underline.
2055 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2056 call WaitForAssert({-> assert_equal([1, 2],
2057 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2058
2059 " Make cursor a steady underline.
2060 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2061 call WaitForAssert({-> assert_equal([0, 2],
2062 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2063
2064 " Make cursor a blinking vertical bar.
2065 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2066 call WaitForAssert({-> assert_equal([1, 3],
2067 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2068
2069 " Make cursor a steady vertical bar.
2070 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2071 call WaitForAssert({-> assert_equal([0, 3],
2072 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2073
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002074 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002075endfunc
2076
Bram Moolenaar1e115362019-01-09 23:01:02 +01002077func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002078 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002079 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002080 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002081 call assert_equal('', term_gettitle(bufnr('$') + 1))
2082
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002083 if !has('title') || &title == 0 || empty(&t_ts)
2084 throw "Skipped: can't get/set title"
2085 endif
2086
Bram Moolenaar1e115362019-01-09 23:01:02 +01002087 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002088 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002089 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2090 call term_sendkeys(term, ":e Xfoo\r")
2091 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002092 else
2093 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002094 call term_sendkeys(term, ":e Xfoo\r")
2095 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002096 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002097
Bram Moolenaar1e115362019-01-09 23:01:02 +01002098 call term_sendkeys(term, ":set titlestring=foo\r")
2099 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2100
2101 exe term . 'bwipe!'
2102endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002103
2104" When drawing the statusline the cursor position may not have been updated
2105" yet.
2106" 1. create a terminal, make it show 2 lines
2107" 2. 0.5 sec later: leave terminal window, execute "i"
2108" 3. 0.5 sec later: clear terminal window, now it's 1 line
2109" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2110" 4. 0.5 sec later: should be done, clean up
2111func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002112 CheckUnix
2113
Bram Moolenaar10772302019-01-20 18:25:54 +01002114 set statusline=x
2115 terminal
2116 let tbuf = bufnr('')
2117 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2118 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2119 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2120 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2121
2122 sleep 2
2123 exe tbuf . 'bwipe!'
2124 au! BufLeave
2125 set statusline=
2126endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002127
2128func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002129 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002130
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002131 " split, go to the bottom-right window
2132 split
2133 wincmd j
2134 set splitright
2135
2136 call writefile([
2137 \ 'echo getwinpos()',
2138 \ ], 'XTest_getwinpos')
2139 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2140 call term_wait(buf)
2141
2142 " Find the output of getwinpos() in the bottom line.
2143 let rows = term_getsize(buf)[0]
2144 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2145 let line = term_getline(buf, rows)
2146 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2147 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2148
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002149 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002150 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002151 " In the GUI it can be more, let's assume a 20 x 14 cell.
2152 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002153 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002154 let winpos = 50->getwinpos()
2155 call assert_equal(xroot, winpos[0])
2156 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002157 let [winrow, wincol] = win_screenpos('.')
2158 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2159 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2160 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2161 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002162
2163 call term_wait(buf)
2164 call term_sendkeys(buf, ":q\<CR>")
2165 call StopVimInTerminal(buf)
2166 call delete('XTest_getwinpos')
2167 exe buf . 'bwipe!'
2168 set splitright&
2169 only!
2170endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002171
2172func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002173 " somehow doesn't work on MS-Windows
2174 CheckUnix
2175 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002176
2177 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002178 call writefile(["\<Esc>[?1047h"], 'Xtext')
2179 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002180 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2181
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002182 call writefile(["\<Esc>[?1047l"], 'Xtext')
2183 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002184 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002185
2186 call term_sendkeys(buf, "exit\r")
2187 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002188 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002189endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002190
2191func Test_terminal_setapi_and_call()
2192 if !CanRunVimInTerminal()
2193 return
2194 endif
2195
2196 call WriteApiCall('Tapi_TryThis')
2197 call ch_logfile('Xlog', 'w')
2198
2199 unlet! g:called_bufnum
2200 unlet! g:called_arg
2201
2202 let buf = RunVimInTerminal('-S Xscript', {'term_api': 0})
2203 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2204 call assert_false(exists('g:called_bufnum'))
2205 call assert_false(exists('g:called_arg'))
2206
2207 call term_setapi(buf, 'Tapi_TryThis')
2208 call term_sendkeys(buf, ":set notitle\<CR>")
2209 call term_sendkeys(buf, ":source Xscript\<CR>")
2210 call WaitFor({-> exists('g:called_bufnum')})
2211 call assert_equal(buf, g:called_bufnum)
2212 call assert_equal(['hello', 123], g:called_arg)
2213 call StopVimInTerminal(buf)
2214
2215 call delete('Xscript')
2216 call ch_logfile('')
2217 call delete('Xlog')
2218 unlet! g:called_bufnum
2219 unlet! g:called_arg
2220endfunc