blob: c285b087a4afb9b54b595d2501a2a671aad27e7d [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 Moolenaarfe8e9f62022-03-16 13:09:15 +00007import './vim9.vim' as v9
Bram Moolenaar4facea32019-10-12 20:17:40 +02008
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00009func SetUp()
10 func SaveLastScreenLine()
11 let g:Sline = Screenline(&lines - 1)
12 return ''
13 endfunc
14 cnoremap <expr> <F4> SaveLastScreenLine()
15endfunc
16
17func TearDown()
18 delfunc SaveLastScreenLine
19 cunmap <F4>
20endfunc
21
Bram Moolenaarae3150e2016-06-11 23:22:36 +020022func Test_complete_tab()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010023 call writefile(['testfile'], 'Xtestfile', 'D')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020024 call feedkeys(":e Xtest\t\r", "tx")
25 call assert_equal('testfile', getline(1))
Albert Liu6024c042021-08-27 20:59:35 +020026
27 " Pressing <Tab> after '%' completes the current file, also on MS-Windows
28 call feedkeys(":e %\t\r", "tx")
29 call assert_equal('e Xtestfile', @:)
Bram Moolenaarae3150e2016-06-11 23:22:36 +020030endfunc
31
32func Test_complete_list()
33 " We can't see the output, but at least we check the code runs properly.
34 call feedkeys(":e test\<C-D>\r", "tx")
35 call assert_equal('test', expand('%:t'))
Bram Moolenaar578fe942020-02-27 21:32:51 +010036
37 " If a command doesn't support completion, then CTRL-D should be literally
38 " used.
39 call feedkeys(":chistory \<C-D>\<C-B>\"\<CR>", 'xt')
40 call assert_equal("\"chistory \<C-D>", @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +000041
42 " Test for displaying the tail of the completion matches
43 set wildmode=longest,full
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010044 call mkdir('Xtest', 'R')
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +000045 call writefile([], 'Xtest/a.c')
46 call writefile([], 'Xtest/a.h')
47 let g:Sline = ''
48 call feedkeys(":e Xtest/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
49 call assert_equal('a.c a.h', g:Sline)
50 call assert_equal('"e Xtest/', @:)
51 if has('win32')
52 " Test for 'completeslash'
53 set completeslash=backslash
54 call feedkeys(":e Xtest\<Tab>\<C-B>\"\<CR>", 'xt')
55 call assert_equal('"e Xtest\', @:)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +000056 call feedkeys(":e Xtest/\<Tab>\<C-B>\"\<CR>", 'xt')
57 call assert_equal('"e Xtest\a.', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +000058 set completeslash=slash
59 call feedkeys(":e Xtest\<Tab>\<C-B>\"\<CR>", 'xt')
60 call assert_equal('"e Xtest/', @:)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +000061 call feedkeys(":e Xtest\\\<Tab>\<C-B>\"\<CR>", 'xt')
62 call assert_equal('"e Xtest/a.', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +000063 set completeslash&
64 endif
65
66 " Test for displaying the tail with wildcards
67 let g:Sline = ''
68 call feedkeys(":e Xtes?/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
69 call assert_equal('Xtest/a.c Xtest/a.h', g:Sline)
70 call assert_equal('"e Xtes?/', @:)
71 let g:Sline = ''
72 call feedkeys(":e Xtes*/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
73 call assert_equal('Xtest/a.c Xtest/a.h', g:Sline)
74 call assert_equal('"e Xtes*/', @:)
75 let g:Sline = ''
76 call feedkeys(":e Xtes[/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
77 call assert_equal(':e Xtes[/', g:Sline)
78 call assert_equal('"e Xtes[/', @:)
79
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +000080 set wildmode&
Bram Moolenaarae3150e2016-06-11 23:22:36 +020081endfunc
82
83func Test_complete_wildmenu()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010084 call mkdir('Xwilddir1/Xdir2', 'pR')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010085 call writefile(['testfile1'], 'Xwilddir1/Xtestfile1')
86 call writefile(['testfile2'], 'Xwilddir1/Xtestfile2')
87 call writefile(['testfile3'], 'Xwilddir1/Xdir2/Xtestfile3')
88 call writefile(['testfile3'], 'Xwilddir1/Xdir2/Xtestfile4')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020089 set wildmenu
Bram Moolenaar37db6422019-03-28 21:26:23 +010090
91 " Pressing <Tab> completes, and moves to next files when pressing again.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010092 call feedkeys(":e Xwilddir1/\<Tab>\<Tab>\<CR>", 'tx')
Bram Moolenaar37db6422019-03-28 21:26:23 +010093 call assert_equal('testfile1', getline(1))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010094 call feedkeys(":e Xwilddir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
Bram Moolenaarae3150e2016-06-11 23:22:36 +020095 call assert_equal('testfile2', getline(1))
96
Bram Moolenaar37db6422019-03-28 21:26:23 +010097 " <S-Tab> is like <Tab> but begin with the last match and then go to
98 " previous.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +010099 call feedkeys(":e Xwilddir1/Xtest\<S-Tab>\<CR>", 'tx')
Bram Moolenaar37db6422019-03-28 21:26:23 +0100100 call assert_equal('testfile2', getline(1))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100101 call feedkeys(":e Xwilddir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
Bram Moolenaar37db6422019-03-28 21:26:23 +0100102 call assert_equal('testfile1', getline(1))
103
104 " <Left>/<Right> to move to previous/next file.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100105 call feedkeys(":e Xwilddir1/\<Tab>\<Right>\<CR>", 'tx')
Bram Moolenaar37db6422019-03-28 21:26:23 +0100106 call assert_equal('testfile1', getline(1))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100107 call feedkeys(":e Xwilddir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
Bram Moolenaar37db6422019-03-28 21:26:23 +0100108 call assert_equal('testfile2', getline(1))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100109 call feedkeys(":e Xwilddir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
Bram Moolenaar37db6422019-03-28 21:26:23 +0100110 call assert_equal('testfile1', getline(1))
111
112 " <Up>/<Down> to go up/down directories.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100113 call feedkeys(":e Xwilddir1/\<Tab>\<Down>\<CR>", 'tx')
Bram Moolenaar37db6422019-03-28 21:26:23 +0100114 call assert_equal('testfile3', getline(1))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100115 call feedkeys(":e Xwilddir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
Bram Moolenaar37db6422019-03-28 21:26:23 +0100116 call assert_equal('testfile1', getline(1))
117
Bram Moolenaar3e112ac2020-12-28 13:41:53 +0100118 " this fails in some Unix GUIs, not sure why
119 if !has('unix') || !has('gui_running')
120 " <C-J>/<C-K> mappings to go up/down directories when 'wildcharm' is
121 " different than 'wildchar'.
122 set wildcharm=<C-Z>
123 cnoremap <C-J> <Down><C-Z>
124 cnoremap <C-K> <Up><C-Z>
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100125 call feedkeys(":e Xwilddir1/\<Tab>\<C-J>\<CR>", 'tx')
Bram Moolenaar3e112ac2020-12-28 13:41:53 +0100126 call assert_equal('testfile3', getline(1))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100127 call feedkeys(":e Xwilddir1/\<Tab>\<C-J>\<C-K>\<CR>", 'tx')
Bram Moolenaar3e112ac2020-12-28 13:41:53 +0100128 call assert_equal('testfile1', getline(1))
129 set wildcharm=0
130 cunmap <C-J>
131 cunmap <C-K>
132 endif
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +0100133
Bram Moolenaar578fe942020-02-27 21:32:51 +0100134 " Test for canceling the wild menu by adding a character
135 redrawstatus
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100136 call feedkeys(":e Xwilddir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
137 call assert_equal('"e Xwilddir1/Xdir2/x', @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100138
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100139 " Completion using a relative path
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100140 cd Xwilddir1/Xdir2
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100141 call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
142 call assert_equal('"e Xtestfile3 Xtestfile4', @:)
143 cd -
144
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000145 " test for wildmenumode()
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100146 cnoremap <expr> <F2> wildmenumode()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100147 call feedkeys(":cd Xwilddir\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
148 call assert_equal('"cd Xwilddir1/0', @:)
149 call feedkeys(":e Xwilddir1/\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
150 call assert_equal('"e Xwilddir1/Xdir2/1', @:)
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100151 cunmap <F2>
152
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000153 " Test for canceling the wild menu by pressing <PageDown> or <PageUp>.
154 " After this pressing <Left> or <Right> should not change the selection.
155 call feedkeys(":sign \<Tab>\<PageDown>\<Left>\<Right>\<C-A>\<C-B>\"\<CR>", 'tx')
156 call assert_equal('"sign define', @:)
157 call histadd('cmd', 'TestWildMenu')
158 call feedkeys(":sign \<Tab>\<PageUp>\<Left>\<Right>\<C-A>\<C-B>\"\<CR>", 'tx')
159 call assert_equal('"TestWildMenu', @:)
160
Bram Moolenaar37db6422019-03-28 21:26:23 +0100161 " cleanup
162 %bwipe
Bram Moolenaarae3150e2016-06-11 23:22:36 +0200163 set nowildmenu
164endfunc
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100165
Bram Moolenaara60053b2020-09-03 16:50:13 +0200166func Test_wildmenu_screendump()
167 CheckScreendump
168
169 let lines =<< trim [SCRIPT]
170 set wildmenu hlsearch
171 [SCRIPT]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100172 call writefile(lines, 'XTest_wildmenu', 'D')
Bram Moolenaara60053b2020-09-03 16:50:13 +0200173
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +0200174 " Test simple wildmenu
Bram Moolenaara60053b2020-09-03 16:50:13 +0200175 let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
176 call term_sendkeys(buf, ":vim\<Tab>")
177 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
178
179 call term_sendkeys(buf, "\<Tab>")
180 call VerifyScreenDump(buf, 'Test_wildmenu_2', {})
181
182 call term_sendkeys(buf, "\<Tab>")
183 call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
184
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +0200185 " Looped back to the original value
Bram Moolenaar39f3b142021-02-14 12:57:36 +0100186 call term_sendkeys(buf, "\<Tab>\<Tab>")
Bram Moolenaara60053b2020-09-03 16:50:13 +0200187 call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +0200188
189 " Test that the wild menu is cleared properly
190 call term_sendkeys(buf, " ")
191 call VerifyScreenDump(buf, 'Test_wildmenu_5', {})
192
193 " Test that a different wildchar still works
194 call term_sendkeys(buf, "\<Esc>:set wildchar=<Esc>\<CR>")
195 call term_sendkeys(buf, ":vim\<Esc>")
196 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
197
198 " Double-<Esc> is a hard-coded method to escape while wildchar=<Esc>. Make
199 " sure clean up is properly done in edge case like this.
Bram Moolenaara60053b2020-09-03 16:50:13 +0200200 call term_sendkeys(buf, "\<Esc>")
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +0200201 call VerifyScreenDump(buf, 'Test_wildmenu_6', {})
Bram Moolenaara60053b2020-09-03 16:50:13 +0200202
203 " clean up
204 call StopVimInTerminal(buf)
Bram Moolenaara60053b2020-09-03 16:50:13 +0200205endfunc
206
Bram Moolenaara653e532022-04-19 11:38:24 +0100207func Test_redraw_in_autocmd()
208 CheckScreendump
209
210 let lines =<< trim END
211 set cmdheight=2
212 autocmd CmdlineChanged * redraw
213 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100214 call writefile(lines, 'XTest_redraw', 'D')
Bram Moolenaara653e532022-04-19 11:38:24 +0100215
216 let buf = RunVimInTerminal('-S XTest_redraw', {'rows': 8})
217 call term_sendkeys(buf, ":for i in range(3)\<CR>")
218 call VerifyScreenDump(buf, 'Test_redraw_in_autocmd_1', {})
219
220 call term_sendkeys(buf, "let i =")
221 call VerifyScreenDump(buf, 'Test_redraw_in_autocmd_2', {})
222
223 " clean up
224 call term_sendkeys(buf, "\<CR>")
225 call StopVimInTerminal(buf)
Bram Moolenaara653e532022-04-19 11:38:24 +0100226endfunc
227
Bram Moolenaarbcd69242022-09-19 21:16:12 +0100228func Test_redrawstatus_in_autocmd()
229 CheckScreendump
230
231 let lines =<< trim END
zeertzjqc14bfc32022-09-20 13:17:57 +0100232 set laststatus=2
233 set statusline=%=:%{getcmdline()}
zeertzjq320d9102022-09-20 17:12:13 +0100234 autocmd CmdlineChanged * redrawstatus
Bram Moolenaarbcd69242022-09-19 21:16:12 +0100235 END
236 call writefile(lines, 'XTest_redrawstatus', 'D')
237
238 let buf = RunVimInTerminal('-S XTest_redrawstatus', {'rows': 8})
zeertzjqc14bfc32022-09-20 13:17:57 +0100239 " :redrawstatus is postponed if messages have scrolled
Bram Moolenaarbcd69242022-09-19 21:16:12 +0100240 call term_sendkeys(buf, ":echo \"one\\ntwo\\nthree\\nfour\"\<CR>")
241 call term_sendkeys(buf, ":foobar")
242 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_1', {})
zeertzjqc14bfc32022-09-20 13:17:57 +0100243 " it is not postponed if messages have not scrolled
zeertzjq320d9102022-09-20 17:12:13 +0100244 call term_sendkeys(buf, "\<Esc>:for in in range(3)")
zeertzjqc14bfc32022-09-20 13:17:57 +0100245 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_2', {})
zeertzjq320d9102022-09-20 17:12:13 +0100246 " with cmdheight=1 messages have scrolled when typing :endfor
247 call term_sendkeys(buf, "\<CR>:endfor")
248 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_3', {})
249 call term_sendkeys(buf, "\<CR>:set cmdheight=2\<CR>")
250 " with cmdheight=2 messages haven't scrolled when typing :for or :endfor
251 call term_sendkeys(buf, ":for in in range(3)")
252 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_4', {})
253 call term_sendkeys(buf, "\<CR>:endfor")
254 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_5', {})
Bram Moolenaarbcd69242022-09-19 21:16:12 +0100255
256 " clean up
257 call term_sendkeys(buf, "\<CR>")
258 call StopVimInTerminal(buf)
259endfunc
260
Bram Moolenaarf797e302022-08-11 13:17:30 +0100261func Test_changing_cmdheight()
262 CheckScreendump
263
264 let lines =<< trim END
265 set cmdheight=1 laststatus=2
Bram Moolenaar0816f472022-10-05 15:42:32 +0100266 func EchoTwo()
267 set laststatus=2
268 set cmdheight=5
269 echo 'foo'
270 echo 'bar'
271 set cmdheight=1
272 endfunc
Bram Moolenaarf797e302022-08-11 13:17:30 +0100273 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100274 call writefile(lines, 'XTest_cmdheight', 'D')
Bram Moolenaarf797e302022-08-11 13:17:30 +0100275
276 let buf = RunVimInTerminal('-S XTest_cmdheight', {'rows': 8})
277 call term_sendkeys(buf, ":resize -3\<CR>")
278 call VerifyScreenDump(buf, 'Test_changing_cmdheight_1', {})
279
280 " using the space available doesn't change the status line
281 call term_sendkeys(buf, ":set cmdheight+=3\<CR>")
282 call VerifyScreenDump(buf, 'Test_changing_cmdheight_2', {})
283
284 " using more space moves the status line up
285 call term_sendkeys(buf, ":set cmdheight+=1\<CR>")
286 call VerifyScreenDump(buf, 'Test_changing_cmdheight_3', {})
287
288 " reducing cmdheight moves status line down
289 call term_sendkeys(buf, ":set cmdheight-=2\<CR>")
290 call VerifyScreenDump(buf, 'Test_changing_cmdheight_4', {})
291
Bram Moolenaara9a6b032023-02-05 18:00:42 +0000292 " reducing window size and then setting cmdheight
Bram Moolenaard4cf9fc2022-08-11 14:13:37 +0100293 call term_sendkeys(buf, ":resize -1\<CR>")
294 call term_sendkeys(buf, ":set cmdheight=1\<CR>")
295 call VerifyScreenDump(buf, 'Test_changing_cmdheight_5', {})
296
Bram Moolenaar0816f472022-10-05 15:42:32 +0100297 " setting 'cmdheight' works after outputting two messages
298 call term_sendkeys(buf, ":call EchoTwo()\<CR>")
299 call VerifyScreenDump(buf, 'Test_changing_cmdheight_6', {})
300
Bram Moolenaarf797e302022-08-11 13:17:30 +0100301 " clean up
302 call StopVimInTerminal(buf)
Bram Moolenaarf797e302022-08-11 13:17:30 +0100303endfunc
304
Bram Moolenaarc9f5f732022-10-06 11:39:06 +0100305func Test_cmdheight_tabline()
306 CheckScreendump
307
308 let buf = RunVimInTerminal('-c "set ls=2" -c "set stal=2" -c "set cmdheight=1"', {'rows': 6})
309 call VerifyScreenDump(buf, 'Test_cmdheight_tabline_1', {})
310
311 " clean up
312 call StopVimInTerminal(buf)
313endfunc
314
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100315func Test_map_completion()
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100316 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
317 call assert_equal('"map <unique> <silent>', getreg(':'))
318 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
319 call assert_equal('"map <script> <unique>', getreg(':'))
320 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
321 call assert_equal('"map <expr> <script>', getreg(':'))
322 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
323 call assert_equal('"map <buffer> <expr>', getreg(':'))
324 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
325 call assert_equal('"map <nowait> <buffer>', getreg(':'))
326 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
327 call assert_equal('"map <special> <nowait>', getreg(':'))
328 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
329 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200330
Bram Moolenaar1776a282019-05-03 16:05:41 +0200331 map <Middle>x middle
332
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200333 map ,f commaf
334 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200335 map <Left> left
336 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200337 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
338 call assert_equal('"map ,f', getreg(':'))
339 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
340 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200341 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
342 call assert_equal('"map <Left>', getreg(':'))
343 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200344 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
zeertzjqc3a26c62023-02-17 16:40:20 +0000345 call feedkeys(":map <M-Left>\<Tab>\<Home>\"\<CR>", 'xt')
346 call assert_equal("\"map <M-Left>x", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200347 unmap ,f
348 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200349 unmap <Left>
350 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200351
zeertzjqc3a26c62023-02-17 16:40:20 +0000352 set cpo-=< cpo-=k
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200353 map <Left> left
354 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
355 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200356 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200357 call assert_equal("\"map <M\<Tab>", getreg(':'))
zeertzjqc3a26c62023-02-17 16:40:20 +0000358 call feedkeys(":map \<C-V>\<C-V><M\<Tab>\<Home>\"\<CR>", 'xt')
359 call assert_equal("\"map \<C-V><Middle>x", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200360 unmap <Left>
361
362 set cpo+=<
363 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200364 exe "set t_k6=\<Esc>[17~"
365 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200366 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
367 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200368 if !has('gui_running')
369 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
370 call assert_equal("\"map <F6>x", getreg(':'))
371 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200372 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200373 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200374 set cpo-=<
375
376 set cpo+=B
377 map <Left> left
378 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
379 call assert_equal('"map <Left>', getreg(':'))
380 unmap <Left>
381 set cpo-=B
382
383 set cpo+=k
384 map <Left> left
385 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
386 call assert_equal('"map <Left>', getreg(':'))
387 unmap <Left>
388 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200389
Bram Moolenaar531be472020-09-23 22:38:05 +0200390 call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
391
Bram Moolenaar1776a282019-05-03 16:05:41 +0200392 unmap <Middle>x
393 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100394endfunc
395
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100396func Test_match_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100397 hi Aardig ctermfg=green
398 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000399 call assert_equal('"match Aardig', @:)
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100400 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000401 call assert_equal('"match none', @:)
402 call feedkeys(":match | chist\<Tab>\<C-B>\"\<CR>", 'xt')
403 call assert_equal('"match | chistory', @:)
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100404endfunc
405
406func Test_highlight_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100407 hi Aardig ctermfg=green
408 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
409 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200410 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
411 call assert_equal('"hi default Aardig', getreg(':'))
412 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
413 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100414 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
415 call assert_equal('"hi link', getreg(':'))
416 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
417 call assert_equal('"hi default', getreg(':'))
418 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
419 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200420 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
421 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
422 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
423 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200424
425 " A cleared group does not show up in completions.
426 hi Anders ctermfg=green
427 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
428 hi clear Aardig
429 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
430 hi clear Anders
431 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100432endfunc
433
Bram Moolenaar75e15672020-06-28 13:10:22 +0200434" Test for command-line expansion of "hi Ni " (easter egg)
435func Test_highlight_easter_egg()
436 call test_override('ui_delay', 1)
437 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
438 call assert_equal("\"hi Ni \<Tab>", @:)
439 call test_override('ALL', 0)
440endfunc
441
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200442func Test_getcompletion()
443 let groupcount = len(getcompletion('', 'event'))
444 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200445 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200446 call assert_true(matchcount > 0)
447 call assert_true(groupcount > matchcount)
448
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200449 if has('menu')
450 source $VIMRUNTIME/menu.vim
451 let matchcount = len(getcompletion('', 'menu'))
452 call assert_true(matchcount > 0)
453 call assert_equal(['File.'], getcompletion('File', 'menu'))
454 call assert_true(matchcount > 0)
455 let matchcount = len(getcompletion('File.', 'menu'))
456 call assert_true(matchcount > 0)
zeertzjq145a6af2023-01-22 12:41:55 +0000457 source $VIMRUNTIME/delmenu.vim
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200458 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200459
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200460 let l = getcompletion('v:n', 'var')
461 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200462 let l = getcompletion('v:notexists', 'var')
463 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200464
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200465 args a.c b.c
466 let l = getcompletion('', 'arglist')
467 call assert_equal(['a.c', 'b.c'], l)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000468 let l = getcompletion('a.', 'buffer')
469 call assert_equal(['a.c'], l)
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200470 %argdelete
471
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200472 let l = getcompletion('', 'augroup')
473 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200474 let l = getcompletion('blahblah', 'augroup')
475 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200476
477 let l = getcompletion('', 'behave')
478 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200479 let l = getcompletion('not', 'behave')
480 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200481
482 let l = getcompletion('', 'color')
483 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200484 let l = getcompletion('dirty', 'color')
485 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200486
487 let l = getcompletion('', 'command')
488 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200489 let l = getcompletion('awake', 'command')
490 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200491
492 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200493 call assert_true(index(l, 'samples/') >= 0)
494 let l = getcompletion('NoMatch', 'dir')
495 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200496
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100497 if glob('~/*') !=# ''
498 let l = getcompletion('~/', 'dir')
499 call assert_true(l[0][0] ==# '~')
500 endif
501
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200502 let l = getcompletion('exe', 'expression')
503 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200504 let l = getcompletion('kill', 'expression')
505 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200506
507 let l = getcompletion('tag', 'function')
508 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200509 let l = getcompletion('paint', 'function')
510 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200511
Kota Kato90c23532023-01-18 15:27:38 +0000512 if !has('ruby')
Bram Moolenaara9a6b032023-02-05 18:00:42 +0000513 " global_functions[] has an entry but it doesn't have an implementation
Kota Kato90c23532023-01-18 15:27:38 +0000514 let l = getcompletion('ruby', 'function')
515 call assert_equal([], l)
516 endif
517
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200518 let Flambda = {-> 'hello'}
519 let l = getcompletion('', 'function')
520 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200521 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200522
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200523 let l = getcompletion('run', 'file')
524 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200525 let l = getcompletion('walk', 'file')
526 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200527 set wildignore=*.vim
528 let l = getcompletion('run', 'file', 1)
529 call assert_true(index(l, 'runtest.vim') < 0)
530 set wildignore&
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000531 " Directory name with space character
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100532 call mkdir('Xdir with space', 'D')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000533 call assert_equal(['Xdir with space/'], getcompletion('Xdir\ w', 'shellcmd'))
534 call assert_equal(['./Xdir with space/'], getcompletion('./Xdir', 'shellcmd'))
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200535
536 let l = getcompletion('ha', 'filetype')
537 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200538 let l = getcompletion('horse', 'filetype')
539 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200540
541 let l = getcompletion('z', 'syntax')
542 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200543 let l = getcompletion('emacs', 'syntax')
544 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200545
546 let l = getcompletion('jikes', 'compiler')
547 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200548 let l = getcompletion('break', 'compiler')
549 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200550
551 let l = getcompletion('last', 'help')
552 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200553 let l = getcompletion('giveup', 'help')
554 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200555
556 let l = getcompletion('time', 'option')
557 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200558 let l = getcompletion('space', 'option')
559 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200560
561 let l = getcompletion('er', 'highlight')
562 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200563 let l = getcompletion('dark', 'highlight')
564 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200565
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200566 let l = getcompletion('', 'messages')
567 call assert_true(index(l, 'clear') >= 0)
568 let l = getcompletion('not', 'messages')
569 call assert_equal([], l)
570
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200571 let l = getcompletion('', 'mapclear')
572 call assert_true(index(l, '<buffer>') >= 0)
573 let l = getcompletion('not', 'mapclear')
574 call assert_equal([], l)
575
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200576 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200577 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200578 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
579 let root = has('win32') ? 'C:\\' : '/'
580 let l = getcompletion(root, 'shellcmd')
581 let expected = map(filter(glob(root . '*', 0, 1),
582 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
583 call assert_equal(expected, l)
584
Bram Moolenaarb650b982016-08-05 20:35:13 +0200585 if has('cscope')
586 let l = getcompletion('', 'cscope')
587 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
588 call assert_equal(cmds, l)
589 " using cmdline completion must not change the result
590 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
591 let l = getcompletion('', 'cscope')
592 call assert_equal(cmds, l)
593 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
594 let l = getcompletion('find ', 'cscope')
595 call assert_equal(keys, l)
596 endif
597
Bram Moolenaar7522f692016-08-06 14:12:50 +0200598 if has('signs')
599 sign define Testing linehl=Comment
600 let l = getcompletion('', 'sign')
601 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
602 call assert_equal(cmds, l)
603 " using cmdline completion must not change the result
604 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
605 let l = getcompletion('', 'sign')
606 call assert_equal(cmds, l)
607 let l = getcompletion('list ', 'sign')
608 call assert_equal(['Testing'], l)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000609 let l = getcompletion('de*', 'sign')
610 call assert_equal(['define'], l)
611 let l = getcompletion('p?', 'sign')
612 call assert_equal(['place'], l)
613 let l = getcompletion('j.', 'sign')
614 call assert_equal(['jump'], l)
Bram Moolenaar7522f692016-08-06 14:12:50 +0200615 endif
616
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200617 " Command line completion tests
618 let l = getcompletion('cd ', 'cmdline')
619 call assert_true(index(l, 'samples/') >= 0)
620 let l = getcompletion('cd NoMatch', 'cmdline')
621 call assert_equal([], l)
622 let l = getcompletion('let v:n', 'cmdline')
623 call assert_true(index(l, 'v:null') >= 0)
624 let l = getcompletion('let v:notexists', 'cmdline')
625 call assert_equal([], l)
626 let l = getcompletion('call tag', 'cmdline')
627 call assert_true(index(l, 'taglist(') >= 0)
628 let l = getcompletion('call paint', 'cmdline')
629 call assert_equal([], l)
zeertzjqe4c79d32023-08-15 22:41:53 +0200630 let l = getcompletion('autocmd BufEnter * map <bu', 'cmdline')
631 call assert_equal(['<buffer>'], l)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200632
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100633 func T(a, c, p)
ii144785fe02021-11-21 12:13:56 +0000634 let g:cmdline_compl_params = [a:a, a:c, a:p]
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100635 return "oneA\noneB\noneC"
636 endfunc
637 command -nargs=1 -complete=custom,T MyCmd
638 let l = getcompletion('MyCmd ', 'cmdline')
639 call assert_equal(['oneA', 'oneB', 'oneC'], l)
ii144785fe02021-11-21 12:13:56 +0000640 call assert_equal(['', 'MyCmd ', 6], g:cmdline_compl_params)
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100641
642 delcommand MyCmd
643 delfunc T
ii144785fe02021-11-21 12:13:56 +0000644 unlet g:cmdline_compl_params
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100645
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200646 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200647 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200648 if has('cmdline_hist')
649 call add(names, 'history')
650 endif
651 if has('gettext')
652 call add(names, 'locale')
653 endif
654 if has('profile')
655 call add(names, 'syntime')
656 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200657
658 set tags=Xtags
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100659 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags', 'D')
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200660
661 for name in names
662 let matchcount = len(getcompletion('', name))
663 call assert_true(matchcount >= 0, 'No matches for ' . name)
664 endfor
665
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200666 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200667
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000668 edit a~b
669 enew
670 call assert_equal(['a~b'], getcompletion('a~', 'buffer'))
671 bw a~b
672
673 if has('unix')
674 edit Xtest\
675 enew
676 call assert_equal(['Xtest\'], getcompletion('Xtest\', 'buffer'))
677 bw Xtest\
678 endif
679
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200680 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200681 call assert_fails('call getcompletion("", "burp")', 'E475:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100682 call assert_fails('call getcompletion("abc", [])', 'E1174:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200683endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200684
Bram Moolenaarc32949b2023-01-04 15:56:51 +0000685func Test_multibyte_expression()
Bram Moolenaar2468add2023-01-04 18:59:57 +0000686 " Get a dialog in the GUI
687 CheckNotGui
688
Bram Moolenaarc32949b2023-01-04 15:56:51 +0000689 " This was using uninitialized memory.
690 let lines =<< trim END
691 set verbose=6
692 norm @=ٷ
693 qall!
694 END
695 call writefile(lines, 'XmultiScript', 'D')
696 call RunVim('', '', '-u NONE -n -e -s -S XmultiScript')
697endfunc
698
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +0000699" Test for getcompletion() with "fuzzy" in 'wildoptions'
700func Test_getcompletion_wildoptions()
701 let save_wildoptions = &wildoptions
702 set wildoptions&
703 let l = getcompletion('space', 'option')
704 call assert_equal([], l)
705 let l = getcompletion('ier', 'command')
706 call assert_equal([], l)
707 set wildoptions=fuzzy
708 let l = getcompletion('space', 'option')
709 call assert_true(index(l, 'backspace') >= 0)
710 let l = getcompletion('ier', 'command')
711 call assert_true(index(l, 'compiler') >= 0)
712 let &wildoptions = save_wildoptions
713endfunc
714
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000715func Test_complete_autoload_error()
716 let save_rtp = &rtp
717 let lines =<< trim END
718 vim9script
719 export def Complete(..._): string
720 return 'match'
721 enddef
722 echo this will cause an error
723 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100724 call mkdir('Xdir/autoload', 'pR')
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000725 call writefile(lines, 'Xdir/autoload/script.vim')
726 exe 'set rtp+=' .. getcwd() .. '/Xdir'
727
728 let lines =<< trim END
729 vim9script
730 import autoload 'script.vim'
731 command -nargs=* -complete=custom,script.Complete Cmd eval 0 + 0
732 &wildcharm = char2nr("\<Tab>")
733 feedkeys(":Cmd \<Tab>", 'xt')
734 END
735 call v9.CheckScriptFailure(lines, 'E121: Undefined variable: this')
736
737 let &rtp = save_rtp
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000738endfunc
739
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100740func Test_fullcommand()
741 let tests = {
742 \ '': '',
743 \ ':': '',
744 \ ':::': '',
745 \ ':::5': '',
746 \ 'not_a_cmd': '',
747 \ 'Check': '',
748 \ 'syntax': 'syntax',
749 \ ':syntax': 'syntax',
750 \ '::::syntax': 'syntax',
751 \ 'sy': 'syntax',
752 \ 'syn': 'syntax',
753 \ 'synt': 'syntax',
754 \ ':sy': 'syntax',
755 \ '::::sy': 'syntax',
756 \ 'match': 'match',
757 \ '2match': 'match',
758 \ '3match': 'match',
759 \ 'aboveleft': 'aboveleft',
760 \ 'abo': 'aboveleft',
Bram Moolenaaraa534142022-09-15 21:46:02 +0100761 \ 'en': 'endif',
762 \ 'end': 'endif',
763 \ 'endi': 'endif',
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100764 \ 's': 'substitute',
765 \ '5s': 'substitute',
766 \ ':5s': 'substitute',
767 \ "'<,'>s": 'substitute',
768 \ ":'<,'>s": 'substitute',
LemonBoycc766a82022-04-04 15:46:58 +0100769 \ 'CheckLin': 'CheckLinux',
770 \ 'CheckLinux': 'CheckLinux',
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100771 \ }
772
773 for [in, want] in items(tests)
774 call assert_equal(want, fullcommand(in))
775 endfor
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +0200776 call assert_equal('', fullcommand(test_null_string()))
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100777
778 call assert_equal('syntax', 'syn'->fullcommand())
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200779
780 command -buffer BufferLocalCommand :
781 command GlobalCommand :
782 call assert_equal('GlobalCommand', fullcommand('GlobalCom'))
783 call assert_equal('BufferLocalCommand', fullcommand('BufferL'))
784 delcommand BufferLocalCommand
785 delcommand GlobalCommand
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100786endfunc
787
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200788func Test_shellcmd_completion()
789 let save_path = $PATH
790
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100791 call mkdir('Xpathdir/Xpathsubdir', 'pR')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200792 call writefile([''], 'Xpathdir/Xfile.exe')
793 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
794
795 " Set PATH to example directory without trailing slash.
796 let $PATH = getcwd() . '/Xpathdir'
797
798 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
799 " dirs in the PATH, even though they won't be executed. We check that only
800 " subdirs of the PWD and executables from the PATH are included in the
801 " suggestions.
802 let actual = getcompletion('X', 'shellcmd')
803 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
804 call insert(expected, 'Xfile.exe')
805 call assert_equal(expected, actual)
806
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200807 let $PATH = save_path
808endfunc
809
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200810func Test_expand_star_star()
Bram Moolenaarbf630112023-05-19 21:41:02 +0100811 call mkdir('a/b/c', 'pR')
812 call writefile(['asdfasdf'], 'a/b/c/fileXname')
813 call feedkeys(":find a/**/fileXname\<Tab>\<CR>", 'xt')
814 call assert_equal('find a/b/c/fileXname', @:)
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200815 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200816endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100817
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100818func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100819 let @a = "def"
820 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
821 call assert_equal('"abc def ghi', @:)
822
823 new
824 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
825
826 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
827 call assert_equal('"aaa asdf bbb', @:)
828
829 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
830 call assert_equal('"aaa /tmp/some bbb', @:)
831
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200832 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
833 call assert_equal('"aaa '.getline(1).' bbb', @:)
834
Bram Moolenaar21efc362016-12-03 14:05:49 +0100835 set incsearch
836 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
837 call assert_equal('"aaa verylongword bbb', @:)
838
839 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
840 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100841
842 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
843 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100844 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200845
846 " Error while typing a command used to cause that it was not executed
847 " in the end.
848 new
849 try
850 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
851 catch /^Vim\%((\a\+)\)\=:E32/
852 " ignore error E32
853 endtry
854 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100855
Bram Moolenaar578fe942020-02-27 21:32:51 +0100856 " Try to paste an invalid register using <C-R>
857 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
858 call assert_equal('"onetwo', @:)
859
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100860 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100861 let @a = "xy\<C-H>z"
862 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
863 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100864 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
865 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100866 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
867 call assert_equal("\"xy\<C-H>z", @:)
868
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100869 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
870 let @a = "xy\<C-V>z"
871 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
872 call assert_equal('"xyz', @:)
873 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
874 call assert_equal("\"xy\<C-V>z", @:)
875
Bram Moolenaar578fe942020-02-27 21:32:51 +0100876 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
877
Bram Moolenaar72532d32018-04-07 19:09:09 +0200878 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100879endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100880
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100881func Test_cmdline_remove_char()
882 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100883
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100884 for e in ['utf8', 'latin1']
885 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100886
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100887 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
888 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100889
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100890 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
891 call assert_equal('"abcdef', @:)
892
893 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
894 call assert_equal('"abc ghi', @:, e)
895
896 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
897 call assert_equal('"def', @:, e)
Bram Moolenaaref02f162022-05-07 10:49:10 +0100898
899 " This was going before the start in latin1.
900 call feedkeys(": \<C-W>\<CR>", 'tx')
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100901 endfor
902
903 let &encoding = encoding_save
904endfunc
905
906func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200907 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100908
909 set keymap=esperanto
910 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
911 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
912 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100913endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100914
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100915func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100916 new
917 2;'(
918 2;')
919 quit
920endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100921
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100922func Test_illegal_address2()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100923 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim', 'D')
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100924 new
925 source Xtest.vim
926 " Trigger calling validate_cursor()
927 diffsp Xtest.vim
928 quit!
929 bwipe!
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100930endfunc
931
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +0100932func Test_mark_from_line_zero()
933 " this was reading past the end of the first (empty) line
934 new
935 norm oxxxx
936 call assert_fails("0;'(", 'E20:')
937 bwipe!
938endfunc
939
Bram Moolenaarba47b512017-01-24 21:18:19 +0100940func Test_cmdline_complete_wildoptions()
941 help
942 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
943 let a = join(sort(split(@:)),' ')
944 set wildoptions=tagfile
945 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
946 let b = join(sort(split(@:)),' ')
947 call assert_equal(a, b)
948 bw!
949endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100950
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200951func Test_cmdline_complete_user_cmd()
952 command! -complete=color -nargs=1 Foo :
953 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
954 call assert_equal('"Foo blue', @:)
955 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
956 call assert_equal('"Foo blue', @:)
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000957 call feedkeys(":Foo a b\<Tab>\<Home>\"\<cr>", 'tx')
958 call assert_equal('"Foo a blue', @:)
959 call feedkeys(":Foo b\\\<Tab>\<Home>\"\<cr>", 'tx')
960 call assert_equal('"Foo b\', @:)
961 call feedkeys(":Foo b\\x\<Tab>\<Home>\"\<cr>", 'tx')
962 call assert_equal('"Foo b\x', @:)
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200963 delcommand Foo
Bram Moolenaar300175f2022-08-21 18:38:21 +0100964
965 redraw
966 call assert_equal('~', Screenline(&lines - 1))
967 command! FooOne :
968 command! FooTwo :
969
970 set nowildmenu
971 call feedkeys(":Foo\<Tab>\<Home>\"\<cr>", 'tx')
972 call assert_equal('"FooOne', @:)
973 call assert_equal('~', Screenline(&lines - 1))
974
975 call feedkeys(":Foo\<S-Tab>\<Home>\"\<cr>", 'tx')
976 call assert_equal('"FooTwo', @:)
977 call assert_equal('~', Screenline(&lines - 1))
978
979 delcommand FooOne
980 delcommand FooTwo
981 set wildmenu&
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200982endfunc
983
Bram Moolenaarc2842ad2022-07-26 17:23:47 +0100984func Test_complete_user_cmd()
985 command FooBar echo 'global'
986 command -buffer FooBar echo 'local'
987 call feedkeys(":Foo\<C-A>\<Home>\"\<CR>", 'tx')
988 call assert_equal('"FooBar', @:)
989
990 delcommand -buffer FooBar
991 delcommand FooBar
992endfunc
993
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100994func s:ScriptLocalFunction()
995 echo 'yes'
996endfunc
997
998func Test_cmdline_complete_user_func()
999 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
naohiro ono5aec7552021-08-19 21:20:41 +02001000 call assert_match('"func Test_cmdline_complete_user_', @:)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001001 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
1002 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001003
1004 " g: prefix also works
1005 call feedkeys(":echo g:Test_cmdline_complete_user_f\<Tab>\<Home>\"\<cr>", 'tx')
1006 call assert_match('"echo g:Test_cmdline_complete_user_func', @:)
Bram Moolenaar069f9082021-08-13 17:48:25 +02001007
1008 " using g: prefix does not result in just "g:" matches from a lambda
1009 let Fx = { a -> a }
1010 call feedkeys(":echo g:\<Tab>\<Home>\"\<cr>", 'tx')
1011 call assert_match('"echo g:[A-Z]', @:)
naohiro ono5aec7552021-08-19 21:20:41 +02001012
1013 " existence of script-local dict function does not break user function name
1014 " completion
1015 function s:a_dict_func() dict
1016 endfunction
1017 call feedkeys(":call Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
1018 call assert_match('"call Test_cmdline_complete_user_', @:)
1019 delfunction s:a_dict_func
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001020endfunc
1021
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001022func Test_cmdline_complete_user_names()
1023 if has('unix') && executable('whoami')
1024 let whoami = systemlist('whoami')[0]
1025 let first_letter = whoami[0]
1026 if len(first_letter) > 0
1027 " Trying completion of :e ~x where x is the first letter of
1028 " the user name should complete to at least the user name.
1029 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
1030 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
1031 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001032 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001033 " Just in case: check that the system has an Administrator account.
1034 let names = system('net user')
1035 if names =~ 'Administrator'
1036 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +01001037 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001038 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +01001039 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001040 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001041 else
1042 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001043 endif
1044endfunc
1045
Bram Moolenaar297610b2019-12-27 17:20:55 +01001046func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001047 CheckExecutable whoami
1048 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
1049 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +01001050endfunc
1051
Bram Moolenaar8b633132020-03-20 18:20:51 +01001052func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001053 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
1054 call assert_equal(lang, v:lc_time)
1055
1056 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
1057 call assert_equal(lang, v:ctype)
1058
1059 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
1060 call assert_equal(lang, v:collate)
1061
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001062 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001063 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001064
1065 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001066 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001067
Bram Moolenaarec680282020-06-12 19:35:32 +02001068 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001069
Bram Moolenaarec680282020-06-12 19:35:32 +02001070 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
1071 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001072
Bram Moolenaarec680282020-06-12 19:35:32 +02001073 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
1074 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001075
Bram Moolenaarec680282020-06-12 19:35:32 +02001076 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
1077 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001078
1079 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
1080 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001081endfunc
1082
Bram Moolenaar297610b2019-12-27 17:20:55 +01001083func Test_cmdline_complete_env_variable()
1084 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +01001085 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
1086 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +01001087 unlet $X_VIM_TEST_COMPLETE_ENV
1088endfunc
1089
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001090func Test_cmdline_complete_expression()
1091 let g:SomeVar = 'blah'
1092 for cmd in ['exe', 'echo', 'echon', 'echomsg']
1093 call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
1094 call assert_match('"' .. cmd .. ' SomeVar', @:)
1095 call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
1096 call assert_match('"' .. cmd .. ' foo SomeVar', @:)
1097 endfor
1098 unlet g:SomeVar
1099endfunc
1100
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001101func Test_cmdline_complete_argopt()
1102 " completion for ++opt=arg for file commands
1103 call assert_equal('fileformat=', getcompletion('edit ++', 'cmdline')[0])
1104 call assert_equal('encoding=', getcompletion('read ++e', 'cmdline')[0])
1105 call assert_equal('edit', getcompletion('read ++bin ++edi', 'cmdline')[0])
1106
1107 call assert_equal(['fileformat='], getcompletion('edit ++ff', 'cmdline'))
1108
1109 call assert_equal('dos', getcompletion('write ++ff=d', 'cmdline')[0])
1110 call assert_equal('mac', getcompletion('args ++fileformat=m', 'cmdline')[0])
1111 call assert_equal('utf-8', getcompletion('split ++enc=ut*-8', 'cmdline')[0])
1112 call assert_equal('latin1', getcompletion('tabedit ++encoding=lati', 'cmdline')[0])
1113 call assert_equal('keep', getcompletion('edit ++bad=k', 'cmdline')[0])
1114
1115 call assert_equal([], getcompletion('edit ++bogus=', 'cmdline'))
1116
1117 " completion should skip the ++opt and continue
1118 call writefile([], 'Xaaaaa.txt', 'D')
1119 call feedkeys(":split ++enc=latin1 Xaaa\<C-A>\<C-B>\"\<CR>", 'xt')
1120 call assert_equal('"split ++enc=latin1 Xaaaaa.txt', @:)
1121
1122 if has('terminal')
1123 " completion for terminal's [options]
1124 call assert_equal('close', getcompletion('terminal ++cl*e', 'cmdline')[0])
1125 call assert_equal('hidden', getcompletion('terminal ++open ++hidd', 'cmdline')[0])
1126 call assert_equal('term', getcompletion('terminal ++kill=ter', 'cmdline')[0])
1127
1128 call assert_equal([], getcompletion('terminal ++bogus=', 'cmdline'))
1129
1130 " :terminal completion should skip the ++opt when considering what is the
1131 " first option, which is a list of shell commands, unlike second option
1132 " onwards.
1133 let first_param = getcompletion('terminal ', 'cmdline')
1134 let second_param = getcompletion('terminal foo ', 'cmdline')
1135 let skipped_opt_param = getcompletion('terminal ++close ', 'cmdline')
1136 call assert_equal(first_param, skipped_opt_param)
1137 call assert_notequal(first_param, second_param)
1138 endif
1139endfunc
1140
Bram Moolenaar47016f52021-08-26 16:39:58 +02001141" Unique function name for completion below
1142func s:WeirdFunc()
1143 echo 'weird'
1144endfunc
1145
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001146" Test for various command-line completion
1147func Test_cmdline_complete_various()
1148 " completion for a command starting with a comment
1149 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
1150 call assert_equal("\" :|\"\<C-A>", @:)
1151
1152 " completion for a range followed by a comment
1153 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
1154 call assert_equal("\"1,2\"\<C-A>", @:)
1155
1156 " completion for :k command
1157 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
1158 call assert_equal("\"ka\<C-A>", @:)
1159
1160 " completion for short version of the :s command
1161 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
1162 call assert_equal("\"sI \<C-A>", @:)
1163
1164 " completion for :write command
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001165 call mkdir('Xcwdir', 'R')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001166 call writefile(['one'], 'Xcwdir/Xfile1')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001167 let save_cwd = getcwd()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001168 cd Xcwdir
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001169 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
1170 call assert_equal("\"w >> Xfile1", @:)
1171 call chdir(save_cwd)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001172
1173 " completion for :w ! and :r ! commands
1174 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
1175 call assert_equal("\"w !invalid_xyz_cmd", @:)
1176 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
1177 call assert_equal("\"r !invalid_xyz_cmd", @:)
1178
1179 " completion for :>> and :<< commands
1180 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
1181 call assert_equal("\">>>\<C-A>", @:)
1182 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
1183 call assert_equal("\"<<<\<C-A>", @:)
1184
1185 " completion for command with +cmd argument
1186 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
1187 call assert_equal("\"buffer +/pat Xabc", @:)
1188 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
1189 call assert_equal("\"buffer +/pat\<C-A>", @:)
1190
1191 " completion for a command with a trailing comment
1192 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
1193 call assert_equal("\"ls \" comment\<C-A>", @:)
1194
1195 " completion for a command with a trailing command
1196 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
1197 call assert_equal("\"ls | ls", @:)
1198
1199 " completion for a command with an CTRL-V escaped argument
1200 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
1201 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
1202
1203 " completion for a command that doesn't take additional arguments
1204 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
1205 call assert_equal("\"all abc\<C-A>", @:)
1206
zeertzjqd3de1782022-09-01 12:58:52 +01001207 " completion for :wincmd with :horizontal modifier
1208 call feedkeys(":horizontal wincm\<C-A>\<C-B>\"\<CR>", 'xt')
1209 call assert_equal("\"horizontal wincmd", @:)
1210
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001211 " completion for a command with a command modifier
1212 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
1213 call assert_equal("\"topleft new", @:)
1214
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001215 " completion for vim9 and legacy commands
1216 call feedkeys(":vim9 call strle\<C-A>\<C-B>\"\<CR>", 'xt')
1217 call assert_equal("\"vim9 call strlen(", @:)
1218 call feedkeys(":legac call strle\<C-A>\<C-B>\"\<CR>", 'xt')
1219 call assert_equal("\"legac call strlen(", @:)
1220
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001221 " completion for the :disassemble command
1222 call feedkeys(":disas deb\<C-A>\<C-B>\"\<CR>", 'xt')
1223 call assert_equal("\"disas debug", @:)
1224 call feedkeys(":disas pro\<C-A>\<C-B>\"\<CR>", 'xt')
1225 call assert_equal("\"disas profile", @:)
1226 call feedkeys(":disas debug Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
1227 call assert_equal("\"disas debug Test_cmdline_complete_various", @:)
1228 call feedkeys(":disas profile Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
1229 call assert_equal("\"disas profile Test_cmdline_complete_various", @:)
naohiro ono9aecf792021-08-28 15:56:06 +02001230 call feedkeys(":disas Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
1231 call assert_equal("\"disas Test_cmdline_complete_various", @:)
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001232
Bram Moolenaar47016f52021-08-26 16:39:58 +02001233 call feedkeys(":disas s:WeirdF\<C-A>\<C-B>\"\<CR>", 'xt')
naohiro ono9aecf792021-08-28 15:56:06 +02001234 call assert_match('"disas <SNR>\d\+_WeirdFunc', @:)
Bram Moolenaar47016f52021-08-26 16:39:58 +02001235
naohiro onodfe04db2021-09-12 15:45:10 +02001236 call feedkeys(":disas \<S-Tab>\<C-B>\"\<CR>", 'xt')
1237 call assert_match('"disas <SNR>\d\+_', @:)
1238 call feedkeys(":disas debug \<S-Tab>\<C-B>\"\<CR>", 'xt')
1239 call assert_match('"disas debug <SNR>\d\+_', @:)
1240
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001241 " completion for the :match command
1242 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
1243 call assert_equal("\"match Search /pat/\<C-A>", @:)
1244
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001245 " completion for the :doautocmd command
1246 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
1247 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
1248
Bram Moolenaarafe8cf62020-10-05 20:07:18 +02001249 " completion of autocmd group after comma
1250 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
1251 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
1252
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001253 " completion of file name in :doautocmd
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001254 call writefile([], 'Xvarfile1', 'D')
1255 call writefile([], 'Xvarfile2', 'D')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001256 call feedkeys(":doautocmd BufEnter Xvarfi\<C-A>\<C-B>\"\<CR>", 'xt')
1257 call assert_equal("\"doautocmd BufEnter Xvarfile1 Xvarfile2", @:)
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001258
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001259 " completion for the :augroup command
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02001260 augroup XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001261 augroup END
1262 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02001263 call assert_equal("\"augroup XTest.test", @:)
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01001264
1265 " group name completion in :autocmd
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02001266 call feedkeys(":au X\<C-A>\<C-B>\"\<CR>", 'xt')
1267 call assert_equal("\"au XTest.test", @:)
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01001268 call feedkeys(":au XTest.test\<Tab>\<C-B>\"\<CR>", 'xt')
1269 call assert_equal("\"au XTest.test", @:)
1270
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02001271 augroup! XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001272
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01001273 " autocmd pattern completion
1274 call feedkeys(":au BufEnter *.py\<Tab>\<C-B>\"\<CR>", 'xt')
1275 call assert_equal("\"au BufEnter *.py\t", @:)
1276
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001277 " completion for the :unlet command
1278 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
1279 call assert_equal("\"unlet one two", @:)
1280
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001281 " completion for the :buffer command with curlies
Bram Moolenaar39c47c32021-10-17 18:05:26 +01001282 " FIXME: what should happen on MS-Windows?
1283 if !has('win32')
1284 edit \{someFile}
1285 call feedkeys(":buf someFile\<C-A>\<C-B>\"\<CR>", 'xt')
1286 call assert_equal("\"buf {someFile}", @:)
1287 bwipe {someFile}
1288 endif
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001289
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001290 " completion for the :bdelete command
1291 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
1292 call assert_equal("\"bdel a b c", @:)
1293
1294 " completion for the :mapclear command
1295 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
1296 call assert_equal("\"mapclear <buffer>", @:)
1297
1298 " completion for user defined commands with menu names
1299 menu Test.foo :ls<CR>
1300 com -nargs=* -complete=menu MyCmd
1301 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
1302 call assert_equal('"MyCmd Test.', @:)
1303 delcom MyCmd
1304 unmenu Test
1305
1306 " completion for user defined commands with mappings
1307 mapclear
1308 map <F3> :ls<CR>
1309 com -nargs=* -complete=mapping MyCmd
1310 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001311 call assert_equal('"MyCmd <F3> <F4>', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001312 mapclear
1313 delcom MyCmd
1314
Yee Cheng Chin54844852023-10-09 18:12:31 +02001315 " Prepare for path completion
1316 call mkdir('Xa b c', 'D')
1317 defer delete('Xcomma,foobar.txt')
1318 call writefile([], 'Xcomma,foobar.txt')
1319
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001320 " completion for :set path= with multiple backslashes
Yee Cheng Chin54844852023-10-09 18:12:31 +02001321 call feedkeys(':set path=Xa\\\ b' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1322 call assert_equal('"set path=Xa\\\ b\\\ c/', @:)
1323 set path&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001324
1325 " completion for :set dir= with a backslash
Yee Cheng Chin54844852023-10-09 18:12:31 +02001326 call feedkeys(':set dir=Xa\ b' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1327 call assert_equal('"set dir=Xa\ b\ c/', @:)
1328 set dir&
1329
1330 " completion for :set tags= / set dictionary= with escaped commas
1331 if has('win32')
1332 " In Windows backslashes are rounded up, so both '\,' and '\\,' escape to
1333 " '\,'
1334 call feedkeys(':set dictionary=Xcomma\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1335 call assert_equal('"set dictionary=Xcomma\,foobar.txt', @:)
1336
1337 call feedkeys(':set tags=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1338 call assert_equal('"set tags=Xcomma\,foobar.txt', @:)
1339
1340 call feedkeys(':set tags=Xcomma\\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1341 call assert_equal('"set tags=Xcomma\\\,foo', @:) " Didn't find a match
1342
1343 " completion for :set dictionary= with escaped commas (same behavior, but
1344 " different internal code path from 'set tags=' for escaping the output)
1345 call feedkeys(':set tags=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1346 call assert_equal('"set tags=Xcomma\,foobar.txt', @:)
1347 else
1348 " In other platforms, backslashes are rounded down (since '\,' itself will
1349 " be escaped into ','). As a result '\\,' and '\\\,' escape to '\,'.
1350 call feedkeys(':set tags=Xcomma\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1351 call assert_equal('"set tags=Xcomma\,foo', @:) " Didn't find a match
1352
1353 call feedkeys(':set tags=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1354 call assert_equal('"set tags=Xcomma\\,foobar.txt', @:)
1355
1356 call feedkeys(':set dictionary=Xcomma\\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1357 call assert_equal('"set dictionary=Xcomma\\,foobar.txt', @:)
1358
1359 " completion for :set dictionary= with escaped commas (same behavior, but
1360 " different internal code path from 'set tags=' for escaping the output)
1361 call feedkeys(':set dictionary=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1362 call assert_equal('"set dictionary=Xcomma\\,foobar.txt', @:)
1363 endif
1364 set tags&
1365 set dictionary&
1366
1367 " completion for :set makeprg= with no escaped commas
1368 call feedkeys(':set makeprg=Xcomma,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1369 call assert_equal('"set makeprg=Xcomma,foobar.txt', @:)
1370
1371 if !has('win32')
1372 " Cannot create file with backslash in file name in Windows, so only test
1373 " this elsewhere.
1374 defer delete('Xcomma\,fooslash.txt')
1375 call writefile([], 'Xcomma\,fooslash.txt')
1376 call feedkeys(':set makeprg=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1377 call assert_equal('"set makeprg=Xcomma\\,fooslash.txt', @:)
1378 endif
1379 set makeprg&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001380
1381 " completion for the :py3 commands
1382 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
1383 call assert_equal('"py3 py3do py3file', @:)
1384
Bram Moolenaardf749a22021-03-28 15:29:43 +02001385 " completion for the :vim9 commands
1386 call feedkeys(":vim9\<C-A>\<C-B>\"\<CR>", 'xt')
1387 call assert_equal('"vim9cmd vim9script', @:)
1388
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001389 " redir @" is not the start of a comment. So complete after that
1390 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
1391 call assert_equal('"redir @" | cwindow', @:)
1392
1393 " completion after a backtick
1394 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
1395 call assert_equal('"e `a1b2c', @:)
1396
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001397 " completion for :language command with an invalid argument
1398 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
1399 call assert_equal("\"language dummy \t", @:)
1400
1401 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +01001402 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
1403 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001404
1405 " completion with ambiguous user defined commands
1406 com TCmd1 echo 'TCmd1'
1407 com TCmd2 echo 'TCmd2'
1408 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
1409 call assert_equal('"TCmd ', @:)
1410 delcom TCmd1
1411 delcom TCmd2
1412
1413 " completion after a range followed by a pipe (|) character
1414 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
1415 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +02001416
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001417 " completion after a :global command
1418 call feedkeys(":g/a/chist\t\<C-B>\"\<CR>", 'xt')
1419 call assert_equal('"g/a/chistory', @:)
1420 call feedkeys(":g/a\\/chist\t\<C-B>\"\<CR>", 'xt')
1421 call assert_equal("\"g/a\\/chist\t", @:)
1422
Bram Moolenaar9c929712020-09-07 22:05:28 +02001423 " use <Esc> as the 'wildchar' for completion
1424 set wildchar=<Esc>
1425 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
1426 call assert_equal('"g/a\xb/clearjumps', @:)
1427 " pressing <esc> twice should cancel the command
1428 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
1429 call assert_equal('"g/a\xb/clearjumps', @:)
1430 set wildchar&
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001431
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001432 if has('unix')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001433 " should be able to complete a file name that starts with a '~'.
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001434 call writefile([], '~Xtest')
1435 call feedkeys(":e \\~X\<Tab>\<C-B>\"\<CR>", 'xt')
1436 call assert_equal('"e \~Xtest', @:)
1437 call delete('~Xtest')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001438
1439 " should be able to complete a file name that has a '*'
1440 call writefile([], 'Xx*Yy')
1441 call feedkeys(":e Xx\*\<Tab>\<C-B>\"\<CR>", 'xt')
1442 call assert_equal('"e Xx\*Yy', @:)
1443 call delete('Xx*Yy')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001444
1445 " use a literal star
1446 call feedkeys(":e \\*\<Tab>\<C-B>\"\<CR>", 'xt')
1447 call assert_equal('"e \*', @:)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001448 endif
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001449
1450 call feedkeys(":py3f\<Tab>\<C-B>\"\<CR>", 'xt')
1451 call assert_equal('"py3file', @:)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001452endfunc
1453
zeertzjq094dd152023-06-15 22:51:57 +01001454" Test that expanding a pattern doesn't interfere with cmdline completion.
1455func Test_expand_during_cmdline_completion()
1456 func ExpandStuff()
1457 badd <script>:p:h/README.*
1458 call assert_equal(expand('<script>:p:h') .. '/README.txt', bufname('$'))
1459 $bwipe
1460 call assert_equal('README.txt', expand('README.*'))
1461 call assert_equal(['README.txt'], getcompletion('README.*', 'file'))
1462 endfunc
1463 augroup test_CmdlineChanged
1464 autocmd!
1465 autocmd CmdlineChanged * call ExpandStuff()
1466 augroup END
1467
1468 call feedkeys(":sign \<Tab>\<Tab>\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
1469 call assert_equal('"sign place', @:)
1470
1471 augroup test_CmdlineChanged
1472 au!
1473 augroup END
1474 augroup! test_CmdlineChanged
1475 delfunc ExpandStuff
1476endfunc
1477
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001478" Test for 'wildignorecase'
1479func Test_cmdline_wildignorecase()
1480 CheckUnix
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001481 call writefile([], 'XTEST', 'D')
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001482 set wildignorecase
1483 call feedkeys(":e xt\<Tab>\<C-B>\"\<CR>", 'xt')
1484 call assert_equal('"e XTEST', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001485 call assert_equal(['XTEST'], getcompletion('xt', 'file'))
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001486 let g:Sline = ''
1487 call feedkeys(":e xt\<C-d>\<F4>\<C-B>\"\<CR>", 'xt')
1488 call assert_equal('"e xt', @:)
1489 call assert_equal('XTEST', g:Sline)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001490 set wildignorecase&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001491endfunc
1492
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001493func Test_cmdline_write_alternatefile()
1494 new
1495 call setline('.', ['one', 'two'])
1496 f foo.txt
1497 new
1498 f #-A
1499 call assert_equal('foo.txt-A', expand('%'))
1500 f #<-B.txt
1501 call assert_equal('foo-B.txt', expand('%'))
1502 f %<
1503 call assert_equal('foo-B', expand('%'))
1504 new
Bram Moolenaare2e40752020-09-04 21:18:46 +02001505 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001506 bw!
1507 f foo-B.txt
1508 f %<-A
1509 call assert_equal('foo-B-A', expand('%'))
1510 bw!
1511 bw!
1512endfunc
1513
Bram Moolenaarf5724372022-09-02 19:45:15 +01001514func Test_cmdline_expand_cur_alt_file()
1515 enew
1516 file http://some.com/file.txt
1517 call feedkeys(":e %\<Tab>\<C-B>\"\<CR>", 'xt')
1518 call assert_equal('"e http://some.com/file.txt', @:)
1519 edit another
1520 call feedkeys(":e #\<Tab>\<C-B>\"\<CR>", 'xt')
1521 call assert_equal('"e http://some.com/file.txt', @:)
1522 bwipe
1523 bwipe http://some.com/file.txt
1524endfunc
1525
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001526" using a leading backslash here
1527set cpo+=C
1528
1529func Test_cmdline_search_range()
1530 new
1531 call setline(1, ['a', 'b', 'c', 'd'])
1532 /d
1533 1,\/s/b/B/
1534 call assert_equal('B', getline(2))
1535
1536 /a
1537 $
1538 \?,4s/c/C/
1539 call assert_equal('C', getline(3))
1540
1541 call setline(1, ['a', 'b', 'c', 'd'])
1542 %s/c/c/
1543 1,\&s/b/B/
1544 call assert_equal('B', getline(2))
1545
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001546 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001547 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001548
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001549 bwipe!
1550endfunc
1551
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001552" Test for the tick mark (') in an excmd range
1553func Test_tick_mark_in_range()
1554 " If only the tick is passed as a range and no command is specified, there
1555 " should not be an error
1556 call feedkeys(":'\<CR>", 'xt')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001557 call assert_equal("'", @:)
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001558 call assert_fails("',print", 'E78:')
1559endfunc
1560
1561" Test for using a line number followed by a search pattern as range
1562func Test_lnum_and_pattern_as_range()
1563 new
1564 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
1565 let @" = ''
1566 2/foo/yank
1567 call assert_equal("foo 3\n", @")
1568 call assert_equal(1, line('.'))
1569 close!
1570endfunc
1571
Bram Moolenaar65189a12017-02-06 22:22:17 +01001572" Tests for getcmdline(), getcmdpos() and getcmdtype()
1573func Check_cmdline(cmdtype)
1574 call assert_equal('MyCmd a', getcmdline())
1575 call assert_equal(8, getcmdpos())
1576 call assert_equal(a:cmdtype, getcmdtype())
1577 return ''
1578endfunc
1579
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001580set cpo&
1581
Bram Moolenaar65189a12017-02-06 22:22:17 +01001582func Test_getcmdtype()
1583 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
1584
1585 let cmdtype = ''
1586 debuggreedy
1587 call feedkeys(":debug echo 'test'\<CR>", "t")
1588 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
1589 call feedkeys("cont\<CR>", "xt")
1590 0debuggreedy
1591 call assert_equal('>', cmdtype)
1592
1593 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
1594 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
1595
1596 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +01001597 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +01001598
1599 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
1600
1601 cnoremap <expr> <F6> Check_cmdline('=')
1602 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
1603 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001604
1605 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +01001606endfunc
1607
Bram Moolenaar52604f22017-04-07 16:17:39 +02001608func Test_verbosefile()
1609 set verbosefile=Xlog
1610 echomsg 'foo'
1611 echomsg 'bar'
1612 set verbosefile=
1613 let log = readfile('Xlog')
1614 call assert_match("foo\nbar", join(log, "\n"))
1615 call delete('Xlog')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001616
1617 call mkdir('Xdir', 'D')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001618 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaar52604f22017-04-07 16:17:39 +02001619endfunc
1620
Bram Moolenaar4facea32019-10-12 20:17:40 +02001621func Test_verbose_option()
1622 CheckScreendump
1623
1624 let lines =<< trim [SCRIPT]
1625 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1626 call feedkeys("\r", 't') " for the hit-enter prompt
1627 set verbose=20
1628 [SCRIPT]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001629 call writefile(lines, 'XTest_verbose', 'D')
Bram Moolenaar4facea32019-10-12 20:17:40 +02001630
1631 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001632 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001633 call term_sendkeys(buf, ":DoSomething\<CR>")
1634 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1635
1636 " clean up
1637 call StopVimInTerminal(buf)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001638endfunc
1639
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001640func Test_setcmdpos()
1641 func InsertTextAtPos(text, pos)
1642 call assert_equal(0, setcmdpos(a:pos))
1643 return a:text
1644 endfunc
1645
1646 " setcmdpos() with position in the middle of the command line.
1647 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1648 call assert_equal('"1ab2', @:)
1649
1650 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1651 call assert_equal('"1b2a', @:)
1652
1653 " setcmdpos() with position beyond the end of the command line.
1654 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1655 call assert_equal('"12ab', @:)
1656
1657 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001658 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001659endfunc
1660
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001661func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001662 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001663 let encoding_save = &encoding
1664
1665 for e in encodings
1666 exe 'set encoding=' . e
1667
1668 " Test overstrike in the middle of the command line.
1669 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001670 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001671
1672 " Test overstrike going beyond end of command line.
1673 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001674 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001675
1676 " Test toggling insert/overstrike a few times.
1677 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001678 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001679 endfor
1680
Bram Moolenaar30276f22019-01-24 17:59:39 +01001681 " Test overstrike with multi-byte characters.
1682 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001683 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001684
1685 let &encoding = encoding_save
1686endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001687
Bram Moolenaar52410572019-10-27 05:12:45 +01001688func Test_buffers_lastused()
1689 " check that buffers are sorted by time when wildmode has lastused
1690 call test_settime(1550020000) " middle
1691 edit bufa
1692 enew
1693 call test_settime(1550030000) " newest
1694 edit bufb
1695 enew
1696 call test_settime(1550010000) " oldest
1697 edit bufc
1698 enew
1699 call test_settime(0)
1700 enew
1701
1702 call assert_equal(['bufa', 'bufb', 'bufc'],
1703 \ getcompletion('', 'buffer'))
1704
1705 let save_wildmode = &wildmode
1706 set wildmode=full:lastused
1707
1708 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1709 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1710 call assert_equal('b bufb', X)
1711 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1712 call assert_equal('b bufa', X)
1713 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1714 call assert_equal('b bufc', X)
1715 enew
1716
1717 edit other
1718 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1719 call assert_equal('b bufb', X)
1720 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1721 call assert_equal('b bufa', X)
1722 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1723 call assert_equal('b bufc', X)
1724 enew
1725
1726 let &wildmode = save_wildmode
1727
1728 bwipeout bufa
1729 bwipeout bufb
1730 bwipeout bufc
1731endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001732
Bram Moolenaar479950f2020-01-19 15:45:17 +01001733func Test_cmdlineclear_tabenter()
1734 CheckScreendump
1735
1736 let lines =<< trim [SCRIPT]
1737 call setline(1, range(30))
1738 [SCRIPT]
1739
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001740 call writefile(lines, 'XtestCmdlineClearTabenter', 'D')
Bram Moolenaar479950f2020-01-19 15:45:17 +01001741 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001742 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001743 " in one tab make the command line higher with CTRL-W -
1744 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1745 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1746
1747 call StopVimInTerminal(buf)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001748endfunc
1749
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001750" Test for expanding special keywords in cmdline
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001751func Test_cmdline_expand_special()
1752 %bwipe!
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02001753 call assert_fails('e #', 'E194:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001754 call assert_fails('e <afile>', 'E495:')
1755 call assert_fails('e <abuf>', 'E496:')
1756 call assert_fails('e <amatch>', 'E497:')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001757
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001758 call writefile([], 'Xfile.cpp', 'D')
1759 call writefile([], 'Xfile.java', 'D')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001760 new Xfile.cpp
1761 call feedkeys(":e %:r\<C-A>\<C-B>\"\<CR>", 'xt')
1762 call assert_equal('"e Xfile.cpp Xfile.java', @:)
1763 close
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001764endfunc
1765
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001766" Test for backtick expression in the command line
1767func Test_cmd_backtick()
1768 %argd
1769 argadd `=['a', 'b', 'c']`
1770 call assert_equal(['a', 'b', 'c'], argv())
1771 %argd
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001772
1773 argadd `echo abc def`
1774 call assert_equal(['abc def'], argv())
1775 %argd
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001776endfunc
1777
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001778" Test for the :! command
1779func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001780 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001781
1782 let lines =<< trim [SCRIPT]
1783 " Test for no previous command
1784 call assert_fails('!!', 'E34:')
1785 set nomore
1786 " Test for cmdline expansion with :!
1787 call setline(1, 'foo!')
1788 silent !echo <cWORD> > Xfile.out
1789 call assert_equal(['foo!'], readfile('Xfile.out'))
1790 " Test for using previous command
1791 silent !echo \! !
1792 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1793 call writefile(v:errors, 'Xresult')
1794 call delete('Xfile.out')
1795 qall!
1796 [SCRIPT]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001797 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001798 if RunVim([], [], '--clean -S Xscript')
1799 call assert_equal([], readfile('Xresult'))
1800 endif
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001801 call delete('Xresult')
1802endfunc
1803
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001804" Test error: "E135: *Filter* Autocommands must not change current buffer"
1805func Test_cmd_bang_E135()
1806 new
1807 call setline(1, ['a', 'b', 'c', 'd'])
1808 augroup test_cmd_filter_E135
1809 au!
1810 autocmd FilterReadPost * help
1811 augroup END
1812 call assert_fails('2,3!echo "x"', 'E135:')
1813
1814 augroup test_cmd_filter_E135
1815 au!
1816 augroup END
zeertzjq094dd152023-06-15 22:51:57 +01001817 augroup! test_cmd_filter_E135
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001818 %bwipe!
1819endfunc
1820
shane.xb.qian4e7590e2022-11-08 21:40:04 +00001821func Test_cmd_bang_args()
1822 new
1823 :.!
1824 call assert_equal(0, v:shell_error)
1825
1826 " Note that below there is one space char after the '!'. This caused a
1827 " shell error in the past, see https://github.com/vim/vim/issues/11495.
1828 :.!
1829 call assert_equal(0, v:shell_error)
1830 bwipe!
1831
1832 CheckUnix
1833 :.!pwd
1834 call assert_equal(0, v:shell_error)
1835 :.! pwd
1836 call assert_equal(0, v:shell_error)
1837
1838 " Note there is one space after 'pwd'.
1839 :.! pwd
1840 call assert_equal(0, v:shell_error)
1841
1842 " Note there are two spaces after 'pwd'.
1843 :.! pwd
1844 call assert_equal(0, v:shell_error)
1845 :.!ls ~
1846 call assert_equal(0, v:shell_error)
1847
1848 " Note there is one space char after '~'.
1849 :.!ls ~
1850 call assert_equal(0, v:shell_error)
1851
1852 " Note there are two spaces after '~'.
1853 :.!ls ~
1854 call assert_equal(0, v:shell_error)
1855
1856 :.!echo "foo"
1857 call assert_equal(getline('.'), "foo")
1858 :.!echo "foo "
1859 call assert_equal(getline('.'), "foo ")
1860 :.!echo " foo "
1861 call assert_equal(getline('.'), " foo ")
1862 :.!echo " foo "
1863 call assert_equal(getline('.'), " foo ")
1864
1865 %bwipe!
1866endfunc
1867
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001868" Test for using ~ for home directory in cmdline completion matches
1869func Test_cmdline_expand_home()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001870 call mkdir('Xexpdir', 'R')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001871 call writefile([], 'Xexpdir/Xfile1')
1872 call writefile([], 'Xexpdir/Xfile2')
1873 cd Xexpdir
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001874 let save_HOME = $HOME
1875 let $HOME = getcwd()
1876 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1877 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1878 let $HOME = save_HOME
1879 cd ..
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001880endfunc
1881
Bram Moolenaar578fe942020-02-27 21:32:51 +01001882" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1883" or insert mode (when 'insertmode' is set)
1884func Test_cmdline_ctrl_g()
1885 new
1886 call setline(1, 'abc')
1887 call cursor(1, 3)
1888 " If command line is entered from insert mode, using C-\ C-G should back to
1889 " insert mode
1890 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1891 call assert_equal('abxyc', getline(1))
1892 call assert_equal(4, col('.'))
1893
1894 " If command line is entered in 'insertmode', using C-\ C-G should back to
1895 " 'insertmode'
1896 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1897 call assert_equal('ab12xyc', getline(1))
1898 close!
1899endfunc
1900
Bram Moolenaar578fe942020-02-27 21:32:51 +01001901" Test for 'wildmode'
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001902func Wildmode_tests()
Bram Moolenaar578fe942020-02-27 21:32:51 +01001903 func T(a, c, p)
1904 return "oneA\noneB\noneC"
1905 endfunc
1906 command -nargs=1 -complete=custom,T MyCmd
1907
Bram Moolenaar578fe942020-02-27 21:32:51 +01001908 set nowildmenu
1909 set wildmode=full,list
1910 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001911 call feedkeys(":MyCmd \t\t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001912 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001913 call assert_equal('"MyCmd oneA', @:)
1914
1915 set wildmode=longest,full
1916 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1917 call assert_equal('"MyCmd one', @:)
1918 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1919 call assert_equal('"MyCmd oneC', @:)
1920
1921 set wildmode=longest
1922 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1923 call assert_equal('"MyCmd one', @:)
1924
1925 set wildmode=list:longest
1926 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001927 call feedkeys(":MyCmd \t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001928 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001929 call assert_equal('"MyCmd one', @:)
1930
1931 set wildmode=""
1932 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1933 call assert_equal('"MyCmd oneA', @:)
1934
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001935 " Test for wildmode=longest with 'fileignorecase' set
1936 set wildmode=longest
1937 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001938 argadd AAA AAAA AAAAA
1939 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1940 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001941 set fileignorecase&
1942
1943 " Test for listing files with wildmode=list
1944 set wildmode=list
1945 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001946 call feedkeys(":b A\t\t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001947 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001948 call assert_equal('"b A', @:)
1949
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001950 " when using longest completion match, matches shorter than the argument
1951 " should be ignored (happens with :help)
1952 set wildmode=longest,full
1953 set wildmenu
1954 call feedkeys(":help a*\t\<C-B>\"\<CR>", 'xt')
1955 call assert_equal('"help a', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001956 " non existing file
1957 call feedkeys(":e a1b2y3z4\t\<C-B>\"\<CR>", 'xt')
1958 call assert_equal('"e a1b2y3z4', @:)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001959 set wildmenu&
1960
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001961 " Test for longest file name completion with 'fileignorecase'
1962 " On MS-Windows, file names are case insensitive.
1963 if has('unix')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001964 call writefile([], 'XTESTfoo', 'D')
1965 call writefile([], 'Xtestbar', 'D')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001966 set nofileignorecase
1967 call feedkeys(":e XT\<Tab>\<C-B>\"\<CR>", 'xt')
1968 call assert_equal('"e XTESTfoo', @:)
1969 call feedkeys(":e Xt\<Tab>\<C-B>\"\<CR>", 'xt')
1970 call assert_equal('"e Xtestbar', @:)
1971 set fileignorecase
1972 call feedkeys(":e XT\<Tab>\<C-B>\"\<CR>", 'xt')
1973 call assert_equal('"e Xtest', @:)
1974 call feedkeys(":e Xt\<Tab>\<C-B>\"\<CR>", 'xt')
1975 call assert_equal('"e Xtest', @:)
1976 set fileignorecase&
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001977 endif
1978
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001979 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001980 delcommand MyCmd
1981 delfunc T
Bram Moolenaar578fe942020-02-27 21:32:51 +01001982 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001983 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01001984endfunc
1985
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001986func Test_wildmode()
1987 " Test with utf-8 encoding
1988 call Wildmode_tests()
1989
1990 " Test with latin1 encoding
1991 let save_encoding = &encoding
1992 set encoding=latin1
1993 call Wildmode_tests()
1994 let &encoding = save_encoding
1995endfunc
1996
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00001997func Test_custom_complete_autoload()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001998 call mkdir('Xcustdir/autoload', 'pR')
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00001999 let save_rtp = &rtp
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002000 exe 'set rtp=' .. getcwd() .. '/Xcustdir'
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00002001 let lines =<< trim END
2002 func vim8#Complete(a, c, p)
2003 return "oneA\noneB\noneC"
2004 endfunc
2005 END
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002006 call writefile(lines, 'Xcustdir/autoload/vim8.vim')
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00002007
2008 command -nargs=1 -complete=custom,vim8#Complete MyCmd
2009 set nowildmenu
2010 set wildmode=full,list
2011 call feedkeys(":MyCmd \<C-A>\<C-B>\"\<CR>", 'xt')
2012 call assert_equal('"MyCmd oneA oneB oneC', @:)
2013
2014 let &rtp = save_rtp
2015 set wildmode& wildmenu&
2016 delcommand MyCmd
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00002017endfunc
2018
Bram Moolenaar578fe942020-02-27 21:32:51 +01002019" Test for interrupting the command-line completion
2020func Test_interrupt_compl()
2021 func F(lead, cmdl, p)
2022 if a:lead =~ 'tw'
2023 call interrupt()
2024 return
2025 endif
2026 return "one\ntwo\nthree"
2027 endfunc
2028 command -nargs=1 -complete=custom,F Tcmd
2029
2030 set nowildmenu
2031 set wildmode=full
2032 let interrupted = 0
2033 try
2034 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
2035 catch /^Vim:Interrupt$/
2036 let interrupted = 1
2037 endtry
2038 call assert_equal(1, interrupted)
2039
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002040 let interrupted = 0
2041 try
2042 call feedkeys(":Tcmd tw\<C-d>\<C-B>\"\<CR>", 'xt')
2043 catch /^Vim:Interrupt$/
2044 let interrupted = 1
2045 endtry
2046 call assert_equal(1, interrupted)
2047
Bram Moolenaar578fe942020-02-27 21:32:51 +01002048 delcommand Tcmd
2049 delfunc F
2050 set wildmode&
2051endfunc
2052
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01002053" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01002054func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01002055 let str = ":one two\<C-U>"
2056 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01002057 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01002058 let str ..= "\<Left>five\<Right>"
2059 let str ..= "\<Home>two "
2060 let str ..= "\<C-Left>one "
2061 let str ..= "\<C-Right> three"
2062 let str ..= "\<End>\<S-Left>four "
2063 let str ..= "\<S-Right> six"
2064 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
2065 call feedkeys(str, 'xt')
2066 call assert_equal("\"one two three four five six seven", @:)
2067endfunc
2068
2069" Test for moving the cursor on the / command line in 'rightleft' mode
2070func Test_cmdline_edit_rightleft()
2071 CheckFeature rightleft
2072 set rightleft
2073 set rightleftcmd=search
2074 let str = "/one two\<C-U>"
2075 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01002076 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01002077 let str ..= "\<Right>five\<Left>"
2078 let str ..= "\<Home>two "
2079 let str ..= "\<C-Right>one "
2080 let str ..= "\<C-Left> three"
2081 let str ..= "\<End>\<S-Right>four "
2082 let str ..= "\<S-Left> six"
2083 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
2084 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
2085 call assert_equal("\"one two three four five six seven", @/)
2086 set rightleftcmd&
2087 set rightleft&
2088endfunc
2089
2090" Test for using <C-\>e in the command line to evaluate an expression
2091func Test_cmdline_expr()
2092 " Evaluate an expression from the beginning of a command line
2093 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
2094 call assert_equal('"hello', @:)
2095
2096 " Use an invalid expression for <C-\>e
2097 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
2098
2099 " Insert literal <CTRL-\> in the command line
2100 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
2101 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01002102endfunc
2103
Bram Moolenaar6046ade2022-06-22 13:51:54 +01002104" This was making the insert position negative
2105func Test_cmdline_expr_register()
2106 exe "sil! norm! ?\<C-\>e0\<C-R>0\<Esc>?\<C-\>e0\<CR>"
2107endfunc
2108
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01002109" Test for 'imcmdline' and 'imsearch'
2110" This test doesn't actually test the input method functionality.
2111func Test_cmdline_inputmethod()
2112 new
2113 call setline(1, ['', 'abc', ''])
2114 set imcmdline
2115
2116 call feedkeys(":\"abc\<CR>", 'xt')
2117 call assert_equal("\"abc", @:)
2118 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
2119 call assert_equal("\"abc", @:)
2120 call feedkeys("/abc\<CR>", 'xt')
2121 call assert_equal([2, 1], [line('.'), col('.')])
2122 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
2123 call assert_equal([2, 1], [line('.'), col('.')])
2124
2125 set imsearch=2
2126 call cursor(1, 1)
2127 call feedkeys("/abc\<CR>", 'xt')
2128 call assert_equal([2, 1], [line('.'), col('.')])
2129 call cursor(1, 1)
2130 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
2131 call assert_equal([2, 1], [line('.'), col('.')])
2132 set imdisable
2133 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
2134 call assert_equal([2, 1], [line('.'), col('.')])
2135 set imdisable&
2136 set imsearch&
2137
2138 set imcmdline&
2139 %bwipe!
2140endfunc
2141
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01002142" Test for using CTRL-_ in the command line with 'allowrevins'
2143func Test_cmdline_revins()
2144 CheckNotMSWindows
2145 CheckFeature rightleft
2146 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
2147 call assert_equal("\"abc\<c-_>", @:)
2148 set allowrevins
2149 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
2150 call assert_equal('"abcñèæ', @:)
2151 set allowrevins&
2152endfunc
2153
2154" Test for typing UTF-8 composing characters in the command line
2155func Test_cmdline_composing_chars()
2156 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
2157 call assert_equal('"ゔ', @:)
2158endfunc
2159
Bram Moolenaar0e717042020-04-27 19:29:01 +02002160" test that ";" works to find a match at the start of the first line
2161func Test_zero_line_search()
2162 new
2163 call setline(1, ["1, pattern", "2, ", "3, pattern"])
2164 call cursor(1,1)
2165 0;/pattern/d
2166 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
2167 q!
2168endfunc
2169
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02002170func Test_read_shellcmd()
2171 CheckUnix
2172 if executable('ls')
2173 " There should be ls in the $PATH
2174 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
2175 call assert_match('^"r! .*\<ls\>', @:)
2176 endif
2177
2178 if executable('rm')
2179 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
2180 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
2181 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02002182
2183 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
2184 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
2185 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02002186 endif
2187endfunc
2188
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002189" Test for going up and down the directory tree using 'wildmenu'
2190func Test_wildmenu_dirstack()
2191 CheckUnix
2192 %bw!
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002193 call mkdir('Xwildmenu/dir2/dir3/dir4', 'pR')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002194 call writefile([], 'Xwildmenu/file1_1.txt')
2195 call writefile([], 'Xwildmenu/file1_2.txt')
2196 call writefile([], 'Xwildmenu/dir2/file2_1.txt')
2197 call writefile([], 'Xwildmenu/dir2/file2_2.txt')
2198 call writefile([], 'Xwildmenu/dir2/dir3/file3_1.txt')
2199 call writefile([], 'Xwildmenu/dir2/dir3/file3_2.txt')
2200 call writefile([], 'Xwildmenu/dir2/dir3/dir4/file4_1.txt')
2201 call writefile([], 'Xwildmenu/dir2/dir3/dir4/file4_2.txt')
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002202 set wildmenu
2203
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002204 cd Xwildmenu/dir2/dir3/dir4
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002205 call feedkeys(":e \<Tab>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002206 call assert_equal('"e file4_1.txt', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002207 call feedkeys(":e \<Tab>\<Up>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002208 call assert_equal('"e ../dir4/', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002209 call feedkeys(":e \<Tab>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002210 call assert_equal('"e ../../dir3/', @:)
2211 call feedkeys(":e \<Tab>\<Up>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
2212 call assert_equal('"e ../../../dir2/', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002213 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002214 call assert_equal('"e ../../dir3/dir4/', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002215 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002216 call assert_equal('"e ../../dir3/dir4/file4_1.txt', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002217 cd -
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002218 call feedkeys(":e Xwildmenu/\<Tab>\<Down>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
2219 call assert_equal('"e Xwildmenu/dir2/dir3/dir4/file4_1.txt', @:)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002220
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002221 set wildmenu&
2222endfunc
2223
obcat71c6f7a2021-05-13 20:23:10 +02002224" Test for recalling newer or older cmdline from history with <Up>, <Down>,
Yegappan Lakshmanan155b0882022-03-17 13:03:09 +00002225" <S-Up>, <S-Down>, <PageUp>, <PageDown>, <kPageUp>, <kPageDown>, <C-p>, or
2226" <C-n>.
obcat71c6f7a2021-05-13 20:23:10 +02002227func Test_recalling_cmdline()
2228 CheckFeature cmdline_hist
2229
2230 let g:cmdlines = []
2231 cnoremap <Plug>(save-cmdline) <Cmd>let g:cmdlines += [getcmdline()]<CR>
2232
2233 let histories = [
Yegappan Lakshmanan155b0882022-03-17 13:03:09 +00002234 \ #{name: 'cmd', enter: ':', exit: "\<Esc>"},
2235 \ #{name: 'search', enter: '/', exit: "\<Esc>"},
2236 \ #{name: 'expr', enter: ":\<C-r>=", exit: "\<Esc>\<Esc>"},
2237 \ #{name: 'input', enter: ":call input('')\<CR>", exit: "\<CR>"},
obcat71c6f7a2021-05-13 20:23:10 +02002238 "\ TODO: {'name': 'debug', ...}
2239 \]
2240 let keypairs = [
Yegappan Lakshmanan155b0882022-03-17 13:03:09 +00002241 \ #{older: "\<Up>", newer: "\<Down>", prefixmatch: v:true},
2242 \ #{older: "\<S-Up>", newer: "\<S-Down>", prefixmatch: v:false},
2243 \ #{older: "\<PageUp>", newer: "\<PageDown>", prefixmatch: v:false},
2244 \ #{older: "\<kPageUp>", newer: "\<kPageDown>", prefixmatch: v:false},
2245 \ #{older: "\<C-p>", newer: "\<C-n>", prefixmatch: v:false},
obcat71c6f7a2021-05-13 20:23:10 +02002246 \]
2247 let prefix = 'vi'
2248 for h in histories
2249 call histadd(h.name, 'vim')
2250 call histadd(h.name, 'virtue')
2251 call histadd(h.name, 'Virgo')
2252 call histadd(h.name, 'vogue')
2253 call histadd(h.name, 'emacs')
2254 for k in keypairs
2255 let g:cmdlines = []
2256 let keyseqs = h.enter
2257 \ .. prefix
2258 \ .. repeat(k.older .. "\<Plug>(save-cmdline)", 2)
2259 \ .. repeat(k.newer .. "\<Plug>(save-cmdline)", 2)
2260 \ .. h.exit
2261 call feedkeys(keyseqs, 'xt')
2262 call histdel(h.name, -1) " delete the history added by feedkeys above
2263 let expect = k.prefixmatch
2264 \ ? ['virtue', 'vim', 'virtue', prefix]
2265 \ : ['emacs', 'vogue', 'emacs', prefix]
2266 call assert_equal(expect, g:cmdlines)
2267 endfor
2268 endfor
2269
2270 unlet g:cmdlines
2271 cunmap <Plug>(save-cmdline)
2272endfunc
2273
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002274func Test_cmd_map_cmdlineChanged()
2275 let g:log = []
2276 cnoremap <F1> l<Cmd><CR>s
zeertzjq094dd152023-06-15 22:51:57 +01002277 augroup test_CmdlineChanged
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002278 autocmd!
2279 autocmd CmdlineChanged : let g:log += [getcmdline()]
2280 augroup END
2281
2282 call feedkeys(":\<F1>\<CR>", 'xt')
2283 call assert_equal(['l', 'ls'], g:log)
2284
Bram Moolenaar796139a2021-05-18 21:38:45 +02002285 let @b = 'b'
2286 cnoremap <F1> a<C-R>b
2287 let g:log = []
2288 call feedkeys(":\<F1>\<CR>", 'xt')
2289 call assert_equal(['a', 'ab'], g:log)
2290
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002291 unlet g:log
2292 cunmap <F1>
zeertzjq094dd152023-06-15 22:51:57 +01002293 augroup test_CmdlineChanged
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002294 autocmd!
2295 augroup END
zeertzjq094dd152023-06-15 22:51:57 +01002296 augroup! test_CmdlineChanged
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002297endfunc
2298
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002299" Test for the 'suffixes' option
2300func Test_suffixes_opt()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002301 call writefile([], 'Xsuffile', 'D')
2302 call writefile([], 'Xsuffile.c', 'D')
2303 call writefile([], 'Xsuffile.o', 'D')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002304 set suffixes=
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002305 call feedkeys(":e Xsuffi*\<C-A>\<C-B>\"\<CR>", 'xt')
2306 call assert_equal('"e Xsuffile Xsuffile.c Xsuffile.o', @:)
2307 call feedkeys(":e Xsuffi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2308 call assert_equal('"e Xsuffile.c', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002309 set suffixes=.c
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002310 call feedkeys(":e Xsuffi*\<C-A>\<C-B>\"\<CR>", 'xt')
2311 call assert_equal('"e Xsuffile Xsuffile.o Xsuffile.c', @:)
2312 call feedkeys(":e Xsuffi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2313 call assert_equal('"e Xsuffile.o', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002314 set suffixes=,,
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002315 call feedkeys(":e Xsuffi*\<C-A>\<C-B>\"\<CR>", 'xt')
2316 call assert_equal('"e Xsuffile.c Xsuffile.o Xsuffile', @:)
2317 call feedkeys(":e Xsuffi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2318 call assert_equal('"e Xsuffile.o', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002319 set suffixes&
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00002320 " Test for getcompletion() with different patterns
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002321 call assert_equal(['Xsuffile', 'Xsuffile.c', 'Xsuffile.o'], getcompletion('Xsuffile', 'file'))
2322 call assert_equal(['Xsuffile'], getcompletion('Xsuffile$', 'file'))
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002323endfunc
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002324
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002325" Test for using a popup menu for the command line completion matches
2326" (wildoptions=pum)
2327func Test_wildmenu_pum()
2328 CheckRunVimInTerminal
2329
2330 let commands =<< trim [CODE]
2331 set wildmenu
2332 set wildoptions=pum
2333 set shm+=I
2334 set noruler
2335 set noshowcmd
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002336
2337 func CmdCompl(a, b, c)
2338 return repeat(['aaaa'], 120)
2339 endfunc
2340 command -nargs=* -complete=customlist,CmdCompl Tcmd
Bram Moolenaar481acb12022-02-11 18:51:45 +00002341
2342 func MyStatusLine() abort
2343 return 'status'
2344 endfunc
2345 func SetupStatusline()
2346 set statusline=%!MyStatusLine()
2347 set laststatus=2
2348 endfunc
Bram Moolenaare4835bf2022-02-14 19:17:53 +00002349
2350 func MyTabLine()
2351 return 'my tab line'
2352 endfunc
2353 func SetupTabline()
2354 set statusline=
2355 set tabline=%!MyTabLine()
2356 set showtabline=2
2357 endfunc
Bram Moolenaar5c52be42022-02-27 14:28:31 +00002358
2359 func DoFeedKeys()
2360 let &wildcharm = char2nr("\t")
2361 call feedkeys(":edit $VIMRUNTIME/\<Tab>\<Left>\<C-U>ab\<Tab>")
2362 endfunc
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002363 [CODE]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002364 call writefile(commands, 'Xtest', 'D')
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002365
2366 let buf = RunVimInTerminal('-S Xtest', #{rows: 10})
2367
2368 call term_sendkeys(buf, ":sign \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002369 call VerifyScreenDump(buf, 'Test_wildmenu_pum_01', {})
2370
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002371 " going down the popup menu using <Down>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002372 call term_sendkeys(buf, "\<Down>\<Down>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002373 call VerifyScreenDump(buf, 'Test_wildmenu_pum_02', {})
2374
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002375 " going down the popup menu using <C-N>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002376 call term_sendkeys(buf, "\<C-N>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002377 call VerifyScreenDump(buf, 'Test_wildmenu_pum_03', {})
2378
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002379 " going up the popup menu using <C-P>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002380 call term_sendkeys(buf, "\<C-P>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002381 call VerifyScreenDump(buf, 'Test_wildmenu_pum_04', {})
2382
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002383 " going up the popup menu using <Up>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002384 call term_sendkeys(buf, "\<Up>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002385 call VerifyScreenDump(buf, 'Test_wildmenu_pum_05', {})
2386
2387 " pressing <C-E> should end completion and go back to the original match
2388 call term_sendkeys(buf, "\<C-E>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002389 call VerifyScreenDump(buf, 'Test_wildmenu_pum_06', {})
2390
2391 " pressing <C-Y> should select the current match and end completion
2392 call term_sendkeys(buf, "\<Tab>\<C-P>\<C-P>\<C-Y>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002393 call VerifyScreenDump(buf, 'Test_wildmenu_pum_07', {})
2394
2395 " With 'wildmode' set to 'longest,full', completing a match should display
2396 " the longest match, the wildmenu should not be displayed.
2397 call term_sendkeys(buf, ":\<C-U>set wildmode=longest,full\<CR>")
2398 call TermWait(buf)
2399 call term_sendkeys(buf, ":sign u\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002400 call VerifyScreenDump(buf, 'Test_wildmenu_pum_08', {})
2401
2402 " pressing <Tab> should display the wildmenu
2403 call term_sendkeys(buf, "\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002404 call VerifyScreenDump(buf, 'Test_wildmenu_pum_09', {})
2405
2406 " pressing <Tab> second time should select the next entry in the menu
2407 call term_sendkeys(buf, "\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002408 call VerifyScreenDump(buf, 'Test_wildmenu_pum_10', {})
2409
2410 call term_sendkeys(buf, ":\<C-U>set wildmode=full\<CR>")
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002411 " showing popup menu in different columns in the cmdline
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002412 call term_sendkeys(buf, ":sign define \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002413 call VerifyScreenDump(buf, 'Test_wildmenu_pum_11', {})
2414
2415 call term_sendkeys(buf, " \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002416 call VerifyScreenDump(buf, 'Test_wildmenu_pum_12', {})
2417
2418 call term_sendkeys(buf, " \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002419 call VerifyScreenDump(buf, 'Test_wildmenu_pum_13', {})
2420
2421 " Directory name completion
Dominique Pellefebe1382022-09-14 12:51:49 +01002422 call mkdir('Xnamedir/XdirA/XdirB', 'pR')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002423 call writefile([], 'Xnamedir/XfileA')
2424 call writefile([], 'Xnamedir/XdirA/XfileB')
2425 call writefile([], 'Xnamedir/XdirA/XdirB/XfileC')
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002426
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002427 call term_sendkeys(buf, "\<C-U>e Xnamedi\<Tab>\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002428 call VerifyScreenDump(buf, 'Test_wildmenu_pum_14', {})
2429
2430 " Pressing <Right> on a directory name should go into that directory
2431 call term_sendkeys(buf, "\<Right>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002432 call VerifyScreenDump(buf, 'Test_wildmenu_pum_15', {})
2433
2434 " Pressing <Left> on a directory name should go to the parent directory
2435 call term_sendkeys(buf, "\<Left>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002436 call VerifyScreenDump(buf, 'Test_wildmenu_pum_16', {})
2437
2438 " Pressing <C-A> when the popup menu is displayed should list all the
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002439 " matches but the popup menu should still remain
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002440 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-A>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002441 call VerifyScreenDump(buf, 'Test_wildmenu_pum_17', {})
2442
2443 " Pressing <C-D> when the popup menu is displayed should remove the popup
2444 " menu
2445 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-D>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002446 call VerifyScreenDump(buf, 'Test_wildmenu_pum_18', {})
2447
2448 " Pressing <S-Tab> should open the popup menu with the last entry selected
2449 call term_sendkeys(buf, "\<C-U>\<CR>:sign \<S-Tab>\<C-P>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002450 call VerifyScreenDump(buf, 'Test_wildmenu_pum_19', {})
2451
2452 " Pressing <Esc> should close the popup menu and cancel the cmd line
2453 call term_sendkeys(buf, "\<C-U>\<CR>:sign \<Tab>\<Esc>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002454 call VerifyScreenDump(buf, 'Test_wildmenu_pum_20', {})
2455
2456 " Typing a character when the popup is open, should close the popup
2457 call term_sendkeys(buf, ":sign \<Tab>x")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002458 call VerifyScreenDump(buf, 'Test_wildmenu_pum_21', {})
2459
2460 " When the popup is open, entering the cmdline window should close the popup
2461 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-F>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002462 call VerifyScreenDump(buf, 'Test_wildmenu_pum_22', {})
2463 call term_sendkeys(buf, ":q\<CR>")
2464
2465 " After the last popup menu item, <C-N> should show the original string
2466 call term_sendkeys(buf, ":sign u\<Tab>\<C-N>\<C-N>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002467 call VerifyScreenDump(buf, 'Test_wildmenu_pum_23', {})
2468
2469 " Use the popup menu for the command name
2470 call term_sendkeys(buf, "\<C-U>bu\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002471 call VerifyScreenDump(buf, 'Test_wildmenu_pum_24', {})
2472
2473 " Pressing the left arrow should remove the popup menu
2474 call term_sendkeys(buf, "\<Left>\<Left>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002475 call VerifyScreenDump(buf, 'Test_wildmenu_pum_25', {})
2476
2477 " Pressing <BS> should remove the popup menu and erase the last character
2478 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<BS>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002479 call VerifyScreenDump(buf, 'Test_wildmenu_pum_26', {})
2480
2481 " Pressing <C-W> should remove the popup menu and erase the previous word
2482 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<C-W>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002483 call VerifyScreenDump(buf, 'Test_wildmenu_pum_27', {})
2484
2485 " Pressing <C-U> should remove the popup menu and erase the entire line
2486 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<C-U>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002487 call VerifyScreenDump(buf, 'Test_wildmenu_pum_28', {})
2488
2489 " Using <C-E> to cancel the popup menu and then pressing <Up> should recall
2490 " the cmdline from history
2491 call term_sendkeys(buf, "sign xyz\<Esc>:sign \<Tab>\<C-E>\<Up>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002492 call VerifyScreenDump(buf, 'Test_wildmenu_pum_29', {})
2493
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002494 " Check "list" still works
2495 call term_sendkeys(buf, "\<C-U>set wildmode=longest,list\<CR>")
2496 call term_sendkeys(buf, ":cn\<Tab>")
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002497 call VerifyScreenDump(buf, 'Test_wildmenu_pum_30', {})
2498 call term_sendkeys(buf, "s")
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002499 call VerifyScreenDump(buf, 'Test_wildmenu_pum_31', {})
2500
rbtnn68cc2b82022-02-09 11:55:47 +00002501 " Tests a directory name contained full-width characters.
Dominique Pellefebe1382022-09-14 12:51:49 +01002502 call mkdir('Xnamedir/あいう', 'p')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002503 call writefile([], 'Xnamedir/あいう/abc')
2504 call writefile([], 'Xnamedir/あいう/xyz')
2505 call writefile([], 'Xnamedir/あいう/123')
rbtnn68cc2b82022-02-09 11:55:47 +00002506
2507 call term_sendkeys(buf, "\<C-U>set wildmode&\<CR>")
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002508 call term_sendkeys(buf, ":\<C-U>e Xnamedir/あいう/\<Tab>")
rbtnn68cc2b82022-02-09 11:55:47 +00002509 call VerifyScreenDump(buf, 'Test_wildmenu_pum_32', {})
2510
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002511 " Pressing <C-A> when the popup menu is displayed should list all the
2512 " matches and pressing a key after that should remove the popup menu
2513 call term_sendkeys(buf, "\<C-U>set wildmode=full\<CR>")
2514 call term_sendkeys(buf, ":sign \<Tab>\<C-A>x")
2515 call VerifyScreenDump(buf, 'Test_wildmenu_pum_33', {})
2516
2517 " Pressing <C-A> when the popup menu is displayed should list all the
2518 " matches and pressing <Left> after that should move the cursor
2519 call term_sendkeys(buf, "\<C-U>abc\<Esc>")
2520 call term_sendkeys(buf, ":sign \<Tab>\<C-A>\<Left>")
2521 call VerifyScreenDump(buf, 'Test_wildmenu_pum_34', {})
2522
2523 " When <C-A> displays a lot of matches (screen scrolls), all the matches
2524 " should be displayed correctly on the screen.
2525 call term_sendkeys(buf, "\<End>\<C-U>Tcmd \<Tab>\<C-A>\<Left>\<Left>")
2526 call VerifyScreenDump(buf, 'Test_wildmenu_pum_35', {})
2527
2528 " After using <C-A> to expand all the filename matches, pressing <Up>
2529 " should not open the popup menu again.
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002530 call term_sendkeys(buf, "\<C-E>\<C-U>:cd Xnamedir/XdirA\<CR>")
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002531 call term_sendkeys(buf, ":e \<Tab>\<C-A>\<Up>")
2532 call VerifyScreenDump(buf, 'Test_wildmenu_pum_36', {})
2533 call term_sendkeys(buf, "\<C-E>\<C-U>:cd -\<CR>")
2534
2535 " After using <C-A> to expand all the matches, pressing <S-Tab> used to
2536 " crash Vim
2537 call term_sendkeys(buf, ":sign \<Tab>\<C-A>\<S-Tab>")
2538 call VerifyScreenDump(buf, 'Test_wildmenu_pum_37', {})
2539
Bram Moolenaar414acd32022-02-10 21:09:45 +00002540 " After removing the pum the command line is redrawn
2541 call term_sendkeys(buf, ":edit foo\<CR>")
2542 call term_sendkeys(buf, ":edit bar\<CR>")
2543 call term_sendkeys(buf, ":ls\<CR>")
2544 call term_sendkeys(buf, ":com\<Tab> ")
2545 call VerifyScreenDump(buf, 'Test_wildmenu_pum_38', {})
Bram Moolenaar481acb12022-02-11 18:51:45 +00002546 call term_sendkeys(buf, "\<C-U>\<CR>")
2547
2548 " Esc still works to abort the command when 'statusline' is set
2549 call term_sendkeys(buf, ":call SetupStatusline()\<CR>")
2550 call term_sendkeys(buf, ":si\<Tab>")
2551 call term_sendkeys(buf, "\<Esc>")
2552 call VerifyScreenDump(buf, 'Test_wildmenu_pum_39', {})
Bram Moolenaar414acd32022-02-10 21:09:45 +00002553
Bram Moolenaare4835bf2022-02-14 19:17:53 +00002554 " Esc still works to abort the command when 'tabline' is set
2555 call term_sendkeys(buf, ":call SetupTabline()\<CR>")
2556 call term_sendkeys(buf, ":si\<Tab>")
2557 call term_sendkeys(buf, "\<Esc>")
2558 call VerifyScreenDump(buf, 'Test_wildmenu_pum_40', {})
2559
Bram Moolenaar5c52be42022-02-27 14:28:31 +00002560 " popup is cleared also when 'lazyredraw' is set
2561 call term_sendkeys(buf, ":set showtabline=1 laststatus=1 lazyredraw\<CR>")
2562 call term_sendkeys(buf, ":call DoFeedKeys()\<CR>")
2563 call VerifyScreenDump(buf, 'Test_wildmenu_pum_41', {})
2564 call term_sendkeys(buf, "\<Esc>")
2565
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00002566 " Pressing <PageDown> should scroll the menu downward
2567 call term_sendkeys(buf, ":sign \<Tab>\<PageDown>")
2568 call VerifyScreenDump(buf, 'Test_wildmenu_pum_42', {})
2569 call term_sendkeys(buf, "\<PageDown>")
2570 call VerifyScreenDump(buf, 'Test_wildmenu_pum_43', {})
2571 call term_sendkeys(buf, "\<PageDown>")
2572 call VerifyScreenDump(buf, 'Test_wildmenu_pum_44', {})
2573 call term_sendkeys(buf, "\<PageDown>")
2574 call VerifyScreenDump(buf, 'Test_wildmenu_pum_45', {})
2575 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<Down>\<Down>\<PageDown>")
2576 call VerifyScreenDump(buf, 'Test_wildmenu_pum_46', {})
2577
2578 " Pressing <PageUp> should scroll the menu upward
2579 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<PageUp>")
2580 call VerifyScreenDump(buf, 'Test_wildmenu_pum_47', {})
2581 call term_sendkeys(buf, "\<PageUp>")
2582 call VerifyScreenDump(buf, 'Test_wildmenu_pum_48', {})
2583 call term_sendkeys(buf, "\<PageUp>")
2584 call VerifyScreenDump(buf, 'Test_wildmenu_pum_49', {})
2585 call term_sendkeys(buf, "\<PageUp>")
2586 call VerifyScreenDump(buf, 'Test_wildmenu_pum_50', {})
2587
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002588 call term_sendkeys(buf, "\<C-U>\<CR>")
2589 call StopVimInTerminal(buf)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002590endfunc
2591
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002592" Test for wildmenumode() with the cmdline popup menu
2593func Test_wildmenumode_with_pum()
2594 set wildmenu
2595 set wildoptions=pum
2596 cnoremap <expr> <F2> wildmenumode()
2597 call feedkeys(":sign \<Tab>\<F2>\<F2>\<C-B>\"\<CR>", 'xt')
2598 call assert_equal('"sign define10', @:)
2599 call feedkeys(":sign \<Tab>\<C-A>\<F2>\<C-B>\"\<CR>", 'xt')
2600 call assert_equal('"sign define jump list place undefine unplace0', @:)
2601 call feedkeys(":sign \<Tab>\<C-E>\<F2>\<C-B>\"\<CR>", 'xt')
2602 call assert_equal('"sign 0', @:)
2603 call feedkeys(":sign \<Tab>\<C-Y>\<F2>\<C-B>\"\<CR>", 'xt')
2604 call assert_equal('"sign define0', @:)
2605 set nowildmenu wildoptions&
2606 cunmap <F2>
2607endfunc
2608
Bram Moolenaar11a57df2022-04-11 19:38:56 +01002609func Test_wildmenu_with_pum_foldexpr()
2610 CheckRunVimInTerminal
2611
2612 let lines =<< trim END
2613 call setline(1, ['folded one', 'folded two', 'some more text'])
2614 func MyFoldText()
2615 return 'foo'
2616 endfunc
2617 set foldtext=MyFoldText() wildoptions=pum
2618 normal ggzfj
2619 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002620 call writefile(lines, 'Xpumfold', 'D')
Bram Moolenaar11a57df2022-04-11 19:38:56 +01002621 let buf = RunVimInTerminal('-S Xpumfold', #{rows: 10})
2622 call term_sendkeys(buf, ":set\<Tab>")
2623 call VerifyScreenDump(buf, 'Test_wildmenu_with_pum_foldexpr_1', {})
2624
2625 call term_sendkeys(buf, "\<Esc>")
2626 call VerifyScreenDump(buf, 'Test_wildmenu_with_pum_foldexpr_2', {})
2627
2628 call StopVimInTerminal(buf)
Bram Moolenaar11a57df2022-04-11 19:38:56 +01002629endfunc
2630
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01002631" Test for opening the cmdline completion popup menu from the terminal window.
2632" The popup menu should be positioned correctly over the status line of the
2633" bottom-most window.
2634func Test_wildmenu_pum_from_terminal()
2635 CheckRunVimInTerminal
2636 let python = PythonProg()
2637 call CheckPython(python)
2638
2639 %bw!
2640 let cmds = ['set wildmenu wildoptions=pum']
2641 let pcmd = python .. ' -c "import sys; sys.stdout.write(sys.stdin.read())"'
2642 call add(cmds, "call term_start('" .. pcmd .. "')")
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002643 call writefile(cmds, 'Xtest', 'D')
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01002644 let buf = RunVimInTerminal('-S Xtest', #{rows: 10})
2645 call term_sendkeys(buf, "\r\r\r")
2646 call term_wait(buf)
2647 call term_sendkeys(buf, "\<C-W>:sign \<Tab>")
2648 call term_wait(buf)
2649 call VerifyScreenDump(buf, 'Test_wildmenu_pum_term_01', {})
2650 call term_wait(buf)
2651 call StopVimInTerminal(buf)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01002652endfunc
2653
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002654func Test_wildmenu_pum_odd_wildchar()
Bram Moolenaarb9603f62022-12-08 15:44:22 +00002655 CheckRunVimInTerminal
2656
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002657 " Test odd wildchar interactions with pum. Make sure they behave properly
2658 " and don't lead to memory corruption due to improperly cleaned up memory.
Bram Moolenaar038e6d22022-12-08 12:00:50 +00002659 let lines =<< trim END
2660 set wildoptions=pum
2661 set wildchar=<C-E>
2662 END
2663 call writefile(lines, 'XwildmenuTest', 'D')
2664 let buf = RunVimInTerminal('-S XwildmenuTest', #{rows: 10})
2665
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002666 call term_sendkeys(buf, ":\<C-E>")
2667 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
Bram Moolenaar038e6d22022-12-08 12:00:50 +00002668
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002669 " <C-E> being a wildchar takes priority over its original functionality
2670 call term_sendkeys(buf, "\<C-E>")
2671 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_2', {})
2672
2673 call term_sendkeys(buf, "\<Esc>")
2674 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
2675
2676 " Escape key can be wildchar too. Double-<Esc> is hard-coded to escape
2677 " command-line, and we need to make sure to clean up properly.
2678 call term_sendkeys(buf, ":set wildchar=<Esc>\<CR>")
2679 call term_sendkeys(buf, ":\<Esc>")
2680 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
2681
2682 call term_sendkeys(buf, "\<Esc>")
2683 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
2684
2685 " <C-\> can also be wildchar. <C-\><C-N> however will still escape cmdline
2686 " and we again need to make sure we clean up properly.
2687 call term_sendkeys(buf, ":set wildchar=<C-\\>\<CR>")
2688 call term_sendkeys(buf, ":\<C-\>\<C-\>")
2689 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
2690
2691 call term_sendkeys(buf, "\<C-N>")
2692 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
2693
2694 call StopVimInTerminal(buf)
Bram Moolenaar038e6d22022-12-08 12:00:50 +00002695endfunc
2696
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002697" Test for completion after a :substitute command followed by a pipe (|)
2698" character
2699func Test_cmdline_complete_substitute()
2700 call feedkeys(":s | \t\<C-B>\"\<CR>", 'xt')
2701 call assert_equal("\"s | \t", @:)
2702 call feedkeys(":s/ | \t\<C-B>\"\<CR>", 'xt')
2703 call assert_equal("\"s/ | \t", @:)
2704 call feedkeys(":s/one | \t\<C-B>\"\<CR>", 'xt')
2705 call assert_equal("\"s/one | \t", @:)
2706 call feedkeys(":s/one/ | \t\<C-B>\"\<CR>", 'xt')
2707 call assert_equal("\"s/one/ | \t", @:)
2708 call feedkeys(":s/one/two | \t\<C-B>\"\<CR>", 'xt')
2709 call assert_equal("\"s/one/two | \t", @:)
2710 call feedkeys(":s/one/two/ | chist\t\<C-B>\"\<CR>", 'xt')
2711 call assert_equal('"s/one/two/ | chistory', @:)
2712 call feedkeys(":s/one/two/g \t\<C-B>\"\<CR>", 'xt')
2713 call assert_equal("\"s/one/two/g \t", @:)
2714 call feedkeys(":s/one/two/g | chist\t\<C-B>\"\<CR>", 'xt')
2715 call assert_equal("\"s/one/two/g | chistory", @:)
2716 call feedkeys(":s/one/t\\/ | \t\<C-B>\"\<CR>", 'xt')
2717 call assert_equal("\"s/one/t\\/ | \t", @:)
2718 call feedkeys(":s/one/t\"o/ | chist\t\<C-B>\"\<CR>", 'xt')
2719 call assert_equal('"s/one/t"o/ | chistory', @:)
2720 call feedkeys(":s/one/t|o/ | chist\t\<C-B>\"\<CR>", 'xt')
2721 call assert_equal('"s/one/t|o/ | chistory', @:)
2722 call feedkeys(":&\t\<C-B>\"\<CR>", 'xt')
2723 call assert_equal("\"&\t", @:)
2724endfunc
2725
2726" Test for the :dlist command completion
2727func Test_cmdline_complete_dlist()
2728 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
2729 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
2730 call feedkeys(":dlist 10 /pat/ \t\<C-B>\"\<CR>", 'xt')
2731 call assert_equal("\"dlist 10 /pat/ \t", @:)
2732 call feedkeys(":dlist 10 /pa\\t/\t\<C-B>\"\<CR>", 'xt')
2733 call assert_equal("\"dlist 10 /pa\\t/\t", @:)
2734 call feedkeys(":dlist 10 /pat\\\t\<C-B>\"\<CR>", 'xt')
2735 call assert_equal("\"dlist 10 /pat\\\t", @:)
2736 call feedkeys(":dlist 10 /pat/ | chist\<Tab>\<C-B>\"\<CR>", 'xt')
2737 call assert_equal("\"dlist 10 /pat/ | chistory", @:)
2738endfunc
2739
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002740" argument list (only for :argdel) fuzzy completion
2741func Test_fuzzy_completion_arglist()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002742 argadd change.py count.py charge.py
2743 set wildoptions&
2744 call feedkeys(":argdel cge\<C-A>\<C-B>\"\<CR>", 'tx')
2745 call assert_equal('"argdel cge', @:)
2746 set wildoptions=fuzzy
2747 call feedkeys(":argdel cge\<C-A>\<C-B>\"\<CR>", 'tx')
2748 call assert_equal('"argdel change.py charge.py', @:)
2749 %argdelete
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002750 set wildoptions&
2751endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002752
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002753" autocmd group name fuzzy completion
2754func Test_fuzzy_completion_autocmd()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002755 set wildoptions&
2756 augroup MyFuzzyGroup
2757 augroup END
2758 call feedkeys(":augroup mfg\<Tab>\<C-B>\"\<CR>", 'tx')
2759 call assert_equal('"augroup mfg', @:)
2760 call feedkeys(":augroup My*p\<Tab>\<C-B>\"\<CR>", 'tx')
2761 call assert_equal('"augroup MyFuzzyGroup', @:)
2762 set wildoptions=fuzzy
2763 call feedkeys(":augroup mfg\<Tab>\<C-B>\"\<CR>", 'tx')
2764 call assert_equal('"augroup MyFuzzyGroup', @:)
2765 call feedkeys(":augroup My*p\<Tab>\<C-B>\"\<CR>", 'tx')
2766 call assert_equal('"augroup My*p', @:)
2767 augroup! MyFuzzyGroup
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002768 set wildoptions&
2769endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002770
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002771" buffer name fuzzy completion
2772func Test_fuzzy_completion_bufname()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002773 set wildoptions&
Bram Moolenaar5ac4b1a2022-08-06 10:28:19 +01002774 " Use a long name to reduce the risk of matching a random directory name
2775 edit SomeRandomFileWithLetters.txt
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002776 enew
Bram Moolenaar5ac4b1a2022-08-06 10:28:19 +01002777 call feedkeys(":b SRFWL\<Tab>\<C-B>\"\<CR>", 'tx')
2778 call assert_equal('"b SRFWL', @:)
2779 call feedkeys(":b S*FileWithLetters.txt\<Tab>\<C-B>\"\<CR>", 'tx')
2780 call assert_equal('"b SomeRandomFileWithLetters.txt', @:)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002781 set wildoptions=fuzzy
Bram Moolenaar5ac4b1a2022-08-06 10:28:19 +01002782 call feedkeys(":b SRFWL\<Tab>\<C-B>\"\<CR>", 'tx')
2783 call assert_equal('"b SomeRandomFileWithLetters.txt', @:)
2784 call feedkeys(":b S*FileWithLetters.txt\<Tab>\<C-B>\"\<CR>", 'tx')
2785 call assert_equal('"b S*FileWithLetters.txt', @:)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002786 %bw!
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002787 set wildoptions&
2788endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002789
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002790" buffer name (full path) fuzzy completion
2791func Test_fuzzy_completion_bufname_fullpath()
2792 CheckUnix
2793 set wildoptions&
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002794 call mkdir('Xcmd/Xstate/Xfile.js', 'pR')
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002795 edit Xcmd/Xstate/Xfile.js
2796 cd Xcmd/Xstate
2797 enew
2798 call feedkeys(":b CmdStateFile\<Tab>\<C-B>\"\<CR>", 'tx')
2799 call assert_equal('"b CmdStateFile', @:)
2800 set wildoptions=fuzzy
2801 call feedkeys(":b CmdStateFile\<Tab>\<C-B>\"\<CR>", 'tx')
2802 call assert_match('Xcmd/Xstate/Xfile.js$', @:)
2803 cd -
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002804 set wildoptions&
2805endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002806
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002807" :behave suboptions fuzzy completion
2808func Test_fuzzy_completion_behave()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002809 set wildoptions&
2810 call feedkeys(":behave xm\<Tab>\<C-B>\"\<CR>", 'tx')
2811 call assert_equal('"behave xm', @:)
2812 call feedkeys(":behave xt*m\<Tab>\<C-B>\"\<CR>", 'tx')
2813 call assert_equal('"behave xterm', @:)
2814 set wildoptions=fuzzy
2815 call feedkeys(":behave xm\<Tab>\<C-B>\"\<CR>", 'tx')
2816 call assert_equal('"behave xterm', @:)
2817 call feedkeys(":behave xt*m\<Tab>\<C-B>\"\<CR>", 'tx')
2818 call assert_equal('"behave xt*m', @:)
2819 let g:Sline = ''
2820 call feedkeys(":behave win\<C-D>\<F4>\<C-B>\"\<CR>", 'tx')
2821 call assert_equal('mswin', g:Sline)
2822 call assert_equal('"behave win', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002823 set wildoptions&
2824endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002825
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002826" " colorscheme name fuzzy completion - NOT supported
2827" func Test_fuzzy_completion_colorscheme()
2828" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002829
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002830" built-in command name fuzzy completion
2831func Test_fuzzy_completion_cmdname()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002832 set wildoptions&
2833 call feedkeys(":sbwin\<Tab>\<C-B>\"\<CR>", 'tx')
2834 call assert_equal('"sbwin', @:)
2835 call feedkeys(":sbr*d\<Tab>\<C-B>\"\<CR>", 'tx')
2836 call assert_equal('"sbrewind', @:)
2837 set wildoptions=fuzzy
2838 call feedkeys(":sbwin\<Tab>\<C-B>\"\<CR>", 'tx')
2839 call assert_equal('"sbrewind', @:)
2840 call feedkeys(":sbr*d\<Tab>\<C-B>\"\<CR>", 'tx')
2841 call assert_equal('"sbr*d', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002842 set wildoptions&
2843endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002844
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002845" " compiler name fuzzy completion - NOT supported
2846" func Test_fuzzy_completion_compiler()
2847" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002848
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002849" :cscope suboptions fuzzy completion
2850func Test_fuzzy_completion_cscope()
2851 CheckFeature cscope
2852 set wildoptions&
2853 call feedkeys(":cscope ret\<Tab>\<C-B>\"\<CR>", 'tx')
2854 call assert_equal('"cscope ret', @:)
2855 call feedkeys(":cscope re*t\<Tab>\<C-B>\"\<CR>", 'tx')
2856 call assert_equal('"cscope reset', @:)
2857 set wildoptions=fuzzy
2858 call feedkeys(":cscope ret\<Tab>\<C-B>\"\<CR>", 'tx')
2859 call assert_equal('"cscope reset', @:)
2860 call feedkeys(":cscope re*t\<Tab>\<C-B>\"\<CR>", 'tx')
2861 call assert_equal('"cscope re*t', @:)
2862 set wildoptions&
2863endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002864
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002865" :diffget/:diffput buffer name fuzzy completion
2866func Test_fuzzy_completion_diff()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002867 new SomeBuffer
2868 diffthis
2869 new OtherBuffer
2870 diffthis
2871 set wildoptions&
2872 call feedkeys(":diffget sbuf\<Tab>\<C-B>\"\<CR>", 'tx')
2873 call assert_equal('"diffget sbuf', @:)
2874 call feedkeys(":diffput sbuf\<Tab>\<C-B>\"\<CR>", 'tx')
2875 call assert_equal('"diffput sbuf', @:)
2876 set wildoptions=fuzzy
2877 call feedkeys(":diffget sbuf\<Tab>\<C-B>\"\<CR>", 'tx')
2878 call assert_equal('"diffget SomeBuffer', @:)
2879 call feedkeys(":diffput sbuf\<Tab>\<C-B>\"\<CR>", 'tx')
2880 call assert_equal('"diffput SomeBuffer', @:)
2881 %bw!
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002882 set wildoptions&
2883endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002884
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002885" " directory name fuzzy completion - NOT supported
2886" func Test_fuzzy_completion_dirname()
2887" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002888
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002889" environment variable name fuzzy completion
2890func Test_fuzzy_completion_env()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002891 set wildoptions&
2892 call feedkeys(":echo $VUT\<Tab>\<C-B>\"\<CR>", 'tx')
2893 call assert_equal('"echo $VUT', @:)
2894 set wildoptions=fuzzy
2895 call feedkeys(":echo $VUT\<Tab>\<C-B>\"\<CR>", 'tx')
2896 call assert_equal('"echo $VIMRUNTIME', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002897 set wildoptions&
2898endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002899
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002900" autocmd event fuzzy completion
2901func Test_fuzzy_completion_autocmd_event()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002902 set wildoptions&
2903 call feedkeys(":autocmd BWout\<Tab>\<C-B>\"\<CR>", 'tx')
2904 call assert_equal('"autocmd BWout', @:)
2905 set wildoptions=fuzzy
2906 call feedkeys(":autocmd BWout\<Tab>\<C-B>\"\<CR>", 'tx')
2907 call assert_equal('"autocmd BufWipeout', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002908 set wildoptions&
2909endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002910
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002911" vim expression fuzzy completion
2912func Test_fuzzy_completion_expr()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002913 let g:PerPlaceCount = 10
2914 set wildoptions&
2915 call feedkeys(":let c = ppc\<Tab>\<C-B>\"\<CR>", 'tx')
2916 call assert_equal('"let c = ppc', @:)
2917 set wildoptions=fuzzy
2918 call feedkeys(":let c = ppc\<Tab>\<C-B>\"\<CR>", 'tx')
2919 call assert_equal('"let c = PerPlaceCount', @:)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002920 set wildoptions&
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002921endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002922
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002923" " file name fuzzy completion - NOT supported
2924" func Test_fuzzy_completion_filename()
2925" endfunc
2926
2927" " files in path fuzzy completion - NOT supported
2928" func Test_fuzzy_completion_filesinpath()
2929" endfunc
2930
2931" " filetype name fuzzy completion - NOT supported
2932" func Test_fuzzy_completion_filetype()
2933" endfunc
2934
2935" user defined function name completion
2936func Test_fuzzy_completion_userdefined_func()
2937 set wildoptions&
2938 call feedkeys(":call Test_f_u_f\<Tab>\<C-B>\"\<CR>", 'tx')
2939 call assert_equal('"call Test_f_u_f', @:)
2940 set wildoptions=fuzzy
2941 call feedkeys(":call Test_f_u_f\<Tab>\<C-B>\"\<CR>", 'tx')
2942 call assert_equal('"call Test_fuzzy_completion_userdefined_func()', @:)
2943 set wildoptions&
2944endfunc
2945
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002946" <SNR> functions should be sorted to the end
2947func Test_fuzzy_completion_userdefined_snr_func()
2948 func s:Sendmail()
2949 endfunc
2950 func SendSomemail()
2951 endfunc
2952 func S1e2n3dmail()
2953 endfunc
2954 set wildoptions=fuzzy
2955 call feedkeys(":call sendmail\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +00002956 call assert_match('"call SendSomemail() S1e2n3dmail() <SNR>\d\+_Sendmail()', @:)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002957 set wildoptions&
2958 delfunc s:Sendmail
2959 delfunc SendSomemail
2960 delfunc S1e2n3dmail
2961endfunc
2962
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002963" user defined command name completion
2964func Test_fuzzy_completion_userdefined_cmd()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002965 set wildoptions&
2966 call feedkeys(":MsFeat\<Tab>\<C-B>\"\<CR>", 'tx')
2967 call assert_equal('"MsFeat', @:)
2968 set wildoptions=fuzzy
2969 call feedkeys(":MsFeat\<Tab>\<C-B>\"\<CR>", 'tx')
2970 call assert_equal('"MissingFeature', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002971 set wildoptions&
2972endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002973
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002974" " :help tag fuzzy completion - NOT supported
2975" func Test_fuzzy_completion_helptag()
2976" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002977
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002978" highlight group name fuzzy completion
2979func Test_fuzzy_completion_hlgroup()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002980 set wildoptions&
2981 call feedkeys(":highlight SKey\<Tab>\<C-B>\"\<CR>", 'tx')
2982 call assert_equal('"highlight SKey', @:)
2983 call feedkeys(":highlight Sp*Key\<Tab>\<C-B>\"\<CR>", 'tx')
2984 call assert_equal('"highlight SpecialKey', @:)
2985 set wildoptions=fuzzy
2986 call feedkeys(":highlight SKey\<Tab>\<C-B>\"\<CR>", 'tx')
2987 call assert_equal('"highlight SpecialKey', @:)
2988 call feedkeys(":highlight Sp*Key\<Tab>\<C-B>\"\<CR>", 'tx')
2989 call assert_equal('"highlight Sp*Key', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002990 set wildoptions&
2991endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002992
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002993" :history suboptions fuzzy completion
2994func Test_fuzzy_completion_history()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002995 set wildoptions&
2996 call feedkeys(":history dg\<Tab>\<C-B>\"\<CR>", 'tx')
2997 call assert_equal('"history dg', @:)
2998 call feedkeys(":history se*h\<Tab>\<C-B>\"\<CR>", 'tx')
2999 call assert_equal('"history search', @:)
3000 set wildoptions=fuzzy
3001 call feedkeys(":history dg\<Tab>\<C-B>\"\<CR>", 'tx')
3002 call assert_equal('"history debug', @:)
3003 call feedkeys(":history se*h\<Tab>\<C-B>\"\<CR>", 'tx')
3004 call assert_equal('"history se*h', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003005 set wildoptions&
3006endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003007
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003008" :language locale name fuzzy completion
3009func Test_fuzzy_completion_lang()
3010 CheckUnix
3011 set wildoptions&
3012 call feedkeys(":lang psx\<Tab>\<C-B>\"\<CR>", 'tx')
3013 call assert_equal('"lang psx', @:)
3014 set wildoptions=fuzzy
3015 call feedkeys(":lang psx\<Tab>\<C-B>\"\<CR>", 'tx')
3016 call assert_equal('"lang POSIX', @:)
3017 set wildoptions&
3018endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003019
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003020" :mapclear buffer argument fuzzy completion
3021func Test_fuzzy_completion_mapclear()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003022 set wildoptions&
3023 call feedkeys(":mapclear buf\<Tab>\<C-B>\"\<CR>", 'tx')
3024 call assert_equal('"mapclear buf', @:)
3025 set wildoptions=fuzzy
3026 call feedkeys(":mapclear buf\<Tab>\<C-B>\"\<CR>", 'tx')
3027 call assert_equal('"mapclear <buffer>', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003028 set wildoptions&
3029endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003030
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003031" map name fuzzy completion
3032func Test_fuzzy_completion_mapname()
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003033 " test regex completion works
3034 set wildoptions=fuzzy
3035 call feedkeys(":cnoremap <ex\<Tab> <esc> \<Tab>\<C-B>\"\<CR>", 'tx')
3036 call assert_equal("\"cnoremap <expr> <esc> \<Tab>", @:)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003037 nmap <plug>MyLongMap :p<CR>
3038 call feedkeys(":nmap MLM\<Tab>\<C-B>\"\<CR>", 'tx')
3039 call assert_equal("\"nmap <Plug>MyLongMap", @:)
3040 call feedkeys(":nmap MLM \<Tab>\<C-B>\"\<CR>", 'tx')
3041 call assert_equal("\"nmap MLM \t", @:)
3042 call feedkeys(":nmap <F2> one two \<Tab>\<C-B>\"\<CR>", 'tx')
3043 call assert_equal("\"nmap <F2> one two \t", @:)
3044 " duplicate entries should be removed
3045 vmap <plug>MyLongMap :<C-U>#<CR>
3046 call feedkeys(":nmap MLM\<Tab>\<C-B>\"\<CR>", 'tx')
3047 call assert_equal("\"nmap <Plug>MyLongMap", @:)
3048 nunmap <plug>MyLongMap
3049 vunmap <plug>MyLongMap
3050 call feedkeys(":nmap ABC\<Tab>\<C-B>\"\<CR>", 'tx')
3051 call assert_equal("\"nmap ABC\t", @:)
3052 " results should be sorted by best match
3053 nmap <Plug>format :
3054 nmap <Plug>goformat :
3055 nmap <Plug>TestFOrmat :
3056 nmap <Plug>fendoff :
3057 nmap <Plug>state :
3058 nmap <Plug>FendingOff :
3059 call feedkeys(":nmap <Plug>fo\<C-A>\<C-B>\"\<CR>", 'tx')
3060 call assert_equal("\"nmap <Plug>format <Plug>TestFOrmat <Plug>FendingOff <Plug>goformat <Plug>fendoff", @:)
3061 nunmap <Plug>format
3062 nunmap <Plug>goformat
3063 nunmap <Plug>TestFOrmat
3064 nunmap <Plug>fendoff
3065 nunmap <Plug>state
3066 nunmap <Plug>FendingOff
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003067 set wildoptions&
3068endfunc
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003069
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003070" abbreviation fuzzy completion
3071func Test_fuzzy_completion_abbr()
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003072 set wildoptions=fuzzy
3073 call feedkeys(":iabbr wait\<Tab>\<C-B>\"\<CR>", 'tx')
3074 call assert_equal("\"iabbr <nowait>", @:)
3075 iabbr WaitForCompletion WFC
3076 call feedkeys(":iabbr fcl\<Tab>\<C-B>\"\<CR>", 'tx')
3077 call assert_equal("\"iabbr WaitForCompletion", @:)
3078 call feedkeys(":iabbr a1z\<Tab>\<C-B>\"\<CR>", 'tx')
3079 call assert_equal("\"iabbr a1z\t", @:)
zeertzjq145a6af2023-01-22 12:41:55 +00003080
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003081 iunabbrev WaitForCompletion
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003082 set wildoptions&
3083endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003084
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003085" menu name fuzzy completion
3086func Test_fuzzy_completion_menu()
zeertzjq145a6af2023-01-22 12:41:55 +00003087 CheckFeature menu
3088
3089 source $VIMRUNTIME/menu.vim
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003090 set wildoptions&
3091 call feedkeys(":menu pup\<Tab>\<C-B>\"\<CR>", 'tx')
3092 call assert_equal('"menu pup', @:)
3093 set wildoptions=fuzzy
3094 call feedkeys(":menu pup\<Tab>\<C-B>\"\<CR>", 'tx')
3095 call assert_equal('"menu PopUp.', @:)
zeertzjq145a6af2023-01-22 12:41:55 +00003096
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003097 set wildoptions&
zeertzjq145a6af2023-01-22 12:41:55 +00003098 source $VIMRUNTIME/delmenu.vim
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003099endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003100
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003101" :messages suboptions fuzzy completion
3102func Test_fuzzy_completion_messages()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003103 set wildoptions&
3104 call feedkeys(":messages clr\<Tab>\<C-B>\"\<CR>", 'tx')
3105 call assert_equal('"messages clr', @:)
3106 set wildoptions=fuzzy
3107 call feedkeys(":messages clr\<Tab>\<C-B>\"\<CR>", 'tx')
3108 call assert_equal('"messages clear', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003109 set wildoptions&
3110endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003111
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003112" :set option name fuzzy completion
3113func Test_fuzzy_completion_option()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003114 set wildoptions&
3115 call feedkeys(":set brkopt\<Tab>\<C-B>\"\<CR>", 'tx')
3116 call assert_equal('"set brkopt', @:)
3117 set wildoptions=fuzzy
3118 call feedkeys(":set brkopt\<Tab>\<C-B>\"\<CR>", 'tx')
3119 call assert_equal('"set breakindentopt', @:)
3120 set wildoptions&
3121 call feedkeys(":set fixeol\<Tab>\<C-B>\"\<CR>", 'tx')
3122 call assert_equal('"set fixendofline', @:)
3123 set wildoptions=fuzzy
3124 call feedkeys(":set fixeol\<Tab>\<C-B>\"\<CR>", 'tx')
3125 call assert_equal('"set fixendofline', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003126 set wildoptions&
3127endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003128
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003129" :set <term_option>
3130func Test_fuzzy_completion_term_option()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003131 set wildoptions&
3132 call feedkeys(":set t_E\<Tab>\<C-B>\"\<CR>", 'tx')
3133 call assert_equal('"set t_EC', @:)
3134 call feedkeys(":set <t_E\<Tab>\<C-B>\"\<CR>", 'tx')
3135 call assert_equal('"set <t_EC>', @:)
3136 set wildoptions=fuzzy
3137 call feedkeys(":set t_E\<Tab>\<C-B>\"\<CR>", 'tx')
3138 call assert_equal('"set t_EC', @:)
3139 call feedkeys(":set <t_E\<Tab>\<C-B>\"\<CR>", 'tx')
3140 call assert_equal('"set <t_EC>', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003141 set wildoptions&
3142endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003143
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003144" " :packadd directory name fuzzy completion - NOT supported
3145" func Test_fuzzy_completion_packadd()
3146" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003147
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003148" " shell command name fuzzy completion - NOT supported
3149" func Test_fuzzy_completion_shellcmd()
3150" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003151
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003152" :sign suboptions fuzzy completion
3153func Test_fuzzy_completion_sign()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003154 set wildoptions&
3155 call feedkeys(":sign ufe\<Tab>\<C-B>\"\<CR>", 'tx')
3156 call assert_equal('"sign ufe', @:)
3157 set wildoptions=fuzzy
3158 call feedkeys(":sign ufe\<Tab>\<C-B>\"\<CR>", 'tx')
3159 call assert_equal('"sign undefine', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003160 set wildoptions&
3161endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003162
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003163" :syntax suboptions fuzzy completion
3164func Test_fuzzy_completion_syntax_cmd()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003165 set wildoptions&
3166 call feedkeys(":syntax kwd\<Tab>\<C-B>\"\<CR>", 'tx')
3167 call assert_equal('"syntax kwd', @:)
3168 set wildoptions=fuzzy
3169 call feedkeys(":syntax kwd\<Tab>\<C-B>\"\<CR>", 'tx')
3170 call assert_equal('"syntax keyword', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003171 set wildoptions&
3172endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003173
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003174" syntax group name fuzzy completion
3175func Test_fuzzy_completion_syntax_group()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003176 set wildoptions&
3177 call feedkeys(":syntax list mpar\<Tab>\<C-B>\"\<CR>", 'tx')
3178 call assert_equal('"syntax list mpar', @:)
3179 set wildoptions=fuzzy
3180 call feedkeys(":syntax list mpar\<Tab>\<C-B>\"\<CR>", 'tx')
3181 call assert_equal('"syntax list MatchParen', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003182 set wildoptions&
3183endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003184
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003185" :syntime suboptions fuzzy completion
3186func Test_fuzzy_completion_syntime()
3187 CheckFeature profile
3188 set wildoptions&
3189 call feedkeys(":syntime clr\<Tab>\<C-B>\"\<CR>", 'tx')
3190 call assert_equal('"syntime clr', @:)
3191 set wildoptions=fuzzy
3192 call feedkeys(":syntime clr\<Tab>\<C-B>\"\<CR>", 'tx')
3193 call assert_equal('"syntime clear', @:)
3194 set wildoptions&
3195endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003196
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003197" " tag name fuzzy completion - NOT supported
3198" func Test_fuzzy_completion_tagname()
3199" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003200
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003201" " tag name and file fuzzy completion - NOT supported
3202" func Test_fuzzy_completion_tagfile()
3203" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003204
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003205" " user names fuzzy completion - how to test this functionality?
3206" func Test_fuzzy_completion_username()
3207" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003208
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003209" user defined variable name fuzzy completion
3210func Test_fuzzy_completion_userdefined_var()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003211 let g:SomeVariable=10
3212 set wildoptions&
3213 call feedkeys(":let SVar\<Tab>\<C-B>\"\<CR>", 'tx')
3214 call assert_equal('"let SVar', @:)
3215 set wildoptions=fuzzy
3216 call feedkeys(":let SVar\<Tab>\<C-B>\"\<CR>", 'tx')
3217 call assert_equal('"let SomeVariable', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003218 set wildoptions&
3219endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003220
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003221" Test for sorting the results by the best match
3222func Test_fuzzy_completion_cmd_sort_results()
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003223 %bw!
3224 command T123format :
3225 command T123goformat :
3226 command T123TestFOrmat :
3227 command T123fendoff :
3228 command T123state :
3229 command T123FendingOff :
3230 set wildoptions=fuzzy
3231 call feedkeys(":T123fo\<C-A>\<C-B>\"\<CR>", 'tx')
3232 call assert_equal('"T123format T123TestFOrmat T123FendingOff T123goformat T123fendoff', @:)
3233 delcommand T123format
3234 delcommand T123goformat
3235 delcommand T123TestFOrmat
3236 delcommand T123fendoff
3237 delcommand T123state
3238 delcommand T123FendingOff
3239 %bw
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003240 set wildoptions&
3241endfunc
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003242
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003243" Test for fuzzy completion of a command with lower case letters and a number
3244func Test_fuzzy_completion_cmd_alnum()
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00003245 command Foo2Bar :
3246 set wildoptions=fuzzy
3247 call feedkeys(":foo2\<Tab>\<C-B>\"\<CR>", 'tx')
3248 call assert_equal('"Foo2Bar', @:)
3249 call feedkeys(":foo\<Tab>\<C-B>\"\<CR>", 'tx')
3250 call assert_equal('"Foo2Bar', @:)
3251 call feedkeys(":bar\<Tab>\<C-B>\"\<CR>", 'tx')
3252 call assert_equal('"Foo2Bar', @:)
3253 delcommand Foo2Bar
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003254 set wildoptions&
3255endfunc
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00003256
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003257" Test for command completion for a command starting with 'k'
3258func Test_fuzzy_completion_cmd_k()
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003259 command KillKillKill :
3260 set wildoptions&
3261 call feedkeys(":killkill\<Tab>\<C-B>\"\<CR>", 'tx')
3262 call assert_equal("\"killkill\<Tab>", @:)
3263 set wildoptions=fuzzy
3264 call feedkeys(":killkill\<Tab>\<C-B>\"\<CR>", 'tx')
3265 call assert_equal('"KillKillKill', @:)
3266 delcom KillKillKill
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003267 set wildoptions&
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003268endfunc
3269
3270" Test for fuzzy completion for user defined custom completion function
3271func Test_fuzzy_completion_custom_func()
3272 func Tcompl(a, c, p)
3273 return "format\ngoformat\nTestFOrmat\nfendoff\nstate"
3274 endfunc
3275 command -nargs=* -complete=custom,Tcompl Fuzzy :
3276 set wildoptions&
3277 call feedkeys(":Fuzzy fo\<C-A>\<C-B>\"\<CR>", 'tx')
3278 call assert_equal("\"Fuzzy format", @:)
3279 call feedkeys(":Fuzzy xy\<Tab>\<C-B>\"\<CR>", 'tx')
3280 call assert_equal("\"Fuzzy xy", @:)
3281 call feedkeys(":Fuzzy ttt\<C-A>\<C-B>\"\<CR>", 'tx')
3282 call assert_equal("\"Fuzzy ttt", @:)
3283 set wildoptions=fuzzy
3284 call feedkeys(":Fuzzy \<C-A>\<C-B>\"\<CR>", 'tx')
3285 call assert_equal("\"Fuzzy format goformat TestFOrmat fendoff state", @:)
3286 call feedkeys(":Fuzzy fo\<C-A>\<C-B>\"\<CR>", 'tx')
3287 call assert_equal("\"Fuzzy format TestFOrmat goformat fendoff", @:)
3288 call feedkeys(":Fuzzy xy\<Tab>\<C-B>\"\<CR>", 'tx')
3289 call assert_equal("\"Fuzzy xy", @:)
3290 call feedkeys(":Fuzzy ttt\<C-A>\<C-B>\"\<CR>", 'tx')
3291 call assert_equal("\"Fuzzy TestFOrmat", @:)
3292 delcom Fuzzy
3293 set wildoptions&
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003294endfunc
3295
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003296" Test for :breakadd argument completion
3297func Test_cmdline_complete_breakadd()
3298 call feedkeys(":breakadd \<C-A>\<C-B>\"\<CR>", 'tx')
3299 call assert_equal("\"breakadd expr file func here", @:)
3300 call feedkeys(":breakadd \<Tab>\<C-B>\"\<CR>", 'tx')
3301 call assert_equal("\"breakadd expr", @:)
3302 call feedkeys(":breakadd \<Tab>\<C-B>\"\<CR>", 'tx')
3303 call assert_equal("\"breakadd expr", @:)
3304 call feedkeys(":breakadd he\<Tab>\<C-B>\"\<CR>", 'tx')
3305 call assert_equal("\"breakadd here", @:)
3306 call feedkeys(":breakadd he\<Tab>\<C-B>\"\<CR>", 'tx')
3307 call assert_equal("\"breakadd here", @:)
3308 call feedkeys(":breakadd abc\<Tab>\<C-B>\"\<CR>", 'tx')
3309 call assert_equal("\"breakadd abc", @:)
3310 call assert_equal(['expr', 'file', 'func', 'here'], getcompletion('', 'breakpoint'))
3311 let l = getcompletion('not', 'breakpoint')
3312 call assert_equal([], l)
3313
3314 " Test for :breakadd file [lnum] <file>
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01003315 call writefile([], 'Xscript', 'D')
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003316 call feedkeys(":breakadd file Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3317 call assert_equal("\"breakadd file Xscript", @:)
3318 call feedkeys(":breakadd file Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3319 call assert_equal("\"breakadd file Xscript", @:)
3320 call feedkeys(":breakadd file 20 Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3321 call assert_equal("\"breakadd file 20 Xscript", @:)
3322 call feedkeys(":breakadd file 20 Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3323 call assert_equal("\"breakadd file 20 Xscript", @:)
3324 call feedkeys(":breakadd file 20x Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3325 call assert_equal("\"breakadd file 20x Xsc\t", @:)
3326 call feedkeys(":breakadd file 20\<Tab>\<C-B>\"\<CR>", 'tx')
3327 call assert_equal("\"breakadd file 20\t", @:)
3328 call feedkeys(":breakadd file 20x\<Tab>\<C-B>\"\<CR>", 'tx')
3329 call assert_equal("\"breakadd file 20x\t", @:)
3330 call feedkeys(":breakadd file Xscript \<Tab>\<C-B>\"\<CR>", 'tx')
3331 call assert_equal("\"breakadd file Xscript ", @:)
3332 call feedkeys(":breakadd file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3333 call assert_equal("\"breakadd file X1B2C3", @:)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003334
3335 " Test for :breakadd func [lnum] <function>
3336 func Xbreak_func()
3337 endfunc
3338 call feedkeys(":breakadd func Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3339 call assert_equal("\"breakadd func Xbreak_func", @:)
3340 call feedkeys(":breakadd func Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3341 call assert_equal("\"breakadd func Xbreak_func", @:)
3342 call feedkeys(":breakadd func 20 Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3343 call assert_equal("\"breakadd func 20 Xbreak_func", @:)
3344 call feedkeys(":breakadd func 20 Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3345 call assert_equal("\"breakadd func 20 Xbreak_func", @:)
3346 call feedkeys(":breakadd func 20x Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3347 call assert_equal("\"breakadd func 20x Xbr\t", @:)
3348 call feedkeys(":breakadd func 20\<Tab>\<C-B>\"\<CR>", 'tx')
3349 call assert_equal("\"breakadd func 20\t", @:)
3350 call feedkeys(":breakadd func 20x\<Tab>\<C-B>\"\<CR>", 'tx')
3351 call assert_equal("\"breakadd func 20x\t", @:)
3352 call feedkeys(":breakadd func Xbreak_func \<Tab>\<C-B>\"\<CR>", 'tx')
3353 call assert_equal("\"breakadd func Xbreak_func ", @:)
3354 call feedkeys(":breakadd func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3355 call assert_equal("\"breakadd func X1B2C3", @:)
3356 delfunc Xbreak_func
3357
3358 " Test for :breakadd expr <expression>
3359 let g:Xtest_var = 10
3360 call feedkeys(":breakadd expr Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3361 call assert_equal("\"breakadd expr Xtest_var", @:)
3362 call feedkeys(":breakadd expr Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3363 call assert_equal("\"breakadd expr Xtest_var", @:)
3364 call feedkeys(":breakadd expr Xtest_var \<Tab>\<C-B>\"\<CR>", 'tx')
3365 call assert_equal("\"breakadd expr Xtest_var ", @:)
3366 call feedkeys(":breakadd expr X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3367 call assert_equal("\"breakadd expr X1B2C3", @:)
3368 unlet g:Xtest_var
3369
3370 " Test for :breakadd here
3371 call feedkeys(":breakadd here Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3372 call assert_equal("\"breakadd here Xtest", @:)
3373 call feedkeys(":breakadd here Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3374 call assert_equal("\"breakadd here Xtest", @:)
3375 call feedkeys(":breakadd here \<Tab>\<C-B>\"\<CR>", 'tx')
3376 call assert_equal("\"breakadd here ", @:)
3377endfunc
3378
3379" Test for :breakdel argument completion
3380func Test_cmdline_complete_breakdel()
3381 call feedkeys(":breakdel \<C-A>\<C-B>\"\<CR>", 'tx')
3382 call assert_equal("\"breakdel file func here", @:)
3383 call feedkeys(":breakdel \<Tab>\<C-B>\"\<CR>", 'tx')
3384 call assert_equal("\"breakdel file", @:)
3385 call feedkeys(":breakdel \<Tab>\<C-B>\"\<CR>", 'tx')
3386 call assert_equal("\"breakdel file", @:)
3387 call feedkeys(":breakdel he\<Tab>\<C-B>\"\<CR>", 'tx')
3388 call assert_equal("\"breakdel here", @:)
3389 call feedkeys(":breakdel he\<Tab>\<C-B>\"\<CR>", 'tx')
3390 call assert_equal("\"breakdel here", @:)
3391 call feedkeys(":breakdel abc\<Tab>\<C-B>\"\<CR>", 'tx')
3392 call assert_equal("\"breakdel abc", @:)
3393
3394 " Test for :breakdel file [lnum] <file>
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01003395 call writefile([], 'Xscript', 'D')
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003396 call feedkeys(":breakdel file Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3397 call assert_equal("\"breakdel file Xscript", @:)
3398 call feedkeys(":breakdel file Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3399 call assert_equal("\"breakdel file Xscript", @:)
3400 call feedkeys(":breakdel file 20 Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3401 call assert_equal("\"breakdel file 20 Xscript", @:)
3402 call feedkeys(":breakdel file 20 Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3403 call assert_equal("\"breakdel file 20 Xscript", @:)
3404 call feedkeys(":breakdel file 20x Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3405 call assert_equal("\"breakdel file 20x Xsc\t", @:)
3406 call feedkeys(":breakdel file 20\<Tab>\<C-B>\"\<CR>", 'tx')
3407 call assert_equal("\"breakdel file 20\t", @:)
3408 call feedkeys(":breakdel file 20x\<Tab>\<C-B>\"\<CR>", 'tx')
3409 call assert_equal("\"breakdel file 20x\t", @:)
3410 call feedkeys(":breakdel file Xscript \<Tab>\<C-B>\"\<CR>", 'tx')
3411 call assert_equal("\"breakdel file Xscript ", @:)
3412 call feedkeys(":breakdel file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3413 call assert_equal("\"breakdel file X1B2C3", @:)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003414
3415 " Test for :breakdel func [lnum] <function>
3416 func Xbreak_func()
3417 endfunc
3418 call feedkeys(":breakdel func Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3419 call assert_equal("\"breakdel func Xbreak_func", @:)
3420 call feedkeys(":breakdel func Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3421 call assert_equal("\"breakdel func Xbreak_func", @:)
3422 call feedkeys(":breakdel func 20 Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3423 call assert_equal("\"breakdel func 20 Xbreak_func", @:)
3424 call feedkeys(":breakdel func 20 Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3425 call assert_equal("\"breakdel func 20 Xbreak_func", @:)
3426 call feedkeys(":breakdel func 20x Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3427 call assert_equal("\"breakdel func 20x Xbr\t", @:)
3428 call feedkeys(":breakdel func 20\<Tab>\<C-B>\"\<CR>", 'tx')
3429 call assert_equal("\"breakdel func 20\t", @:)
3430 call feedkeys(":breakdel func 20x\<Tab>\<C-B>\"\<CR>", 'tx')
3431 call assert_equal("\"breakdel func 20x\t", @:)
3432 call feedkeys(":breakdel func Xbreak_func \<Tab>\<C-B>\"\<CR>", 'tx')
3433 call assert_equal("\"breakdel func Xbreak_func ", @:)
3434 call feedkeys(":breakdel func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3435 call assert_equal("\"breakdel func X1B2C3", @:)
3436 delfunc Xbreak_func
3437
3438 " Test for :breakdel here
3439 call feedkeys(":breakdel here Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3440 call assert_equal("\"breakdel here Xtest", @:)
3441 call feedkeys(":breakdel here Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3442 call assert_equal("\"breakdel here Xtest", @:)
3443 call feedkeys(":breakdel here \<Tab>\<C-B>\"\<CR>", 'tx')
3444 call assert_equal("\"breakdel here ", @:)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003445endfunc
3446
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003447" Test for :scriptnames argument completion
3448func Test_cmdline_complete_scriptnames()
3449 set wildmenu
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01003450 call writefile(['let a = 1'], 'Xa1b2c3.vim', 'D')
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003451 source Xa1b2c3.vim
3452 call feedkeys(":script \<Tab>\<Left>\<Left>\<C-B>\"\<CR>", 'tx')
3453 call assert_match("\"script .*Xa1b2c3.vim$", @:)
3454 call feedkeys(":script \<Tab>\<Left>\<Left>\<C-B>\"\<CR>", 'tx')
3455 call assert_match("\"script .*Xa1b2c3.vim$", @:)
3456 call feedkeys(":script b2c3\<Tab>\<C-B>\"\<CR>", 'tx')
3457 call assert_equal("\"script b2c3", @:)
3458 call feedkeys(":script 2\<Tab>\<C-B>\"\<CR>", 'tx')
3459 call assert_match("\"script 2\<Tab>$", @:)
3460 call feedkeys(":script \<Tab>\<Left>\<Left> \<Tab>\<C-B>\"\<CR>", 'tx')
3461 call assert_match("\"script .*Xa1b2c3.vim $", @:)
3462 call feedkeys(":script \<Tab>\<Left>\<C-B>\"\<CR>", 'tx')
3463 call assert_equal("\"script ", @:)
3464 call assert_match('Xa1b2c3.vim$', getcompletion('.*Xa1b2.*', 'scriptnames')[0])
3465 call assert_equal([], getcompletion('Xa1b2', 'scriptnames'))
3466 new
3467 call feedkeys(":script \<Tab>\<Left>\<Left>\<CR>", 'tx')
3468 call assert_equal('Xa1b2c3.vim', fnamemodify(@%, ':t'))
3469 bw!
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003470 set wildmenu&
3471endfunc
3472
Bram Moolenaard8893442022-05-06 20:38:47 +01003473" this was going over the end of IObuff
3474func Test_report_error_with_composing()
3475 let caught = 'no'
3476 try
3477 exe repeat('0', 987) .. "0\xdd\x80\xdd\x80\xdd\x80\xdd\x80"
3478 catch /E492:/
3479 let caught = 'yes'
3480 endtry
3481 call assert_equal('yes', caught)
3482endfunc
3483
Yegappan Lakshmanan5e877ba2022-03-25 21:19:26 +00003484" Test for expanding 2-letter and 3-letter :substitute command arguments.
3485" These commands don't accept an argument.
3486func Test_cmdline_complete_substitute_short()
3487 for cmd in ['sc', 'sce', 'scg', 'sci', 'scI', 'scn', 'scp', 'scl',
3488 \ 'sgc', 'sge', 'sg', 'sgi', 'sgI', 'sgn', 'sgp', 'sgl', 'sgr',
3489 \ 'sic', 'sie', 'si', 'siI', 'sin', 'sip', 'sir',
3490 \ 'sIc', 'sIe', 'sIg', 'sIi', 'sI', 'sIn', 'sIp', 'sIl', 'sIr',
3491 \ 'src', 'srg', 'sri', 'srI', 'srn', 'srp', 'srl', 'sr']
3492 call feedkeys(':' .. cmd .. " \<Tab>\<C-B>\"\<CR>", 'tx')
3493 call assert_equal('"' .. cmd .. " \<Tab>", @:)
3494 endfor
3495endfunc
3496
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01003497" Test for :! shell command argument completion
3498func Test_cmdline_complete_bang_cmd_argument()
3499 set wildoptions=fuzzy
3500 call feedkeys(":!vim test_cmdline.\<Tab>\<C-B>\"\<CR>", 'xt')
3501 call assert_equal('"!vim test_cmdline.vim', @:)
3502 set wildoptions&
3503 call feedkeys(":!vim test_cmdline.\<Tab>\<C-B>\"\<CR>", 'xt')
3504 call assert_equal('"!vim test_cmdline.vim', @:)
3505endfunc
3506
zeertzjq961b2e52023-04-17 15:53:24 +01003507func Call_cmd_funcs()
3508 return string([getcmdpos(), getcmdscreenpos(), getcmdcompltype()])
Shougo Matsushita79d599b2022-05-07 12:48:29 +01003509endfunc
3510
3511func Test_screenpos_and_completion()
zeertzjq961b2e52023-04-17 15:53:24 +01003512 call assert_equal(0, getcmdpos())
3513 call assert_equal(0, getcmdscreenpos())
3514 call assert_equal('', getcmdcompltype())
3515
3516 cnoremap <expr> <F2> string([getcmdpos(), getcmdscreenpos(), getcmdcompltype()])
3517 call feedkeys(":let a\<F2>\<C-B>\"\<CR>", "xt")
3518 call assert_equal("\"let a[6, 7, 'var']", @:)
3519 call feedkeys(":quit \<F2>\<C-B>\"\<CR>", "xt")
3520 call assert_equal("\"quit [6, 7, '']", @:)
3521 call feedkeys(":nosuchcommand \<F2>\<C-B>\"\<CR>", "xt")
3522 call assert_equal("\"nosuchcommand [15, 16, '']", @:)
3523 cunmap <F2>
Shougo Matsushita79d599b2022-05-07 12:48:29 +01003524endfunc
3525
Bram Moolenaar51f0bfb2022-05-17 20:11:02 +01003526func Test_recursive_register()
3527 let @= = ''
3528 silent! ?e/
3529 let caught = 'no'
3530 try
3531 normal //
3532 catch /E169:/
3533 let caught = 'yes'
3534 endtry
3535 call assert_equal('yes', caught)
3536endfunc
3537
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003538func Test_long_error_message()
3539 " the error should be truncated, not overrun IObuff
3540 silent! norm Q00000000000000     000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000                                                                                                                                                                                                                        
3541endfunc
3542
zeertzjq6791adc2022-07-26 20:42:25 +01003543func Test_cmdline_redraw_tabline()
3544 CheckRunVimInTerminal
3545
3546 let lines =<< trim END
3547 set showtabline=2
3548 autocmd CmdlineEnter * set tabline=foo
3549 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01003550 call writefile(lines, 'Xcmdline_redraw_tabline', 'D')
zeertzjq6791adc2022-07-26 20:42:25 +01003551 let buf = RunVimInTerminal('-S Xcmdline_redraw_tabline', #{rows: 6})
3552 call term_sendkeys(buf, ':')
3553 call WaitForAssert({-> assert_match('^foo', term_getline(buf, 1))})
3554
3555 call StopVimInTerminal(buf)
zeertzjq6791adc2022-07-26 20:42:25 +01003556endfunc
3557
zeertzjqb82a2ab2022-08-21 14:33:57 +01003558func Test_wildmenu_pum_disable_while_shown()
3559 set wildoptions=pum
3560 set wildmenu
3561 cnoremap <F2> <Cmd>set nowildmenu<CR>
3562 call feedkeys(":sign \<Tab>\<F2>\<Esc>", 'tx')
3563 call assert_equal(0, pumvisible())
3564 cunmap <F2>
3565 set wildoptions& wildmenu&
3566endfunc
3567
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003568func Test_setcmdline()
3569 func SetText(text, pos)
zeertzjqac6cd312023-04-12 16:21:14 +01003570 call assert_equal(0, setcmdline(test_null_string()))
3571 call assert_equal('', getcmdline())
3572 call assert_equal(1, getcmdpos())
3573
3574 call assert_equal(0, setcmdline(''[: -1]))
3575 call assert_equal('', getcmdline())
3576 call assert_equal(1, getcmdpos())
3577
zeertzjq54acb902022-08-29 16:21:25 +01003578 autocmd CmdlineChanged * let g:cmdtype = expand('<afile>')
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003579 call assert_equal(0, setcmdline(a:text))
3580 call assert_equal(a:text, getcmdline())
3581 call assert_equal(len(a:text) + 1, getcmdpos())
zeertzjq54acb902022-08-29 16:21:25 +01003582 call assert_equal(getcmdtype(), g:cmdtype)
3583 unlet g:cmdtype
3584 autocmd! CmdlineChanged
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003585
3586 call assert_equal(0, setcmdline(a:text, a:pos))
3587 call assert_equal(a:text, getcmdline())
3588 call assert_equal(a:pos, getcmdpos())
3589
3590 call assert_fails('call setcmdline("' .. a:text .. '", -1)', 'E487:')
Yegappan Lakshmanan25f1e552022-08-28 17:25:04 +01003591 call assert_fails('call setcmdline({}, 0)', 'E1174:')
3592 call assert_fails('call setcmdline("' .. a:text .. '", {})', 'E1210:')
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003593
3594 return ''
3595 endfunc
3596
3597 call feedkeys(":\<C-R>=SetText('set rtp?', 2)\<CR>\<CR>", 'xt')
3598 call assert_equal('set rtp?', @:)
3599
zeertzjq54acb902022-08-29 16:21:25 +01003600 call feedkeys(":let g:str = input('? ')\<CR>", 't')
3601 call feedkeys("\<C-R>=SetText('foo', 4)\<CR>\<CR>", 'xt')
3602 call assert_equal('foo', g:str)
3603 unlet g:str
3604
3605 delfunc SetText
3606
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003607 " setcmdline() returns 1 when not editing the command line.
3608 call assert_equal(1, 'foo'->setcmdline())
3609
3610 " Called in custom function
3611 func CustomComplete(A, L, P)
3612 call assert_equal(0, setcmdline("DoCmd "))
3613 return "January\nFebruary\nMars\n"
3614 endfunc
3615
3616 com! -nargs=* -complete=custom,CustomComplete DoCmd :
3617 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
3618 call assert_equal('"DoCmd January February Mars', @:)
zeertzjq54acb902022-08-29 16:21:25 +01003619 delcom DoCmd
3620 delfunc CustomComplete
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003621
3622 " Called in <expr>
3623 cnoremap <expr>a setcmdline('let foo=')
3624 call feedkeys(":a\<CR>", 'tx')
3625 call assert_equal('let foo=0', @:)
3626 cunmap a
3627endfunc
3628
Sean Dewarfc8a6012023-04-17 16:41:20 +01003629func Test_rulerformat_position()
3630 CheckScreendump
3631
3632 let buf = RunVimInTerminal('', #{rows: 2, cols: 20})
3633 call term_sendkeys(buf, ":set ruler rulerformat=longish\<CR>")
3634 call term_sendkeys(buf, ":set laststatus=0 winwidth=1\<CR>")
3635 call term_sendkeys(buf, "\<C-W>v\<C-W>|\<C-W>p")
3636 call VerifyScreenDump(buf, 'Test_rulerformat_position', {})
3637
3638 " clean up
3639 call StopVimInTerminal(buf)
3640endfunc
3641
zeertzjqe4c79d32023-08-15 22:41:53 +02003642func Test_getcompletion_usercmd()
Christian Brabandt8ef1fbc2023-07-17 20:09:37 +02003643 command! -nargs=* -complete=command TestCompletion echo <q-args>
Christian Brabandt8ef1fbc2023-07-17 20:09:37 +02003644
zeertzjqe4c79d32023-08-15 22:41:53 +02003645 call assert_equal(getcompletion('', 'cmdline'),
3646 \ getcompletion('TestCompletion ', 'cmdline'))
3647 call assert_equal(['<buffer>'],
3648 \ getcompletion('TestCompletion map <bu', 'cmdline'))
3649
Christian Brabandt8ef1fbc2023-07-17 20:09:37 +02003650 delcom TestCompletion
Christian Brabandt8ef1fbc2023-07-17 20:09:37 +02003651endfunc
zeertzjqe4c79d32023-08-15 22:41:53 +02003652
Shougo Matsushita92997dd2023-08-20 20:55:55 +02003653func Test_custom_completion()
3654 func CustomComplete1(lead, line, pos)
3655 return "a\nb\nc"
3656 endfunc
3657 func CustomComplete2(lead, line, pos)
3658 return ['a', 'b']->filter({ _, val -> val->stridx(a:lead) == 0 })
3659 endfunc
3660 func Check_custom_completion()
3661 call assert_equal('custom,CustomComplete1', getcmdcompltype())
3662 return ''
3663 endfunc
3664 func Check_customlist_completion()
3665 call assert_equal('customlist,CustomComplete2', getcmdcompltype())
3666 return ''
3667 endfunc
3668
3669 command -nargs=1 -complete=custom,CustomComplete1 Test1 echo
3670 command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
3671
3672 call feedkeys(":Test1 \<C-R>=Check_custom_completion()\<CR>\<Esc>", "xt")
3673 call feedkeys(":Test2 \<C-R>=Check_customlist_completion()\<CR>\<Esc>", "xt")
3674
3675 call assert_fails("call getcompletion('', 'custom')", 'E475:')
3676 call assert_fails("call getcompletion('', 'customlist')", 'E475:')
3677
3678 call assert_equal(getcompletion('', 'custom,CustomComplete1'), ['a', 'b', 'c'])
3679 call assert_equal(getcompletion('', 'customlist,CustomComplete2'), ['a', 'b'])
3680 call assert_equal(getcompletion('b', 'customlist,CustomComplete2'), ['b'])
3681
3682 delcom Test1
3683 delcom Test2
3684
3685 delfunc CustomComplete1
3686 delfunc CustomComplete2
3687 delfunc Check_custom_completion
3688 delfunc Check_customlist_completion
3689endfunc
3690
zeertzjq28a23602023-09-29 19:58:35 +02003691func Test_custom_completion_with_glob()
3692 func TestGlobComplete(A, L, P)
3693 return split(glob('Xglob*'), "\n")
3694 endfunc
3695
3696 command -nargs=* -complete=customlist,TestGlobComplete TestGlobComplete :
3697 call writefile([], 'Xglob1', 'D')
3698 call writefile([], 'Xglob2', 'D')
3699
3700 call feedkeys(":TestGlobComplete \<Tab> \<Tab>\<C-N> \<Tab>\<C-P>;\<C-B>\"\<CR>", 'xt')
3701 call assert_equal('"TestGlobComplete Xglob1 Xglob2 ;', @:)
3702
3703 delcommand TestGlobComplete
3704 delfunc TestGlobComplete
3705endfunc
3706
Bram Moolenaar309976e2019-12-05 18:16:33 +01003707" vim: shiftwidth=2 sts=2 expandtab