blob: 9aa90492c8db7c21fb494cd4fd8ff4e92e201dc7 [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)
Drew Vogelea67ba72025-05-07 22:05:17 +020073 CheckScreendump
Bram Moolenaar87fd0922021-11-20 13:47:45 +000074 CheckRunVimInTerminal
75 CheckUnix
76
77 let lines = [
78 \ 'call setline(1, range(20))',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010079 \ 'func NotifyParent()',
80 \ ' call echoraw("' .. TermNotifyParentCmd(v:true) .. '")',
81 \ 'endfunc',
Bram Moolenaar87fd0922021-11-20 13:47:45 +000082 \ 'func OpenTerm()',
83 \ ' set noruler',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010084 \ " call term_start('cat', #{vertical: 1, "
85 \ .. 'exit_cb: {->NotifyParent()}, '
86 \ .. a:highlight_opt .. "})",
87 \ ' call NotifyParent()',
Bram Moolenaar87fd0922021-11-20 13:47:45 +000088 \ ] + a:open_cmds + [
89 \ 'endfunc',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010090 \ ] + a:highlight_cmds + [
91 \ 'call NotifyParent()',
92 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010093 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +000094 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
Yee Cheng Chine70587d2025-02-13 20:55:45 +010095 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +000096 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +010097 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +000098 call term_sendkeys(buf, "hello\<CR>")
99 call VerifyScreenDump(buf, 'Test_terminal_color_' .. a:group_name, {})
100
101 call term_sendkeys(buf, "\<C-D>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100102 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000103 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000104endfunc
105
106func Test_terminal_color_Terminal()
107 call Terminal_color("Terminal", [
108 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
109 \ ], "", [])
110endfunc
111
112func Test_terminal_color_group()
113 call Terminal_color("MyTermCol", [
114 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
115 \ ], "term_highlight: 'MyTermCol',", [])
116endfunc
117
118func Test_terminal_color_wincolor()
119 call Terminal_color("MyWinCol", [
120 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
121 \ ], "", [
122 \ 'set wincolor=MyWinCol',
123 \ ])
124endfunc
125
126func Test_terminal_color_group_over_Terminal()
127 call Terminal_color("MyTermCol_over_Terminal", [
128 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
129 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
130 \ ], "term_highlight: 'MyTermCol',", [])
131endfunc
132
133func Test_terminal_color_wincolor_over_group()
134 call Terminal_color("MyWinCol_over_group", [
135 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
136 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
137 \ ], "term_highlight: 'MyTermCol',", [
138 \ 'set wincolor=MyWinCol',
139 \ ])
140endfunc
141
142func Test_terminal_color_wincolor_split()
Drew Vogelea67ba72025-05-07 22:05:17 +0200143 CheckScreendump
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000144 CheckRunVimInTerminal
145 CheckUnix
146
147 let lines = [
148 \ 'call setline(1, range(20))',
149 \ 'func OpenTerm()',
150 \ ' set noruler',
151 \ " call term_start('cat', #{vertical: 1, term_highlight: 'MyTermCol'})",
152 \ 'endfunc',
153 \ 'highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue',
154 \ 'highlight MyWinCol ctermfg=red ctermbg=darkyellow',
155 \ 'highlight MyWinCol2 ctermfg=black ctermbg=blue',
156 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100157 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000158 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
159 call TermWait(buf, 100)
160 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
161 call TermWait(buf, 50)
162 call term_sendkeys(buf, "hello\<CR>")
163 call TermWait(buf, 50)
164
165 call term_sendkeys(buf, "\<C-W>:split\<CR>")
166 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol\<CR>")
167 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol', {})
168
169 call term_sendkeys(buf, "\<C-W>b:2sb\<CR>")
170 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol2\<CR>")
171 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol2', {})
172
173 call term_sendkeys(buf, "\<C-D>")
174 call TermWait(buf, 50)
175 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000176endfunc
177
178func Test_terminal_color_transp_Terminal()
179 call Terminal_color("transp_Terminal", [
180 \ "highlight Terminal ctermfg=blue",
181 \ ], "", [])
182endfunc
183
184func Test_terminal_color_transp_group()
185 call Terminal_color("transp_MyTermCol", [
186 \ "highlight MyTermCol ctermfg=darkgreen",
187 \ ], "term_highlight: 'MyTermCol',", [])
188endfunc
189
190func Test_terminal_color_transp_wincolor()
191 call Terminal_color("transp_MyWinCol", [
192 \ "highlight MyWinCol ctermfg=red",
193 \ ], "", [
194 \ 'set wincolor=MyWinCol',
195 \ ])
196endfunc
197
198func Test_terminal_color_gui_Terminal()
199 CheckFeature termguicolors
200 call Terminal_color("gui_Terminal", [
201 \ "set termguicolors",
202 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
203 \ ], "", [])
204endfunc
205
206func Test_terminal_color_gui_group()
207 CheckFeature termguicolors
208 call Terminal_color("gui_MyTermCol", [
209 \ "set termguicolors",
210 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
211 \ ], "term_highlight: 'MyTermCol',", [])
212endfunc
213
214func Test_terminal_color_gui_wincolor()
215 CheckFeature termguicolors
216 call Terminal_color("gui_MyWinCol", [
217 \ "set termguicolors",
218 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
219 \ ], "", [
220 \ 'set wincolor=MyWinCol',
221 \ ])
222endfunc
223
224func Test_terminal_color_gui_transp_Terminal()
225 CheckFeature termguicolors
226 call Terminal_color("gui_transp_Terminal", [
227 \ "set termguicolors",
228 \ "highlight Terminal guifg=#3344ff",
229 \ ], "", [])
230endfunc
231
232func Test_terminal_color_gui_transp_group()
233 CheckFeature termguicolors
234 call Terminal_color("gui_transp_MyTermCol", [
235 \ "set termguicolors",
236 \ "highlight MyTermCol guifg=#007800",
237 \ ], "term_highlight: 'MyTermCol',", [])
238endfunc
239
240func Test_terminal_color_gui_transp_wincolor()
241 CheckFeature termguicolors
242 call Terminal_color("gui_transp_MyWinCol", [
243 \ "set termguicolors",
244 \ "highlight MyWinCol guifg=#fe1122",
245 \ ], "", [
246 \ 'set wincolor=MyWinCol',
247 \ ])
248endfunc
249
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200250func Test_terminal_in_popup()
Drew Vogelea67ba72025-05-07 22:05:17 +0200251 CheckScreendump
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200252 CheckRunVimInTerminal
253
254 let text =<< trim END
255 some text
256 to edit
257 in a popup window
258 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100259 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200260 let cmd = GetVimCommandCleanTerm()
261 let lines = [
262 \ 'call setline(1, range(20))',
263 \ 'hi PopTerm ctermbg=grey',
264 \ 'func OpenTerm(setColor)',
265 \ " set noruler",
266 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
267 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
268 \ ' if a:setColor',
269 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
270 \ ' endif',
271 \ 'endfunc',
272 \ 'func HidePopup()',
273 \ ' call popup_hide(g:winid)',
274 \ 'endfunc',
275 \ 'func ClosePopup()',
276 \ ' call popup_close(g:winid)',
277 \ 'endfunc',
278 \ 'func ReopenPopup()',
279 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
280 \ 'endfunc',
281 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100282 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200283 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100284 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200285 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100286 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200287 call term_sendkeys(buf, ":\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100288 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200289 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
290 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
291
292 call term_sendkeys(buf, ":q\<CR>")
293 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
294
295 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100296 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200297 call term_sendkeys(buf, ":set hlsearch\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100298 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200299 call term_sendkeys(buf, "/edit\<CR>")
300 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
301
302 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
303 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
304 call term_sendkeys(buf, "\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100305 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200306
307 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
308 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
309
310 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
311 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
312
313 " Go to terminal-Normal mode and visually select text.
314 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
315 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
316
317 " Back to job mode, redraws
318 call term_sendkeys(buf, "A")
319 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
320
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100321 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200322 call term_sendkeys(buf, ":q\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100323 call WaitForAssert({-> assert_equal(0, match(term_getline(buf, 6), '^5\s*$'))}, 250) " wait for terminal to vanish
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200324
325 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200326endfunc
327
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100328" Check a terminal in popup window uses the default minimum size.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200329func Test_terminal_in_popup_min_size()
Drew Vogelea67ba72025-05-07 22:05:17 +0200330 CheckScreendump
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200331 CheckRunVimInTerminal
332
333 let text =<< trim END
334 another text
335 to show
336 in a popup window
337 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100338 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200339 let lines = [
340 \ 'call setline(1, range(20))',
341 \ 'func OpenTerm()',
342 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
343 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
344 \ 'endfunc',
345 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100346 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200347 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
348 call TermWait(buf, 100)
349 call term_sendkeys(buf, ":set noruler\<CR>")
350 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
351 call TermWait(buf, 50)
352 call term_sendkeys(buf, ":\<CR>")
353 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
354
355 call TermWait(buf, 50)
356 call term_sendkeys(buf, ":q\<CR>")
357 call TermWait(buf, 50) " wait for terminal to vanish
358 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200359endfunc
360
361" Check a terminal in popup window with different colors
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000362func Terminal_in_popup_color(group_name, highlight_cmds, highlight_opt, popup_cmds, popup_opt)
Drew Vogelea67ba72025-05-07 22:05:17 +0200363 CheckScreendump
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200364 CheckRunVimInTerminal
365 CheckUnix
366
367 let lines = [
368 \ 'call setline(1, range(20))',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100369 \ 'func NotifyParent(...)',
370 \ ' call echoraw("' .. TermNotifyParentCmd(v:true) .. '")',
371 \ 'endfunc',
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200372 \ 'func OpenTerm()',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100373 \ " let s:buf = term_start('cat', #{hidden: 1, term_finish: 'close', "
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200374 \ .. a:highlight_opt .. "})",
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100375 \ ' let g:winid = popup_create(s:buf, #{border: [], '
376 \ .. 'callback: {->NotifyParent()}, '
377 \ .. a:popup_opt .. '})',
378 \ ] + a:popup_cmds + [
379 \ ' call NotifyParent()',
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200380 \ 'endfunc',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100381 \ ] + a:highlight_cmds + [
382 \ 'call NotifyParent()',
383 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100384 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200385 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100386 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200387 call term_sendkeys(buf, ":set noruler\<CR>")
388 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100389 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200390 call term_sendkeys(buf, "hello\<CR>")
391 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
392
393 call term_sendkeys(buf, "\<C-D>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100394 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200395 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200396endfunc
397
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000398func Test_terminal_in_popup_color_Terminal()
399 call Terminal_in_popup_color("Terminal", [
400 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
401 \ ], "", [], "")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200402endfunc
403
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000404func Test_terminal_in_popup_color_group()
405 call Terminal_in_popup_color("MyTermCol", [
406 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
407 \ ], "term_highlight: 'MyTermCol',", [], "")
408endfunc
409
410func Test_terminal_in_popup_color_wincolor()
411 call Terminal_in_popup_color("MyWinCol", [
412 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
413 \ ], "", [
414 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
415 \ ], "")
416endfunc
417
418func Test_terminal_in_popup_color_popup_highlight()
419 call Terminal_in_popup_color("MyPopupHlCol", [
420 \ "highlight MyPopupHlCol ctermfg=cyan ctermbg=green",
421 \ ], "", [], "highlight: 'MyPopupHlCol'")
422endfunc
423
424func Test_terminal_in_popup_color_group_over_Terminal()
425 call Terminal_in_popup_color("MyTermCol_over_Terminal", [
426 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
427 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
428 \ ], "term_highlight: 'MyTermCol',", [], "")
429endfunc
430
431func Test_terminal_in_popup_color_wincolor_over_group()
432 call Terminal_in_popup_color("MyWinCol_over_group", [
433 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
434 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
435 \ ], "term_highlight: 'MyTermCol',", [
436 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
437 \ ], "")
438endfunc
439
440func Test_terminal_in_popup_color_transp_Terminal()
441 call Terminal_in_popup_color("transp_Terminal", [
442 \ "highlight Terminal ctermfg=blue",
443 \ ], "", [], "")
444endfunc
445
446func Test_terminal_in_popup_color_transp_group()
447 call Terminal_in_popup_color("transp_MyTermCol", [
448 \ "highlight MyTermCol ctermfg=darkgreen",
449 \ ], "term_highlight: 'MyTermCol',", [], "")
450endfunc
451
452func Test_terminal_in_popup_color_transp_wincolor()
453 call Terminal_in_popup_color("transp_MyWinCol", [
454 \ "highlight MyWinCol ctermfg=red",
455 \ ], "", [
456 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
457 \ ], "")
458endfunc
459
460func Test_terminal_in_popup_color_transp_popup_highlight()
461 call Terminal_in_popup_color("transp_MyPopupHlCol", [
462 \ "highlight MyPopupHlCol ctermfg=cyan",
463 \ ], "", [], "highlight: 'MyPopupHlCol'")
464endfunc
465
466func Test_terminal_in_popup_color_gui_Terminal()
467 CheckFeature termguicolors
468 call Terminal_in_popup_color("gui_Terminal", [
469 \ "set termguicolors",
470 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
471 \ ], "", [], "")
472endfunc
473
474func Test_terminal_in_popup_color_gui_group()
475 CheckFeature termguicolors
476 call Terminal_in_popup_color("gui_MyTermCol", [
477 \ "set termguicolors",
478 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
479 \ ], "term_highlight: 'MyTermCol',", [], "")
480endfunc
481
482func Test_terminal_in_popup_color_gui_wincolor()
483 CheckFeature termguicolors
484 call Terminal_in_popup_color("gui_MyWinCol", [
485 \ "set termguicolors",
486 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
487 \ ], "", [
488 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
489 \ ], "")
490endfunc
491
492func Test_terminal_in_popup_color_gui_popup_highlight()
493 CheckFeature termguicolors
494 call Terminal_in_popup_color("gui_MyPopupHlCol", [
495 \ "set termguicolors",
496 \ "highlight MyPopupHlCol guifg=#00e8f0 guibg=#126521",
497 \ ], "", [], "highlight: 'MyPopupHlCol'")
498endfunc
499
500func Test_terminal_in_popup_color_gui_transp_Terminal()
501 CheckFeature termguicolors
502 call Terminal_in_popup_color("gui_transp_Terminal", [
503 \ "set termguicolors",
504 \ "highlight Terminal guifg=#3344ff",
505 \ ], "", [], "")
506endfunc
507
508func Test_terminal_in_popup_color_gui_transp_group()
509 CheckFeature termguicolors
510 call Terminal_in_popup_color("gui_transp_MyTermCol", [
511 \ "set termguicolors",
512 \ "highlight MyTermCol guifg=#007800",
513 \ ], "term_highlight: 'MyTermCol',", [], "")
514endfunc
515
516func Test_terminal_in_popup_color_gui_transp_wincolor()
517 CheckFeature termguicolors
518 call Terminal_in_popup_color("gui_transp_MyWinCol", [
519 \ "set termguicolors",
520 \ "highlight MyWinCol guifg=#fe1122",
521 \ ], "", [
522 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
523 \ ], "")
524endfunc
525
526func Test_terminal_in_popup_color_gui_transp_popup_highlight()
527 CheckFeature termguicolors
528 call Terminal_in_popup_color("gui_transp_MyPopupHlCol", [
529 \ "set termguicolors",
530 \ "highlight MyPopupHlCol guifg=#00e8f0",
531 \ ], "", [], "highlight: 'MyPopupHlCol'")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200532endfunc
533
534func Test_double_popup_terminal()
535 let buf1 = term_start(&shell, #{hidden: 1})
536 let win1 = popup_create(buf1, {})
537 let buf2 = term_start(&shell, #{hidden: 1})
538 call assert_fails('call popup_create(buf2, {})', 'E861:')
539 call popup_close(win1)
540 exe buf1 .. 'bwipe!'
541 exe buf2 .. 'bwipe!'
542endfunc
543
LemonBoy4a392d22022-04-23 14:07:56 +0100544func Test_escape_popup_terminal()
545 set hidden
546
547 " Cannot escape a terminal popup window using win_gotoid
548 let prev_win = win_getid()
549 eval term_start('sh', #{hidden: 1, term_finish: 'close'})->popup_create({})
550 call assert_fails("call win_gotoid(" .. prev_win .. ")", 'E863:')
551
552 call popup_clear(1)
553 set hidden&
554endfunc
555
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200556func Test_issue_5607()
557 let wincount = winnr('$')
558 exe 'terminal' &shell &shellcmdflag 'exit'
559 let job = term_getjob(bufnr())
560 call WaitForAssert({-> assert_equal("dead", job_status(job))})
561
562 let old_wincolor = &wincolor
563 try
564 set wincolor=
565 finally
566 let &wincolor = old_wincolor
567 bw!
568 endtry
569endfunc
570
571func Test_hidden_terminal()
572 let buf = term_start(&shell, #{hidden: 1})
573 call assert_equal('', bufname('^$'))
574 call StopShellInTerminal(buf)
575endfunc
576
577func Test_term_nasty_callback()
578 CheckExecutable sh
579
580 set hidden
581 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
582 call popup_create(g:buf0, {})
583 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
584
585 call popup_clear(1)
586 set hidden&
587endfunc
588
589func Test_term_and_startinsert()
590 CheckRunVimInTerminal
591 CheckUnix
592
593 let lines =<< trim EOL
594 put='some text'
595 term
596 startinsert
597 EOL
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100598 call writefile(lines, 'XTest_startinsert', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200599 let buf = RunVimInTerminal('-S XTest_startinsert', {})
600
601 call term_sendkeys(buf, "exit\r")
602 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
603 call term_sendkeys(buf, "0l")
604 call term_sendkeys(buf, "A<\<Esc>")
605 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
606
607 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200608endfunc
609
610" Test for passing invalid arguments to terminal functions
611func Test_term_func_invalid_arg()
612 call assert_fails('let b = term_getaltscreen([])', 'E745:')
613 call assert_fails('let a = term_getattr(1, [])', 'E730:')
614 call assert_fails('let c = term_getcursor([])', 'E745:')
615 call assert_fails('let l = term_getline([], 1)', 'E745:')
616 call assert_fails('let l = term_getscrolled([])', 'E745:')
617 call assert_fails('let s = term_getsize([])', 'E745:')
618 call assert_fails('let s = term_getstatus([])', 'E745:')
619 call assert_fails('let s = term_scrape([], 1)', 'E745:')
620 call assert_fails('call term_sendkeys([], "a")', 'E745:')
621 call assert_fails('call term_setapi([], "")', 'E745:')
622 call assert_fails('call term_setrestore([], "")', 'E745:')
623 call assert_fails('call term_setkill([], "")', 'E745:')
624 if has('gui') || has('termguicolors')
625 call assert_fails('let p = term_getansicolors([])', 'E745:')
626 call assert_fails('call term_setansicolors([], [])', 'E745:')
627 endif
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100628 let buf = term_start('echo')
629 call assert_fails('call term_setapi(' .. buf .. ', {})', 'E731:')
630 call assert_fails('call term_setkill(' .. buf .. ', {})', 'E731:')
631 call assert_fails('call term_setrestore(' .. buf .. ', {})', 'E731:')
632 exe buf . "bwipe!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200633endfunc
634
635" Test for sending various special keycodes to a terminal
636func Test_term_keycode_translation()
637 CheckRunVimInTerminal
638
639 let buf = RunVimInTerminal('', {})
640 call term_sendkeys(buf, ":set nocompatible\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100641 call term_sendkeys(buf, ":set timeoutlen=20\<CR>")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200642
643 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
644 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
645 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
646 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
647 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
648 \ "\<S-Down>"]
649 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
650 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
651 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
652 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
653 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
654
655 call term_sendkeys(buf, "i")
656 for i in range(len(keys))
657 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100658 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))}, 200)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200659 endfor
660
661 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
662 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
663 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
664 let keypad_output = ['0', '1', '2', '3', '4', '5',
665 \ '6', '7', '8', '9', '.', '+',
666 \ '-', '*', '/']
667 for i in range(len(keypad_keys))
668 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
669 if keypad_output[i] == '3' || keypad_output[i] == '9'
670 continue
671 endif
672 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100673 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))}, 100)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200674 endfor
675
676 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
677 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
678
679 call StopVimInTerminal(buf)
680endfunc
681
682" Test for using the mouse in a terminal
683func Test_term_mouse()
684 CheckNotGui
685 CheckRunVimInTerminal
686
687 let save_mouse = &mouse
688 let save_term = &term
689 let save_ttymouse = &ttymouse
690 let save_clipboard = &clipboard
691 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
692
693 let lines =<< trim END
694 one two three four five
695 red green yellow red blue
696 vim emacs sublime nano
697 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100698 call writefile(lines, 'Xtest_mouse', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200699
700 " Create a terminal window running Vim for the test with mouse enabled
701 let prev_win = win_getid()
702 let buf = RunVimInTerminal('Xtest_mouse -n', {})
703 call term_sendkeys(buf, ":set nocompatible\<CR>")
704 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
705 call term_sendkeys(buf, ":set clipboard=\<CR>")
706 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200707 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200708 redraw!
709
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000710 " Funcref used in WaitFor() to check that the "Xbuf" file is readable and
711 " has some contents. This avoids a "List index out of range" error when the
712 " file hasn't been written yet.
713 let XbufNotEmpty = {-> filereadable('Xbuf') && len(readfile('Xbuf')) > 0}
James McCoy157241e2022-11-09 23:29:14 +0000714
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200715 " Use the mouse to enter the terminal window
716 call win_gotoid(prev_win)
717 call feedkeys(MouseLeftClickCode(1, 1), 'x')
718 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
719 call assert_equal(1, getwininfo(win_getid())[0].terminal)
720
721 " Test for <LeftMouse> click/release
722 call test_setmouse(2, 5)
723 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
724 call test_setmouse(3, 8)
725 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200726 call TermWait(buf, 50)
James McCoy157241e2022-11-09 23:29:14 +0000727 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200728 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200729 call TermWait(buf, 50)
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000730 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200731 let pos = json_decode(readfile('Xbuf')[0])
732 call assert_equal([3, 8], pos[1:2])
James McCoy157241e2022-11-09 23:29:14 +0000733 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200734
735 " Test for selecting text using mouse
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200736 call test_setmouse(2, 11)
737 call term_sendkeys(buf, "\<LeftMouse>")
738 call test_setmouse(2, 16)
739 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200740 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200741 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000742 call WaitFor(XbufNotEmpty)
Bram Moolenaar1d139a02022-11-10 00:09:22 +0000743 call WaitForAssert({-> assert_equal('yellow', readfile('Xbuf')[0])})
James McCoy157241e2022-11-09 23:29:14 +0000744 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200745
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000746 " Test for selecting text using double click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200747 call test_setmouse(1, 11)
748 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
749 call test_setmouse(1, 17)
750 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200751 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200752 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000753 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200754 call assert_equal('three four', readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000755 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200756
757 " Test for selecting a line using triple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200758 call test_setmouse(3, 2)
759 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200760 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200761 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000762 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200763 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000764 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200765
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000766 " Test for selecting a block using quadruple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200767 call test_setmouse(1, 11)
768 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
769 call test_setmouse(3, 13)
770 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200771 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200772 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000773 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200774 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000775 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200776
777 " Test for extending a selection using right click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200778 call test_setmouse(2, 9)
779 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
780 call test_setmouse(2, 16)
781 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200782 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200783 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000784 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200785 call assert_equal("n yellow", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000786 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200787
788 " Test for pasting text using middle click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200789 call term_sendkeys(buf, ":let @r='bright '\<CR>")
790 call test_setmouse(2, 22)
791 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200792 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200793 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000794 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200795 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
James McCoy157241e2022-11-09 23:29:14 +0000796 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200797
798 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200799 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200800 call StopVimInTerminal(buf)
801 let &mouse = save_mouse
802 let &term = save_term
803 let &ttymouse = save_ttymouse
804 let &clipboard = save_clipboard
805 set mousetime&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200806 call delete('Xbuf')
807endfunc
808
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200809" Test for sync buffer cwd with shell's pwd
810func Test_terminal_sync_shell_dir()
811 CheckUnix
812 " The test always use sh (see src/testdir/unix.vim).
Bram Moolenaar7bfa6d62022-01-14 12:06:47 +0000813 " BSD's sh doesn't seem to play well with the OSC 7 escape sequence.
814 CheckNotBSD
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200815
816 set asd
817 " , is
818 " 1. a valid character for directory names
819 " 2. a reserved character in url-encoding
820 let chars = ",a"
821 " "," is url-encoded as '%2C'
822 let chars_url = "%2Ca"
Bram Moolenaarced2b382022-01-13 15:25:32 +0000823 let tmpfolder = fnamemodify(tempname(),':h') .. '/' .. chars
824 let tmpfolder_url = fnamemodify(tempname(),':h') .. '/' .. chars_url
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200825 call mkdir(tmpfolder, "p")
826 let buf = Run_shell_in_terminal({})
Bram Moolenaarced2b382022-01-13 15:25:32 +0000827 call term_sendkeys(buf, "echo $'\\e\]7;file://" .. tmpfolder_url .. "\\a'\<CR>")
828 "call term_sendkeys(buf, "cd " .. tmpfolder .. "\<CR>")
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200829 call TermWait(buf)
830 if has("mac")
Bram Moolenaarced2b382022-01-13 15:25:32 +0000831 let expected = "/private" .. tmpfolder
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200832 else
833 let expected = tmpfolder
834 endif
835 call assert_equal(expected, getcwd(winnr()))
Bram Moolenaar82820d92021-03-30 20:54:28 +0200836
837 set noasd
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200838endfunc
839
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200840" Test for modeless selection in a terminal
841func Test_term_modeless_selection()
842 CheckUnix
843 CheckNotGui
844 CheckRunVimInTerminal
845 CheckFeature clipboard_working
846
847 let save_mouse = &mouse
848 let save_term = &term
849 let save_ttymouse = &ttymouse
850 set mouse=a term=xterm ttymouse=sgr mousetime=200
851 set clipboard=autoselectml
852
853 let lines =<< trim END
854 one two three four five
855 red green yellow red blue
856 vim emacs sublime nano
857 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100858 call writefile(lines, 'Xtest_modeless', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200859
860 " Create a terminal window running Vim for the test with mouse disabled
861 let prev_win = win_getid()
862 let buf = RunVimInTerminal('Xtest_modeless -n', {})
863 call term_sendkeys(buf, ":set nocompatible\<CR>")
864 call term_sendkeys(buf, ":set mouse=\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200865 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200866 redraw!
867
868 " Use the mouse to enter the terminal window
869 call win_gotoid(prev_win)
870 call feedkeys(MouseLeftClickCode(1, 1), 'x')
871 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200872 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200873 call assert_equal(1, getwininfo(win_getid())[0].terminal)
874
875 " Test for copying a modeless selection to clipboard
876 let @* = 'clean'
877 " communicating with X server may take a little time
878 sleep 100m
879 call feedkeys(MouseLeftClickCode(2, 3), 'x')
880 call feedkeys(MouseLeftDragCode(2, 11), 'x')
881 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
882 call assert_equal("d green y", @*)
883
884 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200885 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200886 call StopVimInTerminal(buf)
887 let &mouse = save_mouse
888 let &term = save_term
889 let &ttymouse = save_ttymouse
890 set mousetime& clipboard&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200891 new | only!
892endfunc
893
Bram Moolenaara4b44262020-07-12 21:38:29 +0200894func Test_terminal_getwinpos()
895 CheckRunVimInTerminal
896
897 " split, go to the bottom-right window
898 split
899 wincmd j
900 set splitright
901
Bram Moolenaar42095212020-07-21 21:48:58 +0200902 let buf = RunVimInTerminal('', {'cols': 60})
903 call TermWait(buf, 100)
904 call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
Bram Moolenaara4b44262020-07-12 21:38:29 +0200905
906 " Find the output of getwinpos() in the bottom line.
907 let rows = term_getsize(buf)[0]
908 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
909 let line = term_getline(buf, rows)
910 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
911 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
912
913 " Position must be bigger than the getwinpos() result of Vim itself.
914 " The calculation in the console assumes a 10 x 7 character cell.
915 " In the GUI it can be more, let's assume a 20 x 14 cell.
916 " And then add 100 / 200 tolerance.
917 let [xroot, yroot] = getwinpos()
918 let winpos = 50->getwinpos()
919 call assert_equal(xroot, winpos[0])
920 call assert_equal(yroot, winpos[1])
Bram Moolenaar7dfc5ce2020-09-05 15:05:30 +0200921 let [winrow, wincol] = win_screenpos(0)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200922 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
923 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
924 call assert_inrange(xroot + 2, xroot + xoff, xpos)
925 call assert_inrange(yroot + 2, yroot + yoff, ypos)
926
927 call TermWait(buf)
928 call term_sendkeys(buf, ":q\<CR>")
929 call StopVimInTerminal(buf)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200930 set splitright&
931 only!
932endfunc
933
ichizokc3f91c02021-12-17 09:44:33 +0000934func Test_terminal_term_start_error()
935 func s:term_start_error() abort
936 try
937 return term_start([[]])
938 catch
939 return v:exception
940 finally
941 "
942 endtry
943 endfunc
944 autocmd WinEnter * call type(0)
945
946 " Must not crash in s:term_start_error, nor the exception thrown.
947 let result = s:term_start_error()
948 call assert_match('^Vim(return):E730:', result)
949
950 autocmd! WinEnter
951 delfunc s:term_start_error
952endfunc
953
Anton Sharonov49528da2024-04-14 20:02:24 +0200954func Test_terminal_vt420()
955 CheckRunVimInTerminal
956 " For Termcap
957 CheckUnix
Christian Brabandt83d3b3b2024-04-30 20:45:09 +0200958 CheckExecutable infocmp
959 let a = system('infocmp vt420')
960 if v:shell_error
961 " reset v:shell_error
962 let a = system('true')
963 throw 'Skipped: vt420 terminfo not available'
964 endif
965 let rows = 15
Anton Sharonov49528da2024-04-14 20:02:24 +0200966 call writefile([':set term=vt420'], 'Xterm420', 'D')
967
968 let buf = RunVimInTerminal('-S Xterm420', #{rows: rows})
969 call TermWait(buf, 100)
970 call term_sendkeys(buf, ":set t_xo?\<CR>")
971 call WaitForAssert({-> assert_match('t_xo=y', term_getline(buf, rows))})
972 call StopVimInTerminal(buf)
973
974 call writefile([''], 'Xterm420')
975 let buf = RunVimInTerminal('-S Xterm420', #{rows: rows})
976 call TermWait(buf, 100)
977 call term_sendkeys(buf, ":set t_xo?\<CR>")
978 call WaitForAssert({-> assert_match('t_xo=\s\+', term_getline(buf, rows))})
979 call StopVimInTerminal(buf)
980endfunc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200981
Yegappan Lakshmanand603e952024-06-10 18:16:34 +0200982" Test for using 'vertical' with term_start(). If a following term_start(),
983" doesn't have the 'vertical' attribute, then it should be split horizontally.
984func Test_terminal_vertical()
985 let lines =<< trim END
986 call term_start("NONE", {'vertical': 1})
987 call term_start("NONE")
988 VAR layout = winlayout()
989 call assert_equal('row', layout[0], string(layout))
990 call assert_equal('col', layout[1][0][0], string(layout))
991 :%bw!
992 END
993 call v9.CheckLegacyAndVim9Success(lines)
994endfunc
995
Christian Brabandt991657e2024-10-15 20:31:14 +0200996" Needs to come before Test_hidden_terminal(), why?
997func Test_autocmd_buffilepost_with_hidden_term()
998 CheckExecutable true
999 new XTestFile
1000 defer delete('XTestFile')
1001 call setline(1, ['one', 'two', 'three'])
1002 call cursor(3, 10)
1003 augroup TestCursor
1004 au!
1005 autocmd BufFilePost * call setbufvar(3, '&tabstop', 4)
1006 augroup END
1007
1008 let buf = term_start(['true'], #{hidden: 1, term_finish: 'close'})
1009 call term_wait(buf)
1010 redraw!
1011 call assert_equal('XTestFile', bufname('%'))
1012 call assert_equal([0, 3, 5, 0], getpos('.'))
1013
1014 augroup TestCursor
1015 au!
1016 augroup END
1017 augroup! TestCursor
1018 bw! XTestFile
1019endfunc
1020
Christian Brabandta899b272025-06-28 18:40:15 +02001021func Test_terminal_visual_empty_listchars()
1022 CheckScreendump
1023 CheckRunVimInTerminal
1024 CheckUnix
1025
1026 let lines = [
1027 \ 'set listchars=',
1028 \ ':term sh -c "printf ''hello\\n\\nhello''"'
1029 \ ]
1030 call writefile(lines, 'XtermStart1', 'D')
1031 let buf = RunVimInTerminal('-S XtermStart1', #{rows: 15})
1032 call term_wait(buf)
1033 call term_sendkeys(buf, "V2k")
1034 call VerifyScreenDump(buf, 'Test_terminal_empty_listchars', {})
1035
1036 call StopVimInTerminal(buf)
1037endfunc
1038
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001039" vim: shiftwidth=2 sts=2 expandtab