blob: cb16de12469c903b698437c7dc3fb0fbb6e3ff8e [file] [log] [blame]
Bram Moolenaarae3150e2016-06-11 23:22:36 +02001" Tests for editing the command line.
2
Bram Moolenaar4facea32019-10-12 20:17:40 +02003source check.vim
4source screendump.vim
Bram Moolenaar24ebd832020-03-16 21:25:24 +01005source view_util.vim
Bram Moolenaarc82dd862020-06-07 17:30:33 +02006source shared.vim
Bram Moolenaar4facea32019-10-12 20:17:40 +02007
Bram Moolenaarae3150e2016-06-11 23:22:36 +02008func 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'))
Bram Moolenaar578fe942020-02-27 21:32:51 +010019
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>", @:)
Bram Moolenaarae3150e2016-06-11 23:22:36 +020024endfunc
25
26func Test_complete_wildmenu()
Bram Moolenaar37db6422019-03-28 21:26:23 +010027 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')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020032 set wildmenu
Bram Moolenaar37db6422019-03-28 21:26:23 +010033
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')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020038 call assert_equal('testfile2', getline(1))
39
Bram Moolenaar37db6422019-03-28 21:26:23 +010040 " <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
Bram Moolenaar3e112ac2020-12-28 13:41:53 +010061 " this fails in some Unix GUIs, not sure why
62 if !has('unix') || !has('gui_running')
63 " <C-J>/<C-K> mappings to go up/down directories when 'wildcharm' is
64 " different than 'wildchar'.
65 set wildcharm=<C-Z>
66 cnoremap <C-J> <Down><C-Z>
67 cnoremap <C-K> <Up><C-Z>
68 call feedkeys(":e Xdir1/\<Tab>\<C-J>\<CR>", 'tx')
69 call assert_equal('testfile3', getline(1))
70 call feedkeys(":e Xdir1/\<Tab>\<C-J>\<C-K>\<CR>", 'tx')
71 call assert_equal('testfile1', getline(1))
72 set wildcharm=0
73 cunmap <C-J>
74 cunmap <C-K>
75 endif
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +010076
Bram Moolenaar578fe942020-02-27 21:32:51 +010077 " Test for canceling the wild menu by adding a character
78 redrawstatus
79 call feedkeys(":e Xdir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
80 call assert_equal('"e Xdir1/Xdir2/x', @:)
81
Bram Moolenaar8d588cc2020-02-25 21:47:45 +010082 " Completion using a relative path
83 cd Xdir1/Xdir2
84 call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
85 call assert_equal('"e Xtestfile3 Xtestfile4', @:)
86 cd -
87
Bram Moolenaar0e05de42020-03-25 22:23:46 +010088 cnoremap <expr> <F2> wildmenumode()
89 call feedkeys(":cd Xdir\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
90 call assert_equal('"cd Xdir1/1', @:)
91 cunmap <F2>
92
Bram Moolenaar37db6422019-03-28 21:26:23 +010093 " cleanup
94 %bwipe
95 call delete('Xdir1/Xdir2/Xtestfile4')
96 call delete('Xdir1/Xdir2/Xtestfile3')
97 call delete('Xdir1/Xtestfile2')
98 call delete('Xdir1/Xtestfile1')
99 call delete('Xdir1/Xdir2', 'd')
100 call delete('Xdir1', 'd')
Bram Moolenaarae3150e2016-06-11 23:22:36 +0200101 set nowildmenu
102endfunc
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100103
Bram Moolenaara60053b2020-09-03 16:50:13 +0200104func Test_wildmenu_screendump()
105 CheckScreendump
106
107 let lines =<< trim [SCRIPT]
108 set wildmenu hlsearch
109 [SCRIPT]
110 call writefile(lines, 'XTest_wildmenu')
111
112 let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
113 call term_sendkeys(buf, ":vim\<Tab>")
114 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
115
116 call term_sendkeys(buf, "\<Tab>")
117 call VerifyScreenDump(buf, 'Test_wildmenu_2', {})
118
119 call term_sendkeys(buf, "\<Tab>")
120 call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
121
122 call term_sendkeys(buf, "\<Tab>")
123 call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
124 call term_sendkeys(buf, "\<Esc>")
125
126 " clean up
127 call StopVimInTerminal(buf)
128 call delete('XTest_wildmenu')
129endfunc
130
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100131func Test_map_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200132 CheckFeature cmdline_compl
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100133 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
134 call assert_equal('"map <unique> <silent>', getreg(':'))
135 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
136 call assert_equal('"map <script> <unique>', getreg(':'))
137 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
138 call assert_equal('"map <expr> <script>', getreg(':'))
139 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
140 call assert_equal('"map <buffer> <expr>', getreg(':'))
141 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
142 call assert_equal('"map <nowait> <buffer>', getreg(':'))
143 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
144 call assert_equal('"map <special> <nowait>', getreg(':'))
145 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
146 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200147
Bram Moolenaar1776a282019-05-03 16:05:41 +0200148 map <Middle>x middle
149
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200150 map ,f commaf
151 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200152 map <Left> left
153 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200154 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
155 call assert_equal('"map ,f', getreg(':'))
156 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
157 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200158 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
159 call assert_equal('"map <Left>', getreg(':'))
160 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200161 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200162 unmap ,f
163 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200164 unmap <Left>
165 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200166
167 set cpo-=< cpo-=B cpo-=k
168 map <Left> left
169 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
170 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200171 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200172 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200173 unmap <Left>
174
175 set cpo+=<
176 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200177 exe "set t_k6=\<Esc>[17~"
178 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200179 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
180 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200181 if !has('gui_running')
182 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
183 call assert_equal("\"map <F6>x", getreg(':'))
184 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200185 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200186 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200187 set cpo-=<
188
189 set cpo+=B
190 map <Left> left
191 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
192 call assert_equal('"map <Left>', getreg(':'))
193 unmap <Left>
194 set cpo-=B
195
196 set cpo+=k
197 map <Left> left
198 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
199 call assert_equal('"map <Left>', getreg(':'))
200 unmap <Left>
201 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200202
Bram Moolenaar531be472020-09-23 22:38:05 +0200203 call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
204
Bram Moolenaar1776a282019-05-03 16:05:41 +0200205 unmap <Middle>x
206 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100207endfunc
208
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100209func Test_match_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200210 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100211 hi Aardig ctermfg=green
212 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
213 call assert_equal('"match Aardig', getreg(':'))
214 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
215 call assert_equal('"match none', getreg(':'))
216endfunc
217
218func Test_highlight_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200219 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100220 hi Aardig ctermfg=green
221 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
222 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200223 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
224 call assert_equal('"hi default Aardig', getreg(':'))
225 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
226 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100227 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
228 call assert_equal('"hi link', getreg(':'))
229 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
230 call assert_equal('"hi default', getreg(':'))
231 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
232 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200233 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
234 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
235 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
236 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200237
238 " A cleared group does not show up in completions.
239 hi Anders ctermfg=green
240 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
241 hi clear Aardig
242 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
243 hi clear Anders
244 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100245endfunc
246
Bram Moolenaar75e15672020-06-28 13:10:22 +0200247" Test for command-line expansion of "hi Ni " (easter egg)
248func Test_highlight_easter_egg()
249 call test_override('ui_delay', 1)
250 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
251 call assert_equal("\"hi Ni \<Tab>", @:)
252 call test_override('ALL', 0)
253endfunc
254
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200255func Test_getcompletion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200256 CheckFeature cmdline_compl
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200257 let groupcount = len(getcompletion('', 'event'))
258 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200259 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200260 call assert_true(matchcount > 0)
261 call assert_true(groupcount > matchcount)
262
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200263 if has('menu')
264 source $VIMRUNTIME/menu.vim
265 let matchcount = len(getcompletion('', 'menu'))
266 call assert_true(matchcount > 0)
267 call assert_equal(['File.'], getcompletion('File', 'menu'))
268 call assert_true(matchcount > 0)
269 let matchcount = len(getcompletion('File.', 'menu'))
270 call assert_true(matchcount > 0)
271 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200272
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200273 let l = getcompletion('v:n', 'var')
274 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200275 let l = getcompletion('v:notexists', 'var')
276 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200277
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200278 args a.c b.c
279 let l = getcompletion('', 'arglist')
280 call assert_equal(['a.c', 'b.c'], l)
281 %argdelete
282
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200283 let l = getcompletion('', 'augroup')
284 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200285 let l = getcompletion('blahblah', 'augroup')
286 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200287
288 let l = getcompletion('', 'behave')
289 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200290 let l = getcompletion('not', 'behave')
291 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200292
293 let l = getcompletion('', 'color')
294 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200295 let l = getcompletion('dirty', 'color')
296 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200297
298 let l = getcompletion('', 'command')
299 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200300 let l = getcompletion('awake', 'command')
301 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200302
303 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200304 call assert_true(index(l, 'samples/') >= 0)
305 let l = getcompletion('NoMatch', 'dir')
306 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200307
308 let l = getcompletion('exe', 'expression')
309 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200310 let l = getcompletion('kill', 'expression')
311 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200312
313 let l = getcompletion('tag', 'function')
314 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200315 let l = getcompletion('paint', 'function')
316 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200317
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200318 let Flambda = {-> 'hello'}
319 let l = getcompletion('', 'function')
320 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200321 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200322
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200323 let l = getcompletion('run', 'file')
324 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200325 let l = getcompletion('walk', 'file')
326 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200327 set wildignore=*.vim
328 let l = getcompletion('run', 'file', 1)
329 call assert_true(index(l, 'runtest.vim') < 0)
330 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200331
332 let l = getcompletion('ha', 'filetype')
333 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200334 let l = getcompletion('horse', 'filetype')
335 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200336
337 let l = getcompletion('z', 'syntax')
338 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200339 let l = getcompletion('emacs', 'syntax')
340 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200341
342 let l = getcompletion('jikes', 'compiler')
343 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200344 let l = getcompletion('break', 'compiler')
345 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200346
347 let l = getcompletion('last', 'help')
348 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200349 let l = getcompletion('giveup', 'help')
350 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200351
352 let l = getcompletion('time', 'option')
353 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200354 let l = getcompletion('space', 'option')
355 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200356
357 let l = getcompletion('er', 'highlight')
358 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200359 let l = getcompletion('dark', 'highlight')
360 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200361
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200362 let l = getcompletion('', 'messages')
363 call assert_true(index(l, 'clear') >= 0)
364 let l = getcompletion('not', 'messages')
365 call assert_equal([], l)
366
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200367 let l = getcompletion('', 'mapclear')
368 call assert_true(index(l, '<buffer>') >= 0)
369 let l = getcompletion('not', 'mapclear')
370 call assert_equal([], l)
371
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200372 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200373 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200374 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
375 let root = has('win32') ? 'C:\\' : '/'
376 let l = getcompletion(root, 'shellcmd')
377 let expected = map(filter(glob(root . '*', 0, 1),
378 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
379 call assert_equal(expected, l)
380
Bram Moolenaarb650b982016-08-05 20:35:13 +0200381 if has('cscope')
382 let l = getcompletion('', 'cscope')
383 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
384 call assert_equal(cmds, l)
385 " using cmdline completion must not change the result
386 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
387 let l = getcompletion('', 'cscope')
388 call assert_equal(cmds, l)
389 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
390 let l = getcompletion('find ', 'cscope')
391 call assert_equal(keys, l)
392 endif
393
Bram Moolenaar7522f692016-08-06 14:12:50 +0200394 if has('signs')
395 sign define Testing linehl=Comment
396 let l = getcompletion('', 'sign')
397 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
398 call assert_equal(cmds, l)
399 " using cmdline completion must not change the result
400 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
401 let l = getcompletion('', 'sign')
402 call assert_equal(cmds, l)
403 let l = getcompletion('list ', 'sign')
404 call assert_equal(['Testing'], l)
405 endif
406
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200407 " Command line completion tests
408 let l = getcompletion('cd ', 'cmdline')
409 call assert_true(index(l, 'samples/') >= 0)
410 let l = getcompletion('cd NoMatch', 'cmdline')
411 call assert_equal([], l)
412 let l = getcompletion('let v:n', 'cmdline')
413 call assert_true(index(l, 'v:null') >= 0)
414 let l = getcompletion('let v:notexists', 'cmdline')
415 call assert_equal([], l)
416 let l = getcompletion('call tag', 'cmdline')
417 call assert_true(index(l, 'taglist(') >= 0)
418 let l = getcompletion('call paint', 'cmdline')
419 call assert_equal([], l)
420
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200421 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200422 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200423 if has('cmdline_hist')
424 call add(names, 'history')
425 endif
426 if has('gettext')
427 call add(names, 'locale')
428 endif
429 if has('profile')
430 call add(names, 'syntime')
431 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200432
433 set tags=Xtags
434 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
435
436 for name in names
437 let matchcount = len(getcompletion('', name))
438 call assert_true(matchcount >= 0, 'No matches for ' . name)
439 endfor
440
441 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200442 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200443
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200444 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200445 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200446 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200447endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200448
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200449func Test_shellcmd_completion()
450 let save_path = $PATH
451
452 call mkdir('Xpathdir/Xpathsubdir', 'p')
453 call writefile([''], 'Xpathdir/Xfile.exe')
454 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
455
456 " Set PATH to example directory without trailing slash.
457 let $PATH = getcwd() . '/Xpathdir'
458
459 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
460 " dirs in the PATH, even though they won't be executed. We check that only
461 " subdirs of the PWD and executables from the PATH are included in the
462 " suggestions.
463 let actual = getcompletion('X', 'shellcmd')
464 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
465 call insert(expected, 'Xfile.exe')
466 call assert_equal(expected, actual)
467
468 call delete('Xpathdir', 'rf')
469 let $PATH = save_path
470endfunc
471
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200472func Test_expand_star_star()
473 call mkdir('a/b', 'p')
474 call writefile(['asdfasdf'], 'a/b/fileXname')
475 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
476 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200477 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200478 call delete('a', 'rf')
479endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100480
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100481func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100482 let @a = "def"
483 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
484 call assert_equal('"abc def ghi', @:)
485
486 new
487 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
488
489 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
490 call assert_equal('"aaa asdf bbb', @:)
491
492 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
493 call assert_equal('"aaa /tmp/some bbb', @:)
494
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200495 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
496 call assert_equal('"aaa '.getline(1).' bbb', @:)
497
Bram Moolenaar21efc362016-12-03 14:05:49 +0100498 set incsearch
499 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
500 call assert_equal('"aaa verylongword bbb', @:)
501
502 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
503 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100504
505 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
506 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100507 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200508
509 " Error while typing a command used to cause that it was not executed
510 " in the end.
511 new
512 try
513 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
514 catch /^Vim\%((\a\+)\)\=:E32/
515 " ignore error E32
516 endtry
517 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100518
Bram Moolenaar578fe942020-02-27 21:32:51 +0100519 " Try to paste an invalid register using <C-R>
520 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
521 call assert_equal('"onetwo', @:)
522
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100523 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100524 let @a = "xy\<C-H>z"
525 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
526 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100527 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
528 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100529 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
530 call assert_equal("\"xy\<C-H>z", @:)
531
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100532 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
533 let @a = "xy\<C-V>z"
534 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
535 call assert_equal('"xyz', @:)
536 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
537 call assert_equal("\"xy\<C-V>z", @:)
538
Bram Moolenaar578fe942020-02-27 21:32:51 +0100539 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
540
Bram Moolenaar72532d32018-04-07 19:09:09 +0200541 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100542endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100543
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100544func Test_cmdline_remove_char()
545 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100546
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100547 for e in ['utf8', 'latin1']
548 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100549
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100550 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
551 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100552
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100553 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
554 call assert_equal('"abcdef', @:)
555
556 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
557 call assert_equal('"abc ghi', @:, e)
558
559 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
560 call assert_equal('"def', @:, e)
561 endfor
562
563 let &encoding = encoding_save
564endfunc
565
566func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200567 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100568
569 set keymap=esperanto
570 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
571 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
572 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100573endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100574
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100575func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100576 new
577 2;'(
578 2;')
579 quit
580endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100581
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100582func Test_illegal_address2()
583 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
584 new
585 source Xtest.vim
586 " Trigger calling validate_cursor()
587 diffsp Xtest.vim
588 quit!
589 bwipe!
590 call delete('Xtest.vim')
591endfunc
592
Bram Moolenaarba47b512017-01-24 21:18:19 +0100593func Test_cmdline_complete_wildoptions()
594 help
595 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
596 let a = join(sort(split(@:)),' ')
597 set wildoptions=tagfile
598 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
599 let b = join(sort(split(@:)),' ')
600 call assert_equal(a, b)
601 bw!
602endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100603
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200604func Test_cmdline_complete_user_cmd()
605 command! -complete=color -nargs=1 Foo :
606 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
607 call assert_equal('"Foo blue', @:)
608 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
609 call assert_equal('"Foo blue', @:)
610 delcommand Foo
611endfunc
612
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100613func s:ScriptLocalFunction()
614 echo 'yes'
615endfunc
616
617func Test_cmdline_complete_user_func()
618 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
619 call assert_match('"func Test_cmdline_complete_user', @:)
620 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
621 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +0100622
623 " g: prefix also works
624 call feedkeys(":echo g:Test_cmdline_complete_user_f\<Tab>\<Home>\"\<cr>", 'tx')
625 call assert_match('"echo g:Test_cmdline_complete_user_func', @:)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100626endfunc
627
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200628func Test_cmdline_complete_user_names()
629 if has('unix') && executable('whoami')
630 let whoami = systemlist('whoami')[0]
631 let first_letter = whoami[0]
632 if len(first_letter) > 0
633 " Trying completion of :e ~x where x is the first letter of
634 " the user name should complete to at least the user name.
635 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
636 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
637 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200638 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200639 " Just in case: check that the system has an Administrator account.
640 let names = system('net user')
641 if names =~ 'Administrator'
642 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100643 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200644 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100645 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200646 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200647 else
648 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200649 endif
650endfunc
651
Bram Moolenaar297610b2019-12-27 17:20:55 +0100652func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200653 CheckExecutable whoami
654 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
655 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100656endfunc
657
Bram Moolenaar8b633132020-03-20 18:20:51 +0100658func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200659 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
660 call assert_equal(lang, v:lc_time)
661
662 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
663 call assert_equal(lang, v:ctype)
664
665 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
666 call assert_equal(lang, v:collate)
667
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200668 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200669 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200670
671 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200672 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200673
Bram Moolenaarec680282020-06-12 19:35:32 +0200674 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200675
Bram Moolenaarec680282020-06-12 19:35:32 +0200676 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
677 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200678
Bram Moolenaarec680282020-06-12 19:35:32 +0200679 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
680 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200681
Bram Moolenaarec680282020-06-12 19:35:32 +0200682 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
683 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200684
685 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
686 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200687endfunc
688
Bram Moolenaar297610b2019-12-27 17:20:55 +0100689func Test_cmdline_complete_env_variable()
690 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100691 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
692 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100693 unlet $X_VIM_TEST_COMPLETE_ENV
694endfunc
695
Bram Moolenaar4941b5e2020-12-24 17:15:53 +0100696func Test_cmdline_complete_expression()
697 let g:SomeVar = 'blah'
698 for cmd in ['exe', 'echo', 'echon', 'echomsg']
699 call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
700 call assert_match('"' .. cmd .. ' SomeVar', @:)
701 call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
702 call assert_match('"' .. cmd .. ' foo SomeVar', @:)
703 endfor
704 unlet g:SomeVar
705endfunc
706
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100707" Test for various command-line completion
708func Test_cmdline_complete_various()
709 " completion for a command starting with a comment
710 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
711 call assert_equal("\" :|\"\<C-A>", @:)
712
713 " completion for a range followed by a comment
714 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
715 call assert_equal("\"1,2\"\<C-A>", @:)
716
717 " completion for :k command
718 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
719 call assert_equal("\"ka\<C-A>", @:)
720
721 " completion for short version of the :s command
722 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
723 call assert_equal("\"sI \<C-A>", @:)
724
725 " completion for :write command
726 call mkdir('Xdir')
727 call writefile(['one'], 'Xdir/Xfile1')
728 let save_cwd = getcwd()
729 cd Xdir
730 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
731 call assert_equal("\"w >> Xfile1", @:)
732 call chdir(save_cwd)
733 call delete('Xdir', 'rf')
734
735 " completion for :w ! and :r ! commands
736 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
737 call assert_equal("\"w !invalid_xyz_cmd", @:)
738 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
739 call assert_equal("\"r !invalid_xyz_cmd", @:)
740
741 " completion for :>> and :<< commands
742 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
743 call assert_equal("\">>>\<C-A>", @:)
744 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
745 call assert_equal("\"<<<\<C-A>", @:)
746
747 " completion for command with +cmd argument
748 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
749 call assert_equal("\"buffer +/pat Xabc", @:)
750 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
751 call assert_equal("\"buffer +/pat\<C-A>", @:)
752
753 " completion for a command with a trailing comment
754 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
755 call assert_equal("\"ls \" comment\<C-A>", @:)
756
757 " completion for a command with a trailing command
758 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
759 call assert_equal("\"ls | ls", @:)
760
761 " completion for a command with an CTRL-V escaped argument
762 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
763 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
764
765 " completion for a command that doesn't take additional arguments
766 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
767 call assert_equal("\"all abc\<C-A>", @:)
768
769 " completion for a command with a command modifier
770 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
771 call assert_equal("\"topleft new", @:)
772
773 " completion for the :match command
774 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
775 call assert_equal("\"match Search /pat/\<C-A>", @:)
776
777 " completion for the :s command
778 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
779 call assert_equal("\"s/from/to/g\<C-A>", @:)
780
781 " completion for the :dlist command
782 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
783 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
784
785 " completion for the :doautocmd command
786 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
787 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
788
Bram Moolenaarafe8cf62020-10-05 20:07:18 +0200789 " completion of autocmd group after comma
790 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
791 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
792
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100793 " completion for the :augroup command
794 augroup XTest
795 augroup END
796 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
797 call assert_equal("\"augroup XTest", @:)
798 augroup! XTest
799
800 " completion for the :unlet command
801 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
802 call assert_equal("\"unlet one two", @:)
803
804 " completion for the :bdelete command
805 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
806 call assert_equal("\"bdel a b c", @:)
807
808 " completion for the :mapclear command
809 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
810 call assert_equal("\"mapclear <buffer>", @:)
811
812 " completion for user defined commands with menu names
813 menu Test.foo :ls<CR>
814 com -nargs=* -complete=menu MyCmd
815 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
816 call assert_equal('"MyCmd Test.', @:)
817 delcom MyCmd
818 unmenu Test
819
820 " completion for user defined commands with mappings
821 mapclear
822 map <F3> :ls<CR>
823 com -nargs=* -complete=mapping MyCmd
824 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
825 call assert_equal('"MyCmd <F3>', @:)
826 mapclear
827 delcom MyCmd
828
829 " completion for :set path= with multiple backslashes
830 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
831 call assert_equal('"set path=a\\\ b', @:)
832
833 " completion for :set dir= with a backslash
834 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
835 call assert_equal('"set dir=a\ b', @:)
836
837 " completion for the :py3 commands
838 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
839 call assert_equal('"py3 py3do py3file', @:)
840
841 " redir @" is not the start of a comment. So complete after that
842 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
843 call assert_equal('"redir @" | cwindow', @:)
844
845 " completion after a backtick
846 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
847 call assert_equal('"e `a1b2c', @:)
848
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100849 " completion for :language command with an invalid argument
850 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
851 call assert_equal("\"language dummy \t", @:)
852
853 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100854 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
855 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100856
857 " completion with ambiguous user defined commands
858 com TCmd1 echo 'TCmd1'
859 com TCmd2 echo 'TCmd2'
860 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
861 call assert_equal('"TCmd ', @:)
862 delcom TCmd1
863 delcom TCmd2
864
865 " completion after a range followed by a pipe (|) character
866 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
867 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +0200868
869 " use <Esc> as the 'wildchar' for completion
870 set wildchar=<Esc>
871 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
872 call assert_equal('"g/a\xb/clearjumps', @:)
873 " pressing <esc> twice should cancel the command
874 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
875 call assert_equal('"g/a\xb/clearjumps', @:)
876 set wildchar&
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100877endfunc
878
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200879func Test_cmdline_write_alternatefile()
880 new
881 call setline('.', ['one', 'two'])
882 f foo.txt
883 new
884 f #-A
885 call assert_equal('foo.txt-A', expand('%'))
886 f #<-B.txt
887 call assert_equal('foo-B.txt', expand('%'))
888 f %<
889 call assert_equal('foo-B', expand('%'))
890 new
Bram Moolenaare2e40752020-09-04 21:18:46 +0200891 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200892 bw!
893 f foo-B.txt
894 f %<-A
895 call assert_equal('foo-B-A', expand('%'))
896 bw!
897 bw!
898endfunc
899
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100900" using a leading backslash here
901set cpo+=C
902
903func Test_cmdline_search_range()
904 new
905 call setline(1, ['a', 'b', 'c', 'd'])
906 /d
907 1,\/s/b/B/
908 call assert_equal('B', getline(2))
909
910 /a
911 $
912 \?,4s/c/C/
913 call assert_equal('C', getline(3))
914
915 call setline(1, ['a', 'b', 'c', 'd'])
916 %s/c/c/
917 1,\&s/b/B/
918 call assert_equal('B', getline(2))
919
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100920 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200921 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100922
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100923 bwipe!
924endfunc
925
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100926" Test for the tick mark (') in an excmd range
927func Test_tick_mark_in_range()
928 " If only the tick is passed as a range and no command is specified, there
929 " should not be an error
930 call feedkeys(":'\<CR>", 'xt')
931 call assert_equal("'", getreg(':'))
932 call assert_fails("',print", 'E78:')
933endfunc
934
935" Test for using a line number followed by a search pattern as range
936func Test_lnum_and_pattern_as_range()
937 new
938 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
939 let @" = ''
940 2/foo/yank
941 call assert_equal("foo 3\n", @")
942 call assert_equal(1, line('.'))
943 close!
944endfunc
945
Bram Moolenaar65189a12017-02-06 22:22:17 +0100946" Tests for getcmdline(), getcmdpos() and getcmdtype()
947func Check_cmdline(cmdtype)
948 call assert_equal('MyCmd a', getcmdline())
949 call assert_equal(8, getcmdpos())
950 call assert_equal(a:cmdtype, getcmdtype())
951 return ''
952endfunc
953
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200954set cpo&
955
Bram Moolenaar65189a12017-02-06 22:22:17 +0100956func Test_getcmdtype()
957 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
958
959 let cmdtype = ''
960 debuggreedy
961 call feedkeys(":debug echo 'test'\<CR>", "t")
962 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
963 call feedkeys("cont\<CR>", "xt")
964 0debuggreedy
965 call assert_equal('>', cmdtype)
966
967 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
968 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
969
970 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100971 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100972
973 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
974
975 cnoremap <expr> <F6> Check_cmdline('=')
976 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
977 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100978
979 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100980endfunc
981
Bram Moolenaar81612b72018-06-23 14:55:03 +0200982func Test_getcmdwintype()
983 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
984 call assert_equal('/', a)
985
986 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
987 call assert_equal('?', a)
988
989 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
990 call assert_equal(':', a)
991
992 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
993 call assert_equal(':', a)
994
995 call assert_equal('', getcmdwintype())
996endfunc
997
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200998func Test_getcmdwin_autocmd()
999 let s:seq = []
1000 augroup CmdWin
1001 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
1002 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
1003 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
1004 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
1005 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
1006 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
1007
1008 let org_winid = win_getid()
1009 let org_bufnr = bufnr()
1010 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
1011 call assert_equal(':', a)
1012 call assert_equal([
1013 \ 'WinLeave ' .. org_winid,
1014 \ 'WinEnter ' .. s:cmd_winid,
1015 \ 'BufLeave ' .. org_bufnr,
1016 \ 'BufEnter ' .. s:cmd_bufnr,
1017 \ 'CmdWinEnter ' .. s:cmd_winid,
1018 \ 'CmdWinLeave ' .. s:cmd_winid,
1019 \ 'BufLeave ' .. s:cmd_bufnr,
1020 \ 'WinLeave ' .. s:cmd_winid,
1021 \ 'WinEnter ' .. org_winid,
1022 \ 'BufEnter ' .. org_bufnr,
1023 \ ], s:seq)
1024
1025 au!
1026 augroup END
1027endfunc
1028
Bram Moolenaar52604f22017-04-07 16:17:39 +02001029func Test_verbosefile()
1030 set verbosefile=Xlog
1031 echomsg 'foo'
1032 echomsg 'bar'
1033 set verbosefile=
1034 let log = readfile('Xlog')
1035 call assert_match("foo\nbar", join(log, "\n"))
1036 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001037 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001038 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001039 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +02001040endfunc
1041
Bram Moolenaar4facea32019-10-12 20:17:40 +02001042func Test_verbose_option()
1043 CheckScreendump
1044
1045 let lines =<< trim [SCRIPT]
1046 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1047 call feedkeys("\r", 't') " for the hit-enter prompt
1048 set verbose=20
1049 [SCRIPT]
1050 call writefile(lines, 'XTest_verbose')
1051
1052 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001053 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001054 call term_sendkeys(buf, ":DoSomething\<CR>")
1055 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1056
1057 " clean up
1058 call StopVimInTerminal(buf)
1059 call delete('XTest_verbose')
1060endfunc
1061
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001062func Test_setcmdpos()
1063 func InsertTextAtPos(text, pos)
1064 call assert_equal(0, setcmdpos(a:pos))
1065 return a:text
1066 endfunc
1067
1068 " setcmdpos() with position in the middle of the command line.
1069 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1070 call assert_equal('"1ab2', @:)
1071
1072 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1073 call assert_equal('"1b2a', @:)
1074
1075 " setcmdpos() with position beyond the end of the command line.
1076 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1077 call assert_equal('"12ab', @:)
1078
1079 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001080 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001081endfunc
1082
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001083func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001084 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001085 let encoding_save = &encoding
1086
1087 for e in encodings
1088 exe 'set encoding=' . e
1089
1090 " Test overstrike in the middle of the command line.
1091 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001092 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001093
1094 " Test overstrike going beyond end of command line.
1095 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001096 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001097
1098 " Test toggling insert/overstrike a few times.
1099 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001100 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001101 endfor
1102
Bram Moolenaar30276f22019-01-24 17:59:39 +01001103 " Test overstrike with multi-byte characters.
1104 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001105 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001106
1107 let &encoding = encoding_save
1108endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001109
1110func Test_cmdwin_bug()
1111 let winid = win_getid()
1112 sp
1113 try
1114 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1115 catch /^Vim\%((\a\+)\)\=:E11/
1116 endtry
1117 bw!
1118endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001119
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001120func Test_cmdwin_restore()
1121 CheckScreendump
1122
1123 let lines =<< trim [SCRIPT]
1124 call setline(1, range(30))
1125 2split
1126 [SCRIPT]
1127 call writefile(lines, 'XTest_restore')
1128
1129 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001130 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001131 call term_sendkeys(buf, "q:")
1132 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1133
1134 " normal restore
1135 call term_sendkeys(buf, ":q\<CR>")
1136 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1137
1138 " restore after setting 'lines' with one window
1139 call term_sendkeys(buf, ":close\<CR>")
1140 call term_sendkeys(buf, "q:")
1141 call term_sendkeys(buf, ":set lines=18\<CR>")
1142 call term_sendkeys(buf, ":q\<CR>")
1143 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1144
1145 " clean up
1146 call StopVimInTerminal(buf)
1147 call delete('XTest_restore')
1148endfunc
1149
Bram Moolenaar52410572019-10-27 05:12:45 +01001150func Test_buffers_lastused()
1151 " check that buffers are sorted by time when wildmode has lastused
1152 call test_settime(1550020000) " middle
1153 edit bufa
1154 enew
1155 call test_settime(1550030000) " newest
1156 edit bufb
1157 enew
1158 call test_settime(1550010000) " oldest
1159 edit bufc
1160 enew
1161 call test_settime(0)
1162 enew
1163
1164 call assert_equal(['bufa', 'bufb', 'bufc'],
1165 \ getcompletion('', 'buffer'))
1166
1167 let save_wildmode = &wildmode
1168 set wildmode=full:lastused
1169
1170 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1171 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1172 call assert_equal('b bufb', X)
1173 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1174 call assert_equal('b bufa', X)
1175 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1176 call assert_equal('b bufc', X)
1177 enew
1178
1179 edit other
1180 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1181 call assert_equal('b bufb', X)
1182 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1183 call assert_equal('b bufa', X)
1184 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1185 call assert_equal('b bufc', X)
1186 enew
1187
1188 let &wildmode = save_wildmode
1189
1190 bwipeout bufa
1191 bwipeout bufb
1192 bwipeout bufc
1193endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001194
1195func Test_cmdwin_feedkeys()
1196 " This should not generate E488
1197 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001198 " Using feedkeys with q: only should automatically close the cmd window
1199 call feedkeys('q:', 'xt')
1200 call assert_equal(1, winnr('$'))
1201 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001202endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001203
1204" Tests for the issues fixed in 7.4.441.
1205" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1206func Test_cmdwin_cedit()
1207 exe "set cedit=\<C-c>"
1208 normal! :
1209 call assert_equal(1, winnr('$'))
1210
1211 let g:cmd_wintype = ''
1212 func CmdWinType()
1213 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001214 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001215 return ''
1216 endfunc
1217
1218 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1219 echo input('')
1220 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001221 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001222
1223 set cedit&vim
1224 delfunc CmdWinType
1225endfunc
1226
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001227" Test for CmdwinEnter autocmd
1228func Test_cmdwin_autocmd()
1229 augroup CmdWin
1230 au!
1231 autocmd CmdwinEnter * startinsert
1232 augroup END
1233
1234 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1235 call assert_equal('xyz', @:)
1236
1237 augroup CmdWin
1238 au!
1239 augroup END
1240 augroup! CmdWin
1241endfunc
1242
Bram Moolenaar479950f2020-01-19 15:45:17 +01001243func Test_cmdlineclear_tabenter()
1244 CheckScreendump
1245
1246 let lines =<< trim [SCRIPT]
1247 call setline(1, range(30))
1248 [SCRIPT]
1249
1250 call writefile(lines, 'XtestCmdlineClearTabenter')
1251 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001252 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001253 " in one tab make the command line higher with CTRL-W -
1254 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1255 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1256
1257 call StopVimInTerminal(buf)
1258 call delete('XtestCmdlineClearTabenter')
1259endfunc
1260
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001261" Test for failure in expanding special keywords in cmdline
1262func Test_cmdline_expand_special()
1263 %bwipe!
1264 call assert_fails('e #', 'E499:')
1265 call assert_fails('e <afile>', 'E495:')
1266 call assert_fails('e <abuf>', 'E496:')
1267 call assert_fails('e <amatch>', 'E497:')
1268endfunc
1269
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001270func Test_cmdwin_jump_to_win()
1271 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1272 new
1273 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001274 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001275 close!
1276 call feedkeys("q/:close\<CR>", "xt")
1277 call assert_equal(1, winnr('$'))
1278 call feedkeys("q/:exit\<CR>", "xt")
1279 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001280
1281 " opening command window twice should fail
1282 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1283 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001284endfunc
1285
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001286func Test_cmdwin_interrupted()
1287 CheckScreendump
1288
1289 " aborting the :smile output caused the cmdline window to use the current
1290 " buffer.
1291 let lines =<< trim [SCRIPT]
1292 au WinNew * smile
1293 [SCRIPT]
1294 call writefile(lines, 'XTest_cmdwin')
1295
1296 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001297 " open cmdwin
1298 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001299 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001300 " quit more prompt for :smile command
1301 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001302 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001303 " execute a simple command
1304 call term_sendkeys(buf, "aecho 'done'\<CR>")
1305 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1306
1307 " clean up
1308 call StopVimInTerminal(buf)
1309 call delete('XTest_cmdwin')
1310endfunc
1311
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001312" Test for backtick expression in the command line
1313func Test_cmd_backtick()
1314 %argd
1315 argadd `=['a', 'b', 'c']`
1316 call assert_equal(['a', 'b', 'c'], argv())
1317 %argd
1318endfunc
1319
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001320" Test for the :! command
1321func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001322 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001323
1324 let lines =<< trim [SCRIPT]
1325 " Test for no previous command
1326 call assert_fails('!!', 'E34:')
1327 set nomore
1328 " Test for cmdline expansion with :!
1329 call setline(1, 'foo!')
1330 silent !echo <cWORD> > Xfile.out
1331 call assert_equal(['foo!'], readfile('Xfile.out'))
1332 " Test for using previous command
1333 silent !echo \! !
1334 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1335 call writefile(v:errors, 'Xresult')
1336 call delete('Xfile.out')
1337 qall!
1338 [SCRIPT]
1339 call writefile(lines, 'Xscript')
1340 if RunVim([], [], '--clean -S Xscript')
1341 call assert_equal([], readfile('Xresult'))
1342 endif
1343 call delete('Xscript')
1344 call delete('Xresult')
1345endfunc
1346
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001347" Test error: "E135: *Filter* Autocommands must not change current buffer"
1348func Test_cmd_bang_E135()
1349 new
1350 call setline(1, ['a', 'b', 'c', 'd'])
1351 augroup test_cmd_filter_E135
1352 au!
1353 autocmd FilterReadPost * help
1354 augroup END
1355 call assert_fails('2,3!echo "x"', 'E135:')
1356
1357 augroup test_cmd_filter_E135
1358 au!
1359 augroup END
1360 %bwipe!
1361endfunc
1362
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001363" Test for using ~ for home directory in cmdline completion matches
1364func Test_cmdline_expand_home()
1365 call mkdir('Xdir')
1366 call writefile([], 'Xdir/Xfile1')
1367 call writefile([], 'Xdir/Xfile2')
1368 cd Xdir
1369 let save_HOME = $HOME
1370 let $HOME = getcwd()
1371 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1372 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1373 let $HOME = save_HOME
1374 cd ..
1375 call delete('Xdir', 'rf')
1376endfunc
1377
Bram Moolenaar578fe942020-02-27 21:32:51 +01001378" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1379" or insert mode (when 'insertmode' is set)
1380func Test_cmdline_ctrl_g()
1381 new
1382 call setline(1, 'abc')
1383 call cursor(1, 3)
1384 " If command line is entered from insert mode, using C-\ C-G should back to
1385 " insert mode
1386 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1387 call assert_equal('abxyc', getline(1))
1388 call assert_equal(4, col('.'))
1389
1390 " If command line is entered in 'insertmode', using C-\ C-G should back to
1391 " 'insertmode'
1392 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1393 call assert_equal('ab12xyc', getline(1))
1394 close!
1395endfunc
1396
Bram Moolenaar578fe942020-02-27 21:32:51 +01001397" Test for 'wildmode'
1398func Test_wildmode()
1399 func T(a, c, p)
1400 return "oneA\noneB\noneC"
1401 endfunc
1402 command -nargs=1 -complete=custom,T MyCmd
1403
1404 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001405 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001406 return ''
1407 endfunc
1408 cnoremap <expr> <F2> SaveScreenLine()
1409
1410 set nowildmenu
1411 set wildmode=full,list
1412 let g:Sline = ''
1413 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001414 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001415 call assert_equal('"MyCmd oneA', @:)
1416
1417 set wildmode=longest,full
1418 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1419 call assert_equal('"MyCmd one', @:)
1420 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1421 call assert_equal('"MyCmd oneC', @:)
1422
1423 set wildmode=longest
1424 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1425 call assert_equal('"MyCmd one', @:)
1426
1427 set wildmode=list:longest
1428 let g:Sline = ''
1429 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001430 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001431 call assert_equal('"MyCmd one', @:)
1432
1433 set wildmode=""
1434 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1435 call assert_equal('"MyCmd oneA', @:)
1436
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001437 " Test for wildmode=longest with 'fileignorecase' set
1438 set wildmode=longest
1439 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001440 argadd AAA AAAA AAAAA
1441 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1442 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001443 set fileignorecase&
1444
1445 " Test for listing files with wildmode=list
1446 set wildmode=list
1447 let g:Sline = ''
1448 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001449 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001450 call assert_equal('"b A', @:)
1451
1452 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001453 delcommand MyCmd
1454 delfunc T
1455 delfunc SaveScreenLine
1456 cunmap <F2>
1457 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001458 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001459endfunc
1460
1461" Test for interrupting the command-line completion
1462func Test_interrupt_compl()
1463 func F(lead, cmdl, p)
1464 if a:lead =~ 'tw'
1465 call interrupt()
1466 return
1467 endif
1468 return "one\ntwo\nthree"
1469 endfunc
1470 command -nargs=1 -complete=custom,F Tcmd
1471
1472 set nowildmenu
1473 set wildmode=full
1474 let interrupted = 0
1475 try
1476 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1477 catch /^Vim:Interrupt$/
1478 let interrupted = 1
1479 endtry
1480 call assert_equal(1, interrupted)
1481
1482 delcommand Tcmd
1483 delfunc F
1484 set wildmode&
1485endfunc
1486
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001487" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001488func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001489 let str = ":one two\<C-U>"
1490 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001491 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001492 let str ..= "\<Left>five\<Right>"
1493 let str ..= "\<Home>two "
1494 let str ..= "\<C-Left>one "
1495 let str ..= "\<C-Right> three"
1496 let str ..= "\<End>\<S-Left>four "
1497 let str ..= "\<S-Right> six"
1498 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1499 call feedkeys(str, 'xt')
1500 call assert_equal("\"one two three four five six seven", @:)
1501endfunc
1502
1503" Test for moving the cursor on the / command line in 'rightleft' mode
1504func Test_cmdline_edit_rightleft()
1505 CheckFeature rightleft
1506 set rightleft
1507 set rightleftcmd=search
1508 let str = "/one two\<C-U>"
1509 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001510 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001511 let str ..= "\<Right>five\<Left>"
1512 let str ..= "\<Home>two "
1513 let str ..= "\<C-Right>one "
1514 let str ..= "\<C-Left> three"
1515 let str ..= "\<End>\<S-Right>four "
1516 let str ..= "\<S-Left> six"
1517 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1518 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1519 call assert_equal("\"one two three four five six seven", @/)
1520 set rightleftcmd&
1521 set rightleft&
1522endfunc
1523
1524" Test for using <C-\>e in the command line to evaluate an expression
1525func Test_cmdline_expr()
1526 " Evaluate an expression from the beginning of a command line
1527 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1528 call assert_equal('"hello', @:)
1529
1530 " Use an invalid expression for <C-\>e
1531 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1532
1533 " Insert literal <CTRL-\> in the command line
1534 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1535 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001536endfunc
1537
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001538" Test for 'imcmdline' and 'imsearch'
1539" This test doesn't actually test the input method functionality.
1540func Test_cmdline_inputmethod()
1541 new
1542 call setline(1, ['', 'abc', ''])
1543 set imcmdline
1544
1545 call feedkeys(":\"abc\<CR>", 'xt')
1546 call assert_equal("\"abc", @:)
1547 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1548 call assert_equal("\"abc", @:)
1549 call feedkeys("/abc\<CR>", 'xt')
1550 call assert_equal([2, 1], [line('.'), col('.')])
1551 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1552 call assert_equal([2, 1], [line('.'), col('.')])
1553
1554 set imsearch=2
1555 call cursor(1, 1)
1556 call feedkeys("/abc\<CR>", 'xt')
1557 call assert_equal([2, 1], [line('.'), col('.')])
1558 call cursor(1, 1)
1559 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1560 call assert_equal([2, 1], [line('.'), col('.')])
1561 set imdisable
1562 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1563 call assert_equal([2, 1], [line('.'), col('.')])
1564 set imdisable&
1565 set imsearch&
1566
1567 set imcmdline&
1568 %bwipe!
1569endfunc
1570
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001571" Test for recursively getting multiple command line inputs
1572func Test_cmdwin_multi_input()
1573 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1574 call assert_equal('"cyan', @:)
1575endfunc
1576
1577" Test for using CTRL-_ in the command line with 'allowrevins'
1578func Test_cmdline_revins()
1579 CheckNotMSWindows
1580 CheckFeature rightleft
1581 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1582 call assert_equal("\"abc\<c-_>", @:)
1583 set allowrevins
1584 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1585 call assert_equal('"abcñèæ', @:)
1586 set allowrevins&
1587endfunc
1588
1589" Test for typing UTF-8 composing characters in the command line
1590func Test_cmdline_composing_chars()
1591 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1592 call assert_equal('"ゔ', @:)
1593endfunc
1594
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001595" Test for normal mode commands not supported in the cmd window
1596func Test_cmdwin_blocked_commands()
1597 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1598 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1599 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1600 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1601 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1602 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001603 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1604 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1605 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1606 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1607 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1608 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1609 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1610 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1611 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1612 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1613 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1614 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1615 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1616 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1617 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1618 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1619 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1620 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1621 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1622 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1623 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001624endfunc
1625
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001626" Close the Cmd-line window in insert mode using CTRL-C
1627func Test_cmdwin_insert_mode_close()
1628 %bw!
1629 let s = ''
1630 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1631 call assert_equal('Hello', s)
1632 call assert_equal(1, winnr('$'))
1633endfunc
1634
Bram Moolenaar0e717042020-04-27 19:29:01 +02001635" test that ";" works to find a match at the start of the first line
1636func Test_zero_line_search()
1637 new
1638 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1639 call cursor(1,1)
1640 0;/pattern/d
1641 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1642 q!
1643endfunc
1644
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001645func Test_read_shellcmd()
1646 CheckUnix
1647 if executable('ls')
1648 " There should be ls in the $PATH
1649 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1650 call assert_match('^"r! .*\<ls\>', @:)
1651 endif
1652
1653 if executable('rm')
1654 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1655 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1656 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001657
1658 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1659 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1660 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001661 endif
1662endfunc
1663
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01001664" Test for going up and down the directory tree using 'wildmenu'
1665func Test_wildmenu_dirstack()
1666 CheckUnix
1667 %bw!
1668 call mkdir('Xdir1/dir2/dir3', 'p')
1669 call writefile([], 'Xdir1/file1_1.txt')
1670 call writefile([], 'Xdir1/file1_2.txt')
1671 call writefile([], 'Xdir1/dir2/file2_1.txt')
1672 call writefile([], 'Xdir1/dir2/file2_2.txt')
1673 call writefile([], 'Xdir1/dir2/dir3/file3_1.txt')
1674 call writefile([], 'Xdir1/dir2/dir3/file3_2.txt')
1675 cd Xdir1/dir2/dir3
1676 set wildmenu
1677
1678 call feedkeys(":e \<Tab>\<C-B>\"\<CR>", 'xt')
1679 call assert_equal('"e file3_1.txt', @:)
1680 call feedkeys(":e \<Tab>\<Up>\<C-B>\"\<CR>", 'xt')
1681 call assert_equal('"e ../dir3/', @:)
1682 call feedkeys(":e \<Tab>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
1683 call assert_equal('"e ../../dir2/', @:)
1684 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<C-B>\"\<CR>", 'xt')
1685 call assert_equal('"e ../../dir2/dir3/', @:)
1686 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
1687 call assert_equal('"e ../../dir2/dir3/file3_1.txt', @:)
1688
1689 cd -
1690 call delete('Xdir1', 'rf')
1691 set wildmenu&
1692endfunc
1693
Bram Moolenaar309976e2019-12-05 18:16:33 +01001694" vim: shiftwidth=2 sts=2 expandtab