blob: 218b4e6a0401440a7b65dacc8158f5cbad71474f [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
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02005CheckFeature terminal
6
Christian Brabandteb380b92025-07-07 20:53:55 +02007source util/screendump.vim
8source util/mouse.vim
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02009
Christian Brabandteb380b92025-07-07 20:53:55 +020010import './util/vim9.vim' as v9
Yegappan Lakshmanand603e952024-06-10 18:16:34 +020011
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020012let $PROMPT_COMMAND=''
13
14func Test_terminal_altscreen()
15 " somehow doesn't work on MS-Windows
16 CheckUnix
17 let cmd = "cat Xtext\<CR>"
18
19 let buf = term_start(&shell, {})
Yegappan Lakshmanane446a012023-01-19 17:49:58 +000020 call TermWait(buf)
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010021 call writefile(["\<Esc>[?1047h"], 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020022 call term_sendkeys(buf, cmd)
23 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
24
25 call writefile(["\<Esc>[?1047l"], 'Xtext')
26 call term_sendkeys(buf, cmd)
27 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
28
29 call term_sendkeys(buf, "exit\r")
30 exe buf . "bwipe!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020031endfunc
32
33func Test_terminal_shell_option()
34 if has('unix')
35 " exec is a shell builtin command, should fail without a shell.
36 term exec ls runtest.vim
37 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
38 bwipe!
39
40 term ++shell exec ls runtest.vim
41 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
42 bwipe!
43 elseif has('win32')
44 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar066b12e2020-07-28 21:40:27 +020045 " However, if dir.exe (which might be provided by Cygwin/MSYS2) exists in
46 " the %PATH%, "term dir" succeeds unintentionally. Use dir.com instead.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020047 try
Bram Moolenaar066b12e2020-07-28 21:40:27 +020048 term dir.com /b runtest.vim
49 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020050 catch /CreateProcess/
51 " ignore
52 endtry
53 bwipe!
54
Bram Moolenaar066b12e2020-07-28 21:40:27 +020055 " This should execute the dir builtin command even with ".com".
56 term ++shell dir.com /b runtest.vim
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020057 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
58 bwipe!
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020059 else
60 throw 'Skipped: does not work on this platform'
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020061 endif
62endfunc
63
64func Test_terminal_invalid_arg()
65 call assert_fails('terminal ++xyz', 'E181:')
66endfunc
67
Bram Moolenaar87fd0922021-11-20 13:47:45 +000068" Check a terminal with different colors
69func Terminal_color(group_name, highlight_cmds, highlight_opt, open_cmds)
Drew Vogelea67ba72025-05-07 22:05:17 +020070 CheckScreendump
Bram Moolenaar87fd0922021-11-20 13:47:45 +000071 CheckRunVimInTerminal
72 CheckUnix
73
74 let lines = [
75 \ 'call setline(1, range(20))',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010076 \ 'func NotifyParent()',
77 \ ' call echoraw("' .. TermNotifyParentCmd(v:true) .. '")',
78 \ 'endfunc',
Bram Moolenaar87fd0922021-11-20 13:47:45 +000079 \ 'func OpenTerm()',
80 \ ' set noruler',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010081 \ " call term_start('cat', #{vertical: 1, "
82 \ .. 'exit_cb: {->NotifyParent()}, '
83 \ .. a:highlight_opt .. "})",
84 \ ' call NotifyParent()',
Bram Moolenaar87fd0922021-11-20 13:47:45 +000085 \ ] + a:open_cmds + [
86 \ 'endfunc',
Yee Cheng Chine70587d2025-02-13 20:55:45 +010087 \ ] + a:highlight_cmds + [
88 \ 'call NotifyParent()',
89 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010090 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +000091 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
Yee Cheng Chine70587d2025-02-13 20:55:45 +010092 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +000093 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +010094 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +000095 call term_sendkeys(buf, "hello\<CR>")
96 call VerifyScreenDump(buf, 'Test_terminal_color_' .. a:group_name, {})
97
98 call term_sendkeys(buf, "\<C-D>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +010099 call WaitForChildNotification()
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000100 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000101endfunc
102
103func Test_terminal_color_Terminal()
104 call Terminal_color("Terminal", [
105 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
106 \ ], "", [])
107endfunc
108
109func Test_terminal_color_group()
110 call Terminal_color("MyTermCol", [
111 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
112 \ ], "term_highlight: 'MyTermCol',", [])
113endfunc
114
115func Test_terminal_color_wincolor()
116 call Terminal_color("MyWinCol", [
117 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
118 \ ], "", [
119 \ 'set wincolor=MyWinCol',
120 \ ])
121endfunc
122
123func Test_terminal_color_group_over_Terminal()
124 call Terminal_color("MyTermCol_over_Terminal", [
125 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
126 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
127 \ ], "term_highlight: 'MyTermCol',", [])
128endfunc
129
130func Test_terminal_color_wincolor_over_group()
131 call Terminal_color("MyWinCol_over_group", [
132 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
133 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
134 \ ], "term_highlight: 'MyTermCol',", [
135 \ 'set wincolor=MyWinCol',
136 \ ])
137endfunc
138
139func Test_terminal_color_wincolor_split()
Drew Vogelea67ba72025-05-07 22:05:17 +0200140 CheckScreendump
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000141 CheckRunVimInTerminal
142 CheckUnix
143
144 let lines = [
145 \ 'call setline(1, range(20))',
146 \ 'func OpenTerm()',
147 \ ' set noruler',
148 \ " call term_start('cat', #{vertical: 1, term_highlight: 'MyTermCol'})",
149 \ 'endfunc',
150 \ 'highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue',
151 \ 'highlight MyWinCol ctermfg=red ctermbg=darkyellow',
152 \ 'highlight MyWinCol2 ctermfg=black ctermbg=blue',
153 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100154 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000155 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
156 call TermWait(buf, 100)
157 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
158 call TermWait(buf, 50)
159 call term_sendkeys(buf, "hello\<CR>")
160 call TermWait(buf, 50)
161
162 call term_sendkeys(buf, "\<C-W>:split\<CR>")
163 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol\<CR>")
164 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol', {})
165
166 call term_sendkeys(buf, "\<C-W>b:2sb\<CR>")
167 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol2\<CR>")
168 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol2', {})
169
170 call term_sendkeys(buf, "\<C-D>")
171 call TermWait(buf, 50)
172 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000173endfunc
174
175func Test_terminal_color_transp_Terminal()
176 call Terminal_color("transp_Terminal", [
177 \ "highlight Terminal ctermfg=blue",
178 \ ], "", [])
179endfunc
180
181func Test_terminal_color_transp_group()
182 call Terminal_color("transp_MyTermCol", [
183 \ "highlight MyTermCol ctermfg=darkgreen",
184 \ ], "term_highlight: 'MyTermCol',", [])
185endfunc
186
187func Test_terminal_color_transp_wincolor()
188 call Terminal_color("transp_MyWinCol", [
189 \ "highlight MyWinCol ctermfg=red",
190 \ ], "", [
191 \ 'set wincolor=MyWinCol',
192 \ ])
193endfunc
194
195func Test_terminal_color_gui_Terminal()
196 CheckFeature termguicolors
197 call Terminal_color("gui_Terminal", [
198 \ "set termguicolors",
199 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
200 \ ], "", [])
201endfunc
202
203func Test_terminal_color_gui_group()
204 CheckFeature termguicolors
205 call Terminal_color("gui_MyTermCol", [
206 \ "set termguicolors",
207 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
208 \ ], "term_highlight: 'MyTermCol',", [])
209endfunc
210
211func Test_terminal_color_gui_wincolor()
212 CheckFeature termguicolors
213 call Terminal_color("gui_MyWinCol", [
214 \ "set termguicolors",
215 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
216 \ ], "", [
217 \ 'set wincolor=MyWinCol',
218 \ ])
219endfunc
220
221func Test_terminal_color_gui_transp_Terminal()
222 CheckFeature termguicolors
223 call Terminal_color("gui_transp_Terminal", [
224 \ "set termguicolors",
225 \ "highlight Terminal guifg=#3344ff",
226 \ ], "", [])
227endfunc
228
229func Test_terminal_color_gui_transp_group()
230 CheckFeature termguicolors
231 call Terminal_color("gui_transp_MyTermCol", [
232 \ "set termguicolors",
233 \ "highlight MyTermCol guifg=#007800",
234 \ ], "term_highlight: 'MyTermCol',", [])
235endfunc
236
237func Test_terminal_color_gui_transp_wincolor()
238 CheckFeature termguicolors
239 call Terminal_color("gui_transp_MyWinCol", [
240 \ "set termguicolors",
241 \ "highlight MyWinCol guifg=#fe1122",
242 \ ], "", [
243 \ 'set wincolor=MyWinCol',
244 \ ])
245endfunc
246
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200247func Test_terminal_in_popup()
Drew Vogelea67ba72025-05-07 22:05:17 +0200248 CheckScreendump
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200249 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()
Drew Vogelea67ba72025-05-07 22:05:17 +0200327 CheckScreendump
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200328 CheckRunVimInTerminal
329
330 let text =<< trim END
331 another text
332 to show
333 in a popup window
334 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100335 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200336 let lines = [
337 \ 'call setline(1, range(20))',
338 \ 'func OpenTerm()',
339 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
340 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
341 \ 'endfunc',
342 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100343 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200344 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
345 call TermWait(buf, 100)
346 call term_sendkeys(buf, ":set noruler\<CR>")
347 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
348 call TermWait(buf, 50)
349 call term_sendkeys(buf, ":\<CR>")
350 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
351
352 call TermWait(buf, 50)
353 call term_sendkeys(buf, ":q\<CR>")
354 call TermWait(buf, 50) " wait for terminal to vanish
355 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200356endfunc
357
358" Check a terminal in popup window with different colors
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000359func Terminal_in_popup_color(group_name, highlight_cmds, highlight_opt, popup_cmds, popup_opt)
Drew Vogelea67ba72025-05-07 22:05:17 +0200360 CheckScreendump
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200361 CheckRunVimInTerminal
362 CheckUnix
363
364 let lines = [
365 \ 'call setline(1, range(20))',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100366 \ 'func NotifyParent(...)',
367 \ ' call echoraw("' .. TermNotifyParentCmd(v:true) .. '")',
368 \ 'endfunc',
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200369 \ 'func OpenTerm()',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100370 \ " let s:buf = term_start('cat', #{hidden: 1, term_finish: 'close', "
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200371 \ .. a:highlight_opt .. "})",
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100372 \ ' let g:winid = popup_create(s:buf, #{border: [], '
373 \ .. 'callback: {->NotifyParent()}, '
374 \ .. a:popup_opt .. '})',
375 \ ] + a:popup_cmds + [
376 \ ' call NotifyParent()',
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200377 \ 'endfunc',
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100378 \ ] + a:highlight_cmds + [
379 \ 'call NotifyParent()',
380 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100381 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200382 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100383 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200384 call term_sendkeys(buf, ":set noruler\<CR>")
385 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100386 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200387 call term_sendkeys(buf, "hello\<CR>")
388 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
389
390 call term_sendkeys(buf, "\<C-D>")
Yee Cheng Chine70587d2025-02-13 20:55:45 +0100391 call WaitForChildNotification()
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200392 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200393endfunc
394
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000395func Test_terminal_in_popup_color_Terminal()
396 call Terminal_in_popup_color("Terminal", [
397 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
398 \ ], "", [], "")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200399endfunc
400
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000401func Test_terminal_in_popup_color_group()
402 call Terminal_in_popup_color("MyTermCol", [
403 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
404 \ ], "term_highlight: 'MyTermCol',", [], "")
405endfunc
406
407func Test_terminal_in_popup_color_wincolor()
408 call Terminal_in_popup_color("MyWinCol", [
409 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
410 \ ], "", [
411 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
412 \ ], "")
413endfunc
414
415func Test_terminal_in_popup_color_popup_highlight()
416 call Terminal_in_popup_color("MyPopupHlCol", [
417 \ "highlight MyPopupHlCol ctermfg=cyan ctermbg=green",
418 \ ], "", [], "highlight: 'MyPopupHlCol'")
419endfunc
420
421func Test_terminal_in_popup_color_group_over_Terminal()
422 call Terminal_in_popup_color("MyTermCol_over_Terminal", [
423 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
424 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
425 \ ], "term_highlight: 'MyTermCol',", [], "")
426endfunc
427
428func Test_terminal_in_popup_color_wincolor_over_group()
429 call Terminal_in_popup_color("MyWinCol_over_group", [
430 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
431 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
432 \ ], "term_highlight: 'MyTermCol',", [
433 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
434 \ ], "")
435endfunc
436
437func Test_terminal_in_popup_color_transp_Terminal()
438 call Terminal_in_popup_color("transp_Terminal", [
439 \ "highlight Terminal ctermfg=blue",
440 \ ], "", [], "")
441endfunc
442
443func Test_terminal_in_popup_color_transp_group()
444 call Terminal_in_popup_color("transp_MyTermCol", [
445 \ "highlight MyTermCol ctermfg=darkgreen",
446 \ ], "term_highlight: 'MyTermCol',", [], "")
447endfunc
448
449func Test_terminal_in_popup_color_transp_wincolor()
450 call Terminal_in_popup_color("transp_MyWinCol", [
451 \ "highlight MyWinCol ctermfg=red",
452 \ ], "", [
453 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
454 \ ], "")
455endfunc
456
457func Test_terminal_in_popup_color_transp_popup_highlight()
458 call Terminal_in_popup_color("transp_MyPopupHlCol", [
459 \ "highlight MyPopupHlCol ctermfg=cyan",
460 \ ], "", [], "highlight: 'MyPopupHlCol'")
461endfunc
462
463func Test_terminal_in_popup_color_gui_Terminal()
464 CheckFeature termguicolors
465 call Terminal_in_popup_color("gui_Terminal", [
466 \ "set termguicolors",
467 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
468 \ ], "", [], "")
469endfunc
470
471func Test_terminal_in_popup_color_gui_group()
472 CheckFeature termguicolors
473 call Terminal_in_popup_color("gui_MyTermCol", [
474 \ "set termguicolors",
475 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
476 \ ], "term_highlight: 'MyTermCol',", [], "")
477endfunc
478
479func Test_terminal_in_popup_color_gui_wincolor()
480 CheckFeature termguicolors
481 call Terminal_in_popup_color("gui_MyWinCol", [
482 \ "set termguicolors",
483 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
484 \ ], "", [
485 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
486 \ ], "")
487endfunc
488
489func Test_terminal_in_popup_color_gui_popup_highlight()
490 CheckFeature termguicolors
491 call Terminal_in_popup_color("gui_MyPopupHlCol", [
492 \ "set termguicolors",
493 \ "highlight MyPopupHlCol guifg=#00e8f0 guibg=#126521",
494 \ ], "", [], "highlight: 'MyPopupHlCol'")
495endfunc
496
497func Test_terminal_in_popup_color_gui_transp_Terminal()
498 CheckFeature termguicolors
499 call Terminal_in_popup_color("gui_transp_Terminal", [
500 \ "set termguicolors",
501 \ "highlight Terminal guifg=#3344ff",
502 \ ], "", [], "")
503endfunc
504
505func Test_terminal_in_popup_color_gui_transp_group()
506 CheckFeature termguicolors
507 call Terminal_in_popup_color("gui_transp_MyTermCol", [
508 \ "set termguicolors",
509 \ "highlight MyTermCol guifg=#007800",
510 \ ], "term_highlight: 'MyTermCol',", [], "")
511endfunc
512
513func Test_terminal_in_popup_color_gui_transp_wincolor()
514 CheckFeature termguicolors
515 call Terminal_in_popup_color("gui_transp_MyWinCol", [
516 \ "set termguicolors",
517 \ "highlight MyWinCol guifg=#fe1122",
518 \ ], "", [
519 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
520 \ ], "")
521endfunc
522
523func Test_terminal_in_popup_color_gui_transp_popup_highlight()
524 CheckFeature termguicolors
525 call Terminal_in_popup_color("gui_transp_MyPopupHlCol", [
526 \ "set termguicolors",
527 \ "highlight MyPopupHlCol guifg=#00e8f0",
528 \ ], "", [], "highlight: 'MyPopupHlCol'")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200529endfunc
530
531func Test_double_popup_terminal()
532 let buf1 = term_start(&shell, #{hidden: 1})
533 let win1 = popup_create(buf1, {})
534 let buf2 = term_start(&shell, #{hidden: 1})
535 call assert_fails('call popup_create(buf2, {})', 'E861:')
536 call popup_close(win1)
537 exe buf1 .. 'bwipe!'
538 exe buf2 .. 'bwipe!'
539endfunc
540
LemonBoy4a392d22022-04-23 14:07:56 +0100541func Test_escape_popup_terminal()
542 set hidden
543
544 " Cannot escape a terminal popup window using win_gotoid
545 let prev_win = win_getid()
546 eval term_start('sh', #{hidden: 1, term_finish: 'close'})->popup_create({})
547 call assert_fails("call win_gotoid(" .. prev_win .. ")", 'E863:')
548
549 call popup_clear(1)
550 set hidden&
551endfunc
552
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200553func Test_issue_5607()
554 let wincount = winnr('$')
555 exe 'terminal' &shell &shellcmdflag 'exit'
556 let job = term_getjob(bufnr())
557 call WaitForAssert({-> assert_equal("dead", job_status(job))})
558
559 let old_wincolor = &wincolor
560 try
561 set wincolor=
562 finally
563 let &wincolor = old_wincolor
564 bw!
565 endtry
566endfunc
567
568func Test_hidden_terminal()
569 let buf = term_start(&shell, #{hidden: 1})
570 call assert_equal('', bufname('^$'))
571 call StopShellInTerminal(buf)
572endfunc
573
574func Test_term_nasty_callback()
575 CheckExecutable sh
576
577 set hidden
578 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
579 call popup_create(g:buf0, {})
580 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
581
582 call popup_clear(1)
583 set hidden&
584endfunc
585
586func Test_term_and_startinsert()
587 CheckRunVimInTerminal
588 CheckUnix
589
590 let lines =<< trim EOL
591 put='some text'
592 term
593 startinsert
594 EOL
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100595 call writefile(lines, 'XTest_startinsert', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200596 let buf = RunVimInTerminal('-S XTest_startinsert', {})
597
598 call term_sendkeys(buf, "exit\r")
599 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
600 call term_sendkeys(buf, "0l")
601 call term_sendkeys(buf, "A<\<Esc>")
602 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
603
604 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200605endfunc
606
607" Test for passing invalid arguments to terminal functions
608func Test_term_func_invalid_arg()
609 call assert_fails('let b = term_getaltscreen([])', 'E745:')
610 call assert_fails('let a = term_getattr(1, [])', 'E730:')
611 call assert_fails('let c = term_getcursor([])', 'E745:')
612 call assert_fails('let l = term_getline([], 1)', 'E745:')
613 call assert_fails('let l = term_getscrolled([])', 'E745:')
614 call assert_fails('let s = term_getsize([])', 'E745:')
615 call assert_fails('let s = term_getstatus([])', 'E745:')
616 call assert_fails('let s = term_scrape([], 1)', 'E745:')
617 call assert_fails('call term_sendkeys([], "a")', 'E745:')
618 call assert_fails('call term_setapi([], "")', 'E745:')
619 call assert_fails('call term_setrestore([], "")', 'E745:')
620 call assert_fails('call term_setkill([], "")', 'E745:')
621 if has('gui') || has('termguicolors')
622 call assert_fails('let p = term_getansicolors([])', 'E745:')
623 call assert_fails('call term_setansicolors([], [])', 'E745:')
624 endif
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100625 let buf = term_start('echo')
626 call assert_fails('call term_setapi(' .. buf .. ', {})', 'E731:')
627 call assert_fails('call term_setkill(' .. buf .. ', {})', 'E731:')
628 call assert_fails('call term_setrestore(' .. buf .. ', {})', 'E731:')
629 exe buf . "bwipe!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200630endfunc
631
632" Test for sending various special keycodes to a terminal
633func Test_term_keycode_translation()
634 CheckRunVimInTerminal
635
636 let buf = RunVimInTerminal('', {})
637 call term_sendkeys(buf, ":set nocompatible\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100638 call term_sendkeys(buf, ":set timeoutlen=20\<CR>")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200639
640 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
641 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
642 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
643 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
644 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
645 \ "\<S-Down>"]
646 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
647 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
648 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
649 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
650 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
651
652 call term_sendkeys(buf, "i")
653 for i in range(len(keys))
654 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100655 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))}, 200)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200656 endfor
657
658 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
659 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
660 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
661 let keypad_output = ['0', '1', '2', '3', '4', '5',
662 \ '6', '7', '8', '9', '.', '+',
663 \ '-', '*', '/']
664 for i in range(len(keypad_keys))
665 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
666 if keypad_output[i] == '3' || keypad_output[i] == '9'
667 continue
668 endif
669 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100670 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))}, 100)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200671 endfor
672
673 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
674 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
675
676 call StopVimInTerminal(buf)
677endfunc
678
679" Test for using the mouse in a terminal
680func Test_term_mouse()
681 CheckNotGui
682 CheckRunVimInTerminal
683
684 let save_mouse = &mouse
685 let save_term = &term
686 let save_ttymouse = &ttymouse
687 let save_clipboard = &clipboard
688 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
689
690 let lines =<< trim END
691 one two three four five
692 red green yellow red blue
693 vim emacs sublime nano
694 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100695 call writefile(lines, 'Xtest_mouse', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200696
697 " Create a terminal window running Vim for the test with mouse enabled
698 let prev_win = win_getid()
699 let buf = RunVimInTerminal('Xtest_mouse -n', {})
700 call term_sendkeys(buf, ":set nocompatible\<CR>")
701 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
702 call term_sendkeys(buf, ":set clipboard=\<CR>")
703 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200704 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200705 redraw!
706
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000707 " Funcref used in WaitFor() to check that the "Xbuf" file is readable and
708 " has some contents. This avoids a "List index out of range" error when the
709 " file hasn't been written yet.
710 let XbufNotEmpty = {-> filereadable('Xbuf') && len(readfile('Xbuf')) > 0}
James McCoy157241e2022-11-09 23:29:14 +0000711
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200712 " Use the mouse to enter the terminal window
713 call win_gotoid(prev_win)
714 call feedkeys(MouseLeftClickCode(1, 1), 'x')
715 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
716 call assert_equal(1, getwininfo(win_getid())[0].terminal)
717
718 " Test for <LeftMouse> click/release
719 call test_setmouse(2, 5)
720 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
721 call test_setmouse(3, 8)
722 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200723 call TermWait(buf, 50)
James McCoy157241e2022-11-09 23:29:14 +0000724 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200725 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200726 call TermWait(buf, 50)
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000727 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200728 let pos = json_decode(readfile('Xbuf')[0])
729 call assert_equal([3, 8], pos[1:2])
James McCoy157241e2022-11-09 23:29:14 +0000730 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200731
732 " Test for selecting text using mouse
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200733 call test_setmouse(2, 11)
734 call term_sendkeys(buf, "\<LeftMouse>")
735 call test_setmouse(2, 16)
736 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200737 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200738 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000739 call WaitFor(XbufNotEmpty)
Bram Moolenaar1d139a02022-11-10 00:09:22 +0000740 call WaitForAssert({-> assert_equal('yellow', readfile('Xbuf')[0])})
James McCoy157241e2022-11-09 23:29:14 +0000741 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200742
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000743 " Test for selecting text using double click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200744 call test_setmouse(1, 11)
745 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
746 call test_setmouse(1, 17)
747 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200748 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200749 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000750 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200751 call assert_equal('three four', readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000752 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200753
754 " Test for selecting a line using triple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200755 call test_setmouse(3, 2)
756 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200757 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200758 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000759 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200760 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000761 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200762
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000763 " Test for selecting a block using quadruple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200764 call test_setmouse(1, 11)
765 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
766 call test_setmouse(3, 13)
767 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200768 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200769 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000770 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200771 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000772 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200773
774 " Test for extending a selection using right click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200775 call test_setmouse(2, 9)
776 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
777 call test_setmouse(2, 16)
778 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200779 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200780 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000781 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200782 call assert_equal("n yellow", readfile('Xbuf')[0])
James McCoy157241e2022-11-09 23:29:14 +0000783 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200784
785 " Test for pasting text using middle click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200786 call term_sendkeys(buf, ":let @r='bright '\<CR>")
787 call test_setmouse(2, 22)
788 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200789 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200790 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
Bram Moolenaar98aebcc2022-11-10 12:38:16 +0000791 call WaitFor(XbufNotEmpty)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200792 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
James McCoy157241e2022-11-09 23:29:14 +0000793 call delete('Xbuf')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200794
795 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200796 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200797 call StopVimInTerminal(buf)
798 let &mouse = save_mouse
799 let &term = save_term
800 let &ttymouse = save_ttymouse
801 let &clipboard = save_clipboard
802 set mousetime&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200803 call delete('Xbuf')
804endfunc
805
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200806" Test for sync buffer cwd with shell's pwd
807func Test_terminal_sync_shell_dir()
808 CheckUnix
Christian Brabandteb380b92025-07-07 20:53:55 +0200809 " The test always use sh (see src/testdir/util/unix.vim).
Bram Moolenaar7bfa6d62022-01-14 12:06:47 +0000810 " BSD's sh doesn't seem to play well with the OSC 7 escape sequence.
811 CheckNotBSD
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200812
813 set asd
814 " , is
815 " 1. a valid character for directory names
816 " 2. a reserved character in url-encoding
817 let chars = ",a"
818 " "," is url-encoded as '%2C'
819 let chars_url = "%2Ca"
Bram Moolenaarced2b382022-01-13 15:25:32 +0000820 let tmpfolder = fnamemodify(tempname(),':h') .. '/' .. chars
821 let tmpfolder_url = fnamemodify(tempname(),':h') .. '/' .. chars_url
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200822 call mkdir(tmpfolder, "p")
823 let buf = Run_shell_in_terminal({})
Bram Moolenaarced2b382022-01-13 15:25:32 +0000824 call term_sendkeys(buf, "echo $'\\e\]7;file://" .. tmpfolder_url .. "\\a'\<CR>")
825 "call term_sendkeys(buf, "cd " .. tmpfolder .. "\<CR>")
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200826 call TermWait(buf)
827 if has("mac")
Bram Moolenaarced2b382022-01-13 15:25:32 +0000828 let expected = "/private" .. tmpfolder
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200829 else
830 let expected = tmpfolder
831 endif
832 call assert_equal(expected, getcwd(winnr()))
Bram Moolenaar82820d92021-03-30 20:54:28 +0200833
834 set noasd
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200835endfunc
836
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200837" Test for modeless selection in a terminal
838func Test_term_modeless_selection()
839 CheckUnix
840 CheckNotGui
841 CheckRunVimInTerminal
842 CheckFeature clipboard_working
843
844 let save_mouse = &mouse
845 let save_term = &term
846 let save_ttymouse = &ttymouse
847 set mouse=a term=xterm ttymouse=sgr mousetime=200
848 set clipboard=autoselectml
849
850 let lines =<< trim END
851 one two three four five
852 red green yellow red blue
853 vim emacs sublime nano
854 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100855 call writefile(lines, 'Xtest_modeless', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200856
857 " Create a terminal window running Vim for the test with mouse disabled
858 let prev_win = win_getid()
859 let buf = RunVimInTerminal('Xtest_modeless -n', {})
860 call term_sendkeys(buf, ":set nocompatible\<CR>")
861 call term_sendkeys(buf, ":set mouse=\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200862 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200863 redraw!
864
865 " Use the mouse to enter the terminal window
866 call win_gotoid(prev_win)
867 call feedkeys(MouseLeftClickCode(1, 1), 'x')
868 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200869 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200870 call assert_equal(1, getwininfo(win_getid())[0].terminal)
871
872 " Test for copying a modeless selection to clipboard
873 let @* = 'clean'
874 " communicating with X server may take a little time
875 sleep 100m
876 call feedkeys(MouseLeftClickCode(2, 3), 'x')
877 call feedkeys(MouseLeftDragCode(2, 11), 'x')
878 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
879 call assert_equal("d green y", @*)
880
881 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200882 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200883 call StopVimInTerminal(buf)
884 let &mouse = save_mouse
885 let &term = save_term
886 let &ttymouse = save_ttymouse
887 set mousetime& clipboard&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200888 new | only!
889endfunc
890
Bram Moolenaara4b44262020-07-12 21:38:29 +0200891func Test_terminal_getwinpos()
892 CheckRunVimInTerminal
893
894 " split, go to the bottom-right window
895 split
896 wincmd j
897 set splitright
898
Bram Moolenaar42095212020-07-21 21:48:58 +0200899 let buf = RunVimInTerminal('', {'cols': 60})
900 call TermWait(buf, 100)
901 call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
Bram Moolenaara4b44262020-07-12 21:38:29 +0200902
903 " Find the output of getwinpos() in the bottom line.
904 let rows = term_getsize(buf)[0]
905 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
906 let line = term_getline(buf, rows)
907 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
908 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
909
910 " Position must be bigger than the getwinpos() result of Vim itself.
911 " The calculation in the console assumes a 10 x 7 character cell.
912 " In the GUI it can be more, let's assume a 20 x 14 cell.
913 " And then add 100 / 200 tolerance.
914 let [xroot, yroot] = getwinpos()
915 let winpos = 50->getwinpos()
916 call assert_equal(xroot, winpos[0])
917 call assert_equal(yroot, winpos[1])
Bram Moolenaar7dfc5ce2020-09-05 15:05:30 +0200918 let [winrow, wincol] = win_screenpos(0)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200919 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
920 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
921 call assert_inrange(xroot + 2, xroot + xoff, xpos)
922 call assert_inrange(yroot + 2, yroot + yoff, ypos)
923
924 call TermWait(buf)
925 call term_sendkeys(buf, ":q\<CR>")
926 call StopVimInTerminal(buf)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200927 set splitright&
928 only!
929endfunc
930
ichizokc3f91c02021-12-17 09:44:33 +0000931func Test_terminal_term_start_error()
932 func s:term_start_error() abort
933 try
934 return term_start([[]])
935 catch
936 return v:exception
937 finally
938 "
939 endtry
940 endfunc
941 autocmd WinEnter * call type(0)
942
943 " Must not crash in s:term_start_error, nor the exception thrown.
944 let result = s:term_start_error()
945 call assert_match('^Vim(return):E730:', result)
946
947 autocmd! WinEnter
948 delfunc s:term_start_error
949endfunc
950
Anton Sharonov49528da2024-04-14 20:02:24 +0200951func Test_terminal_vt420()
952 CheckRunVimInTerminal
953 " For Termcap
954 CheckUnix
Christian Brabandt83d3b3b2024-04-30 20:45:09 +0200955 CheckExecutable infocmp
956 let a = system('infocmp vt420')
957 if v:shell_error
958 " reset v:shell_error
959 let a = system('true')
960 throw 'Skipped: vt420 terminfo not available'
961 endif
962 let rows = 15
Anton Sharonov49528da2024-04-14 20:02:24 +0200963 call writefile([':set term=vt420'], 'Xterm420', 'D')
964
965 let buf = RunVimInTerminal('-S Xterm420', #{rows: rows})
966 call TermWait(buf, 100)
967 call term_sendkeys(buf, ":set t_xo?\<CR>")
968 call WaitForAssert({-> assert_match('t_xo=y', term_getline(buf, rows))})
969 call StopVimInTerminal(buf)
970
971 call writefile([''], 'Xterm420')
972 let buf = RunVimInTerminal('-S Xterm420', #{rows: rows})
973 call TermWait(buf, 100)
974 call term_sendkeys(buf, ":set t_xo?\<CR>")
975 call WaitForAssert({-> assert_match('t_xo=\s\+', term_getline(buf, rows))})
976 call StopVimInTerminal(buf)
977endfunc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200978
Yegappan Lakshmanand603e952024-06-10 18:16:34 +0200979" Test for using 'vertical' with term_start(). If a following term_start(),
980" doesn't have the 'vertical' attribute, then it should be split horizontally.
981func Test_terminal_vertical()
982 let lines =<< trim END
983 call term_start("NONE", {'vertical': 1})
984 call term_start("NONE")
985 VAR layout = winlayout()
986 call assert_equal('row', layout[0], string(layout))
987 call assert_equal('col', layout[1][0][0], string(layout))
988 :%bw!
989 END
990 call v9.CheckLegacyAndVim9Success(lines)
991endfunc
992
Christian Brabandt991657e2024-10-15 20:31:14 +0200993" Needs to come before Test_hidden_terminal(), why?
994func Test_autocmd_buffilepost_with_hidden_term()
995 CheckExecutable true
996 new XTestFile
997 defer delete('XTestFile')
998 call setline(1, ['one', 'two', 'three'])
999 call cursor(3, 10)
1000 augroup TestCursor
1001 au!
1002 autocmd BufFilePost * call setbufvar(3, '&tabstop', 4)
1003 augroup END
1004
1005 let buf = term_start(['true'], #{hidden: 1, term_finish: 'close'})
1006 call term_wait(buf)
1007 redraw!
1008 call assert_equal('XTestFile', bufname('%'))
1009 call assert_equal([0, 3, 5, 0], getpos('.'))
1010
1011 augroup TestCursor
1012 au!
1013 augroup END
1014 augroup! TestCursor
1015 bw! XTestFile
1016endfunc
1017
Christian Brabandta899b272025-06-28 18:40:15 +02001018func Test_terminal_visual_empty_listchars()
1019 CheckScreendump
1020 CheckRunVimInTerminal
1021 CheckUnix
1022
1023 let lines = [
1024 \ 'set listchars=',
1025 \ ':term sh -c "printf ''hello\\n\\nhello''"'
1026 \ ]
1027 call writefile(lines, 'XtermStart1', 'D')
1028 let buf = RunVimInTerminal('-S XtermStart1', #{rows: 15})
1029 call term_wait(buf)
1030 call term_sendkeys(buf, "V2k")
1031 call VerifyScreenDump(buf, 'Test_terminal_empty_listchars', {})
Christian Brabandt66b72f42025-06-29 22:22:05 +02001032 call term_sendkeys(buf, "\<esc>")
1033 call term_sendkeys(buf, ":set nu\<cr>")
1034 call term_sendkeys(buf, "ggV2j")
1035 call VerifyScreenDump(buf, 'Test_terminal_empty_listchars2', {})
Christian Brabandta899b272025-06-28 18:40:15 +02001036
1037 call StopVimInTerminal(buf)
1038endfunc
1039
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001040" vim: shiftwidth=2 sts=2 expandtab