blob: 1baed7961f8cc031d7a2bc3b84bb24859446cee4 [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
5
Bram Moolenaarae3150e2016-06-11 23:22:36 +02006func Test_complete_tab()
7 call writefile(['testfile'], 'Xtestfile')
8 call feedkeys(":e Xtest\t\r", "tx")
9 call assert_equal('testfile', getline(1))
10 call delete('Xtestfile')
11endfunc
12
13func Test_complete_list()
14 " We can't see the output, but at least we check the code runs properly.
15 call feedkeys(":e test\<C-D>\r", "tx")
16 call assert_equal('test', expand('%:t'))
Bram Moolenaar578fe942020-02-27 21:32:51 +010017
18 " If a command doesn't support completion, then CTRL-D should be literally
19 " used.
20 call feedkeys(":chistory \<C-D>\<C-B>\"\<CR>", 'xt')
21 call assert_equal("\"chistory \<C-D>", @:)
Bram Moolenaarae3150e2016-06-11 23:22:36 +020022endfunc
23
24func Test_complete_wildmenu()
Bram Moolenaar37db6422019-03-28 21:26:23 +010025 call mkdir('Xdir1/Xdir2', 'p')
26 call writefile(['testfile1'], 'Xdir1/Xtestfile1')
27 call writefile(['testfile2'], 'Xdir1/Xtestfile2')
28 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3')
29 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020030 set wildmenu
Bram Moolenaar37db6422019-03-28 21:26:23 +010031
32 " Pressing <Tab> completes, and moves to next files when pressing again.
33 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx')
34 call assert_equal('testfile1', getline(1))
35 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020036 call assert_equal('testfile2', getline(1))
37
Bram Moolenaar37db6422019-03-28 21:26:23 +010038 " <S-Tab> is like <Tab> but begin with the last match and then go to
39 " previous.
40 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx')
41 call assert_equal('testfile2', getline(1))
42 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
43 call assert_equal('testfile1', getline(1))
44
45 " <Left>/<Right> to move to previous/next file.
46 call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx')
47 call assert_equal('testfile1', getline(1))
48 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
49 call assert_equal('testfile2', getline(1))
50 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
51 call assert_equal('testfile1', getline(1))
52
53 " <Up>/<Down> to go up/down directories.
54 call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx')
55 call assert_equal('testfile3', getline(1))
56 call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
57 call assert_equal('testfile1', getline(1))
58
Bram Moolenaar578fe942020-02-27 21:32:51 +010059 " Test for canceling the wild menu by adding a character
60 redrawstatus
61 call feedkeys(":e Xdir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
62 call assert_equal('"e Xdir1/Xdir2/x', @:)
63
Bram Moolenaar8d588cc2020-02-25 21:47:45 +010064 " Completion using a relative path
65 cd Xdir1/Xdir2
66 call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
67 call assert_equal('"e Xtestfile3 Xtestfile4', @:)
68 cd -
69
Bram Moolenaar37db6422019-03-28 21:26:23 +010070 " cleanup
71 %bwipe
72 call delete('Xdir1/Xdir2/Xtestfile4')
73 call delete('Xdir1/Xdir2/Xtestfile3')
74 call delete('Xdir1/Xtestfile2')
75 call delete('Xdir1/Xtestfile1')
76 call delete('Xdir1/Xdir2', 'd')
77 call delete('Xdir1', 'd')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020078 set nowildmenu
79endfunc
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020080
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +010081func Test_map_completion()
82 if !has('cmdline_compl')
83 return
84 endif
85 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
86 call assert_equal('"map <unique> <silent>', getreg(':'))
87 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
88 call assert_equal('"map <script> <unique>', getreg(':'))
89 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
90 call assert_equal('"map <expr> <script>', getreg(':'))
91 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
92 call assert_equal('"map <buffer> <expr>', getreg(':'))
93 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
94 call assert_equal('"map <nowait> <buffer>', getreg(':'))
95 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
96 call assert_equal('"map <special> <nowait>', getreg(':'))
97 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
98 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +020099
Bram Moolenaar1776a282019-05-03 16:05:41 +0200100 map <Middle>x middle
101
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200102 map ,f commaf
103 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200104 map <Left> left
105 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200106 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
107 call assert_equal('"map ,f', getreg(':'))
108 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
109 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200110 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
111 call assert_equal('"map <Left>', getreg(':'))
112 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200113 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200114 unmap ,f
115 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200116 unmap <Left>
117 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200118
119 set cpo-=< cpo-=B cpo-=k
120 map <Left> left
121 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
122 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200123 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200124 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200125 unmap <Left>
126
127 set cpo+=<
128 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200129 exe "set t_k6=\<Esc>[17~"
130 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200131 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
132 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200133 if !has('gui_running')
134 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
135 call assert_equal("\"map <F6>x", getreg(':'))
136 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200137 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200138 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200139 set cpo-=<
140
141 set cpo+=B
142 map <Left> left
143 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
144 call assert_equal('"map <Left>', getreg(':'))
145 unmap <Left>
146 set cpo-=B
147
148 set cpo+=k
149 map <Left> left
150 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
151 call assert_equal('"map <Left>', getreg(':'))
152 unmap <Left>
153 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200154
155 unmap <Middle>x
156 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100157endfunc
158
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100159func Test_match_completion()
160 if !has('cmdline_compl')
161 return
162 endif
163 hi Aardig ctermfg=green
164 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
165 call assert_equal('"match Aardig', getreg(':'))
166 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
167 call assert_equal('"match none', getreg(':'))
168endfunc
169
170func Test_highlight_completion()
171 if !has('cmdline_compl')
172 return
173 endif
174 hi Aardig ctermfg=green
175 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
176 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200177 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
178 call assert_equal('"hi default Aardig', getreg(':'))
179 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
180 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100181 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
182 call assert_equal('"hi link', getreg(':'))
183 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
184 call assert_equal('"hi default', getreg(':'))
185 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
186 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200187
188 " A cleared group does not show up in completions.
189 hi Anders ctermfg=green
190 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
191 hi clear Aardig
192 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
193 hi clear Anders
194 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100195endfunc
196
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100197func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100198 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100199 return
200 endif
201 for cmd in [
202 \ 'let a = ',
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +0100203 \ 'const a = ',
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100204 \ 'if',
205 \ 'elseif',
206 \ 'while',
207 \ 'for',
208 \ 'echo',
209 \ 'echon',
210 \ 'execute',
211 \ 'echomsg',
212 \ 'echoerr',
213 \ 'call',
214 \ 'return',
215 \ 'cexpr',
216 \ 'caddexpr',
217 \ 'cgetexpr',
218 \ 'lexpr',
219 \ 'laddexpr',
220 \ 'lgetexpr']
221 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
222 call assert_equal('"' . cmd . ' getline(', getreg(':'))
223 endfor
224endfunc
225
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200226func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200227 if !has('cmdline_compl')
228 return
229 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200230 let groupcount = len(getcompletion('', 'event'))
231 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200232 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200233 call assert_true(matchcount > 0)
234 call assert_true(groupcount > matchcount)
235
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200236 if has('menu')
237 source $VIMRUNTIME/menu.vim
238 let matchcount = len(getcompletion('', 'menu'))
239 call assert_true(matchcount > 0)
240 call assert_equal(['File.'], getcompletion('File', 'menu'))
241 call assert_true(matchcount > 0)
242 let matchcount = len(getcompletion('File.', 'menu'))
243 call assert_true(matchcount > 0)
244 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200245
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200246 let l = getcompletion('v:n', 'var')
247 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200248 let l = getcompletion('v:notexists', 'var')
249 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200250
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200251 args a.c b.c
252 let l = getcompletion('', 'arglist')
253 call assert_equal(['a.c', 'b.c'], l)
254 %argdelete
255
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200256 let l = getcompletion('', 'augroup')
257 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200258 let l = getcompletion('blahblah', 'augroup')
259 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200260
261 let l = getcompletion('', 'behave')
262 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200263 let l = getcompletion('not', 'behave')
264 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200265
266 let l = getcompletion('', 'color')
267 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200268 let l = getcompletion('dirty', 'color')
269 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200270
271 let l = getcompletion('', 'command')
272 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200273 let l = getcompletion('awake', 'command')
274 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200275
276 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200277 call assert_true(index(l, 'samples/') >= 0)
278 let l = getcompletion('NoMatch', 'dir')
279 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200280
281 let l = getcompletion('exe', 'expression')
282 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200283 let l = getcompletion('kill', 'expression')
284 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200285
286 let l = getcompletion('tag', 'function')
287 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200288 let l = getcompletion('paint', 'function')
289 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200290
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200291 let Flambda = {-> 'hello'}
292 let l = getcompletion('', 'function')
293 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200294 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200295
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200296 let l = getcompletion('run', 'file')
297 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200298 let l = getcompletion('walk', 'file')
299 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200300 set wildignore=*.vim
301 let l = getcompletion('run', 'file', 1)
302 call assert_true(index(l, 'runtest.vim') < 0)
303 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200304
305 let l = getcompletion('ha', 'filetype')
306 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200307 let l = getcompletion('horse', 'filetype')
308 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200309
310 let l = getcompletion('z', 'syntax')
311 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200312 let l = getcompletion('emacs', 'syntax')
313 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200314
315 let l = getcompletion('jikes', 'compiler')
316 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200317 let l = getcompletion('break', 'compiler')
318 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200319
320 let l = getcompletion('last', 'help')
321 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200322 let l = getcompletion('giveup', 'help')
323 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200324
325 let l = getcompletion('time', 'option')
326 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200327 let l = getcompletion('space', 'option')
328 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200329
330 let l = getcompletion('er', 'highlight')
331 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200332 let l = getcompletion('dark', 'highlight')
333 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200334
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200335 let l = getcompletion('', 'messages')
336 call assert_true(index(l, 'clear') >= 0)
337 let l = getcompletion('not', 'messages')
338 call assert_equal([], l)
339
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200340 let l = getcompletion('', 'mapclear')
341 call assert_true(index(l, '<buffer>') >= 0)
342 let l = getcompletion('not', 'mapclear')
343 call assert_equal([], l)
344
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200345 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200346 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200347 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
348 let root = has('win32') ? 'C:\\' : '/'
349 let l = getcompletion(root, 'shellcmd')
350 let expected = map(filter(glob(root . '*', 0, 1),
351 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
352 call assert_equal(expected, l)
353
Bram Moolenaarb650b982016-08-05 20:35:13 +0200354 if has('cscope')
355 let l = getcompletion('', 'cscope')
356 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
357 call assert_equal(cmds, l)
358 " using cmdline completion must not change the result
359 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
360 let l = getcompletion('', 'cscope')
361 call assert_equal(cmds, l)
362 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
363 let l = getcompletion('find ', 'cscope')
364 call assert_equal(keys, l)
365 endif
366
Bram Moolenaar7522f692016-08-06 14:12:50 +0200367 if has('signs')
368 sign define Testing linehl=Comment
369 let l = getcompletion('', 'sign')
370 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
371 call assert_equal(cmds, l)
372 " using cmdline completion must not change the result
373 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
374 let l = getcompletion('', 'sign')
375 call assert_equal(cmds, l)
376 let l = getcompletion('list ', 'sign')
377 call assert_equal(['Testing'], l)
378 endif
379
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200380 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200381 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200382 if has('cmdline_hist')
383 call add(names, 'history')
384 endif
385 if has('gettext')
386 call add(names, 'locale')
387 endif
388 if has('profile')
389 call add(names, 'syntime')
390 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200391
392 set tags=Xtags
393 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
394
395 for name in names
396 let matchcount = len(getcompletion('', name))
397 call assert_true(matchcount >= 0, 'No matches for ' . name)
398 endfor
399
400 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200401 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200402
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200403 call assert_fails('call getcompletion("", "burp")', 'E475:')
404endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200405
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200406func Test_shellcmd_completion()
407 let save_path = $PATH
408
409 call mkdir('Xpathdir/Xpathsubdir', 'p')
410 call writefile([''], 'Xpathdir/Xfile.exe')
411 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
412
413 " Set PATH to example directory without trailing slash.
414 let $PATH = getcwd() . '/Xpathdir'
415
416 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
417 " dirs in the PATH, even though they won't be executed. We check that only
418 " subdirs of the PWD and executables from the PATH are included in the
419 " suggestions.
420 let actual = getcompletion('X', 'shellcmd')
421 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
422 call insert(expected, 'Xfile.exe')
423 call assert_equal(expected, actual)
424
425 call delete('Xpathdir', 'rf')
426 let $PATH = save_path
427endfunc
428
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200429func Test_expand_star_star()
430 call mkdir('a/b', 'p')
431 call writefile(['asdfasdf'], 'a/b/fileXname')
432 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
433 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200434 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200435 call delete('a', 'rf')
436endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100437
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100438func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100439 let @a = "def"
440 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
441 call assert_equal('"abc def ghi', @:)
442
443 new
444 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
445
446 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
447 call assert_equal('"aaa asdf bbb', @:)
448
449 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
450 call assert_equal('"aaa /tmp/some bbb', @:)
451
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200452 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
453 call assert_equal('"aaa '.getline(1).' bbb', @:)
454
Bram Moolenaar21efc362016-12-03 14:05:49 +0100455 set incsearch
456 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
457 call assert_equal('"aaa verylongword bbb', @:)
458
459 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
460 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100461
462 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
463 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100464 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200465
466 " Error while typing a command used to cause that it was not executed
467 " in the end.
468 new
469 try
470 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
471 catch /^Vim\%((\a\+)\)\=:E32/
472 " ignore error E32
473 endtry
474 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100475
Bram Moolenaar578fe942020-02-27 21:32:51 +0100476 " Try to paste an invalid register using <C-R>
477 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
478 call assert_equal('"onetwo', @:)
479
480 let @a = "xy\<C-H>z"
481 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
482 call assert_equal('"xz', @:)
483 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
484 call assert_equal("\"xy\<C-H>z", @:)
485
486 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
487
Bram Moolenaar72532d32018-04-07 19:09:09 +0200488 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100489endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100490
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100491func Test_cmdline_remove_char()
492 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100493
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100494 for e in ['utf8', 'latin1']
495 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100496
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100497 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
498 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100499
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100500 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
501 call assert_equal('"abcdef', @:)
502
503 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
504 call assert_equal('"abc ghi', @:, e)
505
506 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
507 call assert_equal('"def', @:, e)
508 endfor
509
510 let &encoding = encoding_save
511endfunc
512
513func Test_cmdline_keymap_ctrl_hat()
514 if !has('keymap')
515 return
516 endif
517
518 set keymap=esperanto
519 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
520 call assert_equal('"Jxauxdo Ä´aÅ­do Jxauxdo', @:)
521 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100522endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100523
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100524func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100525 new
526 2;'(
527 2;')
528 quit
529endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100530
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100531func Test_illegal_address2()
532 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
533 new
534 source Xtest.vim
535 " Trigger calling validate_cursor()
536 diffsp Xtest.vim
537 quit!
538 bwipe!
539 call delete('Xtest.vim')
540endfunc
541
Bram Moolenaarba47b512017-01-24 21:18:19 +0100542func Test_cmdline_complete_wildoptions()
543 help
544 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
545 let a = join(sort(split(@:)),' ')
546 set wildoptions=tagfile
547 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
548 let b = join(sort(split(@:)),' ')
549 call assert_equal(a, b)
550 bw!
551endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100552
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200553func Test_cmdline_complete_user_cmd()
554 command! -complete=color -nargs=1 Foo :
555 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
556 call assert_equal('"Foo blue', @:)
557 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
558 call assert_equal('"Foo blue', @:)
559 delcommand Foo
560endfunc
561
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200562func Test_cmdline_complete_user_names()
563 if has('unix') && executable('whoami')
564 let whoami = systemlist('whoami')[0]
565 let first_letter = whoami[0]
566 if len(first_letter) > 0
567 " Trying completion of :e ~x where x is the first letter of
568 " the user name should complete to at least the user name.
569 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
570 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
571 endif
572 endif
573 if has('win32')
574 " Just in case: check that the system has an Administrator account.
575 let names = system('net user')
576 if names =~ 'Administrator'
577 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100578 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200579 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100580 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200581 endif
582 endif
583endfunc
584
Bram Moolenaar297610b2019-12-27 17:20:55 +0100585func Test_cmdline_complete_bang()
586 if executable('whoami')
Bram Moolenaar731a7992019-12-28 14:06:50 +0100587 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
588 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100589 endif
Bram Moolenaar297610b2019-12-27 17:20:55 +0100590endfunc
591
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200592funct Test_cmdline_complete_languages()
593 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
594
595 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
596 call assert_match('^"language .*\<ctype\>.*\<messages\>.*\<time\>', @:)
597
598 if has('unix')
599 " TODO: these tests don't work on Windows. lang appears to be 'C'
600 " but C does not appear in the completion. Why?
601 call assert_match('^"language .*\<' . lang . '\>', @:)
602
603 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
604 call assert_match('^"language .*\<' . lang . '\>', @:)
605
606 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
607 call assert_match('^"language .*\<' . lang . '\>', @:)
608
609 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
610 call assert_match('^"language .*\<' . lang . '\>', @:)
611 endif
612endfunc
613
Bram Moolenaar297610b2019-12-27 17:20:55 +0100614func Test_cmdline_complete_env_variable()
615 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
616
617 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
618 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
619
620 unlet $X_VIM_TEST_COMPLETE_ENV
621endfunc
622
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200623func Test_cmdline_write_alternatefile()
624 new
625 call setline('.', ['one', 'two'])
626 f foo.txt
627 new
628 f #-A
629 call assert_equal('foo.txt-A', expand('%'))
630 f #<-B.txt
631 call assert_equal('foo-B.txt', expand('%'))
632 f %<
633 call assert_equal('foo-B', expand('%'))
634 new
635 call assert_fails('f #<', 'E95')
636 bw!
637 f foo-B.txt
638 f %<-A
639 call assert_equal('foo-B-A', expand('%'))
640 bw!
641 bw!
642endfunc
643
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100644" using a leading backslash here
645set cpo+=C
646
647func Test_cmdline_search_range()
648 new
649 call setline(1, ['a', 'b', 'c', 'd'])
650 /d
651 1,\/s/b/B/
652 call assert_equal('B', getline(2))
653
654 /a
655 $
656 \?,4s/c/C/
657 call assert_equal('C', getline(3))
658
659 call setline(1, ['a', 'b', 'c', 'd'])
660 %s/c/c/
661 1,\&s/b/B/
662 call assert_equal('B', getline(2))
663
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100664 let @/ = 'apple'
665 call assert_fails('\/print', 'E486:')
666
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100667 bwipe!
668endfunc
669
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100670" Test for the tick mark (') in an excmd range
671func Test_tick_mark_in_range()
672 " If only the tick is passed as a range and no command is specified, there
673 " should not be an error
674 call feedkeys(":'\<CR>", 'xt')
675 call assert_equal("'", getreg(':'))
676 call assert_fails("',print", 'E78:')
677endfunc
678
679" Test for using a line number followed by a search pattern as range
680func Test_lnum_and_pattern_as_range()
681 new
682 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
683 let @" = ''
684 2/foo/yank
685 call assert_equal("foo 3\n", @")
686 call assert_equal(1, line('.'))
687 close!
688endfunc
689
Bram Moolenaar65189a12017-02-06 22:22:17 +0100690" Tests for getcmdline(), getcmdpos() and getcmdtype()
691func Check_cmdline(cmdtype)
692 call assert_equal('MyCmd a', getcmdline())
693 call assert_equal(8, getcmdpos())
694 call assert_equal(a:cmdtype, getcmdtype())
695 return ''
696endfunc
697
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200698set cpo&
699
Bram Moolenaar65189a12017-02-06 22:22:17 +0100700func Test_getcmdtype()
701 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
702
703 let cmdtype = ''
704 debuggreedy
705 call feedkeys(":debug echo 'test'\<CR>", "t")
706 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
707 call feedkeys("cont\<CR>", "xt")
708 0debuggreedy
709 call assert_equal('>', cmdtype)
710
711 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
712 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
713
714 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100715 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100716
717 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
718
719 cnoremap <expr> <F6> Check_cmdline('=')
720 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
721 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100722
723 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100724endfunc
725
Bram Moolenaar81612b72018-06-23 14:55:03 +0200726func Test_getcmdwintype()
727 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
728 call assert_equal('/', a)
729
730 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
731 call assert_equal('?', a)
732
733 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
734 call assert_equal(':', a)
735
736 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
737 call assert_equal(':', a)
738
739 call assert_equal('', getcmdwintype())
740endfunc
741
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200742func Test_getcmdwin_autocmd()
743 let s:seq = []
744 augroup CmdWin
745 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
746 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
747 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
748 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
749 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
750 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
751
752 let org_winid = win_getid()
753 let org_bufnr = bufnr()
754 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
755 call assert_equal(':', a)
756 call assert_equal([
757 \ 'WinLeave ' .. org_winid,
758 \ 'WinEnter ' .. s:cmd_winid,
759 \ 'BufLeave ' .. org_bufnr,
760 \ 'BufEnter ' .. s:cmd_bufnr,
761 \ 'CmdWinEnter ' .. s:cmd_winid,
762 \ 'CmdWinLeave ' .. s:cmd_winid,
763 \ 'BufLeave ' .. s:cmd_bufnr,
764 \ 'WinLeave ' .. s:cmd_winid,
765 \ 'WinEnter ' .. org_winid,
766 \ 'BufEnter ' .. org_bufnr,
767 \ ], s:seq)
768
769 au!
770 augroup END
771endfunc
772
Bram Moolenaar52604f22017-04-07 16:17:39 +0200773func Test_verbosefile()
774 set verbosefile=Xlog
775 echomsg 'foo'
776 echomsg 'bar'
777 set verbosefile=
778 let log = readfile('Xlog')
779 call assert_match("foo\nbar", join(log, "\n"))
780 call delete('Xlog')
781endfunc
782
Bram Moolenaar4facea32019-10-12 20:17:40 +0200783func Test_verbose_option()
784 CheckScreendump
785
786 let lines =<< trim [SCRIPT]
787 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
788 call feedkeys("\r", 't') " for the hit-enter prompt
789 set verbose=20
790 [SCRIPT]
791 call writefile(lines, 'XTest_verbose')
792
793 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
794 call term_wait(buf, 100)
795 call term_sendkeys(buf, ":DoSomething\<CR>")
796 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
797
798 " clean up
799 call StopVimInTerminal(buf)
800 call delete('XTest_verbose')
801endfunc
802
Bram Moolenaarff3be4f2018-05-12 13:18:46 +0200803func Test_setcmdpos()
804 func InsertTextAtPos(text, pos)
805 call assert_equal(0, setcmdpos(a:pos))
806 return a:text
807 endfunc
808
809 " setcmdpos() with position in the middle of the command line.
810 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
811 call assert_equal('"1ab2', @:)
812
813 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
814 call assert_equal('"1b2a', @:)
815
816 " setcmdpos() with position beyond the end of the command line.
817 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
818 call assert_equal('"12ab', @:)
819
820 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +0200821 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +0200822endfunc
823
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100824func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100825 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100826 let encoding_save = &encoding
827
828 for e in encodings
829 exe 'set encoding=' . e
830
831 " Test overstrike in the middle of the command line.
832 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100833 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100834
835 " Test overstrike going beyond end of command line.
836 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100837 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100838
839 " Test toggling insert/overstrike a few times.
840 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100841 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100842 endfor
843
Bram Moolenaar30276f22019-01-24 17:59:39 +0100844 " Test overstrike with multi-byte characters.
845 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100846 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100847
848 let &encoding = encoding_save
849endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +0200850
851func Test_cmdwin_bug()
852 let winid = win_getid()
853 sp
854 try
855 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
856 catch /^Vim\%((\a\+)\)\=:E11/
857 endtry
858 bw!
859endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +0100860
Bram Moolenaar1c329c02019-10-27 20:37:35 +0100861func Test_cmdwin_restore()
862 CheckScreendump
863
864 let lines =<< trim [SCRIPT]
865 call setline(1, range(30))
866 2split
867 [SCRIPT]
868 call writefile(lines, 'XTest_restore')
869
870 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
871 call term_wait(buf, 100)
872 call term_sendkeys(buf, "q:")
873 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
874
875 " normal restore
876 call term_sendkeys(buf, ":q\<CR>")
877 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
878
879 " restore after setting 'lines' with one window
880 call term_sendkeys(buf, ":close\<CR>")
881 call term_sendkeys(buf, "q:")
882 call term_sendkeys(buf, ":set lines=18\<CR>")
883 call term_sendkeys(buf, ":q\<CR>")
884 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
885
886 " clean up
887 call StopVimInTerminal(buf)
888 call delete('XTest_restore')
889endfunc
890
Bram Moolenaar52410572019-10-27 05:12:45 +0100891func Test_buffers_lastused()
892 " check that buffers are sorted by time when wildmode has lastused
893 call test_settime(1550020000) " middle
894 edit bufa
895 enew
896 call test_settime(1550030000) " newest
897 edit bufb
898 enew
899 call test_settime(1550010000) " oldest
900 edit bufc
901 enew
902 call test_settime(0)
903 enew
904
905 call assert_equal(['bufa', 'bufb', 'bufc'],
906 \ getcompletion('', 'buffer'))
907
908 let save_wildmode = &wildmode
909 set wildmode=full:lastused
910
911 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
912 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
913 call assert_equal('b bufb', X)
914 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
915 call assert_equal('b bufa', X)
916 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
917 call assert_equal('b bufc', X)
918 enew
919
920 edit other
921 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
922 call assert_equal('b bufb', X)
923 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
924 call assert_equal('b bufa', X)
925 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
926 call assert_equal('b bufc', X)
927 enew
928
929 let &wildmode = save_wildmode
930
931 bwipeout bufa
932 bwipeout bufb
933 bwipeout bufc
934endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +0100935
936func Test_cmdwin_feedkeys()
937 " This should not generate E488
938 call feedkeys("q:\<CR>", 'x')
939endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +0100940
941" Tests for the issues fixed in 7.4.441.
942" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
943func Test_cmdwin_cedit()
944 exe "set cedit=\<C-c>"
945 normal! :
946 call assert_equal(1, winnr('$'))
947
948 let g:cmd_wintype = ''
949 func CmdWinType()
950 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +0100951 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +0100952 return ''
953 endfunc
954
955 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
956 echo input('')
957 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +0100958 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +0100959
960 set cedit&vim
961 delfunc CmdWinType
962endfunc
963
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100964" Test for CmdwinEnter autocmd
965func Test_cmdwin_autocmd()
966 augroup CmdWin
967 au!
968 autocmd CmdwinEnter * startinsert
969 augroup END
970
971 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
972 call assert_equal('xyz', @:)
973
974 augroup CmdWin
975 au!
976 augroup END
977 augroup! CmdWin
978endfunc
979
Bram Moolenaar479950f2020-01-19 15:45:17 +0100980func Test_cmdlineclear_tabenter()
981 CheckScreendump
982
983 let lines =<< trim [SCRIPT]
984 call setline(1, range(30))
985 [SCRIPT]
986
987 call writefile(lines, 'XtestCmdlineClearTabenter')
988 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
989 call term_wait(buf, 50)
990 " in one tab make the command line higher with CTRL-W -
991 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
992 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
993
994 call StopVimInTerminal(buf)
995 call delete('XtestCmdlineClearTabenter')
996endfunc
997
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100998" Test for failure in expanding special keywords in cmdline
999func Test_cmdline_expand_special()
1000 %bwipe!
1001 call assert_fails('e #', 'E499:')
1002 call assert_fails('e <afile>', 'E495:')
1003 call assert_fails('e <abuf>', 'E496:')
1004 call assert_fails('e <amatch>', 'E497:')
1005endfunc
1006
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001007func Test_cmdwin_jump_to_win()
1008 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1009 new
1010 set modified
1011 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', 'E162:')
1012 close!
1013 call feedkeys("q/:close\<CR>", "xt")
1014 call assert_equal(1, winnr('$'))
1015 call feedkeys("q/:exit\<CR>", "xt")
1016 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001017
1018 " opening command window twice should fail
1019 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1020 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001021endfunc
1022
1023" Test for backtick expression in the command line
1024func Test_cmd_backtick()
1025 %argd
1026 argadd `=['a', 'b', 'c']`
1027 call assert_equal(['a', 'b', 'c'], argv())
1028 %argd
1029endfunc
1030
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001031" Test for the :! command
1032func Test_cmd_bang()
1033 if !has('unix')
1034 return
1035 endif
1036
1037 let lines =<< trim [SCRIPT]
1038 " Test for no previous command
1039 call assert_fails('!!', 'E34:')
1040 set nomore
1041 " Test for cmdline expansion with :!
1042 call setline(1, 'foo!')
1043 silent !echo <cWORD> > Xfile.out
1044 call assert_equal(['foo!'], readfile('Xfile.out'))
1045 " Test for using previous command
1046 silent !echo \! !
1047 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1048 call writefile(v:errors, 'Xresult')
1049 call delete('Xfile.out')
1050 qall!
1051 [SCRIPT]
1052 call writefile(lines, 'Xscript')
1053 if RunVim([], [], '--clean -S Xscript')
1054 call assert_equal([], readfile('Xresult'))
1055 endif
1056 call delete('Xscript')
1057 call delete('Xresult')
1058endfunc
1059
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001060" Test for using ~ for home directory in cmdline completion matches
1061func Test_cmdline_expand_home()
1062 call mkdir('Xdir')
1063 call writefile([], 'Xdir/Xfile1')
1064 call writefile([], 'Xdir/Xfile2')
1065 cd Xdir
1066 let save_HOME = $HOME
1067 let $HOME = getcwd()
1068 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1069 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1070 let $HOME = save_HOME
1071 cd ..
1072 call delete('Xdir', 'rf')
1073endfunc
1074
Bram Moolenaar578fe942020-02-27 21:32:51 +01001075" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1076" or insert mode (when 'insertmode' is set)
1077func Test_cmdline_ctrl_g()
1078 new
1079 call setline(1, 'abc')
1080 call cursor(1, 3)
1081 " If command line is entered from insert mode, using C-\ C-G should back to
1082 " insert mode
1083 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1084 call assert_equal('abxyc', getline(1))
1085 call assert_equal(4, col('.'))
1086
1087 " If command line is entered in 'insertmode', using C-\ C-G should back to
1088 " 'insertmode'
1089 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1090 call assert_equal('ab12xyc', getline(1))
1091 close!
1092endfunc
1093
1094" Return the 'len' characters in screen starting from (row,col)
1095func s:ScreenLine(row, col, len)
1096 let s = ''
1097 for i in range(a:len)
1098 let s .= nr2char(screenchar(a:row, a:col + i))
1099 endfor
1100 return s
1101endfunc
1102
1103" Test for 'wildmode'
1104func Test_wildmode()
1105 func T(a, c, p)
1106 return "oneA\noneB\noneC"
1107 endfunc
1108 command -nargs=1 -complete=custom,T MyCmd
1109
1110 func SaveScreenLine()
1111 let g:Sline = s:ScreenLine(&lines - 1, 1, 20)
1112 return ''
1113 endfunc
1114 cnoremap <expr> <F2> SaveScreenLine()
1115
1116 set nowildmenu
1117 set wildmode=full,list
1118 let g:Sline = ''
1119 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
1120 call assert_equal('oneA oneB oneC ', g:Sline)
1121 call assert_equal('"MyCmd oneA', @:)
1122
1123 set wildmode=longest,full
1124 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1125 call assert_equal('"MyCmd one', @:)
1126 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1127 call assert_equal('"MyCmd oneC', @:)
1128
1129 set wildmode=longest
1130 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1131 call assert_equal('"MyCmd one', @:)
1132
1133 set wildmode=list:longest
1134 let g:Sline = ''
1135 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
1136 call assert_equal('oneA oneB oneC ', g:Sline)
1137 call assert_equal('"MyCmd one', @:)
1138
1139 set wildmode=""
1140 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1141 call assert_equal('"MyCmd oneA', @:)
1142
1143 delcommand MyCmd
1144 delfunc T
1145 delfunc SaveScreenLine
1146 cunmap <F2>
1147 set wildmode&
1148endfunc
1149
1150" Test for interrupting the command-line completion
1151func Test_interrupt_compl()
1152 func F(lead, cmdl, p)
1153 if a:lead =~ 'tw'
1154 call interrupt()
1155 return
1156 endif
1157 return "one\ntwo\nthree"
1158 endfunc
1159 command -nargs=1 -complete=custom,F Tcmd
1160
1161 set nowildmenu
1162 set wildmode=full
1163 let interrupted = 0
1164 try
1165 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1166 catch /^Vim:Interrupt$/
1167 let interrupted = 1
1168 endtry
1169 call assert_equal(1, interrupted)
1170
1171 delcommand Tcmd
1172 delfunc F
1173 set wildmode&
1174endfunc
1175
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001176" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001177func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001178 let str = ":one two\<C-U>"
1179 let str ..= "one two\<C-W>\<C-W>"
1180 let str ..= "\<Left>five\<Right>"
1181 let str ..= "\<Home>two "
1182 let str ..= "\<C-Left>one "
1183 let str ..= "\<C-Right> three"
1184 let str ..= "\<End>\<S-Left>four "
1185 let str ..= "\<S-Right> six"
1186 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1187 call feedkeys(str, 'xt')
1188 call assert_equal("\"one two three four five six seven", @:)
1189endfunc
1190
1191" Test for moving the cursor on the / command line in 'rightleft' mode
1192func Test_cmdline_edit_rightleft()
1193 CheckFeature rightleft
1194 set rightleft
1195 set rightleftcmd=search
1196 let str = "/one two\<C-U>"
1197 let str ..= "one two\<C-W>\<C-W>"
1198 let str ..= "\<Right>five\<Left>"
1199 let str ..= "\<Home>two "
1200 let str ..= "\<C-Right>one "
1201 let str ..= "\<C-Left> three"
1202 let str ..= "\<End>\<S-Right>four "
1203 let str ..= "\<S-Left> six"
1204 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1205 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1206 call assert_equal("\"one two three four five six seven", @/)
1207 set rightleftcmd&
1208 set rightleft&
1209endfunc
1210
1211" Test for using <C-\>e in the command line to evaluate an expression
1212func Test_cmdline_expr()
1213 " Evaluate an expression from the beginning of a command line
1214 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1215 call assert_equal('"hello', @:)
1216
1217 " Use an invalid expression for <C-\>e
1218 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1219
1220 " Insert literal <CTRL-\> in the command line
1221 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1222 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001223endfunc
1224
Bram Moolenaar309976e2019-12-05 18:16:33 +01001225" vim: shiftwidth=2 sts=2 expandtab