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