blob: bd8995793d29d79e40627419d77060b2161972e6 [file] [log] [blame]
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02001" Tests for the terminal window.
2" This is split in two, because it can take a lot of time.
3" See 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
13let s:python = PythonProg()
14let $PROMPT_COMMAND=''
15
16func Api_drop_common(options)
17 call assert_equal(1, winnr('$'))
18
19 " Use the title termcap entries to output the escape sequence.
20 call writefile([
21 \ 'set title',
22 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
23 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
24 \ 'redraw',
25 \ "set t_ts=",
26 \ ], 'Xscript')
27 let buf = RunVimInTerminal('-S Xscript', {})
28 call WaitFor({-> bufnr('Xtextfile') > 0})
29 call assert_equal('Xtextfile', expand('%:t'))
30 call assert_true(winnr('$') >= 3)
31 return buf
32endfunc
33
34func Test_terminal_api_drop_newwin()
35 CheckRunVimInTerminal
36 let buf = Api_drop_common('')
37 call assert_equal(0, &bin)
38 call assert_equal('', &fenc)
39
40 call StopVimInTerminal(buf)
41 call delete('Xscript')
42 bwipe Xtextfile
43endfunc
44
45func Test_terminal_api_drop_newwin_bin()
46 CheckRunVimInTerminal
47 let buf = Api_drop_common(',{"bin":1}')
48 call assert_equal(1, &bin)
49
50 call StopVimInTerminal(buf)
51 call delete('Xscript')
52 bwipe Xtextfile
53endfunc
54
55func Test_terminal_api_drop_newwin_binary()
56 CheckRunVimInTerminal
57 let buf = Api_drop_common(',{"binary":1}')
58 call assert_equal(1, &bin)
59
60 call StopVimInTerminal(buf)
61 call delete('Xscript')
62 bwipe Xtextfile
63endfunc
64
65func Test_terminal_api_drop_newwin_nobin()
66 CheckRunVimInTerminal
67 set binary
68 let buf = Api_drop_common(',{"nobin":1}')
69 call assert_equal(0, &bin)
70
71 call StopVimInTerminal(buf)
72 call delete('Xscript')
73 bwipe Xtextfile
74 set nobinary
75endfunc
76
77func Test_terminal_api_drop_newwin_nobinary()
78 CheckRunVimInTerminal
79 set binary
80 let buf = Api_drop_common(',{"nobinary":1}')
81 call assert_equal(0, &bin)
82
83 call StopVimInTerminal(buf)
84 call delete('Xscript')
85 bwipe Xtextfile
86 set nobinary
87endfunc
88
89func Test_terminal_api_drop_newwin_ff()
90 CheckRunVimInTerminal
91 let buf = Api_drop_common(',{"ff":"dos"}')
92 call assert_equal("dos", &ff)
93
94 call StopVimInTerminal(buf)
95 call delete('Xscript')
96 bwipe Xtextfile
97endfunc
98
99func Test_terminal_api_drop_newwin_fileformat()
100 CheckRunVimInTerminal
101 let buf = Api_drop_common(',{"fileformat":"dos"}')
102 call assert_equal("dos", &ff)
103
104 call StopVimInTerminal(buf)
105 call delete('Xscript')
106 bwipe Xtextfile
107endfunc
108
109func Test_terminal_api_drop_newwin_enc()
110 CheckRunVimInTerminal
111 let buf = Api_drop_common(',{"enc":"utf-16"}')
112 call assert_equal("utf-16", &fenc)
113
114 call StopVimInTerminal(buf)
115 call delete('Xscript')
116 bwipe Xtextfile
117endfunc
118
119func Test_terminal_api_drop_newwin_encoding()
120 CheckRunVimInTerminal
121 let buf = Api_drop_common(',{"encoding":"utf-16"}')
122 call assert_equal("utf-16", &fenc)
123
124 call StopVimInTerminal(buf)
125 call delete('Xscript')
126 bwipe Xtextfile
127endfunc
128
129func Test_terminal_api_drop_oldwin()
130 CheckRunVimInTerminal
131 let firstwinid = win_getid()
132 split Xtextfile
133 let textfile_winid = win_getid()
134 call assert_equal(2, winnr('$'))
135 call win_gotoid(firstwinid)
136
137 " Use the title termcap entries to output the escape sequence.
138 call writefile([
139 \ 'set title',
140 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
141 \ 'let &titlestring = ''["drop","Xtextfile"]''',
142 \ 'redraw',
143 \ "set t_ts=",
144 \ ], 'Xscript')
145 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
146 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
147 call assert_equal(textfile_winid, win_getid())
148
149 call StopVimInTerminal(buf)
150 call delete('Xscript')
151 bwipe Xtextfile
152endfunc
153
154func Tapi_TryThis(bufnum, arg)
155 let g:called_bufnum = a:bufnum
156 let g:called_arg = a:arg
157endfunc
158
159func WriteApiCall(funcname)
160 " Use the title termcap entries to output the escape sequence.
161 call writefile([
162 \ 'set title',
163 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
164 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
165 \ 'redraw',
166 \ "set t_ts=",
167 \ ], 'Xscript')
168endfunc
169
170func Test_terminal_api_call()
171 CheckRunVimInTerminal
172
173 unlet! g:called_bufnum
174 unlet! g:called_arg
175
176 call WriteApiCall('Tapi_TryThis')
177
178 " Default
179 let buf = RunVimInTerminal('-S Xscript', {})
180 call WaitFor({-> exists('g:called_bufnum')})
181 call assert_equal(buf, g:called_bufnum)
182 call assert_equal(['hello', 123], g:called_arg)
183 call StopVimInTerminal(buf)
184
185 unlet! g:called_bufnum
186 unlet! g:called_arg
187
188 " Enable explicitly
189 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
190 call WaitFor({-> exists('g:called_bufnum')})
191 call assert_equal(buf, g:called_bufnum)
192 call assert_equal(['hello', 123], g:called_arg)
193 call StopVimInTerminal(buf)
194
195 unlet! g:called_bufnum
196 unlet! g:called_arg
197
198 func! ApiCall_TryThis(bufnum, arg)
199 let g:called_bufnum2 = a:bufnum
200 let g:called_arg2 = a:arg
201 endfunc
202
203 call WriteApiCall('ApiCall_TryThis')
204
205 " Use prefix match
206 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
207 call WaitFor({-> exists('g:called_bufnum2')})
208 call assert_equal(buf, g:called_bufnum2)
209 call assert_equal(['hello', 123], g:called_arg2)
210 call StopVimInTerminal(buf)
211
212 call assert_fails("call term_start('ls', {'term_api' : []})", 'E475:')
213
214 unlet! g:called_bufnum2
215 unlet! g:called_arg2
216
217 call delete('Xscript')
218 delfunction! ApiCall_TryThis
219 unlet! g:called_bufnum2
220 unlet! g:called_arg2
221endfunc
222
223func Test_terminal_api_call_fails()
224 CheckRunVimInTerminal
225
226 func! TryThis(bufnum, arg)
227 let g:called_bufnum3 = a:bufnum
228 let g:called_arg3 = a:arg
229 endfunc
230
231 call WriteApiCall('TryThis')
232
233 unlet! g:called_bufnum3
234 unlet! g:called_arg3
235
236 " Not permitted
237 call ch_logfile('Xlog', 'w')
238 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
239 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
240 call assert_false(exists('g:called_bufnum3'))
241 call assert_false(exists('g:called_arg3'))
242 call StopVimInTerminal(buf)
243
244 " No match
245 call ch_logfile('Xlog', 'w')
246 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
247 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
248 call assert_false(exists('g:called_bufnum3'))
249 call assert_false(exists('g:called_arg3'))
250 call StopVimInTerminal(buf)
251
252 call delete('Xscript')
253 call ch_logfile('')
254 call delete('Xlog')
255 delfunction! TryThis
256 unlet! g:called_bufnum3
257 unlet! g:called_arg3
258endfunc
259
260let s:caught_e937 = 0
261
262func Tapi_Delete(bufnum, arg)
263 try
264 execute 'bdelete!' a:bufnum
265 catch /E937:/
266 let s:caught_e937 = 1
267 endtry
268endfunc
269
270func Test_terminal_api_call_fail_delete()
271 CheckRunVimInTerminal
272
273 call WriteApiCall('Tapi_Delete')
274 let buf = RunVimInTerminal('-S Xscript', {})
275 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
276
277 call StopVimInTerminal(buf)
278 call delete('Xscript')
279 call ch_logfile('', '')
280endfunc
281
282func Test_terminal_ansicolors_default()
283 CheckFunction term_getansicolors
284
285 let colors = [
286 \ '#000000', '#e00000',
287 \ '#00e000', '#e0e000',
288 \ '#0000e0', '#e000e0',
289 \ '#00e0e0', '#e0e0e0',
290 \ '#808080', '#ff4040',
291 \ '#40ff40', '#ffff40',
292 \ '#4040ff', '#ff40ff',
293 \ '#40ffff', '#ffffff',
294 \]
295
296 let buf = Run_shell_in_terminal({})
297 call assert_equal(colors, term_getansicolors(buf))
298 call StopShellInTerminal(buf)
299 call TermWait(buf)
300 call assert_equal([], term_getansicolors(buf))
301
302 exe buf . 'bwipe'
303endfunc
304
305let s:test_colors = [
306 \ '#616e64', '#0d0a79',
307 \ '#6d610d', '#0a7373',
308 \ '#690d0a', '#6d696e',
309 \ '#0d0a6f', '#616e0d',
310 \ '#0a6479', '#6d0d0a',
311 \ '#617373', '#0d0a69',
312 \ '#6d690d', '#0a6e6f',
313 \ '#610d0a', '#6e6479',
314 \]
315
316func Test_terminal_ansicolors_global()
317 CheckFeature termguicolors
318 CheckFunction term_getansicolors
319
320 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
321 let buf = Run_shell_in_terminal({})
322 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
323 call StopShellInTerminal(buf)
324 call TermWait(buf)
325
326 exe buf . 'bwipe'
327 unlet g:terminal_ansi_colors
328endfunc
329
330func Test_terminal_ansicolors_func()
331 CheckFeature termguicolors
332 CheckFunction term_getansicolors
333
334 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
335 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
336 call assert_equal(s:test_colors, term_getansicolors(buf))
337
338 call term_setansicolors(buf, g:terminal_ansi_colors)
339 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
340
341 let colors = [
342 \ 'ivory', 'AliceBlue',
343 \ 'grey67', 'dark goldenrod',
344 \ 'SteelBlue3', 'PaleVioletRed4',
345 \ 'MediumPurple2', 'yellow2',
346 \ 'RosyBrown3', 'OrangeRed2',
347 \ 'white smoke', 'navy blue',
348 \ 'grey47', 'gray97',
349 \ 'MistyRose2', 'DodgerBlue4',
350 \]
351 eval buf->term_setansicolors(colors)
352
353 let colors[4] = 'Invalid'
354 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
355 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
356
357 call StopShellInTerminal(buf)
358 call TermWait(buf)
359 call assert_equal(0, term_setansicolors(buf, []))
360 exe buf . 'bwipe'
361endfunc
362
363func Test_terminal_all_ansi_colors()
364 CheckRunVimInTerminal
365
366 " Use all the ANSI colors.
367 call writefile([
368 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
369 \ 'hi Tblack ctermfg=0 ctermbg=8',
370 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
371 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
372 \ 'hi Tbrown ctermfg=3 ctermbg=11',
373 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
374 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
375 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
376 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
377 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
378 \ 'hi Tred ctermfg=9 ctermbg=1',
379 \ 'hi Tgreen ctermfg=10 ctermbg=2',
380 \ 'hi Tyellow ctermfg=11 ctermbg=3',
381 \ 'hi Tblue ctermfg=12 ctermbg=4',
382 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
383 \ 'hi Tcyan ctermfg=14 ctermbg=6',
384 \ 'hi Twhite ctermfg=15 ctermbg=7',
385 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
386 \ 'hi TgreenBold ctermfg=10 cterm=bold',
387 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
388 \ '',
389 \ 'call matchadd("Tblack", "A")',
390 \ 'call matchadd("Tdarkred", "B")',
391 \ 'call matchadd("Tdarkgreen", "C")',
392 \ 'call matchadd("Tbrown", "D")',
393 \ 'call matchadd("Tdarkblue", "E")',
394 \ 'call matchadd("Tdarkmagenta", "F")',
395 \ 'call matchadd("Tdarkcyan", "G")',
396 \ 'call matchadd("Tlightgrey", "H")',
397 \ 'call matchadd("Tdarkgrey", "I")',
398 \ 'call matchadd("Tred", "J")',
399 \ 'call matchadd("Tgreen", "K")',
400 \ 'call matchadd("Tyellow", "L")',
401 \ 'call matchadd("Tblue", "M")',
402 \ 'call matchadd("Tmagenta", "N")',
403 \ 'call matchadd("Tcyan", "O")',
404 \ 'call matchadd("Twhite", "P")',
405 \ 'call matchadd("TdarkredBold", "X")',
406 \ 'call matchadd("TgreenBold", "Y")',
407 \ 'call matchadd("TmagentaBold", "Z")',
408 \ 'redraw',
409 \ ], 'Xcolorscript')
410 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
411 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
412
413 call term_sendkeys(buf, ":q\<CR>")
414 call StopVimInTerminal(buf)
415 call delete('Xcolorscript')
416endfunc
417
418func Test_terminal_termwinsize_option_fixed()
419 CheckRunVimInTerminal
420 set termwinsize=6x40
421 let text = []
422 for n in range(10)
423 call add(text, repeat(n, 50))
424 endfor
425 call writefile(text, 'Xwinsize')
426 let buf = RunVimInTerminal('Xwinsize', {})
427 let win = bufwinid(buf)
428 call assert_equal([6, 40], term_getsize(buf))
429 call assert_equal(6, winheight(win))
430 call assert_equal(40, winwidth(win))
431
432 " resizing the window doesn't resize the terminal.
433 resize 10
434 vertical resize 60
435 call assert_equal([6, 40], term_getsize(buf))
436 call assert_equal(10, winheight(win))
437 call assert_equal(60, winwidth(win))
438
439 call StopVimInTerminal(buf)
440 call delete('Xwinsize')
441
442 call assert_fails('set termwinsize=40', 'E474')
443 call assert_fails('set termwinsize=10+40', 'E474')
444 call assert_fails('set termwinsize=abc', 'E474')
445
446 set termwinsize=
447endfunc
448
449func Test_terminal_termwinsize_option_zero()
450 set termwinsize=0x0
451 let buf = Run_shell_in_terminal({})
452 let win = bufwinid(buf)
453 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
454 call StopShellInTerminal(buf)
455 call TermWait(buf)
456 exe buf . 'bwipe'
457
458 set termwinsize=7x0
459 let buf = Run_shell_in_terminal({})
460 let win = bufwinid(buf)
461 call assert_equal([7, winwidth(win)], term_getsize(buf))
462 call StopShellInTerminal(buf)
463 call TermWait(buf)
464 exe buf . 'bwipe'
465
466 set termwinsize=0x33
467 let buf = Run_shell_in_terminal({})
468 let win = bufwinid(buf)
469 call assert_equal([winheight(win), 33], term_getsize(buf))
470 call StopShellInTerminal(buf)
471 call TermWait(buf)
472 exe buf . 'bwipe'
473
474 set termwinsize=
475endfunc
476
477func Test_terminal_termwinsize_minimum()
478 set termwinsize=10*50
479 vsplit
480 let buf = Run_shell_in_terminal({})
481 let win = bufwinid(buf)
482 call assert_inrange(10, 1000, winheight(win))
483 call assert_inrange(50, 1000, winwidth(win))
484 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
485
486 resize 15
487 vertical resize 60
488 redraw
489 call assert_equal([15, 60], term_getsize(buf))
490 call assert_equal(15, winheight(win))
491 call assert_equal(60, winwidth(win))
492
493 resize 7
494 vertical resize 30
495 redraw
496 call assert_equal([10, 50], term_getsize(buf))
497 call assert_equal(7, winheight(win))
498 call assert_equal(30, winwidth(win))
499
500 call StopShellInTerminal(buf)
501 call TermWait(buf)
502 exe buf . 'bwipe'
503
504 set termwinsize=0*0
505 let buf = Run_shell_in_terminal({})
506 let win = bufwinid(buf)
507 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
508 call StopShellInTerminal(buf)
509 call TermWait(buf)
510 exe buf . 'bwipe'
511
512 set termwinsize=
513endfunc
514
515func Test_terminal_termwinkey()
516 " make three tabpages, terminal in the middle
517 0tabnew
518 tabnext
519 tabnew
520 tabprev
521 call assert_equal(1, winnr('$'))
522 call assert_equal(2, tabpagenr())
523 let thiswin = win_getid()
524
525 let buf = Run_shell_in_terminal({})
526 let termwin = bufwinid(buf)
527 set termwinkey=<C-L>
528 call feedkeys("\<C-L>w", 'tx')
529 call assert_equal(thiswin, win_getid())
530 call feedkeys("\<C-W>w", 'tx')
531 call assert_equal(termwin, win_getid())
532
533 if has('langmap')
534 set langmap=xjyk
535 call feedkeys("\<C-L>x", 'tx')
536 call assert_equal(thiswin, win_getid())
537 call feedkeys("\<C-W>y", 'tx')
538 call assert_equal(termwin, win_getid())
539 set langmap=
540 endif
541
542 call feedkeys("\<C-L>gt", "xt")
543 call assert_equal(3, tabpagenr())
544 tabprev
545 call assert_equal(2, tabpagenr())
546 call assert_equal(termwin, win_getid())
547
548 call feedkeys("\<C-L>gT", "xt")
549 call assert_equal(1, tabpagenr())
550 tabnext
551 call assert_equal(2, tabpagenr())
552 call assert_equal(termwin, win_getid())
553
554 let job = term_getjob(buf)
555 call feedkeys("\<C-L>\<C-C>", 'tx')
556 call WaitForAssert({-> assert_equal("dead", job_status(job))})
557
558 set termwinkey&
559 tabnext
560 tabclose
561 tabprev
562 tabclose
563endfunc
564
565func Test_terminal_out_err()
566 CheckUnix
567
568 call writefile([
569 \ '#!/bin/sh',
570 \ 'echo "this is standard error" >&2',
571 \ 'echo "this is standard out" >&1',
572 \ ], 'Xechoerrout.sh')
573 call setfperm('Xechoerrout.sh', 'rwxrwx---')
574
575 let outfile = 'Xtermstdout'
576 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
577
578 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
579 call assert_equal(['this is standard out'], readfile(outfile))
580 call assert_equal('this is standard error', term_getline(buf, 1))
581
582 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
583 exe buf . 'bwipe'
584 call delete('Xechoerrout.sh')
585 call delete(outfile)
586endfunc
587
588func Test_termwinscroll()
589 CheckUnix
590
591 " Let the terminal output more than 'termwinscroll' lines, some at the start
592 " will be dropped.
593 exe 'set termwinscroll=' . &lines
594 let buf = term_start('/bin/sh')
595 for i in range(1, &lines)
596 call feedkeys("echo " . i . "\<CR>", 'xt')
597 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
598 endfor
599 " Go to Terminal-Normal mode to update the buffer.
600 call feedkeys("\<C-W>N", 'xt')
601 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
602
603 " Every "echo nr" must only appear once
604 let lines = getline(1, line('$'))
605 for i in range(&lines - len(lines) / 2 + 2, &lines)
606 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
607 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
608 endfor
609
610 exe buf . 'bwipe!'
611endfunc
612
613" Resizing the terminal window caused an ml_get error.
614" TODO: This does not reproduce the original problem.
615func Test_terminal_resize()
616 set statusline=x
617 terminal
618 call assert_equal(2, winnr('$'))
619
620 " Fill the terminal with text.
621 if has('win32')
622 call feedkeys("dir\<CR>", 'xt')
623 else
624 call feedkeys("ls\<CR>", 'xt')
625 endif
626 " Go to Terminal-Normal mode for a moment.
627 call feedkeys("\<C-W>N", 'xt')
628 " Open a new window
629 call feedkeys("i\<C-W>n", 'xt')
630 call assert_equal(3, winnr('$'))
631 redraw
632
633 close
634 call assert_equal(2, winnr('$'))
635 call feedkeys("exit\<CR>", 'xt')
636 set statusline&
637endfunc
638
639" must be nearly the last, we can't go back from GUI to terminal
640func Test_zz1_terminal_in_gui()
641 CheckCanRunGui
642
643 " Ignore the "failed to create input context" error.
644 call test_ignore_error('E285:')
645
646 gui -f
647
648 call assert_equal(1, winnr('$'))
649 let buf = Run_shell_in_terminal({'term_finish': 'close'})
650 call StopShellInTerminal(buf)
651 call TermWait(buf)
652
653 " closing window wipes out the terminal buffer a with finished job
654 call WaitForAssert({-> assert_equal(1, winnr('$'))})
655 call assert_equal("", bufname(buf))
656
657 unlet g:job
658endfunc
659
660func Test_zz2_terminal_guioptions_bang()
661 CheckGui
662 set guioptions+=!
663
664 let filename = 'Xtestscript'
665 if has('win32')
666 let filename .= '.bat'
667 let prefix = ''
668 let contents = ['@echo off', 'exit %1']
669 else
670 let filename .= '.sh'
671 let prefix = './'
672 let contents = ['#!/bin/sh', 'exit $1']
673 endif
674 call writefile(contents, filename)
675 call setfperm(filename, 'rwxrwx---')
676
677 " Check if v:shell_error is equal to the exit status.
678 let exitval = 0
679 execute printf(':!%s%s %d', prefix, filename, exitval)
680 call assert_equal(exitval, v:shell_error)
681
682 let exitval = 9
683 execute printf(':!%s%s %d', prefix, filename, exitval)
684 call assert_equal(exitval, v:shell_error)
685
686 set guioptions&
687 call delete(filename)
688endfunc
689
690func Test_terminal_hidden()
691 CheckUnix
692
693 term ++hidden cat
694 let bnr = bufnr('$')
695 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
696 exe 'sbuf ' . bnr
697 call assert_equal('terminal', &buftype)
698 call term_sendkeys(bnr, "asdf\<CR>")
699 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
700 call term_sendkeys(bnr, "\<C-D>")
701 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
702 bwipe!
703endfunc
704
705func Test_terminal_switch_mode()
706 term
707 let bnr = bufnr('$')
708 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
709 call feedkeys("\<C-W>N", 'xt')
710 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
711 call feedkeys("A", 'xt')
712 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
713 call feedkeys("\<C-\>\<C-N>", 'xt')
714 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
715 call feedkeys("I", 'xt')
716 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
717 call feedkeys("\<C-W>Nv", 'xt')
718 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
719 call feedkeys("I", 'xt')
720 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
721 call feedkeys("\<C-W>Nv", 'xt')
722 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
723 call feedkeys("A", 'xt')
724 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
725 bwipe!
726endfunc
727
728func Test_terminal_normal_mode()
729 CheckRunVimInTerminal
730
731 " Run Vim in a terminal and open a terminal window to run Vim in.
732 let lines =<< trim END
733 call setline(1, range(11111, 11122))
734 3
735 END
736 call writefile(lines, 'XtermNormal')
737 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
738 call TermWait(buf)
739
740 call term_sendkeys(buf, "\<C-W>N")
741 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
742 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
743
744 call term_sendkeys(buf, ":set culopt=number\r")
745 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
746
747 call term_sendkeys(buf, ":set culopt=line\r")
748 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
749
750 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
751 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
752 call StopVimInTerminal(buf)
753 call delete('XtermNormal')
754endfunc
755
756func Test_terminal_hidden_and_close()
757 CheckUnix
758
759 call assert_equal(1, winnr('$'))
760 term ++hidden ++close ls
761 let bnr = bufnr('$')
762 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
763 call WaitForAssert({-> assert_false(bufexists(bnr))})
764 call assert_equal(1, winnr('$'))
765endfunc
766
767func Test_terminal_does_not_truncate_last_newlines()
768 " This test does not pass through ConPTY.
769 if has('conpty')
770 return
771 endif
772 let contents = [
773 \ [ 'One', '', 'X' ],
774 \ [ 'Two', '', '' ],
775 \ [ 'Three' ] + repeat([''], 30)
776 \ ]
777
778 for c in contents
779 call writefile(c, 'Xfile')
780 if has('win32')
781 term cmd /c type Xfile
782 else
783 term cat Xfile
784 endif
785 let bnr = bufnr('$')
786 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
787 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
788 sleep 100m
789 call assert_equal(c, getline(1, line('$')))
790 quit
791 endfor
792
793 call delete('Xfile')
794endfunc
795
796func Test_terminal_no_job()
797 if has('win32')
798 let cmd = 'cmd /c ""'
799 else
800 CheckExecutable false
801 let cmd = 'false'
802 endif
803 let term = term_start(cmd, {'term_finish': 'close'})
804 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
805endfunc
806
807func Test_term_getcursor()
808 CheckUnix
809
810 let buf = Run_shell_in_terminal({})
811
812 " Wait for the shell to display a prompt.
813 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
814
815 " Hide the cursor.
816 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
817 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
818
819 " Show the cursor.
820 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
821 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
822
823 " Change color of cursor.
824 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
825 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
826 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
827 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
828 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
829
830 " Make cursor a blinking block.
831 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
832 call WaitForAssert({-> assert_equal([1, 1],
833 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
834
835 " Make cursor a steady block.
836 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
837 call WaitForAssert({-> assert_equal([0, 1],
838 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
839
840 " Make cursor a blinking underline.
841 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
842 call WaitForAssert({-> assert_equal([1, 2],
843 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
844
845 " Make cursor a steady underline.
846 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
847 call WaitForAssert({-> assert_equal([0, 2],
848 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
849
850 " Make cursor a blinking vertical bar.
851 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
852 call WaitForAssert({-> assert_equal([1, 3],
853 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
854
855 " Make cursor a steady vertical bar.
856 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
857 call WaitForAssert({-> assert_equal([0, 3],
858 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
859
860 call StopShellInTerminal(buf)
861endfunc
862
863" Test for term_gettitle()
864func Test_term_gettitle()
865 " term_gettitle() returns an empty string for a non-terminal buffer
866 " and for a non-existing buffer.
867 call assert_equal('', bufnr('%')->term_gettitle())
868 call assert_equal('', term_gettitle(bufnr('$') + 1))
869
870 if !has('title') || empty(&t_ts)
871 throw "Skipped: can't get/set title"
872 endif
873
874 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
875 if has('autoservername')
876 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
877 call term_sendkeys(term, ":e Xfoo\r")
878 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
879 else
880 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
881 call term_sendkeys(term, ":e Xfoo\r")
882 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
883 endif
884
885 call term_sendkeys(term, ":set titlestring=foo\r")
886 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
887
888 exe term . 'bwipe!'
889endfunc
890
891func Test_term_gettty()
892 let buf = Run_shell_in_terminal({})
893 let gettty = term_gettty(buf)
894
895 if has('unix') && executable('tty')
896 " Find tty using the tty shell command.
897 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
898 call term_sendkeys(buf, "tty\r")
899 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
900 let tty = term_getline(buf, 2)
901 call assert_equal(tty, gettty)
902 endif
903
904 let gettty0 = term_gettty(buf, 0)
905 let gettty1 = term_gettty(buf, 1)
906
907 call assert_equal(gettty, gettty0)
908 call assert_equal(job_info(g:job).tty_out, gettty0)
909 call assert_equal(job_info(g:job).tty_in, gettty1)
910
911 if has('unix')
912 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
913 " are identical according to :help term_gettty()
914 call assert_equal(gettty0, gettty1)
915 call assert_match('^/dev/', gettty)
916 else
917 " ConPTY works on anonymous pipe.
918 if !has('conpty')
919 call assert_match('^\\\\.\\pipe\\', gettty0)
920 call assert_match('^\\\\.\\pipe\\', gettty1)
921 endif
922 endif
923
924 call assert_fails('call term_gettty(buf, 2)', 'E475:')
925 call assert_fails('call term_gettty(buf, -1)', 'E475:')
926
927 call assert_equal('', term_gettty(buf + 1))
928
929 call StopShellInTerminal(buf)
930 call TermWait(buf)
931 exe buf . 'bwipe'
932endfunc
933
934func Test_terminal_getwinpos()
935 CheckRunVimInTerminal
936
937 " split, go to the bottom-right window
938 split
939 wincmd j
940 set splitright
941
942 call writefile([
943 \ 'echo getwinpos()',
944 \ ], 'XTest_getwinpos')
945 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
946 call TermWait(buf)
947
948 " Find the output of getwinpos() in the bottom line.
949 let rows = term_getsize(buf)[0]
950 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
951 let line = term_getline(buf, rows)
952 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
953 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
954
955 " Position must be bigger than the getwinpos() result of Vim itself.
956 " The calculation in the console assumes a 10 x 7 character cell.
957 " In the GUI it can be more, let's assume a 20 x 14 cell.
958 " And then add 100 / 200 tolerance.
959 let [xroot, yroot] = getwinpos()
960 let winpos = 50->getwinpos()
961 call assert_equal(xroot, winpos[0])
962 call assert_equal(yroot, winpos[1])
963 let [winrow, wincol] = win_screenpos('.')
964 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
965 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
966 call assert_inrange(xroot + 2, xroot + xoff, xpos)
967 call assert_inrange(yroot + 2, yroot + yoff, ypos)
968
969 call TermWait(buf)
970 call term_sendkeys(buf, ":q\<CR>")
971 call StopVimInTerminal(buf)
972 call delete('XTest_getwinpos')
973 exe buf . 'bwipe!'
974 set splitright&
975 only!
976endfunc
977
978func Test_terminal_altscreen()
979 " somehow doesn't work on MS-Windows
980 CheckUnix
981 let cmd = "cat Xtext\<CR>"
982
983 let buf = term_start(&shell, {})
984 call writefile(["\<Esc>[?1047h"], 'Xtext')
985 call term_sendkeys(buf, cmd)
986 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
987
988 call writefile(["\<Esc>[?1047l"], 'Xtext')
989 call term_sendkeys(buf, cmd)
990 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
991
992 call term_sendkeys(buf, "exit\r")
993 exe buf . "bwipe!"
994 call delete('Xtext')
995endfunc
996
997func Test_terminal_shell_option()
998 if has('unix')
999 " exec is a shell builtin command, should fail without a shell.
1000 term exec ls runtest.vim
1001 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
1002 bwipe!
1003
1004 term ++shell exec ls runtest.vim
1005 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
1006 bwipe!
1007 elseif has('win32')
1008 " dir is a shell builtin command, should fail without a shell.
1009 try
1010 term dir /b runtest.vim
1011 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
1012 catch /CreateProcess/
1013 " ignore
1014 endtry
1015 bwipe!
1016
1017 term ++shell dir /b runtest.vim
1018 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
1019 bwipe!
1020 endif
1021endfunc
1022
1023func Test_terminal_setapi_and_call()
1024 CheckRunVimInTerminal
1025
1026 call WriteApiCall('Tapi_TryThis')
1027 call ch_logfile('Xlog', 'w')
1028
1029 unlet! g:called_bufnum
1030 unlet! g:called_arg
1031
1032 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1033 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1034 call assert_false(exists('g:called_bufnum'))
1035 call assert_false(exists('g:called_arg'))
1036
1037 eval buf->term_setapi('Tapi_')
1038 call term_sendkeys(buf, ":set notitle\<CR>")
1039 call term_sendkeys(buf, ":source Xscript\<CR>")
1040 call WaitFor({-> exists('g:called_bufnum')})
1041 call assert_equal(buf, g:called_bufnum)
1042 call assert_equal(['hello', 123], g:called_arg)
1043
1044 call StopVimInTerminal(buf)
1045
1046 call delete('Xscript')
1047 call ch_logfile('')
1048 call delete('Xlog')
1049 unlet! g:called_bufnum
1050 unlet! g:called_arg
1051endfunc
1052
1053func Test_terminal_api_arg()
1054 CheckRunVimInTerminal
1055
1056 call WriteApiCall('Tapi_TryThis')
1057 call ch_logfile('Xlog', 'w')
1058
1059 unlet! g:called_bufnum
1060 unlet! g:called_arg
1061
1062 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1063 let buf = bufnr('%')
1064 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1065 call assert_false(exists('g:called_bufnum'))
1066 call assert_false(exists('g:called_arg'))
1067
1068 call StopVimInTerminal(buf)
1069
1070 call ch_logfile('Xlog', 'w')
1071
1072 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1073 let buf = bufnr('%')
1074 call WaitFor({-> exists('g:called_bufnum')})
1075 call assert_equal(buf, g:called_bufnum)
1076 call assert_equal(['hello', 123], g:called_arg)
1077
1078 call StopVimInTerminal(buf)
1079
1080 call delete('Xscript')
1081 call ch_logfile('')
1082 call delete('Xlog')
1083 unlet! g:called_bufnum
1084 unlet! g:called_arg
1085endfunc
1086
1087func Test_terminal_invalid_arg()
1088 call assert_fails('terminal ++xyz', 'E181:')
1089endfunc
1090
1091func Test_terminal_in_popup()
1092 CheckRunVimInTerminal
1093
1094 let text =<< trim END
1095 some text
1096 to edit
1097 in a popup window
1098 END
1099 call writefile(text, 'Xtext')
1100 let cmd = GetVimCommandCleanTerm()
1101 let lines = [
1102 \ 'call setline(1, range(20))',
1103 \ 'hi PopTerm ctermbg=grey',
1104 \ 'func OpenTerm(setColor)',
1105 \ " set noruler",
1106 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
1107 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
1108 \ ' if a:setColor',
1109 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
1110 \ ' endif',
1111 \ 'endfunc',
1112 \ 'func HidePopup()',
1113 \ ' call popup_hide(g:winid)',
1114 \ 'endfunc',
1115 \ 'func ClosePopup()',
1116 \ ' call popup_close(g:winid)',
1117 \ 'endfunc',
1118 \ 'func ReopenPopup()',
1119 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
1120 \ 'endfunc',
1121 \ ]
1122 call writefile(lines, 'XtermPopup')
1123 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
1124 call TermWait(buf, 100)
1125 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
1126 call TermWait(buf, 100)
1127 call term_sendkeys(buf, ":\<CR>")
1128 call TermWait(buf, 100)
1129 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
1130 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
1131
1132 call term_sendkeys(buf, ":q\<CR>")
1133 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
1134
1135 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
1136 call TermWait(buf, 150)
1137 call term_sendkeys(buf, ":set hlsearch\<CR>")
1138 call TermWait(buf, 100)
1139 call term_sendkeys(buf, "/edit\<CR>")
1140 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
1141
1142 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
1143 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
1144 call term_sendkeys(buf, "\<CR>")
1145 call TermWait(buf, 50)
1146
1147 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
1148 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
1149
1150 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
1151 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
1152
1153 " Go to terminal-Normal mode and visually select text.
1154 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
1155 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
1156
1157 " Back to job mode, redraws
1158 call term_sendkeys(buf, "A")
1159 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
1160
1161 call TermWait(buf, 50)
1162 call term_sendkeys(buf, ":q\<CR>")
1163 call TermWait(buf, 150) " wait for terminal to vanish
1164
1165 call StopVimInTerminal(buf)
1166 call delete('Xtext')
1167 call delete('XtermPopup')
1168endfunc
1169
1170" Check a terminal in popup window uses the default mininum size.
1171func Test_terminal_in_popup_min_size()
1172 CheckRunVimInTerminal
1173
1174 let text =<< trim END
1175 another text
1176 to show
1177 in a popup window
1178 END
1179 call writefile(text, 'Xtext')
1180 let lines = [
1181 \ 'call setline(1, range(20))',
1182 \ 'func OpenTerm()',
1183 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
1184 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
1185 \ 'endfunc',
1186 \ ]
1187 call writefile(lines, 'XtermPopup')
1188 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
1189 call TermWait(buf, 100)
1190 call term_sendkeys(buf, ":set noruler\<CR>")
1191 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
1192 call TermWait(buf, 50)
1193 call term_sendkeys(buf, ":\<CR>")
1194 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
1195
1196 call TermWait(buf, 50)
1197 call term_sendkeys(buf, ":q\<CR>")
1198 call TermWait(buf, 50) " wait for terminal to vanish
1199 call StopVimInTerminal(buf)
1200 call delete('Xtext')
1201 call delete('XtermPopup')
1202endfunc
1203
1204" Check a terminal in popup window with different colors
1205func Terminal_in_popup_colored(group_name, highlight_cmd, highlight_opt)
1206 CheckRunVimInTerminal
1207 CheckUnix
1208
1209 let lines = [
1210 \ 'call setline(1, range(20))',
1211 \ 'func OpenTerm()',
1212 \ " let s:buf = term_start('cat', #{hidden: 1, "
1213 \ .. a:highlight_opt .. "})",
1214 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
1215 \ 'endfunc',
1216 \ a:highlight_cmd,
1217 \ ]
1218 call writefile(lines, 'XtermPopup')
1219 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
1220 call TermWait(buf, 100)
1221 call term_sendkeys(buf, ":set noruler\<CR>")
1222 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
1223 call TermWait(buf, 50)
1224 call term_sendkeys(buf, "hello\<CR>")
1225 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
1226
1227 call term_sendkeys(buf, "\<C-D>")
1228 call TermWait(buf, 50)
1229 call term_sendkeys(buf, ":q\<CR>")
1230 call TermWait(buf, 50) " wait for terminal to vanish
1231 call StopVimInTerminal(buf)
1232 call delete('XtermPopup')
1233endfunc
1234
1235func Test_terminal_in_popup_colored_Terminal()
1236 call Terminal_in_popup_colored("Terminal", "highlight Terminal ctermfg=blue ctermbg=yellow", "")
1237endfunc
1238
1239func Test_terminal_in_popup_colored_group()
1240 call Terminal_in_popup_colored("MyTermCol", "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue", "term_highlight: 'MyTermCol',")
1241endfunc
1242
1243func Test_double_popup_terminal()
1244 let buf1 = term_start(&shell, #{hidden: 1})
1245 let win1 = popup_create(buf1, {})
1246 let buf2 = term_start(&shell, #{hidden: 1})
1247 call assert_fails('call popup_create(buf2, {})', 'E861:')
1248 call popup_close(win1)
1249 exe buf1 .. 'bwipe!'
1250 exe buf2 .. 'bwipe!'
1251endfunc
1252
1253func Test_issue_5607()
1254 let wincount = winnr('$')
1255 exe 'terminal' &shell &shellcmdflag 'exit'
1256 let job = term_getjob(bufnr())
1257 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1258
1259 let old_wincolor = &wincolor
1260 try
1261 set wincolor=
1262 finally
1263 let &wincolor = old_wincolor
1264 bw!
1265 endtry
1266endfunc
1267
1268func Test_hidden_terminal()
1269 let buf = term_start(&shell, #{hidden: 1})
1270 call assert_equal('', bufname('^$'))
1271 call StopShellInTerminal(buf)
1272endfunc
1273
1274func Test_term_nasty_callback()
1275 CheckExecutable sh
1276
1277 set hidden
1278 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
1279 call popup_create(g:buf0, {})
1280 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
1281
1282 call popup_clear(1)
1283 set hidden&
1284endfunc
1285
1286func Test_term_and_startinsert()
1287 CheckRunVimInTerminal
1288 CheckUnix
1289
1290 let lines =<< trim EOL
1291 put='some text'
1292 term
1293 startinsert
1294 EOL
1295 call writefile(lines, 'XTest_startinsert')
1296 let buf = RunVimInTerminal('-S XTest_startinsert', {})
1297
1298 call term_sendkeys(buf, "exit\r")
1299 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
1300 call term_sendkeys(buf, "0l")
1301 call term_sendkeys(buf, "A<\<Esc>")
1302 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
1303
1304 call StopVimInTerminal(buf)
1305 call delete('XTest_startinsert')
1306endfunc
1307
1308" Test for passing invalid arguments to terminal functions
1309func Test_term_func_invalid_arg()
1310 call assert_fails('let b = term_getaltscreen([])', 'E745:')
1311 call assert_fails('let a = term_getattr(1, [])', 'E730:')
1312 call assert_fails('let c = term_getcursor([])', 'E745:')
1313 call assert_fails('let l = term_getline([], 1)', 'E745:')
1314 call assert_fails('let l = term_getscrolled([])', 'E745:')
1315 call assert_fails('let s = term_getsize([])', 'E745:')
1316 call assert_fails('let s = term_getstatus([])', 'E745:')
1317 call assert_fails('let s = term_scrape([], 1)', 'E745:')
1318 call assert_fails('call term_sendkeys([], "a")', 'E745:')
1319 call assert_fails('call term_setapi([], "")', 'E745:')
1320 call assert_fails('call term_setrestore([], "")', 'E745:')
1321 call assert_fails('call term_setkill([], "")', 'E745:')
1322 if has('gui') || has('termguicolors')
1323 call assert_fails('let p = term_getansicolors([])', 'E745:')
1324 call assert_fails('call term_setansicolors([], [])', 'E745:')
1325 endif
1326endfunc
1327
1328" Test for sending various special keycodes to a terminal
1329func Test_term_keycode_translation()
1330 CheckRunVimInTerminal
1331
1332 let buf = RunVimInTerminal('', {})
1333 call term_sendkeys(buf, ":set nocompatible\<CR>")
1334
1335 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
1336 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
1337 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
1338 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
1339 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
1340 \ "\<S-Down>"]
1341 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
1342 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
1343 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
1344 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
1345 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
1346
1347 call term_sendkeys(buf, "i")
1348 for i in range(len(keys))
1349 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
1350 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))})
1351 endfor
1352
1353 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
1354 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
1355 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
1356 let keypad_output = ['0', '1', '2', '3', '4', '5',
1357 \ '6', '7', '8', '9', '.', '+',
1358 \ '-', '*', '/']
1359 for i in range(len(keypad_keys))
1360 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
1361 if keypad_output[i] == '3' || keypad_output[i] == '9'
1362 continue
1363 endif
1364 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
1365 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))})
1366 endfor
1367
1368 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
1369 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
1370
1371 call StopVimInTerminal(buf)
1372endfunc
1373
1374" Test for using the mouse in a terminal
1375func Test_term_mouse()
1376 CheckNotGui
1377 CheckRunVimInTerminal
1378
1379 let save_mouse = &mouse
1380 let save_term = &term
1381 let save_ttymouse = &ttymouse
1382 let save_clipboard = &clipboard
1383 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
1384
1385 let lines =<< trim END
1386 one two three four five
1387 red green yellow red blue
1388 vim emacs sublime nano
1389 END
1390 call writefile(lines, 'Xtest_mouse')
1391
1392 " Create a terminal window running Vim for the test with mouse enabled
1393 let prev_win = win_getid()
1394 let buf = RunVimInTerminal('Xtest_mouse -n', {})
1395 call term_sendkeys(buf, ":set nocompatible\<CR>")
1396 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
1397 call term_sendkeys(buf, ":set clipboard=\<CR>")
1398 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
1399 call term_wait(buf)
1400 redraw!
1401
1402 " Use the mouse to enter the terminal window
1403 call win_gotoid(prev_win)
1404 call feedkeys(MouseLeftClickCode(1, 1), 'x')
1405 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
1406 call assert_equal(1, getwininfo(win_getid())[0].terminal)
1407
1408 " Test for <LeftMouse> click/release
1409 call test_setmouse(2, 5)
1410 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
1411 call test_setmouse(3, 8)
1412 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
1413 call term_wait(buf, 50)
1414 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
1415 call term_wait(buf, 50)
1416 let pos = json_decode(readfile('Xbuf')[0])
1417 call assert_equal([3, 8], pos[1:2])
1418
1419 " Test for selecting text using mouse
1420 call delete('Xbuf')
1421 call test_setmouse(2, 11)
1422 call term_sendkeys(buf, "\<LeftMouse>")
1423 call test_setmouse(2, 16)
1424 call term_sendkeys(buf, "\<LeftRelease>y")
1425 call term_wait(buf, 50)
1426 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
1427 call term_wait(buf, 50)
1428 call assert_equal('yellow', readfile('Xbuf')[0])
1429
1430 " Test for selecting text using doubleclick
1431 call delete('Xbuf')
1432 call test_setmouse(1, 11)
1433 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
1434 call test_setmouse(1, 17)
1435 call term_sendkeys(buf, "\<LeftRelease>y")
1436 call term_wait(buf, 50)
1437 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
1438 call term_wait(buf, 50)
1439 call assert_equal('three four', readfile('Xbuf')[0])
1440
1441 " Test for selecting a line using triple click
1442 call delete('Xbuf')
1443 call test_setmouse(3, 2)
1444 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
1445 call term_wait(buf, 50)
1446 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
1447 call term_wait(buf, 50)
1448 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
1449
1450 " Test for selecting a block using qudraple click
1451 call delete('Xbuf')
1452 call test_setmouse(1, 11)
1453 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
1454 call test_setmouse(3, 13)
1455 call term_sendkeys(buf, "\<LeftRelease>y")
1456 call term_wait(buf, 50)
1457 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
1458 call term_wait(buf, 50)
1459 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
1460
1461 " Test for extending a selection using right click
1462 call delete('Xbuf')
1463 call test_setmouse(2, 9)
1464 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
1465 call test_setmouse(2, 16)
1466 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
1467 call term_wait(buf, 50)
1468 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
1469 call term_wait(buf, 50)
1470 call assert_equal("n yellow", readfile('Xbuf')[0])
1471
1472 " Test for pasting text using middle click
1473 call delete('Xbuf')
1474 call term_sendkeys(buf, ":let @r='bright '\<CR>")
1475 call test_setmouse(2, 22)
1476 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
1477 call term_wait(buf, 50)
1478 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
1479 call term_wait(buf, 50)
1480 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
1481
1482 " cleanup
1483 call term_wait(buf)
1484 call StopVimInTerminal(buf)
1485 let &mouse = save_mouse
1486 let &term = save_term
1487 let &ttymouse = save_ttymouse
1488 let &clipboard = save_clipboard
1489 set mousetime&
1490 call delete('Xtest_mouse')
1491 call delete('Xbuf')
1492endfunc
1493
1494" Test for modeless selection in a terminal
1495func Test_term_modeless_selection()
1496 CheckUnix
1497 CheckNotGui
1498 CheckRunVimInTerminal
1499 CheckFeature clipboard_working
1500
1501 let save_mouse = &mouse
1502 let save_term = &term
1503 let save_ttymouse = &ttymouse
1504 set mouse=a term=xterm ttymouse=sgr mousetime=200
1505 set clipboard=autoselectml
1506
1507 let lines =<< trim END
1508 one two three four five
1509 red green yellow red blue
1510 vim emacs sublime nano
1511 END
1512 call writefile(lines, 'Xtest_modeless')
1513
1514 " Create a terminal window running Vim for the test with mouse disabled
1515 let prev_win = win_getid()
1516 let buf = RunVimInTerminal('Xtest_modeless -n', {})
1517 call term_sendkeys(buf, ":set nocompatible\<CR>")
1518 call term_sendkeys(buf, ":set mouse=\<CR>")
1519 call term_wait(buf)
1520 redraw!
1521
1522 " Use the mouse to enter the terminal window
1523 call win_gotoid(prev_win)
1524 call feedkeys(MouseLeftClickCode(1, 1), 'x')
1525 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
1526 call term_wait(buf)
1527 call assert_equal(1, getwininfo(win_getid())[0].terminal)
1528
1529 " Test for copying a modeless selection to clipboard
1530 let @* = 'clean'
1531 " communicating with X server may take a little time
1532 sleep 100m
1533 call feedkeys(MouseLeftClickCode(2, 3), 'x')
1534 call feedkeys(MouseLeftDragCode(2, 11), 'x')
1535 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
1536 call assert_equal("d green y", @*)
1537
1538 " cleanup
1539 call term_wait(buf)
1540 call StopVimInTerminal(buf)
1541 let &mouse = save_mouse
1542 let &term = save_term
1543 let &ttymouse = save_ttymouse
1544 set mousetime& clipboard&
1545 call delete('Xtest_modeless')
1546 new | only!
1547endfunc
1548
1549
1550" vim: shiftwidth=2 sts=2 expandtab