blob: 395de5207b0d7972eaebc73d69f2f5201cb8ca7e [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02005
6source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01007source screendump.vim
Bram Moolenaar91689ea2020-05-11 22:04:53 +02008source mouse.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020011let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020012
Bram Moolenaar94053a52017-08-01 21:44:33 +020013" Open a terminal with a shell, assign the job to g:job and return the buffer
14" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020015func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010016 if has('win32')
17 let buf = term_start([&shell,'/k'], a:options)
18 else
19 let buf = term_start(&shell, a:options)
20 endif
Bram Moolenaar373a8762020-03-19 19:44:32 +010021 let g:test_is_flaky = 1
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020022
23 let termlist = term_list()
24 call assert_equal(1, len(termlist))
25 call assert_equal(buf, termlist[0])
26
27 let g:job = term_getjob(buf)
28 call assert_equal(v:t_job, type(g:job))
29
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020030 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020031 call assert_match("{'job': 'process \\d\\+ run'}", string)
32
Bram Moolenaar94053a52017-08-01 21:44:33 +020033 return buf
34endfunc
35
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020036func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020037 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020038 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020039
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020040 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020041 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020042 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020043 call assert_match('%aR[^\n]*running]', execute('ls R'))
44 call assert_notmatch('%[^\n]*running]', execute('ls F'))
45 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar004a6782020-04-11 17:09:31 +020046 call assert_fails('set modifiable', 'E946:')
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020047
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020048 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020049 call TermWait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_equal('n', mode())
51 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020052 call assert_match('%aF[^\n]*finished]', execute('ls F'))
53 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
54 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020055
Bram Moolenaar94053a52017-08-01 21:44:33 +020056 " closing window wipes out the terminal buffer a with finished job
57 close
58 call assert_equal("", bufname(buf))
59
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020060 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020061 unlet g:job
62endfunc
63
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020064func Test_terminal_TerminalWinOpen()
65 au TerminalWinOpen * let b:done = 'yes'
66 let buf = Run_shell_in_terminal({})
67 call assert_equal('yes', b:done)
68 call StopShellInTerminal(buf)
69 " closing window wipes out the terminal buffer with the finished job
70 close
71
72 if has("unix")
73 terminal ++hidden ++open sleep 1
74 sleep 1
75 call assert_fails("echo b:done", 'E121:')
76 endif
77
78 au! TerminalWinOpen
79endfunc
80
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020081func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020082 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020083 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020084 call TermWait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020085
86 setlocal modifiable
87 exe "normal Axxx\<Esc>"
88 call assert_fails(buf . 'bwipe', 'E517')
89 undo
90
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020091 exe buf . 'bwipe'
92 unlet g:job
93endfunc
94
Bram Moolenaar5b868a82019-02-25 06:11:53 +010095func Test_terminal_paste_register()
96 let @" = "text to paste"
97
98 let buf = Run_shell_in_terminal({})
99 " Wait for the shell to display a prompt
100 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
101
102 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
103 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200104 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +0100105
106 exe buf . 'bwipe!'
107 unlet g:job
108endfunc
109
Bram Moolenaar94053a52017-08-01 21:44:33 +0200110func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200111 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200112 call assert_fails(buf . 'bwipe', 'E517')
113 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200114 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200115 call assert_equal("", bufname(buf))
116
117 unlet g:job
118endfunc
119
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200120func Test_terminal_split_quit()
121 let buf = Run_shell_in_terminal({})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200122 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200123 split
124 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200125 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200126 sleep 50m
127 call assert_equal('run', job_status(g:job))
128
129 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200130 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200131
132 exe buf . 'bwipe'
133 unlet g:job
134endfunc
135
Bram Moolenaar94053a52017-08-01 21:44:33 +0200136func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200137 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200138 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200139 quit
140 for nr in range(1, winnr('$'))
141 call assert_notequal(winbufnr(nr), buf)
142 endfor
143 call assert_true(bufloaded(buf))
144 call assert_true(buflisted(buf))
145
146 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200147 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200148 exe buf . 'bwipe'
149
150 unlet g:job
151endfunc
152
Bram Moolenaar1e115362019-01-09 23:01:02 +0100153func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200154 exe g:buf . 'bwipe!'
155 let g:buf = 0
156endfunc
157
Bram Moolenaar9d189612017-09-09 18:11:00 +0200158func Get_cat_123_cmd()
159 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100160 if !has('conpty')
161 return 'cmd /c "cls && color 2 && echo 123"'
162 else
163 " When clearing twice, extra sequence is not output.
164 return 'cmd /c "cls && cls && color 2 && echo 123"'
165 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200166 else
167 call writefile(["\<Esc>[32m123"], 'Xtext')
168 return "cat Xtext"
169 endif
170endfunc
171
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200172func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200173 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200174 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
175 let g:job = term_getjob(g:buf)
176
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200177 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
178 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200179 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100180 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200181 call delete('Xtext')
182endfunc
183
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200184func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200185 let l = term_scrape(a:buf, 0)
186 call assert_true(len(l) == 0)
187 let l = term_scrape(a:buf, 999)
188 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200189 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200190 call assert_true(len(l) > 0)
191 call assert_equal('1', l[0].chars)
192 call assert_equal('2', l[1].chars)
193 call assert_equal('3', l[2].chars)
194 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200195 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
196 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100197 if has('win32')
198 " On Windows 'background' always defaults to dark, even though the terminal
199 " may use a light background. Therefore accept both white and black.
200 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200201 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100202 if &background == 'light'
203 call assert_equal('#ffffff', l[0].bg)
204 else
205 call assert_equal('#000000', l[0].bg)
206 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200207 endif
208
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200209 let l = term_getline(a:buf, -1)
210 call assert_equal('', l)
211 let l = term_getline(a:buf, 0)
212 call assert_equal('', l)
213 let l = term_getline(a:buf, 999)
214 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200215 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200216 call assert_equal('123', l)
217endfunc
218
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200219func Test_terminal_scrape_123()
220 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200221 let buf = term_start(cmd)
222
223 let termlist = term_list()
224 call assert_equal(1, len(termlist))
225 call assert_equal(buf, termlist[0])
226
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200227 " Nothing happens with invalid buffer number
228 call term_wait(1234)
229
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200230 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200231 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200232 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200233 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200234 call Check_123(buf)
235
236 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100237 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200238 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200239 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200240 call Check_123(buf)
241
242 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200243 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200244endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200245
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200246func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200247 call writefile(["léttまrs"], 'Xtext')
248 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200249 " Run cmd with UTF-8 codepage to make the type command print the expected
250 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100251 let buf = term_start("cmd /K chcp 65001")
252 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200253 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100254 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200255 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100256 let buf = term_start("cat Xtext")
257 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200258 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200259
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100260 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
261 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200262 call assert_true(len(l) >= 7)
263 call assert_equal('l', l[0].chars)
264 call assert_equal('é', l[1].chars)
265 call assert_equal(1, l[1].width)
266 call assert_equal('t', l[2].chars)
267 call assert_equal('t', l[3].chars)
268 call assert_equal('ま', l[4].chars)
269 call assert_equal(2, l[4].width)
270 call assert_equal('r', l[5].chars)
271 call assert_equal('s', l[6].chars)
272
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100273 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200274 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200275 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200276
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100277 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200278 call delete('Xtext')
279endfunc
280
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200281func Test_terminal_scroll()
282 call writefile(range(1, 200), 'Xtext')
283 if has('win32')
284 let cmd = 'cmd /c "type Xtext"'
285 else
286 let cmd = "cat Xtext"
287 endif
288 let buf = term_start(cmd)
289
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100290 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200291 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200292 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200293
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100294 " wait until the scrolling stops
295 while 1
296 let scrolled = buf->term_getscrolled()
297 sleep 20m
298 if scrolled == buf->term_getscrolled()
299 break
300 endif
301 endwhile
302
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200303 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200304 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200305 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200306 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200307 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200308 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200309
310 exe buf . 'bwipe'
311 call delete('Xtext')
312endfunc
313
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200314func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200315 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200316 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200317 call writefile(range(150), 'Xtext')
318 if has('win32')
319 call term_sendkeys(buf, "type Xtext\<CR>")
320 else
321 call term_sendkeys(buf, "cat Xtext\<CR>")
322 endif
323 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200324 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200325 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200326 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200327 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200328
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200329 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200330 call TermWait(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200331 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200332 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100333 call delete('Xtext')
334endfunc
335
336func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200337 " tail -f only works on Unix
338 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100339
340 call writefile(range(50), 'Xtext')
341 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100342 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100343 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100344 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100345 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
346 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100347 \ 'call feedkeys("\<C-W>N", "xt")',
348 \ ], 'XTest_postponed')
349 let buf = RunVimInTerminal('-S XTest_postponed', {})
350 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100351 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100352
353 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200354 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100355 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100356 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100357
358 " Back to Terminal-Job mode, text will scroll and show the extra line.
359 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100360 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100361
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100362 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100363 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200364 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100365 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100366 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200367 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100368 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100369 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
370
371 " check type of term_sendkeys() return value
372 echo type(tsk_ret)
373
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100374 call StopVimInTerminal(buf)
375 call delete('XTest_postponed')
376 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200377endfunc
378
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100379" Run diff on two dumps with different size.
380func Test_terminal_dumpdiff_size()
381 call assert_equal(1, winnr('$'))
382 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
383 call assert_equal(2, winnr('$'))
384 call assert_match('Test_incsearch_search_01.dump', getline(10))
385 call assert_match(' +++++$', getline(11))
386 call assert_match('Test_popup_command_01.dump', getline(31))
387 call assert_equal(repeat('+', 75), getline(30))
388 quit
389endfunc
390
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200391func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200392 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200393
Bram Moolenaarb2412082017-08-20 18:09:14 +0200394 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200395 let size = term_getsize('')
396 bwipe!
397 call assert_equal(5, size[0])
398
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200399 call term_start(cmd, {'term_rows': 6})
400 let size = term_getsize('')
401 bwipe!
402 call assert_equal(6, size[0])
403
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200404 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200405 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200406 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200407
408 call term_setsize('', 6, 0)
409 call assert_equal([6, 33], term_getsize(''))
410
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200411 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200412 call assert_equal([6, 35], term_getsize(''))
413
414 call term_setsize('', 7, 30)
415 call assert_equal([7, 30], term_getsize(''))
416
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200417 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200418 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200419
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200420 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
421 let size = term_getsize('')
422 bwipe!
423 call assert_equal([6, 36], size)
424
Bram Moolenaarb2412082017-08-20 18:09:14 +0200425 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200426 let size = term_getsize('')
427 bwipe!
428 call assert_equal(20, size[1])
429
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200430 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200431 let size = term_getsize('')
432 bwipe!
433 call assert_equal(26, size[1])
434
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200435 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200436 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200437 let size = term_getsize('')
438 bwipe!
439 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200440
441 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
442 let size = term_getsize('')
443 bwipe!
444 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200445
446 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200447endfunc
448
449func Test_terminal_curwin()
450 let cmd = Get_cat_123_cmd()
451 call assert_equal(1, winnr('$'))
452
453 split dummy
454 exe 'terminal ++curwin ' . cmd
455 call assert_equal(2, winnr('$'))
456 bwipe!
457
458 split dummy
459 call term_start(cmd, {'curwin': 1})
460 call assert_equal(2, winnr('$'))
461 bwipe!
462
463 split dummy
464 call setline(1, 'change')
465 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
466 call assert_equal(2, winnr('$'))
467 exe 'terminal! ++curwin ' . cmd
468 call assert_equal(2, winnr('$'))
469 bwipe!
470
471 split dummy
472 call setline(1, 'change')
473 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
474 call assert_equal(2, winnr('$'))
475 bwipe!
476
477 split dummy
478 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200479 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200480endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200481
Bram Moolenaarff546792017-11-21 14:47:57 +0100482func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200483 if s:python != ''
484 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200485 " 500 was not enough for Travis
486 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200487 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200488 echo 'This will take five seconds...'
489 let waittime = 2000
490 if has('win32')
491 let cmd = $windir . '\system32\timeout.exe 1'
492 else
493 let cmd = 'sleep 1'
494 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200495 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100496 return [cmd, waittime]
497endfunc
498
499func Test_terminal_finish_open_close()
500 call assert_equal(1, winnr('$'))
501
502 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200503
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100504 " shell terminal closes automatically
505 terminal
506 let buf = bufnr('%')
507 call assert_equal(2, winnr('$'))
508 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200509 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200510 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200511 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100512
513 " shell terminal that does not close automatically
514 terminal ++noclose
515 let buf = bufnr('%')
516 call assert_equal(2, winnr('$'))
517 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200518 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200519 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100520 call assert_equal(2, winnr('$'))
521 quit
522 call assert_equal(1, winnr('$'))
523
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200524 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200525 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200526 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200527 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200528
529 call term_start(cmd, {'term_finish': 'close'})
530 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200531 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200532 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200533 call assert_equal(1, winnr('$'))
534
535 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200536 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200537 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200538 bwipe
539
540 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200541 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200542 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200543 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200544
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200545 exe 'terminal ++hidden ++open ' . cmd
546 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200547 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200548 bwipe
549
550 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
551 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200552 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200553 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200554
555 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
556 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
557 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
558 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
559
560 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200561 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200562 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200563 call assert_equal(4, winheight(0))
564 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200565endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200566
567func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100568 if has('win32')
569 let cmd = 'cmd /c cd'
570 else
571 CheckExecutable pwd
572 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200573 endif
574 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100575 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200576 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200577
578 exe buf . 'bwipe'
579 call delete('Xdir', 'rf')
580endfunc
581
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200582func Test_terminal_cwd_failure()
583 " Case 1: Provided directory is not actually a directory. Attempt to make
584 " the file executable as well.
585 call writefile([], 'Xfile')
586 call setfperm('Xfile', 'rwx------')
587 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
588 call delete('Xfile')
589
590 " Case 2: Directory does not exist.
591 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
592
593 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100594 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200595 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100596 call mkdir('XdirNoAccess', '', '0600')
597 " return early if the directory permissions could not be set properly
598 if getfperm('XdirNoAccess')[2] == 'x'
599 call delete('XdirNoAccess', 'rf')
600 return
601 endif
602 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
603 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200604 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200605endfunc
606
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100607func Test_terminal_servername()
608 if !has('clientserver')
609 return
610 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200611 call s:test_environment("VIM_SERVERNAME", v:servername)
612endfunc
613
614func Test_terminal_version()
615 call s:test_environment("VIM_TERMINAL", string(v:version))
616endfunc
617
618func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100619 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100620 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200621 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100622 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200623 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100624 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200625 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100626 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200627 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200628 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200629 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100630
Bram Moolenaar012eb662018-03-13 17:55:27 +0100631 exe buf . 'bwipe'
632 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100633endfunc
634
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200635func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100636 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200637 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200638 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100639 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100640 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100641 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100642 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100643 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200644 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200645 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200646 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200647
Bram Moolenaar012eb662018-03-13 17:55:27 +0100648 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200649endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200650
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200651func Test_terminal_list_args()
652 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
653 call assert_fails(buf . 'bwipe', 'E517')
654 exe buf . 'bwipe!'
655 call assert_equal("", bufname(buf))
656endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200657
658func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100659 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100660 let wait_time = 5000
661 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100662 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200663 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100664 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200665 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100666 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100667 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100668 " NPFS is used in Windows, nonblocking mode does not work properly.
669 elseif has('win32')
670 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200671 else
672 let len = 5000
673 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200674
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100675 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100676 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100677 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200678 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100679 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200680
681 " On MS-Windows there is an extra empty line below "done". Find "done" in
682 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100683 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100684 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100685 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200686 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100687 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200688 endif
689 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200690
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100691 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200692 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200693 call TermWait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200694 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200695 bwipe
696endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200697
698func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100699 " TODO: enable once writing to stdin works on MS-Windows
700 CheckNotMSWindows
701 CheckExecutable wc
702
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200703 call setline(1, ['one', 'two', 'three'])
704 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200705 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200706 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200707 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100708 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200709
710 call setline(1, ['one', 'two', 'three', 'four'])
711 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200712 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200713 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200714 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100715 %bwipe!
716endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200717
Bram Moolenaar21109272020-01-30 16:27:20 +0100718func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200719 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200720
Bram Moolenaar21109272020-01-30 16:27:20 +0100721 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200722 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100723 " MS-Windows echoes the input, Unix doesn't.
724 if has('win32')
725 call WaitFor({-> getline('$') =~ 'exit(123)'})
726 call assert_equal('hello', getline(line('$') - 1))
727 else
728 call WaitFor({-> getline('$') =~ 'hello'})
729 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200730 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100731 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
732 %bwipe!
733endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200734
Bram Moolenaar21109272020-01-30 16:27:20 +0100735func Test_terminal_eof_arg_win32_ctrl_z()
736 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200737 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100738
739 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200740 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100741 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
742 call assert_match('\^Z', getline(line('$') - 1))
743 %bwipe!
744endfunc
745
746func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200747 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100748
749 " Check the last specified ++eof arg is used and should not memory leak.
750 new
751 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200752 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100753 " MS-Windows echoes the input, Unix doesn't.
754 if has('win32')
755 call WaitFor({-> getline('$') =~ 'exit(123)'})
756 call assert_equal('hello', getline(line('$') - 1))
757 else
758 call WaitFor({-> getline('$') =~ 'hello'})
759 call assert_equal('hello', getline('$'))
760 endif
761 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
762 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200763endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200764
765func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200766 let buf = term_start('NONE', {})
767 call assert_notequal(0, buf)
768
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200769 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200770 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100771 if has('gui_running') && !has('win32')
772 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200773 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100774 else
775 " Otherwise using a job works on all systems.
776 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200777 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200778 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200779
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200780 bwipe!
781endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200782
783func Test_terminal_special_chars()
784 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200785 CheckUnix
786
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200787 call mkdir('Xdir with spaces')
788 call writefile(['x'], 'Xdir with spaces/quoted"file')
789 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200790 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100791 " make sure the job has finished
792 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200793
794 call delete('Xdir with spaces', 'rf')
795 bwipe
796endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200797
798func Test_terminal_wrong_options()
799 call assert_fails('call term_start(&shell, {
800 \ "in_io": "file",
801 \ "in_name": "xxx",
802 \ "out_io": "file",
803 \ "out_name": "xxx",
804 \ "err_io": "file",
805 \ "err_name": "xxx"
806 \ })', 'E474:')
807 call assert_fails('call term_start(&shell, {
808 \ "out_buf": bufnr("%")
809 \ })', 'E474:')
810 call assert_fails('call term_start(&shell, {
811 \ "err_buf": bufnr("%")
812 \ })', 'E474:')
813endfunc
814
815func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200816 let cmd = Get_cat_123_cmd()
817 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200818 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100819 " ConPTY may precede escape sequence. There are things that are not so.
820 if !has('conpty')
821 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
822 call assert_match('123', readfile('Xfile')[0])
823 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200824 let g:job = term_getjob(buf)
825 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
826 call delete('Xfile')
827 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200828
829 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200830 call writefile(['one line'], 'Xfile')
831 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200832 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200833 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200834 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200835 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200836 bwipe
837 call delete('Xfile')
838 endif
839endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200840
841func TerminalTmap(remap)
842 let buf = Run_shell_in_terminal({})
843 call assert_equal('t', mode())
844
845 if a:remap
846 tmap 123 456
847 else
848 tnoremap 123 456
849 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100850 " don't use abcde, it's an existing command
851 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200852 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100853 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200854 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200855 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200856 let lnum = term_getcursor(buf)[0]
857 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100858 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200859 else
860 call assert_match('456', term_getline(buf, lnum))
861 endif
862
863 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200864 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200865 call TermWait(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200866
867 tunmap 123
868 tunmap 456
869 call assert_equal('', maparg('123', 't'))
870 close
871 unlet g:job
872endfunc
873
874func Test_terminal_tmap()
875 call TerminalTmap(1)
876 call TerminalTmap(0)
877endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200878
879func Test_terminal_wall()
880 let buf = Run_shell_in_terminal({})
881 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200882 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200883 call TermWait(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200884 exe buf . 'bwipe'
885 unlet g:job
886endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200887
Bram Moolenaar7a760922018-02-19 23:10:02 +0100888func Test_terminal_wqall()
889 let buf = Run_shell_in_terminal({})
890 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200891 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200892 call TermWait(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100893 exe buf . 'bwipe'
894 unlet g:job
895endfunc
896
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200897func Test_terminal_composing_unicode()
898 let save_enc = &encoding
899 set encoding=utf-8
900
901 if has('win32')
902 let cmd = "cmd /K chcp 65001"
903 let lnum = [3, 6, 9]
904 else
905 let cmd = &shell
906 let lnum = [1, 3, 5]
907 endif
908
909 enew
910 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100911 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200912 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200913
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200914 if has('win32')
915 call assert_equal('cmd', job_info(g:job).cmd[0])
916 else
917 call assert_equal(&shell, job_info(g:job).cmd[0])
918 endif
919
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200920 " ascii + composing
921 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200922 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200923 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200924 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200925 call term_sendkeys(buf, "\<cr>")
926 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200927 let l = term_scrape(buf, lnum[0] + 1)
928 call assert_equal("a\u0308", l[0].chars)
929 call assert_equal("b", l[1].chars)
930 call assert_equal("c", l[2].chars)
931
932 " multibyte + composing
933 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200934 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200935 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200936 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200937 call term_sendkeys(buf, "\<cr>")
938 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200939 let l = term_scrape(buf, lnum[1] + 1)
940 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar79ea6802020-05-17 15:09:27 +0200941 call assert_equal("\u304e", l[2].chars)
942 call assert_equal("\u304f\u3099", l[3].chars)
943 call assert_equal("\u3052", l[5].chars)
944 call assert_equal("\u3053\u3099", l[6].chars)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200945
946 " \u00a0 + composing
947 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200948 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200949 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200950 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200951 call term_sendkeys(buf, "\<cr>")
952 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200953 let l = term_scrape(buf, lnum[2] + 1)
954 call assert_equal("\u00a0\u0308", l[3].chars)
955
956 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200957 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200958 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100959 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200960 let &encoding = save_enc
961endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100962
963func Test_terminal_aucmd_on_close()
964 fun Nop()
965 let s:called = 1
966 endfun
967
968 aug repro
969 au!
970 au BufWinLeave * call Nop()
971 aug END
972
973 let [cmd, waittime] = s:get_sleep_cmd()
974
975 call assert_equal(1, winnr('$'))
976 new
977 call setline(1, ['one', 'two'])
978 exe 'term ++close ' . cmd
979 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200980 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100981 call assert_equal(1, s:called)
982 bwipe!
983
984 unlet s:called
985 au! repro
986 delfunc Nop
987endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100988
989func Test_terminal_term_start_empty_command()
990 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
991 call assert_fails(cmd, 'E474')
992 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
993 call assert_fails(cmd, 'E474')
994 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
995 call assert_fails(cmd, 'E474')
996 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
997 call assert_fails(cmd, 'E474')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200998 let cmd = "call term_start('', {'term_name' : []})"
999 call assert_fails(cmd, 'E475')
1000 let cmd = "call term_start('', {'term_finish' : 'axby'})"
1001 call assert_fails(cmd, 'E475')
1002 let cmd = "call term_start('', {'eof_chars' : []})"
1003 call assert_fails(cmd, 'E475:')
1004 let cmd = "call term_start('', {'term_kill' : []})"
1005 call assert_fails(cmd, 'E475:')
1006 let cmd = "call term_start('', {'tty_type' : []})"
1007 call assert_fails(cmd, 'E475:')
1008 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1009 call assert_fails(cmd, 'E475:')
1010 let cmd = "call term_start('', {'term_highlight' : []})"
1011 call assert_fails(cmd, 'E475:')
1012 if has('gui')
1013 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1014 call assert_fails(cmd, 'E475:')
1015 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1016 call assert_fails(cmd, 'E730:')
1017 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
1018 call assert_fails(cmd, 'E475:')
1019 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001020endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001021
1022func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001023 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001024
1025 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001026 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001027
Bram Moolenaar086eb872018-03-25 21:24:12 +02001028 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001029 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001030
Bram Moolenaar086eb872018-03-25 21:24:12 +02001031 " Request the cursor position.
1032 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001033
1034 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001035 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001036
Bram Moolenaar086eb872018-03-25 21:24:12 +02001037 " End "cat" gently.
1038 call term_sendkeys(buf, "\<CR>\<C-D>")
1039
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001040 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001041 exe buf . 'bwipe'
1042 unlet g:job
1043endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001044
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001045" Run Vim, start a terminal in that Vim with the kill argument,
1046" :qall works.
1047func Run_terminal_qall_kill(line1, line2)
1048 " 1. Open a terminal window and wait for the prompt to appear
1049 " 2. set kill using term_setkill()
1050 " 3. make Vim exit, it will kill the shell
1051 let after = [
1052 \ a:line1,
1053 \ 'let buf = bufnr("%")',
1054 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1055 \ ' sleep 10m',
1056 \ 'endwhile',
1057 \ a:line2,
1058 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1059 \ 'qall',
1060 \ ]
1061 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001062 return
1063 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001064 call assert_equal("done", readfile("Xdone")[0])
1065 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001066endfunc
1067
1068" Run Vim in a terminal, then start a terminal in that Vim with a kill
1069" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001070func Test_terminal_qall_kill_arg()
1071 call Run_terminal_qall_kill('term ++kill=kill', '')
1072endfunc
1073
1074" Run Vim, start a terminal in that Vim, set the kill argument with
1075" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001076func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001077 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001078endfunc
1079
1080" Run Vim, start a terminal in that Vim without the kill argument,
1081" check that :qall does not exit, :qall! does.
1082func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001083 let after =<< trim [CODE]
1084 term
1085 let buf = bufnr("%")
1086 while term_getline(buf, 1) =~ "^\\s*$"
1087 sleep 10m
1088 endwhile
1089 set nomore
1090 au VimLeavePre * call writefile(["too early"], "Xdone")
1091 qall
1092 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1093 cquit
1094 [CODE]
1095
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001096 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001097 return
1098 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001099 call assert_equal("done", readfile("Xdone")[0])
1100 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001101endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001102
1103" Run Vim in a terminal, then start a terminal in that Vim without a kill
1104" argument, check that :confirm qall works.
1105func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001106 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001107 let buf = RunVimInTerminal('', {})
1108
1109 " Open a terminal window and wait for the prompt to appear
1110 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001111 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1112 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001113
1114 " make Vim exit, it will prompt to kill the shell
1115 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001116 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001117 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001118 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001119
1120 " close the terminal window where Vim was running
1121 quit
1122endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001123
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001124" Run Vim in a terminal, then start a terminal window with a shell and check
1125" that Vim exits if it is closed.
1126func Test_terminal_exit()
1127 CheckRunVimInTerminal
1128
1129 let lines =<< trim END
1130 let winid = win_getid()
1131 help
1132 term
1133 let termid = win_getid()
1134 call win_gotoid(winid)
1135 close
1136 call win_gotoid(termid)
1137 END
1138 call writefile(lines, 'XtermExit')
1139 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1140 let job = term_getjob(buf)
1141 call WaitForAssert({-> assert_equal("run", job_status(job))})
1142
1143 " quit the shell, it will make Vim exit
1144 call term_sendkeys(buf, "exit\<CR>")
1145 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1146
1147 call delete('XtermExit')
1148endfunc
1149
Bram Moolenaar012eb662018-03-13 17:55:27 +01001150func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001151 augroup repro
1152 au!
1153 au TerminalOpen * let s:called += 1
1154 augroup END
1155
1156 let s:called = 0
1157
1158 " Open a terminal window with :terminal
1159 terminal
1160 call assert_equal(1, s:called)
1161 bwipe!
1162
1163 " Open a terminal window with term_start()
1164 call term_start(&shell)
1165 call assert_equal(2, s:called)
1166 bwipe!
1167
1168 " Open a hidden terminal buffer with :terminal
1169 terminal ++hidden
1170 call assert_equal(3, s:called)
1171 for buf in term_list()
1172 exe buf . "bwipe!"
1173 endfor
1174
1175 " Open a hidden terminal buffer with term_start()
1176 let buf = term_start(&shell, {'hidden': 1})
1177 call assert_equal(4, s:called)
1178 exe buf . "bwipe!"
1179
1180 unlet s:called
1181 au! repro
1182endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001183
1184func Check_dump01(off)
1185 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1186 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001187 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001188endfunc
1189
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001190func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001191 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001192 let save_enc = &encoding
1193 set encoding=utf-8
1194 call assert_equal(1, winnr('$'))
1195
1196 let text = " a\u0300 e\u0302 o\u0308"
1197 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001198 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001199 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001200 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001201 let dumpline = readfile('Xdump')[0]
1202 call assert_match('|à| |ê| |ö', dumpline)
1203
1204 call StopVimInTerminal(buf)
1205 call delete('Xcomposing')
1206 call delete('Xdump')
1207 let &encoding = save_enc
1208endfunc
1209
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001210" Tests for failures in the term_dumpwrite() function
1211func Test_terminal_dumpwrite_errors()
1212 CheckRunVimInTerminal
1213 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1214 let buf = RunVimInTerminal('', {})
1215 call term_wait(buf)
1216 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1217 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1218 call writefile([], 'Xtest.dump')
1219 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1220 call delete('Xtest.dump')
1221 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1222 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001223 call test_garbagecollect_now()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001224 call StopVimInTerminal(buf)
1225 call term_wait(buf)
1226 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1227 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1228 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1229endfunc
1230
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001231" just testing basic functionality.
1232func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001233 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001234 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001235 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001236 call assert_equal(2, winnr('$'))
1237 call assert_equal(20, line('$'))
1238 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001239
1240 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001241 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001242 call assert_equal(buf, buf2)
1243 call assert_notequal('one two three four five', trim(getline(1)))
1244
1245 " Load the first dump again in the same window
1246 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1247 call assert_equal(buf, buf2)
1248 call Check_dump01(0)
1249
1250 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1251 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1252 new
1253 let closedbuf = winbufnr('')
1254 quit
1255 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001256 call assert_fails('call term_dumpload([])', 'E474:')
1257 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001258
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001259 quit
1260endfunc
1261
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001262func Test_terminal_dumpload_dump()
1263 CheckRunVimInTerminal
1264
1265 let lines =<< trim END
1266 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1267 END
1268 call writefile(lines, 'XtermDumpload')
1269 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1270 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1271
1272 call StopVimInTerminal(buf)
1273 call delete('XtermDumpload')
1274endfunc
1275
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001276func Test_terminal_dumpdiff()
1277 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001278 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001279 call assert_equal(2, winnr('$'))
1280 call assert_equal(62, line('$'))
1281 call Check_dump01(0)
1282 call Check_dump01(42)
1283 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1284 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001285
1286 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E474:')
1287 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1288 call writefile([], 'X1.dump')
1289 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1290 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001291endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001292
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001293func Test_terminal_dumpdiff_swap()
1294 call assert_equal(1, winnr('$'))
1295 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1296 call assert_equal(2, winnr('$'))
1297 call assert_equal(62, line('$'))
1298 call assert_match('Test_popup_command_01.dump', getline(21))
1299 call assert_match('Test_popup_command_03.dump', getline(42))
1300 call assert_match('Undo', getline(3))
1301 call assert_match('three four five', getline(45))
1302
1303 normal s
1304 call assert_match('Test_popup_command_03.dump', getline(21))
1305 call assert_match('Test_popup_command_01.dump', getline(42))
1306 call assert_match('three four five', getline(3))
1307 call assert_match('Undo', getline(45))
1308 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001309
1310 " Diff two terminal dump files with different number of rows
1311 " Swap the diffs
1312 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1313 call assert_match('Test_popup_command_01.dump', getline(21))
1314 call assert_match('Test_winline_rnu.dump', getline(42))
1315 normal s
1316 call assert_match('Test_winline_rnu.dump', getline(6))
1317 call assert_match('Test_popup_command_01.dump', getline(27))
1318 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001319endfunc
1320
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001321func Test_terminal_dumpdiff_options()
1322 set laststatus=0
1323 call assert_equal(1, winnr('$'))
1324 let height = winheight(0)
1325 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1326 call assert_equal(2, winnr('$'))
1327 call assert_equal(height, winheight(winnr()))
1328 call assert_equal(33, winwidth(winnr()))
1329 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1330 quit
1331
1332 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001333 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1334 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001335 call assert_equal(&columns, winwidth(0))
1336 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001337 call assert_equal('something else', bufname('%'))
1338 quit
1339
1340 call assert_equal(1, winnr('$'))
1341 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1342 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001343 call assert_fails("call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'bufnr': -1})", 'E475:')
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001344 bwipe
1345
1346 set laststatus&
1347endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001348
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001349func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001350 call assert_equal(1, winnr('$'))
1351
1352 " Use the title termcap entries to output the escape sequence.
1353 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001354 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001355 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001356 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001357 \ 'redraw',
1358 \ "set t_ts=",
1359 \ ], 'Xscript')
1360 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001361 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001362 call assert_equal('Xtextfile', expand('%:t'))
1363 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001364 return buf
1365endfunc
1366
1367func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001368 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001369 let buf = Api_drop_common('')
1370 call assert_equal(0, &bin)
1371 call assert_equal('', &fenc)
1372
1373 call StopVimInTerminal(buf)
1374 call delete('Xscript')
1375 bwipe Xtextfile
1376endfunc
1377
1378func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001379 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001380 let buf = Api_drop_common(',{"bin":1}')
1381 call assert_equal(1, &bin)
1382
1383 call StopVimInTerminal(buf)
1384 call delete('Xscript')
1385 bwipe Xtextfile
1386endfunc
1387
1388func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001389 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001390 let buf = Api_drop_common(',{"binary":1}')
1391 call assert_equal(1, &bin)
1392
1393 call StopVimInTerminal(buf)
1394 call delete('Xscript')
1395 bwipe Xtextfile
1396endfunc
1397
1398func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001399 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001400 set binary
1401 let buf = Api_drop_common(',{"nobin":1}')
1402 call assert_equal(0, &bin)
1403
1404 call StopVimInTerminal(buf)
1405 call delete('Xscript')
1406 bwipe Xtextfile
1407 set nobinary
1408endfunc
1409
1410func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001411 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001412 set binary
1413 let buf = Api_drop_common(',{"nobinary":1}')
1414 call assert_equal(0, &bin)
1415
1416 call StopVimInTerminal(buf)
1417 call delete('Xscript')
1418 bwipe Xtextfile
1419 set nobinary
1420endfunc
1421
1422func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001423 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001424 let buf = Api_drop_common(',{"ff":"dos"}')
1425 call assert_equal("dos", &ff)
1426
1427 call StopVimInTerminal(buf)
1428 call delete('Xscript')
1429 bwipe Xtextfile
1430endfunc
1431
1432func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001433 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001434 let buf = Api_drop_common(',{"fileformat":"dos"}')
1435 call assert_equal("dos", &ff)
1436
1437 call StopVimInTerminal(buf)
1438 call delete('Xscript')
1439 bwipe Xtextfile
1440endfunc
1441
1442func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001443 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001444 let buf = Api_drop_common(',{"enc":"utf-16"}')
1445 call assert_equal("utf-16", &fenc)
1446
1447 call StopVimInTerminal(buf)
1448 call delete('Xscript')
1449 bwipe Xtextfile
1450endfunc
1451
1452func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001453 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001454 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1455 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001456
1457 call StopVimInTerminal(buf)
1458 call delete('Xscript')
1459 bwipe Xtextfile
1460endfunc
1461
1462func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001463 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001464 let firstwinid = win_getid()
1465 split Xtextfile
1466 let textfile_winid = win_getid()
1467 call assert_equal(2, winnr('$'))
1468 call win_gotoid(firstwinid)
1469
1470 " Use the title termcap entries to output the escape sequence.
1471 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001472 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001473 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1474 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1475 \ 'redraw',
1476 \ "set t_ts=",
1477 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001478 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001479 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001480 call assert_equal(textfile_winid, win_getid())
1481
1482 call StopVimInTerminal(buf)
1483 call delete('Xscript')
1484 bwipe Xtextfile
1485endfunc
1486
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001487func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001488 let g:called_bufnum = a:bufnum
1489 let g:called_arg = a:arg
1490endfunc
1491
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001492func WriteApiCall(funcname)
1493 " Use the title termcap entries to output the escape sequence.
1494 call writefile([
1495 \ 'set title',
1496 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1497 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1498 \ 'redraw',
1499 \ "set t_ts=",
1500 \ ], 'Xscript')
1501endfunc
1502
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001503func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001504 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001505
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001506 unlet! g:called_bufnum
1507 unlet! g:called_arg
1508
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001509 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001510
1511 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001512 let buf = RunVimInTerminal('-S Xscript', {})
1513 call WaitFor({-> exists('g:called_bufnum')})
1514 call assert_equal(buf, g:called_bufnum)
1515 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001516 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001517
1518 unlet! g:called_bufnum
1519 unlet! g:called_arg
1520
1521 " Enable explicitly
1522 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1523 call WaitFor({-> exists('g:called_bufnum')})
1524 call assert_equal(buf, g:called_bufnum)
1525 call assert_equal(['hello', 123], g:called_arg)
1526 call StopVimInTerminal(buf)
1527
1528 unlet! g:called_bufnum
1529 unlet! g:called_arg
1530
1531 func! ApiCall_TryThis(bufnum, arg)
1532 let g:called_bufnum2 = a:bufnum
1533 let g:called_arg2 = a:arg
1534 endfunc
1535
1536 call WriteApiCall('ApiCall_TryThis')
1537
1538 " Use prefix match
1539 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1540 call WaitFor({-> exists('g:called_bufnum2')})
1541 call assert_equal(buf, g:called_bufnum2)
1542 call assert_equal(['hello', 123], g:called_arg2)
1543 call StopVimInTerminal(buf)
1544
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001545 call assert_fails("call term_start('ls', {'term_api' : []})", 'E475:')
1546
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001547 unlet! g:called_bufnum2
1548 unlet! g:called_arg2
1549
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001550 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001551 delfunction! ApiCall_TryThis
1552 unlet! g:called_bufnum2
1553 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001554endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001555
1556func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001557 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001558
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001559 func! TryThis(bufnum, arg)
1560 let g:called_bufnum3 = a:bufnum
1561 let g:called_arg3 = a:arg
1562 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001563
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001564 call WriteApiCall('TryThis')
1565
1566 unlet! g:called_bufnum3
1567 unlet! g:called_arg3
1568
1569 " Not permitted
1570 call ch_logfile('Xlog', 'w')
1571 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1572 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1573 call assert_false(exists('g:called_bufnum3'))
1574 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001575 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001576
1577 " No match
1578 call ch_logfile('Xlog', 'w')
1579 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1580 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1581 call assert_false(exists('g:called_bufnum3'))
1582 call assert_false(exists('g:called_arg3'))
1583 call StopVimInTerminal(buf)
1584
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001585 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001586 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001587 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001588 delfunction! TryThis
1589 unlet! g:called_bufnum3
1590 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001591endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001592
Bram Moolenaara997b452018-04-17 23:24:06 +02001593let s:caught_e937 = 0
1594
1595func Tapi_Delete(bufnum, arg)
1596 try
1597 execute 'bdelete!' a:bufnum
1598 catch /E937:/
1599 let s:caught_e937 = 1
1600 endtry
1601endfunc
1602
1603func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001604 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001605
1606 call WriteApiCall('Tapi_Delete')
1607 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001608 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001609
1610 call StopVimInTerminal(buf)
1611 call delete('Xscript')
1612 call ch_logfile('', '')
1613endfunc
1614
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001615func Test_terminal_ansicolors_default()
Bram Moolenaara161cb52020-04-30 19:09:35 +02001616 CheckFunction term_getansicolors
1617
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001618 let colors = [
1619 \ '#000000', '#e00000',
1620 \ '#00e000', '#e0e000',
1621 \ '#0000e0', '#e000e0',
1622 \ '#00e0e0', '#e0e0e0',
1623 \ '#808080', '#ff4040',
1624 \ '#40ff40', '#ffff40',
1625 \ '#4040ff', '#ff40ff',
1626 \ '#40ffff', '#ffffff',
1627 \]
1628
1629 let buf = Run_shell_in_terminal({})
1630 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001631 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001632 call TermWait(buf)
Bram Moolenaar98f16712020-05-22 13:34:01 +02001633 call assert_equal([], term_getansicolors(buf))
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001634
1635 exe buf . 'bwipe'
1636endfunc
1637
1638let s:test_colors = [
1639 \ '#616e64', '#0d0a79',
1640 \ '#6d610d', '#0a7373',
1641 \ '#690d0a', '#6d696e',
1642 \ '#0d0a6f', '#616e0d',
1643 \ '#0a6479', '#6d0d0a',
1644 \ '#617373', '#0d0a69',
1645 \ '#6d690d', '#0a6e6f',
1646 \ '#610d0a', '#6e6479',
1647 \]
1648
1649func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001650 CheckFeature termguicolors
Bram Moolenaara161cb52020-04-30 19:09:35 +02001651 CheckFunction term_getansicolors
1652
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001653 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1654 let buf = Run_shell_in_terminal({})
1655 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001656 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001657 call TermWait(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001658
1659 exe buf . 'bwipe'
1660 unlet g:terminal_ansi_colors
1661endfunc
1662
1663func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001664 CheckFeature termguicolors
Bram Moolenaara161cb52020-04-30 19:09:35 +02001665 CheckFunction term_getansicolors
1666
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001667 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1668 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1669 call assert_equal(s:test_colors, term_getansicolors(buf))
1670
1671 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001672 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001673
1674 let colors = [
1675 \ 'ivory', 'AliceBlue',
1676 \ 'grey67', 'dark goldenrod',
1677 \ 'SteelBlue3', 'PaleVioletRed4',
1678 \ 'MediumPurple2', 'yellow2',
1679 \ 'RosyBrown3', 'OrangeRed2',
1680 \ 'white smoke', 'navy blue',
1681 \ 'grey47', 'gray97',
1682 \ 'MistyRose2', 'DodgerBlue4',
1683 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001684 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001685
1686 let colors[4] = 'Invalid'
1687 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001688 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001689
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001690 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001691 call TermWait(buf)
Bram Moolenaar98f16712020-05-22 13:34:01 +02001692 call assert_equal(0, term_setansicolors(buf, []))
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001693 exe buf . 'bwipe'
1694endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001695
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001696func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001697 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001698
1699 " Use all the ANSI colors.
1700 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001701 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001702 \ 'hi Tblack ctermfg=0 ctermbg=8',
1703 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1704 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1705 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1706 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1707 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1708 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1709 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1710 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1711 \ 'hi Tred ctermfg=9 ctermbg=1',
1712 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1713 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1714 \ 'hi Tblue ctermfg=12 ctermbg=4',
1715 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1716 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1717 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001718 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1719 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1720 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001721 \ '',
1722 \ 'call matchadd("Tblack", "A")',
1723 \ 'call matchadd("Tdarkred", "B")',
1724 \ 'call matchadd("Tdarkgreen", "C")',
1725 \ 'call matchadd("Tbrown", "D")',
1726 \ 'call matchadd("Tdarkblue", "E")',
1727 \ 'call matchadd("Tdarkmagenta", "F")',
1728 \ 'call matchadd("Tdarkcyan", "G")',
1729 \ 'call matchadd("Tlightgrey", "H")',
1730 \ 'call matchadd("Tdarkgrey", "I")',
1731 \ 'call matchadd("Tred", "J")',
1732 \ 'call matchadd("Tgreen", "K")',
1733 \ 'call matchadd("Tyellow", "L")',
1734 \ 'call matchadd("Tblue", "M")',
1735 \ 'call matchadd("Tmagenta", "N")',
1736 \ 'call matchadd("Tcyan", "O")',
1737 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001738 \ 'call matchadd("TdarkredBold", "X")',
1739 \ 'call matchadd("TgreenBold", "Y")',
1740 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001741 \ 'redraw',
1742 \ ], 'Xcolorscript')
1743 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1744 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1745
1746 call term_sendkeys(buf, ":q\<CR>")
1747 call StopVimInTerminal(buf)
1748 call delete('Xcolorscript')
1749endfunc
1750
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001751func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001752 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001753 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001754 let text = []
1755 for n in range(10)
1756 call add(text, repeat(n, 50))
1757 endfor
1758 call writefile(text, 'Xwinsize')
1759 let buf = RunVimInTerminal('Xwinsize', {})
1760 let win = bufwinid(buf)
1761 call assert_equal([6, 40], term_getsize(buf))
1762 call assert_equal(6, winheight(win))
1763 call assert_equal(40, winwidth(win))
1764
1765 " resizing the window doesn't resize the terminal.
1766 resize 10
1767 vertical resize 60
1768 call assert_equal([6, 40], term_getsize(buf))
1769 call assert_equal(10, winheight(win))
1770 call assert_equal(60, winwidth(win))
1771
1772 call StopVimInTerminal(buf)
1773 call delete('Xwinsize')
1774
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001775 call assert_fails('set termwinsize=40', 'E474')
1776 call assert_fails('set termwinsize=10+40', 'E474')
1777 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001778
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001779 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001780endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001781
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001782func Test_terminal_termwinsize_option_zero()
1783 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001784 let buf = Run_shell_in_terminal({})
1785 let win = bufwinid(buf)
1786 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001787 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001788 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001789 exe buf . 'bwipe'
1790
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001791 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001792 let buf = Run_shell_in_terminal({})
1793 let win = bufwinid(buf)
1794 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001795 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001796 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001797 exe buf . 'bwipe'
1798
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001799 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001800 let buf = Run_shell_in_terminal({})
1801 let win = bufwinid(buf)
1802 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001803 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001804 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001805 exe buf . 'bwipe'
1806
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001807 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001808endfunc
1809
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001810func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001811 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001812 vsplit
1813 let buf = Run_shell_in_terminal({})
1814 let win = bufwinid(buf)
1815 call assert_inrange(10, 1000, winheight(win))
1816 call assert_inrange(50, 1000, winwidth(win))
1817 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1818
1819 resize 15
1820 vertical resize 60
1821 redraw
1822 call assert_equal([15, 60], term_getsize(buf))
1823 call assert_equal(15, winheight(win))
1824 call assert_equal(60, winwidth(win))
1825
1826 resize 7
1827 vertical resize 30
1828 redraw
1829 call assert_equal([10, 50], term_getsize(buf))
1830 call assert_equal(7, winheight(win))
1831 call assert_equal(30, winwidth(win))
1832
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001833 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001834 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001835 exe buf . 'bwipe'
1836
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001837 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001838 let buf = Run_shell_in_terminal({})
1839 let win = bufwinid(buf)
1840 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001841 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001842 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001843 exe buf . 'bwipe'
1844
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001845 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001846endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001847
1848func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001849 " make three tabpages, terminal in the middle
1850 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001851 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001852 tabnew
1853 tabprev
1854 call assert_equal(1, winnr('$'))
1855 call assert_equal(2, tabpagenr())
1856 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001857
1858 let buf = Run_shell_in_terminal({})
1859 let termwin = bufwinid(buf)
1860 set termwinkey=<C-L>
1861 call feedkeys("\<C-L>w", 'tx')
1862 call assert_equal(thiswin, win_getid())
1863 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001864 call assert_equal(termwin, win_getid())
1865
Bram Moolenaar997d4242019-09-14 21:23:40 +02001866 if has('langmap')
1867 set langmap=xjyk
1868 call feedkeys("\<C-L>x", 'tx')
1869 call assert_equal(thiswin, win_getid())
1870 call feedkeys("\<C-W>y", 'tx')
1871 call assert_equal(termwin, win_getid())
1872 set langmap=
1873 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001874
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001875 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001876 call assert_equal(3, tabpagenr())
1877 tabprev
1878 call assert_equal(2, tabpagenr())
1879 call assert_equal(termwin, win_getid())
1880
1881 call feedkeys("\<C-L>gT", "xt")
1882 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001883 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001884 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001885 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001886
1887 let job = term_getjob(buf)
1888 call feedkeys("\<C-L>\<C-C>", 'tx')
1889 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001890
1891 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001892 tabnext
1893 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001894 tabprev
1895 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001896endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001897
1898func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001899 CheckUnix
1900
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001901 call writefile([
1902 \ '#!/bin/sh',
1903 \ 'echo "this is standard error" >&2',
1904 \ 'echo "this is standard out" >&1',
1905 \ ], 'Xechoerrout.sh')
1906 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1907
1908 let outfile = 'Xtermstdout'
1909 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001910
1911 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1912 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001913 call assert_equal('this is standard error', term_getline(buf, 1))
1914
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001915 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001916 exe buf . 'bwipe'
1917 call delete('Xechoerrout.sh')
1918 call delete(outfile)
1919endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001920
Bram Moolenaare219f732019-11-30 15:34:08 +01001921func Test_termwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001922 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001923
1924 " Let the terminal output more than 'termwinscroll' lines, some at the start
1925 " will be dropped.
1926 exe 'set termwinscroll=' . &lines
1927 let buf = term_start('/bin/sh')
1928 for i in range(1, &lines)
1929 call feedkeys("echo " . i . "\<CR>", 'xt')
1930 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1931 endfor
1932 " Go to Terminal-Normal mode to update the buffer.
1933 call feedkeys("\<C-W>N", 'xt')
1934 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1935
1936 " Every "echo nr" must only appear once
1937 let lines = getline(1, line('$'))
1938 for i in range(&lines - len(lines) / 2 + 2, &lines)
1939 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1940 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1941 endfor
1942
1943 exe buf . 'bwipe!'
1944endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001945
Bram Moolenaar875cf872018-07-08 20:49:07 +02001946" Resizing the terminal window caused an ml_get error.
1947" TODO: This does not reproduce the original problem.
1948func Test_terminal_resize()
1949 set statusline=x
1950 terminal
1951 call assert_equal(2, winnr('$'))
1952
1953 " Fill the terminal with text.
1954 if has('win32')
1955 call feedkeys("dir\<CR>", 'xt')
1956 else
1957 call feedkeys("ls\<CR>", 'xt')
1958 endif
1959 " Go to Terminal-Normal mode for a moment.
1960 call feedkeys("\<C-W>N", 'xt')
1961 " Open a new window
1962 call feedkeys("i\<C-W>n", 'xt')
1963 call assert_equal(3, winnr('$'))
1964 redraw
1965
1966 close
1967 call assert_equal(2, winnr('$'))
1968 call feedkeys("exit\<CR>", 'xt')
1969 set statusline&
1970endfunc
1971
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001972" must be nearly the last, we can't go back from GUI to terminal
1973func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001974 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001975
1976 " Ignore the "failed to create input context" error.
1977 call test_ignore_error('E285:')
1978
1979 gui -f
1980
1981 call assert_equal(1, winnr('$'))
1982 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001983 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001984 call TermWait(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001985
1986 " closing window wipes out the terminal buffer a with finished job
1987 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1988 call assert_equal("", bufname(buf))
1989
1990 unlet g:job
1991endfunc
1992
1993func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001994 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001995 set guioptions+=!
1996
1997 let filename = 'Xtestscript'
1998 if has('win32')
1999 let filename .= '.bat'
2000 let prefix = ''
2001 let contents = ['@echo off', 'exit %1']
2002 else
2003 let filename .= '.sh'
2004 let prefix = './'
2005 let contents = ['#!/bin/sh', 'exit $1']
2006 endif
2007 call writefile(contents, filename)
2008 call setfperm(filename, 'rwxrwx---')
2009
2010 " Check if v:shell_error is equal to the exit status.
2011 let exitval = 0
2012 execute printf(':!%s%s %d', prefix, filename, exitval)
2013 call assert_equal(exitval, v:shell_error)
2014
2015 let exitval = 9
2016 execute printf(':!%s%s %d', prefix, filename, exitval)
2017 call assert_equal(exitval, v:shell_error)
2018
2019 set guioptions&
2020 call delete(filename)
2021endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002022
2023func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002024 CheckUnix
2025
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002026 term ++hidden cat
2027 let bnr = bufnr('$')
2028 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2029 exe 'sbuf ' . bnr
2030 call assert_equal('terminal', &buftype)
2031 call term_sendkeys(bnr, "asdf\<CR>")
2032 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
2033 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002034 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002035 bwipe!
2036endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002037
Bram Moolenaara4c2a242019-03-25 23:01:38 +01002038func Test_terminal_switch_mode()
2039 term
2040 let bnr = bufnr('$')
2041 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2042 call feedkeys("\<C-W>N", 'xt')
2043 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2044 call feedkeys("A", 'xt')
2045 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaar98f16712020-05-22 13:34:01 +02002046 call feedkeys("\<C-\>\<C-N>", 'xt')
Bram Moolenaara4c2a242019-03-25 23:01:38 +01002047 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2048 call feedkeys("I", 'xt')
2049 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2050 call feedkeys("\<C-W>Nv", 'xt')
2051 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2052 call feedkeys("I", 'xt')
2053 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2054 call feedkeys("\<C-W>Nv", 'xt')
2055 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2056 call feedkeys("A", 'xt')
2057 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2058 bwipe!
2059endfunc
2060
Bram Moolenaara3817732019-10-16 16:31:44 +02002061func Test_terminal_normal_mode()
2062 CheckRunVimInTerminal
2063
2064 " Run Vim in a terminal and open a terminal window to run Vim in.
2065 let lines =<< trim END
2066 call setline(1, range(11111, 11122))
2067 3
2068 END
2069 call writefile(lines, 'XtermNormal')
2070 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002071 call TermWait(buf)
Bram Moolenaara3817732019-10-16 16:31:44 +02002072
2073 call term_sendkeys(buf, "\<C-W>N")
2074 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
2075 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
2076
2077 call term_sendkeys(buf, ":set culopt=number\r")
2078 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
2079
2080 call term_sendkeys(buf, ":set culopt=line\r")
2081 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
2082
Bram Moolenaar98f16712020-05-22 13:34:01 +02002083 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
Bram Moolenaara3817732019-10-16 16:31:44 +02002084 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
2085 call StopVimInTerminal(buf)
2086 call delete('XtermNormal')
2087endfunc
2088
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002089func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002090 CheckUnix
2091
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002092 call assert_equal(1, winnr('$'))
2093 term ++hidden ++close ls
2094 let bnr = bufnr('$')
2095 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2096 call WaitForAssert({-> assert_false(bufexists(bnr))})
2097 call assert_equal(1, winnr('$'))
2098endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002099
2100func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002101 " This test does not pass through ConPTY.
2102 if has('conpty')
2103 return
2104 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002105 let contents = [
2106 \ [ 'One', '', 'X' ],
2107 \ [ 'Two', '', '' ],
2108 \ [ 'Three' ] + repeat([''], 30)
2109 \ ]
2110
2111 for c in contents
2112 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002113 if has('win32')
2114 term cmd /c type Xfile
2115 else
2116 term cat Xfile
2117 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002118 let bnr = bufnr('$')
2119 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2120 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002121 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002122 call assert_equal(c, getline(1, line('$')))
2123 quit
2124 endfor
2125
2126 call delete('Xfile')
2127endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002128
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002129func Test_terminal_no_job()
Bram Moolenaar077ff432019-10-28 00:42:21 +01002130 if has('win32')
2131 let cmd = 'cmd /c ""'
2132 else
2133 CheckExecutable false
2134 let cmd = 'false'
2135 endif
2136 let term = term_start(cmd, {'term_finish': 'close'})
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002137 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2138endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002139
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002140func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002141 CheckUnix
2142
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002143 let buf = Run_shell_in_terminal({})
2144
2145 " Wait for the shell to display a prompt.
2146 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2147
2148 " Hide the cursor.
2149 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2150 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2151
2152 " Show the cursor.
2153 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002154 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002155
2156 " Change color of cursor.
2157 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2158 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2159 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2160 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2161 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2162
2163 " Make cursor a blinking block.
2164 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2165 call WaitForAssert({-> assert_equal([1, 1],
2166 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2167
2168 " Make cursor a steady block.
2169 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2170 call WaitForAssert({-> assert_equal([0, 1],
2171 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2172
2173 " Make cursor a blinking underline.
2174 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2175 call WaitForAssert({-> assert_equal([1, 2],
2176 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2177
2178 " Make cursor a steady underline.
2179 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2180 call WaitForAssert({-> assert_equal([0, 2],
2181 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2182
2183 " Make cursor a blinking vertical bar.
2184 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2185 call WaitForAssert({-> assert_equal([1, 3],
2186 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2187
2188 " Make cursor a steady vertical bar.
2189 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2190 call WaitForAssert({-> assert_equal([0, 3],
2191 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2192
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002193 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002194endfunc
2195
Bram Moolenaar98f16712020-05-22 13:34:01 +02002196" Test for term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002197func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002198 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002199 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002200 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002201 call assert_equal('', term_gettitle(bufnr('$') + 1))
2202
Bram Moolenaar98f16712020-05-22 13:34:01 +02002203 if !has('title') || empty(&t_ts)
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002204 throw "Skipped: can't get/set title"
2205 endif
2206
Bram Moolenaar98f16712020-05-22 13:34:01 +02002207 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002208 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002209 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2210 call term_sendkeys(term, ":e Xfoo\r")
2211 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002212 else
2213 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002214 call term_sendkeys(term, ":e Xfoo\r")
2215 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002216 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002217
Bram Moolenaar1e115362019-01-09 23:01:02 +01002218 call term_sendkeys(term, ":set titlestring=foo\r")
2219 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2220
2221 exe term . 'bwipe!'
2222endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002223
Bram Moolenaar01603a92020-04-03 12:56:17 +02002224func Test_term_gettty()
2225 let buf = Run_shell_in_terminal({})
2226 let gettty = term_gettty(buf)
2227
2228 if has('unix') && executable('tty')
2229 " Find tty using the tty shell command.
2230 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2231 call term_sendkeys(buf, "tty\r")
2232 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
2233 let tty = term_getline(buf, 2)
2234 call assert_equal(tty, gettty)
2235 endif
2236
2237 let gettty0 = term_gettty(buf, 0)
2238 let gettty1 = term_gettty(buf, 1)
2239
2240 call assert_equal(gettty, gettty0)
2241 call assert_equal(job_info(g:job).tty_out, gettty0)
2242 call assert_equal(job_info(g:job).tty_in, gettty1)
2243
2244 if has('unix')
2245 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
2246 " are identical according to :help term_gettty()
2247 call assert_equal(gettty0, gettty1)
2248 call assert_match('^/dev/', gettty)
2249 else
2250 " ConPTY works on anonymous pipe.
2251 if !has('conpty')
2252 call assert_match('^\\\\.\\pipe\\', gettty0)
2253 call assert_match('^\\\\.\\pipe\\', gettty1)
2254 endif
2255 endif
2256
2257 call assert_fails('call term_gettty(buf, 2)', 'E475:')
2258 call assert_fails('call term_gettty(buf, -1)', 'E475:')
2259
2260 call assert_equal('', term_gettty(buf + 1))
2261
2262 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002263 call TermWait(buf)
Bram Moolenaar01603a92020-04-03 12:56:17 +02002264 exe buf . 'bwipe'
2265endfunc
2266
Bram Moolenaar10772302019-01-20 18:25:54 +01002267" When drawing the statusline the cursor position may not have been updated
2268" yet.
2269" 1. create a terminal, make it show 2 lines
2270" 2. 0.5 sec later: leave terminal window, execute "i"
2271" 3. 0.5 sec later: clear terminal window, now it's 1 line
2272" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2273" 4. 0.5 sec later: should be done, clean up
2274func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002275 CheckUnix
2276
Bram Moolenaar10772302019-01-20 18:25:54 +01002277 set statusline=x
2278 terminal
2279 let tbuf = bufnr('')
2280 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2281 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2282 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2283 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2284
2285 sleep 2
2286 exe tbuf . 'bwipe!'
2287 au! BufLeave
2288 set statusline=
2289endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002290
2291func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002292 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002293
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002294 " split, go to the bottom-right window
2295 split
2296 wincmd j
2297 set splitright
2298
2299 call writefile([
2300 \ 'echo getwinpos()',
2301 \ ], 'XTest_getwinpos')
2302 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002303 call TermWait(buf)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002304
2305 " Find the output of getwinpos() in the bottom line.
2306 let rows = term_getsize(buf)[0]
2307 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2308 let line = term_getline(buf, rows)
2309 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2310 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2311
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002312 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002313 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002314 " In the GUI it can be more, let's assume a 20 x 14 cell.
2315 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002316 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002317 let winpos = 50->getwinpos()
2318 call assert_equal(xroot, winpos[0])
2319 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002320 let [winrow, wincol] = win_screenpos('.')
2321 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2322 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2323 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2324 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002325
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002326 call TermWait(buf)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002327 call term_sendkeys(buf, ":q\<CR>")
2328 call StopVimInTerminal(buf)
2329 call delete('XTest_getwinpos')
2330 exe buf . 'bwipe!'
2331 set splitright&
2332 only!
2333endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002334
2335func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002336 " somehow doesn't work on MS-Windows
2337 CheckUnix
2338 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002339
2340 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002341 call writefile(["\<Esc>[?1047h"], 'Xtext')
2342 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002343 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2344
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002345 call writefile(["\<Esc>[?1047l"], 'Xtext')
2346 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002347 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002348
2349 call term_sendkeys(buf, "exit\r")
2350 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002351 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002352endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002353
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002354func Test_terminal_shell_option()
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002355 if has('unix')
2356 " exec is a shell builtin command, should fail without a shell.
2357 term exec ls runtest.vim
2358 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
2359 bwipe!
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002360
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002361 term ++shell exec ls runtest.vim
2362 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2363 bwipe!
2364 elseif has('win32')
2365 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar36ec6f62019-11-05 22:38:47 +01002366 try
2367 term dir /b runtest.vim
2368 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
2369 catch /CreateProcess/
2370 " ignore
2371 endtry
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002372 bwipe!
2373
2374 term ++shell dir /b runtest.vim
2375 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2376 bwipe!
2377 endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002378endfunc
2379
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002380func Test_terminal_setapi_and_call()
Bram Moolenaar21109272020-01-30 16:27:20 +01002381 CheckRunVimInTerminal
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002382
2383 call WriteApiCall('Tapi_TryThis')
2384 call ch_logfile('Xlog', 'w')
2385
2386 unlet! g:called_bufnum
2387 unlet! g:called_arg
2388
Bram Moolenaar21109272020-01-30 16:27:20 +01002389 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002390 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2391 call assert_false(exists('g:called_bufnum'))
2392 call assert_false(exists('g:called_arg'))
2393
Bram Moolenaar21109272020-01-30 16:27:20 +01002394 eval buf->term_setapi('Tapi_')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002395 call term_sendkeys(buf, ":set notitle\<CR>")
2396 call term_sendkeys(buf, ":source Xscript\<CR>")
2397 call WaitFor({-> exists('g:called_bufnum')})
2398 call assert_equal(buf, g:called_bufnum)
2399 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar21109272020-01-30 16:27:20 +01002400
2401 call StopVimInTerminal(buf)
2402
2403 call delete('Xscript')
2404 call ch_logfile('')
2405 call delete('Xlog')
2406 unlet! g:called_bufnum
2407 unlet! g:called_arg
2408endfunc
2409
2410func Test_terminal_api_arg()
2411 CheckRunVimInTerminal
2412
2413 call WriteApiCall('Tapi_TryThis')
2414 call ch_logfile('Xlog', 'w')
2415
2416 unlet! g:called_bufnum
2417 unlet! g:called_arg
2418
2419 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2420 let buf = bufnr('%')
2421 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2422 call assert_false(exists('g:called_bufnum'))
2423 call assert_false(exists('g:called_arg'))
2424
2425 call StopVimInTerminal(buf)
2426
2427 call ch_logfile('Xlog', 'w')
2428
2429 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2430 let buf = bufnr('%')
2431 call WaitFor({-> exists('g:called_bufnum')})
2432 call assert_equal(buf, g:called_bufnum)
2433 call assert_equal(['hello', 123], g:called_arg)
2434
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002435 call StopVimInTerminal(buf)
2436
2437 call delete('Xscript')
2438 call ch_logfile('')
2439 call delete('Xlog')
2440 unlet! g:called_bufnum
2441 unlet! g:called_arg
2442endfunc
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002443
Bram Moolenaar9b9be002020-03-22 14:41:22 +01002444func Test_terminal_invalid_arg()
2445 call assert_fails('terminal ++xyz', 'E181:')
2446endfunc
2447
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002448func Test_terminal_in_popup()
2449 CheckRunVimInTerminal
2450
2451 let text =<< trim END
2452 some text
2453 to edit
2454 in a popup window
2455 END
2456 call writefile(text, 'Xtext')
Bram Moolenaar3180fe62020-02-02 13:47:06 +01002457 let cmd = GetVimCommandCleanTerm()
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002458 let lines = [
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01002459 \ 'set t_u7=',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002460 \ 'call setline(1, range(20))',
2461 \ 'hi PopTerm ctermbg=grey',
2462 \ 'func OpenTerm(setColor)',
Bram Moolenaar353c3512020-03-15 14:19:26 +01002463 \ " set noruler",
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002464 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
Bram Moolenaar57c33952020-02-29 16:09:16 +01002465 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002466 \ ' if a:setColor',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002467 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002468 \ ' endif',
2469 \ 'endfunc',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002470 \ 'func HidePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002471 \ ' call popup_hide(g:winid)',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002472 \ 'endfunc',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002473 \ 'func ClosePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002474 \ ' call popup_close(g:winid)',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002475 \ 'endfunc',
2476 \ 'func ReopenPopup()',
2477 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
2478 \ 'endfunc',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002479 \ ]
2480 call writefile(lines, 'XtermPopup')
2481 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002482 call TermWait(buf, 100)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002483 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002484 call TermWait(buf, 100)
Bram Moolenaarc2adc002020-02-14 17:05:18 +01002485 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002486 call TermWait(buf, 100)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002487 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002488 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
2489
2490 call term_sendkeys(buf, ":q\<CR>")
2491 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
2492
2493 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002494 call TermWait(buf, 150)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002495 call term_sendkeys(buf, ":set hlsearch\<CR>")
2496 call term_sendkeys(buf, "/edit\<CR>")
2497 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
2498
Bram Moolenaar1af5ce02020-02-06 11:54:35 +01002499 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
2500 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
2501 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002502 call TermWait(buf, 50)
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002503
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002504 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
2505 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
2506
2507 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
2508 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002509
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002510 " Go to terminal-Normal mode and visually select text.
2511 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
2512 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
2513
2514 " Back to job mode, redraws
2515 call term_sendkeys(buf, "A")
2516 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
2517
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002518 call TermWait(buf, 50)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002519 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002520 call TermWait(buf, 100) " wait for terminal to vanish
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002521
2522 call StopVimInTerminal(buf)
Bram Moolenaar19398262020-03-14 15:28:08 +01002523 call delete('Xtext')
2524 call delete('XtermPopup')
2525endfunc
2526
2527" Check a terminal in popup window uses the default mininum size.
2528func Test_terminal_in_popup_min_size()
2529 CheckRunVimInTerminal
2530
2531 let text =<< trim END
2532 another text
2533 to show
2534 in a popup window
2535 END
2536 call writefile(text, 'Xtext')
2537 let lines = [
2538 \ 'set t_u7=',
2539 \ 'call setline(1, range(20))',
Bram Moolenaar19398262020-03-14 15:28:08 +01002540 \ 'func OpenTerm()',
2541 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
2542 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2543 \ 'endfunc',
2544 \ ]
2545 call writefile(lines, 'XtermPopup')
2546 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002547 call TermWait(buf, 100)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002548 call term_sendkeys(buf, ":set noruler\<CR>")
Bram Moolenaar19398262020-03-14 15:28:08 +01002549 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002550 call TermWait(buf, 50)
Bram Moolenaar19398262020-03-14 15:28:08 +01002551 call term_sendkeys(buf, ":\<CR>")
2552 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
2553
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002554 call TermWait(buf, 50)
Bram Moolenaar19398262020-03-14 15:28:08 +01002555 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002556 call TermWait(buf, 50) " wait for terminal to vanish
Bram Moolenaar19398262020-03-14 15:28:08 +01002557 call StopVimInTerminal(buf)
2558 call delete('Xtext')
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002559 call delete('XtermPopup')
2560endfunc
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002561
Bram Moolenaar83d47902020-03-26 20:34:00 +01002562" Check a terminal in popup window with different colors
2563func Terminal_in_popup_colored(group_name, highlight_cmd, highlight_opt)
2564 CheckRunVimInTerminal
2565 CheckUnix
2566
2567 let lines = [
2568 \ 'set t_u7=',
2569 \ 'call setline(1, range(20))',
2570 \ 'func OpenTerm()',
2571 \ " let s:buf = term_start('cat', #{hidden: 1, "
2572 \ .. a:highlight_opt .. "})",
2573 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2574 \ 'endfunc',
2575 \ a:highlight_cmd,
2576 \ ]
2577 call writefile(lines, 'XtermPopup')
2578 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002579 call TermWait(buf, 100)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002580 call term_sendkeys(buf, ":set noruler\<CR>")
2581 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002582 call TermWait(buf, 50)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002583 call term_sendkeys(buf, "hello\<CR>")
2584 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
2585
2586 call term_sendkeys(buf, "\<C-D>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002587 call TermWait(buf, 50)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002588 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002589 call TermWait(buf, 50) " wait for terminal to vanish
Bram Moolenaar83d47902020-03-26 20:34:00 +01002590 call StopVimInTerminal(buf)
2591 call delete('XtermPopup')
2592endfunc
2593
2594func Test_terminal_in_popup_colored_Terminal()
2595 call Terminal_in_popup_colored("Terminal", "highlight Terminal ctermfg=blue ctermbg=yellow", "")
2596endfunc
2597
2598func Test_terminal_in_popup_colored_group()
2599 call Terminal_in_popup_colored("MyTermCol", "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue", "term_highlight: 'MyTermCol',")
2600endfunc
2601
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002602func Test_double_popup_terminal()
2603 let buf1 = term_start(&shell, #{hidden: 1})
2604 let win1 = popup_create(buf1, {})
2605 let buf2 = term_start(&shell, #{hidden: 1})
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002606 call assert_fails('call popup_create(buf2, {})', 'E861:')
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002607 call popup_close(win1)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002608 exe buf1 .. 'bwipe!'
2609 exe buf2 .. 'bwipe!'
2610endfunc
2611
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002612func Test_issue_5607()
2613 let wincount = winnr('$')
2614 exe 'terminal' &shell &shellcmdflag 'exit'
2615 let job = term_getjob(bufnr())
2616 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2617
2618 let old_wincolor = &wincolor
2619 try
2620 set wincolor=
2621 finally
2622 let &wincolor = old_wincolor
2623 bw!
2624 endtry
2625endfunc
Bram Moolenaare010c722020-02-24 21:37:54 +01002626
2627func Test_hidden_terminal()
2628 let buf = term_start(&shell, #{hidden: 1})
2629 call assert_equal('', bufname('^$'))
2630 call StopShellInTerminal(buf)
2631endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002632
2633func Test_term_nasty_callback()
Bram Moolenaara161cb52020-04-30 19:09:35 +02002634 CheckExecutable sh
Bram Moolenaarcee52202020-03-11 14:19:58 +01002635
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002636 set hidden
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002637 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002638 call popup_create(g:buf0, {})
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002639 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
2640
2641 call popup_clear(1)
Bram Moolenaarcee52202020-03-11 14:19:58 +01002642 set hidden&
2643endfunc
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002644
Bram Moolenaareb58a242020-04-19 18:13:19 +02002645func Test_term_and_startinsert()
2646 CheckRunVimInTerminal
2647 CheckUnix
2648
2649 let lines =<< trim EOL
2650 put='some text'
2651 term
2652 startinsert
2653 EOL
2654 call writefile(lines, 'XTest_startinsert')
2655 let buf = RunVimInTerminal('-S XTest_startinsert', {})
2656
2657 call term_sendkeys(buf, "exit\r")
2658 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
2659 call term_sendkeys(buf, "0l")
2660 call term_sendkeys(buf, "A<\<Esc>")
2661 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
2662
2663 call StopVimInTerminal(buf)
2664 call delete('XTest_startinsert')
2665endfunc
2666
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002667" Test for passing invalid arguments to terminal functions
2668func Test_term_func_invalid_arg()
2669 call assert_fails('let b = term_getaltscreen([])', 'E745:')
2670 call assert_fails('let p = term_getansicolors([])', 'E745:')
2671 call assert_fails('let a = term_getattr(1, [])', 'E730:')
2672 call assert_fails('let c = term_getcursor([])', 'E745:')
2673 call assert_fails('let l = term_getline([], 1)', 'E745:')
2674 call assert_fails('let l = term_getscrolled([])', 'E745:')
2675 call assert_fails('let s = term_getsize([])', 'E745:')
2676 call assert_fails('let s = term_getstatus([])', 'E745:')
2677 call assert_fails('let s = term_scrape([], 1)', 'E745:')
2678 call assert_fails('call term_sendkeys([], "a")', 'E745:')
2679 call assert_fails('call term_setansicolors([], [])', 'E745:')
2680 call assert_fails('call term_setapi([], "")', 'E745:')
2681 call assert_fails('call term_setrestore([], "")', 'E745:')
2682 call assert_fails('call term_setkill([], "")', 'E745:')
2683endfunc
2684
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002685" Test for sending various special keycodes to a terminal
2686func Test_term_keycode_translation()
2687 CheckRunVimInTerminal
2688
2689 let buf = RunVimInTerminal('', {})
2690 call term_sendkeys(buf, ":set nocompatible\<CR>")
2691
2692 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
2693 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
2694 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
2695 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
2696 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
2697 \ "\<S-Down>"]
2698 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
2699 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
2700 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
2701 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
Bram Moolenaarfe813892020-05-21 20:38:31 +02002702 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002703
Bram Moolenaarfe813892020-05-21 20:38:31 +02002704 call term_sendkeys(buf, "i")
2705 for i in range(len(keys))
2706 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
2707 call term_wait(buf)
2708 call assert_equal(output[i], term_getline(buf, 1))
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002709 endfor
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002710
Bram Moolenaarfe813892020-05-21 20:38:31 +02002711 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
2712 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
2713 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
2714 let keypad_output = ['0', '1', '2', '3', '4', '5',
2715 \ '6', '7', '8', '9', '.', '+',
2716 \ '-', '*', '/']
2717 for i in range(len(keypad_keys))
2718 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
2719 if keypad_output[i] == '3' || keypad_output[i] == '9'
2720 continue
2721 endif
2722 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
2723 call term_wait(buf)
2724 call assert_equal(keypad_output[i], term_getline(buf, 1))
2725 endfor
2726
2727 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002728 call term_wait(buf)
Bram Moolenaarfe813892020-05-21 20:38:31 +02002729 call assert_equal('two', term_getline(buf, 1))
2730
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002731 call StopVimInTerminal(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002732endfunc
2733
2734" Test for using the mouse in a terminal
2735func Test_term_mouse()
2736 CheckNotGui
2737 CheckRunVimInTerminal
2738
2739 let save_mouse = &mouse
2740 let save_term = &term
2741 let save_ttymouse = &ttymouse
2742 let save_clipboard = &clipboard
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002743 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
2744
2745 let lines =<< trim END
2746 one two three four five
2747 red green yellow red blue
2748 vim emacs sublime nano
2749 END
2750 call writefile(lines, 'Xtest_mouse')
2751
Bram Moolenaar98f16712020-05-22 13:34:01 +02002752 " Create a terminal window running Vim for the test with mouse enabled
2753 let prev_win = win_getid()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002754 let buf = RunVimInTerminal('Xtest_mouse -n', {})
2755 call term_sendkeys(buf, ":set nocompatible\<CR>")
2756 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
2757 call term_sendkeys(buf, ":set clipboard=\<CR>")
2758 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
2759 call term_wait(buf)
2760 redraw!
2761
Bram Moolenaar98f16712020-05-22 13:34:01 +02002762 " Use the mouse to enter the terminal window
2763 call win_gotoid(prev_win)
2764 call feedkeys(MouseLeftClickCode(1, 1), 'x')
2765 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
2766 call assert_equal(1, getwininfo(win_getid())[0].terminal)
2767
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002768 " Test for <LeftMouse> click/release
2769 call test_setmouse(2, 5)
2770 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
2771 call test_setmouse(3, 8)
2772 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
2773 call term_wait(buf, 50)
2774 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
2775 call term_wait(buf, 50)
2776 let pos = json_decode(readfile('Xbuf')[0])
2777 call assert_equal([3, 8], pos[1:2])
2778
2779 " Test for selecting text using mouse
2780 call delete('Xbuf')
2781 call test_setmouse(2, 11)
2782 call term_sendkeys(buf, "\<LeftMouse>")
2783 call test_setmouse(2, 16)
2784 call term_sendkeys(buf, "\<LeftRelease>y")
2785 call term_wait(buf, 50)
2786 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2787 call term_wait(buf, 50)
2788 call assert_equal('yellow', readfile('Xbuf')[0])
2789
2790 " Test for selecting text using doubleclick
2791 call delete('Xbuf')
2792 call test_setmouse(1, 11)
2793 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
2794 call test_setmouse(1, 17)
2795 call term_sendkeys(buf, "\<LeftRelease>y")
2796 call term_wait(buf, 50)
2797 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2798 call term_wait(buf, 50)
2799 call assert_equal('three four', readfile('Xbuf')[0])
2800
2801 " Test for selecting a line using triple click
2802 call delete('Xbuf')
2803 call test_setmouse(3, 2)
2804 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
2805 call term_wait(buf, 50)
2806 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2807 call term_wait(buf, 50)
2808 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
2809
2810 " Test for selecting a block using qudraple click
2811 call delete('Xbuf')
2812 call test_setmouse(1, 11)
2813 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
2814 call test_setmouse(3, 13)
2815 call term_sendkeys(buf, "\<LeftRelease>y")
2816 call term_wait(buf, 50)
2817 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2818 call term_wait(buf, 50)
2819 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
2820
2821 " Test for extending a selection using right click
2822 call delete('Xbuf')
2823 call test_setmouse(2, 9)
2824 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
2825 call test_setmouse(2, 16)
2826 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
2827 call term_wait(buf, 50)
2828 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2829 call term_wait(buf, 50)
2830 call assert_equal("n yellow", readfile('Xbuf')[0])
2831
2832 " Test for pasting text using middle click
2833 call delete('Xbuf')
2834 call term_sendkeys(buf, ":let @r='bright '\<CR>")
2835 call test_setmouse(2, 22)
2836 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
2837 call term_wait(buf, 50)
2838 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
2839 call term_wait(buf, 50)
2840 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
2841
2842 " cleanup
2843 call term_wait(buf)
2844 call StopVimInTerminal(buf)
2845 let &mouse = save_mouse
2846 let &term = save_term
2847 let &ttymouse = save_ttymouse
2848 let &clipboard = save_clipboard
2849 set mousetime&
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002850 call delete('Xtest_mouse')
2851 call delete('Xbuf')
2852endfunc
2853
2854" Test for modeless selection in a terminal
2855func Test_term_modeless_selection()
2856 CheckUnix
2857 CheckNotGui
2858 CheckRunVimInTerminal
2859 CheckFeature clipboard_working
2860
2861 let save_mouse = &mouse
2862 let save_term = &term
2863 let save_ttymouse = &ttymouse
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002864 set mouse=a term=xterm ttymouse=sgr mousetime=200
2865 set clipboard=autoselectml
2866
2867 let lines =<< trim END
2868 one two three four five
2869 red green yellow red blue
2870 vim emacs sublime nano
2871 END
2872 call writefile(lines, 'Xtest_modeless')
2873
Bram Moolenaar98f16712020-05-22 13:34:01 +02002874 " Create a terminal window running Vim for the test with mouse disabled
2875 let prev_win = win_getid()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002876 let buf = RunVimInTerminal('Xtest_modeless -n', {})
2877 call term_sendkeys(buf, ":set nocompatible\<CR>")
2878 call term_sendkeys(buf, ":set mouse=\<CR>")
2879 call term_wait(buf)
2880 redraw!
2881
Bram Moolenaar98f16712020-05-22 13:34:01 +02002882 " Use the mouse to enter the terminal window
2883 call win_gotoid(prev_win)
2884 call feedkeys(MouseLeftClickCode(1, 1), 'x')
2885 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
2886 call term_wait(buf)
2887 call assert_equal(1, getwininfo(win_getid())[0].terminal)
2888
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002889 " Test for copying a modeless selection to clipboard
2890 let @* = 'clean'
2891 " communicating with X server may take a little time
2892 sleep 100m
2893 call feedkeys(MouseLeftClickCode(2, 3), 'x')
2894 call feedkeys(MouseLeftDragCode(2, 11), 'x')
2895 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
2896 call assert_equal("d green y", @*)
2897
2898 " cleanup
2899 call term_wait(buf)
2900 call StopVimInTerminal(buf)
2901 let &mouse = save_mouse
2902 let &term = save_term
2903 let &ttymouse = save_ttymouse
2904 set mousetime& clipboard&
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002905 call delete('Xtest_modeless')
Bram Moolenaar98f16712020-05-22 13:34:01 +02002906 new | only!
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002907endfunc
2908
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002909" vim: shiftwidth=2 sts=2 expandtab