blob: a8222603f8e36bb3eaa6a75458d6798f1dd1ee14 [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')
94 call assert_equal('"map <A-Left>x', 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')
105 call assert_equal('"map <M', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200106 unmap <Left>
107
108 set cpo+=<
109 map <Left> left
110 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
111 call assert_equal('"map <Left>', getreg(':'))
112 unmap <Left>
113 set cpo-=<
114
115 set cpo+=B
116 map <Left> left
117 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
118 call assert_equal('"map <Left>', getreg(':'))
119 unmap <Left>
120 set cpo-=B
121
122 set cpo+=k
123 map <Left> left
124 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
125 call assert_equal('"map <Left>', getreg(':'))
126 unmap <Left>
127 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200128
129 unmap <Middle>x
130 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100131endfunc
132
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100133func Test_match_completion()
134 if !has('cmdline_compl')
135 return
136 endif
137 hi Aardig ctermfg=green
138 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
139 call assert_equal('"match Aardig', getreg(':'))
140 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
141 call assert_equal('"match none', getreg(':'))
142endfunc
143
144func Test_highlight_completion()
145 if !has('cmdline_compl')
146 return
147 endif
148 hi Aardig ctermfg=green
149 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
150 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200151 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
152 call assert_equal('"hi default Aardig', getreg(':'))
153 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
154 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100155 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
156 call assert_equal('"hi link', getreg(':'))
157 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
158 call assert_equal('"hi default', getreg(':'))
159 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
160 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200161
162 " A cleared group does not show up in completions.
163 hi Anders ctermfg=green
164 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
165 hi clear Aardig
166 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
167 hi clear Anders
168 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100169endfunc
170
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100171func Test_expr_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100172 if !has('cmdline_compl')
Bram Moolenaar2b2207b2017-01-22 16:46:56 +0100173 return
174 endif
175 for cmd in [
176 \ 'let a = ',
177 \ 'if',
178 \ 'elseif',
179 \ 'while',
180 \ 'for',
181 \ 'echo',
182 \ 'echon',
183 \ 'execute',
184 \ 'echomsg',
185 \ 'echoerr',
186 \ 'call',
187 \ 'return',
188 \ 'cexpr',
189 \ 'caddexpr',
190 \ 'cgetexpr',
191 \ 'lexpr',
192 \ 'laddexpr',
193 \ 'lgetexpr']
194 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
195 call assert_equal('"' . cmd . ' getline(', getreg(':'))
196 endfor
197endfunc
198
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200199func Test_getcompletion()
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200200 if !has('cmdline_compl')
201 return
202 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200203 let groupcount = len(getcompletion('', 'event'))
204 call assert_true(groupcount > 0)
205 let matchcount = len(getcompletion('File', 'event'))
206 call assert_true(matchcount > 0)
207 call assert_true(groupcount > matchcount)
208
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200209 if has('menu')
210 source $VIMRUNTIME/menu.vim
211 let matchcount = len(getcompletion('', 'menu'))
212 call assert_true(matchcount > 0)
213 call assert_equal(['File.'], getcompletion('File', 'menu'))
214 call assert_true(matchcount > 0)
215 let matchcount = len(getcompletion('File.', 'menu'))
216 call assert_true(matchcount > 0)
217 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200218
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200219 let l = getcompletion('v:n', 'var')
220 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200221 let l = getcompletion('v:notexists', 'var')
222 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200223
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200224 args a.c b.c
225 let l = getcompletion('', 'arglist')
226 call assert_equal(['a.c', 'b.c'], l)
227 %argdelete
228
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200229 let l = getcompletion('', 'augroup')
230 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200231 let l = getcompletion('blahblah', 'augroup')
232 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200233
234 let l = getcompletion('', 'behave')
235 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200236 let l = getcompletion('not', 'behave')
237 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200238
239 let l = getcompletion('', 'color')
240 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200241 let l = getcompletion('dirty', 'color')
242 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200243
244 let l = getcompletion('', 'command')
245 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200246 let l = getcompletion('awake', 'command')
247 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200248
249 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200250 call assert_true(index(l, 'samples/') >= 0)
251 let l = getcompletion('NoMatch', 'dir')
252 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200253
254 let l = getcompletion('exe', 'expression')
255 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200256 let l = getcompletion('kill', 'expression')
257 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200258
259 let l = getcompletion('tag', 'function')
260 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200261 let l = getcompletion('paint', 'function')
262 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200263
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200264 let Flambda = {-> 'hello'}
265 let l = getcompletion('', 'function')
266 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200267 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200268
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200269 let l = getcompletion('run', 'file')
270 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200271 let l = getcompletion('walk', 'file')
272 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200273 set wildignore=*.vim
274 let l = getcompletion('run', 'file', 1)
275 call assert_true(index(l, 'runtest.vim') < 0)
276 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200277
278 let l = getcompletion('ha', 'filetype')
279 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200280 let l = getcompletion('horse', 'filetype')
281 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200282
283 let l = getcompletion('z', 'syntax')
284 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200285 let l = getcompletion('emacs', 'syntax')
286 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200287
288 let l = getcompletion('jikes', 'compiler')
289 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200290 let l = getcompletion('break', 'compiler')
291 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200292
293 let l = getcompletion('last', 'help')
294 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200295 let l = getcompletion('giveup', 'help')
296 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200297
298 let l = getcompletion('time', 'option')
299 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200300 let l = getcompletion('space', 'option')
301 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200302
303 let l = getcompletion('er', 'highlight')
304 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200305 let l = getcompletion('dark', 'highlight')
306 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200307
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200308 let l = getcompletion('', 'messages')
309 call assert_true(index(l, 'clear') >= 0)
310 let l = getcompletion('not', 'messages')
311 call assert_equal([], l)
312
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200313 let l = getcompletion('', 'mapclear')
314 call assert_true(index(l, '<buffer>') >= 0)
315 let l = getcompletion('not', 'mapclear')
316 call assert_equal([], l)
317
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200318 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200319 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200320 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
321 let root = has('win32') ? 'C:\\' : '/'
322 let l = getcompletion(root, 'shellcmd')
323 let expected = map(filter(glob(root . '*', 0, 1),
324 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
325 call assert_equal(expected, l)
326
Bram Moolenaarb650b982016-08-05 20:35:13 +0200327 if has('cscope')
328 let l = getcompletion('', 'cscope')
329 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
330 call assert_equal(cmds, l)
331 " using cmdline completion must not change the result
332 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
333 let l = getcompletion('', 'cscope')
334 call assert_equal(cmds, l)
335 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
336 let l = getcompletion('find ', 'cscope')
337 call assert_equal(keys, l)
338 endif
339
Bram Moolenaar7522f692016-08-06 14:12:50 +0200340 if has('signs')
341 sign define Testing linehl=Comment
342 let l = getcompletion('', 'sign')
343 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
344 call assert_equal(cmds, l)
345 " using cmdline completion must not change the result
346 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
347 let l = getcompletion('', 'sign')
348 call assert_equal(cmds, l)
349 let l = getcompletion('list ', 'sign')
350 call assert_equal(['Testing'], l)
351 endif
352
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200353 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200354 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200355 if has('cmdline_hist')
356 call add(names, 'history')
357 endif
358 if has('gettext')
359 call add(names, 'locale')
360 endif
361 if has('profile')
362 call add(names, 'syntime')
363 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200364
365 set tags=Xtags
366 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
367
368 for name in names
369 let matchcount = len(getcompletion('', name))
370 call assert_true(matchcount >= 0, 'No matches for ' . name)
371 endfor
372
373 call delete('Xtags')
374
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200375 call assert_fails('call getcompletion("", "burp")', 'E475:')
376endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200377
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200378func Test_shellcmd_completion()
379 let save_path = $PATH
380
381 call mkdir('Xpathdir/Xpathsubdir', 'p')
382 call writefile([''], 'Xpathdir/Xfile.exe')
383 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
384
385 " Set PATH to example directory without trailing slash.
386 let $PATH = getcwd() . '/Xpathdir'
387
388 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
389 " dirs in the PATH, even though they won't be executed. We check that only
390 " subdirs of the PWD and executables from the PATH are included in the
391 " suggestions.
392 let actual = getcompletion('X', 'shellcmd')
393 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
394 call insert(expected, 'Xfile.exe')
395 call assert_equal(expected, actual)
396
397 call delete('Xpathdir', 'rf')
398 let $PATH = save_path
399endfunc
400
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200401func Test_expand_star_star()
402 call mkdir('a/b', 'p')
403 call writefile(['asdfasdf'], 'a/b/fileXname')
404 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
405 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200406 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200407 call delete('a', 'rf')
408endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100409
410func Test_paste_in_cmdline()
411 let @a = "def"
412 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
413 call assert_equal('"abc def ghi', @:)
414
415 new
416 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
417
418 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
419 call assert_equal('"aaa asdf bbb', @:)
420
421 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
422 call assert_equal('"aaa /tmp/some bbb', @:)
423
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200424 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
425 call assert_equal('"aaa '.getline(1).' bbb', @:)
426
Bram Moolenaar21efc362016-12-03 14:05:49 +0100427 set incsearch
428 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
429 call assert_equal('"aaa verylongword bbb', @:)
430
431 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
432 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100433
434 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
435 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100436 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200437
438 " Error while typing a command used to cause that it was not executed
439 " in the end.
440 new
441 try
442 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
443 catch /^Vim\%((\a\+)\)\=:E32/
444 " ignore error E32
445 endtry
446 call assert_equal("Xtestfile", bufname("%"))
447 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100448endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100449
450func Test_remove_char_in_cmdline()
451 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
452 call assert_equal('"abc ef', @:)
453
454 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
455 call assert_equal('"abcdef', @:)
456
457 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
458 call assert_equal('"abc ghi', @:)
459
460 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
461 call assert_equal('"def', @:)
462endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100463
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100464func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100465 new
466 2;'(
467 2;')
468 quit
469endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100470
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100471func Test_illegal_address2()
472 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
473 new
474 source Xtest.vim
475 " Trigger calling validate_cursor()
476 diffsp Xtest.vim
477 quit!
478 bwipe!
479 call delete('Xtest.vim')
480endfunc
481
Bram Moolenaarba47b512017-01-24 21:18:19 +0100482func Test_cmdline_complete_wildoptions()
483 help
484 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
485 let a = join(sort(split(@:)),' ')
486 set wildoptions=tagfile
487 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
488 let b = join(sort(split(@:)),' ')
489 call assert_equal(a, b)
490 bw!
491endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100492
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200493func Test_cmdline_complete_user_cmd()
494 command! -complete=color -nargs=1 Foo :
495 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
496 call assert_equal('"Foo blue', @:)
497 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
498 call assert_equal('"Foo blue', @:)
499 delcommand Foo
500endfunc
501
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200502func Test_cmdline_complete_user_names()
503 if has('unix') && executable('whoami')
504 let whoami = systemlist('whoami')[0]
505 let first_letter = whoami[0]
506 if len(first_letter) > 0
507 " Trying completion of :e ~x where x is the first letter of
508 " the user name should complete to at least the user name.
509 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
510 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
511 endif
512 endif
513 if has('win32')
514 " Just in case: check that the system has an Administrator account.
515 let names = system('net user')
516 if names =~ 'Administrator'
517 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100518 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200519 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100520 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200521 endif
522 endif
523endfunc
524
525funct Test_cmdline_complete_languages()
526 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
527
528 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
529 call assert_match('^"language .*\<ctype\>.*\<messages\>.*\<time\>', @:)
530
531 if has('unix')
532 " TODO: these tests don't work on Windows. lang appears to be 'C'
533 " but C does not appear in the completion. Why?
534 call assert_match('^"language .*\<' . lang . '\>', @:)
535
536 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
537 call assert_match('^"language .*\<' . lang . '\>', @:)
538
539 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
540 call assert_match('^"language .*\<' . lang . '\>', @:)
541
542 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
543 call assert_match('^"language .*\<' . lang . '\>', @:)
544 endif
545endfunc
546
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200547func Test_cmdline_write_alternatefile()
548 new
549 call setline('.', ['one', 'two'])
550 f foo.txt
551 new
552 f #-A
553 call assert_equal('foo.txt-A', expand('%'))
554 f #<-B.txt
555 call assert_equal('foo-B.txt', expand('%'))
556 f %<
557 call assert_equal('foo-B', expand('%'))
558 new
559 call assert_fails('f #<', 'E95')
560 bw!
561 f foo-B.txt
562 f %<-A
563 call assert_equal('foo-B-A', expand('%'))
564 bw!
565 bw!
566endfunc
567
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100568" using a leading backslash here
569set cpo+=C
570
571func Test_cmdline_search_range()
572 new
573 call setline(1, ['a', 'b', 'c', 'd'])
574 /d
575 1,\/s/b/B/
576 call assert_equal('B', getline(2))
577
578 /a
579 $
580 \?,4s/c/C/
581 call assert_equal('C', getline(3))
582
583 call setline(1, ['a', 'b', 'c', 'd'])
584 %s/c/c/
585 1,\&s/b/B/
586 call assert_equal('B', getline(2))
587
588 bwipe!
589endfunc
590
Bram Moolenaar65189a12017-02-06 22:22:17 +0100591" Tests for getcmdline(), getcmdpos() and getcmdtype()
592func Check_cmdline(cmdtype)
593 call assert_equal('MyCmd a', getcmdline())
594 call assert_equal(8, getcmdpos())
595 call assert_equal(a:cmdtype, getcmdtype())
596 return ''
597endfunc
598
599func Test_getcmdtype()
600 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
601
602 let cmdtype = ''
603 debuggreedy
604 call feedkeys(":debug echo 'test'\<CR>", "t")
605 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
606 call feedkeys("cont\<CR>", "xt")
607 0debuggreedy
608 call assert_equal('>', cmdtype)
609
610 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
611 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
612
613 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100614 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100615
616 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
617
618 cnoremap <expr> <F6> Check_cmdline('=')
619 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
620 cunmap <F6>
621endfunc
622
Bram Moolenaar81612b72018-06-23 14:55:03 +0200623func Test_getcmdwintype()
624 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
625 call assert_equal('/', a)
626
627 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
628 call assert_equal('?', a)
629
630 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
631 call assert_equal(':', a)
632
633 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
634 call assert_equal(':', a)
635
636 call assert_equal('', getcmdwintype())
637endfunc
638
Bram Moolenaar52604f22017-04-07 16:17:39 +0200639func Test_verbosefile()
640 set verbosefile=Xlog
641 echomsg 'foo'
642 echomsg 'bar'
643 set verbosefile=
644 let log = readfile('Xlog')
645 call assert_match("foo\nbar", join(log, "\n"))
646 call delete('Xlog')
647endfunc
648
Bram Moolenaarff3be4f2018-05-12 13:18:46 +0200649func Test_setcmdpos()
650 func InsertTextAtPos(text, pos)
651 call assert_equal(0, setcmdpos(a:pos))
652 return a:text
653 endfunc
654
655 " setcmdpos() with position in the middle of the command line.
656 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
657 call assert_equal('"1ab2', @:)
658
659 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
660 call assert_equal('"1b2a', @:)
661
662 " setcmdpos() with position beyond the end of the command line.
663 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
664 call assert_equal('"12ab', @:)
665
666 " setcmdpos() returns 1 when not editing the command line.
667 call assert_equal(1, setcmdpos(3))
668endfunc
669
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100670func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +0100671 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100672 let encoding_save = &encoding
673
674 for e in encodings
675 exe 'set encoding=' . e
676
677 " Test overstrike in the middle of the command line.
678 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
679 call assert_equal('"0ab1cd4', @:)
680
681 " Test overstrike going beyond end of command line.
682 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
683 call assert_equal('"0ab1cdefgh', @:)
684
685 " Test toggling insert/overstrike a few times.
686 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
687 call assert_equal('"ab0cd3ef4', @:)
688 endfor
689
Bram Moolenaar30276f22019-01-24 17:59:39 +0100690 " Test overstrike with multi-byte characters.
691 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
692 call assert_equal('"テabキcdエディタ', @:)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +0100693
694 let &encoding = encoding_save
695endfunc
696
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100697set cpo&