blob: 7f85f6cf10fdcfef0eeb80147dd0dd28cdd4dad0 [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:')
1223 call StopVimInTerminal(buf)
1224 call term_wait(buf)
1225 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1226 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1227 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1228endfunc
1229
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001230" just testing basic functionality.
1231func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001232 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001233 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001234 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001235 call assert_equal(2, winnr('$'))
1236 call assert_equal(20, line('$'))
1237 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001238
1239 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001240 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001241 call assert_equal(buf, buf2)
1242 call assert_notequal('one two three four five', trim(getline(1)))
1243
1244 " Load the first dump again in the same window
1245 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1246 call assert_equal(buf, buf2)
1247 call Check_dump01(0)
1248
1249 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1250 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1251 new
1252 let closedbuf = winbufnr('')
1253 quit
1254 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001255 call assert_fails('call term_dumpload([])', 'E474:')
1256 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001257
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001258 quit
1259endfunc
1260
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001261func Test_terminal_dumpload_dump()
1262 CheckRunVimInTerminal
1263
1264 let lines =<< trim END
1265 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1266 END
1267 call writefile(lines, 'XtermDumpload')
1268 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1269 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1270
1271 call StopVimInTerminal(buf)
1272 call delete('XtermDumpload')
1273endfunc
1274
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001275func Test_terminal_dumpdiff()
1276 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001277 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001278 call assert_equal(2, winnr('$'))
1279 call assert_equal(62, line('$'))
1280 call Check_dump01(0)
1281 call Check_dump01(42)
1282 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1283 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001284
1285 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E474:')
1286 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1287 call writefile([], 'X1.dump')
1288 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1289 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001290endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001291
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001292func Test_terminal_dumpdiff_swap()
1293 call assert_equal(1, winnr('$'))
1294 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1295 call assert_equal(2, winnr('$'))
1296 call assert_equal(62, line('$'))
1297 call assert_match('Test_popup_command_01.dump', getline(21))
1298 call assert_match('Test_popup_command_03.dump', getline(42))
1299 call assert_match('Undo', getline(3))
1300 call assert_match('three four five', getline(45))
1301
1302 normal s
1303 call assert_match('Test_popup_command_03.dump', getline(21))
1304 call assert_match('Test_popup_command_01.dump', getline(42))
1305 call assert_match('three four five', getline(3))
1306 call assert_match('Undo', getline(45))
1307 quit
1308endfunc
1309
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001310func Test_terminal_dumpdiff_options()
1311 set laststatus=0
1312 call assert_equal(1, winnr('$'))
1313 let height = winheight(0)
1314 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1315 call assert_equal(2, winnr('$'))
1316 call assert_equal(height, winheight(winnr()))
1317 call assert_equal(33, winwidth(winnr()))
1318 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1319 quit
1320
1321 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001322 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1323 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001324 call assert_equal(&columns, winwidth(0))
1325 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001326 call assert_equal('something else', bufname('%'))
1327 quit
1328
1329 call assert_equal(1, winnr('$'))
1330 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1331 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001332 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 +01001333 bwipe
1334
1335 set laststatus&
1336endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001337
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001338func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001339 call assert_equal(1, winnr('$'))
1340
1341 " Use the title termcap entries to output the escape sequence.
1342 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001343 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001344 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001345 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001346 \ 'redraw',
1347 \ "set t_ts=",
1348 \ ], 'Xscript')
1349 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001350 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001351 call assert_equal('Xtextfile', expand('%:t'))
1352 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001353 return buf
1354endfunc
1355
1356func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001357 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001358 let buf = Api_drop_common('')
1359 call assert_equal(0, &bin)
1360 call assert_equal('', &fenc)
1361
1362 call StopVimInTerminal(buf)
1363 call delete('Xscript')
1364 bwipe Xtextfile
1365endfunc
1366
1367func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001368 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001369 let buf = Api_drop_common(',{"bin":1}')
1370 call assert_equal(1, &bin)
1371
1372 call StopVimInTerminal(buf)
1373 call delete('Xscript')
1374 bwipe Xtextfile
1375endfunc
1376
1377func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001378 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001379 let buf = Api_drop_common(',{"binary":1}')
1380 call assert_equal(1, &bin)
1381
1382 call StopVimInTerminal(buf)
1383 call delete('Xscript')
1384 bwipe Xtextfile
1385endfunc
1386
1387func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001388 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001389 set binary
1390 let buf = Api_drop_common(',{"nobin":1}')
1391 call assert_equal(0, &bin)
1392
1393 call StopVimInTerminal(buf)
1394 call delete('Xscript')
1395 bwipe Xtextfile
1396 set nobinary
1397endfunc
1398
1399func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001400 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001401 set binary
1402 let buf = Api_drop_common(',{"nobinary":1}')
1403 call assert_equal(0, &bin)
1404
1405 call StopVimInTerminal(buf)
1406 call delete('Xscript')
1407 bwipe Xtextfile
1408 set nobinary
1409endfunc
1410
1411func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001412 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001413 let buf = Api_drop_common(',{"ff":"dos"}')
1414 call assert_equal("dos", &ff)
1415
1416 call StopVimInTerminal(buf)
1417 call delete('Xscript')
1418 bwipe Xtextfile
1419endfunc
1420
1421func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001422 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001423 let buf = Api_drop_common(',{"fileformat":"dos"}')
1424 call assert_equal("dos", &ff)
1425
1426 call StopVimInTerminal(buf)
1427 call delete('Xscript')
1428 bwipe Xtextfile
1429endfunc
1430
1431func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001432 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001433 let buf = Api_drop_common(',{"enc":"utf-16"}')
1434 call assert_equal("utf-16", &fenc)
1435
1436 call StopVimInTerminal(buf)
1437 call delete('Xscript')
1438 bwipe Xtextfile
1439endfunc
1440
1441func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001442 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001443 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1444 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001445
1446 call StopVimInTerminal(buf)
1447 call delete('Xscript')
1448 bwipe Xtextfile
1449endfunc
1450
1451func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001452 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001453 let firstwinid = win_getid()
1454 split Xtextfile
1455 let textfile_winid = win_getid()
1456 call assert_equal(2, winnr('$'))
1457 call win_gotoid(firstwinid)
1458
1459 " Use the title termcap entries to output the escape sequence.
1460 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001461 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001462 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1463 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1464 \ 'redraw',
1465 \ "set t_ts=",
1466 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001467 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001468 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001469 call assert_equal(textfile_winid, win_getid())
1470
1471 call StopVimInTerminal(buf)
1472 call delete('Xscript')
1473 bwipe Xtextfile
1474endfunc
1475
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001476func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001477 let g:called_bufnum = a:bufnum
1478 let g:called_arg = a:arg
1479endfunc
1480
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001481func WriteApiCall(funcname)
1482 " Use the title termcap entries to output the escape sequence.
1483 call writefile([
1484 \ 'set title',
1485 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1486 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1487 \ 'redraw',
1488 \ "set t_ts=",
1489 \ ], 'Xscript')
1490endfunc
1491
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001492func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001493 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001494
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001495 unlet! g:called_bufnum
1496 unlet! g:called_arg
1497
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001498 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001499
1500 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001501 let buf = RunVimInTerminal('-S Xscript', {})
1502 call WaitFor({-> exists('g:called_bufnum')})
1503 call assert_equal(buf, g:called_bufnum)
1504 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001505 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001506
1507 unlet! g:called_bufnum
1508 unlet! g:called_arg
1509
1510 " Enable explicitly
1511 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1512 call WaitFor({-> exists('g:called_bufnum')})
1513 call assert_equal(buf, g:called_bufnum)
1514 call assert_equal(['hello', 123], g:called_arg)
1515 call StopVimInTerminal(buf)
1516
1517 unlet! g:called_bufnum
1518 unlet! g:called_arg
1519
1520 func! ApiCall_TryThis(bufnum, arg)
1521 let g:called_bufnum2 = a:bufnum
1522 let g:called_arg2 = a:arg
1523 endfunc
1524
1525 call WriteApiCall('ApiCall_TryThis')
1526
1527 " Use prefix match
1528 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1529 call WaitFor({-> exists('g:called_bufnum2')})
1530 call assert_equal(buf, g:called_bufnum2)
1531 call assert_equal(['hello', 123], g:called_arg2)
1532 call StopVimInTerminal(buf)
1533
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001534 call assert_fails("call term_start('ls', {'term_api' : []})", 'E475:')
1535
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001536 unlet! g:called_bufnum2
1537 unlet! g:called_arg2
1538
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001539 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001540 delfunction! ApiCall_TryThis
1541 unlet! g:called_bufnum2
1542 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001543endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001544
1545func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001546 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001547
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001548 func! TryThis(bufnum, arg)
1549 let g:called_bufnum3 = a:bufnum
1550 let g:called_arg3 = a:arg
1551 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001552
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001553 call WriteApiCall('TryThis')
1554
1555 unlet! g:called_bufnum3
1556 unlet! g:called_arg3
1557
1558 " Not permitted
1559 call ch_logfile('Xlog', 'w')
1560 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1561 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1562 call assert_false(exists('g:called_bufnum3'))
1563 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001564 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001565
1566 " No match
1567 call ch_logfile('Xlog', 'w')
1568 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1569 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1570 call assert_false(exists('g:called_bufnum3'))
1571 call assert_false(exists('g:called_arg3'))
1572 call StopVimInTerminal(buf)
1573
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001574 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001575 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001576 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001577 delfunction! TryThis
1578 unlet! g:called_bufnum3
1579 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001580endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001581
Bram Moolenaara997b452018-04-17 23:24:06 +02001582let s:caught_e937 = 0
1583
1584func Tapi_Delete(bufnum, arg)
1585 try
1586 execute 'bdelete!' a:bufnum
1587 catch /E937:/
1588 let s:caught_e937 = 1
1589 endtry
1590endfunc
1591
1592func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001593 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001594
1595 call WriteApiCall('Tapi_Delete')
1596 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001597 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001598
1599 call StopVimInTerminal(buf)
1600 call delete('Xscript')
1601 call ch_logfile('', '')
1602endfunc
1603
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001604func Test_terminal_ansicolors_default()
Bram Moolenaara161cb52020-04-30 19:09:35 +02001605 CheckFunction term_getansicolors
1606
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001607 let colors = [
1608 \ '#000000', '#e00000',
1609 \ '#00e000', '#e0e000',
1610 \ '#0000e0', '#e000e0',
1611 \ '#00e0e0', '#e0e0e0',
1612 \ '#808080', '#ff4040',
1613 \ '#40ff40', '#ffff40',
1614 \ '#4040ff', '#ff40ff',
1615 \ '#40ffff', '#ffffff',
1616 \]
1617
1618 let buf = Run_shell_in_terminal({})
1619 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001620 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001621 call TermWait(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001622
1623 exe buf . 'bwipe'
1624endfunc
1625
1626let s:test_colors = [
1627 \ '#616e64', '#0d0a79',
1628 \ '#6d610d', '#0a7373',
1629 \ '#690d0a', '#6d696e',
1630 \ '#0d0a6f', '#616e0d',
1631 \ '#0a6479', '#6d0d0a',
1632 \ '#617373', '#0d0a69',
1633 \ '#6d690d', '#0a6e6f',
1634 \ '#610d0a', '#6e6479',
1635 \]
1636
1637func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001638 CheckFeature termguicolors
Bram Moolenaara161cb52020-04-30 19:09:35 +02001639 CheckFunction term_getansicolors
1640
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001641 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1642 let buf = Run_shell_in_terminal({})
1643 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001644 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001645 call TermWait(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001646
1647 exe buf . 'bwipe'
1648 unlet g:terminal_ansi_colors
1649endfunc
1650
1651func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001652 CheckFeature termguicolors
Bram Moolenaara161cb52020-04-30 19:09:35 +02001653 CheckFunction term_getansicolors
1654
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001655 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1656 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1657 call assert_equal(s:test_colors, term_getansicolors(buf))
1658
1659 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001660 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001661
1662 let colors = [
1663 \ 'ivory', 'AliceBlue',
1664 \ 'grey67', 'dark goldenrod',
1665 \ 'SteelBlue3', 'PaleVioletRed4',
1666 \ 'MediumPurple2', 'yellow2',
1667 \ 'RosyBrown3', 'OrangeRed2',
1668 \ 'white smoke', 'navy blue',
1669 \ 'grey47', 'gray97',
1670 \ 'MistyRose2', 'DodgerBlue4',
1671 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001672 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001673
1674 let colors[4] = 'Invalid'
1675 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1676
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001677 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001678 call TermWait(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001679 exe buf . 'bwipe'
1680endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001681
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001682func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001683 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001684
1685 " Use all the ANSI colors.
1686 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001687 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001688 \ 'hi Tblack ctermfg=0 ctermbg=8',
1689 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1690 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1691 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1692 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1693 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1694 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1695 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1696 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1697 \ 'hi Tred ctermfg=9 ctermbg=1',
1698 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1699 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1700 \ 'hi Tblue ctermfg=12 ctermbg=4',
1701 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1702 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1703 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001704 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1705 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1706 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001707 \ '',
1708 \ 'call matchadd("Tblack", "A")',
1709 \ 'call matchadd("Tdarkred", "B")',
1710 \ 'call matchadd("Tdarkgreen", "C")',
1711 \ 'call matchadd("Tbrown", "D")',
1712 \ 'call matchadd("Tdarkblue", "E")',
1713 \ 'call matchadd("Tdarkmagenta", "F")',
1714 \ 'call matchadd("Tdarkcyan", "G")',
1715 \ 'call matchadd("Tlightgrey", "H")',
1716 \ 'call matchadd("Tdarkgrey", "I")',
1717 \ 'call matchadd("Tred", "J")',
1718 \ 'call matchadd("Tgreen", "K")',
1719 \ 'call matchadd("Tyellow", "L")',
1720 \ 'call matchadd("Tblue", "M")',
1721 \ 'call matchadd("Tmagenta", "N")',
1722 \ 'call matchadd("Tcyan", "O")',
1723 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001724 \ 'call matchadd("TdarkredBold", "X")',
1725 \ 'call matchadd("TgreenBold", "Y")',
1726 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001727 \ 'redraw',
1728 \ ], 'Xcolorscript')
1729 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1730 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1731
1732 call term_sendkeys(buf, ":q\<CR>")
1733 call StopVimInTerminal(buf)
1734 call delete('Xcolorscript')
1735endfunc
1736
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001737func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001738 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001739 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001740 let text = []
1741 for n in range(10)
1742 call add(text, repeat(n, 50))
1743 endfor
1744 call writefile(text, 'Xwinsize')
1745 let buf = RunVimInTerminal('Xwinsize', {})
1746 let win = bufwinid(buf)
1747 call assert_equal([6, 40], term_getsize(buf))
1748 call assert_equal(6, winheight(win))
1749 call assert_equal(40, winwidth(win))
1750
1751 " resizing the window doesn't resize the terminal.
1752 resize 10
1753 vertical resize 60
1754 call assert_equal([6, 40], term_getsize(buf))
1755 call assert_equal(10, winheight(win))
1756 call assert_equal(60, winwidth(win))
1757
1758 call StopVimInTerminal(buf)
1759 call delete('Xwinsize')
1760
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001761 call assert_fails('set termwinsize=40', 'E474')
1762 call assert_fails('set termwinsize=10+40', 'E474')
1763 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001764
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001765 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001766endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001767
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001768func Test_terminal_termwinsize_option_zero()
1769 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001770 let buf = Run_shell_in_terminal({})
1771 let win = bufwinid(buf)
1772 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001773 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001774 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001775 exe buf . 'bwipe'
1776
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001777 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001778 let buf = Run_shell_in_terminal({})
1779 let win = bufwinid(buf)
1780 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001781 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001782 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001783 exe buf . 'bwipe'
1784
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001785 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001786 let buf = Run_shell_in_terminal({})
1787 let win = bufwinid(buf)
1788 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001789 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001790 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001791 exe buf . 'bwipe'
1792
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001793 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001794endfunc
1795
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001796func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001797 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001798 vsplit
1799 let buf = Run_shell_in_terminal({})
1800 let win = bufwinid(buf)
1801 call assert_inrange(10, 1000, winheight(win))
1802 call assert_inrange(50, 1000, winwidth(win))
1803 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1804
1805 resize 15
1806 vertical resize 60
1807 redraw
1808 call assert_equal([15, 60], term_getsize(buf))
1809 call assert_equal(15, winheight(win))
1810 call assert_equal(60, winwidth(win))
1811
1812 resize 7
1813 vertical resize 30
1814 redraw
1815 call assert_equal([10, 50], term_getsize(buf))
1816 call assert_equal(7, winheight(win))
1817 call assert_equal(30, winwidth(win))
1818
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001819 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001820 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001821 exe buf . 'bwipe'
1822
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001823 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001824 let buf = Run_shell_in_terminal({})
1825 let win = bufwinid(buf)
1826 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001827 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001828 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001829 exe buf . 'bwipe'
1830
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001831 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001832endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001833
1834func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001835 " make three tabpages, terminal in the middle
1836 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001837 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001838 tabnew
1839 tabprev
1840 call assert_equal(1, winnr('$'))
1841 call assert_equal(2, tabpagenr())
1842 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001843
1844 let buf = Run_shell_in_terminal({})
1845 let termwin = bufwinid(buf)
1846 set termwinkey=<C-L>
1847 call feedkeys("\<C-L>w", 'tx')
1848 call assert_equal(thiswin, win_getid())
1849 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001850 call assert_equal(termwin, win_getid())
1851
Bram Moolenaar997d4242019-09-14 21:23:40 +02001852 if has('langmap')
1853 set langmap=xjyk
1854 call feedkeys("\<C-L>x", 'tx')
1855 call assert_equal(thiswin, win_getid())
1856 call feedkeys("\<C-W>y", 'tx')
1857 call assert_equal(termwin, win_getid())
1858 set langmap=
1859 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001860
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001861 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001862 call assert_equal(3, tabpagenr())
1863 tabprev
1864 call assert_equal(2, tabpagenr())
1865 call assert_equal(termwin, win_getid())
1866
1867 call feedkeys("\<C-L>gT", "xt")
1868 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001869 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001870 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001871 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001872
1873 let job = term_getjob(buf)
1874 call feedkeys("\<C-L>\<C-C>", 'tx')
1875 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001876
1877 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001878 tabnext
1879 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001880 tabprev
1881 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001882endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001883
1884func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001885 CheckUnix
1886
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001887 call writefile([
1888 \ '#!/bin/sh',
1889 \ 'echo "this is standard error" >&2',
1890 \ 'echo "this is standard out" >&1',
1891 \ ], 'Xechoerrout.sh')
1892 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1893
1894 let outfile = 'Xtermstdout'
1895 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001896
1897 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1898 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001899 call assert_equal('this is standard error', term_getline(buf, 1))
1900
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001901 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001902 exe buf . 'bwipe'
1903 call delete('Xechoerrout.sh')
1904 call delete(outfile)
1905endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001906
Bram Moolenaare219f732019-11-30 15:34:08 +01001907func Test_termwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001908 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001909
1910 " Let the terminal output more than 'termwinscroll' lines, some at the start
1911 " will be dropped.
1912 exe 'set termwinscroll=' . &lines
1913 let buf = term_start('/bin/sh')
1914 for i in range(1, &lines)
1915 call feedkeys("echo " . i . "\<CR>", 'xt')
1916 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1917 endfor
1918 " Go to Terminal-Normal mode to update the buffer.
1919 call feedkeys("\<C-W>N", 'xt')
1920 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1921
1922 " Every "echo nr" must only appear once
1923 let lines = getline(1, line('$'))
1924 for i in range(&lines - len(lines) / 2 + 2, &lines)
1925 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1926 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1927 endfor
1928
1929 exe buf . 'bwipe!'
1930endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001931
Bram Moolenaar875cf872018-07-08 20:49:07 +02001932" Resizing the terminal window caused an ml_get error.
1933" TODO: This does not reproduce the original problem.
1934func Test_terminal_resize()
1935 set statusline=x
1936 terminal
1937 call assert_equal(2, winnr('$'))
1938
1939 " Fill the terminal with text.
1940 if has('win32')
1941 call feedkeys("dir\<CR>", 'xt')
1942 else
1943 call feedkeys("ls\<CR>", 'xt')
1944 endif
1945 " Go to Terminal-Normal mode for a moment.
1946 call feedkeys("\<C-W>N", 'xt')
1947 " Open a new window
1948 call feedkeys("i\<C-W>n", 'xt')
1949 call assert_equal(3, winnr('$'))
1950 redraw
1951
1952 close
1953 call assert_equal(2, winnr('$'))
1954 call feedkeys("exit\<CR>", 'xt')
1955 set statusline&
1956endfunc
1957
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001958" must be nearly the last, we can't go back from GUI to terminal
1959func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001960 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001961
1962 " Ignore the "failed to create input context" error.
1963 call test_ignore_error('E285:')
1964
1965 gui -f
1966
1967 call assert_equal(1, winnr('$'))
1968 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001969 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001970 call TermWait(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001971
1972 " closing window wipes out the terminal buffer a with finished job
1973 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1974 call assert_equal("", bufname(buf))
1975
1976 unlet g:job
1977endfunc
1978
1979func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001980 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001981 set guioptions+=!
1982
1983 let filename = 'Xtestscript'
1984 if has('win32')
1985 let filename .= '.bat'
1986 let prefix = ''
1987 let contents = ['@echo off', 'exit %1']
1988 else
1989 let filename .= '.sh'
1990 let prefix = './'
1991 let contents = ['#!/bin/sh', 'exit $1']
1992 endif
1993 call writefile(contents, filename)
1994 call setfperm(filename, 'rwxrwx---')
1995
1996 " Check if v:shell_error is equal to the exit status.
1997 let exitval = 0
1998 execute printf(':!%s%s %d', prefix, filename, exitval)
1999 call assert_equal(exitval, v:shell_error)
2000
2001 let exitval = 9
2002 execute printf(':!%s%s %d', prefix, filename, exitval)
2003 call assert_equal(exitval, v:shell_error)
2004
2005 set guioptions&
2006 call delete(filename)
2007endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002008
2009func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002010 CheckUnix
2011
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002012 term ++hidden cat
2013 let bnr = bufnr('$')
2014 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2015 exe 'sbuf ' . bnr
2016 call assert_equal('terminal', &buftype)
2017 call term_sendkeys(bnr, "asdf\<CR>")
2018 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
2019 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002020 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002021 bwipe!
2022endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002023
Bram Moolenaara4c2a242019-03-25 23:01:38 +01002024func Test_terminal_switch_mode()
2025 term
2026 let bnr = bufnr('$')
2027 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2028 call feedkeys("\<C-W>N", 'xt')
2029 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2030 call feedkeys("A", 'xt')
2031 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2032 call feedkeys("\<C-W>N", 'xt')
2033 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2034 call feedkeys("I", 'xt')
2035 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2036 call feedkeys("\<C-W>Nv", 'xt')
2037 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2038 call feedkeys("I", 'xt')
2039 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2040 call feedkeys("\<C-W>Nv", 'xt')
2041 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2042 call feedkeys("A", 'xt')
2043 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2044 bwipe!
2045endfunc
2046
Bram Moolenaara3817732019-10-16 16:31:44 +02002047func Test_terminal_normal_mode()
2048 CheckRunVimInTerminal
2049
2050 " Run Vim in a terminal and open a terminal window to run Vim in.
2051 let lines =<< trim END
2052 call setline(1, range(11111, 11122))
2053 3
2054 END
2055 call writefile(lines, 'XtermNormal')
2056 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002057 call TermWait(buf)
Bram Moolenaara3817732019-10-16 16:31:44 +02002058
2059 call term_sendkeys(buf, "\<C-W>N")
2060 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
2061 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
2062
2063 call term_sendkeys(buf, ":set culopt=number\r")
2064 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
2065
2066 call term_sendkeys(buf, ":set culopt=line\r")
2067 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
2068
2069 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
2070 call StopVimInTerminal(buf)
2071 call delete('XtermNormal')
2072endfunc
2073
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002074func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002075 CheckUnix
2076
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002077 call assert_equal(1, winnr('$'))
2078 term ++hidden ++close ls
2079 let bnr = bufnr('$')
2080 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2081 call WaitForAssert({-> assert_false(bufexists(bnr))})
2082 call assert_equal(1, winnr('$'))
2083endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002084
2085func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002086 " This test does not pass through ConPTY.
2087 if has('conpty')
2088 return
2089 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002090 let contents = [
2091 \ [ 'One', '', 'X' ],
2092 \ [ 'Two', '', '' ],
2093 \ [ 'Three' ] + repeat([''], 30)
2094 \ ]
2095
2096 for c in contents
2097 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002098 if has('win32')
2099 term cmd /c type Xfile
2100 else
2101 term cat Xfile
2102 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002103 let bnr = bufnr('$')
2104 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2105 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002106 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002107 call assert_equal(c, getline(1, line('$')))
2108 quit
2109 endfor
2110
2111 call delete('Xfile')
2112endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002113
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002114func Test_terminal_no_job()
Bram Moolenaar077ff432019-10-28 00:42:21 +01002115 if has('win32')
2116 let cmd = 'cmd /c ""'
2117 else
2118 CheckExecutable false
2119 let cmd = 'false'
2120 endif
2121 let term = term_start(cmd, {'term_finish': 'close'})
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002122 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2123endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002124
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002125func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002126 CheckUnix
2127
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002128 let buf = Run_shell_in_terminal({})
2129
2130 " Wait for the shell to display a prompt.
2131 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2132
2133 " Hide the cursor.
2134 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2135 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2136
2137 " Show the cursor.
2138 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002139 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002140
2141 " Change color of cursor.
2142 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2143 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2144 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2145 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2146 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2147
2148 " Make cursor a blinking block.
2149 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2150 call WaitForAssert({-> assert_equal([1, 1],
2151 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2152
2153 " Make cursor a steady block.
2154 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2155 call WaitForAssert({-> assert_equal([0, 1],
2156 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2157
2158 " Make cursor a blinking underline.
2159 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2160 call WaitForAssert({-> assert_equal([1, 2],
2161 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2162
2163 " Make cursor a steady underline.
2164 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2165 call WaitForAssert({-> assert_equal([0, 2],
2166 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2167
2168 " Make cursor a blinking vertical bar.
2169 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2170 call WaitForAssert({-> assert_equal([1, 3],
2171 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2172
2173 " Make cursor a steady vertical bar.
2174 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2175 call WaitForAssert({-> assert_equal([0, 3],
2176 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2177
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002178 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002179endfunc
2180
Bram Moolenaar1e115362019-01-09 23:01:02 +01002181func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002182 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002183 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002184 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002185 call assert_equal('', term_gettitle(bufnr('$') + 1))
2186
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002187 if !has('title') || &title == 0 || empty(&t_ts)
2188 throw "Skipped: can't get/set title"
2189 endif
2190
Bram Moolenaar1e115362019-01-09 23:01:02 +01002191 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002192 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002193 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2194 call term_sendkeys(term, ":e Xfoo\r")
2195 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002196 else
2197 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002198 call term_sendkeys(term, ":e Xfoo\r")
2199 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002200 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002201
Bram Moolenaar1e115362019-01-09 23:01:02 +01002202 call term_sendkeys(term, ":set titlestring=foo\r")
2203 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2204
2205 exe term . 'bwipe!'
2206endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002207
Bram Moolenaar01603a92020-04-03 12:56:17 +02002208func Test_term_gettty()
2209 let buf = Run_shell_in_terminal({})
2210 let gettty = term_gettty(buf)
2211
2212 if has('unix') && executable('tty')
2213 " Find tty using the tty shell command.
2214 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2215 call term_sendkeys(buf, "tty\r")
2216 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
2217 let tty = term_getline(buf, 2)
2218 call assert_equal(tty, gettty)
2219 endif
2220
2221 let gettty0 = term_gettty(buf, 0)
2222 let gettty1 = term_gettty(buf, 1)
2223
2224 call assert_equal(gettty, gettty0)
2225 call assert_equal(job_info(g:job).tty_out, gettty0)
2226 call assert_equal(job_info(g:job).tty_in, gettty1)
2227
2228 if has('unix')
2229 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
2230 " are identical according to :help term_gettty()
2231 call assert_equal(gettty0, gettty1)
2232 call assert_match('^/dev/', gettty)
2233 else
2234 " ConPTY works on anonymous pipe.
2235 if !has('conpty')
2236 call assert_match('^\\\\.\\pipe\\', gettty0)
2237 call assert_match('^\\\\.\\pipe\\', gettty1)
2238 endif
2239 endif
2240
2241 call assert_fails('call term_gettty(buf, 2)', 'E475:')
2242 call assert_fails('call term_gettty(buf, -1)', 'E475:')
2243
2244 call assert_equal('', term_gettty(buf + 1))
2245
2246 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002247 call TermWait(buf)
Bram Moolenaar01603a92020-04-03 12:56:17 +02002248 exe buf . 'bwipe'
2249endfunc
2250
Bram Moolenaar10772302019-01-20 18:25:54 +01002251" When drawing the statusline the cursor position may not have been updated
2252" yet.
2253" 1. create a terminal, make it show 2 lines
2254" 2. 0.5 sec later: leave terminal window, execute "i"
2255" 3. 0.5 sec later: clear terminal window, now it's 1 line
2256" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2257" 4. 0.5 sec later: should be done, clean up
2258func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002259 CheckUnix
2260
Bram Moolenaar10772302019-01-20 18:25:54 +01002261 set statusline=x
2262 terminal
2263 let tbuf = bufnr('')
2264 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2265 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2266 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2267 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2268
2269 sleep 2
2270 exe tbuf . 'bwipe!'
2271 au! BufLeave
2272 set statusline=
2273endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002274
2275func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002276 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002277
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002278 " split, go to the bottom-right window
2279 split
2280 wincmd j
2281 set splitright
2282
2283 call writefile([
2284 \ 'echo getwinpos()',
2285 \ ], 'XTest_getwinpos')
2286 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002287 call TermWait(buf)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002288
2289 " Find the output of getwinpos() in the bottom line.
2290 let rows = term_getsize(buf)[0]
2291 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2292 let line = term_getline(buf, rows)
2293 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2294 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2295
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002296 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002297 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002298 " In the GUI it can be more, let's assume a 20 x 14 cell.
2299 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002300 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002301 let winpos = 50->getwinpos()
2302 call assert_equal(xroot, winpos[0])
2303 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002304 let [winrow, wincol] = win_screenpos('.')
2305 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2306 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2307 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2308 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002309
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002310 call TermWait(buf)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002311 call term_sendkeys(buf, ":q\<CR>")
2312 call StopVimInTerminal(buf)
2313 call delete('XTest_getwinpos')
2314 exe buf . 'bwipe!'
2315 set splitright&
2316 only!
2317endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002318
2319func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002320 " somehow doesn't work on MS-Windows
2321 CheckUnix
2322 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002323
2324 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002325 call writefile(["\<Esc>[?1047h"], 'Xtext')
2326 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002327 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2328
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002329 call writefile(["\<Esc>[?1047l"], 'Xtext')
2330 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002331 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002332
2333 call term_sendkeys(buf, "exit\r")
2334 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002335 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002336endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002337
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002338func Test_terminal_shell_option()
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002339 if has('unix')
2340 " exec is a shell builtin command, should fail without a shell.
2341 term exec ls runtest.vim
2342 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
2343 bwipe!
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002344
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002345 term ++shell exec ls runtest.vim
2346 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2347 bwipe!
2348 elseif has('win32')
2349 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar36ec6f62019-11-05 22:38:47 +01002350 try
2351 term dir /b runtest.vim
2352 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
2353 catch /CreateProcess/
2354 " ignore
2355 endtry
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002356 bwipe!
2357
2358 term ++shell dir /b runtest.vim
2359 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2360 bwipe!
2361 endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002362endfunc
2363
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002364func Test_terminal_setapi_and_call()
Bram Moolenaar21109272020-01-30 16:27:20 +01002365 CheckRunVimInTerminal
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002366
2367 call WriteApiCall('Tapi_TryThis')
2368 call ch_logfile('Xlog', 'w')
2369
2370 unlet! g:called_bufnum
2371 unlet! g:called_arg
2372
Bram Moolenaar21109272020-01-30 16:27:20 +01002373 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002374 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2375 call assert_false(exists('g:called_bufnum'))
2376 call assert_false(exists('g:called_arg'))
2377
Bram Moolenaar21109272020-01-30 16:27:20 +01002378 eval buf->term_setapi('Tapi_')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002379 call term_sendkeys(buf, ":set notitle\<CR>")
2380 call term_sendkeys(buf, ":source Xscript\<CR>")
2381 call WaitFor({-> exists('g:called_bufnum')})
2382 call assert_equal(buf, g:called_bufnum)
2383 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar21109272020-01-30 16:27:20 +01002384
2385 call StopVimInTerminal(buf)
2386
2387 call delete('Xscript')
2388 call ch_logfile('')
2389 call delete('Xlog')
2390 unlet! g:called_bufnum
2391 unlet! g:called_arg
2392endfunc
2393
2394func Test_terminal_api_arg()
2395 CheckRunVimInTerminal
2396
2397 call WriteApiCall('Tapi_TryThis')
2398 call ch_logfile('Xlog', 'w')
2399
2400 unlet! g:called_bufnum
2401 unlet! g:called_arg
2402
2403 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2404 let buf = bufnr('%')
2405 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2406 call assert_false(exists('g:called_bufnum'))
2407 call assert_false(exists('g:called_arg'))
2408
2409 call StopVimInTerminal(buf)
2410
2411 call ch_logfile('Xlog', 'w')
2412
2413 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2414 let buf = bufnr('%')
2415 call WaitFor({-> exists('g:called_bufnum')})
2416 call assert_equal(buf, g:called_bufnum)
2417 call assert_equal(['hello', 123], g:called_arg)
2418
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002419 call StopVimInTerminal(buf)
2420
2421 call delete('Xscript')
2422 call ch_logfile('')
2423 call delete('Xlog')
2424 unlet! g:called_bufnum
2425 unlet! g:called_arg
2426endfunc
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002427
Bram Moolenaar9b9be002020-03-22 14:41:22 +01002428func Test_terminal_invalid_arg()
2429 call assert_fails('terminal ++xyz', 'E181:')
2430endfunc
2431
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002432func Test_terminal_in_popup()
2433 CheckRunVimInTerminal
2434
2435 let text =<< trim END
2436 some text
2437 to edit
2438 in a popup window
2439 END
2440 call writefile(text, 'Xtext')
Bram Moolenaar3180fe62020-02-02 13:47:06 +01002441 let cmd = GetVimCommandCleanTerm()
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002442 let lines = [
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01002443 \ 'set t_u7=',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002444 \ 'call setline(1, range(20))',
2445 \ 'hi PopTerm ctermbg=grey',
2446 \ 'func OpenTerm(setColor)',
Bram Moolenaar353c3512020-03-15 14:19:26 +01002447 \ " set noruler",
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002448 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
Bram Moolenaar57c33952020-02-29 16:09:16 +01002449 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002450 \ ' if a:setColor',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002451 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002452 \ ' endif',
2453 \ 'endfunc',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002454 \ 'func HidePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002455 \ ' call popup_hide(g:winid)',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002456 \ 'endfunc',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002457 \ 'func ClosePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002458 \ ' call popup_close(g:winid)',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002459 \ 'endfunc',
2460 \ 'func ReopenPopup()',
2461 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
2462 \ 'endfunc',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002463 \ ]
2464 call writefile(lines, 'XtermPopup')
2465 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002466 call TermWait(buf, 100)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002467 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002468 call TermWait(buf, 100)
Bram Moolenaarc2adc002020-02-14 17:05:18 +01002469 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002470 call TermWait(buf, 100)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002471 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002472 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
2473
2474 call term_sendkeys(buf, ":q\<CR>")
2475 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
2476
2477 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002478 call TermWait(buf, 150)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002479 call term_sendkeys(buf, ":set hlsearch\<CR>")
2480 call term_sendkeys(buf, "/edit\<CR>")
2481 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
2482
Bram Moolenaar1af5ce02020-02-06 11:54:35 +01002483 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
2484 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
2485 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002486 call TermWait(buf, 50)
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002487
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002488 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
2489 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
2490
2491 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
2492 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002493
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002494 " Go to terminal-Normal mode and visually select text.
2495 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
2496 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
2497
2498 " Back to job mode, redraws
2499 call term_sendkeys(buf, "A")
2500 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
2501
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002502 call TermWait(buf, 50)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002503 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002504 call TermWait(buf, 100) " wait for terminal to vanish
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002505
2506 call StopVimInTerminal(buf)
Bram Moolenaar19398262020-03-14 15:28:08 +01002507 call delete('Xtext')
2508 call delete('XtermPopup')
2509endfunc
2510
2511" Check a terminal in popup window uses the default mininum size.
2512func Test_terminal_in_popup_min_size()
2513 CheckRunVimInTerminal
2514
2515 let text =<< trim END
2516 another text
2517 to show
2518 in a popup window
2519 END
2520 call writefile(text, 'Xtext')
2521 let lines = [
2522 \ 'set t_u7=',
2523 \ 'call setline(1, range(20))',
Bram Moolenaar19398262020-03-14 15:28:08 +01002524 \ 'func OpenTerm()',
2525 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
2526 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2527 \ 'endfunc',
2528 \ ]
2529 call writefile(lines, 'XtermPopup')
2530 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002531 call TermWait(buf, 100)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002532 call term_sendkeys(buf, ":set noruler\<CR>")
Bram Moolenaar19398262020-03-14 15:28:08 +01002533 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002534 call TermWait(buf, 50)
Bram Moolenaar19398262020-03-14 15:28:08 +01002535 call term_sendkeys(buf, ":\<CR>")
2536 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
2537
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002538 call TermWait(buf, 50)
Bram Moolenaar19398262020-03-14 15:28:08 +01002539 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002540 call TermWait(buf, 50) " wait for terminal to vanish
Bram Moolenaar19398262020-03-14 15:28:08 +01002541 call StopVimInTerminal(buf)
2542 call delete('Xtext')
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002543 call delete('XtermPopup')
2544endfunc
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002545
Bram Moolenaar83d47902020-03-26 20:34:00 +01002546" Check a terminal in popup window with different colors
2547func Terminal_in_popup_colored(group_name, highlight_cmd, highlight_opt)
2548 CheckRunVimInTerminal
2549 CheckUnix
2550
2551 let lines = [
2552 \ 'set t_u7=',
2553 \ 'call setline(1, range(20))',
2554 \ 'func OpenTerm()',
2555 \ " let s:buf = term_start('cat', #{hidden: 1, "
2556 \ .. a:highlight_opt .. "})",
2557 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2558 \ 'endfunc',
2559 \ a:highlight_cmd,
2560 \ ]
2561 call writefile(lines, 'XtermPopup')
2562 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002563 call TermWait(buf, 100)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002564 call term_sendkeys(buf, ":set noruler\<CR>")
2565 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002566 call TermWait(buf, 50)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002567 call term_sendkeys(buf, "hello\<CR>")
2568 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
2569
2570 call term_sendkeys(buf, "\<C-D>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002571 call TermWait(buf, 50)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002572 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002573 call TermWait(buf, 50) " wait for terminal to vanish
Bram Moolenaar83d47902020-03-26 20:34:00 +01002574 call StopVimInTerminal(buf)
2575 call delete('XtermPopup')
2576endfunc
2577
2578func Test_terminal_in_popup_colored_Terminal()
2579 call Terminal_in_popup_colored("Terminal", "highlight Terminal ctermfg=blue ctermbg=yellow", "")
2580endfunc
2581
2582func Test_terminal_in_popup_colored_group()
2583 call Terminal_in_popup_colored("MyTermCol", "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue", "term_highlight: 'MyTermCol',")
2584endfunc
2585
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002586func Test_double_popup_terminal()
2587 let buf1 = term_start(&shell, #{hidden: 1})
2588 let win1 = popup_create(buf1, {})
2589 let buf2 = term_start(&shell, #{hidden: 1})
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002590 call assert_fails('call popup_create(buf2, {})', 'E861:')
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002591 call popup_close(win1)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002592 exe buf1 .. 'bwipe!'
2593 exe buf2 .. 'bwipe!'
2594endfunc
2595
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002596func Test_issue_5607()
2597 let wincount = winnr('$')
2598 exe 'terminal' &shell &shellcmdflag 'exit'
2599 let job = term_getjob(bufnr())
2600 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2601
2602 let old_wincolor = &wincolor
2603 try
2604 set wincolor=
2605 finally
2606 let &wincolor = old_wincolor
2607 bw!
2608 endtry
2609endfunc
Bram Moolenaare010c722020-02-24 21:37:54 +01002610
2611func Test_hidden_terminal()
2612 let buf = term_start(&shell, #{hidden: 1})
2613 call assert_equal('', bufname('^$'))
2614 call StopShellInTerminal(buf)
2615endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002616
2617func Test_term_nasty_callback()
Bram Moolenaara161cb52020-04-30 19:09:35 +02002618 CheckExecutable sh
Bram Moolenaarcee52202020-03-11 14:19:58 +01002619
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002620 set hidden
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002621 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002622 call popup_create(g:buf0, {})
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002623 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
2624
2625 call popup_clear(1)
Bram Moolenaarcee52202020-03-11 14:19:58 +01002626 set hidden&
2627endfunc
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002628
Bram Moolenaareb58a242020-04-19 18:13:19 +02002629func Test_term_and_startinsert()
2630 CheckRunVimInTerminal
2631 CheckUnix
2632
2633 let lines =<< trim EOL
2634 put='some text'
2635 term
2636 startinsert
2637 EOL
2638 call writefile(lines, 'XTest_startinsert')
2639 let buf = RunVimInTerminal('-S XTest_startinsert', {})
2640
2641 call term_sendkeys(buf, "exit\r")
2642 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
2643 call term_sendkeys(buf, "0l")
2644 call term_sendkeys(buf, "A<\<Esc>")
2645 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
2646
2647 call StopVimInTerminal(buf)
2648 call delete('XTest_startinsert')
2649endfunc
2650
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002651" Test for passing invalid arguments to terminal functions
2652func Test_term_func_invalid_arg()
2653 call assert_fails('let b = term_getaltscreen([])', 'E745:')
2654 call assert_fails('let p = term_getansicolors([])', 'E745:')
2655 call assert_fails('let a = term_getattr(1, [])', 'E730:')
2656 call assert_fails('let c = term_getcursor([])', 'E745:')
2657 call assert_fails('let l = term_getline([], 1)', 'E745:')
2658 call assert_fails('let l = term_getscrolled([])', 'E745:')
2659 call assert_fails('let s = term_getsize([])', 'E745:')
2660 call assert_fails('let s = term_getstatus([])', 'E745:')
2661 call assert_fails('let s = term_scrape([], 1)', 'E745:')
2662 call assert_fails('call term_sendkeys([], "a")', 'E745:')
2663 call assert_fails('call term_setansicolors([], [])', 'E745:')
2664 call assert_fails('call term_setapi([], "")', 'E745:')
2665 call assert_fails('call term_setrestore([], "")', 'E745:')
2666 call assert_fails('call term_setkill([], "")', 'E745:')
2667endfunc
2668
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002669" Test for sending various special keycodes to a terminal
2670func Test_term_keycode_translation()
2671 CheckRunVimInTerminal
2672
2673 let buf = RunVimInTerminal('', {})
2674 call term_sendkeys(buf, ":set nocompatible\<CR>")
2675
2676 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
2677 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
2678 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
2679 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
2680 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
2681 \ "\<S-Down>"]
2682 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
2683 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
2684 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
2685 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
2686 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>',
2687 \ '0123456789', "\t\t.+-*/"]
2688
2689 for k in keys
2690 call term_sendkeys(buf, "i\<C-K>" .. k .. "\<CR>\<C-\>\<C-N>")
2691 endfor
2692 call term_sendkeys(buf, "i\<K0>\<K1>\<K2>\<K3>\<K4>\<K5>\<K6>\<K7>")
2693 call term_sendkeys(buf, "\<K8>\<K9>\<kEnter>\<kPoint>\<kPlus>")
2694 call term_sendkeys(buf, "\<kMinus>\<kMultiply>\<kDivide>\<C-\>\<C-N>")
2695 call term_sendkeys(buf, "\<Home>\<Ins>\<Tab>\<S-Tab>\<C-\>\<C-N>")
2696
2697 call term_sendkeys(buf, ":write Xkeycodes\<CR>")
2698 call term_wait(buf)
2699 call StopVimInTerminal(buf)
2700 call assert_equal(output, readfile('Xkeycodes'))
2701 call delete('Xkeycodes')
2702endfunc
2703
2704" Test for using the mouse in a terminal
2705func Test_term_mouse()
2706 CheckNotGui
2707 CheckRunVimInTerminal
2708
2709 let save_mouse = &mouse
2710 let save_term = &term
2711 let save_ttymouse = &ttymouse
2712 let save_clipboard = &clipboard
2713 call test_override('no_query_mouse', 1)
2714 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
2715
2716 let lines =<< trim END
2717 one two three four five
2718 red green yellow red blue
2719 vim emacs sublime nano
2720 END
2721 call writefile(lines, 'Xtest_mouse')
2722
2723 let buf = RunVimInTerminal('Xtest_mouse -n', {})
2724 call term_sendkeys(buf, ":set nocompatible\<CR>")
2725 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
2726 call term_sendkeys(buf, ":set clipboard=\<CR>")
2727 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
2728 call term_wait(buf)
2729 redraw!
2730
2731 " Test for <LeftMouse> click/release
2732 call test_setmouse(2, 5)
2733 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
2734 call test_setmouse(3, 8)
2735 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
2736 call term_wait(buf, 50)
2737 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
2738 call term_wait(buf, 50)
2739 let pos = json_decode(readfile('Xbuf')[0])
2740 call assert_equal([3, 8], pos[1:2])
2741
2742 " Test for selecting text using mouse
2743 call delete('Xbuf')
2744 call test_setmouse(2, 11)
2745 call term_sendkeys(buf, "\<LeftMouse>")
2746 call test_setmouse(2, 16)
2747 call term_sendkeys(buf, "\<LeftRelease>y")
2748 call term_wait(buf, 50)
2749 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2750 call term_wait(buf, 50)
2751 call assert_equal('yellow', readfile('Xbuf')[0])
2752
2753 " Test for selecting text using doubleclick
2754 call delete('Xbuf')
2755 call test_setmouse(1, 11)
2756 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
2757 call test_setmouse(1, 17)
2758 call term_sendkeys(buf, "\<LeftRelease>y")
2759 call term_wait(buf, 50)
2760 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2761 call term_wait(buf, 50)
2762 call assert_equal('three four', readfile('Xbuf')[0])
2763
2764 " Test for selecting a line using triple click
2765 call delete('Xbuf')
2766 call test_setmouse(3, 2)
2767 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
2768 call term_wait(buf, 50)
2769 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2770 call term_wait(buf, 50)
2771 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
2772
2773 " Test for selecting a block using qudraple click
2774 call delete('Xbuf')
2775 call test_setmouse(1, 11)
2776 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
2777 call test_setmouse(3, 13)
2778 call term_sendkeys(buf, "\<LeftRelease>y")
2779 call term_wait(buf, 50)
2780 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2781 call term_wait(buf, 50)
2782 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
2783
2784 " Test for extending a selection using right click
2785 call delete('Xbuf')
2786 call test_setmouse(2, 9)
2787 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
2788 call test_setmouse(2, 16)
2789 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
2790 call term_wait(buf, 50)
2791 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2792 call term_wait(buf, 50)
2793 call assert_equal("n yellow", readfile('Xbuf')[0])
2794
2795 " Test for pasting text using middle click
2796 call delete('Xbuf')
2797 call term_sendkeys(buf, ":let @r='bright '\<CR>")
2798 call test_setmouse(2, 22)
2799 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
2800 call term_wait(buf, 50)
2801 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
2802 call term_wait(buf, 50)
2803 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
2804
2805 " cleanup
2806 call term_wait(buf)
2807 call StopVimInTerminal(buf)
2808 let &mouse = save_mouse
2809 let &term = save_term
2810 let &ttymouse = save_ttymouse
2811 let &clipboard = save_clipboard
2812 set mousetime&
2813 call test_override('no_query_mouse', 0)
2814 call delete('Xtest_mouse')
2815 call delete('Xbuf')
2816endfunc
2817
2818" Test for modeless selection in a terminal
2819func Test_term_modeless_selection()
2820 CheckUnix
2821 CheckNotGui
2822 CheckRunVimInTerminal
2823 CheckFeature clipboard_working
2824
2825 let save_mouse = &mouse
2826 let save_term = &term
2827 let save_ttymouse = &ttymouse
2828 call test_override('no_query_mouse', 1)
2829 set mouse=a term=xterm ttymouse=sgr mousetime=200
2830 set clipboard=autoselectml
2831
2832 let lines =<< trim END
2833 one two three four five
2834 red green yellow red blue
2835 vim emacs sublime nano
2836 END
2837 call writefile(lines, 'Xtest_modeless')
2838
2839 let buf = RunVimInTerminal('Xtest_modeless -n', {})
2840 call term_sendkeys(buf, ":set nocompatible\<CR>")
2841 call term_sendkeys(buf, ":set mouse=\<CR>")
2842 call term_wait(buf)
2843 redraw!
2844
2845 " Test for copying a modeless selection to clipboard
2846 let @* = 'clean'
2847 " communicating with X server may take a little time
2848 sleep 100m
2849 call feedkeys(MouseLeftClickCode(2, 3), 'x')
2850 call feedkeys(MouseLeftDragCode(2, 11), 'x')
2851 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
2852 call assert_equal("d green y", @*)
2853
2854 " cleanup
2855 call term_wait(buf)
2856 call StopVimInTerminal(buf)
2857 let &mouse = save_mouse
2858 let &term = save_term
2859 let &ttymouse = save_ttymouse
2860 set mousetime& clipboard&
2861 call test_override('no_query_mouse', 0)
2862 call delete('Xtest_modeless')
2863endfunc
2864
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002865" vim: shiftwidth=2 sts=2 expandtab