blob: 5d772eacdcd2a2d1ffc680145ff663883dba3885 [file] [log] [blame]
Bram Moolenaarae3150e2016-06-11 23:22:36 +02001" Tests for editing the command line.
2
Bram Moolenaar4facea32019-10-12 20:17:40 +02003source check.vim
4source screendump.vim
Bram Moolenaar24ebd832020-03-16 21:25:24 +01005source view_util.vim
Bram Moolenaarc82dd862020-06-07 17:30:33 +02006source shared.vim
Bram Moolenaar4facea32019-10-12 20:17:40 +02007
Bram Moolenaarae3150e2016-06-11 23:22:36 +02008func Test_complete_tab()
9 call writefile(['testfile'], 'Xtestfile')
10 call feedkeys(":e Xtest\t\r", "tx")
11 call assert_equal('testfile', getline(1))
12 call delete('Xtestfile')
13endfunc
14
15func Test_complete_list()
16 " We can't see the output, but at least we check the code runs properly.
17 call feedkeys(":e test\<C-D>\r", "tx")
18 call assert_equal('test', expand('%:t'))
Bram Moolenaar578fe942020-02-27 21:32:51 +010019
20 " If a command doesn't support completion, then CTRL-D should be literally
21 " used.
22 call feedkeys(":chistory \<C-D>\<C-B>\"\<CR>", 'xt')
23 call assert_equal("\"chistory \<C-D>", @:)
Bram Moolenaarae3150e2016-06-11 23:22:36 +020024endfunc
25
26func Test_complete_wildmenu()
Bram Moolenaar37db6422019-03-28 21:26:23 +010027 call mkdir('Xdir1/Xdir2', 'p')
28 call writefile(['testfile1'], 'Xdir1/Xtestfile1')
29 call writefile(['testfile2'], 'Xdir1/Xtestfile2')
30 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3')
31 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020032 set wildmenu
Bram Moolenaar37db6422019-03-28 21:26:23 +010033
34 " Pressing <Tab> completes, and moves to next files when pressing again.
35 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx')
36 call assert_equal('testfile1', getline(1))
37 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020038 call assert_equal('testfile2', getline(1))
39
Bram Moolenaar37db6422019-03-28 21:26:23 +010040 " <S-Tab> is like <Tab> but begin with the last match and then go to
41 " previous.
42 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx')
43 call assert_equal('testfile2', getline(1))
44 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
45 call assert_equal('testfile1', getline(1))
46
47 " <Left>/<Right> to move to previous/next file.
48 call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx')
49 call assert_equal('testfile1', getline(1))
50 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
51 call assert_equal('testfile2', getline(1))
52 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
53 call assert_equal('testfile1', getline(1))
54
55 " <Up>/<Down> to go up/down directories.
56 call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx')
57 call assert_equal('testfile3', getline(1))
58 call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
59 call assert_equal('testfile1', getline(1))
60
Bram Moolenaar578fe942020-02-27 21:32:51 +010061 " Test for canceling the wild menu by adding a character
62 redrawstatus
63 call feedkeys(":e Xdir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
64 call assert_equal('"e Xdir1/Xdir2/x', @:)
65
Bram Moolenaar8d588cc2020-02-25 21:47:45 +010066 " Completion using a relative path
67 cd Xdir1/Xdir2
68 call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
69 call assert_equal('"e Xtestfile3 Xtestfile4', @:)
70 cd -
71
Bram Moolenaar0e05de42020-03-25 22:23:46 +010072 cnoremap <expr> <F2> wildmenumode()
73 call feedkeys(":cd Xdir\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
74 call assert_equal('"cd Xdir1/1', @:)
75 cunmap <F2>
76
Bram Moolenaar37db6422019-03-28 21:26:23 +010077 " cleanup
78 %bwipe
79 call delete('Xdir1/Xdir2/Xtestfile4')
80 call delete('Xdir1/Xdir2/Xtestfile3')
81 call delete('Xdir1/Xtestfile2')
82 call delete('Xdir1/Xtestfile1')
83 call delete('Xdir1/Xdir2', 'd')
84 call delete('Xdir1', 'd')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020085 set nowildmenu
86endfunc
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020087
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +010088func Test_map_completion()
89 if !has('cmdline_compl')
90 return
91 endif
92 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
93 call assert_equal('"map <unique> <silent>', getreg(':'))
94 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
95 call assert_equal('"map <script> <unique>', getreg(':'))
96 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
97 call assert_equal('"map <expr> <script>', getreg(':'))
98 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
99 call assert_equal('"map <buffer> <expr>', getreg(':'))
100 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
101 call assert_equal('"map <nowait> <buffer>', getreg(':'))
102 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
103 call assert_equal('"map <special> <nowait>', getreg(':'))
104 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
105 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200106
Bram Moolenaar1776a282019-05-03 16:05:41 +0200107 map <Middle>x middle
108
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200109 map ,f commaf
110 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200111 map <Left> left
112 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200113 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
114 call assert_equal('"map ,f', getreg(':'))
115 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
116 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200117 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
118 call assert_equal('"map <Left>', getreg(':'))
119 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200120 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200121 unmap ,f
122 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200123 unmap <Left>
124 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200125
126 set cpo-=< cpo-=B cpo-=k
127 map <Left> left
128 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
129 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200130 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200131 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200132 unmap <Left>
133
134 set cpo+=<
135 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200136 exe "set t_k6=\<Esc>[17~"
137 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200138 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
139 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200140 if !has('gui_running')
141 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
142 call assert_equal("\"map <F6>x", getreg(':'))
143 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200144 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200145 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200146 set cpo-=<
147
148 set cpo+=B
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-=B
154
155 set cpo+=k
156 map <Left> left
157 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
158 call assert_equal('"map <Left>', getreg(':'))
159 unmap <Left>
160 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200161
162 unmap <Middle>x
163 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100164endfunc
165
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100166func Test_match_completion()
167 if !has('cmdline_compl')
168 return
169 endif
170 hi Aardig ctermfg=green
171 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
172 call assert_equal('"match Aardig', getreg(':'))
173 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
174 call assert_equal('"match none', getreg(':'))
175endfunc
176
177func Test_highlight_completion()
178 if !has('cmdline_compl')
179 return
180 endif
181 hi Aardig ctermfg=green
182 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
183 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200184 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
185 call assert_equal('"hi default Aardig', getreg(':'))
186 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
187 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100188 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
189 call assert_equal('"hi link', getreg(':'))
190 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
191 call assert_equal('"hi default', getreg(':'))
192 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
193 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200194
195 " A cleared group does not show up in completions.
196 hi Anders ctermfg=green
197 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
198 hi clear Aardig
199 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
200 hi clear Anders
201 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100202endfunc
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)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200210 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200211 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 Moolenaar1f1fd442020-06-07 18:45:14 +0200358 " Command line completion tests
359 let l = getcompletion('cd ', 'cmdline')
360 call assert_true(index(l, 'samples/') >= 0)
361 let l = getcompletion('cd NoMatch', 'cmdline')
362 call assert_equal([], l)
363 let l = getcompletion('let v:n', 'cmdline')
364 call assert_true(index(l, 'v:null') >= 0)
365 let l = getcompletion('let v:notexists', 'cmdline')
366 call assert_equal([], l)
367 let l = getcompletion('call tag', 'cmdline')
368 call assert_true(index(l, 'taglist(') >= 0)
369 let l = getcompletion('call paint', 'cmdline')
370 call assert_equal([], l)
371
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200372 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200373 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200374 if has('cmdline_hist')
375 call add(names, 'history')
376 endif
377 if has('gettext')
378 call add(names, 'locale')
379 endif
380 if has('profile')
381 call add(names, 'syntime')
382 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200383
384 set tags=Xtags
385 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
386
387 for name in names
388 let matchcount = len(getcompletion('', name))
389 call assert_true(matchcount >= 0, 'No matches for ' . name)
390 endfor
391
392 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200393 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200394
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200395 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200396 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200397endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200398
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200399func Test_shellcmd_completion()
400 let save_path = $PATH
401
402 call mkdir('Xpathdir/Xpathsubdir', 'p')
403 call writefile([''], 'Xpathdir/Xfile.exe')
404 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
405
406 " Set PATH to example directory without trailing slash.
407 let $PATH = getcwd() . '/Xpathdir'
408
409 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
410 " dirs in the PATH, even though they won't be executed. We check that only
411 " subdirs of the PWD and executables from the PATH are included in the
412 " suggestions.
413 let actual = getcompletion('X', 'shellcmd')
414 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
415 call insert(expected, 'Xfile.exe')
416 call assert_equal(expected, actual)
417
418 call delete('Xpathdir', 'rf')
419 let $PATH = save_path
420endfunc
421
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200422func Test_expand_star_star()
423 call mkdir('a/b', 'p')
424 call writefile(['asdfasdf'], 'a/b/fileXname')
425 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
426 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200427 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200428 call delete('a', 'rf')
429endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100430
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100431func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100432 let @a = "def"
433 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
434 call assert_equal('"abc def ghi', @:)
435
436 new
437 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
438
439 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
440 call assert_equal('"aaa asdf bbb', @:)
441
442 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
443 call assert_equal('"aaa /tmp/some bbb', @:)
444
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200445 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
446 call assert_equal('"aaa '.getline(1).' bbb', @:)
447
Bram Moolenaar21efc362016-12-03 14:05:49 +0100448 set incsearch
449 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
450 call assert_equal('"aaa verylongword bbb', @:)
451
452 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
453 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100454
455 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
456 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100457 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200458
459 " Error while typing a command used to cause that it was not executed
460 " in the end.
461 new
462 try
463 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
464 catch /^Vim\%((\a\+)\)\=:E32/
465 " ignore error E32
466 endtry
467 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100468
Bram Moolenaar578fe942020-02-27 21:32:51 +0100469 " Try to paste an invalid register using <C-R>
470 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
471 call assert_equal('"onetwo', @:)
472
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100473 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100474 let @a = "xy\<C-H>z"
475 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
476 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100477 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
478 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100479 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
480 call assert_equal("\"xy\<C-H>z", @:)
481
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100482 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
483 let @a = "xy\<C-V>z"
484 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
485 call assert_equal('"xyz', @:)
486 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
487 call assert_equal("\"xy\<C-V>z", @:)
488
Bram Moolenaar578fe942020-02-27 21:32:51 +0100489 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
490
Bram Moolenaar72532d32018-04-07 19:09:09 +0200491 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100492endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100493
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100494func Test_cmdline_remove_char()
495 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100496
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100497 for e in ['utf8', 'latin1']
498 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100499
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100500 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
501 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100502
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100503 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
504 call assert_equal('"abcdef', @:)
505
506 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
507 call assert_equal('"abc ghi', @:, e)
508
509 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
510 call assert_equal('"def', @:, e)
511 endfor
512
513 let &encoding = encoding_save
514endfunc
515
516func Test_cmdline_keymap_ctrl_hat()
517 if !has('keymap')
518 return
519 endif
520
521 set keymap=esperanto
522 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
523 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
524 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100525endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100526
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100527func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100528 new
529 2;'(
530 2;')
531 quit
532endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100533
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100534func Test_illegal_address2()
535 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
536 new
537 source Xtest.vim
538 " Trigger calling validate_cursor()
539 diffsp Xtest.vim
540 quit!
541 bwipe!
542 call delete('Xtest.vim')
543endfunc
544
Bram Moolenaarba47b512017-01-24 21:18:19 +0100545func Test_cmdline_complete_wildoptions()
546 help
547 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
548 let a = join(sort(split(@:)),' ')
549 set wildoptions=tagfile
550 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
551 let b = join(sort(split(@:)),' ')
552 call assert_equal(a, b)
553 bw!
554endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100555
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200556func Test_cmdline_complete_user_cmd()
557 command! -complete=color -nargs=1 Foo :
558 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
559 call assert_equal('"Foo blue', @:)
560 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
561 call assert_equal('"Foo blue', @:)
562 delcommand Foo
563endfunc
564
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100565func s:ScriptLocalFunction()
566 echo 'yes'
567endfunc
568
569func Test_cmdline_complete_user_func()
570 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
571 call assert_match('"func Test_cmdline_complete_user', @:)
572 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
573 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
574endfunc
575
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200576func Test_cmdline_complete_user_names()
577 if has('unix') && executable('whoami')
578 let whoami = systemlist('whoami')[0]
579 let first_letter = whoami[0]
580 if len(first_letter) > 0
581 " Trying completion of :e ~x where x is the first letter of
582 " the user name should complete to at least the user name.
583 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
584 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
585 endif
586 endif
587 if has('win32')
588 " Just in case: check that the system has an Administrator account.
589 let names = system('net user')
590 if names =~ 'Administrator'
591 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100592 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200593 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100594 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200595 endif
596 endif
597endfunc
598
Bram Moolenaar297610b2019-12-27 17:20:55 +0100599func Test_cmdline_complete_bang()
600 if executable('whoami')
Bram Moolenaar731a7992019-12-28 14:06:50 +0100601 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
602 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100603 endif
Bram Moolenaar297610b2019-12-27 17:20:55 +0100604endfunc
605
Bram Moolenaar8b633132020-03-20 18:20:51 +0100606func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200607 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
608 call assert_equal(lang, v:lc_time)
609
610 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
611 call assert_equal(lang, v:ctype)
612
613 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
614 call assert_equal(lang, v:collate)
615
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200616 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200617 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200618
619 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200620 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200621
Bram Moolenaarec680282020-06-12 19:35:32 +0200622 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200623
Bram Moolenaarec680282020-06-12 19:35:32 +0200624 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
625 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200626
Bram Moolenaarec680282020-06-12 19:35:32 +0200627 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
628 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200629
Bram Moolenaarec680282020-06-12 19:35:32 +0200630 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
631 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200632
633 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
634 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200635endfunc
636
Bram Moolenaar297610b2019-12-27 17:20:55 +0100637func Test_cmdline_complete_env_variable()
638 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100639 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
640 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100641 unlet $X_VIM_TEST_COMPLETE_ENV
642endfunc
643
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100644" Test for various command-line completion
645func Test_cmdline_complete_various()
646 " completion for a command starting with a comment
647 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
648 call assert_equal("\" :|\"\<C-A>", @:)
649
650 " completion for a range followed by a comment
651 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
652 call assert_equal("\"1,2\"\<C-A>", @:)
653
654 " completion for :k command
655 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
656 call assert_equal("\"ka\<C-A>", @:)
657
658 " completion for short version of the :s command
659 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
660 call assert_equal("\"sI \<C-A>", @:)
661
662 " completion for :write command
663 call mkdir('Xdir')
664 call writefile(['one'], 'Xdir/Xfile1')
665 let save_cwd = getcwd()
666 cd Xdir
667 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
668 call assert_equal("\"w >> Xfile1", @:)
669 call chdir(save_cwd)
670 call delete('Xdir', 'rf')
671
672 " completion for :w ! and :r ! commands
673 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
674 call assert_equal("\"w !invalid_xyz_cmd", @:)
675 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
676 call assert_equal("\"r !invalid_xyz_cmd", @:)
677
678 " completion for :>> and :<< commands
679 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
680 call assert_equal("\">>>\<C-A>", @:)
681 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
682 call assert_equal("\"<<<\<C-A>", @:)
683
684 " completion for command with +cmd argument
685 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
686 call assert_equal("\"buffer +/pat Xabc", @:)
687 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
688 call assert_equal("\"buffer +/pat\<C-A>", @:)
689
690 " completion for a command with a trailing comment
691 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
692 call assert_equal("\"ls \" comment\<C-A>", @:)
693
694 " completion for a command with a trailing command
695 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
696 call assert_equal("\"ls | ls", @:)
697
698 " completion for a command with an CTRL-V escaped argument
699 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
700 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
701
702 " completion for a command that doesn't take additional arguments
703 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
704 call assert_equal("\"all abc\<C-A>", @:)
705
706 " completion for a command with a command modifier
707 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
708 call assert_equal("\"topleft new", @:)
709
710 " completion for the :match command
711 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
712 call assert_equal("\"match Search /pat/\<C-A>", @:)
713
714 " completion for the :s command
715 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
716 call assert_equal("\"s/from/to/g\<C-A>", @:)
717
718 " completion for the :dlist command
719 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
720 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
721
722 " completion for the :doautocmd command
723 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
724 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
725
726 " completion for the :augroup command
727 augroup XTest
728 augroup END
729 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
730 call assert_equal("\"augroup XTest", @:)
731 augroup! XTest
732
733 " completion for the :unlet command
734 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
735 call assert_equal("\"unlet one two", @:)
736
737 " completion for the :bdelete command
738 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
739 call assert_equal("\"bdel a b c", @:)
740
741 " completion for the :mapclear command
742 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
743 call assert_equal("\"mapclear <buffer>", @:)
744
745 " completion for user defined commands with menu names
746 menu Test.foo :ls<CR>
747 com -nargs=* -complete=menu MyCmd
748 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
749 call assert_equal('"MyCmd Test.', @:)
750 delcom MyCmd
751 unmenu Test
752
753 " completion for user defined commands with mappings
754 mapclear
755 map <F3> :ls<CR>
756 com -nargs=* -complete=mapping MyCmd
757 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
758 call assert_equal('"MyCmd <F3>', @:)
759 mapclear
760 delcom MyCmd
761
762 " completion for :set path= with multiple backslashes
763 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
764 call assert_equal('"set path=a\\\ b', @:)
765
766 " completion for :set dir= with a backslash
767 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
768 call assert_equal('"set dir=a\ b', @:)
769
770 " completion for the :py3 commands
771 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
772 call assert_equal('"py3 py3do py3file', @:)
773
774 " redir @" is not the start of a comment. So complete after that
775 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
776 call assert_equal('"redir @" | cwindow', @:)
777
778 " completion after a backtick
779 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
780 call assert_equal('"e `a1b2c', @:)
781
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100782 " completion for :language command with an invalid argument
783 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
784 call assert_equal("\"language dummy \t", @:)
785
786 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100787 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
788 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100789
790 " completion with ambiguous user defined commands
791 com TCmd1 echo 'TCmd1'
792 com TCmd2 echo 'TCmd2'
793 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
794 call assert_equal('"TCmd ', @:)
795 delcom TCmd1
796 delcom TCmd2
797
798 " completion after a range followed by a pipe (|) character
799 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
800 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100801endfunc
802
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200803func Test_cmdline_write_alternatefile()
804 new
805 call setline('.', ['one', 'two'])
806 f foo.txt
807 new
808 f #-A
809 call assert_equal('foo.txt-A', expand('%'))
810 f #<-B.txt
811 call assert_equal('foo-B.txt', expand('%'))
812 f %<
813 call assert_equal('foo-B', expand('%'))
814 new
815 call assert_fails('f #<', 'E95')
816 bw!
817 f foo-B.txt
818 f %<-A
819 call assert_equal('foo-B-A', expand('%'))
820 bw!
821 bw!
822endfunc
823
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100824" using a leading backslash here
825set cpo+=C
826
827func Test_cmdline_search_range()
828 new
829 call setline(1, ['a', 'b', 'c', 'd'])
830 /d
831 1,\/s/b/B/
832 call assert_equal('B', getline(2))
833
834 /a
835 $
836 \?,4s/c/C/
837 call assert_equal('C', getline(3))
838
839 call setline(1, ['a', 'b', 'c', 'd'])
840 %s/c/c/
841 1,\&s/b/B/
842 call assert_equal('B', getline(2))
843
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100844 let @/ = 'apple'
845 call assert_fails('\/print', 'E486:')
846
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100847 bwipe!
848endfunc
849
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100850" Test for the tick mark (') in an excmd range
851func Test_tick_mark_in_range()
852 " If only the tick is passed as a range and no command is specified, there
853 " should not be an error
854 call feedkeys(":'\<CR>", 'xt')
855 call assert_equal("'", getreg(':'))
856 call assert_fails("',print", 'E78:')
857endfunc
858
859" Test for using a line number followed by a search pattern as range
860func Test_lnum_and_pattern_as_range()
861 new
862 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
863 let @" = ''
864 2/foo/yank
865 call assert_equal("foo 3\n", @")
866 call assert_equal(1, line('.'))
867 close!
868endfunc
869
Bram Moolenaar65189a12017-02-06 22:22:17 +0100870" Tests for getcmdline(), getcmdpos() and getcmdtype()
871func Check_cmdline(cmdtype)
872 call assert_equal('MyCmd a', getcmdline())
873 call assert_equal(8, getcmdpos())
874 call assert_equal(a:cmdtype, getcmdtype())
875 return ''
876endfunc
877
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200878set cpo&
879
Bram Moolenaar65189a12017-02-06 22:22:17 +0100880func Test_getcmdtype()
881 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
882
883 let cmdtype = ''
884 debuggreedy
885 call feedkeys(":debug echo 'test'\<CR>", "t")
886 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
887 call feedkeys("cont\<CR>", "xt")
888 0debuggreedy
889 call assert_equal('>', cmdtype)
890
891 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
892 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
893
894 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100895 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100896
897 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
898
899 cnoremap <expr> <F6> Check_cmdline('=')
900 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
901 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100902
903 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100904endfunc
905
Bram Moolenaar81612b72018-06-23 14:55:03 +0200906func Test_getcmdwintype()
907 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
908 call assert_equal('/', a)
909
910 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
911 call assert_equal('?', a)
912
913 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
914 call assert_equal(':', a)
915
916 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
917 call assert_equal(':', a)
918
919 call assert_equal('', getcmdwintype())
920endfunc
921
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200922func Test_getcmdwin_autocmd()
923 let s:seq = []
924 augroup CmdWin
925 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
926 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
927 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
928 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
929 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
930 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
931
932 let org_winid = win_getid()
933 let org_bufnr = bufnr()
934 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
935 call assert_equal(':', a)
936 call assert_equal([
937 \ 'WinLeave ' .. org_winid,
938 \ 'WinEnter ' .. s:cmd_winid,
939 \ 'BufLeave ' .. org_bufnr,
940 \ 'BufEnter ' .. s:cmd_bufnr,
941 \ 'CmdWinEnter ' .. s:cmd_winid,
942 \ 'CmdWinLeave ' .. s:cmd_winid,
943 \ 'BufLeave ' .. s:cmd_bufnr,
944 \ 'WinLeave ' .. s:cmd_winid,
945 \ 'WinEnter ' .. org_winid,
946 \ 'BufEnter ' .. org_bufnr,
947 \ ], s:seq)
948
949 au!
950 augroup END
951endfunc
952
Bram Moolenaar52604f22017-04-07 16:17:39 +0200953func Test_verbosefile()
954 set verbosefile=Xlog
955 echomsg 'foo'
956 echomsg 'bar'
957 set verbosefile=
958 let log = readfile('Xlog')
959 call assert_match("foo\nbar", join(log, "\n"))
960 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200961 call mkdir('Xdir')
962 call assert_fails('set verbosefile=Xdir', 'E474:')
963 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +0200964endfunc
965
Bram Moolenaar4facea32019-10-12 20:17:40 +0200966func Test_verbose_option()
967 CheckScreendump
968
969 let lines =<< trim [SCRIPT]
970 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
971 call feedkeys("\r", 't') " for the hit-enter prompt
972 set verbose=20
973 [SCRIPT]
974 call writefile(lines, 'XTest_verbose')
975
976 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200977 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +0200978 call term_sendkeys(buf, ":DoSomething\<CR>")
979 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
980
981 " clean up
982 call StopVimInTerminal(buf)
983 call delete('XTest_verbose')
984endfunc
985
Bram Moolenaarff3be4f2018-05-12 13:18:46 +0200986func Test_setcmdpos()
987 func InsertTextAtPos(text, pos)
988 call assert_equal(0, setcmdpos(a:pos))
989 return a:text
990 endfunc
991
992 " setcmdpos() with position in the middle of the command line.
993 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
994 call assert_equal('"1ab2', @:)
995
996 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
997 call assert_equal('"1b2a', @:)
998
999 " setcmdpos() with position beyond the end of the command line.
1000 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1001 call assert_equal('"12ab', @:)
1002
1003 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001004 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001005endfunc
1006
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001007func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001008 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001009 let encoding_save = &encoding
1010
1011 for e in encodings
1012 exe 'set encoding=' . e
1013
1014 " Test overstrike in the middle of the command line.
1015 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001016 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001017
1018 " Test overstrike going beyond end of command line.
1019 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001020 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001021
1022 " Test toggling insert/overstrike a few times.
1023 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001024 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001025 endfor
1026
Bram Moolenaar30276f22019-01-24 17:59:39 +01001027 " Test overstrike with multi-byte characters.
1028 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001029 call assert_equal('"abcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001030
1031 let &encoding = encoding_save
1032endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001033
1034func Test_cmdwin_bug()
1035 let winid = win_getid()
1036 sp
1037 try
1038 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1039 catch /^Vim\%((\a\+)\)\=:E11/
1040 endtry
1041 bw!
1042endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001043
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001044func Test_cmdwin_restore()
1045 CheckScreendump
1046
1047 let lines =<< trim [SCRIPT]
1048 call setline(1, range(30))
1049 2split
1050 [SCRIPT]
1051 call writefile(lines, 'XTest_restore')
1052
1053 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001054 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001055 call term_sendkeys(buf, "q:")
1056 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1057
1058 " normal restore
1059 call term_sendkeys(buf, ":q\<CR>")
1060 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1061
1062 " restore after setting 'lines' with one window
1063 call term_sendkeys(buf, ":close\<CR>")
1064 call term_sendkeys(buf, "q:")
1065 call term_sendkeys(buf, ":set lines=18\<CR>")
1066 call term_sendkeys(buf, ":q\<CR>")
1067 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1068
1069 " clean up
1070 call StopVimInTerminal(buf)
1071 call delete('XTest_restore')
1072endfunc
1073
Bram Moolenaar52410572019-10-27 05:12:45 +01001074func Test_buffers_lastused()
1075 " check that buffers are sorted by time when wildmode has lastused
1076 call test_settime(1550020000) " middle
1077 edit bufa
1078 enew
1079 call test_settime(1550030000) " newest
1080 edit bufb
1081 enew
1082 call test_settime(1550010000) " oldest
1083 edit bufc
1084 enew
1085 call test_settime(0)
1086 enew
1087
1088 call assert_equal(['bufa', 'bufb', 'bufc'],
1089 \ getcompletion('', 'buffer'))
1090
1091 let save_wildmode = &wildmode
1092 set wildmode=full:lastused
1093
1094 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1095 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1096 call assert_equal('b bufb', X)
1097 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1098 call assert_equal('b bufa', X)
1099 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1100 call assert_equal('b bufc', X)
1101 enew
1102
1103 edit other
1104 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1105 call assert_equal('b bufb', X)
1106 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1107 call assert_equal('b bufa', X)
1108 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1109 call assert_equal('b bufc', X)
1110 enew
1111
1112 let &wildmode = save_wildmode
1113
1114 bwipeout bufa
1115 bwipeout bufb
1116 bwipeout bufc
1117endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001118
1119func Test_cmdwin_feedkeys()
1120 " This should not generate E488
1121 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001122 " Using feedkeys with q: only should automatically close the cmd window
1123 call feedkeys('q:', 'xt')
1124 call assert_equal(1, winnr('$'))
1125 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001126endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001127
1128" Tests for the issues fixed in 7.4.441.
1129" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1130func Test_cmdwin_cedit()
1131 exe "set cedit=\<C-c>"
1132 normal! :
1133 call assert_equal(1, winnr('$'))
1134
1135 let g:cmd_wintype = ''
1136 func CmdWinType()
1137 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001138 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001139 return ''
1140 endfunc
1141
1142 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1143 echo input('')
1144 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001145 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001146
1147 set cedit&vim
1148 delfunc CmdWinType
1149endfunc
1150
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001151" Test for CmdwinEnter autocmd
1152func Test_cmdwin_autocmd()
1153 augroup CmdWin
1154 au!
1155 autocmd CmdwinEnter * startinsert
1156 augroup END
1157
1158 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1159 call assert_equal('xyz', @:)
1160
1161 augroup CmdWin
1162 au!
1163 augroup END
1164 augroup! CmdWin
1165endfunc
1166
Bram Moolenaar479950f2020-01-19 15:45:17 +01001167func Test_cmdlineclear_tabenter()
1168 CheckScreendump
1169
1170 let lines =<< trim [SCRIPT]
1171 call setline(1, range(30))
1172 [SCRIPT]
1173
1174 call writefile(lines, 'XtestCmdlineClearTabenter')
1175 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001176 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001177 " in one tab make the command line higher with CTRL-W -
1178 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1179 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1180
1181 call StopVimInTerminal(buf)
1182 call delete('XtestCmdlineClearTabenter')
1183endfunc
1184
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001185" Test for failure in expanding special keywords in cmdline
1186func Test_cmdline_expand_special()
1187 %bwipe!
1188 call assert_fails('e #', 'E499:')
1189 call assert_fails('e <afile>', 'E495:')
1190 call assert_fails('e <abuf>', 'E496:')
1191 call assert_fails('e <amatch>', 'E497:')
1192endfunc
1193
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001194func Test_cmdwin_jump_to_win()
1195 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1196 new
1197 set modified
1198 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', 'E162:')
1199 close!
1200 call feedkeys("q/:close\<CR>", "xt")
1201 call assert_equal(1, winnr('$'))
1202 call feedkeys("q/:exit\<CR>", "xt")
1203 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001204
1205 " opening command window twice should fail
1206 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1207 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001208endfunc
1209
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001210func Test_cmdwin_interrupted()
1211 CheckScreendump
1212
1213 " aborting the :smile output caused the cmdline window to use the current
1214 " buffer.
1215 let lines =<< trim [SCRIPT]
1216 au WinNew * smile
1217 [SCRIPT]
1218 call writefile(lines, 'XTest_cmdwin')
1219
1220 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001221 " open cmdwin
1222 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001223 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001224 " quit more prompt for :smile command
1225 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001226 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001227 " execute a simple command
1228 call term_sendkeys(buf, "aecho 'done'\<CR>")
1229 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1230
1231 " clean up
1232 call StopVimInTerminal(buf)
1233 call delete('XTest_cmdwin')
1234endfunc
1235
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001236" Test for backtick expression in the command line
1237func Test_cmd_backtick()
1238 %argd
1239 argadd `=['a', 'b', 'c']`
1240 call assert_equal(['a', 'b', 'c'], argv())
1241 %argd
1242endfunc
1243
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001244" Test for the :! command
1245func Test_cmd_bang()
1246 if !has('unix')
1247 return
1248 endif
1249
1250 let lines =<< trim [SCRIPT]
1251 " Test for no previous command
1252 call assert_fails('!!', 'E34:')
1253 set nomore
1254 " Test for cmdline expansion with :!
1255 call setline(1, 'foo!')
1256 silent !echo <cWORD> > Xfile.out
1257 call assert_equal(['foo!'], readfile('Xfile.out'))
1258 " Test for using previous command
1259 silent !echo \! !
1260 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1261 call writefile(v:errors, 'Xresult')
1262 call delete('Xfile.out')
1263 qall!
1264 [SCRIPT]
1265 call writefile(lines, 'Xscript')
1266 if RunVim([], [], '--clean -S Xscript')
1267 call assert_equal([], readfile('Xresult'))
1268 endif
1269 call delete('Xscript')
1270 call delete('Xresult')
1271endfunc
1272
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001273" Test error: "E135: *Filter* Autocommands must not change current buffer"
1274func Test_cmd_bang_E135()
1275 new
1276 call setline(1, ['a', 'b', 'c', 'd'])
1277 augroup test_cmd_filter_E135
1278 au!
1279 autocmd FilterReadPost * help
1280 augroup END
1281 call assert_fails('2,3!echo "x"', 'E135:')
1282
1283 augroup test_cmd_filter_E135
1284 au!
1285 augroup END
1286 %bwipe!
1287endfunc
1288
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001289" Test for using ~ for home directory in cmdline completion matches
1290func Test_cmdline_expand_home()
1291 call mkdir('Xdir')
1292 call writefile([], 'Xdir/Xfile1')
1293 call writefile([], 'Xdir/Xfile2')
1294 cd Xdir
1295 let save_HOME = $HOME
1296 let $HOME = getcwd()
1297 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1298 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1299 let $HOME = save_HOME
1300 cd ..
1301 call delete('Xdir', 'rf')
1302endfunc
1303
Bram Moolenaar578fe942020-02-27 21:32:51 +01001304" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1305" or insert mode (when 'insertmode' is set)
1306func Test_cmdline_ctrl_g()
1307 new
1308 call setline(1, 'abc')
1309 call cursor(1, 3)
1310 " If command line is entered from insert mode, using C-\ C-G should back to
1311 " insert mode
1312 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1313 call assert_equal('abxyc', getline(1))
1314 call assert_equal(4, col('.'))
1315
1316 " If command line is entered in 'insertmode', using C-\ C-G should back to
1317 " 'insertmode'
1318 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1319 call assert_equal('ab12xyc', getline(1))
1320 close!
1321endfunc
1322
Bram Moolenaar578fe942020-02-27 21:32:51 +01001323" Test for 'wildmode'
1324func Test_wildmode()
1325 func T(a, c, p)
1326 return "oneA\noneB\noneC"
1327 endfunc
1328 command -nargs=1 -complete=custom,T MyCmd
1329
1330 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001331 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001332 return ''
1333 endfunc
1334 cnoremap <expr> <F2> SaveScreenLine()
1335
1336 set nowildmenu
1337 set wildmode=full,list
1338 let g:Sline = ''
1339 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001340 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001341 call assert_equal('"MyCmd oneA', @:)
1342
1343 set wildmode=longest,full
1344 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1345 call assert_equal('"MyCmd one', @:)
1346 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1347 call assert_equal('"MyCmd oneC', @:)
1348
1349 set wildmode=longest
1350 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1351 call assert_equal('"MyCmd one', @:)
1352
1353 set wildmode=list:longest
1354 let g:Sline = ''
1355 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001356 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001357 call assert_equal('"MyCmd one', @:)
1358
1359 set wildmode=""
1360 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1361 call assert_equal('"MyCmd oneA', @:)
1362
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001363 " Test for wildmode=longest with 'fileignorecase' set
1364 set wildmode=longest
1365 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001366 argadd AAA AAAA AAAAA
1367 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1368 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001369 set fileignorecase&
1370
1371 " Test for listing files with wildmode=list
1372 set wildmode=list
1373 let g:Sline = ''
1374 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001375 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001376 call assert_equal('"b A', @:)
1377
1378 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001379 delcommand MyCmd
1380 delfunc T
1381 delfunc SaveScreenLine
1382 cunmap <F2>
1383 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001384 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001385endfunc
1386
1387" Test for interrupting the command-line completion
1388func Test_interrupt_compl()
1389 func F(lead, cmdl, p)
1390 if a:lead =~ 'tw'
1391 call interrupt()
1392 return
1393 endif
1394 return "one\ntwo\nthree"
1395 endfunc
1396 command -nargs=1 -complete=custom,F Tcmd
1397
1398 set nowildmenu
1399 set wildmode=full
1400 let interrupted = 0
1401 try
1402 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1403 catch /^Vim:Interrupt$/
1404 let interrupted = 1
1405 endtry
1406 call assert_equal(1, interrupted)
1407
1408 delcommand Tcmd
1409 delfunc F
1410 set wildmode&
1411endfunc
1412
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001413" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001414func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001415 let str = ":one two\<C-U>"
1416 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001417 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001418 let str ..= "\<Left>five\<Right>"
1419 let str ..= "\<Home>two "
1420 let str ..= "\<C-Left>one "
1421 let str ..= "\<C-Right> three"
1422 let str ..= "\<End>\<S-Left>four "
1423 let str ..= "\<S-Right> six"
1424 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1425 call feedkeys(str, 'xt')
1426 call assert_equal("\"one two three four five six seven", @:)
1427endfunc
1428
1429" Test for moving the cursor on the / command line in 'rightleft' mode
1430func Test_cmdline_edit_rightleft()
1431 CheckFeature rightleft
1432 set rightleft
1433 set rightleftcmd=search
1434 let str = "/one two\<C-U>"
1435 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001436 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001437 let str ..= "\<Right>five\<Left>"
1438 let str ..= "\<Home>two "
1439 let str ..= "\<C-Right>one "
1440 let str ..= "\<C-Left> three"
1441 let str ..= "\<End>\<S-Right>four "
1442 let str ..= "\<S-Left> six"
1443 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1444 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1445 call assert_equal("\"one two three four five six seven", @/)
1446 set rightleftcmd&
1447 set rightleft&
1448endfunc
1449
1450" Test for using <C-\>e in the command line to evaluate an expression
1451func Test_cmdline_expr()
1452 " Evaluate an expression from the beginning of a command line
1453 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1454 call assert_equal('"hello', @:)
1455
1456 " Use an invalid expression for <C-\>e
1457 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1458
1459 " Insert literal <CTRL-\> in the command line
1460 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1461 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001462endfunc
1463
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001464" Test for 'imcmdline' and 'imsearch'
1465" This test doesn't actually test the input method functionality.
1466func Test_cmdline_inputmethod()
1467 new
1468 call setline(1, ['', 'abc', ''])
1469 set imcmdline
1470
1471 call feedkeys(":\"abc\<CR>", 'xt')
1472 call assert_equal("\"abc", @:)
1473 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1474 call assert_equal("\"abc", @:)
1475 call feedkeys("/abc\<CR>", 'xt')
1476 call assert_equal([2, 1], [line('.'), col('.')])
1477 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1478 call assert_equal([2, 1], [line('.'), col('.')])
1479
1480 set imsearch=2
1481 call cursor(1, 1)
1482 call feedkeys("/abc\<CR>", 'xt')
1483 call assert_equal([2, 1], [line('.'), col('.')])
1484 call cursor(1, 1)
1485 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1486 call assert_equal([2, 1], [line('.'), col('.')])
1487 set imdisable
1488 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1489 call assert_equal([2, 1], [line('.'), col('.')])
1490 set imdisable&
1491 set imsearch&
1492
1493 set imcmdline&
1494 %bwipe!
1495endfunc
1496
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001497" Test for recursively getting multiple command line inputs
1498func Test_cmdwin_multi_input()
1499 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1500 call assert_equal('"cyan', @:)
1501endfunc
1502
1503" Test for using CTRL-_ in the command line with 'allowrevins'
1504func Test_cmdline_revins()
1505 CheckNotMSWindows
1506 CheckFeature rightleft
1507 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1508 call assert_equal("\"abc\<c-_>", @:)
1509 set allowrevins
1510 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1511 call assert_equal('"abcñèæ', @:)
1512 set allowrevins&
1513endfunc
1514
1515" Test for typing UTF-8 composing characters in the command line
1516func Test_cmdline_composing_chars()
1517 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1518 call assert_equal('"ゔ', @:)
1519endfunc
1520
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001521" Test for normal mode commands not supported in the cmd window
1522func Test_cmdwin_blocked_commands()
1523 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1524 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1525 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1526 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1527 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1528 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001529 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1530 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1531 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1532 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1533 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1534 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1535 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1536 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1537 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1538 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1539 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1540 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1541 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1542 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1543 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1544 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1545 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1546 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1547 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1548 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1549 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001550endfunc
1551
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001552" Close the Cmd-line window in insert mode using CTRL-C
1553func Test_cmdwin_insert_mode_close()
1554 %bw!
1555 let s = ''
1556 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1557 call assert_equal('Hello', s)
1558 call assert_equal(1, winnr('$'))
1559endfunc
1560
Bram Moolenaar0e717042020-04-27 19:29:01 +02001561" test that ";" works to find a match at the start of the first line
1562func Test_zero_line_search()
1563 new
1564 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1565 call cursor(1,1)
1566 0;/pattern/d
1567 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1568 q!
1569endfunc
1570
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001571func Test_read_shellcmd()
1572 CheckUnix
1573 if executable('ls')
1574 " There should be ls in the $PATH
1575 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1576 call assert_match('^"r! .*\<ls\>', @:)
1577 endif
1578
1579 if executable('rm')
1580 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1581 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1582 call assert_match('^"r!.*\<rm\>', @:)
1583 endif
1584endfunc
1585
Bram Moolenaar0e717042020-04-27 19:29:01 +02001586
Bram Moolenaar309976e2019-12-05 18:16:33 +01001587" vim: shiftwidth=2 sts=2 expandtab