blob: 0597c36a6f5057e18c0313cc99854eea1f1304c3 [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 +02005CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02006
Christian Brabandteb380b92025-07-07 20:53:55 +02007source util/screendump.vim
8source util/mouse.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020011let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020012
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020013func Test_terminal_basic()
ichizokae1bd872022-01-20 14:57:29 +000014 call test_override('vterm_title', 1)
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020015 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020016 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020017
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020018 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020019 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020020 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020021 call assert_match('%aR[^\n]*running]', execute('ls R'))
22 call assert_notmatch('%[^\n]*running]', execute('ls F'))
23 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar004a6782020-04-11 17:09:31 +020024 call assert_fails('set modifiable', 'E946:')
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020025
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020026 call StopShellInTerminal(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020027 call assert_equal('n', mode())
28 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020029 call assert_match('%aF[^\n]*finished]', execute('ls F'))
30 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
31 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020032
Bram Moolenaar94053a52017-08-01 21:44:33 +020033 " closing window wipes out the terminal buffer a with finished job
34 close
35 call assert_equal("", bufname(buf))
36
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020037 au! TerminalOpen
ichizokae1bd872022-01-20 14:57:29 +000038 call test_override('ALL', 0)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020039 unlet g:job
40endfunc
41
Bram Moolenaar00806bc2020-11-05 19:36:38 +010042func Test_terminal_no_name()
43 let buf = Run_shell_in_terminal({})
44 call assert_match('^!', bufname(buf))
45 0file
46 call assert_equal("", bufname(buf))
47 call assert_match('\[No Name\]', execute('file'))
48 call StopShellInTerminal(buf)
Bram Moolenaar00806bc2020-11-05 19:36:38 +010049endfunc
50
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020051func Test_terminal_TerminalWinOpen()
52 au TerminalWinOpen * let b:done = 'yes'
53 let buf = Run_shell_in_terminal({})
54 call assert_equal('yes', b:done)
55 call StopShellInTerminal(buf)
56 " closing window wipes out the terminal buffer with the finished job
57 close
58
59 if has("unix")
Yee Cheng Chine70587d2025-02-13 20:55:45 +010060 terminal ++hidden ++open echo
61 call WaitForAssert({-> assert_equal('terminal', &buftype)})
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020062 call assert_fails("echo b:done", 'E121:')
63 endif
64
65 au! TerminalWinOpen
66endfunc
67
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020068func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020069 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020070 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071
72 setlocal modifiable
73 exe "normal Axxx\<Esc>"
Bram Moolenaar28ee8922020-10-28 20:20:00 +010074 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020075 undo
76
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020077 exe buf . 'bwipe'
78 unlet g:job
79endfunc
80
Bram Moolenaar5b868a82019-02-25 06:11:53 +010081func Test_terminal_paste_register()
82 let @" = "text to paste"
83
84 let buf = Run_shell_in_terminal({})
85 " Wait for the shell to display a prompt
86 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
87
88 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
89 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020090 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010091
92 exe buf . 'bwipe!'
93 unlet g:job
94endfunc
95
Yee Cheng Chin42826332022-10-10 11:46:16 +010096func Test_terminal_unload_buffer()
97 let buf = Run_shell_in_terminal({})
98 call assert_fails(buf . 'bunload', 'E948:')
99 exe buf . 'bunload!'
100 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
101 call assert_equal("", bufname(buf))
102
103 unlet g:job
104endfunc
105
Bram Moolenaar94053a52017-08-01 21:44:33 +0200106func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200107 let buf = Run_shell_in_terminal({})
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100108 call assert_fails(buf . 'bwipe', 'E948:')
Bram Moolenaareb44a682017-08-03 22:44:55 +0200109 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200110 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200111 call assert_equal("", bufname(buf))
112
113 unlet g:job
114endfunc
115
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100116" Test that using ':confirm bwipe' on terminal works
117func Test_terminal_confirm_wipe_buffer()
118 CheckUnix
119 CheckNotGui
120 CheckFeature dialog_con
121 let buf = Run_shell_in_terminal({})
122 call assert_fails(buf . 'bwipe', 'E948:')
123 call feedkeys('n', 'L')
124 call assert_fails('confirm ' .. buf .. 'bwipe', 'E517:')
125 call assert_equal(buf, bufnr())
126 call assert_equal(1, &modified)
127 call feedkeys('y', 'L')
128 exe 'confirm ' .. buf .. 'bwipe'
129 call assert_notequal(buf, bufnr())
130 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
131 call assert_equal("", bufname(buf))
132
133 unlet g:job
134endfunc
135
136" Test that using :b! will hide the terminal
137func Test_terminal_goto_buffer()
138 let buf_mod = bufnr()
139 let buf_term = Run_shell_in_terminal({})
140 call assert_equal(buf_term, bufnr())
141 call assert_fails(buf_mod . 'b', 'E948:')
142 exe buf_mod . 'b!'
143 call assert_equal(buf_mod, bufnr())
144 call assert_equal('run', job_status(g:job))
145 call assert_notequal('', bufname(buf_term))
146 exec buf_mod .. 'bwipe!'
147 exec buf_term .. 'bwipe!'
148
149 unlet g:job
150endfunc
151
152" Test that using ':confirm :b' will kill terminal
153func Test_terminal_confirm_goto_buffer()
154 CheckUnix
155 CheckNotGui
156 CheckFeature dialog_con
157 let buf_mod = bufnr()
158 let buf_term = Run_shell_in_terminal({})
159 call feedkeys('n', 'L')
160 exe 'confirm ' .. buf_mod .. 'b'
161 call assert_equal(buf_term, bufnr())
162 call feedkeys('y', 'L')
163 exec 'confirm ' .. buf_mod .. 'b'
164 call assert_equal(buf_mod, bufnr())
165 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
166 call assert_equal("", bufname(buf_term))
167 exec buf_mod .. 'bwipe!'
168
169 unlet g:job
170endfunc
171
172" Test that using :close! will hide the terminal
173func Test_terminal_close_win()
174 let buf = Run_shell_in_terminal({})
175 call assert_equal(buf, bufnr())
176 call assert_fails('close', 'E948:')
177 close!
178 call assert_notequal(buf, bufnr())
179 call assert_equal('run', job_status(g:job))
180 call assert_notequal('', bufname(buf))
181 exec buf .. 'bwipe!'
182
183 unlet g:job
184endfunc
185
186" Test that using ':confirm close' will kill terminal
187func Test_terminal_confirm_close_win()
188 CheckUnix
189 CheckNotGui
190 CheckFeature dialog_con
191 let buf = Run_shell_in_terminal({})
192 call feedkeys('n', 'L')
193 confirm close
194 call assert_equal(buf, bufnr())
195 call feedkeys('y', 'L')
196 confirm close
197 call assert_notequal(buf, bufnr())
198 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
199 call assert_equal("", bufname(buf))
200
201 unlet g:job
202endfunc
203
204" Test that using :quit! will kill the terminal
205func Test_terminal_quit()
206 let buf = Run_shell_in_terminal({})
207 call assert_equal(buf, bufnr())
208 call assert_fails('quit', 'E948:')
209 quit!
210 call assert_notequal(buf, bufnr())
211 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Yee Cheng Chin42826332022-10-10 11:46:16 +0100212 call assert_equal("", bufname(buf))
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100213
214 unlet g:job
215endfunc
216
217" Test that using ':confirm quit' will kill terminal
218func Test_terminal_confirm_quit()
219 CheckUnix
220 CheckNotGui
221 CheckFeature dialog_con
222 let buf = Run_shell_in_terminal({})
223 call feedkeys('n', 'L')
224 confirm quit
225 call assert_equal(buf, bufnr())
226 call feedkeys('y', 'L')
227 confirm quit
228 call assert_notequal(buf, bufnr())
229 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
230
231 unlet g:job
232endfunc
233
234" Test :q or :next
235
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200236func Test_terminal_split_quit()
237 let buf = Run_shell_in_terminal({})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200238 split
239 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200240 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200241 sleep 50m
242 call assert_equal('run', job_status(g:job))
243
244 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200245 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200246
Yee Cheng Chin42826332022-10-10 11:46:16 +0100247 call assert_equal("", bufname(buf))
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200248 unlet g:job
249endfunc
250
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100251func Test_terminal_hide_buffer_job_running()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200252 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200253 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200254 quit
255 for nr in range(1, winnr('$'))
256 call assert_notequal(winbufnr(nr), buf)
257 endfor
258 call assert_true(bufloaded(buf))
259 call assert_true(buflisted(buf))
260
261 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200262 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200263 exe buf . 'bwipe'
264
265 unlet g:job
266endfunc
267
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100268func Test_terminal_hide_buffer_job_finished()
269 term echo hello
270 let buf = bufnr()
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100271 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Yee Cheng Chin42826332022-10-10 11:46:16 +0100272
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100273 call assert_true(bufloaded(buf))
274 call assert_true(buflisted(buf))
Yee Cheng Chin42826332022-10-10 11:46:16 +0100275
276 " Test :hide
277 hide
278 call assert_true(bufloaded(buf))
279 call assert_true(buflisted(buf))
280 split
281 exe buf .. 'buf'
282 call assert_equal(buf, bufnr())
283
284 " Test bufhidden, which exercises a different code path
285 setlocal bufhidden=hide
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100286 edit Xasdfasdf
287 call assert_true(bufloaded(buf))
288 call assert_true(buflisted(buf))
289 exe buf .. 'buf'
290 call assert_equal(buf, bufnr())
291 setlocal bufhidden=
Yee Cheng Chin42826332022-10-10 11:46:16 +0100292
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100293 edit Xasdfasdf
294 call assert_false(bufloaded(buf))
295 call assert_false(buflisted(buf))
296 bwipe Xasdfasdf
297endfunc
298
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000299func Test_terminal_rename_buffer()
300 let cmd = Get_cat_123_cmd()
301 let buf = term_start(cmd, {'term_name': 'foo'})
302 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
303 call assert_equal('foo', bufname())
304 call assert_match('foo.*finished', execute('ls'))
305 file bar
306 call assert_equal('bar', bufname())
307 call assert_match('bar.*finished', execute('ls'))
308 exe 'bwipe! ' .. buf
Christian Brabandt84bc00e2023-07-13 11:45:54 +0200309 call delete('Xtext')
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000310endfunc
311
Bram Moolenaar1e115362019-01-09 23:01:02 +0100312func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200313 exe g:buf . 'bwipe!'
314 let g:buf = 0
315endfunc
316
Bram Moolenaar9d189612017-09-09 18:11:00 +0200317func Get_cat_123_cmd()
318 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100319 if !has('conpty')
Milly4f5681d2024-10-20 11:06:00 +0200320 return 'cmd /D /c "cls && color 2 && echo 123"'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100321 else
322 " When clearing twice, extra sequence is not output.
Milly4f5681d2024-10-20 11:06:00 +0200323 return 'cmd /D /c "cls && cls && color 2 && echo 123"'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100324 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200325 else
326 call writefile(["\<Esc>[32m123"], 'Xtext')
327 return "cat Xtext"
328 endif
329endfunc
330
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200331func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200332 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200333 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
334 let g:job = term_getjob(g:buf)
335
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200336 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
337 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200338 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100339 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200340 call delete('Xtext')
341endfunc
342
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200343func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200344 let l = term_scrape(a:buf, 0)
345 call assert_true(len(l) == 0)
346 let l = term_scrape(a:buf, 999)
347 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200348 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200349 call assert_true(len(l) > 0)
350 call assert_equal('1', l[0].chars)
351 call assert_equal('2', l[1].chars)
352 call assert_equal('3', l[2].chars)
353 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200354 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
355 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100356 if has('win32')
357 " On Windows 'background' always defaults to dark, even though the terminal
358 " may use a light background. Therefore accept both white and black.
359 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200360 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100361 if &background == 'light'
362 call assert_equal('#ffffff', l[0].bg)
363 else
364 call assert_equal('#000000', l[0].bg)
365 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200366 endif
367
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200368 let l = term_getline(a:buf, -1)
369 call assert_equal('', l)
370 let l = term_getline(a:buf, 0)
371 call assert_equal('', l)
372 let l = term_getline(a:buf, 999)
373 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200374 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200375 call assert_equal('123', l)
376endfunc
377
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200378func Test_terminal_scrape_123()
379 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200380 let buf = term_start(cmd)
381
382 let termlist = term_list()
383 call assert_equal(1, len(termlist))
384 call assert_equal(buf, termlist[0])
385
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200386 " Nothing happens with invalid buffer number
387 call term_wait(1234)
388
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200389 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200390 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200391 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200392 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200393 call Check_123(buf)
394
395 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100396 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200397 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200398 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200399 call Check_123(buf)
400
401 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200402 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200403endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200404
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200405func Test_terminal_scrape_multibyte()
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100406 call writefile(["léttまrs"], 'Xtext', 'D')
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200407 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200408 " Run cmd with UTF-8 codepage to make the type command print the expected
409 " multibyte characters.
Milly4f5681d2024-10-20 11:06:00 +0200410 let buf = term_start("cmd /D /K chcp 65001")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100411 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200412 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100413 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200414 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100415 let buf = term_start("cat Xtext")
416 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200417 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200418
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100419 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
420 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200421 call assert_true(len(l) >= 7)
422 call assert_equal('l', l[0].chars)
423 call assert_equal('é', l[1].chars)
424 call assert_equal(1, l[1].width)
425 call assert_equal('t', l[2].chars)
426 call assert_equal('t', l[3].chars)
427 call assert_equal('ま', l[4].chars)
428 call assert_equal(2, l[4].width)
429 call assert_equal('r', l[5].chars)
430 call assert_equal('s', l[6].chars)
431
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100432 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200433 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200434 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200435
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100436 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200437endfunc
438
Bram Moolenaar8b896142020-08-02 15:05:05 +0200439func Test_terminal_one_column()
440 " This creates a terminal, displays a double-wide character and makes the
441 " window one column wide. This used to cause a crash.
442 let width = &columns
443 botright vert term
444 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200445 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200446 exe "set columns=" .. (width / 2)
447 redraw
448 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200449 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200450 exe "set columns=" .. width
451 exe buf . 'bwipe!'
452endfunc
453
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200454func Test_terminal_scroll()
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100455 call writefile(range(1, 200), 'Xtext', 'D')
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200456 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +0200457 let cmd = 'cmd /D /c "type Xtext"'
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200458 else
459 let cmd = "cat Xtext"
460 endif
461 let buf = term_start(cmd)
462
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100463 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200464 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200465 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200466
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100467 " wait until the scrolling stops
468 while 1
469 let scrolled = buf->term_getscrolled()
470 sleep 20m
471 if scrolled == buf->term_getscrolled()
472 break
473 endif
474 endwhile
475
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200476 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200477 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200478 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200479 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200480 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200481 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200482
483 exe buf . 'bwipe'
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200484endfunc
485
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200486func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200487 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200488 set termwinscroll=100
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100489 call writefile(range(150), 'Xtext', 'D')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200490 if has('win32')
491 call term_sendkeys(buf, "type Xtext\<CR>")
492 else
493 call term_sendkeys(buf, "cat Xtext\<CR>")
494 endif
495 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200496 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200497 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200498 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200499 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200500
Milly6d5f4a02024-10-22 23:17:45 +0200501 " When 'termwinscroll' becomes small, the scrollback should become small.
502 set termwinscroll=20
503 call term_sendkeys(buf, "echo set20\<CR>")
504 call WaitForAssert({-> assert_true([term_getline(buf, rows - 1), term_getline(buf, rows - 2)]->index('set20') >= 0)})
505 let lines = line('$')
506 call assert_inrange(19, 20, lines)
507
508 " When 'termwinscroll' under 10 which means 10% of it will be 0,
509 " the scrollback should become small.
510 set termwinscroll=1
511 call term_sendkeys(buf, "echo set1\<CR>")
512 call WaitForAssert({-> assert_true([term_getline(buf, rows - 1), term_getline(buf, rows - 2)]->index('set1') >= 0)})
513 let lines = line('$')
514 call assert_inrange(1, 2, lines)
515
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200516 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200517 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200518 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100519endfunc
520
521func Test_terminal_postponed_scrollback()
Drew Vogelea67ba72025-05-07 22:05:17 +0200522 CheckScreendump
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200523 " tail -f only works on Unix
524 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100525
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100526 call writefile(range(50), 'Xtext', 'D')
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100527 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100528 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100529 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100530 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100531 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
532 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100533 \ 'call feedkeys("\<C-W>N", "xt")',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100534 \ ], 'XTest_postponed', 'D')
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100535 let buf = RunVimInTerminal('-S XTest_postponed', {})
536 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100537 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100538
539 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200540 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100541 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100542 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100543
544 " Back to Terminal-Job mode, text will scroll and show the extra line.
545 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100546 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100547
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100548 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100549 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200550 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100551 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100552 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200553 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100554 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100555 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
556
557 " check type of term_sendkeys() return value
558 echo type(tsk_ret)
559
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100560 call StopVimInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200561endfunc
562
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100563" Run diff on two dumps with different size.
564func Test_terminal_dumpdiff_size()
565 call assert_equal(1, winnr('$'))
566 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
567 call assert_equal(2, winnr('$'))
568 call assert_match('Test_incsearch_search_01.dump', getline(10))
569 call assert_match(' +++++$', getline(11))
570 call assert_match('Test_popup_command_01.dump', getline(31))
571 call assert_equal(repeat('+', 75), getline(30))
572 quit
573endfunc
574
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200575func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200576 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200577
Bram Moolenaarb2412082017-08-20 18:09:14 +0200578 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200579 let size = term_getsize('')
580 bwipe!
581 call assert_equal(5, size[0])
582
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200583 call term_start(cmd, {'term_rows': 6})
584 let size = term_getsize('')
585 bwipe!
586 call assert_equal(6, size[0])
587
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200588 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200589 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200590 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200591
592 call term_setsize('', 6, 0)
593 call assert_equal([6, 33], term_getsize(''))
594
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200595 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200596 call assert_equal([6, 35], term_getsize(''))
597
598 call term_setsize('', 7, 30)
599 call assert_equal([7, 30], term_getsize(''))
600
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200601 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200602 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200603
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200604 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
605 let size = term_getsize('')
606 bwipe!
607 call assert_equal([6, 36], size)
608
Bram Moolenaarb2412082017-08-20 18:09:14 +0200609 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200610 let size = term_getsize('')
611 bwipe!
612 call assert_equal(20, size[1])
613
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200614 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200615 let size = term_getsize('')
616 bwipe!
617 call assert_equal(26, size[1])
618
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200619 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200620 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200621 let size = term_getsize('')
622 bwipe!
623 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200624
625 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
626 let size = term_getsize('')
627 bwipe!
628 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200629
Bram Moolenaar5300be62021-11-13 10:27:40 +0000630 call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:')
631 call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100632 call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:')
Bram Moolenaar88137392021-11-12 16:01:15 +0000633
Kenta Satoc28e7a22023-05-08 18:26:03 +0100634 call assert_fails("call term_start(cmd, {'term_cols': -1})", 'E475:')
635 call assert_fails("call term_start(cmd, {'term_cols': 1001})", 'E475:')
636 call assert_fails("call term_start(cmd, {'term_cols': 10.0})", 'E805:')
637
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200638 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200639endfunc
640
Bram Moolenaareba13e42021-02-23 17:47:23 +0100641func Test_terminal_zero_height()
642 split
643 wincmd j
644 anoremenu 1.1 WinBar.test :
645 terminal ++curwin
646 wincmd k
647 wincmd _
648 redraw
649
650 call term_sendkeys(bufnr(), "exit\r")
651 bwipe!
652endfunc
653
Bram Moolenaarda43b612017-08-11 22:27:50 +0200654func Test_terminal_curwin()
655 let cmd = Get_cat_123_cmd()
656 call assert_equal(1, winnr('$'))
657
Bram Moolenaarb1009092020-05-31 16:04:42 +0200658 split Xdummy
659 call setline(1, 'dummy')
660 write
661 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200662 exe 'terminal ++curwin ' . cmd
663 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200664 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200665 bwipe!
666
Bram Moolenaarb1009092020-05-31 16:04:42 +0200667 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200668 call term_start(cmd, {'curwin': 1})
669 call assert_equal(2, winnr('$'))
670 bwipe!
671
Bram Moolenaarb1009092020-05-31 16:04:42 +0200672 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200673 call setline(1, 'change')
674 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
675 call assert_equal(2, winnr('$'))
676 exe 'terminal! ++curwin ' . cmd
677 call assert_equal(2, winnr('$'))
678 bwipe!
679
Bram Moolenaarb1009092020-05-31 16:04:42 +0200680 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200681 call setline(1, 'change')
682 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
683 call assert_equal(2, winnr('$'))
684 bwipe!
685
Bram Moolenaarb1009092020-05-31 16:04:42 +0200686 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200687 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200688 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200689 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200690endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200691
Bram Moolenaarff546792017-11-21 14:47:57 +0100692func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200693 if s:python != ''
694 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200695 " 500 was not enough for Travis
696 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200697 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200698 echo 'This will take five seconds...'
699 let waittime = 2000
700 if has('win32')
701 let cmd = $windir . '\system32\timeout.exe 1'
702 else
703 let cmd = 'sleep 1'
704 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200705 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100706 return [cmd, waittime]
707endfunc
708
709func Test_terminal_finish_open_close()
710 call assert_equal(1, winnr('$'))
711
712 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200713
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100714 " shell terminal closes automatically
715 terminal
716 let buf = bufnr('%')
717 call assert_equal(2, winnr('$'))
718 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200719 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200720 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200721 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100722
723 " shell terminal that does not close automatically
724 terminal ++noclose
725 let buf = bufnr('%')
726 call assert_equal(2, winnr('$'))
727 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200728 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200729 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100730 call assert_equal(2, winnr('$'))
731 quit
732 call assert_equal(1, winnr('$'))
733
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200734 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200735 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200736 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200737 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200738
739 call term_start(cmd, {'term_finish': 'close'})
740 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200741 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200742 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200743 call assert_equal(1, winnr('$'))
744
745 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200746 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200747 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200748 bwipe
749
750 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200751 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200752 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200753 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200754
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200755 exe 'terminal ++hidden ++open ' . cmd
756 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200757 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200758 bwipe
759
760 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
761 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200762 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200763 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200764
765 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
766 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
767 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
768 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
769
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100770 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 +0200771 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200772 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200773 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100774 call assert_equal('opened the buffer in a window', g:result)
775 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200776 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200777endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200778
779func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100780 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +0200781 let cmd = 'cmd /D /c cd'
Bram Moolenaar077ff432019-10-28 00:42:21 +0100782 else
783 CheckExecutable pwd
784 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200785 endif
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100786 call mkdir('Xtermdir')
787 let buf = term_start(cmd, {'cwd': 'Xtermdir'})
Bram Moolenaaree7c8d92022-09-22 12:57:06 +0100788 " if the path is very long it may be split over two lines, join them
789 " together
790 call WaitForAssert({-> assert_equal('Xtermdir', fnamemodify(getline(1) .. getline(2), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200791
792 exe buf . 'bwipe'
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100793 call delete('Xtermdir', 'rf')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200794endfunc
795
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200796func Test_terminal_cwd_failure()
797 " Case 1: Provided directory is not actually a directory. Attempt to make
798 " the file executable as well.
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100799 call writefile([], 'Xtcfile', 'D')
Dominique Pelle91a874e2022-09-03 10:59:32 +0100800 call setfperm('Xtcfile', 'rwx------')
801 call assert_fails("call term_start(&shell, {'cwd': 'Xtcfile'})", 'E475:')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200802
803 " Case 2: Directory does not exist.
804 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
805
806 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100807 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200808 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100809 call mkdir('XdirNoAccess', '', '0600')
810 " return early if the directory permissions could not be set properly
811 if getfperm('XdirNoAccess')[2] == 'x'
812 call delete('XdirNoAccess', 'rf')
813 return
814 endif
815 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
816 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200817 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200818endfunc
819
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100820func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200821 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200822 call s:test_environment("VIM_SERVERNAME", v:servername)
823endfunc
824
825func Test_terminal_version()
826 call s:test_environment("VIM_TERMINAL", string(v:version))
827endfunc
828
829func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100830 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100831 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200832 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100833 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200834 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100835 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200836 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100837 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200838 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200839 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200840 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100841
Bram Moolenaar012eb662018-03-13 17:55:27 +0100842 exe buf . 'bwipe'
843 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100844endfunc
845
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200846func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100847 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200848 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200849 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100850 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100851 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100852 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100853 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100854 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200855 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200856 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200857 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200858
Bram Moolenaar012eb662018-03-13 17:55:27 +0100859 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200860endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200861
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200862func Test_terminal_list_args()
863 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100864 call assert_fails(buf . 'bwipe', 'E948:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200865 exe buf . 'bwipe!'
866 call assert_equal("", bufname(buf))
867endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200868
869func Test_terminal_noblock()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100870 let g:test_is_flaky = 1
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100871 let buf = term_start(&shell)
Zdenek Dohnaldd2dfb32022-02-23 14:25:17 +0000872 " Starting a terminal can be slow, esp. on busy CI machines.
873 let wait_time = 7500
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100874 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100875 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200876 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100877 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200878 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100879 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100880 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100881 " NPFS is used in Windows, nonblocking mode does not work properly.
882 elseif has('win32')
883 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200884 else
885 let len = 5000
886 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200887
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100888 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100889 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100890 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200891 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100892 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200893
894 " On MS-Windows there is an extra empty line below "done". Find "done" in
895 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100896 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100897 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100898 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200899 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100900 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200901 endif
902 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200903
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100904 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200905 call StopShellInTerminal(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200906 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200907 bwipe
908endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200909
910func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100911 " TODO: enable once writing to stdin works on MS-Windows
912 CheckNotMSWindows
913 CheckExecutable wc
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100914 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100915
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200916 call setline(1, ['one', 'two', 'three'])
917 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200918 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200919 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200920 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100921 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200922
923 call setline(1, ['one', 'two', 'three', 'four'])
924 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200925 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200926 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200927 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100928 %bwipe!
929endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200930
Bram Moolenaar21109272020-01-30 16:27:20 +0100931func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200932 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100933 let g:test_is_flaky = 1
Bram Moolenaardada6d22017-09-02 17:18:35 +0200934
Bram Moolenaar21109272020-01-30 16:27:20 +0100935 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200936 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100937 " MS-Windows echoes the input, Unix doesn't.
938 if has('win32')
939 call WaitFor({-> getline('$') =~ 'exit(123)'})
940 call assert_equal('hello', getline(line('$') - 1))
941 else
942 call WaitFor({-> getline('$') =~ 'hello'})
943 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200944 endif
Christian Brabandt2e4361b2025-02-09 17:18:07 +0100945 let exitval = bufnr()->term_getjob()->job_info().exitval
946 if !has('win32')
947 call assert_equal(123, exitval)
948 else
949 " python 3.13 on Windows returns exit code 1
950 " older versions returned correctly exit code 123
951 " https://github.com/python/cpython/issues/129900
952 call assert_match('1\|123', exitval)
953 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100954 %bwipe!
955endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200956
Bram Moolenaar21109272020-01-30 16:27:20 +0100957func Test_terminal_eof_arg_win32_ctrl_z()
958 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200959 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100960 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100961
962 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200963 exe '1term ++eof=<C-Z> ' .. s:python
Christian Brabandt3cfac592025-02-09 17:22:30 +0100964 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1) .. getline(line('$')))})
965 " until python 3.12 there was an extra line break, with 3.13 it was removed,
966 " so depending on the python version the ^Z is on the last or second-last line
967 call assert_match('\^Z', getline(line('$') - 1) .. getline(line('$')))
Bram Moolenaar21109272020-01-30 16:27:20 +0100968 %bwipe!
969endfunc
970
971func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200972 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100973 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100974
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100975 " Check the last specified ++eof arg is used and does not leak memory.
Bram Moolenaar21109272020-01-30 16:27:20 +0100976 new
977 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200978 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100979 " MS-Windows echoes the input, Unix doesn't.
980 if has('win32')
981 call WaitFor({-> getline('$') =~ 'exit(123)'})
982 call assert_equal('hello', getline(line('$') - 1))
983 else
984 call WaitFor({-> getline('$') =~ 'hello'})
985 call assert_equal('hello', getline('$'))
986 endif
Christian Brabandt2e4361b2025-02-09 17:18:07 +0100987 let exitval = bufnr()->term_getjob()->job_info().exitval
988 if !has('win32')
989 call assert_equal(123, exitval)
990 else
991 " python 3.13 on Windows returns exit code 1
992 " older versions returned correctly exit code 123
993 " https://github.com/python/cpython/issues/129900
994 call assert_match('1\|123', exitval)
995 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100996 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200997endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200998
999func Test_terminal_no_cmd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001000 let g:test_is_flaky = 1
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001001 let buf = term_start('NONE', {})
1002 call assert_notequal(0, buf)
1003
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001004 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001005 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001006 if has('gui_running') && !has('win32')
1007 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001008 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001009 else
1010 " Otherwise using a job works on all systems.
1011 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001012 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001013 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001014
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001015 bwipe!
1016endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001017
1018func Test_terminal_special_chars()
1019 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001020 CheckUnix
1021
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001022 call mkdir('Xdir with spaces', 'R')
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001023 call writefile(['x'], 'Xdir with spaces/quoted"file')
1024 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001025 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +01001026 " make sure the job has finished
1027 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001028
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001029 bwipe
1030endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001031
1032func Test_terminal_wrong_options()
1033 call assert_fails('call term_start(&shell, {
1034 \ "in_io": "file",
1035 \ "in_name": "xxx",
1036 \ "out_io": "file",
1037 \ "out_name": "xxx",
1038 \ "err_io": "file",
1039 \ "err_name": "xxx"
1040 \ })', 'E474:')
1041 call assert_fails('call term_start(&shell, {
1042 \ "out_buf": bufnr("%")
1043 \ })', 'E474:')
1044 call assert_fails('call term_start(&shell, {
1045 \ "err_buf": bufnr("%")
1046 \ })', 'E474:')
1047endfunc
1048
1049func Test_terminal_redir_file()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001050 let g:test_is_flaky = 1
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001051 let cmd = Get_cat_123_cmd()
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001052 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xtrfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001053 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001054 " ConPTY may precede escape sequence. There are things that are not so.
1055 if !has('conpty')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001056 call WaitForAssert({-> assert_notequal(0, len(readfile("Xtrfile")))})
1057 call assert_match('123', readfile('Xtrfile')[0])
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001058 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001059 let g:job = term_getjob(buf)
1060 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +02001061
1062 if has('win32')
1063 " On Windows we cannot delete a file being used by a process. When
1064 " job_status() returns "dead", the process remains for a short time.
1065 " Just wait for a moment.
1066 sleep 50m
1067 endif
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001068 call delete('Xtrfile')
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001069 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001070
1071 if has('unix')
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001072 call writefile(['one line'], 'Xtrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001073 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xtrfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001074 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001075 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +02001076 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001077 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001078 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001079 endif
Christian Brabandt84bc00e2023-07-13 11:45:54 +02001080
1081 call delete('Xtext')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001082endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001083
1084func TerminalTmap(remap)
1085 let buf = Run_shell_in_terminal({})
Bram Moolenaarf4a2ed02021-03-23 16:25:09 +01001086 " Wait for the shell to display a prompt
1087 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001088 call assert_equal('t', mode())
1089
1090 if a:remap
1091 tmap 123 456
1092 else
1093 tnoremap 123 456
1094 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +01001095 " don't use abcde, it's an existing command
1096 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001097 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +01001098 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001099 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001100 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001101 let lnum = term_getcursor(buf)[0]
1102 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +01001103 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001104 else
1105 call assert_match('456', term_getline(buf, lnum))
1106 endif
1107
1108 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001109 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001110
1111 tunmap 123
1112 tunmap 456
1113 call assert_equal('', maparg('123', 't'))
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01001114 exe buf . 'bwipe'
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001115 unlet g:job
1116endfunc
1117
1118func Test_terminal_tmap()
1119 call TerminalTmap(1)
1120 call TerminalTmap(0)
1121endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +02001122
1123func Test_terminal_wall()
1124 let buf = Run_shell_in_terminal({})
1125 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001126 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +02001127 exe buf . 'bwipe'
1128 unlet g:job
1129endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001130
Bram Moolenaar7a760922018-02-19 23:10:02 +01001131func Test_terminal_wqall()
1132 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001133 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001134 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +01001135 exe buf . 'bwipe'
1136 unlet g:job
1137endfunc
1138
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001139func Test_terminal_composing_unicode()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001140 let g:test_is_flaky = 1
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001141 let save_enc = &encoding
1142 set encoding=utf-8
1143
1144 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +02001145 let cmd = "cmd /D /K chcp 65001"
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001146 let lnum = [3, 6, 9]
1147 else
1148 let cmd = &shell
1149 let lnum = [1, 3, 5]
1150 endif
1151
1152 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +02001153 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001154 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +02001155 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001156
Bram Moolenaarebe74b72018-04-21 23:34:43 +02001157 if has('win32')
1158 call assert_equal('cmd', job_info(g:job).cmd[0])
1159 else
1160 call assert_equal(&shell, job_info(g:job).cmd[0])
1161 endif
1162
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001163 " ascii + composing
1164 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001165 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001166 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001167 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001168 call term_sendkeys(buf, "\<cr>")
1169 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001170 let l = term_scrape(buf, lnum[0] + 1)
1171 call assert_equal("a\u0308", l[0].chars)
1172 call assert_equal("b", l[1].chars)
1173 call assert_equal("c", l[2].chars)
1174
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001175 " multibyte + composing: がぎぐげご
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001176 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001177 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001178 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001179 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001180 call term_sendkeys(buf, "\<cr>")
1181 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001182 let l = term_scrape(buf, lnum[1] + 1)
1183 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001184 call assert_equal(2, l[0].width)
1185 call assert_equal("\u304e", l[1].chars)
1186 call assert_equal(2, l[1].width)
1187 call assert_equal("\u304f\u3099", l[2].chars)
1188 call assert_equal(2, l[2].width)
1189 call assert_equal("\u3052", l[3].chars)
1190 call assert_equal(2, l[3].width)
1191 call assert_equal("\u3053\u3099", l[4].chars)
1192 call assert_equal(2, l[4].width)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001193
1194 " \u00a0 + composing
1195 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001196 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001197 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001198 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001199 call term_sendkeys(buf, "\<cr>")
1200 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001201 let l = term_scrape(buf, lnum[2] + 1)
1202 call assert_equal("\u00a0\u0308", l[3].chars)
1203
1204 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001205 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001206 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001207 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001208 let &encoding = save_enc
1209endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +01001210
1211func Test_terminal_aucmd_on_close()
1212 fun Nop()
1213 let s:called = 1
1214 endfun
1215
1216 aug repro
1217 au!
1218 au BufWinLeave * call Nop()
1219 aug END
1220
1221 let [cmd, waittime] = s:get_sleep_cmd()
1222
1223 call assert_equal(1, winnr('$'))
1224 new
1225 call setline(1, ['one', 'two'])
1226 exe 'term ++close ' . cmd
1227 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001228 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +01001229 call assert_equal(1, s:called)
1230 bwipe!
1231
1232 unlet s:called
1233 au! repro
1234 delfunc Nop
1235endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001236
1237func Test_terminal_term_start_empty_command()
1238 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001239 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001240 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001241 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001242 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001243 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001244 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001245 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001246 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001247 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001248 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001249 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001250 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001251 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001252 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001253 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001254 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001255 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001256 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1257 call assert_fails(cmd, 'E475:')
1258 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001259 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001260 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001261 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1262 call assert_fails(cmd, 'E475:')
1263 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1264 call assert_fails(cmd, 'E730:')
1265 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001266 if has('gui_running') || has('termguicolors')
1267 call assert_fails(cmd, 'E475:')
1268 else
1269 call assert_fails(cmd, 'E254:')
1270 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001271 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001272endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001273
1274func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001275 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001276
1277 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001278 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001279
Bram Moolenaar086eb872018-03-25 21:24:12 +02001280 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001281 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001282
Bram Moolenaar086eb872018-03-25 21:24:12 +02001283 " Request the cursor position.
1284 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001285
1286 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001287 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001288
Bram Moolenaar086eb872018-03-25 21:24:12 +02001289 " End "cat" gently.
1290 call term_sendkeys(buf, "\<CR>\<C-D>")
1291
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001292 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001293 exe buf . 'bwipe'
1294 unlet g:job
1295endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001296
Bram Moolenaarc2958582021-12-14 11:16:31 +00001297" Run this first, it fails when run after other tests.
1298func Test_aa_terminal_focus_events()
Drew Vogelea67ba72025-05-07 22:05:17 +02001299 CheckScreendump
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001300 CheckNotGui
1301 CheckUnix
1302 CheckRunVimInTerminal
1303
1304 let save_term = &term
1305 let save_ttymouse = &ttymouse
1306 set term=xterm ttymouse=xterm2
1307
1308 let lines =<< trim END
1309 set term=xterm ttymouse=xterm2
Bram Moolenaare5050712021-12-09 10:51:05 +00001310 au FocusLost * call setline(1, 'I am lost') | set nomod
1311 au FocusGained * call setline(1, 'I am back') | set nomod
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001312 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001313 call writefile(lines, 'XtermFocus', 'D')
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001314 let buf = RunVimInTerminal('-S XtermFocus', #{rows: 6})
1315
1316 " Send a focus event to ourselves, it should be forwarded to the terminal
1317 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001318 call VerifyScreenDump(buf, 'Test_terminal_focus_1', {})
1319
1320 call feedkeys("\<Esc>[I", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001321 call VerifyScreenDump(buf, 'Test_terminal_focus_2', {})
1322
Bram Moolenaare5050712021-12-09 10:51:05 +00001323 " check that a command line being edited is redrawn in place
1324 call term_sendkeys(buf, ":" .. repeat('x', 80))
1325 call TermWait(buf)
1326 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaare5050712021-12-09 10:51:05 +00001327 call VerifyScreenDump(buf, 'Test_terminal_focus_3', {})
1328 call term_sendkeys(buf, "\<Esc>")
1329
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001330 call StopVimInTerminal(buf)
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001331 let &term = save_term
1332 let &ttymouse = save_ttymouse
1333endfunc
1334
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001335" Run Vim, start a terminal in that Vim with the kill argument,
1336" :qall works.
1337func Run_terminal_qall_kill(line1, line2)
1338 " 1. Open a terminal window and wait for the prompt to appear
1339 " 2. set kill using term_setkill()
1340 " 3. make Vim exit, it will kill the shell
1341 let after = [
1342 \ a:line1,
1343 \ 'let buf = bufnr("%")',
1344 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1345 \ ' sleep 10m',
1346 \ 'endwhile',
1347 \ a:line2,
1348 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1349 \ 'qall',
1350 \ ]
1351 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001352 return
1353 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001354 call assert_equal("done", readfile("Xdone")[0])
1355 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001356endfunc
1357
1358" Run Vim in a terminal, then start a terminal in that Vim with a kill
1359" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001360func Test_terminal_qall_kill_arg()
1361 call Run_terminal_qall_kill('term ++kill=kill', '')
1362endfunc
1363
1364" Run Vim, start a terminal in that Vim, set the kill argument with
1365" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001366func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001367 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001368endfunc
1369
1370" Run Vim, start a terminal in that Vim without the kill argument,
1371" check that :qall does not exit, :qall! does.
1372func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001373 let after =<< trim [CODE]
1374 term
1375 let buf = bufnr("%")
1376 while term_getline(buf, 1) =~ "^\\s*$"
1377 sleep 10m
1378 endwhile
1379 set nomore
1380 au VimLeavePre * call writefile(["too early"], "Xdone")
1381 qall
1382 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1383 cquit
1384 [CODE]
1385
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001386 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001387 return
1388 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001389 call assert_equal("done", readfile("Xdone")[0])
1390 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001391endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001392
1393" Run Vim in a terminal, then start a terminal in that Vim without a kill
1394" argument, check that :confirm qall works.
1395func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001396 CheckRunVimInTerminal
Bram Moolenaare564c702022-06-14 15:00:28 +01001397
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001398 let buf = RunVimInTerminal('', {})
1399
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001400 " the shell may set the window title, we don't want that here
Bram Moolenaar377d92a2022-06-14 21:22:12 +01001401 call term_sendkeys(buf, ":call test_override('vterm_title', 1)\<CR>")
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001402
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001403 " Open a terminal window and wait for the prompt to appear
1404 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001405 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1406 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001407
1408 " make Vim exit, it will prompt to kill the shell
1409 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001410 call WaitForAssert({-> assert_match('\[Y\]es, (N)o:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001411 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001412 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001413
1414 " close the terminal window where Vim was running
1415 quit
1416endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001417
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001418" Run Vim in a terminal, then start a terminal window with a shell and check
1419" that Vim exits if it is closed.
1420func Test_terminal_exit()
1421 CheckRunVimInTerminal
1422
1423 let lines =<< trim END
1424 let winid = win_getid()
1425 help
1426 term
1427 let termid = win_getid()
1428 call win_gotoid(winid)
1429 close
1430 call win_gotoid(termid)
1431 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001432 call writefile(lines, 'XtermExit', 'D')
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001433 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1434 let job = term_getjob(buf)
1435 call WaitForAssert({-> assert_equal("run", job_status(job))})
1436
1437 " quit the shell, it will make Vim exit
1438 call term_sendkeys(buf, "exit\<CR>")
1439 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001440endfunc
1441
Bram Moolenaar012eb662018-03-13 17:55:27 +01001442func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001443 augroup repro
1444 au!
1445 au TerminalOpen * let s:called += 1
1446 augroup END
1447
1448 let s:called = 0
1449
1450 " Open a terminal window with :terminal
1451 terminal
1452 call assert_equal(1, s:called)
1453 bwipe!
1454
1455 " Open a terminal window with term_start()
1456 call term_start(&shell)
1457 call assert_equal(2, s:called)
1458 bwipe!
1459
1460 " Open a hidden terminal buffer with :terminal
1461 terminal ++hidden
1462 call assert_equal(3, s:called)
1463 for buf in term_list()
1464 exe buf . "bwipe!"
1465 endfor
1466
1467 " Open a hidden terminal buffer with term_start()
1468 let buf = term_start(&shell, {'hidden': 1})
1469 call assert_equal(4, s:called)
1470 exe buf . "bwipe!"
1471
1472 unlet s:called
1473 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001474endfunc
1475
1476func Test_open_term_from_cmd()
Drew Vogelea67ba72025-05-07 22:05:17 +02001477 CheckScreendump
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001478 CheckUnix
1479 CheckRunVimInTerminal
1480
1481 let lines =<< trim END
1482 call setline(1, ['a', 'b', 'c'])
1483 3
1484 set incsearch
1485 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1486 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001487 call writefile(lines, 'Xopenterm', 'D')
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001488 let buf = RunVimInTerminal('-S Xopenterm', {})
1489
1490 " this opens a window, incsearch should not use the old cursor position
1491 call term_sendkeys(buf, "/\<F3>")
1492 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1493 call term_sendkeys(buf, "\<Esc>")
1494 call term_sendkeys(buf, ":q\<CR>")
1495
1496 call StopVimInTerminal(buf)
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001497endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001498
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001499func Test_combining_double_width()
Drew Vogelea67ba72025-05-07 22:05:17 +02001500 CheckScreendump
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001501 CheckUnix
1502 CheckRunVimInTerminal
1503
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001504 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble', 'D')
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001505 let lines =<< trim END
1506 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1507 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001508 call writefile(lines, 'Xcombining', 'D')
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001509 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1510
1511 " this opens a window, incsearch should not use the old cursor position
1512 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1513 call term_sendkeys(buf, ":q\<CR>")
1514
1515 call StopVimInTerminal(buf)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001516endfunc
1517
Bram Moolenaar02764712020-11-14 20:21:55 +01001518func Test_terminal_popup_with_cmd()
1519 " this was crashing
1520 let buf = term_start(&shell, #{hidden: v:true})
1521 let s:winid = popup_create(buf, {})
1522 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1523 call feedkeys("\<F3>", 'xt')
1524
1525 tunmap <F3>
1526 exe 'bwipe! ' .. buf
1527 unlet s:winid
1528endfunc
1529
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001530func Test_terminal_popup_bufload()
1531 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1532 let winid = popup_create(termbuf, {})
1533 sleep 50m
1534
1535 let newbuf = bufadd('')
1536 call bufload(newbuf)
1537 call setbufline(newbuf, 1, 'foobar')
1538
1539 " must not have switched to another window
1540 call assert_equal(winid, win_getid())
1541
Bram Moolenaare6329e42020-11-16 21:10:34 +01001542 call StopShellInTerminal(termbuf)
1543 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001544 exe 'bwipe! ' .. newbuf
1545endfunc
1546
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001547func Test_terminal_popup_two_windows()
Bram Moolenaar0407d272021-12-13 22:17:44 +00001548 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001549 CheckUnix
1550
1551 " use "sh" instead of "&shell" in the hope it will use a short prompt
1552 let lines =<< trim END
1553 let termbuf = term_start('sh', #{hidden: v:true, term_finish: 'close'})
1554 exe 'buffer ' .. termbuf
1555
1556 let winid = popup_create(termbuf, #{line: 2, minwidth: 30, border: []})
1557 sleep 50m
1558
1559 call term_sendkeys(termbuf, "echo 'test'")
1560 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001561 call writefile(lines, 'XpopupScript', 'D')
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001562 let buf = RunVimInTerminal('-S XpopupScript', {})
1563
1564 " typed text appears both in normal window and in popup
1565 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 1))})
1566 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 3))})
1567
Bram Moolenaar0407d272021-12-13 22:17:44 +00001568 call term_sendkeys(buf, "\<CR>\<CR>exit\<CR>")
1569 call TermWait(buf)
1570 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001571 call StopVimInTerminal(buf)
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001572endfunc
1573
Bram Moolenaare41decc2020-11-14 21:34:59 +01001574func Test_terminal_popup_insert_cmd()
1575 CheckUnix
1576
1577 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1578 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001579 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001580 endfunc
1581 call feedkeys("i\<F3>")
1582 sleep 10m
1583 call assert_equal('n', mode())
1584
1585 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001586 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001587 delfunc StartTermInPopup
1588 iunmap <F3>
1589endfunc
1590
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001591func Check_dump01(off)
1592 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1593 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001594 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001595endfunc
1596
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001597func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001598 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001599
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001600 let save_enc = &encoding
1601 set encoding=utf-8
1602 call assert_equal(1, winnr('$'))
1603
1604 let text = " a\u0300 e\u0302 o\u0308"
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001605 call writefile([text], 'Xcomposing', 'D')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001606 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001607 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001608 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001609 let dumpline = readfile('Xdump')[0]
1610 call assert_match('|à| |ê| |ö', dumpline)
1611
1612 call StopVimInTerminal(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001613 call delete('Xdump')
1614 let &encoding = save_enc
1615endfunc
1616
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001617" Tests for failures in the term_dumpwrite() function
1618func Test_terminal_dumpwrite_errors()
1619 CheckRunVimInTerminal
1620 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1621 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001622 call TermWait(buf)
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001623 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E1206:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001624 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1625 call writefile([], 'Xtest.dump')
1626 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1627 call delete('Xtest.dump')
1628 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1629 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001630 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001631 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001632 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001633 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1634 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1635 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1636endfunc
1637
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001638" just testing basic functionality.
1639func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001640 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001641 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001642 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001643 call assert_equal(2, winnr('$'))
1644 call assert_equal(20, line('$'))
1645 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001646
1647 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001648 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001649 call assert_equal(buf, buf2)
1650 call assert_notequal('one two three four five', trim(getline(1)))
1651
1652 " Load the first dump again in the same window
1653 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1654 call assert_equal(buf, buf2)
1655 call Check_dump01(0)
1656
1657 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1658 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1659 new
1660 let closedbuf = winbufnr('')
1661 quit
1662 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001663 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001664 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001665
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001666 quit
1667endfunc
1668
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001669func Test_terminal_dumpload_dump()
Drew Vogelea67ba72025-05-07 22:05:17 +02001670 CheckScreendump
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001671 CheckRunVimInTerminal
1672
1673 let lines =<< trim END
1674 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1675 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001676 call writefile(lines, 'XtermDumpload', 'D')
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001677 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1678 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1679
1680 call StopVimInTerminal(buf)
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001681endfunc
1682
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001683func Test_terminal_dumpdiff()
1684 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001685 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001686 call assert_equal(2, winnr('$'))
1687 call assert_equal(62, line('$'))
1688 call Check_dump01(0)
1689 call Check_dump01(42)
1690 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1691 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001692
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001693 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001694 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001695 call writefile([], 'X1.dump', 'D')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001696 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001697endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001698
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001699func Test_terminal_dumpdiff_swap()
1700 call assert_equal(1, winnr('$'))
1701 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1702 call assert_equal(2, winnr('$'))
1703 call assert_equal(62, line('$'))
1704 call assert_match('Test_popup_command_01.dump', getline(21))
1705 call assert_match('Test_popup_command_03.dump', getline(42))
1706 call assert_match('Undo', getline(3))
1707 call assert_match('three four five', getline(45))
1708
1709 normal s
1710 call assert_match('Test_popup_command_03.dump', getline(21))
1711 call assert_match('Test_popup_command_01.dump', getline(42))
1712 call assert_match('three four five', getline(3))
1713 call assert_match('Undo', getline(45))
1714 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001715
1716 " Diff two terminal dump files with different number of rows
1717 " Swap the diffs
1718 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1719 call assert_match('Test_popup_command_01.dump', getline(21))
1720 call assert_match('Test_winline_rnu.dump', getline(42))
1721 normal s
1722 call assert_match('Test_winline_rnu.dump', getline(6))
1723 call assert_match('Test_popup_command_01.dump', getline(27))
1724 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001725endfunc
1726
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001727func Test_terminal_dumpdiff_options()
1728 set laststatus=0
1729 call assert_equal(1, winnr('$'))
1730 let height = winheight(0)
1731 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1732 call assert_equal(2, winnr('$'))
1733 call assert_equal(height, winheight(winnr()))
1734 call assert_equal(33, winwidth(winnr()))
1735 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1736 quit
1737
1738 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001739 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1740 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001741 call assert_equal(&columns, winwidth(0))
1742 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001743 call assert_equal('something else', bufname('%'))
1744 quit
1745
1746 call assert_equal(1, winnr('$'))
1747 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1748 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001749 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 +01001750 bwipe
1751
1752 set laststatus&
1753endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001754
Bram Moolenaar10772302019-01-20 18:25:54 +01001755" When drawing the statusline the cursor position may not have been updated
1756" yet.
1757" 1. create a terminal, make it show 2 lines
1758" 2. 0.5 sec later: leave terminal window, execute "i"
1759" 3. 0.5 sec later: clear terminal window, now it's 1 line
1760" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1761" 4. 0.5 sec later: should be done, clean up
1762func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001763 CheckUnix
Bram Moolenaar81035272021-12-16 18:02:07 +00001764 CheckFeature timers
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001765
Bram Moolenaar10772302019-01-20 18:25:54 +01001766 set statusline=x
1767 terminal
1768 let tbuf = bufnr('')
1769 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1770 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1771 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1772 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1773
1774 sleep 2
1775 exe tbuf . 'bwipe!'
1776 au! BufLeave
1777 set statusline=
1778endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001779
Bram Moolenaar81035272021-12-16 18:02:07 +00001780func CheckTerminalWindowWorks(buf)
1781 call WaitForAssert({-> assert_match('!sh \[running\]', term_getline(a:buf, 10))})
1782 call term_sendkeys(a:buf, "exit\<CR>")
1783 call WaitForAssert({-> assert_match('!sh \[finished\]', term_getline(a:buf, 10))})
1784 call term_sendkeys(a:buf, ":q\<CR>")
1785 call WaitForAssert({-> assert_match('^\~', term_getline(a:buf, 10))})
1786endfunc
1787
1788func Test_start_terminal_from_timer()
1789 CheckUnix
1790 CheckFeature timers
1791
1792 " Open a terminal window from a timer, typed text goes to the terminal
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001793 call writefile(["call timer_start(100, { -> term_start('sh') })"], 'XtimerTerm', 'D')
Bram Moolenaar81035272021-12-16 18:02:07 +00001794 let buf = RunVimInTerminal('-S XtimerTerm', {})
1795 call CheckTerminalWindowWorks(buf)
1796
1797 " do the same in Insert mode
1798 call term_sendkeys(buf, ":call timer_start(200, { -> term_start('sh') })\<CR>a")
1799 call CheckTerminalWindowWorks(buf)
1800
1801 call StopVimInTerminal(buf)
Bram Moolenaar81035272021-12-16 18:02:07 +00001802endfunc
1803
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001804func Test_terminal_window_focus()
1805 let winid1 = win_getid()
1806 terminal
1807 let winid2 = win_getid()
1808 call feedkeys("\<C-W>j", 'xt')
1809 call assert_equal(winid1, win_getid())
1810 call feedkeys("\<C-W>k", 'xt')
1811 call assert_equal(winid2, win_getid())
1812 " can use a cursor key here
1813 call feedkeys("\<C-W>\<Down>", 'xt')
1814 call assert_equal(winid1, win_getid())
1815 call feedkeys("\<C-W>\<Up>", 'xt')
1816 call assert_equal(winid2, win_getid())
1817
1818 bwipe!
1819endfunc
1820
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001821func Api_drop_common(options)
1822 call assert_equal(1, winnr('$'))
1823
1824 " Use the title termcap entries to output the escape sequence.
1825 call writefile([
1826 \ 'set title',
1827 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1828 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1829 \ 'redraw',
1830 \ "set t_ts=",
1831 \ ], 'Xscript')
1832 let buf = RunVimInTerminal('-S Xscript', {})
1833 call WaitFor({-> bufnr('Xtextfile') > 0})
1834 call assert_equal('Xtextfile', expand('%:t'))
1835 call assert_true(winnr('$') >= 3)
1836 return buf
1837endfunc
1838
1839func Test_terminal_api_drop_newwin()
1840 CheckRunVimInTerminal
1841 let buf = Api_drop_common('')
1842 call assert_equal(0, &bin)
1843 call assert_equal('', &fenc)
1844
1845 call StopVimInTerminal(buf)
1846 call delete('Xscript')
1847 bwipe Xtextfile
1848endfunc
1849
1850func Test_terminal_api_drop_newwin_bin()
1851 CheckRunVimInTerminal
1852 let buf = Api_drop_common(',{"bin":1}')
1853 call assert_equal(1, &bin)
1854
1855 call StopVimInTerminal(buf)
1856 call delete('Xscript')
1857 bwipe Xtextfile
1858endfunc
1859
1860func Test_terminal_api_drop_newwin_binary()
1861 CheckRunVimInTerminal
1862 let buf = Api_drop_common(',{"binary":1}')
1863 call assert_equal(1, &bin)
1864
1865 call StopVimInTerminal(buf)
1866 call delete('Xscript')
1867 bwipe Xtextfile
1868endfunc
1869
1870func Test_terminal_api_drop_newwin_nobin()
1871 CheckRunVimInTerminal
1872 set binary
1873 let buf = Api_drop_common(',{"nobin":1}')
1874 call assert_equal(0, &bin)
1875
1876 call StopVimInTerminal(buf)
1877 call delete('Xscript')
1878 bwipe Xtextfile
1879 set nobinary
1880endfunc
1881
1882func Test_terminal_api_drop_newwin_nobinary()
1883 CheckRunVimInTerminal
1884 set binary
1885 let buf = Api_drop_common(',{"nobinary":1}')
1886 call assert_equal(0, &bin)
1887
1888 call StopVimInTerminal(buf)
1889 call delete('Xscript')
1890 bwipe Xtextfile
1891 set nobinary
1892endfunc
1893
1894func Test_terminal_api_drop_newwin_ff()
1895 CheckRunVimInTerminal
1896 let buf = Api_drop_common(',{"ff":"dos"}')
1897 call assert_equal("dos", &ff)
1898
1899 call StopVimInTerminal(buf)
1900 call delete('Xscript')
1901 bwipe Xtextfile
1902endfunc
1903
1904func Test_terminal_api_drop_newwin_fileformat()
1905 CheckRunVimInTerminal
1906 let buf = Api_drop_common(',{"fileformat":"dos"}')
1907 call assert_equal("dos", &ff)
1908
1909 call StopVimInTerminal(buf)
1910 call delete('Xscript')
1911 bwipe Xtextfile
1912endfunc
1913
1914func Test_terminal_api_drop_newwin_enc()
1915 CheckRunVimInTerminal
1916 let buf = Api_drop_common(',{"enc":"utf-16"}')
1917 call assert_equal("utf-16", &fenc)
1918
1919 call StopVimInTerminal(buf)
1920 call delete('Xscript')
1921 bwipe Xtextfile
1922endfunc
1923
1924func Test_terminal_api_drop_newwin_encoding()
1925 CheckRunVimInTerminal
1926 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1927 call assert_equal("utf-16", &fenc)
1928
1929 call StopVimInTerminal(buf)
1930 call delete('Xscript')
1931 bwipe Xtextfile
1932endfunc
1933
1934func Test_terminal_api_drop_oldwin()
1935 CheckRunVimInTerminal
1936 let firstwinid = win_getid()
1937 split Xtextfile
1938 let textfile_winid = win_getid()
1939 call assert_equal(2, winnr('$'))
1940 call win_gotoid(firstwinid)
1941
1942 " Use the title termcap entries to output the escape sequence.
1943 call writefile([
1944 \ 'set title',
1945 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1946 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1947 \ 'redraw',
1948 \ "set t_ts=",
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001949 \ ], 'Xscript', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001950 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1951 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1952 call assert_equal(textfile_winid, win_getid())
1953
1954 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001955 bwipe Xtextfile
1956endfunc
1957
1958func Tapi_TryThis(bufnum, arg)
1959 let g:called_bufnum = a:bufnum
1960 let g:called_arg = a:arg
1961endfunc
1962
1963func WriteApiCall(funcname)
1964 " Use the title termcap entries to output the escape sequence.
1965 call writefile([
1966 \ 'set title',
1967 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1968 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1969 \ 'redraw',
1970 \ "set t_ts=",
1971 \ ], 'Xscript')
1972endfunc
1973
1974func Test_terminal_api_call()
1975 CheckRunVimInTerminal
1976
1977 unlet! g:called_bufnum
1978 unlet! g:called_arg
1979
1980 call WriteApiCall('Tapi_TryThis')
1981
1982 " Default
1983 let buf = RunVimInTerminal('-S Xscript', {})
1984 call WaitFor({-> exists('g:called_bufnum')})
1985 call assert_equal(buf, g:called_bufnum)
1986 call assert_equal(['hello', 123], g:called_arg)
1987 call StopVimInTerminal(buf)
1988
1989 unlet! g:called_bufnum
1990 unlet! g:called_arg
1991
1992 " Enable explicitly
1993 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1994 call WaitFor({-> exists('g:called_bufnum')})
1995 call assert_equal(buf, g:called_bufnum)
1996 call assert_equal(['hello', 123], g:called_arg)
1997 call StopVimInTerminal(buf)
1998
1999 unlet! g:called_bufnum
2000 unlet! g:called_arg
2001
2002 func! ApiCall_TryThis(bufnum, arg)
2003 let g:called_bufnum2 = a:bufnum
2004 let g:called_arg2 = a:arg
2005 endfunc
2006
2007 call WriteApiCall('ApiCall_TryThis')
2008
2009 " Use prefix match
2010 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
2011 call WaitFor({-> exists('g:called_bufnum2')})
2012 call assert_equal(buf, g:called_bufnum2)
2013 call assert_equal(['hello', 123], g:called_arg2)
2014 call StopVimInTerminal(buf)
2015
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002016 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002017
2018 unlet! g:called_bufnum2
2019 unlet! g:called_arg2
2020
2021 call delete('Xscript')
2022 delfunction! ApiCall_TryThis
2023 unlet! g:called_bufnum2
2024 unlet! g:called_arg2
2025endfunc
2026
2027func Test_terminal_api_call_fails()
2028 CheckRunVimInTerminal
2029
2030 func! TryThis(bufnum, arg)
2031 let g:called_bufnum3 = a:bufnum
2032 let g:called_arg3 = a:arg
2033 endfunc
2034
2035 call WriteApiCall('TryThis')
2036
2037 unlet! g:called_bufnum3
2038 unlet! g:called_arg3
2039
2040 " Not permitted
2041 call ch_logfile('Xlog', 'w')
2042 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
2043 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
2044 call assert_false(exists('g:called_bufnum3'))
2045 call assert_false(exists('g:called_arg3'))
2046 call StopVimInTerminal(buf)
2047
2048 " No match
2049 call ch_logfile('Xlog', 'w')
2050 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
2051 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
2052 call assert_false(exists('g:called_bufnum3'))
2053 call assert_false(exists('g:called_arg3'))
2054 call StopVimInTerminal(buf)
2055
2056 call delete('Xscript')
2057 call ch_logfile('')
2058 call delete('Xlog')
2059 delfunction! TryThis
2060 unlet! g:called_bufnum3
2061 unlet! g:called_arg3
2062endfunc
2063
2064let s:caught_e937 = 0
2065
2066func Tapi_Delete(bufnum, arg)
2067 try
2068 execute 'bdelete!' a:bufnum
2069 catch /E937:/
2070 let s:caught_e937 = 1
2071 endtry
2072endfunc
2073
2074func Test_terminal_api_call_fail_delete()
2075 CheckRunVimInTerminal
2076
2077 call WriteApiCall('Tapi_Delete')
2078 let buf = RunVimInTerminal('-S Xscript', {})
2079 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
2080
2081 call StopVimInTerminal(buf)
2082 call delete('Xscript')
2083 call ch_logfile('', '')
2084endfunc
2085
2086func Test_terminal_setapi_and_call()
2087 CheckRunVimInTerminal
2088
2089 call WriteApiCall('Tapi_TryThis')
2090 call ch_logfile('Xlog', 'w')
2091
2092 unlet! g:called_bufnum
2093 unlet! g:called_arg
2094
2095 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
2096 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2097 call assert_false(exists('g:called_bufnum'))
2098 call assert_false(exists('g:called_arg'))
2099
2100 eval buf->term_setapi('Tapi_')
2101 call term_sendkeys(buf, ":set notitle\<CR>")
2102 call term_sendkeys(buf, ":source Xscript\<CR>")
2103 call WaitFor({-> exists('g:called_bufnum')})
2104 call assert_equal(buf, g:called_bufnum)
2105 call assert_equal(['hello', 123], g:called_arg)
2106
2107 call StopVimInTerminal(buf)
2108
2109 call delete('Xscript')
2110 call ch_logfile('')
2111 call delete('Xlog')
2112 unlet! g:called_bufnum
2113 unlet! g:called_arg
2114endfunc
2115
2116func Test_terminal_api_arg()
2117 CheckRunVimInTerminal
2118
2119 call WriteApiCall('Tapi_TryThis')
2120 call ch_logfile('Xlog', 'w')
2121
2122 unlet! g:called_bufnum
2123 unlet! g:called_arg
2124
2125 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2126 let buf = bufnr('%')
2127 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2128 call assert_false(exists('g:called_bufnum'))
2129 call assert_false(exists('g:called_arg'))
2130
2131 call StopVimInTerminal(buf)
2132
2133 call ch_logfile('Xlog', 'w')
2134
2135 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2136 let buf = bufnr('%')
2137 call WaitFor({-> exists('g:called_bufnum')})
2138 call assert_equal(buf, g:called_bufnum)
2139 call assert_equal(['hello', 123], g:called_arg)
2140
2141 call StopVimInTerminal(buf)
2142
2143 call delete('Xscript')
2144 call ch_logfile('')
2145 call delete('Xlog')
2146 unlet! g:called_bufnum
2147 unlet! g:called_arg
2148endfunc
2149
2150func Test_terminal_ansicolors_default()
2151 CheckFunction term_getansicolors
2152
2153 let colors = [
2154 \ '#000000', '#e00000',
2155 \ '#00e000', '#e0e000',
2156 \ '#0000e0', '#e000e0',
2157 \ '#00e0e0', '#e0e0e0',
2158 \ '#808080', '#ff4040',
2159 \ '#40ff40', '#ffff40',
2160 \ '#4040ff', '#ff40ff',
2161 \ '#40ffff', '#ffffff',
2162 \]
2163
2164 let buf = Run_shell_in_terminal({})
2165 call assert_equal(colors, term_getansicolors(buf))
2166 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002167 call assert_equal([], term_getansicolors(buf))
2168
2169 exe buf . 'bwipe'
2170endfunc
2171
Julio Bcde8ff62025-02-06 20:31:27 +01002172func Test_terminal_ansicolors_default_reset_tgc()
Drew Vogelea67ba72025-05-07 22:05:17 +02002173 CheckScreendump
Julio Bcde8ff62025-02-06 20:31:27 +01002174 CheckFeature termguicolors
2175 CheckRunVimInTerminal
2176
2177 let $PS1="$ "
2178 let buf = RunVimInTerminal('-c "term sh"', {'rows': 12})
2179 call TermWait(buf)
2180 " Wait for the shell to display a prompt
2181 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2182
2183 call term_sendkeys(buf, "printf '\\033[0;30;41mhello world\\033[0m\\n'\<CR>")
2184 call WaitForAssert({-> assert_match('hello world', term_getline(buf, 2))})
2185 call term_sendkeys(buf, "\<C-W>:set notgc\<CR>")
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, 4))})
2188
2189 call VerifyScreenDump(buf, 'Test_terminal_ansi_reset_tgc', {})
2190
2191 call term_sendkeys(buf, "exit\<CR>")
2192 call TermWait(buf)
2193 call StopVimInTerminal(buf)
2194 unlet! $PS1
2195endfunc
2196
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002197let s:test_colors = [
2198 \ '#616e64', '#0d0a79',
2199 \ '#6d610d', '#0a7373',
2200 \ '#690d0a', '#6d696e',
2201 \ '#0d0a6f', '#616e0d',
2202 \ '#0a6479', '#6d0d0a',
2203 \ '#617373', '#0d0a69',
2204 \ '#6d690d', '#0a6e6f',
2205 \ '#610d0a', '#6e6479',
2206 \]
2207
2208func Test_terminal_ansicolors_global()
2209 CheckFeature termguicolors
2210 CheckFunction term_getansicolors
2211
LemonBoyb2b3acb2022-05-20 10:10:34 +01002212 if has('vtp') && !has('vcon') && !has('gui_running')
2213 throw 'Skipped: does not support termguicolors'
2214 endif
2215
2216 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002217 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2218 let buf = Run_shell_in_terminal({})
2219 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
2220 call StopShellInTerminal(buf)
LemonBoyb2b3acb2022-05-20 10:10:34 +01002221 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002222
2223 exe buf . 'bwipe'
2224 unlet g:terminal_ansi_colors
2225endfunc
2226
2227func Test_terminal_ansicolors_func()
2228 CheckFeature termguicolors
2229 CheckFunction term_getansicolors
2230
LemonBoyb2b3acb2022-05-20 10:10:34 +01002231 if has('vtp') && !has('vcon') && !has('gui_running')
2232 throw 'Skipped: does not support termguicolors'
2233 endif
2234
2235 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002236 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2237 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
2238 call assert_equal(s:test_colors, term_getansicolors(buf))
2239
2240 call term_setansicolors(buf, g:terminal_ansi_colors)
2241 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
2242
2243 let colors = [
2244 \ 'ivory', 'AliceBlue',
2245 \ 'grey67', 'dark goldenrod',
2246 \ 'SteelBlue3', 'PaleVioletRed4',
2247 \ 'MediumPurple2', 'yellow2',
2248 \ 'RosyBrown3', 'OrangeRed2',
2249 \ 'white smoke', 'navy blue',
2250 \ 'grey47', 'gray97',
2251 \ 'MistyRose2', 'DodgerBlue4',
2252 \]
2253 eval buf->term_setansicolors(colors)
2254
2255 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002256 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaard83392a2022-09-01 12:22:46 +01002257 call assert_fails('call term_setansicolors(buf, {})', 'E1211:')
Bram Moolenaar23919542023-05-05 22:12:22 +01002258 call assert_fails('call term_setansicolors(buf, [])', 'E475: Invalid value for argument "colors"')
LemonBoyb2b3acb2022-05-20 10:10:34 +01002259 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002260
2261 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002262 call assert_equal(0, term_setansicolors(buf, []))
2263 exe buf . 'bwipe'
2264endfunc
2265
2266func Test_terminal_all_ansi_colors()
Drew Vogelea67ba72025-05-07 22:05:17 +02002267 CheckScreendump
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002268 CheckRunVimInTerminal
2269
2270 " Use all the ANSI colors.
2271 call writefile([
2272 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
2273 \ 'hi Tblack ctermfg=0 ctermbg=8',
2274 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
2275 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
2276 \ 'hi Tbrown ctermfg=3 ctermbg=11',
2277 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
2278 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
2279 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
2280 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
2281 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
2282 \ 'hi Tred ctermfg=9 ctermbg=1',
2283 \ 'hi Tgreen ctermfg=10 ctermbg=2',
2284 \ 'hi Tyellow ctermfg=11 ctermbg=3',
2285 \ 'hi Tblue ctermfg=12 ctermbg=4',
2286 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
2287 \ 'hi Tcyan ctermfg=14 ctermbg=6',
2288 \ 'hi Twhite ctermfg=15 ctermbg=7',
2289 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
2290 \ 'hi TgreenBold ctermfg=10 cterm=bold',
2291 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
2292 \ '',
2293 \ 'call matchadd("Tblack", "A")',
2294 \ 'call matchadd("Tdarkred", "B")',
2295 \ 'call matchadd("Tdarkgreen", "C")',
2296 \ 'call matchadd("Tbrown", "D")',
2297 \ 'call matchadd("Tdarkblue", "E")',
2298 \ 'call matchadd("Tdarkmagenta", "F")',
2299 \ 'call matchadd("Tdarkcyan", "G")',
2300 \ 'call matchadd("Tlightgrey", "H")',
2301 \ 'call matchadd("Tdarkgrey", "I")',
2302 \ 'call matchadd("Tred", "J")',
2303 \ 'call matchadd("Tgreen", "K")',
2304 \ 'call matchadd("Tyellow", "L")',
2305 \ 'call matchadd("Tblue", "M")',
2306 \ 'call matchadd("Tmagenta", "N")',
2307 \ 'call matchadd("Tcyan", "O")',
2308 \ 'call matchadd("Twhite", "P")',
2309 \ 'call matchadd("TdarkredBold", "X")',
2310 \ 'call matchadd("TgreenBold", "Y")',
2311 \ 'call matchadd("TmagentaBold", "Z")',
2312 \ 'redraw',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01002313 \ ], 'Xcolorscript', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002314 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2315 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2316
2317 call term_sendkeys(buf, ":q\<CR>")
2318 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002319endfunc
2320
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002321function On_BufFilePost()
2322 doautocmd <nomodeline> User UserEvent
2323endfunction
2324
2325func Test_terminal_nested_autocmd()
2326 new
2327 call setline(1, range(500))
2328 $
2329 let lastline = line('.')
2330
2331 augroup TermTest
2332 autocmd BufFilePost * call On_BufFilePost()
2333 autocmd User UserEvent silent
2334 augroup END
2335
2336 let cmd = Get_cat_123_cmd()
2337 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2338 call assert_equal(lastline, line('.'))
2339
Bram Moolenaar64374752021-04-03 17:22:29 +02002340 let job = term_getjob(buf)
2341 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002342 call delete('Xtext')
2343 augroup TermTest
2344 au!
2345 augroup END
2346endfunc
2347
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002348func Test_terminal_adds_jump()
2349 clearjumps
2350 call term_start("ls", #{curwin: 1})
2351 call assert_equal(1, getjumplist()[0]->len())
2352 bwipe!
2353endfunc
2354
Bram Moolenaareea32af2021-11-21 14:51:13 +00002355func Close_cb(ch, ctx)
2356 call term_wait(a:ctx.bufnr)
2357 let g:close_done = 'done'
2358endfunc
2359
2360func Test_term_wait_in_close_cb()
2361 let g:close_done = ''
2362 let ctx = {}
2363 let ctx.bufnr = term_start('echo "HELLO WORLD"',
2364 \ {'close_cb': {ch -> Close_cb(ch, ctx)}})
2365
2366 call WaitForAssert({-> assert_equal("done", g:close_done)})
2367
2368 unlet g:close_done
2369 bwipe!
2370endfunc
2371
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01002372func Test_term_TextChangedT()
2373 augroup TermTest
2374 autocmd TextChangedT * ++once
2375 \ execute expand('<abuf>') . 'buffer' |
2376 \ let b:called = 1 |
2377 \ split |
2378 \ enew
2379 augroup END
2380
2381 terminal
2382
2383 let term_buf = bufnr()
2384
2385 let b:called = 0
2386
2387 call term_sendkeys(term_buf, "aaabbc\r")
2388 call TermWait(term_buf)
2389
2390 call assert_equal(1, getbufvar(term_buf, 'called'))
2391
2392 " Current buffer will be restored
2393 call assert_equal(bufnr(), term_buf)
2394
2395 bwipe!
2396 augroup TermTest
2397 au!
2398 augroup END
2399endfunc
2400
2401func Test_term_TextChangedT_close()
2402 augroup TermTest
2403 autocmd TextChangedT * ++once split | enew | 1close!
2404 augroup END
2405
2406 terminal
2407
2408 let term_buf = bufnr()
2409
2410 call term_sendkeys(term_buf, "aaabbc\r")
2411 call TermWait(term_buf)
2412
2413 " Current buffer will be restored
2414 call assert_equal(bufnr(), term_buf)
2415
2416 bwipe!
2417 augroup TermTest
2418 au!
2419 augroup END
2420endfunc
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002421
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002422" vim: shiftwidth=2 sts=2 expandtab