blob: d641b292305cb1b2a0a5af0c38260aab2f21c4f7 [file] [log] [blame]
Bram Moolenaar2d295012022-07-02 16:29:34 +01001" Tests for editing the command line.
2
3source check.vim
4CheckFeature cmdwin
5
6source screendump.vim
7
8func Test_getcmdwintype()
9 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
10 call assert_equal('/', a)
11
12 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
13 call assert_equal('?', a)
14
15 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
16 call assert_equal(':', a)
17
18 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
19 call assert_equal(':', a)
20
21 call assert_equal('', getcmdwintype())
22endfunc
23
24func Test_getcmdwin_autocmd()
25 let s:seq = []
26 augroup CmdWin
27 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
28 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
29 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
30 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
31 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
32 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
33
34 let org_winid = win_getid()
35 let org_bufnr = bufnr()
36 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
37 call assert_equal(':', a)
38 call assert_equal([
39 \ 'WinLeave ' .. org_winid,
40 \ 'WinEnter ' .. s:cmd_winid,
41 \ 'BufLeave ' .. org_bufnr,
42 \ 'BufEnter ' .. s:cmd_bufnr,
43 \ 'CmdWinEnter ' .. s:cmd_winid,
44 \ 'CmdWinLeave ' .. s:cmd_winid,
45 \ 'BufLeave ' .. s:cmd_bufnr,
46 \ 'WinLeave ' .. s:cmd_winid,
47 \ 'WinEnter ' .. org_winid,
48 \ 'BufEnter ' .. org_bufnr,
49 \ ], s:seq)
50
51 au!
52 augroup END
53endfunc
54
55func Test_cmdwin_bug()
56 let winid = win_getid()
57 sp
58 try
59 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
60 catch /^Vim\%((\a\+)\)\=:E11/
61 endtry
62 bw!
63endfunc
64
65func Test_cmdwin_restore()
66 CheckScreendump
67
68 let lines =<< trim [SCRIPT]
69 augroup vimHints | au! | augroup END
70 call setline(1, range(30))
71 2split
72 [SCRIPT]
73 call writefile(lines, 'XTest_restore')
74
75 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
76 call TermWait(buf, 50)
77 call term_sendkeys(buf, "q:")
78 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
79
80 " normal restore
81 call term_sendkeys(buf, ":q\<CR>")
82 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
83
84 " restore after setting 'lines' with one window
85 call term_sendkeys(buf, ":close\<CR>")
86 call term_sendkeys(buf, "q:")
87 call term_sendkeys(buf, ":set lines=18\<CR>")
88 call term_sendkeys(buf, ":q\<CR>")
89 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
90
91 " clean up
92 call StopVimInTerminal(buf)
93 call delete('XTest_restore')
94endfunc
95
96func Test_cmdwin_no_terminal()
97 CheckFeature terminal
98 CheckNotMSWindows
99
100 let buf = RunVimInTerminal('', {'rows': 12})
101 call TermWait(buf, 50)
102 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
103 call term_sendkeys(buf, "q:")
104 call term_sendkeys(buf, ":let buf = term_start(['/bin/echo'], #{hidden: 1})\<CR>")
105 call VerifyScreenDump(buf, 'Test_cmdwin_no_terminal', {})
106 call term_sendkeys(buf, ":q\<CR>")
107 call StopVimInTerminal(buf)
108endfunc
109
110func Test_cmdwin_feedkeys()
111 " This should not generate E488
112 call feedkeys("q:\<CR>", 'x')
113 " Using feedkeys with q: only should automatically close the cmd window
114 call feedkeys('q:', 'xt')
115 call assert_equal(1, winnr('$'))
116 call assert_equal('', getcmdwintype())
117endfunc
118
119" Tests for the issues fixed in 7.4.441.
120" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
121func Test_cmdwin_cedit()
122 exe "set cedit=\<C-c>"
123 normal! :
124 call assert_equal(1, winnr('$'))
125
126 let g:cmd_wintype = ''
127 func CmdWinType()
128 let g:cmd_wintype = getcmdwintype()
129 let g:wintype = win_gettype()
130 return ''
131 endfunc
132
133 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
134 echo input('')
135 call assert_equal('@', g:cmd_wintype)
136 call assert_equal('command', g:wintype)
137
138 set cedit&vim
139 delfunc CmdWinType
140endfunc
141
142" Test for CmdwinEnter autocmd
143func Test_cmdwin_autocmd()
144 augroup CmdWin
145 au!
146 autocmd BufLeave * if &buftype == '' | update | endif
147 autocmd CmdwinEnter * startinsert
148 augroup END
149
150 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
151 call assert_equal('xyz', @:)
152
153 augroup CmdWin
154 au!
155 augroup END
156 augroup! CmdWin
157endfunc
158
159func Test_cmdwin_jump_to_win()
160 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
161 new
162 set modified
163 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
164 close!
165 call feedkeys("q/:close\<CR>", "xt")
166 call assert_equal(1, winnr('$'))
167 call feedkeys("q/:exit\<CR>", "xt")
168 call assert_equal(1, winnr('$'))
169
170 " opening command window twice should fail
171 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
172 call assert_equal(1, winnr('$'))
173endfunc
174
175func Test_cmdwin_tabpage()
176 tabedit
177 call assert_fails("silent norm q/g :I\<Esc>", 'E11:')
178 tabclose!
179endfunc
180
181func Test_cmdwin_interrupted()
182 CheckScreendump
183
184 " aborting the :smile output caused the cmdline window to use the current
185 " buffer.
186 let lines =<< trim [SCRIPT]
187 au WinNew * smile
188 [SCRIPT]
189 call writefile(lines, 'XTest_cmdwin')
190
191 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
192 " open cmdwin
193 call term_sendkeys(buf, "q:")
194 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
195 " quit more prompt for :smile command
196 call term_sendkeys(buf, "q")
197 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
198 " execute a simple command
199 call term_sendkeys(buf, "aecho 'done'\<CR>")
200 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
201
202 " clean up
203 call StopVimInTerminal(buf)
204 call delete('XTest_cmdwin')
205endfunc
206
207" Test for recursively getting multiple command line inputs
208func Test_cmdwin_multi_input()
209 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
210 call assert_equal('"cyan', @:)
211endfunc
212
213" Test for normal mode commands not supported in the cmd window
214func Test_cmdwin_blocked_commands()
215 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
216 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
217 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
218 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
219 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
220 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
221 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
222 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
223 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
224 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
225 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
226 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
227 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
228 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
229 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
230 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
231 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
232 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
233 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
234 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
235 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
236 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
237 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
238 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
239 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
240 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
241 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
242endfunc
243
244" Close the Cmd-line window in insert mode using CTRL-C
245func Test_cmdwin_insert_mode_close()
246 %bw!
247 let s = ''
248 exe "normal q:a\<C-C>let s='Hello'\<CR>"
249 call assert_equal('Hello', s)
250 call assert_equal(1, winnr('$'))
251endfunc
252
253func Test_cmdwin_ex_mode_with_modifier()
254 " this was accessing memory after allocated text in Ex mode
255 new
256 call setline(1, ['some', 'text', 'lines'])
257 silent! call feedkeys("gQnormal vq:atopleft\<C-V>\<CR>\<CR>", 'xt')
258 bwipe!
259endfunc
260
261func s:ComplInCmdwin_GlobalCompletion(a, l, p)
262 return 'global'
263endfunc
264
265func s:ComplInCmdwin_LocalCompletion(a, l, p)
266 return 'local'
267endfunc
268
269func Test_compl_in_cmdwin()
270 set wildmenu wildchar=<Tab>
271 com! -nargs=1 -complete=command GetInput let input = <q-args>
272 com! -buffer TestCommand echo 'TestCommand'
273 let w:test_winvar = 'winvar'
274 let b:test_bufvar = 'bufvar'
275
276 " User-defined commands
277 let input = ''
278 call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
279 call assert_equal('TestCommand', input)
280
281 let input = ''
282 call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
283 call assert_equal('T', input)
284
285
286 com! -nargs=1 -complete=var GetInput let input = <q-args>
287 " Window-local variables
288 let input = ''
289 call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
290 call assert_equal('w:test_winvar', input)
291
292 let input = ''
293 call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
294 call assert_equal('w:test_', input)
295
296 " Buffer-local variables
297 let input = ''
298 call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
299 call assert_equal('b:test_bufvar', input)
300
301 let input = ''
302 call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
303 call assert_equal('b:test_', input)
304
305
306 " Argument completion of buffer-local command
307 func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
308 return ['global']
309 endfunc
310
311 func s:ComplInCmdwin_LocalCompletionList(a, l, p)
312 return ['local']
313 endfunc
314
315 func s:ComplInCmdwin_CheckCompletion(arg)
316 call assert_equal('local', a:arg)
317 endfunc
318
319 com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
320 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
321 com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
322 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
323 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
324
325 com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
326 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
327 com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
328 \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
329
330 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
331
332 func! s:ComplInCmdwin_CheckCompletion(arg)
333 call assert_equal('global', a:arg)
334 endfunc
335 new
336 call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
337 quit
338
339 delfunc s:ComplInCmdwin_GlobalCompletion
340 delfunc s:ComplInCmdwin_LocalCompletion
341 delfunc s:ComplInCmdwin_GlobalCompletionList
342 delfunc s:ComplInCmdwin_LocalCompletionList
343 delfunc s:ComplInCmdwin_CheckCompletion
344
345 delcom -buffer TestCommand
346 delcom TestCommand
347 delcom GetInput
348 unlet w:test_winvar
349 unlet b:test_bufvar
350 set wildmenu& wildchar&
351endfunc
352
353func Test_cmdwin_ctrl_bsl()
354 " Using CTRL-\ CTRL-N in cmd window should close the window
355 call feedkeys("q:\<C-\>\<C-N>", 'xt')
356 call assert_equal('', getcmdwintype())
357endfunc
358
Bram Moolenaarc963ec32022-07-24 20:08:01 +0100359func Test_cant_open_cmdwin_in_cmdwin()
360 try
361 call feedkeys("q:q::q\<CR>", "x!")
362 catch
363 let caught = v:exception
364 endtry
365 call assert_match('E1292:', caught)
366endfunc
367
Bram Moolenaar2d295012022-07-02 16:29:34 +0100368
369" vim: shiftwidth=2 sts=2 expandtab