blob: 3cca1b05cb8117d24f1db996a7d7ce1b8db0fc2c [file] [log] [blame]
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001" Tests for the terminal window.
2" This is split in two, because it can take a lot of time.
3" See test_terminal.vim and test_terminal2.vim for further tests.
4
5source check.vim
6CheckFeature terminal
7
8source shared.vim
9source screendump.vim
10source mouse.vim
11source term_util.vim
12
Yegappan Lakshmanand603e952024-06-10 18:16:34 +020013import './vim9.vim' as v9
14
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020015let $PROMPT_COMMAND=''
16
17func Test_terminal_altscreen()
18 " somehow doesn't work on MS-Windows
19 CheckUnix
20 let cmd = "cat Xtext\<CR>"
21
22 let buf = term_start(&shell, {})
Yegappan Lakshmanane446a012023-01-19 17:49:58 +000023 call TermWait(buf)
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010024 call writefile(["\<Esc>[?1047h"], 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020025 call term_sendkeys(buf, cmd)
26 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
27
28 call writefile(["\<Esc>[?1047l"], 'Xtext')
29 call term_sendkeys(buf, cmd)
30 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
31
32 call term_sendkeys(buf, "exit\r")
33 exe buf . "bwipe!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020034endfunc
35
36func Test_terminal_shell_option()
37 if has('unix')
38 " exec is a shell builtin command, should fail without a shell.
39 term exec ls runtest.vim
40 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
41 bwipe!
42
43 term ++shell exec ls runtest.vim
44 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
45 bwipe!
46 elseif has('win32')
47 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar066b12e2020-07-28 21:40:27 +020048 " However, if dir.exe (which might be provided by Cygwin/MSYS2) exists in
49 " the %PATH%, "term dir" succeeds unintentionally. Use dir.com instead.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020050 try
Bram Moolenaar066b12e2020-07-28 21:40:27 +020051 term dir.com /b runtest.vim
52 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020053 catch /CreateProcess/
54 " ignore
55 endtry
56 bwipe!
57
Bram Moolenaar066b12e2020-07-28 21:40:27 +020058 " This should execute the dir builtin command even with ".com".
59 term ++shell dir.com /b runtest.vim
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020060 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
61 bwipe!
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020062 else
63 throw 'Skipped: does not work on this platform'
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020064 endif
65endfunc
66
67func Test_terminal_invalid_arg()
68 call assert_fails('terminal ++xyz', 'E181:')
69endfunc
70
Bram Moolenaar87fd0922021-11-20 13:47:45 +000071" Check a terminal with different colors
72func Terminal_color(group_name, highlight_cmds, highlight_opt, open_cmds)
73 CheckRunVimInTerminal
74 CheckUnix
75
76 let lines = [
77 \ 'call setline(1, range(20))',
78 \ 'func OpenTerm()',
79 \ ' set noruler',
80 \ " call term_start('cat', #{vertical: 1, " .. a:highlight_opt .. "})",
81 \ ] + a:open_cmds + [
82 \ 'endfunc',
83 \ ] + a:highlight_cmds
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010084 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +000085 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
86 call TermWait(buf, 100)
87 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
88 call TermWait(buf, 50)
89 call term_sendkeys(buf, "hello\<CR>")
90 call VerifyScreenDump(buf, 'Test_terminal_color_' .. a:group_name, {})
91
92 call term_sendkeys(buf, "\<C-D>")
93 call TermWait(buf, 50)
94 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +000095endfunc
96
97func Test_terminal_color_Terminal()
98 call Terminal_color("Terminal", [
99 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
100 \ ], "", [])
101endfunc
102
103func Test_terminal_color_group()
104 call Terminal_color("MyTermCol", [
105 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
106 \ ], "term_highlight: 'MyTermCol',", [])
107endfunc
108
109func Test_terminal_color_wincolor()
110 call Terminal_color("MyWinCol", [
111 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
112 \ ], "", [
113 \ 'set wincolor=MyWinCol',
114 \ ])
115endfunc
116
117func Test_terminal_color_group_over_Terminal()
118 call Terminal_color("MyTermCol_over_Terminal", [
119 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
120 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
121 \ ], "term_highlight: 'MyTermCol',", [])
122endfunc
123
124func Test_terminal_color_wincolor_over_group()
125 call Terminal_color("MyWinCol_over_group", [
126 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
127 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
128 \ ], "term_highlight: 'MyTermCol',", [
129 \ 'set wincolor=MyWinCol',
130 \ ])
131endfunc
132
133func Test_terminal_color_wincolor_split()
134 CheckRunVimInTerminal
135 CheckUnix
136
137 let lines = [
138 \ 'call setline(1, range(20))',
139 \ 'func OpenTerm()',
140 \ ' set noruler',
141 \ " call term_start('cat', #{vertical: 1, term_highlight: 'MyTermCol'})",
142 \ 'endfunc',
143 \ 'highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue',
144 \ 'highlight MyWinCol ctermfg=red ctermbg=darkyellow',
145 \ 'highlight MyWinCol2 ctermfg=black ctermbg=blue',
146 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100147 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000148 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
149 call TermWait(buf, 100)
150 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
151 call TermWait(buf, 50)
152 call term_sendkeys(buf, "hello\<CR>")
153 call TermWait(buf, 50)
154
155 call term_sendkeys(buf, "\<C-W>:split\<CR>")
156 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol\<CR>")
157 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol', {})
158
159 call term_sendkeys(buf, "\<C-W>b:2sb\<CR>")
160 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol2\<CR>")
161 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol2', {})
162
163 call term_sendkeys(buf, "\<C-D>")
164 call TermWait(buf, 50)
165 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000166endfunc
167
168func Test_terminal_color_transp_Terminal()
169 call Terminal_color("transp_Terminal", [
170 \ "highlight Terminal ctermfg=blue",
171 \ ], "", [])
172endfunc
173
174func Test_terminal_color_transp_group()
175 call Terminal_color("transp_MyTermCol", [
176 \ "highlight MyTermCol ctermfg=darkgreen",
177 \ ], "term_highlight: 'MyTermCol',", [])
178endfunc
179
180func Test_terminal_color_transp_wincolor()
181 call Terminal_color("transp_MyWinCol", [
182 \ "highlight MyWinCol ctermfg=red",
183 \ ], "", [
184 \ 'set wincolor=MyWinCol',
185 \ ])
186endfunc
187
188func Test_terminal_color_gui_Terminal()
189 CheckFeature termguicolors
190 call Terminal_color("gui_Terminal", [
191 \ "set termguicolors",
192 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
193 \ ], "", [])
194endfunc
195
196func Test_terminal_color_gui_group()
197 CheckFeature termguicolors
198 call Terminal_color("gui_MyTermCol", [
199 \ "set termguicolors",
200 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
201 \ ], "term_highlight: 'MyTermCol',", [])
202endfunc
203
204func Test_terminal_color_gui_wincolor()
205 CheckFeature termguicolors
206 call Terminal_color("gui_MyWinCol", [
207 \ "set termguicolors",
208 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
209 \ ], "", [
210 \ 'set wincolor=MyWinCol',
211 \ ])
212endfunc
213
214func Test_terminal_color_gui_transp_Terminal()
215 CheckFeature termguicolors
216 call Terminal_color("gui_transp_Terminal", [
217 \ "set termguicolors",
218 \ "highlight Terminal guifg=#3344ff",
219 \ ], "", [])
220endfunc
221
222func Test_terminal_color_gui_transp_group()
223 CheckFeature termguicolors
224 call Terminal_color("gui_transp_MyTermCol", [
225 \ "set termguicolors",
226 \ "highlight MyTermCol guifg=#007800",
227 \ ], "term_highlight: 'MyTermCol',", [])
228endfunc
229
230func Test_terminal_color_gui_transp_wincolor()
231 CheckFeature termguicolors
232 call Terminal_color("gui_transp_MyWinCol", [
233 \ "set termguicolors",
234 \ "highlight MyWinCol guifg=#fe1122",
235 \ ], "", [
236 \ 'set wincolor=MyWinCol',
237 \ ])
238endfunc
239
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200240func Test_terminal_in_popup()
241 CheckRunVimInTerminal
242
243 let text =<< trim END
244 some text
245 to edit
246 in a popup window
247 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100248 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200249 let cmd = GetVimCommandCleanTerm()
250 let lines = [
251 \ 'call setline(1, range(20))',
252 \ 'hi PopTerm ctermbg=grey',
253 \ 'func OpenTerm(setColor)',
254 \ " set noruler",
255 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
256 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
257 \ ' if a:setColor',
258 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
259 \ ' endif',
260 \ 'endfunc',
261 \ 'func HidePopup()',
262 \ ' call popup_hide(g:winid)',
263 \ 'endfunc',
264 \ 'func ClosePopup()',
265 \ ' call popup_close(g:winid)',
266 \ 'endfunc',
267 \ 'func ReopenPopup()',
268 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
269 \ 'endfunc',
270 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100271 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200272 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
James McCoy157241e2022-11-09 23:29:14 +0000273 call TermWait(buf, 200)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200274 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
James McCoy157241e2022-11-09 23:29:14 +0000275 call TermWait(buf, 800)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200276 call term_sendkeys(buf, ":\<CR>")
James McCoy157241e2022-11-09 23:29:14 +0000277 call TermWait(buf, 500)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200278 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
279 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
280
281 call term_sendkeys(buf, ":q\<CR>")
282 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
283
284 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
James McCoy157241e2022-11-09 23:29:14 +0000285 call TermWait(buf, 800)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200286 call term_sendkeys(buf, ":set hlsearch\<CR>")
James McCoy157241e2022-11-09 23:29:14 +0000287 call TermWait(buf, 500)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200288 call term_sendkeys(buf, "/edit\<CR>")
289 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
290
291 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
292 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
293 call term_sendkeys(buf, "\<CR>")
294 call TermWait(buf, 50)
295
296 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
297 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
298
299 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
300 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
301
302 " Go to terminal-Normal mode and visually select text.
303 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
304 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
305
306 " Back to job mode, redraws
307 call term_sendkeys(buf, "A")
308 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
309
310 call TermWait(buf, 50)
311 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100312 call TermWait(buf, 250) " wait for terminal to vanish
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200313
314 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200315endfunc
316
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100317" Check a terminal in popup window uses the default minimum size.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200318func Test_terminal_in_popup_min_size()
319 CheckRunVimInTerminal
320
321 let text =<< trim END
322 another text
323 to show
324 in a popup window
325 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100326 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200327 let lines = [
328 \ 'call setline(1, range(20))',
329 \ 'func OpenTerm()',
330 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
331 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
332 \ 'endfunc',
333 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100334 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200335 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
336 call TermWait(buf, 100)
337 call term_sendkeys(buf, ":set noruler\<CR>")
338 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
339 call TermWait(buf, 50)
340 call term_sendkeys(buf, ":\<CR>")
341 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
342
343 call TermWait(buf, 50)
344 call term_sendkeys(buf, ":q\<CR>")
345 call TermWait(buf, 50) " wait for terminal to vanish
346 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200347endfunc
348
349" Check a terminal in popup window with different colors
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000350func Terminal_in_popup_color(group_name, highlight_cmds, highlight_opt, popup_cmds, popup_opt)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200351 CheckRunVimInTerminal
352 CheckUnix
353
354 let lines = [
355 \ 'call setline(1, range(20))',
356 \ 'func OpenTerm()',
357 \ " let s:buf = term_start('cat', #{hidden: 1, "
358 \ .. a:highlight_opt .. "})",
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000359 \ ' let g:winid = popup_create(s:buf, #{border: [], '
360 \ .. a:popup_opt .. '})',
361 \ ] + a:popup_cmds + [
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200362 \ 'endfunc',
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000363 \ ] + a:highlight_cmds
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100364 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200365 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
366 call TermWait(buf, 100)
367 call term_sendkeys(buf, ":set noruler\<CR>")
368 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
369 call TermWait(buf, 50)
370 call term_sendkeys(buf, "hello\<CR>")
371 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
372
373 call term_sendkeys(buf, "\<C-D>")
374 call TermWait(buf, 50)
375 call term_sendkeys(buf, ":q\<CR>")
376 call TermWait(buf, 50) " wait for terminal to vanish
377 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200378endfunc
379
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000380func Test_terminal_in_popup_color_Terminal()
381 call Terminal_in_popup_color("Terminal", [
382 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
383 \ ], "", [], "")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200384endfunc
385
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000386func Test_terminal_in_popup_color_group()
387 call Terminal_in_popup_color("MyTermCol", [
388 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
389 \ ], "term_highlight: 'MyTermCol',", [], "")
390endfunc
391
392func Test_terminal_in_popup_color_wincolor()
393 call Terminal_in_popup_color("MyWinCol", [
394 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
395 \ ], "", [
396 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
397 \ ], "")
398endfunc
399
400func Test_terminal_in_popup_color_popup_highlight()
401 call Terminal_in_popup_color("MyPopupHlCol", [
402 \ "highlight MyPopupHlCol ctermfg=cyan ctermbg=green",
403 \ ], "", [], "highlight: 'MyPopupHlCol'")
404endfunc
405
406func Test_terminal_in_popup_color_group_over_Terminal()
407 call Terminal_in_popup_color("MyTermCol_over_Terminal", [
408 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
409 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
410 \ ], "term_highlight: 'MyTermCol',", [], "")
411endfunc
412
413func Test_terminal_in_popup_color_wincolor_over_group()
414 call Terminal_in_popup_color("MyWinCol_over_group", [
415 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
416 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
417 \ ], "term_highlight: 'MyTermCol',", [
418 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
419 \ ], "")
420endfunc
421
422func Test_terminal_in_popup_color_transp_Terminal()
423 call Terminal_in_popup_color("transp_Terminal", [
424 \ "highlight Terminal ctermfg=blue",
425 \ ], "", [], "")
426endfunc
427
428func Test_terminal_in_popup_color_transp_group()
429 call Terminal_in_popup_color("transp_MyTermCol", [
430 \ "highlight MyTermCol ctermfg=darkgreen",
431 \ ], "term_highlight: 'MyTermCol',", [], "")
432endfunc
433
434func Test_terminal_in_popup_color_transp_wincolor()
435 call Terminal_in_popup_color("transp_MyWinCol", [
436 \ "highlight MyWinCol ctermfg=red",
437 \ ], "", [
438 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
439 \ ], "")
440endfunc
441
442func Test_terminal_in_popup_color_transp_popup_highlight()
443 call Terminal_in_popup_color("transp_MyPopupHlCol", [
444 \ "highlight MyPopupHlCol ctermfg=cyan",
445 \ ], "", [], "highlight: 'MyPopupHlCol'")
446endfunc
447
448func Test_terminal_in_popup_color_gui_Terminal()
449 CheckFeature termguicolors
450 call Terminal_in_popup_color("gui_Terminal", [
451 \ "set termguicolors",
452 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
453 \ ], "", [], "")
454endfunc
455
456func Test_terminal_in_popup_color_gui_group()
457 CheckFeature termguicolors
458 call Terminal_in_popup_color("gui_MyTermCol", [
459 \ "set termguicolors",
460 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
461 \ ], "term_highlight: 'MyTermCol',", [], "")
462endfunc
463
464func Test_terminal_in_popup_color_gui_wincolor()
465 CheckFeature termguicolors
466 call Terminal_in_popup_color("gui_MyWinCol", [
467 \ "set termguicolors",
468 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
469 \ ], "", [
470 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
471 \ ], "")
472endfunc
473
474func Test_terminal_in_popup_color_gui_popup_highlight()
475 CheckFeature termguicolors
476 call Terminal_in_popup_color("gui_MyPopupHlCol", [
477 \ "set termguicolors",
478 \ "highlight MyPopupHlCol guifg=#00e8f0 guibg=#126521",
479 \ ], "", [], "highlight: 'MyPopupHlCol'")
480endfunc
481
482func Test_terminal_in_popup_color_gui_transp_Terminal()
483 CheckFeature termguicolors
484 call Terminal_in_popup_color("gui_transp_Terminal", [
485 \ "set termguicolors",
486 \ "highlight Terminal guifg=#3344ff",
487 \ ], "", [], "")
488endfunc
489
490func Test_terminal_in_popup_color_gui_transp_group()
491 CheckFeature termguicolors
492 call Terminal_in_popup_color("gui_transp_MyTermCol", [
493 \ "set termguicolors",
494 \ "highlight MyTermCol guifg=#007800",
495 \ ], "term_highlight: 'MyTermCol',", [], "")
496endfunc
497
498func Test_terminal_in_popup_color_gui_transp_wincolor()
499 CheckFeature termguicolors
500 call Terminal_in_popup_color("gui_transp_MyWinCol", [
501 \ "set termguicolors",
502 \ "highlight MyWinCol guifg=#fe1122",
503 \ ], "", [
504 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
505 \ ], "")
506endfunc
507
508func Test_terminal_in_popup_color_gui_transp_popup_highlight()
509 CheckFeature termguicolors
510 call Terminal_in_popup_color("gui_transp_MyPopupHlCol", [
511 \ "set termguicolors",
512 \ "highlight MyPopupHlCol guifg=#00e8f0",
513 \ ], "", [], "highlight: 'MyPopupHlCol'")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200514endfunc
515
516func Test_double_popup_terminal()
517 let buf1 = term_start(&shell, #{hidden: 1})
518 let win1 = popup_create(buf1, {})
519 let buf2 = term_start(&shell, #{hidden: 1})
520 call assert_fails('call popup_create(buf2, {})', 'E861:')
521 call popup_close(win1)
522 exe buf1 .. 'bwipe!'
523 exe buf2 .. 'bwipe!'
524endfunc
525
LemonBoy4a392d22022-04-23 14:07:56 +0100526func Test_escape_popup_terminal()
527 set hidden
528
529 " Cannot escape a terminal popup window using win_gotoid
530 let prev_win = win_getid()
531 eval term_start('sh', #{hidden: 1, term_finish: 'close'})->popup_create({})
532 call assert_fails("call win_gotoid(" .. prev_win .. ")", 'E863:')
533
534 call popup_clear(1)
535 set hidden&
536endfunc
537
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200538func Test_issue_5607()
539 let wincount = winnr('$')
540 exe 'terminal' &shell &shellcmdflag 'exit'
541 let job = term_getjob(bufnr())
542 call WaitForAssert({-> assert_equal("dead", job_status(job))})
543
544 let old_wincolor = &wincolor
545 try
546 set wincolor=
547 finally
548 let &wincolor = old_wincolor
549 bw!
550 endtry
551endfunc
552
553func Test_hidden_terminal()
554 let buf = term_start(&shell, #{hidden: 1})
555 call assert_equal('', bufname('^$'))
556 call StopShellInTerminal(buf)
557endfunc
558
559func Test_term_nasty_callback()
560 CheckExecutable sh
561
562 set hidden
563 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
564 call popup_create(g:buf0, {})
565 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
566
567 call popup_clear(1)
568 set hidden&
569endfunc
570
571func Test_term_and_startinsert()
572 CheckRunVimInTerminal
573 CheckUnix
574
575 let lines =<< trim EOL
576 put='some text'
577 term
578 startinsert
579 EOL
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100580 call writefile(lines, 'XTest_startinsert', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200581 let buf = RunVimInTerminal('-S XTest_startinsert', {})
582
583 call term_sendkeys(buf, "exit\r")
584 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
585 call term_sendkeys(buf, "0l")
586 call term_sendkeys(buf, "A<\<Esc>")
587 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
588
589 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200590endfunc
591
592" Test for passing invalid arguments to terminal functions
593func Test_term_func_invalid_arg()
594 call assert_fails('let b = term_getaltscreen([])', 'E745:')
595 call assert_fails('let a = term_getattr(1, [])', 'E730:')
596 call assert_fails('let c = term_getcursor([])', 'E745:')
597 call assert_fails('let l = term_getline([], 1)', 'E745:')
598 call assert_fails('let l = term_getscrolled([])', 'E745:')
599 call assert_fails('let s = term_getsize([])', 'E745:')
600 call assert_fails('let s = term_getstatus([])', 'E745:')
601 call assert_fails('let s = term_scrape([], 1)', 'E745:')
602 call assert_fails('call term_sendkeys([], "a")', 'E745:')
603 call assert_fails('call term_setapi([], "")', 'E745:')
604 call assert_fails('call term_setrestore([], "")', 'E745:')
605 call assert_fails('call term_setkill([], "")', 'E745:')
606 if has('gui') || has('termguicolors')
607 call assert_fails('let p = term_getansicolors([])', 'E745:')
608 call assert_fails('call term_setansicolors([], [])', 'E745:')
609 endif
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100610 let buf = term_start('echo')
611 call assert_fails('call term_setapi(' .. buf .. ', {})', 'E731:')
612 call assert_fails('call term_setkill(' .. buf .. ', {})', 'E731:')
613 call assert_fails('call term_setrestore(' .. buf .. ', {})', 'E731:')
614 exe buf . "bwipe!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200615endfunc
616
617" Test for sending various special keycodes to a terminal
618func Test_term_keycode_translation()
619 CheckRunVimInTerminal
620
621 let buf = RunVimInTerminal('', {})
622 call term_sendkeys(buf, ":set nocompatible\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100623 call term_sendkeys(buf, ":set timeoutlen=20\<CR>")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200624
625 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
626 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
627 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
628 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
629 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
630 \ "\<S-Down>"]
631 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
632 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
633 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
634 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
635 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
636
637 call term_sendkeys(buf, "i")
638 for i in range(len(keys))
639 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100640 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))}, 200)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200641 endfor
642
643 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
644 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
645 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
646 let keypad_output = ['0', '1', '2', '3', '4', '5',
647 \ '6', '7', '8', '9', '.', '+',
648 \ '-', '*', '/']
649 for i in range(len(keypad_keys))
650 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
651 if keypad_output[i] == '3' || keypad_output[i] == '9'
652 continue
653 endif
654 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100655 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))}, 100)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200656 endfor
657
658 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
659 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
660
661 call StopVimInTerminal(buf)
662endfunc
663
664" Test for using the mouse in a terminal
665func Test_term_mouse()
666 CheckNotGui
667 CheckRunVimInTerminal
668
669 let save_mouse = &mouse
670 let save_term = &term
671 let save_ttymouse = &ttymouse
672 let save_clipboard = &clipboard
673 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
674
675 let lines =<< trim END
676 one two three four five
677 red green yellow red blue
678 vim emacs sublime nano
679 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100680 call writefile(lines, 'Xtest_mouse', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200681
682 " Create a terminal window running Vim for the test with mouse enabled
683 let prev_win = win_getid()
684 let buf = RunVimInTerminal('Xtest_mouse -n', {})
685 call term_sendkeys(buf, ":set nocompatible\<CR>")
686 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
687 call term_sendkeys(buf, ":set clipboard=\<CR>")
688 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200689 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200690 redraw!
691
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000692 " Funcref used in WaitFor() to check that the "Xbuf" file is readable and
693 " has some contents. This avoids a "List index out of range" error when the
694 " file hasn't been written yet.
695 let XbufNotEmpty = {-> filereadable('Xbuf') && len(readfile('Xbuf')) > 0}
James McCoy157241e2022-11-09 23:29:14 +0000696
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200697 " Use the mouse to enter the terminal window
698 call win_gotoid(prev_win)
699 call feedkeys(MouseLeftClickCode(1, 1), 'x')
700 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
701 call assert_equal(1, getwininfo(win_getid())[0].terminal)
702
703 " Test for <LeftMouse> click/release
704 call test_setmouse(2, 5)
705 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
706 call test_setmouse(3, 8)
707 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200708 call TermWait(buf, 50)
James McCoy157241e2022-11-09 23:29:14 +0000709 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200710 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200711 call TermWait(buf, 50)
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000712 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200713 let pos = json_decode(readfile('Xbuf')[0])
714 call assert_equal([3, 8], pos[1:2])
James McCoy157241e2022-11-09 23:29:14 +0000715 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200716
717 " Test for selecting text using mouse
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200718 call test_setmouse(2, 11)
719 call term_sendkeys(buf, "\<LeftMouse>")
720 call test_setmouse(2, 16)
721 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200722 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200723 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000724 call WaitFor(XbufNotEmpty)
Bram Moolenaar1d139a02022-11-10 00:09:22 +0000725 call WaitForAssert({-> assert_equal('yellow', readfile('Xbuf')[0])})
James McCoy157241e2022-11-09 23:29:14 +0000726 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200727
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000728 " Test for selecting text using double click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200729 call test_setmouse(1, 11)
730 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
731 call test_setmouse(1, 17)
732 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200733 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200734 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000735 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200736 call assert_equal('three four', readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000737 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200738
739 " Test for selecting a line using triple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200740 call test_setmouse(3, 2)
741 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200742 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200743 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000744 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200745 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000746 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200747
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000748 " Test for selecting a block using quadruple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200749 call test_setmouse(1, 11)
750 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
751 call test_setmouse(3, 13)
752 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200753 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200754 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000755 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200756 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000757 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200758
759 " Test for extending a selection using right click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200760 call test_setmouse(2, 9)
761 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
762 call test_setmouse(2, 16)
763 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200764 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200765 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000766 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200767 call assert_equal("n yellow", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000768 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200769
770 " Test for pasting text using middle click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200771 call term_sendkeys(buf, ":let @r='bright '\<CR>")
772 call test_setmouse(2, 22)
773 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200774 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200775 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000776 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200777 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
James McCoy157241e2022-11-09 23:29:14 +0000778 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200779
780 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200781 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200782 call StopVimInTerminal(buf)
783 let &mouse = save_mouse
784 let &term = save_term
785 let &ttymouse = save_ttymouse
786 let &clipboard = save_clipboard
787 set mousetime&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200788 call delete('Xbuf')
789endfunc
790
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200791" Test for sync buffer cwd with shell's pwd
792func Test_terminal_sync_shell_dir()
793 CheckUnix
794 " The test always use sh (see src/testdir/unix.vim).
Bram Moolenaar7bfa6d62022-01-14 12:06:47 +0000795 " BSD's sh doesn't seem to play well with the OSC 7 escape sequence.
796 CheckNotBSD
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200797
798 set asd
799 " , is
800 " 1. a valid character for directory names
801 " 2. a reserved character in url-encoding
802 let chars = ",a"
803 " "," is url-encoded as '%2C'
804 let chars_url = "%2Ca"
Bram Moolenaarced2b382022-01-13 15:25:32 +0000805 let tmpfolder = fnamemodify(tempname(),':h') .. '/' .. chars
806 let tmpfolder_url = fnamemodify(tempname(),':h') .. '/' .. chars_url
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200807 call mkdir(tmpfolder, "p")
808 let buf = Run_shell_in_terminal({})
Bram Moolenaarced2b382022-01-13 15:25:32 +0000809 call term_sendkeys(buf, "echo $'\\e\]7;file://" .. tmpfolder_url .. "\\a'\<CR>")
810 "call term_sendkeys(buf, "cd " .. tmpfolder .. "\<CR>")
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200811 call TermWait(buf)
812 if has("mac")
Bram Moolenaarced2b382022-01-13 15:25:32 +0000813 let expected = "/private" .. tmpfolder
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200814 else
815 let expected = tmpfolder
816 endif
817 call assert_equal(expected, getcwd(winnr()))
Bram Moolenaar82820d92021-03-30 20:54:28 +0200818
819 set noasd
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200820endfunc
821
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200822" Test for modeless selection in a terminal
823func Test_term_modeless_selection()
824 CheckUnix
825 CheckNotGui
826 CheckRunVimInTerminal
827 CheckFeature clipboard_working
828
829 let save_mouse = &mouse
830 let save_term = &term
831 let save_ttymouse = &ttymouse
832 set mouse=a term=xterm ttymouse=sgr mousetime=200
833 set clipboard=autoselectml
834
835 let lines =<< trim END
836 one two three four five
837 red green yellow red blue
838 vim emacs sublime nano
839 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100840 call writefile(lines, 'Xtest_modeless', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200841
842 " Create a terminal window running Vim for the test with mouse disabled
843 let prev_win = win_getid()
844 let buf = RunVimInTerminal('Xtest_modeless -n', {})
845 call term_sendkeys(buf, ":set nocompatible\<CR>")
846 call term_sendkeys(buf, ":set mouse=\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200847 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200848 redraw!
849
850 " Use the mouse to enter the terminal window
851 call win_gotoid(prev_win)
852 call feedkeys(MouseLeftClickCode(1, 1), 'x')
853 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200854 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200855 call assert_equal(1, getwininfo(win_getid())[0].terminal)
856
857 " Test for copying a modeless selection to clipboard
858 let @* = 'clean'
859 " communicating with X server may take a little time
860 sleep 100m
861 call feedkeys(MouseLeftClickCode(2, 3), 'x')
862 call feedkeys(MouseLeftDragCode(2, 11), 'x')
863 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
864 call assert_equal("d green y", @*)
865
866 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200867 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200868 call StopVimInTerminal(buf)
869 let &mouse = save_mouse
870 let &term = save_term
871 let &ttymouse = save_ttymouse
872 set mousetime& clipboard&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200873 new | only!
874endfunc
875
Bram Moolenaara4b44262020-07-12 21:38:29 +0200876func Test_terminal_getwinpos()
877 CheckRunVimInTerminal
878
879 " split, go to the bottom-right window
880 split
881 wincmd j
882 set splitright
883
Bram Moolenaar42095212020-07-21 21:48:58 +0200884 let buf = RunVimInTerminal('', {'cols': 60})
885 call TermWait(buf, 100)
886 call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
Bram Moolenaara4b44262020-07-12 21:38:29 +0200887
888 " Find the output of getwinpos() in the bottom line.
889 let rows = term_getsize(buf)[0]
890 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
891 let line = term_getline(buf, rows)
892 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
893 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
894
895 " Position must be bigger than the getwinpos() result of Vim itself.
896 " The calculation in the console assumes a 10 x 7 character cell.
897 " In the GUI it can be more, let's assume a 20 x 14 cell.
898 " And then add 100 / 200 tolerance.
899 let [xroot, yroot] = getwinpos()
900 let winpos = 50->getwinpos()
901 call assert_equal(xroot, winpos[0])
902 call assert_equal(yroot, winpos[1])
Bram Moolenaar7dfc5ce2020-09-05 15:05:30 +0200903 let [winrow, wincol] = win_screenpos(0)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200904 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
905 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
906 call assert_inrange(xroot + 2, xroot + xoff, xpos)
907 call assert_inrange(yroot + 2, yroot + yoff, ypos)
908
909 call TermWait(buf)
910 call term_sendkeys(buf, ":q\<CR>")
911 call StopVimInTerminal(buf)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200912 set splitright&
913 only!
914endfunc
915
ichizokc3f91c02021-12-17 09:44:33 +0000916func Test_terminal_term_start_error()
917 func s:term_start_error() abort
918 try
919 return term_start([[]])
920 catch
921 return v:exception
922 finally
923 "
924 endtry
925 endfunc
926 autocmd WinEnter * call type(0)
927
928 " Must not crash in s:term_start_error, nor the exception thrown.
929 let result = s:term_start_error()
930 call assert_match('^Vim(return):E730:', result)
931
932 autocmd! WinEnter
933 delfunc s:term_start_error
934endfunc
935
Anton Sharonov49528da2024-04-14 20:02:24 +0200936func Test_terminal_vt420()
937 CheckRunVimInTerminal
938 " For Termcap
939 CheckUnix
Christian Brabandt83d3b3b2024-04-30 20:45:09 +0200940 CheckExecutable infocmp
941 let a = system('infocmp vt420')
942 if v:shell_error
943 " reset v:shell_error
944 let a = system('true')
945 throw 'Skipped: vt420 terminfo not available'
946 endif
947 let rows = 15
Anton Sharonov49528da2024-04-14 20:02:24 +0200948 call writefile([':set term=vt420'], 'Xterm420', 'D')
949
950 let buf = RunVimInTerminal('-S Xterm420', #{rows: rows})
951 call TermWait(buf, 100)
952 call term_sendkeys(buf, ":set t_xo?\<CR>")
953 call WaitForAssert({-> assert_match('t_xo=y', term_getline(buf, rows))})
954 call StopVimInTerminal(buf)
955
956 call writefile([''], 'Xterm420')
957 let buf = RunVimInTerminal('-S Xterm420', #{rows: rows})
958 call TermWait(buf, 100)
959 call term_sendkeys(buf, ":set t_xo?\<CR>")
960 call WaitForAssert({-> assert_match('t_xo=\s\+', term_getline(buf, rows))})
961 call StopVimInTerminal(buf)
962endfunc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200963
Yegappan Lakshmanand603e952024-06-10 18:16:34 +0200964" Test for using 'vertical' with term_start(). If a following term_start(),
965" doesn't have the 'vertical' attribute, then it should be split horizontally.
966func Test_terminal_vertical()
967 let lines =<< trim END
968 call term_start("NONE", {'vertical': 1})
969 call term_start("NONE")
970 VAR layout = winlayout()
971 call assert_equal('row', layout[0], string(layout))
972 call assert_equal('col', layout[1][0][0], string(layout))
973 :%bw!
974 END
975 call v9.CheckLegacyAndVim9Success(lines)
976endfunc
977
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200978" vim: shiftwidth=2 sts=2 expandtab