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