blob: 4dca8ea6ea63c8b9dd8ddadf31d041dba6015fcd [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 Moolenaarb7e24832020-06-24 13:37:35 +0200395 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200396 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200397 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200398endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200399
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200400func Test_shellcmd_completion()
401 let save_path = $PATH
402
403 call mkdir('Xpathdir/Xpathsubdir', 'p')
404 call writefile([''], 'Xpathdir/Xfile.exe')
405 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
406
407 " Set PATH to example directory without trailing slash.
408 let $PATH = getcwd() . '/Xpathdir'
409
410 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
411 " dirs in the PATH, even though they won't be executed. We check that only
412 " subdirs of the PWD and executables from the PATH are included in the
413 " suggestions.
414 let actual = getcompletion('X', 'shellcmd')
415 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
416 call insert(expected, 'Xfile.exe')
417 call assert_equal(expected, actual)
418
419 call delete('Xpathdir', 'rf')
420 let $PATH = save_path
421endfunc
422
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200423func Test_expand_star_star()
424 call mkdir('a/b', 'p')
425 call writefile(['asdfasdf'], 'a/b/fileXname')
426 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
427 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200428 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200429 call delete('a', 'rf')
430endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100431
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100432func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100433 let @a = "def"
434 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
435 call assert_equal('"abc def ghi', @:)
436
437 new
438 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
439
440 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
441 call assert_equal('"aaa asdf bbb', @:)
442
443 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
444 call assert_equal('"aaa /tmp/some bbb', @:)
445
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200446 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
447 call assert_equal('"aaa '.getline(1).' bbb', @:)
448
Bram Moolenaar21efc362016-12-03 14:05:49 +0100449 set incsearch
450 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
451 call assert_equal('"aaa verylongword bbb', @:)
452
453 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
454 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100455
456 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
457 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100458 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200459
460 " Error while typing a command used to cause that it was not executed
461 " in the end.
462 new
463 try
464 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
465 catch /^Vim\%((\a\+)\)\=:E32/
466 " ignore error E32
467 endtry
468 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100469
Bram Moolenaar578fe942020-02-27 21:32:51 +0100470 " Try to paste an invalid register using <C-R>
471 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
472 call assert_equal('"onetwo', @:)
473
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100474 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100475 let @a = "xy\<C-H>z"
476 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
477 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100478 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
479 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100480 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
481 call assert_equal("\"xy\<C-H>z", @:)
482
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100483 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
484 let @a = "xy\<C-V>z"
485 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
486 call assert_equal('"xyz', @:)
487 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
488 call assert_equal("\"xy\<C-V>z", @:)
489
Bram Moolenaar578fe942020-02-27 21:32:51 +0100490 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
491
Bram Moolenaar72532d32018-04-07 19:09:09 +0200492 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100493endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100494
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100495func Test_cmdline_remove_char()
496 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100497
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100498 for e in ['utf8', 'latin1']
499 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100500
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100501 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
502 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100503
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100504 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
505 call assert_equal('"abcdef', @:)
506
507 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
508 call assert_equal('"abc ghi', @:, e)
509
510 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
511 call assert_equal('"def', @:, e)
512 endfor
513
514 let &encoding = encoding_save
515endfunc
516
517func Test_cmdline_keymap_ctrl_hat()
518 if !has('keymap')
519 return
520 endif
521
522 set keymap=esperanto
523 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
524 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
525 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100526endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100527
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100528func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100529 new
530 2;'(
531 2;')
532 quit
533endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100534
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100535func Test_illegal_address2()
536 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
537 new
538 source Xtest.vim
539 " Trigger calling validate_cursor()
540 diffsp Xtest.vim
541 quit!
542 bwipe!
543 call delete('Xtest.vim')
544endfunc
545
Bram Moolenaarba47b512017-01-24 21:18:19 +0100546func Test_cmdline_complete_wildoptions()
547 help
548 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
549 let a = join(sort(split(@:)),' ')
550 set wildoptions=tagfile
551 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
552 let b = join(sort(split(@:)),' ')
553 call assert_equal(a, b)
554 bw!
555endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100556
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200557func Test_cmdline_complete_user_cmd()
558 command! -complete=color -nargs=1 Foo :
559 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
560 call assert_equal('"Foo blue', @:)
561 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
562 call assert_equal('"Foo blue', @:)
563 delcommand Foo
564endfunc
565
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100566func s:ScriptLocalFunction()
567 echo 'yes'
568endfunc
569
570func Test_cmdline_complete_user_func()
571 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
572 call assert_match('"func Test_cmdline_complete_user', @:)
573 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
574 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
575endfunc
576
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200577func Test_cmdline_complete_user_names()
578 if has('unix') && executable('whoami')
579 let whoami = systemlist('whoami')[0]
580 let first_letter = whoami[0]
581 if len(first_letter) > 0
582 " Trying completion of :e ~x where x is the first letter of
583 " the user name should complete to at least the user name.
584 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
585 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
586 endif
587 endif
588 if has('win32')
589 " Just in case: check that the system has an Administrator account.
590 let names = system('net user')
591 if names =~ 'Administrator'
592 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100593 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200594 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100595 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200596 endif
597 endif
598endfunc
599
Bram Moolenaar297610b2019-12-27 17:20:55 +0100600func Test_cmdline_complete_bang()
601 if executable('whoami')
Bram Moolenaar731a7992019-12-28 14:06:50 +0100602 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
603 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100604 endif
Bram Moolenaar297610b2019-12-27 17:20:55 +0100605endfunc
606
Bram Moolenaar8b633132020-03-20 18:20:51 +0100607func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200608 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
609 call assert_equal(lang, v:lc_time)
610
611 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
612 call assert_equal(lang, v:ctype)
613
614 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
615 call assert_equal(lang, v:collate)
616
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200617 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200618 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200619
620 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200621 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200622
Bram Moolenaarec680282020-06-12 19:35:32 +0200623 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200624
Bram Moolenaarec680282020-06-12 19:35:32 +0200625 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
626 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200627
Bram Moolenaarec680282020-06-12 19:35:32 +0200628 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
629 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200630
Bram Moolenaarec680282020-06-12 19:35:32 +0200631 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
632 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200633
634 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
635 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200636endfunc
637
Bram Moolenaar297610b2019-12-27 17:20:55 +0100638func Test_cmdline_complete_env_variable()
639 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100640 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
641 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100642 unlet $X_VIM_TEST_COMPLETE_ENV
643endfunc
644
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100645" Test for various command-line completion
646func Test_cmdline_complete_various()
647 " completion for a command starting with a comment
648 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
649 call assert_equal("\" :|\"\<C-A>", @:)
650
651 " completion for a range followed by a comment
652 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
653 call assert_equal("\"1,2\"\<C-A>", @:)
654
655 " completion for :k command
656 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
657 call assert_equal("\"ka\<C-A>", @:)
658
659 " completion for short version of the :s command
660 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
661 call assert_equal("\"sI \<C-A>", @:)
662
663 " completion for :write command
664 call mkdir('Xdir')
665 call writefile(['one'], 'Xdir/Xfile1')
666 let save_cwd = getcwd()
667 cd Xdir
668 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
669 call assert_equal("\"w >> Xfile1", @:)
670 call chdir(save_cwd)
671 call delete('Xdir', 'rf')
672
673 " completion for :w ! and :r ! commands
674 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
675 call assert_equal("\"w !invalid_xyz_cmd", @:)
676 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
677 call assert_equal("\"r !invalid_xyz_cmd", @:)
678
679 " completion for :>> and :<< commands
680 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
681 call assert_equal("\">>>\<C-A>", @:)
682 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
683 call assert_equal("\"<<<\<C-A>", @:)
684
685 " completion for command with +cmd argument
686 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
687 call assert_equal("\"buffer +/pat Xabc", @:)
688 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
689 call assert_equal("\"buffer +/pat\<C-A>", @:)
690
691 " completion for a command with a trailing comment
692 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
693 call assert_equal("\"ls \" comment\<C-A>", @:)
694
695 " completion for a command with a trailing command
696 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
697 call assert_equal("\"ls | ls", @:)
698
699 " completion for a command with an CTRL-V escaped argument
700 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
701 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
702
703 " completion for a command that doesn't take additional arguments
704 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
705 call assert_equal("\"all abc\<C-A>", @:)
706
707 " completion for a command with a command modifier
708 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
709 call assert_equal("\"topleft new", @:)
710
711 " completion for the :match command
712 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
713 call assert_equal("\"match Search /pat/\<C-A>", @:)
714
715 " completion for the :s command
716 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
717 call assert_equal("\"s/from/to/g\<C-A>", @:)
718
719 " completion for the :dlist command
720 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
721 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
722
723 " completion for the :doautocmd command
724 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
725 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
726
727 " completion for the :augroup command
728 augroup XTest
729 augroup END
730 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
731 call assert_equal("\"augroup XTest", @:)
732 augroup! XTest
733
734 " completion for the :unlet command
735 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
736 call assert_equal("\"unlet one two", @:)
737
738 " completion for the :bdelete command
739 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
740 call assert_equal("\"bdel a b c", @:)
741
742 " completion for the :mapclear command
743 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
744 call assert_equal("\"mapclear <buffer>", @:)
745
746 " completion for user defined commands with menu names
747 menu Test.foo :ls<CR>
748 com -nargs=* -complete=menu MyCmd
749 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
750 call assert_equal('"MyCmd Test.', @:)
751 delcom MyCmd
752 unmenu Test
753
754 " completion for user defined commands with mappings
755 mapclear
756 map <F3> :ls<CR>
757 com -nargs=* -complete=mapping MyCmd
758 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
759 call assert_equal('"MyCmd <F3>', @:)
760 mapclear
761 delcom MyCmd
762
763 " completion for :set path= with multiple backslashes
764 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
765 call assert_equal('"set path=a\\\ b', @:)
766
767 " completion for :set dir= with a backslash
768 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
769 call assert_equal('"set dir=a\ b', @:)
770
771 " completion for the :py3 commands
772 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
773 call assert_equal('"py3 py3do py3file', @:)
774
775 " redir @" is not the start of a comment. So complete after that
776 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
777 call assert_equal('"redir @" | cwindow', @:)
778
779 " completion after a backtick
780 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
781 call assert_equal('"e `a1b2c', @:)
782
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100783 " completion for :language command with an invalid argument
784 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
785 call assert_equal("\"language dummy \t", @:)
786
787 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100788 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
789 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100790
791 " completion with ambiguous user defined commands
792 com TCmd1 echo 'TCmd1'
793 com TCmd2 echo 'TCmd2'
794 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
795 call assert_equal('"TCmd ', @:)
796 delcom TCmd1
797 delcom TCmd2
798
799 " completion after a range followed by a pipe (|) character
800 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
801 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100802endfunc
803
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200804func Test_cmdline_write_alternatefile()
805 new
806 call setline('.', ['one', 'two'])
807 f foo.txt
808 new
809 f #-A
810 call assert_equal('foo.txt-A', expand('%'))
811 f #<-B.txt
812 call assert_equal('foo-B.txt', expand('%'))
813 f %<
814 call assert_equal('foo-B', expand('%'))
815 new
816 call assert_fails('f #<', 'E95')
817 bw!
818 f foo-B.txt
819 f %<-A
820 call assert_equal('foo-B-A', expand('%'))
821 bw!
822 bw!
823endfunc
824
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100825" using a leading backslash here
826set cpo+=C
827
828func Test_cmdline_search_range()
829 new
830 call setline(1, ['a', 'b', 'c', 'd'])
831 /d
832 1,\/s/b/B/
833 call assert_equal('B', getline(2))
834
835 /a
836 $
837 \?,4s/c/C/
838 call assert_equal('C', getline(3))
839
840 call setline(1, ['a', 'b', 'c', 'd'])
841 %s/c/c/
842 1,\&s/b/B/
843 call assert_equal('B', getline(2))
844
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100845 let @/ = 'apple'
846 call assert_fails('\/print', 'E486:')
847
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100848 bwipe!
849endfunc
850
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100851" Test for the tick mark (') in an excmd range
852func Test_tick_mark_in_range()
853 " If only the tick is passed as a range and no command is specified, there
854 " should not be an error
855 call feedkeys(":'\<CR>", 'xt')
856 call assert_equal("'", getreg(':'))
857 call assert_fails("',print", 'E78:')
858endfunc
859
860" Test for using a line number followed by a search pattern as range
861func Test_lnum_and_pattern_as_range()
862 new
863 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
864 let @" = ''
865 2/foo/yank
866 call assert_equal("foo 3\n", @")
867 call assert_equal(1, line('.'))
868 close!
869endfunc
870
Bram Moolenaar65189a12017-02-06 22:22:17 +0100871" Tests for getcmdline(), getcmdpos() and getcmdtype()
872func Check_cmdline(cmdtype)
873 call assert_equal('MyCmd a', getcmdline())
874 call assert_equal(8, getcmdpos())
875 call assert_equal(a:cmdtype, getcmdtype())
876 return ''
877endfunc
878
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200879set cpo&
880
Bram Moolenaar65189a12017-02-06 22:22:17 +0100881func Test_getcmdtype()
882 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
883
884 let cmdtype = ''
885 debuggreedy
886 call feedkeys(":debug echo 'test'\<CR>", "t")
887 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
888 call feedkeys("cont\<CR>", "xt")
889 0debuggreedy
890 call assert_equal('>', cmdtype)
891
892 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
893 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
894
895 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100896 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100897
898 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
899
900 cnoremap <expr> <F6> Check_cmdline('=')
901 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
902 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100903
904 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100905endfunc
906
Bram Moolenaar81612b72018-06-23 14:55:03 +0200907func Test_getcmdwintype()
908 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
909 call assert_equal('/', a)
910
911 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
912 call assert_equal('?', a)
913
914 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
915 call assert_equal(':', a)
916
917 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
918 call assert_equal(':', a)
919
920 call assert_equal('', getcmdwintype())
921endfunc
922
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200923func Test_getcmdwin_autocmd()
924 let s:seq = []
925 augroup CmdWin
926 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
927 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
928 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
929 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
930 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
931 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
932
933 let org_winid = win_getid()
934 let org_bufnr = bufnr()
935 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
936 call assert_equal(':', a)
937 call assert_equal([
938 \ 'WinLeave ' .. org_winid,
939 \ 'WinEnter ' .. s:cmd_winid,
940 \ 'BufLeave ' .. org_bufnr,
941 \ 'BufEnter ' .. s:cmd_bufnr,
942 \ 'CmdWinEnter ' .. s:cmd_winid,
943 \ 'CmdWinLeave ' .. s:cmd_winid,
944 \ 'BufLeave ' .. s:cmd_bufnr,
945 \ 'WinLeave ' .. s:cmd_winid,
946 \ 'WinEnter ' .. org_winid,
947 \ 'BufEnter ' .. org_bufnr,
948 \ ], s:seq)
949
950 au!
951 augroup END
952endfunc
953
Bram Moolenaar52604f22017-04-07 16:17:39 +0200954func Test_verbosefile()
955 set verbosefile=Xlog
956 echomsg 'foo'
957 echomsg 'bar'
958 set verbosefile=
959 let log = readfile('Xlog')
960 call assert_match("foo\nbar", join(log, "\n"))
961 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200962 call mkdir('Xdir')
963 call assert_fails('set verbosefile=Xdir', 'E474:')
964 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +0200965endfunc
966
Bram Moolenaar4facea32019-10-12 20:17:40 +0200967func Test_verbose_option()
968 CheckScreendump
969
970 let lines =<< trim [SCRIPT]
971 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
972 call feedkeys("\r", 't') " for the hit-enter prompt
973 set verbose=20
974 [SCRIPT]
975 call writefile(lines, 'XTest_verbose')
976
977 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200978 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +0200979 call term_sendkeys(buf, ":DoSomething\<CR>")
980 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
981
982 " clean up
983 call StopVimInTerminal(buf)
984 call delete('XTest_verbose')
985endfunc
986
Bram Moolenaarff3be4f2018-05-12 13:18:46 +0200987func Test_setcmdpos()
988 func InsertTextAtPos(text, pos)
989 call assert_equal(0, setcmdpos(a:pos))
990 return a:text
991 endfunc
992
993 " setcmdpos() with position in the middle of the command line.
994 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
995 call assert_equal('"1ab2', @:)
996
997 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
998 call assert_equal('"1b2a', @:)
999
1000 " setcmdpos() with position beyond the end of the command line.
1001 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1002 call assert_equal('"12ab', @:)
1003
1004 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001005 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001006endfunc
1007
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001008func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001009 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001010 let encoding_save = &encoding
1011
1012 for e in encodings
1013 exe 'set encoding=' . e
1014
1015 " Test overstrike in the middle of the command line.
1016 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001017 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001018
1019 " Test overstrike going beyond end of command line.
1020 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001021 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001022
1023 " Test toggling insert/overstrike a few times.
1024 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001025 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001026 endfor
1027
Bram Moolenaar30276f22019-01-24 17:59:39 +01001028 " Test overstrike with multi-byte characters.
1029 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001030 call assert_equal('"abcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001031
1032 let &encoding = encoding_save
1033endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001034
1035func Test_cmdwin_bug()
1036 let winid = win_getid()
1037 sp
1038 try
1039 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1040 catch /^Vim\%((\a\+)\)\=:E11/
1041 endtry
1042 bw!
1043endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001044
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001045func Test_cmdwin_restore()
1046 CheckScreendump
1047
1048 let lines =<< trim [SCRIPT]
1049 call setline(1, range(30))
1050 2split
1051 [SCRIPT]
1052 call writefile(lines, 'XTest_restore')
1053
1054 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001055 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001056 call term_sendkeys(buf, "q:")
1057 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1058
1059 " normal restore
1060 call term_sendkeys(buf, ":q\<CR>")
1061 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1062
1063 " restore after setting 'lines' with one window
1064 call term_sendkeys(buf, ":close\<CR>")
1065 call term_sendkeys(buf, "q:")
1066 call term_sendkeys(buf, ":set lines=18\<CR>")
1067 call term_sendkeys(buf, ":q\<CR>")
1068 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1069
1070 " clean up
1071 call StopVimInTerminal(buf)
1072 call delete('XTest_restore')
1073endfunc
1074
Bram Moolenaar52410572019-10-27 05:12:45 +01001075func Test_buffers_lastused()
1076 " check that buffers are sorted by time when wildmode has lastused
1077 call test_settime(1550020000) " middle
1078 edit bufa
1079 enew
1080 call test_settime(1550030000) " newest
1081 edit bufb
1082 enew
1083 call test_settime(1550010000) " oldest
1084 edit bufc
1085 enew
1086 call test_settime(0)
1087 enew
1088
1089 call assert_equal(['bufa', 'bufb', 'bufc'],
1090 \ getcompletion('', 'buffer'))
1091
1092 let save_wildmode = &wildmode
1093 set wildmode=full:lastused
1094
1095 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1096 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1097 call assert_equal('b bufb', X)
1098 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1099 call assert_equal('b bufa', X)
1100 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1101 call assert_equal('b bufc', X)
1102 enew
1103
1104 edit other
1105 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1106 call assert_equal('b bufb', X)
1107 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1108 call assert_equal('b bufa', X)
1109 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1110 call assert_equal('b bufc', X)
1111 enew
1112
1113 let &wildmode = save_wildmode
1114
1115 bwipeout bufa
1116 bwipeout bufb
1117 bwipeout bufc
1118endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001119
1120func Test_cmdwin_feedkeys()
1121 " This should not generate E488
1122 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001123 " Using feedkeys with q: only should automatically close the cmd window
1124 call feedkeys('q:', 'xt')
1125 call assert_equal(1, winnr('$'))
1126 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001127endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001128
1129" Tests for the issues fixed in 7.4.441.
1130" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1131func Test_cmdwin_cedit()
1132 exe "set cedit=\<C-c>"
1133 normal! :
1134 call assert_equal(1, winnr('$'))
1135
1136 let g:cmd_wintype = ''
1137 func CmdWinType()
1138 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001139 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001140 return ''
1141 endfunc
1142
1143 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1144 echo input('')
1145 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001146 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001147
1148 set cedit&vim
1149 delfunc CmdWinType
1150endfunc
1151
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001152" Test for CmdwinEnter autocmd
1153func Test_cmdwin_autocmd()
1154 augroup CmdWin
1155 au!
1156 autocmd CmdwinEnter * startinsert
1157 augroup END
1158
1159 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1160 call assert_equal('xyz', @:)
1161
1162 augroup CmdWin
1163 au!
1164 augroup END
1165 augroup! CmdWin
1166endfunc
1167
Bram Moolenaar479950f2020-01-19 15:45:17 +01001168func Test_cmdlineclear_tabenter()
1169 CheckScreendump
1170
1171 let lines =<< trim [SCRIPT]
1172 call setline(1, range(30))
1173 [SCRIPT]
1174
1175 call writefile(lines, 'XtestCmdlineClearTabenter')
1176 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001177 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001178 " in one tab make the command line higher with CTRL-W -
1179 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1180 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1181
1182 call StopVimInTerminal(buf)
1183 call delete('XtestCmdlineClearTabenter')
1184endfunc
1185
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001186" Test for failure in expanding special keywords in cmdline
1187func Test_cmdline_expand_special()
1188 %bwipe!
1189 call assert_fails('e #', 'E499:')
1190 call assert_fails('e <afile>', 'E495:')
1191 call assert_fails('e <abuf>', 'E496:')
1192 call assert_fails('e <amatch>', 'E497:')
1193endfunc
1194
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001195func Test_cmdwin_jump_to_win()
1196 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1197 new
1198 set modified
1199 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', 'E162:')
1200 close!
1201 call feedkeys("q/:close\<CR>", "xt")
1202 call assert_equal(1, winnr('$'))
1203 call feedkeys("q/:exit\<CR>", "xt")
1204 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001205
1206 " opening command window twice should fail
1207 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1208 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001209endfunc
1210
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001211func Test_cmdwin_interrupted()
1212 CheckScreendump
1213
1214 " aborting the :smile output caused the cmdline window to use the current
1215 " buffer.
1216 let lines =<< trim [SCRIPT]
1217 au WinNew * smile
1218 [SCRIPT]
1219 call writefile(lines, 'XTest_cmdwin')
1220
1221 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001222 " open cmdwin
1223 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001224 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001225 " quit more prompt for :smile command
1226 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001227 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001228 " execute a simple command
1229 call term_sendkeys(buf, "aecho 'done'\<CR>")
1230 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1231
1232 " clean up
1233 call StopVimInTerminal(buf)
1234 call delete('XTest_cmdwin')
1235endfunc
1236
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001237" Test for backtick expression in the command line
1238func Test_cmd_backtick()
1239 %argd
1240 argadd `=['a', 'b', 'c']`
1241 call assert_equal(['a', 'b', 'c'], argv())
1242 %argd
1243endfunc
1244
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001245" Test for the :! command
1246func Test_cmd_bang()
1247 if !has('unix')
1248 return
1249 endif
1250
1251 let lines =<< trim [SCRIPT]
1252 " Test for no previous command
1253 call assert_fails('!!', 'E34:')
1254 set nomore
1255 " Test for cmdline expansion with :!
1256 call setline(1, 'foo!')
1257 silent !echo <cWORD> > Xfile.out
1258 call assert_equal(['foo!'], readfile('Xfile.out'))
1259 " Test for using previous command
1260 silent !echo \! !
1261 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1262 call writefile(v:errors, 'Xresult')
1263 call delete('Xfile.out')
1264 qall!
1265 [SCRIPT]
1266 call writefile(lines, 'Xscript')
1267 if RunVim([], [], '--clean -S Xscript')
1268 call assert_equal([], readfile('Xresult'))
1269 endif
1270 call delete('Xscript')
1271 call delete('Xresult')
1272endfunc
1273
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001274" Test error: "E135: *Filter* Autocommands must not change current buffer"
1275func Test_cmd_bang_E135()
1276 new
1277 call setline(1, ['a', 'b', 'c', 'd'])
1278 augroup test_cmd_filter_E135
1279 au!
1280 autocmd FilterReadPost * help
1281 augroup END
1282 call assert_fails('2,3!echo "x"', 'E135:')
1283
1284 augroup test_cmd_filter_E135
1285 au!
1286 augroup END
1287 %bwipe!
1288endfunc
1289
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001290" Test for using ~ for home directory in cmdline completion matches
1291func Test_cmdline_expand_home()
1292 call mkdir('Xdir')
1293 call writefile([], 'Xdir/Xfile1')
1294 call writefile([], 'Xdir/Xfile2')
1295 cd Xdir
1296 let save_HOME = $HOME
1297 let $HOME = getcwd()
1298 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1299 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1300 let $HOME = save_HOME
1301 cd ..
1302 call delete('Xdir', 'rf')
1303endfunc
1304
Bram Moolenaar578fe942020-02-27 21:32:51 +01001305" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1306" or insert mode (when 'insertmode' is set)
1307func Test_cmdline_ctrl_g()
1308 new
1309 call setline(1, 'abc')
1310 call cursor(1, 3)
1311 " If command line is entered from insert mode, using C-\ C-G should back to
1312 " insert mode
1313 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1314 call assert_equal('abxyc', getline(1))
1315 call assert_equal(4, col('.'))
1316
1317 " If command line is entered in 'insertmode', using C-\ C-G should back to
1318 " 'insertmode'
1319 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1320 call assert_equal('ab12xyc', getline(1))
1321 close!
1322endfunc
1323
Bram Moolenaar578fe942020-02-27 21:32:51 +01001324" Test for 'wildmode'
1325func Test_wildmode()
1326 func T(a, c, p)
1327 return "oneA\noneB\noneC"
1328 endfunc
1329 command -nargs=1 -complete=custom,T MyCmd
1330
1331 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001332 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001333 return ''
1334 endfunc
1335 cnoremap <expr> <F2> SaveScreenLine()
1336
1337 set nowildmenu
1338 set wildmode=full,list
1339 let g:Sline = ''
1340 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001341 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001342 call assert_equal('"MyCmd oneA', @:)
1343
1344 set wildmode=longest,full
1345 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1346 call assert_equal('"MyCmd one', @:)
1347 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1348 call assert_equal('"MyCmd oneC', @:)
1349
1350 set wildmode=longest
1351 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1352 call assert_equal('"MyCmd one', @:)
1353
1354 set wildmode=list:longest
1355 let g:Sline = ''
1356 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001357 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001358 call assert_equal('"MyCmd one', @:)
1359
1360 set wildmode=""
1361 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1362 call assert_equal('"MyCmd oneA', @:)
1363
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001364 " Test for wildmode=longest with 'fileignorecase' set
1365 set wildmode=longest
1366 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001367 argadd AAA AAAA AAAAA
1368 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1369 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001370 set fileignorecase&
1371
1372 " Test for listing files with wildmode=list
1373 set wildmode=list
1374 let g:Sline = ''
1375 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001376 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001377 call assert_equal('"b A', @:)
1378
1379 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001380 delcommand MyCmd
1381 delfunc T
1382 delfunc SaveScreenLine
1383 cunmap <F2>
1384 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001385 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001386endfunc
1387
1388" Test for interrupting the command-line completion
1389func Test_interrupt_compl()
1390 func F(lead, cmdl, p)
1391 if a:lead =~ 'tw'
1392 call interrupt()
1393 return
1394 endif
1395 return "one\ntwo\nthree"
1396 endfunc
1397 command -nargs=1 -complete=custom,F Tcmd
1398
1399 set nowildmenu
1400 set wildmode=full
1401 let interrupted = 0
1402 try
1403 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1404 catch /^Vim:Interrupt$/
1405 let interrupted = 1
1406 endtry
1407 call assert_equal(1, interrupted)
1408
1409 delcommand Tcmd
1410 delfunc F
1411 set wildmode&
1412endfunc
1413
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001414" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001415func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001416 let str = ":one two\<C-U>"
1417 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001418 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001419 let str ..= "\<Left>five\<Right>"
1420 let str ..= "\<Home>two "
1421 let str ..= "\<C-Left>one "
1422 let str ..= "\<C-Right> three"
1423 let str ..= "\<End>\<S-Left>four "
1424 let str ..= "\<S-Right> six"
1425 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1426 call feedkeys(str, 'xt')
1427 call assert_equal("\"one two three four five six seven", @:)
1428endfunc
1429
1430" Test for moving the cursor on the / command line in 'rightleft' mode
1431func Test_cmdline_edit_rightleft()
1432 CheckFeature rightleft
1433 set rightleft
1434 set rightleftcmd=search
1435 let str = "/one two\<C-U>"
1436 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001437 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001438 let str ..= "\<Right>five\<Left>"
1439 let str ..= "\<Home>two "
1440 let str ..= "\<C-Right>one "
1441 let str ..= "\<C-Left> three"
1442 let str ..= "\<End>\<S-Right>four "
1443 let str ..= "\<S-Left> six"
1444 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1445 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1446 call assert_equal("\"one two three four five six seven", @/)
1447 set rightleftcmd&
1448 set rightleft&
1449endfunc
1450
1451" Test for using <C-\>e in the command line to evaluate an expression
1452func Test_cmdline_expr()
1453 " Evaluate an expression from the beginning of a command line
1454 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1455 call assert_equal('"hello', @:)
1456
1457 " Use an invalid expression for <C-\>e
1458 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1459
1460 " Insert literal <CTRL-\> in the command line
1461 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1462 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001463endfunc
1464
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001465" Test for 'imcmdline' and 'imsearch'
1466" This test doesn't actually test the input method functionality.
1467func Test_cmdline_inputmethod()
1468 new
1469 call setline(1, ['', 'abc', ''])
1470 set imcmdline
1471
1472 call feedkeys(":\"abc\<CR>", 'xt')
1473 call assert_equal("\"abc", @:)
1474 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1475 call assert_equal("\"abc", @:)
1476 call feedkeys("/abc\<CR>", 'xt')
1477 call assert_equal([2, 1], [line('.'), col('.')])
1478 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1479 call assert_equal([2, 1], [line('.'), col('.')])
1480
1481 set imsearch=2
1482 call cursor(1, 1)
1483 call feedkeys("/abc\<CR>", 'xt')
1484 call assert_equal([2, 1], [line('.'), col('.')])
1485 call cursor(1, 1)
1486 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1487 call assert_equal([2, 1], [line('.'), col('.')])
1488 set imdisable
1489 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1490 call assert_equal([2, 1], [line('.'), col('.')])
1491 set imdisable&
1492 set imsearch&
1493
1494 set imcmdline&
1495 %bwipe!
1496endfunc
1497
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001498" Test for recursively getting multiple command line inputs
1499func Test_cmdwin_multi_input()
1500 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1501 call assert_equal('"cyan', @:)
1502endfunc
1503
1504" Test for using CTRL-_ in the command line with 'allowrevins'
1505func Test_cmdline_revins()
1506 CheckNotMSWindows
1507 CheckFeature rightleft
1508 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1509 call assert_equal("\"abc\<c-_>", @:)
1510 set allowrevins
1511 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1512 call assert_equal('"abcñèæ', @:)
1513 set allowrevins&
1514endfunc
1515
1516" Test for typing UTF-8 composing characters in the command line
1517func Test_cmdline_composing_chars()
1518 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1519 call assert_equal('"ゔ', @:)
1520endfunc
1521
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001522" Test for normal mode commands not supported in the cmd window
1523func Test_cmdwin_blocked_commands()
1524 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1525 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1526 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1527 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1528 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1529 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001530 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1531 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1532 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1533 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1534 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1535 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1536 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1537 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1538 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1539 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1540 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1541 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1542 call assert_fails('call feedkeys("q:\<C-W>x\<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>R\<CR>", "xt")', 'E11:')
1545 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1546 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1547 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1548 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1549 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1550 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001551endfunc
1552
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001553" Close the Cmd-line window in insert mode using CTRL-C
1554func Test_cmdwin_insert_mode_close()
1555 %bw!
1556 let s = ''
1557 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1558 call assert_equal('Hello', s)
1559 call assert_equal(1, winnr('$'))
1560endfunc
1561
Bram Moolenaar0e717042020-04-27 19:29:01 +02001562" test that ";" works to find a match at the start of the first line
1563func Test_zero_line_search()
1564 new
1565 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1566 call cursor(1,1)
1567 0;/pattern/d
1568 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1569 q!
1570endfunc
1571
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001572func Test_read_shellcmd()
1573 CheckUnix
1574 if executable('ls')
1575 " There should be ls in the $PATH
1576 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1577 call assert_match('^"r! .*\<ls\>', @:)
1578 endif
1579
1580 if executable('rm')
1581 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1582 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1583 call assert_match('^"r!.*\<rm\>', @:)
1584 endif
1585endfunc
1586
Bram Moolenaar0e717042020-04-27 19:29:01 +02001587
Bram Moolenaar309976e2019-12-05 18:16:33 +01001588" vim: shiftwidth=2 sts=2 expandtab