blob: 628db67a60187caec53aeeb6377d5c38ece3ebe7 [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
188 unmap <Middle>x
189 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100190endfunc
191
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100192func Test_match_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200193 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100194 hi Aardig ctermfg=green
195 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
196 call assert_equal('"match Aardig', getreg(':'))
197 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
198 call assert_equal('"match none', getreg(':'))
199endfunc
200
201func Test_highlight_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200202 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100203 hi Aardig ctermfg=green
204 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
205 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200206 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
207 call assert_equal('"hi default Aardig', getreg(':'))
208 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
209 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100210 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
211 call assert_equal('"hi link', getreg(':'))
212 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
213 call assert_equal('"hi default', getreg(':'))
214 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
215 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200216 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
217 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
218 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
219 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200220
221 " A cleared group does not show up in completions.
222 hi Anders ctermfg=green
223 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
224 hi clear Aardig
225 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
226 hi clear Anders
227 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100228endfunc
229
Bram Moolenaar75e15672020-06-28 13:10:22 +0200230" Test for command-line expansion of "hi Ni " (easter egg)
231func Test_highlight_easter_egg()
232 call test_override('ui_delay', 1)
233 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
234 call assert_equal("\"hi Ni \<Tab>", @:)
235 call test_override('ALL', 0)
236endfunc
237
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200238func Test_getcompletion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200239 CheckFeature cmdline_compl
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200240 let groupcount = len(getcompletion('', 'event'))
241 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200242 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200243 call assert_true(matchcount > 0)
244 call assert_true(groupcount > matchcount)
245
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200246 if has('menu')
247 source $VIMRUNTIME/menu.vim
248 let matchcount = len(getcompletion('', 'menu'))
249 call assert_true(matchcount > 0)
250 call assert_equal(['File.'], getcompletion('File', 'menu'))
251 call assert_true(matchcount > 0)
252 let matchcount = len(getcompletion('File.', 'menu'))
253 call assert_true(matchcount > 0)
254 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200255
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200256 let l = getcompletion('v:n', 'var')
257 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200258 let l = getcompletion('v:notexists', 'var')
259 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200260
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200261 args a.c b.c
262 let l = getcompletion('', 'arglist')
263 call assert_equal(['a.c', 'b.c'], l)
264 %argdelete
265
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200266 let l = getcompletion('', 'augroup')
267 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200268 let l = getcompletion('blahblah', 'augroup')
269 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200270
271 let l = getcompletion('', 'behave')
272 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200273 let l = getcompletion('not', 'behave')
274 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200275
276 let l = getcompletion('', 'color')
277 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200278 let l = getcompletion('dirty', 'color')
279 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200280
281 let l = getcompletion('', 'command')
282 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200283 let l = getcompletion('awake', 'command')
284 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200285
286 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200287 call assert_true(index(l, 'samples/') >= 0)
288 let l = getcompletion('NoMatch', 'dir')
289 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200290
291 let l = getcompletion('exe', 'expression')
292 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200293 let l = getcompletion('kill', 'expression')
294 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200295
296 let l = getcompletion('tag', 'function')
297 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200298 let l = getcompletion('paint', 'function')
299 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200300
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200301 let Flambda = {-> 'hello'}
302 let l = getcompletion('', 'function')
303 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200304 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200305
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200306 let l = getcompletion('run', 'file')
307 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200308 let l = getcompletion('walk', 'file')
309 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200310 set wildignore=*.vim
311 let l = getcompletion('run', 'file', 1)
312 call assert_true(index(l, 'runtest.vim') < 0)
313 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200314
315 let l = getcompletion('ha', 'filetype')
316 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200317 let l = getcompletion('horse', 'filetype')
318 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200319
320 let l = getcompletion('z', 'syntax')
321 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200322 let l = getcompletion('emacs', 'syntax')
323 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200324
325 let l = getcompletion('jikes', 'compiler')
326 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200327 let l = getcompletion('break', 'compiler')
328 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200329
330 let l = getcompletion('last', 'help')
331 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200332 let l = getcompletion('giveup', 'help')
333 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200334
335 let l = getcompletion('time', 'option')
336 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200337 let l = getcompletion('space', 'option')
338 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200339
340 let l = getcompletion('er', 'highlight')
341 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200342 let l = getcompletion('dark', 'highlight')
343 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200344
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200345 let l = getcompletion('', 'messages')
346 call assert_true(index(l, 'clear') >= 0)
347 let l = getcompletion('not', 'messages')
348 call assert_equal([], l)
349
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200350 let l = getcompletion('', 'mapclear')
351 call assert_true(index(l, '<buffer>') >= 0)
352 let l = getcompletion('not', 'mapclear')
353 call assert_equal([], l)
354
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200355 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200356 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200357 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
358 let root = has('win32') ? 'C:\\' : '/'
359 let l = getcompletion(root, 'shellcmd')
360 let expected = map(filter(glob(root . '*', 0, 1),
361 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
362 call assert_equal(expected, l)
363
Bram Moolenaarb650b982016-08-05 20:35:13 +0200364 if has('cscope')
365 let l = getcompletion('', 'cscope')
366 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
367 call assert_equal(cmds, l)
368 " using cmdline completion must not change the result
369 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
370 let l = getcompletion('', 'cscope')
371 call assert_equal(cmds, l)
372 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
373 let l = getcompletion('find ', 'cscope')
374 call assert_equal(keys, l)
375 endif
376
Bram Moolenaar7522f692016-08-06 14:12:50 +0200377 if has('signs')
378 sign define Testing linehl=Comment
379 let l = getcompletion('', 'sign')
380 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
381 call assert_equal(cmds, l)
382 " using cmdline completion must not change the result
383 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
384 let l = getcompletion('', 'sign')
385 call assert_equal(cmds, l)
386 let l = getcompletion('list ', 'sign')
387 call assert_equal(['Testing'], l)
388 endif
389
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200390 " Command line completion tests
391 let l = getcompletion('cd ', 'cmdline')
392 call assert_true(index(l, 'samples/') >= 0)
393 let l = getcompletion('cd NoMatch', 'cmdline')
394 call assert_equal([], l)
395 let l = getcompletion('let v:n', 'cmdline')
396 call assert_true(index(l, 'v:null') >= 0)
397 let l = getcompletion('let v:notexists', 'cmdline')
398 call assert_equal([], l)
399 let l = getcompletion('call tag', 'cmdline')
400 call assert_true(index(l, 'taglist(') >= 0)
401 let l = getcompletion('call paint', 'cmdline')
402 call assert_equal([], l)
403
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200404 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200405 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200406 if has('cmdline_hist')
407 call add(names, 'history')
408 endif
409 if has('gettext')
410 call add(names, 'locale')
411 endif
412 if has('profile')
413 call add(names, 'syntime')
414 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200415
416 set tags=Xtags
417 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
418
419 for name in names
420 let matchcount = len(getcompletion('', name))
421 call assert_true(matchcount >= 0, 'No matches for ' . name)
422 endfor
423
424 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200425 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200426
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200427 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200428 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200429 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200430endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200431
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200432func Test_shellcmd_completion()
433 let save_path = $PATH
434
435 call mkdir('Xpathdir/Xpathsubdir', 'p')
436 call writefile([''], 'Xpathdir/Xfile.exe')
437 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
438
439 " Set PATH to example directory without trailing slash.
440 let $PATH = getcwd() . '/Xpathdir'
441
442 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
443 " dirs in the PATH, even though they won't be executed. We check that only
444 " subdirs of the PWD and executables from the PATH are included in the
445 " suggestions.
446 let actual = getcompletion('X', 'shellcmd')
447 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
448 call insert(expected, 'Xfile.exe')
449 call assert_equal(expected, actual)
450
451 call delete('Xpathdir', 'rf')
452 let $PATH = save_path
453endfunc
454
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200455func Test_expand_star_star()
456 call mkdir('a/b', 'p')
457 call writefile(['asdfasdf'], 'a/b/fileXname')
458 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
459 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200460 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200461 call delete('a', 'rf')
462endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100463
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100464func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100465 let @a = "def"
466 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
467 call assert_equal('"abc def ghi', @:)
468
469 new
470 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
471
472 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
473 call assert_equal('"aaa asdf bbb', @:)
474
475 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
476 call assert_equal('"aaa /tmp/some bbb', @:)
477
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200478 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
479 call assert_equal('"aaa '.getline(1).' bbb', @:)
480
Bram Moolenaar21efc362016-12-03 14:05:49 +0100481 set incsearch
482 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
483 call assert_equal('"aaa verylongword bbb', @:)
484
485 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
486 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100487
488 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
489 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100490 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200491
492 " Error while typing a command used to cause that it was not executed
493 " in the end.
494 new
495 try
496 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
497 catch /^Vim\%((\a\+)\)\=:E32/
498 " ignore error E32
499 endtry
500 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100501
Bram Moolenaar578fe942020-02-27 21:32:51 +0100502 " Try to paste an invalid register using <C-R>
503 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
504 call assert_equal('"onetwo', @:)
505
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100506 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100507 let @a = "xy\<C-H>z"
508 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
509 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100510 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
511 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100512 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
513 call assert_equal("\"xy\<C-H>z", @:)
514
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100515 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
516 let @a = "xy\<C-V>z"
517 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
518 call assert_equal('"xyz', @:)
519 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
520 call assert_equal("\"xy\<C-V>z", @:)
521
Bram Moolenaar578fe942020-02-27 21:32:51 +0100522 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
523
Bram Moolenaar72532d32018-04-07 19:09:09 +0200524 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100525endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100526
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100527func Test_cmdline_remove_char()
528 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100529
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100530 for e in ['utf8', 'latin1']
531 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100532
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100533 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
534 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100535
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100536 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
537 call assert_equal('"abcdef', @:)
538
539 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
540 call assert_equal('"abc ghi', @:, e)
541
542 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
543 call assert_equal('"def', @:, e)
544 endfor
545
546 let &encoding = encoding_save
547endfunc
548
549func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200550 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100551
552 set keymap=esperanto
553 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
554 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
555 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100556endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100557
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100558func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100559 new
560 2;'(
561 2;')
562 quit
563endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100564
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100565func Test_illegal_address2()
566 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
567 new
568 source Xtest.vim
569 " Trigger calling validate_cursor()
570 diffsp Xtest.vim
571 quit!
572 bwipe!
573 call delete('Xtest.vim')
574endfunc
575
Bram Moolenaarba47b512017-01-24 21:18:19 +0100576func Test_cmdline_complete_wildoptions()
577 help
578 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
579 let a = join(sort(split(@:)),' ')
580 set wildoptions=tagfile
581 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
582 let b = join(sort(split(@:)),' ')
583 call assert_equal(a, b)
584 bw!
585endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100586
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200587func Test_cmdline_complete_user_cmd()
588 command! -complete=color -nargs=1 Foo :
589 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
590 call assert_equal('"Foo blue', @:)
591 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
592 call assert_equal('"Foo blue', @:)
593 delcommand Foo
594endfunc
595
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100596func s:ScriptLocalFunction()
597 echo 'yes'
598endfunc
599
600func Test_cmdline_complete_user_func()
601 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
602 call assert_match('"func Test_cmdline_complete_user', @:)
603 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
604 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
605endfunc
606
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200607func Test_cmdline_complete_user_names()
608 if has('unix') && executable('whoami')
609 let whoami = systemlist('whoami')[0]
610 let first_letter = whoami[0]
611 if len(first_letter) > 0
612 " Trying completion of :e ~x where x is the first letter of
613 " the user name should complete to at least the user name.
614 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
615 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
616 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200617 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200618 " Just in case: check that the system has an Administrator account.
619 let names = system('net user')
620 if names =~ 'Administrator'
621 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100622 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200623 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100624 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200625 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200626 else
627 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200628 endif
629endfunc
630
Bram Moolenaar297610b2019-12-27 17:20:55 +0100631func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200632 CheckExecutable whoami
633 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
634 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100635endfunc
636
Bram Moolenaar8b633132020-03-20 18:20:51 +0100637func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200638 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
639 call assert_equal(lang, v:lc_time)
640
641 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
642 call assert_equal(lang, v:ctype)
643
644 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
645 call assert_equal(lang, v:collate)
646
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200647 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200648 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200649
650 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200651 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200652
Bram Moolenaarec680282020-06-12 19:35:32 +0200653 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200654
Bram Moolenaarec680282020-06-12 19:35:32 +0200655 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
656 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200657
Bram Moolenaarec680282020-06-12 19:35:32 +0200658 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
659 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200660
Bram Moolenaarec680282020-06-12 19:35:32 +0200661 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
662 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200663
664 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
665 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200666endfunc
667
Bram Moolenaar297610b2019-12-27 17:20:55 +0100668func Test_cmdline_complete_env_variable()
669 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100670 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
671 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100672 unlet $X_VIM_TEST_COMPLETE_ENV
673endfunc
674
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100675" Test for various command-line completion
676func Test_cmdline_complete_various()
677 " completion for a command starting with a comment
678 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
679 call assert_equal("\" :|\"\<C-A>", @:)
680
681 " completion for a range followed by a comment
682 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
683 call assert_equal("\"1,2\"\<C-A>", @:)
684
685 " completion for :k command
686 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
687 call assert_equal("\"ka\<C-A>", @:)
688
689 " completion for short version of the :s command
690 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
691 call assert_equal("\"sI \<C-A>", @:)
692
693 " completion for :write command
694 call mkdir('Xdir')
695 call writefile(['one'], 'Xdir/Xfile1')
696 let save_cwd = getcwd()
697 cd Xdir
698 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
699 call assert_equal("\"w >> Xfile1", @:)
700 call chdir(save_cwd)
701 call delete('Xdir', 'rf')
702
703 " completion for :w ! and :r ! commands
704 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
705 call assert_equal("\"w !invalid_xyz_cmd", @:)
706 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
707 call assert_equal("\"r !invalid_xyz_cmd", @:)
708
709 " completion for :>> and :<< commands
710 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
711 call assert_equal("\">>>\<C-A>", @:)
712 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
713 call assert_equal("\"<<<\<C-A>", @:)
714
715 " completion for command with +cmd argument
716 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
717 call assert_equal("\"buffer +/pat Xabc", @:)
718 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
719 call assert_equal("\"buffer +/pat\<C-A>", @:)
720
721 " completion for a command with a trailing comment
722 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
723 call assert_equal("\"ls \" comment\<C-A>", @:)
724
725 " completion for a command with a trailing command
726 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
727 call assert_equal("\"ls | ls", @:)
728
729 " completion for a command with an CTRL-V escaped argument
730 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
731 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
732
733 " completion for a command that doesn't take additional arguments
734 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
735 call assert_equal("\"all abc\<C-A>", @:)
736
737 " completion for a command with a command modifier
738 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
739 call assert_equal("\"topleft new", @:)
740
741 " completion for the :match command
742 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
743 call assert_equal("\"match Search /pat/\<C-A>", @:)
744
745 " completion for the :s command
746 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
747 call assert_equal("\"s/from/to/g\<C-A>", @:)
748
749 " completion for the :dlist command
750 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
751 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
752
753 " completion for the :doautocmd command
754 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
755 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
756
757 " completion for the :augroup command
758 augroup XTest
759 augroup END
760 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
761 call assert_equal("\"augroup XTest", @:)
762 augroup! XTest
763
764 " completion for the :unlet command
765 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
766 call assert_equal("\"unlet one two", @:)
767
768 " completion for the :bdelete command
769 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
770 call assert_equal("\"bdel a b c", @:)
771
772 " completion for the :mapclear command
773 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
774 call assert_equal("\"mapclear <buffer>", @:)
775
776 " completion for user defined commands with menu names
777 menu Test.foo :ls<CR>
778 com -nargs=* -complete=menu MyCmd
779 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
780 call assert_equal('"MyCmd Test.', @:)
781 delcom MyCmd
782 unmenu Test
783
784 " completion for user defined commands with mappings
785 mapclear
786 map <F3> :ls<CR>
787 com -nargs=* -complete=mapping MyCmd
788 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
789 call assert_equal('"MyCmd <F3>', @:)
790 mapclear
791 delcom MyCmd
792
793 " completion for :set path= with multiple backslashes
794 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
795 call assert_equal('"set path=a\\\ b', @:)
796
797 " completion for :set dir= with a backslash
798 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
799 call assert_equal('"set dir=a\ b', @:)
800
801 " completion for the :py3 commands
802 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
803 call assert_equal('"py3 py3do py3file', @:)
804
805 " redir @" is not the start of a comment. So complete after that
806 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
807 call assert_equal('"redir @" | cwindow', @:)
808
809 " completion after a backtick
810 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
811 call assert_equal('"e `a1b2c', @:)
812
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100813 " completion for :language command with an invalid argument
814 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
815 call assert_equal("\"language dummy \t", @:)
816
817 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100818 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
819 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100820
821 " completion with ambiguous user defined commands
822 com TCmd1 echo 'TCmd1'
823 com TCmd2 echo 'TCmd2'
824 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
825 call assert_equal('"TCmd ', @:)
826 delcom TCmd1
827 delcom TCmd2
828
829 " completion after a range followed by a pipe (|) character
830 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
831 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100832endfunc
833
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200834func Test_cmdline_write_alternatefile()
835 new
836 call setline('.', ['one', 'two'])
837 f foo.txt
838 new
839 f #-A
840 call assert_equal('foo.txt-A', expand('%'))
841 f #<-B.txt
842 call assert_equal('foo-B.txt', expand('%'))
843 f %<
844 call assert_equal('foo-B', expand('%'))
845 new
Bram Moolenaare2e40752020-09-04 21:18:46 +0200846 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200847 bw!
848 f foo-B.txt
849 f %<-A
850 call assert_equal('foo-B-A', expand('%'))
851 bw!
852 bw!
853endfunc
854
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100855" using a leading backslash here
856set cpo+=C
857
858func Test_cmdline_search_range()
859 new
860 call setline(1, ['a', 'b', 'c', 'd'])
861 /d
862 1,\/s/b/B/
863 call assert_equal('B', getline(2))
864
865 /a
866 $
867 \?,4s/c/C/
868 call assert_equal('C', getline(3))
869
870 call setline(1, ['a', 'b', 'c', 'd'])
871 %s/c/c/
872 1,\&s/b/B/
873 call assert_equal('B', getline(2))
874
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100875 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200876 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100877
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100878 bwipe!
879endfunc
880
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100881" Test for the tick mark (') in an excmd range
882func Test_tick_mark_in_range()
883 " If only the tick is passed as a range and no command is specified, there
884 " should not be an error
885 call feedkeys(":'\<CR>", 'xt')
886 call assert_equal("'", getreg(':'))
887 call assert_fails("',print", 'E78:')
888endfunc
889
890" Test for using a line number followed by a search pattern as range
891func Test_lnum_and_pattern_as_range()
892 new
893 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
894 let @" = ''
895 2/foo/yank
896 call assert_equal("foo 3\n", @")
897 call assert_equal(1, line('.'))
898 close!
899endfunc
900
Bram Moolenaar65189a12017-02-06 22:22:17 +0100901" Tests for getcmdline(), getcmdpos() and getcmdtype()
902func Check_cmdline(cmdtype)
903 call assert_equal('MyCmd a', getcmdline())
904 call assert_equal(8, getcmdpos())
905 call assert_equal(a:cmdtype, getcmdtype())
906 return ''
907endfunc
908
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200909set cpo&
910
Bram Moolenaar65189a12017-02-06 22:22:17 +0100911func Test_getcmdtype()
912 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
913
914 let cmdtype = ''
915 debuggreedy
916 call feedkeys(":debug echo 'test'\<CR>", "t")
917 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
918 call feedkeys("cont\<CR>", "xt")
919 0debuggreedy
920 call assert_equal('>', cmdtype)
921
922 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
923 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
924
925 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100926 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100927
928 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
929
930 cnoremap <expr> <F6> Check_cmdline('=')
931 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
932 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100933
934 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100935endfunc
936
Bram Moolenaar81612b72018-06-23 14:55:03 +0200937func Test_getcmdwintype()
938 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
939 call assert_equal('/', a)
940
941 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
942 call assert_equal('?', a)
943
944 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
945 call assert_equal(':', a)
946
947 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
948 call assert_equal(':', a)
949
950 call assert_equal('', getcmdwintype())
951endfunc
952
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200953func Test_getcmdwin_autocmd()
954 let s:seq = []
955 augroup CmdWin
956 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
957 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
958 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
959 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
960 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
961 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
962
963 let org_winid = win_getid()
964 let org_bufnr = bufnr()
965 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
966 call assert_equal(':', a)
967 call assert_equal([
968 \ 'WinLeave ' .. org_winid,
969 \ 'WinEnter ' .. s:cmd_winid,
970 \ 'BufLeave ' .. org_bufnr,
971 \ 'BufEnter ' .. s:cmd_bufnr,
972 \ 'CmdWinEnter ' .. s:cmd_winid,
973 \ 'CmdWinLeave ' .. s:cmd_winid,
974 \ 'BufLeave ' .. s:cmd_bufnr,
975 \ 'WinLeave ' .. s:cmd_winid,
976 \ 'WinEnter ' .. org_winid,
977 \ 'BufEnter ' .. org_bufnr,
978 \ ], s:seq)
979
980 au!
981 augroup END
982endfunc
983
Bram Moolenaar52604f22017-04-07 16:17:39 +0200984func Test_verbosefile()
985 set verbosefile=Xlog
986 echomsg 'foo'
987 echomsg 'bar'
988 set verbosefile=
989 let log = readfile('Xlog')
990 call assert_match("foo\nbar", join(log, "\n"))
991 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200992 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200993 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200994 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +0200995endfunc
996
Bram Moolenaar4facea32019-10-12 20:17:40 +0200997func Test_verbose_option()
998 CheckScreendump
999
1000 let lines =<< trim [SCRIPT]
1001 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1002 call feedkeys("\r", 't') " for the hit-enter prompt
1003 set verbose=20
1004 [SCRIPT]
1005 call writefile(lines, 'XTest_verbose')
1006
1007 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001008 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001009 call term_sendkeys(buf, ":DoSomething\<CR>")
1010 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1011
1012 " clean up
1013 call StopVimInTerminal(buf)
1014 call delete('XTest_verbose')
1015endfunc
1016
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001017func Test_setcmdpos()
1018 func InsertTextAtPos(text, pos)
1019 call assert_equal(0, setcmdpos(a:pos))
1020 return a:text
1021 endfunc
1022
1023 " setcmdpos() with position in the middle of the command line.
1024 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1025 call assert_equal('"1ab2', @:)
1026
1027 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1028 call assert_equal('"1b2a', @:)
1029
1030 " setcmdpos() with position beyond the end of the command line.
1031 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1032 call assert_equal('"12ab', @:)
1033
1034 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001035 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001036endfunc
1037
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001038func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001039 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001040 let encoding_save = &encoding
1041
1042 for e in encodings
1043 exe 'set encoding=' . e
1044
1045 " Test overstrike in the middle of the command line.
1046 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001047 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001048
1049 " Test overstrike going beyond end of command line.
1050 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001051 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001052
1053 " Test toggling insert/overstrike a few times.
1054 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001055 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001056 endfor
1057
Bram Moolenaar30276f22019-01-24 17:59:39 +01001058 " Test overstrike with multi-byte characters.
1059 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001060 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001061
1062 let &encoding = encoding_save
1063endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001064
1065func Test_cmdwin_bug()
1066 let winid = win_getid()
1067 sp
1068 try
1069 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1070 catch /^Vim\%((\a\+)\)\=:E11/
1071 endtry
1072 bw!
1073endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001074
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001075func Test_cmdwin_restore()
1076 CheckScreendump
1077
1078 let lines =<< trim [SCRIPT]
1079 call setline(1, range(30))
1080 2split
1081 [SCRIPT]
1082 call writefile(lines, 'XTest_restore')
1083
1084 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001085 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001086 call term_sendkeys(buf, "q:")
1087 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1088
1089 " normal restore
1090 call term_sendkeys(buf, ":q\<CR>")
1091 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1092
1093 " restore after setting 'lines' with one window
1094 call term_sendkeys(buf, ":close\<CR>")
1095 call term_sendkeys(buf, "q:")
1096 call term_sendkeys(buf, ":set lines=18\<CR>")
1097 call term_sendkeys(buf, ":q\<CR>")
1098 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1099
1100 " clean up
1101 call StopVimInTerminal(buf)
1102 call delete('XTest_restore')
1103endfunc
1104
Bram Moolenaar52410572019-10-27 05:12:45 +01001105func Test_buffers_lastused()
1106 " check that buffers are sorted by time when wildmode has lastused
1107 call test_settime(1550020000) " middle
1108 edit bufa
1109 enew
1110 call test_settime(1550030000) " newest
1111 edit bufb
1112 enew
1113 call test_settime(1550010000) " oldest
1114 edit bufc
1115 enew
1116 call test_settime(0)
1117 enew
1118
1119 call assert_equal(['bufa', 'bufb', 'bufc'],
1120 \ getcompletion('', 'buffer'))
1121
1122 let save_wildmode = &wildmode
1123 set wildmode=full:lastused
1124
1125 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1126 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1127 call assert_equal('b bufb', X)
1128 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1129 call assert_equal('b bufa', X)
1130 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1131 call assert_equal('b bufc', X)
1132 enew
1133
1134 edit other
1135 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1136 call assert_equal('b bufb', X)
1137 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1138 call assert_equal('b bufa', X)
1139 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1140 call assert_equal('b bufc', X)
1141 enew
1142
1143 let &wildmode = save_wildmode
1144
1145 bwipeout bufa
1146 bwipeout bufb
1147 bwipeout bufc
1148endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001149
1150func Test_cmdwin_feedkeys()
1151 " This should not generate E488
1152 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001153 " Using feedkeys with q: only should automatically close the cmd window
1154 call feedkeys('q:', 'xt')
1155 call assert_equal(1, winnr('$'))
1156 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001157endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001158
1159" Tests for the issues fixed in 7.4.441.
1160" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1161func Test_cmdwin_cedit()
1162 exe "set cedit=\<C-c>"
1163 normal! :
1164 call assert_equal(1, winnr('$'))
1165
1166 let g:cmd_wintype = ''
1167 func CmdWinType()
1168 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001169 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001170 return ''
1171 endfunc
1172
1173 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1174 echo input('')
1175 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001176 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001177
1178 set cedit&vim
1179 delfunc CmdWinType
1180endfunc
1181
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001182" Test for CmdwinEnter autocmd
1183func Test_cmdwin_autocmd()
1184 augroup CmdWin
1185 au!
1186 autocmd CmdwinEnter * startinsert
1187 augroup END
1188
1189 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1190 call assert_equal('xyz', @:)
1191
1192 augroup CmdWin
1193 au!
1194 augroup END
1195 augroup! CmdWin
1196endfunc
1197
Bram Moolenaar479950f2020-01-19 15:45:17 +01001198func Test_cmdlineclear_tabenter()
1199 CheckScreendump
1200
1201 let lines =<< trim [SCRIPT]
1202 call setline(1, range(30))
1203 [SCRIPT]
1204
1205 call writefile(lines, 'XtestCmdlineClearTabenter')
1206 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001207 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001208 " in one tab make the command line higher with CTRL-W -
1209 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1210 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1211
1212 call StopVimInTerminal(buf)
1213 call delete('XtestCmdlineClearTabenter')
1214endfunc
1215
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001216" Test for failure in expanding special keywords in cmdline
1217func Test_cmdline_expand_special()
1218 %bwipe!
1219 call assert_fails('e #', 'E499:')
1220 call assert_fails('e <afile>', 'E495:')
1221 call assert_fails('e <abuf>', 'E496:')
1222 call assert_fails('e <amatch>', 'E497:')
1223endfunc
1224
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001225func Test_cmdwin_jump_to_win()
1226 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1227 new
1228 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001229 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001230 close!
1231 call feedkeys("q/:close\<CR>", "xt")
1232 call assert_equal(1, winnr('$'))
1233 call feedkeys("q/:exit\<CR>", "xt")
1234 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001235
1236 " opening command window twice should fail
1237 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1238 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001239endfunc
1240
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001241func Test_cmdwin_interrupted()
1242 CheckScreendump
1243
1244 " aborting the :smile output caused the cmdline window to use the current
1245 " buffer.
1246 let lines =<< trim [SCRIPT]
1247 au WinNew * smile
1248 [SCRIPT]
1249 call writefile(lines, 'XTest_cmdwin')
1250
1251 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001252 " open cmdwin
1253 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001254 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001255 " quit more prompt for :smile command
1256 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001257 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001258 " execute a simple command
1259 call term_sendkeys(buf, "aecho 'done'\<CR>")
1260 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1261
1262 " clean up
1263 call StopVimInTerminal(buf)
1264 call delete('XTest_cmdwin')
1265endfunc
1266
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001267" Test for backtick expression in the command line
1268func Test_cmd_backtick()
1269 %argd
1270 argadd `=['a', 'b', 'c']`
1271 call assert_equal(['a', 'b', 'c'], argv())
1272 %argd
1273endfunc
1274
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001275" Test for the :! command
1276func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001277 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001278
1279 let lines =<< trim [SCRIPT]
1280 " Test for no previous command
1281 call assert_fails('!!', 'E34:')
1282 set nomore
1283 " Test for cmdline expansion with :!
1284 call setline(1, 'foo!')
1285 silent !echo <cWORD> > Xfile.out
1286 call assert_equal(['foo!'], readfile('Xfile.out'))
1287 " Test for using previous command
1288 silent !echo \! !
1289 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1290 call writefile(v:errors, 'Xresult')
1291 call delete('Xfile.out')
1292 qall!
1293 [SCRIPT]
1294 call writefile(lines, 'Xscript')
1295 if RunVim([], [], '--clean -S Xscript')
1296 call assert_equal([], readfile('Xresult'))
1297 endif
1298 call delete('Xscript')
1299 call delete('Xresult')
1300endfunc
1301
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001302" Test error: "E135: *Filter* Autocommands must not change current buffer"
1303func Test_cmd_bang_E135()
1304 new
1305 call setline(1, ['a', 'b', 'c', 'd'])
1306 augroup test_cmd_filter_E135
1307 au!
1308 autocmd FilterReadPost * help
1309 augroup END
1310 call assert_fails('2,3!echo "x"', 'E135:')
1311
1312 augroup test_cmd_filter_E135
1313 au!
1314 augroup END
1315 %bwipe!
1316endfunc
1317
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001318" Test for using ~ for home directory in cmdline completion matches
1319func Test_cmdline_expand_home()
1320 call mkdir('Xdir')
1321 call writefile([], 'Xdir/Xfile1')
1322 call writefile([], 'Xdir/Xfile2')
1323 cd Xdir
1324 let save_HOME = $HOME
1325 let $HOME = getcwd()
1326 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1327 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1328 let $HOME = save_HOME
1329 cd ..
1330 call delete('Xdir', 'rf')
1331endfunc
1332
Bram Moolenaar578fe942020-02-27 21:32:51 +01001333" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1334" or insert mode (when 'insertmode' is set)
1335func Test_cmdline_ctrl_g()
1336 new
1337 call setline(1, 'abc')
1338 call cursor(1, 3)
1339 " If command line is entered from insert mode, using C-\ C-G should back to
1340 " insert mode
1341 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1342 call assert_equal('abxyc', getline(1))
1343 call assert_equal(4, col('.'))
1344
1345 " If command line is entered in 'insertmode', using C-\ C-G should back to
1346 " 'insertmode'
1347 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1348 call assert_equal('ab12xyc', getline(1))
1349 close!
1350endfunc
1351
Bram Moolenaar578fe942020-02-27 21:32:51 +01001352" Test for 'wildmode'
1353func Test_wildmode()
1354 func T(a, c, p)
1355 return "oneA\noneB\noneC"
1356 endfunc
1357 command -nargs=1 -complete=custom,T MyCmd
1358
1359 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001360 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001361 return ''
1362 endfunc
1363 cnoremap <expr> <F2> SaveScreenLine()
1364
1365 set nowildmenu
1366 set wildmode=full,list
1367 let g:Sline = ''
1368 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001369 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001370 call assert_equal('"MyCmd oneA', @:)
1371
1372 set wildmode=longest,full
1373 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1374 call assert_equal('"MyCmd one', @:)
1375 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1376 call assert_equal('"MyCmd oneC', @:)
1377
1378 set wildmode=longest
1379 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1380 call assert_equal('"MyCmd one', @:)
1381
1382 set wildmode=list:longest
1383 let g:Sline = ''
1384 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001385 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001386 call assert_equal('"MyCmd one', @:)
1387
1388 set wildmode=""
1389 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1390 call assert_equal('"MyCmd oneA', @:)
1391
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001392 " Test for wildmode=longest with 'fileignorecase' set
1393 set wildmode=longest
1394 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001395 argadd AAA AAAA AAAAA
1396 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1397 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001398 set fileignorecase&
1399
1400 " Test for listing files with wildmode=list
1401 set wildmode=list
1402 let g:Sline = ''
1403 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001404 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001405 call assert_equal('"b A', @:)
1406
1407 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001408 delcommand MyCmd
1409 delfunc T
1410 delfunc SaveScreenLine
1411 cunmap <F2>
1412 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001413 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001414endfunc
1415
1416" Test for interrupting the command-line completion
1417func Test_interrupt_compl()
1418 func F(lead, cmdl, p)
1419 if a:lead =~ 'tw'
1420 call interrupt()
1421 return
1422 endif
1423 return "one\ntwo\nthree"
1424 endfunc
1425 command -nargs=1 -complete=custom,F Tcmd
1426
1427 set nowildmenu
1428 set wildmode=full
1429 let interrupted = 0
1430 try
1431 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1432 catch /^Vim:Interrupt$/
1433 let interrupted = 1
1434 endtry
1435 call assert_equal(1, interrupted)
1436
1437 delcommand Tcmd
1438 delfunc F
1439 set wildmode&
1440endfunc
1441
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001442" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001443func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001444 let str = ":one two\<C-U>"
1445 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001446 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001447 let str ..= "\<Left>five\<Right>"
1448 let str ..= "\<Home>two "
1449 let str ..= "\<C-Left>one "
1450 let str ..= "\<C-Right> three"
1451 let str ..= "\<End>\<S-Left>four "
1452 let str ..= "\<S-Right> six"
1453 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1454 call feedkeys(str, 'xt')
1455 call assert_equal("\"one two three four five six seven", @:)
1456endfunc
1457
1458" Test for moving the cursor on the / command line in 'rightleft' mode
1459func Test_cmdline_edit_rightleft()
1460 CheckFeature rightleft
1461 set rightleft
1462 set rightleftcmd=search
1463 let str = "/one two\<C-U>"
1464 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001465 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001466 let str ..= "\<Right>five\<Left>"
1467 let str ..= "\<Home>two "
1468 let str ..= "\<C-Right>one "
1469 let str ..= "\<C-Left> three"
1470 let str ..= "\<End>\<S-Right>four "
1471 let str ..= "\<S-Left> six"
1472 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1473 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1474 call assert_equal("\"one two three four five six seven", @/)
1475 set rightleftcmd&
1476 set rightleft&
1477endfunc
1478
1479" Test for using <C-\>e in the command line to evaluate an expression
1480func Test_cmdline_expr()
1481 " Evaluate an expression from the beginning of a command line
1482 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1483 call assert_equal('"hello', @:)
1484
1485 " Use an invalid expression for <C-\>e
1486 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1487
1488 " Insert literal <CTRL-\> in the command line
1489 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1490 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001491endfunc
1492
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001493" Test for 'imcmdline' and 'imsearch'
1494" This test doesn't actually test the input method functionality.
1495func Test_cmdline_inputmethod()
1496 new
1497 call setline(1, ['', 'abc', ''])
1498 set imcmdline
1499
1500 call feedkeys(":\"abc\<CR>", 'xt')
1501 call assert_equal("\"abc", @:)
1502 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1503 call assert_equal("\"abc", @:)
1504 call feedkeys("/abc\<CR>", 'xt')
1505 call assert_equal([2, 1], [line('.'), col('.')])
1506 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1507 call assert_equal([2, 1], [line('.'), col('.')])
1508
1509 set imsearch=2
1510 call cursor(1, 1)
1511 call feedkeys("/abc\<CR>", 'xt')
1512 call assert_equal([2, 1], [line('.'), col('.')])
1513 call cursor(1, 1)
1514 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1515 call assert_equal([2, 1], [line('.'), col('.')])
1516 set imdisable
1517 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1518 call assert_equal([2, 1], [line('.'), col('.')])
1519 set imdisable&
1520 set imsearch&
1521
1522 set imcmdline&
1523 %bwipe!
1524endfunc
1525
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001526" Test for recursively getting multiple command line inputs
1527func Test_cmdwin_multi_input()
1528 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1529 call assert_equal('"cyan', @:)
1530endfunc
1531
1532" Test for using CTRL-_ in the command line with 'allowrevins'
1533func Test_cmdline_revins()
1534 CheckNotMSWindows
1535 CheckFeature rightleft
1536 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1537 call assert_equal("\"abc\<c-_>", @:)
1538 set allowrevins
1539 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1540 call assert_equal('"abcñèæ', @:)
1541 set allowrevins&
1542endfunc
1543
1544" Test for typing UTF-8 composing characters in the command line
1545func Test_cmdline_composing_chars()
1546 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1547 call assert_equal('"ゔ', @:)
1548endfunc
1549
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001550" Test for normal mode commands not supported in the cmd window
1551func Test_cmdwin_blocked_commands()
1552 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1553 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1554 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1555 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1556 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1557 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001558 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1559 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1560 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1561 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1562 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1563 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1564 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1565 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1566 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1567 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1568 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1569 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1570 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1571 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1572 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1573 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1574 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1575 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1576 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1577 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1578 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001579endfunc
1580
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001581" Close the Cmd-line window in insert mode using CTRL-C
1582func Test_cmdwin_insert_mode_close()
1583 %bw!
1584 let s = ''
1585 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1586 call assert_equal('Hello', s)
1587 call assert_equal(1, winnr('$'))
1588endfunc
1589
Bram Moolenaar0e717042020-04-27 19:29:01 +02001590" test that ";" works to find a match at the start of the first line
1591func Test_zero_line_search()
1592 new
1593 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1594 call cursor(1,1)
1595 0;/pattern/d
1596 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1597 q!
1598endfunc
1599
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001600func Test_read_shellcmd()
1601 CheckUnix
1602 if executable('ls')
1603 " There should be ls in the $PATH
1604 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1605 call assert_match('^"r! .*\<ls\>', @:)
1606 endif
1607
1608 if executable('rm')
1609 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1610 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1611 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001612
1613 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1614 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1615 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001616 endif
1617endfunc
1618
Bram Moolenaar309976e2019-12-05 18:16:33 +01001619" vim: shiftwidth=2 sts=2 expandtab