blob: 91a4b4e8450add3a40db8806a07dbd5ebb817a4e [file] [log] [blame]
Bram Moolenaarae3150e2016-06-11 23:22:36 +02001" Tests for editing the command line.
2
3func Test_complete_tab()
4 call writefile(['testfile'], 'Xtestfile')
5 call feedkeys(":e Xtest\t\r", "tx")
6 call assert_equal('testfile', getline(1))
7 call delete('Xtestfile')
8endfunc
9
10func Test_complete_list()
11 " We can't see the output, but at least we check the code runs properly.
12 call feedkeys(":e test\<C-D>\r", "tx")
13 call assert_equal('test', expand('%:t'))
14endfunc
15
16func Test_complete_wildmenu()
Bram Moolenaar37db6422019-03-28 21:26:23 +010017 call mkdir('Xdir1/Xdir2', 'p')
18 call writefile(['testfile1'], 'Xdir1/Xtestfile1')
19 call writefile(['testfile2'], 'Xdir1/Xtestfile2')
20 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3')
21 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020022 set wildmenu
Bram Moolenaar37db6422019-03-28 21:26:23 +010023
24 " Pressing <Tab> completes, and moves to next files when pressing again.
25 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx')
26 call assert_equal('testfile1', getline(1))
27 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020028 call assert_equal('testfile2', getline(1))
29
Bram Moolenaar37db6422019-03-28 21:26:23 +010030 " <S-Tab> is like <Tab> but begin with the last match and then go to
31 " previous.
32 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx')
33 call assert_equal('testfile2', getline(1))
34 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
35 call assert_equal('testfile1', getline(1))
36
37 " <Left>/<Right> to move to previous/next file.
38 call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx')
39 call assert_equal('testfile1', getline(1))
40 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
41 call assert_equal('testfile2', getline(1))
42 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
43 call assert_equal('testfile1', getline(1))
44
45 " <Up>/<Down> to go up/down directories.
46 call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx')
47 call assert_equal('testfile3', getline(1))
48 call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
49 call assert_equal('testfile1', getline(1))
50
51 " cleanup
52 %bwipe
53 call delete('Xdir1/Xdir2/Xtestfile4')
54 call delete('Xdir1/Xdir2/Xtestfile3')
55 call delete('Xdir1/Xtestfile2')
56 call delete('Xdir1/Xtestfile1')
57 call delete('Xdir1/Xdir2', 'd')
58 call delete('Xdir1', 'd')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020059 set nowildmenu
60endfunc
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020061
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +010062func Test_map_completion()
63 if !has('cmdline_compl')
64 return
65 endif
66 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
67 call assert_equal('"map <unique> <silent>', getreg(':'))
68 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
69 call assert_equal('"map <script> <unique>', getreg(':'))
70 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
71 call assert_equal('"map <expr> <script>', getreg(':'))
72 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
73 call assert_equal('"map <buffer> <expr>', getreg(':'))
74 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
75 call assert_equal('"map <nowait> <buffer>', getreg(':'))
76 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
77 call assert_equal('"map <special> <nowait>', getreg(':'))
78 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
79 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +020080
Bram Moolenaar1776a282019-05-03 16:05:41 +020081 map <Middle>x middle
82
Bram Moolenaar2cb9f022019-05-03 15:13:57 +020083 map ,f commaf
84 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +020085 map <Left> left
86 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +020087 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
88 call assert_equal('"map ,f', getreg(':'))
89 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
90 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +020091 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
92 call assert_equal('"map <Left>', getreg(':'))
93 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +020094 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +020095 unmap ,f
96 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +020097 unmap <Left>
98 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +020099
100 set cpo-=< cpo-=B cpo-=k
101 map <Left> left
102 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
103 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200104 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200105 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200106 unmap <Left>
107
108 set cpo+=<
109 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200110 exe "set t_k6=\<Esc>[17~"
111 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200112 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
113 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200114 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
115 call assert_equal("\"map <F6>x", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200116 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200117 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200118 set cpo-=<
119
120 set cpo+=B
121 map <Left> left
122 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
123 call assert_equal('"map <Left>', getreg(':'))
124 unmap <Left>
125 set cpo-=B
126
127 set cpo+=k
128 map <Left> left
129 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
130 call assert_equal('"map <Left>', getreg(':'))
131 unmap <Left>
132 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200133
134 unmap <Middle>x
135 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100136endfunc
137
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100138func Test_match_completion()
139 if !has('cmdline_compl')
140 return
141 endif
142 hi Aardig ctermfg=green
143 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
144 call assert_equal('"match Aardig', getreg(':'))
145 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
146 call assert_equal('"match none', getreg(':'))
147endfunc
148
149func Test_highlight_completion()
150 if !has('cmdline_compl')
151 return
152 endif
153 hi Aardig ctermfg=green
154 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
155 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200156 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
157 call assert_equal('"hi default Aardig', getreg(':'))
158 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
159 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100160 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
161 call assert_equal('"hi link', getreg(':'))
162 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
163 call assert_equal('"hi default', getreg(':'))
164 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
165 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200166
167 " A cleared group does not show up in completions.
168 hi Anders ctermfg=green
169 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
170 hi clear Aardig
171 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
172 hi clear Anders
173 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100174endfunc
175
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100176func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100177 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100178 return
179 endif
180 for cmd in [
181 \ 'let a = ',
182 \ 'if',
183 \ 'elseif',
184 \ 'while',
185 \ 'for',
186 \ 'echo',
187 \ 'echon',
188 \ 'execute',
189 \ 'echomsg',
190 \ 'echoerr',
191 \ 'call',
192 \ 'return',
193 \ 'cexpr',
194 \ 'caddexpr',
195 \ 'cgetexpr',
196 \ 'lexpr',
197 \ 'laddexpr',
198 \ 'lgetexpr']
199 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
200 call assert_equal('"' . cmd . ' getline(', getreg(':'))
201 endfor
202endfunc
203
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200204func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200205 if !has('cmdline_compl')
206 return
207 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200208 let groupcount = len(getcompletion('', 'event'))
209 call assert_true(groupcount > 0)
210 let matchcount = len(getcompletion('File', 'event'))
211 call assert_true(matchcount > 0)
212 call assert_true(groupcount > matchcount)
213
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200214 if has('menu')
215 source $VIMRUNTIME/menu.vim
216 let matchcount = len(getcompletion('', 'menu'))
217 call assert_true(matchcount > 0)
218 call assert_equal(['File.'], getcompletion('File', 'menu'))
219 call assert_true(matchcount > 0)
220 let matchcount = len(getcompletion('File.', 'menu'))
221 call assert_true(matchcount > 0)
222 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200223
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200224 let l = getcompletion('v:n', 'var')
225 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200226 let l = getcompletion('v:notexists', 'var')
227 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200228
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200229 args a.c b.c
230 let l = getcompletion('', 'arglist')
231 call assert_equal(['a.c', 'b.c'], l)
232 %argdelete
233
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200234 let l = getcompletion('', 'augroup')
235 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200236 let l = getcompletion('blahblah', 'augroup')
237 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200238
239 let l = getcompletion('', 'behave')
240 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200241 let l = getcompletion('not', 'behave')
242 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200243
244 let l = getcompletion('', 'color')
245 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200246 let l = getcompletion('dirty', 'color')
247 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200248
249 let l = getcompletion('', 'command')
250 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200251 let l = getcompletion('awake', 'command')
252 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200253
254 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200255 call assert_true(index(l, 'samples/') >= 0)
256 let l = getcompletion('NoMatch', 'dir')
257 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200258
259 let l = getcompletion('exe', 'expression')
260 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200261 let l = getcompletion('kill', 'expression')
262 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200263
264 let l = getcompletion('tag', 'function')
265 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200266 let l = getcompletion('paint', 'function')
267 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200268
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200269 let Flambda = {-> 'hello'}
270 let l = getcompletion('', 'function')
271 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200272 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200273
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200274 let l = getcompletion('run', 'file')
275 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200276 let l = getcompletion('walk', 'file')
277 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200278 set wildignore=*.vim
279 let l = getcompletion('run', 'file', 1)
280 call assert_true(index(l, 'runtest.vim') < 0)
281 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200282
283 let l = getcompletion('ha', 'filetype')
284 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200285 let l = getcompletion('horse', 'filetype')
286 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200287
288 let l = getcompletion('z', 'syntax')
289 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200290 let l = getcompletion('emacs', 'syntax')
291 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200292
293 let l = getcompletion('jikes', 'compiler')
294 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200295 let l = getcompletion('break', 'compiler')
296 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200297
298 let l = getcompletion('last', 'help')
299 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200300 let l = getcompletion('giveup', 'help')
301 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200302
303 let l = getcompletion('time', 'option')
304 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200305 let l = getcompletion('space', 'option')
306 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200307
308 let l = getcompletion('er', 'highlight')
309 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200310 let l = getcompletion('dark', 'highlight')
311 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200312
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200313 let l = getcompletion('', 'messages')
314 call assert_true(index(l, 'clear') >= 0)
315 let l = getcompletion('not', 'messages')
316 call assert_equal([], l)
317
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200318 let l = getcompletion('', 'mapclear')
319 call assert_true(index(l, '<buffer>') >= 0)
320 let l = getcompletion('not', 'mapclear')
321 call assert_equal([], l)
322
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200323 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200324 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200325 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
326 let root = has('win32') ? 'C:\\' : '/'
327 let l = getcompletion(root, 'shellcmd')
328 let expected = map(filter(glob(root . '*', 0, 1),
329 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
330 call assert_equal(expected, l)
331
Bram Moolenaarb650b982016-08-05 20:35:13 +0200332 if has('cscope')
333 let l = getcompletion('', 'cscope')
334 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
335 call assert_equal(cmds, l)
336 " using cmdline completion must not change the result
337 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
338 let l = getcompletion('', 'cscope')
339 call assert_equal(cmds, l)
340 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
341 let l = getcompletion('find ', 'cscope')
342 call assert_equal(keys, l)
343 endif
344
Bram Moolenaar7522f692016-08-06 14:12:50 +0200345 if has('signs')
346 sign define Testing linehl=Comment
347 let l = getcompletion('', 'sign')
348 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
349 call assert_equal(cmds, l)
350 " using cmdline completion must not change the result
351 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
352 let l = getcompletion('', 'sign')
353 call assert_equal(cmds, l)
354 let l = getcompletion('list ', 'sign')
355 call assert_equal(['Testing'], l)
356 endif
357
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200358 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200359 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200360 if has('cmdline_hist')
361 call add(names, 'history')
362 endif
363 if has('gettext')
364 call add(names, 'locale')
365 endif
366 if has('profile')
367 call add(names, 'syntime')
368 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200369
370 set tags=Xtags
371 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
372
373 for name in names
374 let matchcount = len(getcompletion('', name))
375 call assert_true(matchcount >= 0, 'No matches for ' . name)
376 endfor
377
378 call delete('Xtags')
379
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200380 call assert_fails('call getcompletion("", "burp")', 'E475:')
381endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200382
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200383func Test_shellcmd_completion()
384 let save_path = $PATH
385
386 call mkdir('Xpathdir/Xpathsubdir', 'p')
387 call writefile([''], 'Xpathdir/Xfile.exe')
388 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
389
390 " Set PATH to example directory without trailing slash.
391 let $PATH = getcwd() . '/Xpathdir'
392
393 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
394 " dirs in the PATH, even though they won't be executed. We check that only
395 " subdirs of the PWD and executables from the PATH are included in the
396 " suggestions.
397 let actual = getcompletion('X', 'shellcmd')
398 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
399 call insert(expected, 'Xfile.exe')
400 call assert_equal(expected, actual)
401
402 call delete('Xpathdir', 'rf')
403 let $PATH = save_path
404endfunc
405
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200406func Test_expand_star_star()
407 call mkdir('a/b', 'p')
408 call writefile(['asdfasdf'], 'a/b/fileXname')
409 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
410 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200411 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200412 call delete('a', 'rf')
413endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100414
415func Test_paste_in_cmdline()
416 let @a = "def"
417 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
418 call assert_equal('"abc def ghi', @:)
419
420 new
421 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
422
423 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
424 call assert_equal('"aaa asdf bbb', @:)
425
426 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
427 call assert_equal('"aaa /tmp/some bbb', @:)
428
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200429 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
430 call assert_equal('"aaa '.getline(1).' bbb', @:)
431
Bram Moolenaar21efc362016-12-03 14:05:49 +0100432 set incsearch
433 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
434 call assert_equal('"aaa verylongword bbb', @:)
435
436 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
437 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100438
439 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
440 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100441 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200442
443 " Error while typing a command used to cause that it was not executed
444 " in the end.
445 new
446 try
447 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
448 catch /^Vim\%((\a\+)\)\=:E32/
449 " ignore error E32
450 endtry
451 call assert_equal("Xtestfile", bufname("%"))
452 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100453endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100454
455func Test_remove_char_in_cmdline()
456 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
457 call assert_equal('"abc ef', @:)
458
459 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
460 call assert_equal('"abcdef', @:)
461
462 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
463 call assert_equal('"abc ghi', @:)
464
465 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
466 call assert_equal('"def', @:)
467endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100468
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100469func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100470 new
471 2;'(
472 2;')
473 quit
474endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100475
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100476func Test_illegal_address2()
477 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
478 new
479 source Xtest.vim
480 " Trigger calling validate_cursor()
481 diffsp Xtest.vim
482 quit!
483 bwipe!
484 call delete('Xtest.vim')
485endfunc
486
Bram Moolenaarba47b512017-01-24 21:18:19 +0100487func Test_cmdline_complete_wildoptions()
488 help
489 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
490 let a = join(sort(split(@:)),' ')
491 set wildoptions=tagfile
492 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
493 let b = join(sort(split(@:)),' ')
494 call assert_equal(a, b)
495 bw!
496endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100497
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200498func Test_cmdline_complete_user_cmd()
499 command! -complete=color -nargs=1 Foo :
500 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
501 call assert_equal('"Foo blue', @:)
502 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
503 call assert_equal('"Foo blue', @:)
504 delcommand Foo
505endfunc
506
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200507func Test_cmdline_complete_user_names()
508 if has('unix') && executable('whoami')
509 let whoami = systemlist('whoami')[0]
510 let first_letter = whoami[0]
511 if len(first_letter) > 0
512 " Trying completion of :e ~x where x is the first letter of
513 " the user name should complete to at least the user name.
514 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
515 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
516 endif
517 endif
518 if has('win32')
519 " Just in case: check that the system has an Administrator account.
520 let names = system('net user')
521 if names =~ 'Administrator'
522 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100523 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200524 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100525 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200526 endif
527 endif
528endfunc
529
530funct Test_cmdline_complete_languages()
531 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
532
533 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
534 call assert_match('^"language .*\<ctype\>.*\<messages\>.*\<time\>', @:)
535
536 if has('unix')
537 " TODO: these tests don't work on Windows. lang appears to be 'C'
538 " but C does not appear in the completion. Why?
539 call assert_match('^"language .*\<' . lang . '\>', @:)
540
541 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
542 call assert_match('^"language .*\<' . lang . '\>', @:)
543
544 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
545 call assert_match('^"language .*\<' . lang . '\>', @:)
546
547 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
548 call assert_match('^"language .*\<' . lang . '\>', @:)
549 endif
550endfunc
551
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200552func Test_cmdline_write_alternatefile()
553 new
554 call setline('.', ['one', 'two'])
555 f foo.txt
556 new
557 f #-A
558 call assert_equal('foo.txt-A', expand('%'))
559 f #<-B.txt
560 call assert_equal('foo-B.txt', expand('%'))
561 f %<
562 call assert_equal('foo-B', expand('%'))
563 new
564 call assert_fails('f #<', 'E95')
565 bw!
566 f foo-B.txt
567 f %<-A
568 call assert_equal('foo-B-A', expand('%'))
569 bw!
570 bw!
571endfunc
572
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100573" using a leading backslash here
574set cpo+=C
575
576func Test_cmdline_search_range()
577 new
578 call setline(1, ['a', 'b', 'c', 'd'])
579 /d
580 1,\/s/b/B/
581 call assert_equal('B', getline(2))
582
583 /a
584 $
585 \?,4s/c/C/
586 call assert_equal('C', getline(3))
587
588 call setline(1, ['a', 'b', 'c', 'd'])
589 %s/c/c/
590 1,\&s/b/B/
591 call assert_equal('B', getline(2))
592
593 bwipe!
594endfunc
595
Bram Moolenaar65189a12017-02-06 22:22:17 +0100596" Tests for getcmdline(), getcmdpos() and getcmdtype()
597func Check_cmdline(cmdtype)
598 call assert_equal('MyCmd a', getcmdline())
599 call assert_equal(8, getcmdpos())
600 call assert_equal(a:cmdtype, getcmdtype())
601 return ''
602endfunc
603
604func Test_getcmdtype()
605 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
606
607 let cmdtype = ''
608 debuggreedy
609 call feedkeys(":debug echo 'test'\<CR>", "t")
610 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
611 call feedkeys("cont\<CR>", "xt")
612 0debuggreedy
613 call assert_equal('>', cmdtype)
614
615 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
616 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
617
618 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100619 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100620
621 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
622
623 cnoremap <expr> <F6> Check_cmdline('=')
624 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
625 cunmap <F6>
626endfunc
627
Bram Moolenaar81612b72018-06-23 14:55:03 +0200628func Test_getcmdwintype()
629 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
630 call assert_equal('/', a)
631
632 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
633 call assert_equal('?', a)
634
635 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
636 call assert_equal(':', a)
637
638 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
639 call assert_equal(':', a)
640
641 call assert_equal('', getcmdwintype())
642endfunc
643
Bram Moolenaar52604f22017-04-07 16:17:39 +0200644func Test_verbosefile()
645 set verbosefile=Xlog
646 echomsg 'foo'
647 echomsg 'bar'
648 set verbosefile=
649 let log = readfile('Xlog')
650 call assert_match("foo\nbar", join(log, "\n"))
651 call delete('Xlog')
652endfunc
653
Bram Moolenaarff3be4f2018-05-12 13:18:46 +0200654func Test_setcmdpos()
655 func InsertTextAtPos(text, pos)
656 call assert_equal(0, setcmdpos(a:pos))
657 return a:text
658 endfunc
659
660 " setcmdpos() with position in the middle of the command line.
661 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
662 call assert_equal('"1ab2', @:)
663
664 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
665 call assert_equal('"1b2a', @:)
666
667 " setcmdpos() with position beyond the end of the command line.
668 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
669 call assert_equal('"12ab', @:)
670
671 " setcmdpos() returns 1 when not editing the command line.
672 call assert_equal(1, setcmdpos(3))
673endfunc
674
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100675func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100676 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100677 let encoding_save = &encoding
678
679 for e in encodings
680 exe 'set encoding=' . e
681
682 " Test overstrike in the middle of the command line.
683 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
684 call assert_equal('"0ab1cd4', @:)
685
686 " Test overstrike going beyond end of command line.
687 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
688 call assert_equal('"0ab1cdefgh', @:)
689
690 " Test toggling insert/overstrike a few times.
691 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
692 call assert_equal('"ab0cd3ef4', @:)
693 endfor
694
Bram Moolenaar30276f22019-01-24 17:59:39 +0100695 " Test overstrike with multi-byte characters.
696 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
697 call assert_equal('"テabキcdエディタ', @:)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100698
699 let &encoding = encoding_save
700endfunc
701
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100702set cpo&