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