blob: 0e5f70b944fab343694e97066f4cff5b08f14b00 [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))',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010078 \ 'func NotifyParent()',
79 \ ' call echoraw("' .. TermNotifyParentCmd(v:true) .. '")',
80 \ 'endfunc',
Bram Moolenaar87fd0922021-11-20 13:47:45 +000081 \ 'func OpenTerm()',
82 \ ' set noruler',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010083 \ " call term_start('cat', #{vertical: 1, "
84 \ .. 'exit_cb: {->NotifyParent()}, '
85 \ .. a:highlight_opt .. "})",
86 \ ' call NotifyParent()',
Bram Moolenaar87fd0922021-11-20 13:47:45 +000087 \ ] + a:open_cmds + [
88 \ 'endfunc',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010089 \ ] + a:highlight_cmds + [
90 \ 'call NotifyParent()',
91 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010092 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +000093 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
Yee Cheng Chine70587d2025-02-13 20:55:45 +010094 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +000095 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +010096 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +000097 call term_sendkeys(buf, "hello\<CR>")
98 call VerifyScreenDump(buf, 'Test_terminal_color_' .. a:group_name, {})
99
100 call term_sendkeys(buf, "\<C-D>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100101 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000102 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000103endfunc
104
105func Test_terminal_color_Terminal()
106 call Terminal_color("Terminal", [
107 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
108 \ ], "", [])
109endfunc
110
111func Test_terminal_color_group()
112 call Terminal_color("MyTermCol", [
113 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
114 \ ], "term_highlight: 'MyTermCol',", [])
115endfunc
116
117func Test_terminal_color_wincolor()
118 call Terminal_color("MyWinCol", [
119 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
120 \ ], "", [
121 \ 'set wincolor=MyWinCol',
122 \ ])
123endfunc
124
125func Test_terminal_color_group_over_Terminal()
126 call Terminal_color("MyTermCol_over_Terminal", [
127 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
128 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
129 \ ], "term_highlight: 'MyTermCol',", [])
130endfunc
131
132func Test_terminal_color_wincolor_over_group()
133 call Terminal_color("MyWinCol_over_group", [
134 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
135 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
136 \ ], "term_highlight: 'MyTermCol',", [
137 \ 'set wincolor=MyWinCol',
138 \ ])
139endfunc
140
141func Test_terminal_color_wincolor_split()
142 CheckRunVimInTerminal
143 CheckUnix
144
145 let lines = [
146 \ 'call setline(1, range(20))',
147 \ 'func OpenTerm()',
148 \ ' set noruler',
149 \ " call term_start('cat', #{vertical: 1, term_highlight: 'MyTermCol'})",
150 \ 'endfunc',
151 \ 'highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue',
152 \ 'highlight MyWinCol ctermfg=red ctermbg=darkyellow',
153 \ 'highlight MyWinCol2 ctermfg=black ctermbg=blue',
154 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100155 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000156 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
157 call TermWait(buf, 100)
158 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
159 call TermWait(buf, 50)
160 call term_sendkeys(buf, "hello\<CR>")
161 call TermWait(buf, 50)
162
163 call term_sendkeys(buf, "\<C-W>:split\<CR>")
164 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol\<CR>")
165 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol', {})
166
167 call term_sendkeys(buf, "\<C-W>b:2sb\<CR>")
168 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol2\<CR>")
169 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol2', {})
170
171 call term_sendkeys(buf, "\<C-D>")
172 call TermWait(buf, 50)
173 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000174endfunc
175
176func Test_terminal_color_transp_Terminal()
177 call Terminal_color("transp_Terminal", [
178 \ "highlight Terminal ctermfg=blue",
179 \ ], "", [])
180endfunc
181
182func Test_terminal_color_transp_group()
183 call Terminal_color("transp_MyTermCol", [
184 \ "highlight MyTermCol ctermfg=darkgreen",
185 \ ], "term_highlight: 'MyTermCol',", [])
186endfunc
187
188func Test_terminal_color_transp_wincolor()
189 call Terminal_color("transp_MyWinCol", [
190 \ "highlight MyWinCol ctermfg=red",
191 \ ], "", [
192 \ 'set wincolor=MyWinCol',
193 \ ])
194endfunc
195
196func Test_terminal_color_gui_Terminal()
197 CheckFeature termguicolors
198 call Terminal_color("gui_Terminal", [
199 \ "set termguicolors",
200 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
201 \ ], "", [])
202endfunc
203
204func Test_terminal_color_gui_group()
205 CheckFeature termguicolors
206 call Terminal_color("gui_MyTermCol", [
207 \ "set termguicolors",
208 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
209 \ ], "term_highlight: 'MyTermCol',", [])
210endfunc
211
212func Test_terminal_color_gui_wincolor()
213 CheckFeature termguicolors
214 call Terminal_color("gui_MyWinCol", [
215 \ "set termguicolors",
216 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
217 \ ], "", [
218 \ 'set wincolor=MyWinCol',
219 \ ])
220endfunc
221
222func Test_terminal_color_gui_transp_Terminal()
223 CheckFeature termguicolors
224 call Terminal_color("gui_transp_Terminal", [
225 \ "set termguicolors",
226 \ "highlight Terminal guifg=#3344ff",
227 \ ], "", [])
228endfunc
229
230func Test_terminal_color_gui_transp_group()
231 CheckFeature termguicolors
232 call Terminal_color("gui_transp_MyTermCol", [
233 \ "set termguicolors",
234 \ "highlight MyTermCol guifg=#007800",
235 \ ], "term_highlight: 'MyTermCol',", [])
236endfunc
237
238func Test_terminal_color_gui_transp_wincolor()
239 CheckFeature termguicolors
240 call Terminal_color("gui_transp_MyWinCol", [
241 \ "set termguicolors",
242 \ "highlight MyWinCol guifg=#fe1122",
243 \ ], "", [
244 \ 'set wincolor=MyWinCol',
245 \ ])
246endfunc
247
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200248func Test_terminal_in_popup()
249 CheckRunVimInTerminal
250
251 let text =<< trim END
252 some text
253 to edit
254 in a popup window
255 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100256 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200257 let cmd = GetVimCommandCleanTerm()
258 let lines = [
259 \ 'call setline(1, range(20))',
260 \ 'hi PopTerm ctermbg=grey',
261 \ 'func OpenTerm(setColor)',
262 \ " set noruler",
263 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
264 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
265 \ ' if a:setColor',
266 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
267 \ ' endif',
268 \ 'endfunc',
269 \ 'func HidePopup()',
270 \ ' call popup_hide(g:winid)',
271 \ 'endfunc',
272 \ 'func ClosePopup()',
273 \ ' call popup_close(g:winid)',
274 \ 'endfunc',
275 \ 'func ReopenPopup()',
276 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
277 \ 'endfunc',
278 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100279 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200280 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100281 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200282 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100283 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200284 call term_sendkeys(buf, ":\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100285 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200286 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
287 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
288
289 call term_sendkeys(buf, ":q\<CR>")
290 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
291
292 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100293 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200294 call term_sendkeys(buf, ":set hlsearch\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100295 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200296 call term_sendkeys(buf, "/edit\<CR>")
297 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
298
299 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
300 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
301 call term_sendkeys(buf, "\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100302 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200303
304 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
305 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
306
307 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
308 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
309
310 " Go to terminal-Normal mode and visually select text.
311 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
312 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
313
314 " Back to job mode, redraws
315 call term_sendkeys(buf, "A")
316 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
317
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100318 call TermWait(buf,0)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200319 call term_sendkeys(buf, ":q\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100320 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 +0200321
322 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200323endfunc
324
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325" Check a terminal in popup window uses the default minimum size.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200326func Test_terminal_in_popup_min_size()
327 CheckRunVimInTerminal
328
329 let text =<< trim END
330 another text
331 to show
332 in a popup window
333 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100334 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200335 let lines = [
336 \ 'call setline(1, range(20))',
337 \ 'func OpenTerm()',
338 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
339 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
340 \ 'endfunc',
341 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100342 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200343 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
344 call TermWait(buf, 100)
345 call term_sendkeys(buf, ":set noruler\<CR>")
346 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
347 call TermWait(buf, 50)
348 call term_sendkeys(buf, ":\<CR>")
349 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
350
351 call TermWait(buf, 50)
352 call term_sendkeys(buf, ":q\<CR>")
353 call TermWait(buf, 50) " wait for terminal to vanish
354 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200355endfunc
356
357" Check a terminal in popup window with different colors
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000358func Terminal_in_popup_color(group_name, highlight_cmds, highlight_opt, popup_cmds, popup_opt)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200359 CheckRunVimInTerminal
360 CheckUnix
361
362 let lines = [
363 \ 'call setline(1, range(20))',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100364 \ 'func NotifyParent(...)',
365 \ ' call echoraw("' .. TermNotifyParentCmd(v:true) .. '")',
366 \ 'endfunc',
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200367 \ 'func OpenTerm()',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100368 \ " let s:buf = term_start('cat', #{hidden: 1, term_finish: 'close', "
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200369 \ .. a:highlight_opt .. "})",
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100370 \ ' let g:winid = popup_create(s:buf, #{border: [], '
371 \ .. 'callback: {->NotifyParent()}, '
372 \ .. a:popup_opt .. '})',
373 \ ] + a:popup_cmds + [
374 \ ' call NotifyParent()',
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200375 \ 'endfunc',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100376 \ ] + a:highlight_cmds + [
377 \ 'call NotifyParent()',
378 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100379 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200380 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100381 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200382 call term_sendkeys(buf, ":set noruler\<CR>")
383 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100384 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200385 call term_sendkeys(buf, "hello\<CR>")
386 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
387
388 call term_sendkeys(buf, "\<C-D>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100389 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200390 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200391endfunc
392
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000393func Test_terminal_in_popup_color_Terminal()
394 call Terminal_in_popup_color("Terminal", [
395 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
396 \ ], "", [], "")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200397endfunc
398
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000399func Test_terminal_in_popup_color_group()
400 call Terminal_in_popup_color("MyTermCol", [
401 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
402 \ ], "term_highlight: 'MyTermCol',", [], "")
403endfunc
404
405func Test_terminal_in_popup_color_wincolor()
406 call Terminal_in_popup_color("MyWinCol", [
407 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
408 \ ], "", [
409 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
410 \ ], "")
411endfunc
412
413func Test_terminal_in_popup_color_popup_highlight()
414 call Terminal_in_popup_color("MyPopupHlCol", [
415 \ "highlight MyPopupHlCol ctermfg=cyan ctermbg=green",
416 \ ], "", [], "highlight: 'MyPopupHlCol'")
417endfunc
418
419func Test_terminal_in_popup_color_group_over_Terminal()
420 call Terminal_in_popup_color("MyTermCol_over_Terminal", [
421 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
422 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
423 \ ], "term_highlight: 'MyTermCol',", [], "")
424endfunc
425
426func Test_terminal_in_popup_color_wincolor_over_group()
427 call Terminal_in_popup_color("MyWinCol_over_group", [
428 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
429 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
430 \ ], "term_highlight: 'MyTermCol',", [
431 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
432 \ ], "")
433endfunc
434
435func Test_terminal_in_popup_color_transp_Terminal()
436 call Terminal_in_popup_color("transp_Terminal", [
437 \ "highlight Terminal ctermfg=blue",
438 \ ], "", [], "")
439endfunc
440
441func Test_terminal_in_popup_color_transp_group()
442 call Terminal_in_popup_color("transp_MyTermCol", [
443 \ "highlight MyTermCol ctermfg=darkgreen",
444 \ ], "term_highlight: 'MyTermCol',", [], "")
445endfunc
446
447func Test_terminal_in_popup_color_transp_wincolor()
448 call Terminal_in_popup_color("transp_MyWinCol", [
449 \ "highlight MyWinCol ctermfg=red",
450 \ ], "", [
451 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
452 \ ], "")
453endfunc
454
455func Test_terminal_in_popup_color_transp_popup_highlight()
456 call Terminal_in_popup_color("transp_MyPopupHlCol", [
457 \ "highlight MyPopupHlCol ctermfg=cyan",
458 \ ], "", [], "highlight: 'MyPopupHlCol'")
459endfunc
460
461func Test_terminal_in_popup_color_gui_Terminal()
462 CheckFeature termguicolors
463 call Terminal_in_popup_color("gui_Terminal", [
464 \ "set termguicolors",
465 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
466 \ ], "", [], "")
467endfunc
468
469func Test_terminal_in_popup_color_gui_group()
470 CheckFeature termguicolors
471 call Terminal_in_popup_color("gui_MyTermCol", [
472 \ "set termguicolors",
473 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
474 \ ], "term_highlight: 'MyTermCol',", [], "")
475endfunc
476
477func Test_terminal_in_popup_color_gui_wincolor()
478 CheckFeature termguicolors
479 call Terminal_in_popup_color("gui_MyWinCol", [
480 \ "set termguicolors",
481 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
482 \ ], "", [
483 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
484 \ ], "")
485endfunc
486
487func Test_terminal_in_popup_color_gui_popup_highlight()
488 CheckFeature termguicolors
489 call Terminal_in_popup_color("gui_MyPopupHlCol", [
490 \ "set termguicolors",
491 \ "highlight MyPopupHlCol guifg=#00e8f0 guibg=#126521",
492 \ ], "", [], "highlight: 'MyPopupHlCol'")
493endfunc
494
495func Test_terminal_in_popup_color_gui_transp_Terminal()
496 CheckFeature termguicolors
497 call Terminal_in_popup_color("gui_transp_Terminal", [
498 \ "set termguicolors",
499 \ "highlight Terminal guifg=#3344ff",
500 \ ], "", [], "")
501endfunc
502
503func Test_terminal_in_popup_color_gui_transp_group()
504 CheckFeature termguicolors
505 call Terminal_in_popup_color("gui_transp_MyTermCol", [
506 \ "set termguicolors",
507 \ "highlight MyTermCol guifg=#007800",
508 \ ], "term_highlight: 'MyTermCol',", [], "")
509endfunc
510
511func Test_terminal_in_popup_color_gui_transp_wincolor()
512 CheckFeature termguicolors
513 call Terminal_in_popup_color("gui_transp_MyWinCol", [
514 \ "set termguicolors",
515 \ "highlight MyWinCol guifg=#fe1122",
516 \ ], "", [
517 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
518 \ ], "")
519endfunc
520
521func Test_terminal_in_popup_color_gui_transp_popup_highlight()
522 CheckFeature termguicolors
523 call Terminal_in_popup_color("gui_transp_MyPopupHlCol", [
524 \ "set termguicolors",
525 \ "highlight MyPopupHlCol guifg=#00e8f0",
526 \ ], "", [], "highlight: 'MyPopupHlCol'")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200527endfunc
528
529func Test_double_popup_terminal()
530 let buf1 = term_start(&shell, #{hidden: 1})
531 let win1 = popup_create(buf1, {})
532 let buf2 = term_start(&shell, #{hidden: 1})
533 call assert_fails('call popup_create(buf2, {})', 'E861:')
534 call popup_close(win1)
535 exe buf1 .. 'bwipe!'
536 exe buf2 .. 'bwipe!'
537endfunc
538
LemonBoy4a392d22022-04-23 14:07:56 +0100539func Test_escape_popup_terminal()
540 set hidden
541
542 " Cannot escape a terminal popup window using win_gotoid
543 let prev_win = win_getid()
544 eval term_start('sh', #{hidden: 1, term_finish: 'close'})->popup_create({})
545 call assert_fails("call win_gotoid(" .. prev_win .. ")", 'E863:')
546
547 call popup_clear(1)
548 set hidden&
549endfunc
550
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200551func Test_issue_5607()
552 let wincount = winnr('$')
553 exe 'terminal' &shell &shellcmdflag 'exit'
554 let job = term_getjob(bufnr())
555 call WaitForAssert({-> assert_equal("dead", job_status(job))})
556
557 let old_wincolor = &wincolor
558 try
559 set wincolor=
560 finally
561 let &wincolor = old_wincolor
562 bw!
563 endtry
564endfunc
565
566func Test_hidden_terminal()
567 let buf = term_start(&shell, #{hidden: 1})
568 call assert_equal('', bufname('^$'))
569 call StopShellInTerminal(buf)
570endfunc
571
572func Test_term_nasty_callback()
573 CheckExecutable sh
574
575 set hidden
576 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
577 call popup_create(g:buf0, {})
578 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
579
580 call popup_clear(1)
581 set hidden&
582endfunc
583
584func Test_term_and_startinsert()
585 CheckRunVimInTerminal
586 CheckUnix
587
588 let lines =<< trim EOL
589 put='some text'
590 term
591 startinsert
592 EOL
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100593 call writefile(lines, 'XTest_startinsert', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200594 let buf = RunVimInTerminal('-S XTest_startinsert', {})
595
596 call term_sendkeys(buf, "exit\r")
597 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
598 call term_sendkeys(buf, "0l")
599 call term_sendkeys(buf, "A<\<Esc>")
600 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
601
602 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200603endfunc
604
605" Test for passing invalid arguments to terminal functions
606func Test_term_func_invalid_arg()
607 call assert_fails('let b = term_getaltscreen([])', 'E745:')
608 call assert_fails('let a = term_getattr(1, [])', 'E730:')
609 call assert_fails('let c = term_getcursor([])', 'E745:')
610 call assert_fails('let l = term_getline([], 1)', 'E745:')
611 call assert_fails('let l = term_getscrolled([])', 'E745:')
612 call assert_fails('let s = term_getsize([])', 'E745:')
613 call assert_fails('let s = term_getstatus([])', 'E745:')
614 call assert_fails('let s = term_scrape([], 1)', 'E745:')
615 call assert_fails('call term_sendkeys([], "a")', 'E745:')
616 call assert_fails('call term_setapi([], "")', 'E745:')
617 call assert_fails('call term_setrestore([], "")', 'E745:')
618 call assert_fails('call term_setkill([], "")', 'E745:')
619 if has('gui') || has('termguicolors')
620 call assert_fails('let p = term_getansicolors([])', 'E745:')
621 call assert_fails('call term_setansicolors([], [])', 'E745:')
622 endif
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100623 let buf = term_start('echo')
624 call assert_fails('call term_setapi(' .. buf .. ', {})', 'E731:')
625 call assert_fails('call term_setkill(' .. buf .. ', {})', 'E731:')
626 call assert_fails('call term_setrestore(' .. buf .. ', {})', 'E731:')
627 exe buf . "bwipe!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200628endfunc
629
630" Test for sending various special keycodes to a terminal
631func Test_term_keycode_translation()
632 CheckRunVimInTerminal
633
634 let buf = RunVimInTerminal('', {})
635 call term_sendkeys(buf, ":set nocompatible\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100636 call term_sendkeys(buf, ":set timeoutlen=20\<CR>")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200637
638 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
639 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
640 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
641 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
642 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
643 \ "\<S-Down>"]
644 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
645 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
646 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
647 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
648 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
649
650 call term_sendkeys(buf, "i")
651 for i in range(len(keys))
652 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100653 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))}, 200)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200654 endfor
655
656 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
657 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
658 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
659 let keypad_output = ['0', '1', '2', '3', '4', '5',
660 \ '6', '7', '8', '9', '.', '+',
661 \ '-', '*', '/']
662 for i in range(len(keypad_keys))
663 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
664 if keypad_output[i] == '3' || keypad_output[i] == '9'
665 continue
666 endif
667 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100668 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))}, 100)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200669 endfor
670
671 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
672 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
673
674 call StopVimInTerminal(buf)
675endfunc
676
677" Test for using the mouse in a terminal
678func Test_term_mouse()
679 CheckNotGui
680 CheckRunVimInTerminal
681
682 let save_mouse = &mouse
683 let save_term = &term
684 let save_ttymouse = &ttymouse
685 let save_clipboard = &clipboard
686 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
687
688 let lines =<< trim END
689 one two three four five
690 red green yellow red blue
691 vim emacs sublime nano
692 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100693 call writefile(lines, 'Xtest_mouse', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200694
695 " Create a terminal window running Vim for the test with mouse enabled
696 let prev_win = win_getid()
697 let buf = RunVimInTerminal('Xtest_mouse -n', {})
698 call term_sendkeys(buf, ":set nocompatible\<CR>")
699 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
700 call term_sendkeys(buf, ":set clipboard=\<CR>")
701 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200702 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200703 redraw!
704
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000705 " Funcref used in WaitFor() to check that the "Xbuf" file is readable and
706 " has some contents. This avoids a "List index out of range" error when the
707 " file hasn't been written yet.
708 let XbufNotEmpty = {-> filereadable('Xbuf') && len(readfile('Xbuf')) > 0}
James McCoy157241e2022-11-09 23:29:14 +0000709
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200710 " Use the mouse to enter the terminal window
711 call win_gotoid(prev_win)
712 call feedkeys(MouseLeftClickCode(1, 1), 'x')
713 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
714 call assert_equal(1, getwininfo(win_getid())[0].terminal)
715
716 " Test for <LeftMouse> click/release
717 call test_setmouse(2, 5)
718 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
719 call test_setmouse(3, 8)
720 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200721 call TermWait(buf, 50)
James McCoy157241e2022-11-09 23:29:14 +0000722 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200723 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200724 call TermWait(buf, 50)
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000725 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200726 let pos = json_decode(readfile('Xbuf')[0])
727 call assert_equal([3, 8], pos[1:2])
James McCoy157241e2022-11-09 23:29:14 +0000728 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200729
730 " Test for selecting text using mouse
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200731 call test_setmouse(2, 11)
732 call term_sendkeys(buf, "\<LeftMouse>")
733 call test_setmouse(2, 16)
734 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200735 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200736 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000737 call WaitFor(XbufNotEmpty)
Bram Moolenaar1d139a02022-11-10 00:09:22 +0000738 call WaitForAssert({-> assert_equal('yellow', readfile('Xbuf')[0])})
James McCoy157241e2022-11-09 23:29:14 +0000739 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200740
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000741 " Test for selecting text using double click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200742 call test_setmouse(1, 11)
743 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
744 call test_setmouse(1, 17)
745 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200746 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200747 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000748 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200749 call assert_equal('three four', readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000750 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200751
752 " Test for selecting a line using triple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200753 call test_setmouse(3, 2)
754 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200755 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200756 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000757 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200758 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000759 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200760
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000761 " Test for selecting a block using quadruple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200762 call test_setmouse(1, 11)
763 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
764 call test_setmouse(3, 13)
765 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200766 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200767 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000768 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200769 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000770 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200771
772 " Test for extending a selection using right click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200773 call test_setmouse(2, 9)
774 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
775 call test_setmouse(2, 16)
776 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200777 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200778 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000779 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200780 call assert_equal("n yellow", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000781 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200782
783 " Test for pasting text using middle click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200784 call term_sendkeys(buf, ":let @r='bright '\<CR>")
785 call test_setmouse(2, 22)
786 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200787 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200788 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000789 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200790 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
James McCoy157241e2022-11-09 23:29:14 +0000791 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200792
793 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200794 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200795 call StopVimInTerminal(buf)
796 let &mouse = save_mouse
797 let &term = save_term
798 let &ttymouse = save_ttymouse
799 let &clipboard = save_clipboard
800 set mousetime&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200801 call delete('Xbuf')
802endfunc
803
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200804" Test for sync buffer cwd with shell's pwd
805func Test_terminal_sync_shell_dir()
806 CheckUnix
807 " The test always use sh (see src/testdir/unix.vim).
Bram Moolenaar7bfa6d62022-01-14 12:06:47 +0000808 " BSD's sh doesn't seem to play well with the OSC 7 escape sequence.
809 CheckNotBSD
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200810
811 set asd
812 " , is
813 " 1. a valid character for directory names
814 " 2. a reserved character in url-encoding
815 let chars = ",a"
816 " "," is url-encoded as '%2C'
817 let chars_url = "%2Ca"
Bram Moolenaarced2b382022-01-13 15:25:32 +0000818 let tmpfolder = fnamemodify(tempname(),':h') .. '/' .. chars
819 let tmpfolder_url = fnamemodify(tempname(),':h') .. '/' .. chars_url
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200820 call mkdir(tmpfolder, "p")
821 let buf = Run_shell_in_terminal({})
Bram Moolenaarced2b382022-01-13 15:25:32 +0000822 call term_sendkeys(buf, "echo $'\\e\]7;file://" .. tmpfolder_url .. "\\a'\<CR>")
823 "call term_sendkeys(buf, "cd " .. tmpfolder .. "\<CR>")
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200824 call TermWait(buf)
825 if has("mac")
Bram Moolenaarced2b382022-01-13 15:25:32 +0000826 let expected = "/private" .. tmpfolder
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200827 else
828 let expected = tmpfolder
829 endif
830 call assert_equal(expected, getcwd(winnr()))
Bram Moolenaar82820d92021-03-30 20:54:28 +0200831
832 set noasd
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200833endfunc
834
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200835" Test for modeless selection in a terminal
836func Test_term_modeless_selection()
837 CheckUnix
838 CheckNotGui
839 CheckRunVimInTerminal
840 CheckFeature clipboard_working
841
842 let save_mouse = &mouse
843 let save_term = &term
844 let save_ttymouse = &ttymouse
845 set mouse=a term=xterm ttymouse=sgr mousetime=200
846 set clipboard=autoselectml
847
848 let lines =<< trim END
849 one two three four five
850 red green yellow red blue
851 vim emacs sublime nano
852 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100853 call writefile(lines, 'Xtest_modeless', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200854
855 " Create a terminal window running Vim for the test with mouse disabled
856 let prev_win = win_getid()
857 let buf = RunVimInTerminal('Xtest_modeless -n', {})
858 call term_sendkeys(buf, ":set nocompatible\<CR>")
859 call term_sendkeys(buf, ":set mouse=\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200860 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200861 redraw!
862
863 " Use the mouse to enter the terminal window
864 call win_gotoid(prev_win)
865 call feedkeys(MouseLeftClickCode(1, 1), 'x')
866 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200867 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200868 call assert_equal(1, getwininfo(win_getid())[0].terminal)
869
870 " Test for copying a modeless selection to clipboard
871 let @* = 'clean'
872 " communicating with X server may take a little time
873 sleep 100m
874 call feedkeys(MouseLeftClickCode(2, 3), 'x')
875 call feedkeys(MouseLeftDragCode(2, 11), 'x')
876 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
877 call assert_equal("d green y", @*)
878
879 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200880 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200881 call StopVimInTerminal(buf)
882 let &mouse = save_mouse
883 let &term = save_term
884 let &ttymouse = save_ttymouse
885 set mousetime& clipboard&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200886 new | only!
887endfunc
888
Bram Moolenaara4b44262020-07-12 21:38:29 +0200889func Test_terminal_getwinpos()
890 CheckRunVimInTerminal
891
892 " split, go to the bottom-right window
893 split
894 wincmd j
895 set splitright
896
Bram Moolenaar42095212020-07-21 21:48:58 +0200897 let buf = RunVimInTerminal('', {'cols': 60})
898 call TermWait(buf, 100)
899 call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
Bram Moolenaara4b44262020-07-12 21:38:29 +0200900
901 " Find the output of getwinpos() in the bottom line.
902 let rows = term_getsize(buf)[0]
903 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
904 let line = term_getline(buf, rows)
905 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
906 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
907
908 " Position must be bigger than the getwinpos() result of Vim itself.
909 " The calculation in the console assumes a 10 x 7 character cell.
910 " In the GUI it can be more, let's assume a 20 x 14 cell.
911 " And then add 100 / 200 tolerance.
912 let [xroot, yroot] = getwinpos()
913 let winpos = 50->getwinpos()
914 call assert_equal(xroot, winpos[0])
915 call assert_equal(yroot, winpos[1])
Bram Moolenaar7dfc5ce2020-09-05 15:05:30 +0200916 let [winrow, wincol] = win_screenpos(0)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200917 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
918 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
919 call assert_inrange(xroot + 2, xroot + xoff, xpos)
920 call assert_inrange(yroot + 2, yroot + yoff, ypos)
921
922 call TermWait(buf)
923 call term_sendkeys(buf, ":q\<CR>")
924 call StopVimInTerminal(buf)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200925 set splitright&
926 only!
927endfunc
928
ichizokc3f91c02021-12-17 09:44:33 +0000929func Test_terminal_term_start_error()
930 func s:term_start_error() abort
931 try
932 return term_start([[]])
933 catch
934 return v:exception
935 finally
936 "
937 endtry
938 endfunc
939 autocmd WinEnter * call type(0)
940
941 " Must not crash in s:term_start_error, nor the exception thrown.
942 let result = s:term_start_error()
943 call assert_match('^Vim(return):E730:', result)
944
945 autocmd! WinEnter
946 delfunc s:term_start_error
947endfunc
948
Anton Sharonov49528da2024-04-14 20:02:24 +0200949func Test_terminal_vt420()
950 CheckRunVimInTerminal
951 " For Termcap
952 CheckUnix
Christian Brabandt83d3b3b2024-04-30 20:45:09 +0200953 CheckExecutable infocmp
954 let a = system('infocmp vt420')
955 if v:shell_error
956 " reset v:shell_error
957 let a = system('true')
958 throw 'Skipped: vt420 terminfo not available'
959 endif
960 let rows = 15
Anton Sharonov49528da2024-04-14 20:02:24 +0200961 call writefile([':set term=vt420'], 'Xterm420', 'D')
962
963 let buf = RunVimInTerminal('-S Xterm420', #{rows: rows})
964 call TermWait(buf, 100)
965 call term_sendkeys(buf, ":set t_xo?\<CR>")
966 call WaitForAssert({-> assert_match('t_xo=y', term_getline(buf, rows))})
967 call StopVimInTerminal(buf)
968
969 call writefile([''], 'Xterm420')
970 let buf = RunVimInTerminal('-S Xterm420', #{rows: rows})
971 call TermWait(buf, 100)
972 call term_sendkeys(buf, ":set t_xo?\<CR>")
973 call WaitForAssert({-> assert_match('t_xo=\s\+', term_getline(buf, rows))})
974 call StopVimInTerminal(buf)
975endfunc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200976
Yegappan Lakshmanand603e952024-06-10 18:16:34 +0200977" Test for using 'vertical' with term_start(). If a following term_start(),
978" doesn't have the 'vertical' attribute, then it should be split horizontally.
979func Test_terminal_vertical()
980 let lines =<< trim END
981 call term_start("NONE", {'vertical': 1})
982 call term_start("NONE")
983 VAR layout = winlayout()
984 call assert_equal('row', layout[0], string(layout))
985 call assert_equal('col', layout[1][0][0], string(layout))
986 :%bw!
987 END
988 call v9.CheckLegacyAndVim9Success(lines)
989endfunc
990
Christian Brabandt991657e2024-10-15 20:31:14 +0200991" Needs to come before Test_hidden_terminal(), why?
992func Test_autocmd_buffilepost_with_hidden_term()
993 CheckExecutable true
994 new XTestFile
995 defer delete('XTestFile')
996 call setline(1, ['one', 'two', 'three'])
997 call cursor(3, 10)
998 augroup TestCursor
999 au!
1000 autocmd BufFilePost * call setbufvar(3, '&tabstop', 4)
1001 augroup END
1002
1003 let buf = term_start(['true'], #{hidden: 1, term_finish: 'close'})
1004 call term_wait(buf)
1005 redraw!
1006 call assert_equal('XTestFile', bufname('%'))
1007 call assert_equal([0, 3, 5, 0], getpos('.'))
1008
1009 augroup TestCursor
1010 au!
1011 augroup END
1012 augroup! TestCursor
1013 bw! XTestFile
1014endfunc
1015
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001016" vim: shiftwidth=2 sts=2 expandtab