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