blob: b354115e1685c227421f82156f73131fa37aa898 [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 Moolenaarc6df10e2017-07-29 20:15:08 +02008
Bram Moolenaarb81bc772017-08-11 22:45:01 +02009let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020010let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020011
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 if has('win32')
16 let buf = term_start([&shell,'/k'], a:options)
17 else
18 let buf = term_start(&shell, a:options)
19 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020028 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020029 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010042 " ConPTY works on anonymous pipe.
43 if !has('conpty')
44 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020045 call assert_match('^\\\\.\\pipe\\', ''->term_gettty())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010046 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020047 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020048 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020049 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020051 call assert_match('%aR[^\n]*running]', execute('ls R'))
52 call assert_notmatch('%[^\n]*running]', execute('ls F'))
53 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020055 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +020056 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020057 call assert_equal('n', mode())
58 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020059 call assert_match('%aF[^\n]*finished]', execute('ls F'))
60 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020062
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 " closing window wipes out the terminal buffer a with finished job
64 close
65 call assert_equal("", bufname(buf))
66
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020067 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020068 unlet g:job
69endfunc
70
71func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020073 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020074 call term_wait(buf)
75
76 setlocal modifiable
77 exe "normal Axxx\<Esc>"
78 call assert_fails(buf . 'bwipe', 'E517')
79 undo
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081 exe buf . 'bwipe'
82 unlet g:job
83endfunc
84
Bram Moolenaar5b868a82019-02-25 06:11:53 +010085func Test_terminal_paste_register()
86 let @" = "text to paste"
87
88 let buf = Run_shell_in_terminal({})
89 " Wait for the shell to display a prompt
90 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
91
92 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
93 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020094 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010095
96 exe buf . 'bwipe!'
97 unlet g:job
98endfunc
99
Bram Moolenaar94053a52017-08-01 21:44:33 +0200100func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200101 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200102 call assert_fails(buf . 'bwipe', 'E517')
103 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200104 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200105 call assert_equal("", bufname(buf))
106
107 unlet g:job
108endfunc
109
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200110func Test_terminal_split_quit()
111 let buf = Run_shell_in_terminal({})
112 call term_wait(buf)
113 split
114 quit!
115 call term_wait(buf)
116 sleep 50m
117 call assert_equal('run', job_status(g:job))
118
119 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200120 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200121
122 exe buf . 'bwipe'
123 unlet g:job
124endfunc
125
Bram Moolenaar94053a52017-08-01 21:44:33 +0200126func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200127 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200128 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200129 quit
130 for nr in range(1, winnr('$'))
131 call assert_notequal(winbufnr(nr), buf)
132 endfor
133 call assert_true(bufloaded(buf))
134 call assert_true(buflisted(buf))
135
136 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200137 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200138 exe buf . 'bwipe'
139
140 unlet g:job
141endfunc
142
Bram Moolenaar1e115362019-01-09 23:01:02 +0100143func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200144 exe g:buf . 'bwipe!'
145 let g:buf = 0
146endfunc
147
Bram Moolenaar9d189612017-09-09 18:11:00 +0200148func Get_cat_123_cmd()
149 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100150 if !has('conpty')
151 return 'cmd /c "cls && color 2 && echo 123"'
152 else
153 " When clearing twice, extra sequence is not output.
154 return 'cmd /c "cls && cls && color 2 && echo 123"'
155 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200156 else
157 call writefile(["\<Esc>[32m123"], 'Xtext')
158 return "cat Xtext"
159 endif
160endfunc
161
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200163 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200164 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
165 let g:job = term_getjob(g:buf)
166
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200167 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
168 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200169 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100170 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200171 call delete('Xtext')
172endfunc
173
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200175 let l = term_scrape(a:buf, 0)
176 call assert_true(len(l) == 0)
177 let l = term_scrape(a:buf, 999)
178 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200179 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 call assert_true(len(l) > 0)
181 call assert_equal('1', l[0].chars)
182 call assert_equal('2', l[1].chars)
183 call assert_equal('3', l[2].chars)
184 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200185 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
186 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100187 if has('win32')
188 " On Windows 'background' always defaults to dark, even though the terminal
189 " may use a light background. Therefore accept both white and black.
190 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100192 if &background == 'light'
193 call assert_equal('#ffffff', l[0].bg)
194 else
195 call assert_equal('#000000', l[0].bg)
196 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200197 endif
198
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200199 let l = term_getline(a:buf, -1)
200 call assert_equal('', l)
201 let l = term_getline(a:buf, 0)
202 call assert_equal('', l)
203 let l = term_getline(a:buf, 999)
204 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200205 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200206 call assert_equal('123', l)
207endfunc
208
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200209func Test_terminal_scrape_123()
210 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200211 let buf = term_start(cmd)
212
213 let termlist = term_list()
214 call assert_equal(1, len(termlist))
215 call assert_equal(buf, termlist[0])
216
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200217 " Nothing happens with invalid buffer number
218 call term_wait(1234)
219
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200220 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200221 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200222 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200223 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224 call Check_123(buf)
225
226 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100227 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200228 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200229 call term_wait(buf)
230 call Check_123(buf)
231
232 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200233 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200234endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200235
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200237 call writefile(["léttまrs"], 'Xtext')
238 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200239 " Run cmd with UTF-8 codepage to make the type command print the expected
240 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 let buf = term_start("cmd /K chcp 65001")
242 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200243 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100244 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200245 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let buf = term_start("cat Xtext")
247 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200248 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200249
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100250 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
251 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200252 call assert_true(len(l) >= 7)
253 call assert_equal('l', l[0].chars)
254 call assert_equal('é', l[1].chars)
255 call assert_equal(1, l[1].width)
256 call assert_equal('t', l[2].chars)
257 call assert_equal('t', l[3].chars)
258 call assert_equal('ま', l[4].chars)
259 call assert_equal(2, l[4].width)
260 call assert_equal('r', l[5].chars)
261 call assert_equal('s', l[6].chars)
262
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100265 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100267 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200268 call delete('Xtext')
269endfunc
270
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200271func Test_terminal_scroll()
272 call writefile(range(1, 200), 'Xtext')
273 if has('win32')
274 let cmd = 'cmd /c "type Xtext"'
275 else
276 let cmd = "cat Xtext"
277 endif
278 let buf = term_start(cmd)
279
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100280 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200281 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200282 call term_wait(buf)
283 if has('win32')
284 " TODO: this should not be needed
285 sleep 100m
286 endif
287
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200288 let scrolled = buf->term_getscrolled()
289 call assert_equal(scrolled, term_getscrolled(buf))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200290 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200291 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200292 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200293 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200294 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200295 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200296
297 exe buf . 'bwipe'
298 call delete('Xtext')
299endfunc
300
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200301func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200302 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200303 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200304 call writefile(range(150), 'Xtext')
305 if has('win32')
306 call term_sendkeys(buf, "type Xtext\<CR>")
307 else
308 call term_sendkeys(buf, "cat Xtext\<CR>")
309 endif
310 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200311 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200312 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200313 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200314 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200315
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200316 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200317 call term_wait(buf)
318 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200319 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100320 call delete('Xtext')
321endfunc
322
323func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200324 " tail -f only works on Unix
325 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100326
327 call writefile(range(50), 'Xtext')
328 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100329 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100330 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100331 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100332 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
333 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100334 \ 'call feedkeys("\<C-W>N", "xt")',
335 \ ], 'XTest_postponed')
336 let buf = RunVimInTerminal('-S XTest_postponed', {})
337 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar96baf022019-02-14 23:49:38 +0100338 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100339
340 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200341 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100342 call term_sendkeys(buf, "k")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100343 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100344
345 " Back to Terminal-Job mode, text will scroll and show the extra line.
346 call term_sendkeys(buf, "a")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100347 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100348
349 call term_wait(buf)
350 call term_sendkeys(buf, "\<C-C>")
351 call term_wait(buf)
352 call term_sendkeys(buf, "exit\<CR>")
353 call term_wait(buf)
354 call term_sendkeys(buf, ":q\<CR>")
355 call StopVimInTerminal(buf)
356 call delete('XTest_postponed')
357 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200358endfunc
359
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100360" Run diff on two dumps with different size.
361func Test_terminal_dumpdiff_size()
362 call assert_equal(1, winnr('$'))
363 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
364 call assert_equal(2, winnr('$'))
365 call assert_match('Test_incsearch_search_01.dump', getline(10))
366 call assert_match(' +++++$', getline(11))
367 call assert_match('Test_popup_command_01.dump', getline(31))
368 call assert_equal(repeat('+', 75), getline(30))
369 quit
370endfunc
371
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200372func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200373 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200374
Bram Moolenaarb2412082017-08-20 18:09:14 +0200375 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200376 let size = term_getsize('')
377 bwipe!
378 call assert_equal(5, size[0])
379
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200380 call term_start(cmd, {'term_rows': 6})
381 let size = term_getsize('')
382 bwipe!
383 call assert_equal(6, size[0])
384
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200385 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200386 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200387 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200388
389 call term_setsize('', 6, 0)
390 call assert_equal([6, 33], term_getsize(''))
391
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200392 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200393 call assert_equal([6, 35], term_getsize(''))
394
395 call term_setsize('', 7, 30)
396 call assert_equal([7, 30], term_getsize(''))
397
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200398 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200399 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200400
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200401 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
402 let size = term_getsize('')
403 bwipe!
404 call assert_equal([6, 36], size)
405
Bram Moolenaarb2412082017-08-20 18:09:14 +0200406 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200407 let size = term_getsize('')
408 bwipe!
409 call assert_equal(20, size[1])
410
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200411 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200412 let size = term_getsize('')
413 bwipe!
414 call assert_equal(26, size[1])
415
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200416 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200417 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200418 let size = term_getsize('')
419 bwipe!
420 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200421
422 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
423 let size = term_getsize('')
424 bwipe!
425 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200426
427 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200428endfunc
429
430func Test_terminal_curwin()
431 let cmd = Get_cat_123_cmd()
432 call assert_equal(1, winnr('$'))
433
434 split dummy
435 exe 'terminal ++curwin ' . cmd
436 call assert_equal(2, winnr('$'))
437 bwipe!
438
439 split dummy
440 call term_start(cmd, {'curwin': 1})
441 call assert_equal(2, winnr('$'))
442 bwipe!
443
444 split dummy
445 call setline(1, 'change')
446 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
447 call assert_equal(2, winnr('$'))
448 exe 'terminal! ++curwin ' . cmd
449 call assert_equal(2, winnr('$'))
450 bwipe!
451
452 split dummy
453 call setline(1, 'change')
454 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
455 call assert_equal(2, winnr('$'))
456 bwipe!
457
458 split dummy
459 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200460 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200461endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462
Bram Moolenaarff546792017-11-21 14:47:57 +0100463func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200464 if s:python != ''
465 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200466 " 500 was not enough for Travis
467 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200468 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200469 echo 'This will take five seconds...'
470 let waittime = 2000
471 if has('win32')
472 let cmd = $windir . '\system32\timeout.exe 1'
473 else
474 let cmd = 'sleep 1'
475 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200476 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100477 return [cmd, waittime]
478endfunc
479
480func Test_terminal_finish_open_close()
481 call assert_equal(1, winnr('$'))
482
483 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200484
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100485 " shell terminal closes automatically
486 terminal
487 let buf = bufnr('%')
488 call assert_equal(2, winnr('$'))
489 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200490 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200491 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200492 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100493
494 " shell terminal that does not close automatically
495 terminal ++noclose
496 let buf = bufnr('%')
497 call assert_equal(2, winnr('$'))
498 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200499 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200500 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100501 call assert_equal(2, winnr('$'))
502 quit
503 call assert_equal(1, winnr('$'))
504
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200505 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200506 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200507 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200508 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200509
510 call term_start(cmd, {'term_finish': 'close'})
511 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200512 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200513 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200514 call assert_equal(1, winnr('$'))
515
516 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200517 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200518 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200519 bwipe
520
521 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200522 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200523 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200524 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200525
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200526 exe 'terminal ++hidden ++open ' . cmd
527 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200528 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200529 bwipe
530
531 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
532 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200533 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200534 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200535
536 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
537 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
538 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
539 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
540
541 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200542 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200543 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200544 call assert_equal(4, winheight(0))
545 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200546endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200547
548func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200549 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200550 return
551 endif
552 call mkdir('Xdir')
553 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200554 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200555
556 exe buf . 'bwipe'
557 call delete('Xdir', 'rf')
558endfunc
559
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200560func Test_terminal_cwd_failure()
561 " Case 1: Provided directory is not actually a directory. Attempt to make
562 " the file executable as well.
563 call writefile([], 'Xfile')
564 call setfperm('Xfile', 'rwx------')
565 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
566 call delete('Xfile')
567
568 " Case 2: Directory does not exist.
569 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
570
571 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100572 " Skip this for root, it will be accessible anyway.
573 if $USER != 'root'
574 call mkdir('XdirNoAccess', '', '0600')
575 " return early if the directory permissions could not be set properly
576 if getfperm('XdirNoAccess')[2] == 'x'
577 call delete('XdirNoAccess', 'rf')
578 return
579 endif
580 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
581 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200582 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200583endfunc
584
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100585func Test_terminal_servername()
586 if !has('clientserver')
587 return
588 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200589 call s:test_environment("VIM_SERVERNAME", v:servername)
590endfunc
591
592func Test_terminal_version()
593 call s:test_environment("VIM_TERMINAL", string(v:version))
594endfunc
595
596func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100597 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100598 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200599 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100600 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200601 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100602 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200603 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100604 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100605 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200606 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200607 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100608
Bram Moolenaar012eb662018-03-13 17:55:27 +0100609 exe buf . 'bwipe'
610 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100611endfunc
612
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200613func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100614 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200615 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200616 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100617 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100618 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100619 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100620 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100621 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200622 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200623 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200624 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200625
Bram Moolenaar012eb662018-03-13 17:55:27 +0100626 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200627endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200628
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200629func Test_terminal_list_args()
630 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
631 call assert_fails(buf . 'bwipe', 'E517')
632 exe buf . 'bwipe!'
633 call assert_equal("", bufname(buf))
634endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200635
636func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100637 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100638 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200639 " The shell or something else has a problem dealing with more than 1000
640 " characters at the same time.
641 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100642 " NPFS is used in Windows, nonblocking mode does not work properly.
643 elseif has('win32')
644 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200645 else
646 let len = 5000
647 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200648
649 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100650 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200651 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100652 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200653
654 " On MS-Windows there is an extra empty line below "done". Find "done" in
655 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100656 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100657 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100658 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200659 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100660 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200661 endif
662 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200663
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100664 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200665 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100666 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200667 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200668 bwipe
669endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200670
671func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200672 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200673 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200674 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200675 if has('win32')
676 " TODO: enable once writing to stdin works on MS-Windows
677 return
678 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200679 new
680 call setline(1, ['one', 'two', 'three'])
681 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200682 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200683 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200684 call assert_equal(['3', '3', '14'], nrs)
685 bwipe
686
Bram Moolenaardada6d22017-09-02 17:18:35 +0200687 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200688 call setline(1, ['one', 'two', 'three', 'four'])
689 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200690 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200691 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200692 call assert_equal(['2', '2', '10'], nrs)
693 bwipe
694
Bram Moolenaardada6d22017-09-02 17:18:35 +0200695 if executable('python')
696 new
697 call setline(1, ['print("hello")'])
698 1term ++eof=exit() python
699 " MS-Windows echoes the input, Unix doesn't.
700 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
701 if getline(1) =~ 'hello'
702 call assert_equal('hello', getline(1))
703 else
704 call assert_equal('hello', getline(line('$') - 1))
705 endif
706 bwipe
707
708 if has('win32')
709 new
710 call setline(1, ['print("hello")'])
711 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200712 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200713 call assert_equal('hello', getline(line('$') - 1))
714 bwipe
715 endif
716 endif
717
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200718 bwipe!
719endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200720
721func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200722 let buf = term_start('NONE', {})
723 call assert_notequal(0, buf)
724
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200725 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200726 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100727 if has('gui_running') && !has('win32')
728 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200729 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100730 else
731 " Otherwise using a job works on all systems.
732 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200733 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200734 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200735
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200736 bwipe!
737endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200738
739func Test_terminal_special_chars()
740 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200741 CheckUnix
742
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200743 call mkdir('Xdir with spaces')
744 call writefile(['x'], 'Xdir with spaces/quoted"file')
745 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200746 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200747 call term_wait('')
748
749 call delete('Xdir with spaces', 'rf')
750 bwipe
751endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200752
753func Test_terminal_wrong_options()
754 call assert_fails('call term_start(&shell, {
755 \ "in_io": "file",
756 \ "in_name": "xxx",
757 \ "out_io": "file",
758 \ "out_name": "xxx",
759 \ "err_io": "file",
760 \ "err_name": "xxx"
761 \ })', 'E474:')
762 call assert_fails('call term_start(&shell, {
763 \ "out_buf": bufnr("%")
764 \ })', 'E474:')
765 call assert_fails('call term_start(&shell, {
766 \ "err_buf": bufnr("%")
767 \ })', 'E474:')
768endfunc
769
770func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200771 let cmd = Get_cat_123_cmd()
772 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
773 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100774 " ConPTY may precede escape sequence. There are things that are not so.
775 if !has('conpty')
776 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
777 call assert_match('123', readfile('Xfile')[0])
778 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200779 let g:job = term_getjob(buf)
780 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
781 call delete('Xfile')
782 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200783
784 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200785 call writefile(['one line'], 'Xfile')
786 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
787 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200788 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200789 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200790 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200791 bwipe
792 call delete('Xfile')
793 endif
794endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200795
796func TerminalTmap(remap)
797 let buf = Run_shell_in_terminal({})
798 call assert_equal('t', mode())
799
800 if a:remap
801 tmap 123 456
802 else
803 tnoremap 123 456
804 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100805 " don't use abcde, it's an existing command
806 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200807 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100808 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200809 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200810 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200811 let lnum = term_getcursor(buf)[0]
812 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100813 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200814 else
815 call assert_match('456', term_getline(buf, lnum))
816 endif
817
818 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200819 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200820 call term_wait(buf)
821
822 tunmap 123
823 tunmap 456
824 call assert_equal('', maparg('123', 't'))
825 close
826 unlet g:job
827endfunc
828
829func Test_terminal_tmap()
830 call TerminalTmap(1)
831 call TerminalTmap(0)
832endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200833
834func Test_terminal_wall()
835 let buf = Run_shell_in_terminal({})
836 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200837 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200838 call term_wait(buf)
839 exe buf . 'bwipe'
840 unlet g:job
841endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200842
Bram Moolenaar7a760922018-02-19 23:10:02 +0100843func Test_terminal_wqall()
844 let buf = Run_shell_in_terminal({})
845 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200846 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100847 call term_wait(buf)
848 exe buf . 'bwipe'
849 unlet g:job
850endfunc
851
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200852func Test_terminal_composing_unicode()
853 let save_enc = &encoding
854 set encoding=utf-8
855
856 if has('win32')
857 let cmd = "cmd /K chcp 65001"
858 let lnum = [3, 6, 9]
859 else
860 let cmd = &shell
861 let lnum = [1, 3, 5]
862 endif
863
864 enew
865 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100866 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200867 call term_wait(buf, 50)
868
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200869 if has('win32')
870 call assert_equal('cmd', job_info(g:job).cmd[0])
871 else
872 call assert_equal(&shell, job_info(g:job).cmd[0])
873 endif
874
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200875 " ascii + composing
876 let txt = "a\u0308bc"
877 call term_sendkeys(buf, "echo " . txt . "\r")
878 call term_wait(buf, 50)
879 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
880 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
881 let l = term_scrape(buf, lnum[0] + 1)
882 call assert_equal("a\u0308", l[0].chars)
883 call assert_equal("b", l[1].chars)
884 call assert_equal("c", l[2].chars)
885
886 " multibyte + composing
887 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
888 call term_sendkeys(buf, "echo " . txt . "\r")
889 call term_wait(buf, 50)
890 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
891 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
892 let l = term_scrape(buf, lnum[1] + 1)
893 call assert_equal("\u304b\u3099", l[0].chars)
894 call assert_equal("\u304e", l[1].chars)
895 call assert_equal("\u304f\u3099", l[2].chars)
896 call assert_equal("\u3052", l[3].chars)
897 call assert_equal("\u3053\u3099", l[4].chars)
898
899 " \u00a0 + composing
900 let txt = "abc\u00a0\u0308"
901 call term_sendkeys(buf, "echo " . txt . "\r")
902 call term_wait(buf, 50)
903 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
904 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
905 let l = term_scrape(buf, lnum[2] + 1)
906 call assert_equal("\u00a0\u0308", l[3].chars)
907
908 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200909 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200910 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100911 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200912 let &encoding = save_enc
913endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100914
915func Test_terminal_aucmd_on_close()
916 fun Nop()
917 let s:called = 1
918 endfun
919
920 aug repro
921 au!
922 au BufWinLeave * call Nop()
923 aug END
924
925 let [cmd, waittime] = s:get_sleep_cmd()
926
927 call assert_equal(1, winnr('$'))
928 new
929 call setline(1, ['one', 'two'])
930 exe 'term ++close ' . cmd
931 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200932 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100933 call assert_equal(1, s:called)
934 bwipe!
935
936 unlet s:called
937 au! repro
938 delfunc Nop
939endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100940
941func Test_terminal_term_start_empty_command()
942 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
943 call assert_fails(cmd, 'E474')
944 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
945 call assert_fails(cmd, 'E474')
946 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
947 call assert_fails(cmd, 'E474')
948 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
949 call assert_fails(cmd, 'E474')
950endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100951
952func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200953 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100954
955 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200956 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100957
Bram Moolenaar086eb872018-03-25 21:24:12 +0200958 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200959 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100960
Bram Moolenaar086eb872018-03-25 21:24:12 +0200961 " Request the cursor position.
962 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100963
964 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200965 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100966
Bram Moolenaar086eb872018-03-25 21:24:12 +0200967 " End "cat" gently.
968 call term_sendkeys(buf, "\<CR>\<C-D>")
969
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200970 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100971 exe buf . 'bwipe'
972 unlet g:job
973endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100974
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100975" Run Vim, start a terminal in that Vim with the kill argument,
976" :qall works.
977func Run_terminal_qall_kill(line1, line2)
978 " 1. Open a terminal window and wait for the prompt to appear
979 " 2. set kill using term_setkill()
980 " 3. make Vim exit, it will kill the shell
981 let after = [
982 \ a:line1,
983 \ 'let buf = bufnr("%")',
984 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
985 \ ' sleep 10m',
986 \ 'endwhile',
987 \ a:line2,
988 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
989 \ 'qall',
990 \ ]
991 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100992 return
993 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100994 call assert_equal("done", readfile("Xdone")[0])
995 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100996endfunc
997
998" Run Vim in a terminal, then start a terminal in that Vim with a kill
999" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001000func Test_terminal_qall_kill_arg()
1001 call Run_terminal_qall_kill('term ++kill=kill', '')
1002endfunc
1003
1004" Run Vim, start a terminal in that Vim, set the kill argument with
1005" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001006func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001007 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001008endfunc
1009
1010" Run Vim, start a terminal in that Vim without the kill argument,
1011" check that :qall does not exit, :qall! does.
1012func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001013 let after =<< trim [CODE]
1014 term
1015 let buf = bufnr("%")
1016 while term_getline(buf, 1) =~ "^\\s*$"
1017 sleep 10m
1018 endwhile
1019 set nomore
1020 au VimLeavePre * call writefile(["too early"], "Xdone")
1021 qall
1022 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1023 cquit
1024 [CODE]
1025
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001026 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001027 return
1028 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001029 call assert_equal("done", readfile("Xdone")[0])
1030 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001031endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001032
1033" Run Vim in a terminal, then start a terminal in that Vim without a kill
1034" argument, check that :confirm qall works.
1035func Test_terminal_qall_prompt()
1036 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001037 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001038 endif
1039 let buf = RunVimInTerminal('', {})
1040
1041 " Open a terminal window and wait for the prompt to appear
1042 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001043 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1044 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001045
1046 " make Vim exit, it will prompt to kill the shell
1047 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001048 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001049 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001050 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001051
1052 " close the terminal window where Vim was running
1053 quit
1054endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001055
Bram Moolenaar012eb662018-03-13 17:55:27 +01001056func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001057 augroup repro
1058 au!
1059 au TerminalOpen * let s:called += 1
1060 augroup END
1061
1062 let s:called = 0
1063
1064 " Open a terminal window with :terminal
1065 terminal
1066 call assert_equal(1, s:called)
1067 bwipe!
1068
1069 " Open a terminal window with term_start()
1070 call term_start(&shell)
1071 call assert_equal(2, s:called)
1072 bwipe!
1073
1074 " Open a hidden terminal buffer with :terminal
1075 terminal ++hidden
1076 call assert_equal(3, s:called)
1077 for buf in term_list()
1078 exe buf . "bwipe!"
1079 endfor
1080
1081 " Open a hidden terminal buffer with term_start()
1082 let buf = term_start(&shell, {'hidden': 1})
1083 call assert_equal(4, s:called)
1084 exe buf . "bwipe!"
1085
1086 unlet s:called
1087 au! repro
1088endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001089
1090func Check_dump01(off)
1091 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1092 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001093 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001094endfunc
1095
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001096func Test_terminal_dumpwrite_composing()
1097 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001098 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001099 endif
1100 let save_enc = &encoding
1101 set encoding=utf-8
1102 call assert_equal(1, winnr('$'))
1103
1104 let text = " a\u0300 e\u0302 o\u0308"
1105 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001106 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001107 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001108 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001109 let dumpline = readfile('Xdump')[0]
1110 call assert_match('|à| |ê| |ö', dumpline)
1111
1112 call StopVimInTerminal(buf)
1113 call delete('Xcomposing')
1114 call delete('Xdump')
1115 let &encoding = save_enc
1116endfunc
1117
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001118" just testing basic functionality.
1119func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001120 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001121 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001122 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001123 call assert_equal(2, winnr('$'))
1124 call assert_equal(20, line('$'))
1125 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001126
1127 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001128 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001129 call assert_equal(buf, buf2)
1130 call assert_notequal('one two three four five', trim(getline(1)))
1131
1132 " Load the first dump again in the same window
1133 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1134 call assert_equal(buf, buf2)
1135 call Check_dump01(0)
1136
1137 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1138 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1139 new
1140 let closedbuf = winbufnr('')
1141 quit
1142 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1143
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001144 quit
1145endfunc
1146
1147func Test_terminal_dumpdiff()
1148 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001149 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001150 call assert_equal(2, winnr('$'))
1151 call assert_equal(62, line('$'))
1152 call Check_dump01(0)
1153 call Check_dump01(42)
1154 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1155 quit
1156endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001157
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001158func Test_terminal_dumpdiff_swap()
1159 call assert_equal(1, winnr('$'))
1160 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1161 call assert_equal(2, winnr('$'))
1162 call assert_equal(62, line('$'))
1163 call assert_match('Test_popup_command_01.dump', getline(21))
1164 call assert_match('Test_popup_command_03.dump', getline(42))
1165 call assert_match('Undo', getline(3))
1166 call assert_match('three four five', getline(45))
1167
1168 normal s
1169 call assert_match('Test_popup_command_03.dump', getline(21))
1170 call assert_match('Test_popup_command_01.dump', getline(42))
1171 call assert_match('three four five', getline(3))
1172 call assert_match('Undo', getline(45))
1173 quit
1174endfunc
1175
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001176func Test_terminal_dumpdiff_options()
1177 set laststatus=0
1178 call assert_equal(1, winnr('$'))
1179 let height = winheight(0)
1180 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1181 call assert_equal(2, winnr('$'))
1182 call assert_equal(height, winheight(winnr()))
1183 call assert_equal(33, winwidth(winnr()))
1184 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1185 quit
1186
1187 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001188 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1189 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001190 call assert_equal(&columns, winwidth(0))
1191 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001192 call assert_equal('something else', bufname('%'))
1193 quit
1194
1195 call assert_equal(1, winnr('$'))
1196 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1197 call assert_equal(1, winnr('$'))
1198 bwipe
1199
1200 set laststatus&
1201endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001202
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001203func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001204 call assert_equal(1, winnr('$'))
1205
1206 " Use the title termcap entries to output the escape sequence.
1207 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001208 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001209 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001210 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001211 \ 'redraw',
1212 \ "set t_ts=",
1213 \ ], 'Xscript')
1214 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001215 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001216 call assert_equal('Xtextfile', expand('%:t'))
1217 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001218 return buf
1219endfunc
1220
1221func Test_terminal_api_drop_newwin()
1222 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001223 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001224 endif
1225 let buf = Api_drop_common('')
1226 call assert_equal(0, &bin)
1227 call assert_equal('', &fenc)
1228
1229 call StopVimInTerminal(buf)
1230 call delete('Xscript')
1231 bwipe Xtextfile
1232endfunc
1233
1234func Test_terminal_api_drop_newwin_bin()
1235 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001236 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001237 endif
1238 let buf = Api_drop_common(',{"bin":1}')
1239 call assert_equal(1, &bin)
1240
1241 call StopVimInTerminal(buf)
1242 call delete('Xscript')
1243 bwipe Xtextfile
1244endfunc
1245
1246func Test_terminal_api_drop_newwin_binary()
1247 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001248 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001249 endif
1250 let buf = Api_drop_common(',{"binary":1}')
1251 call assert_equal(1, &bin)
1252
1253 call StopVimInTerminal(buf)
1254 call delete('Xscript')
1255 bwipe Xtextfile
1256endfunc
1257
1258func Test_terminal_api_drop_newwin_nobin()
1259 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001260 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001261 endif
1262 set binary
1263 let buf = Api_drop_common(',{"nobin":1}')
1264 call assert_equal(0, &bin)
1265
1266 call StopVimInTerminal(buf)
1267 call delete('Xscript')
1268 bwipe Xtextfile
1269 set nobinary
1270endfunc
1271
1272func Test_terminal_api_drop_newwin_nobinary()
1273 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001274 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001275 endif
1276 set binary
1277 let buf = Api_drop_common(',{"nobinary":1}')
1278 call assert_equal(0, &bin)
1279
1280 call StopVimInTerminal(buf)
1281 call delete('Xscript')
1282 bwipe Xtextfile
1283 set nobinary
1284endfunc
1285
1286func Test_terminal_api_drop_newwin_ff()
1287 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001288 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001289 endif
1290 let buf = Api_drop_common(',{"ff":"dos"}')
1291 call assert_equal("dos", &ff)
1292
1293 call StopVimInTerminal(buf)
1294 call delete('Xscript')
1295 bwipe Xtextfile
1296endfunc
1297
1298func Test_terminal_api_drop_newwin_fileformat()
1299 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001300 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001301 endif
1302 let buf = Api_drop_common(',{"fileformat":"dos"}')
1303 call assert_equal("dos", &ff)
1304
1305 call StopVimInTerminal(buf)
1306 call delete('Xscript')
1307 bwipe Xtextfile
1308endfunc
1309
1310func Test_terminal_api_drop_newwin_enc()
1311 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001312 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001313 endif
1314 let buf = Api_drop_common(',{"enc":"utf-16"}')
1315 call assert_equal("utf-16", &fenc)
1316
1317 call StopVimInTerminal(buf)
1318 call delete('Xscript')
1319 bwipe Xtextfile
1320endfunc
1321
1322func Test_terminal_api_drop_newwin_encoding()
1323 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001324 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001325 endif
1326 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1327 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001328
1329 call StopVimInTerminal(buf)
1330 call delete('Xscript')
1331 bwipe Xtextfile
1332endfunc
1333
1334func Test_terminal_api_drop_oldwin()
1335 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001336 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001337 endif
1338 let firstwinid = win_getid()
1339 split Xtextfile
1340 let textfile_winid = win_getid()
1341 call assert_equal(2, winnr('$'))
1342 call win_gotoid(firstwinid)
1343
1344 " Use the title termcap entries to output the escape sequence.
1345 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001346 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001347 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1348 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1349 \ 'redraw',
1350 \ "set t_ts=",
1351 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001352 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001353 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001354 call assert_equal(textfile_winid, win_getid())
1355
1356 call StopVimInTerminal(buf)
1357 call delete('Xscript')
1358 bwipe Xtextfile
1359endfunc
1360
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001361func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001362 let g:called_bufnum = a:bufnum
1363 let g:called_arg = a:arg
1364endfunc
1365
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001366func WriteApiCall(funcname)
1367 " Use the title termcap entries to output the escape sequence.
1368 call writefile([
1369 \ 'set title',
1370 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1371 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1372 \ 'redraw',
1373 \ "set t_ts=",
1374 \ ], 'Xscript')
1375endfunc
1376
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001377func Test_terminal_api_call()
1378 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001379 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001380 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001381
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001382 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001383 let buf = RunVimInTerminal('-S Xscript', {})
1384 call WaitFor({-> exists('g:called_bufnum')})
1385 call assert_equal(buf, g:called_bufnum)
1386 call assert_equal(['hello', 123], g:called_arg)
1387
1388 call StopVimInTerminal(buf)
1389 call delete('Xscript')
1390 unlet g:called_bufnum
1391 unlet g:called_arg
1392endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001393
1394func Test_terminal_api_call_fails()
1395 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001396 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001397 endif
1398
1399 call WriteApiCall('TryThis')
1400 call ch_logfile('Xlog', 'w')
1401 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001402 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001403
1404 call StopVimInTerminal(buf)
1405 call delete('Xscript')
1406 call ch_logfile('', '')
1407 call delete('Xlog')
1408endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001409
Bram Moolenaara997b452018-04-17 23:24:06 +02001410let s:caught_e937 = 0
1411
1412func Tapi_Delete(bufnum, arg)
1413 try
1414 execute 'bdelete!' a:bufnum
1415 catch /E937:/
1416 let s:caught_e937 = 1
1417 endtry
1418endfunc
1419
1420func Test_terminal_api_call_fail_delete()
1421 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001422 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaara997b452018-04-17 23:24:06 +02001423 endif
1424
1425 call WriteApiCall('Tapi_Delete')
1426 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001427 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001428
1429 call StopVimInTerminal(buf)
1430 call delete('Xscript')
1431 call ch_logfile('', '')
1432endfunc
1433
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001434func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001435 if !exists('*term_getansicolors')
1436 throw 'Skipped: term_getansicolors() not supported'
1437 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001438 let colors = [
1439 \ '#000000', '#e00000',
1440 \ '#00e000', '#e0e000',
1441 \ '#0000e0', '#e000e0',
1442 \ '#00e0e0', '#e0e0e0',
1443 \ '#808080', '#ff4040',
1444 \ '#40ff40', '#ffff40',
1445 \ '#4040ff', '#ff40ff',
1446 \ '#40ffff', '#ffffff',
1447 \]
1448
1449 let buf = Run_shell_in_terminal({})
1450 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001451 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001452 call term_wait(buf)
1453
1454 exe buf . 'bwipe'
1455endfunc
1456
1457let s:test_colors = [
1458 \ '#616e64', '#0d0a79',
1459 \ '#6d610d', '#0a7373',
1460 \ '#690d0a', '#6d696e',
1461 \ '#0d0a6f', '#616e0d',
1462 \ '#0a6479', '#6d0d0a',
1463 \ '#617373', '#0d0a69',
1464 \ '#6d690d', '#0a6e6f',
1465 \ '#610d0a', '#6e6479',
1466 \]
1467
1468func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001469 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001470 if !exists('*term_getansicolors')
1471 throw 'Skipped: term_getansicolors() not supported'
1472 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001473 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1474 let buf = Run_shell_in_terminal({})
1475 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001476 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001477 call term_wait(buf)
1478
1479 exe buf . 'bwipe'
1480 unlet g:terminal_ansi_colors
1481endfunc
1482
1483func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001484 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001485 if !exists('*term_getansicolors')
1486 throw 'Skipped: term_getansicolors() not supported'
1487 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001488 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1489 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1490 call assert_equal(s:test_colors, term_getansicolors(buf))
1491
1492 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001493 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001494
1495 let colors = [
1496 \ 'ivory', 'AliceBlue',
1497 \ 'grey67', 'dark goldenrod',
1498 \ 'SteelBlue3', 'PaleVioletRed4',
1499 \ 'MediumPurple2', 'yellow2',
1500 \ 'RosyBrown3', 'OrangeRed2',
1501 \ 'white smoke', 'navy blue',
1502 \ 'grey47', 'gray97',
1503 \ 'MistyRose2', 'DodgerBlue4',
1504 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001505 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001506
1507 let colors[4] = 'Invalid'
1508 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1509
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001510 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001511 call term_wait(buf)
1512 exe buf . 'bwipe'
1513endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001514
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001515func Test_terminal_all_ansi_colors()
1516 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001517 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001518 endif
1519
1520 " Use all the ANSI colors.
1521 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001522 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001523 \ 'hi Tblack ctermfg=0 ctermbg=8',
1524 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1525 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1526 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1527 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1528 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1529 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1530 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1531 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1532 \ 'hi Tred ctermfg=9 ctermbg=1',
1533 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1534 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1535 \ 'hi Tblue ctermfg=12 ctermbg=4',
1536 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1537 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1538 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001539 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1540 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1541 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001542 \ '',
1543 \ 'call matchadd("Tblack", "A")',
1544 \ 'call matchadd("Tdarkred", "B")',
1545 \ 'call matchadd("Tdarkgreen", "C")',
1546 \ 'call matchadd("Tbrown", "D")',
1547 \ 'call matchadd("Tdarkblue", "E")',
1548 \ 'call matchadd("Tdarkmagenta", "F")',
1549 \ 'call matchadd("Tdarkcyan", "G")',
1550 \ 'call matchadd("Tlightgrey", "H")',
1551 \ 'call matchadd("Tdarkgrey", "I")',
1552 \ 'call matchadd("Tred", "J")',
1553 \ 'call matchadd("Tgreen", "K")',
1554 \ 'call matchadd("Tyellow", "L")',
1555 \ 'call matchadd("Tblue", "M")',
1556 \ 'call matchadd("Tmagenta", "N")',
1557 \ 'call matchadd("Tcyan", "O")',
1558 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001559 \ 'call matchadd("TdarkredBold", "X")',
1560 \ 'call matchadd("TgreenBold", "Y")',
1561 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001562 \ 'redraw',
1563 \ ], 'Xcolorscript')
1564 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1565 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1566
1567 call term_sendkeys(buf, ":q\<CR>")
1568 call StopVimInTerminal(buf)
1569 call delete('Xcolorscript')
1570endfunc
1571
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001572func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001573 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001574 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001575 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001576 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001577 let text = []
1578 for n in range(10)
1579 call add(text, repeat(n, 50))
1580 endfor
1581 call writefile(text, 'Xwinsize')
1582 let buf = RunVimInTerminal('Xwinsize', {})
1583 let win = bufwinid(buf)
1584 call assert_equal([6, 40], term_getsize(buf))
1585 call assert_equal(6, winheight(win))
1586 call assert_equal(40, winwidth(win))
1587
1588 " resizing the window doesn't resize the terminal.
1589 resize 10
1590 vertical resize 60
1591 call assert_equal([6, 40], term_getsize(buf))
1592 call assert_equal(10, winheight(win))
1593 call assert_equal(60, winwidth(win))
1594
1595 call StopVimInTerminal(buf)
1596 call delete('Xwinsize')
1597
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001598 call assert_fails('set termwinsize=40', 'E474')
1599 call assert_fails('set termwinsize=10+40', 'E474')
1600 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001601
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001602 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001603endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001604
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001605func Test_terminal_termwinsize_option_zero()
1606 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001607 let buf = Run_shell_in_terminal({})
1608 let win = bufwinid(buf)
1609 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001610 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001611 call term_wait(buf)
1612 exe buf . 'bwipe'
1613
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001614 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001615 let buf = Run_shell_in_terminal({})
1616 let win = bufwinid(buf)
1617 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001618 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001619 call term_wait(buf)
1620 exe buf . 'bwipe'
1621
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001622 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001623 let buf = Run_shell_in_terminal({})
1624 let win = bufwinid(buf)
1625 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001626 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001627 call term_wait(buf)
1628 exe buf . 'bwipe'
1629
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001630 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001631endfunc
1632
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001633func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001634 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001635 vsplit
1636 let buf = Run_shell_in_terminal({})
1637 let win = bufwinid(buf)
1638 call assert_inrange(10, 1000, winheight(win))
1639 call assert_inrange(50, 1000, winwidth(win))
1640 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1641
1642 resize 15
1643 vertical resize 60
1644 redraw
1645 call assert_equal([15, 60], term_getsize(buf))
1646 call assert_equal(15, winheight(win))
1647 call assert_equal(60, winwidth(win))
1648
1649 resize 7
1650 vertical resize 30
1651 redraw
1652 call assert_equal([10, 50], term_getsize(buf))
1653 call assert_equal(7, winheight(win))
1654 call assert_equal(30, winwidth(win))
1655
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001656 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001657 call term_wait(buf)
1658 exe buf . 'bwipe'
1659
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001660 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001661 let buf = Run_shell_in_terminal({})
1662 let win = bufwinid(buf)
1663 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001664 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001665 call term_wait(buf)
1666 exe buf . 'bwipe'
1667
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001668 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001669endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001670
1671func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001672 " make three tabpages, terminal in the middle
1673 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001674 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001675 tabnew
1676 tabprev
1677 call assert_equal(1, winnr('$'))
1678 call assert_equal(2, tabpagenr())
1679 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001680
1681 let buf = Run_shell_in_terminal({})
1682 let termwin = bufwinid(buf)
1683 set termwinkey=<C-L>
1684 call feedkeys("\<C-L>w", 'tx')
1685 call assert_equal(thiswin, win_getid())
1686 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001687 call assert_equal(termwin, win_getid())
1688
Bram Moolenaar997d4242019-09-14 21:23:40 +02001689 if has('langmap')
1690 set langmap=xjyk
1691 call feedkeys("\<C-L>x", 'tx')
1692 call assert_equal(thiswin, win_getid())
1693 call feedkeys("\<C-W>y", 'tx')
1694 call assert_equal(termwin, win_getid())
1695 set langmap=
1696 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001697
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001698 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001699 call assert_equal(3, tabpagenr())
1700 tabprev
1701 call assert_equal(2, tabpagenr())
1702 call assert_equal(termwin, win_getid())
1703
1704 call feedkeys("\<C-L>gT", "xt")
1705 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001706 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001707 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001708 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001709
1710 let job = term_getjob(buf)
1711 call feedkeys("\<C-L>\<C-C>", 'tx')
1712 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001713
1714 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001715 tabnext
1716 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001717 tabprev
1718 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001719endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001720
1721func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001722 CheckUnix
1723
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001724 call writefile([
1725 \ '#!/bin/sh',
1726 \ 'echo "this is standard error" >&2',
1727 \ 'echo "this is standard out" >&1',
1728 \ ], 'Xechoerrout.sh')
1729 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1730
1731 let outfile = 'Xtermstdout'
1732 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001733
1734 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1735 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001736 call assert_equal('this is standard error', term_getline(buf, 1))
1737
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001738 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001739 exe buf . 'bwipe'
1740 call delete('Xechoerrout.sh')
1741 call delete(outfile)
1742endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001743
1744func Test_terminwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001745 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001746
1747 " Let the terminal output more than 'termwinscroll' lines, some at the start
1748 " will be dropped.
1749 exe 'set termwinscroll=' . &lines
1750 let buf = term_start('/bin/sh')
1751 for i in range(1, &lines)
1752 call feedkeys("echo " . i . "\<CR>", 'xt')
1753 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1754 endfor
1755 " Go to Terminal-Normal mode to update the buffer.
1756 call feedkeys("\<C-W>N", 'xt')
1757 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1758
1759 " Every "echo nr" must only appear once
1760 let lines = getline(1, line('$'))
1761 for i in range(&lines - len(lines) / 2 + 2, &lines)
1762 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1763 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1764 endfor
1765
1766 exe buf . 'bwipe!'
1767endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001768
Bram Moolenaar875cf872018-07-08 20:49:07 +02001769" Resizing the terminal window caused an ml_get error.
1770" TODO: This does not reproduce the original problem.
1771func Test_terminal_resize()
1772 set statusline=x
1773 terminal
1774 call assert_equal(2, winnr('$'))
1775
1776 " Fill the terminal with text.
1777 if has('win32')
1778 call feedkeys("dir\<CR>", 'xt')
1779 else
1780 call feedkeys("ls\<CR>", 'xt')
1781 endif
1782 " Go to Terminal-Normal mode for a moment.
1783 call feedkeys("\<C-W>N", 'xt')
1784 " Open a new window
1785 call feedkeys("i\<C-W>n", 'xt')
1786 call assert_equal(3, winnr('$'))
1787 redraw
1788
1789 close
1790 call assert_equal(2, winnr('$'))
1791 call feedkeys("exit\<CR>", 'xt')
1792 set statusline&
1793endfunc
1794
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001795" must be nearly the last, we can't go back from GUI to terminal
1796func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001797 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001798
1799 " Ignore the "failed to create input context" error.
1800 call test_ignore_error('E285:')
1801
1802 gui -f
1803
1804 call assert_equal(1, winnr('$'))
1805 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001806 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001807 call term_wait(buf)
1808
1809 " closing window wipes out the terminal buffer a with finished job
1810 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1811 call assert_equal("", bufname(buf))
1812
1813 unlet g:job
1814endfunc
1815
1816func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001817 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001818 set guioptions+=!
1819
1820 let filename = 'Xtestscript'
1821 if has('win32')
1822 let filename .= '.bat'
1823 let prefix = ''
1824 let contents = ['@echo off', 'exit %1']
1825 else
1826 let filename .= '.sh'
1827 let prefix = './'
1828 let contents = ['#!/bin/sh', 'exit $1']
1829 endif
1830 call writefile(contents, filename)
1831 call setfperm(filename, 'rwxrwx---')
1832
1833 " Check if v:shell_error is equal to the exit status.
1834 let exitval = 0
1835 execute printf(':!%s%s %d', prefix, filename, exitval)
1836 call assert_equal(exitval, v:shell_error)
1837
1838 let exitval = 9
1839 execute printf(':!%s%s %d', prefix, filename, exitval)
1840 call assert_equal(exitval, v:shell_error)
1841
1842 set guioptions&
1843 call delete(filename)
1844endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001845
1846func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001847 CheckUnix
1848
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001849 term ++hidden cat
1850 let bnr = bufnr('$')
1851 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1852 exe 'sbuf ' . bnr
1853 call assert_equal('terminal', &buftype)
1854 call term_sendkeys(bnr, "asdf\<CR>")
1855 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1856 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001857 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001858 bwipe!
1859endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001860
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001861func Test_terminal_switch_mode()
1862 term
1863 let bnr = bufnr('$')
1864 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1865 call feedkeys("\<C-W>N", 'xt')
1866 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1867 call feedkeys("A", 'xt')
1868 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1869 call feedkeys("\<C-W>N", 'xt')
1870 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1871 call feedkeys("I", 'xt')
1872 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1873 call feedkeys("\<C-W>Nv", 'xt')
1874 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1875 call feedkeys("I", 'xt')
1876 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1877 call feedkeys("\<C-W>Nv", 'xt')
1878 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1879 call feedkeys("A", 'xt')
1880 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1881 bwipe!
1882endfunc
1883
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001884func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001885 CheckUnix
1886
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001887 call assert_equal(1, winnr('$'))
1888 term ++hidden ++close ls
1889 let bnr = bufnr('$')
1890 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1891 call WaitForAssert({-> assert_false(bufexists(bnr))})
1892 call assert_equal(1, winnr('$'))
1893endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001894
1895func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001896 " This test does not pass through ConPTY.
1897 if has('conpty')
1898 return
1899 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001900 let contents = [
1901 \ [ 'One', '', 'X' ],
1902 \ [ 'Two', '', '' ],
1903 \ [ 'Three' ] + repeat([''], 30)
1904 \ ]
1905
1906 for c in contents
1907 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001908 if has('win32')
1909 term cmd /c type Xfile
1910 else
1911 term cat Xfile
1912 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001913 let bnr = bufnr('$')
1914 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1915 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001916 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001917 call assert_equal(c, getline(1, line('$')))
1918 quit
1919 endfor
1920
1921 call delete('Xfile')
1922endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001923
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001924func Test_terminal_no_job()
1925 let term = term_start('false', {'term_finish': 'close'})
1926 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1927endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001928
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001929func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001930 CheckUnix
1931
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001932 let buf = Run_shell_in_terminal({})
1933
1934 " Wait for the shell to display a prompt.
1935 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
1936
1937 " Hide the cursor.
1938 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
1939 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
1940
1941 " Show the cursor.
1942 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001943 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001944
1945 " Change color of cursor.
1946 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
1947 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
1948 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
1949 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
1950 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
1951
1952 " Make cursor a blinking block.
1953 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
1954 call WaitForAssert({-> assert_equal([1, 1],
1955 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1956
1957 " Make cursor a steady block.
1958 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
1959 call WaitForAssert({-> assert_equal([0, 1],
1960 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1961
1962 " Make cursor a blinking underline.
1963 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
1964 call WaitForAssert({-> assert_equal([1, 2],
1965 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1966
1967 " Make cursor a steady underline.
1968 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
1969 call WaitForAssert({-> assert_equal([0, 2],
1970 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1971
1972 " Make cursor a blinking vertical bar.
1973 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
1974 call WaitForAssert({-> assert_equal([1, 3],
1975 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1976
1977 " Make cursor a steady vertical bar.
1978 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
1979 call WaitForAssert({-> assert_equal([0, 3],
1980 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1981
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001982 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001983endfunc
1984
Bram Moolenaar1e115362019-01-09 23:01:02 +01001985func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01001986 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02001987 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001988 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01001989 call assert_equal('', term_gettitle(bufnr('$') + 1))
1990
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02001991 if !has('title') || &title == 0 || empty(&t_ts)
1992 throw "Skipped: can't get/set title"
1993 endif
1994
Bram Moolenaar1e115362019-01-09 23:01:02 +01001995 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001996 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02001997 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
1998 call term_sendkeys(term, ":e Xfoo\r")
1999 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002000 else
2001 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002002 call term_sendkeys(term, ":e Xfoo\r")
2003 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002004 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002005
Bram Moolenaar1e115362019-01-09 23:01:02 +01002006 call term_sendkeys(term, ":set titlestring=foo\r")
2007 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2008
2009 exe term . 'bwipe!'
2010endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002011
2012" When drawing the statusline the cursor position may not have been updated
2013" yet.
2014" 1. create a terminal, make it show 2 lines
2015" 2. 0.5 sec later: leave terminal window, execute "i"
2016" 3. 0.5 sec later: clear terminal window, now it's 1 line
2017" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2018" 4. 0.5 sec later: should be done, clean up
2019func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002020 CheckUnix
2021
Bram Moolenaar10772302019-01-20 18:25:54 +01002022 set statusline=x
2023 terminal
2024 let tbuf = bufnr('')
2025 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2026 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2027 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2028 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2029
2030 sleep 2
2031 exe tbuf . 'bwipe!'
2032 au! BufLeave
2033 set statusline=
2034endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002035
2036func Test_terminal_getwinpos()
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002037 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02002038 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002039 endif
2040
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002041 " split, go to the bottom-right window
2042 split
2043 wincmd j
2044 set splitright
2045
2046 call writefile([
2047 \ 'echo getwinpos()',
2048 \ ], 'XTest_getwinpos')
2049 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2050 call term_wait(buf)
2051
2052 " Find the output of getwinpos() in the bottom line.
2053 let rows = term_getsize(buf)[0]
2054 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2055 let line = term_getline(buf, rows)
2056 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2057 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2058
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002059 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002060 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002061 " In the GUI it can be more, let's assume a 20 x 14 cell.
2062 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002063 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002064 let winpos = 50->getwinpos()
2065 call assert_equal(xroot, winpos[0])
2066 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002067 let [winrow, wincol] = win_screenpos('.')
2068 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2069 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2070 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2071 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002072
2073 call term_wait(buf)
2074 call term_sendkeys(buf, ":q\<CR>")
2075 call StopVimInTerminal(buf)
2076 call delete('XTest_getwinpos')
2077 exe buf . 'bwipe!'
2078 set splitright&
2079 only!
2080endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002081
2082func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002083 " somehow doesn't work on MS-Windows
2084 CheckUnix
2085 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002086
2087 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002088 call writefile(["\<Esc>[?1047h"], 'Xtext')
2089 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002090 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2091
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002092 call writefile(["\<Esc>[?1047l"], 'Xtext')
2093 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002094 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002095
2096 call term_sendkeys(buf, "exit\r")
2097 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002098 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002099endfunc