blob: 7476b49959893f41d97b3102387899430db50d25 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02002" This is split in two, because it can take a lot of time.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02003" See test_terminal2.vim and test_terminal3.vim for further tests.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02005source check.vim
6CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02007
8source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01009source screendump.vim
Bram Moolenaar91689ea2020-05-11 22:04:53 +020010source mouse.vim
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020011source term_util.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020012
Bram Moolenaarb81bc772017-08-11 22:45:01 +020013let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020014let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020015
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020016func Test_terminal_basic()
ichizokae1bd872022-01-20 14:57:29 +000017 call test_override('vterm_title', 1)
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020018 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020019 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020020
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020021 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020022 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020023 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020024 call assert_match('%aR[^\n]*running]', execute('ls R'))
25 call assert_notmatch('%[^\n]*running]', execute('ls F'))
26 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar004a6782020-04-11 17:09:31 +020027 call assert_fails('set modifiable', 'E946:')
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020028
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020029 call StopShellInTerminal(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020030 call assert_equal('n', mode())
31 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020032 call assert_match('%aF[^\n]*finished]', execute('ls F'))
33 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
34 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035
Bram Moolenaar94053a52017-08-01 21:44:33 +020036 " closing window wipes out the terminal buffer a with finished job
37 close
38 call assert_equal("", bufname(buf))
39
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020040 au! TerminalOpen
ichizokae1bd872022-01-20 14:57:29 +000041 call test_override('ALL', 0)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020042 unlet g:job
43endfunc
44
Bram Moolenaar00806bc2020-11-05 19:36:38 +010045func Test_terminal_no_name()
46 let buf = Run_shell_in_terminal({})
47 call assert_match('^!', bufname(buf))
48 0file
49 call assert_equal("", bufname(buf))
50 call assert_match('\[No Name\]', execute('file'))
51 call StopShellInTerminal(buf)
Bram Moolenaar00806bc2020-11-05 19:36:38 +010052endfunc
53
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020054func Test_terminal_TerminalWinOpen()
55 au TerminalWinOpen * let b:done = 'yes'
56 let buf = Run_shell_in_terminal({})
57 call assert_equal('yes', b:done)
58 call StopShellInTerminal(buf)
59 " closing window wipes out the terminal buffer with the finished job
60 close
61
62 if has("unix")
Yee Cheng Chine70587d2025-02-13 20:55:45 +010063 terminal ++hidden ++open echo
64 call WaitForAssert({-> assert_equal('terminal', &buftype)})
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020065 call assert_fails("echo b:done", 'E121:')
66 endif
67
68 au! TerminalWinOpen
69endfunc
70
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071func 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
75 setlocal modifiable
76 exe "normal Axxx\<Esc>"
Bram Moolenaar28ee8922020-10-28 20:20:00 +010077 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020078 undo
79
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020080 exe buf . 'bwipe'
81 unlet g:job
82endfunc
83
Bram Moolenaar5b868a82019-02-25 06:11:53 +010084func Test_terminal_paste_register()
85 let @" = "text to paste"
86
87 let buf = Run_shell_in_terminal({})
88 " Wait for the shell to display a prompt
89 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
90
91 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
92 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020093 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010094
95 exe buf . 'bwipe!'
96 unlet g:job
97endfunc
98
Yee Cheng Chin42826332022-10-10 11:46:16 +010099func Test_terminal_unload_buffer()
100 let buf = Run_shell_in_terminal({})
101 call assert_fails(buf . 'bunload', 'E948:')
102 exe buf . 'bunload!'
103 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
104 call assert_equal("", bufname(buf))
105
106 unlet g:job
107endfunc
108
Bram Moolenaar94053a52017-08-01 21:44:33 +0200109func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200110 let buf = Run_shell_in_terminal({})
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100111 call assert_fails(buf . 'bwipe', 'E948:')
Bram Moolenaareb44a682017-08-03 22:44:55 +0200112 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200113 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200114 call assert_equal("", bufname(buf))
115
116 unlet g:job
117endfunc
118
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100119" Test that using ':confirm bwipe' on terminal works
120func Test_terminal_confirm_wipe_buffer()
121 CheckUnix
122 CheckNotGui
123 CheckFeature dialog_con
124 let buf = Run_shell_in_terminal({})
125 call assert_fails(buf . 'bwipe', 'E948:')
126 call feedkeys('n', 'L')
127 call assert_fails('confirm ' .. buf .. 'bwipe', 'E517:')
128 call assert_equal(buf, bufnr())
129 call assert_equal(1, &modified)
130 call feedkeys('y', 'L')
131 exe 'confirm ' .. buf .. 'bwipe'
132 call assert_notequal(buf, bufnr())
133 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
134 call assert_equal("", bufname(buf))
135
136 unlet g:job
137endfunc
138
139" Test that using :b! will hide the terminal
140func Test_terminal_goto_buffer()
141 let buf_mod = bufnr()
142 let buf_term = Run_shell_in_terminal({})
143 call assert_equal(buf_term, bufnr())
144 call assert_fails(buf_mod . 'b', 'E948:')
145 exe buf_mod . 'b!'
146 call assert_equal(buf_mod, bufnr())
147 call assert_equal('run', job_status(g:job))
148 call assert_notequal('', bufname(buf_term))
149 exec buf_mod .. 'bwipe!'
150 exec buf_term .. 'bwipe!'
151
152 unlet g:job
153endfunc
154
155" Test that using ':confirm :b' will kill terminal
156func Test_terminal_confirm_goto_buffer()
157 CheckUnix
158 CheckNotGui
159 CheckFeature dialog_con
160 let buf_mod = bufnr()
161 let buf_term = Run_shell_in_terminal({})
162 call feedkeys('n', 'L')
163 exe 'confirm ' .. buf_mod .. 'b'
164 call assert_equal(buf_term, bufnr())
165 call feedkeys('y', 'L')
166 exec 'confirm ' .. buf_mod .. 'b'
167 call assert_equal(buf_mod, bufnr())
168 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
169 call assert_equal("", bufname(buf_term))
170 exec buf_mod .. 'bwipe!'
171
172 unlet g:job
173endfunc
174
175" Test that using :close! will hide the terminal
176func Test_terminal_close_win()
177 let buf = Run_shell_in_terminal({})
178 call assert_equal(buf, bufnr())
179 call assert_fails('close', 'E948:')
180 close!
181 call assert_notequal(buf, bufnr())
182 call assert_equal('run', job_status(g:job))
183 call assert_notequal('', bufname(buf))
184 exec buf .. 'bwipe!'
185
186 unlet g:job
187endfunc
188
189" Test that using ':confirm close' will kill terminal
190func Test_terminal_confirm_close_win()
191 CheckUnix
192 CheckNotGui
193 CheckFeature dialog_con
194 let buf = Run_shell_in_terminal({})
195 call feedkeys('n', 'L')
196 confirm close
197 call assert_equal(buf, bufnr())
198 call feedkeys('y', 'L')
199 confirm close
200 call assert_notequal(buf, bufnr())
201 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
202 call assert_equal("", bufname(buf))
203
204 unlet g:job
205endfunc
206
207" Test that using :quit! will kill the terminal
208func Test_terminal_quit()
209 let buf = Run_shell_in_terminal({})
210 call assert_equal(buf, bufnr())
211 call assert_fails('quit', 'E948:')
212 quit!
213 call assert_notequal(buf, bufnr())
214 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Yee Cheng Chin42826332022-10-10 11:46:16 +0100215 call assert_equal("", bufname(buf))
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100216
217 unlet g:job
218endfunc
219
220" Test that using ':confirm quit' will kill terminal
221func Test_terminal_confirm_quit()
222 CheckUnix
223 CheckNotGui
224 CheckFeature dialog_con
225 let buf = Run_shell_in_terminal({})
226 call feedkeys('n', 'L')
227 confirm quit
228 call assert_equal(buf, bufnr())
229 call feedkeys('y', 'L')
230 confirm quit
231 call assert_notequal(buf, bufnr())
232 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
233
234 unlet g:job
235endfunc
236
237" Test :q or :next
238
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200239func Test_terminal_split_quit()
240 let buf = Run_shell_in_terminal({})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200241 split
242 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200243 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200244 sleep 50m
245 call assert_equal('run', job_status(g:job))
246
247 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200248 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200249
Yee Cheng Chin42826332022-10-10 11:46:16 +0100250 call assert_equal("", bufname(buf))
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200251 unlet g:job
252endfunc
253
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100254func Test_terminal_hide_buffer_job_running()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200255 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200256 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200257 quit
258 for nr in range(1, winnr('$'))
259 call assert_notequal(winbufnr(nr), buf)
260 endfor
261 call assert_true(bufloaded(buf))
262 call assert_true(buflisted(buf))
263
264 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200265 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200266 exe buf . 'bwipe'
267
268 unlet g:job
269endfunc
270
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100271func Test_terminal_hide_buffer_job_finished()
272 term echo hello
273 let buf = bufnr()
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100274 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Yee Cheng Chin42826332022-10-10 11:46:16 +0100275
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100276 call assert_true(bufloaded(buf))
277 call assert_true(buflisted(buf))
Yee Cheng Chin42826332022-10-10 11:46:16 +0100278
279 " Test :hide
280 hide
281 call assert_true(bufloaded(buf))
282 call assert_true(buflisted(buf))
283 split
284 exe buf .. 'buf'
285 call assert_equal(buf, bufnr())
286
287 " Test bufhidden, which exercises a different code path
288 setlocal bufhidden=hide
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100289 edit Xasdfasdf
290 call assert_true(bufloaded(buf))
291 call assert_true(buflisted(buf))
292 exe buf .. 'buf'
293 call assert_equal(buf, bufnr())
294 setlocal bufhidden=
Yee Cheng Chin42826332022-10-10 11:46:16 +0100295
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100296 edit Xasdfasdf
297 call assert_false(bufloaded(buf))
298 call assert_false(buflisted(buf))
299 bwipe Xasdfasdf
300endfunc
301
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000302func Test_terminal_rename_buffer()
303 let cmd = Get_cat_123_cmd()
304 let buf = term_start(cmd, {'term_name': 'foo'})
305 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
306 call assert_equal('foo', bufname())
307 call assert_match('foo.*finished', execute('ls'))
308 file bar
309 call assert_equal('bar', bufname())
310 call assert_match('bar.*finished', execute('ls'))
311 exe 'bwipe! ' .. buf
Christian Brabandt84bc00e2023-07-13 11:45:54 +0200312 call delete('Xtext')
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000313endfunc
314
Bram Moolenaar1e115362019-01-09 23:01:02 +0100315func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200316 exe g:buf . 'bwipe!'
317 let g:buf = 0
318endfunc
319
Bram Moolenaar9d189612017-09-09 18:11:00 +0200320func Get_cat_123_cmd()
321 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100322 if !has('conpty')
Milly4f5681d2024-10-20 11:06:00 +0200323 return 'cmd /D /c "cls && color 2 && echo 123"'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100324 else
325 " When clearing twice, extra sequence is not output.
Milly4f5681d2024-10-20 11:06:00 +0200326 return 'cmd /D /c "cls && cls && color 2 && echo 123"'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100327 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200328 else
329 call writefile(["\<Esc>[32m123"], 'Xtext')
330 return "cat Xtext"
331 endif
332endfunc
333
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200334func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200335 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200336 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
337 let g:job = term_getjob(g:buf)
338
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200339 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
340 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200341 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100342 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200343 call delete('Xtext')
344endfunc
345
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200346func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200347 let l = term_scrape(a:buf, 0)
348 call assert_true(len(l) == 0)
349 let l = term_scrape(a:buf, 999)
350 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200351 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200352 call assert_true(len(l) > 0)
353 call assert_equal('1', l[0].chars)
354 call assert_equal('2', l[1].chars)
355 call assert_equal('3', l[2].chars)
356 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200357 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
358 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100359 if has('win32')
360 " On Windows 'background' always defaults to dark, even though the terminal
361 " may use a light background. Therefore accept both white and black.
362 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200363 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100364 if &background == 'light'
365 call assert_equal('#ffffff', l[0].bg)
366 else
367 call assert_equal('#000000', l[0].bg)
368 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200369 endif
370
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200371 let l = term_getline(a:buf, -1)
372 call assert_equal('', l)
373 let l = term_getline(a:buf, 0)
374 call assert_equal('', l)
375 let l = term_getline(a:buf, 999)
376 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200377 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200378 call assert_equal('123', l)
379endfunc
380
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200381func Test_terminal_scrape_123()
382 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200383 let buf = term_start(cmd)
384
385 let termlist = term_list()
386 call assert_equal(1, len(termlist))
387 call assert_equal(buf, termlist[0])
388
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200389 " Nothing happens with invalid buffer number
390 call term_wait(1234)
391
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200392 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200393 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200394 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200395 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200396 call Check_123(buf)
397
398 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100399 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200400 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200401 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200402 call Check_123(buf)
403
404 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200405 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200406endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200407
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200408func Test_terminal_scrape_multibyte()
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100409 call writefile(["léttまrs"], 'Xtext', 'D')
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200410 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200411 " Run cmd with UTF-8 codepage to make the type command print the expected
412 " multibyte characters.
Milly4f5681d2024-10-20 11:06:00 +0200413 let buf = term_start("cmd /D /K chcp 65001")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100414 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200415 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100416 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200417 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100418 let buf = term_start("cat Xtext")
419 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200420 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200421
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100422 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
423 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200424 call assert_true(len(l) >= 7)
425 call assert_equal('l', l[0].chars)
426 call assert_equal('é', l[1].chars)
427 call assert_equal(1, l[1].width)
428 call assert_equal('t', l[2].chars)
429 call assert_equal('t', l[3].chars)
430 call assert_equal('ま', l[4].chars)
431 call assert_equal(2, l[4].width)
432 call assert_equal('r', l[5].chars)
433 call assert_equal('s', l[6].chars)
434
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100435 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200436 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200437 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200438
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100439 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200440endfunc
441
Bram Moolenaar8b896142020-08-02 15:05:05 +0200442func Test_terminal_one_column()
443 " This creates a terminal, displays a double-wide character and makes the
444 " window one column wide. This used to cause a crash.
445 let width = &columns
446 botright vert term
447 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200448 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200449 exe "set columns=" .. (width / 2)
450 redraw
451 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200452 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200453 exe "set columns=" .. width
454 exe buf . 'bwipe!'
455endfunc
456
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200457func Test_terminal_scroll()
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100458 call writefile(range(1, 200), 'Xtext', 'D')
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200459 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +0200460 let cmd = 'cmd /D /c "type Xtext"'
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200461 else
462 let cmd = "cat Xtext"
463 endif
464 let buf = term_start(cmd)
465
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100466 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200467 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200468 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200469
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100470 " wait until the scrolling stops
471 while 1
472 let scrolled = buf->term_getscrolled()
473 sleep 20m
474 if scrolled == buf->term_getscrolled()
475 break
476 endif
477 endwhile
478
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200479 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200480 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200481 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200482 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200483 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200484 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200485
486 exe buf . 'bwipe'
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200487endfunc
488
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200489func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200490 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200491 set termwinscroll=100
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100492 call writefile(range(150), 'Xtext', 'D')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200493 if has('win32')
494 call term_sendkeys(buf, "type Xtext\<CR>")
495 else
496 call term_sendkeys(buf, "cat Xtext\<CR>")
497 endif
498 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200499 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200500 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200501 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200502 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200503
Milly6d5f4a02024-10-22 23:17:45 +0200504 " When 'termwinscroll' becomes small, the scrollback should become small.
505 set termwinscroll=20
506 call term_sendkeys(buf, "echo set20\<CR>")
507 call WaitForAssert({-> assert_true([term_getline(buf, rows - 1), term_getline(buf, rows - 2)]->index('set20') >= 0)})
508 let lines = line('$')
509 call assert_inrange(19, 20, lines)
510
511 " When 'termwinscroll' under 10 which means 10% of it will be 0,
512 " the scrollback should become small.
513 set termwinscroll=1
514 call term_sendkeys(buf, "echo set1\<CR>")
515 call WaitForAssert({-> assert_true([term_getline(buf, rows - 1), term_getline(buf, rows - 2)]->index('set1') >= 0)})
516 let lines = line('$')
517 call assert_inrange(1, 2, lines)
518
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200519 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200520 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200521 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100522endfunc
523
524func Test_terminal_postponed_scrollback()
Drew Vogelea67ba72025-05-07 22:05:17 +0200525 CheckScreendump
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200526 " tail -f only works on Unix
527 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100528
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100529 call writefile(range(50), 'Xtext', 'D')
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100530 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100531 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100532 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100533 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100534 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
535 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100536 \ 'call feedkeys("\<C-W>N", "xt")',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100537 \ ], 'XTest_postponed', 'D')
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100538 let buf = RunVimInTerminal('-S XTest_postponed', {})
539 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100540 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100541
542 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200543 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100544 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100545 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100546
547 " Back to Terminal-Job mode, text will scroll and show the extra line.
548 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100549 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100550
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100551 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100552 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200553 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100554 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100555 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200556 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100557 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100558 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
559
560 " check type of term_sendkeys() return value
561 echo type(tsk_ret)
562
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100563 call StopVimInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200564endfunc
565
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100566" Run diff on two dumps with different size.
567func Test_terminal_dumpdiff_size()
568 call assert_equal(1, winnr('$'))
569 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
570 call assert_equal(2, winnr('$'))
571 call assert_match('Test_incsearch_search_01.dump', getline(10))
572 call assert_match(' +++++$', getline(11))
573 call assert_match('Test_popup_command_01.dump', getline(31))
574 call assert_equal(repeat('+', 75), getline(30))
575 quit
576endfunc
577
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200578func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200579 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200580
Bram Moolenaarb2412082017-08-20 18:09:14 +0200581 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200582 let size = term_getsize('')
583 bwipe!
584 call assert_equal(5, size[0])
585
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200586 call term_start(cmd, {'term_rows': 6})
587 let size = term_getsize('')
588 bwipe!
589 call assert_equal(6, size[0])
590
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200591 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200592 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200593 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200594
595 call term_setsize('', 6, 0)
596 call assert_equal([6, 33], term_getsize(''))
597
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200598 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200599 call assert_equal([6, 35], term_getsize(''))
600
601 call term_setsize('', 7, 30)
602 call assert_equal([7, 30], term_getsize(''))
603
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200604 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200605 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200606
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200607 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
608 let size = term_getsize('')
609 bwipe!
610 call assert_equal([6, 36], size)
611
Bram Moolenaarb2412082017-08-20 18:09:14 +0200612 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200613 let size = term_getsize('')
614 bwipe!
615 call assert_equal(20, size[1])
616
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200617 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200618 let size = term_getsize('')
619 bwipe!
620 call assert_equal(26, size[1])
621
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200622 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200623 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200624 let size = term_getsize('')
625 bwipe!
626 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200627
628 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
629 let size = term_getsize('')
630 bwipe!
631 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200632
Bram Moolenaar5300be62021-11-13 10:27:40 +0000633 call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:')
634 call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100635 call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:')
Bram Moolenaar88137392021-11-12 16:01:15 +0000636
Kenta Satoc28e7a22023-05-08 18:26:03 +0100637 call assert_fails("call term_start(cmd, {'term_cols': -1})", 'E475:')
638 call assert_fails("call term_start(cmd, {'term_cols': 1001})", 'E475:')
639 call assert_fails("call term_start(cmd, {'term_cols': 10.0})", 'E805:')
640
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200641 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200642endfunc
643
Bram Moolenaareba13e42021-02-23 17:47:23 +0100644func Test_terminal_zero_height()
645 split
646 wincmd j
647 anoremenu 1.1 WinBar.test :
648 terminal ++curwin
649 wincmd k
650 wincmd _
651 redraw
652
653 call term_sendkeys(bufnr(), "exit\r")
654 bwipe!
655endfunc
656
Bram Moolenaarda43b612017-08-11 22:27:50 +0200657func Test_terminal_curwin()
658 let cmd = Get_cat_123_cmd()
659 call assert_equal(1, winnr('$'))
660
Bram Moolenaarb1009092020-05-31 16:04:42 +0200661 split Xdummy
662 call setline(1, 'dummy')
663 write
664 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200665 exe 'terminal ++curwin ' . cmd
666 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200667 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200668 bwipe!
669
Bram Moolenaarb1009092020-05-31 16:04:42 +0200670 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200671 call term_start(cmd, {'curwin': 1})
672 call assert_equal(2, winnr('$'))
673 bwipe!
674
Bram Moolenaarb1009092020-05-31 16:04:42 +0200675 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200676 call setline(1, 'change')
677 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
678 call assert_equal(2, winnr('$'))
679 exe 'terminal! ++curwin ' . cmd
680 call assert_equal(2, winnr('$'))
681 bwipe!
682
Bram Moolenaarb1009092020-05-31 16:04:42 +0200683 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200684 call setline(1, 'change')
685 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
686 call assert_equal(2, winnr('$'))
687 bwipe!
688
Bram Moolenaarb1009092020-05-31 16:04:42 +0200689 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200690 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200691 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200692 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200693endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200694
Bram Moolenaarff546792017-11-21 14:47:57 +0100695func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200696 if s:python != ''
697 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200698 " 500 was not enough for Travis
699 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200700 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200701 echo 'This will take five seconds...'
702 let waittime = 2000
703 if has('win32')
704 let cmd = $windir . '\system32\timeout.exe 1'
705 else
706 let cmd = 'sleep 1'
707 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200708 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100709 return [cmd, waittime]
710endfunc
711
712func Test_terminal_finish_open_close()
713 call assert_equal(1, winnr('$'))
714
715 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200716
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100717 " shell terminal closes automatically
718 terminal
719 let buf = bufnr('%')
720 call assert_equal(2, winnr('$'))
721 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200722 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200723 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200724 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100725
726 " shell terminal that does not close automatically
727 terminal ++noclose
728 let buf = bufnr('%')
729 call assert_equal(2, winnr('$'))
730 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200731 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200732 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100733 call assert_equal(2, winnr('$'))
734 quit
735 call assert_equal(1, winnr('$'))
736
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200737 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200738 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200739 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200740 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200741
742 call term_start(cmd, {'term_finish': 'close'})
743 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200744 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200745 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200746 call assert_equal(1, winnr('$'))
747
748 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200749 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200750 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200751 bwipe
752
753 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200754 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200755 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200756 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200757
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200758 exe 'terminal ++hidden ++open ' . cmd
759 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200760 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200761 bwipe
762
763 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
764 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200765 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200766 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200767
768 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
769 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
770 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
771 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
772
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100773 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d | let g:result = "opened the buffer in a window"'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200774 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200775 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200776 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100777 call assert_equal('opened the buffer in a window', g:result)
778 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200779 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200780endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200781
782func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100783 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +0200784 let cmd = 'cmd /D /c cd'
Bram Moolenaar077ff432019-10-28 00:42:21 +0100785 else
786 CheckExecutable pwd
787 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200788 endif
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100789 call mkdir('Xtermdir')
790 let buf = term_start(cmd, {'cwd': 'Xtermdir'})
Bram Moolenaaree7c8d92022-09-22 12:57:06 +0100791 " if the path is very long it may be split over two lines, join them
792 " together
793 call WaitForAssert({-> assert_equal('Xtermdir', fnamemodify(getline(1) .. getline(2), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200794
795 exe buf . 'bwipe'
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100796 call delete('Xtermdir', 'rf')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200797endfunc
798
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200799func Test_terminal_cwd_failure()
800 " Case 1: Provided directory is not actually a directory. Attempt to make
801 " the file executable as well.
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100802 call writefile([], 'Xtcfile', 'D')
Dominique Pelle91a874e2022-09-03 10:59:32 +0100803 call setfperm('Xtcfile', 'rwx------')
804 call assert_fails("call term_start(&shell, {'cwd': 'Xtcfile'})", 'E475:')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200805
806 " Case 2: Directory does not exist.
807 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
808
809 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100810 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200811 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100812 call mkdir('XdirNoAccess', '', '0600')
813 " return early if the directory permissions could not be set properly
814 if getfperm('XdirNoAccess')[2] == 'x'
815 call delete('XdirNoAccess', 'rf')
816 return
817 endif
818 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
819 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200820 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200821endfunc
822
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100823func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200824 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200825 call s:test_environment("VIM_SERVERNAME", v:servername)
826endfunc
827
828func Test_terminal_version()
829 call s:test_environment("VIM_TERMINAL", string(v:version))
830endfunc
831
832func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100833 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100834 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200835 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100836 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200837 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100838 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200839 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100840 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200841 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200842 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200843 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100844
Bram Moolenaar012eb662018-03-13 17:55:27 +0100845 exe buf . 'bwipe'
846 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100847endfunc
848
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200849func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100850 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200851 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200852 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100853 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100854 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100855 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100856 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100857 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200858 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200859 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200860 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200861
Bram Moolenaar012eb662018-03-13 17:55:27 +0100862 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200863endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200864
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200865func Test_terminal_list_args()
866 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100867 call assert_fails(buf . 'bwipe', 'E948:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200868 exe buf . 'bwipe!'
869 call assert_equal("", bufname(buf))
870endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200871
872func Test_terminal_noblock()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100873 let g:test_is_flaky = 1
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100874 let buf = term_start(&shell)
Zdenek Dohnaldd2dfb32022-02-23 14:25:17 +0000875 " Starting a terminal can be slow, esp. on busy CI machines.
876 let wait_time = 7500
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100877 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100878 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200879 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100880 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200881 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100882 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100883 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100884 " NPFS is used in Windows, nonblocking mode does not work properly.
885 elseif has('win32')
886 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200887 else
888 let len = 5000
889 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200890
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100891 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100892 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100893 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200894 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100895 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200896
897 " On MS-Windows there is an extra empty line below "done". Find "done" in
898 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100899 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100900 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100901 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200902 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100903 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200904 endif
905 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200906
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100907 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200908 call StopShellInTerminal(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200909 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200910 bwipe
911endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200912
913func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100914 " TODO: enable once writing to stdin works on MS-Windows
915 CheckNotMSWindows
916 CheckExecutable wc
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100917 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100918
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200919 call setline(1, ['one', 'two', 'three'])
920 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200921 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200922 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200923 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100924 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200925
926 call setline(1, ['one', 'two', 'three', 'four'])
927 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200928 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200929 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200930 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100931 %bwipe!
932endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200933
Bram Moolenaar21109272020-01-30 16:27:20 +0100934func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200935 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100936 let g:test_is_flaky = 1
Bram Moolenaardada6d22017-09-02 17:18:35 +0200937
Bram Moolenaar21109272020-01-30 16:27:20 +0100938 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200939 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100940 " MS-Windows echoes the input, Unix doesn't.
941 if has('win32')
942 call WaitFor({-> getline('$') =~ 'exit(123)'})
943 call assert_equal('hello', getline(line('$') - 1))
944 else
945 call WaitFor({-> getline('$') =~ 'hello'})
946 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200947 endif
Christian Brabandt2e4361b2025-02-09 17:18:07 +0100948 let exitval = bufnr()->term_getjob()->job_info().exitval
949 if !has('win32')
950 call assert_equal(123, exitval)
951 else
952 " python 3.13 on Windows returns exit code 1
953 " older versions returned correctly exit code 123
954 " https://github.com/python/cpython/issues/129900
955 call assert_match('1\|123', exitval)
956 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100957 %bwipe!
958endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200959
Bram Moolenaar21109272020-01-30 16:27:20 +0100960func Test_terminal_eof_arg_win32_ctrl_z()
961 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200962 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100963 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100964
965 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200966 exe '1term ++eof=<C-Z> ' .. s:python
Christian Brabandt3cfac592025-02-09 17:22:30 +0100967 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1) .. getline(line('$')))})
968 " until python 3.12 there was an extra line break, with 3.13 it was removed,
969 " so depending on the python version the ^Z is on the last or second-last line
970 call assert_match('\^Z', getline(line('$') - 1) .. getline(line('$')))
Bram Moolenaar21109272020-01-30 16:27:20 +0100971 %bwipe!
972endfunc
973
974func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200975 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100976 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100977
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100978 " Check the last specified ++eof arg is used and does not leak memory.
Bram Moolenaar21109272020-01-30 16:27:20 +0100979 new
980 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200981 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100982 " MS-Windows echoes the input, Unix doesn't.
983 if has('win32')
984 call WaitFor({-> getline('$') =~ 'exit(123)'})
985 call assert_equal('hello', getline(line('$') - 1))
986 else
987 call WaitFor({-> getline('$') =~ 'hello'})
988 call assert_equal('hello', getline('$'))
989 endif
Christian Brabandt2e4361b2025-02-09 17:18:07 +0100990 let exitval = bufnr()->term_getjob()->job_info().exitval
991 if !has('win32')
992 call assert_equal(123, exitval)
993 else
994 " python 3.13 on Windows returns exit code 1
995 " older versions returned correctly exit code 123
996 " https://github.com/python/cpython/issues/129900
997 call assert_match('1\|123', exitval)
998 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100999 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +02001000endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001001
1002func Test_terminal_no_cmd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001003 let g:test_is_flaky = 1
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001004 let buf = term_start('NONE', {})
1005 call assert_notequal(0, buf)
1006
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001007 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001008 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001009 if has('gui_running') && !has('win32')
1010 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001011 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001012 else
1013 " Otherwise using a job works on all systems.
1014 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001015 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001016 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001017
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001018 bwipe!
1019endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001020
1021func Test_terminal_special_chars()
1022 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001023 CheckUnix
1024
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001025 call mkdir('Xdir with spaces', 'R')
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001026 call writefile(['x'], 'Xdir with spaces/quoted"file')
1027 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001028 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +01001029 " make sure the job has finished
1030 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001031
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001032 bwipe
1033endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001034
1035func Test_terminal_wrong_options()
1036 call assert_fails('call term_start(&shell, {
1037 \ "in_io": "file",
1038 \ "in_name": "xxx",
1039 \ "out_io": "file",
1040 \ "out_name": "xxx",
1041 \ "err_io": "file",
1042 \ "err_name": "xxx"
1043 \ })', 'E474:')
1044 call assert_fails('call term_start(&shell, {
1045 \ "out_buf": bufnr("%")
1046 \ })', 'E474:')
1047 call assert_fails('call term_start(&shell, {
1048 \ "err_buf": bufnr("%")
1049 \ })', 'E474:')
1050endfunc
1051
1052func Test_terminal_redir_file()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001053 let g:test_is_flaky = 1
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001054 let cmd = Get_cat_123_cmd()
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001055 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xtrfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001056 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001057 " ConPTY may precede escape sequence. There are things that are not so.
1058 if !has('conpty')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001059 call WaitForAssert({-> assert_notequal(0, len(readfile("Xtrfile")))})
1060 call assert_match('123', readfile('Xtrfile')[0])
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001061 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001062 let g:job = term_getjob(buf)
1063 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +02001064
1065 if has('win32')
1066 " On Windows we cannot delete a file being used by a process. When
1067 " job_status() returns "dead", the process remains for a short time.
1068 " Just wait for a moment.
1069 sleep 50m
1070 endif
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001071 call delete('Xtrfile')
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001072 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001073
1074 if has('unix')
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001075 call writefile(['one line'], 'Xtrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001076 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xtrfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001077 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001078 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +02001079 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001080 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001081 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001082 endif
Christian Brabandt84bc00e2023-07-13 11:45:54 +02001083
1084 call delete('Xtext')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001085endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001086
1087func TerminalTmap(remap)
1088 let buf = Run_shell_in_terminal({})
Bram Moolenaarf4a2ed02021-03-23 16:25:09 +01001089 " Wait for the shell to display a prompt
1090 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001091 call assert_equal('t', mode())
1092
1093 if a:remap
1094 tmap 123 456
1095 else
1096 tnoremap 123 456
1097 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +01001098 " don't use abcde, it's an existing command
1099 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001100 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +01001101 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001102 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001103 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001104 let lnum = term_getcursor(buf)[0]
1105 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +01001106 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001107 else
1108 call assert_match('456', term_getline(buf, lnum))
1109 endif
1110
1111 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001112 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001113
1114 tunmap 123
1115 tunmap 456
1116 call assert_equal('', maparg('123', 't'))
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01001117 exe buf . 'bwipe'
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001118 unlet g:job
1119endfunc
1120
1121func Test_terminal_tmap()
1122 call TerminalTmap(1)
1123 call TerminalTmap(0)
1124endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +02001125
1126func Test_terminal_wall()
1127 let buf = Run_shell_in_terminal({})
1128 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001129 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +02001130 exe buf . 'bwipe'
1131 unlet g:job
1132endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001133
Bram Moolenaar7a760922018-02-19 23:10:02 +01001134func Test_terminal_wqall()
1135 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001136 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001137 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +01001138 exe buf . 'bwipe'
1139 unlet g:job
1140endfunc
1141
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001142func Test_terminal_composing_unicode()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001143 let g:test_is_flaky = 1
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001144 let save_enc = &encoding
1145 set encoding=utf-8
1146
1147 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +02001148 let cmd = "cmd /D /K chcp 65001"
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001149 let lnum = [3, 6, 9]
1150 else
1151 let cmd = &shell
1152 let lnum = [1, 3, 5]
1153 endif
1154
1155 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +02001156 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001157 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +02001158 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001159
Bram Moolenaarebe74b72018-04-21 23:34:43 +02001160 if has('win32')
1161 call assert_equal('cmd', job_info(g:job).cmd[0])
1162 else
1163 call assert_equal(&shell, job_info(g:job).cmd[0])
1164 endif
1165
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001166 " ascii + composing
1167 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001168 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001169 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001170 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001171 call term_sendkeys(buf, "\<cr>")
1172 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001173 let l = term_scrape(buf, lnum[0] + 1)
1174 call assert_equal("a\u0308", l[0].chars)
1175 call assert_equal("b", l[1].chars)
1176 call assert_equal("c", l[2].chars)
1177
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001178 " multibyte + composing: がぎぐげご
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001179 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001180 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001181 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001182 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001183 call term_sendkeys(buf, "\<cr>")
1184 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001185 let l = term_scrape(buf, lnum[1] + 1)
1186 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001187 call assert_equal(2, l[0].width)
1188 call assert_equal("\u304e", l[1].chars)
1189 call assert_equal(2, l[1].width)
1190 call assert_equal("\u304f\u3099", l[2].chars)
1191 call assert_equal(2, l[2].width)
1192 call assert_equal("\u3052", l[3].chars)
1193 call assert_equal(2, l[3].width)
1194 call assert_equal("\u3053\u3099", l[4].chars)
1195 call assert_equal(2, l[4].width)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001196
1197 " \u00a0 + composing
1198 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001199 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001200 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001201 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001202 call term_sendkeys(buf, "\<cr>")
1203 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001204 let l = term_scrape(buf, lnum[2] + 1)
1205 call assert_equal("\u00a0\u0308", l[3].chars)
1206
1207 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001208 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001209 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001210 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001211 let &encoding = save_enc
1212endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +01001213
1214func Test_terminal_aucmd_on_close()
1215 fun Nop()
1216 let s:called = 1
1217 endfun
1218
1219 aug repro
1220 au!
1221 au BufWinLeave * call Nop()
1222 aug END
1223
1224 let [cmd, waittime] = s:get_sleep_cmd()
1225
1226 call assert_equal(1, winnr('$'))
1227 new
1228 call setline(1, ['one', 'two'])
1229 exe 'term ++close ' . cmd
1230 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001231 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +01001232 call assert_equal(1, s:called)
1233 bwipe!
1234
1235 unlet s:called
1236 au! repro
1237 delfunc Nop
1238endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001239
1240func Test_terminal_term_start_empty_command()
1241 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001242 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001243 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001244 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001245 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001246 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001247 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001248 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001249 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001250 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001251 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001252 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001253 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001254 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001255 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001256 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001257 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001258 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001259 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1260 call assert_fails(cmd, 'E475:')
1261 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001262 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001263 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001264 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1265 call assert_fails(cmd, 'E475:')
1266 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1267 call assert_fails(cmd, 'E730:')
1268 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001269 if has('gui_running') || has('termguicolors')
1270 call assert_fails(cmd, 'E475:')
1271 else
1272 call assert_fails(cmd, 'E254:')
1273 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001274 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001275endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001276
1277func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001278 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001279
1280 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001281 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001282
Bram Moolenaar086eb872018-03-25 21:24:12 +02001283 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001284 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001285
Bram Moolenaar086eb872018-03-25 21:24:12 +02001286 " Request the cursor position.
1287 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001288
1289 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001290 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001291
Bram Moolenaar086eb872018-03-25 21:24:12 +02001292 " End "cat" gently.
1293 call term_sendkeys(buf, "\<CR>\<C-D>")
1294
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001295 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001296 exe buf . 'bwipe'
1297 unlet g:job
1298endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001299
Bram Moolenaarc2958582021-12-14 11:16:31 +00001300" Run this first, it fails when run after other tests.
1301func Test_aa_terminal_focus_events()
Drew Vogelea67ba72025-05-07 22:05:17 +02001302 CheckScreendump
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001303 CheckNotGui
1304 CheckUnix
1305 CheckRunVimInTerminal
1306
1307 let save_term = &term
1308 let save_ttymouse = &ttymouse
1309 set term=xterm ttymouse=xterm2
1310
1311 let lines =<< trim END
1312 set term=xterm ttymouse=xterm2
Bram Moolenaare5050712021-12-09 10:51:05 +00001313 au FocusLost * call setline(1, 'I am lost') | set nomod
1314 au FocusGained * call setline(1, 'I am back') | set nomod
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001315 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001316 call writefile(lines, 'XtermFocus', 'D')
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001317 let buf = RunVimInTerminal('-S XtermFocus', #{rows: 6})
1318
1319 " Send a focus event to ourselves, it should be forwarded to the terminal
1320 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001321 call VerifyScreenDump(buf, 'Test_terminal_focus_1', {})
1322
1323 call feedkeys("\<Esc>[I", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001324 call VerifyScreenDump(buf, 'Test_terminal_focus_2', {})
1325
Bram Moolenaare5050712021-12-09 10:51:05 +00001326 " check that a command line being edited is redrawn in place
1327 call term_sendkeys(buf, ":" .. repeat('x', 80))
1328 call TermWait(buf)
1329 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaare5050712021-12-09 10:51:05 +00001330 call VerifyScreenDump(buf, 'Test_terminal_focus_3', {})
1331 call term_sendkeys(buf, "\<Esc>")
1332
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001333 call StopVimInTerminal(buf)
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001334 let &term = save_term
1335 let &ttymouse = save_ttymouse
1336endfunc
1337
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001338" Run Vim, start a terminal in that Vim with the kill argument,
1339" :qall works.
1340func Run_terminal_qall_kill(line1, line2)
1341 " 1. Open a terminal window and wait for the prompt to appear
1342 " 2. set kill using term_setkill()
1343 " 3. make Vim exit, it will kill the shell
1344 let after = [
1345 \ a:line1,
1346 \ 'let buf = bufnr("%")',
1347 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1348 \ ' sleep 10m',
1349 \ 'endwhile',
1350 \ a:line2,
1351 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1352 \ 'qall',
1353 \ ]
1354 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001355 return
1356 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001357 call assert_equal("done", readfile("Xdone")[0])
1358 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001359endfunc
1360
1361" Run Vim in a terminal, then start a terminal in that Vim with a kill
1362" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001363func Test_terminal_qall_kill_arg()
1364 call Run_terminal_qall_kill('term ++kill=kill', '')
1365endfunc
1366
1367" Run Vim, start a terminal in that Vim, set the kill argument with
1368" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001369func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001370 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001371endfunc
1372
1373" Run Vim, start a terminal in that Vim without the kill argument,
1374" check that :qall does not exit, :qall! does.
1375func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001376 let after =<< trim [CODE]
1377 term
1378 let buf = bufnr("%")
1379 while term_getline(buf, 1) =~ "^\\s*$"
1380 sleep 10m
1381 endwhile
1382 set nomore
1383 au VimLeavePre * call writefile(["too early"], "Xdone")
1384 qall
1385 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1386 cquit
1387 [CODE]
1388
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001389 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001390 return
1391 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001392 call assert_equal("done", readfile("Xdone")[0])
1393 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001394endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001395
1396" Run Vim in a terminal, then start a terminal in that Vim without a kill
1397" argument, check that :confirm qall works.
1398func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001399 CheckRunVimInTerminal
Bram Moolenaare564c702022-06-14 15:00:28 +01001400
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001401 let buf = RunVimInTerminal('', {})
1402
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001403 " the shell may set the window title, we don't want that here
Bram Moolenaar377d92a2022-06-14 21:22:12 +01001404 call term_sendkeys(buf, ":call test_override('vterm_title', 1)\<CR>")
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001405
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001406 " Open a terminal window and wait for the prompt to appear
1407 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001408 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1409 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001410
1411 " make Vim exit, it will prompt to kill the shell
1412 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001413 call WaitForAssert({-> assert_match('\[Y\]es, (N)o:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001414 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001415 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001416
1417 " close the terminal window where Vim was running
1418 quit
1419endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001420
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001421" Run Vim in a terminal, then start a terminal window with a shell and check
1422" that Vim exits if it is closed.
1423func Test_terminal_exit()
1424 CheckRunVimInTerminal
1425
1426 let lines =<< trim END
1427 let winid = win_getid()
1428 help
1429 term
1430 let termid = win_getid()
1431 call win_gotoid(winid)
1432 close
1433 call win_gotoid(termid)
1434 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001435 call writefile(lines, 'XtermExit', 'D')
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001436 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1437 let job = term_getjob(buf)
1438 call WaitForAssert({-> assert_equal("run", job_status(job))})
1439
1440 " quit the shell, it will make Vim exit
1441 call term_sendkeys(buf, "exit\<CR>")
1442 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001443endfunc
1444
Bram Moolenaar012eb662018-03-13 17:55:27 +01001445func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001446 augroup repro
1447 au!
1448 au TerminalOpen * let s:called += 1
1449 augroup END
1450
1451 let s:called = 0
1452
1453 " Open a terminal window with :terminal
1454 terminal
1455 call assert_equal(1, s:called)
1456 bwipe!
1457
1458 " Open a terminal window with term_start()
1459 call term_start(&shell)
1460 call assert_equal(2, s:called)
1461 bwipe!
1462
1463 " Open a hidden terminal buffer with :terminal
1464 terminal ++hidden
1465 call assert_equal(3, s:called)
1466 for buf in term_list()
1467 exe buf . "bwipe!"
1468 endfor
1469
1470 " Open a hidden terminal buffer with term_start()
1471 let buf = term_start(&shell, {'hidden': 1})
1472 call assert_equal(4, s:called)
1473 exe buf . "bwipe!"
1474
1475 unlet s:called
1476 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001477endfunc
1478
1479func Test_open_term_from_cmd()
Drew Vogelea67ba72025-05-07 22:05:17 +02001480 CheckScreendump
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001481 CheckUnix
1482 CheckRunVimInTerminal
1483
1484 let lines =<< trim END
1485 call setline(1, ['a', 'b', 'c'])
1486 3
1487 set incsearch
1488 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1489 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001490 call writefile(lines, 'Xopenterm', 'D')
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001491 let buf = RunVimInTerminal('-S Xopenterm', {})
1492
1493 " this opens a window, incsearch should not use the old cursor position
1494 call term_sendkeys(buf, "/\<F3>")
1495 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1496 call term_sendkeys(buf, "\<Esc>")
1497 call term_sendkeys(buf, ":q\<CR>")
1498
1499 call StopVimInTerminal(buf)
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001500endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001501
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001502func Test_combining_double_width()
Drew Vogelea67ba72025-05-07 22:05:17 +02001503 CheckScreendump
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001504 CheckUnix
1505 CheckRunVimInTerminal
1506
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001507 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble', 'D')
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001508 let lines =<< trim END
1509 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1510 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001511 call writefile(lines, 'Xcombining', 'D')
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001512 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1513
1514 " this opens a window, incsearch should not use the old cursor position
1515 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1516 call term_sendkeys(buf, ":q\<CR>")
1517
1518 call StopVimInTerminal(buf)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001519endfunc
1520
Bram Moolenaar02764712020-11-14 20:21:55 +01001521func Test_terminal_popup_with_cmd()
1522 " this was crashing
1523 let buf = term_start(&shell, #{hidden: v:true})
1524 let s:winid = popup_create(buf, {})
1525 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1526 call feedkeys("\<F3>", 'xt')
1527
1528 tunmap <F3>
1529 exe 'bwipe! ' .. buf
1530 unlet s:winid
1531endfunc
1532
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001533func Test_terminal_popup_bufload()
1534 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1535 let winid = popup_create(termbuf, {})
1536 sleep 50m
1537
1538 let newbuf = bufadd('')
1539 call bufload(newbuf)
1540 call setbufline(newbuf, 1, 'foobar')
1541
1542 " must not have switched to another window
1543 call assert_equal(winid, win_getid())
1544
Bram Moolenaare6329e42020-11-16 21:10:34 +01001545 call StopShellInTerminal(termbuf)
1546 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001547 exe 'bwipe! ' .. newbuf
1548endfunc
1549
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001550func Test_terminal_popup_two_windows()
Bram Moolenaar0407d272021-12-13 22:17:44 +00001551 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001552 CheckUnix
1553
1554 " use "sh" instead of "&shell" in the hope it will use a short prompt
1555 let lines =<< trim END
1556 let termbuf = term_start('sh', #{hidden: v:true, term_finish: 'close'})
1557 exe 'buffer ' .. termbuf
1558
1559 let winid = popup_create(termbuf, #{line: 2, minwidth: 30, border: []})
1560 sleep 50m
1561
1562 call term_sendkeys(termbuf, "echo 'test'")
1563 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001564 call writefile(lines, 'XpopupScript', 'D')
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001565 let buf = RunVimInTerminal('-S XpopupScript', {})
1566
1567 " typed text appears both in normal window and in popup
1568 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 1))})
1569 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 3))})
1570
Bram Moolenaar0407d272021-12-13 22:17:44 +00001571 call term_sendkeys(buf, "\<CR>\<CR>exit\<CR>")
1572 call TermWait(buf)
1573 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001574 call StopVimInTerminal(buf)
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001575endfunc
1576
Bram Moolenaare41decc2020-11-14 21:34:59 +01001577func Test_terminal_popup_insert_cmd()
1578 CheckUnix
1579
1580 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1581 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001582 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001583 endfunc
1584 call feedkeys("i\<F3>")
1585 sleep 10m
1586 call assert_equal('n', mode())
1587
1588 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001589 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001590 delfunc StartTermInPopup
1591 iunmap <F3>
1592endfunc
1593
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001594func Check_dump01(off)
1595 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1596 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001597 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001598endfunc
1599
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001600func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001601 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001602
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001603 let save_enc = &encoding
1604 set encoding=utf-8
1605 call assert_equal(1, winnr('$'))
1606
1607 let text = " a\u0300 e\u0302 o\u0308"
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001608 call writefile([text], 'Xcomposing', 'D')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001609 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001610 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001611 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001612 let dumpline = readfile('Xdump')[0]
1613 call assert_match('|à| |ê| |ö', dumpline)
1614
1615 call StopVimInTerminal(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001616 call delete('Xdump')
1617 let &encoding = save_enc
1618endfunc
1619
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001620" Tests for failures in the term_dumpwrite() function
1621func Test_terminal_dumpwrite_errors()
1622 CheckRunVimInTerminal
1623 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1624 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001625 call TermWait(buf)
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001626 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E1206:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001627 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1628 call writefile([], 'Xtest.dump')
1629 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1630 call delete('Xtest.dump')
1631 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1632 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001633 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001634 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001635 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001636 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1637 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1638 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1639endfunc
1640
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001641" just testing basic functionality.
1642func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001643 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001644 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001645 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001646 call assert_equal(2, winnr('$'))
1647 call assert_equal(20, line('$'))
1648 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001649
1650 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001651 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001652 call assert_equal(buf, buf2)
1653 call assert_notequal('one two three four five', trim(getline(1)))
1654
1655 " Load the first dump again in the same window
1656 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1657 call assert_equal(buf, buf2)
1658 call Check_dump01(0)
1659
1660 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1661 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1662 new
1663 let closedbuf = winbufnr('')
1664 quit
1665 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001666 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001667 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001668
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001669 quit
1670endfunc
1671
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001672func Test_terminal_dumpload_dump()
Drew Vogelea67ba72025-05-07 22:05:17 +02001673 CheckScreendump
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001674 CheckRunVimInTerminal
1675
1676 let lines =<< trim END
1677 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1678 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001679 call writefile(lines, 'XtermDumpload', 'D')
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001680 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1681 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1682
1683 call StopVimInTerminal(buf)
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001684endfunc
1685
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001686func Test_terminal_dumpdiff()
1687 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001688 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001689 call assert_equal(2, winnr('$'))
1690 call assert_equal(62, line('$'))
1691 call Check_dump01(0)
1692 call Check_dump01(42)
1693 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1694 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001695
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001696 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001697 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001698 call writefile([], 'X1.dump', 'D')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001699 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001700endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001701
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001702func Test_terminal_dumpdiff_swap()
1703 call assert_equal(1, winnr('$'))
1704 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1705 call assert_equal(2, winnr('$'))
1706 call assert_equal(62, line('$'))
1707 call assert_match('Test_popup_command_01.dump', getline(21))
1708 call assert_match('Test_popup_command_03.dump', getline(42))
1709 call assert_match('Undo', getline(3))
1710 call assert_match('three four five', getline(45))
1711
1712 normal s
1713 call assert_match('Test_popup_command_03.dump', getline(21))
1714 call assert_match('Test_popup_command_01.dump', getline(42))
1715 call assert_match('three four five', getline(3))
1716 call assert_match('Undo', getline(45))
1717 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001718
1719 " Diff two terminal dump files with different number of rows
1720 " Swap the diffs
1721 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1722 call assert_match('Test_popup_command_01.dump', getline(21))
1723 call assert_match('Test_winline_rnu.dump', getline(42))
1724 normal s
1725 call assert_match('Test_winline_rnu.dump', getline(6))
1726 call assert_match('Test_popup_command_01.dump', getline(27))
1727 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001728endfunc
1729
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001730func Test_terminal_dumpdiff_options()
1731 set laststatus=0
1732 call assert_equal(1, winnr('$'))
1733 let height = winheight(0)
1734 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1735 call assert_equal(2, winnr('$'))
1736 call assert_equal(height, winheight(winnr()))
1737 call assert_equal(33, winwidth(winnr()))
1738 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1739 quit
1740
1741 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001742 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1743 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001744 call assert_equal(&columns, winwidth(0))
1745 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001746 call assert_equal('something else', bufname('%'))
1747 quit
1748
1749 call assert_equal(1, winnr('$'))
1750 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1751 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001752 call assert_fails("call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'bufnr': -1})", 'E475:')
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001753 bwipe
1754
1755 set laststatus&
1756endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001757
Bram Moolenaar10772302019-01-20 18:25:54 +01001758" When drawing the statusline the cursor position may not have been updated
1759" yet.
1760" 1. create a terminal, make it show 2 lines
1761" 2. 0.5 sec later: leave terminal window, execute "i"
1762" 3. 0.5 sec later: clear terminal window, now it's 1 line
1763" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1764" 4. 0.5 sec later: should be done, clean up
1765func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001766 CheckUnix
Bram Moolenaar81035272021-12-16 18:02:07 +00001767 CheckFeature timers
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001768
Bram Moolenaar10772302019-01-20 18:25:54 +01001769 set statusline=x
1770 terminal
1771 let tbuf = bufnr('')
1772 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1773 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1774 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1775 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1776
1777 sleep 2
1778 exe tbuf . 'bwipe!'
1779 au! BufLeave
1780 set statusline=
1781endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001782
Bram Moolenaar81035272021-12-16 18:02:07 +00001783func CheckTerminalWindowWorks(buf)
1784 call WaitForAssert({-> assert_match('!sh \[running\]', term_getline(a:buf, 10))})
1785 call term_sendkeys(a:buf, "exit\<CR>")
1786 call WaitForAssert({-> assert_match('!sh \[finished\]', term_getline(a:buf, 10))})
1787 call term_sendkeys(a:buf, ":q\<CR>")
1788 call WaitForAssert({-> assert_match('^\~', term_getline(a:buf, 10))})
1789endfunc
1790
1791func Test_start_terminal_from_timer()
1792 CheckUnix
1793 CheckFeature timers
1794
1795 " Open a terminal window from a timer, typed text goes to the terminal
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001796 call writefile(["call timer_start(100, { -> term_start('sh') })"], 'XtimerTerm', 'D')
Bram Moolenaar81035272021-12-16 18:02:07 +00001797 let buf = RunVimInTerminal('-S XtimerTerm', {})
1798 call CheckTerminalWindowWorks(buf)
1799
1800 " do the same in Insert mode
1801 call term_sendkeys(buf, ":call timer_start(200, { -> term_start('sh') })\<CR>a")
1802 call CheckTerminalWindowWorks(buf)
1803
1804 call StopVimInTerminal(buf)
Bram Moolenaar81035272021-12-16 18:02:07 +00001805endfunc
1806
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001807func Test_terminal_window_focus()
1808 let winid1 = win_getid()
1809 terminal
1810 let winid2 = win_getid()
1811 call feedkeys("\<C-W>j", 'xt')
1812 call assert_equal(winid1, win_getid())
1813 call feedkeys("\<C-W>k", 'xt')
1814 call assert_equal(winid2, win_getid())
1815 " can use a cursor key here
1816 call feedkeys("\<C-W>\<Down>", 'xt')
1817 call assert_equal(winid1, win_getid())
1818 call feedkeys("\<C-W>\<Up>", 'xt')
1819 call assert_equal(winid2, win_getid())
1820
1821 bwipe!
1822endfunc
1823
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001824func Api_drop_common(options)
1825 call assert_equal(1, winnr('$'))
1826
1827 " Use the title termcap entries to output the escape sequence.
1828 call writefile([
1829 \ 'set title',
1830 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1831 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1832 \ 'redraw',
1833 \ "set t_ts=",
1834 \ ], 'Xscript')
1835 let buf = RunVimInTerminal('-S Xscript', {})
1836 call WaitFor({-> bufnr('Xtextfile') > 0})
1837 call assert_equal('Xtextfile', expand('%:t'))
1838 call assert_true(winnr('$') >= 3)
1839 return buf
1840endfunc
1841
1842func Test_terminal_api_drop_newwin()
1843 CheckRunVimInTerminal
1844 let buf = Api_drop_common('')
1845 call assert_equal(0, &bin)
1846 call assert_equal('', &fenc)
1847
1848 call StopVimInTerminal(buf)
1849 call delete('Xscript')
1850 bwipe Xtextfile
1851endfunc
1852
1853func Test_terminal_api_drop_newwin_bin()
1854 CheckRunVimInTerminal
1855 let buf = Api_drop_common(',{"bin":1}')
1856 call assert_equal(1, &bin)
1857
1858 call StopVimInTerminal(buf)
1859 call delete('Xscript')
1860 bwipe Xtextfile
1861endfunc
1862
1863func Test_terminal_api_drop_newwin_binary()
1864 CheckRunVimInTerminal
1865 let buf = Api_drop_common(',{"binary":1}')
1866 call assert_equal(1, &bin)
1867
1868 call StopVimInTerminal(buf)
1869 call delete('Xscript')
1870 bwipe Xtextfile
1871endfunc
1872
1873func Test_terminal_api_drop_newwin_nobin()
1874 CheckRunVimInTerminal
1875 set binary
1876 let buf = Api_drop_common(',{"nobin":1}')
1877 call assert_equal(0, &bin)
1878
1879 call StopVimInTerminal(buf)
1880 call delete('Xscript')
1881 bwipe Xtextfile
1882 set nobinary
1883endfunc
1884
1885func Test_terminal_api_drop_newwin_nobinary()
1886 CheckRunVimInTerminal
1887 set binary
1888 let buf = Api_drop_common(',{"nobinary":1}')
1889 call assert_equal(0, &bin)
1890
1891 call StopVimInTerminal(buf)
1892 call delete('Xscript')
1893 bwipe Xtextfile
1894 set nobinary
1895endfunc
1896
1897func Test_terminal_api_drop_newwin_ff()
1898 CheckRunVimInTerminal
1899 let buf = Api_drop_common(',{"ff":"dos"}')
1900 call assert_equal("dos", &ff)
1901
1902 call StopVimInTerminal(buf)
1903 call delete('Xscript')
1904 bwipe Xtextfile
1905endfunc
1906
1907func Test_terminal_api_drop_newwin_fileformat()
1908 CheckRunVimInTerminal
1909 let buf = Api_drop_common(',{"fileformat":"dos"}')
1910 call assert_equal("dos", &ff)
1911
1912 call StopVimInTerminal(buf)
1913 call delete('Xscript')
1914 bwipe Xtextfile
1915endfunc
1916
1917func Test_terminal_api_drop_newwin_enc()
1918 CheckRunVimInTerminal
1919 let buf = Api_drop_common(',{"enc":"utf-16"}')
1920 call assert_equal("utf-16", &fenc)
1921
1922 call StopVimInTerminal(buf)
1923 call delete('Xscript')
1924 bwipe Xtextfile
1925endfunc
1926
1927func Test_terminal_api_drop_newwin_encoding()
1928 CheckRunVimInTerminal
1929 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1930 call assert_equal("utf-16", &fenc)
1931
1932 call StopVimInTerminal(buf)
1933 call delete('Xscript')
1934 bwipe Xtextfile
1935endfunc
1936
1937func Test_terminal_api_drop_oldwin()
1938 CheckRunVimInTerminal
1939 let firstwinid = win_getid()
1940 split Xtextfile
1941 let textfile_winid = win_getid()
1942 call assert_equal(2, winnr('$'))
1943 call win_gotoid(firstwinid)
1944
1945 " Use the title termcap entries to output the escape sequence.
1946 call writefile([
1947 \ 'set title',
1948 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1949 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1950 \ 'redraw',
1951 \ "set t_ts=",
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001952 \ ], 'Xscript', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001953 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1954 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1955 call assert_equal(textfile_winid, win_getid())
1956
1957 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001958 bwipe Xtextfile
1959endfunc
1960
1961func Tapi_TryThis(bufnum, arg)
1962 let g:called_bufnum = a:bufnum
1963 let g:called_arg = a:arg
1964endfunc
1965
1966func WriteApiCall(funcname)
1967 " Use the title termcap entries to output the escape sequence.
1968 call writefile([
1969 \ 'set title',
1970 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1971 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1972 \ 'redraw',
1973 \ "set t_ts=",
1974 \ ], 'Xscript')
1975endfunc
1976
1977func Test_terminal_api_call()
1978 CheckRunVimInTerminal
1979
1980 unlet! g:called_bufnum
1981 unlet! g:called_arg
1982
1983 call WriteApiCall('Tapi_TryThis')
1984
1985 " Default
1986 let buf = RunVimInTerminal('-S Xscript', {})
1987 call WaitFor({-> exists('g:called_bufnum')})
1988 call assert_equal(buf, g:called_bufnum)
1989 call assert_equal(['hello', 123], g:called_arg)
1990 call StopVimInTerminal(buf)
1991
1992 unlet! g:called_bufnum
1993 unlet! g:called_arg
1994
1995 " Enable explicitly
1996 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1997 call WaitFor({-> exists('g:called_bufnum')})
1998 call assert_equal(buf, g:called_bufnum)
1999 call assert_equal(['hello', 123], g:called_arg)
2000 call StopVimInTerminal(buf)
2001
2002 unlet! g:called_bufnum
2003 unlet! g:called_arg
2004
2005 func! ApiCall_TryThis(bufnum, arg)
2006 let g:called_bufnum2 = a:bufnum
2007 let g:called_arg2 = a:arg
2008 endfunc
2009
2010 call WriteApiCall('ApiCall_TryThis')
2011
2012 " Use prefix match
2013 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
2014 call WaitFor({-> exists('g:called_bufnum2')})
2015 call assert_equal(buf, g:called_bufnum2)
2016 call assert_equal(['hello', 123], g:called_arg2)
2017 call StopVimInTerminal(buf)
2018
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002019 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002020
2021 unlet! g:called_bufnum2
2022 unlet! g:called_arg2
2023
2024 call delete('Xscript')
2025 delfunction! ApiCall_TryThis
2026 unlet! g:called_bufnum2
2027 unlet! g:called_arg2
2028endfunc
2029
2030func Test_terminal_api_call_fails()
2031 CheckRunVimInTerminal
2032
2033 func! TryThis(bufnum, arg)
2034 let g:called_bufnum3 = a:bufnum
2035 let g:called_arg3 = a:arg
2036 endfunc
2037
2038 call WriteApiCall('TryThis')
2039
2040 unlet! g:called_bufnum3
2041 unlet! g:called_arg3
2042
2043 " Not permitted
2044 call ch_logfile('Xlog', 'w')
2045 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
2046 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
2047 call assert_false(exists('g:called_bufnum3'))
2048 call assert_false(exists('g:called_arg3'))
2049 call StopVimInTerminal(buf)
2050
2051 " No match
2052 call ch_logfile('Xlog', 'w')
2053 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
2054 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
2055 call assert_false(exists('g:called_bufnum3'))
2056 call assert_false(exists('g:called_arg3'))
2057 call StopVimInTerminal(buf)
2058
2059 call delete('Xscript')
2060 call ch_logfile('')
2061 call delete('Xlog')
2062 delfunction! TryThis
2063 unlet! g:called_bufnum3
2064 unlet! g:called_arg3
2065endfunc
2066
2067let s:caught_e937 = 0
2068
2069func Tapi_Delete(bufnum, arg)
2070 try
2071 execute 'bdelete!' a:bufnum
2072 catch /E937:/
2073 let s:caught_e937 = 1
2074 endtry
2075endfunc
2076
2077func Test_terminal_api_call_fail_delete()
2078 CheckRunVimInTerminal
2079
2080 call WriteApiCall('Tapi_Delete')
2081 let buf = RunVimInTerminal('-S Xscript', {})
2082 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
2083
2084 call StopVimInTerminal(buf)
2085 call delete('Xscript')
2086 call ch_logfile('', '')
2087endfunc
2088
2089func Test_terminal_setapi_and_call()
2090 CheckRunVimInTerminal
2091
2092 call WriteApiCall('Tapi_TryThis')
2093 call ch_logfile('Xlog', 'w')
2094
2095 unlet! g:called_bufnum
2096 unlet! g:called_arg
2097
2098 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
2099 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2100 call assert_false(exists('g:called_bufnum'))
2101 call assert_false(exists('g:called_arg'))
2102
2103 eval buf->term_setapi('Tapi_')
2104 call term_sendkeys(buf, ":set notitle\<CR>")
2105 call term_sendkeys(buf, ":source Xscript\<CR>")
2106 call WaitFor({-> exists('g:called_bufnum')})
2107 call assert_equal(buf, g:called_bufnum)
2108 call assert_equal(['hello', 123], g:called_arg)
2109
2110 call StopVimInTerminal(buf)
2111
2112 call delete('Xscript')
2113 call ch_logfile('')
2114 call delete('Xlog')
2115 unlet! g:called_bufnum
2116 unlet! g:called_arg
2117endfunc
2118
2119func Test_terminal_api_arg()
2120 CheckRunVimInTerminal
2121
2122 call WriteApiCall('Tapi_TryThis')
2123 call ch_logfile('Xlog', 'w')
2124
2125 unlet! g:called_bufnum
2126 unlet! g:called_arg
2127
2128 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2129 let buf = bufnr('%')
2130 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2131 call assert_false(exists('g:called_bufnum'))
2132 call assert_false(exists('g:called_arg'))
2133
2134 call StopVimInTerminal(buf)
2135
2136 call ch_logfile('Xlog', 'w')
2137
2138 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2139 let buf = bufnr('%')
2140 call WaitFor({-> exists('g:called_bufnum')})
2141 call assert_equal(buf, g:called_bufnum)
2142 call assert_equal(['hello', 123], g:called_arg)
2143
2144 call StopVimInTerminal(buf)
2145
2146 call delete('Xscript')
2147 call ch_logfile('')
2148 call delete('Xlog')
2149 unlet! g:called_bufnum
2150 unlet! g:called_arg
2151endfunc
2152
2153func Test_terminal_ansicolors_default()
2154 CheckFunction term_getansicolors
2155
2156 let colors = [
2157 \ '#000000', '#e00000',
2158 \ '#00e000', '#e0e000',
2159 \ '#0000e0', '#e000e0',
2160 \ '#00e0e0', '#e0e0e0',
2161 \ '#808080', '#ff4040',
2162 \ '#40ff40', '#ffff40',
2163 \ '#4040ff', '#ff40ff',
2164 \ '#40ffff', '#ffffff',
2165 \]
2166
2167 let buf = Run_shell_in_terminal({})
2168 call assert_equal(colors, term_getansicolors(buf))
2169 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002170 call assert_equal([], term_getansicolors(buf))
2171
2172 exe buf . 'bwipe'
2173endfunc
2174
Julio Bcde8ff62025-02-06 20:31:27 +01002175func Test_terminal_ansicolors_default_reset_tgc()
Drew Vogelea67ba72025-05-07 22:05:17 +02002176 CheckScreendump
Julio Bcde8ff62025-02-06 20:31:27 +01002177 CheckFeature termguicolors
2178 CheckRunVimInTerminal
2179
2180 let $PS1="$ "
2181 let buf = RunVimInTerminal('-c "term sh"', {'rows': 12})
2182 call TermWait(buf)
2183 " Wait for the shell to display a prompt
2184 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2185
2186 call term_sendkeys(buf, "printf '\\033[0;30;41mhello world\\033[0m\\n'\<CR>")
2187 call WaitForAssert({-> assert_match('hello world', term_getline(buf, 2))})
2188 call term_sendkeys(buf, "\<C-W>:set notgc\<CR>")
2189 call term_sendkeys(buf, "printf '\\033[0;30;41mhello world\\033[0m\\n'\<CR>")
2190 call WaitForAssert({-> assert_match('hello world', term_getline(buf, 4))})
2191
2192 call VerifyScreenDump(buf, 'Test_terminal_ansi_reset_tgc', {})
2193
2194 call term_sendkeys(buf, "exit\<CR>")
2195 call TermWait(buf)
2196 call StopVimInTerminal(buf)
2197 unlet! $PS1
2198endfunc
2199
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002200let s:test_colors = [
2201 \ '#616e64', '#0d0a79',
2202 \ '#6d610d', '#0a7373',
2203 \ '#690d0a', '#6d696e',
2204 \ '#0d0a6f', '#616e0d',
2205 \ '#0a6479', '#6d0d0a',
2206 \ '#617373', '#0d0a69',
2207 \ '#6d690d', '#0a6e6f',
2208 \ '#610d0a', '#6e6479',
2209 \]
2210
2211func Test_terminal_ansicolors_global()
2212 CheckFeature termguicolors
2213 CheckFunction term_getansicolors
2214
LemonBoyb2b3acb2022-05-20 10:10:34 +01002215 if has('vtp') && !has('vcon') && !has('gui_running')
2216 throw 'Skipped: does not support termguicolors'
2217 endif
2218
2219 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002220 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2221 let buf = Run_shell_in_terminal({})
2222 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
2223 call StopShellInTerminal(buf)
LemonBoyb2b3acb2022-05-20 10:10:34 +01002224 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002225
2226 exe buf . 'bwipe'
2227 unlet g:terminal_ansi_colors
2228endfunc
2229
2230func Test_terminal_ansicolors_func()
2231 CheckFeature termguicolors
2232 CheckFunction term_getansicolors
2233
LemonBoyb2b3acb2022-05-20 10:10:34 +01002234 if has('vtp') && !has('vcon') && !has('gui_running')
2235 throw 'Skipped: does not support termguicolors'
2236 endif
2237
2238 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002239 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2240 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
2241 call assert_equal(s:test_colors, term_getansicolors(buf))
2242
2243 call term_setansicolors(buf, g:terminal_ansi_colors)
2244 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
2245
2246 let colors = [
2247 \ 'ivory', 'AliceBlue',
2248 \ 'grey67', 'dark goldenrod',
2249 \ 'SteelBlue3', 'PaleVioletRed4',
2250 \ 'MediumPurple2', 'yellow2',
2251 \ 'RosyBrown3', 'OrangeRed2',
2252 \ 'white smoke', 'navy blue',
2253 \ 'grey47', 'gray97',
2254 \ 'MistyRose2', 'DodgerBlue4',
2255 \]
2256 eval buf->term_setansicolors(colors)
2257
2258 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002259 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaard83392a2022-09-01 12:22:46 +01002260 call assert_fails('call term_setansicolors(buf, {})', 'E1211:')
Bram Moolenaar23919542023-05-05 22:12:22 +01002261 call assert_fails('call term_setansicolors(buf, [])', 'E475: Invalid value for argument "colors"')
LemonBoyb2b3acb2022-05-20 10:10:34 +01002262 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002263
2264 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002265 call assert_equal(0, term_setansicolors(buf, []))
2266 exe buf . 'bwipe'
2267endfunc
2268
2269func Test_terminal_all_ansi_colors()
Drew Vogelea67ba72025-05-07 22:05:17 +02002270 CheckScreendump
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002271 CheckRunVimInTerminal
2272
2273 " Use all the ANSI colors.
2274 call writefile([
2275 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
2276 \ 'hi Tblack ctermfg=0 ctermbg=8',
2277 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
2278 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
2279 \ 'hi Tbrown ctermfg=3 ctermbg=11',
2280 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
2281 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
2282 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
2283 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
2284 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
2285 \ 'hi Tred ctermfg=9 ctermbg=1',
2286 \ 'hi Tgreen ctermfg=10 ctermbg=2',
2287 \ 'hi Tyellow ctermfg=11 ctermbg=3',
2288 \ 'hi Tblue ctermfg=12 ctermbg=4',
2289 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
2290 \ 'hi Tcyan ctermfg=14 ctermbg=6',
2291 \ 'hi Twhite ctermfg=15 ctermbg=7',
2292 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
2293 \ 'hi TgreenBold ctermfg=10 cterm=bold',
2294 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
2295 \ '',
2296 \ 'call matchadd("Tblack", "A")',
2297 \ 'call matchadd("Tdarkred", "B")',
2298 \ 'call matchadd("Tdarkgreen", "C")',
2299 \ 'call matchadd("Tbrown", "D")',
2300 \ 'call matchadd("Tdarkblue", "E")',
2301 \ 'call matchadd("Tdarkmagenta", "F")',
2302 \ 'call matchadd("Tdarkcyan", "G")',
2303 \ 'call matchadd("Tlightgrey", "H")',
2304 \ 'call matchadd("Tdarkgrey", "I")',
2305 \ 'call matchadd("Tred", "J")',
2306 \ 'call matchadd("Tgreen", "K")',
2307 \ 'call matchadd("Tyellow", "L")',
2308 \ 'call matchadd("Tblue", "M")',
2309 \ 'call matchadd("Tmagenta", "N")',
2310 \ 'call matchadd("Tcyan", "O")',
2311 \ 'call matchadd("Twhite", "P")',
2312 \ 'call matchadd("TdarkredBold", "X")',
2313 \ 'call matchadd("TgreenBold", "Y")',
2314 \ 'call matchadd("TmagentaBold", "Z")',
2315 \ 'redraw',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01002316 \ ], 'Xcolorscript', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002317 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2318 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2319
2320 call term_sendkeys(buf, ":q\<CR>")
2321 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002322endfunc
2323
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002324function On_BufFilePost()
2325 doautocmd <nomodeline> User UserEvent
2326endfunction
2327
2328func Test_terminal_nested_autocmd()
2329 new
2330 call setline(1, range(500))
2331 $
2332 let lastline = line('.')
2333
2334 augroup TermTest
2335 autocmd BufFilePost * call On_BufFilePost()
2336 autocmd User UserEvent silent
2337 augroup END
2338
2339 let cmd = Get_cat_123_cmd()
2340 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2341 call assert_equal(lastline, line('.'))
2342
Bram Moolenaar64374752021-04-03 17:22:29 +02002343 let job = term_getjob(buf)
2344 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002345 call delete('Xtext')
2346 augroup TermTest
2347 au!
2348 augroup END
2349endfunc
2350
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002351func Test_terminal_adds_jump()
2352 clearjumps
2353 call term_start("ls", #{curwin: 1})
2354 call assert_equal(1, getjumplist()[0]->len())
2355 bwipe!
2356endfunc
2357
Bram Moolenaareea32af2021-11-21 14:51:13 +00002358func Close_cb(ch, ctx)
2359 call term_wait(a:ctx.bufnr)
2360 let g:close_done = 'done'
2361endfunc
2362
2363func Test_term_wait_in_close_cb()
2364 let g:close_done = ''
2365 let ctx = {}
2366 let ctx.bufnr = term_start('echo "HELLO WORLD"',
2367 \ {'close_cb': {ch -> Close_cb(ch, ctx)}})
2368
2369 call WaitForAssert({-> assert_equal("done", g:close_done)})
2370
2371 unlet g:close_done
2372 bwipe!
2373endfunc
2374
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01002375func Test_term_TextChangedT()
2376 augroup TermTest
2377 autocmd TextChangedT * ++once
2378 \ execute expand('<abuf>') . 'buffer' |
2379 \ let b:called = 1 |
2380 \ split |
2381 \ enew
2382 augroup END
2383
2384 terminal
2385
2386 let term_buf = bufnr()
2387
2388 let b:called = 0
2389
2390 call term_sendkeys(term_buf, "aaabbc\r")
2391 call TermWait(term_buf)
2392
2393 call assert_equal(1, getbufvar(term_buf, 'called'))
2394
2395 " Current buffer will be restored
2396 call assert_equal(bufnr(), term_buf)
2397
2398 bwipe!
2399 augroup TermTest
2400 au!
2401 augroup END
2402endfunc
2403
2404func Test_term_TextChangedT_close()
2405 augroup TermTest
2406 autocmd TextChangedT * ++once split | enew | 1close!
2407 augroup END
2408
2409 terminal
2410
2411 let term_buf = bufnr()
2412
2413 call term_sendkeys(term_buf, "aaabbc\r")
2414 call TermWait(term_buf)
2415
2416 " Current buffer will be restored
2417 call assert_equal(bufnr(), term_buf)
2418
2419 bwipe!
2420 augroup TermTest
2421 au!
2422 augroup END
2423endfunc
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002424
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002425" vim: shiftwidth=2 sts=2 expandtab