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