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