blob: a8a549cbd9c10013c708e2ee9dd631ff7545e161 [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:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001012 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001013 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)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001018 if has('gui_running') || has('termguicolors')
1019 call assert_fails(cmd, 'E475:')
1020 else
1021 call assert_fails(cmd, 'E254:')
1022 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001023 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001024endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001025
1026func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001027 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001028
1029 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001030 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001031
Bram Moolenaar086eb872018-03-25 21:24:12 +02001032 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001033 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001034
Bram Moolenaar086eb872018-03-25 21:24:12 +02001035 " Request the cursor position.
1036 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001037
1038 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001039 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001040
Bram Moolenaar086eb872018-03-25 21:24:12 +02001041 " End "cat" gently.
1042 call term_sendkeys(buf, "\<CR>\<C-D>")
1043
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001044 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001045 exe buf . 'bwipe'
1046 unlet g:job
1047endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001048
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001049" Run Vim, start a terminal in that Vim with the kill argument,
1050" :qall works.
1051func Run_terminal_qall_kill(line1, line2)
1052 " 1. Open a terminal window and wait for the prompt to appear
1053 " 2. set kill using term_setkill()
1054 " 3. make Vim exit, it will kill the shell
1055 let after = [
1056 \ a:line1,
1057 \ 'let buf = bufnr("%")',
1058 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1059 \ ' sleep 10m',
1060 \ 'endwhile',
1061 \ a:line2,
1062 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1063 \ 'qall',
1064 \ ]
1065 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001066 return
1067 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001068 call assert_equal("done", readfile("Xdone")[0])
1069 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001070endfunc
1071
1072" Run Vim in a terminal, then start a terminal in that Vim with a kill
1073" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001074func Test_terminal_qall_kill_arg()
1075 call Run_terminal_qall_kill('term ++kill=kill', '')
1076endfunc
1077
1078" Run Vim, start a terminal in that Vim, set the kill argument with
1079" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001080func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001081 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001082endfunc
1083
1084" Run Vim, start a terminal in that Vim without the kill argument,
1085" check that :qall does not exit, :qall! does.
1086func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001087 let after =<< trim [CODE]
1088 term
1089 let buf = bufnr("%")
1090 while term_getline(buf, 1) =~ "^\\s*$"
1091 sleep 10m
1092 endwhile
1093 set nomore
1094 au VimLeavePre * call writefile(["too early"], "Xdone")
1095 qall
1096 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1097 cquit
1098 [CODE]
1099
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001100 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001101 return
1102 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001103 call assert_equal("done", readfile("Xdone")[0])
1104 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001105endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001106
1107" Run Vim in a terminal, then start a terminal in that Vim without a kill
1108" argument, check that :confirm qall works.
1109func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001110 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001111 let buf = RunVimInTerminal('', {})
1112
1113 " Open a terminal window and wait for the prompt to appear
1114 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001115 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1116 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001117
1118 " make Vim exit, it will prompt to kill the shell
1119 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001120 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001121 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001122 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001123
1124 " close the terminal window where Vim was running
1125 quit
1126endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001127
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001128" Run Vim in a terminal, then start a terminal window with a shell and check
1129" that Vim exits if it is closed.
1130func Test_terminal_exit()
1131 CheckRunVimInTerminal
1132
1133 let lines =<< trim END
1134 let winid = win_getid()
1135 help
1136 term
1137 let termid = win_getid()
1138 call win_gotoid(winid)
1139 close
1140 call win_gotoid(termid)
1141 END
1142 call writefile(lines, 'XtermExit')
1143 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1144 let job = term_getjob(buf)
1145 call WaitForAssert({-> assert_equal("run", job_status(job))})
1146
1147 " quit the shell, it will make Vim exit
1148 call term_sendkeys(buf, "exit\<CR>")
1149 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1150
1151 call delete('XtermExit')
1152endfunc
1153
Bram Moolenaar012eb662018-03-13 17:55:27 +01001154func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001155 augroup repro
1156 au!
1157 au TerminalOpen * let s:called += 1
1158 augroup END
1159
1160 let s:called = 0
1161
1162 " Open a terminal window with :terminal
1163 terminal
1164 call assert_equal(1, s:called)
1165 bwipe!
1166
1167 " Open a terminal window with term_start()
1168 call term_start(&shell)
1169 call assert_equal(2, s:called)
1170 bwipe!
1171
1172 " Open a hidden terminal buffer with :terminal
1173 terminal ++hidden
1174 call assert_equal(3, s:called)
1175 for buf in term_list()
1176 exe buf . "bwipe!"
1177 endfor
1178
1179 " Open a hidden terminal buffer with term_start()
1180 let buf = term_start(&shell, {'hidden': 1})
1181 call assert_equal(4, s:called)
1182 exe buf . "bwipe!"
1183
1184 unlet s:called
1185 au! repro
1186endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001187
1188func Check_dump01(off)
1189 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1190 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001191 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001192endfunc
1193
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001194func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001195 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001196 let save_enc = &encoding
1197 set encoding=utf-8
1198 call assert_equal(1, winnr('$'))
1199
1200 let text = " a\u0300 e\u0302 o\u0308"
1201 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001202 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001203 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001204 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001205 let dumpline = readfile('Xdump')[0]
1206 call assert_match('|à| |ê| |ö', dumpline)
1207
1208 call StopVimInTerminal(buf)
1209 call delete('Xcomposing')
1210 call delete('Xdump')
1211 let &encoding = save_enc
1212endfunc
1213
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001214" Tests for failures in the term_dumpwrite() function
1215func Test_terminal_dumpwrite_errors()
1216 CheckRunVimInTerminal
1217 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1218 let buf = RunVimInTerminal('', {})
1219 call term_wait(buf)
1220 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1221 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1222 call writefile([], 'Xtest.dump')
1223 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1224 call delete('Xtest.dump')
1225 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1226 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001227 call test_garbagecollect_now()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001228 call StopVimInTerminal(buf)
1229 call term_wait(buf)
1230 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1231 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1232 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1233endfunc
1234
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001235" just testing basic functionality.
1236func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001237 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001238 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001239 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001240 call assert_equal(2, winnr('$'))
1241 call assert_equal(20, line('$'))
1242 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001243
1244 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001245 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001246 call assert_equal(buf, buf2)
1247 call assert_notequal('one two three four five', trim(getline(1)))
1248
1249 " Load the first dump again in the same window
1250 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1251 call assert_equal(buf, buf2)
1252 call Check_dump01(0)
1253
1254 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1255 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1256 new
1257 let closedbuf = winbufnr('')
1258 quit
1259 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001260 call assert_fails('call term_dumpload([])', 'E474:')
1261 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001262
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001263 quit
1264endfunc
1265
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001266func Test_terminal_dumpload_dump()
1267 CheckRunVimInTerminal
1268
1269 let lines =<< trim END
1270 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1271 END
1272 call writefile(lines, 'XtermDumpload')
1273 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1274 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1275
1276 call StopVimInTerminal(buf)
1277 call delete('XtermDumpload')
1278endfunc
1279
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001280func Test_terminal_dumpdiff()
1281 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001282 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001283 call assert_equal(2, winnr('$'))
1284 call assert_equal(62, line('$'))
1285 call Check_dump01(0)
1286 call Check_dump01(42)
1287 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1288 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001289
1290 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E474:')
1291 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1292 call writefile([], 'X1.dump')
1293 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1294 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001295endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001296
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001297func Test_terminal_dumpdiff_swap()
1298 call assert_equal(1, winnr('$'))
1299 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1300 call assert_equal(2, winnr('$'))
1301 call assert_equal(62, line('$'))
1302 call assert_match('Test_popup_command_01.dump', getline(21))
1303 call assert_match('Test_popup_command_03.dump', getline(42))
1304 call assert_match('Undo', getline(3))
1305 call assert_match('three four five', getline(45))
1306
1307 normal s
1308 call assert_match('Test_popup_command_03.dump', getline(21))
1309 call assert_match('Test_popup_command_01.dump', getline(42))
1310 call assert_match('three four five', getline(3))
1311 call assert_match('Undo', getline(45))
1312 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001313
1314 " Diff two terminal dump files with different number of rows
1315 " Swap the diffs
1316 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1317 call assert_match('Test_popup_command_01.dump', getline(21))
1318 call assert_match('Test_winline_rnu.dump', getline(42))
1319 normal s
1320 call assert_match('Test_winline_rnu.dump', getline(6))
1321 call assert_match('Test_popup_command_01.dump', getline(27))
1322 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001323endfunc
1324
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001325func Test_terminal_dumpdiff_options()
1326 set laststatus=0
1327 call assert_equal(1, winnr('$'))
1328 let height = winheight(0)
1329 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1330 call assert_equal(2, winnr('$'))
1331 call assert_equal(height, winheight(winnr()))
1332 call assert_equal(33, winwidth(winnr()))
1333 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1334 quit
1335
1336 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001337 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1338 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001339 call assert_equal(&columns, winwidth(0))
1340 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001341 call assert_equal('something else', bufname('%'))
1342 quit
1343
1344 call assert_equal(1, winnr('$'))
1345 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1346 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001347 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 +01001348 bwipe
1349
1350 set laststatus&
1351endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001352
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001353func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001354 call assert_equal(1, winnr('$'))
1355
1356 " Use the title termcap entries to output the escape sequence.
1357 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001358 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001359 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001360 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001361 \ 'redraw',
1362 \ "set t_ts=",
1363 \ ], 'Xscript')
1364 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001365 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001366 call assert_equal('Xtextfile', expand('%:t'))
1367 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001368 return buf
1369endfunc
1370
1371func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001372 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001373 let buf = Api_drop_common('')
1374 call assert_equal(0, &bin)
1375 call assert_equal('', &fenc)
1376
1377 call StopVimInTerminal(buf)
1378 call delete('Xscript')
1379 bwipe Xtextfile
1380endfunc
1381
1382func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001383 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001384 let buf = Api_drop_common(',{"bin":1}')
1385 call assert_equal(1, &bin)
1386
1387 call StopVimInTerminal(buf)
1388 call delete('Xscript')
1389 bwipe Xtextfile
1390endfunc
1391
1392func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001393 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001394 let buf = Api_drop_common(',{"binary":1}')
1395 call assert_equal(1, &bin)
1396
1397 call StopVimInTerminal(buf)
1398 call delete('Xscript')
1399 bwipe Xtextfile
1400endfunc
1401
1402func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001403 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001404 set binary
1405 let buf = Api_drop_common(',{"nobin":1}')
1406 call assert_equal(0, &bin)
1407
1408 call StopVimInTerminal(buf)
1409 call delete('Xscript')
1410 bwipe Xtextfile
1411 set nobinary
1412endfunc
1413
1414func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001415 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001416 set binary
1417 let buf = Api_drop_common(',{"nobinary":1}')
1418 call assert_equal(0, &bin)
1419
1420 call StopVimInTerminal(buf)
1421 call delete('Xscript')
1422 bwipe Xtextfile
1423 set nobinary
1424endfunc
1425
1426func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001427 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001428 let buf = Api_drop_common(',{"ff":"dos"}')
1429 call assert_equal("dos", &ff)
1430
1431 call StopVimInTerminal(buf)
1432 call delete('Xscript')
1433 bwipe Xtextfile
1434endfunc
1435
1436func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001437 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001438 let buf = Api_drop_common(',{"fileformat":"dos"}')
1439 call assert_equal("dos", &ff)
1440
1441 call StopVimInTerminal(buf)
1442 call delete('Xscript')
1443 bwipe Xtextfile
1444endfunc
1445
1446func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001447 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001448 let buf = Api_drop_common(',{"enc":"utf-16"}')
1449 call assert_equal("utf-16", &fenc)
1450
1451 call StopVimInTerminal(buf)
1452 call delete('Xscript')
1453 bwipe Xtextfile
1454endfunc
1455
1456func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001457 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001458 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1459 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001460
1461 call StopVimInTerminal(buf)
1462 call delete('Xscript')
1463 bwipe Xtextfile
1464endfunc
1465
1466func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001467 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001468 let firstwinid = win_getid()
1469 split Xtextfile
1470 let textfile_winid = win_getid()
1471 call assert_equal(2, winnr('$'))
1472 call win_gotoid(firstwinid)
1473
1474 " Use the title termcap entries to output the escape sequence.
1475 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001476 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001477 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1478 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1479 \ 'redraw',
1480 \ "set t_ts=",
1481 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001482 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001483 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001484 call assert_equal(textfile_winid, win_getid())
1485
1486 call StopVimInTerminal(buf)
1487 call delete('Xscript')
1488 bwipe Xtextfile
1489endfunc
1490
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001491func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001492 let g:called_bufnum = a:bufnum
1493 let g:called_arg = a:arg
1494endfunc
1495
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001496func WriteApiCall(funcname)
1497 " Use the title termcap entries to output the escape sequence.
1498 call writefile([
1499 \ 'set title',
1500 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1501 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1502 \ 'redraw',
1503 \ "set t_ts=",
1504 \ ], 'Xscript')
1505endfunc
1506
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001507func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001508 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001509
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001510 unlet! g:called_bufnum
1511 unlet! g:called_arg
1512
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001513 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001514
1515 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001516 let buf = RunVimInTerminal('-S Xscript', {})
1517 call WaitFor({-> exists('g:called_bufnum')})
1518 call assert_equal(buf, g:called_bufnum)
1519 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001520 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001521
1522 unlet! g:called_bufnum
1523 unlet! g:called_arg
1524
1525 " Enable explicitly
1526 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1527 call WaitFor({-> exists('g:called_bufnum')})
1528 call assert_equal(buf, g:called_bufnum)
1529 call assert_equal(['hello', 123], g:called_arg)
1530 call StopVimInTerminal(buf)
1531
1532 unlet! g:called_bufnum
1533 unlet! g:called_arg
1534
1535 func! ApiCall_TryThis(bufnum, arg)
1536 let g:called_bufnum2 = a:bufnum
1537 let g:called_arg2 = a:arg
1538 endfunc
1539
1540 call WriteApiCall('ApiCall_TryThis')
1541
1542 " Use prefix match
1543 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1544 call WaitFor({-> exists('g:called_bufnum2')})
1545 call assert_equal(buf, g:called_bufnum2)
1546 call assert_equal(['hello', 123], g:called_arg2)
1547 call StopVimInTerminal(buf)
1548
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001549 call assert_fails("call term_start('ls', {'term_api' : []})", 'E475:')
1550
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001551 unlet! g:called_bufnum2
1552 unlet! g:called_arg2
1553
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001554 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001555 delfunction! ApiCall_TryThis
1556 unlet! g:called_bufnum2
1557 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001558endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001559
1560func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001561 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001562
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001563 func! TryThis(bufnum, arg)
1564 let g:called_bufnum3 = a:bufnum
1565 let g:called_arg3 = a:arg
1566 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001567
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001568 call WriteApiCall('TryThis')
1569
1570 unlet! g:called_bufnum3
1571 unlet! g:called_arg3
1572
1573 " Not permitted
1574 call ch_logfile('Xlog', 'w')
1575 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1576 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1577 call assert_false(exists('g:called_bufnum3'))
1578 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001579 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001580
1581 " No match
1582 call ch_logfile('Xlog', 'w')
1583 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1584 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1585 call assert_false(exists('g:called_bufnum3'))
1586 call assert_false(exists('g:called_arg3'))
1587 call StopVimInTerminal(buf)
1588
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001589 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001590 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001591 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001592 delfunction! TryThis
1593 unlet! g:called_bufnum3
1594 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001595endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001596
Bram Moolenaara997b452018-04-17 23:24:06 +02001597let s:caught_e937 = 0
1598
1599func Tapi_Delete(bufnum, arg)
1600 try
1601 execute 'bdelete!' a:bufnum
1602 catch /E937:/
1603 let s:caught_e937 = 1
1604 endtry
1605endfunc
1606
1607func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001608 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001609
1610 call WriteApiCall('Tapi_Delete')
1611 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001612 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001613
1614 call StopVimInTerminal(buf)
1615 call delete('Xscript')
1616 call ch_logfile('', '')
1617endfunc
1618
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001619func Test_terminal_ansicolors_default()
Bram Moolenaara161cb52020-04-30 19:09:35 +02001620 CheckFunction term_getansicolors
1621
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001622 let colors = [
1623 \ '#000000', '#e00000',
1624 \ '#00e000', '#e0e000',
1625 \ '#0000e0', '#e000e0',
1626 \ '#00e0e0', '#e0e0e0',
1627 \ '#808080', '#ff4040',
1628 \ '#40ff40', '#ffff40',
1629 \ '#4040ff', '#ff40ff',
1630 \ '#40ffff', '#ffffff',
1631 \]
1632
1633 let buf = Run_shell_in_terminal({})
1634 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001635 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001636 call TermWait(buf)
Bram Moolenaar98f16712020-05-22 13:34:01 +02001637 call assert_equal([], term_getansicolors(buf))
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001638
1639 exe buf . 'bwipe'
1640endfunc
1641
1642let s:test_colors = [
1643 \ '#616e64', '#0d0a79',
1644 \ '#6d610d', '#0a7373',
1645 \ '#690d0a', '#6d696e',
1646 \ '#0d0a6f', '#616e0d',
1647 \ '#0a6479', '#6d0d0a',
1648 \ '#617373', '#0d0a69',
1649 \ '#6d690d', '#0a6e6f',
1650 \ '#610d0a', '#6e6479',
1651 \]
1652
1653func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001654 CheckFeature termguicolors
Bram Moolenaara161cb52020-04-30 19:09:35 +02001655 CheckFunction term_getansicolors
1656
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001657 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1658 let buf = Run_shell_in_terminal({})
1659 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001660 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001661 call TermWait(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001662
1663 exe buf . 'bwipe'
1664 unlet g:terminal_ansi_colors
1665endfunc
1666
1667func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001668 CheckFeature termguicolors
Bram Moolenaara161cb52020-04-30 19:09:35 +02001669 CheckFunction term_getansicolors
1670
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001671 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1672 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1673 call assert_equal(s:test_colors, term_getansicolors(buf))
1674
1675 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001676 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001677
1678 let colors = [
1679 \ 'ivory', 'AliceBlue',
1680 \ 'grey67', 'dark goldenrod',
1681 \ 'SteelBlue3', 'PaleVioletRed4',
1682 \ 'MediumPurple2', 'yellow2',
1683 \ 'RosyBrown3', 'OrangeRed2',
1684 \ 'white smoke', 'navy blue',
1685 \ 'grey47', 'gray97',
1686 \ 'MistyRose2', 'DodgerBlue4',
1687 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001688 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001689
1690 let colors[4] = 'Invalid'
1691 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001692 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001693
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001694 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001695 call TermWait(buf)
Bram Moolenaar98f16712020-05-22 13:34:01 +02001696 call assert_equal(0, term_setansicolors(buf, []))
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001697 exe buf . 'bwipe'
1698endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001699
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001700func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001701 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001702
1703 " Use all the ANSI colors.
1704 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001705 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001706 \ 'hi Tblack ctermfg=0 ctermbg=8',
1707 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1708 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1709 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1710 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1711 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1712 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1713 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1714 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1715 \ 'hi Tred ctermfg=9 ctermbg=1',
1716 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1717 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1718 \ 'hi Tblue ctermfg=12 ctermbg=4',
1719 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1720 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1721 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001722 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1723 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1724 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001725 \ '',
1726 \ 'call matchadd("Tblack", "A")',
1727 \ 'call matchadd("Tdarkred", "B")',
1728 \ 'call matchadd("Tdarkgreen", "C")',
1729 \ 'call matchadd("Tbrown", "D")',
1730 \ 'call matchadd("Tdarkblue", "E")',
1731 \ 'call matchadd("Tdarkmagenta", "F")',
1732 \ 'call matchadd("Tdarkcyan", "G")',
1733 \ 'call matchadd("Tlightgrey", "H")',
1734 \ 'call matchadd("Tdarkgrey", "I")',
1735 \ 'call matchadd("Tred", "J")',
1736 \ 'call matchadd("Tgreen", "K")',
1737 \ 'call matchadd("Tyellow", "L")',
1738 \ 'call matchadd("Tblue", "M")',
1739 \ 'call matchadd("Tmagenta", "N")',
1740 \ 'call matchadd("Tcyan", "O")',
1741 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001742 \ 'call matchadd("TdarkredBold", "X")',
1743 \ 'call matchadd("TgreenBold", "Y")',
1744 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001745 \ 'redraw',
1746 \ ], 'Xcolorscript')
1747 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1748 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1749
1750 call term_sendkeys(buf, ":q\<CR>")
1751 call StopVimInTerminal(buf)
1752 call delete('Xcolorscript')
1753endfunc
1754
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001755func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001756 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001757 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001758 let text = []
1759 for n in range(10)
1760 call add(text, repeat(n, 50))
1761 endfor
1762 call writefile(text, 'Xwinsize')
1763 let buf = RunVimInTerminal('Xwinsize', {})
1764 let win = bufwinid(buf)
1765 call assert_equal([6, 40], term_getsize(buf))
1766 call assert_equal(6, winheight(win))
1767 call assert_equal(40, winwidth(win))
1768
1769 " resizing the window doesn't resize the terminal.
1770 resize 10
1771 vertical resize 60
1772 call assert_equal([6, 40], term_getsize(buf))
1773 call assert_equal(10, winheight(win))
1774 call assert_equal(60, winwidth(win))
1775
1776 call StopVimInTerminal(buf)
1777 call delete('Xwinsize')
1778
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001779 call assert_fails('set termwinsize=40', 'E474')
1780 call assert_fails('set termwinsize=10+40', 'E474')
1781 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001782
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001783 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001784endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001785
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001786func Test_terminal_termwinsize_option_zero()
1787 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001788 let buf = Run_shell_in_terminal({})
1789 let win = bufwinid(buf)
1790 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001791 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001792 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001793 exe buf . 'bwipe'
1794
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001795 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001796 let buf = Run_shell_in_terminal({})
1797 let win = bufwinid(buf)
1798 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001799 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001800 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001801 exe buf . 'bwipe'
1802
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001803 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001804 let buf = Run_shell_in_terminal({})
1805 let win = bufwinid(buf)
1806 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001807 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001808 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001809 exe buf . 'bwipe'
1810
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001811 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001812endfunc
1813
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001814func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001815 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001816 vsplit
1817 let buf = Run_shell_in_terminal({})
1818 let win = bufwinid(buf)
1819 call assert_inrange(10, 1000, winheight(win))
1820 call assert_inrange(50, 1000, winwidth(win))
1821 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1822
1823 resize 15
1824 vertical resize 60
1825 redraw
1826 call assert_equal([15, 60], term_getsize(buf))
1827 call assert_equal(15, winheight(win))
1828 call assert_equal(60, winwidth(win))
1829
1830 resize 7
1831 vertical resize 30
1832 redraw
1833 call assert_equal([10, 50], term_getsize(buf))
1834 call assert_equal(7, winheight(win))
1835 call assert_equal(30, winwidth(win))
1836
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001837 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001838 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001839 exe buf . 'bwipe'
1840
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001841 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001842 let buf = Run_shell_in_terminal({})
1843 let win = bufwinid(buf)
1844 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001845 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001846 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001847 exe buf . 'bwipe'
1848
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001849 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001850endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001851
1852func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001853 " make three tabpages, terminal in the middle
1854 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001855 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001856 tabnew
1857 tabprev
1858 call assert_equal(1, winnr('$'))
1859 call assert_equal(2, tabpagenr())
1860 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001861
1862 let buf = Run_shell_in_terminal({})
1863 let termwin = bufwinid(buf)
1864 set termwinkey=<C-L>
1865 call feedkeys("\<C-L>w", 'tx')
1866 call assert_equal(thiswin, win_getid())
1867 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001868 call assert_equal(termwin, win_getid())
1869
Bram Moolenaar997d4242019-09-14 21:23:40 +02001870 if has('langmap')
1871 set langmap=xjyk
1872 call feedkeys("\<C-L>x", 'tx')
1873 call assert_equal(thiswin, win_getid())
1874 call feedkeys("\<C-W>y", 'tx')
1875 call assert_equal(termwin, win_getid())
1876 set langmap=
1877 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001878
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001879 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001880 call assert_equal(3, tabpagenr())
1881 tabprev
1882 call assert_equal(2, tabpagenr())
1883 call assert_equal(termwin, win_getid())
1884
1885 call feedkeys("\<C-L>gT", "xt")
1886 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001887 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001888 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001889 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001890
1891 let job = term_getjob(buf)
1892 call feedkeys("\<C-L>\<C-C>", 'tx')
1893 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001894
1895 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001896 tabnext
1897 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001898 tabprev
1899 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001900endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001901
1902func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001903 CheckUnix
1904
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001905 call writefile([
1906 \ '#!/bin/sh',
1907 \ 'echo "this is standard error" >&2',
1908 \ 'echo "this is standard out" >&1',
1909 \ ], 'Xechoerrout.sh')
1910 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1911
1912 let outfile = 'Xtermstdout'
1913 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001914
1915 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1916 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001917 call assert_equal('this is standard error', term_getline(buf, 1))
1918
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001919 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001920 exe buf . 'bwipe'
1921 call delete('Xechoerrout.sh')
1922 call delete(outfile)
1923endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001924
Bram Moolenaare219f732019-11-30 15:34:08 +01001925func Test_termwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001926 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001927
1928 " Let the terminal output more than 'termwinscroll' lines, some at the start
1929 " will be dropped.
1930 exe 'set termwinscroll=' . &lines
1931 let buf = term_start('/bin/sh')
1932 for i in range(1, &lines)
1933 call feedkeys("echo " . i . "\<CR>", 'xt')
1934 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1935 endfor
1936 " Go to Terminal-Normal mode to update the buffer.
1937 call feedkeys("\<C-W>N", 'xt')
1938 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1939
1940 " Every "echo nr" must only appear once
1941 let lines = getline(1, line('$'))
1942 for i in range(&lines - len(lines) / 2 + 2, &lines)
1943 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1944 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1945 endfor
1946
1947 exe buf . 'bwipe!'
1948endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001949
Bram Moolenaar875cf872018-07-08 20:49:07 +02001950" Resizing the terminal window caused an ml_get error.
1951" TODO: This does not reproduce the original problem.
1952func Test_terminal_resize()
1953 set statusline=x
1954 terminal
1955 call assert_equal(2, winnr('$'))
1956
1957 " Fill the terminal with text.
1958 if has('win32')
1959 call feedkeys("dir\<CR>", 'xt')
1960 else
1961 call feedkeys("ls\<CR>", 'xt')
1962 endif
1963 " Go to Terminal-Normal mode for a moment.
1964 call feedkeys("\<C-W>N", 'xt')
1965 " Open a new window
1966 call feedkeys("i\<C-W>n", 'xt')
1967 call assert_equal(3, winnr('$'))
1968 redraw
1969
1970 close
1971 call assert_equal(2, winnr('$'))
1972 call feedkeys("exit\<CR>", 'xt')
1973 set statusline&
1974endfunc
1975
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001976" must be nearly the last, we can't go back from GUI to terminal
1977func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001978 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001979
1980 " Ignore the "failed to create input context" error.
1981 call test_ignore_error('E285:')
1982
1983 gui -f
1984
1985 call assert_equal(1, winnr('$'))
1986 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001987 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001988 call TermWait(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001989
1990 " closing window wipes out the terminal buffer a with finished job
1991 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1992 call assert_equal("", bufname(buf))
1993
1994 unlet g:job
1995endfunc
1996
1997func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001998 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001999 set guioptions+=!
2000
2001 let filename = 'Xtestscript'
2002 if has('win32')
2003 let filename .= '.bat'
2004 let prefix = ''
2005 let contents = ['@echo off', 'exit %1']
2006 else
2007 let filename .= '.sh'
2008 let prefix = './'
2009 let contents = ['#!/bin/sh', 'exit $1']
2010 endif
2011 call writefile(contents, filename)
2012 call setfperm(filename, 'rwxrwx---')
2013
2014 " Check if v:shell_error is equal to the exit status.
2015 let exitval = 0
2016 execute printf(':!%s%s %d', prefix, filename, exitval)
2017 call assert_equal(exitval, v:shell_error)
2018
2019 let exitval = 9
2020 execute printf(':!%s%s %d', prefix, filename, exitval)
2021 call assert_equal(exitval, v:shell_error)
2022
2023 set guioptions&
2024 call delete(filename)
2025endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002026
2027func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002028 CheckUnix
2029
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002030 term ++hidden cat
2031 let bnr = bufnr('$')
2032 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2033 exe 'sbuf ' . bnr
2034 call assert_equal('terminal', &buftype)
2035 call term_sendkeys(bnr, "asdf\<CR>")
2036 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
2037 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002038 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002039 bwipe!
2040endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002041
Bram Moolenaara4c2a242019-03-25 23:01:38 +01002042func Test_terminal_switch_mode()
2043 term
2044 let bnr = bufnr('$')
2045 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2046 call feedkeys("\<C-W>N", 'xt')
2047 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2048 call feedkeys("A", 'xt')
2049 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaar98f16712020-05-22 13:34:01 +02002050 call feedkeys("\<C-\>\<C-N>", 'xt')
Bram Moolenaara4c2a242019-03-25 23:01:38 +01002051 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2052 call feedkeys("I", 'xt')
2053 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2054 call feedkeys("\<C-W>Nv", 'xt')
2055 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2056 call feedkeys("I", 'xt')
2057 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2058 call feedkeys("\<C-W>Nv", 'xt')
2059 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2060 call feedkeys("A", 'xt')
2061 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2062 bwipe!
2063endfunc
2064
Bram Moolenaara3817732019-10-16 16:31:44 +02002065func Test_terminal_normal_mode()
2066 CheckRunVimInTerminal
2067
2068 " Run Vim in a terminal and open a terminal window to run Vim in.
2069 let lines =<< trim END
2070 call setline(1, range(11111, 11122))
2071 3
2072 END
2073 call writefile(lines, 'XtermNormal')
2074 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002075 call TermWait(buf)
Bram Moolenaara3817732019-10-16 16:31:44 +02002076
2077 call term_sendkeys(buf, "\<C-W>N")
2078 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
2079 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
2080
2081 call term_sendkeys(buf, ":set culopt=number\r")
2082 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
2083
2084 call term_sendkeys(buf, ":set culopt=line\r")
2085 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
2086
Bram Moolenaar98f16712020-05-22 13:34:01 +02002087 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
Bram Moolenaara3817732019-10-16 16:31:44 +02002088 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
2089 call StopVimInTerminal(buf)
2090 call delete('XtermNormal')
2091endfunc
2092
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002093func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002094 CheckUnix
2095
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002096 call assert_equal(1, winnr('$'))
2097 term ++hidden ++close ls
2098 let bnr = bufnr('$')
2099 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2100 call WaitForAssert({-> assert_false(bufexists(bnr))})
2101 call assert_equal(1, winnr('$'))
2102endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002103
2104func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002105 " This test does not pass through ConPTY.
2106 if has('conpty')
2107 return
2108 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002109 let contents = [
2110 \ [ 'One', '', 'X' ],
2111 \ [ 'Two', '', '' ],
2112 \ [ 'Three' ] + repeat([''], 30)
2113 \ ]
2114
2115 for c in contents
2116 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002117 if has('win32')
2118 term cmd /c type Xfile
2119 else
2120 term cat Xfile
2121 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002122 let bnr = bufnr('$')
2123 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2124 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002125 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002126 call assert_equal(c, getline(1, line('$')))
2127 quit
2128 endfor
2129
2130 call delete('Xfile')
2131endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002132
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002133func Test_terminal_no_job()
Bram Moolenaar077ff432019-10-28 00:42:21 +01002134 if has('win32')
2135 let cmd = 'cmd /c ""'
2136 else
2137 CheckExecutable false
2138 let cmd = 'false'
2139 endif
2140 let term = term_start(cmd, {'term_finish': 'close'})
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002141 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2142endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002143
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002144func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002145 CheckUnix
2146
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002147 let buf = Run_shell_in_terminal({})
2148
2149 " Wait for the shell to display a prompt.
2150 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2151
2152 " Hide the cursor.
2153 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2154 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2155
2156 " Show the cursor.
2157 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002158 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002159
2160 " Change color of cursor.
2161 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2162 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2163 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2164 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2165 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2166
2167 " Make cursor a blinking block.
2168 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2169 call WaitForAssert({-> assert_equal([1, 1],
2170 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2171
2172 " Make cursor a steady block.
2173 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2174 call WaitForAssert({-> assert_equal([0, 1],
2175 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2176
2177 " Make cursor a blinking underline.
2178 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2179 call WaitForAssert({-> assert_equal([1, 2],
2180 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2181
2182 " Make cursor a steady underline.
2183 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2184 call WaitForAssert({-> assert_equal([0, 2],
2185 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2186
2187 " Make cursor a blinking vertical bar.
2188 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2189 call WaitForAssert({-> assert_equal([1, 3],
2190 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2191
2192 " Make cursor a steady vertical bar.
2193 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2194 call WaitForAssert({-> assert_equal([0, 3],
2195 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2196
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002197 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002198endfunc
2199
Bram Moolenaar98f16712020-05-22 13:34:01 +02002200" Test for term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002201func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002202 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002203 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002204 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002205 call assert_equal('', term_gettitle(bufnr('$') + 1))
2206
Bram Moolenaar98f16712020-05-22 13:34:01 +02002207 if !has('title') || empty(&t_ts)
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002208 throw "Skipped: can't get/set title"
2209 endif
2210
Bram Moolenaar98f16712020-05-22 13:34:01 +02002211 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002212 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002213 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2214 call term_sendkeys(term, ":e Xfoo\r")
2215 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002216 else
2217 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002218 call term_sendkeys(term, ":e Xfoo\r")
2219 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002220 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002221
Bram Moolenaar1e115362019-01-09 23:01:02 +01002222 call term_sendkeys(term, ":set titlestring=foo\r")
2223 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2224
2225 exe term . 'bwipe!'
2226endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002227
Bram Moolenaar01603a92020-04-03 12:56:17 +02002228func Test_term_gettty()
2229 let buf = Run_shell_in_terminal({})
2230 let gettty = term_gettty(buf)
2231
2232 if has('unix') && executable('tty')
2233 " Find tty using the tty shell command.
2234 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2235 call term_sendkeys(buf, "tty\r")
2236 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
2237 let tty = term_getline(buf, 2)
2238 call assert_equal(tty, gettty)
2239 endif
2240
2241 let gettty0 = term_gettty(buf, 0)
2242 let gettty1 = term_gettty(buf, 1)
2243
2244 call assert_equal(gettty, gettty0)
2245 call assert_equal(job_info(g:job).tty_out, gettty0)
2246 call assert_equal(job_info(g:job).tty_in, gettty1)
2247
2248 if has('unix')
2249 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
2250 " are identical according to :help term_gettty()
2251 call assert_equal(gettty0, gettty1)
2252 call assert_match('^/dev/', gettty)
2253 else
2254 " ConPTY works on anonymous pipe.
2255 if !has('conpty')
2256 call assert_match('^\\\\.\\pipe\\', gettty0)
2257 call assert_match('^\\\\.\\pipe\\', gettty1)
2258 endif
2259 endif
2260
2261 call assert_fails('call term_gettty(buf, 2)', 'E475:')
2262 call assert_fails('call term_gettty(buf, -1)', 'E475:')
2263
2264 call assert_equal('', term_gettty(buf + 1))
2265
2266 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002267 call TermWait(buf)
Bram Moolenaar01603a92020-04-03 12:56:17 +02002268 exe buf . 'bwipe'
2269endfunc
2270
Bram Moolenaar10772302019-01-20 18:25:54 +01002271" When drawing the statusline the cursor position may not have been updated
2272" yet.
2273" 1. create a terminal, make it show 2 lines
2274" 2. 0.5 sec later: leave terminal window, execute "i"
2275" 3. 0.5 sec later: clear terminal window, now it's 1 line
2276" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2277" 4. 0.5 sec later: should be done, clean up
2278func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002279 CheckUnix
2280
Bram Moolenaar10772302019-01-20 18:25:54 +01002281 set statusline=x
2282 terminal
2283 let tbuf = bufnr('')
2284 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2285 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2286 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2287 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2288
2289 sleep 2
2290 exe tbuf . 'bwipe!'
2291 au! BufLeave
2292 set statusline=
2293endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002294
2295func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002296 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002297
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002298 " split, go to the bottom-right window
2299 split
2300 wincmd j
2301 set splitright
2302
2303 call writefile([
2304 \ 'echo getwinpos()',
2305 \ ], 'XTest_getwinpos')
2306 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002307 call TermWait(buf)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002308
2309 " Find the output of getwinpos() in the bottom line.
2310 let rows = term_getsize(buf)[0]
2311 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2312 let line = term_getline(buf, rows)
2313 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2314 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2315
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002316 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002317 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002318 " In the GUI it can be more, let's assume a 20 x 14 cell.
2319 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002320 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002321 let winpos = 50->getwinpos()
2322 call assert_equal(xroot, winpos[0])
2323 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002324 let [winrow, wincol] = win_screenpos('.')
2325 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2326 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2327 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2328 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002329
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002330 call TermWait(buf)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002331 call term_sendkeys(buf, ":q\<CR>")
2332 call StopVimInTerminal(buf)
2333 call delete('XTest_getwinpos')
2334 exe buf . 'bwipe!'
2335 set splitright&
2336 only!
2337endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002338
2339func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002340 " somehow doesn't work on MS-Windows
2341 CheckUnix
2342 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002343
2344 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002345 call writefile(["\<Esc>[?1047h"], 'Xtext')
2346 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002347 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2348
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002349 call writefile(["\<Esc>[?1047l"], 'Xtext')
2350 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002351 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002352
2353 call term_sendkeys(buf, "exit\r")
2354 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002355 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002356endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002357
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002358func Test_terminal_shell_option()
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002359 if has('unix')
2360 " exec is a shell builtin command, should fail without a shell.
2361 term exec ls runtest.vim
2362 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
2363 bwipe!
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002364
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002365 term ++shell exec ls runtest.vim
2366 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2367 bwipe!
2368 elseif has('win32')
2369 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar36ec6f62019-11-05 22:38:47 +01002370 try
2371 term dir /b runtest.vim
2372 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
2373 catch /CreateProcess/
2374 " ignore
2375 endtry
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002376 bwipe!
2377
2378 term ++shell dir /b runtest.vim
2379 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2380 bwipe!
2381 endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002382endfunc
2383
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002384func Test_terminal_setapi_and_call()
Bram Moolenaar21109272020-01-30 16:27:20 +01002385 CheckRunVimInTerminal
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002386
2387 call WriteApiCall('Tapi_TryThis')
2388 call ch_logfile('Xlog', 'w')
2389
2390 unlet! g:called_bufnum
2391 unlet! g:called_arg
2392
Bram Moolenaar21109272020-01-30 16:27:20 +01002393 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002394 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2395 call assert_false(exists('g:called_bufnum'))
2396 call assert_false(exists('g:called_arg'))
2397
Bram Moolenaar21109272020-01-30 16:27:20 +01002398 eval buf->term_setapi('Tapi_')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002399 call term_sendkeys(buf, ":set notitle\<CR>")
2400 call term_sendkeys(buf, ":source Xscript\<CR>")
2401 call WaitFor({-> exists('g:called_bufnum')})
2402 call assert_equal(buf, g:called_bufnum)
2403 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar21109272020-01-30 16:27:20 +01002404
2405 call StopVimInTerminal(buf)
2406
2407 call delete('Xscript')
2408 call ch_logfile('')
2409 call delete('Xlog')
2410 unlet! g:called_bufnum
2411 unlet! g:called_arg
2412endfunc
2413
2414func Test_terminal_api_arg()
2415 CheckRunVimInTerminal
2416
2417 call WriteApiCall('Tapi_TryThis')
2418 call ch_logfile('Xlog', 'w')
2419
2420 unlet! g:called_bufnum
2421 unlet! g:called_arg
2422
2423 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2424 let buf = bufnr('%')
2425 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2426 call assert_false(exists('g:called_bufnum'))
2427 call assert_false(exists('g:called_arg'))
2428
2429 call StopVimInTerminal(buf)
2430
2431 call ch_logfile('Xlog', 'w')
2432
2433 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2434 let buf = bufnr('%')
2435 call WaitFor({-> exists('g:called_bufnum')})
2436 call assert_equal(buf, g:called_bufnum)
2437 call assert_equal(['hello', 123], g:called_arg)
2438
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002439 call StopVimInTerminal(buf)
2440
2441 call delete('Xscript')
2442 call ch_logfile('')
2443 call delete('Xlog')
2444 unlet! g:called_bufnum
2445 unlet! g:called_arg
2446endfunc
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002447
Bram Moolenaar9b9be002020-03-22 14:41:22 +01002448func Test_terminal_invalid_arg()
2449 call assert_fails('terminal ++xyz', 'E181:')
2450endfunc
2451
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002452func Test_terminal_in_popup()
2453 CheckRunVimInTerminal
2454
2455 let text =<< trim END
2456 some text
2457 to edit
2458 in a popup window
2459 END
2460 call writefile(text, 'Xtext')
Bram Moolenaar3180fe62020-02-02 13:47:06 +01002461 let cmd = GetVimCommandCleanTerm()
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002462 let lines = [
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01002463 \ 'set t_u7=',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002464 \ 'call setline(1, range(20))',
2465 \ 'hi PopTerm ctermbg=grey',
2466 \ 'func OpenTerm(setColor)',
Bram Moolenaar353c3512020-03-15 14:19:26 +01002467 \ " set noruler",
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002468 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
Bram Moolenaar57c33952020-02-29 16:09:16 +01002469 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002470 \ ' if a:setColor',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002471 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002472 \ ' endif',
2473 \ 'endfunc',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002474 \ 'func HidePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002475 \ ' call popup_hide(g:winid)',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002476 \ 'endfunc',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002477 \ 'func ClosePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002478 \ ' call popup_close(g:winid)',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002479 \ 'endfunc',
2480 \ 'func ReopenPopup()',
2481 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
2482 \ 'endfunc',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002483 \ ]
2484 call writefile(lines, 'XtermPopup')
2485 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002486 call TermWait(buf, 100)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002487 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002488 call TermWait(buf, 100)
Bram Moolenaarc2adc002020-02-14 17:05:18 +01002489 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002490 call TermWait(buf, 100)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002491 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002492 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
2493
2494 call term_sendkeys(buf, ":q\<CR>")
2495 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
2496
2497 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002498 call TermWait(buf, 150)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002499 call term_sendkeys(buf, ":set hlsearch\<CR>")
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002500 call TermWait(buf, 100)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002501 call term_sendkeys(buf, "/edit\<CR>")
2502 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
2503
Bram Moolenaar1af5ce02020-02-06 11:54:35 +01002504 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
2505 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
2506 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002507 call TermWait(buf, 50)
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002508
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002509 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
2510 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
2511
2512 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
2513 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002514
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002515 " Go to terminal-Normal mode and visually select text.
2516 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
2517 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
2518
2519 " Back to job mode, redraws
2520 call term_sendkeys(buf, "A")
2521 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
2522
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002523 call TermWait(buf, 50)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002524 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002525 call TermWait(buf, 150) " wait for terminal to vanish
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002526
2527 call StopVimInTerminal(buf)
Bram Moolenaar19398262020-03-14 15:28:08 +01002528 call delete('Xtext')
2529 call delete('XtermPopup')
2530endfunc
2531
2532" Check a terminal in popup window uses the default mininum size.
2533func Test_terminal_in_popup_min_size()
2534 CheckRunVimInTerminal
2535
2536 let text =<< trim END
2537 another text
2538 to show
2539 in a popup window
2540 END
2541 call writefile(text, 'Xtext')
2542 let lines = [
2543 \ 'set t_u7=',
2544 \ 'call setline(1, range(20))',
Bram Moolenaar19398262020-03-14 15:28:08 +01002545 \ 'func OpenTerm()',
2546 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
2547 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2548 \ 'endfunc',
2549 \ ]
2550 call writefile(lines, 'XtermPopup')
2551 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002552 call TermWait(buf, 100)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002553 call term_sendkeys(buf, ":set noruler\<CR>")
Bram Moolenaar19398262020-03-14 15:28:08 +01002554 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002555 call TermWait(buf, 50)
Bram Moolenaar19398262020-03-14 15:28:08 +01002556 call term_sendkeys(buf, ":\<CR>")
2557 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
2558
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002559 call TermWait(buf, 50)
Bram Moolenaar19398262020-03-14 15:28:08 +01002560 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002561 call TermWait(buf, 50) " wait for terminal to vanish
Bram Moolenaar19398262020-03-14 15:28:08 +01002562 call StopVimInTerminal(buf)
2563 call delete('Xtext')
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002564 call delete('XtermPopup')
2565endfunc
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002566
Bram Moolenaar83d47902020-03-26 20:34:00 +01002567" Check a terminal in popup window with different colors
2568func Terminal_in_popup_colored(group_name, highlight_cmd, highlight_opt)
2569 CheckRunVimInTerminal
2570 CheckUnix
2571
2572 let lines = [
2573 \ 'set t_u7=',
2574 \ 'call setline(1, range(20))',
2575 \ 'func OpenTerm()',
2576 \ " let s:buf = term_start('cat', #{hidden: 1, "
2577 \ .. a:highlight_opt .. "})",
2578 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2579 \ 'endfunc',
2580 \ a:highlight_cmd,
2581 \ ]
2582 call writefile(lines, 'XtermPopup')
2583 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002584 call TermWait(buf, 100)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002585 call term_sendkeys(buf, ":set noruler\<CR>")
2586 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002587 call TermWait(buf, 50)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002588 call term_sendkeys(buf, "hello\<CR>")
2589 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
2590
2591 call term_sendkeys(buf, "\<C-D>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002592 call TermWait(buf, 50)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002593 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002594 call TermWait(buf, 50) " wait for terminal to vanish
Bram Moolenaar83d47902020-03-26 20:34:00 +01002595 call StopVimInTerminal(buf)
2596 call delete('XtermPopup')
2597endfunc
2598
2599func Test_terminal_in_popup_colored_Terminal()
2600 call Terminal_in_popup_colored("Terminal", "highlight Terminal ctermfg=blue ctermbg=yellow", "")
2601endfunc
2602
2603func Test_terminal_in_popup_colored_group()
2604 call Terminal_in_popup_colored("MyTermCol", "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue", "term_highlight: 'MyTermCol',")
2605endfunc
2606
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002607func Test_double_popup_terminal()
2608 let buf1 = term_start(&shell, #{hidden: 1})
2609 let win1 = popup_create(buf1, {})
2610 let buf2 = term_start(&shell, #{hidden: 1})
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002611 call assert_fails('call popup_create(buf2, {})', 'E861:')
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002612 call popup_close(win1)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002613 exe buf1 .. 'bwipe!'
2614 exe buf2 .. 'bwipe!'
2615endfunc
2616
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002617func Test_issue_5607()
2618 let wincount = winnr('$')
2619 exe 'terminal' &shell &shellcmdflag 'exit'
2620 let job = term_getjob(bufnr())
2621 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2622
2623 let old_wincolor = &wincolor
2624 try
2625 set wincolor=
2626 finally
2627 let &wincolor = old_wincolor
2628 bw!
2629 endtry
2630endfunc
Bram Moolenaare010c722020-02-24 21:37:54 +01002631
2632func Test_hidden_terminal()
2633 let buf = term_start(&shell, #{hidden: 1})
2634 call assert_equal('', bufname('^$'))
2635 call StopShellInTerminal(buf)
2636endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002637
2638func Test_term_nasty_callback()
Bram Moolenaara161cb52020-04-30 19:09:35 +02002639 CheckExecutable sh
Bram Moolenaarcee52202020-03-11 14:19:58 +01002640
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002641 set hidden
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002642 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002643 call popup_create(g:buf0, {})
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002644 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
2645
2646 call popup_clear(1)
Bram Moolenaarcee52202020-03-11 14:19:58 +01002647 set hidden&
2648endfunc
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002649
Bram Moolenaareb58a242020-04-19 18:13:19 +02002650func Test_term_and_startinsert()
2651 CheckRunVimInTerminal
2652 CheckUnix
2653
2654 let lines =<< trim EOL
2655 put='some text'
2656 term
2657 startinsert
2658 EOL
2659 call writefile(lines, 'XTest_startinsert')
2660 let buf = RunVimInTerminal('-S XTest_startinsert', {})
2661
2662 call term_sendkeys(buf, "exit\r")
2663 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
2664 call term_sendkeys(buf, "0l")
2665 call term_sendkeys(buf, "A<\<Esc>")
2666 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
2667
2668 call StopVimInTerminal(buf)
2669 call delete('XTest_startinsert')
2670endfunc
2671
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002672" Test for passing invalid arguments to terminal functions
2673func Test_term_func_invalid_arg()
2674 call assert_fails('let b = term_getaltscreen([])', 'E745:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002675 call assert_fails('let a = term_getattr(1, [])', 'E730:')
2676 call assert_fails('let c = term_getcursor([])', 'E745:')
2677 call assert_fails('let l = term_getline([], 1)', 'E745:')
2678 call assert_fails('let l = term_getscrolled([])', 'E745:')
2679 call assert_fails('let s = term_getsize([])', 'E745:')
2680 call assert_fails('let s = term_getstatus([])', 'E745:')
2681 call assert_fails('let s = term_scrape([], 1)', 'E745:')
2682 call assert_fails('call term_sendkeys([], "a")', 'E745:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002683 call assert_fails('call term_setapi([], "")', 'E745:')
2684 call assert_fails('call term_setrestore([], "")', 'E745:')
2685 call assert_fails('call term_setkill([], "")', 'E745:')
Bram Moolenaar87202262020-05-24 17:23:45 +02002686 if has('gui') || has('termguicolors')
2687 call assert_fails('let p = term_getansicolors([])', 'E745:')
2688 call assert_fails('call term_setansicolors([], [])', 'E745:')
2689 endif
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002690endfunc
2691
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002692" Test for sending various special keycodes to a terminal
2693func Test_term_keycode_translation()
2694 CheckRunVimInTerminal
2695
2696 let buf = RunVimInTerminal('', {})
2697 call term_sendkeys(buf, ":set nocompatible\<CR>")
2698
2699 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
2700 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
2701 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
2702 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
2703 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
2704 \ "\<S-Down>"]
2705 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
2706 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
2707 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
2708 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
Bram Moolenaarfe813892020-05-21 20:38:31 +02002709 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002710
Bram Moolenaarfe813892020-05-21 20:38:31 +02002711 call term_sendkeys(buf, "i")
2712 for i in range(len(keys))
2713 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002714 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))})
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002715 endfor
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002716
Bram Moolenaarfe813892020-05-21 20:38:31 +02002717 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
2718 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
2719 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
2720 let keypad_output = ['0', '1', '2', '3', '4', '5',
2721 \ '6', '7', '8', '9', '.', '+',
2722 \ '-', '*', '/']
2723 for i in range(len(keypad_keys))
2724 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
2725 if keypad_output[i] == '3' || keypad_output[i] == '9'
2726 continue
2727 endif
2728 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002729 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))})
Bram Moolenaarfe813892020-05-21 20:38:31 +02002730 endfor
2731
2732 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002733 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
Bram Moolenaarfe813892020-05-21 20:38:31 +02002734
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002735 call StopVimInTerminal(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002736endfunc
2737
2738" Test for using the mouse in a terminal
2739func Test_term_mouse()
2740 CheckNotGui
2741 CheckRunVimInTerminal
2742
2743 let save_mouse = &mouse
2744 let save_term = &term
2745 let save_ttymouse = &ttymouse
2746 let save_clipboard = &clipboard
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002747 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
2748
2749 let lines =<< trim END
2750 one two three four five
2751 red green yellow red blue
2752 vim emacs sublime nano
2753 END
2754 call writefile(lines, 'Xtest_mouse')
2755
Bram Moolenaar98f16712020-05-22 13:34:01 +02002756 " Create a terminal window running Vim for the test with mouse enabled
2757 let prev_win = win_getid()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002758 let buf = RunVimInTerminal('Xtest_mouse -n', {})
2759 call term_sendkeys(buf, ":set nocompatible\<CR>")
2760 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
2761 call term_sendkeys(buf, ":set clipboard=\<CR>")
2762 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
2763 call term_wait(buf)
2764 redraw!
2765
Bram Moolenaar98f16712020-05-22 13:34:01 +02002766 " Use the mouse to enter the terminal window
2767 call win_gotoid(prev_win)
2768 call feedkeys(MouseLeftClickCode(1, 1), 'x')
2769 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
2770 call assert_equal(1, getwininfo(win_getid())[0].terminal)
2771
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002772 " Test for <LeftMouse> click/release
2773 call test_setmouse(2, 5)
2774 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
2775 call test_setmouse(3, 8)
2776 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
2777 call term_wait(buf, 50)
2778 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
2779 call term_wait(buf, 50)
2780 let pos = json_decode(readfile('Xbuf')[0])
2781 call assert_equal([3, 8], pos[1:2])
2782
2783 " Test for selecting text using mouse
2784 call delete('Xbuf')
2785 call test_setmouse(2, 11)
2786 call term_sendkeys(buf, "\<LeftMouse>")
2787 call test_setmouse(2, 16)
2788 call term_sendkeys(buf, "\<LeftRelease>y")
2789 call term_wait(buf, 50)
2790 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2791 call term_wait(buf, 50)
2792 call assert_equal('yellow', readfile('Xbuf')[0])
2793
2794 " Test for selecting text using doubleclick
2795 call delete('Xbuf')
2796 call test_setmouse(1, 11)
2797 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
2798 call test_setmouse(1, 17)
2799 call term_sendkeys(buf, "\<LeftRelease>y")
2800 call term_wait(buf, 50)
2801 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2802 call term_wait(buf, 50)
2803 call assert_equal('three four', readfile('Xbuf')[0])
2804
2805 " Test for selecting a line using triple click
2806 call delete('Xbuf')
2807 call test_setmouse(3, 2)
2808 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
2809 call term_wait(buf, 50)
2810 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2811 call term_wait(buf, 50)
2812 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
2813
2814 " Test for selecting a block using qudraple click
2815 call delete('Xbuf')
2816 call test_setmouse(1, 11)
2817 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
2818 call test_setmouse(3, 13)
2819 call term_sendkeys(buf, "\<LeftRelease>y")
2820 call term_wait(buf, 50)
2821 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2822 call term_wait(buf, 50)
2823 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
2824
2825 " Test for extending a selection using right click
2826 call delete('Xbuf')
2827 call test_setmouse(2, 9)
2828 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
2829 call test_setmouse(2, 16)
2830 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
2831 call term_wait(buf, 50)
2832 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2833 call term_wait(buf, 50)
2834 call assert_equal("n yellow", readfile('Xbuf')[0])
2835
2836 " Test for pasting text using middle click
2837 call delete('Xbuf')
2838 call term_sendkeys(buf, ":let @r='bright '\<CR>")
2839 call test_setmouse(2, 22)
2840 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
2841 call term_wait(buf, 50)
2842 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
2843 call term_wait(buf, 50)
2844 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
2845
2846 " cleanup
2847 call term_wait(buf)
2848 call StopVimInTerminal(buf)
2849 let &mouse = save_mouse
2850 let &term = save_term
2851 let &ttymouse = save_ttymouse
2852 let &clipboard = save_clipboard
2853 set mousetime&
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002854 call delete('Xtest_mouse')
2855 call delete('Xbuf')
2856endfunc
2857
2858" Test for modeless selection in a terminal
2859func Test_term_modeless_selection()
2860 CheckUnix
2861 CheckNotGui
2862 CheckRunVimInTerminal
2863 CheckFeature clipboard_working
2864
2865 let save_mouse = &mouse
2866 let save_term = &term
2867 let save_ttymouse = &ttymouse
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002868 set mouse=a term=xterm ttymouse=sgr mousetime=200
2869 set clipboard=autoselectml
2870
2871 let lines =<< trim END
2872 one two three four five
2873 red green yellow red blue
2874 vim emacs sublime nano
2875 END
2876 call writefile(lines, 'Xtest_modeless')
2877
Bram Moolenaar98f16712020-05-22 13:34:01 +02002878 " Create a terminal window running Vim for the test with mouse disabled
2879 let prev_win = win_getid()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002880 let buf = RunVimInTerminal('Xtest_modeless -n', {})
2881 call term_sendkeys(buf, ":set nocompatible\<CR>")
2882 call term_sendkeys(buf, ":set mouse=\<CR>")
2883 call term_wait(buf)
2884 redraw!
2885
Bram Moolenaar98f16712020-05-22 13:34:01 +02002886 " Use the mouse to enter the terminal window
2887 call win_gotoid(prev_win)
2888 call feedkeys(MouseLeftClickCode(1, 1), 'x')
2889 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
2890 call term_wait(buf)
2891 call assert_equal(1, getwininfo(win_getid())[0].terminal)
2892
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002893 " Test for copying a modeless selection to clipboard
2894 let @* = 'clean'
2895 " communicating with X server may take a little time
2896 sleep 100m
2897 call feedkeys(MouseLeftClickCode(2, 3), 'x')
2898 call feedkeys(MouseLeftDragCode(2, 11), 'x')
2899 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
2900 call assert_equal("d green y", @*)
2901
2902 " cleanup
2903 call term_wait(buf)
2904 call StopVimInTerminal(buf)
2905 let &mouse = save_mouse
2906 let &term = save_term
2907 let &ttymouse = save_ttymouse
2908 set mousetime& clipboard&
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002909 call delete('Xtest_modeless')
Bram Moolenaar98f16712020-05-22 13:34:01 +02002910 new | only!
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002911endfunc
2912
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002913" vim: shiftwidth=2 sts=2 expandtab