blob: 76a5c7ca42b5601bae371e38b78e050f3f399ebc [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
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00008func SetUp()
9 func SaveLastScreenLine()
10 let g:Sline = Screenline(&lines - 1)
11 return ''
12 endfunc
13 cnoremap <expr> <F4> SaveLastScreenLine()
14endfunc
15
16func TearDown()
17 delfunc SaveLastScreenLine
18 cunmap <F4>
19endfunc
20
Bram Moolenaarae3150e2016-06-11 23:22:36 +020021func Test_complete_tab()
22 call writefile(['testfile'], 'Xtestfile')
23 call feedkeys(":e Xtest\t\r", "tx")
24 call assert_equal('testfile', getline(1))
Albert Liu6024c042021-08-27 20:59:35 +020025
26 " Pressing <Tab> after '%' completes the current file, also on MS-Windows
27 call feedkeys(":e %\t\r", "tx")
28 call assert_equal('e Xtestfile', @:)
Bram Moolenaarae3150e2016-06-11 23:22:36 +020029 call delete('Xtestfile')
30endfunc
31
32func Test_complete_list()
33 " We can't see the output, but at least we check the code runs properly.
34 call feedkeys(":e test\<C-D>\r", "tx")
35 call assert_equal('test', expand('%:t'))
Bram Moolenaar578fe942020-02-27 21:32:51 +010036
37 " If a command doesn't support completion, then CTRL-D should be literally
38 " used.
39 call feedkeys(":chistory \<C-D>\<C-B>\"\<CR>", 'xt')
40 call assert_equal("\"chistory \<C-D>", @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +000041
42 " Test for displaying the tail of the completion matches
43 set wildmode=longest,full
44 call mkdir('Xtest')
45 call writefile([], 'Xtest/a.c')
46 call writefile([], 'Xtest/a.h')
47 let g:Sline = ''
48 call feedkeys(":e Xtest/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
49 call assert_equal('a.c a.h', g:Sline)
50 call assert_equal('"e Xtest/', @:)
51 if has('win32')
52 " Test for 'completeslash'
53 set completeslash=backslash
54 call feedkeys(":e Xtest\<Tab>\<C-B>\"\<CR>", 'xt')
55 call assert_equal('"e Xtest\', @:)
56 set completeslash=slash
57 call feedkeys(":e Xtest\<Tab>\<C-B>\"\<CR>", 'xt')
58 call assert_equal('"e Xtest/', @:)
59 set completeslash&
60 endif
61
62 " Test for displaying the tail with wildcards
63 let g:Sline = ''
64 call feedkeys(":e Xtes?/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
65 call assert_equal('Xtest/a.c Xtest/a.h', g:Sline)
66 call assert_equal('"e Xtes?/', @:)
67 let g:Sline = ''
68 call feedkeys(":e Xtes*/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
69 call assert_equal('Xtest/a.c Xtest/a.h', g:Sline)
70 call assert_equal('"e Xtes*/', @:)
71 let g:Sline = ''
72 call feedkeys(":e Xtes[/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
73 call assert_equal(':e Xtes[/', g:Sline)
74 call assert_equal('"e Xtes[/', @:)
75
76 call delete('Xtest', 'rf')
77 set wildmode&
Bram Moolenaarae3150e2016-06-11 23:22:36 +020078endfunc
79
80func Test_complete_wildmenu()
Bram Moolenaar37db6422019-03-28 21:26:23 +010081 call mkdir('Xdir1/Xdir2', 'p')
82 call writefile(['testfile1'], 'Xdir1/Xtestfile1')
83 call writefile(['testfile2'], 'Xdir1/Xtestfile2')
84 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3')
85 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020086 set wildmenu
Bram Moolenaar37db6422019-03-28 21:26:23 +010087
88 " Pressing <Tab> completes, and moves to next files when pressing again.
89 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx')
90 call assert_equal('testfile1', getline(1))
91 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020092 call assert_equal('testfile2', getline(1))
93
Bram Moolenaar37db6422019-03-28 21:26:23 +010094 " <S-Tab> is like <Tab> but begin with the last match and then go to
95 " previous.
96 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx')
97 call assert_equal('testfile2', getline(1))
98 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
99 call assert_equal('testfile1', getline(1))
100
101 " <Left>/<Right> to move to previous/next file.
102 call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx')
103 call assert_equal('testfile1', getline(1))
104 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
105 call assert_equal('testfile2', getline(1))
106 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
107 call assert_equal('testfile1', getline(1))
108
109 " <Up>/<Down> to go up/down directories.
110 call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx')
111 call assert_equal('testfile3', getline(1))
112 call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
113 call assert_equal('testfile1', getline(1))
114
Bram Moolenaar3e112ac2020-12-28 13:41:53 +0100115 " this fails in some Unix GUIs, not sure why
116 if !has('unix') || !has('gui_running')
117 " <C-J>/<C-K> mappings to go up/down directories when 'wildcharm' is
118 " different than 'wildchar'.
119 set wildcharm=<C-Z>
120 cnoremap <C-J> <Down><C-Z>
121 cnoremap <C-K> <Up><C-Z>
122 call feedkeys(":e Xdir1/\<Tab>\<C-J>\<CR>", 'tx')
123 call assert_equal('testfile3', getline(1))
124 call feedkeys(":e Xdir1/\<Tab>\<C-J>\<C-K>\<CR>", 'tx')
125 call assert_equal('testfile1', getline(1))
126 set wildcharm=0
127 cunmap <C-J>
128 cunmap <C-K>
129 endif
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +0100130
Bram Moolenaar578fe942020-02-27 21:32:51 +0100131 " Test for canceling the wild menu by adding a character
132 redrawstatus
133 call feedkeys(":e Xdir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
134 call assert_equal('"e Xdir1/Xdir2/x', @:)
135
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100136 " Completion using a relative path
137 cd Xdir1/Xdir2
138 call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
139 call assert_equal('"e Xtestfile3 Xtestfile4', @:)
140 cd -
141
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100142 cnoremap <expr> <F2> wildmenumode()
143 call feedkeys(":cd Xdir\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000144 call assert_equal('"cd Xdir1/0', @:)
145 call feedkeys(":e Xdir1/\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
146 call assert_equal('"e Xdir1/Xdir2/1', @:)
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100147 cunmap <F2>
148
Bram Moolenaar37db6422019-03-28 21:26:23 +0100149 " cleanup
150 %bwipe
151 call delete('Xdir1/Xdir2/Xtestfile4')
152 call delete('Xdir1/Xdir2/Xtestfile3')
153 call delete('Xdir1/Xtestfile2')
154 call delete('Xdir1/Xtestfile1')
155 call delete('Xdir1/Xdir2', 'd')
156 call delete('Xdir1', 'd')
Bram Moolenaarae3150e2016-06-11 23:22:36 +0200157 set nowildmenu
158endfunc
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100159
Bram Moolenaara60053b2020-09-03 16:50:13 +0200160func Test_wildmenu_screendump()
161 CheckScreendump
162
163 let lines =<< trim [SCRIPT]
164 set wildmenu hlsearch
165 [SCRIPT]
166 call writefile(lines, 'XTest_wildmenu')
167
168 let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
169 call term_sendkeys(buf, ":vim\<Tab>")
170 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
171
172 call term_sendkeys(buf, "\<Tab>")
173 call VerifyScreenDump(buf, 'Test_wildmenu_2', {})
174
175 call term_sendkeys(buf, "\<Tab>")
176 call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
177
Bram Moolenaar39f3b142021-02-14 12:57:36 +0100178 call term_sendkeys(buf, "\<Tab>\<Tab>")
Bram Moolenaara60053b2020-09-03 16:50:13 +0200179 call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
180 call term_sendkeys(buf, "\<Esc>")
181
182 " clean up
183 call StopVimInTerminal(buf)
184 call delete('XTest_wildmenu')
185endfunc
186
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100187func Test_map_completion()
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100188 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
189 call assert_equal('"map <unique> <silent>', getreg(':'))
190 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
191 call assert_equal('"map <script> <unique>', getreg(':'))
192 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
193 call assert_equal('"map <expr> <script>', getreg(':'))
194 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
195 call assert_equal('"map <buffer> <expr>', getreg(':'))
196 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
197 call assert_equal('"map <nowait> <buffer>', getreg(':'))
198 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
199 call assert_equal('"map <special> <nowait>', getreg(':'))
200 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
201 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200202
Bram Moolenaar1776a282019-05-03 16:05:41 +0200203 map <Middle>x middle
204
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200205 map ,f commaf
206 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200207 map <Left> left
208 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200209 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
210 call assert_equal('"map ,f', getreg(':'))
211 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
212 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200213 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
214 call assert_equal('"map <Left>', getreg(':'))
215 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200216 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200217 unmap ,f
218 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200219 unmap <Left>
220 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200221
222 set cpo-=< cpo-=B cpo-=k
223 map <Left> left
224 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
225 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200226 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200227 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200228 unmap <Left>
229
230 set cpo+=<
231 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200232 exe "set t_k6=\<Esc>[17~"
233 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200234 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
235 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200236 if !has('gui_running')
237 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
238 call assert_equal("\"map <F6>x", getreg(':'))
239 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200240 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200241 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200242 set cpo-=<
243
244 set cpo+=B
245 map <Left> left
246 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
247 call assert_equal('"map <Left>', getreg(':'))
248 unmap <Left>
249 set cpo-=B
250
251 set cpo+=k
252 map <Left> left
253 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
254 call assert_equal('"map <Left>', getreg(':'))
255 unmap <Left>
256 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200257
Bram Moolenaar531be472020-09-23 22:38:05 +0200258 call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
259
Bram Moolenaar1776a282019-05-03 16:05:41 +0200260 unmap <Middle>x
261 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100262endfunc
263
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100264func Test_match_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100265 hi Aardig ctermfg=green
266 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
267 call assert_equal('"match Aardig', getreg(':'))
268 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
269 call assert_equal('"match none', getreg(':'))
270endfunc
271
272func Test_highlight_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100273 hi Aardig ctermfg=green
274 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
275 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200276 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
277 call assert_equal('"hi default Aardig', getreg(':'))
278 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
279 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100280 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
281 call assert_equal('"hi link', getreg(':'))
282 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
283 call assert_equal('"hi default', getreg(':'))
284 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
285 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200286 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
287 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
288 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
289 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200290
291 " A cleared group does not show up in completions.
292 hi Anders ctermfg=green
293 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
294 hi clear Aardig
295 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
296 hi clear Anders
297 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100298endfunc
299
Bram Moolenaar75e15672020-06-28 13:10:22 +0200300" Test for command-line expansion of "hi Ni " (easter egg)
301func Test_highlight_easter_egg()
302 call test_override('ui_delay', 1)
303 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
304 call assert_equal("\"hi Ni \<Tab>", @:)
305 call test_override('ALL', 0)
306endfunc
307
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200308func Test_getcompletion()
309 let groupcount = len(getcompletion('', 'event'))
310 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200311 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200312 call assert_true(matchcount > 0)
313 call assert_true(groupcount > matchcount)
314
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200315 if has('menu')
316 source $VIMRUNTIME/menu.vim
317 let matchcount = len(getcompletion('', 'menu'))
318 call assert_true(matchcount > 0)
319 call assert_equal(['File.'], getcompletion('File', 'menu'))
320 call assert_true(matchcount > 0)
321 let matchcount = len(getcompletion('File.', 'menu'))
322 call assert_true(matchcount > 0)
323 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200324
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200325 let l = getcompletion('v:n', 'var')
326 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200327 let l = getcompletion('v:notexists', 'var')
328 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200329
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200330 args a.c b.c
331 let l = getcompletion('', 'arglist')
332 call assert_equal(['a.c', 'b.c'], l)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000333 let l = getcompletion('a.', 'buffer')
334 call assert_equal(['a.c'], l)
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200335 %argdelete
336
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200337 let l = getcompletion('', 'augroup')
338 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200339 let l = getcompletion('blahblah', 'augroup')
340 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200341
342 let l = getcompletion('', 'behave')
343 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200344 let l = getcompletion('not', 'behave')
345 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200346
347 let l = getcompletion('', 'color')
348 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200349 let l = getcompletion('dirty', 'color')
350 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200351
352 let l = getcompletion('', 'command')
353 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200354 let l = getcompletion('awake', 'command')
355 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200356
357 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200358 call assert_true(index(l, 'samples/') >= 0)
359 let l = getcompletion('NoMatch', 'dir')
360 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200361
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100362 if glob('~/*') !=# ''
363 let l = getcompletion('~/', 'dir')
364 call assert_true(l[0][0] ==# '~')
365 endif
366
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200367 let l = getcompletion('exe', 'expression')
368 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200369 let l = getcompletion('kill', 'expression')
370 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200371
372 let l = getcompletion('tag', 'function')
373 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200374 let l = getcompletion('paint', 'function')
375 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200376
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200377 let Flambda = {-> 'hello'}
378 let l = getcompletion('', 'function')
379 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200380 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200381
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200382 let l = getcompletion('run', 'file')
383 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200384 let l = getcompletion('walk', 'file')
385 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200386 set wildignore=*.vim
387 let l = getcompletion('run', 'file', 1)
388 call assert_true(index(l, 'runtest.vim') < 0)
389 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200390
391 let l = getcompletion('ha', 'filetype')
392 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200393 let l = getcompletion('horse', 'filetype')
394 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200395
396 let l = getcompletion('z', 'syntax')
397 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200398 let l = getcompletion('emacs', 'syntax')
399 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200400
401 let l = getcompletion('jikes', 'compiler')
402 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200403 let l = getcompletion('break', 'compiler')
404 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200405
406 let l = getcompletion('last', 'help')
407 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200408 let l = getcompletion('giveup', 'help')
409 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200410
411 let l = getcompletion('time', 'option')
412 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200413 let l = getcompletion('space', 'option')
414 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200415
416 let l = getcompletion('er', 'highlight')
417 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200418 let l = getcompletion('dark', 'highlight')
419 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200420
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200421 let l = getcompletion('', 'messages')
422 call assert_true(index(l, 'clear') >= 0)
423 let l = getcompletion('not', 'messages')
424 call assert_equal([], l)
425
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200426 let l = getcompletion('', 'mapclear')
427 call assert_true(index(l, '<buffer>') >= 0)
428 let l = getcompletion('not', 'mapclear')
429 call assert_equal([], l)
430
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200431 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200432 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200433 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
434 let root = has('win32') ? 'C:\\' : '/'
435 let l = getcompletion(root, 'shellcmd')
436 let expected = map(filter(glob(root . '*', 0, 1),
437 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
438 call assert_equal(expected, l)
439
Bram Moolenaarb650b982016-08-05 20:35:13 +0200440 if has('cscope')
441 let l = getcompletion('', 'cscope')
442 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
443 call assert_equal(cmds, l)
444 " using cmdline completion must not change the result
445 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
446 let l = getcompletion('', 'cscope')
447 call assert_equal(cmds, l)
448 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
449 let l = getcompletion('find ', 'cscope')
450 call assert_equal(keys, l)
451 endif
452
Bram Moolenaar7522f692016-08-06 14:12:50 +0200453 if has('signs')
454 sign define Testing linehl=Comment
455 let l = getcompletion('', 'sign')
456 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
457 call assert_equal(cmds, l)
458 " using cmdline completion must not change the result
459 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
460 let l = getcompletion('', 'sign')
461 call assert_equal(cmds, l)
462 let l = getcompletion('list ', 'sign')
463 call assert_equal(['Testing'], l)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000464 let l = getcompletion('de*', 'sign')
465 call assert_equal(['define'], l)
466 let l = getcompletion('p?', 'sign')
467 call assert_equal(['place'], l)
468 let l = getcompletion('j.', 'sign')
469 call assert_equal(['jump'], l)
Bram Moolenaar7522f692016-08-06 14:12:50 +0200470 endif
471
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200472 " Command line completion tests
473 let l = getcompletion('cd ', 'cmdline')
474 call assert_true(index(l, 'samples/') >= 0)
475 let l = getcompletion('cd NoMatch', 'cmdline')
476 call assert_equal([], l)
477 let l = getcompletion('let v:n', 'cmdline')
478 call assert_true(index(l, 'v:null') >= 0)
479 let l = getcompletion('let v:notexists', 'cmdline')
480 call assert_equal([], l)
481 let l = getcompletion('call tag', 'cmdline')
482 call assert_true(index(l, 'taglist(') >= 0)
483 let l = getcompletion('call paint', 'cmdline')
484 call assert_equal([], l)
485
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100486 func T(a, c, p)
ii144785fe02021-11-21 12:13:56 +0000487 let g:cmdline_compl_params = [a:a, a:c, a:p]
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100488 return "oneA\noneB\noneC"
489 endfunc
490 command -nargs=1 -complete=custom,T MyCmd
491 let l = getcompletion('MyCmd ', 'cmdline')
492 call assert_equal(['oneA', 'oneB', 'oneC'], l)
ii144785fe02021-11-21 12:13:56 +0000493 call assert_equal(['', 'MyCmd ', 6], g:cmdline_compl_params)
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100494
495 delcommand MyCmd
496 delfunc T
ii144785fe02021-11-21 12:13:56 +0000497 unlet g:cmdline_compl_params
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100498
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200499 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200500 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200501 if has('cmdline_hist')
502 call add(names, 'history')
503 endif
504 if has('gettext')
505 call add(names, 'locale')
506 endif
507 if has('profile')
508 call add(names, 'syntime')
509 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200510
511 set tags=Xtags
512 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
513
514 for name in names
515 let matchcount = len(getcompletion('', name))
516 call assert_true(matchcount >= 0, 'No matches for ' . name)
517 endfor
518
519 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200520 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200521
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000522 edit a~b
523 enew
524 call assert_equal(['a~b'], getcompletion('a~', 'buffer'))
525 bw a~b
526
527 if has('unix')
528 edit Xtest\
529 enew
530 call assert_equal(['Xtest\'], getcompletion('Xtest\', 'buffer'))
531 bw Xtest\
532 endif
533
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200534 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200535 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200536 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200537endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200538
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100539func Test_fullcommand()
540 let tests = {
541 \ '': '',
542 \ ':': '',
543 \ ':::': '',
544 \ ':::5': '',
545 \ 'not_a_cmd': '',
546 \ 'Check': '',
547 \ 'syntax': 'syntax',
548 \ ':syntax': 'syntax',
549 \ '::::syntax': 'syntax',
550 \ 'sy': 'syntax',
551 \ 'syn': 'syntax',
552 \ 'synt': 'syntax',
553 \ ':sy': 'syntax',
554 \ '::::sy': 'syntax',
555 \ 'match': 'match',
556 \ '2match': 'match',
557 \ '3match': 'match',
558 \ 'aboveleft': 'aboveleft',
559 \ 'abo': 'aboveleft',
560 \ 's': 'substitute',
561 \ '5s': 'substitute',
562 \ ':5s': 'substitute',
563 \ "'<,'>s": 'substitute',
564 \ ":'<,'>s": 'substitute',
565 \ 'CheckUni': 'CheckUnix',
566 \ 'CheckUnix': 'CheckUnix',
567 \ }
568
569 for [in, want] in items(tests)
570 call assert_equal(want, fullcommand(in))
571 endfor
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +0200572 call assert_equal('', fullcommand(test_null_string()))
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100573
574 call assert_equal('syntax', 'syn'->fullcommand())
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200575
576 command -buffer BufferLocalCommand :
577 command GlobalCommand :
578 call assert_equal('GlobalCommand', fullcommand('GlobalCom'))
579 call assert_equal('BufferLocalCommand', fullcommand('BufferL'))
580 delcommand BufferLocalCommand
581 delcommand GlobalCommand
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100582endfunc
583
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200584func Test_shellcmd_completion()
585 let save_path = $PATH
586
587 call mkdir('Xpathdir/Xpathsubdir', 'p')
588 call writefile([''], 'Xpathdir/Xfile.exe')
589 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
590
591 " Set PATH to example directory without trailing slash.
592 let $PATH = getcwd() . '/Xpathdir'
593
594 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
595 " dirs in the PATH, even though they won't be executed. We check that only
596 " subdirs of the PWD and executables from the PATH are included in the
597 " suggestions.
598 let actual = getcompletion('X', 'shellcmd')
599 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
600 call insert(expected, 'Xfile.exe')
601 call assert_equal(expected, actual)
602
603 call delete('Xpathdir', 'rf')
604 let $PATH = save_path
605endfunc
606
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200607func Test_expand_star_star()
608 call mkdir('a/b', 'p')
609 call writefile(['asdfasdf'], 'a/b/fileXname')
610 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
611 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200612 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200613 call delete('a', 'rf')
614endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100615
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100616func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100617 let @a = "def"
618 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
619 call assert_equal('"abc def ghi', @:)
620
621 new
622 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
623
624 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
625 call assert_equal('"aaa asdf bbb', @:)
626
627 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
628 call assert_equal('"aaa /tmp/some bbb', @:)
629
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200630 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
631 call assert_equal('"aaa '.getline(1).' bbb', @:)
632
Bram Moolenaar21efc362016-12-03 14:05:49 +0100633 set incsearch
634 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
635 call assert_equal('"aaa verylongword bbb', @:)
636
637 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
638 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100639
640 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
641 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100642 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200643
644 " Error while typing a command used to cause that it was not executed
645 " in the end.
646 new
647 try
648 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
649 catch /^Vim\%((\a\+)\)\=:E32/
650 " ignore error E32
651 endtry
652 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100653
Bram Moolenaar578fe942020-02-27 21:32:51 +0100654 " Try to paste an invalid register using <C-R>
655 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
656 call assert_equal('"onetwo', @:)
657
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100658 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100659 let @a = "xy\<C-H>z"
660 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
661 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100662 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
663 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100664 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
665 call assert_equal("\"xy\<C-H>z", @:)
666
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100667 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
668 let @a = "xy\<C-V>z"
669 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
670 call assert_equal('"xyz', @:)
671 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
672 call assert_equal("\"xy\<C-V>z", @:)
673
Bram Moolenaar578fe942020-02-27 21:32:51 +0100674 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
675
Bram Moolenaar72532d32018-04-07 19:09:09 +0200676 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100677endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100678
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100679func Test_cmdline_remove_char()
680 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100681
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100682 for e in ['utf8', 'latin1']
683 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100684
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100685 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
686 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100687
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100688 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
689 call assert_equal('"abcdef', @:)
690
691 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
692 call assert_equal('"abc ghi', @:, e)
693
694 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
695 call assert_equal('"def', @:, e)
696 endfor
697
698 let &encoding = encoding_save
699endfunc
700
701func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200702 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100703
704 set keymap=esperanto
705 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
706 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
707 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100708endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100709
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100710func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100711 new
712 2;'(
713 2;')
714 quit
715endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100716
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100717func Test_illegal_address2()
718 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
719 new
720 source Xtest.vim
721 " Trigger calling validate_cursor()
722 diffsp Xtest.vim
723 quit!
724 bwipe!
725 call delete('Xtest.vim')
726endfunc
727
Bram Moolenaarba47b512017-01-24 21:18:19 +0100728func Test_cmdline_complete_wildoptions()
729 help
730 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
731 let a = join(sort(split(@:)),' ')
732 set wildoptions=tagfile
733 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
734 let b = join(sort(split(@:)),' ')
735 call assert_equal(a, b)
736 bw!
737endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100738
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200739func Test_cmdline_complete_user_cmd()
740 command! -complete=color -nargs=1 Foo :
741 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
742 call assert_equal('"Foo blue', @:)
743 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
744 call assert_equal('"Foo blue', @:)
745 delcommand Foo
746endfunc
747
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100748func s:ScriptLocalFunction()
749 echo 'yes'
750endfunc
751
752func Test_cmdline_complete_user_func()
753 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
naohiro ono5aec7552021-08-19 21:20:41 +0200754 call assert_match('"func Test_cmdline_complete_user_', @:)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100755 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
756 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +0100757
758 " g: prefix also works
759 call feedkeys(":echo g:Test_cmdline_complete_user_f\<Tab>\<Home>\"\<cr>", 'tx')
760 call assert_match('"echo g:Test_cmdline_complete_user_func', @:)
Bram Moolenaar069f9082021-08-13 17:48:25 +0200761
762 " using g: prefix does not result in just "g:" matches from a lambda
763 let Fx = { a -> a }
764 call feedkeys(":echo g:\<Tab>\<Home>\"\<cr>", 'tx')
765 call assert_match('"echo g:[A-Z]', @:)
naohiro ono5aec7552021-08-19 21:20:41 +0200766
767 " existence of script-local dict function does not break user function name
768 " completion
769 function s:a_dict_func() dict
770 endfunction
771 call feedkeys(":call Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
772 call assert_match('"call Test_cmdline_complete_user_', @:)
773 delfunction s:a_dict_func
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100774endfunc
775
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200776func Test_cmdline_complete_user_names()
777 if has('unix') && executable('whoami')
778 let whoami = systemlist('whoami')[0]
779 let first_letter = whoami[0]
780 if len(first_letter) > 0
781 " Trying completion of :e ~x where x is the first letter of
782 " the user name should complete to at least the user name.
783 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
784 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
785 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200786 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200787 " Just in case: check that the system has an Administrator account.
788 let names = system('net user')
789 if names =~ 'Administrator'
790 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100791 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200792 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100793 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200794 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200795 else
796 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200797 endif
798endfunc
799
Bram Moolenaar297610b2019-12-27 17:20:55 +0100800func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200801 CheckExecutable whoami
802 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
803 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100804endfunc
805
Bram Moolenaar8b633132020-03-20 18:20:51 +0100806func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200807 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
808 call assert_equal(lang, v:lc_time)
809
810 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
811 call assert_equal(lang, v:ctype)
812
813 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
814 call assert_equal(lang, v:collate)
815
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200816 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200817 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200818
819 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200820 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200821
Bram Moolenaarec680282020-06-12 19:35:32 +0200822 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200823
Bram Moolenaarec680282020-06-12 19:35:32 +0200824 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
825 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200826
Bram Moolenaarec680282020-06-12 19:35:32 +0200827 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
828 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200829
Bram Moolenaarec680282020-06-12 19:35:32 +0200830 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
831 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200832
833 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
834 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200835endfunc
836
Bram Moolenaar297610b2019-12-27 17:20:55 +0100837func Test_cmdline_complete_env_variable()
838 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100839 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
840 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100841 unlet $X_VIM_TEST_COMPLETE_ENV
842endfunc
843
Bram Moolenaar4941b5e2020-12-24 17:15:53 +0100844func Test_cmdline_complete_expression()
845 let g:SomeVar = 'blah'
846 for cmd in ['exe', 'echo', 'echon', 'echomsg']
847 call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
848 call assert_match('"' .. cmd .. ' SomeVar', @:)
849 call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
850 call assert_match('"' .. cmd .. ' foo SomeVar', @:)
851 endfor
852 unlet g:SomeVar
853endfunc
854
Bram Moolenaar47016f52021-08-26 16:39:58 +0200855" Unique function name for completion below
856func s:WeirdFunc()
857 echo 'weird'
858endfunc
859
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100860" Test for various command-line completion
861func Test_cmdline_complete_various()
862 " completion for a command starting with a comment
863 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
864 call assert_equal("\" :|\"\<C-A>", @:)
865
866 " completion for a range followed by a comment
867 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
868 call assert_equal("\"1,2\"\<C-A>", @:)
869
870 " completion for :k command
871 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
872 call assert_equal("\"ka\<C-A>", @:)
873
874 " completion for short version of the :s command
875 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
876 call assert_equal("\"sI \<C-A>", @:)
877
878 " completion for :write command
879 call mkdir('Xdir')
880 call writefile(['one'], 'Xdir/Xfile1')
881 let save_cwd = getcwd()
882 cd Xdir
883 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
884 call assert_equal("\"w >> Xfile1", @:)
885 call chdir(save_cwd)
886 call delete('Xdir', 'rf')
887
888 " completion for :w ! and :r ! commands
889 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
890 call assert_equal("\"w !invalid_xyz_cmd", @:)
891 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
892 call assert_equal("\"r !invalid_xyz_cmd", @:)
893
894 " completion for :>> and :<< commands
895 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
896 call assert_equal("\">>>\<C-A>", @:)
897 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
898 call assert_equal("\"<<<\<C-A>", @:)
899
900 " completion for command with +cmd argument
901 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
902 call assert_equal("\"buffer +/pat Xabc", @:)
903 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
904 call assert_equal("\"buffer +/pat\<C-A>", @:)
905
906 " completion for a command with a trailing comment
907 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
908 call assert_equal("\"ls \" comment\<C-A>", @:)
909
910 " completion for a command with a trailing command
911 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
912 call assert_equal("\"ls | ls", @:)
913
914 " completion for a command with an CTRL-V escaped argument
915 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
916 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
917
918 " completion for a command that doesn't take additional arguments
919 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
920 call assert_equal("\"all abc\<C-A>", @:)
921
922 " completion for a command with a command modifier
923 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
924 call assert_equal("\"topleft new", @:)
925
Bram Moolenaare70e12b2021-06-13 17:20:08 +0200926 " completion for vim9 and legacy commands
927 call feedkeys(":vim9 call strle\<C-A>\<C-B>\"\<CR>", 'xt')
928 call assert_equal("\"vim9 call strlen(", @:)
929 call feedkeys(":legac call strle\<C-A>\<C-B>\"\<CR>", 'xt')
930 call assert_equal("\"legac call strlen(", @:)
931
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +0200932 " completion for the :disassemble command
933 call feedkeys(":disas deb\<C-A>\<C-B>\"\<CR>", 'xt')
934 call assert_equal("\"disas debug", @:)
935 call feedkeys(":disas pro\<C-A>\<C-B>\"\<CR>", 'xt')
936 call assert_equal("\"disas profile", @:)
937 call feedkeys(":disas debug Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
938 call assert_equal("\"disas debug Test_cmdline_complete_various", @:)
939 call feedkeys(":disas profile Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
940 call assert_equal("\"disas profile Test_cmdline_complete_various", @:)
naohiro ono9aecf792021-08-28 15:56:06 +0200941 call feedkeys(":disas Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
942 call assert_equal("\"disas Test_cmdline_complete_various", @:)
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +0200943
Bram Moolenaar47016f52021-08-26 16:39:58 +0200944 call feedkeys(":disas s:WeirdF\<C-A>\<C-B>\"\<CR>", 'xt')
naohiro ono9aecf792021-08-28 15:56:06 +0200945 call assert_match('"disas <SNR>\d\+_WeirdFunc', @:)
Bram Moolenaar47016f52021-08-26 16:39:58 +0200946
naohiro onodfe04db2021-09-12 15:45:10 +0200947 call feedkeys(":disas \<S-Tab>\<C-B>\"\<CR>", 'xt')
948 call assert_match('"disas <SNR>\d\+_', @:)
949 call feedkeys(":disas debug \<S-Tab>\<C-B>\"\<CR>", 'xt')
950 call assert_match('"disas debug <SNR>\d\+_', @:)
951
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100952 " completion for the :match command
953 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
954 call assert_equal("\"match Search /pat/\<C-A>", @:)
955
956 " completion for the :s command
957 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
958 call assert_equal("\"s/from/to/g\<C-A>", @:)
959
960 " completion for the :dlist command
961 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
962 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
963
964 " completion for the :doautocmd command
965 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
966 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
967
Bram Moolenaarafe8cf62020-10-05 20:07:18 +0200968 " completion of autocmd group after comma
969 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
970 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
971
Dominique Pellebfb2bb12021-08-14 21:11:51 +0200972 " completion of file name in :doautocmd
973 call writefile([], 'Xfile1')
974 call writefile([], 'Xfile2')
975 call feedkeys(":doautocmd BufEnter Xfi\<C-A>\<C-B>\"\<CR>", 'xt')
976 call assert_equal("\"doautocmd BufEnter Xfile1 Xfile2", @:)
977 call delete('Xfile1')
978 call delete('Xfile2')
979
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100980 " completion for the :augroup command
Bram Moolenaarb4d82e22021-09-01 13:03:39 +0200981 augroup XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100982 augroup END
983 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
Bram Moolenaarb4d82e22021-09-01 13:03:39 +0200984 call assert_equal("\"augroup XTest.test", @:)
985 call feedkeys(":au X\<C-A>\<C-B>\"\<CR>", 'xt')
986 call assert_equal("\"au XTest.test", @:)
987 augroup! XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100988
989 " completion for the :unlet command
990 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
991 call assert_equal("\"unlet one two", @:)
992
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100993 " completion for the :buffer command with curlies
Bram Moolenaar39c47c32021-10-17 18:05:26 +0100994 " FIXME: what should happen on MS-Windows?
995 if !has('win32')
996 edit \{someFile}
997 call feedkeys(":buf someFile\<C-A>\<C-B>\"\<CR>", 'xt')
998 call assert_equal("\"buf {someFile}", @:)
999 bwipe {someFile}
1000 endif
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001001
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001002 " completion for the :bdelete command
1003 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
1004 call assert_equal("\"bdel a b c", @:)
1005
1006 " completion for the :mapclear command
1007 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
1008 call assert_equal("\"mapclear <buffer>", @:)
1009
1010 " completion for user defined commands with menu names
1011 menu Test.foo :ls<CR>
1012 com -nargs=* -complete=menu MyCmd
1013 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
1014 call assert_equal('"MyCmd Test.', @:)
1015 delcom MyCmd
1016 unmenu Test
1017
1018 " completion for user defined commands with mappings
1019 mapclear
1020 map <F3> :ls<CR>
1021 com -nargs=* -complete=mapping MyCmd
1022 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001023 call assert_equal('"MyCmd <F3> <F4>', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001024 mapclear
1025 delcom MyCmd
1026
1027 " completion for :set path= with multiple backslashes
1028 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
1029 call assert_equal('"set path=a\\\ b', @:)
1030
1031 " completion for :set dir= with a backslash
1032 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
1033 call assert_equal('"set dir=a\ b', @:)
1034
1035 " completion for the :py3 commands
1036 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
1037 call assert_equal('"py3 py3do py3file', @:)
1038
Bram Moolenaardf749a22021-03-28 15:29:43 +02001039 " completion for the :vim9 commands
1040 call feedkeys(":vim9\<C-A>\<C-B>\"\<CR>", 'xt')
1041 call assert_equal('"vim9cmd vim9script', @:)
1042
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001043 " redir @" is not the start of a comment. So complete after that
1044 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
1045 call assert_equal('"redir @" | cwindow', @:)
1046
1047 " completion after a backtick
1048 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
1049 call assert_equal('"e `a1b2c', @:)
1050
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001051 " completion for :language command with an invalid argument
1052 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
1053 call assert_equal("\"language dummy \t", @:)
1054
1055 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +01001056 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
1057 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001058
1059 " completion with ambiguous user defined commands
1060 com TCmd1 echo 'TCmd1'
1061 com TCmd2 echo 'TCmd2'
1062 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
1063 call assert_equal('"TCmd ', @:)
1064 delcom TCmd1
1065 delcom TCmd2
1066
1067 " completion after a range followed by a pipe (|) character
1068 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
1069 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +02001070
1071 " use <Esc> as the 'wildchar' for completion
1072 set wildchar=<Esc>
1073 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
1074 call assert_equal('"g/a\xb/clearjumps', @:)
1075 " pressing <esc> twice should cancel the command
1076 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
1077 call assert_equal('"g/a\xb/clearjumps', @:)
1078 set wildchar&
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001079
1080 " should be able to complete a file name that starts with a '~'.
1081 if has('unix')
1082 call writefile([], '~Xtest')
1083 call feedkeys(":e \\~X\<Tab>\<C-B>\"\<CR>", 'xt')
1084 call assert_equal('"e \~Xtest', @:)
1085 call delete('~Xtest')
1086 endif
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001087
1088 call feedkeys(":py3f\<Tab>\<C-B>\"\<CR>", 'xt')
1089 call assert_equal('"py3file', @:)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001090endfunc
1091
1092" Test for 'wildignorecase'
1093func Test_cmdline_wildignorecase()
1094 CheckUnix
1095 call writefile([], 'XTEST')
1096 set wildignorecase
1097 call feedkeys(":e xt\<Tab>\<C-B>\"\<CR>", 'xt')
1098 call assert_equal('"e XTEST', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001099 call assert_equal(['XTEST'], getcompletion('xt', 'file'))
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001100 set wildignorecase&
1101 call delete('XTEST')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001102endfunc
1103
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001104func Test_cmdline_write_alternatefile()
1105 new
1106 call setline('.', ['one', 'two'])
1107 f foo.txt
1108 new
1109 f #-A
1110 call assert_equal('foo.txt-A', expand('%'))
1111 f #<-B.txt
1112 call assert_equal('foo-B.txt', expand('%'))
1113 f %<
1114 call assert_equal('foo-B', expand('%'))
1115 new
Bram Moolenaare2e40752020-09-04 21:18:46 +02001116 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001117 bw!
1118 f foo-B.txt
1119 f %<-A
1120 call assert_equal('foo-B-A', expand('%'))
1121 bw!
1122 bw!
1123endfunc
1124
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001125" using a leading backslash here
1126set cpo+=C
1127
1128func Test_cmdline_search_range()
1129 new
1130 call setline(1, ['a', 'b', 'c', 'd'])
1131 /d
1132 1,\/s/b/B/
1133 call assert_equal('B', getline(2))
1134
1135 /a
1136 $
1137 \?,4s/c/C/
1138 call assert_equal('C', getline(3))
1139
1140 call setline(1, ['a', 'b', 'c', 'd'])
1141 %s/c/c/
1142 1,\&s/b/B/
1143 call assert_equal('B', getline(2))
1144
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001145 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001146 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001147
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001148 bwipe!
1149endfunc
1150
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001151" Test for the tick mark (') in an excmd range
1152func Test_tick_mark_in_range()
1153 " If only the tick is passed as a range and no command is specified, there
1154 " should not be an error
1155 call feedkeys(":'\<CR>", 'xt')
1156 call assert_equal("'", getreg(':'))
1157 call assert_fails("',print", 'E78:')
1158endfunc
1159
1160" Test for using a line number followed by a search pattern as range
1161func Test_lnum_and_pattern_as_range()
1162 new
1163 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
1164 let @" = ''
1165 2/foo/yank
1166 call assert_equal("foo 3\n", @")
1167 call assert_equal(1, line('.'))
1168 close!
1169endfunc
1170
Bram Moolenaar65189a12017-02-06 22:22:17 +01001171" Tests for getcmdline(), getcmdpos() and getcmdtype()
1172func Check_cmdline(cmdtype)
1173 call assert_equal('MyCmd a', getcmdline())
1174 call assert_equal(8, getcmdpos())
1175 call assert_equal(a:cmdtype, getcmdtype())
1176 return ''
1177endfunc
1178
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001179set cpo&
1180
Bram Moolenaar65189a12017-02-06 22:22:17 +01001181func Test_getcmdtype()
1182 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
1183
1184 let cmdtype = ''
1185 debuggreedy
1186 call feedkeys(":debug echo 'test'\<CR>", "t")
1187 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
1188 call feedkeys("cont\<CR>", "xt")
1189 0debuggreedy
1190 call assert_equal('>', cmdtype)
1191
1192 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
1193 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
1194
1195 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +01001196 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +01001197
1198 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
1199
1200 cnoremap <expr> <F6> Check_cmdline('=')
1201 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
1202 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001203
1204 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +01001205endfunc
1206
Bram Moolenaar81612b72018-06-23 14:55:03 +02001207func Test_getcmdwintype()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001208 CheckFeature cmdwin
1209
Bram Moolenaar81612b72018-06-23 14:55:03 +02001210 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1211 call assert_equal('/', a)
1212
1213 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1214 call assert_equal('?', a)
1215
1216 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1217 call assert_equal(':', a)
1218
1219 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1220 call assert_equal(':', a)
1221
1222 call assert_equal('', getcmdwintype())
1223endfunc
1224
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001225func Test_getcmdwin_autocmd()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001226 CheckFeature cmdwin
1227
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001228 let s:seq = []
1229 augroup CmdWin
1230 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
1231 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
1232 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
1233 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
1234 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
1235 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
1236
1237 let org_winid = win_getid()
1238 let org_bufnr = bufnr()
1239 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
1240 call assert_equal(':', a)
1241 call assert_equal([
1242 \ 'WinLeave ' .. org_winid,
1243 \ 'WinEnter ' .. s:cmd_winid,
1244 \ 'BufLeave ' .. org_bufnr,
1245 \ 'BufEnter ' .. s:cmd_bufnr,
1246 \ 'CmdWinEnter ' .. s:cmd_winid,
1247 \ 'CmdWinLeave ' .. s:cmd_winid,
1248 \ 'BufLeave ' .. s:cmd_bufnr,
1249 \ 'WinLeave ' .. s:cmd_winid,
1250 \ 'WinEnter ' .. org_winid,
1251 \ 'BufEnter ' .. org_bufnr,
1252 \ ], s:seq)
1253
1254 au!
1255 augroup END
1256endfunc
1257
Bram Moolenaar52604f22017-04-07 16:17:39 +02001258func Test_verbosefile()
1259 set verbosefile=Xlog
1260 echomsg 'foo'
1261 echomsg 'bar'
1262 set verbosefile=
1263 let log = readfile('Xlog')
1264 call assert_match("foo\nbar", join(log, "\n"))
1265 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001266 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001267 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001268 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +02001269endfunc
1270
Bram Moolenaar4facea32019-10-12 20:17:40 +02001271func Test_verbose_option()
1272 CheckScreendump
1273
1274 let lines =<< trim [SCRIPT]
1275 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1276 call feedkeys("\r", 't') " for the hit-enter prompt
1277 set verbose=20
1278 [SCRIPT]
1279 call writefile(lines, 'XTest_verbose')
1280
1281 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001282 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001283 call term_sendkeys(buf, ":DoSomething\<CR>")
1284 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1285
1286 " clean up
1287 call StopVimInTerminal(buf)
1288 call delete('XTest_verbose')
1289endfunc
1290
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001291func Test_setcmdpos()
1292 func InsertTextAtPos(text, pos)
1293 call assert_equal(0, setcmdpos(a:pos))
1294 return a:text
1295 endfunc
1296
1297 " setcmdpos() with position in the middle of the command line.
1298 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1299 call assert_equal('"1ab2', @:)
1300
1301 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1302 call assert_equal('"1b2a', @:)
1303
1304 " setcmdpos() with position beyond the end of the command line.
1305 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1306 call assert_equal('"12ab', @:)
1307
1308 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001309 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001310endfunc
1311
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001312func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001313 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001314 let encoding_save = &encoding
1315
1316 for e in encodings
1317 exe 'set encoding=' . e
1318
1319 " Test overstrike in the middle of the command line.
1320 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001321 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001322
1323 " Test overstrike going beyond end of command line.
1324 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001325 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001326
1327 " Test toggling insert/overstrike a few times.
1328 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001329 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001330 endfor
1331
Bram Moolenaar30276f22019-01-24 17:59:39 +01001332 " Test overstrike with multi-byte characters.
1333 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001334 call assert_equal('"abcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001335
1336 let &encoding = encoding_save
1337endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001338
1339func Test_cmdwin_bug()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001340 CheckFeature cmdwin
1341
Bram Moolenaara046b372019-09-15 17:26:07 +02001342 let winid = win_getid()
1343 sp
1344 try
1345 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1346 catch /^Vim\%((\a\+)\)\=:E11/
1347 endtry
1348 bw!
1349endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001350
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001351func Test_cmdwin_restore()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001352 CheckFeature cmdwin
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001353 CheckScreendump
1354
1355 let lines =<< trim [SCRIPT]
Egor Zvorykin125ffd22021-11-17 14:01:14 +00001356 augroup vimHints | au! | augroup END
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001357 call setline(1, range(30))
1358 2split
1359 [SCRIPT]
1360 call writefile(lines, 'XTest_restore')
1361
1362 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001363 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001364 call term_sendkeys(buf, "q:")
1365 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1366
1367 " normal restore
1368 call term_sendkeys(buf, ":q\<CR>")
1369 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1370
1371 " restore after setting 'lines' with one window
1372 call term_sendkeys(buf, ":close\<CR>")
1373 call term_sendkeys(buf, "q:")
1374 call term_sendkeys(buf, ":set lines=18\<CR>")
1375 call term_sendkeys(buf, ":q\<CR>")
1376 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1377
1378 " clean up
1379 call StopVimInTerminal(buf)
1380 call delete('XTest_restore')
1381endfunc
1382
Bram Moolenaare5b44862021-05-30 13:54:03 +02001383func Test_cmdwin_no_terminal()
1384 CheckFeature cmdwin
1385 CheckFeature terminal
Bram Moolenaar0b496482021-05-30 14:21:57 +02001386 CheckNotMSWindows
Bram Moolenaare5b44862021-05-30 13:54:03 +02001387
1388 let buf = RunVimInTerminal('', {'rows': 12})
1389 call TermWait(buf, 50)
1390 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1391 call term_sendkeys(buf, "q:")
1392 call term_sendkeys(buf, ":let buf = term_start(['/bin/echo'], #{hidden: 1})\<CR>")
1393 call VerifyScreenDump(buf, 'Test_cmdwin_no_terminal', {})
1394 call term_sendkeys(buf, ":q\<CR>")
1395 call StopVimInTerminal(buf)
1396endfunc
1397
Bram Moolenaar52410572019-10-27 05:12:45 +01001398func Test_buffers_lastused()
1399 " check that buffers are sorted by time when wildmode has lastused
1400 call test_settime(1550020000) " middle
1401 edit bufa
1402 enew
1403 call test_settime(1550030000) " newest
1404 edit bufb
1405 enew
1406 call test_settime(1550010000) " oldest
1407 edit bufc
1408 enew
1409 call test_settime(0)
1410 enew
1411
1412 call assert_equal(['bufa', 'bufb', 'bufc'],
1413 \ getcompletion('', 'buffer'))
1414
1415 let save_wildmode = &wildmode
1416 set wildmode=full:lastused
1417
1418 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1419 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1420 call assert_equal('b bufb', X)
1421 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1422 call assert_equal('b bufa', X)
1423 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1424 call assert_equal('b bufc', X)
1425 enew
1426
1427 edit other
1428 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1429 call assert_equal('b bufb', X)
1430 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1431 call assert_equal('b bufa', X)
1432 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1433 call assert_equal('b bufc', X)
1434 enew
1435
1436 let &wildmode = save_wildmode
1437
1438 bwipeout bufa
1439 bwipeout bufb
1440 bwipeout bufc
1441endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001442
1443func Test_cmdwin_feedkeys()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001444 CheckFeature cmdwin
1445
Bram Moolenaar85db5472019-12-04 15:11:08 +01001446 " This should not generate E488
1447 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001448 " Using feedkeys with q: only should automatically close the cmd window
1449 call feedkeys('q:', 'xt')
1450 call assert_equal(1, winnr('$'))
1451 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001452endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001453
1454" Tests for the issues fixed in 7.4.441.
1455" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1456func Test_cmdwin_cedit()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001457 CheckFeature cmdwin
1458
Bram Moolenaar309976e2019-12-05 18:16:33 +01001459 exe "set cedit=\<C-c>"
1460 normal! :
1461 call assert_equal(1, winnr('$'))
1462
1463 let g:cmd_wintype = ''
1464 func CmdWinType()
1465 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001466 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001467 return ''
1468 endfunc
1469
1470 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1471 echo input('')
1472 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001473 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001474
1475 set cedit&vim
1476 delfunc CmdWinType
1477endfunc
1478
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001479" Test for CmdwinEnter autocmd
1480func Test_cmdwin_autocmd()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001481 CheckFeature cmdwin
1482
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001483 augroup CmdWin
1484 au!
Bram Moolenaarb63f3ca2021-01-30 21:40:03 +01001485 autocmd BufLeave * if &buftype == '' | update | endif
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001486 autocmd CmdwinEnter * startinsert
1487 augroup END
1488
1489 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1490 call assert_equal('xyz', @:)
1491
1492 augroup CmdWin
1493 au!
1494 augroup END
1495 augroup! CmdWin
1496endfunc
1497
Bram Moolenaar479950f2020-01-19 15:45:17 +01001498func Test_cmdlineclear_tabenter()
1499 CheckScreendump
1500
1501 let lines =<< trim [SCRIPT]
1502 call setline(1, range(30))
1503 [SCRIPT]
1504
1505 call writefile(lines, 'XtestCmdlineClearTabenter')
1506 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001507 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001508 " in one tab make the command line higher with CTRL-W -
1509 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1510 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1511
1512 call StopVimInTerminal(buf)
1513 call delete('XtestCmdlineClearTabenter')
1514endfunc
1515
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001516" Test for expanding special keywords in cmdline
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001517func Test_cmdline_expand_special()
1518 %bwipe!
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02001519 call assert_fails('e #', 'E194:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001520 call assert_fails('e <afile>', 'E495:')
1521 call assert_fails('e <abuf>', 'E496:')
1522 call assert_fails('e <amatch>', 'E497:')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001523
1524 call writefile([], 'Xfile.cpp')
1525 call writefile([], 'Xfile.java')
1526 new Xfile.cpp
1527 call feedkeys(":e %:r\<C-A>\<C-B>\"\<CR>", 'xt')
1528 call assert_equal('"e Xfile.cpp Xfile.java', @:)
1529 close
1530 call delete('Xfile.cpp')
1531 call delete('Xfile.java')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001532endfunc
1533
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001534func Test_cmdwin_jump_to_win()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001535 CheckFeature cmdwin
1536
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001537 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1538 new
1539 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001540 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001541 close!
1542 call feedkeys("q/:close\<CR>", "xt")
1543 call assert_equal(1, winnr('$'))
1544 call feedkeys("q/:exit\<CR>", "xt")
1545 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001546
1547 " opening command window twice should fail
1548 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1549 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001550endfunc
1551
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001552func Test_cmdwin_interrupted()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001553 CheckFeature cmdwin
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001554 CheckScreendump
1555
1556 " aborting the :smile output caused the cmdline window to use the current
1557 " buffer.
1558 let lines =<< trim [SCRIPT]
1559 au WinNew * smile
1560 [SCRIPT]
1561 call writefile(lines, 'XTest_cmdwin')
1562
1563 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001564 " open cmdwin
1565 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001566 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001567 " quit more prompt for :smile command
1568 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001569 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001570 " execute a simple command
1571 call term_sendkeys(buf, "aecho 'done'\<CR>")
1572 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1573
1574 " clean up
1575 call StopVimInTerminal(buf)
1576 call delete('XTest_cmdwin')
1577endfunc
1578
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001579" Test for backtick expression in the command line
1580func Test_cmd_backtick()
1581 %argd
1582 argadd `=['a', 'b', 'c']`
1583 call assert_equal(['a', 'b', 'c'], argv())
1584 %argd
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001585
1586 argadd `echo abc def`
1587 call assert_equal(['abc def'], argv())
1588 %argd
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001589endfunc
1590
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001591" Test for the :! command
1592func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001593 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001594
1595 let lines =<< trim [SCRIPT]
1596 " Test for no previous command
1597 call assert_fails('!!', 'E34:')
1598 set nomore
1599 " Test for cmdline expansion with :!
1600 call setline(1, 'foo!')
1601 silent !echo <cWORD> > Xfile.out
1602 call assert_equal(['foo!'], readfile('Xfile.out'))
1603 " Test for using previous command
1604 silent !echo \! !
1605 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1606 call writefile(v:errors, 'Xresult')
1607 call delete('Xfile.out')
1608 qall!
1609 [SCRIPT]
1610 call writefile(lines, 'Xscript')
1611 if RunVim([], [], '--clean -S Xscript')
1612 call assert_equal([], readfile('Xresult'))
1613 endif
1614 call delete('Xscript')
1615 call delete('Xresult')
1616endfunc
1617
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001618" Test error: "E135: *Filter* Autocommands must not change current buffer"
1619func Test_cmd_bang_E135()
1620 new
1621 call setline(1, ['a', 'b', 'c', 'd'])
1622 augroup test_cmd_filter_E135
1623 au!
1624 autocmd FilterReadPost * help
1625 augroup END
1626 call assert_fails('2,3!echo "x"', 'E135:')
1627
1628 augroup test_cmd_filter_E135
1629 au!
1630 augroup END
1631 %bwipe!
1632endfunc
1633
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001634" Test for using ~ for home directory in cmdline completion matches
1635func Test_cmdline_expand_home()
1636 call mkdir('Xdir')
1637 call writefile([], 'Xdir/Xfile1')
1638 call writefile([], 'Xdir/Xfile2')
1639 cd Xdir
1640 let save_HOME = $HOME
1641 let $HOME = getcwd()
1642 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1643 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1644 let $HOME = save_HOME
1645 cd ..
1646 call delete('Xdir', 'rf')
1647endfunc
1648
Bram Moolenaar578fe942020-02-27 21:32:51 +01001649" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1650" or insert mode (when 'insertmode' is set)
1651func Test_cmdline_ctrl_g()
1652 new
1653 call setline(1, 'abc')
1654 call cursor(1, 3)
1655 " If command line is entered from insert mode, using C-\ C-G should back to
1656 " insert mode
1657 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1658 call assert_equal('abxyc', getline(1))
1659 call assert_equal(4, col('.'))
1660
1661 " If command line is entered in 'insertmode', using C-\ C-G should back to
1662 " 'insertmode'
1663 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1664 call assert_equal('ab12xyc', getline(1))
1665 close!
1666endfunc
1667
Bram Moolenaar578fe942020-02-27 21:32:51 +01001668" Test for 'wildmode'
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001669func Wildmode_tests()
Bram Moolenaar578fe942020-02-27 21:32:51 +01001670 func T(a, c, p)
1671 return "oneA\noneB\noneC"
1672 endfunc
1673 command -nargs=1 -complete=custom,T MyCmd
1674
Bram Moolenaar578fe942020-02-27 21:32:51 +01001675 set nowildmenu
1676 set wildmode=full,list
1677 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001678 call feedkeys(":MyCmd \t\t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001679 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001680 call assert_equal('"MyCmd oneA', @:)
1681
1682 set wildmode=longest,full
1683 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1684 call assert_equal('"MyCmd one', @:)
1685 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1686 call assert_equal('"MyCmd oneC', @:)
1687
1688 set wildmode=longest
1689 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1690 call assert_equal('"MyCmd one', @:)
1691
1692 set wildmode=list:longest
1693 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001694 call feedkeys(":MyCmd \t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001695 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001696 call assert_equal('"MyCmd one', @:)
1697
1698 set wildmode=""
1699 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1700 call assert_equal('"MyCmd oneA', @:)
1701
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001702 " Test for wildmode=longest with 'fileignorecase' set
1703 set wildmode=longest
1704 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001705 argadd AAA AAAA AAAAA
1706 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1707 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001708 set fileignorecase&
1709
1710 " Test for listing files with wildmode=list
1711 set wildmode=list
1712 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001713 call feedkeys(":b A\t\t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001714 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001715 call assert_equal('"b A', @:)
1716
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001717 " when using longest completion match, matches shorter than the argument
1718 " should be ignored (happens with :help)
1719 set wildmode=longest,full
1720 set wildmenu
1721 call feedkeys(":help a*\t\<C-B>\"\<CR>", 'xt')
1722 call assert_equal('"help a', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001723 " non existing file
1724 call feedkeys(":e a1b2y3z4\t\<C-B>\"\<CR>", 'xt')
1725 call assert_equal('"e a1b2y3z4', @:)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001726 set wildmenu&
1727
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001728 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001729 delcommand MyCmd
1730 delfunc T
Bram Moolenaar578fe942020-02-27 21:32:51 +01001731 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001732 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001733endfunc
1734
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001735func Test_wildmode()
1736 " Test with utf-8 encoding
1737 call Wildmode_tests()
1738
1739 " Test with latin1 encoding
1740 let save_encoding = &encoding
1741 set encoding=latin1
1742 call Wildmode_tests()
1743 let &encoding = save_encoding
1744endfunc
1745
Bram Moolenaar578fe942020-02-27 21:32:51 +01001746" Test for interrupting the command-line completion
1747func Test_interrupt_compl()
1748 func F(lead, cmdl, p)
1749 if a:lead =~ 'tw'
1750 call interrupt()
1751 return
1752 endif
1753 return "one\ntwo\nthree"
1754 endfunc
1755 command -nargs=1 -complete=custom,F Tcmd
1756
1757 set nowildmenu
1758 set wildmode=full
1759 let interrupted = 0
1760 try
1761 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1762 catch /^Vim:Interrupt$/
1763 let interrupted = 1
1764 endtry
1765 call assert_equal(1, interrupted)
1766
1767 delcommand Tcmd
1768 delfunc F
1769 set wildmode&
1770endfunc
1771
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001772" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001773func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001774 let str = ":one two\<C-U>"
1775 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001776 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001777 let str ..= "\<Left>five\<Right>"
1778 let str ..= "\<Home>two "
1779 let str ..= "\<C-Left>one "
1780 let str ..= "\<C-Right> three"
1781 let str ..= "\<End>\<S-Left>four "
1782 let str ..= "\<S-Right> six"
1783 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1784 call feedkeys(str, 'xt')
1785 call assert_equal("\"one two three four five six seven", @:)
1786endfunc
1787
1788" Test for moving the cursor on the / command line in 'rightleft' mode
1789func Test_cmdline_edit_rightleft()
1790 CheckFeature rightleft
1791 set rightleft
1792 set rightleftcmd=search
1793 let str = "/one two\<C-U>"
1794 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001795 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001796 let str ..= "\<Right>five\<Left>"
1797 let str ..= "\<Home>two "
1798 let str ..= "\<C-Right>one "
1799 let str ..= "\<C-Left> three"
1800 let str ..= "\<End>\<S-Right>four "
1801 let str ..= "\<S-Left> six"
1802 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1803 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1804 call assert_equal("\"one two three four five six seven", @/)
1805 set rightleftcmd&
1806 set rightleft&
1807endfunc
1808
1809" Test for using <C-\>e in the command line to evaluate an expression
1810func Test_cmdline_expr()
1811 " Evaluate an expression from the beginning of a command line
1812 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1813 call assert_equal('"hello', @:)
1814
1815 " Use an invalid expression for <C-\>e
1816 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1817
1818 " Insert literal <CTRL-\> in the command line
1819 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1820 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001821endfunc
1822
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001823" Test for 'imcmdline' and 'imsearch'
1824" This test doesn't actually test the input method functionality.
1825func Test_cmdline_inputmethod()
1826 new
1827 call setline(1, ['', 'abc', ''])
1828 set imcmdline
1829
1830 call feedkeys(":\"abc\<CR>", 'xt')
1831 call assert_equal("\"abc", @:)
1832 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1833 call assert_equal("\"abc", @:)
1834 call feedkeys("/abc\<CR>", 'xt')
1835 call assert_equal([2, 1], [line('.'), col('.')])
1836 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1837 call assert_equal([2, 1], [line('.'), col('.')])
1838
1839 set imsearch=2
1840 call cursor(1, 1)
1841 call feedkeys("/abc\<CR>", 'xt')
1842 call assert_equal([2, 1], [line('.'), col('.')])
1843 call cursor(1, 1)
1844 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1845 call assert_equal([2, 1], [line('.'), col('.')])
1846 set imdisable
1847 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1848 call assert_equal([2, 1], [line('.'), col('.')])
1849 set imdisable&
1850 set imsearch&
1851
1852 set imcmdline&
1853 %bwipe!
1854endfunc
1855
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001856" Test for recursively getting multiple command line inputs
1857func Test_cmdwin_multi_input()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001858 CheckFeature cmdwin
1859
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001860 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1861 call assert_equal('"cyan', @:)
1862endfunc
1863
1864" Test for using CTRL-_ in the command line with 'allowrevins'
1865func Test_cmdline_revins()
1866 CheckNotMSWindows
1867 CheckFeature rightleft
1868 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1869 call assert_equal("\"abc\<c-_>", @:)
1870 set allowrevins
1871 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1872 call assert_equal('"abcñèæ', @:)
1873 set allowrevins&
1874endfunc
1875
1876" Test for typing UTF-8 composing characters in the command line
1877func Test_cmdline_composing_chars()
1878 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1879 call assert_equal('"ゔ', @:)
1880endfunc
1881
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001882" Test for normal mode commands not supported in the cmd window
1883func Test_cmdwin_blocked_commands()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001884 CheckFeature cmdwin
1885
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001886 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1887 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1888 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1889 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1890 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1891 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001892 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1893 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1894 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1895 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1896 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1897 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1898 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1899 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1900 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1901 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1902 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1903 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1904 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1905 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1906 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1907 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1908 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1909 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1910 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1911 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1912 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001913endfunc
1914
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001915" Close the Cmd-line window in insert mode using CTRL-C
1916func Test_cmdwin_insert_mode_close()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001917 CheckFeature cmdwin
1918
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001919 %bw!
1920 let s = ''
1921 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1922 call assert_equal('Hello', s)
1923 call assert_equal(1, winnr('$'))
1924endfunc
1925
Bram Moolenaar0e717042020-04-27 19:29:01 +02001926" test that ";" works to find a match at the start of the first line
1927func Test_zero_line_search()
1928 new
1929 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1930 call cursor(1,1)
1931 0;/pattern/d
1932 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1933 q!
1934endfunc
1935
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001936func Test_read_shellcmd()
1937 CheckUnix
1938 if executable('ls')
1939 " There should be ls in the $PATH
1940 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1941 call assert_match('^"r! .*\<ls\>', @:)
1942 endif
1943
1944 if executable('rm')
1945 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1946 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1947 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001948
1949 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1950 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1951 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001952 endif
1953endfunc
1954
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01001955" Test for going up and down the directory tree using 'wildmenu'
1956func Test_wildmenu_dirstack()
1957 CheckUnix
1958 %bw!
1959 call mkdir('Xdir1/dir2/dir3', 'p')
1960 call writefile([], 'Xdir1/file1_1.txt')
1961 call writefile([], 'Xdir1/file1_2.txt')
1962 call writefile([], 'Xdir1/dir2/file2_1.txt')
1963 call writefile([], 'Xdir1/dir2/file2_2.txt')
1964 call writefile([], 'Xdir1/dir2/dir3/file3_1.txt')
1965 call writefile([], 'Xdir1/dir2/dir3/file3_2.txt')
1966 cd Xdir1/dir2/dir3
1967 set wildmenu
1968
1969 call feedkeys(":e \<Tab>\<C-B>\"\<CR>", 'xt')
1970 call assert_equal('"e file3_1.txt', @:)
1971 call feedkeys(":e \<Tab>\<Up>\<C-B>\"\<CR>", 'xt')
1972 call assert_equal('"e ../dir3/', @:)
1973 call feedkeys(":e \<Tab>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
1974 call assert_equal('"e ../../dir2/', @:)
1975 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<C-B>\"\<CR>", 'xt')
1976 call assert_equal('"e ../../dir2/dir3/', @:)
1977 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
1978 call assert_equal('"e ../../dir2/dir3/file3_1.txt', @:)
1979
1980 cd -
1981 call delete('Xdir1', 'rf')
1982 set wildmenu&
1983endfunc
1984
obcat71c6f7a2021-05-13 20:23:10 +02001985" Test for recalling newer or older cmdline from history with <Up>, <Down>,
1986" <S-Up>, <S-Down>, <PageUp>, <PageDown>, <C-p>, or <C-n>.
1987func Test_recalling_cmdline()
1988 CheckFeature cmdline_hist
1989
1990 let g:cmdlines = []
1991 cnoremap <Plug>(save-cmdline) <Cmd>let g:cmdlines += [getcmdline()]<CR>
1992
1993 let histories = [
1994 \ {'name': 'cmd', 'enter': ':', 'exit': "\<Esc>"},
1995 \ {'name': 'search', 'enter': '/', 'exit': "\<Esc>"},
1996 \ {'name': 'expr', 'enter': ":\<C-r>=", 'exit': "\<Esc>\<Esc>"},
1997 \ {'name': 'input', 'enter': ":call input('')\<CR>", 'exit': "\<CR>"},
1998 "\ TODO: {'name': 'debug', ...}
1999 \]
2000 let keypairs = [
2001 \ {'older': "\<Up>", 'newer': "\<Down>", 'prefixmatch': v:true},
2002 \ {'older': "\<S-Up>", 'newer': "\<S-Down>", 'prefixmatch': v:false},
2003 \ {'older': "\<PageUp>", 'newer': "\<PageDown>", 'prefixmatch': v:false},
2004 \ {'older': "\<C-p>", 'newer': "\<C-n>", 'prefixmatch': v:false},
2005 \]
2006 let prefix = 'vi'
2007 for h in histories
2008 call histadd(h.name, 'vim')
2009 call histadd(h.name, 'virtue')
2010 call histadd(h.name, 'Virgo')
2011 call histadd(h.name, 'vogue')
2012 call histadd(h.name, 'emacs')
2013 for k in keypairs
2014 let g:cmdlines = []
2015 let keyseqs = h.enter
2016 \ .. prefix
2017 \ .. repeat(k.older .. "\<Plug>(save-cmdline)", 2)
2018 \ .. repeat(k.newer .. "\<Plug>(save-cmdline)", 2)
2019 \ .. h.exit
2020 call feedkeys(keyseqs, 'xt')
2021 call histdel(h.name, -1) " delete the history added by feedkeys above
2022 let expect = k.prefixmatch
2023 \ ? ['virtue', 'vim', 'virtue', prefix]
2024 \ : ['emacs', 'vogue', 'emacs', prefix]
2025 call assert_equal(expect, g:cmdlines)
2026 endfor
2027 endfor
2028
2029 unlet g:cmdlines
2030 cunmap <Plug>(save-cmdline)
2031endfunc
2032
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002033func Test_cmd_map_cmdlineChanged()
2034 let g:log = []
2035 cnoremap <F1> l<Cmd><CR>s
2036 augroup test
2037 autocmd!
2038 autocmd CmdlineChanged : let g:log += [getcmdline()]
2039 augroup END
2040
2041 call feedkeys(":\<F1>\<CR>", 'xt')
2042 call assert_equal(['l', 'ls'], g:log)
2043
Bram Moolenaar796139a2021-05-18 21:38:45 +02002044 let @b = 'b'
2045 cnoremap <F1> a<C-R>b
2046 let g:log = []
2047 call feedkeys(":\<F1>\<CR>", 'xt')
2048 call assert_equal(['a', 'ab'], g:log)
2049
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002050 unlet g:log
2051 cunmap <F1>
2052 augroup test
2053 autocmd!
2054 augroup END
2055endfunc
2056
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002057" Test for the 'suffixes' option
2058func Test_suffixes_opt()
2059 call writefile([], 'Xfile')
2060 call writefile([], 'Xfile.c')
2061 call writefile([], 'Xfile.o')
2062 set suffixes=
2063 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
2064 call assert_equal('"e Xfile Xfile.c Xfile.o', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00002065 call feedkeys(":e Xfi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2066 call assert_equal('"e Xfile.c', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002067 set suffixes=.c
2068 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
2069 call assert_equal('"e Xfile Xfile.o Xfile.c', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00002070 call feedkeys(":e Xfi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2071 call assert_equal('"e Xfile.o', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002072 set suffixes=,,
2073 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
2074 call assert_equal('"e Xfile.c Xfile.o Xfile', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00002075 call feedkeys(":e Xfi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2076 call assert_equal('"e Xfile.o', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002077 set suffixes&
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00002078 " Test for getcompletion() with different patterns
2079 call assert_equal(['Xfile', 'Xfile.c', 'Xfile.o'], getcompletion('Xfile', 'file'))
2080 call assert_equal(['Xfile'], getcompletion('Xfile$', 'file'))
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002081 call delete('Xfile')
2082 call delete('Xfile.c')
2083 call delete('Xfile.o')
2084endfunc
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002085
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002086" Test for using a popup menu for the command line completion matches
2087" (wildoptions=pum)
2088func Test_wildmenu_pum()
2089 CheckRunVimInTerminal
2090
2091 let commands =<< trim [CODE]
2092 set wildmenu
2093 set wildoptions=pum
2094 set shm+=I
2095 set noruler
2096 set noshowcmd
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002097
2098 func CmdCompl(a, b, c)
2099 return repeat(['aaaa'], 120)
2100 endfunc
2101 command -nargs=* -complete=customlist,CmdCompl Tcmd
Bram Moolenaar481acb12022-02-11 18:51:45 +00002102
2103 func MyStatusLine() abort
2104 return 'status'
2105 endfunc
2106 func SetupStatusline()
2107 set statusline=%!MyStatusLine()
2108 set laststatus=2
2109 endfunc
Bram Moolenaare4835bf2022-02-14 19:17:53 +00002110
2111 func MyTabLine()
2112 return 'my tab line'
2113 endfunc
2114 func SetupTabline()
2115 set statusline=
2116 set tabline=%!MyTabLine()
2117 set showtabline=2
2118 endfunc
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002119 [CODE]
2120 call writefile(commands, 'Xtest')
2121
2122 let buf = RunVimInTerminal('-S Xtest', #{rows: 10})
2123
2124 call term_sendkeys(buf, ":sign \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002125 call VerifyScreenDump(buf, 'Test_wildmenu_pum_01', {})
2126
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002127 " going down the popup menu using <Down>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002128 call term_sendkeys(buf, "\<Down>\<Down>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002129 call VerifyScreenDump(buf, 'Test_wildmenu_pum_02', {})
2130
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002131 " going down the popup menu using <C-N>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002132 call term_sendkeys(buf, "\<C-N>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002133 call VerifyScreenDump(buf, 'Test_wildmenu_pum_03', {})
2134
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002135 " going up the popup menu using <C-P>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002136 call term_sendkeys(buf, "\<C-P>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002137 call VerifyScreenDump(buf, 'Test_wildmenu_pum_04', {})
2138
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002139 " going up the popup menu using <Up>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002140 call term_sendkeys(buf, "\<Up>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002141 call VerifyScreenDump(buf, 'Test_wildmenu_pum_05', {})
2142
2143 " pressing <C-E> should end completion and go back to the original match
2144 call term_sendkeys(buf, "\<C-E>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002145 call VerifyScreenDump(buf, 'Test_wildmenu_pum_06', {})
2146
2147 " pressing <C-Y> should select the current match and end completion
2148 call term_sendkeys(buf, "\<Tab>\<C-P>\<C-P>\<C-Y>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002149 call VerifyScreenDump(buf, 'Test_wildmenu_pum_07', {})
2150
2151 " With 'wildmode' set to 'longest,full', completing a match should display
2152 " the longest match, the wildmenu should not be displayed.
2153 call term_sendkeys(buf, ":\<C-U>set wildmode=longest,full\<CR>")
2154 call TermWait(buf)
2155 call term_sendkeys(buf, ":sign u\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002156 call VerifyScreenDump(buf, 'Test_wildmenu_pum_08', {})
2157
2158 " pressing <Tab> should display the wildmenu
2159 call term_sendkeys(buf, "\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002160 call VerifyScreenDump(buf, 'Test_wildmenu_pum_09', {})
2161
2162 " pressing <Tab> second time should select the next entry in the menu
2163 call term_sendkeys(buf, "\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002164 call VerifyScreenDump(buf, 'Test_wildmenu_pum_10', {})
2165
2166 call term_sendkeys(buf, ":\<C-U>set wildmode=full\<CR>")
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002167 " showing popup menu in different columns in the cmdline
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002168 call term_sendkeys(buf, ":sign define \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002169 call VerifyScreenDump(buf, 'Test_wildmenu_pum_11', {})
2170
2171 call term_sendkeys(buf, " \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002172 call VerifyScreenDump(buf, 'Test_wildmenu_pum_12', {})
2173
2174 call term_sendkeys(buf, " \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002175 call VerifyScreenDump(buf, 'Test_wildmenu_pum_13', {})
2176
2177 " Directory name completion
2178 call mkdir('Xdir/XdirA/XdirB', 'p')
2179 call writefile([], 'Xdir/XfileA')
2180 call writefile([], 'Xdir/XdirA/XfileB')
2181 call writefile([], 'Xdir/XdirA/XdirB/XfileC')
2182
2183 call term_sendkeys(buf, "\<C-U>e Xdi\<Tab>\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002184 call VerifyScreenDump(buf, 'Test_wildmenu_pum_14', {})
2185
2186 " Pressing <Right> on a directory name should go into that directory
2187 call term_sendkeys(buf, "\<Right>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002188 call VerifyScreenDump(buf, 'Test_wildmenu_pum_15', {})
2189
2190 " Pressing <Left> on a directory name should go to the parent directory
2191 call term_sendkeys(buf, "\<Left>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002192 call VerifyScreenDump(buf, 'Test_wildmenu_pum_16', {})
2193
2194 " Pressing <C-A> when the popup menu is displayed should list all the
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002195 " matches but the popup menu should still remain
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002196 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-A>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002197 call VerifyScreenDump(buf, 'Test_wildmenu_pum_17', {})
2198
2199 " Pressing <C-D> when the popup menu is displayed should remove the popup
2200 " menu
2201 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-D>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002202 call VerifyScreenDump(buf, 'Test_wildmenu_pum_18', {})
2203
2204 " Pressing <S-Tab> should open the popup menu with the last entry selected
2205 call term_sendkeys(buf, "\<C-U>\<CR>:sign \<S-Tab>\<C-P>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002206 call VerifyScreenDump(buf, 'Test_wildmenu_pum_19', {})
2207
2208 " Pressing <Esc> should close the popup menu and cancel the cmd line
2209 call term_sendkeys(buf, "\<C-U>\<CR>:sign \<Tab>\<Esc>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002210 call VerifyScreenDump(buf, 'Test_wildmenu_pum_20', {})
2211
2212 " Typing a character when the popup is open, should close the popup
2213 call term_sendkeys(buf, ":sign \<Tab>x")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002214 call VerifyScreenDump(buf, 'Test_wildmenu_pum_21', {})
2215
2216 " When the popup is open, entering the cmdline window should close the popup
2217 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-F>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002218 call VerifyScreenDump(buf, 'Test_wildmenu_pum_22', {})
2219 call term_sendkeys(buf, ":q\<CR>")
2220
2221 " After the last popup menu item, <C-N> should show the original string
2222 call term_sendkeys(buf, ":sign u\<Tab>\<C-N>\<C-N>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002223 call VerifyScreenDump(buf, 'Test_wildmenu_pum_23', {})
2224
2225 " Use the popup menu for the command name
2226 call term_sendkeys(buf, "\<C-U>bu\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002227 call VerifyScreenDump(buf, 'Test_wildmenu_pum_24', {})
2228
2229 " Pressing the left arrow should remove the popup menu
2230 call term_sendkeys(buf, "\<Left>\<Left>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002231 call VerifyScreenDump(buf, 'Test_wildmenu_pum_25', {})
2232
2233 " Pressing <BS> should remove the popup menu and erase the last character
2234 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<BS>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002235 call VerifyScreenDump(buf, 'Test_wildmenu_pum_26', {})
2236
2237 " Pressing <C-W> should remove the popup menu and erase the previous word
2238 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<C-W>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002239 call VerifyScreenDump(buf, 'Test_wildmenu_pum_27', {})
2240
2241 " Pressing <C-U> should remove the popup menu and erase the entire line
2242 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<C-U>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002243 call VerifyScreenDump(buf, 'Test_wildmenu_pum_28', {})
2244
2245 " Using <C-E> to cancel the popup menu and then pressing <Up> should recall
2246 " the cmdline from history
2247 call term_sendkeys(buf, "sign xyz\<Esc>:sign \<Tab>\<C-E>\<Up>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002248 call VerifyScreenDump(buf, 'Test_wildmenu_pum_29', {})
2249
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002250 " Check "list" still works
2251 call term_sendkeys(buf, "\<C-U>set wildmode=longest,list\<CR>")
2252 call term_sendkeys(buf, ":cn\<Tab>")
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002253 call VerifyScreenDump(buf, 'Test_wildmenu_pum_30', {})
2254 call term_sendkeys(buf, "s")
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002255 call VerifyScreenDump(buf, 'Test_wildmenu_pum_31', {})
2256
rbtnn68cc2b82022-02-09 11:55:47 +00002257 " Tests a directory name contained full-width characters.
2258 call mkdir('Xdir/あいう', 'p')
2259 call writefile([], 'Xdir/あいう/abc')
2260 call writefile([], 'Xdir/あいう/xyz')
2261 call writefile([], 'Xdir/あいう/123')
2262
2263 call term_sendkeys(buf, "\<C-U>set wildmode&\<CR>")
2264 call term_sendkeys(buf, ":\<C-U>e Xdir/あいう/\<Tab>")
rbtnn68cc2b82022-02-09 11:55:47 +00002265 call VerifyScreenDump(buf, 'Test_wildmenu_pum_32', {})
2266
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002267 " Pressing <C-A> when the popup menu is displayed should list all the
2268 " matches and pressing a key after that should remove the popup menu
2269 call term_sendkeys(buf, "\<C-U>set wildmode=full\<CR>")
2270 call term_sendkeys(buf, ":sign \<Tab>\<C-A>x")
2271 call VerifyScreenDump(buf, 'Test_wildmenu_pum_33', {})
2272
2273 " Pressing <C-A> when the popup menu is displayed should list all the
2274 " matches and pressing <Left> after that should move the cursor
2275 call term_sendkeys(buf, "\<C-U>abc\<Esc>")
2276 call term_sendkeys(buf, ":sign \<Tab>\<C-A>\<Left>")
2277 call VerifyScreenDump(buf, 'Test_wildmenu_pum_34', {})
2278
2279 " When <C-A> displays a lot of matches (screen scrolls), all the matches
2280 " should be displayed correctly on the screen.
2281 call term_sendkeys(buf, "\<End>\<C-U>Tcmd \<Tab>\<C-A>\<Left>\<Left>")
2282 call VerifyScreenDump(buf, 'Test_wildmenu_pum_35', {})
2283
2284 " After using <C-A> to expand all the filename matches, pressing <Up>
2285 " should not open the popup menu again.
2286 call term_sendkeys(buf, "\<C-E>\<C-U>:cd Xdir/XdirA\<CR>")
2287 call term_sendkeys(buf, ":e \<Tab>\<C-A>\<Up>")
2288 call VerifyScreenDump(buf, 'Test_wildmenu_pum_36', {})
2289 call term_sendkeys(buf, "\<C-E>\<C-U>:cd -\<CR>")
2290
2291 " After using <C-A> to expand all the matches, pressing <S-Tab> used to
2292 " crash Vim
2293 call term_sendkeys(buf, ":sign \<Tab>\<C-A>\<S-Tab>")
2294 call VerifyScreenDump(buf, 'Test_wildmenu_pum_37', {})
2295
Bram Moolenaar414acd32022-02-10 21:09:45 +00002296 " After removing the pum the command line is redrawn
2297 call term_sendkeys(buf, ":edit foo\<CR>")
2298 call term_sendkeys(buf, ":edit bar\<CR>")
2299 call term_sendkeys(buf, ":ls\<CR>")
2300 call term_sendkeys(buf, ":com\<Tab> ")
2301 call VerifyScreenDump(buf, 'Test_wildmenu_pum_38', {})
Bram Moolenaar481acb12022-02-11 18:51:45 +00002302 call term_sendkeys(buf, "\<C-U>\<CR>")
2303
2304 " Esc still works to abort the command when 'statusline' is set
2305 call term_sendkeys(buf, ":call SetupStatusline()\<CR>")
2306 call term_sendkeys(buf, ":si\<Tab>")
2307 call term_sendkeys(buf, "\<Esc>")
2308 call VerifyScreenDump(buf, 'Test_wildmenu_pum_39', {})
Bram Moolenaar414acd32022-02-10 21:09:45 +00002309
Bram Moolenaare4835bf2022-02-14 19:17:53 +00002310 " Esc still works to abort the command when 'tabline' is set
2311 call term_sendkeys(buf, ":call SetupTabline()\<CR>")
2312 call term_sendkeys(buf, ":si\<Tab>")
2313 call term_sendkeys(buf, "\<Esc>")
2314 call VerifyScreenDump(buf, 'Test_wildmenu_pum_40', {})
2315
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002316 call term_sendkeys(buf, "\<C-U>\<CR>")
2317 call StopVimInTerminal(buf)
2318 call delete('Xtest')
2319 call delete('Xdir', 'rf')
2320endfunc
2321
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002322" Test for wildmenumode() with the cmdline popup menu
2323func Test_wildmenumode_with_pum()
2324 set wildmenu
2325 set wildoptions=pum
2326 cnoremap <expr> <F2> wildmenumode()
2327 call feedkeys(":sign \<Tab>\<F2>\<F2>\<C-B>\"\<CR>", 'xt')
2328 call assert_equal('"sign define10', @:)
2329 call feedkeys(":sign \<Tab>\<C-A>\<F2>\<C-B>\"\<CR>", 'xt')
2330 call assert_equal('"sign define jump list place undefine unplace0', @:)
2331 call feedkeys(":sign \<Tab>\<C-E>\<F2>\<C-B>\"\<CR>", 'xt')
2332 call assert_equal('"sign 0', @:)
2333 call feedkeys(":sign \<Tab>\<C-Y>\<F2>\<C-B>\"\<CR>", 'xt')
2334 call assert_equal('"sign define0', @:)
2335 set nowildmenu wildoptions&
2336 cunmap <F2>
2337endfunc
2338
Bram Moolenaar309976e2019-12-05 18:16:33 +01002339" vim: shiftwidth=2 sts=2 expandtab