blob: 9557828caecc4d1de111d2aa7c29a33d1e13a344 [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
Bram Moolenaarafe8cf62020-10-05 20:07:18 +0200759 " completion of autocmd group after comma
760 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
761 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
762
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100763 " completion for the :augroup command
764 augroup XTest
765 augroup END
766 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
767 call assert_equal("\"augroup XTest", @:)
768 augroup! XTest
769
770 " completion for the :unlet command
771 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
772 call assert_equal("\"unlet one two", @:)
773
774 " completion for the :bdelete command
775 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
776 call assert_equal("\"bdel a b c", @:)
777
778 " completion for the :mapclear command
779 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
780 call assert_equal("\"mapclear <buffer>", @:)
781
782 " completion for user defined commands with menu names
783 menu Test.foo :ls<CR>
784 com -nargs=* -complete=menu MyCmd
785 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
786 call assert_equal('"MyCmd Test.', @:)
787 delcom MyCmd
788 unmenu Test
789
790 " completion for user defined commands with mappings
791 mapclear
792 map <F3> :ls<CR>
793 com -nargs=* -complete=mapping MyCmd
794 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
795 call assert_equal('"MyCmd <F3>', @:)
796 mapclear
797 delcom MyCmd
798
799 " completion for :set path= with multiple backslashes
800 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
801 call assert_equal('"set path=a\\\ b', @:)
802
803 " completion for :set dir= with a backslash
804 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
805 call assert_equal('"set dir=a\ b', @:)
806
807 " completion for the :py3 commands
808 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
809 call assert_equal('"py3 py3do py3file', @:)
810
811 " redir @" is not the start of a comment. So complete after that
812 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
813 call assert_equal('"redir @" | cwindow', @:)
814
815 " completion after a backtick
816 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
817 call assert_equal('"e `a1b2c', @:)
818
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100819 " completion for :language command with an invalid argument
820 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
821 call assert_equal("\"language dummy \t", @:)
822
823 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100824 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
825 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100826
827 " completion with ambiguous user defined commands
828 com TCmd1 echo 'TCmd1'
829 com TCmd2 echo 'TCmd2'
830 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
831 call assert_equal('"TCmd ', @:)
832 delcom TCmd1
833 delcom TCmd2
834
835 " completion after a range followed by a pipe (|) character
836 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
837 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +0200838
839 " use <Esc> as the 'wildchar' for completion
840 set wildchar=<Esc>
841 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
842 call assert_equal('"g/a\xb/clearjumps', @:)
843 " pressing <esc> twice should cancel the command
844 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
845 call assert_equal('"g/a\xb/clearjumps', @:)
846 set wildchar&
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100847endfunc
848
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200849func Test_cmdline_write_alternatefile()
850 new
851 call setline('.', ['one', 'two'])
852 f foo.txt
853 new
854 f #-A
855 call assert_equal('foo.txt-A', expand('%'))
856 f #<-B.txt
857 call assert_equal('foo-B.txt', expand('%'))
858 f %<
859 call assert_equal('foo-B', expand('%'))
860 new
Bram Moolenaare2e40752020-09-04 21:18:46 +0200861 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200862 bw!
863 f foo-B.txt
864 f %<-A
865 call assert_equal('foo-B-A', expand('%'))
866 bw!
867 bw!
868endfunc
869
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100870" using a leading backslash here
871set cpo+=C
872
873func Test_cmdline_search_range()
874 new
875 call setline(1, ['a', 'b', 'c', 'd'])
876 /d
877 1,\/s/b/B/
878 call assert_equal('B', getline(2))
879
880 /a
881 $
882 \?,4s/c/C/
883 call assert_equal('C', getline(3))
884
885 call setline(1, ['a', 'b', 'c', 'd'])
886 %s/c/c/
887 1,\&s/b/B/
888 call assert_equal('B', getline(2))
889
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100890 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200891 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100892
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100893 bwipe!
894endfunc
895
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100896" Test for the tick mark (') in an excmd range
897func Test_tick_mark_in_range()
898 " If only the tick is passed as a range and no command is specified, there
899 " should not be an error
900 call feedkeys(":'\<CR>", 'xt')
901 call assert_equal("'", getreg(':'))
902 call assert_fails("',print", 'E78:')
903endfunc
904
905" Test for using a line number followed by a search pattern as range
906func Test_lnum_and_pattern_as_range()
907 new
908 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
909 let @" = ''
910 2/foo/yank
911 call assert_equal("foo 3\n", @")
912 call assert_equal(1, line('.'))
913 close!
914endfunc
915
Bram Moolenaar65189a12017-02-06 22:22:17 +0100916" Tests for getcmdline(), getcmdpos() and getcmdtype()
917func Check_cmdline(cmdtype)
918 call assert_equal('MyCmd a', getcmdline())
919 call assert_equal(8, getcmdpos())
920 call assert_equal(a:cmdtype, getcmdtype())
921 return ''
922endfunc
923
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200924set cpo&
925
Bram Moolenaar65189a12017-02-06 22:22:17 +0100926func Test_getcmdtype()
927 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
928
929 let cmdtype = ''
930 debuggreedy
931 call feedkeys(":debug echo 'test'\<CR>", "t")
932 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
933 call feedkeys("cont\<CR>", "xt")
934 0debuggreedy
935 call assert_equal('>', cmdtype)
936
937 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
938 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
939
940 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100941 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100942
943 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
944
945 cnoremap <expr> <F6> Check_cmdline('=')
946 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
947 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100948
949 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100950endfunc
951
Bram Moolenaar81612b72018-06-23 14:55:03 +0200952func Test_getcmdwintype()
953 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
954 call assert_equal('/', a)
955
956 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
957 call assert_equal('?', a)
958
959 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
960 call assert_equal(':', a)
961
962 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
963 call assert_equal(':', a)
964
965 call assert_equal('', getcmdwintype())
966endfunc
967
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200968func Test_getcmdwin_autocmd()
969 let s:seq = []
970 augroup CmdWin
971 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
972 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
973 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
974 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
975 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
976 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
977
978 let org_winid = win_getid()
979 let org_bufnr = bufnr()
980 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
981 call assert_equal(':', a)
982 call assert_equal([
983 \ 'WinLeave ' .. org_winid,
984 \ 'WinEnter ' .. s:cmd_winid,
985 \ 'BufLeave ' .. org_bufnr,
986 \ 'BufEnter ' .. s:cmd_bufnr,
987 \ 'CmdWinEnter ' .. s:cmd_winid,
988 \ 'CmdWinLeave ' .. s:cmd_winid,
989 \ 'BufLeave ' .. s:cmd_bufnr,
990 \ 'WinLeave ' .. s:cmd_winid,
991 \ 'WinEnter ' .. org_winid,
992 \ 'BufEnter ' .. org_bufnr,
993 \ ], s:seq)
994
995 au!
996 augroup END
997endfunc
998
Bram Moolenaar52604f22017-04-07 16:17:39 +0200999func Test_verbosefile()
1000 set verbosefile=Xlog
1001 echomsg 'foo'
1002 echomsg 'bar'
1003 set verbosefile=
1004 let log = readfile('Xlog')
1005 call assert_match("foo\nbar", join(log, "\n"))
1006 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001007 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001008 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001009 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +02001010endfunc
1011
Bram Moolenaar4facea32019-10-12 20:17:40 +02001012func Test_verbose_option()
1013 CheckScreendump
1014
1015 let lines =<< trim [SCRIPT]
1016 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1017 call feedkeys("\r", 't') " for the hit-enter prompt
1018 set verbose=20
1019 [SCRIPT]
1020 call writefile(lines, 'XTest_verbose')
1021
1022 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001023 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001024 call term_sendkeys(buf, ":DoSomething\<CR>")
1025 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1026
1027 " clean up
1028 call StopVimInTerminal(buf)
1029 call delete('XTest_verbose')
1030endfunc
1031
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001032func Test_setcmdpos()
1033 func InsertTextAtPos(text, pos)
1034 call assert_equal(0, setcmdpos(a:pos))
1035 return a:text
1036 endfunc
1037
1038 " setcmdpos() with position in the middle of the command line.
1039 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1040 call assert_equal('"1ab2', @:)
1041
1042 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1043 call assert_equal('"1b2a', @:)
1044
1045 " setcmdpos() with position beyond the end of the command line.
1046 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1047 call assert_equal('"12ab', @:)
1048
1049 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001050 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001051endfunc
1052
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001053func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001054 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001055 let encoding_save = &encoding
1056
1057 for e in encodings
1058 exe 'set encoding=' . e
1059
1060 " Test overstrike in the middle of the command line.
1061 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001062 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001063
1064 " Test overstrike going beyond end of command line.
1065 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001066 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001067
1068 " Test toggling insert/overstrike a few times.
1069 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001070 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001071 endfor
1072
Bram Moolenaar30276f22019-01-24 17:59:39 +01001073 " Test overstrike with multi-byte characters.
1074 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001075 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001076
1077 let &encoding = encoding_save
1078endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001079
1080func Test_cmdwin_bug()
1081 let winid = win_getid()
1082 sp
1083 try
1084 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1085 catch /^Vim\%((\a\+)\)\=:E11/
1086 endtry
1087 bw!
1088endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001089
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001090func Test_cmdwin_restore()
1091 CheckScreendump
1092
1093 let lines =<< trim [SCRIPT]
1094 call setline(1, range(30))
1095 2split
1096 [SCRIPT]
1097 call writefile(lines, 'XTest_restore')
1098
1099 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001100 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001101 call term_sendkeys(buf, "q:")
1102 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1103
1104 " normal restore
1105 call term_sendkeys(buf, ":q\<CR>")
1106 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1107
1108 " restore after setting 'lines' with one window
1109 call term_sendkeys(buf, ":close\<CR>")
1110 call term_sendkeys(buf, "q:")
1111 call term_sendkeys(buf, ":set lines=18\<CR>")
1112 call term_sendkeys(buf, ":q\<CR>")
1113 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1114
1115 " clean up
1116 call StopVimInTerminal(buf)
1117 call delete('XTest_restore')
1118endfunc
1119
Bram Moolenaar52410572019-10-27 05:12:45 +01001120func Test_buffers_lastused()
1121 " check that buffers are sorted by time when wildmode has lastused
1122 call test_settime(1550020000) " middle
1123 edit bufa
1124 enew
1125 call test_settime(1550030000) " newest
1126 edit bufb
1127 enew
1128 call test_settime(1550010000) " oldest
1129 edit bufc
1130 enew
1131 call test_settime(0)
1132 enew
1133
1134 call assert_equal(['bufa', 'bufb', 'bufc'],
1135 \ getcompletion('', 'buffer'))
1136
1137 let save_wildmode = &wildmode
1138 set wildmode=full:lastused
1139
1140 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1141 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1142 call assert_equal('b bufb', X)
1143 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1144 call assert_equal('b bufa', X)
1145 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1146 call assert_equal('b bufc', X)
1147 enew
1148
1149 edit other
1150 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1151 call assert_equal('b bufb', X)
1152 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1153 call assert_equal('b bufa', X)
1154 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1155 call assert_equal('b bufc', X)
1156 enew
1157
1158 let &wildmode = save_wildmode
1159
1160 bwipeout bufa
1161 bwipeout bufb
1162 bwipeout bufc
1163endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001164
1165func Test_cmdwin_feedkeys()
1166 " This should not generate E488
1167 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001168 " Using feedkeys with q: only should automatically close the cmd window
1169 call feedkeys('q:', 'xt')
1170 call assert_equal(1, winnr('$'))
1171 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001172endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001173
1174" Tests for the issues fixed in 7.4.441.
1175" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1176func Test_cmdwin_cedit()
1177 exe "set cedit=\<C-c>"
1178 normal! :
1179 call assert_equal(1, winnr('$'))
1180
1181 let g:cmd_wintype = ''
1182 func CmdWinType()
1183 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001184 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001185 return ''
1186 endfunc
1187
1188 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1189 echo input('')
1190 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001191 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001192
1193 set cedit&vim
1194 delfunc CmdWinType
1195endfunc
1196
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001197" Test for CmdwinEnter autocmd
1198func Test_cmdwin_autocmd()
1199 augroup CmdWin
1200 au!
1201 autocmd CmdwinEnter * startinsert
1202 augroup END
1203
1204 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1205 call assert_equal('xyz', @:)
1206
1207 augroup CmdWin
1208 au!
1209 augroup END
1210 augroup! CmdWin
1211endfunc
1212
Bram Moolenaar479950f2020-01-19 15:45:17 +01001213func Test_cmdlineclear_tabenter()
1214 CheckScreendump
1215
1216 let lines =<< trim [SCRIPT]
1217 call setline(1, range(30))
1218 [SCRIPT]
1219
1220 call writefile(lines, 'XtestCmdlineClearTabenter')
1221 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001222 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001223 " in one tab make the command line higher with CTRL-W -
1224 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1225 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1226
1227 call StopVimInTerminal(buf)
1228 call delete('XtestCmdlineClearTabenter')
1229endfunc
1230
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001231" Test for failure in expanding special keywords in cmdline
1232func Test_cmdline_expand_special()
1233 %bwipe!
1234 call assert_fails('e #', 'E499:')
1235 call assert_fails('e <afile>', 'E495:')
1236 call assert_fails('e <abuf>', 'E496:')
1237 call assert_fails('e <amatch>', 'E497:')
1238endfunc
1239
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001240func Test_cmdwin_jump_to_win()
1241 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1242 new
1243 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001244 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001245 close!
1246 call feedkeys("q/:close\<CR>", "xt")
1247 call assert_equal(1, winnr('$'))
1248 call feedkeys("q/:exit\<CR>", "xt")
1249 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001250
1251 " opening command window twice should fail
1252 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1253 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001254endfunc
1255
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001256func Test_cmdwin_interrupted()
1257 CheckScreendump
1258
1259 " aborting the :smile output caused the cmdline window to use the current
1260 " buffer.
1261 let lines =<< trim [SCRIPT]
1262 au WinNew * smile
1263 [SCRIPT]
1264 call writefile(lines, 'XTest_cmdwin')
1265
1266 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001267 " open cmdwin
1268 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001269 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001270 " quit more prompt for :smile command
1271 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001272 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001273 " execute a simple command
1274 call term_sendkeys(buf, "aecho 'done'\<CR>")
1275 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1276
1277 " clean up
1278 call StopVimInTerminal(buf)
1279 call delete('XTest_cmdwin')
1280endfunc
1281
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001282" Test for backtick expression in the command line
1283func Test_cmd_backtick()
1284 %argd
1285 argadd `=['a', 'b', 'c']`
1286 call assert_equal(['a', 'b', 'c'], argv())
1287 %argd
1288endfunc
1289
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001290" Test for the :! command
1291func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001292 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001293
1294 let lines =<< trim [SCRIPT]
1295 " Test for no previous command
1296 call assert_fails('!!', 'E34:')
1297 set nomore
1298 " Test for cmdline expansion with :!
1299 call setline(1, 'foo!')
1300 silent !echo <cWORD> > Xfile.out
1301 call assert_equal(['foo!'], readfile('Xfile.out'))
1302 " Test for using previous command
1303 silent !echo \! !
1304 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1305 call writefile(v:errors, 'Xresult')
1306 call delete('Xfile.out')
1307 qall!
1308 [SCRIPT]
1309 call writefile(lines, 'Xscript')
1310 if RunVim([], [], '--clean -S Xscript')
1311 call assert_equal([], readfile('Xresult'))
1312 endif
1313 call delete('Xscript')
1314 call delete('Xresult')
1315endfunc
1316
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001317" Test error: "E135: *Filter* Autocommands must not change current buffer"
1318func Test_cmd_bang_E135()
1319 new
1320 call setline(1, ['a', 'b', 'c', 'd'])
1321 augroup test_cmd_filter_E135
1322 au!
1323 autocmd FilterReadPost * help
1324 augroup END
1325 call assert_fails('2,3!echo "x"', 'E135:')
1326
1327 augroup test_cmd_filter_E135
1328 au!
1329 augroup END
1330 %bwipe!
1331endfunc
1332
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001333" Test for using ~ for home directory in cmdline completion matches
1334func Test_cmdline_expand_home()
1335 call mkdir('Xdir')
1336 call writefile([], 'Xdir/Xfile1')
1337 call writefile([], 'Xdir/Xfile2')
1338 cd Xdir
1339 let save_HOME = $HOME
1340 let $HOME = getcwd()
1341 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1342 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1343 let $HOME = save_HOME
1344 cd ..
1345 call delete('Xdir', 'rf')
1346endfunc
1347
Bram Moolenaar578fe942020-02-27 21:32:51 +01001348" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1349" or insert mode (when 'insertmode' is set)
1350func Test_cmdline_ctrl_g()
1351 new
1352 call setline(1, 'abc')
1353 call cursor(1, 3)
1354 " If command line is entered from insert mode, using C-\ C-G should back to
1355 " insert mode
1356 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1357 call assert_equal('abxyc', getline(1))
1358 call assert_equal(4, col('.'))
1359
1360 " If command line is entered in 'insertmode', using C-\ C-G should back to
1361 " 'insertmode'
1362 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1363 call assert_equal('ab12xyc', getline(1))
1364 close!
1365endfunc
1366
Bram Moolenaar578fe942020-02-27 21:32:51 +01001367" Test for 'wildmode'
1368func Test_wildmode()
1369 func T(a, c, p)
1370 return "oneA\noneB\noneC"
1371 endfunc
1372 command -nargs=1 -complete=custom,T MyCmd
1373
1374 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001375 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001376 return ''
1377 endfunc
1378 cnoremap <expr> <F2> SaveScreenLine()
1379
1380 set nowildmenu
1381 set wildmode=full,list
1382 let g:Sline = ''
1383 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001384 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001385 call assert_equal('"MyCmd oneA', @:)
1386
1387 set wildmode=longest,full
1388 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1389 call assert_equal('"MyCmd one', @:)
1390 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1391 call assert_equal('"MyCmd oneC', @:)
1392
1393 set wildmode=longest
1394 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1395 call assert_equal('"MyCmd one', @:)
1396
1397 set wildmode=list:longest
1398 let g:Sline = ''
1399 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001400 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001401 call assert_equal('"MyCmd one', @:)
1402
1403 set wildmode=""
1404 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1405 call assert_equal('"MyCmd oneA', @:)
1406
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001407 " Test for wildmode=longest with 'fileignorecase' set
1408 set wildmode=longest
1409 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001410 argadd AAA AAAA AAAAA
1411 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1412 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001413 set fileignorecase&
1414
1415 " Test for listing files with wildmode=list
1416 set wildmode=list
1417 let g:Sline = ''
1418 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001419 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001420 call assert_equal('"b A', @:)
1421
1422 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001423 delcommand MyCmd
1424 delfunc T
1425 delfunc SaveScreenLine
1426 cunmap <F2>
1427 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001428 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001429endfunc
1430
1431" Test for interrupting the command-line completion
1432func Test_interrupt_compl()
1433 func F(lead, cmdl, p)
1434 if a:lead =~ 'tw'
1435 call interrupt()
1436 return
1437 endif
1438 return "one\ntwo\nthree"
1439 endfunc
1440 command -nargs=1 -complete=custom,F Tcmd
1441
1442 set nowildmenu
1443 set wildmode=full
1444 let interrupted = 0
1445 try
1446 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1447 catch /^Vim:Interrupt$/
1448 let interrupted = 1
1449 endtry
1450 call assert_equal(1, interrupted)
1451
1452 delcommand Tcmd
1453 delfunc F
1454 set wildmode&
1455endfunc
1456
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001457" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001458func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001459 let str = ":one two\<C-U>"
1460 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001461 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001462 let str ..= "\<Left>five\<Right>"
1463 let str ..= "\<Home>two "
1464 let str ..= "\<C-Left>one "
1465 let str ..= "\<C-Right> three"
1466 let str ..= "\<End>\<S-Left>four "
1467 let str ..= "\<S-Right> six"
1468 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1469 call feedkeys(str, 'xt')
1470 call assert_equal("\"one two three four five six seven", @:)
1471endfunc
1472
1473" Test for moving the cursor on the / command line in 'rightleft' mode
1474func Test_cmdline_edit_rightleft()
1475 CheckFeature rightleft
1476 set rightleft
1477 set rightleftcmd=search
1478 let str = "/one two\<C-U>"
1479 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001480 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001481 let str ..= "\<Right>five\<Left>"
1482 let str ..= "\<Home>two "
1483 let str ..= "\<C-Right>one "
1484 let str ..= "\<C-Left> three"
1485 let str ..= "\<End>\<S-Right>four "
1486 let str ..= "\<S-Left> six"
1487 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1488 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1489 call assert_equal("\"one two three four five six seven", @/)
1490 set rightleftcmd&
1491 set rightleft&
1492endfunc
1493
1494" Test for using <C-\>e in the command line to evaluate an expression
1495func Test_cmdline_expr()
1496 " Evaluate an expression from the beginning of a command line
1497 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1498 call assert_equal('"hello', @:)
1499
1500 " Use an invalid expression for <C-\>e
1501 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1502
1503 " Insert literal <CTRL-\> in the command line
1504 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1505 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001506endfunc
1507
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001508" Test for 'imcmdline' and 'imsearch'
1509" This test doesn't actually test the input method functionality.
1510func Test_cmdline_inputmethod()
1511 new
1512 call setline(1, ['', 'abc', ''])
1513 set imcmdline
1514
1515 call feedkeys(":\"abc\<CR>", 'xt')
1516 call assert_equal("\"abc", @:)
1517 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1518 call assert_equal("\"abc", @:)
1519 call feedkeys("/abc\<CR>", 'xt')
1520 call assert_equal([2, 1], [line('.'), col('.')])
1521 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1522 call assert_equal([2, 1], [line('.'), col('.')])
1523
1524 set imsearch=2
1525 call cursor(1, 1)
1526 call feedkeys("/abc\<CR>", 'xt')
1527 call assert_equal([2, 1], [line('.'), col('.')])
1528 call cursor(1, 1)
1529 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1530 call assert_equal([2, 1], [line('.'), col('.')])
1531 set imdisable
1532 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1533 call assert_equal([2, 1], [line('.'), col('.')])
1534 set imdisable&
1535 set imsearch&
1536
1537 set imcmdline&
1538 %bwipe!
1539endfunc
1540
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001541" Test for recursively getting multiple command line inputs
1542func Test_cmdwin_multi_input()
1543 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1544 call assert_equal('"cyan', @:)
1545endfunc
1546
1547" Test for using CTRL-_ in the command line with 'allowrevins'
1548func Test_cmdline_revins()
1549 CheckNotMSWindows
1550 CheckFeature rightleft
1551 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1552 call assert_equal("\"abc\<c-_>", @:)
1553 set allowrevins
1554 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1555 call assert_equal('"abcñèæ', @:)
1556 set allowrevins&
1557endfunc
1558
1559" Test for typing UTF-8 composing characters in the command line
1560func Test_cmdline_composing_chars()
1561 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1562 call assert_equal('"ゔ', @:)
1563endfunc
1564
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001565" Test for normal mode commands not supported in the cmd window
1566func Test_cmdwin_blocked_commands()
1567 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1568 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1569 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1570 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1571 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1572 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001573 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1574 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1575 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1576 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1577 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1578 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1579 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1580 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1581 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1582 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1583 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1584 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1585 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1586 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1587 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1588 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1589 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1590 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1591 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1592 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1593 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001594endfunc
1595
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001596" Close the Cmd-line window in insert mode using CTRL-C
1597func Test_cmdwin_insert_mode_close()
1598 %bw!
1599 let s = ''
1600 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1601 call assert_equal('Hello', s)
1602 call assert_equal(1, winnr('$'))
1603endfunc
1604
Bram Moolenaar0e717042020-04-27 19:29:01 +02001605" test that ";" works to find a match at the start of the first line
1606func Test_zero_line_search()
1607 new
1608 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1609 call cursor(1,1)
1610 0;/pattern/d
1611 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1612 q!
1613endfunc
1614
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001615func Test_read_shellcmd()
1616 CheckUnix
1617 if executable('ls')
1618 " There should be ls in the $PATH
1619 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1620 call assert_match('^"r! .*\<ls\>', @:)
1621 endif
1622
1623 if executable('rm')
1624 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1625 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1626 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001627
1628 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1629 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1630 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001631 endif
1632endfunc
1633
Bram Moolenaar309976e2019-12-05 18:16:33 +01001634" vim: shiftwidth=2 sts=2 expandtab