blob: 9c21a205f78fbd291a81eedacb5b6de97d44a23e [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
308 let l = getcompletion('exe', 'expression')
309 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200310 let l = getcompletion('kill', 'expression')
311 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200312
313 let l = getcompletion('tag', 'function')
314 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200315 let l = getcompletion('paint', 'function')
316 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200317
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200318 let Flambda = {-> 'hello'}
319 let l = getcompletion('', 'function')
320 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200321 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200322
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200323 let l = getcompletion('run', 'file')
324 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200325 let l = getcompletion('walk', 'file')
326 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200327 set wildignore=*.vim
328 let l = getcompletion('run', 'file', 1)
329 call assert_true(index(l, 'runtest.vim') < 0)
330 set wildignore&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200331
332 let l = getcompletion('ha', 'filetype')
333 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200334 let l = getcompletion('horse', 'filetype')
335 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200336
337 let l = getcompletion('z', 'syntax')
338 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200339 let l = getcompletion('emacs', 'syntax')
340 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200341
342 let l = getcompletion('jikes', 'compiler')
343 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200344 let l = getcompletion('break', 'compiler')
345 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200346
347 let l = getcompletion('last', 'help')
348 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200349 let l = getcompletion('giveup', 'help')
350 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200351
352 let l = getcompletion('time', 'option')
353 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200354 let l = getcompletion('space', 'option')
355 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200356
357 let l = getcompletion('er', 'highlight')
358 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200359 let l = getcompletion('dark', 'highlight')
360 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200361
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200362 let l = getcompletion('', 'messages')
363 call assert_true(index(l, 'clear') >= 0)
364 let l = getcompletion('not', 'messages')
365 call assert_equal([], l)
366
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200367 let l = getcompletion('', 'mapclear')
368 call assert_true(index(l, '<buffer>') >= 0)
369 let l = getcompletion('not', 'mapclear')
370 call assert_equal([], l)
371
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200372 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200373 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200374 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
375 let root = has('win32') ? 'C:\\' : '/'
376 let l = getcompletion(root, 'shellcmd')
377 let expected = map(filter(glob(root . '*', 0, 1),
378 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
379 call assert_equal(expected, l)
380
Bram Moolenaarb650b982016-08-05 20:35:13 +0200381 if has('cscope')
382 let l = getcompletion('', 'cscope')
383 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
384 call assert_equal(cmds, l)
385 " using cmdline completion must not change the result
386 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
387 let l = getcompletion('', 'cscope')
388 call assert_equal(cmds, l)
389 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
390 let l = getcompletion('find ', 'cscope')
391 call assert_equal(keys, l)
392 endif
393
Bram Moolenaar7522f692016-08-06 14:12:50 +0200394 if has('signs')
395 sign define Testing linehl=Comment
396 let l = getcompletion('', 'sign')
397 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
398 call assert_equal(cmds, l)
399 " using cmdline completion must not change the result
400 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
401 let l = getcompletion('', 'sign')
402 call assert_equal(cmds, l)
403 let l = getcompletion('list ', 'sign')
404 call assert_equal(['Testing'], l)
405 endif
406
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200407 " Command line completion tests
408 let l = getcompletion('cd ', 'cmdline')
409 call assert_true(index(l, 'samples/') >= 0)
410 let l = getcompletion('cd NoMatch', 'cmdline')
411 call assert_equal([], l)
412 let l = getcompletion('let v:n', 'cmdline')
413 call assert_true(index(l, 'v:null') >= 0)
414 let l = getcompletion('let v:notexists', 'cmdline')
415 call assert_equal([], l)
416 let l = getcompletion('call tag', 'cmdline')
417 call assert_true(index(l, 'taglist(') >= 0)
418 let l = getcompletion('call paint', 'cmdline')
419 call assert_equal([], l)
420
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200421 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200422 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200423 if has('cmdline_hist')
424 call add(names, 'history')
425 endif
426 if has('gettext')
427 call add(names, 'locale')
428 endif
429 if has('profile')
430 call add(names, 'syntime')
431 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200432
433 set tags=Xtags
434 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
435
436 for name in names
437 let matchcount = len(getcompletion('', name))
438 call assert_true(matchcount >= 0, 'No matches for ' . name)
439 endfor
440
441 call delete('Xtags')
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200442 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200443
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200444 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200445 call assert_fails('call getcompletion("", "burp")', 'E475:')
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200446 call assert_fails('call getcompletion("abc", [])', 'E475:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200447endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200448
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100449func Test_fullcommand()
450 let tests = {
451 \ '': '',
452 \ ':': '',
453 \ ':::': '',
454 \ ':::5': '',
455 \ 'not_a_cmd': '',
456 \ 'Check': '',
457 \ 'syntax': 'syntax',
458 \ ':syntax': 'syntax',
459 \ '::::syntax': 'syntax',
460 \ 'sy': 'syntax',
461 \ 'syn': 'syntax',
462 \ 'synt': 'syntax',
463 \ ':sy': 'syntax',
464 \ '::::sy': 'syntax',
465 \ 'match': 'match',
466 \ '2match': 'match',
467 \ '3match': 'match',
468 \ 'aboveleft': 'aboveleft',
469 \ 'abo': 'aboveleft',
470 \ 's': 'substitute',
471 \ '5s': 'substitute',
472 \ ':5s': 'substitute',
473 \ "'<,'>s": 'substitute',
474 \ ":'<,'>s": 'substitute',
475 \ 'CheckUni': 'CheckUnix',
476 \ 'CheckUnix': 'CheckUnix',
477 \ }
478
479 for [in, want] in items(tests)
480 call assert_equal(want, fullcommand(in))
481 endfor
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +0200482 call assert_equal('', fullcommand(test_null_string()))
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100483
484 call assert_equal('syntax', 'syn'->fullcommand())
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200485
486 command -buffer BufferLocalCommand :
487 command GlobalCommand :
488 call assert_equal('GlobalCommand', fullcommand('GlobalCom'))
489 call assert_equal('BufferLocalCommand', fullcommand('BufferL'))
490 delcommand BufferLocalCommand
491 delcommand GlobalCommand
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100492endfunc
493
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200494func Test_shellcmd_completion()
495 let save_path = $PATH
496
497 call mkdir('Xpathdir/Xpathsubdir', 'p')
498 call writefile([''], 'Xpathdir/Xfile.exe')
499 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
500
501 " Set PATH to example directory without trailing slash.
502 let $PATH = getcwd() . '/Xpathdir'
503
504 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
505 " dirs in the PATH, even though they won't be executed. We check that only
506 " subdirs of the PWD and executables from the PATH are included in the
507 " suggestions.
508 let actual = getcompletion('X', 'shellcmd')
509 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
510 call insert(expected, 'Xfile.exe')
511 call assert_equal(expected, actual)
512
513 call delete('Xpathdir', 'rf')
514 let $PATH = save_path
515endfunc
516
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200517func Test_expand_star_star()
518 call mkdir('a/b', 'p')
519 call writefile(['asdfasdf'], 'a/b/fileXname')
520 call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
521 call assert_equal('find a/b/fileXname', getreg(':'))
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200522 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200523 call delete('a', 'rf')
524endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100525
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100526func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100527 let @a = "def"
528 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
529 call assert_equal('"abc def ghi', @:)
530
531 new
532 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
533
534 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
535 call assert_equal('"aaa asdf bbb', @:)
536
537 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
538 call assert_equal('"aaa /tmp/some bbb', @:)
539
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200540 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
541 call assert_equal('"aaa '.getline(1).' bbb', @:)
542
Bram Moolenaar21efc362016-12-03 14:05:49 +0100543 set incsearch
544 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
545 call assert_equal('"aaa verylongword bbb', @:)
546
547 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
548 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100549
550 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
551 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100552 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200553
554 " Error while typing a command used to cause that it was not executed
555 " in the end.
556 new
557 try
558 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
559 catch /^Vim\%((\a\+)\)\=:E32/
560 " ignore error E32
561 endtry
562 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100563
Bram Moolenaar578fe942020-02-27 21:32:51 +0100564 " Try to paste an invalid register using <C-R>
565 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
566 call assert_equal('"onetwo', @:)
567
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100568 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100569 let @a = "xy\<C-H>z"
570 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
571 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100572 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
573 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100574 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
575 call assert_equal("\"xy\<C-H>z", @:)
576
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100577 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
578 let @a = "xy\<C-V>z"
579 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
580 call assert_equal('"xyz', @:)
581 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
582 call assert_equal("\"xy\<C-V>z", @:)
583
Bram Moolenaar578fe942020-02-27 21:32:51 +0100584 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
585
Bram Moolenaar72532d32018-04-07 19:09:09 +0200586 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100587endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100588
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100589func Test_cmdline_remove_char()
590 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100591
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100592 for e in ['utf8', 'latin1']
593 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100594
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100595 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
596 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100597
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100598 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
599 call assert_equal('"abcdef', @:)
600
601 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
602 call assert_equal('"abc ghi', @:, e)
603
604 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
605 call assert_equal('"def', @:, e)
606 endfor
607
608 let &encoding = encoding_save
609endfunc
610
611func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200612 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100613
614 set keymap=esperanto
615 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
616 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
617 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100618endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100619
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100620func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100621 new
622 2;'(
623 2;')
624 quit
625endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100626
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100627func Test_illegal_address2()
628 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
629 new
630 source Xtest.vim
631 " Trigger calling validate_cursor()
632 diffsp Xtest.vim
633 quit!
634 bwipe!
635 call delete('Xtest.vim')
636endfunc
637
Bram Moolenaarba47b512017-01-24 21:18:19 +0100638func Test_cmdline_complete_wildoptions()
639 help
640 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
641 let a = join(sort(split(@:)),' ')
642 set wildoptions=tagfile
643 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
644 let b = join(sort(split(@:)),' ')
645 call assert_equal(a, b)
646 bw!
647endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100648
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200649func Test_cmdline_complete_user_cmd()
650 command! -complete=color -nargs=1 Foo :
651 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
652 call assert_equal('"Foo blue', @:)
653 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
654 call assert_equal('"Foo blue', @:)
655 delcommand Foo
656endfunc
657
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100658func s:ScriptLocalFunction()
659 echo 'yes'
660endfunc
661
662func Test_cmdline_complete_user_func()
663 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
naohiro ono5aec7552021-08-19 21:20:41 +0200664 call assert_match('"func Test_cmdline_complete_user_', @:)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100665 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
666 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +0100667
668 " g: prefix also works
669 call feedkeys(":echo g:Test_cmdline_complete_user_f\<Tab>\<Home>\"\<cr>", 'tx')
670 call assert_match('"echo g:Test_cmdline_complete_user_func', @:)
Bram Moolenaar069f9082021-08-13 17:48:25 +0200671
672 " using g: prefix does not result in just "g:" matches from a lambda
673 let Fx = { a -> a }
674 call feedkeys(":echo g:\<Tab>\<Home>\"\<cr>", 'tx')
675 call assert_match('"echo g:[A-Z]', @:)
naohiro ono5aec7552021-08-19 21:20:41 +0200676
677 " existence of script-local dict function does not break user function name
678 " completion
679 function s:a_dict_func() dict
680 endfunction
681 call feedkeys(":call Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
682 call assert_match('"call Test_cmdline_complete_user_', @:)
683 delfunction s:a_dict_func
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100684endfunc
685
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200686func Test_cmdline_complete_user_names()
687 if has('unix') && executable('whoami')
688 let whoami = systemlist('whoami')[0]
689 let first_letter = whoami[0]
690 if len(first_letter) > 0
691 " Trying completion of :e ~x where x is the first letter of
692 " the user name should complete to at least the user name.
693 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
694 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
695 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200696 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200697 " Just in case: check that the system has an Administrator account.
698 let names = system('net user')
699 if names =~ 'Administrator'
700 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100701 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200702 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +0100703 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200704 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200705 else
706 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200707 endif
708endfunc
709
Bram Moolenaar297610b2019-12-27 17:20:55 +0100710func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200711 CheckExecutable whoami
712 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
713 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100714endfunc
715
Bram Moolenaar8b633132020-03-20 18:20:51 +0100716func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200717 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
718 call assert_equal(lang, v:lc_time)
719
720 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
721 call assert_equal(lang, v:ctype)
722
723 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
724 call assert_equal(lang, v:collate)
725
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200726 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200727 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200728
729 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200730 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200731
Bram Moolenaarec680282020-06-12 19:35:32 +0200732 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200733
Bram Moolenaarec680282020-06-12 19:35:32 +0200734 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
735 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200736
Bram Moolenaarec680282020-06-12 19:35:32 +0200737 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
738 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200739
Bram Moolenaarec680282020-06-12 19:35:32 +0200740 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
741 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200742
743 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
744 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +0200745endfunc
746
Bram Moolenaar297610b2019-12-27 17:20:55 +0100747func Test_cmdline_complete_env_variable()
748 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +0100749 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
750 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +0100751 unlet $X_VIM_TEST_COMPLETE_ENV
752endfunc
753
Bram Moolenaar4941b5e2020-12-24 17:15:53 +0100754func Test_cmdline_complete_expression()
755 let g:SomeVar = 'blah'
756 for cmd in ['exe', 'echo', 'echon', 'echomsg']
757 call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
758 call assert_match('"' .. cmd .. ' SomeVar', @:)
759 call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
760 call assert_match('"' .. cmd .. ' foo SomeVar', @:)
761 endfor
762 unlet g:SomeVar
763endfunc
764
Bram Moolenaar47016f52021-08-26 16:39:58 +0200765" Unique function name for completion below
766func s:WeirdFunc()
767 echo 'weird'
768endfunc
769
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100770" Test for various command-line completion
771func Test_cmdline_complete_various()
772 " completion for a command starting with a comment
773 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
774 call assert_equal("\" :|\"\<C-A>", @:)
775
776 " completion for a range followed by a comment
777 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
778 call assert_equal("\"1,2\"\<C-A>", @:)
779
780 " completion for :k command
781 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
782 call assert_equal("\"ka\<C-A>", @:)
783
784 " completion for short version of the :s command
785 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
786 call assert_equal("\"sI \<C-A>", @:)
787
788 " completion for :write command
789 call mkdir('Xdir')
790 call writefile(['one'], 'Xdir/Xfile1')
791 let save_cwd = getcwd()
792 cd Xdir
793 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
794 call assert_equal("\"w >> Xfile1", @:)
795 call chdir(save_cwd)
796 call delete('Xdir', 'rf')
797
798 " completion for :w ! and :r ! commands
799 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
800 call assert_equal("\"w !invalid_xyz_cmd", @:)
801 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
802 call assert_equal("\"r !invalid_xyz_cmd", @:)
803
804 " completion for :>> and :<< commands
805 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
806 call assert_equal("\">>>\<C-A>", @:)
807 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
808 call assert_equal("\"<<<\<C-A>", @:)
809
810 " completion for command with +cmd argument
811 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
812 call assert_equal("\"buffer +/pat Xabc", @:)
813 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
814 call assert_equal("\"buffer +/pat\<C-A>", @:)
815
816 " completion for a command with a trailing comment
817 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
818 call assert_equal("\"ls \" comment\<C-A>", @:)
819
820 " completion for a command with a trailing command
821 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
822 call assert_equal("\"ls | ls", @:)
823
824 " completion for a command with an CTRL-V escaped argument
825 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
826 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
827
828 " completion for a command that doesn't take additional arguments
829 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
830 call assert_equal("\"all abc\<C-A>", @:)
831
832 " completion for a command with a command modifier
833 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
834 call assert_equal("\"topleft new", @:)
835
Bram Moolenaare70e12b2021-06-13 17:20:08 +0200836 " completion for vim9 and legacy commands
837 call feedkeys(":vim9 call strle\<C-A>\<C-B>\"\<CR>", 'xt')
838 call assert_equal("\"vim9 call strlen(", @:)
839 call feedkeys(":legac call strle\<C-A>\<C-B>\"\<CR>", 'xt')
840 call assert_equal("\"legac call strlen(", @:)
841
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +0200842 " completion for the :disassemble command
843 call feedkeys(":disas deb\<C-A>\<C-B>\"\<CR>", 'xt')
844 call assert_equal("\"disas debug", @:)
845 call feedkeys(":disas pro\<C-A>\<C-B>\"\<CR>", 'xt')
846 call assert_equal("\"disas profile", @:)
847 call feedkeys(":disas debug Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
848 call assert_equal("\"disas debug Test_cmdline_complete_various", @:)
849 call feedkeys(":disas profile Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
850 call assert_equal("\"disas profile Test_cmdline_complete_various", @:)
naohiro ono9aecf792021-08-28 15:56:06 +0200851 call feedkeys(":disas Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
852 call assert_equal("\"disas Test_cmdline_complete_various", @:)
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +0200853
Bram Moolenaar47016f52021-08-26 16:39:58 +0200854 call feedkeys(":disas s:WeirdF\<C-A>\<C-B>\"\<CR>", 'xt')
naohiro ono9aecf792021-08-28 15:56:06 +0200855 call assert_match('"disas <SNR>\d\+_WeirdFunc', @:)
Bram Moolenaar47016f52021-08-26 16:39:58 +0200856
naohiro onodfe04db2021-09-12 15:45:10 +0200857 call feedkeys(":disas \<S-Tab>\<C-B>\"\<CR>", 'xt')
858 call assert_match('"disas <SNR>\d\+_', @:)
859 call feedkeys(":disas debug \<S-Tab>\<C-B>\"\<CR>", 'xt')
860 call assert_match('"disas debug <SNR>\d\+_', @:)
861
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100862 " completion for the :match command
863 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
864 call assert_equal("\"match Search /pat/\<C-A>", @:)
865
866 " completion for the :s command
867 call feedkeys(":s/from/to/g\<C-A>\<C-B>\"\<CR>", 'xt')
868 call assert_equal("\"s/from/to/g\<C-A>", @:)
869
870 " completion for the :dlist command
871 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
872 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
873
874 " completion for the :doautocmd command
875 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
876 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
877
Bram Moolenaarafe8cf62020-10-05 20:07:18 +0200878 " completion of autocmd group after comma
879 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
880 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
881
Dominique Pellebfb2bb12021-08-14 21:11:51 +0200882 " completion of file name in :doautocmd
883 call writefile([], 'Xfile1')
884 call writefile([], 'Xfile2')
885 call feedkeys(":doautocmd BufEnter Xfi\<C-A>\<C-B>\"\<CR>", 'xt')
886 call assert_equal("\"doautocmd BufEnter Xfile1 Xfile2", @:)
887 call delete('Xfile1')
888 call delete('Xfile2')
889
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100890 " completion for the :augroup command
Bram Moolenaarb4d82e22021-09-01 13:03:39 +0200891 augroup XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100892 augroup END
893 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
Bram Moolenaarb4d82e22021-09-01 13:03:39 +0200894 call assert_equal("\"augroup XTest.test", @:)
895 call feedkeys(":au X\<C-A>\<C-B>\"\<CR>", 'xt')
896 call assert_equal("\"au XTest.test", @:)
897 augroup! XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100898
899 " completion for the :unlet command
900 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
901 call assert_equal("\"unlet one two", @:)
902
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100903 " completion for the :buffer command with curlies
Bram Moolenaar39c47c32021-10-17 18:05:26 +0100904 " FIXME: what should happen on MS-Windows?
905 if !has('win32')
906 edit \{someFile}
907 call feedkeys(":buf someFile\<C-A>\<C-B>\"\<CR>", 'xt')
908 call assert_equal("\"buf {someFile}", @:)
909 bwipe {someFile}
910 endif
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100911
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100912 " completion for the :bdelete command
913 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
914 call assert_equal("\"bdel a b c", @:)
915
916 " completion for the :mapclear command
917 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
918 call assert_equal("\"mapclear <buffer>", @:)
919
920 " completion for user defined commands with menu names
921 menu Test.foo :ls<CR>
922 com -nargs=* -complete=menu MyCmd
923 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
924 call assert_equal('"MyCmd Test.', @:)
925 delcom MyCmd
926 unmenu Test
927
928 " completion for user defined commands with mappings
929 mapclear
930 map <F3> :ls<CR>
931 com -nargs=* -complete=mapping MyCmd
932 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
933 call assert_equal('"MyCmd <F3>', @:)
934 mapclear
935 delcom MyCmd
936
937 " completion for :set path= with multiple backslashes
938 call feedkeys(":set path=a\\\\\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
939 call assert_equal('"set path=a\\\ b', @:)
940
941 " completion for :set dir= with a backslash
942 call feedkeys(":set dir=a\\ b\<C-A>\<C-B>\"\<CR>", 'xt')
943 call assert_equal('"set dir=a\ b', @:)
944
945 " completion for the :py3 commands
946 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
947 call assert_equal('"py3 py3do py3file', @:)
948
Bram Moolenaardf749a22021-03-28 15:29:43 +0200949 " completion for the :vim9 commands
950 call feedkeys(":vim9\<C-A>\<C-B>\"\<CR>", 'xt')
951 call assert_equal('"vim9cmd vim9script', @:)
952
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100953 " redir @" is not the start of a comment. So complete after that
954 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
955 call assert_equal('"redir @" | cwindow', @:)
956
957 " completion after a backtick
958 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
959 call assert_equal('"e `a1b2c', @:)
960
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100961 " completion for :language command with an invalid argument
962 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
963 call assert_equal("\"language dummy \t", @:)
964
965 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +0100966 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
967 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100968
969 " completion with ambiguous user defined commands
970 com TCmd1 echo 'TCmd1'
971 com TCmd2 echo 'TCmd2'
972 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
973 call assert_equal('"TCmd ', @:)
974 delcom TCmd1
975 delcom TCmd2
976
977 " completion after a range followed by a pipe (|) character
978 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
979 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +0200980
981 " use <Esc> as the 'wildchar' for completion
982 set wildchar=<Esc>
983 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
984 call assert_equal('"g/a\xb/clearjumps', @:)
985 " pressing <esc> twice should cancel the command
986 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
987 call assert_equal('"g/a\xb/clearjumps', @:)
988 set wildchar&
Bram Moolenaar24ebd832020-03-16 21:25:24 +0100989endfunc
990
Bram Moolenaarc312b8b2017-10-28 17:53:04 +0200991func Test_cmdline_write_alternatefile()
992 new
993 call setline('.', ['one', 'two'])
994 f foo.txt
995 new
996 f #-A
997 call assert_equal('foo.txt-A', expand('%'))
998 f #<-B.txt
999 call assert_equal('foo-B.txt', expand('%'))
1000 f %<
1001 call assert_equal('foo-B', expand('%'))
1002 new
Bram Moolenaare2e40752020-09-04 21:18:46 +02001003 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001004 bw!
1005 f foo-B.txt
1006 f %<-A
1007 call assert_equal('foo-B-A', expand('%'))
1008 bw!
1009 bw!
1010endfunc
1011
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001012" using a leading backslash here
1013set cpo+=C
1014
1015func Test_cmdline_search_range()
1016 new
1017 call setline(1, ['a', 'b', 'c', 'd'])
1018 /d
1019 1,\/s/b/B/
1020 call assert_equal('B', getline(2))
1021
1022 /a
1023 $
1024 \?,4s/c/C/
1025 call assert_equal('C', getline(3))
1026
1027 call setline(1, ['a', 'b', 'c', 'd'])
1028 %s/c/c/
1029 1,\&s/b/B/
1030 call assert_equal('B', getline(2))
1031
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001032 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001033 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001034
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001035 bwipe!
1036endfunc
1037
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001038" Test for the tick mark (') in an excmd range
1039func Test_tick_mark_in_range()
1040 " If only the tick is passed as a range and no command is specified, there
1041 " should not be an error
1042 call feedkeys(":'\<CR>", 'xt')
1043 call assert_equal("'", getreg(':'))
1044 call assert_fails("',print", 'E78:')
1045endfunc
1046
1047" Test for using a line number followed by a search pattern as range
1048func Test_lnum_and_pattern_as_range()
1049 new
1050 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
1051 let @" = ''
1052 2/foo/yank
1053 call assert_equal("foo 3\n", @")
1054 call assert_equal(1, line('.'))
1055 close!
1056endfunc
1057
Bram Moolenaar65189a12017-02-06 22:22:17 +01001058" Tests for getcmdline(), getcmdpos() and getcmdtype()
1059func Check_cmdline(cmdtype)
1060 call assert_equal('MyCmd a', getcmdline())
1061 call assert_equal(8, getcmdpos())
1062 call assert_equal(a:cmdtype, getcmdtype())
1063 return ''
1064endfunc
1065
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001066set cpo&
1067
Bram Moolenaar65189a12017-02-06 22:22:17 +01001068func Test_getcmdtype()
1069 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
1070
1071 let cmdtype = ''
1072 debuggreedy
1073 call feedkeys(":debug echo 'test'\<CR>", "t")
1074 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
1075 call feedkeys("cont\<CR>", "xt")
1076 0debuggreedy
1077 call assert_equal('>', cmdtype)
1078
1079 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
1080 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
1081
1082 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +01001083 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +01001084
1085 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
1086
1087 cnoremap <expr> <F6> Check_cmdline('=')
1088 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
1089 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001090
1091 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +01001092endfunc
1093
Bram Moolenaar81612b72018-06-23 14:55:03 +02001094func Test_getcmdwintype()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001095 CheckFeature cmdwin
1096
Bram Moolenaar81612b72018-06-23 14:55:03 +02001097 call feedkeys("q/:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1098 call assert_equal('/', a)
1099
1100 call feedkeys("q?:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1101 call assert_equal('?', a)
1102
1103 call feedkeys("q::let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1104 call assert_equal(':', a)
1105
1106 call feedkeys(":\<C-F>:let a = getcmdwintype()\<CR>:q\<CR>", 'x!')
1107 call assert_equal(':', a)
1108
1109 call assert_equal('', getcmdwintype())
1110endfunc
1111
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001112func Test_getcmdwin_autocmd()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001113 CheckFeature cmdwin
1114
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001115 let s:seq = []
1116 augroup CmdWin
1117 au WinEnter * call add(s:seq, 'WinEnter ' .. win_getid())
1118 au WinLeave * call add(s:seq, 'WinLeave ' .. win_getid())
1119 au BufEnter * call add(s:seq, 'BufEnter ' .. bufnr())
1120 au BufLeave * call add(s:seq, 'BufLeave ' .. bufnr())
1121 au CmdWinEnter * call add(s:seq, 'CmdWinEnter ' .. win_getid())
1122 au CmdWinLeave * call add(s:seq, 'CmdWinLeave ' .. win_getid())
1123
1124 let org_winid = win_getid()
1125 let org_bufnr = bufnr()
1126 call feedkeys("q::let a = getcmdwintype()\<CR>:let s:cmd_winid = win_getid()\<CR>:let s:cmd_bufnr = bufnr()\<CR>:q\<CR>", 'x!')
1127 call assert_equal(':', a)
1128 call assert_equal([
1129 \ 'WinLeave ' .. org_winid,
1130 \ 'WinEnter ' .. s:cmd_winid,
1131 \ 'BufLeave ' .. org_bufnr,
1132 \ 'BufEnter ' .. s:cmd_bufnr,
1133 \ 'CmdWinEnter ' .. s:cmd_winid,
1134 \ 'CmdWinLeave ' .. s:cmd_winid,
1135 \ 'BufLeave ' .. s:cmd_bufnr,
1136 \ 'WinLeave ' .. s:cmd_winid,
1137 \ 'WinEnter ' .. org_winid,
1138 \ 'BufEnter ' .. org_bufnr,
1139 \ ], s:seq)
1140
1141 au!
1142 augroup END
1143endfunc
1144
Bram Moolenaar52604f22017-04-07 16:17:39 +02001145func Test_verbosefile()
1146 set verbosefile=Xlog
1147 echomsg 'foo'
1148 echomsg 'bar'
1149 set verbosefile=
1150 let log = readfile('Xlog')
1151 call assert_match("foo\nbar", join(log, "\n"))
1152 call delete('Xlog')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001153 call mkdir('Xdir')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001154 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001155 call delete('Xdir', 'd')
Bram Moolenaar52604f22017-04-07 16:17:39 +02001156endfunc
1157
Bram Moolenaar4facea32019-10-12 20:17:40 +02001158func Test_verbose_option()
1159 CheckScreendump
1160
1161 let lines =<< trim [SCRIPT]
1162 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1163 call feedkeys("\r", 't') " for the hit-enter prompt
1164 set verbose=20
1165 [SCRIPT]
1166 call writefile(lines, 'XTest_verbose')
1167
1168 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001169 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001170 call term_sendkeys(buf, ":DoSomething\<CR>")
1171 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1172
1173 " clean up
1174 call StopVimInTerminal(buf)
1175 call delete('XTest_verbose')
1176endfunc
1177
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001178func Test_setcmdpos()
1179 func InsertTextAtPos(text, pos)
1180 call assert_equal(0, setcmdpos(a:pos))
1181 return a:text
1182 endfunc
1183
1184 " setcmdpos() with position in the middle of the command line.
1185 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1186 call assert_equal('"1ab2', @:)
1187
1188 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1189 call assert_equal('"1b2a', @:)
1190
1191 " setcmdpos() with position beyond the end of the command line.
1192 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1193 call assert_equal('"12ab', @:)
1194
1195 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001196 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001197endfunc
1198
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001199func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001200 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001201 let encoding_save = &encoding
1202
1203 for e in encodings
1204 exe 'set encoding=' . e
1205
1206 " Test overstrike in the middle of the command line.
1207 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001208 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001209
1210 " Test overstrike going beyond end of command line.
1211 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001212 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001213
1214 " Test toggling insert/overstrike a few times.
1215 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001216 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001217 endfor
1218
Bram Moolenaar30276f22019-01-24 17:59:39 +01001219 " Test overstrike with multi-byte characters.
1220 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001221 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001222
1223 let &encoding = encoding_save
1224endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001225
1226func Test_cmdwin_bug()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001227 CheckFeature cmdwin
1228
Bram Moolenaara046b372019-09-15 17:26:07 +02001229 let winid = win_getid()
1230 sp
1231 try
1232 call feedkeys("q::call win_gotoid(" .. winid .. ")\<CR>:q\<CR>", 'x!')
1233 catch /^Vim\%((\a\+)\)\=:E11/
1234 endtry
1235 bw!
1236endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +01001237
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001238func Test_cmdwin_restore()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001239 CheckFeature cmdwin
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001240 CheckScreendump
1241
1242 let lines =<< trim [SCRIPT]
1243 call setline(1, range(30))
1244 2split
1245 [SCRIPT]
1246 call writefile(lines, 'XTest_restore')
1247
1248 let buf = RunVimInTerminal('-S XTest_restore', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001249 call TermWait(buf, 50)
Bram Moolenaar1c329c02019-10-27 20:37:35 +01001250 call term_sendkeys(buf, "q:")
1251 call VerifyScreenDump(buf, 'Test_cmdwin_restore_1', {})
1252
1253 " normal restore
1254 call term_sendkeys(buf, ":q\<CR>")
1255 call VerifyScreenDump(buf, 'Test_cmdwin_restore_2', {})
1256
1257 " restore after setting 'lines' with one window
1258 call term_sendkeys(buf, ":close\<CR>")
1259 call term_sendkeys(buf, "q:")
1260 call term_sendkeys(buf, ":set lines=18\<CR>")
1261 call term_sendkeys(buf, ":q\<CR>")
1262 call VerifyScreenDump(buf, 'Test_cmdwin_restore_3', {})
1263
1264 " clean up
1265 call StopVimInTerminal(buf)
1266 call delete('XTest_restore')
1267endfunc
1268
Bram Moolenaare5b44862021-05-30 13:54:03 +02001269func Test_cmdwin_no_terminal()
1270 CheckFeature cmdwin
1271 CheckFeature terminal
Bram Moolenaar0b496482021-05-30 14:21:57 +02001272 CheckNotMSWindows
Bram Moolenaare5b44862021-05-30 13:54:03 +02001273
1274 let buf = RunVimInTerminal('', {'rows': 12})
1275 call TermWait(buf, 50)
1276 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1277 call term_sendkeys(buf, "q:")
1278 call term_sendkeys(buf, ":let buf = term_start(['/bin/echo'], #{hidden: 1})\<CR>")
1279 call VerifyScreenDump(buf, 'Test_cmdwin_no_terminal', {})
1280 call term_sendkeys(buf, ":q\<CR>")
1281 call StopVimInTerminal(buf)
1282endfunc
1283
Bram Moolenaar52410572019-10-27 05:12:45 +01001284func Test_buffers_lastused()
1285 " check that buffers are sorted by time when wildmode has lastused
1286 call test_settime(1550020000) " middle
1287 edit bufa
1288 enew
1289 call test_settime(1550030000) " newest
1290 edit bufb
1291 enew
1292 call test_settime(1550010000) " oldest
1293 edit bufc
1294 enew
1295 call test_settime(0)
1296 enew
1297
1298 call assert_equal(['bufa', 'bufb', 'bufc'],
1299 \ getcompletion('', 'buffer'))
1300
1301 let save_wildmode = &wildmode
1302 set wildmode=full:lastused
1303
1304 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1305 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1306 call assert_equal('b bufb', X)
1307 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1308 call assert_equal('b bufa', X)
1309 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1310 call assert_equal('b bufc', X)
1311 enew
1312
1313 edit other
1314 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1315 call assert_equal('b bufb', X)
1316 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1317 call assert_equal('b bufa', X)
1318 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1319 call assert_equal('b bufc', X)
1320 enew
1321
1322 let &wildmode = save_wildmode
1323
1324 bwipeout bufa
1325 bwipeout bufb
1326 bwipeout bufc
1327endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001328
1329func Test_cmdwin_feedkeys()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001330 CheckFeature cmdwin
1331
Bram Moolenaar85db5472019-12-04 15:11:08 +01001332 " This should not generate E488
1333 call feedkeys("q:\<CR>", 'x')
Bram Moolenaar1671f442020-03-10 07:48:13 +01001334 " Using feedkeys with q: only should automatically close the cmd window
1335 call feedkeys('q:', 'xt')
1336 call assert_equal(1, winnr('$'))
1337 call assert_equal('', getcmdwintype())
Bram Moolenaar85db5472019-12-04 15:11:08 +01001338endfunc
Bram Moolenaar309976e2019-12-05 18:16:33 +01001339
1340" Tests for the issues fixed in 7.4.441.
1341" When 'cedit' is set to Ctrl-C, opening the command window hangs Vim
1342func Test_cmdwin_cedit()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001343 CheckFeature cmdwin
1344
Bram Moolenaar309976e2019-12-05 18:16:33 +01001345 exe "set cedit=\<C-c>"
1346 normal! :
1347 call assert_equal(1, winnr('$'))
1348
1349 let g:cmd_wintype = ''
1350 func CmdWinType()
1351 let g:cmd_wintype = getcmdwintype()
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001352 let g:wintype = win_gettype()
Bram Moolenaar309976e2019-12-05 18:16:33 +01001353 return ''
1354 endfunc
1355
1356 call feedkeys("\<C-c>a\<C-R>=CmdWinType()\<CR>\<CR>")
1357 echo input('')
1358 call assert_equal('@', g:cmd_wintype)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001359 call assert_equal('command', g:wintype)
Bram Moolenaar309976e2019-12-05 18:16:33 +01001360
1361 set cedit&vim
1362 delfunc CmdWinType
1363endfunc
1364
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001365" Test for CmdwinEnter autocmd
1366func Test_cmdwin_autocmd()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001367 CheckFeature cmdwin
1368
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001369 augroup CmdWin
1370 au!
Bram Moolenaarb63f3ca2021-01-30 21:40:03 +01001371 autocmd BufLeave * if &buftype == '' | update | endif
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001372 autocmd CmdwinEnter * startinsert
1373 augroup END
1374
1375 call assert_fails('call feedkeys("q:xyz\<CR>", "xt")', 'E492:')
1376 call assert_equal('xyz', @:)
1377
1378 augroup CmdWin
1379 au!
1380 augroup END
1381 augroup! CmdWin
1382endfunc
1383
Bram Moolenaar479950f2020-01-19 15:45:17 +01001384func Test_cmdlineclear_tabenter()
1385 CheckScreendump
1386
1387 let lines =<< trim [SCRIPT]
1388 call setline(1, range(30))
1389 [SCRIPT]
1390
1391 call writefile(lines, 'XtestCmdlineClearTabenter')
1392 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001393 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001394 " in one tab make the command line higher with CTRL-W -
1395 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1396 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1397
1398 call StopVimInTerminal(buf)
1399 call delete('XtestCmdlineClearTabenter')
1400endfunc
1401
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001402" Test for expanding special keywords in cmdline
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001403func Test_cmdline_expand_special()
1404 %bwipe!
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02001405 call assert_fails('e #', 'E194:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001406 call assert_fails('e <afile>', 'E495:')
1407 call assert_fails('e <abuf>', 'E496:')
1408 call assert_fails('e <amatch>', 'E497:')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001409
1410 call writefile([], 'Xfile.cpp')
1411 call writefile([], 'Xfile.java')
1412 new Xfile.cpp
1413 call feedkeys(":e %:r\<C-A>\<C-B>\"\<CR>", 'xt')
1414 call assert_equal('"e Xfile.cpp Xfile.java', @:)
1415 close
1416 call delete('Xfile.cpp')
1417 call delete('Xfile.java')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001418endfunc
1419
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001420func Test_cmdwin_jump_to_win()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001421 CheckFeature cmdwin
1422
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001423 call assert_fails('call feedkeys("q:\<C-W>\<C-W>\<CR>", "xt")', 'E11:')
1424 new
1425 set modified
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001426 call assert_fails('call feedkeys("q/:qall\<CR>", "xt")', ['E37:', 'E162:'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001427 close!
1428 call feedkeys("q/:close\<CR>", "xt")
1429 call assert_equal(1, winnr('$'))
1430 call feedkeys("q/:exit\<CR>", "xt")
1431 call assert_equal(1, winnr('$'))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001432
1433 " opening command window twice should fail
1434 call assert_beeps('call feedkeys("q:q:\<CR>\<CR>", "xt")')
1435 call assert_equal(1, winnr('$'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001436endfunc
1437
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001438func Test_cmdwin_interrupted()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001439 CheckFeature cmdwin
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001440 CheckScreendump
1441
1442 " aborting the :smile output caused the cmdline window to use the current
1443 " buffer.
1444 let lines =<< trim [SCRIPT]
1445 au WinNew * smile
1446 [SCRIPT]
1447 call writefile(lines, 'XTest_cmdwin')
1448
1449 let buf = RunVimInTerminal('-S XTest_cmdwin', {'rows': 18})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001450 " open cmdwin
1451 call term_sendkeys(buf, "q:")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001452 call WaitForAssert({-> assert_match('-- More --', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001453 " quit more prompt for :smile command
1454 call term_sendkeys(buf, "q")
Bram Moolenaarc82dd862020-06-07 17:30:33 +02001455 call WaitForAssert({-> assert_match('^$', term_getline(buf, 18))})
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02001456 " execute a simple command
1457 call term_sendkeys(buf, "aecho 'done'\<CR>")
1458 call VerifyScreenDump(buf, 'Test_cmdwin_interrupted', {})
1459
1460 " clean up
1461 call StopVimInTerminal(buf)
1462 call delete('XTest_cmdwin')
1463endfunc
1464
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001465" Test for backtick expression in the command line
1466func Test_cmd_backtick()
1467 %argd
1468 argadd `=['a', 'b', 'c']`
1469 call assert_equal(['a', 'b', 'c'], argv())
1470 %argd
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001471
1472 argadd `echo abc def`
1473 call assert_equal(['abc def'], argv())
1474 %argd
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001475endfunc
1476
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001477" Test for the :! command
1478func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001479 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001480
1481 let lines =<< trim [SCRIPT]
1482 " Test for no previous command
1483 call assert_fails('!!', 'E34:')
1484 set nomore
1485 " Test for cmdline expansion with :!
1486 call setline(1, 'foo!')
1487 silent !echo <cWORD> > Xfile.out
1488 call assert_equal(['foo!'], readfile('Xfile.out'))
1489 " Test for using previous command
1490 silent !echo \! !
1491 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1492 call writefile(v:errors, 'Xresult')
1493 call delete('Xfile.out')
1494 qall!
1495 [SCRIPT]
1496 call writefile(lines, 'Xscript')
1497 if RunVim([], [], '--clean -S Xscript')
1498 call assert_equal([], readfile('Xresult'))
1499 endif
1500 call delete('Xscript')
1501 call delete('Xresult')
1502endfunc
1503
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001504" Test error: "E135: *Filter* Autocommands must not change current buffer"
1505func Test_cmd_bang_E135()
1506 new
1507 call setline(1, ['a', 'b', 'c', 'd'])
1508 augroup test_cmd_filter_E135
1509 au!
1510 autocmd FilterReadPost * help
1511 augroup END
1512 call assert_fails('2,3!echo "x"', 'E135:')
1513
1514 augroup test_cmd_filter_E135
1515 au!
1516 augroup END
1517 %bwipe!
1518endfunc
1519
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001520" Test for using ~ for home directory in cmdline completion matches
1521func Test_cmdline_expand_home()
1522 call mkdir('Xdir')
1523 call writefile([], 'Xdir/Xfile1')
1524 call writefile([], 'Xdir/Xfile2')
1525 cd Xdir
1526 let save_HOME = $HOME
1527 let $HOME = getcwd()
1528 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1529 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1530 let $HOME = save_HOME
1531 cd ..
1532 call delete('Xdir', 'rf')
1533endfunc
1534
Bram Moolenaar578fe942020-02-27 21:32:51 +01001535" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1536" or insert mode (when 'insertmode' is set)
1537func Test_cmdline_ctrl_g()
1538 new
1539 call setline(1, 'abc')
1540 call cursor(1, 3)
1541 " If command line is entered from insert mode, using C-\ C-G should back to
1542 " insert mode
1543 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1544 call assert_equal('abxyc', getline(1))
1545 call assert_equal(4, col('.'))
1546
1547 " If command line is entered in 'insertmode', using C-\ C-G should back to
1548 " 'insertmode'
1549 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1550 call assert_equal('ab12xyc', getline(1))
1551 close!
1552endfunc
1553
Bram Moolenaar578fe942020-02-27 21:32:51 +01001554" Test for 'wildmode'
1555func Test_wildmode()
1556 func T(a, c, p)
1557 return "oneA\noneB\noneC"
1558 endfunc
1559 command -nargs=1 -complete=custom,T MyCmd
1560
1561 func SaveScreenLine()
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001562 let g:Sline = Screenline(&lines - 1)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001563 return ''
1564 endfunc
1565 cnoremap <expr> <F2> SaveScreenLine()
1566
1567 set nowildmenu
1568 set wildmode=full,list
1569 let g:Sline = ''
1570 call feedkeys(":MyCmd \t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001571 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001572 call assert_equal('"MyCmd oneA', @:)
1573
1574 set wildmode=longest,full
1575 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1576 call assert_equal('"MyCmd one', @:)
1577 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1578 call assert_equal('"MyCmd oneC', @:)
1579
1580 set wildmode=longest
1581 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1582 call assert_equal('"MyCmd one', @:)
1583
1584 set wildmode=list:longest
1585 let g:Sline = ''
1586 call feedkeys(":MyCmd \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 one', @:)
1589
1590 set wildmode=""
1591 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1592 call assert_equal('"MyCmd oneA', @:)
1593
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001594 " Test for wildmode=longest with 'fileignorecase' set
1595 set wildmode=longest
1596 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001597 argadd AAA AAAA AAAAA
1598 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1599 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001600 set fileignorecase&
1601
1602 " Test for listing files with wildmode=list
1603 set wildmode=list
1604 let g:Sline = ''
1605 call feedkeys(":b A\t\t\<F2>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001606 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001607 call assert_equal('"b A', @:)
1608
1609 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001610 delcommand MyCmd
1611 delfunc T
1612 delfunc SaveScreenLine
1613 cunmap <F2>
1614 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001615 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001616endfunc
1617
1618" Test for interrupting the command-line completion
1619func Test_interrupt_compl()
1620 func F(lead, cmdl, p)
1621 if a:lead =~ 'tw'
1622 call interrupt()
1623 return
1624 endif
1625 return "one\ntwo\nthree"
1626 endfunc
1627 command -nargs=1 -complete=custom,F Tcmd
1628
1629 set nowildmenu
1630 set wildmode=full
1631 let interrupted = 0
1632 try
1633 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
1634 catch /^Vim:Interrupt$/
1635 let interrupted = 1
1636 endtry
1637 call assert_equal(1, interrupted)
1638
1639 delcommand Tcmd
1640 delfunc F
1641 set wildmode&
1642endfunc
1643
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001644" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01001645func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001646 let str = ":one two\<C-U>"
1647 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001648 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001649 let str ..= "\<Left>five\<Right>"
1650 let str ..= "\<Home>two "
1651 let str ..= "\<C-Left>one "
1652 let str ..= "\<C-Right> three"
1653 let str ..= "\<End>\<S-Left>four "
1654 let str ..= "\<S-Right> six"
1655 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1656 call feedkeys(str, 'xt')
1657 call assert_equal("\"one two three four five six seven", @:)
1658endfunc
1659
1660" Test for moving the cursor on the / command line in 'rightleft' mode
1661func Test_cmdline_edit_rightleft()
1662 CheckFeature rightleft
1663 set rightleft
1664 set rightleftcmd=search
1665 let str = "/one two\<C-U>"
1666 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001667 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01001668 let str ..= "\<Right>five\<Left>"
1669 let str ..= "\<Home>two "
1670 let str ..= "\<C-Right>one "
1671 let str ..= "\<C-Left> three"
1672 let str ..= "\<End>\<S-Right>four "
1673 let str ..= "\<S-Left> six"
1674 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
1675 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
1676 call assert_equal("\"one two three four five six seven", @/)
1677 set rightleftcmd&
1678 set rightleft&
1679endfunc
1680
1681" Test for using <C-\>e in the command line to evaluate an expression
1682func Test_cmdline_expr()
1683 " Evaluate an expression from the beginning of a command line
1684 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
1685 call assert_equal('"hello', @:)
1686
1687 " Use an invalid expression for <C-\>e
1688 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
1689
1690 " Insert literal <CTRL-\> in the command line
1691 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
1692 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001693endfunc
1694
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01001695" Test for 'imcmdline' and 'imsearch'
1696" This test doesn't actually test the input method functionality.
1697func Test_cmdline_inputmethod()
1698 new
1699 call setline(1, ['', 'abc', ''])
1700 set imcmdline
1701
1702 call feedkeys(":\"abc\<CR>", 'xt')
1703 call assert_equal("\"abc", @:)
1704 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
1705 call assert_equal("\"abc", @:)
1706 call feedkeys("/abc\<CR>", 'xt')
1707 call assert_equal([2, 1], [line('.'), col('.')])
1708 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1709 call assert_equal([2, 1], [line('.'), col('.')])
1710
1711 set imsearch=2
1712 call cursor(1, 1)
1713 call feedkeys("/abc\<CR>", 'xt')
1714 call assert_equal([2, 1], [line('.'), col('.')])
1715 call cursor(1, 1)
1716 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1717 call assert_equal([2, 1], [line('.'), col('.')])
1718 set imdisable
1719 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
1720 call assert_equal([2, 1], [line('.'), col('.')])
1721 set imdisable&
1722 set imsearch&
1723
1724 set imcmdline&
1725 %bwipe!
1726endfunc
1727
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001728" Test for recursively getting multiple command line inputs
1729func Test_cmdwin_multi_input()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001730 CheckFeature cmdwin
1731
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01001732 call feedkeys(":\<C-R>=input('P: ')\<CR>\"cyan\<CR>\<CR>", 'xt')
1733 call assert_equal('"cyan', @:)
1734endfunc
1735
1736" Test for using CTRL-_ in the command line with 'allowrevins'
1737func Test_cmdline_revins()
1738 CheckNotMSWindows
1739 CheckFeature rightleft
1740 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
1741 call assert_equal("\"abc\<c-_>", @:)
1742 set allowrevins
1743 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
1744 call assert_equal('"abcñèæ', @:)
1745 set allowrevins&
1746endfunc
1747
1748" Test for typing UTF-8 composing characters in the command line
1749func Test_cmdline_composing_chars()
1750 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
1751 call assert_equal('"ゔ', @:)
1752endfunc
1753
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001754" Test for normal mode commands not supported in the cmd window
1755func Test_cmdwin_blocked_commands()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001756 CheckFeature cmdwin
1757
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001758 call assert_fails('call feedkeys("q:\<C-T>\<CR>", "xt")', 'E11:')
1759 call assert_fails('call feedkeys("q:\<C-]>\<CR>", "xt")', 'E11:')
1760 call assert_fails('call feedkeys("q:\<C-^>\<CR>", "xt")', 'E11:')
1761 call assert_fails('call feedkeys("q:Q\<CR>", "xt")', 'E11:')
1762 call assert_fails('call feedkeys("q:Z\<CR>", "xt")', 'E11:')
1763 call assert_fails('call feedkeys("q:\<F1>\<CR>", "xt")', 'E11:')
Bram Moolenaar951a2fb2020-06-07 19:38:10 +02001764 call assert_fails('call feedkeys("q:\<C-W>s\<CR>", "xt")', 'E11:')
1765 call assert_fails('call feedkeys("q:\<C-W>v\<CR>", "xt")', 'E11:')
1766 call assert_fails('call feedkeys("q:\<C-W>^\<CR>", "xt")', 'E11:')
1767 call assert_fails('call feedkeys("q:\<C-W>n\<CR>", "xt")', 'E11:')
1768 call assert_fails('call feedkeys("q:\<C-W>z\<CR>", "xt")', 'E11:')
1769 call assert_fails('call feedkeys("q:\<C-W>o\<CR>", "xt")', 'E11:')
1770 call assert_fails('call feedkeys("q:\<C-W>w\<CR>", "xt")', 'E11:')
1771 call assert_fails('call feedkeys("q:\<C-W>j\<CR>", "xt")', 'E11:')
1772 call assert_fails('call feedkeys("q:\<C-W>k\<CR>", "xt")', 'E11:')
1773 call assert_fails('call feedkeys("q:\<C-W>h\<CR>", "xt")', 'E11:')
1774 call assert_fails('call feedkeys("q:\<C-W>l\<CR>", "xt")', 'E11:')
1775 call assert_fails('call feedkeys("q:\<C-W>T\<CR>", "xt")', 'E11:')
1776 call assert_fails('call feedkeys("q:\<C-W>x\<CR>", "xt")', 'E11:')
1777 call assert_fails('call feedkeys("q:\<C-W>r\<CR>", "xt")', 'E11:')
1778 call assert_fails('call feedkeys("q:\<C-W>R\<CR>", "xt")', 'E11:')
1779 call assert_fails('call feedkeys("q:\<C-W>K\<CR>", "xt")', 'E11:')
1780 call assert_fails('call feedkeys("q:\<C-W>}\<CR>", "xt")', 'E11:')
1781 call assert_fails('call feedkeys("q:\<C-W>]\<CR>", "xt")', 'E11:')
1782 call assert_fails('call feedkeys("q:\<C-W>f\<CR>", "xt")', 'E11:')
1783 call assert_fails('call feedkeys("q:\<C-W>d\<CR>", "xt")', 'E11:')
1784 call assert_fails('call feedkeys("q:\<C-W>g\<CR>", "xt")', 'E11:')
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001785endfunc
1786
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001787" Close the Cmd-line window in insert mode using CTRL-C
1788func Test_cmdwin_insert_mode_close()
Bram Moolenaar21829c52021-01-26 22:42:21 +01001789 CheckFeature cmdwin
1790
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001791 %bw!
1792 let s = ''
1793 exe "normal q:a\<C-C>let s='Hello'\<CR>"
1794 call assert_equal('Hello', s)
1795 call assert_equal(1, winnr('$'))
1796endfunc
1797
Bram Moolenaar0e717042020-04-27 19:29:01 +02001798" test that ";" works to find a match at the start of the first line
1799func Test_zero_line_search()
1800 new
1801 call setline(1, ["1, pattern", "2, ", "3, pattern"])
1802 call cursor(1,1)
1803 0;/pattern/d
1804 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
1805 q!
1806endfunc
1807
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001808func Test_read_shellcmd()
1809 CheckUnix
1810 if executable('ls')
1811 " There should be ls in the $PATH
1812 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
1813 call assert_match('^"r! .*\<ls\>', @:)
1814 endif
1815
1816 if executable('rm')
1817 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
1818 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
1819 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02001820
1821 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
1822 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
1823 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001824 endif
1825endfunc
1826
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01001827" Test for going up and down the directory tree using 'wildmenu'
1828func Test_wildmenu_dirstack()
1829 CheckUnix
1830 %bw!
1831 call mkdir('Xdir1/dir2/dir3', 'p')
1832 call writefile([], 'Xdir1/file1_1.txt')
1833 call writefile([], 'Xdir1/file1_2.txt')
1834 call writefile([], 'Xdir1/dir2/file2_1.txt')
1835 call writefile([], 'Xdir1/dir2/file2_2.txt')
1836 call writefile([], 'Xdir1/dir2/dir3/file3_1.txt')
1837 call writefile([], 'Xdir1/dir2/dir3/file3_2.txt')
1838 cd Xdir1/dir2/dir3
1839 set wildmenu
1840
1841 call feedkeys(":e \<Tab>\<C-B>\"\<CR>", 'xt')
1842 call assert_equal('"e file3_1.txt', @:)
1843 call feedkeys(":e \<Tab>\<Up>\<C-B>\"\<CR>", 'xt')
1844 call assert_equal('"e ../dir3/', @:)
1845 call feedkeys(":e \<Tab>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
1846 call assert_equal('"e ../../dir2/', @:)
1847 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<C-B>\"\<CR>", 'xt')
1848 call assert_equal('"e ../../dir2/dir3/', @:)
1849 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
1850 call assert_equal('"e ../../dir2/dir3/file3_1.txt', @:)
1851
1852 cd -
1853 call delete('Xdir1', 'rf')
1854 set wildmenu&
1855endfunc
1856
obcat71c6f7a2021-05-13 20:23:10 +02001857" Test for recalling newer or older cmdline from history with <Up>, <Down>,
1858" <S-Up>, <S-Down>, <PageUp>, <PageDown>, <C-p>, or <C-n>.
1859func Test_recalling_cmdline()
1860 CheckFeature cmdline_hist
1861
1862 let g:cmdlines = []
1863 cnoremap <Plug>(save-cmdline) <Cmd>let g:cmdlines += [getcmdline()]<CR>
1864
1865 let histories = [
1866 \ {'name': 'cmd', 'enter': ':', 'exit': "\<Esc>"},
1867 \ {'name': 'search', 'enter': '/', 'exit': "\<Esc>"},
1868 \ {'name': 'expr', 'enter': ":\<C-r>=", 'exit': "\<Esc>\<Esc>"},
1869 \ {'name': 'input', 'enter': ":call input('')\<CR>", 'exit': "\<CR>"},
1870 "\ TODO: {'name': 'debug', ...}
1871 \]
1872 let keypairs = [
1873 \ {'older': "\<Up>", 'newer': "\<Down>", 'prefixmatch': v:true},
1874 \ {'older': "\<S-Up>", 'newer': "\<S-Down>", 'prefixmatch': v:false},
1875 \ {'older': "\<PageUp>", 'newer': "\<PageDown>", 'prefixmatch': v:false},
1876 \ {'older': "\<C-p>", 'newer': "\<C-n>", 'prefixmatch': v:false},
1877 \]
1878 let prefix = 'vi'
1879 for h in histories
1880 call histadd(h.name, 'vim')
1881 call histadd(h.name, 'virtue')
1882 call histadd(h.name, 'Virgo')
1883 call histadd(h.name, 'vogue')
1884 call histadd(h.name, 'emacs')
1885 for k in keypairs
1886 let g:cmdlines = []
1887 let keyseqs = h.enter
1888 \ .. prefix
1889 \ .. repeat(k.older .. "\<Plug>(save-cmdline)", 2)
1890 \ .. repeat(k.newer .. "\<Plug>(save-cmdline)", 2)
1891 \ .. h.exit
1892 call feedkeys(keyseqs, 'xt')
1893 call histdel(h.name, -1) " delete the history added by feedkeys above
1894 let expect = k.prefixmatch
1895 \ ? ['virtue', 'vim', 'virtue', prefix]
1896 \ : ['emacs', 'vogue', 'emacs', prefix]
1897 call assert_equal(expect, g:cmdlines)
1898 endfor
1899 endfor
1900
1901 unlet g:cmdlines
1902 cunmap <Plug>(save-cmdline)
1903endfunc
1904
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001905func Test_cmd_map_cmdlineChanged()
1906 let g:log = []
1907 cnoremap <F1> l<Cmd><CR>s
1908 augroup test
1909 autocmd!
1910 autocmd CmdlineChanged : let g:log += [getcmdline()]
1911 augroup END
1912
1913 call feedkeys(":\<F1>\<CR>", 'xt')
1914 call assert_equal(['l', 'ls'], g:log)
1915
Bram Moolenaar796139a2021-05-18 21:38:45 +02001916 let @b = 'b'
1917 cnoremap <F1> a<C-R>b
1918 let g:log = []
1919 call feedkeys(":\<F1>\<CR>", 'xt')
1920 call assert_equal(['a', 'ab'], g:log)
1921
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001922 unlet g:log
1923 cunmap <F1>
1924 augroup test
1925 autocmd!
1926 augroup END
1927endfunc
1928
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001929" Test for the 'suffixes' option
1930func Test_suffixes_opt()
1931 call writefile([], 'Xfile')
1932 call writefile([], 'Xfile.c')
1933 call writefile([], 'Xfile.o')
1934 set suffixes=
1935 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
1936 call assert_equal('"e Xfile Xfile.c Xfile.o', @:)
1937 set suffixes=.c
1938 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
1939 call assert_equal('"e Xfile Xfile.o Xfile.c', @:)
1940 set suffixes=,,
1941 call feedkeys(":e Xfi*\<C-A>\<C-B>\"\<CR>", 'xt')
1942 call assert_equal('"e Xfile.c Xfile.o Xfile', @:)
1943 set suffixes&
1944 call delete('Xfile')
1945 call delete('Xfile.c')
1946 call delete('Xfile.o')
1947endfunc
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001948
Bram Moolenaar309976e2019-12-05 18:16:33 +01001949" vim: shiftwidth=2 sts=2 expandtab