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