blob: 0127c29741bf83804b95d3897a030f989280172e [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 Moolenaarb0ac4ea2020-12-26 12:06:54 +010061 + " <C-J>/<C-K> mappings to go up/down directories when 'wildcharm' is
62 " different than 'wildchar'.
63 set wildcharm=<C-Z>
64 cnoremap <C-J> <Down><C-Z>
65 cnoremap <C-K> <Up><C-Z>
66 call feedkeys(":e Xdir1/\<Tab>\<C-J>\<CR>", 'tx')
67 call assert_equal('testfile3', getline(1))
68 call feedkeys(":e Xdir1/\<Tab>\<C-J>\<C-K>\<CR>", 'tx')
69 call assert_equal('testfile1', getline(1))
70 set wildcharm=0
71 cunmap <C-J>
72 cunmap <C-K>
73
Bram Moolenaar578fe942020-02-27 21:32:51 +010074 " Test for canceling the wild menu by adding a character
75 redrawstatus
76 call feedkeys(":e Xdir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
77 call assert_equal('"e Xdir1/Xdir2/x', @:)
78
Bram Moolenaar8d588cc2020-02-25 21:47:45 +010079 " Completion using a relative path
80 cd Xdir1/Xdir2
81 call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
82 call assert_equal('"e Xtestfile3 Xtestfile4', @:)
83 cd -
84
Bram Moolenaar0e05de42020-03-25 22:23:46 +010085 cnoremap <expr> <F2> wildmenumode()
86 call feedkeys(":cd Xdir\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
87 call assert_equal('"cd Xdir1/1', @:)
88 cunmap <F2>
89
Bram Moolenaar37db6422019-03-28 21:26:23 +010090 " cleanup
91 %bwipe
92 call delete('Xdir1/Xdir2/Xtestfile4')
93 call delete('Xdir1/Xdir2/Xtestfile3')
94 call delete('Xdir1/Xtestfile2')
95 call delete('Xdir1/Xtestfile1')
96 call delete('Xdir1/Xdir2', 'd')
97 call delete('Xdir1', 'd')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020098 set nowildmenu
99endfunc
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100100
Bram Moolenaara60053b2020-09-03 16:50:13 +0200101func Test_wildmenu_screendump()
102 CheckScreendump
103
104 let lines =<< trim [SCRIPT]
105 set wildmenu hlsearch
106 [SCRIPT]
107 call writefile(lines, 'XTest_wildmenu')
108
109 let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
110 call term_sendkeys(buf, ":vim\<Tab>")
111 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
112
113 call term_sendkeys(buf, "\<Tab>")
114 call VerifyScreenDump(buf, 'Test_wildmenu_2', {})
115
116 call term_sendkeys(buf, "\<Tab>")
117 call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
118
119 call term_sendkeys(buf, "\<Tab>")
120 call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
121 call term_sendkeys(buf, "\<Esc>")
122
123 " clean up
124 call StopVimInTerminal(buf)
125 call delete('XTest_wildmenu')
126endfunc
127
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100128func Test_map_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200129 CheckFeature cmdline_compl
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100130 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
131 call assert_equal('"map <unique> <silent>', getreg(':'))
132 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
133 call assert_equal('"map <script> <unique>', getreg(':'))
134 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
135 call assert_equal('"map <expr> <script>', getreg(':'))
136 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
137 call assert_equal('"map <buffer> <expr>', getreg(':'))
138 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
139 call assert_equal('"map <nowait> <buffer>', getreg(':'))
140 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
141 call assert_equal('"map <special> <nowait>', getreg(':'))
142 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
143 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200144
Bram Moolenaar1776a282019-05-03 16:05:41 +0200145 map <Middle>x middle
146
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200147 map ,f commaf
148 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200149 map <Left> left
150 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200151 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
152 call assert_equal('"map ,f', getreg(':'))
153 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
154 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200155 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
156 call assert_equal('"map <Left>', getreg(':'))
157 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200158 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200159 unmap ,f
160 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200161 unmap <Left>
162 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200163
164 set cpo-=< cpo-=B cpo-=k
165 map <Left> left
166 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
167 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200168 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200169 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200170 unmap <Left>
171
172 set cpo+=<
173 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200174 exe "set t_k6=\<Esc>[17~"
175 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200176 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
177 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200178 if !has('gui_running')
179 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
180 call assert_equal("\"map <F6>x", getreg(':'))
181 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200182 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200183 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200184 set cpo-=<
185
186 set cpo+=B
187 map <Left> left
188 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
189 call assert_equal('"map <Left>', getreg(':'))
190 unmap <Left>
191 set cpo-=B
192
193 set cpo+=k
194 map <Left> left
195 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
196 call assert_equal('"map <Left>', getreg(':'))
197 unmap <Left>
198 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200199
Bram Moolenaar531be472020-09-23 22:38:05 +0200200 call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
201
Bram Moolenaar1776a282019-05-03 16:05:41 +0200202 unmap <Middle>x
203 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100204endfunc
205
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100206func Test_match_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200207 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100208 hi Aardig ctermfg=green
209 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
210 call assert_equal('"match Aardig', getreg(':'))
211 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
212 call assert_equal('"match none', getreg(':'))
213endfunc
214
215func Test_highlight_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200216 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100217 hi Aardig ctermfg=green
218 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
219 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200220 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
221 call assert_equal('"hi default Aardig', getreg(':'))
222 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
223 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100224 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
225 call assert_equal('"hi link', getreg(':'))
226 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
227 call assert_equal('"hi default', getreg(':'))
228 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
229 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200230 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
231 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
232 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
233 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200234
235 " A cleared group does not show up in completions.
236 hi Anders ctermfg=green
237 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
238 hi clear Aardig
239 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
240 hi clear Anders
241 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100242endfunc
243
Bram Moolenaar75e15672020-06-28 13:10:22 +0200244" Test for command-line expansion of "hi Ni " (easter egg)
245func Test_highlight_easter_egg()
246 call test_override('ui_delay', 1)
247 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
248 call assert_equal("\"hi Ni \<Tab>", @:)
249 call test_override('ALL', 0)
250endfunc
251
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200252func Test_getcompletion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200253 CheckFeature cmdline_compl
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200254 let groupcount = len(getcompletion('', 'event'))
255 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200256 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200257 call assert_true(matchcount > 0)
258 call assert_true(groupcount > matchcount)
259
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200260 if has('menu')
261 source $VIMRUNTIME/menu.vim
262 let matchcount = len(getcompletion('', 'menu'))
263 call assert_true(matchcount > 0)
264 call assert_equal(['File.'], getcompletion('File', 'menu'))
265 call assert_true(matchcount > 0)
266 let matchcount = len(getcompletion('File.', 'menu'))
267 call assert_true(matchcount > 0)
268 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200269
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200270 let l = getcompletion('v:n', 'var')
271 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200272 let l = getcompletion('v:notexists', 'var')
273 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200274
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200275 args a.c b.c
276 let l = getcompletion('', 'arglist')
277 call assert_equal(['a.c', 'b.c'], l)
278 %argdelete
279
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200280 let l = getcompletion('', 'augroup')
281 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200282 let l = getcompletion('blahblah', 'augroup')
283 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200284
285 let l = getcompletion('', 'behave')
286 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200287 let l = getcompletion('not', 'behave')
288 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200289
290 let l = getcompletion('', 'color')
291 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200292 let l = getcompletion('dirty', 'color')
293 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200294
295 let l = getcompletion('', 'command')
296 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200297 let l = getcompletion('awake', 'command')
298 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200299
300 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200301 call assert_true(index(l, 'samples/') >= 0)
302 let l = getcompletion('NoMatch', 'dir')
303 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200304
305 let l = getcompletion('exe', 'expression')
306 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200307 let l = getcompletion('kill', 'expression')
308 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200309
310 let l = getcompletion('tag', 'function')
311 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200312 let l = getcompletion('paint', 'function')
313 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200314
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200315 let Flambda = {-> 'hello'}
316 let l = getcompletion('', 'function')
317 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200318 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200319
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200320 let l = getcompletion('run', 'file')
321 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200322 let l = getcompletion('walk', 'file')
323 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200324 set wildignore=*.vim
325 let l = getcompletion('run', 'file', 1)
326 call assert_true(index(l, 'runtest.vim') < 0)
327 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200328
329 let l = getcompletion('ha', 'filetype')
330 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200331 let l = getcompletion('horse', 'filetype')
332 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200333
334 let l = getcompletion('z', 'syntax')
335 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200336 let l = getcompletion('emacs', 'syntax')
337 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200338
339 let l = getcompletion('jikes', 'compiler')
340 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200341 let l = getcompletion('break', 'compiler')
342 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200343
344 let l = getcompletion('last', 'help')
345 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200346 let l = getcompletion('giveup', 'help')
347 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200348
349 let l = getcompletion('time', 'option')
350 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200351 let l = getcompletion('space', 'option')
352 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200353
354 let l = getcompletion('er', 'highlight')
355 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200356 let l = getcompletion('dark', 'highlight')
357 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200358
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200359 let l = getcompletion('', 'messages')
360 call assert_true(index(l, 'clear') >= 0)
361 let l = getcompletion('not', 'messages')
362 call assert_equal([], l)
363
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200364 let l = getcompletion('', 'mapclear')
365 call assert_true(index(l, '<buffer>') >= 0)
366 let l = getcompletion('not', 'mapclear')
367 call assert_equal([], l)
368
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200369 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200370 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200371 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
372 let root = has('win32') ? 'C:\\' : '/'
373 let l = getcompletion(root, 'shellcmd')
374 let expected = map(filter(glob(root . '*', 0, 1),
375 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
376 call assert_equal(expected, l)
377
Bram Moolenaarb650b982016-08-05 20:35:13 +0200378 if has('cscope')
379 let l = getcompletion('', 'cscope')
380 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
381 call assert_equal(cmds, l)
382 " using cmdline completion must not change the result
383 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
384 let l = getcompletion('', 'cscope')
385 call assert_equal(cmds, l)
386 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
387 let l = getcompletion('find ', 'cscope')
388 call assert_equal(keys, l)
389 endif
390
Bram Moolenaar7522f692016-08-06 14:12:50 +0200391 if has('signs')
392 sign define Testing linehl=Comment
393 let l = getcompletion('', 'sign')
394 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
395 call assert_equal(cmds, l)
396 " using cmdline completion must not change the result
397 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
398 let l = getcompletion('', 'sign')
399 call assert_equal(cmds, l)
400 let l = getcompletion('list ', 'sign')
401 call assert_equal(['Testing'], l)
402 endif
403
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200404 " Command line completion tests
405 let l = getcompletion('cd ', 'cmdline')
406 call assert_true(index(l, 'samples/') >= 0)
407 let l = getcompletion('cd NoMatch', 'cmdline')
408 call assert_equal([], l)
409 let l = getcompletion('let v:n', 'cmdline')
410 call assert_true(index(l, 'v:null') >= 0)
411 let l = getcompletion('let v:notexists', 'cmdline')
412 call assert_equal([], l)
413 let l = getcompletion('call tag', 'cmdline')
414 call assert_true(index(l, 'taglist(') >= 0)
415 let l = getcompletion('call paint', 'cmdline')
416 call assert_equal([], l)
417
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200418 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200419 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200420 if has('cmdline_hist')
421 call add(names, 'history')
422 endif
423 if has('gettext')
424 call add(names, 'locale')
425 endif
426 if has('profile')
427 call add(names, 'syntime')
428 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200429
430 set tags=Xtags
431 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
432
433 for name in names
434 let matchcount = len(getcompletion('', name))
435 call assert_true(matchcount >= 0, 'No matches for ' . name)
436 endfor
437
438 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200439 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200440
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200441 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200442 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200443 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200444endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200445
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200446func Test_shellcmd_completion()
447 let save_path = $PATH
448
449 call mkdir('Xpathdir/Xpathsubdir', 'p')
450 call writefile([''], 'Xpathdir/Xfile.exe')
451 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
452
453 " Set PATH to example directory without trailing slash.
454 let $PATH = getcwd() . '/Xpathdir'
455
456 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
457 " dirs in the PATH, even though they won't be executed. We check that only
458 " subdirs of the PWD and executables from the PATH are included in the
459 " suggestions.
460 let actual = getcompletion('X', 'shellcmd')
461 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
462 call insert(expected, 'Xfile.exe')
463 call assert_equal(expected, actual)
464
465 call delete('Xpathdir', 'rf')
466 let $PATH = save_path
467endfunc
468
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200469func Test_expand_star_star()
470 call mkdir('a/b', 'p')
471 call writefile(['asdfasdf'], 'a/b/fileXname')
472 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
473 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200474 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200475 call delete('a', 'rf')
476endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100477
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100478func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100479 let @a = "def"
480 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
481 call assert_equal('"abc def ghi', @:)
482
483 new
484 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
485
486 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
487 call assert_equal('"aaa asdf bbb', @:)
488
489 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
490 call assert_equal('"aaa /tmp/some bbb', @:)
491
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200492 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
493 call assert_equal('"aaa '.getline(1).' bbb', @:)
494
Bram Moolenaar21efc362016-12-03 14:05:49 +0100495 set incsearch
496 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
497 call assert_equal('"aaa verylongword bbb', @:)
498
499 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
500 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100501
502 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
503 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100504 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200505
506 " Error while typing a command used to cause that it was not executed
507 " in the end.
508 new
509 try
510 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
511 catch /^Vim\%((\a\+)\)\=:E32/
512 " ignore error E32
513 endtry
514 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100515
Bram Moolenaar578fe942020-02-27 21:32:51 +0100516 " Try to paste an invalid register using <C-R>
517 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
518 call assert_equal('"onetwo', @:)
519
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100520 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100521 let @a = "xy\<C-H>z"
522 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
523 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100524 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
525 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100526 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
527 call assert_equal("\"xy\<C-H>z", @:)
528
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100529 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
530 let @a = "xy\<C-V>z"
531 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
532 call assert_equal('"xyz', @:)
533 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
534 call assert_equal("\"xy\<C-V>z", @:)
535
Bram Moolenaar578fe942020-02-27 21:32:51 +0100536 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
537
Bram Moolenaar72532d32018-04-07 19:09:09 +0200538 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100539endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100540
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100541func Test_cmdline_remove_char()
542 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100543
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100544 for e in ['utf8', 'latin1']
545 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100546
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100547 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
548 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100549
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100550 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
551 call assert_equal('"abcdef', @:)
552
553 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
554 call assert_equal('"abc ghi', @:, e)
555
556 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
557 call assert_equal('"def', @:, e)
558 endfor
559
560 let &encoding = encoding_save
561endfunc
562
563func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200564 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100565
566 set keymap=esperanto
567 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
568 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
569 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100570endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100571
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100572func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100573 new
574 2;'(
575 2;')
576 quit
577endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100578
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100579func Test_illegal_address2()
580 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
581 new
582 source Xtest.vim
583 " Trigger calling validate_cursor()
584 diffsp Xtest.vim
585 quit!
586 bwipe!
587 call delete('Xtest.vim')
588endfunc
589
Bram Moolenaarba47b512017-01-24 21:18:19 +0100590func Test_cmdline_complete_wildoptions()
591 help
592 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
593 let a = join(sort(split(@:)),' ')
594 set wildoptions=tagfile
595 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
596 let b = join(sort(split(@:)),' ')
597 call assert_equal(a, b)
598 bw!
599endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100600
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200601func Test_cmdline_complete_user_cmd()
602 command! -complete=color -nargs=1 Foo :
603 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
604 call assert_equal('"Foo blue', @:)
605 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
606 call assert_equal('"Foo blue', @:)
607 delcommand Foo
608endfunc
609
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100610func s:ScriptLocalFunction()
611 echo 'yes'
612endfunc
613
614func Test_cmdline_complete_user_func()
615 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
616 call assert_match('"func Test_cmdline_complete_user', @:)
617 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
618 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
619endfunc
620
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200621func Test_cmdline_complete_user_names()
622 if has('unix') && executable('whoami')
623 let whoami = systemlist('whoami')[0]
624 let first_letter = whoami[0]
625 if len(first_letter) > 0
626 " Trying completion of :e ~x where x is the first letter of
627 " the user name should complete to at least the user name.
628 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
629 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
630 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200631 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200632 " Just in case: check that the system has an Administrator account.
633 let names = system('net user')
634 if names =~ 'Administrator'
635 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100636 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200637 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100638 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200639 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200640 else
641 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200642 endif
643endfunc
644
Bram Moolenaar297610b2019-12-27 17:20:55 +0100645func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200646 CheckExecutable whoami
647 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
648 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100649endfunc
650
Bram Moolenaar8b633132020-03-20 18:20:51 +0100651func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200652 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
653 call assert_equal(lang, v:lc_time)
654
655 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
656 call assert_equal(lang, v:ctype)
657
658 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
659 call assert_equal(lang, v:collate)
660
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200661 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200662 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200663
664 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200665 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200666
Bram Moolenaarec680282020-06-12 19:35:32 +0200667 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200668
Bram Moolenaarec680282020-06-12 19:35:32 +0200669 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
670 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200671
Bram Moolenaarec680282020-06-12 19:35:32 +0200672 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
673 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200674
Bram Moolenaarec680282020-06-12 19:35:32 +0200675 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
676 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200677
678 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
679 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200680endfunc
681
Bram Moolenaar297610b2019-12-27 17:20:55 +0100682func Test_cmdline_complete_env_variable()
683 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100684 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
685 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100686 unlet $X_VIM_TEST_COMPLETE_ENV
687endfunc
688
Bram Moolenaar4941b5e2020-12-24 17:15:53 +0100689func Test_cmdline_complete_expression()
690 let g:SomeVar = 'blah'
691 for cmd in ['exe', 'echo', 'echon', 'echomsg']
692 call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
693 call assert_match('"' .. cmd .. ' SomeVar', @:)
694 call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
695 call assert_match('"' .. cmd .. ' foo SomeVar', @:)
696 endfor
697 unlet g:SomeVar
698endfunc
699
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100700" Test for various command-line completion
701func Test_cmdline_complete_various()
702 " completion for a command starting with a comment
703 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
704 call assert_equal("\" :|\"\<C-A>", @:)
705
706 " completion for a range followed by a comment
707 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
708 call assert_equal("\"1,2\"\<C-A>", @:)
709
710 " completion for :k command
711 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
712 call assert_equal("\"ka\<C-A>", @:)
713
714 " completion for short version of the :s command
715 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
716 call assert_equal("\"sI \<C-A>", @:)
717
718 " completion for :write command
719 call mkdir('Xdir')
720 call writefile(['one'], 'Xdir/Xfile1')
721 let save_cwd = getcwd()
722 cd Xdir
723 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
724 call assert_equal("\"w >> Xfile1", @:)
725 call chdir(save_cwd)
726 call delete('Xdir', 'rf')
727
728 " completion for :w ! and :r ! commands
729 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
730 call assert_equal("\"w !invalid_xyz_cmd", @:)
731 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
732 call assert_equal("\"r !invalid_xyz_cmd", @:)
733
734 " completion for :>> and :<< commands
735 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
736 call assert_equal("\">>>\<C-A>", @:)
737 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
738 call assert_equal("\"<<<\<C-A>", @:)
739
740 " completion for command with +cmd argument
741 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
742 call assert_equal("\"buffer +/pat Xabc", @:)
743 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
744 call assert_equal("\"buffer +/pat\<C-A>", @:)
745
746 " completion for a command with a trailing comment
747 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
748 call assert_equal("\"ls \" comment\<C-A>", @:)
749
750 " completion for a command with a trailing command
751 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
752 call assert_equal("\"ls | ls", @:)
753
754 " completion for a command with an CTRL-V escaped argument
755 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
756 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
757
758 " completion for a command that doesn't take additional arguments
759 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
760 call assert_equal("\"all abc\<C-A>", @:)
761
762 " completion for a command with a command modifier
763 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
764 call assert_equal("\"topleft new", @:)
765
766 " completion for the :match command
767 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
768 call assert_equal("\"match Search /pat/\<C-A>", @:)
769
770 " completion for the :s command
771 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
772 call assert_equal("\"s/from/to/g\<C-A>", @:)
773
774 " completion for the :dlist command
775 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
776 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
777
778 " completion for the :doautocmd command
779 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
780 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
781
Bram Moolenaarafe8cf62020-10-05 20:07:18 +0200782 " completion of autocmd group after comma
783 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
784 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
785
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100786 " completion for the :augroup command
787 augroup XTest
788 augroup END
789 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
790 call assert_equal("\"augroup XTest", @:)
791 augroup! XTest
792
793 " completion for the :unlet command
794 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
795 call assert_equal("\"unlet one two", @:)
796
797 " completion for the :bdelete command
798 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
799 call assert_equal("\"bdel a b c", @:)
800
801 " completion for the :mapclear command
802 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
803 call assert_equal("\"mapclear <buffer>", @:)
804
805 " completion for user defined commands with menu names
806 menu Test.foo :ls<CR>
807 com -nargs=* -complete=menu MyCmd
808 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
809 call assert_equal('"MyCmd Test.', @:)
810 delcom MyCmd
811 unmenu Test
812
813 " completion for user defined commands with mappings
814 mapclear
815 map <F3> :ls<CR>
816 com -nargs=* -complete=mapping MyCmd
817 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
818 call assert_equal('"MyCmd <F3>', @:)
819 mapclear
820 delcom MyCmd
821
822 " completion for :set path= with multiple backslashes
823 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
824 call assert_equal('"set path=a\\\ b', @:)
825
826 " completion for :set dir= with a backslash
827 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
828 call assert_equal('"set dir=a\ b', @:)
829
830 " completion for the :py3 commands
831 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
832 call assert_equal('"py3 py3do py3file', @:)
833
834 " redir @" is not the start of a comment. So complete after that
835 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
836 call assert_equal('"redir @" | cwindow', @:)
837
838 " completion after a backtick
839 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
840 call assert_equal('"e `a1b2c', @:)
841
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100842 " completion for :language command with an invalid argument
843 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
844 call assert_equal("\"language dummy \t", @:)
845
846 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100847 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
848 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100849
850 " completion with ambiguous user defined commands
851 com TCmd1 echo 'TCmd1'
852 com TCmd2 echo 'TCmd2'
853 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
854 call assert_equal('"TCmd ', @:)
855 delcom TCmd1
856 delcom TCmd2
857
858 " completion after a range followed by a pipe (|) character
859 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
860 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +0200861
862 " use <Esc> as the 'wildchar' for completion
863 set wildchar=<Esc>
864 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
865 call assert_equal('"g/a\xb/clearjumps', @:)
866 " pressing <esc> twice should cancel the command
867 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
868 call assert_equal('"g/a\xb/clearjumps', @:)
869 set wildchar&
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100870endfunc
871
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200872func Test_cmdline_write_alternatefile()
873 new
874 call setline('.', ['one', 'two'])
875 f foo.txt
876 new
877 f #-A
878 call assert_equal('foo.txt-A', expand('%'))
879 f #<-B.txt
880 call assert_equal('foo-B.txt', expand('%'))
881 f %<
882 call assert_equal('foo-B', expand('%'))
883 new
Bram Moolenaare2e40752020-09-04 21:18:46 +0200884 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200885 bw!
886 f foo-B.txt
887 f %<-A
888 call assert_equal('foo-B-A', expand('%'))
889 bw!
890 bw!
891endfunc
892
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100893" using a leading backslash here
894set cpo+=C
895
896func Test_cmdline_search_range()
897 new
898 call setline(1, ['a', 'b', 'c', 'd'])
899 /d
900 1,\/s/b/B/
901 call assert_equal('B', getline(2))
902
903 /a
904 $
905 \?,4s/c/C/
906 call assert_equal('C', getline(3))
907
908 call setline(1, ['a', 'b', 'c', 'd'])
909 %s/c/c/
910 1,\&s/b/B/
911 call assert_equal('B', getline(2))
912
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100913 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200914 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100915
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100916 bwipe!
917endfunc
918
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100919" Test for the tick mark (') in an excmd range
920func Test_tick_mark_in_range()
921 " If only the tick is passed as a range and no command is specified, there
922 " should not be an error
923 call feedkeys(":'\<CR>", 'xt')
924 call assert_equal("'", getreg(':'))
925 call assert_fails("',print", 'E78:')
926endfunc
927
928" Test for using a line number followed by a search pattern as range
929func Test_lnum_and_pattern_as_range()
930 new
931 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
932 let @" = ''
933 2/foo/yank
934 call assert_equal("foo 3\n", @")
935 call assert_equal(1, line('.'))
936 close!
937endfunc
938
Bram Moolenaar65189a12017-02-06 22:22:17 +0100939" Tests for getcmdline(), getcmdpos() and getcmdtype()
940func Check_cmdline(cmdtype)
941 call assert_equal('MyCmd a', getcmdline())
942 call assert_equal(8, getcmdpos())
943 call assert_equal(a:cmdtype, getcmdtype())
944 return ''
945endfunc
946
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200947set cpo&
948
Bram Moolenaar65189a12017-02-06 22:22:17 +0100949func Test_getcmdtype()
950 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
951
952 let cmdtype = ''
953 debuggreedy
954 call feedkeys(":debug echo 'test'\<CR>", "t")
955 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
956 call feedkeys("cont\<CR>", "xt")
957 0debuggreedy
958 call assert_equal('>', cmdtype)
959
960 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
961 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
962
963 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100964 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100965
966 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
967
968 cnoremap <expr> <F6> Check_cmdline('=')
969 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
970 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100971
972 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100973endfunc
974
Bram Moolenaar81612b72018-06-23 14:55:03 +0200975func Test_getcmdwintype()
976 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
977 call assert_equal('/', a)
978
979 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
980 call assert_equal('?', a)
981
982 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
983 call assert_equal(':', a)
984
985 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
986 call assert_equal(':', a)
987
988 call assert_equal('', getcmdwintype())
989endfunc
990
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200991func Test_getcmdwin_autocmd()
992 let s:seq = []
993 augroup CmdWin
994 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
995 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
996 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
997 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
998 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
999 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
1000
1001 let org_winid = win_getid()
1002 let org_bufnr = bufnr()
1003 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
1004 call assert_equal(':', a)
1005 call assert_equal([
1006 \ 'WinLeave ' .. org_winid,
1007 \ 'WinEnter ' .. s:cmd_winid,
1008 \ 'BufLeave ' .. org_bufnr,
1009 \ 'BufEnter ' .. s:cmd_bufnr,
1010 \ 'CmdWinEnter ' .. s:cmd_winid,
1011 \ 'CmdWinLeave ' .. s:cmd_winid,
1012 \ 'BufLeave ' .. s:cmd_bufnr,
1013 \ 'WinLeave ' .. s:cmd_winid,
1014 \ 'WinEnter ' .. org_winid,
1015 \ 'BufEnter ' .. org_bufnr,
1016 \ ], s:seq)
1017
1018 au!
1019 augroup END
1020endfunc
1021
Bram Moolenaar52604f22017-04-07 16:17:39 +02001022func Test_verbosefile()
1023 set verbosefile=Xlog
1024 echomsg 'foo'
1025 echomsg 'bar'
1026 set verbosefile=
1027 let log = readfile('Xlog')
1028 call assert_match("foo\nbar", join(log, "\n"))
1029 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001030 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001031 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001032 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +02001033endfunc
1034
Bram Moolenaar4facea32019-10-12 20:17:40 +02001035func Test_verbose_option()
1036 CheckScreendump
1037
1038 let lines =<< trim [SCRIPT]
1039 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1040 call feedkeys("\r", 't') " for the hit-enter prompt
1041 set verbose=20
1042 [SCRIPT]
1043 call writefile(lines, 'XTest_verbose')
1044
1045 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001046 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001047 call term_sendkeys(buf, ":DoSomething\<CR>")
1048 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1049
1050 " clean up
1051 call StopVimInTerminal(buf)
1052 call delete('XTest_verbose')
1053endfunc
1054
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001055func Test_setcmdpos()
1056 func InsertTextAtPos(text, pos)
1057 call assert_equal(0, setcmdpos(a:pos))
1058 return a:text
1059 endfunc
1060
1061 " setcmdpos() with position in the middle of the command line.
1062 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1063 call assert_equal('"1ab2', @:)
1064
1065 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1066 call assert_equal('"1b2a', @:)
1067
1068 " setcmdpos() with position beyond the end of the command line.
1069 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1070 call assert_equal('"12ab', @:)
1071
1072 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001073 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001074endfunc
1075
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001076func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001077 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001078 let encoding_save = &encoding
1079
1080 for e in encodings
1081 exe 'set encoding=' . e
1082
1083 " Test overstrike in the middle of the command line.
1084 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001085 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001086
1087 " Test overstrike going beyond end of command line.
1088 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001089 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001090
1091 " Test toggling insert/overstrike a few times.
1092 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001093 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001094 endfor
1095
Bram Moolenaar30276f22019-01-24 17:59:39 +01001096 " Test overstrike with multi-byte characters.
1097 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001098 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001099
1100 let &encoding = encoding_save
1101endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001102
1103func Test_cmdwin_bug()
1104 let winid = win_getid()
1105 sp
1106 try
1107 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1108 catch /^Vim\%((\a\+)\)\=:E11/
1109 endtry
1110 bw!
1111endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001112
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001113func Test_cmdwin_restore()
1114 CheckScreendump
1115
1116 let lines =<< trim [SCRIPT]
1117 call setline(1, range(30))
1118 2split
1119 [SCRIPT]
1120 call writefile(lines, 'XTest_restore')
1121
1122 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001123 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001124 call term_sendkeys(buf, "q:")
1125 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1126
1127 " normal restore
1128 call term_sendkeys(buf, ":q\<CR>")
1129 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1130
1131 " restore after setting 'lines' with one window
1132 call term_sendkeys(buf, ":close\<CR>")
1133 call term_sendkeys(buf, "q:")
1134 call term_sendkeys(buf, ":set lines=18\<CR>")
1135 call term_sendkeys(buf, ":q\<CR>")
1136 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1137
1138 " clean up
1139 call StopVimInTerminal(buf)
1140 call delete('XTest_restore')
1141endfunc
1142
Bram Moolenaar52410572019-10-27 05:12:45 +01001143func Test_buffers_lastused()
1144 " check that buffers are sorted by time when wildmode has lastused
1145 call test_settime(1550020000) " middle
1146 edit bufa
1147 enew
1148 call test_settime(1550030000) " newest
1149 edit bufb
1150 enew
1151 call test_settime(1550010000) " oldest
1152 edit bufc
1153 enew
1154 call test_settime(0)
1155 enew
1156
1157 call assert_equal(['bufa', 'bufb', 'bufc'],
1158 \ getcompletion('', 'buffer'))
1159
1160 let save_wildmode = &wildmode
1161 set wildmode=full:lastused
1162
1163 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1164 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1165 call assert_equal('b bufb', X)
1166 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1167 call assert_equal('b bufa', X)
1168 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1169 call assert_equal('b bufc', X)
1170 enew
1171
1172 edit other
1173 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1174 call assert_equal('b bufb', X)
1175 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1176 call assert_equal('b bufa', X)
1177 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1178 call assert_equal('b bufc', X)
1179 enew
1180
1181 let &wildmode = save_wildmode
1182
1183 bwipeout bufa
1184 bwipeout bufb
1185 bwipeout bufc
1186endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001187
1188func Test_cmdwin_feedkeys()
1189 " This should not generate E488
1190 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001191 " Using feedkeys with q: only should automatically close the cmd window
1192 call feedkeys('q:', 'xt')
1193 call assert_equal(1, winnr('$'))
1194 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001195endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001196
1197" Tests for the issues fixed in 7.4.441.
1198" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1199func Test_cmdwin_cedit()
1200 exe "set cedit=\<C-c>"
1201 normal! :
1202 call assert_equal(1, winnr('$'))
1203
1204 let g:cmd_wintype = ''
1205 func CmdWinType()
1206 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001207 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001208 return ''
1209 endfunc
1210
1211 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1212 echo input('')
1213 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001214 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001215
1216 set cedit&vim
1217 delfunc CmdWinType
1218endfunc
1219
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001220" Test for CmdwinEnter autocmd
1221func Test_cmdwin_autocmd()
1222 augroup CmdWin
1223 au!
1224 autocmd CmdwinEnter * startinsert
1225 augroup END
1226
1227 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1228 call assert_equal('xyz', @:)
1229
1230 augroup CmdWin
1231 au!
1232 augroup END
1233 augroup! CmdWin
1234endfunc
1235
Bram Moolenaar479950f2020-01-19 15:45:17 +01001236func Test_cmdlineclear_tabenter()
1237 CheckScreendump
1238
1239 let lines =<< trim [SCRIPT]
1240 call setline(1, range(30))
1241 [SCRIPT]
1242
1243 call writefile(lines, 'XtestCmdlineClearTabenter')
1244 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001245 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001246 " in one tab make the command line higher with CTRL-W -
1247 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1248 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1249
1250 call StopVimInTerminal(buf)
1251 call delete('XtestCmdlineClearTabenter')
1252endfunc
1253
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001254" Test for failure in expanding special keywords in cmdline
1255func Test_cmdline_expand_special()
1256 %bwipe!
1257 call assert_fails('e #', 'E499:')
1258 call assert_fails('e <afile>', 'E495:')
1259 call assert_fails('e <abuf>', 'E496:')
1260 call assert_fails('e <amatch>', 'E497:')
1261endfunc
1262
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001263func Test_cmdwin_jump_to_win()
1264 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1265 new
1266 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001267 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001268 close!
1269 call feedkeys("q/:close\<CR>", "xt")
1270 call assert_equal(1, winnr('$'))
1271 call feedkeys("q/:exit\<CR>", "xt")
1272 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001273
1274 " opening command window twice should fail
1275 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1276 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001277endfunc
1278
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001279func Test_cmdwin_interrupted()
1280 CheckScreendump
1281
1282 " aborting the :smile output caused the cmdline window to use the current
1283 " buffer.
1284 let lines =<< trim [SCRIPT]
1285 au WinNew * smile
1286 [SCRIPT]
1287 call writefile(lines, 'XTest_cmdwin')
1288
1289 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001290 " open cmdwin
1291 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001292 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001293 " quit more prompt for :smile command
1294 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001295 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001296 " execute a simple command
1297 call term_sendkeys(buf, "aecho 'done'\<CR>")
1298 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1299
1300 " clean up
1301 call StopVimInTerminal(buf)
1302 call delete('XTest_cmdwin')
1303endfunc
1304
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001305" Test for backtick expression in the command line
1306func Test_cmd_backtick()
1307 %argd
1308 argadd `=['a', 'b', 'c']`
1309 call assert_equal(['a', 'b', 'c'], argv())
1310 %argd
1311endfunc
1312
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001313" Test for the :! command
1314func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001315 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001316
1317 let lines =<< trim [SCRIPT]
1318 " Test for no previous command
1319 call assert_fails('!!', 'E34:')
1320 set nomore
1321 " Test for cmdline expansion with :!
1322 call setline(1, 'foo!')
1323 silent !echo <cWORD> > Xfile.out
1324 call assert_equal(['foo!'], readfile('Xfile.out'))
1325 " Test for using previous command
1326 silent !echo \! !
1327 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1328 call writefile(v:errors, 'Xresult')
1329 call delete('Xfile.out')
1330 qall!
1331 [SCRIPT]
1332 call writefile(lines, 'Xscript')
1333 if RunVim([], [], '--clean -S Xscript')
1334 call assert_equal([], readfile('Xresult'))
1335 endif
1336 call delete('Xscript')
1337 call delete('Xresult')
1338endfunc
1339
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001340" Test error: "E135: *Filter* Autocommands must not change current buffer"
1341func Test_cmd_bang_E135()
1342 new
1343 call setline(1, ['a', 'b', 'c', 'd'])
1344 augroup test_cmd_filter_E135
1345 au!
1346 autocmd FilterReadPost * help
1347 augroup END
1348 call assert_fails('2,3!echo "x"', 'E135:')
1349
1350 augroup test_cmd_filter_E135
1351 au!
1352 augroup END
1353 %bwipe!
1354endfunc
1355
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001356" Test for using ~ for home directory in cmdline completion matches
1357func Test_cmdline_expand_home()
1358 call mkdir('Xdir')
1359 call writefile([], 'Xdir/Xfile1')
1360 call writefile([], 'Xdir/Xfile2')
1361 cd Xdir
1362 let save_HOME = $HOME
1363 let $HOME = getcwd()
1364 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1365 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1366 let $HOME = save_HOME
1367 cd ..
1368 call delete('Xdir', 'rf')
1369endfunc
1370
Bram Moolenaar578fe942020-02-27 21:32:51 +01001371" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1372" or insert mode (when 'insertmode' is set)
1373func Test_cmdline_ctrl_g()
1374 new
1375 call setline(1, 'abc')
1376 call cursor(1, 3)
1377 " If command line is entered from insert mode, using C-\ C-G should back to
1378 " insert mode
1379 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1380 call assert_equal('abxyc', getline(1))
1381 call assert_equal(4, col('.'))
1382
1383 " If command line is entered in 'insertmode', using C-\ C-G should back to
1384 " 'insertmode'
1385 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1386 call assert_equal('ab12xyc', getline(1))
1387 close!
1388endfunc
1389
Bram Moolenaar578fe942020-02-27 21:32:51 +01001390" Test for 'wildmode'
1391func Test_wildmode()
1392 func T(a, c, p)
1393 return "oneA\noneB\noneC"
1394 endfunc
1395 command -nargs=1 -complete=custom,T MyCmd
1396
1397 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001398 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001399 return ''
1400 endfunc
1401 cnoremap <expr> <F2> SaveScreenLine()
1402
1403 set nowildmenu
1404 set wildmode=full,list
1405 let g:Sline = ''
1406 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001407 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001408 call assert_equal('"MyCmd oneA', @:)
1409
1410 set wildmode=longest,full
1411 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1412 call assert_equal('"MyCmd one', @:)
1413 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1414 call assert_equal('"MyCmd oneC', @:)
1415
1416 set wildmode=longest
1417 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1418 call assert_equal('"MyCmd one', @:)
1419
1420 set wildmode=list:longest
1421 let g:Sline = ''
1422 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001423 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001424 call assert_equal('"MyCmd one', @:)
1425
1426 set wildmode=""
1427 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1428 call assert_equal('"MyCmd oneA', @:)
1429
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001430 " Test for wildmode=longest with 'fileignorecase' set
1431 set wildmode=longest
1432 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001433 argadd AAA AAAA AAAAA
1434 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1435 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001436 set fileignorecase&
1437
1438 " Test for listing files with wildmode=list
1439 set wildmode=list
1440 let g:Sline = ''
1441 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001442 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001443 call assert_equal('"b A', @:)
1444
1445 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001446 delcommand MyCmd
1447 delfunc T
1448 delfunc SaveScreenLine
1449 cunmap <F2>
1450 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001451 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001452endfunc
1453
1454" Test for interrupting the command-line completion
1455func Test_interrupt_compl()
1456 func F(lead, cmdl, p)
1457 if a:lead =~ 'tw'
1458 call interrupt()
1459 return
1460 endif
1461 return "one\ntwo\nthree"
1462 endfunc
1463 command -nargs=1 -complete=custom,F Tcmd
1464
1465 set nowildmenu
1466 set wildmode=full
1467 let interrupted = 0
1468 try
1469 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1470 catch /^Vim:Interrupt$/
1471 let interrupted = 1
1472 endtry
1473 call assert_equal(1, interrupted)
1474
1475 delcommand Tcmd
1476 delfunc F
1477 set wildmode&
1478endfunc
1479
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001480" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001481func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001482 let str = ":one two\<C-U>"
1483 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001484 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001485 let str ..= "\<Left>five\<Right>"
1486 let str ..= "\<Home>two "
1487 let str ..= "\<C-Left>one "
1488 let str ..= "\<C-Right> three"
1489 let str ..= "\<End>\<S-Left>four "
1490 let str ..= "\<S-Right> six"
1491 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1492 call feedkeys(str, 'xt')
1493 call assert_equal("\"one two three four five six seven", @:)
1494endfunc
1495
1496" Test for moving the cursor on the / command line in 'rightleft' mode
1497func Test_cmdline_edit_rightleft()
1498 CheckFeature rightleft
1499 set rightleft
1500 set rightleftcmd=search
1501 let str = "/one two\<C-U>"
1502 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001503 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001504 let str ..= "\<Right>five\<Left>"
1505 let str ..= "\<Home>two "
1506 let str ..= "\<C-Right>one "
1507 let str ..= "\<C-Left> three"
1508 let str ..= "\<End>\<S-Right>four "
1509 let str ..= "\<S-Left> six"
1510 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1511 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1512 call assert_equal("\"one two three four five six seven", @/)
1513 set rightleftcmd&
1514 set rightleft&
1515endfunc
1516
1517" Test for using <C-\>e in the command line to evaluate an expression
1518func Test_cmdline_expr()
1519 " Evaluate an expression from the beginning of a command line
1520 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1521 call assert_equal('"hello', @:)
1522
1523 " Use an invalid expression for <C-\>e
1524 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1525
1526 " Insert literal <CTRL-\> in the command line
1527 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1528 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001529endfunc
1530
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001531" Test for 'imcmdline' and 'imsearch'
1532" This test doesn't actually test the input method functionality.
1533func Test_cmdline_inputmethod()
1534 new
1535 call setline(1, ['', 'abc', ''])
1536 set imcmdline
1537
1538 call feedkeys(":\"abc\<CR>", 'xt')
1539 call assert_equal("\"abc", @:)
1540 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1541 call assert_equal("\"abc", @:)
1542 call feedkeys("/abc\<CR>", 'xt')
1543 call assert_equal([2, 1], [line('.'), col('.')])
1544 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1545 call assert_equal([2, 1], [line('.'), col('.')])
1546
1547 set imsearch=2
1548 call cursor(1, 1)
1549 call feedkeys("/abc\<CR>", 'xt')
1550 call assert_equal([2, 1], [line('.'), col('.')])
1551 call cursor(1, 1)
1552 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1553 call assert_equal([2, 1], [line('.'), col('.')])
1554 set imdisable
1555 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1556 call assert_equal([2, 1], [line('.'), col('.')])
1557 set imdisable&
1558 set imsearch&
1559
1560 set imcmdline&
1561 %bwipe!
1562endfunc
1563
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001564" Test for recursively getting multiple command line inputs
1565func Test_cmdwin_multi_input()
1566 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1567 call assert_equal('"cyan', @:)
1568endfunc
1569
1570" Test for using CTRL-_ in the command line with 'allowrevins'
1571func Test_cmdline_revins()
1572 CheckNotMSWindows
1573 CheckFeature rightleft
1574 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1575 call assert_equal("\"abc\<c-_>", @:)
1576 set allowrevins
1577 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1578 call assert_equal('"abcñèæ', @:)
1579 set allowrevins&
1580endfunc
1581
1582" Test for typing UTF-8 composing characters in the command line
1583func Test_cmdline_composing_chars()
1584 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1585 call assert_equal('"ゔ', @:)
1586endfunc
1587
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001588" Test for normal mode commands not supported in the cmd window
1589func Test_cmdwin_blocked_commands()
1590 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1591 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1592 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1593 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1594 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1595 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001596 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1597 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1598 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1599 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1600 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1601 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1602 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1603 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1604 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1605 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1606 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1607 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1608 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1609 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1610 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1611 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1612 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1613 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1614 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1615 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1616 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001617endfunc
1618
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001619" Close the Cmd-line window in insert mode using CTRL-C
1620func Test_cmdwin_insert_mode_close()
1621 %bw!
1622 let s = ''
1623 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1624 call assert_equal('Hello', s)
1625 call assert_equal(1, winnr('$'))
1626endfunc
1627
Bram Moolenaar0e717042020-04-27 19:29:01 +02001628" test that ";" works to find a match at the start of the first line
1629func Test_zero_line_search()
1630 new
1631 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1632 call cursor(1,1)
1633 0;/pattern/d
1634 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1635 q!
1636endfunc
1637
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001638func Test_read_shellcmd()
1639 CheckUnix
1640 if executable('ls')
1641 " There should be ls in the $PATH
1642 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1643 call assert_match('^"r! .*\<ls\>', @:)
1644 endif
1645
1646 if executable('rm')
1647 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1648 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1649 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001650
1651 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1652 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1653 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001654 endif
1655endfunc
1656
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01001657" Test for going up and down the directory tree using 'wildmenu'
1658func Test_wildmenu_dirstack()
1659 CheckUnix
1660 %bw!
1661 call mkdir('Xdir1/dir2/dir3', 'p')
1662 call writefile([], 'Xdir1/file1_1.txt')
1663 call writefile([], 'Xdir1/file1_2.txt')
1664 call writefile([], 'Xdir1/dir2/file2_1.txt')
1665 call writefile([], 'Xdir1/dir2/file2_2.txt')
1666 call writefile([], 'Xdir1/dir2/dir3/file3_1.txt')
1667 call writefile([], 'Xdir1/dir2/dir3/file3_2.txt')
1668 cd Xdir1/dir2/dir3
1669 set wildmenu
1670
1671 call feedkeys(":e \<Tab>\<C-B>\"\<CR>", 'xt')
1672 call assert_equal('"e file3_1.txt', @:)
1673 call feedkeys(":e \<Tab>\<Up>\<C-B>\"\<CR>", 'xt')
1674 call assert_equal('"e ../dir3/', @:)
1675 call feedkeys(":e \<Tab>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
1676 call assert_equal('"e ../../dir2/', @:)
1677 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<C-B>\"\<CR>", 'xt')
1678 call assert_equal('"e ../../dir2/dir3/', @:)
1679 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
1680 call assert_equal('"e ../../dir2/dir3/file3_1.txt', @:)
1681
1682 cd -
1683 call delete('Xdir1', 'rf')
1684 set wildmenu&
1685endfunc
1686
Bram Moolenaar309976e2019-12-05 18:16:33 +01001687" vim: shiftwidth=2 sts=2 expandtab