blob: 1c5cac76bf1f5ae7f0e7cb7350fab666db138c1e [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 Moolenaar4b2ce122020-11-21 21:41:41 +010087
Bram Moolenaara60053b2020-09-03 16:50:13 +020088func 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 Moolenaarcf5fdf72017-03-02 23:05:51 +0100115func Test_map_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200116 CheckFeature cmdline_compl
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100117 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
118 call assert_equal('"map <unique> <silent>', getreg(':'))
119 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
120 call assert_equal('"map <script> <unique>', getreg(':'))
121 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
122 call assert_equal('"map <expr> <script>', getreg(':'))
123 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
124 call assert_equal('"map <buffer> <expr>', getreg(':'))
125 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
126 call assert_equal('"map <nowait> <buffer>', getreg(':'))
127 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
128 call assert_equal('"map <special> <nowait>', getreg(':'))
129 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
130 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200131
Bram Moolenaar1776a282019-05-03 16:05:41 +0200132 map <Middle>x middle
133
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200134 map ,f commaf
135 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200136 map <Left> left
137 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200138 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
139 call assert_equal('"map ,f', getreg(':'))
140 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
141 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200142 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
143 call assert_equal('"map <Left>', getreg(':'))
144 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200145 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200146 unmap ,f
147 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200148 unmap <Left>
149 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200150
151 set cpo-=< cpo-=B cpo-=k
152 map <Left> left
153 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
154 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200155 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200156 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200157 unmap <Left>
158
159 set cpo+=<
160 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200161 exe "set t_k6=\<Esc>[17~"
162 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200163 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
164 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200165 if !has('gui_running')
166 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
167 call assert_equal("\"map <F6>x", getreg(':'))
168 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200169 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200170 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200171 set cpo-=<
172
173 set cpo+=B
174 map <Left> left
175 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
176 call assert_equal('"map <Left>', getreg(':'))
177 unmap <Left>
178 set cpo-=B
179
180 set cpo+=k
181 map <Left> left
182 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
183 call assert_equal('"map <Left>', getreg(':'))
184 unmap <Left>
185 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200186
Bram Moolenaar531be472020-09-23 22:38:05 +0200187 call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
188
Bram Moolenaar1776a282019-05-03 16:05:41 +0200189 unmap <Middle>x
190 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100191endfunc
192
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100193func Test_match_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200194 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100195 hi Aardig ctermfg=green
196 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
197 call assert_equal('"match Aardig', getreg(':'))
198 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
199 call assert_equal('"match none', getreg(':'))
200endfunc
201
202func Test_highlight_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200203 CheckFeature cmdline_compl
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100204 hi Aardig ctermfg=green
205 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
206 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200207 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
208 call assert_equal('"hi default Aardig', getreg(':'))
209 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
210 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100211 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
212 call assert_equal('"hi link', getreg(':'))
213 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
214 call assert_equal('"hi default', getreg(':'))
215 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
216 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200217 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
218 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
219 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
220 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200221
222 " A cleared group does not show up in completions.
223 hi Anders ctermfg=green
224 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
225 hi clear Aardig
226 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
227 hi clear Anders
228 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100229endfunc
230
Bram Moolenaar75e15672020-06-28 13:10:22 +0200231" Test for command-line expansion of "hi Ni " (easter egg)
232func Test_highlight_easter_egg()
233 call test_override('ui_delay', 1)
234 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
235 call assert_equal("\"hi Ni \<Tab>", @:)
236 call test_override('ALL', 0)
237endfunc
238
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200239func Test_getcompletion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200240 CheckFeature cmdline_compl
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200241 let groupcount = len(getcompletion('', 'event'))
242 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200243 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200244 call assert_true(matchcount > 0)
245 call assert_true(groupcount > matchcount)
246
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200247 if has('menu')
248 source $VIMRUNTIME/menu.vim
249 let matchcount = len(getcompletion('', 'menu'))
250 call assert_true(matchcount > 0)
251 call assert_equal(['File.'], getcompletion('File', 'menu'))
252 call assert_true(matchcount > 0)
253 let matchcount = len(getcompletion('File.', 'menu'))
254 call assert_true(matchcount > 0)
255 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200256
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200257 let l = getcompletion('v:n', 'var')
258 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200259 let l = getcompletion('v:notexists', 'var')
260 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200261
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200262 args a.c b.c
263 let l = getcompletion('', 'arglist')
264 call assert_equal(['a.c', 'b.c'], l)
265 %argdelete
266
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200267 let l = getcompletion('', 'augroup')
268 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200269 let l = getcompletion('blahblah', 'augroup')
270 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200271
272 let l = getcompletion('', 'behave')
273 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200274 let l = getcompletion('not', 'behave')
275 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200276
277 let l = getcompletion('', 'color')
278 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200279 let l = getcompletion('dirty', 'color')
280 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200281
282 let l = getcompletion('', 'command')
283 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200284 let l = getcompletion('awake', 'command')
285 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200286
287 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200288 call assert_true(index(l, 'samples/') >= 0)
289 let l = getcompletion('NoMatch', 'dir')
290 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200291
292 let l = getcompletion('exe', 'expression')
293 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200294 let l = getcompletion('kill', 'expression')
295 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200296
297 let l = getcompletion('tag', 'function')
298 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200299 let l = getcompletion('paint', 'function')
300 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200301
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200302 let Flambda = {-> 'hello'}
303 let l = getcompletion('', 'function')
304 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200305 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200306
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200307 let l = getcompletion('run', 'file')
308 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200309 let l = getcompletion('walk', 'file')
310 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200311 set wildignore=*.vim
312 let l = getcompletion('run', 'file', 1)
313 call assert_true(index(l, 'runtest.vim') < 0)
314 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200315
316 let l = getcompletion('ha', 'filetype')
317 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200318 let l = getcompletion('horse', 'filetype')
319 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200320
321 let l = getcompletion('z', 'syntax')
322 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200323 let l = getcompletion('emacs', 'syntax')
324 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200325
326 let l = getcompletion('jikes', 'compiler')
327 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200328 let l = getcompletion('break', 'compiler')
329 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200330
331 let l = getcompletion('last', 'help')
332 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200333 let l = getcompletion('giveup', 'help')
334 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200335
336 let l = getcompletion('time', 'option')
337 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200338 let l = getcompletion('space', 'option')
339 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200340
341 let l = getcompletion('er', 'highlight')
342 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200343 let l = getcompletion('dark', 'highlight')
344 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200345
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200346 let l = getcompletion('', 'messages')
347 call assert_true(index(l, 'clear') >= 0)
348 let l = getcompletion('not', 'messages')
349 call assert_equal([], l)
350
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200351 let l = getcompletion('', 'mapclear')
352 call assert_true(index(l, '<buffer>') >= 0)
353 let l = getcompletion('not', 'mapclear')
354 call assert_equal([], l)
355
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200356 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200357 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200358 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
359 let root = has('win32') ? 'C:\\' : '/'
360 let l = getcompletion(root, 'shellcmd')
361 let expected = map(filter(glob(root . '*', 0, 1),
362 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
363 call assert_equal(expected, l)
364
Bram Moolenaarb650b982016-08-05 20:35:13 +0200365 if has('cscope')
366 let l = getcompletion('', 'cscope')
367 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
368 call assert_equal(cmds, l)
369 " using cmdline completion must not change the result
370 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
371 let l = getcompletion('', 'cscope')
372 call assert_equal(cmds, l)
373 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
374 let l = getcompletion('find ', 'cscope')
375 call assert_equal(keys, l)
376 endif
377
Bram Moolenaar7522f692016-08-06 14:12:50 +0200378 if has('signs')
379 sign define Testing linehl=Comment
380 let l = getcompletion('', 'sign')
381 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
382 call assert_equal(cmds, l)
383 " using cmdline completion must not change the result
384 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
385 let l = getcompletion('', 'sign')
386 call assert_equal(cmds, l)
387 let l = getcompletion('list ', 'sign')
388 call assert_equal(['Testing'], l)
389 endif
390
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200391 " Command line completion tests
392 let l = getcompletion('cd ', 'cmdline')
393 call assert_true(index(l, 'samples/') >= 0)
394 let l = getcompletion('cd NoMatch', 'cmdline')
395 call assert_equal([], l)
396 let l = getcompletion('let v:n', 'cmdline')
397 call assert_true(index(l, 'v:null') >= 0)
398 let l = getcompletion('let v:notexists', 'cmdline')
399 call assert_equal([], l)
400 let l = getcompletion('call tag', 'cmdline')
401 call assert_true(index(l, 'taglist(') >= 0)
402 let l = getcompletion('call paint', 'cmdline')
403 call assert_equal([], l)
404
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200405 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200406 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200407 if has('cmdline_hist')
408 call add(names, 'history')
409 endif
410 if has('gettext')
411 call add(names, 'locale')
412 endif
413 if has('profile')
414 call add(names, 'syntime')
415 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200416
417 set tags=Xtags
418 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
419
420 for name in names
421 let matchcount = len(getcompletion('', name))
422 call assert_true(matchcount >= 0, 'No matches for ' . name)
423 endfor
424
425 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200426 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200427
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200428 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200429 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200430 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200431endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200432
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200433func Test_shellcmd_completion()
434 let save_path = $PATH
435
436 call mkdir('Xpathdir/Xpathsubdir', 'p')
437 call writefile([''], 'Xpathdir/Xfile.exe')
438 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
439
440 " Set PATH to example directory without trailing slash.
441 let $PATH = getcwd() . '/Xpathdir'
442
443 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
444 " dirs in the PATH, even though they won't be executed. We check that only
445 " subdirs of the PWD and executables from the PATH are included in the
446 " suggestions.
447 let actual = getcompletion('X', 'shellcmd')
448 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
449 call insert(expected, 'Xfile.exe')
450 call assert_equal(expected, actual)
451
452 call delete('Xpathdir', 'rf')
453 let $PATH = save_path
454endfunc
455
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200456func Test_expand_star_star()
457 call mkdir('a/b', 'p')
458 call writefile(['asdfasdf'], 'a/b/fileXname')
459 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
460 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200461 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200462 call delete('a', 'rf')
463endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100464
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100465func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100466 let @a = "def"
467 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
468 call assert_equal('"abc def ghi', @:)
469
470 new
471 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
472
473 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
474 call assert_equal('"aaa asdf bbb', @:)
475
476 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
477 call assert_equal('"aaa /tmp/some bbb', @:)
478
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200479 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
480 call assert_equal('"aaa '.getline(1).' bbb', @:)
481
Bram Moolenaar21efc362016-12-03 14:05:49 +0100482 set incsearch
483 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
484 call assert_equal('"aaa verylongword bbb', @:)
485
486 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
487 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100488
489 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
490 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100491 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200492
493 " Error while typing a command used to cause that it was not executed
494 " in the end.
495 new
496 try
497 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
498 catch /^Vim\%((\a\+)\)\=:E32/
499 " ignore error E32
500 endtry
501 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100502
Bram Moolenaar578fe942020-02-27 21:32:51 +0100503 " Try to paste an invalid register using <C-R>
504 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
505 call assert_equal('"onetwo', @:)
506
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100507 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100508 let @a = "xy\<C-H>z"
509 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
510 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100511 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
512 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100513 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
514 call assert_equal("\"xy\<C-H>z", @:)
515
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100516 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
517 let @a = "xy\<C-V>z"
518 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
519 call assert_equal('"xyz', @:)
520 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
521 call assert_equal("\"xy\<C-V>z", @:)
522
Bram Moolenaar578fe942020-02-27 21:32:51 +0100523 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
524
Bram Moolenaar72532d32018-04-07 19:09:09 +0200525 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100526endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100527
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100528func Test_cmdline_remove_char()
529 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100530
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100531 for e in ['utf8', 'latin1']
532 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100533
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100534 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
535 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100536
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100537 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
538 call assert_equal('"abcdef', @:)
539
540 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
541 call assert_equal('"abc ghi', @:, e)
542
543 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
544 call assert_equal('"def', @:, e)
545 endfor
546
547 let &encoding = encoding_save
548endfunc
549
550func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200551 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100552
553 set keymap=esperanto
554 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
555 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
556 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100557endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100558
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100559func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100560 new
561 2;'(
562 2;')
563 quit
564endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100565
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100566func Test_illegal_address2()
567 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
568 new
569 source Xtest.vim
570 " Trigger calling validate_cursor()
571 diffsp Xtest.vim
572 quit!
573 bwipe!
574 call delete('Xtest.vim')
575endfunc
576
Bram Moolenaarba47b512017-01-24 21:18:19 +0100577func Test_cmdline_complete_wildoptions()
578 help
579 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
580 let a = join(sort(split(@:)),' ')
581 set wildoptions=tagfile
582 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
583 let b = join(sort(split(@:)),' ')
584 call assert_equal(a, b)
585 bw!
586endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100587
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200588func Test_cmdline_complete_user_cmd()
589 command! -complete=color -nargs=1 Foo :
590 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
591 call assert_equal('"Foo blue', @:)
592 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
593 call assert_equal('"Foo blue', @:)
594 delcommand Foo
595endfunc
596
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100597func s:ScriptLocalFunction()
598 echo 'yes'
599endfunc
600
601func Test_cmdline_complete_user_func()
602 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
603 call assert_match('"func Test_cmdline_complete_user', @:)
604 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
605 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
606endfunc
607
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200608func Test_cmdline_complete_user_names()
609 if has('unix') && executable('whoami')
610 let whoami = systemlist('whoami')[0]
611 let first_letter = whoami[0]
612 if len(first_letter) > 0
613 " Trying completion of :e ~x where x is the first letter of
614 " the user name should complete to at least the user name.
615 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
616 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
617 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200618 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200619 " Just in case: check that the system has an Administrator account.
620 let names = system('net user')
621 if names =~ 'Administrator'
622 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100623 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200624 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100625 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200626 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200627 else
628 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200629 endif
630endfunc
631
Bram Moolenaar297610b2019-12-27 17:20:55 +0100632func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200633 CheckExecutable whoami
634 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
635 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100636endfunc
637
Bram Moolenaar8b633132020-03-20 18:20:51 +0100638func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200639 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
640 call assert_equal(lang, v:lc_time)
641
642 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
643 call assert_equal(lang, v:ctype)
644
645 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
646 call assert_equal(lang, v:collate)
647
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200648 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200649 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200650
651 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200652 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200653
Bram Moolenaarec680282020-06-12 19:35:32 +0200654 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200655
Bram Moolenaarec680282020-06-12 19:35:32 +0200656 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
657 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200658
Bram Moolenaarec680282020-06-12 19:35:32 +0200659 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
660 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200661
Bram Moolenaarec680282020-06-12 19:35:32 +0200662 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
663 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200664
665 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
666 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200667endfunc
668
Bram Moolenaar297610b2019-12-27 17:20:55 +0100669func Test_cmdline_complete_env_variable()
670 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100671 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
672 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100673 unlet $X_VIM_TEST_COMPLETE_ENV
674endfunc
675
Bram Moolenaar4941b5e2020-12-24 17:15:53 +0100676func Test_cmdline_complete_expression()
677 let g:SomeVar = 'blah'
678 for cmd in ['exe', 'echo', 'echon', 'echomsg']
679 call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
680 call assert_match('"' .. cmd .. ' SomeVar', @:)
681 call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
682 call assert_match('"' .. cmd .. ' foo SomeVar', @:)
683 endfor
684 unlet g:SomeVar
685endfunc
686
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100687" Test for various command-line completion
688func Test_cmdline_complete_various()
689 " completion for a command starting with a comment
690 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
691 call assert_equal("\" :|\"\<C-A>", @:)
692
693 " completion for a range followed by a comment
694 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
695 call assert_equal("\"1,2\"\<C-A>", @:)
696
697 " completion for :k command
698 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
699 call assert_equal("\"ka\<C-A>", @:)
700
701 " completion for short version of the :s command
702 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
703 call assert_equal("\"sI \<C-A>", @:)
704
705 " completion for :write command
706 call mkdir('Xdir')
707 call writefile(['one'], 'Xdir/Xfile1')
708 let save_cwd = getcwd()
709 cd Xdir
710 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
711 call assert_equal("\"w >> Xfile1", @:)
712 call chdir(save_cwd)
713 call delete('Xdir', 'rf')
714
715 " completion for :w ! and :r ! commands
716 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
717 call assert_equal("\"w !invalid_xyz_cmd", @:)
718 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
719 call assert_equal("\"r !invalid_xyz_cmd", @:)
720
721 " completion for :>> and :<< commands
722 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
723 call assert_equal("\">>>\<C-A>", @:)
724 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
725 call assert_equal("\"<<<\<C-A>", @:)
726
727 " completion for command with +cmd argument
728 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
729 call assert_equal("\"buffer +/pat Xabc", @:)
730 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
731 call assert_equal("\"buffer +/pat\<C-A>", @:)
732
733 " completion for a command with a trailing comment
734 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
735 call assert_equal("\"ls \" comment\<C-A>", @:)
736
737 " completion for a command with a trailing command
738 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
739 call assert_equal("\"ls | ls", @:)
740
741 " completion for a command with an CTRL-V escaped argument
742 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
743 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
744
745 " completion for a command that doesn't take additional arguments
746 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
747 call assert_equal("\"all abc\<C-A>", @:)
748
749 " completion for a command with a command modifier
750 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
751 call assert_equal("\"topleft new", @:)
752
753 " completion for the :match command
754 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
755 call assert_equal("\"match Search /pat/\<C-A>", @:)
756
757 " completion for the :s command
758 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
759 call assert_equal("\"s/from/to/g\<C-A>", @:)
760
761 " completion for the :dlist command
762 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
763 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
764
765 " completion for the :doautocmd command
766 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
767 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
768
Bram Moolenaarafe8cf62020-10-05 20:07:18 +0200769 " completion of autocmd group after comma
770 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
771 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
772
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100773 " completion for the :augroup command
774 augroup XTest
775 augroup END
776 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
777 call assert_equal("\"augroup XTest", @:)
778 augroup! XTest
779
780 " completion for the :unlet command
781 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
782 call assert_equal("\"unlet one two", @:)
783
784 " completion for the :bdelete command
785 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
786 call assert_equal("\"bdel a b c", @:)
787
788 " completion for the :mapclear command
789 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
790 call assert_equal("\"mapclear <buffer>", @:)
791
792 " completion for user defined commands with menu names
793 menu Test.foo :ls<CR>
794 com -nargs=* -complete=menu MyCmd
795 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
796 call assert_equal('"MyCmd Test.', @:)
797 delcom MyCmd
798 unmenu Test
799
800 " completion for user defined commands with mappings
801 mapclear
802 map <F3> :ls<CR>
803 com -nargs=* -complete=mapping MyCmd
804 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
805 call assert_equal('"MyCmd <F3>', @:)
806 mapclear
807 delcom MyCmd
808
809 " completion for :set path= with multiple backslashes
810 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
811 call assert_equal('"set path=a\\\ b', @:)
812
813 " completion for :set dir= with a backslash
814 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
815 call assert_equal('"set dir=a\ b', @:)
816
817 " completion for the :py3 commands
818 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
819 call assert_equal('"py3 py3do py3file', @:)
820
821 " redir @" is not the start of a comment. So complete after that
822 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
823 call assert_equal('"redir @" | cwindow', @:)
824
825 " completion after a backtick
826 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
827 call assert_equal('"e `a1b2c', @:)
828
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100829 " completion for :language command with an invalid argument
830 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
831 call assert_equal("\"language dummy \t", @:)
832
833 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100834 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
835 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100836
837 " completion with ambiguous user defined commands
838 com TCmd1 echo 'TCmd1'
839 com TCmd2 echo 'TCmd2'
840 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
841 call assert_equal('"TCmd ', @:)
842 delcom TCmd1
843 delcom TCmd2
844
845 " completion after a range followed by a pipe (|) character
846 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
847 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +0200848
849 " use <Esc> as the 'wildchar' for completion
850 set wildchar=<Esc>
851 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
852 call assert_equal('"g/a\xb/clearjumps', @:)
853 " pressing <esc> twice should cancel the command
854 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
855 call assert_equal('"g/a\xb/clearjumps', @:)
856 set wildchar&
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100857endfunc
858
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200859func Test_cmdline_write_alternatefile()
860 new
861 call setline('.', ['one', 'two'])
862 f foo.txt
863 new
864 f #-A
865 call assert_equal('foo.txt-A', expand('%'))
866 f #<-B.txt
867 call assert_equal('foo-B.txt', expand('%'))
868 f %<
869 call assert_equal('foo-B', expand('%'))
870 new
Bram Moolenaare2e40752020-09-04 21:18:46 +0200871 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200872 bw!
873 f foo-B.txt
874 f %<-A
875 call assert_equal('foo-B-A', expand('%'))
876 bw!
877 bw!
878endfunc
879
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100880" using a leading backslash here
881set cpo+=C
882
883func Test_cmdline_search_range()
884 new
885 call setline(1, ['a', 'b', 'c', 'd'])
886 /d
887 1,\/s/b/B/
888 call assert_equal('B', getline(2))
889
890 /a
891 $
892 \?,4s/c/C/
893 call assert_equal('C', getline(3))
894
895 call setline(1, ['a', 'b', 'c', 'd'])
896 %s/c/c/
897 1,\&s/b/B/
898 call assert_equal('B', getline(2))
899
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100900 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200901 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100902
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100903 bwipe!
904endfunc
905
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100906" Test for the tick mark (') in an excmd range
907func Test_tick_mark_in_range()
908 " If only the tick is passed as a range and no command is specified, there
909 " should not be an error
910 call feedkeys(":'\<CR>", 'xt')
911 call assert_equal("'", getreg(':'))
912 call assert_fails("',print", 'E78:')
913endfunc
914
915" Test for using a line number followed by a search pattern as range
916func Test_lnum_and_pattern_as_range()
917 new
918 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
919 let @" = ''
920 2/foo/yank
921 call assert_equal("foo 3\n", @")
922 call assert_equal(1, line('.'))
923 close!
924endfunc
925
Bram Moolenaar65189a12017-02-06 22:22:17 +0100926" Tests for getcmdline(), getcmdpos() and getcmdtype()
927func Check_cmdline(cmdtype)
928 call assert_equal('MyCmd a', getcmdline())
929 call assert_equal(8, getcmdpos())
930 call assert_equal(a:cmdtype, getcmdtype())
931 return ''
932endfunc
933
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200934set cpo&
935
Bram Moolenaar65189a12017-02-06 22:22:17 +0100936func Test_getcmdtype()
937 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
938
939 let cmdtype = ''
940 debuggreedy
941 call feedkeys(":debug echo 'test'\<CR>", "t")
942 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
943 call feedkeys("cont\<CR>", "xt")
944 0debuggreedy
945 call assert_equal('>', cmdtype)
946
947 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
948 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
949
950 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +0100951 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +0100952
953 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
954
955 cnoremap <expr> <F6> Check_cmdline('=')
956 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
957 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100958
959 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +0100960endfunc
961
Bram Moolenaar81612b72018-06-23 14:55:03 +0200962func Test_getcmdwintype()
963 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
964 call assert_equal('/', a)
965
966 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
967 call assert_equal('?', a)
968
969 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
970 call assert_equal(':', a)
971
972 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
973 call assert_equal(':', a)
974
975 call assert_equal('', getcmdwintype())
976endfunc
977
Bram Moolenaar96e38a82019-09-09 18:35:33 +0200978func Test_getcmdwin_autocmd()
979 let s:seq = []
980 augroup CmdWin
981 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
982 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
983 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
984 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
985 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
986 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
987
988 let org_winid = win_getid()
989 let org_bufnr = bufnr()
990 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
991 call assert_equal(':', a)
992 call assert_equal([
993 \ 'WinLeave ' .. org_winid,
994 \ 'WinEnter ' .. s:cmd_winid,
995 \ 'BufLeave ' .. org_bufnr,
996 \ 'BufEnter ' .. s:cmd_bufnr,
997 \ 'CmdWinEnter ' .. s:cmd_winid,
998 \ 'CmdWinLeave ' .. s:cmd_winid,
999 \ 'BufLeave ' .. s:cmd_bufnr,
1000 \ 'WinLeave ' .. s:cmd_winid,
1001 \ 'WinEnter ' .. org_winid,
1002 \ 'BufEnter ' .. org_bufnr,
1003 \ ], s:seq)
1004
1005 au!
1006 augroup END
1007endfunc
1008
Bram Moolenaar52604f22017-04-07 16:17:39 +02001009func Test_verbosefile()
1010 set verbosefile=Xlog
1011 echomsg 'foo'
1012 echomsg 'bar'
1013 set verbosefile=
1014 let log = readfile('Xlog')
1015 call assert_match("foo\nbar", join(log, "\n"))
1016 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001017 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001018 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001019 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +02001020endfunc
1021
Bram Moolenaar4facea32019-10-12 20:17:40 +02001022func Test_verbose_option()
1023 CheckScreendump
1024
1025 let lines =<< trim [SCRIPT]
1026 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1027 call feedkeys("\r", 't') " for the hit-enter prompt
1028 set verbose=20
1029 [SCRIPT]
1030 call writefile(lines, 'XTest_verbose')
1031
1032 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001033 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001034 call term_sendkeys(buf, ":DoSomething\<CR>")
1035 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1036
1037 " clean up
1038 call StopVimInTerminal(buf)
1039 call delete('XTest_verbose')
1040endfunc
1041
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001042func Test_setcmdpos()
1043 func InsertTextAtPos(text, pos)
1044 call assert_equal(0, setcmdpos(a:pos))
1045 return a:text
1046 endfunc
1047
1048 " setcmdpos() with position in the middle of the command line.
1049 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1050 call assert_equal('"1ab2', @:)
1051
1052 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1053 call assert_equal('"1b2a', @:)
1054
1055 " setcmdpos() with position beyond the end of the command line.
1056 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1057 call assert_equal('"12ab', @:)
1058
1059 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001060 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001061endfunc
1062
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001063func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001064 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001065 let encoding_save = &encoding
1066
1067 for e in encodings
1068 exe 'set encoding=' . e
1069
1070 " Test overstrike in the middle of the command line.
1071 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001072 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001073
1074 " Test overstrike going beyond end of command line.
1075 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001076 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001077
1078 " Test toggling insert/overstrike a few times.
1079 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001080 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001081 endfor
1082
Bram Moolenaar30276f22019-01-24 17:59:39 +01001083 " Test overstrike with multi-byte characters.
1084 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001085 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001086
1087 let &encoding = encoding_save
1088endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001089
1090func Test_cmdwin_bug()
1091 let winid = win_getid()
1092 sp
1093 try
1094 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1095 catch /^Vim\%((\a\+)\)\=:E11/
1096 endtry
1097 bw!
1098endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001099
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001100func Test_cmdwin_restore()
1101 CheckScreendump
1102
1103 let lines =<< trim [SCRIPT]
1104 call setline(1, range(30))
1105 2split
1106 [SCRIPT]
1107 call writefile(lines, 'XTest_restore')
1108
1109 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001110 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001111 call term_sendkeys(buf, "q:")
1112 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1113
1114 " normal restore
1115 call term_sendkeys(buf, ":q\<CR>")
1116 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1117
1118 " restore after setting 'lines' with one window
1119 call term_sendkeys(buf, ":close\<CR>")
1120 call term_sendkeys(buf, "q:")
1121 call term_sendkeys(buf, ":set lines=18\<CR>")
1122 call term_sendkeys(buf, ":q\<CR>")
1123 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1124
1125 " clean up
1126 call StopVimInTerminal(buf)
1127 call delete('XTest_restore')
1128endfunc
1129
Bram Moolenaar52410572019-10-27 05:12:45 +01001130func Test_buffers_lastused()
1131 " check that buffers are sorted by time when wildmode has lastused
1132 call test_settime(1550020000) " middle
1133 edit bufa
1134 enew
1135 call test_settime(1550030000) " newest
1136 edit bufb
1137 enew
1138 call test_settime(1550010000) " oldest
1139 edit bufc
1140 enew
1141 call test_settime(0)
1142 enew
1143
1144 call assert_equal(['bufa', 'bufb', 'bufc'],
1145 \ getcompletion('', 'buffer'))
1146
1147 let save_wildmode = &wildmode
1148 set wildmode=full:lastused
1149
1150 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1151 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1152 call assert_equal('b bufb', X)
1153 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1154 call assert_equal('b bufa', X)
1155 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1156 call assert_equal('b bufc', X)
1157 enew
1158
1159 edit other
1160 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1161 call assert_equal('b bufb', X)
1162 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1163 call assert_equal('b bufa', X)
1164 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1165 call assert_equal('b bufc', X)
1166 enew
1167
1168 let &wildmode = save_wildmode
1169
1170 bwipeout bufa
1171 bwipeout bufb
1172 bwipeout bufc
1173endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001174
1175func Test_cmdwin_feedkeys()
1176 " This should not generate E488
1177 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001178 " Using feedkeys with q: only should automatically close the cmd window
1179 call feedkeys('q:', 'xt')
1180 call assert_equal(1, winnr('$'))
1181 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001182endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001183
1184" Tests for the issues fixed in 7.4.441.
1185" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1186func Test_cmdwin_cedit()
1187 exe "set cedit=\<C-c>"
1188 normal! :
1189 call assert_equal(1, winnr('$'))
1190
1191 let g:cmd_wintype = ''
1192 func CmdWinType()
1193 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001194 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001195 return ''
1196 endfunc
1197
1198 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1199 echo input('')
1200 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001201 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001202
1203 set cedit&vim
1204 delfunc CmdWinType
1205endfunc
1206
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001207" Test for CmdwinEnter autocmd
1208func Test_cmdwin_autocmd()
1209 augroup CmdWin
1210 au!
1211 autocmd CmdwinEnter * startinsert
1212 augroup END
1213
1214 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1215 call assert_equal('xyz', @:)
1216
1217 augroup CmdWin
1218 au!
1219 augroup END
1220 augroup! CmdWin
1221endfunc
1222
Bram Moolenaar479950f2020-01-19 15:45:17 +01001223func Test_cmdlineclear_tabenter()
1224 CheckScreendump
1225
1226 let lines =<< trim [SCRIPT]
1227 call setline(1, range(30))
1228 [SCRIPT]
1229
1230 call writefile(lines, 'XtestCmdlineClearTabenter')
1231 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001232 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001233 " in one tab make the command line higher with CTRL-W -
1234 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1235 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1236
1237 call StopVimInTerminal(buf)
1238 call delete('XtestCmdlineClearTabenter')
1239endfunc
1240
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001241" Test for failure in expanding special keywords in cmdline
1242func Test_cmdline_expand_special()
1243 %bwipe!
1244 call assert_fails('e #', 'E499:')
1245 call assert_fails('e <afile>', 'E495:')
1246 call assert_fails('e <abuf>', 'E496:')
1247 call assert_fails('e <amatch>', 'E497:')
1248endfunc
1249
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001250func Test_cmdwin_jump_to_win()
1251 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1252 new
1253 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001254 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001255 close!
1256 call feedkeys("q/:close\<CR>", "xt")
1257 call assert_equal(1, winnr('$'))
1258 call feedkeys("q/:exit\<CR>", "xt")
1259 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001260
1261 " opening command window twice should fail
1262 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1263 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001264endfunc
1265
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001266func Test_cmdwin_interrupted()
1267 CheckScreendump
1268
1269 " aborting the :smile output caused the cmdline window to use the current
1270 " buffer.
1271 let lines =<< trim [SCRIPT]
1272 au WinNew * smile
1273 [SCRIPT]
1274 call writefile(lines, 'XTest_cmdwin')
1275
1276 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001277 " open cmdwin
1278 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001279 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001280 " quit more prompt for :smile command
1281 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001282 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001283 " execute a simple command
1284 call term_sendkeys(buf, "aecho 'done'\<CR>")
1285 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1286
1287 " clean up
1288 call StopVimInTerminal(buf)
1289 call delete('XTest_cmdwin')
1290endfunc
1291
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001292" Test for backtick expression in the command line
1293func Test_cmd_backtick()
1294 %argd
1295 argadd `=['a', 'b', 'c']`
1296 call assert_equal(['a', 'b', 'c'], argv())
1297 %argd
1298endfunc
1299
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001300" Test for the :! command
1301func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001302 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001303
1304 let lines =<< trim [SCRIPT]
1305 " Test for no previous command
1306 call assert_fails('!!', 'E34:')
1307 set nomore
1308 " Test for cmdline expansion with :!
1309 call setline(1, 'foo!')
1310 silent !echo <cWORD> > Xfile.out
1311 call assert_equal(['foo!'], readfile('Xfile.out'))
1312 " Test for using previous command
1313 silent !echo \! !
1314 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1315 call writefile(v:errors, 'Xresult')
1316 call delete('Xfile.out')
1317 qall!
1318 [SCRIPT]
1319 call writefile(lines, 'Xscript')
1320 if RunVim([], [], '--clean -S Xscript')
1321 call assert_equal([], readfile('Xresult'))
1322 endif
1323 call delete('Xscript')
1324 call delete('Xresult')
1325endfunc
1326
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001327" Test error: "E135: *Filter* Autocommands must not change current buffer"
1328func Test_cmd_bang_E135()
1329 new
1330 call setline(1, ['a', 'b', 'c', 'd'])
1331 augroup test_cmd_filter_E135
1332 au!
1333 autocmd FilterReadPost * help
1334 augroup END
1335 call assert_fails('2,3!echo "x"', 'E135:')
1336
1337 augroup test_cmd_filter_E135
1338 au!
1339 augroup END
1340 %bwipe!
1341endfunc
1342
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001343" Test for using ~ for home directory in cmdline completion matches
1344func Test_cmdline_expand_home()
1345 call mkdir('Xdir')
1346 call writefile([], 'Xdir/Xfile1')
1347 call writefile([], 'Xdir/Xfile2')
1348 cd Xdir
1349 let save_HOME = $HOME
1350 let $HOME = getcwd()
1351 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1352 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1353 let $HOME = save_HOME
1354 cd ..
1355 call delete('Xdir', 'rf')
1356endfunc
1357
Bram Moolenaar578fe942020-02-27 21:32:51 +01001358" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1359" or insert mode (when 'insertmode' is set)
1360func Test_cmdline_ctrl_g()
1361 new
1362 call setline(1, 'abc')
1363 call cursor(1, 3)
1364 " If command line is entered from insert mode, using C-\ C-G should back to
1365 " insert mode
1366 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1367 call assert_equal('abxyc', getline(1))
1368 call assert_equal(4, col('.'))
1369
1370 " If command line is entered in 'insertmode', using C-\ C-G should back to
1371 " 'insertmode'
1372 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1373 call assert_equal('ab12xyc', getline(1))
1374 close!
1375endfunc
1376
Bram Moolenaar578fe942020-02-27 21:32:51 +01001377" Test for 'wildmode'
1378func Test_wildmode()
1379 func T(a, c, p)
1380 return "oneA\noneB\noneC"
1381 endfunc
1382 command -nargs=1 -complete=custom,T MyCmd
1383
1384 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001385 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001386 return ''
1387 endfunc
1388 cnoremap <expr> <F2> SaveScreenLine()
1389
1390 set nowildmenu
1391 set wildmode=full,list
1392 let g:Sline = ''
1393 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001394 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001395 call assert_equal('"MyCmd oneA', @:)
1396
1397 set wildmode=longest,full
1398 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1399 call assert_equal('"MyCmd one', @:)
1400 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1401 call assert_equal('"MyCmd oneC', @:)
1402
1403 set wildmode=longest
1404 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1405 call assert_equal('"MyCmd one', @:)
1406
1407 set wildmode=list:longest
1408 let g:Sline = ''
1409 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001410 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001411 call assert_equal('"MyCmd one', @:)
1412
1413 set wildmode=""
1414 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1415 call assert_equal('"MyCmd oneA', @:)
1416
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001417 " Test for wildmode=longest with 'fileignorecase' set
1418 set wildmode=longest
1419 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001420 argadd AAA AAAA AAAAA
1421 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1422 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001423 set fileignorecase&
1424
1425 " Test for listing files with wildmode=list
1426 set wildmode=list
1427 let g:Sline = ''
1428 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001429 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001430 call assert_equal('"b A', @:)
1431
1432 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001433 delcommand MyCmd
1434 delfunc T
1435 delfunc SaveScreenLine
1436 cunmap <F2>
1437 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001438 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001439endfunc
1440
1441" Test for interrupting the command-line completion
1442func Test_interrupt_compl()
1443 func F(lead, cmdl, p)
1444 if a:lead =~ 'tw'
1445 call interrupt()
1446 return
1447 endif
1448 return "one\ntwo\nthree"
1449 endfunc
1450 command -nargs=1 -complete=custom,F Tcmd
1451
1452 set nowildmenu
1453 set wildmode=full
1454 let interrupted = 0
1455 try
1456 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1457 catch /^Vim:Interrupt$/
1458 let interrupted = 1
1459 endtry
1460 call assert_equal(1, interrupted)
1461
1462 delcommand Tcmd
1463 delfunc F
1464 set wildmode&
1465endfunc
1466
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001467" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001468func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001469 let str = ":one two\<C-U>"
1470 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001471 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001472 let str ..= "\<Left>five\<Right>"
1473 let str ..= "\<Home>two "
1474 let str ..= "\<C-Left>one "
1475 let str ..= "\<C-Right> three"
1476 let str ..= "\<End>\<S-Left>four "
1477 let str ..= "\<S-Right> six"
1478 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1479 call feedkeys(str, 'xt')
1480 call assert_equal("\"one two three four five six seven", @:)
1481endfunc
1482
1483" Test for moving the cursor on the / command line in 'rightleft' mode
1484func Test_cmdline_edit_rightleft()
1485 CheckFeature rightleft
1486 set rightleft
1487 set rightleftcmd=search
1488 let str = "/one two\<C-U>"
1489 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001490 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001491 let str ..= "\<Right>five\<Left>"
1492 let str ..= "\<Home>two "
1493 let str ..= "\<C-Right>one "
1494 let str ..= "\<C-Left> three"
1495 let str ..= "\<End>\<S-Right>four "
1496 let str ..= "\<S-Left> six"
1497 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1498 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1499 call assert_equal("\"one two three four five six seven", @/)
1500 set rightleftcmd&
1501 set rightleft&
1502endfunc
1503
1504" Test for using <C-\>e in the command line to evaluate an expression
1505func Test_cmdline_expr()
1506 " Evaluate an expression from the beginning of a command line
1507 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1508 call assert_equal('"hello', @:)
1509
1510 " Use an invalid expression for <C-\>e
1511 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1512
1513 " Insert literal <CTRL-\> in the command line
1514 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1515 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001516endfunc
1517
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001518" Test for 'imcmdline' and 'imsearch'
1519" This test doesn't actually test the input method functionality.
1520func Test_cmdline_inputmethod()
1521 new
1522 call setline(1, ['', 'abc', ''])
1523 set imcmdline
1524
1525 call feedkeys(":\"abc\<CR>", 'xt')
1526 call assert_equal("\"abc", @:)
1527 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1528 call assert_equal("\"abc", @:)
1529 call feedkeys("/abc\<CR>", 'xt')
1530 call assert_equal([2, 1], [line('.'), col('.')])
1531 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1532 call assert_equal([2, 1], [line('.'), col('.')])
1533
1534 set imsearch=2
1535 call cursor(1, 1)
1536 call feedkeys("/abc\<CR>", 'xt')
1537 call assert_equal([2, 1], [line('.'), col('.')])
1538 call cursor(1, 1)
1539 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1540 call assert_equal([2, 1], [line('.'), col('.')])
1541 set imdisable
1542 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1543 call assert_equal([2, 1], [line('.'), col('.')])
1544 set imdisable&
1545 set imsearch&
1546
1547 set imcmdline&
1548 %bwipe!
1549endfunc
1550
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001551" Test for recursively getting multiple command line inputs
1552func Test_cmdwin_multi_input()
1553 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1554 call assert_equal('"cyan', @:)
1555endfunc
1556
1557" Test for using CTRL-_ in the command line with 'allowrevins'
1558func Test_cmdline_revins()
1559 CheckNotMSWindows
1560 CheckFeature rightleft
1561 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1562 call assert_equal("\"abc\<c-_>", @:)
1563 set allowrevins
1564 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1565 call assert_equal('"abcñèæ', @:)
1566 set allowrevins&
1567endfunc
1568
1569" Test for typing UTF-8 composing characters in the command line
1570func Test_cmdline_composing_chars()
1571 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1572 call assert_equal('"ゔ', @:)
1573endfunc
1574
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001575" Test for normal mode commands not supported in the cmd window
1576func Test_cmdwin_blocked_commands()
1577 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1578 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1579 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1580 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1581 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1582 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001583 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1584 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1585 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1586 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1587 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1588 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1589 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1590 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1591 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1592 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1593 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1594 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1595 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1596 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1597 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1598 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1599 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1600 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1601 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1602 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1603 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001604endfunc
1605
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001606" Close the Cmd-line window in insert mode using CTRL-C
1607func Test_cmdwin_insert_mode_close()
1608 %bw!
1609 let s = ''
1610 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1611 call assert_equal('Hello', s)
1612 call assert_equal(1, winnr('$'))
1613endfunc
1614
Bram Moolenaar0e717042020-04-27 19:29:01 +02001615" test that ";" works to find a match at the start of the first line
1616func Test_zero_line_search()
1617 new
1618 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1619 call cursor(1,1)
1620 0;/pattern/d
1621 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1622 q!
1623endfunc
1624
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001625func Test_read_shellcmd()
1626 CheckUnix
1627 if executable('ls')
1628 " There should be ls in the $PATH
1629 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1630 call assert_match('^"r! .*\<ls\>', @:)
1631 endif
1632
1633 if executable('rm')
1634 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1635 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1636 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001637
1638 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1639 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1640 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001641 endif
1642endfunc
1643
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01001644" Test for going up and down the directory tree using 'wildmenu'
1645func Test_wildmenu_dirstack()
1646 CheckUnix
1647 %bw!
1648 call mkdir('Xdir1/dir2/dir3', 'p')
1649 call writefile([], 'Xdir1/file1_1.txt')
1650 call writefile([], 'Xdir1/file1_2.txt')
1651 call writefile([], 'Xdir1/dir2/file2_1.txt')
1652 call writefile([], 'Xdir1/dir2/file2_2.txt')
1653 call writefile([], 'Xdir1/dir2/dir3/file3_1.txt')
1654 call writefile([], 'Xdir1/dir2/dir3/file3_2.txt')
1655 cd Xdir1/dir2/dir3
1656 set wildmenu
1657
1658 call feedkeys(":e \<Tab>\<C-B>\"\<CR>", 'xt')
1659 call assert_equal('"e file3_1.txt', @:)
1660 call feedkeys(":e \<Tab>\<Up>\<C-B>\"\<CR>", 'xt')
1661 call assert_equal('"e ../dir3/', @:)
1662 call feedkeys(":e \<Tab>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
1663 call assert_equal('"e ../../dir2/', @:)
1664 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<C-B>\"\<CR>", 'xt')
1665 call assert_equal('"e ../../dir2/dir3/', @:)
1666 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
1667 call assert_equal('"e ../../dir2/dir3/file3_1.txt', @:)
1668
1669 cd -
1670 call delete('Xdir1', 'rf')
1671 set wildmenu&
1672endfunc
1673
Bram Moolenaar309976e2019-12-05 18:16:33 +01001674" vim: shiftwidth=2 sts=2 expandtab