blob: 026625e0243c8eb109d46b1c58964b0ed870b9c8 [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 Moolenaara60053b2020-09-03 16:50:13 +020087f
88func Test_wildmenu_screendump()
89 CheckScreendump
90
91 let lines =<< trim [SCRIPT]
92 set wildmenu hlsearch
93 [SCRIPT]
94 call writefile(lines, 'XTest_wildmenu')
95
96 let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
97 call term_sendkeys(buf, ":vim\<Tab>")
98 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
99
100 call term_sendkeys(buf, "\<Tab>")
101 call VerifyScreenDump(buf, 'Test_wildmenu_2', {})
102
103 call term_sendkeys(buf, "\<Tab>")
104 call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
105
106 call term_sendkeys(buf, "\<Tab>")
107 call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
108 call term_sendkeys(buf, "\<Esc>")
109
110 " clean up
111 call StopVimInTerminal(buf)
112 call delete('XTest_wildmenu')
113endfunc
114
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200115
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100116func Test_map_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200117 CheckFeature cmdline_compl
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100118 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
119 call assert_equal('"map <unique> <silent>', getreg(':'))
120 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
121 call assert_equal('"map <script> <unique>', getreg(':'))
122 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
123 call assert_equal('"map <expr> <script>', getreg(':'))
124 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
125 call assert_equal('"map <buffer> <expr>', getreg(':'))
126 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
127 call assert_equal('"map <nowait> <buffer>', getreg(':'))
128 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
129 call assert_equal('"map <special> <nowait>', getreg(':'))
130 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
131 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200132
Bram Moolenaar1776a282019-05-03 16:05:41 +0200133 map <Middle>x middle
134
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200135 map ,f commaf
136 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200137 map <Left> left
138 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200139 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
140 call assert_equal('"map ,f', getreg(':'))
141 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
142 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200143 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
144 call assert_equal('"map <Left>', getreg(':'))
145 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200146 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200147 unmap ,f
148 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200149 unmap <Left>
150 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200151
152 set cpo-=< cpo-=B cpo-=k
153 map <Left> left
154 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
155 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200156 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200157 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200158 unmap <Left>
159
160 set cpo+=<
161 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200162 exe "set t_k6=\<Esc>[17~"
163 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200164 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
165 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200166 if !has('gui_running')
167 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
168 call assert_equal("\"map <F6>x", getreg(':'))
169 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200170 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200171 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200172 set cpo-=<
173
174 set cpo+=B
175 map <Left> left
176 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
177 call assert_equal('"map <Left>', getreg(':'))
178 unmap <Left>
179 set cpo-=B
180
181 set cpo+=k
182 map <Left> left
183 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
184 call assert_equal('"map <Left>', getreg(':'))
185 unmap <Left>
186 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200187
Bram Moolenaar531be472020-09-23 22:38:05 +0200188 call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
189
Bram Moolenaar1776a282019-05-03 16:05:41 +0200190 unmap <Middle>x
191 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100192endfunc
193
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100194func Test_match_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200195 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100196 hi Aardig ctermfg=green
197 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
198 call assert_equal('"match Aardig', getreg(':'))
199 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
200 call assert_equal('"match none', getreg(':'))
201endfunc
202
203func Test_highlight_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200204 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100205 hi Aardig ctermfg=green
206 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
207 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200208 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
209 call assert_equal('"hi default Aardig', getreg(':'))
210 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
211 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100212 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
213 call assert_equal('"hi link', getreg(':'))
214 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
215 call assert_equal('"hi default', getreg(':'))
216 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
217 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200218 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
219 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
220 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
221 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200222
223 " A cleared group does not show up in completions.
224 hi Anders ctermfg=green
225 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
226 hi clear Aardig
227 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
228 hi clear Anders
229 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100230endfunc
231
Bram Moolenaar75e15672020-06-28 13:10:22 +0200232" Test for command-line expansion of "hi Ni " (easter egg)
233func Test_highlight_easter_egg()
234 call test_override('ui_delay', 1)
235 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
236 call assert_equal("\"hi Ni \<Tab>", @:)
237 call test_override('ALL', 0)
238endfunc
239
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200240func Test_getcompletion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200241 CheckFeature cmdline_compl
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200242 let groupcount = len(getcompletion('', 'event'))
243 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200244 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200245 call assert_true(matchcount > 0)
246 call assert_true(groupcount > matchcount)
247
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200248 if has('menu')
249 source $VIMRUNTIME/menu.vim
250 let matchcount = len(getcompletion('', 'menu'))
251 call assert_true(matchcount > 0)
252 call assert_equal(['File.'], getcompletion('File', 'menu'))
253 call assert_true(matchcount > 0)
254 let matchcount = len(getcompletion('File.', 'menu'))
255 call assert_true(matchcount > 0)
256 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200257
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200258 let l = getcompletion('v:n', 'var')
259 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200260 let l = getcompletion('v:notexists', 'var')
261 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200262
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200263 args a.c b.c
264 let l = getcompletion('', 'arglist')
265 call assert_equal(['a.c', 'b.c'], l)
266 %argdelete
267
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200268 let l = getcompletion('', 'augroup')
269 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200270 let l = getcompletion('blahblah', 'augroup')
271 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200272
273 let l = getcompletion('', 'behave')
274 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200275 let l = getcompletion('not', 'behave')
276 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200277
278 let l = getcompletion('', 'color')
279 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200280 let l = getcompletion('dirty', 'color')
281 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200282
283 let l = getcompletion('', 'command')
284 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200285 let l = getcompletion('awake', 'command')
286 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200287
288 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200289 call assert_true(index(l, 'samples/') >= 0)
290 let l = getcompletion('NoMatch', 'dir')
291 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200292
293 let l = getcompletion('exe', 'expression')
294 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200295 let l = getcompletion('kill', 'expression')
296 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200297
298 let l = getcompletion('tag', 'function')
299 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200300 let l = getcompletion('paint', 'function')
301 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200302
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200303 let Flambda = {-> 'hello'}
304 let l = getcompletion('', 'function')
305 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200306 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200307
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200308 let l = getcompletion('run', 'file')
309 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200310 let l = getcompletion('walk', 'file')
311 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200312 set wildignore=*.vim
313 let l = getcompletion('run', 'file', 1)
314 call assert_true(index(l, 'runtest.vim') < 0)
315 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200316
317 let l = getcompletion('ha', 'filetype')
318 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200319 let l = getcompletion('horse', 'filetype')
320 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200321
322 let l = getcompletion('z', 'syntax')
323 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200324 let l = getcompletion('emacs', 'syntax')
325 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200326
327 let l = getcompletion('jikes', 'compiler')
328 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200329 let l = getcompletion('break', 'compiler')
330 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200331
332 let l = getcompletion('last', 'help')
333 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200334 let l = getcompletion('giveup', 'help')
335 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200336
337 let l = getcompletion('time', 'option')
338 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200339 let l = getcompletion('space', 'option')
340 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200341
342 let l = getcompletion('er', 'highlight')
343 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200344 let l = getcompletion('dark', 'highlight')
345 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200346
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200347 let l = getcompletion('', 'messages')
348 call assert_true(index(l, 'clear') >= 0)
349 let l = getcompletion('not', 'messages')
350 call assert_equal([], l)
351
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200352 let l = getcompletion('', 'mapclear')
353 call assert_true(index(l, '<buffer>') >= 0)
354 let l = getcompletion('not', 'mapclear')
355 call assert_equal([], l)
356
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200357 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200358 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200359 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
360 let root = has('win32') ? 'C:\\' : '/'
361 let l = getcompletion(root, 'shellcmd')
362 let expected = map(filter(glob(root . '*', 0, 1),
363 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
364 call assert_equal(expected, l)
365
Bram Moolenaarb650b982016-08-05 20:35:13 +0200366 if has('cscope')
367 let l = getcompletion('', 'cscope')
368 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
369 call assert_equal(cmds, l)
370 " using cmdline completion must not change the result
371 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
372 let l = getcompletion('', 'cscope')
373 call assert_equal(cmds, l)
374 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
375 let l = getcompletion('find ', 'cscope')
376 call assert_equal(keys, l)
377 endif
378
Bram Moolenaar7522f692016-08-06 14:12:50 +0200379 if has('signs')
380 sign define Testing linehl=Comment
381 let l = getcompletion('', 'sign')
382 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
383 call assert_equal(cmds, l)
384 " using cmdline completion must not change the result
385 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
386 let l = getcompletion('', 'sign')
387 call assert_equal(cmds, l)
388 let l = getcompletion('list ', 'sign')
389 call assert_equal(['Testing'], l)
390 endif
391
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200392 " Command line completion tests
393 let l = getcompletion('cd ', 'cmdline')
394 call assert_true(index(l, 'samples/') >= 0)
395 let l = getcompletion('cd NoMatch', 'cmdline')
396 call assert_equal([], l)
397 let l = getcompletion('let v:n', 'cmdline')
398 call assert_true(index(l, 'v:null') >= 0)
399 let l = getcompletion('let v:notexists', 'cmdline')
400 call assert_equal([], l)
401 let l = getcompletion('call tag', 'cmdline')
402 call assert_true(index(l, 'taglist(') >= 0)
403 let l = getcompletion('call paint', 'cmdline')
404 call assert_equal([], l)
405
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200406 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200407 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200408 if has('cmdline_hist')
409 call add(names, 'history')
410 endif
411 if has('gettext')
412 call add(names, 'locale')
413 endif
414 if has('profile')
415 call add(names, 'syntime')
416 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200417
418 set tags=Xtags
419 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
420
421 for name in names
422 let matchcount = len(getcompletion('', name))
423 call assert_true(matchcount >= 0, 'No matches for ' . name)
424 endfor
425
426 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200427 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200428
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200429 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200430 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200431 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200432endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200433
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200434func Test_shellcmd_completion()
435 let save_path = $PATH
436
437 call mkdir('Xpathdir/Xpathsubdir', 'p')
438 call writefile([''], 'Xpathdir/Xfile.exe')
439 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
440
441 " Set PATH to example directory without trailing slash.
442 let $PATH = getcwd() . '/Xpathdir'
443
444 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
445 " dirs in the PATH, even though they won't be executed. We check that only
446 " subdirs of the PWD and executables from the PATH are included in the
447 " suggestions.
448 let actual = getcompletion('X', 'shellcmd')
449 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
450 call insert(expected, 'Xfile.exe')
451 call assert_equal(expected, actual)
452
453 call delete('Xpathdir', 'rf')
454 let $PATH = save_path
455endfunc
456
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200457func Test_expand_star_star()
458 call mkdir('a/b', 'p')
459 call writefile(['asdfasdf'], 'a/b/fileXname')
460 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
461 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200462 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200463 call delete('a', 'rf')
464endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100465
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100466func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100467 let @a = "def"
468 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
469 call assert_equal('"abc def ghi', @:)
470
471 new
472 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
473
474 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
475 call assert_equal('"aaa asdf bbb', @:)
476
477 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
478 call assert_equal('"aaa /tmp/some bbb', @:)
479
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200480 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
481 call assert_equal('"aaa '.getline(1).' bbb', @:)
482
Bram Moolenaar21efc362016-12-03 14:05:49 +0100483 set incsearch
484 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
485 call assert_equal('"aaa verylongword bbb', @:)
486
487 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
488 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100489
490 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
491 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100492 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200493
494 " Error while typing a command used to cause that it was not executed
495 " in the end.
496 new
497 try
498 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
499 catch /^Vim\%((\a\+)\)\=:E32/
500 " ignore error E32
501 endtry
502 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100503
Bram Moolenaar578fe942020-02-27 21:32:51 +0100504 " Try to paste an invalid register using <C-R>
505 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
506 call assert_equal('"onetwo', @:)
507
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100508 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100509 let @a = "xy\<C-H>z"
510 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
511 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100512 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
513 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100514 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
515 call assert_equal("\"xy\<C-H>z", @:)
516
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100517 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
518 let @a = "xy\<C-V>z"
519 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
520 call assert_equal('"xyz', @:)
521 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
522 call assert_equal("\"xy\<C-V>z", @:)
523
Bram Moolenaar578fe942020-02-27 21:32:51 +0100524 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
525
Bram Moolenaar72532d32018-04-07 19:09:09 +0200526 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100527endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100528
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100529func Test_cmdline_remove_char()
530 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100531
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100532 for e in ['utf8', 'latin1']
533 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100534
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100535 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
536 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100537
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100538 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
539 call assert_equal('"abcdef', @:)
540
541 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
542 call assert_equal('"abc ghi', @:, e)
543
544 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
545 call assert_equal('"def', @:, e)
546 endfor
547
548 let &encoding = encoding_save
549endfunc
550
551func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200552 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100553
554 set keymap=esperanto
555 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
556 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
557 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100558endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100559
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100560func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100561 new
562 2;'(
563 2;')
564 quit
565endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100566
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100567func Test_illegal_address2()
568 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
569 new
570 source Xtest.vim
571 " Trigger calling validate_cursor()
572 diffsp Xtest.vim
573 quit!
574 bwipe!
575 call delete('Xtest.vim')
576endfunc
577
Bram Moolenaarba47b512017-01-24 21:18:19 +0100578func Test_cmdline_complete_wildoptions()
579 help
580 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
581 let a = join(sort(split(@:)),' ')
582 set wildoptions=tagfile
583 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
584 let b = join(sort(split(@:)),' ')
585 call assert_equal(a, b)
586 bw!
587endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100588
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200589func Test_cmdline_complete_user_cmd()
590 command! -complete=color -nargs=1 Foo :
591 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
592 call assert_equal('"Foo blue', @:)
593 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
594 call assert_equal('"Foo blue', @:)
595 delcommand Foo
596endfunc
597
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100598func s:ScriptLocalFunction()
599 echo 'yes'
600endfunc
601
602func Test_cmdline_complete_user_func()
603 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
604 call assert_match('"func Test_cmdline_complete_user', @:)
605 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
606 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
607endfunc
608
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200609func Test_cmdline_complete_user_names()
610 if has('unix') && executable('whoami')
611 let whoami = systemlist('whoami')[0]
612 let first_letter = whoami[0]
613 if len(first_letter) > 0
614 " Trying completion of :e ~x where x is the first letter of
615 " the user name should complete to at least the user name.
616 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
617 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
618 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200619 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200620 " Just in case: check that the system has an Administrator account.
621 let names = system('net user')
622 if names =~ 'Administrator'
623 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100624 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200625 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100626 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200627 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200628 else
629 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200630 endif
631endfunc
632
Bram Moolenaar297610b2019-12-27 17:20:55 +0100633func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200634 CheckExecutable whoami
635 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
636 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100637endfunc
638
Bram Moolenaar8b633132020-03-20 18:20:51 +0100639func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200640 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
641 call assert_equal(lang, v:lc_time)
642
643 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
644 call assert_equal(lang, v:ctype)
645
646 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
647 call assert_equal(lang, v:collate)
648
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200649 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200650 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200651
652 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200653 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200654
Bram Moolenaarec680282020-06-12 19:35:32 +0200655 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200656
Bram Moolenaarec680282020-06-12 19:35:32 +0200657 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
658 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200659
Bram Moolenaarec680282020-06-12 19:35:32 +0200660 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
661 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200662
Bram Moolenaarec680282020-06-12 19:35:32 +0200663 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
664 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200665
666 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
667 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200668endfunc
669
Bram Moolenaar297610b2019-12-27 17:20:55 +0100670func Test_cmdline_complete_env_variable()
671 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100672 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
673 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100674 unlet $X_VIM_TEST_COMPLETE_ENV
675endfunc
676
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100677" Test for various command-line completion
678func Test_cmdline_complete_various()
679 " completion for a command starting with a comment
680 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
681 call assert_equal("\" :|\"\<C-A>", @:)
682
683 " completion for a range followed by a comment
684 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
685 call assert_equal("\"1,2\"\<C-A>", @:)
686
687 " completion for :k command
688 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
689 call assert_equal("\"ka\<C-A>", @:)
690
691 " completion for short version of the :s command
692 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
693 call assert_equal("\"sI \<C-A>", @:)
694
695 " completion for :write command
696 call mkdir('Xdir')
697 call writefile(['one'], 'Xdir/Xfile1')
698 let save_cwd = getcwd()
699 cd Xdir
700 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
701 call assert_equal("\"w >> Xfile1", @:)
702 call chdir(save_cwd)
703 call delete('Xdir', 'rf')
704
705 " completion for :w ! and :r ! commands
706 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
707 call assert_equal("\"w !invalid_xyz_cmd", @:)
708 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
709 call assert_equal("\"r !invalid_xyz_cmd", @:)
710
711 " completion for :>> and :<< commands
712 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
713 call assert_equal("\">>>\<C-A>", @:)
714 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
715 call assert_equal("\"<<<\<C-A>", @:)
716
717 " completion for command with +cmd argument
718 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
719 call assert_equal("\"buffer +/pat Xabc", @:)
720 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
721 call assert_equal("\"buffer +/pat\<C-A>", @:)
722
723 " completion for a command with a trailing comment
724 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
725 call assert_equal("\"ls \" comment\<C-A>", @:)
726
727 " completion for a command with a trailing command
728 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
729 call assert_equal("\"ls | ls", @:)
730
731 " completion for a command with an CTRL-V escaped argument
732 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
733 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
734
735 " completion for a command that doesn't take additional arguments
736 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
737 call assert_equal("\"all abc\<C-A>", @:)
738
739 " completion for a command with a command modifier
740 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
741 call assert_equal("\"topleft new", @:)
742
743 " completion for the :match command
744 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
745 call assert_equal("\"match Search /pat/\<C-A>", @:)
746
747 " completion for the :s command
748 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
749 call assert_equal("\"s/from/to/g\<C-A>", @:)
750
751 " completion for the :dlist command
752 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
753 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
754
755 " completion for the :doautocmd command
756 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
757 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
758
759 " completion for the :augroup command
760 augroup XTest
761 augroup END
762 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
763 call assert_equal("\"augroup XTest", @:)
764 augroup! XTest
765
766 " completion for the :unlet command
767 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
768 call assert_equal("\"unlet one two", @:)
769
770 " completion for the :bdelete command
771 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
772 call assert_equal("\"bdel a b c", @:)
773
774 " completion for the :mapclear command
775 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
776 call assert_equal("\"mapclear <buffer>", @:)
777
778 " completion for user defined commands with menu names
779 menu Test.foo :ls<CR>
780 com -nargs=* -complete=menu MyCmd
781 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
782 call assert_equal('"MyCmd Test.', @:)
783 delcom MyCmd
784 unmenu Test
785
786 " completion for user defined commands with mappings
787 mapclear
788 map <F3> :ls<CR>
789 com -nargs=* -complete=mapping MyCmd
790 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
791 call assert_equal('"MyCmd <F3>', @:)
792 mapclear
793 delcom MyCmd
794
795 " completion for :set path= with multiple backslashes
796 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
797 call assert_equal('"set path=a\\\ b', @:)
798
799 " completion for :set dir= with a backslash
800 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
801 call assert_equal('"set dir=a\ b', @:)
802
803 " completion for the :py3 commands
804 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
805 call assert_equal('"py3 py3do py3file', @:)
806
807 " redir @" is not the start of a comment. So complete after that
808 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
809 call assert_equal('"redir @" | cwindow', @:)
810
811 " completion after a backtick
812 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
813 call assert_equal('"e `a1b2c', @:)
814
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100815 " completion for :language command with an invalid argument
816 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
817 call assert_equal("\"language dummy \t", @:)
818
819 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100820 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
821 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100822
823 " completion with ambiguous user defined commands
824 com TCmd1 echo 'TCmd1'
825 com TCmd2 echo 'TCmd2'
826 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
827 call assert_equal('"TCmd ', @:)
828 delcom TCmd1
829 delcom TCmd2
830
831 " completion after a range followed by a pipe (|) character
832 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
833 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +0200834
835 " use <Esc> as the 'wildchar' for completion
836 set wildchar=<Esc>
837 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
838 call assert_equal('"g/a\xb/clearjumps', @:)
839 " pressing <esc> twice should cancel the command
840 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
841 call assert_equal('"g/a\xb/clearjumps', @:)
842 set wildchar&
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100843endfunc
844
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200845func Test_cmdline_write_alternatefile()
846 new
847 call setline('.', ['one', 'two'])
848 f foo.txt
849 new
850 f #-A
851 call assert_equal('foo.txt-A', expand('%'))
852 f #<-B.txt
853 call assert_equal('foo-B.txt', expand('%'))
854 f %<
855 call assert_equal('foo-B', expand('%'))
856 new
Bram Moolenaare2e40752020-09-04 21:18:46 +0200857 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200858 bw!
859 f foo-B.txt
860 f %<-A
861 call assert_equal('foo-B-A', expand('%'))
862 bw!
863 bw!
864endfunc
865
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100866" using a leading backslash here
867set cpo+=C
868
869func Test_cmdline_search_range()
870 new
871 call setline(1, ['a', 'b', 'c', 'd'])
872 /d
873 1,\/s/b/B/
874 call assert_equal('B', getline(2))
875
876 /a
877 $
878 \?,4s/c/C/
879 call assert_equal('C', getline(3))
880
881 call setline(1, ['a', 'b', 'c', 'd'])
882 %s/c/c/
883 1,\&s/b/B/
884 call assert_equal('B', getline(2))
885
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100886 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200887 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100888
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100889 bwipe!
890endfunc
891
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100892" Test for the tick mark (') in an excmd range
893func Test_tick_mark_in_range()
894 " If only the tick is passed as a range and no command is specified, there
895 " should not be an error
896 call feedkeys(":'\<CR>", 'xt')
897 call assert_equal("'", getreg(':'))
898 call assert_fails("',print", 'E78:')
899endfunc
900
901" Test for using a line number followed by a search pattern as range
902func Test_lnum_and_pattern_as_range()
903 new
904 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
905 let @" = ''
906 2/foo/yank
907 call assert_equal("foo 3\n", @")
908 call assert_equal(1, line('.'))
909 close!
910endfunc
911
Bram Moolenaar65189a12017-02-06 22:22:17 +0100912" Tests for getcmdline(), getcmdpos() and getcmdtype()
913func Check_cmdline(cmdtype)
914 call assert_equal('MyCmd a', getcmdline())
915 call assert_equal(8, getcmdpos())
916 call assert_equal(a:cmdtype, getcmdtype())
917 return ''
918endfunc
919
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200920set cpo&
921
Bram Moolenaar65189a12017-02-06 22:22:17 +0100922func Test_getcmdtype()
923 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
924
925 let cmdtype = ''
926 debuggreedy
927 call feedkeys(":debug echo 'test'\<CR>", "t")
928 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
929 call feedkeys("cont\<CR>", "xt")
930 0debuggreedy
931 call assert_equal('>', cmdtype)
932
933 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
934 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
935
936 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100937 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100938
939 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
940
941 cnoremap <expr> <F6> Check_cmdline('=')
942 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
943 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100944
945 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100946endfunc
947
Bram Moolenaar81612b72018-06-23 14:55:03 +0200948func Test_getcmdwintype()
949 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
950 call assert_equal('/', a)
951
952 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
953 call assert_equal('?', a)
954
955 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
956 call assert_equal(':', a)
957
958 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
959 call assert_equal(':', a)
960
961 call assert_equal('', getcmdwintype())
962endfunc
963
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200964func Test_getcmdwin_autocmd()
965 let s:seq = []
966 augroup CmdWin
967 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
968 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
969 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
970 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
971 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
972 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
973
974 let org_winid = win_getid()
975 let org_bufnr = bufnr()
976 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
977 call assert_equal(':', a)
978 call assert_equal([
979 \ 'WinLeave ' .. org_winid,
980 \ 'WinEnter ' .. s:cmd_winid,
981 \ 'BufLeave ' .. org_bufnr,
982 \ 'BufEnter ' .. s:cmd_bufnr,
983 \ 'CmdWinEnter ' .. s:cmd_winid,
984 \ 'CmdWinLeave ' .. s:cmd_winid,
985 \ 'BufLeave ' .. s:cmd_bufnr,
986 \ 'WinLeave ' .. s:cmd_winid,
987 \ 'WinEnter ' .. org_winid,
988 \ 'BufEnter ' .. org_bufnr,
989 \ ], s:seq)
990
991 au!
992 augroup END
993endfunc
994
Bram Moolenaar52604f22017-04-07 16:17:39 +0200995func Test_verbosefile()
996 set verbosefile=Xlog
997 echomsg 'foo'
998 echomsg 'bar'
999 set verbosefile=
1000 let log = readfile('Xlog')
1001 call assert_match("foo\nbar", join(log, "\n"))
1002 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001003 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001004 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001005 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +02001006endfunc
1007
Bram Moolenaar4facea32019-10-12 20:17:40 +02001008func Test_verbose_option()
1009 CheckScreendump
1010
1011 let lines =<< trim [SCRIPT]
1012 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1013 call feedkeys("\r", 't') " for the hit-enter prompt
1014 set verbose=20
1015 [SCRIPT]
1016 call writefile(lines, 'XTest_verbose')
1017
1018 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001019 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001020 call term_sendkeys(buf, ":DoSomething\<CR>")
1021 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1022
1023 " clean up
1024 call StopVimInTerminal(buf)
1025 call delete('XTest_verbose')
1026endfunc
1027
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001028func Test_setcmdpos()
1029 func InsertTextAtPos(text, pos)
1030 call assert_equal(0, setcmdpos(a:pos))
1031 return a:text
1032 endfunc
1033
1034 " setcmdpos() with position in the middle of the command line.
1035 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1036 call assert_equal('"1ab2', @:)
1037
1038 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1039 call assert_equal('"1b2a', @:)
1040
1041 " setcmdpos() with position beyond the end of the command line.
1042 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1043 call assert_equal('"12ab', @:)
1044
1045 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001046 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001047endfunc
1048
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001049func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001050 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001051 let encoding_save = &encoding
1052
1053 for e in encodings
1054 exe 'set encoding=' . e
1055
1056 " Test overstrike in the middle of the command line.
1057 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001058 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001059
1060 " Test overstrike going beyond end of command line.
1061 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001062 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001063
1064 " Test toggling insert/overstrike a few times.
1065 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001066 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001067 endfor
1068
Bram Moolenaar30276f22019-01-24 17:59:39 +01001069 " Test overstrike with multi-byte characters.
1070 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001071 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001072
1073 let &encoding = encoding_save
1074endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001075
1076func Test_cmdwin_bug()
1077 let winid = win_getid()
1078 sp
1079 try
1080 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1081 catch /^Vim\%((\a\+)\)\=:E11/
1082 endtry
1083 bw!
1084endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001085
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001086func Test_cmdwin_restore()
1087 CheckScreendump
1088
1089 let lines =<< trim [SCRIPT]
1090 call setline(1, range(30))
1091 2split
1092 [SCRIPT]
1093 call writefile(lines, 'XTest_restore')
1094
1095 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001096 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001097 call term_sendkeys(buf, "q:")
1098 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1099
1100 " normal restore
1101 call term_sendkeys(buf, ":q\<CR>")
1102 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1103
1104 " restore after setting 'lines' with one window
1105 call term_sendkeys(buf, ":close\<CR>")
1106 call term_sendkeys(buf, "q:")
1107 call term_sendkeys(buf, ":set lines=18\<CR>")
1108 call term_sendkeys(buf, ":q\<CR>")
1109 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1110
1111 " clean up
1112 call StopVimInTerminal(buf)
1113 call delete('XTest_restore')
1114endfunc
1115
Bram Moolenaar52410572019-10-27 05:12:45 +01001116func Test_buffers_lastused()
1117 " check that buffers are sorted by time when wildmode has lastused
1118 call test_settime(1550020000) " middle
1119 edit bufa
1120 enew
1121 call test_settime(1550030000) " newest
1122 edit bufb
1123 enew
1124 call test_settime(1550010000) " oldest
1125 edit bufc
1126 enew
1127 call test_settime(0)
1128 enew
1129
1130 call assert_equal(['bufa', 'bufb', 'bufc'],
1131 \ getcompletion('', 'buffer'))
1132
1133 let save_wildmode = &wildmode
1134 set wildmode=full:lastused
1135
1136 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1137 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1138 call assert_equal('b bufb', X)
1139 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1140 call assert_equal('b bufa', X)
1141 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1142 call assert_equal('b bufc', X)
1143 enew
1144
1145 edit other
1146 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1147 call assert_equal('b bufb', X)
1148 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1149 call assert_equal('b bufa', X)
1150 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1151 call assert_equal('b bufc', X)
1152 enew
1153
1154 let &wildmode = save_wildmode
1155
1156 bwipeout bufa
1157 bwipeout bufb
1158 bwipeout bufc
1159endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001160
1161func Test_cmdwin_feedkeys()
1162 " This should not generate E488
1163 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001164 " Using feedkeys with q: only should automatically close the cmd window
1165 call feedkeys('q:', 'xt')
1166 call assert_equal(1, winnr('$'))
1167 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001168endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001169
1170" Tests for the issues fixed in 7.4.441.
1171" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1172func Test_cmdwin_cedit()
1173 exe "set cedit=\<C-c>"
1174 normal! :
1175 call assert_equal(1, winnr('$'))
1176
1177 let g:cmd_wintype = ''
1178 func CmdWinType()
1179 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001180 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001181 return ''
1182 endfunc
1183
1184 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1185 echo input('')
1186 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001187 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001188
1189 set cedit&vim
1190 delfunc CmdWinType
1191endfunc
1192
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001193" Test for CmdwinEnter autocmd
1194func Test_cmdwin_autocmd()
1195 augroup CmdWin
1196 au!
1197 autocmd CmdwinEnter * startinsert
1198 augroup END
1199
1200 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1201 call assert_equal('xyz', @:)
1202
1203 augroup CmdWin
1204 au!
1205 augroup END
1206 augroup! CmdWin
1207endfunc
1208
Bram Moolenaar479950f2020-01-19 15:45:17 +01001209func Test_cmdlineclear_tabenter()
1210 CheckScreendump
1211
1212 let lines =<< trim [SCRIPT]
1213 call setline(1, range(30))
1214 [SCRIPT]
1215
1216 call writefile(lines, 'XtestCmdlineClearTabenter')
1217 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001218 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001219 " in one tab make the command line higher with CTRL-W -
1220 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1221 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1222
1223 call StopVimInTerminal(buf)
1224 call delete('XtestCmdlineClearTabenter')
1225endfunc
1226
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001227" Test for failure in expanding special keywords in cmdline
1228func Test_cmdline_expand_special()
1229 %bwipe!
1230 call assert_fails('e #', 'E499:')
1231 call assert_fails('e <afile>', 'E495:')
1232 call assert_fails('e <abuf>', 'E496:')
1233 call assert_fails('e <amatch>', 'E497:')
1234endfunc
1235
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001236func Test_cmdwin_jump_to_win()
1237 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1238 new
1239 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001240 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001241 close!
1242 call feedkeys("q/:close\<CR>", "xt")
1243 call assert_equal(1, winnr('$'))
1244 call feedkeys("q/:exit\<CR>", "xt")
1245 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001246
1247 " opening command window twice should fail
1248 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1249 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001250endfunc
1251
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001252func Test_cmdwin_interrupted()
1253 CheckScreendump
1254
1255 " aborting the :smile output caused the cmdline window to use the current
1256 " buffer.
1257 let lines =<< trim [SCRIPT]
1258 au WinNew * smile
1259 [SCRIPT]
1260 call writefile(lines, 'XTest_cmdwin')
1261
1262 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001263 " open cmdwin
1264 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001265 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001266 " quit more prompt for :smile command
1267 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001268 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001269 " execute a simple command
1270 call term_sendkeys(buf, "aecho 'done'\<CR>")
1271 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1272
1273 " clean up
1274 call StopVimInTerminal(buf)
1275 call delete('XTest_cmdwin')
1276endfunc
1277
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001278" Test for backtick expression in the command line
1279func Test_cmd_backtick()
1280 %argd
1281 argadd `=['a', 'b', 'c']`
1282 call assert_equal(['a', 'b', 'c'], argv())
1283 %argd
1284endfunc
1285
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001286" Test for the :! command
1287func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001288 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001289
1290 let lines =<< trim [SCRIPT]
1291 " Test for no previous command
1292 call assert_fails('!!', 'E34:')
1293 set nomore
1294 " Test for cmdline expansion with :!
1295 call setline(1, 'foo!')
1296 silent !echo <cWORD> > Xfile.out
1297 call assert_equal(['foo!'], readfile('Xfile.out'))
1298 " Test for using previous command
1299 silent !echo \! !
1300 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1301 call writefile(v:errors, 'Xresult')
1302 call delete('Xfile.out')
1303 qall!
1304 [SCRIPT]
1305 call writefile(lines, 'Xscript')
1306 if RunVim([], [], '--clean -S Xscript')
1307 call assert_equal([], readfile('Xresult'))
1308 endif
1309 call delete('Xscript')
1310 call delete('Xresult')
1311endfunc
1312
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001313" Test error: "E135: *Filter* Autocommands must not change current buffer"
1314func Test_cmd_bang_E135()
1315 new
1316 call setline(1, ['a', 'b', 'c', 'd'])
1317 augroup test_cmd_filter_E135
1318 au!
1319 autocmd FilterReadPost * help
1320 augroup END
1321 call assert_fails('2,3!echo "x"', 'E135:')
1322
1323 augroup test_cmd_filter_E135
1324 au!
1325 augroup END
1326 %bwipe!
1327endfunc
1328
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001329" Test for using ~ for home directory in cmdline completion matches
1330func Test_cmdline_expand_home()
1331 call mkdir('Xdir')
1332 call writefile([], 'Xdir/Xfile1')
1333 call writefile([], 'Xdir/Xfile2')
1334 cd Xdir
1335 let save_HOME = $HOME
1336 let $HOME = getcwd()
1337 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1338 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1339 let $HOME = save_HOME
1340 cd ..
1341 call delete('Xdir', 'rf')
1342endfunc
1343
Bram Moolenaar578fe942020-02-27 21:32:51 +01001344" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1345" or insert mode (when 'insertmode' is set)
1346func Test_cmdline_ctrl_g()
1347 new
1348 call setline(1, 'abc')
1349 call cursor(1, 3)
1350 " If command line is entered from insert mode, using C-\ C-G should back to
1351 " insert mode
1352 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1353 call assert_equal('abxyc', getline(1))
1354 call assert_equal(4, col('.'))
1355
1356 " If command line is entered in 'insertmode', using C-\ C-G should back to
1357 " 'insertmode'
1358 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1359 call assert_equal('ab12xyc', getline(1))
1360 close!
1361endfunc
1362
Bram Moolenaar578fe942020-02-27 21:32:51 +01001363" Test for 'wildmode'
1364func Test_wildmode()
1365 func T(a, c, p)
1366 return "oneA\noneB\noneC"
1367 endfunc
1368 command -nargs=1 -complete=custom,T MyCmd
1369
1370 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001371 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001372 return ''
1373 endfunc
1374 cnoremap <expr> <F2> SaveScreenLine()
1375
1376 set nowildmenu
1377 set wildmode=full,list
1378 let g:Sline = ''
1379 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001380 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001381 call assert_equal('"MyCmd oneA', @:)
1382
1383 set wildmode=longest,full
1384 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1385 call assert_equal('"MyCmd one', @:)
1386 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1387 call assert_equal('"MyCmd oneC', @:)
1388
1389 set wildmode=longest
1390 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1391 call assert_equal('"MyCmd one', @:)
1392
1393 set wildmode=list:longest
1394 let g:Sline = ''
1395 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001396 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001397 call assert_equal('"MyCmd one', @:)
1398
1399 set wildmode=""
1400 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1401 call assert_equal('"MyCmd oneA', @:)
1402
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001403 " Test for wildmode=longest with 'fileignorecase' set
1404 set wildmode=longest
1405 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001406 argadd AAA AAAA AAAAA
1407 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1408 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001409 set fileignorecase&
1410
1411 " Test for listing files with wildmode=list
1412 set wildmode=list
1413 let g:Sline = ''
1414 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001415 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001416 call assert_equal('"b A', @:)
1417
1418 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001419 delcommand MyCmd
1420 delfunc T
1421 delfunc SaveScreenLine
1422 cunmap <F2>
1423 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001424 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001425endfunc
1426
1427" Test for interrupting the command-line completion
1428func Test_interrupt_compl()
1429 func F(lead, cmdl, p)
1430 if a:lead =~ 'tw'
1431 call interrupt()
1432 return
1433 endif
1434 return "one\ntwo\nthree"
1435 endfunc
1436 command -nargs=1 -complete=custom,F Tcmd
1437
1438 set nowildmenu
1439 set wildmode=full
1440 let interrupted = 0
1441 try
1442 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1443 catch /^Vim:Interrupt$/
1444 let interrupted = 1
1445 endtry
1446 call assert_equal(1, interrupted)
1447
1448 delcommand Tcmd
1449 delfunc F
1450 set wildmode&
1451endfunc
1452
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001453" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001454func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001455 let str = ":one two\<C-U>"
1456 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001457 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001458 let str ..= "\<Left>five\<Right>"
1459 let str ..= "\<Home>two "
1460 let str ..= "\<C-Left>one "
1461 let str ..= "\<C-Right> three"
1462 let str ..= "\<End>\<S-Left>four "
1463 let str ..= "\<S-Right> six"
1464 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1465 call feedkeys(str, 'xt')
1466 call assert_equal("\"one two three four five six seven", @:)
1467endfunc
1468
1469" Test for moving the cursor on the / command line in 'rightleft' mode
1470func Test_cmdline_edit_rightleft()
1471 CheckFeature rightleft
1472 set rightleft
1473 set rightleftcmd=search
1474 let str = "/one two\<C-U>"
1475 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001476 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001477 let str ..= "\<Right>five\<Left>"
1478 let str ..= "\<Home>two "
1479 let str ..= "\<C-Right>one "
1480 let str ..= "\<C-Left> three"
1481 let str ..= "\<End>\<S-Right>four "
1482 let str ..= "\<S-Left> six"
1483 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1484 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1485 call assert_equal("\"one two three four five six seven", @/)
1486 set rightleftcmd&
1487 set rightleft&
1488endfunc
1489
1490" Test for using <C-\>e in the command line to evaluate an expression
1491func Test_cmdline_expr()
1492 " Evaluate an expression from the beginning of a command line
1493 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1494 call assert_equal('"hello', @:)
1495
1496 " Use an invalid expression for <C-\>e
1497 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1498
1499 " Insert literal <CTRL-\> in the command line
1500 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1501 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001502endfunc
1503
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001504" Test for 'imcmdline' and 'imsearch'
1505" This test doesn't actually test the input method functionality.
1506func Test_cmdline_inputmethod()
1507 new
1508 call setline(1, ['', 'abc', ''])
1509 set imcmdline
1510
1511 call feedkeys(":\"abc\<CR>", 'xt')
1512 call assert_equal("\"abc", @:)
1513 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1514 call assert_equal("\"abc", @:)
1515 call feedkeys("/abc\<CR>", 'xt')
1516 call assert_equal([2, 1], [line('.'), col('.')])
1517 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1518 call assert_equal([2, 1], [line('.'), col('.')])
1519
1520 set imsearch=2
1521 call cursor(1, 1)
1522 call feedkeys("/abc\<CR>", 'xt')
1523 call assert_equal([2, 1], [line('.'), col('.')])
1524 call cursor(1, 1)
1525 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1526 call assert_equal([2, 1], [line('.'), col('.')])
1527 set imdisable
1528 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1529 call assert_equal([2, 1], [line('.'), col('.')])
1530 set imdisable&
1531 set imsearch&
1532
1533 set imcmdline&
1534 %bwipe!
1535endfunc
1536
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001537" Test for recursively getting multiple command line inputs
1538func Test_cmdwin_multi_input()
1539 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1540 call assert_equal('"cyan', @:)
1541endfunc
1542
1543" Test for using CTRL-_ in the command line with 'allowrevins'
1544func Test_cmdline_revins()
1545 CheckNotMSWindows
1546 CheckFeature rightleft
1547 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1548 call assert_equal("\"abc\<c-_>", @:)
1549 set allowrevins
1550 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1551 call assert_equal('"abcñèæ', @:)
1552 set allowrevins&
1553endfunc
1554
1555" Test for typing UTF-8 composing characters in the command line
1556func Test_cmdline_composing_chars()
1557 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1558 call assert_equal('"ゔ', @:)
1559endfunc
1560
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001561" Test for normal mode commands not supported in the cmd window
1562func Test_cmdwin_blocked_commands()
1563 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1564 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1565 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1566 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1567 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1568 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001569 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1570 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1571 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1572 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1573 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1574 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1575 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1576 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1577 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1578 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1579 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1580 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1581 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1582 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1583 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1584 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1585 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1586 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1587 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1588 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1589 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001590endfunc
1591
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001592" Close the Cmd-line window in insert mode using CTRL-C
1593func Test_cmdwin_insert_mode_close()
1594 %bw!
1595 let s = ''
1596 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1597 call assert_equal('Hello', s)
1598 call assert_equal(1, winnr('$'))
1599endfunc
1600
Bram Moolenaar0e717042020-04-27 19:29:01 +02001601" test that ";" works to find a match at the start of the first line
1602func Test_zero_line_search()
1603 new
1604 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1605 call cursor(1,1)
1606 0;/pattern/d
1607 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1608 q!
1609endfunc
1610
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001611func Test_read_shellcmd()
1612 CheckUnix
1613 if executable('ls')
1614 " There should be ls in the $PATH
1615 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1616 call assert_match('^"r! .*\<ls\>', @:)
1617 endif
1618
1619 if executable('rm')
1620 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1621 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1622 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001623
1624 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1625 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1626 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001627 endif
1628endfunc
1629
Bram Moolenaar309976e2019-12-05 18:16:33 +01001630" vim: shiftwidth=2 sts=2 expandtab