blob: 295e59db2f0e4d01e3882306fd3b580df7f28331 [file] [log] [blame]
Bram Moolenaarae3150e2016-06-11 23:22:36 +02001" Tests for editing the command line.
2
Bram Moolenaar4facea32019-10-12 20:17:40 +02003source check.vim
4source screendump.vim
Bram Moolenaar24ebd832020-03-16 21:25:24 +01005source view_util.vim
Bram Moolenaarc82dd862020-06-07 17:30:33 +02006source shared.vim
Bram Moolenaar4facea32019-10-12 20:17:40 +02007
Bram Moolenaarae3150e2016-06-11 23:22:36 +02008func Test_complete_tab()
9 call writefile(['testfile'], 'Xtestfile')
10 call feedkeys(":e Xtest\t\r", "tx")
11 call assert_equal('testfile', getline(1))
Albert Liu6024c042021-08-27 20:59:35 +020012
13 " Pressing <Tab> after '%' completes the current file, also on MS-Windows
14 call feedkeys(":e %\t\r", "tx")
15 call assert_equal('e Xtestfile', @:)
Bram Moolenaarae3150e2016-06-11 23:22:36 +020016 call delete('Xtestfile')
17endfunc
18
19func Test_complete_list()
20 " We can't see the output, but at least we check the code runs properly.
21 call feedkeys(":e test\<C-D>\r", "tx")
22 call assert_equal('test', expand('%:t'))
Bram Moolenaar578fe942020-02-27 21:32:51 +010023
24 " If a command doesn't support completion, then CTRL-D should be literally
25 " used.
26 call feedkeys(":chistory \<C-D>\<C-B>\"\<CR>", 'xt')
27 call assert_equal("\"chistory \<C-D>", @:)
Bram Moolenaarae3150e2016-06-11 23:22:36 +020028endfunc
29
30func Test_complete_wildmenu()
Bram Moolenaar37db6422019-03-28 21:26:23 +010031 call mkdir('Xdir1/Xdir2', 'p')
32 call writefile(['testfile1'], 'Xdir1/Xtestfile1')
33 call writefile(['testfile2'], 'Xdir1/Xtestfile2')
34 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3')
35 call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020036 set wildmenu
Bram Moolenaar37db6422019-03-28 21:26:23 +010037
38 " Pressing <Tab> completes, and moves to next files when pressing again.
39 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx')
40 call assert_equal('testfile1', getline(1))
41 call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020042 call assert_equal('testfile2', getline(1))
43
Bram Moolenaar37db6422019-03-28 21:26:23 +010044 " <S-Tab> is like <Tab> but begin with the last match and then go to
45 " previous.
46 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx')
47 call assert_equal('testfile2', getline(1))
48 call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
49 call assert_equal('testfile1', getline(1))
50
51 " <Left>/<Right> to move to previous/next file.
52 call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx')
53 call assert_equal('testfile1', getline(1))
54 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
55 call assert_equal('testfile2', getline(1))
56 call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
57 call assert_equal('testfile1', getline(1))
58
59 " <Up>/<Down> to go up/down directories.
60 call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx')
61 call assert_equal('testfile3', getline(1))
62 call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
63 call assert_equal('testfile1', getline(1))
64
Bram Moolenaar3e112ac2020-12-28 13:41:53 +010065 " this fails in some Unix GUIs, not sure why
66 if !has('unix') || !has('gui_running')
67 " <C-J>/<C-K> mappings to go up/down directories when 'wildcharm' is
68 " different than 'wildchar'.
69 set wildcharm=<C-Z>
70 cnoremap <C-J> <Down><C-Z>
71 cnoremap <C-K> <Up><C-Z>
72 call feedkeys(":e Xdir1/\<Tab>\<C-J>\<CR>", 'tx')
73 call assert_equal('testfile3', getline(1))
74 call feedkeys(":e Xdir1/\<Tab>\<C-J>\<C-K>\<CR>", 'tx')
75 call assert_equal('testfile1', getline(1))
76 set wildcharm=0
77 cunmap <C-J>
78 cunmap <C-K>
79 endif
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +010080
Bram Moolenaar578fe942020-02-27 21:32:51 +010081 " Test for canceling the wild menu by adding a character
82 redrawstatus
83 call feedkeys(":e Xdir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
84 call assert_equal('"e Xdir1/Xdir2/x', @:)
85
Bram Moolenaar8d588cc2020-02-25 21:47:45 +010086 " Completion using a relative path
87 cd Xdir1/Xdir2
88 call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
89 call assert_equal('"e Xtestfile3 Xtestfile4', @:)
90 cd -
91
Bram Moolenaar0e05de42020-03-25 22:23:46 +010092 cnoremap <expr> <F2> wildmenumode()
93 call feedkeys(":cd Xdir\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
94 call assert_equal('"cd Xdir1/1', @:)
95 cunmap <F2>
96
Bram Moolenaar37db6422019-03-28 21:26:23 +010097 " cleanup
98 %bwipe
99 call delete('Xdir1/Xdir2/Xtestfile4')
100 call delete('Xdir1/Xdir2/Xtestfile3')
101 call delete('Xdir1/Xtestfile2')
102 call delete('Xdir1/Xtestfile1')
103 call delete('Xdir1/Xdir2', 'd')
104 call delete('Xdir1', 'd')
Bram Moolenaarae3150e2016-06-11 23:22:36 +0200105 set nowildmenu
106endfunc
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100107
Bram Moolenaara60053b2020-09-03 16:50:13 +0200108func Test_wildmenu_screendump()
109 CheckScreendump
110
111 let lines =<< trim [SCRIPT]
112 set wildmenu hlsearch
113 [SCRIPT]
114 call writefile(lines, 'XTest_wildmenu')
115
116 let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
117 call term_sendkeys(buf, ":vim\<Tab>")
118 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
119
120 call term_sendkeys(buf, "\<Tab>")
121 call VerifyScreenDump(buf, 'Test_wildmenu_2', {})
122
123 call term_sendkeys(buf, "\<Tab>")
124 call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
125
Bram Moolenaar39f3b142021-02-14 12:57:36 +0100126 call term_sendkeys(buf, "\<Tab>\<Tab>")
Bram Moolenaara60053b2020-09-03 16:50:13 +0200127 call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
128 call term_sendkeys(buf, "\<Esc>")
129
130 " clean up
131 call StopVimInTerminal(buf)
132 call delete('XTest_wildmenu')
133endfunc
134
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100135func Test_map_completion()
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100136 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
137 call assert_equal('"map <unique> <silent>', getreg(':'))
138 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
139 call assert_equal('"map <script> <unique>', getreg(':'))
140 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
141 call assert_equal('"map <expr> <script>', getreg(':'))
142 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
143 call assert_equal('"map <buffer> <expr>', getreg(':'))
144 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
145 call assert_equal('"map <nowait> <buffer>', getreg(':'))
146 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
147 call assert_equal('"map <special> <nowait>', getreg(':'))
148 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
149 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200150
Bram Moolenaar1776a282019-05-03 16:05:41 +0200151 map <Middle>x middle
152
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200153 map ,f commaf
154 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200155 map <Left> left
156 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200157 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
158 call assert_equal('"map ,f', getreg(':'))
159 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
160 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200161 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
162 call assert_equal('"map <Left>', getreg(':'))
163 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200164 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200165 unmap ,f
166 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200167 unmap <Left>
168 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200169
170 set cpo-=< cpo-=B cpo-=k
171 map <Left> left
172 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
173 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200174 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200175 call assert_equal("\"map <M\<Tab>", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200176 unmap <Left>
177
178 set cpo+=<
179 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200180 exe "set t_k6=\<Esc>[17~"
181 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200182 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
183 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200184 if !has('gui_running')
185 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
186 call assert_equal("\"map <F6>x", getreg(':'))
187 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200188 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200189 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200190 set cpo-=<
191
192 set cpo+=B
193 map <Left> left
194 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
195 call assert_equal('"map <Left>', getreg(':'))
196 unmap <Left>
197 set cpo-=B
198
199 set cpo+=k
200 map <Left> left
201 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
202 call assert_equal('"map <Left>', getreg(':'))
203 unmap <Left>
204 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200205
Bram Moolenaar531be472020-09-23 22:38:05 +0200206 call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
207
Bram Moolenaar1776a282019-05-03 16:05:41 +0200208 unmap <Middle>x
209 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100210endfunc
211
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100212func Test_match_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100213 hi Aardig ctermfg=green
214 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
215 call assert_equal('"match Aardig', getreg(':'))
216 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
217 call assert_equal('"match none', getreg(':'))
218endfunc
219
220func Test_highlight_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100221 hi Aardig ctermfg=green
222 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
223 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200224 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
225 call assert_equal('"hi default Aardig', getreg(':'))
226 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
227 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100228 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
229 call assert_equal('"hi link', getreg(':'))
230 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
231 call assert_equal('"hi default', getreg(':'))
232 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
233 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200234 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
235 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
236 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
237 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200238
239 " A cleared group does not show up in completions.
240 hi Anders ctermfg=green
241 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
242 hi clear Aardig
243 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
244 hi clear Anders
245 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100246endfunc
247
Bram Moolenaar75e15672020-06-28 13:10:22 +0200248" Test for command-line expansion of "hi Ni " (easter egg)
249func Test_highlight_easter_egg()
250 call test_override('ui_delay', 1)
251 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
252 call assert_equal("\"hi Ni \<Tab>", @:)
253 call test_override('ALL', 0)
254endfunc
255
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200256func Test_getcompletion()
257 let groupcount = len(getcompletion('', 'event'))
258 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200259 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200260 call assert_true(matchcount > 0)
261 call assert_true(groupcount > matchcount)
262
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200263 if has('menu')
264 source $VIMRUNTIME/menu.vim
265 let matchcount = len(getcompletion('', 'menu'))
266 call assert_true(matchcount > 0)
267 call assert_equal(['File.'], getcompletion('File', 'menu'))
268 call assert_true(matchcount > 0)
269 let matchcount = len(getcompletion('File.', 'menu'))
270 call assert_true(matchcount > 0)
271 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200272
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200273 let l = getcompletion('v:n', 'var')
274 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200275 let l = getcompletion('v:notexists', 'var')
276 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200277
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200278 args a.c b.c
279 let l = getcompletion('', 'arglist')
280 call assert_equal(['a.c', 'b.c'], l)
281 %argdelete
282
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200283 let l = getcompletion('', 'augroup')
284 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200285 let l = getcompletion('blahblah', 'augroup')
286 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200287
288 let l = getcompletion('', 'behave')
289 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200290 let l = getcompletion('not', 'behave')
291 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200292
293 let l = getcompletion('', 'color')
294 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200295 let l = getcompletion('dirty', 'color')
296 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200297
298 let l = getcompletion('', 'command')
299 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200300 let l = getcompletion('awake', 'command')
301 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200302
303 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200304 call assert_true(index(l, 'samples/') >= 0)
305 let l = getcompletion('NoMatch', 'dir')
306 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200307
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100308 if glob('~/*') !=# ''
309 let l = getcompletion('~/', 'dir')
310 call assert_true(l[0][0] ==# '~')
311 endif
312
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200313 let l = getcompletion('exe', 'expression')
314 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200315 let l = getcompletion('kill', 'expression')
316 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200317
318 let l = getcompletion('tag', 'function')
319 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200320 let l = getcompletion('paint', 'function')
321 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200322
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200323 let Flambda = {-> 'hello'}
324 let l = getcompletion('', 'function')
325 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200326 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200327
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200328 let l = getcompletion('run', 'file')
329 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200330 let l = getcompletion('walk', 'file')
331 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200332 set wildignore=*.vim
333 let l = getcompletion('run', 'file', 1)
334 call assert_true(index(l, 'runtest.vim') < 0)
335 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200336
337 let l = getcompletion('ha', 'filetype')
338 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200339 let l = getcompletion('horse', 'filetype')
340 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200341
342 let l = getcompletion('z', 'syntax')
343 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200344 let l = getcompletion('emacs', 'syntax')
345 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200346
347 let l = getcompletion('jikes', 'compiler')
348 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200349 let l = getcompletion('break', 'compiler')
350 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200351
352 let l = getcompletion('last', 'help')
353 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200354 let l = getcompletion('giveup', 'help')
355 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200356
357 let l = getcompletion('time', 'option')
358 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200359 let l = getcompletion('space', 'option')
360 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200361
362 let l = getcompletion('er', 'highlight')
363 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200364 let l = getcompletion('dark', 'highlight')
365 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200366
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200367 let l = getcompletion('', 'messages')
368 call assert_true(index(l, 'clear') >= 0)
369 let l = getcompletion('not', 'messages')
370 call assert_equal([], l)
371
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200372 let l = getcompletion('', 'mapclear')
373 call assert_true(index(l, '<buffer>') >= 0)
374 let l = getcompletion('not', 'mapclear')
375 call assert_equal([], l)
376
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200377 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200378 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200379 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
380 let root = has('win32') ? 'C:\\' : '/'
381 let l = getcompletion(root, 'shellcmd')
382 let expected = map(filter(glob(root . '*', 0, 1),
383 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
384 call assert_equal(expected, l)
385
Bram Moolenaarb650b982016-08-05 20:35:13 +0200386 if has('cscope')
387 let l = getcompletion('', 'cscope')
388 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
389 call assert_equal(cmds, l)
390 " using cmdline completion must not change the result
391 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
392 let l = getcompletion('', 'cscope')
393 call assert_equal(cmds, l)
394 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
395 let l = getcompletion('find ', 'cscope')
396 call assert_equal(keys, l)
397 endif
398
Bram Moolenaar7522f692016-08-06 14:12:50 +0200399 if has('signs')
400 sign define Testing linehl=Comment
401 let l = getcompletion('', 'sign')
402 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
403 call assert_equal(cmds, l)
404 " using cmdline completion must not change the result
405 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
406 let l = getcompletion('', 'sign')
407 call assert_equal(cmds, l)
408 let l = getcompletion('list ', 'sign')
409 call assert_equal(['Testing'], l)
410 endif
411
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200412 " Command line completion tests
413 let l = getcompletion('cd ', 'cmdline')
414 call assert_true(index(l, 'samples/') >= 0)
415 let l = getcompletion('cd NoMatch', 'cmdline')
416 call assert_equal([], l)
417 let l = getcompletion('let v:n', 'cmdline')
418 call assert_true(index(l, 'v:null') >= 0)
419 let l = getcompletion('let v:notexists', 'cmdline')
420 call assert_equal([], l)
421 let l = getcompletion('call tag', 'cmdline')
422 call assert_true(index(l, 'taglist(') >= 0)
423 let l = getcompletion('call paint', 'cmdline')
424 call assert_equal([], l)
425
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100426 func T(a, c, p)
427 return "oneA\noneB\noneC"
428 endfunc
429 command -nargs=1 -complete=custom,T MyCmd
430 let l = getcompletion('MyCmd ', 'cmdline')
431 call assert_equal(['oneA', 'oneB', 'oneC'], l)
432
433 delcommand MyCmd
434 delfunc T
435
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200436 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200437 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200438 if has('cmdline_hist')
439 call add(names, 'history')
440 endif
441 if has('gettext')
442 call add(names, 'locale')
443 endif
444 if has('profile')
445 call add(names, 'syntime')
446 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200447
448 set tags=Xtags
449 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
450
451 for name in names
452 let matchcount = len(getcompletion('', name))
453 call assert_true(matchcount >= 0, 'No matches for ' . name)
454 endfor
455
456 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200457 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200458
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200459 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200460 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200461 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200462endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200463
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100464func Test_fullcommand()
465 let tests = {
466 \ '': '',
467 \ ':': '',
468 \ ':::': '',
469 \ ':::5': '',
470 \ 'not_a_cmd': '',
471 \ 'Check': '',
472 \ 'syntax': 'syntax',
473 \ ':syntax': 'syntax',
474 \ '::::syntax': 'syntax',
475 \ 'sy': 'syntax',
476 \ 'syn': 'syntax',
477 \ 'synt': 'syntax',
478 \ ':sy': 'syntax',
479 \ '::::sy': 'syntax',
480 \ 'match': 'match',
481 \ '2match': 'match',
482 \ '3match': 'match',
483 \ 'aboveleft': 'aboveleft',
484 \ 'abo': 'aboveleft',
485 \ 's': 'substitute',
486 \ '5s': 'substitute',
487 \ ':5s': 'substitute',
488 \ "'<,'>s": 'substitute',
489 \ ":'<,'>s": 'substitute',
490 \ 'CheckUni': 'CheckUnix',
491 \ 'CheckUnix': 'CheckUnix',
492 \ }
493
494 for [in, want] in items(tests)
495 call assert_equal(want, fullcommand(in))
496 endfor
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +0200497 call assert_equal('', fullcommand(test_null_string()))
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100498
499 call assert_equal('syntax', 'syn'->fullcommand())
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200500
501 command -buffer BufferLocalCommand :
502 command GlobalCommand :
503 call assert_equal('GlobalCommand', fullcommand('GlobalCom'))
504 call assert_equal('BufferLocalCommand', fullcommand('BufferL'))
505 delcommand BufferLocalCommand
506 delcommand GlobalCommand
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100507endfunc
508
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200509func Test_shellcmd_completion()
510 let save_path = $PATH
511
512 call mkdir('Xpathdir/Xpathsubdir', 'p')
513 call writefile([''], 'Xpathdir/Xfile.exe')
514 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
515
516 " Set PATH to example directory without trailing slash.
517 let $PATH = getcwd() . '/Xpathdir'
518
519 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
520 " dirs in the PATH, even though they won't be executed. We check that only
521 " subdirs of the PWD and executables from the PATH are included in the
522 " suggestions.
523 let actual = getcompletion('X', 'shellcmd')
524 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
525 call insert(expected, 'Xfile.exe')
526 call assert_equal(expected, actual)
527
528 call delete('Xpathdir', 'rf')
529 let $PATH = save_path
530endfunc
531
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200532func Test_expand_star_star()
533 call mkdir('a/b', 'p')
534 call writefile(['asdfasdf'], 'a/b/fileXname')
535 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
536 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200537 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200538 call delete('a', 'rf')
539endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100540
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100541func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100542 let @a = "def"
543 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
544 call assert_equal('"abc def ghi', @:)
545
546 new
547 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
548
549 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
550 call assert_equal('"aaa asdf bbb', @:)
551
552 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
553 call assert_equal('"aaa /tmp/some bbb', @:)
554
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200555 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
556 call assert_equal('"aaa '.getline(1).' bbb', @:)
557
Bram Moolenaar21efc362016-12-03 14:05:49 +0100558 set incsearch
559 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
560 call assert_equal('"aaa verylongword bbb', @:)
561
562 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
563 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100564
565 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
566 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100567 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200568
569 " Error while typing a command used to cause that it was not executed
570 " in the end.
571 new
572 try
573 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
574 catch /^Vim\%((\a\+)\)\=:E32/
575 " ignore error E32
576 endtry
577 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100578
Bram Moolenaar578fe942020-02-27 21:32:51 +0100579 " Try to paste an invalid register using <C-R>
580 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
581 call assert_equal('"onetwo', @:)
582
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100583 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100584 let @a = "xy\<C-H>z"
585 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
586 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100587 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
588 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100589 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
590 call assert_equal("\"xy\<C-H>z", @:)
591
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100592 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
593 let @a = "xy\<C-V>z"
594 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
595 call assert_equal('"xyz', @:)
596 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
597 call assert_equal("\"xy\<C-V>z", @:)
598
Bram Moolenaar578fe942020-02-27 21:32:51 +0100599 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
600
Bram Moolenaar72532d32018-04-07 19:09:09 +0200601 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100602endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100603
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100604func Test_cmdline_remove_char()
605 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100606
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100607 for e in ['utf8', 'latin1']
608 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100609
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100610 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
611 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100612
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100613 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
614 call assert_equal('"abcdef', @:)
615
616 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
617 call assert_equal('"abc ghi', @:, e)
618
619 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
620 call assert_equal('"def', @:, e)
621 endfor
622
623 let &encoding = encoding_save
624endfunc
625
626func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200627 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100628
629 set keymap=esperanto
630 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
631 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
632 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100633endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100634
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100635func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100636 new
637 2;'(
638 2;')
639 quit
640endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100641
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100642func Test_illegal_address2()
643 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
644 new
645 source Xtest.vim
646 " Trigger calling validate_cursor()
647 diffsp Xtest.vim
648 quit!
649 bwipe!
650 call delete('Xtest.vim')
651endfunc
652
Bram Moolenaarba47b512017-01-24 21:18:19 +0100653func Test_cmdline_complete_wildoptions()
654 help
655 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
656 let a = join(sort(split(@:)),' ')
657 set wildoptions=tagfile
658 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
659 let b = join(sort(split(@:)),' ')
660 call assert_equal(a, b)
661 bw!
662endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100663
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200664func Test_cmdline_complete_user_cmd()
665 command! -complete=color -nargs=1 Foo :
666 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
667 call assert_equal('"Foo blue', @:)
668 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
669 call assert_equal('"Foo blue', @:)
670 delcommand Foo
671endfunc
672
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100673func s:ScriptLocalFunction()
674 echo 'yes'
675endfunc
676
677func Test_cmdline_complete_user_func()
678 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
naohiro ono5aec7552021-08-19 21:20:41 +0200679 call assert_match('"func Test_cmdline_complete_user_', @:)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100680 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
681 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +0100682
683 " g: prefix also works
684 call feedkeys(":echo g:Test_cmdline_complete_user_f\<Tab>\<Home>\"\<cr>", 'tx')
685 call assert_match('"echo g:Test_cmdline_complete_user_func', @:)
Bram Moolenaar069f9082021-08-13 17:48:25 +0200686
687 " using g: prefix does not result in just "g:" matches from a lambda
688 let Fx = { a -> a }
689 call feedkeys(":echo g:\<Tab>\<Home>\"\<cr>", 'tx')
690 call assert_match('"echo g:[A-Z]', @:)
naohiro ono5aec7552021-08-19 21:20:41 +0200691
692 " existence of script-local dict function does not break user function name
693 " completion
694 function s:a_dict_func() dict
695 endfunction
696 call feedkeys(":call Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
697 call assert_match('"call Test_cmdline_complete_user_', @:)
698 delfunction s:a_dict_func
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100699endfunc
700
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200701func Test_cmdline_complete_user_names()
702 if has('unix') && executable('whoami')
703 let whoami = systemlist('whoami')[0]
704 let first_letter = whoami[0]
705 if len(first_letter) > 0
706 " Trying completion of :e ~x where x is the first letter of
707 " the user name should complete to at least the user name.
708 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
709 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
710 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200711 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200712 " Just in case: check that the system has an Administrator account.
713 let names = system('net user')
714 if names =~ 'Administrator'
715 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100716 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200717 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100718 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200719 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200720 else
721 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200722 endif
723endfunc
724
Bram Moolenaar297610b2019-12-27 17:20:55 +0100725func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200726 CheckExecutable whoami
727 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
728 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100729endfunc
730
Bram Moolenaar8b633132020-03-20 18:20:51 +0100731func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200732 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
733 call assert_equal(lang, v:lc_time)
734
735 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
736 call assert_equal(lang, v:ctype)
737
738 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
739 call assert_equal(lang, v:collate)
740
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200741 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200742 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200743
744 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200745 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200746
Bram Moolenaarec680282020-06-12 19:35:32 +0200747 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200748
Bram Moolenaarec680282020-06-12 19:35:32 +0200749 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
750 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200751
Bram Moolenaarec680282020-06-12 19:35:32 +0200752 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
753 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200754
Bram Moolenaarec680282020-06-12 19:35:32 +0200755 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
756 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200757
758 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
759 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200760endfunc
761
Bram Moolenaar297610b2019-12-27 17:20:55 +0100762func Test_cmdline_complete_env_variable()
763 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100764 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
765 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100766 unlet $X_VIM_TEST_COMPLETE_ENV
767endfunc
768
Bram Moolenaar4941b5e2020-12-24 17:15:53 +0100769func Test_cmdline_complete_expression()
770 let g:SomeVar = 'blah'
771 for cmd in ['exe', 'echo', 'echon', 'echomsg']
772 call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
773 call assert_match('"' .. cmd .. ' SomeVar', @:)
774 call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
775 call assert_match('"' .. cmd .. ' foo SomeVar', @:)
776 endfor
777 unlet g:SomeVar
778endfunc
779
Bram Moolenaar47016f52021-08-26 16:39:58 +0200780" Unique function name for completion below
781func s:WeirdFunc()
782 echo 'weird'
783endfunc
784
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100785" Test for various command-line completion
786func Test_cmdline_complete_various()
787 " completion for a command starting with a comment
788 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
789 call assert_equal("\" :|\"\<C-A>", @:)
790
791 " completion for a range followed by a comment
792 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
793 call assert_equal("\"1,2\"\<C-A>", @:)
794
795 " completion for :k command
796 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
797 call assert_equal("\"ka\<C-A>", @:)
798
799 " completion for short version of the :s command
800 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
801 call assert_equal("\"sI \<C-A>", @:)
802
803 " completion for :write command
804 call mkdir('Xdir')
805 call writefile(['one'], 'Xdir/Xfile1')
806 let save_cwd = getcwd()
807 cd Xdir
808 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
809 call assert_equal("\"w >> Xfile1", @:)
810 call chdir(save_cwd)
811 call delete('Xdir', 'rf')
812
813 " completion for :w ! and :r ! commands
814 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
815 call assert_equal("\"w !invalid_xyz_cmd", @:)
816 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
817 call assert_equal("\"r !invalid_xyz_cmd", @:)
818
819 " completion for :>> and :<< commands
820 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
821 call assert_equal("\">>>\<C-A>", @:)
822 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
823 call assert_equal("\"<<<\<C-A>", @:)
824
825 " completion for command with +cmd argument
826 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
827 call assert_equal("\"buffer +/pat Xabc", @:)
828 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
829 call assert_equal("\"buffer +/pat\<C-A>", @:)
830
831 " completion for a command with a trailing comment
832 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
833 call assert_equal("\"ls \" comment\<C-A>", @:)
834
835 " completion for a command with a trailing command
836 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
837 call assert_equal("\"ls | ls", @:)
838
839 " completion for a command with an CTRL-V escaped argument
840 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
841 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
842
843 " completion for a command that doesn't take additional arguments
844 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
845 call assert_equal("\"all abc\<C-A>", @:)
846
847 " completion for a command with a command modifier
848 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
849 call assert_equal("\"topleft new", @:)
850
Bram Moolenaare70e12b2021-06-13 17:20:08 +0200851 " completion for vim9 and legacy commands
852 call feedkeys(":vim9 call strle\<C-A>\<C-B>\"\<CR>", 'xt')
853 call assert_equal("\"vim9 call strlen(", @:)
854 call feedkeys(":legac call strle\<C-A>\<C-B>\"\<CR>", 'xt')
855 call assert_equal("\"legac call strlen(", @:)
856
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +0200857 " completion for the :disassemble command
858 call feedkeys(":disas deb\<C-A>\<C-B>\"\<CR>", 'xt')
859 call assert_equal("\"disas debug", @:)
860 call feedkeys(":disas pro\<C-A>\<C-B>\"\<CR>", 'xt')
861 call assert_equal("\"disas profile", @:)
862 call feedkeys(":disas debug Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
863 call assert_equal("\"disas debug Test_cmdline_complete_various", @:)
864 call feedkeys(":disas profile Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
865 call assert_equal("\"disas profile Test_cmdline_complete_various", @:)
naohiro ono9aecf792021-08-28 15:56:06 +0200866 call feedkeys(":disas Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
867 call assert_equal("\"disas Test_cmdline_complete_various", @:)
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +0200868
Bram Moolenaar47016f52021-08-26 16:39:58 +0200869 call feedkeys(":disas s:WeirdF\<C-A>\<C-B>\"\<CR>", 'xt')
naohiro ono9aecf792021-08-28 15:56:06 +0200870 call assert_match('"disas <SNR>\d\+_WeirdFunc', @:)
Bram Moolenaar47016f52021-08-26 16:39:58 +0200871
naohiro onodfe04db2021-09-12 15:45:10 +0200872 call feedkeys(":disas \<S-Tab>\<C-B>\"\<CR>", 'xt')
873 call assert_match('"disas <SNR>\d\+_', @:)
874 call feedkeys(":disas debug \<S-Tab>\<C-B>\"\<CR>", 'xt')
875 call assert_match('"disas debug <SNR>\d\+_', @:)
876
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100877 " completion for the :match command
878 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
879 call assert_equal("\"match Search /pat/\<C-A>", @:)
880
881 " completion for the :s command
882 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
883 call assert_equal("\"s/from/to/g\<C-A>", @:)
884
885 " completion for the :dlist command
886 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
887 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
888
889 " completion for the :doautocmd command
890 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
891 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
892
Bram Moolenaarafe8cf62020-10-05 20:07:18 +0200893 " completion of autocmd group after comma
894 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
895 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
896
Dominique Pellebfb2bb12021-08-14 21:11:51 +0200897 " completion of file name in :doautocmd
898 call writefile([], 'Xfile1')
899 call writefile([], 'Xfile2')
900 call feedkeys(":doautocmd BufEnter Xfi\<C-A>\<C-B>\"\<CR>", 'xt')
901 call assert_equal("\"doautocmd BufEnter Xfile1 Xfile2", @:)
902 call delete('Xfile1')
903 call delete('Xfile2')
904
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100905 " completion for the :augroup command
Bram Moolenaarb4d82e22021-09-01 13:03:39 +0200906 augroup XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100907 augroup END
908 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
Bram Moolenaarb4d82e22021-09-01 13:03:39 +0200909 call assert_equal("\"augroup XTest.test", @:)
910 call feedkeys(":au X\<C-A>\<C-B>\"\<CR>", 'xt')
911 call assert_equal("\"au XTest.test", @:)
912 augroup! XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100913
914 " completion for the :unlet command
915 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
916 call assert_equal("\"unlet one two", @:)
917
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100918 " completion for the :buffer command with curlies
Bram Moolenaar39c47c32021-10-17 18:05:26 +0100919 " FIXME: what should happen on MS-Windows?
920 if !has('win32')
921 edit \{someFile}
922 call feedkeys(":buf someFile\<C-A>\<C-B>\"\<CR>", 'xt')
923 call assert_equal("\"buf {someFile}", @:)
924 bwipe {someFile}
925 endif
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100926
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100927 " completion for the :bdelete command
928 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
929 call assert_equal("\"bdel a b c", @:)
930
931 " completion for the :mapclear command
932 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
933 call assert_equal("\"mapclear <buffer>", @:)
934
935 " completion for user defined commands with menu names
936 menu Test.foo :ls<CR>
937 com -nargs=* -complete=menu MyCmd
938 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
939 call assert_equal('"MyCmd Test.', @:)
940 delcom MyCmd
941 unmenu Test
942
943 " completion for user defined commands with mappings
944 mapclear
945 map <F3> :ls<CR>
946 com -nargs=* -complete=mapping MyCmd
947 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
948 call assert_equal('"MyCmd <F3>', @:)
949 mapclear
950 delcom MyCmd
951
952 " completion for :set path= with multiple backslashes
953 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
954 call assert_equal('"set path=a\\\ b', @:)
955
956 " completion for :set dir= with a backslash
957 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
958 call assert_equal('"set dir=a\ b', @:)
959
960 " completion for the :py3 commands
961 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
962 call assert_equal('"py3 py3do py3file', @:)
963
Bram Moolenaardf749a22021-03-28 15:29:43 +0200964 " completion for the :vim9 commands
965 call feedkeys(":vim9\<C-A>\<C-B>\"\<CR>", 'xt')
966 call assert_equal('"vim9cmd vim9script', @:)
967
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100968 " redir @" is not the start of a comment. So complete after that
969 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
970 call assert_equal('"redir @" | cwindow', @:)
971
972 " completion after a backtick
973 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
974 call assert_equal('"e `a1b2c', @:)
975
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100976 " completion for :language command with an invalid argument
977 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
978 call assert_equal("\"language dummy \t", @:)
979
980 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100981 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
982 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100983
984 " completion with ambiguous user defined commands
985 com TCmd1 echo 'TCmd1'
986 com TCmd2 echo 'TCmd2'
987 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
988 call assert_equal('"TCmd ', @:)
989 delcom TCmd1
990 delcom TCmd2
991
992 " completion after a range followed by a pipe (|) character
993 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
994 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +0200995
996 " use <Esc> as the 'wildchar' for completion
997 set wildchar=<Esc>
998 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
999 call assert_equal('"g/a\xb/clearjumps', @:)
1000 " pressing <esc> twice should cancel the command
1001 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
1002 call assert_equal('"g/a\xb/clearjumps', @:)
1003 set wildchar&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001004endfunc
1005
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001006func Test_cmdline_write_alternatefile()
1007 new
1008 call setline('.', ['one', 'two'])
1009 f foo.txt
1010 new
1011 f #-A
1012 call assert_equal('foo.txt-A', expand('%'))
1013 f #<-B.txt
1014 call assert_equal('foo-B.txt', expand('%'))
1015 f %<
1016 call assert_equal('foo-B', expand('%'))
1017 new
Bram Moolenaare2e40752020-09-04 21:18:46 +02001018 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001019 bw!
1020 f foo-B.txt
1021 f %<-A
1022 call assert_equal('foo-B-A', expand('%'))
1023 bw!
1024 bw!
1025endfunc
1026
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001027" using a leading backslash here
1028set cpo+=C
1029
1030func Test_cmdline_search_range()
1031 new
1032 call setline(1, ['a', 'b', 'c', 'd'])
1033 /d
1034 1,\/s/b/B/
1035 call assert_equal('B', getline(2))
1036
1037 /a
1038 $
1039 \?,4s/c/C/
1040 call assert_equal('C', getline(3))
1041
1042 call setline(1, ['a', 'b', 'c', 'd'])
1043 %s/c/c/
1044 1,\&s/b/B/
1045 call assert_equal('B', getline(2))
1046
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001047 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001048 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001049
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001050 bwipe!
1051endfunc
1052
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001053" Test for the tick mark (') in an excmd range
1054func Test_tick_mark_in_range()
1055 " If only the tick is passed as a range and no command is specified, there
1056 " should not be an error
1057 call feedkeys(":'\<CR>", 'xt')
1058 call assert_equal("'", getreg(':'))
1059 call assert_fails("',print", 'E78:')
1060endfunc
1061
1062" Test for using a line number followed by a search pattern as range
1063func Test_lnum_and_pattern_as_range()
1064 new
1065 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
1066 let @" = ''
1067 2/foo/yank
1068 call assert_equal("foo 3\n", @")
1069 call assert_equal(1, line('.'))
1070 close!
1071endfunc
1072
Bram Moolenaar65189a12017-02-06 22:22:17 +01001073" Tests for getcmdline(), getcmdpos() and getcmdtype()
1074func Check_cmdline(cmdtype)
1075 call assert_equal('MyCmd a', getcmdline())
1076 call assert_equal(8, getcmdpos())
1077 call assert_equal(a:cmdtype, getcmdtype())
1078 return ''
1079endfunc
1080
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001081set cpo&
1082
Bram Moolenaar65189a12017-02-06 22:22:17 +01001083func Test_getcmdtype()
1084 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
1085
1086 let cmdtype = ''
1087 debuggreedy
1088 call feedkeys(":debug echo 'test'\<CR>", "t")
1089 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
1090 call feedkeys("cont\<CR>", "xt")
1091 0debuggreedy
1092 call assert_equal('>', cmdtype)
1093
1094 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
1095 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
1096
1097 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +01001098 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +01001099
1100 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
1101
1102 cnoremap <expr> <F6> Check_cmdline('=')
1103 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
1104 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001105
1106 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +01001107endfunc
1108
Bram Moolenaar81612b72018-06-23 14:55:03 +02001109func Test_getcmdwintype()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001110 CheckFeature cmdwin
1111
Bram Moolenaar81612b72018-06-23 14:55:03 +02001112 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1113 call assert_equal('/', a)
1114
1115 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1116 call assert_equal('?', a)
1117
1118 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1119 call assert_equal(':', a)
1120
1121 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1122 call assert_equal(':', a)
1123
1124 call assert_equal('', getcmdwintype())
1125endfunc
1126
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001127func Test_getcmdwin_autocmd()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001128 CheckFeature cmdwin
1129
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001130 let s:seq = []
1131 augroup CmdWin
1132 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
1133 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
1134 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
1135 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
1136 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
1137 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
1138
1139 let org_winid = win_getid()
1140 let org_bufnr = bufnr()
1141 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
1142 call assert_equal(':', a)
1143 call assert_equal([
1144 \ 'WinLeave ' .. org_winid,
1145 \ 'WinEnter ' .. s:cmd_winid,
1146 \ 'BufLeave ' .. org_bufnr,
1147 \ 'BufEnter ' .. s:cmd_bufnr,
1148 \ 'CmdWinEnter ' .. s:cmd_winid,
1149 \ 'CmdWinLeave ' .. s:cmd_winid,
1150 \ 'BufLeave ' .. s:cmd_bufnr,
1151 \ 'WinLeave ' .. s:cmd_winid,
1152 \ 'WinEnter ' .. org_winid,
1153 \ 'BufEnter ' .. org_bufnr,
1154 \ ], s:seq)
1155
1156 au!
1157 augroup END
1158endfunc
1159
Bram Moolenaar52604f22017-04-07 16:17:39 +02001160func Test_verbosefile()
1161 set verbosefile=Xlog
1162 echomsg 'foo'
1163 echomsg 'bar'
1164 set verbosefile=
1165 let log = readfile('Xlog')
1166 call assert_match("foo\nbar", join(log, "\n"))
1167 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001168 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001169 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001170 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +02001171endfunc
1172
Bram Moolenaar4facea32019-10-12 20:17:40 +02001173func Test_verbose_option()
1174 CheckScreendump
1175
1176 let lines =<< trim [SCRIPT]
1177 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1178 call feedkeys("\r", 't') " for the hit-enter prompt
1179 set verbose=20
1180 [SCRIPT]
1181 call writefile(lines, 'XTest_verbose')
1182
1183 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001184 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001185 call term_sendkeys(buf, ":DoSomething\<CR>")
1186 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1187
1188 " clean up
1189 call StopVimInTerminal(buf)
1190 call delete('XTest_verbose')
1191endfunc
1192
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001193func Test_setcmdpos()
1194 func InsertTextAtPos(text, pos)
1195 call assert_equal(0, setcmdpos(a:pos))
1196 return a:text
1197 endfunc
1198
1199 " setcmdpos() with position in the middle of the command line.
1200 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1201 call assert_equal('"1ab2', @:)
1202
1203 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1204 call assert_equal('"1b2a', @:)
1205
1206 " setcmdpos() with position beyond the end of the command line.
1207 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1208 call assert_equal('"12ab', @:)
1209
1210 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001211 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001212endfunc
1213
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001214func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001215 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001216 let encoding_save = &encoding
1217
1218 for e in encodings
1219 exe 'set encoding=' . e
1220
1221 " Test overstrike in the middle of the command line.
1222 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001223 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001224
1225 " Test overstrike going beyond end of command line.
1226 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001227 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001228
1229 " Test toggling insert/overstrike a few times.
1230 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001231 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001232 endfor
1233
Bram Moolenaar30276f22019-01-24 17:59:39 +01001234 " Test overstrike with multi-byte characters.
1235 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001236 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001237
1238 let &encoding = encoding_save
1239endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001240
1241func Test_cmdwin_bug()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001242 CheckFeature cmdwin
1243
Bram Moolenaara046b372019-09-15 17:26:07 +02001244 let winid = win_getid()
1245 sp
1246 try
1247 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1248 catch /^Vim\%((\a\+)\)\=:E11/
1249 endtry
1250 bw!
1251endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001252
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001253func Test_cmdwin_restore()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001254 CheckFeature cmdwin
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001255 CheckScreendump
1256
1257 let lines =<< trim [SCRIPT]
Egor Zvorykin125ffd22021-11-17 14:01:14 +00001258 augroup vimHints | au! | augroup END
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001259 call setline(1, range(30))
1260 2split
1261 [SCRIPT]
1262 call writefile(lines, 'XTest_restore')
1263
1264 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001265 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001266 call term_sendkeys(buf, "q:")
1267 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1268
1269 " normal restore
1270 call term_sendkeys(buf, ":q\<CR>")
1271 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1272
1273 " restore after setting 'lines' with one window
1274 call term_sendkeys(buf, ":close\<CR>")
1275 call term_sendkeys(buf, "q:")
1276 call term_sendkeys(buf, ":set lines=18\<CR>")
1277 call term_sendkeys(buf, ":q\<CR>")
1278 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1279
1280 " clean up
1281 call StopVimInTerminal(buf)
1282 call delete('XTest_restore')
1283endfunc
1284
Bram Moolenaare5b44862021-05-30 13:54:03 +02001285func Test_cmdwin_no_terminal()
1286 CheckFeature cmdwin
1287 CheckFeature terminal
Bram Moolenaar0b496482021-05-30 14:21:57 +02001288 CheckNotMSWindows
Bram Moolenaare5b44862021-05-30 13:54:03 +02001289
1290 let buf = RunVimInTerminal('', {'rows': 12})
1291 call TermWait(buf, 50)
1292 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1293 call term_sendkeys(buf, "q:")
1294 call term_sendkeys(buf, ":let buf = term_start(['/bin/echo'], #{hidden: 1})\<CR>")
1295 call VerifyScreenDump(buf, 'Test_cmdwin_no_terminal', {})
1296 call term_sendkeys(buf, ":q\<CR>")
1297 call StopVimInTerminal(buf)
1298endfunc
1299
Bram Moolenaar52410572019-10-27 05:12:45 +01001300func Test_buffers_lastused()
1301 " check that buffers are sorted by time when wildmode has lastused
1302 call test_settime(1550020000) " middle
1303 edit bufa
1304 enew
1305 call test_settime(1550030000) " newest
1306 edit bufb
1307 enew
1308 call test_settime(1550010000) " oldest
1309 edit bufc
1310 enew
1311 call test_settime(0)
1312 enew
1313
1314 call assert_equal(['bufa', 'bufb', 'bufc'],
1315 \ getcompletion('', 'buffer'))
1316
1317 let save_wildmode = &wildmode
1318 set wildmode=full:lastused
1319
1320 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1321 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1322 call assert_equal('b bufb', X)
1323 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1324 call assert_equal('b bufa', X)
1325 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1326 call assert_equal('b bufc', X)
1327 enew
1328
1329 edit other
1330 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1331 call assert_equal('b bufb', X)
1332 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1333 call assert_equal('b bufa', X)
1334 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1335 call assert_equal('b bufc', X)
1336 enew
1337
1338 let &wildmode = save_wildmode
1339
1340 bwipeout bufa
1341 bwipeout bufb
1342 bwipeout bufc
1343endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001344
1345func Test_cmdwin_feedkeys()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001346 CheckFeature cmdwin
1347
Bram Moolenaar85db5472019-12-04 15:11:08 +01001348 " This should not generate E488
1349 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001350 " Using feedkeys with q: only should automatically close the cmd window
1351 call feedkeys('q:', 'xt')
1352 call assert_equal(1, winnr('$'))
1353 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001354endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001355
1356" Tests for the issues fixed in 7.4.441.
1357" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1358func Test_cmdwin_cedit()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001359 CheckFeature cmdwin
1360
Bram Moolenaar309976e2019-12-05 18:16:33 +01001361 exe "set cedit=\<C-c>"
1362 normal! :
1363 call assert_equal(1, winnr('$'))
1364
1365 let g:cmd_wintype = ''
1366 func CmdWinType()
1367 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001368 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001369 return ''
1370 endfunc
1371
1372 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1373 echo input('')
1374 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001375 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001376
1377 set cedit&vim
1378 delfunc CmdWinType
1379endfunc
1380
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001381" Test for CmdwinEnter autocmd
1382func Test_cmdwin_autocmd()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001383 CheckFeature cmdwin
1384
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001385 augroup CmdWin
1386 au!
Bram Moolenaarb63f3ca2021-01-30 21:40:03 +01001387 autocmd BufLeave * if &buftype == '' | update | endif
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001388 autocmd CmdwinEnter * startinsert
1389 augroup END
1390
1391 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1392 call assert_equal('xyz', @:)
1393
1394 augroup CmdWin
1395 au!
1396 augroup END
1397 augroup! CmdWin
1398endfunc
1399
Bram Moolenaar479950f2020-01-19 15:45:17 +01001400func Test_cmdlineclear_tabenter()
1401 CheckScreendump
1402
1403 let lines =<< trim [SCRIPT]
1404 call setline(1, range(30))
1405 [SCRIPT]
1406
1407 call writefile(lines, 'XtestCmdlineClearTabenter')
1408 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001409 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001410 " in one tab make the command line higher with CTRL-W -
1411 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1412 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1413
1414 call StopVimInTerminal(buf)
1415 call delete('XtestCmdlineClearTabenter')
1416endfunc
1417
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001418" Test for expanding special keywords in cmdline
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001419func Test_cmdline_expand_special()
1420 %bwipe!
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02001421 call assert_fails('e #', 'E194:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001422 call assert_fails('e <afile>', 'E495:')
1423 call assert_fails('e <abuf>', 'E496:')
1424 call assert_fails('e <amatch>', 'E497:')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001425
1426 call writefile([], 'Xfile.cpp')
1427 call writefile([], 'Xfile.java')
1428 new Xfile.cpp
1429 call feedkeys(":e %:r\<C-A>\<C-B>\"\<CR>", 'xt')
1430 call assert_equal('"e Xfile.cpp Xfile.java', @:)
1431 close
1432 call delete('Xfile.cpp')
1433 call delete('Xfile.java')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001434endfunc
1435
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001436func Test_cmdwin_jump_to_win()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001437 CheckFeature cmdwin
1438
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001439 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1440 new
1441 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001442 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001443 close!
1444 call feedkeys("q/:close\<CR>", "xt")
1445 call assert_equal(1, winnr('$'))
1446 call feedkeys("q/:exit\<CR>", "xt")
1447 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001448
1449 " opening command window twice should fail
1450 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1451 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001452endfunc
1453
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001454func Test_cmdwin_interrupted()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001455 CheckFeature cmdwin
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001456 CheckScreendump
1457
1458 " aborting the :smile output caused the cmdline window to use the current
1459 " buffer.
1460 let lines =<< trim [SCRIPT]
1461 au WinNew * smile
1462 [SCRIPT]
1463 call writefile(lines, 'XTest_cmdwin')
1464
1465 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001466 " open cmdwin
1467 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001468 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001469 " quit more prompt for :smile command
1470 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001471 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001472 " execute a simple command
1473 call term_sendkeys(buf, "aecho 'done'\<CR>")
1474 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1475
1476 " clean up
1477 call StopVimInTerminal(buf)
1478 call delete('XTest_cmdwin')
1479endfunc
1480
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001481" Test for backtick expression in the command line
1482func Test_cmd_backtick()
1483 %argd
1484 argadd `=['a', 'b', 'c']`
1485 call assert_equal(['a', 'b', 'c'], argv())
1486 %argd
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001487
1488 argadd `echo abc def`
1489 call assert_equal(['abc def'], argv())
1490 %argd
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001491endfunc
1492
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001493" Test for the :! command
1494func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001495 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001496
1497 let lines =<< trim [SCRIPT]
1498 " Test for no previous command
1499 call assert_fails('!!', 'E34:')
1500 set nomore
1501 " Test for cmdline expansion with :!
1502 call setline(1, 'foo!')
1503 silent !echo <cWORD> > Xfile.out
1504 call assert_equal(['foo!'], readfile('Xfile.out'))
1505 " Test for using previous command
1506 silent !echo \! !
1507 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1508 call writefile(v:errors, 'Xresult')
1509 call delete('Xfile.out')
1510 qall!
1511 [SCRIPT]
1512 call writefile(lines, 'Xscript')
1513 if RunVim([], [], '--clean -S Xscript')
1514 call assert_equal([], readfile('Xresult'))
1515 endif
1516 call delete('Xscript')
1517 call delete('Xresult')
1518endfunc
1519
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001520" Test error: "E135: *Filter* Autocommands must not change current buffer"
1521func Test_cmd_bang_E135()
1522 new
1523 call setline(1, ['a', 'b', 'c', 'd'])
1524 augroup test_cmd_filter_E135
1525 au!
1526 autocmd FilterReadPost * help
1527 augroup END
1528 call assert_fails('2,3!echo "x"', 'E135:')
1529
1530 augroup test_cmd_filter_E135
1531 au!
1532 augroup END
1533 %bwipe!
1534endfunc
1535
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001536" Test for using ~ for home directory in cmdline completion matches
1537func Test_cmdline_expand_home()
1538 call mkdir('Xdir')
1539 call writefile([], 'Xdir/Xfile1')
1540 call writefile([], 'Xdir/Xfile2')
1541 cd Xdir
1542 let save_HOME = $HOME
1543 let $HOME = getcwd()
1544 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1545 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1546 let $HOME = save_HOME
1547 cd ..
1548 call delete('Xdir', 'rf')
1549endfunc
1550
Bram Moolenaar578fe942020-02-27 21:32:51 +01001551" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1552" or insert mode (when 'insertmode' is set)
1553func Test_cmdline_ctrl_g()
1554 new
1555 call setline(1, 'abc')
1556 call cursor(1, 3)
1557 " If command line is entered from insert mode, using C-\ C-G should back to
1558 " insert mode
1559 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1560 call assert_equal('abxyc', getline(1))
1561 call assert_equal(4, col('.'))
1562
1563 " If command line is entered in 'insertmode', using C-\ C-G should back to
1564 " 'insertmode'
1565 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1566 call assert_equal('ab12xyc', getline(1))
1567 close!
1568endfunc
1569
Bram Moolenaar578fe942020-02-27 21:32:51 +01001570" Test for 'wildmode'
1571func Test_wildmode()
1572 func T(a, c, p)
1573 return "oneA\noneB\noneC"
1574 endfunc
1575 command -nargs=1 -complete=custom,T MyCmd
1576
1577 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001578 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001579 return ''
1580 endfunc
1581 cnoremap <expr> <F2> SaveScreenLine()
1582
1583 set nowildmenu
1584 set wildmode=full,list
1585 let g:Sline = ''
1586 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001587 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001588 call assert_equal('"MyCmd oneA', @:)
1589
1590 set wildmode=longest,full
1591 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1592 call assert_equal('"MyCmd one', @:)
1593 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1594 call assert_equal('"MyCmd oneC', @:)
1595
1596 set wildmode=longest
1597 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1598 call assert_equal('"MyCmd one', @:)
1599
1600 set wildmode=list:longest
1601 let g:Sline = ''
1602 call feedkeys(":MyCmd \t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001603 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001604 call assert_equal('"MyCmd one', @:)
1605
1606 set wildmode=""
1607 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1608 call assert_equal('"MyCmd oneA', @:)
1609
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001610 " Test for wildmode=longest with 'fileignorecase' set
1611 set wildmode=longest
1612 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001613 argadd AAA AAAA AAAAA
1614 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1615 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001616 set fileignorecase&
1617
1618 " Test for listing files with wildmode=list
1619 set wildmode=list
1620 let g:Sline = ''
1621 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001622 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001623 call assert_equal('"b A', @:)
1624
1625 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001626 delcommand MyCmd
1627 delfunc T
1628 delfunc SaveScreenLine
1629 cunmap <F2>
1630 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001631 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001632endfunc
1633
1634" Test for interrupting the command-line completion
1635func Test_interrupt_compl()
1636 func F(lead, cmdl, p)
1637 if a:lead =~ 'tw'
1638 call interrupt()
1639 return
1640 endif
1641 return "one\ntwo\nthree"
1642 endfunc
1643 command -nargs=1 -complete=custom,F Tcmd
1644
1645 set nowildmenu
1646 set wildmode=full
1647 let interrupted = 0
1648 try
1649 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1650 catch /^Vim:Interrupt$/
1651 let interrupted = 1
1652 endtry
1653 call assert_equal(1, interrupted)
1654
1655 delcommand Tcmd
1656 delfunc F
1657 set wildmode&
1658endfunc
1659
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001660" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001661func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001662 let str = ":one two\<C-U>"
1663 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001664 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001665 let str ..= "\<Left>five\<Right>"
1666 let str ..= "\<Home>two "
1667 let str ..= "\<C-Left>one "
1668 let str ..= "\<C-Right> three"
1669 let str ..= "\<End>\<S-Left>four "
1670 let str ..= "\<S-Right> six"
1671 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1672 call feedkeys(str, 'xt')
1673 call assert_equal("\"one two three four five six seven", @:)
1674endfunc
1675
1676" Test for moving the cursor on the / command line in 'rightleft' mode
1677func Test_cmdline_edit_rightleft()
1678 CheckFeature rightleft
1679 set rightleft
1680 set rightleftcmd=search
1681 let str = "/one two\<C-U>"
1682 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001683 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001684 let str ..= "\<Right>five\<Left>"
1685 let str ..= "\<Home>two "
1686 let str ..= "\<C-Right>one "
1687 let str ..= "\<C-Left> three"
1688 let str ..= "\<End>\<S-Right>four "
1689 let str ..= "\<S-Left> six"
1690 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1691 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1692 call assert_equal("\"one two three four five six seven", @/)
1693 set rightleftcmd&
1694 set rightleft&
1695endfunc
1696
1697" Test for using <C-\>e in the command line to evaluate an expression
1698func Test_cmdline_expr()
1699 " Evaluate an expression from the beginning of a command line
1700 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1701 call assert_equal('"hello', @:)
1702
1703 " Use an invalid expression for <C-\>e
1704 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1705
1706 " Insert literal <CTRL-\> in the command line
1707 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1708 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001709endfunc
1710
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001711" Test for 'imcmdline' and 'imsearch'
1712" This test doesn't actually test the input method functionality.
1713func Test_cmdline_inputmethod()
1714 new
1715 call setline(1, ['', 'abc', ''])
1716 set imcmdline
1717
1718 call feedkeys(":\"abc\<CR>", 'xt')
1719 call assert_equal("\"abc", @:)
1720 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1721 call assert_equal("\"abc", @:)
1722 call feedkeys("/abc\<CR>", 'xt')
1723 call assert_equal([2, 1], [line('.'), col('.')])
1724 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1725 call assert_equal([2, 1], [line('.'), col('.')])
1726
1727 set imsearch=2
1728 call cursor(1, 1)
1729 call feedkeys("/abc\<CR>", 'xt')
1730 call assert_equal([2, 1], [line('.'), col('.')])
1731 call cursor(1, 1)
1732 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1733 call assert_equal([2, 1], [line('.'), col('.')])
1734 set imdisable
1735 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1736 call assert_equal([2, 1], [line('.'), col('.')])
1737 set imdisable&
1738 set imsearch&
1739
1740 set imcmdline&
1741 %bwipe!
1742endfunc
1743
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001744" Test for recursively getting multiple command line inputs
1745func Test_cmdwin_multi_input()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001746 CheckFeature cmdwin
1747
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001748 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1749 call assert_equal('"cyan', @:)
1750endfunc
1751
1752" Test for using CTRL-_ in the command line with 'allowrevins'
1753func Test_cmdline_revins()
1754 CheckNotMSWindows
1755 CheckFeature rightleft
1756 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1757 call assert_equal("\"abc\<c-_>", @:)
1758 set allowrevins
1759 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1760 call assert_equal('"abcñèæ', @:)
1761 set allowrevins&
1762endfunc
1763
1764" Test for typing UTF-8 composing characters in the command line
1765func Test_cmdline_composing_chars()
1766 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1767 call assert_equal('"ゔ', @:)
1768endfunc
1769
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001770" Test for normal mode commands not supported in the cmd window
1771func Test_cmdwin_blocked_commands()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001772 CheckFeature cmdwin
1773
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001774 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1775 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1776 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1777 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1778 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1779 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001780 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1781 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1782 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1783 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1784 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1785 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1786 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1787 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1788 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1789 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1790 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1791 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1792 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1793 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1794 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1795 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1796 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1797 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1798 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1799 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1800 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001801endfunc
1802
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001803" Close the Cmd-line window in insert mode using CTRL-C
1804func Test_cmdwin_insert_mode_close()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001805 CheckFeature cmdwin
1806
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001807 %bw!
1808 let s = ''
1809 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1810 call assert_equal('Hello', s)
1811 call assert_equal(1, winnr('$'))
1812endfunc
1813
Bram Moolenaar0e717042020-04-27 19:29:01 +02001814" test that ";" works to find a match at the start of the first line
1815func Test_zero_line_search()
1816 new
1817 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1818 call cursor(1,1)
1819 0;/pattern/d
1820 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1821 q!
1822endfunc
1823
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001824func Test_read_shellcmd()
1825 CheckUnix
1826 if executable('ls')
1827 " There should be ls in the $PATH
1828 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1829 call assert_match('^"r! .*\<ls\>', @:)
1830 endif
1831
1832 if executable('rm')
1833 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1834 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1835 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001836
1837 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1838 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1839 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001840 endif
1841endfunc
1842
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01001843" Test for going up and down the directory tree using 'wildmenu'
1844func Test_wildmenu_dirstack()
1845 CheckUnix
1846 %bw!
1847 call mkdir('Xdir1/dir2/dir3', 'p')
1848 call writefile([], 'Xdir1/file1_1.txt')
1849 call writefile([], 'Xdir1/file1_2.txt')
1850 call writefile([], 'Xdir1/dir2/file2_1.txt')
1851 call writefile([], 'Xdir1/dir2/file2_2.txt')
1852 call writefile([], 'Xdir1/dir2/dir3/file3_1.txt')
1853 call writefile([], 'Xdir1/dir2/dir3/file3_2.txt')
1854 cd Xdir1/dir2/dir3
1855 set wildmenu
1856
1857 call feedkeys(":e \<Tab>\<C-B>\"\<CR>", 'xt')
1858 call assert_equal('"e file3_1.txt', @:)
1859 call feedkeys(":e \<Tab>\<Up>\<C-B>\"\<CR>", 'xt')
1860 call assert_equal('"e ../dir3/', @:)
1861 call feedkeys(":e \<Tab>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
1862 call assert_equal('"e ../../dir2/', @:)
1863 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<C-B>\"\<CR>", 'xt')
1864 call assert_equal('"e ../../dir2/dir3/', @:)
1865 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
1866 call assert_equal('"e ../../dir2/dir3/file3_1.txt', @:)
1867
1868 cd -
1869 call delete('Xdir1', 'rf')
1870 set wildmenu&
1871endfunc
1872
obcat71c6f7a2021-05-13 20:23:10 +02001873" Test for recalling newer or older cmdline from history with <Up>, <Down>,
1874" <S-Up>, <S-Down>, <PageUp>, <PageDown>, <C-p>, or <C-n>.
1875func Test_recalling_cmdline()
1876 CheckFeature cmdline_hist
1877
1878 let g:cmdlines = []
1879 cnoremap <Plug>(save-cmdline) <Cmd>let g:cmdlines += [getcmdline()]<CR>
1880
1881 let histories = [
1882 \ {'name': 'cmd', 'enter': ':', 'exit': "\<Esc>"},
1883 \ {'name': 'search', 'enter': '/', 'exit': "\<Esc>"},
1884 \ {'name': 'expr', 'enter': ":\<C-r>=", 'exit': "\<Esc>\<Esc>"},
1885 \ {'name': 'input', 'enter': ":call input('')\<CR>", 'exit': "\<CR>"},
1886 "\ TODO: {'name': 'debug', ...}
1887 \]
1888 let keypairs = [
1889 \ {'older': "\<Up>", 'newer': "\<Down>", 'prefixmatch': v:true},
1890 \ {'older': "\<S-Up>", 'newer': "\<S-Down>", 'prefixmatch': v:false},
1891 \ {'older': "\<PageUp>", 'newer': "\<PageDown>", 'prefixmatch': v:false},
1892 \ {'older': "\<C-p>", 'newer': "\<C-n>", 'prefixmatch': v:false},
1893 \]
1894 let prefix = 'vi'
1895 for h in histories
1896 call histadd(h.name, 'vim')
1897 call histadd(h.name, 'virtue')
1898 call histadd(h.name, 'Virgo')
1899 call histadd(h.name, 'vogue')
1900 call histadd(h.name, 'emacs')
1901 for k in keypairs
1902 let g:cmdlines = []
1903 let keyseqs = h.enter
1904 \ .. prefix
1905 \ .. repeat(k.older .. "\<Plug>(save-cmdline)", 2)
1906 \ .. repeat(k.newer .. "\<Plug>(save-cmdline)", 2)
1907 \ .. h.exit
1908 call feedkeys(keyseqs, 'xt')
1909 call histdel(h.name, -1) " delete the history added by feedkeys above
1910 let expect = k.prefixmatch
1911 \ ? ['virtue', 'vim', 'virtue', prefix]
1912 \ : ['emacs', 'vogue', 'emacs', prefix]
1913 call assert_equal(expect, g:cmdlines)
1914 endfor
1915 endfor
1916
1917 unlet g:cmdlines
1918 cunmap <Plug>(save-cmdline)
1919endfunc
1920
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001921func Test_cmd_map_cmdlineChanged()
1922 let g:log = []
1923 cnoremap <F1> l<Cmd><CR>s
1924 augroup test
1925 autocmd!
1926 autocmd CmdlineChanged : let g:log += [getcmdline()]
1927 augroup END
1928
1929 call feedkeys(":\<F1>\<CR>", 'xt')
1930 call assert_equal(['l', 'ls'], g:log)
1931
Bram Moolenaar796139a2021-05-18 21:38:45 +02001932 let @b = 'b'
1933 cnoremap <F1> a<C-R>b
1934 let g:log = []
1935 call feedkeys(":\<F1>\<CR>", 'xt')
1936 call assert_equal(['a', 'ab'], g:log)
1937
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001938 unlet g:log
1939 cunmap <F1>
1940 augroup test
1941 autocmd!
1942 augroup END
1943endfunc
1944
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001945" Test for the 'suffixes' option
1946func Test_suffixes_opt()
1947 call writefile([], 'Xfile')
1948 call writefile([], 'Xfile.c')
1949 call writefile([], 'Xfile.o')
1950 set suffixes=
1951 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
1952 call assert_equal('"e Xfile Xfile.c Xfile.o', @:)
1953 set suffixes=.c
1954 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
1955 call assert_equal('"e Xfile Xfile.o Xfile.c', @:)
1956 set suffixes=,,
1957 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
1958 call assert_equal('"e Xfile.c Xfile.o Xfile', @:)
1959 set suffixes&
1960 call delete('Xfile')
1961 call delete('Xfile.c')
1962 call delete('Xfile.o')
1963endfunc
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001964
Bram Moolenaar309976e2019-12-05 18:16:33 +01001965" vim: shiftwidth=2 sts=2 expandtab