blob: bbc6b90ab3545cd41b7d274a1baab416d208c462 [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
Bram Moolenaarb1009092020-05-31 16:04:42 +0200453 split Xdummy
454 call setline(1, 'dummy')
455 write
456 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200457 exe 'terminal ++curwin ' . cmd
458 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200459 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200460 bwipe!
461
Bram Moolenaarb1009092020-05-31 16:04:42 +0200462 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200463 call term_start(cmd, {'curwin': 1})
464 call assert_equal(2, winnr('$'))
465 bwipe!
466
Bram Moolenaarb1009092020-05-31 16:04:42 +0200467 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200468 call setline(1, 'change')
469 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
470 call assert_equal(2, winnr('$'))
471 exe 'terminal! ++curwin ' . cmd
472 call assert_equal(2, winnr('$'))
473 bwipe!
474
Bram Moolenaarb1009092020-05-31 16:04:42 +0200475 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200476 call setline(1, 'change')
477 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
478 call assert_equal(2, winnr('$'))
479 bwipe!
480
Bram Moolenaarb1009092020-05-31 16:04:42 +0200481 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200482 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200483 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200484 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200485endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200486
Bram Moolenaarff546792017-11-21 14:47:57 +0100487func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200488 if s:python != ''
489 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200490 " 500 was not enough for Travis
491 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200492 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200493 echo 'This will take five seconds...'
494 let waittime = 2000
495 if has('win32')
496 let cmd = $windir . '\system32\timeout.exe 1'
497 else
498 let cmd = 'sleep 1'
499 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200500 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100501 return [cmd, waittime]
502endfunc
503
504func Test_terminal_finish_open_close()
505 call assert_equal(1, winnr('$'))
506
507 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200508
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100509 " shell terminal closes automatically
510 terminal
511 let buf = bufnr('%')
512 call assert_equal(2, winnr('$'))
513 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200514 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200515 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200516 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100517
518 " shell terminal that does not close automatically
519 terminal ++noclose
520 let buf = bufnr('%')
521 call assert_equal(2, winnr('$'))
522 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200523 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200524 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100525 call assert_equal(2, winnr('$'))
526 quit
527 call assert_equal(1, winnr('$'))
528
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200529 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200530 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
534 call term_start(cmd, {'term_finish': 'close'})
535 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200536 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200537 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200538 call assert_equal(1, winnr('$'))
539
540 exe 'terminal ++open ' . cmd
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 Moolenaardd693ce2017-08-10 23:15:19 +0200543 bwipe
544
545 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200546 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200547 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200548 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200549
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200550 exe 'terminal ++hidden ++open ' . cmd
551 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200552 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200553 bwipe
554
555 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
556 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200557 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200558 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200559
560 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
561 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
562 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
563 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
564
565 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200566 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200567 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200568 call assert_equal(4, winheight(0))
569 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200570endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200571
572func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100573 if has('win32')
574 let cmd = 'cmd /c cd'
575 else
576 CheckExecutable pwd
577 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200578 endif
579 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100580 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200581 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200582
583 exe buf . 'bwipe'
584 call delete('Xdir', 'rf')
585endfunc
586
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200587func Test_terminal_cwd_failure()
588 " Case 1: Provided directory is not actually a directory. Attempt to make
589 " the file executable as well.
590 call writefile([], 'Xfile')
591 call setfperm('Xfile', 'rwx------')
592 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
593 call delete('Xfile')
594
595 " Case 2: Directory does not exist.
596 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
597
598 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100599 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200600 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100601 call mkdir('XdirNoAccess', '', '0600')
602 " return early if the directory permissions could not be set properly
603 if getfperm('XdirNoAccess')[2] == 'x'
604 call delete('XdirNoAccess', 'rf')
605 return
606 endif
607 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
608 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200609 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200610endfunc
611
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100612func Test_terminal_servername()
613 if !has('clientserver')
614 return
615 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200616 call s:test_environment("VIM_SERVERNAME", v:servername)
617endfunc
618
619func Test_terminal_version()
620 call s:test_environment("VIM_TERMINAL", string(v:version))
621endfunc
622
623func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100624 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100625 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200626 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100627 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200628 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100629 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200630 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100631 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200632 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200633 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200634 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100635
Bram Moolenaar012eb662018-03-13 17:55:27 +0100636 exe buf . 'bwipe'
637 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100638endfunc
639
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200640func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100641 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200642 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200643 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100644 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100645 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100646 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100647 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100648 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200649 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200650 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200651 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200652
Bram Moolenaar012eb662018-03-13 17:55:27 +0100653 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200654endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200655
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200656func Test_terminal_list_args()
657 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
658 call assert_fails(buf . 'bwipe', 'E517')
659 exe buf . 'bwipe!'
660 call assert_equal("", bufname(buf))
661endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200662
663func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100664 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100665 let wait_time = 5000
666 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100667 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200668 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100669 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200670 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100671 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100672 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100673 " NPFS is used in Windows, nonblocking mode does not work properly.
674 elseif has('win32')
675 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200676 else
677 let len = 5000
678 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200679
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100680 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100681 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100682 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200683 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100684 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200685
686 " On MS-Windows there is an extra empty line below "done". Find "done" in
687 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100688 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100689 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100690 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200691 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100692 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200693 endif
694 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200695
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100696 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200697 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200698 call TermWait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200699 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200700 bwipe
701endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200702
703func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100704 " TODO: enable once writing to stdin works on MS-Windows
705 CheckNotMSWindows
706 CheckExecutable wc
707
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200708 call setline(1, ['one', 'two', 'three'])
709 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200710 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200711 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200712 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100713 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200714
715 call setline(1, ['one', 'two', 'three', 'four'])
716 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200717 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200718 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200719 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100720 %bwipe!
721endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200722
Bram Moolenaar21109272020-01-30 16:27:20 +0100723func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200724 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200725
Bram Moolenaar21109272020-01-30 16:27:20 +0100726 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200727 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100728 " MS-Windows echoes the input, Unix doesn't.
729 if has('win32')
730 call WaitFor({-> getline('$') =~ 'exit(123)'})
731 call assert_equal('hello', getline(line('$') - 1))
732 else
733 call WaitFor({-> getline('$') =~ 'hello'})
734 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200735 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100736 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
737 %bwipe!
738endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200739
Bram Moolenaar21109272020-01-30 16:27:20 +0100740func Test_terminal_eof_arg_win32_ctrl_z()
741 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200742 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100743
744 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200745 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100746 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
747 call assert_match('\^Z', getline(line('$') - 1))
748 %bwipe!
749endfunc
750
751func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200752 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100753
754 " Check the last specified ++eof arg is used and should not memory leak.
755 new
756 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200757 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100758 " MS-Windows echoes the input, Unix doesn't.
759 if has('win32')
760 call WaitFor({-> getline('$') =~ 'exit(123)'})
761 call assert_equal('hello', getline(line('$') - 1))
762 else
763 call WaitFor({-> getline('$') =~ 'hello'})
764 call assert_equal('hello', getline('$'))
765 endif
766 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
767 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200768endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200769
770func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200771 let buf = term_start('NONE', {})
772 call assert_notequal(0, buf)
773
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200774 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200775 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100776 if has('gui_running') && !has('win32')
777 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200778 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100779 else
780 " Otherwise using a job works on all systems.
781 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200782 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200783 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200784
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200785 bwipe!
786endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200787
788func Test_terminal_special_chars()
789 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200790 CheckUnix
791
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200792 call mkdir('Xdir with spaces')
793 call writefile(['x'], 'Xdir with spaces/quoted"file')
794 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200795 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100796 " make sure the job has finished
797 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200798
799 call delete('Xdir with spaces', 'rf')
800 bwipe
801endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200802
803func Test_terminal_wrong_options()
804 call assert_fails('call term_start(&shell, {
805 \ "in_io": "file",
806 \ "in_name": "xxx",
807 \ "out_io": "file",
808 \ "out_name": "xxx",
809 \ "err_io": "file",
810 \ "err_name": "xxx"
811 \ })', 'E474:')
812 call assert_fails('call term_start(&shell, {
813 \ "out_buf": bufnr("%")
814 \ })', 'E474:')
815 call assert_fails('call term_start(&shell, {
816 \ "err_buf": bufnr("%")
817 \ })', 'E474:')
818endfunc
819
820func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200821 let cmd = Get_cat_123_cmd()
822 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200823 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100824 " ConPTY may precede escape sequence. There are things that are not so.
825 if !has('conpty')
826 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
827 call assert_match('123', readfile('Xfile')[0])
828 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200829 let g:job = term_getjob(buf)
830 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
831 call delete('Xfile')
832 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200833
834 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200835 call writefile(['one line'], 'Xfile')
836 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200837 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200838 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200839 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200840 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200841 bwipe
842 call delete('Xfile')
843 endif
844endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200845
846func TerminalTmap(remap)
847 let buf = Run_shell_in_terminal({})
848 call assert_equal('t', mode())
849
850 if a:remap
851 tmap 123 456
852 else
853 tnoremap 123 456
854 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100855 " don't use abcde, it's an existing command
856 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200857 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100858 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200859 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200860 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200861 let lnum = term_getcursor(buf)[0]
862 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100863 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200864 else
865 call assert_match('456', term_getline(buf, lnum))
866 endif
867
868 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200869 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200870 call TermWait(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200871
872 tunmap 123
873 tunmap 456
874 call assert_equal('', maparg('123', 't'))
875 close
876 unlet g:job
877endfunc
878
879func Test_terminal_tmap()
880 call TerminalTmap(1)
881 call TerminalTmap(0)
882endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200883
884func Test_terminal_wall()
885 let buf = Run_shell_in_terminal({})
886 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200887 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200888 call TermWait(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200889 exe buf . 'bwipe'
890 unlet g:job
891endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200892
Bram Moolenaar7a760922018-02-19 23:10:02 +0100893func Test_terminal_wqall()
894 let buf = Run_shell_in_terminal({})
895 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200896 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200897 call TermWait(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100898 exe buf . 'bwipe'
899 unlet g:job
900endfunc
901
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200902func Test_terminal_composing_unicode()
903 let save_enc = &encoding
904 set encoding=utf-8
905
906 if has('win32')
907 let cmd = "cmd /K chcp 65001"
908 let lnum = [3, 6, 9]
909 else
910 let cmd = &shell
911 let lnum = [1, 3, 5]
912 endif
913
914 enew
915 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100916 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200917 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200918
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200919 if has('win32')
920 call assert_equal('cmd', job_info(g:job).cmd[0])
921 else
922 call assert_equal(&shell, job_info(g:job).cmd[0])
923 endif
924
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200925 " ascii + composing
926 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200927 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200928 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200929 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200930 call term_sendkeys(buf, "\<cr>")
931 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200932 let l = term_scrape(buf, lnum[0] + 1)
933 call assert_equal("a\u0308", l[0].chars)
934 call assert_equal("b", l[1].chars)
935 call assert_equal("c", l[2].chars)
936
937 " multibyte + composing
938 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200939 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200940 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200941 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200942 call term_sendkeys(buf, "\<cr>")
943 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200944 let l = term_scrape(buf, lnum[1] + 1)
945 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar79ea6802020-05-17 15:09:27 +0200946 call assert_equal("\u304e", l[2].chars)
947 call assert_equal("\u304f\u3099", l[3].chars)
948 call assert_equal("\u3052", l[5].chars)
949 call assert_equal("\u3053\u3099", l[6].chars)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200950
951 " \u00a0 + composing
952 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200953 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200954 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200955 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200956 call term_sendkeys(buf, "\<cr>")
957 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200958 let l = term_scrape(buf, lnum[2] + 1)
959 call assert_equal("\u00a0\u0308", l[3].chars)
960
961 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200962 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200963 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100964 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200965 let &encoding = save_enc
966endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100967
968func Test_terminal_aucmd_on_close()
969 fun Nop()
970 let s:called = 1
971 endfun
972
973 aug repro
974 au!
975 au BufWinLeave * call Nop()
976 aug END
977
978 let [cmd, waittime] = s:get_sleep_cmd()
979
980 call assert_equal(1, winnr('$'))
981 new
982 call setline(1, ['one', 'two'])
983 exe 'term ++close ' . cmd
984 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200985 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100986 call assert_equal(1, s:called)
987 bwipe!
988
989 unlet s:called
990 au! repro
991 delfunc Nop
992endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100993
994func Test_terminal_term_start_empty_command()
995 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
996 call assert_fails(cmd, 'E474')
997 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
998 call assert_fails(cmd, 'E474')
999 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
1000 call assert_fails(cmd, 'E474')
1001 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
1002 call assert_fails(cmd, 'E474')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001003 let cmd = "call term_start('', {'term_name' : []})"
1004 call assert_fails(cmd, 'E475')
1005 let cmd = "call term_start('', {'term_finish' : 'axby'})"
1006 call assert_fails(cmd, 'E475')
1007 let cmd = "call term_start('', {'eof_chars' : []})"
1008 call assert_fails(cmd, 'E475:')
1009 let cmd = "call term_start('', {'term_kill' : []})"
1010 call assert_fails(cmd, 'E475:')
1011 let cmd = "call term_start('', {'tty_type' : []})"
1012 call assert_fails(cmd, 'E475:')
1013 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1014 call assert_fails(cmd, 'E475:')
1015 let cmd = "call term_start('', {'term_highlight' : []})"
1016 call assert_fails(cmd, 'E475:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001017 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001018 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1019 call assert_fails(cmd, 'E475:')
1020 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1021 call assert_fails(cmd, 'E730:')
1022 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001023 if has('gui_running') || has('termguicolors')
1024 call assert_fails(cmd, 'E475:')
1025 else
1026 call assert_fails(cmd, 'E254:')
1027 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001028 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001029endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001030
1031func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001032 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001033
1034 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001035 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001036
Bram Moolenaar086eb872018-03-25 21:24:12 +02001037 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001038 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001039
Bram Moolenaar086eb872018-03-25 21:24:12 +02001040 " Request the cursor position.
1041 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001042
1043 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001044 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001045
Bram Moolenaar086eb872018-03-25 21:24:12 +02001046 " End "cat" gently.
1047 call term_sendkeys(buf, "\<CR>\<C-D>")
1048
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001049 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001050 exe buf . 'bwipe'
1051 unlet g:job
1052endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001053
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001054" Run Vim, start a terminal in that Vim with the kill argument,
1055" :qall works.
1056func Run_terminal_qall_kill(line1, line2)
1057 " 1. Open a terminal window and wait for the prompt to appear
1058 " 2. set kill using term_setkill()
1059 " 3. make Vim exit, it will kill the shell
1060 let after = [
1061 \ a:line1,
1062 \ 'let buf = bufnr("%")',
1063 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1064 \ ' sleep 10m',
1065 \ 'endwhile',
1066 \ a:line2,
1067 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1068 \ 'qall',
1069 \ ]
1070 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001071 return
1072 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001073 call assert_equal("done", readfile("Xdone")[0])
1074 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001075endfunc
1076
1077" Run Vim in a terminal, then start a terminal in that Vim with a kill
1078" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001079func Test_terminal_qall_kill_arg()
1080 call Run_terminal_qall_kill('term ++kill=kill', '')
1081endfunc
1082
1083" Run Vim, start a terminal in that Vim, set the kill argument with
1084" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001085func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001086 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001087endfunc
1088
1089" Run Vim, start a terminal in that Vim without the kill argument,
1090" check that :qall does not exit, :qall! does.
1091func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001092 let after =<< trim [CODE]
1093 term
1094 let buf = bufnr("%")
1095 while term_getline(buf, 1) =~ "^\\s*$"
1096 sleep 10m
1097 endwhile
1098 set nomore
1099 au VimLeavePre * call writefile(["too early"], "Xdone")
1100 qall
1101 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1102 cquit
1103 [CODE]
1104
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001105 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001106 return
1107 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001108 call assert_equal("done", readfile("Xdone")[0])
1109 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001110endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001111
1112" Run Vim in a terminal, then start a terminal in that Vim without a kill
1113" argument, check that :confirm qall works.
1114func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001115 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001116 let buf = RunVimInTerminal('', {})
1117
1118 " Open a terminal window and wait for the prompt to appear
1119 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001120 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1121 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001122
1123 " make Vim exit, it will prompt to kill the shell
1124 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001125 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001126 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001127 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001128
1129 " close the terminal window where Vim was running
1130 quit
1131endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001132
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001133" Run Vim in a terminal, then start a terminal window with a shell and check
1134" that Vim exits if it is closed.
1135func Test_terminal_exit()
1136 CheckRunVimInTerminal
1137
1138 let lines =<< trim END
1139 let winid = win_getid()
1140 help
1141 term
1142 let termid = win_getid()
1143 call win_gotoid(winid)
1144 close
1145 call win_gotoid(termid)
1146 END
1147 call writefile(lines, 'XtermExit')
1148 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1149 let job = term_getjob(buf)
1150 call WaitForAssert({-> assert_equal("run", job_status(job))})
1151
1152 " quit the shell, it will make Vim exit
1153 call term_sendkeys(buf, "exit\<CR>")
1154 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1155
1156 call delete('XtermExit')
1157endfunc
1158
Bram Moolenaar012eb662018-03-13 17:55:27 +01001159func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001160 augroup repro
1161 au!
1162 au TerminalOpen * let s:called += 1
1163 augroup END
1164
1165 let s:called = 0
1166
1167 " Open a terminal window with :terminal
1168 terminal
1169 call assert_equal(1, s:called)
1170 bwipe!
1171
1172 " Open a terminal window with term_start()
1173 call term_start(&shell)
1174 call assert_equal(2, s:called)
1175 bwipe!
1176
1177 " Open a hidden terminal buffer with :terminal
1178 terminal ++hidden
1179 call assert_equal(3, s:called)
1180 for buf in term_list()
1181 exe buf . "bwipe!"
1182 endfor
1183
1184 " Open a hidden terminal buffer with term_start()
1185 let buf = term_start(&shell, {'hidden': 1})
1186 call assert_equal(4, s:called)
1187 exe buf . "bwipe!"
1188
1189 unlet s:called
1190 au! repro
1191endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001192
1193func Check_dump01(off)
1194 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1195 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001196 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001197endfunc
1198
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001199func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001200 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001201 let save_enc = &encoding
1202 set encoding=utf-8
1203 call assert_equal(1, winnr('$'))
1204
1205 let text = " a\u0300 e\u0302 o\u0308"
1206 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001207 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001208 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001209 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001210 let dumpline = readfile('Xdump')[0]
1211 call assert_match('|à| |ê| |ö', dumpline)
1212
1213 call StopVimInTerminal(buf)
1214 call delete('Xcomposing')
1215 call delete('Xdump')
1216 let &encoding = save_enc
1217endfunc
1218
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001219" Tests for failures in the term_dumpwrite() function
1220func Test_terminal_dumpwrite_errors()
1221 CheckRunVimInTerminal
1222 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1223 let buf = RunVimInTerminal('', {})
1224 call term_wait(buf)
1225 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1226 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1227 call writefile([], 'Xtest.dump')
1228 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1229 call delete('Xtest.dump')
1230 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1231 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001232 call test_garbagecollect_now()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001233 call StopVimInTerminal(buf)
1234 call term_wait(buf)
1235 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1236 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1237 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1238endfunc
1239
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001240" just testing basic functionality.
1241func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001242 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001243 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001244 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001245 call assert_equal(2, winnr('$'))
1246 call assert_equal(20, line('$'))
1247 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001248
1249 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001250 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001251 call assert_equal(buf, buf2)
1252 call assert_notequal('one two three four five', trim(getline(1)))
1253
1254 " Load the first dump again in the same window
1255 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1256 call assert_equal(buf, buf2)
1257 call Check_dump01(0)
1258
1259 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1260 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1261 new
1262 let closedbuf = winbufnr('')
1263 quit
1264 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001265 call assert_fails('call term_dumpload([])', 'E474:')
1266 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001267
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001268 quit
1269endfunc
1270
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001271func Test_terminal_dumpload_dump()
1272 CheckRunVimInTerminal
1273
1274 let lines =<< trim END
1275 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1276 END
1277 call writefile(lines, 'XtermDumpload')
1278 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1279 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1280
1281 call StopVimInTerminal(buf)
1282 call delete('XtermDumpload')
1283endfunc
1284
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001285func Test_terminal_dumpdiff()
1286 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001287 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001288 call assert_equal(2, winnr('$'))
1289 call assert_equal(62, line('$'))
1290 call Check_dump01(0)
1291 call Check_dump01(42)
1292 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1293 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001294
1295 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E474:')
1296 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1297 call writefile([], 'X1.dump')
1298 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1299 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001300endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001301
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001302func Test_terminal_dumpdiff_swap()
1303 call assert_equal(1, winnr('$'))
1304 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1305 call assert_equal(2, winnr('$'))
1306 call assert_equal(62, line('$'))
1307 call assert_match('Test_popup_command_01.dump', getline(21))
1308 call assert_match('Test_popup_command_03.dump', getline(42))
1309 call assert_match('Undo', getline(3))
1310 call assert_match('three four five', getline(45))
1311
1312 normal s
1313 call assert_match('Test_popup_command_03.dump', getline(21))
1314 call assert_match('Test_popup_command_01.dump', getline(42))
1315 call assert_match('three four five', getline(3))
1316 call assert_match('Undo', getline(45))
1317 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001318
1319 " Diff two terminal dump files with different number of rows
1320 " Swap the diffs
1321 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1322 call assert_match('Test_popup_command_01.dump', getline(21))
1323 call assert_match('Test_winline_rnu.dump', getline(42))
1324 normal s
1325 call assert_match('Test_winline_rnu.dump', getline(6))
1326 call assert_match('Test_popup_command_01.dump', getline(27))
1327 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001328endfunc
1329
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001330func Test_terminal_dumpdiff_options()
1331 set laststatus=0
1332 call assert_equal(1, winnr('$'))
1333 let height = winheight(0)
1334 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1335 call assert_equal(2, winnr('$'))
1336 call assert_equal(height, winheight(winnr()))
1337 call assert_equal(33, winwidth(winnr()))
1338 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1339 quit
1340
1341 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001342 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1343 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001344 call assert_equal(&columns, winwidth(0))
1345 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001346 call assert_equal('something else', bufname('%'))
1347 quit
1348
1349 call assert_equal(1, winnr('$'))
1350 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1351 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001352 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 +01001353 bwipe
1354
1355 set laststatus&
1356endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001357
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001358func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001359 call assert_equal(1, winnr('$'))
1360
1361 " Use the title termcap entries to output the escape sequence.
1362 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001363 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001364 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001365 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001366 \ 'redraw',
1367 \ "set t_ts=",
1368 \ ], 'Xscript')
1369 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001370 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001371 call assert_equal('Xtextfile', expand('%:t'))
1372 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001373 return buf
1374endfunc
1375
1376func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001377 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001378 let buf = Api_drop_common('')
1379 call assert_equal(0, &bin)
1380 call assert_equal('', &fenc)
1381
1382 call StopVimInTerminal(buf)
1383 call delete('Xscript')
1384 bwipe Xtextfile
1385endfunc
1386
1387func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001388 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001389 let buf = Api_drop_common(',{"bin":1}')
1390 call assert_equal(1, &bin)
1391
1392 call StopVimInTerminal(buf)
1393 call delete('Xscript')
1394 bwipe Xtextfile
1395endfunc
1396
1397func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001398 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001399 let buf = Api_drop_common(',{"binary":1}')
1400 call assert_equal(1, &bin)
1401
1402 call StopVimInTerminal(buf)
1403 call delete('Xscript')
1404 bwipe Xtextfile
1405endfunc
1406
1407func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001408 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001409 set binary
1410 let buf = Api_drop_common(',{"nobin":1}')
1411 call assert_equal(0, &bin)
1412
1413 call StopVimInTerminal(buf)
1414 call delete('Xscript')
1415 bwipe Xtextfile
1416 set nobinary
1417endfunc
1418
1419func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001420 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001421 set binary
1422 let buf = Api_drop_common(',{"nobinary":1}')
1423 call assert_equal(0, &bin)
1424
1425 call StopVimInTerminal(buf)
1426 call delete('Xscript')
1427 bwipe Xtextfile
1428 set nobinary
1429endfunc
1430
1431func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001432 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001433 let buf = Api_drop_common(',{"ff":"dos"}')
1434 call assert_equal("dos", &ff)
1435
1436 call StopVimInTerminal(buf)
1437 call delete('Xscript')
1438 bwipe Xtextfile
1439endfunc
1440
1441func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001442 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001443 let buf = Api_drop_common(',{"fileformat":"dos"}')
1444 call assert_equal("dos", &ff)
1445
1446 call StopVimInTerminal(buf)
1447 call delete('Xscript')
1448 bwipe Xtextfile
1449endfunc
1450
1451func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001452 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001453 let buf = Api_drop_common(',{"enc":"utf-16"}')
1454 call assert_equal("utf-16", &fenc)
1455
1456 call StopVimInTerminal(buf)
1457 call delete('Xscript')
1458 bwipe Xtextfile
1459endfunc
1460
1461func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001462 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001463 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1464 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001465
1466 call StopVimInTerminal(buf)
1467 call delete('Xscript')
1468 bwipe Xtextfile
1469endfunc
1470
1471func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001472 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001473 let firstwinid = win_getid()
1474 split Xtextfile
1475 let textfile_winid = win_getid()
1476 call assert_equal(2, winnr('$'))
1477 call win_gotoid(firstwinid)
1478
1479 " Use the title termcap entries to output the escape sequence.
1480 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001481 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001482 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1483 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1484 \ 'redraw',
1485 \ "set t_ts=",
1486 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001487 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001488 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001489 call assert_equal(textfile_winid, win_getid())
1490
1491 call StopVimInTerminal(buf)
1492 call delete('Xscript')
1493 bwipe Xtextfile
1494endfunc
1495
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001496func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001497 let g:called_bufnum = a:bufnum
1498 let g:called_arg = a:arg
1499endfunc
1500
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001501func WriteApiCall(funcname)
1502 " Use the title termcap entries to output the escape sequence.
1503 call writefile([
1504 \ 'set title',
1505 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1506 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1507 \ 'redraw',
1508 \ "set t_ts=",
1509 \ ], 'Xscript')
1510endfunc
1511
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001512func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001513 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001514
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001515 unlet! g:called_bufnum
1516 unlet! g:called_arg
1517
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001518 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001519
1520 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001521 let buf = RunVimInTerminal('-S Xscript', {})
1522 call WaitFor({-> exists('g:called_bufnum')})
1523 call assert_equal(buf, g:called_bufnum)
1524 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001525 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001526
1527 unlet! g:called_bufnum
1528 unlet! g:called_arg
1529
1530 " Enable explicitly
1531 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1532 call WaitFor({-> exists('g:called_bufnum')})
1533 call assert_equal(buf, g:called_bufnum)
1534 call assert_equal(['hello', 123], g:called_arg)
1535 call StopVimInTerminal(buf)
1536
1537 unlet! g:called_bufnum
1538 unlet! g:called_arg
1539
1540 func! ApiCall_TryThis(bufnum, arg)
1541 let g:called_bufnum2 = a:bufnum
1542 let g:called_arg2 = a:arg
1543 endfunc
1544
1545 call WriteApiCall('ApiCall_TryThis')
1546
1547 " Use prefix match
1548 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1549 call WaitFor({-> exists('g:called_bufnum2')})
1550 call assert_equal(buf, g:called_bufnum2)
1551 call assert_equal(['hello', 123], g:called_arg2)
1552 call StopVimInTerminal(buf)
1553
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001554 call assert_fails("call term_start('ls', {'term_api' : []})", 'E475:')
1555
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001556 unlet! g:called_bufnum2
1557 unlet! g:called_arg2
1558
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001559 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001560 delfunction! ApiCall_TryThis
1561 unlet! g:called_bufnum2
1562 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001563endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001564
1565func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001566 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001567
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001568 func! TryThis(bufnum, arg)
1569 let g:called_bufnum3 = a:bufnum
1570 let g:called_arg3 = a:arg
1571 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001572
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001573 call WriteApiCall('TryThis')
1574
1575 unlet! g:called_bufnum3
1576 unlet! g:called_arg3
1577
1578 " Not permitted
1579 call ch_logfile('Xlog', 'w')
1580 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1581 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1582 call assert_false(exists('g:called_bufnum3'))
1583 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001584 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001585
1586 " No match
1587 call ch_logfile('Xlog', 'w')
1588 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1589 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1590 call assert_false(exists('g:called_bufnum3'))
1591 call assert_false(exists('g:called_arg3'))
1592 call StopVimInTerminal(buf)
1593
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001594 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001595 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001596 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001597 delfunction! TryThis
1598 unlet! g:called_bufnum3
1599 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001600endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001601
Bram Moolenaara997b452018-04-17 23:24:06 +02001602let s:caught_e937 = 0
1603
1604func Tapi_Delete(bufnum, arg)
1605 try
1606 execute 'bdelete!' a:bufnum
1607 catch /E937:/
1608 let s:caught_e937 = 1
1609 endtry
1610endfunc
1611
1612func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001613 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001614
1615 call WriteApiCall('Tapi_Delete')
1616 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001617 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001618
1619 call StopVimInTerminal(buf)
1620 call delete('Xscript')
1621 call ch_logfile('', '')
1622endfunc
1623
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001624func Test_terminal_ansicolors_default()
Bram Moolenaara161cb52020-04-30 19:09:35 +02001625 CheckFunction term_getansicolors
1626
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001627 let colors = [
1628 \ '#000000', '#e00000',
1629 \ '#00e000', '#e0e000',
1630 \ '#0000e0', '#e000e0',
1631 \ '#00e0e0', '#e0e0e0',
1632 \ '#808080', '#ff4040',
1633 \ '#40ff40', '#ffff40',
1634 \ '#4040ff', '#ff40ff',
1635 \ '#40ffff', '#ffffff',
1636 \]
1637
1638 let buf = Run_shell_in_terminal({})
1639 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001640 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001641 call TermWait(buf)
Bram Moolenaar98f16712020-05-22 13:34:01 +02001642 call assert_equal([], term_getansicolors(buf))
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001643
1644 exe buf . 'bwipe'
1645endfunc
1646
1647let s:test_colors = [
1648 \ '#616e64', '#0d0a79',
1649 \ '#6d610d', '#0a7373',
1650 \ '#690d0a', '#6d696e',
1651 \ '#0d0a6f', '#616e0d',
1652 \ '#0a6479', '#6d0d0a',
1653 \ '#617373', '#0d0a69',
1654 \ '#6d690d', '#0a6e6f',
1655 \ '#610d0a', '#6e6479',
1656 \]
1657
1658func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001659 CheckFeature termguicolors
Bram Moolenaara161cb52020-04-30 19:09:35 +02001660 CheckFunction term_getansicolors
1661
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001662 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1663 let buf = Run_shell_in_terminal({})
1664 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001665 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001666 call TermWait(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001667
1668 exe buf . 'bwipe'
1669 unlet g:terminal_ansi_colors
1670endfunc
1671
1672func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001673 CheckFeature termguicolors
Bram Moolenaara161cb52020-04-30 19:09:35 +02001674 CheckFunction term_getansicolors
1675
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001676 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1677 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1678 call assert_equal(s:test_colors, term_getansicolors(buf))
1679
1680 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001681 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001682
1683 let colors = [
1684 \ 'ivory', 'AliceBlue',
1685 \ 'grey67', 'dark goldenrod',
1686 \ 'SteelBlue3', 'PaleVioletRed4',
1687 \ 'MediumPurple2', 'yellow2',
1688 \ 'RosyBrown3', 'OrangeRed2',
1689 \ 'white smoke', 'navy blue',
1690 \ 'grey47', 'gray97',
1691 \ 'MistyRose2', 'DodgerBlue4',
1692 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001693 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001694
1695 let colors[4] = 'Invalid'
1696 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001697 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001698
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001699 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001700 call TermWait(buf)
Bram Moolenaar98f16712020-05-22 13:34:01 +02001701 call assert_equal(0, term_setansicolors(buf, []))
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001702 exe buf . 'bwipe'
1703endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001704
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001705func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001706 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001707
1708 " Use all the ANSI colors.
1709 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001710 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001711 \ 'hi Tblack ctermfg=0 ctermbg=8',
1712 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1713 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1714 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1715 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1716 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1717 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1718 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1719 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1720 \ 'hi Tred ctermfg=9 ctermbg=1',
1721 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1722 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1723 \ 'hi Tblue ctermfg=12 ctermbg=4',
1724 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1725 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1726 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001727 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1728 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1729 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001730 \ '',
1731 \ 'call matchadd("Tblack", "A")',
1732 \ 'call matchadd("Tdarkred", "B")',
1733 \ 'call matchadd("Tdarkgreen", "C")',
1734 \ 'call matchadd("Tbrown", "D")',
1735 \ 'call matchadd("Tdarkblue", "E")',
1736 \ 'call matchadd("Tdarkmagenta", "F")',
1737 \ 'call matchadd("Tdarkcyan", "G")',
1738 \ 'call matchadd("Tlightgrey", "H")',
1739 \ 'call matchadd("Tdarkgrey", "I")',
1740 \ 'call matchadd("Tred", "J")',
1741 \ 'call matchadd("Tgreen", "K")',
1742 \ 'call matchadd("Tyellow", "L")',
1743 \ 'call matchadd("Tblue", "M")',
1744 \ 'call matchadd("Tmagenta", "N")',
1745 \ 'call matchadd("Tcyan", "O")',
1746 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001747 \ 'call matchadd("TdarkredBold", "X")',
1748 \ 'call matchadd("TgreenBold", "Y")',
1749 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001750 \ 'redraw',
1751 \ ], 'Xcolorscript')
1752 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1753 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1754
1755 call term_sendkeys(buf, ":q\<CR>")
1756 call StopVimInTerminal(buf)
1757 call delete('Xcolorscript')
1758endfunc
1759
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001760func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001761 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001762 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001763 let text = []
1764 for n in range(10)
1765 call add(text, repeat(n, 50))
1766 endfor
1767 call writefile(text, 'Xwinsize')
1768 let buf = RunVimInTerminal('Xwinsize', {})
1769 let win = bufwinid(buf)
1770 call assert_equal([6, 40], term_getsize(buf))
1771 call assert_equal(6, winheight(win))
1772 call assert_equal(40, winwidth(win))
1773
1774 " resizing the window doesn't resize the terminal.
1775 resize 10
1776 vertical resize 60
1777 call assert_equal([6, 40], term_getsize(buf))
1778 call assert_equal(10, winheight(win))
1779 call assert_equal(60, winwidth(win))
1780
1781 call StopVimInTerminal(buf)
1782 call delete('Xwinsize')
1783
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001784 call assert_fails('set termwinsize=40', 'E474')
1785 call assert_fails('set termwinsize=10+40', 'E474')
1786 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001787
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001788 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001789endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001790
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001791func Test_terminal_termwinsize_option_zero()
1792 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001793 let buf = Run_shell_in_terminal({})
1794 let win = bufwinid(buf)
1795 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001796 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001797 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001798 exe buf . 'bwipe'
1799
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001800 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001801 let buf = Run_shell_in_terminal({})
1802 let win = bufwinid(buf)
1803 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001804 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001805 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001806 exe buf . 'bwipe'
1807
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001808 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001809 let buf = Run_shell_in_terminal({})
1810 let win = bufwinid(buf)
1811 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001812 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001813 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001814 exe buf . 'bwipe'
1815
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001816 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001817endfunc
1818
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001819func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001820 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001821 vsplit
1822 let buf = Run_shell_in_terminal({})
1823 let win = bufwinid(buf)
1824 call assert_inrange(10, 1000, winheight(win))
1825 call assert_inrange(50, 1000, winwidth(win))
1826 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1827
1828 resize 15
1829 vertical resize 60
1830 redraw
1831 call assert_equal([15, 60], term_getsize(buf))
1832 call assert_equal(15, winheight(win))
1833 call assert_equal(60, winwidth(win))
1834
1835 resize 7
1836 vertical resize 30
1837 redraw
1838 call assert_equal([10, 50], term_getsize(buf))
1839 call assert_equal(7, winheight(win))
1840 call assert_equal(30, winwidth(win))
1841
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001842 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001843 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001844 exe buf . 'bwipe'
1845
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001846 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001847 let buf = Run_shell_in_terminal({})
1848 let win = bufwinid(buf)
1849 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001850 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001851 call TermWait(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001852 exe buf . 'bwipe'
1853
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001854 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001855endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001856
1857func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001858 " make three tabpages, terminal in the middle
1859 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001860 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001861 tabnew
1862 tabprev
1863 call assert_equal(1, winnr('$'))
1864 call assert_equal(2, tabpagenr())
1865 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001866
1867 let buf = Run_shell_in_terminal({})
1868 let termwin = bufwinid(buf)
1869 set termwinkey=<C-L>
1870 call feedkeys("\<C-L>w", 'tx')
1871 call assert_equal(thiswin, win_getid())
1872 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001873 call assert_equal(termwin, win_getid())
1874
Bram Moolenaar997d4242019-09-14 21:23:40 +02001875 if has('langmap')
1876 set langmap=xjyk
1877 call feedkeys("\<C-L>x", 'tx')
1878 call assert_equal(thiswin, win_getid())
1879 call feedkeys("\<C-W>y", 'tx')
1880 call assert_equal(termwin, win_getid())
1881 set langmap=
1882 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001883
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001884 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001885 call assert_equal(3, tabpagenr())
1886 tabprev
1887 call assert_equal(2, tabpagenr())
1888 call assert_equal(termwin, win_getid())
1889
1890 call feedkeys("\<C-L>gT", "xt")
1891 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001892 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001893 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001894 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001895
1896 let job = term_getjob(buf)
1897 call feedkeys("\<C-L>\<C-C>", 'tx')
1898 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001899
1900 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001901 tabnext
1902 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001903 tabprev
1904 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001905endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001906
1907func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001908 CheckUnix
1909
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001910 call writefile([
1911 \ '#!/bin/sh',
1912 \ 'echo "this is standard error" >&2',
1913 \ 'echo "this is standard out" >&1',
1914 \ ], 'Xechoerrout.sh')
1915 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1916
1917 let outfile = 'Xtermstdout'
1918 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001919
1920 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1921 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001922 call assert_equal('this is standard error', term_getline(buf, 1))
1923
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001924 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001925 exe buf . 'bwipe'
1926 call delete('Xechoerrout.sh')
1927 call delete(outfile)
1928endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001929
Bram Moolenaare219f732019-11-30 15:34:08 +01001930func Test_termwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001931 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001932
1933 " Let the terminal output more than 'termwinscroll' lines, some at the start
1934 " will be dropped.
1935 exe 'set termwinscroll=' . &lines
1936 let buf = term_start('/bin/sh')
1937 for i in range(1, &lines)
1938 call feedkeys("echo " . i . "\<CR>", 'xt')
1939 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1940 endfor
1941 " Go to Terminal-Normal mode to update the buffer.
1942 call feedkeys("\<C-W>N", 'xt')
1943 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1944
1945 " Every "echo nr" must only appear once
1946 let lines = getline(1, line('$'))
1947 for i in range(&lines - len(lines) / 2 + 2, &lines)
1948 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1949 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1950 endfor
1951
1952 exe buf . 'bwipe!'
1953endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001954
Bram Moolenaar875cf872018-07-08 20:49:07 +02001955" Resizing the terminal window caused an ml_get error.
1956" TODO: This does not reproduce the original problem.
1957func Test_terminal_resize()
1958 set statusline=x
1959 terminal
1960 call assert_equal(2, winnr('$'))
1961
1962 " Fill the terminal with text.
1963 if has('win32')
1964 call feedkeys("dir\<CR>", 'xt')
1965 else
1966 call feedkeys("ls\<CR>", 'xt')
1967 endif
1968 " Go to Terminal-Normal mode for a moment.
1969 call feedkeys("\<C-W>N", 'xt')
1970 " Open a new window
1971 call feedkeys("i\<C-W>n", 'xt')
1972 call assert_equal(3, winnr('$'))
1973 redraw
1974
1975 close
1976 call assert_equal(2, winnr('$'))
1977 call feedkeys("exit\<CR>", 'xt')
1978 set statusline&
1979endfunc
1980
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001981" must be nearly the last, we can't go back from GUI to terminal
1982func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001983 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001984
1985 " Ignore the "failed to create input context" error.
1986 call test_ignore_error('E285:')
1987
1988 gui -f
1989
1990 call assert_equal(1, winnr('$'))
1991 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001992 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001993 call TermWait(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001994
1995 " closing window wipes out the terminal buffer a with finished job
1996 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1997 call assert_equal("", bufname(buf))
1998
1999 unlet g:job
2000endfunc
2001
2002func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002003 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02002004 set guioptions+=!
2005
2006 let filename = 'Xtestscript'
2007 if has('win32')
2008 let filename .= '.bat'
2009 let prefix = ''
2010 let contents = ['@echo off', 'exit %1']
2011 else
2012 let filename .= '.sh'
2013 let prefix = './'
2014 let contents = ['#!/bin/sh', 'exit $1']
2015 endif
2016 call writefile(contents, filename)
2017 call setfperm(filename, 'rwxrwx---')
2018
2019 " Check if v:shell_error is equal to the exit status.
2020 let exitval = 0
2021 execute printf(':!%s%s %d', prefix, filename, exitval)
2022 call assert_equal(exitval, v:shell_error)
2023
2024 let exitval = 9
2025 execute printf(':!%s%s %d', prefix, filename, exitval)
2026 call assert_equal(exitval, v:shell_error)
2027
2028 set guioptions&
2029 call delete(filename)
2030endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002031
2032func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002033 CheckUnix
2034
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002035 term ++hidden cat
2036 let bnr = bufnr('$')
2037 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2038 exe 'sbuf ' . bnr
2039 call assert_equal('terminal', &buftype)
2040 call term_sendkeys(bnr, "asdf\<CR>")
2041 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
2042 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002043 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02002044 bwipe!
2045endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002046
Bram Moolenaara4c2a242019-03-25 23:01:38 +01002047func Test_terminal_switch_mode()
2048 term
2049 let bnr = bufnr('$')
2050 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2051 call feedkeys("\<C-W>N", 'xt')
2052 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2053 call feedkeys("A", 'xt')
2054 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaar98f16712020-05-22 13:34:01 +02002055 call feedkeys("\<C-\>\<C-N>", 'xt')
Bram Moolenaara4c2a242019-03-25 23:01:38 +01002056 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2057 call feedkeys("I", 'xt')
2058 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2059 call feedkeys("\<C-W>Nv", 'xt')
2060 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2061 call feedkeys("I", 'xt')
2062 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2063 call feedkeys("\<C-W>Nv", 'xt')
2064 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
2065 call feedkeys("A", 'xt')
2066 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
2067 bwipe!
2068endfunc
2069
Bram Moolenaara3817732019-10-16 16:31:44 +02002070func Test_terminal_normal_mode()
2071 CheckRunVimInTerminal
2072
2073 " Run Vim in a terminal and open a terminal window to run Vim in.
2074 let lines =<< trim END
2075 call setline(1, range(11111, 11122))
2076 3
2077 END
2078 call writefile(lines, 'XtermNormal')
2079 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002080 call TermWait(buf)
Bram Moolenaara3817732019-10-16 16:31:44 +02002081
2082 call term_sendkeys(buf, "\<C-W>N")
2083 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
2084 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
2085
2086 call term_sendkeys(buf, ":set culopt=number\r")
2087 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
2088
2089 call term_sendkeys(buf, ":set culopt=line\r")
2090 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
2091
Bram Moolenaar98f16712020-05-22 13:34:01 +02002092 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
Bram Moolenaara3817732019-10-16 16:31:44 +02002093 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
2094 call StopVimInTerminal(buf)
2095 call delete('XtermNormal')
2096endfunc
2097
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002098func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002099 CheckUnix
2100
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002101 call assert_equal(1, winnr('$'))
2102 term ++hidden ++close ls
2103 let bnr = bufnr('$')
2104 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2105 call WaitForAssert({-> assert_false(bufexists(bnr))})
2106 call assert_equal(1, winnr('$'))
2107endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002108
2109func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002110 " This test does not pass through ConPTY.
2111 if has('conpty')
2112 return
2113 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002114 let contents = [
2115 \ [ 'One', '', 'X' ],
2116 \ [ 'Two', '', '' ],
2117 \ [ 'Three' ] + repeat([''], 30)
2118 \ ]
2119
2120 for c in contents
2121 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01002122 if has('win32')
2123 term cmd /c type Xfile
2124 else
2125 term cat Xfile
2126 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002127 let bnr = bufnr('$')
2128 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
2129 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01002130 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01002131 call assert_equal(c, getline(1, line('$')))
2132 quit
2133 endfor
2134
2135 call delete('Xfile')
2136endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01002137
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002138func Test_terminal_no_job()
Bram Moolenaar077ff432019-10-28 00:42:21 +01002139 if has('win32')
2140 let cmd = 'cmd /c ""'
2141 else
2142 CheckExecutable false
2143 let cmd = 'false'
2144 endif
2145 let term = term_start(cmd, {'term_finish': 'close'})
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01002146 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
2147endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01002148
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002149func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002150 CheckUnix
2151
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002152 let buf = Run_shell_in_terminal({})
2153
2154 " Wait for the shell to display a prompt.
2155 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2156
2157 " Hide the cursor.
2158 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2159 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2160
2161 " Show the cursor.
2162 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002163 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002164
2165 " Change color of cursor.
2166 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2167 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2168 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2169 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2170 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2171
2172 " Make cursor a blinking block.
2173 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2174 call WaitForAssert({-> assert_equal([1, 1],
2175 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2176
2177 " Make cursor a steady block.
2178 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2179 call WaitForAssert({-> assert_equal([0, 1],
2180 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2181
2182 " Make cursor a blinking underline.
2183 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2184 call WaitForAssert({-> assert_equal([1, 2],
2185 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2186
2187 " Make cursor a steady underline.
2188 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2189 call WaitForAssert({-> assert_equal([0, 2],
2190 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2191
2192 " Make cursor a blinking vertical bar.
2193 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2194 call WaitForAssert({-> assert_equal([1, 3],
2195 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2196
2197 " Make cursor a steady vertical bar.
2198 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2199 call WaitForAssert({-> assert_equal([0, 3],
2200 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2201
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002202 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002203endfunc
2204
Bram Moolenaar98f16712020-05-22 13:34:01 +02002205" Test for term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002206func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002207 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002208 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002209 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002210 call assert_equal('', term_gettitle(bufnr('$') + 1))
2211
Bram Moolenaar98f16712020-05-22 13:34:01 +02002212 if !has('title') || empty(&t_ts)
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002213 throw "Skipped: can't get/set title"
2214 endif
2215
Bram Moolenaar98f16712020-05-22 13:34:01 +02002216 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002217 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002218 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2219 call term_sendkeys(term, ":e Xfoo\r")
2220 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002221 else
2222 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002223 call term_sendkeys(term, ":e Xfoo\r")
2224 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002225 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002226
Bram Moolenaar1e115362019-01-09 23:01:02 +01002227 call term_sendkeys(term, ":set titlestring=foo\r")
2228 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2229
2230 exe term . 'bwipe!'
2231endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002232
Bram Moolenaar01603a92020-04-03 12:56:17 +02002233func Test_term_gettty()
2234 let buf = Run_shell_in_terminal({})
2235 let gettty = term_gettty(buf)
2236
2237 if has('unix') && executable('tty')
2238 " Find tty using the tty shell command.
2239 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2240 call term_sendkeys(buf, "tty\r")
2241 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
2242 let tty = term_getline(buf, 2)
2243 call assert_equal(tty, gettty)
2244 endif
2245
2246 let gettty0 = term_gettty(buf, 0)
2247 let gettty1 = term_gettty(buf, 1)
2248
2249 call assert_equal(gettty, gettty0)
2250 call assert_equal(job_info(g:job).tty_out, gettty0)
2251 call assert_equal(job_info(g:job).tty_in, gettty1)
2252
2253 if has('unix')
2254 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
2255 " are identical according to :help term_gettty()
2256 call assert_equal(gettty0, gettty1)
2257 call assert_match('^/dev/', gettty)
2258 else
2259 " ConPTY works on anonymous pipe.
2260 if !has('conpty')
2261 call assert_match('^\\\\.\\pipe\\', gettty0)
2262 call assert_match('^\\\\.\\pipe\\', gettty1)
2263 endif
2264 endif
2265
2266 call assert_fails('call term_gettty(buf, 2)', 'E475:')
2267 call assert_fails('call term_gettty(buf, -1)', 'E475:')
2268
2269 call assert_equal('', term_gettty(buf + 1))
2270
2271 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002272 call TermWait(buf)
Bram Moolenaar01603a92020-04-03 12:56:17 +02002273 exe buf . 'bwipe'
2274endfunc
2275
Bram Moolenaar10772302019-01-20 18:25:54 +01002276" When drawing the statusline the cursor position may not have been updated
2277" yet.
2278" 1. create a terminal, make it show 2 lines
2279" 2. 0.5 sec later: leave terminal window, execute "i"
2280" 3. 0.5 sec later: clear terminal window, now it's 1 line
2281" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2282" 4. 0.5 sec later: should be done, clean up
2283func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002284 CheckUnix
2285
Bram Moolenaar10772302019-01-20 18:25:54 +01002286 set statusline=x
2287 terminal
2288 let tbuf = bufnr('')
2289 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2290 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2291 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2292 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2293
2294 sleep 2
2295 exe tbuf . 'bwipe!'
2296 au! BufLeave
2297 set statusline=
2298endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002299
2300func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002301 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002302
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002303 " split, go to the bottom-right window
2304 split
2305 wincmd j
2306 set splitright
2307
2308 call writefile([
2309 \ 'echo getwinpos()',
2310 \ ], 'XTest_getwinpos')
2311 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002312 call TermWait(buf)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002313
2314 " Find the output of getwinpos() in the bottom line.
2315 let rows = term_getsize(buf)[0]
2316 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2317 let line = term_getline(buf, rows)
2318 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2319 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2320
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002321 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002322 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002323 " In the GUI it can be more, let's assume a 20 x 14 cell.
2324 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002325 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002326 let winpos = 50->getwinpos()
2327 call assert_equal(xroot, winpos[0])
2328 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002329 let [winrow, wincol] = win_screenpos('.')
2330 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2331 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2332 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2333 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002334
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002335 call TermWait(buf)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002336 call term_sendkeys(buf, ":q\<CR>")
2337 call StopVimInTerminal(buf)
2338 call delete('XTest_getwinpos')
2339 exe buf . 'bwipe!'
2340 set splitright&
2341 only!
2342endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002343
2344func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002345 " somehow doesn't work on MS-Windows
2346 CheckUnix
2347 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002348
2349 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002350 call writefile(["\<Esc>[?1047h"], 'Xtext')
2351 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002352 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2353
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002354 call writefile(["\<Esc>[?1047l"], 'Xtext')
2355 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002356 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002357
2358 call term_sendkeys(buf, "exit\r")
2359 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002360 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002361endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002362
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002363func Test_terminal_shell_option()
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002364 if has('unix')
2365 " exec is a shell builtin command, should fail without a shell.
2366 term exec ls runtest.vim
2367 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
2368 bwipe!
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002369
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002370 term ++shell exec ls runtest.vim
2371 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2372 bwipe!
2373 elseif has('win32')
2374 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar36ec6f62019-11-05 22:38:47 +01002375 try
2376 term dir /b runtest.vim
2377 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
2378 catch /CreateProcess/
2379 " ignore
2380 endtry
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +01002381 bwipe!
2382
2383 term ++shell dir /b runtest.vim
2384 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
2385 bwipe!
2386 endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +01002387endfunc
2388
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002389func Test_terminal_setapi_and_call()
Bram Moolenaar21109272020-01-30 16:27:20 +01002390 CheckRunVimInTerminal
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002391
2392 call WriteApiCall('Tapi_TryThis')
2393 call ch_logfile('Xlog', 'w')
2394
2395 unlet! g:called_bufnum
2396 unlet! g:called_arg
2397
Bram Moolenaar21109272020-01-30 16:27:20 +01002398 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002399 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2400 call assert_false(exists('g:called_bufnum'))
2401 call assert_false(exists('g:called_arg'))
2402
Bram Moolenaar21109272020-01-30 16:27:20 +01002403 eval buf->term_setapi('Tapi_')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002404 call term_sendkeys(buf, ":set notitle\<CR>")
2405 call term_sendkeys(buf, ":source Xscript\<CR>")
2406 call WaitFor({-> exists('g:called_bufnum')})
2407 call assert_equal(buf, g:called_bufnum)
2408 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar21109272020-01-30 16:27:20 +01002409
2410 call StopVimInTerminal(buf)
2411
2412 call delete('Xscript')
2413 call ch_logfile('')
2414 call delete('Xlog')
2415 unlet! g:called_bufnum
2416 unlet! g:called_arg
2417endfunc
2418
2419func Test_terminal_api_arg()
2420 CheckRunVimInTerminal
2421
2422 call WriteApiCall('Tapi_TryThis')
2423 call ch_logfile('Xlog', 'w')
2424
2425 unlet! g:called_bufnum
2426 unlet! g:called_arg
2427
2428 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2429 let buf = bufnr('%')
2430 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2431 call assert_false(exists('g:called_bufnum'))
2432 call assert_false(exists('g:called_arg'))
2433
2434 call StopVimInTerminal(buf)
2435
2436 call ch_logfile('Xlog', 'w')
2437
2438 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2439 let buf = bufnr('%')
2440 call WaitFor({-> exists('g:called_bufnum')})
2441 call assert_equal(buf, g:called_bufnum)
2442 call assert_equal(['hello', 123], g:called_arg)
2443
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002444 call StopVimInTerminal(buf)
2445
2446 call delete('Xscript')
2447 call ch_logfile('')
2448 call delete('Xlog')
2449 unlet! g:called_bufnum
2450 unlet! g:called_arg
2451endfunc
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002452
Bram Moolenaar9b9be002020-03-22 14:41:22 +01002453func Test_terminal_invalid_arg()
2454 call assert_fails('terminal ++xyz', 'E181:')
2455endfunc
2456
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002457func Test_terminal_in_popup()
2458 CheckRunVimInTerminal
2459
2460 let text =<< trim END
2461 some text
2462 to edit
2463 in a popup window
2464 END
2465 call writefile(text, 'Xtext')
Bram Moolenaar3180fe62020-02-02 13:47:06 +01002466 let cmd = GetVimCommandCleanTerm()
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002467 let lines = [
2468 \ 'call setline(1, range(20))',
2469 \ 'hi PopTerm ctermbg=grey',
2470 \ 'func OpenTerm(setColor)',
Bram Moolenaar353c3512020-03-15 14:19:26 +01002471 \ " set noruler",
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002472 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
Bram Moolenaar57c33952020-02-29 16:09:16 +01002473 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002474 \ ' if a:setColor',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002475 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002476 \ ' endif',
2477 \ 'endfunc',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002478 \ 'func HidePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002479 \ ' call popup_hide(g:winid)',
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002480 \ 'endfunc',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002481 \ 'func ClosePopup()',
Bram Moolenaar57c33952020-02-29 16:09:16 +01002482 \ ' call popup_close(g:winid)',
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002483 \ 'endfunc',
2484 \ 'func ReopenPopup()',
2485 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
2486 \ 'endfunc',
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002487 \ ]
2488 call writefile(lines, 'XtermPopup')
2489 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002490 call TermWait(buf, 100)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002491 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002492 call TermWait(buf, 100)
Bram Moolenaarc2adc002020-02-14 17:05:18 +01002493 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002494 call TermWait(buf, 100)
Bram Moolenaar57c33952020-02-29 16:09:16 +01002495 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002496 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
2497
2498 call term_sendkeys(buf, ":q\<CR>")
2499 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
2500
2501 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002502 call TermWait(buf, 150)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002503 call term_sendkeys(buf, ":set hlsearch\<CR>")
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002504 call TermWait(buf, 100)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002505 call term_sendkeys(buf, "/edit\<CR>")
2506 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
2507
Bram Moolenaar1af5ce02020-02-06 11:54:35 +01002508 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
2509 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
2510 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002511 call TermWait(buf, 50)
Bram Moolenaar2e6638d2020-02-05 21:07:18 +01002512
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002513 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
2514 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
2515
2516 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
2517 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
Bram Moolenaar11ec8072020-02-20 20:12:29 +01002518
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002519 " Go to terminal-Normal mode and visually select text.
2520 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
2521 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
2522
2523 " Back to job mode, redraws
2524 call term_sendkeys(buf, "A")
2525 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
2526
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002527 call TermWait(buf, 50)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002528 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002529 call TermWait(buf, 150) " wait for terminal to vanish
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002530
2531 call StopVimInTerminal(buf)
Bram Moolenaar19398262020-03-14 15:28:08 +01002532 call delete('Xtext')
2533 call delete('XtermPopup')
2534endfunc
2535
2536" Check a terminal in popup window uses the default mininum size.
2537func Test_terminal_in_popup_min_size()
2538 CheckRunVimInTerminal
2539
2540 let text =<< trim END
2541 another text
2542 to show
2543 in a popup window
2544 END
2545 call writefile(text, 'Xtext')
2546 let lines = [
Bram Moolenaar19398262020-03-14 15:28:08 +01002547 \ 'call setline(1, range(20))',
Bram Moolenaar19398262020-03-14 15:28:08 +01002548 \ 'func OpenTerm()',
2549 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
2550 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2551 \ 'endfunc',
2552 \ ]
2553 call writefile(lines, 'XtermPopup')
2554 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002555 call TermWait(buf, 100)
Bram Moolenaar353c3512020-03-15 14:19:26 +01002556 call term_sendkeys(buf, ":set noruler\<CR>")
Bram Moolenaar19398262020-03-14 15:28:08 +01002557 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002558 call TermWait(buf, 50)
Bram Moolenaar19398262020-03-14 15:28:08 +01002559 call term_sendkeys(buf, ":\<CR>")
2560 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
2561
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002562 call TermWait(buf, 50)
Bram Moolenaar19398262020-03-14 15:28:08 +01002563 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002564 call TermWait(buf, 50) " wait for terminal to vanish
Bram Moolenaar19398262020-03-14 15:28:08 +01002565 call StopVimInTerminal(buf)
2566 call delete('Xtext')
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002567 call delete('XtermPopup')
2568endfunc
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002569
Bram Moolenaar83d47902020-03-26 20:34:00 +01002570" Check a terminal in popup window with different colors
2571func Terminal_in_popup_colored(group_name, highlight_cmd, highlight_opt)
2572 CheckRunVimInTerminal
2573 CheckUnix
2574
2575 let lines = [
Bram Moolenaar83d47902020-03-26 20:34:00 +01002576 \ 'call setline(1, range(20))',
2577 \ 'func OpenTerm()',
2578 \ " let s:buf = term_start('cat', #{hidden: 1, "
2579 \ .. a:highlight_opt .. "})",
2580 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
2581 \ 'endfunc',
2582 \ a:highlight_cmd,
2583 \ ]
2584 call writefile(lines, 'XtermPopup')
2585 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002586 call TermWait(buf, 100)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002587 call term_sendkeys(buf, ":set noruler\<CR>")
2588 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002589 call TermWait(buf, 50)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002590 call term_sendkeys(buf, "hello\<CR>")
2591 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
2592
2593 call term_sendkeys(buf, "\<C-D>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002594 call TermWait(buf, 50)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002595 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002596 call TermWait(buf, 50) " wait for terminal to vanish
Bram Moolenaar83d47902020-03-26 20:34:00 +01002597 call StopVimInTerminal(buf)
2598 call delete('XtermPopup')
2599endfunc
2600
2601func Test_terminal_in_popup_colored_Terminal()
2602 call Terminal_in_popup_colored("Terminal", "highlight Terminal ctermfg=blue ctermbg=yellow", "")
2603endfunc
2604
2605func Test_terminal_in_popup_colored_group()
2606 call Terminal_in_popup_colored("MyTermCol", "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue", "term_highlight: 'MyTermCol',")
2607endfunc
2608
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002609func Test_double_popup_terminal()
2610 let buf1 = term_start(&shell, #{hidden: 1})
2611 let win1 = popup_create(buf1, {})
2612 let buf2 = term_start(&shell, #{hidden: 1})
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002613 call assert_fails('call popup_create(buf2, {})', 'E861:')
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002614 call popup_close(win1)
Bram Moolenaar80ae8802020-02-28 19:11:18 +01002615 exe buf1 .. 'bwipe!'
2616 exe buf2 .. 'bwipe!'
2617endfunc
2618
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01002619func Test_issue_5607()
2620 let wincount = winnr('$')
2621 exe 'terminal' &shell &shellcmdflag 'exit'
2622 let job = term_getjob(bufnr())
2623 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2624
2625 let old_wincolor = &wincolor
2626 try
2627 set wincolor=
2628 finally
2629 let &wincolor = old_wincolor
2630 bw!
2631 endtry
2632endfunc
Bram Moolenaare010c722020-02-24 21:37:54 +01002633
2634func Test_hidden_terminal()
2635 let buf = term_start(&shell, #{hidden: 1})
2636 call assert_equal('', bufname('^$'))
2637 call StopShellInTerminal(buf)
2638endfunc
Bram Moolenaarcee52202020-03-11 14:19:58 +01002639
2640func Test_term_nasty_callback()
Bram Moolenaara161cb52020-04-30 19:09:35 +02002641 CheckExecutable sh
Bram Moolenaarcee52202020-03-11 14:19:58 +01002642
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002643 set hidden
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002644 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002645 call popup_create(g:buf0, {})
Bram Moolenaar03a9f842020-05-13 13:40:16 +02002646 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
2647
2648 call popup_clear(1)
Bram Moolenaarcee52202020-03-11 14:19:58 +01002649 set hidden&
2650endfunc
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002651
Bram Moolenaareb58a242020-04-19 18:13:19 +02002652func Test_term_and_startinsert()
2653 CheckRunVimInTerminal
2654 CheckUnix
2655
2656 let lines =<< trim EOL
2657 put='some text'
2658 term
2659 startinsert
2660 EOL
2661 call writefile(lines, 'XTest_startinsert')
2662 let buf = RunVimInTerminal('-S XTest_startinsert', {})
2663
2664 call term_sendkeys(buf, "exit\r")
2665 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
2666 call term_sendkeys(buf, "0l")
2667 call term_sendkeys(buf, "A<\<Esc>")
2668 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
2669
2670 call StopVimInTerminal(buf)
2671 call delete('XTest_startinsert')
2672endfunc
2673
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002674" Test for passing invalid arguments to terminal functions
2675func Test_term_func_invalid_arg()
2676 call assert_fails('let b = term_getaltscreen([])', 'E745:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002677 call assert_fails('let a = term_getattr(1, [])', 'E730:')
2678 call assert_fails('let c = term_getcursor([])', 'E745:')
2679 call assert_fails('let l = term_getline([], 1)', 'E745:')
2680 call assert_fails('let l = term_getscrolled([])', 'E745:')
2681 call assert_fails('let s = term_getsize([])', 'E745:')
2682 call assert_fails('let s = term_getstatus([])', 'E745:')
2683 call assert_fails('let s = term_scrape([], 1)', 'E745:')
2684 call assert_fails('call term_sendkeys([], "a")', 'E745:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002685 call assert_fails('call term_setapi([], "")', 'E745:')
2686 call assert_fails('call term_setrestore([], "")', 'E745:')
2687 call assert_fails('call term_setkill([], "")', 'E745:')
Bram Moolenaar87202262020-05-24 17:23:45 +02002688 if has('gui') || has('termguicolors')
2689 call assert_fails('let p = term_getansicolors([])', 'E745:')
2690 call assert_fails('call term_setansicolors([], [])', 'E745:')
2691 endif
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002692endfunc
2693
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002694" Test for sending various special keycodes to a terminal
2695func Test_term_keycode_translation()
2696 CheckRunVimInTerminal
2697
2698 let buf = RunVimInTerminal('', {})
2699 call term_sendkeys(buf, ":set nocompatible\<CR>")
2700
2701 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
2702 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
2703 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
2704 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
2705 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
2706 \ "\<S-Down>"]
2707 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
2708 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
2709 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
2710 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
Bram Moolenaarfe813892020-05-21 20:38:31 +02002711 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002712
Bram Moolenaarfe813892020-05-21 20:38:31 +02002713 call term_sendkeys(buf, "i")
2714 for i in range(len(keys))
2715 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002716 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))})
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002717 endfor
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002718
Bram Moolenaarfe813892020-05-21 20:38:31 +02002719 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
2720 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
2721 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
2722 let keypad_output = ['0', '1', '2', '3', '4', '5',
2723 \ '6', '7', '8', '9', '.', '+',
2724 \ '-', '*', '/']
2725 for i in range(len(keypad_keys))
2726 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
2727 if keypad_output[i] == '3' || keypad_output[i] == '9'
2728 continue
2729 endif
2730 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002731 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))})
Bram Moolenaarfe813892020-05-21 20:38:31 +02002732 endfor
2733
2734 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
Bram Moolenaar9cdcd1d2020-05-22 14:44:26 +02002735 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
Bram Moolenaarfe813892020-05-21 20:38:31 +02002736
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002737 call StopVimInTerminal(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002738endfunc
2739
2740" Test for using the mouse in a terminal
2741func Test_term_mouse()
2742 CheckNotGui
2743 CheckRunVimInTerminal
2744
2745 let save_mouse = &mouse
2746 let save_term = &term
2747 let save_ttymouse = &ttymouse
2748 let save_clipboard = &clipboard
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002749 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
2750
2751 let lines =<< trim END
2752 one two three four five
2753 red green yellow red blue
2754 vim emacs sublime nano
2755 END
2756 call writefile(lines, 'Xtest_mouse')
2757
Bram Moolenaar98f16712020-05-22 13:34:01 +02002758 " Create a terminal window running Vim for the test with mouse enabled
2759 let prev_win = win_getid()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002760 let buf = RunVimInTerminal('Xtest_mouse -n', {})
2761 call term_sendkeys(buf, ":set nocompatible\<CR>")
2762 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
2763 call term_sendkeys(buf, ":set clipboard=\<CR>")
2764 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
2765 call term_wait(buf)
2766 redraw!
2767
Bram Moolenaar98f16712020-05-22 13:34:01 +02002768 " Use the mouse to enter the terminal window
2769 call win_gotoid(prev_win)
2770 call feedkeys(MouseLeftClickCode(1, 1), 'x')
2771 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
2772 call assert_equal(1, getwininfo(win_getid())[0].terminal)
2773
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002774 " Test for <LeftMouse> click/release
2775 call test_setmouse(2, 5)
2776 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
2777 call test_setmouse(3, 8)
2778 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
2779 call term_wait(buf, 50)
2780 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
2781 call term_wait(buf, 50)
2782 let pos = json_decode(readfile('Xbuf')[0])
2783 call assert_equal([3, 8], pos[1:2])
2784
2785 " Test for selecting text using mouse
2786 call delete('Xbuf')
2787 call test_setmouse(2, 11)
2788 call term_sendkeys(buf, "\<LeftMouse>")
2789 call test_setmouse(2, 16)
2790 call term_sendkeys(buf, "\<LeftRelease>y")
2791 call term_wait(buf, 50)
2792 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2793 call term_wait(buf, 50)
2794 call assert_equal('yellow', readfile('Xbuf')[0])
2795
2796 " Test for selecting text using doubleclick
2797 call delete('Xbuf')
2798 call test_setmouse(1, 11)
2799 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
2800 call test_setmouse(1, 17)
2801 call term_sendkeys(buf, "\<LeftRelease>y")
2802 call term_wait(buf, 50)
2803 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2804 call term_wait(buf, 50)
2805 call assert_equal('three four', readfile('Xbuf')[0])
2806
2807 " Test for selecting a line using triple click
2808 call delete('Xbuf')
2809 call test_setmouse(3, 2)
2810 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
2811 call term_wait(buf, 50)
2812 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2813 call term_wait(buf, 50)
2814 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
2815
2816 " Test for selecting a block using qudraple click
2817 call delete('Xbuf')
2818 call test_setmouse(1, 11)
2819 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
2820 call test_setmouse(3, 13)
2821 call term_sendkeys(buf, "\<LeftRelease>y")
2822 call term_wait(buf, 50)
2823 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2824 call term_wait(buf, 50)
2825 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
2826
2827 " Test for extending a selection using right click
2828 call delete('Xbuf')
2829 call test_setmouse(2, 9)
2830 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
2831 call test_setmouse(2, 16)
2832 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
2833 call term_wait(buf, 50)
2834 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
2835 call term_wait(buf, 50)
2836 call assert_equal("n yellow", readfile('Xbuf')[0])
2837
2838 " Test for pasting text using middle click
2839 call delete('Xbuf')
2840 call term_sendkeys(buf, ":let @r='bright '\<CR>")
2841 call test_setmouse(2, 22)
2842 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
2843 call term_wait(buf, 50)
2844 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
2845 call term_wait(buf, 50)
2846 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
2847
2848 " cleanup
2849 call term_wait(buf)
2850 call StopVimInTerminal(buf)
2851 let &mouse = save_mouse
2852 let &term = save_term
2853 let &ttymouse = save_ttymouse
2854 let &clipboard = save_clipboard
2855 set mousetime&
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002856 call delete('Xtest_mouse')
2857 call delete('Xbuf')
2858endfunc
2859
2860" Test for modeless selection in a terminal
2861func Test_term_modeless_selection()
2862 CheckUnix
2863 CheckNotGui
2864 CheckRunVimInTerminal
2865 CheckFeature clipboard_working
2866
2867 let save_mouse = &mouse
2868 let save_term = &term
2869 let save_ttymouse = &ttymouse
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002870 set mouse=a term=xterm ttymouse=sgr mousetime=200
2871 set clipboard=autoselectml
2872
2873 let lines =<< trim END
2874 one two three four five
2875 red green yellow red blue
2876 vim emacs sublime nano
2877 END
2878 call writefile(lines, 'Xtest_modeless')
2879
Bram Moolenaar98f16712020-05-22 13:34:01 +02002880 " Create a terminal window running Vim for the test with mouse disabled
2881 let prev_win = win_getid()
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002882 let buf = RunVimInTerminal('Xtest_modeless -n', {})
2883 call term_sendkeys(buf, ":set nocompatible\<CR>")
2884 call term_sendkeys(buf, ":set mouse=\<CR>")
2885 call term_wait(buf)
2886 redraw!
2887
Bram Moolenaar98f16712020-05-22 13:34:01 +02002888 " Use the mouse to enter the terminal window
2889 call win_gotoid(prev_win)
2890 call feedkeys(MouseLeftClickCode(1, 1), 'x')
2891 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
2892 call term_wait(buf)
2893 call assert_equal(1, getwininfo(win_getid())[0].terminal)
2894
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002895 " Test for copying a modeless selection to clipboard
2896 let @* = 'clean'
2897 " communicating with X server may take a little time
2898 sleep 100m
2899 call feedkeys(MouseLeftClickCode(2, 3), 'x')
2900 call feedkeys(MouseLeftDragCode(2, 11), 'x')
2901 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
2902 call assert_equal("d green y", @*)
2903
2904 " cleanup
2905 call term_wait(buf)
2906 call StopVimInTerminal(buf)
2907 let &mouse = save_mouse
2908 let &term = save_term
2909 let &ttymouse = save_ttymouse
2910 set mousetime& clipboard&
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002911 call delete('Xtest_modeless')
Bram Moolenaar98f16712020-05-22 13:34:01 +02002912 new | only!
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002913endfunc
2914
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002915" vim: shiftwidth=2 sts=2 expandtab