xref: /vim-8.2.3635/src/testdir/test_cmdline.vim (revision a7c4e747)
1" Tests for editing the command line.
2
3source check.vim
4source screendump.vim
5source view_util.vim
6source shared.vim
7
8func Test_complete_tab()
9  call writefile(['testfile'], 'Xtestfile')
10  call feedkeys(":e Xtest\t\r", "tx")
11  call assert_equal('testfile', getline(1))
12  call delete('Xtestfile')
13endfunc
14
15func Test_complete_list()
16  " We can't see the output, but at least we check the code runs properly.
17  call feedkeys(":e test\<C-D>\r", "tx")
18  call assert_equal('test', expand('%:t'))
19
20  " If a command doesn't support completion, then CTRL-D should be literally
21  " used.
22  call feedkeys(":chistory \<C-D>\<C-B>\"\<CR>", 'xt')
23  call assert_equal("\"chistory \<C-D>", @:)
24endfunc
25
26func Test_complete_wildmenu()
27  call mkdir('Xdir1/Xdir2', 'p')
28  call writefile(['testfile1'], 'Xdir1/Xtestfile1')
29  call writefile(['testfile2'], 'Xdir1/Xtestfile2')
30  call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3')
31  call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4')
32  set wildmenu
33
34  " Pressing <Tab> completes, and moves to next files when pressing again.
35  call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx')
36  call assert_equal('testfile1', getline(1))
37  call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
38  call assert_equal('testfile2', getline(1))
39
40  " <S-Tab> is like <Tab> but begin with the last match and then go to
41  " previous.
42  call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx')
43  call assert_equal('testfile2', getline(1))
44  call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
45  call assert_equal('testfile1', getline(1))
46
47  " <Left>/<Right> to move to previous/next file.
48  call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx')
49  call assert_equal('testfile1', getline(1))
50  call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
51  call assert_equal('testfile2', getline(1))
52  call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
53  call assert_equal('testfile1', getline(1))
54
55  " <Up>/<Down> to go up/down directories.
56  call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx')
57  call assert_equal('testfile3', getline(1))
58  call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
59  call assert_equal('testfile1', getline(1))
60
61  " Test for canceling the wild menu by adding a character
62  redrawstatus
63  call feedkeys(":e Xdir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
64  call assert_equal('"e Xdir1/Xdir2/x', @:)
65
66  " Completion using a relative path
67  cd Xdir1/Xdir2
68  call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
69  call assert_equal('"e Xtestfile3 Xtestfile4', @:)
70  cd -
71
72  cnoremap <expr> <F2> wildmenumode()
73  call feedkeys(":cd Xdir\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
74  call assert_equal('"cd Xdir1/1', @:)
75  cunmap <F2>
76
77  " cleanup
78  %bwipe
79  call delete('Xdir1/Xdir2/Xtestfile4')
80  call delete('Xdir1/Xdir2/Xtestfile3')
81  call delete('Xdir1/Xtestfile2')
82  call delete('Xdir1/Xtestfile1')
83  call delete('Xdir1/Xdir2', 'd')
84  call delete('Xdir1', 'd')
85  set nowildmenu
86endfunc
87
88func Test_map_completion()
89  CheckFeature cmdline_compl
90  call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
91  call assert_equal('"map <unique> <silent>', getreg(':'))
92  call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
93  call assert_equal('"map <script> <unique>', getreg(':'))
94  call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
95  call assert_equal('"map <expr> <script>', getreg(':'))
96  call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
97  call assert_equal('"map <buffer> <expr>', getreg(':'))
98  call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
99  call assert_equal('"map <nowait> <buffer>', getreg(':'))
100  call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
101  call assert_equal('"map <special> <nowait>', getreg(':'))
102  call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
103  call assert_equal('"map <silent> <special>', getreg(':'))
104
105  map <Middle>x middle
106
107  map ,f commaf
108  map ,g commaf
109  map <Left> left
110  map <A-Left>x shiftleft
111  call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
112  call assert_equal('"map ,f', getreg(':'))
113  call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
114  call assert_equal('"map ,g', getreg(':'))
115  call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
116  call assert_equal('"map <Left>', getreg(':'))
117  call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
118  call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
119  unmap ,f
120  unmap ,g
121  unmap <Left>
122  unmap <A-Left>x
123
124  set cpo-=< cpo-=B cpo-=k
125  map <Left> left
126  call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
127  call assert_equal('"map <Left>', getreg(':'))
128  call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
129  call assert_equal("\"map <M\<Tab>", getreg(':'))
130  unmap <Left>
131
132  set cpo+=<
133  map <Left> left
134  exe "set t_k6=\<Esc>[17~"
135  call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
136  call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
137  call assert_equal('"map <Left>', getreg(':'))
138  if !has('gui_running')
139    call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
140    call assert_equal("\"map <F6>x", getreg(':'))
141  endif
142  unmap <Left>
143  call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
144  set cpo-=<
145
146  set cpo+=B
147  map <Left> left
148  call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
149  call assert_equal('"map <Left>', getreg(':'))
150  unmap <Left>
151  set cpo-=B
152
153  set cpo+=k
154  map <Left> left
155  call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
156  call assert_equal('"map <Left>', getreg(':'))
157  unmap <Left>
158  set cpo-=k
159
160  unmap <Middle>x
161  set cpo&vim
162endfunc
163
164func Test_match_completion()
165  CheckFeature cmdline_compl
166  hi Aardig ctermfg=green
167  call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
168  call assert_equal('"match Aardig', getreg(':'))
169  call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
170  call assert_equal('"match none', getreg(':'))
171endfunc
172
173func Test_highlight_completion()
174  CheckFeature cmdline_compl
175  hi Aardig ctermfg=green
176  call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
177  call assert_equal('"hi Aardig', getreg(':'))
178  call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
179  call assert_equal('"hi default Aardig', getreg(':'))
180  call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
181  call assert_equal('"hi clear Aardig', getreg(':'))
182  call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
183  call assert_equal('"hi link', getreg(':'))
184  call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
185  call assert_equal('"hi default', getreg(':'))
186  call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
187  call assert_equal('"hi clear', getreg(':'))
188  call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
189  call assert_equal('"hi clear Aardig Aardig', getreg(':'))
190  call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
191  call assert_equal("\"hi Aardig \t", getreg(':'))
192
193  " A cleared group does not show up in completions.
194  hi Anders ctermfg=green
195  call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
196  hi clear Aardig
197  call assert_equal(['Anders'], getcompletion('A', 'highlight'))
198  hi clear Anders
199  call assert_equal([], getcompletion('A', 'highlight'))
200endfunc
201
202" Test for command-line expansion of "hi Ni " (easter egg)
203func Test_highlight_easter_egg()
204  call test_override('ui_delay', 1)
205  call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
206  call assert_equal("\"hi Ni \<Tab>", @:)
207  call test_override('ALL', 0)
208endfunc
209
210func Test_getcompletion()
211  CheckFeature cmdline_compl
212  let groupcount = len(getcompletion('', 'event'))
213  call assert_true(groupcount > 0)
214  let matchcount = len('File'->getcompletion('event'))
215  call assert_true(matchcount > 0)
216  call assert_true(groupcount > matchcount)
217
218  if has('menu')
219    source $VIMRUNTIME/menu.vim
220    let matchcount = len(getcompletion('', 'menu'))
221    call assert_true(matchcount > 0)
222    call assert_equal(['File.'], getcompletion('File', 'menu'))
223    call assert_true(matchcount > 0)
224    let matchcount = len(getcompletion('File.', 'menu'))
225    call assert_true(matchcount > 0)
226  endif
227
228  let l = getcompletion('v:n', 'var')
229  call assert_true(index(l, 'v:null') >= 0)
230  let l = getcompletion('v:notexists', 'var')
231  call assert_equal([], l)
232
233  args a.c b.c
234  let l = getcompletion('', 'arglist')
235  call assert_equal(['a.c', 'b.c'], l)
236  %argdelete
237
238  let l = getcompletion('', 'augroup')
239  call assert_true(index(l, 'END') >= 0)
240  let l = getcompletion('blahblah', 'augroup')
241  call assert_equal([], l)
242
243  let l = getcompletion('', 'behave')
244  call assert_true(index(l, 'mswin') >= 0)
245  let l = getcompletion('not', 'behave')
246  call assert_equal([], l)
247
248  let l = getcompletion('', 'color')
249  call assert_true(index(l, 'default') >= 0)
250  let l = getcompletion('dirty', 'color')
251  call assert_equal([], l)
252
253  let l = getcompletion('', 'command')
254  call assert_true(index(l, 'sleep') >= 0)
255  let l = getcompletion('awake', 'command')
256  call assert_equal([], l)
257
258  let l = getcompletion('', 'dir')
259  call assert_true(index(l, 'samples/') >= 0)
260  let l = getcompletion('NoMatch', 'dir')
261  call assert_equal([], l)
262
263  let l = getcompletion('exe', 'expression')
264  call assert_true(index(l, 'executable(') >= 0)
265  let l = getcompletion('kill', 'expression')
266  call assert_equal([], l)
267
268  let l = getcompletion('tag', 'function')
269  call assert_true(index(l, 'taglist(') >= 0)
270  let l = getcompletion('paint', 'function')
271  call assert_equal([], l)
272
273  let Flambda = {-> 'hello'}
274  let l = getcompletion('', 'function')
275  let l = filter(l, {i, v -> v =~ 'lambda'})
276  call assert_equal([], l)
277
278  let l = getcompletion('run', 'file')
279  call assert_true(index(l, 'runtest.vim') >= 0)
280  let l = getcompletion('walk', 'file')
281  call assert_equal([], l)
282  set wildignore=*.vim
283  let l = getcompletion('run', 'file', 1)
284  call assert_true(index(l, 'runtest.vim') < 0)
285  set wildignore&
286
287  let l = getcompletion('ha', 'filetype')
288  call assert_true(index(l, 'hamster') >= 0)
289  let l = getcompletion('horse', 'filetype')
290  call assert_equal([], l)
291
292  let l = getcompletion('z', 'syntax')
293  call assert_true(index(l, 'zimbu') >= 0)
294  let l = getcompletion('emacs', 'syntax')
295  call assert_equal([], l)
296
297  let l = getcompletion('jikes', 'compiler')
298  call assert_true(index(l, 'jikes') >= 0)
299  let l = getcompletion('break', 'compiler')
300  call assert_equal([], l)
301
302  let l = getcompletion('last', 'help')
303  call assert_true(index(l, ':tablast') >= 0)
304  let l = getcompletion('giveup', 'help')
305  call assert_equal([], l)
306
307  let l = getcompletion('time', 'option')
308  call assert_true(index(l, 'timeoutlen') >= 0)
309  let l = getcompletion('space', 'option')
310  call assert_equal([], l)
311
312  let l = getcompletion('er', 'highlight')
313  call assert_true(index(l, 'ErrorMsg') >= 0)
314  let l = getcompletion('dark', 'highlight')
315  call assert_equal([], l)
316
317  let l = getcompletion('', 'messages')
318  call assert_true(index(l, 'clear') >= 0)
319  let l = getcompletion('not', 'messages')
320  call assert_equal([], l)
321
322  let l = getcompletion('', 'mapclear')
323  call assert_true(index(l, '<buffer>') >= 0)
324  let l = getcompletion('not', 'mapclear')
325  call assert_equal([], l)
326
327  let l = getcompletion('.', 'shellcmd')
328  call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
329  call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
330  let root = has('win32') ? 'C:\\' : '/'
331  let l = getcompletion(root, 'shellcmd')
332  let expected = map(filter(glob(root . '*', 0, 1),
333        \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
334  call assert_equal(expected, l)
335
336  if has('cscope')
337    let l = getcompletion('', 'cscope')
338    let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
339    call assert_equal(cmds, l)
340    " using cmdline completion must not change the result
341    call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
342    let l = getcompletion('', 'cscope')
343    call assert_equal(cmds, l)
344    let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
345    let l = getcompletion('find ', 'cscope')
346    call assert_equal(keys, l)
347  endif
348
349  if has('signs')
350    sign define Testing linehl=Comment
351    let l = getcompletion('', 'sign')
352    let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
353    call assert_equal(cmds, l)
354    " using cmdline completion must not change the result
355    call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
356    let l = getcompletion('', 'sign')
357    call assert_equal(cmds, l)
358    let l = getcompletion('list ', 'sign')
359    call assert_equal(['Testing'], l)
360  endif
361
362  " Command line completion tests
363  let l = getcompletion('cd ', 'cmdline')
364  call assert_true(index(l, 'samples/') >= 0)
365  let l = getcompletion('cd NoMatch', 'cmdline')
366  call assert_equal([], l)
367  let l = getcompletion('let v:n', 'cmdline')
368  call assert_true(index(l, 'v:null') >= 0)
369  let l = getcompletion('let v:notexists', 'cmdline')
370  call assert_equal([], l)
371  let l = getcompletion('call tag', 'cmdline')
372  call assert_true(index(l, 'taglist(') >= 0)
373  let l = getcompletion('call paint', 'cmdline')
374  call assert_equal([], l)
375
376  " For others test if the name is recognized.
377  let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
378  if has('cmdline_hist')
379    call add(names, 'history')
380  endif
381  if has('gettext')
382    call add(names, 'locale')
383  endif
384  if has('profile')
385    call add(names, 'syntime')
386  endif
387
388  set tags=Xtags
389  call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
390
391  for name in names
392    let matchcount = len(getcompletion('', name))
393    call assert_true(matchcount >= 0, 'No matches for ' . name)
394  endfor
395
396  call delete('Xtags')
397  set tags&
398
399  call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
400  call assert_fails('call getcompletion("", "burp")', 'E475:')
401  call assert_fails('call getcompletion("abc", [])', 'E475:')
402endfunc
403
404func Test_shellcmd_completion()
405  let save_path = $PATH
406
407  call mkdir('Xpathdir/Xpathsubdir', 'p')
408  call writefile([''], 'Xpathdir/Xfile.exe')
409  call setfperm('Xpathdir/Xfile.exe', 'rwx------')
410
411  " Set PATH to example directory without trailing slash.
412  let $PATH = getcwd() . '/Xpathdir'
413
414  " Test for the ":!<TAB>" case.  Previously, this would include subdirs of
415  " dirs in the PATH, even though they won't be executed.  We check that only
416  " subdirs of the PWD and executables from the PATH are included in the
417  " suggestions.
418  let actual = getcompletion('X', 'shellcmd')
419  let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
420  call insert(expected, 'Xfile.exe')
421  call assert_equal(expected, actual)
422
423  call delete('Xpathdir', 'rf')
424  let $PATH = save_path
425endfunc
426
427func Test_expand_star_star()
428  call mkdir('a/b', 'p')
429  call writefile(['asdfasdf'], 'a/b/fileXname')
430  call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
431  call assert_equal('find a/b/fileXname', getreg(':'))
432  bwipe!
433  call delete('a', 'rf')
434endfunc
435
436func Test_cmdline_paste()
437  let @a = "def"
438  call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
439  call assert_equal('"abc def ghi', @:)
440
441  new
442  call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
443
444  call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
445  call assert_equal('"aaa asdf bbb', @:)
446
447  call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
448  call assert_equal('"aaa /tmp/some bbb', @:)
449
450  call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
451  call assert_equal('"aaa '.getline(1).' bbb', @:)
452
453  set incsearch
454  call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
455  call assert_equal('"aaa verylongword bbb', @:)
456
457  call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
458  call assert_equal('"aaa a;b-c*d bbb', @:)
459
460  call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
461  call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
462  bwipe!
463
464  " Error while typing a command used to cause that it was not executed
465  " in the end.
466  new
467  try
468    call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
469  catch /^Vim\%((\a\+)\)\=:E32/
470    " ignore error E32
471  endtry
472  call assert_equal("Xtestfile", bufname("%"))
473
474  " Try to paste an invalid register using <C-R>
475  call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
476  call assert_equal('"onetwo', @:)
477
478  " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
479  let @a = "xy\<C-H>z"
480  call feedkeys(":\"\<C-R>a\<CR>", 'xt')
481  call assert_equal('"xz', @:)
482  call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
483  call assert_equal("\"xy\<C-H>z", @:)
484  call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
485  call assert_equal("\"xy\<C-H>z", @:)
486
487  " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
488  let @a = "xy\<C-V>z"
489  call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
490  call assert_equal('"xyz', @:)
491  call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
492  call assert_equal("\"xy\<C-V>z", @:)
493
494  call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
495
496  bwipe!
497endfunc
498
499func Test_cmdline_remove_char()
500  let encoding_save = &encoding
501
502  for e in ['utf8', 'latin1']
503    exe 'set encoding=' . e
504
505    call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
506    call assert_equal('"abc ef', @:, e)
507
508    call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
509    call assert_equal('"abcdef', @:)
510
511    call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
512    call assert_equal('"abc ghi', @:, e)
513
514    call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
515    call assert_equal('"def', @:, e)
516  endfor
517
518  let &encoding = encoding_save
519endfunc
520
521func Test_cmdline_keymap_ctrl_hat()
522  CheckFeature keymap
523
524  set keymap=esperanto
525  call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
526  call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
527  set keymap=
528endfunc
529
530func Test_illegal_address1()
531  new
532  2;'(
533  2;')
534  quit
535endfunc
536
537func Test_illegal_address2()
538  call writefile(['c', 'x', '  x', '.', '1;y'], 'Xtest.vim')
539  new
540  source Xtest.vim
541  " Trigger calling validate_cursor()
542  diffsp Xtest.vim
543  quit!
544  bwipe!
545  call delete('Xtest.vim')
546endfunc
547
548func Test_cmdline_complete_wildoptions()
549  help
550  call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
551  let a = join(sort(split(@:)),' ')
552  set wildoptions=tagfile
553  call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
554  let b = join(sort(split(@:)),' ')
555  call assert_equal(a, b)
556  bw!
557endfunc
558
559func Test_cmdline_complete_user_cmd()
560  command! -complete=color -nargs=1 Foo :
561  call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
562  call assert_equal('"Foo blue', @:)
563  call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
564  call assert_equal('"Foo blue', @:)
565  delcommand Foo
566endfunc
567
568func s:ScriptLocalFunction()
569  echo 'yes'
570endfunc
571
572func Test_cmdline_complete_user_func()
573  call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
574  call assert_match('"func Test_cmdline_complete_user', @:)
575  call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
576  call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
577endfunc
578
579func Test_cmdline_complete_user_names()
580  if has('unix') && executable('whoami')
581    let whoami = systemlist('whoami')[0]
582    let first_letter = whoami[0]
583    if len(first_letter) > 0
584      " Trying completion of  :e ~x  where x is the first letter of
585      " the user name should complete to at least the user name.
586      call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
587      call assert_match('^"e \~.*\<' . whoami . '\>', @:)
588    endif
589  elseif has('win32')
590    " Just in case: check that the system has an Administrator account.
591    let names = system('net user')
592    if names =~ 'Administrator'
593      " Trying completion of  :e ~A  should complete to Administrator.
594      " There could be other names starting with "A" before Administrator.
595      call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
596      call assert_match('^"e \~.*Administrator', @:)
597    endif
598  else
599    throw 'Skipped: does not work on this platform'
600  endif
601endfunc
602
603func Test_cmdline_complete_bang()
604  CheckExecutable whoami
605  call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
606  call assert_match('^".*\<whoami\>', @:)
607endfunc
608
609func Test_cmdline_complete_languages()
610  let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
611  call assert_equal(lang, v:lc_time)
612
613  let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
614  call assert_equal(lang, v:ctype)
615
616  let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
617  call assert_equal(lang, v:collate)
618
619  let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
620  call assert_equal(lang, v:lang)
621
622  call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
623  call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
624
625  call assert_match('^"language .*\<' . lang . '\>', @:)
626
627  call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
628  call assert_match('^"language .*\<' . lang . '\>', @:)
629
630  call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
631  call assert_match('^"language .*\<' . lang . '\>', @:)
632
633  call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
634  call assert_match('^"language .*\<' . lang . '\>', @:)
635
636  call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
637  call assert_match('^"language .*\<' . lang . '\>', @:)
638endfunc
639
640func Test_cmdline_complete_env_variable()
641  let $X_VIM_TEST_COMPLETE_ENV = 'foo'
642  call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
643  call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
644  unlet $X_VIM_TEST_COMPLETE_ENV
645endfunc
646
647" Test for various command-line completion
648func Test_cmdline_complete_various()
649  " completion for a command starting with a comment
650  call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
651  call assert_equal("\" :|\"\<C-A>", @:)
652
653  " completion for a range followed by a comment
654  call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
655  call assert_equal("\"1,2\"\<C-A>", @:)
656
657  " completion for :k command
658  call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
659  call assert_equal("\"ka\<C-A>", @:)
660
661  " completion for short version of the :s command
662  call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
663  call assert_equal("\"sI \<C-A>", @:)
664
665  " completion for :write command
666  call mkdir('Xdir')
667  call writefile(['one'], 'Xdir/Xfile1')
668  let save_cwd = getcwd()
669  cd Xdir
670  call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
671  call assert_equal("\"w >> Xfile1", @:)
672  call chdir(save_cwd)
673  call delete('Xdir', 'rf')
674
675  " completion for :w ! and :r ! commands
676  call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
677  call assert_equal("\"w !invalid_xyz_cmd", @:)
678  call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
679  call assert_equal("\"r !invalid_xyz_cmd", @:)
680
681  " completion for :>> and :<< commands
682  call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
683  call assert_equal("\">>>\<C-A>", @:)
684  call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
685  call assert_equal("\"<<<\<C-A>", @:)
686
687  " completion for command with +cmd argument
688  call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
689  call assert_equal("\"buffer +/pat Xabc", @:)
690  call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
691  call assert_equal("\"buffer +/pat\<C-A>", @:)
692
693  " completion for a command with a trailing comment
694  call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
695  call assert_equal("\"ls \" comment\<C-A>", @:)
696
697  " completion for a command with a trailing command
698  call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
699  call assert_equal("\"ls | ls", @:)
700
701  " completion for a command with an CTRL-V escaped argument
702  call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
703  call assert_equal("\"ls \<C-V>a\<C-A>", @:)
704
705  " completion for a command that doesn't take additional arguments
706  call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
707  call assert_equal("\"all abc\<C-A>", @:)
708
709  " completion for a command with a command modifier
710  call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
711  call assert_equal("\"topleft new", @:)
712
713  " completion for the :match command
714  call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
715  call assert_equal("\"match Search /pat/\<C-A>", @:)
716
717  " completion for the :s command
718  call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
719  call assert_equal("\"s/from/to/g\<C-A>", @:)
720
721  " completion for the :dlist command
722  call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
723  call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
724
725  " completion for the :doautocmd command
726  call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
727  call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
728
729  " completion for the :augroup command
730  augroup XTest
731  augroup END
732  call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
733  call assert_equal("\"augroup XTest", @:)
734  augroup! XTest
735
736  " completion for the :unlet command
737  call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
738  call assert_equal("\"unlet one two", @:)
739
740  " completion for the :bdelete command
741  call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
742  call assert_equal("\"bdel a b c", @:)
743
744  " completion for the :mapclear command
745  call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
746  call assert_equal("\"mapclear <buffer>", @:)
747
748  " completion for user defined commands with menu names
749  menu Test.foo :ls<CR>
750  com -nargs=* -complete=menu MyCmd
751  call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
752  call assert_equal('"MyCmd Test.', @:)
753  delcom MyCmd
754  unmenu Test
755
756  " completion for user defined commands with mappings
757  mapclear
758  map <F3> :ls<CR>
759  com -nargs=* -complete=mapping MyCmd
760  call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
761  call assert_equal('"MyCmd <F3>', @:)
762  mapclear
763  delcom MyCmd
764
765  " completion for :set path= with multiple backslashes
766  call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
767  call assert_equal('"set path=a\\\ b', @:)
768
769  " completion for :set dir= with a backslash
770  call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
771  call assert_equal('"set dir=a\ b', @:)
772
773  " completion for the :py3 commands
774  call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
775  call assert_equal('"py3 py3do py3file', @:)
776
777  " redir @" is not the start of a comment. So complete after that
778  call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
779  call assert_equal('"redir @" | cwindow', @:)
780
781  " completion after a backtick
782  call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
783  call assert_equal('"e `a1b2c', @:)
784
785  " completion for :language command with an invalid argument
786  call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
787  call assert_equal("\"language dummy \t", @:)
788
789  " completion for commands after a :global command
790  call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
791  call assert_equal('"g/a\xb/clearjumps', @:)
792
793  " completion with ambiguous user defined commands
794  com TCmd1 echo 'TCmd1'
795  com TCmd2 echo 'TCmd2'
796  call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
797  call assert_equal('"TCmd ', @:)
798  delcom TCmd1
799  delcom TCmd2
800
801  " completion after a range followed by a pipe (|) character
802  call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
803  call assert_equal('"1,10 | chistory', @:)
804endfunc
805
806func Test_cmdline_write_alternatefile()
807  new
808  call setline('.', ['one', 'two'])
809  f foo.txt
810  new
811  f #-A
812  call assert_equal('foo.txt-A', expand('%'))
813  f #<-B.txt
814  call assert_equal('foo-B.txt', expand('%'))
815  f %<
816  call assert_equal('foo-B', expand('%'))
817  new
818  call assert_fails('f #<', 'E95')
819  bw!
820  f foo-B.txt
821  f %<-A
822  call assert_equal('foo-B-A', expand('%'))
823  bw!
824  bw!
825endfunc
826
827" using a leading backslash here
828set cpo+=C
829
830func Test_cmdline_search_range()
831  new
832  call setline(1, ['a', 'b', 'c', 'd'])
833  /d
834  1,\/s/b/B/
835  call assert_equal('B', getline(2))
836
837  /a
838  $
839  \?,4s/c/C/
840  call assert_equal('C', getline(3))
841
842  call setline(1, ['a', 'b', 'c', 'd'])
843  %s/c/c/
844  1,\&s/b/B/
845  call assert_equal('B', getline(2))
846
847  let @/ = 'apple'
848  call assert_fails('\/print', ['E486:.*apple'])
849
850  bwipe!
851endfunc
852
853" Test for the tick mark (') in an excmd range
854func Test_tick_mark_in_range()
855  " If only the tick is passed as a range and no command is specified, there
856  " should not be an error
857  call feedkeys(":'\<CR>", 'xt')
858  call assert_equal("'", getreg(':'))
859  call assert_fails("',print", 'E78:')
860endfunc
861
862" Test for using a line number followed by a search pattern as range
863func Test_lnum_and_pattern_as_range()
864  new
865  call setline(1, ['foo 1', 'foo 2', 'foo 3'])
866  let @" = ''
867  2/foo/yank
868  call assert_equal("foo 3\n", @")
869  call assert_equal(1, line('.'))
870  close!
871endfunc
872
873" Tests for getcmdline(), getcmdpos() and getcmdtype()
874func Check_cmdline(cmdtype)
875  call assert_equal('MyCmd a', getcmdline())
876  call assert_equal(8, getcmdpos())
877  call assert_equal(a:cmdtype, getcmdtype())
878  return ''
879endfunc
880
881set cpo&
882
883func Test_getcmdtype()
884  call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
885
886  let cmdtype = ''
887  debuggreedy
888  call feedkeys(":debug echo 'test'\<CR>", "t")
889  call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
890  call feedkeys("cont\<CR>", "xt")
891  0debuggreedy
892  call assert_equal('>', cmdtype)
893
894  call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
895  call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
896
897  call feedkeys(":call input('Answer?')\<CR>", "t")
898  call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
899
900  call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
901
902  cnoremap <expr> <F6> Check_cmdline('=')
903  call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
904  cunmap <F6>
905
906  call assert_equal('', getcmdline())
907endfunc
908
909func Test_getcmdwintype()
910  call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
911  call assert_equal('/', a)
912
913  call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
914  call assert_equal('?', a)
915
916  call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
917  call assert_equal(':', a)
918
919  call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
920  call assert_equal(':', a)
921
922  call assert_equal('', getcmdwintype())
923endfunc
924
925func Test_getcmdwin_autocmd()
926  let s:seq = []
927  augroup CmdWin
928  au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
929  au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
930  au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
931  au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
932  au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
933  au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
934
935  let org_winid = win_getid()
936  let org_bufnr = bufnr()
937  call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
938  call assert_equal(':', a)
939  call assert_equal([
940	\ 'WinLeave ' .. org_winid,
941	\ 'WinEnter ' .. s:cmd_winid,
942	\ 'BufLeave ' .. org_bufnr,
943	\ 'BufEnter ' .. s:cmd_bufnr,
944	\ 'CmdWinEnter ' .. s:cmd_winid,
945	\ 'CmdWinLeave ' .. s:cmd_winid,
946	\ 'BufLeave ' .. s:cmd_bufnr,
947	\ 'WinLeave ' .. s:cmd_winid,
948	\ 'WinEnter ' .. org_winid,
949	\ 'BufEnter ' .. org_bufnr,
950	\ ], s:seq)
951
952  au!
953  augroup END
954endfunc
955
956func Test_verbosefile()
957  set verbosefile=Xlog
958  echomsg 'foo'
959  echomsg 'bar'
960  set verbosefile=
961  let log = readfile('Xlog')
962  call assert_match("foo\nbar", join(log, "\n"))
963  call delete('Xlog')
964  call mkdir('Xdir')
965  call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
966  call delete('Xdir', 'd')
967endfunc
968
969func Test_verbose_option()
970  CheckScreendump
971
972  let lines =<< trim [SCRIPT]
973    command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
974    call feedkeys("\r", 't') " for the hit-enter prompt
975    set verbose=20
976  [SCRIPT]
977  call writefile(lines, 'XTest_verbose')
978
979  let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
980  call TermWait(buf, 50)
981  call term_sendkeys(buf, ":DoSomething\<CR>")
982  call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
983
984  " clean up
985  call StopVimInTerminal(buf)
986  call delete('XTest_verbose')
987endfunc
988
989func Test_setcmdpos()
990  func InsertTextAtPos(text, pos)
991    call assert_equal(0, setcmdpos(a:pos))
992    return a:text
993  endfunc
994
995  " setcmdpos() with position in the middle of the command line.
996  call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
997  call assert_equal('"1ab2', @:)
998
999  call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1000  call assert_equal('"1b2a', @:)
1001
1002  " setcmdpos() with position beyond the end of the command line.
1003  call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1004  call assert_equal('"12ab', @:)
1005
1006  " setcmdpos() returns 1 when not editing the command line.
1007  call assert_equal(1, 3->setcmdpos())
1008endfunc
1009
1010func Test_cmdline_overstrike()
1011  let encodings = ['latin1', 'utf8']
1012  let encoding_save = &encoding
1013
1014  for e in encodings
1015    exe 'set encoding=' . e
1016
1017    " Test overstrike in the middle of the command line.
1018    call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
1019    call assert_equal('"0ab1cd4', @:, e)
1020
1021    " Test overstrike going beyond end of command line.
1022    call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
1023    call assert_equal('"0ab1cdefgh', @:, e)
1024
1025    " Test toggling insert/overstrike a few times.
1026    call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
1027    call assert_equal('"ab0cd3ef4', @:, e)
1028  endfor
1029
1030  " Test overstrike with multi-byte characters.
1031  call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
1032  call assert_equal('"テabキcdエディタ', @:, e)
1033
1034  let &encoding = encoding_save
1035endfunc
1036
1037func Test_cmdwin_bug()
1038  let winid = win_getid()
1039  sp
1040  try
1041    call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1042  catch /^Vim\%((\a\+)\)\=:E11/
1043  endtry
1044  bw!
1045endfunc
1046
1047func Test_cmdwin_restore()
1048  CheckScreendump
1049
1050  let lines =<< trim [SCRIPT]
1051    call setline(1, range(30))
1052    2split
1053  [SCRIPT]
1054  call writefile(lines, 'XTest_restore')
1055
1056  let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
1057  call TermWait(buf, 50)
1058  call term_sendkeys(buf, "q:")
1059  call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1060
1061  " normal restore
1062  call term_sendkeys(buf, ":q\<CR>")
1063  call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1064
1065  " restore after setting 'lines' with one window
1066  call term_sendkeys(buf, ":close\<CR>")
1067  call term_sendkeys(buf, "q:")
1068  call term_sendkeys(buf, ":set lines=18\<CR>")
1069  call term_sendkeys(buf, ":q\<CR>")
1070  call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1071
1072  " clean up
1073  call StopVimInTerminal(buf)
1074  call delete('XTest_restore')
1075endfunc
1076
1077func Test_buffers_lastused()
1078  " check that buffers are sorted by time when wildmode has lastused
1079  call test_settime(1550020000)	  " middle
1080  edit bufa
1081  enew
1082  call test_settime(1550030000)	  " newest
1083  edit bufb
1084  enew
1085  call test_settime(1550010000)	  " oldest
1086  edit bufc
1087  enew
1088  call test_settime(0)
1089  enew
1090
1091  call assert_equal(['bufa', 'bufb', 'bufc'],
1092	\ getcompletion('', 'buffer'))
1093
1094  let save_wildmode = &wildmode
1095  set wildmode=full:lastused
1096
1097  let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1098  call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1099  call assert_equal('b bufb', X)
1100  call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1101  call assert_equal('b bufa', X)
1102  call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1103  call assert_equal('b bufc', X)
1104  enew
1105
1106  edit other
1107  call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1108  call assert_equal('b bufb', X)
1109  call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1110  call assert_equal('b bufa', X)
1111  call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1112  call assert_equal('b bufc', X)
1113  enew
1114
1115  let &wildmode = save_wildmode
1116
1117  bwipeout bufa
1118  bwipeout bufb
1119  bwipeout bufc
1120endfunc
1121
1122func Test_cmdwin_feedkeys()
1123  " This should not generate E488
1124  call feedkeys("q:\<CR>", 'x')
1125  " Using feedkeys with q: only should automatically close the cmd window
1126  call feedkeys('q:', 'xt')
1127  call assert_equal(1, winnr('$'))
1128  call assert_equal('', getcmdwintype())
1129endfunc
1130
1131" Tests for the issues fixed in 7.4.441.
1132" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1133func Test_cmdwin_cedit()
1134  exe "set cedit=\<C-c>"
1135  normal! :
1136  call assert_equal(1, winnr('$'))
1137
1138  let g:cmd_wintype = ''
1139  func CmdWinType()
1140      let g:cmd_wintype = getcmdwintype()
1141      let g:wintype = win_gettype()
1142      return ''
1143  endfunc
1144
1145  call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1146  echo input('')
1147  call assert_equal('@', g:cmd_wintype)
1148  call assert_equal('command', g:wintype)
1149
1150  set cedit&vim
1151  delfunc CmdWinType
1152endfunc
1153
1154" Test for CmdwinEnter autocmd
1155func Test_cmdwin_autocmd()
1156  augroup CmdWin
1157    au!
1158    autocmd CmdwinEnter * startinsert
1159  augroup END
1160
1161  call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1162  call assert_equal('xyz', @:)
1163
1164  augroup CmdWin
1165    au!
1166  augroup END
1167  augroup! CmdWin
1168endfunc
1169
1170func Test_cmdlineclear_tabenter()
1171  CheckScreendump
1172
1173  let lines =<< trim [SCRIPT]
1174    call setline(1, range(30))
1175  [SCRIPT]
1176
1177  call writefile(lines, 'XtestCmdlineClearTabenter')
1178  let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
1179  call TermWait(buf, 25)
1180  " in one tab make the command line higher with CTRL-W -
1181  call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1182  call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1183
1184  call StopVimInTerminal(buf)
1185  call delete('XtestCmdlineClearTabenter')
1186endfunc
1187
1188" Test for failure in expanding special keywords in cmdline
1189func Test_cmdline_expand_special()
1190  %bwipe!
1191  call assert_fails('e #', 'E499:')
1192  call assert_fails('e <afile>', 'E495:')
1193  call assert_fails('e <abuf>', 'E496:')
1194  call assert_fails('e <amatch>', 'E497:')
1195endfunc
1196
1197func Test_cmdwin_jump_to_win()
1198  call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1199  new
1200  set modified
1201  call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
1202  close!
1203  call feedkeys("q/:close\<CR>", "xt")
1204  call assert_equal(1, winnr('$'))
1205  call feedkeys("q/:exit\<CR>", "xt")
1206  call assert_equal(1, winnr('$'))
1207
1208  " opening command window twice should fail
1209  call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1210  call assert_equal(1, winnr('$'))
1211endfunc
1212
1213func Test_cmdwin_interrupted()
1214  CheckScreendump
1215
1216  " aborting the :smile output caused the cmdline window to use the current
1217  " buffer.
1218  let lines =<< trim [SCRIPT]
1219    au WinNew * smile
1220  [SCRIPT]
1221  call writefile(lines, 'XTest_cmdwin')
1222
1223  let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
1224  " open cmdwin
1225  call term_sendkeys(buf, "q:")
1226  call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
1227  " quit more prompt for :smile command
1228  call term_sendkeys(buf, "q")
1229  call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
1230  " execute a simple command
1231  call term_sendkeys(buf, "aecho 'done'\<CR>")
1232  call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1233
1234  " clean up
1235  call StopVimInTerminal(buf)
1236  call delete('XTest_cmdwin')
1237endfunc
1238
1239" Test for backtick expression in the command line
1240func Test_cmd_backtick()
1241  %argd
1242  argadd `=['a', 'b', 'c']`
1243  call assert_equal(['a', 'b', 'c'], argv())
1244  %argd
1245endfunc
1246
1247" Test for the :! command
1248func Test_cmd_bang()
1249  CheckUnix
1250
1251  let lines =<< trim [SCRIPT]
1252    " Test for no previous command
1253    call assert_fails('!!', 'E34:')
1254    set nomore
1255    " Test for cmdline expansion with :!
1256    call setline(1, 'foo!')
1257    silent !echo <cWORD> > Xfile.out
1258    call assert_equal(['foo!'], readfile('Xfile.out'))
1259    " Test for using previous command
1260    silent !echo \! !
1261    call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1262    call writefile(v:errors, 'Xresult')
1263    call delete('Xfile.out')
1264    qall!
1265  [SCRIPT]
1266  call writefile(lines, 'Xscript')
1267  if RunVim([], [], '--clean -S Xscript')
1268    call assert_equal([], readfile('Xresult'))
1269  endif
1270  call delete('Xscript')
1271  call delete('Xresult')
1272endfunc
1273
1274" Test error: "E135: *Filter* Autocommands must not change current buffer"
1275func Test_cmd_bang_E135()
1276  new
1277  call setline(1, ['a', 'b', 'c', 'd'])
1278  augroup test_cmd_filter_E135
1279    au!
1280    autocmd FilterReadPost * help
1281  augroup END
1282  call assert_fails('2,3!echo "x"', 'E135:')
1283
1284  augroup test_cmd_filter_E135
1285    au!
1286  augroup END
1287  %bwipe!
1288endfunc
1289
1290" Test for using ~ for home directory in cmdline completion matches
1291func Test_cmdline_expand_home()
1292  call mkdir('Xdir')
1293  call writefile([], 'Xdir/Xfile1')
1294  call writefile([], 'Xdir/Xfile2')
1295  cd Xdir
1296  let save_HOME = $HOME
1297  let $HOME = getcwd()
1298  call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1299  call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1300  let $HOME = save_HOME
1301  cd ..
1302  call delete('Xdir', 'rf')
1303endfunc
1304
1305" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1306" or insert mode (when 'insertmode' is set)
1307func Test_cmdline_ctrl_g()
1308  new
1309  call setline(1, 'abc')
1310  call cursor(1, 3)
1311  " If command line is entered from insert mode, using C-\ C-G should back to
1312  " insert mode
1313  call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1314  call assert_equal('abxyc', getline(1))
1315  call assert_equal(4, col('.'))
1316
1317  " If command line is entered in 'insertmode', using C-\ C-G should back to
1318  " 'insertmode'
1319  call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1320  call assert_equal('ab12xyc', getline(1))
1321  close!
1322endfunc
1323
1324" Test for 'wildmode'
1325func Test_wildmode()
1326  func T(a, c, p)
1327    return "oneA\noneB\noneC"
1328  endfunc
1329  command -nargs=1 -complete=custom,T MyCmd
1330
1331  func SaveScreenLine()
1332    let g:Sline = Screenline(&lines - 1)
1333    return ''
1334  endfunc
1335  cnoremap <expr> <F2> SaveScreenLine()
1336
1337  set nowildmenu
1338  set wildmode=full,list
1339  let g:Sline = ''
1340  call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
1341  call assert_equal('oneA  oneB  oneC', g:Sline)
1342  call assert_equal('"MyCmd oneA', @:)
1343
1344  set wildmode=longest,full
1345  call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1346  call assert_equal('"MyCmd one', @:)
1347  call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1348  call assert_equal('"MyCmd oneC', @:)
1349
1350  set wildmode=longest
1351  call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1352  call assert_equal('"MyCmd one', @:)
1353
1354  set wildmode=list:longest
1355  let g:Sline = ''
1356  call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
1357  call assert_equal('oneA  oneB  oneC', g:Sline)
1358  call assert_equal('"MyCmd one', @:)
1359
1360  set wildmode=""
1361  call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1362  call assert_equal('"MyCmd oneA', @:)
1363
1364  " Test for wildmode=longest with 'fileignorecase' set
1365  set wildmode=longest
1366  set fileignorecase
1367  argadd AAA AAAA AAAAA
1368  call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1369  call assert_equal('"buffer AAA', @:)
1370  set fileignorecase&
1371
1372  " Test for listing files with wildmode=list
1373  set wildmode=list
1374  let g:Sline = ''
1375  call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
1376  call assert_equal('AAA    AAAA   AAAAA', g:Sline)
1377  call assert_equal('"b A', @:)
1378
1379  %argdelete
1380  delcommand MyCmd
1381  delfunc T
1382  delfunc SaveScreenLine
1383  cunmap <F2>
1384  set wildmode&
1385  %bwipe!
1386endfunc
1387
1388" Test for interrupting the command-line completion
1389func Test_interrupt_compl()
1390  func F(lead, cmdl, p)
1391    if a:lead =~ 'tw'
1392      call interrupt()
1393      return
1394    endif
1395    return "one\ntwo\nthree"
1396  endfunc
1397  command -nargs=1 -complete=custom,F Tcmd
1398
1399  set nowildmenu
1400  set wildmode=full
1401  let interrupted = 0
1402  try
1403    call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1404  catch /^Vim:Interrupt$/
1405    let interrupted = 1
1406  endtry
1407  call assert_equal(1, interrupted)
1408
1409  delcommand Tcmd
1410  delfunc F
1411  set wildmode&
1412endfunc
1413
1414" Test for moving the cursor on the : command line
1415func Test_cmdline_edit()
1416  let str = ":one two\<C-U>"
1417  let str ..= "one two\<C-W>\<C-W>"
1418  let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
1419  let str ..= "\<Left>five\<Right>"
1420  let str ..= "\<Home>two "
1421  let str ..= "\<C-Left>one "
1422  let str ..= "\<C-Right> three"
1423  let str ..= "\<End>\<S-Left>four "
1424  let str ..= "\<S-Right> six"
1425  let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1426  call feedkeys(str, 'xt')
1427  call assert_equal("\"one two three four five six seven", @:)
1428endfunc
1429
1430" Test for moving the cursor on the / command line in 'rightleft' mode
1431func Test_cmdline_edit_rightleft()
1432  CheckFeature rightleft
1433  set rightleft
1434  set rightleftcmd=search
1435  let str = "/one two\<C-U>"
1436  let str ..= "one two\<C-W>\<C-W>"
1437  let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
1438  let str ..= "\<Right>five\<Left>"
1439  let str ..= "\<Home>two "
1440  let str ..= "\<C-Right>one "
1441  let str ..= "\<C-Left> three"
1442  let str ..= "\<End>\<S-Right>four "
1443  let str ..= "\<S-Left> six"
1444  let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1445  call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1446  call assert_equal("\"one two three four five six seven", @/)
1447  set rightleftcmd&
1448  set rightleft&
1449endfunc
1450
1451" Test for using <C-\>e in the command line to evaluate an expression
1452func Test_cmdline_expr()
1453  " Evaluate an expression from the beginning of a command line
1454  call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1455  call assert_equal('"hello', @:)
1456
1457  " Use an invalid expression for <C-\>e
1458  call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1459
1460  " Insert literal <CTRL-\> in the command line
1461  call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1462  call assert_equal("\"e \<C-\>\<C-Y>", @:)
1463endfunc
1464
1465" Test for 'imcmdline' and 'imsearch'
1466" This test doesn't actually test the input method functionality.
1467func Test_cmdline_inputmethod()
1468  new
1469  call setline(1, ['', 'abc', ''])
1470  set imcmdline
1471
1472  call feedkeys(":\"abc\<CR>", 'xt')
1473  call assert_equal("\"abc", @:)
1474  call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1475  call assert_equal("\"abc", @:)
1476  call feedkeys("/abc\<CR>", 'xt')
1477  call assert_equal([2, 1], [line('.'), col('.')])
1478  call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1479  call assert_equal([2, 1], [line('.'), col('.')])
1480
1481  set imsearch=2
1482  call cursor(1, 1)
1483  call feedkeys("/abc\<CR>", 'xt')
1484  call assert_equal([2, 1], [line('.'), col('.')])
1485  call cursor(1, 1)
1486  call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1487  call assert_equal([2, 1], [line('.'), col('.')])
1488  set imdisable
1489  call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1490  call assert_equal([2, 1], [line('.'), col('.')])
1491  set imdisable&
1492  set imsearch&
1493
1494  set imcmdline&
1495  %bwipe!
1496endfunc
1497
1498" Test for recursively getting multiple command line inputs
1499func Test_cmdwin_multi_input()
1500  call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1501  call assert_equal('"cyan', @:)
1502endfunc
1503
1504" Test for using CTRL-_ in the command line with 'allowrevins'
1505func Test_cmdline_revins()
1506  CheckNotMSWindows
1507  CheckFeature rightleft
1508  call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1509  call assert_equal("\"abc\<c-_>", @:)
1510  set allowrevins
1511  call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1512  call assert_equal('"abcñèæ', @:)
1513  set allowrevins&
1514endfunc
1515
1516" Test for typing UTF-8 composing characters in the command line
1517func Test_cmdline_composing_chars()
1518  call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1519  call assert_equal('"ゔ', @:)
1520endfunc
1521
1522" Test for normal mode commands not supported in the cmd window
1523func Test_cmdwin_blocked_commands()
1524  call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1525  call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1526  call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1527  call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1528  call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1529  call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
1530  call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1531  call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1532  call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1533  call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1534  call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1535  call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1536  call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1537  call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1538  call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1539  call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1540  call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1541  call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1542  call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1543  call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1544  call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1545  call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1546  call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1547  call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1548  call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1549  call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1550  call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
1551endfunc
1552
1553" Close the Cmd-line window in insert mode using CTRL-C
1554func Test_cmdwin_insert_mode_close()
1555  %bw!
1556  let s = ''
1557  exe "normal q:a\<C-C>let s='Hello'\<CR>"
1558  call assert_equal('Hello', s)
1559  call assert_equal(1, winnr('$'))
1560endfunc
1561
1562" test that ";" works to find a match at the start of the first line
1563func Test_zero_line_search()
1564  new
1565  call setline(1, ["1, pattern", "2, ", "3, pattern"])
1566  call cursor(1,1)
1567  0;/pattern/d
1568  call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1569  q!
1570endfunc
1571
1572func Test_read_shellcmd()
1573  CheckUnix
1574  if executable('ls')
1575    " There should be ls in the $PATH
1576    call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1577    call assert_match('^"r! .*\<ls\>', @:)
1578  endif
1579
1580  if executable('rm')
1581    call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1582    call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1583    call assert_match('^"r!.*\<rm\>', @:)
1584
1585    call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1586    call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1587    call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
1588  endif
1589endfunc
1590
1591" vim: shiftwidth=2 sts=2 expandtab
1592