blob: 455471267068f85f799713a434e25f1b9575b8b2 [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
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200161 " Test for Ctrl-E/Ctrl-Y being able to cancel / accept a match
162 call feedkeys(":sign un zz\<Left>\<Left>\<Left>\<Tab>\<C-E> yy\<C-B>\"\<CR>", 'tx')
163 call assert_equal('"sign un yy zz', @:)
164
165 call feedkeys(":sign un zz\<Left>\<Left>\<Left>\<Tab>\<Tab>\<C-Y> yy\<C-B>\"\<CR>", 'tx')
166 call assert_equal('"sign unplace yy zz', @:)
167
Bram Moolenaar37db6422019-03-28 21:26:23 +0100168 " cleanup
169 %bwipe
Bram Moolenaarae3150e2016-06-11 23:22:36 +0200170 set nowildmenu
171endfunc
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100172
Bram Moolenaara60053b2020-09-03 16:50:13 +0200173func Test_wildmenu_screendump()
174 CheckScreendump
175
176 let lines =<< trim [SCRIPT]
177 set wildmenu hlsearch
178 [SCRIPT]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100179 call writefile(lines, 'XTest_wildmenu', 'D')
Bram Moolenaara60053b2020-09-03 16:50:13 +0200180
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +0200181 " Test simple wildmenu
Bram Moolenaara60053b2020-09-03 16:50:13 +0200182 let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
183 call term_sendkeys(buf, ":vim\<Tab>")
184 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
185
186 call term_sendkeys(buf, "\<Tab>")
187 call VerifyScreenDump(buf, 'Test_wildmenu_2', {})
188
189 call term_sendkeys(buf, "\<Tab>")
190 call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
191
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +0200192 " Looped back to the original value
Bram Moolenaar39f3b142021-02-14 12:57:36 +0100193 call term_sendkeys(buf, "\<Tab>\<Tab>")
Bram Moolenaara60053b2020-09-03 16:50:13 +0200194 call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +0200195
196 " Test that the wild menu is cleared properly
197 call term_sendkeys(buf, " ")
198 call VerifyScreenDump(buf, 'Test_wildmenu_5', {})
199
200 " Test that a different wildchar still works
201 call term_sendkeys(buf, "\<Esc>:set wildchar=<Esc>\<CR>")
202 call term_sendkeys(buf, ":vim\<Esc>")
203 call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
204
205 " Double-<Esc> is a hard-coded method to escape while wildchar=<Esc>. Make
206 " sure clean up is properly done in edge case like this.
Bram Moolenaara60053b2020-09-03 16:50:13 +0200207 call term_sendkeys(buf, "\<Esc>")
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +0200208 call VerifyScreenDump(buf, 'Test_wildmenu_6', {})
Bram Moolenaara60053b2020-09-03 16:50:13 +0200209
210 " clean up
211 call StopVimInTerminal(buf)
Bram Moolenaara60053b2020-09-03 16:50:13 +0200212endfunc
213
Bram Moolenaara653e532022-04-19 11:38:24 +0100214func Test_redraw_in_autocmd()
215 CheckScreendump
216
217 let lines =<< trim END
218 set cmdheight=2
219 autocmd CmdlineChanged * redraw
220 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100221 call writefile(lines, 'XTest_redraw', 'D')
Bram Moolenaara653e532022-04-19 11:38:24 +0100222
223 let buf = RunVimInTerminal('-S XTest_redraw', {'rows': 8})
224 call term_sendkeys(buf, ":for i in range(3)\<CR>")
225 call VerifyScreenDump(buf, 'Test_redraw_in_autocmd_1', {})
226
227 call term_sendkeys(buf, "let i =")
228 call VerifyScreenDump(buf, 'Test_redraw_in_autocmd_2', {})
229
230 " clean up
231 call term_sendkeys(buf, "\<CR>")
232 call StopVimInTerminal(buf)
Bram Moolenaara653e532022-04-19 11:38:24 +0100233endfunc
234
Bram Moolenaarbcd69242022-09-19 21:16:12 +0100235func Test_redrawstatus_in_autocmd()
236 CheckScreendump
237
238 let lines =<< trim END
zeertzjqc14bfc32022-09-20 13:17:57 +0100239 set laststatus=2
240 set statusline=%=:%{getcmdline()}
zeertzjq320d9102022-09-20 17:12:13 +0100241 autocmd CmdlineChanged * redrawstatus
Bram Moolenaarbcd69242022-09-19 21:16:12 +0100242 END
243 call writefile(lines, 'XTest_redrawstatus', 'D')
244
245 let buf = RunVimInTerminal('-S XTest_redrawstatus', {'rows': 8})
zeertzjqc14bfc32022-09-20 13:17:57 +0100246 " :redrawstatus is postponed if messages have scrolled
Bram Moolenaarbcd69242022-09-19 21:16:12 +0100247 call term_sendkeys(buf, ":echo \"one\\ntwo\\nthree\\nfour\"\<CR>")
248 call term_sendkeys(buf, ":foobar")
249 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_1', {})
zeertzjqc14bfc32022-09-20 13:17:57 +0100250 " it is not postponed if messages have not scrolled
zeertzjq320d9102022-09-20 17:12:13 +0100251 call term_sendkeys(buf, "\<Esc>:for in in range(3)")
zeertzjqc14bfc32022-09-20 13:17:57 +0100252 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_2', {})
zeertzjq320d9102022-09-20 17:12:13 +0100253 " with cmdheight=1 messages have scrolled when typing :endfor
254 call term_sendkeys(buf, "\<CR>:endfor")
255 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_3', {})
256 call term_sendkeys(buf, "\<CR>:set cmdheight=2\<CR>")
257 " with cmdheight=2 messages haven't scrolled when typing :for or :endfor
258 call term_sendkeys(buf, ":for in in range(3)")
259 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_4', {})
260 call term_sendkeys(buf, "\<CR>:endfor")
261 call VerifyScreenDump(buf, 'Test_redrawstatus_in_autocmd_5', {})
Bram Moolenaarbcd69242022-09-19 21:16:12 +0100262
263 " clean up
264 call term_sendkeys(buf, "\<CR>")
265 call StopVimInTerminal(buf)
266endfunc
267
Bram Moolenaarf797e302022-08-11 13:17:30 +0100268func Test_changing_cmdheight()
269 CheckScreendump
270
271 let lines =<< trim END
272 set cmdheight=1 laststatus=2
Bram Moolenaar0816f472022-10-05 15:42:32 +0100273 func EchoTwo()
274 set laststatus=2
275 set cmdheight=5
276 echo 'foo'
277 echo 'bar'
278 set cmdheight=1
279 endfunc
Bram Moolenaarf797e302022-08-11 13:17:30 +0100280 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100281 call writefile(lines, 'XTest_cmdheight', 'D')
Bram Moolenaarf797e302022-08-11 13:17:30 +0100282
283 let buf = RunVimInTerminal('-S XTest_cmdheight', {'rows': 8})
284 call term_sendkeys(buf, ":resize -3\<CR>")
285 call VerifyScreenDump(buf, 'Test_changing_cmdheight_1', {})
286
287 " using the space available doesn't change the status line
288 call term_sendkeys(buf, ":set cmdheight+=3\<CR>")
289 call VerifyScreenDump(buf, 'Test_changing_cmdheight_2', {})
290
291 " using more space moves the status line up
292 call term_sendkeys(buf, ":set cmdheight+=1\<CR>")
293 call VerifyScreenDump(buf, 'Test_changing_cmdheight_3', {})
294
295 " reducing cmdheight moves status line down
296 call term_sendkeys(buf, ":set cmdheight-=2\<CR>")
297 call VerifyScreenDump(buf, 'Test_changing_cmdheight_4', {})
298
Bram Moolenaara9a6b032023-02-05 18:00:42 +0000299 " reducing window size and then setting cmdheight
Bram Moolenaard4cf9fc2022-08-11 14:13:37 +0100300 call term_sendkeys(buf, ":resize -1\<CR>")
301 call term_sendkeys(buf, ":set cmdheight=1\<CR>")
302 call VerifyScreenDump(buf, 'Test_changing_cmdheight_5', {})
303
Bram Moolenaar0816f472022-10-05 15:42:32 +0100304 " setting 'cmdheight' works after outputting two messages
305 call term_sendkeys(buf, ":call EchoTwo()\<CR>")
306 call VerifyScreenDump(buf, 'Test_changing_cmdheight_6', {})
307
Bram Moolenaarf797e302022-08-11 13:17:30 +0100308 " clean up
309 call StopVimInTerminal(buf)
Bram Moolenaarf797e302022-08-11 13:17:30 +0100310endfunc
311
Bram Moolenaarc9f5f732022-10-06 11:39:06 +0100312func Test_cmdheight_tabline()
313 CheckScreendump
314
315 let buf = RunVimInTerminal('-c "set ls=2" -c "set stal=2" -c "set cmdheight=1"', {'rows': 6})
316 call VerifyScreenDump(buf, 'Test_cmdheight_tabline_1', {})
317
318 " clean up
319 call StopVimInTerminal(buf)
320endfunc
321
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100322func Test_map_completion()
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100323 call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
324 call assert_equal('"map <unique> <silent>', getreg(':'))
325 call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
326 call assert_equal('"map <script> <unique>', getreg(':'))
327 call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
328 call assert_equal('"map <expr> <script>', getreg(':'))
329 call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
330 call assert_equal('"map <buffer> <expr>', getreg(':'))
331 call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
332 call assert_equal('"map <nowait> <buffer>', getreg(':'))
333 call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
334 call assert_equal('"map <special> <nowait>', getreg(':'))
335 call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
336 call assert_equal('"map <silent> <special>', getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200337
Bram Moolenaar1776a282019-05-03 16:05:41 +0200338 map <Middle>x middle
339
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200340 map ,f commaf
341 map ,g commaf
Bram Moolenaar1776a282019-05-03 16:05:41 +0200342 map <Left> left
343 map <A-Left>x shiftleft
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200344 call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
345 call assert_equal('"map ,f', getreg(':'))
346 call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
347 call assert_equal('"map ,g', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200348 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
349 call assert_equal('"map <Left>', getreg(':'))
350 call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200351 call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
zeertzjqc3a26c62023-02-17 16:40:20 +0000352 call feedkeys(":map <M-Left>\<Tab>\<Home>\"\<CR>", 'xt')
353 call assert_equal("\"map <M-Left>x", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200354 unmap ,f
355 unmap ,g
Bram Moolenaar1776a282019-05-03 16:05:41 +0200356 unmap <Left>
357 unmap <A-Left>x
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200358
zeertzjqc3a26c62023-02-17 16:40:20 +0000359 set cpo-=< cpo-=k
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200360 map <Left> left
361 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
362 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar1776a282019-05-03 16:05:41 +0200363 call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
Bram Moolenaar92b9e602019-05-03 16:49:25 +0200364 call assert_equal("\"map <M\<Tab>", getreg(':'))
zeertzjqc3a26c62023-02-17 16:40:20 +0000365 call feedkeys(":map \<C-V>\<C-V><M\<Tab>\<Home>\"\<CR>", 'xt')
366 call assert_equal("\"map \<C-V><Middle>x", getreg(':'))
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200367 unmap <Left>
368
369 set cpo+=<
370 map <Left> left
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200371 exe "set t_k6=\<Esc>[17~"
372 call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200373 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
374 call assert_equal('"map <Left>', getreg(':'))
Bram Moolenaar510671a2019-05-04 19:26:56 +0200375 if !has('gui_running')
376 call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
377 call assert_equal("\"map <F6>x", getreg(':'))
378 endif
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200379 unmap <Left>
Bram Moolenaar61df0c72019-05-03 21:10:36 +0200380 call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
Bram Moolenaar2cb9f022019-05-03 15:13:57 +0200381 set cpo-=<
382
383 set cpo+=B
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-=B
389
390 set cpo+=k
391 map <Left> left
392 call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
393 call assert_equal('"map <Left>', getreg(':'))
394 unmap <Left>
395 set cpo-=k
Bram Moolenaar1776a282019-05-03 16:05:41 +0200396
Bram Moolenaar531be472020-09-23 22:38:05 +0200397 call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
398
Bram Moolenaar1776a282019-05-03 16:05:41 +0200399 unmap <Middle>x
400 set cpo&vim
Bram Moolenaarcf5fdf72017-03-02 23:05:51 +0100401endfunc
402
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100403func Test_match_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100404 hi Aardig ctermfg=green
405 call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000406 call assert_equal('"match Aardig', @:)
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100407 call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000408 call assert_equal('"match none', @:)
409 call feedkeys(":match | chist\<Tab>\<C-B>\"\<CR>", 'xt')
410 call assert_equal('"match | chistory', @:)
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100411endfunc
412
413func Test_highlight_completion()
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100414 hi Aardig ctermfg=green
415 call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
416 call assert_equal('"hi Aardig', getreg(':'))
Bram Moolenaarea588152017-04-10 22:45:30 +0200417 call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
418 call assert_equal('"hi default Aardig', getreg(':'))
419 call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
420 call assert_equal('"hi clear Aardig', getreg(':'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100421 call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
422 call assert_equal('"hi link', getreg(':'))
423 call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
424 call assert_equal('"hi default', getreg(':'))
425 call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
426 call assert_equal('"hi clear', getreg(':'))
Bram Moolenaar75e15672020-06-28 13:10:22 +0200427 call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
428 call assert_equal('"hi clear Aardig Aardig', getreg(':'))
429 call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
430 call assert_equal("\"hi Aardig \t", getreg(':'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200431
432 " A cleared group does not show up in completions.
433 hi Anders ctermfg=green
434 call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
435 hi clear Aardig
436 call assert_equal(['Anders'], getcompletion('A', 'highlight'))
437 hi clear Anders
438 call assert_equal([], getcompletion('A', 'highlight'))
Bram Moolenaar15eedf12017-01-22 19:25:33 +0100439endfunc
440
Bram Moolenaar75e15672020-06-28 13:10:22 +0200441" Test for command-line expansion of "hi Ni " (easter egg)
442func Test_highlight_easter_egg()
443 call test_override('ui_delay', 1)
444 call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
445 call assert_equal("\"hi Ni \<Tab>", @:)
446 call test_override('ALL', 0)
447endfunc
448
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200449func Test_getcompletion()
450 let groupcount = len(getcompletion('', 'event'))
451 call assert_true(groupcount > 0)
Bram Moolenaar4c313b12019-08-24 22:58:31 +0200452 let matchcount = len('File'->getcompletion('event'))
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200453 call assert_true(matchcount > 0)
454 call assert_true(groupcount > matchcount)
455
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200456 if has('menu')
457 source $VIMRUNTIME/menu.vim
458 let matchcount = len(getcompletion('', 'menu'))
459 call assert_true(matchcount > 0)
460 call assert_equal(['File.'], getcompletion('File', 'menu'))
461 call assert_true(matchcount > 0)
462 let matchcount = len(getcompletion('File.', 'menu'))
463 call assert_true(matchcount > 0)
zeertzjq145a6af2023-01-22 12:41:55 +0000464 source $VIMRUNTIME/delmenu.vim
Bram Moolenaar0d3e24b2016-07-09 19:20:59 +0200465 endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200466
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200467 let l = getcompletion('v:n', 'var')
468 call assert_true(index(l, 'v:null') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200469 let l = getcompletion('v:notexists', 'var')
470 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200471
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200472 args a.c b.c
473 let l = getcompletion('', 'arglist')
474 call assert_equal(['a.c', 'b.c'], l)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +0000475 let l = getcompletion('a.', 'buffer')
476 call assert_equal(['a.c'], l)
Bram Moolenaarcd43eff2018-03-29 15:55:38 +0200477 %argdelete
478
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200479 let l = getcompletion('', 'augroup')
480 call assert_true(index(l, 'END') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200481 let l = getcompletion('blahblah', 'augroup')
482 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200483
484 let l = getcompletion('', 'behave')
485 call assert_true(index(l, 'mswin') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200486 let l = getcompletion('not', 'behave')
487 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200488
489 let l = getcompletion('', 'color')
490 call assert_true(index(l, 'default') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200491 let l = getcompletion('dirty', 'color')
492 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200493
494 let l = getcompletion('', 'command')
495 call assert_true(index(l, 'sleep') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200496 let l = getcompletion('awake', 'command')
497 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200498
499 let l = getcompletion('', 'dir')
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200500 call assert_true(index(l, 'samples/') >= 0)
501 let l = getcompletion('NoMatch', 'dir')
502 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200503
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100504 if glob('~/*') !=# ''
505 let l = getcompletion('~/', 'dir')
506 call assert_true(l[0][0] ==# '~')
507 endif
508
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200509 let l = getcompletion('exe', 'expression')
510 call assert_true(index(l, 'executable(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200511 let l = getcompletion('kill', 'expression')
512 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200513
514 let l = getcompletion('tag', 'function')
515 call assert_true(index(l, 'taglist(') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200516 let l = getcompletion('paint', 'function')
517 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200518
Kota Kato90c23532023-01-18 15:27:38 +0000519 if !has('ruby')
Bram Moolenaara9a6b032023-02-05 18:00:42 +0000520 " global_functions[] has an entry but it doesn't have an implementation
Kota Kato90c23532023-01-18 15:27:38 +0000521 let l = getcompletion('ruby', 'function')
522 call assert_equal([], l)
523 endif
524
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200525 let Flambda = {-> 'hello'}
526 let l = getcompletion('', 'function')
527 let l = filter(l, {i, v -> v =~ 'lambda'})
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200528 call assert_equal([], l)
Bram Moolenaarb49edc12016-07-23 15:47:34 +0200529
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200530 let l = getcompletion('run', 'file')
531 call assert_true(index(l, 'runtest.vim') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200532 let l = getcompletion('walk', 'file')
533 call assert_equal([], l)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200534 set wildignore=*.vim
535 let l = getcompletion('run', 'file', 1)
536 call assert_true(index(l, 'runtest.vim') < 0)
537 set wildignore&
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000538 " Directory name with space character
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100539 call mkdir('Xdir with space', 'D')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000540 call assert_equal(['Xdir with space/'], getcompletion('Xdir\ w', 'shellcmd'))
541 call assert_equal(['./Xdir with space/'], getcompletion('./Xdir', 'shellcmd'))
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200542
543 let l = getcompletion('ha', 'filetype')
544 call assert_true(index(l, 'hamster') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200545 let l = getcompletion('horse', 'filetype')
546 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200547
Doug Kearns81642d92024-01-04 22:37:44 +0100548 if has('keymap')
549 let l = getcompletion('acc', 'keymap')
550 call assert_true(index(l, 'accents') >= 0)
551 let l = getcompletion('nullkeymap', 'keymap')
552 call assert_equal([], l)
553 endif
554
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200555 let l = getcompletion('z', 'syntax')
556 call assert_true(index(l, 'zimbu') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200557 let l = getcompletion('emacs', 'syntax')
558 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200559
560 let l = getcompletion('jikes', 'compiler')
561 call assert_true(index(l, 'jikes') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200562 let l = getcompletion('break', 'compiler')
563 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200564
565 let l = getcompletion('last', 'help')
566 call assert_true(index(l, ':tablast') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200567 let l = getcompletion('giveup', 'help')
568 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200569
570 let l = getcompletion('time', 'option')
571 call assert_true(index(l, 'timeoutlen') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200572 let l = getcompletion('space', 'option')
573 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200574
575 let l = getcompletion('er', 'highlight')
576 call assert_true(index(l, 'ErrorMsg') >= 0)
Bram Moolenaarb56195e2016-07-28 22:53:37 +0200577 let l = getcompletion('dark', 'highlight')
578 call assert_equal([], l)
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200579
Bram Moolenaar9e507ca2016-10-15 15:39:39 +0200580 let l = getcompletion('', 'messages')
581 call assert_true(index(l, 'clear') >= 0)
582 let l = getcompletion('not', 'messages')
583 call assert_equal([], l)
584
Bram Moolenaarcae92dc2017-08-06 15:22:15 +0200585 let l = getcompletion('', 'mapclear')
586 call assert_true(index(l, '<buffer>') >= 0)
587 let l = getcompletion('not', 'mapclear')
588 call assert_equal([], l)
589
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200590 let l = getcompletion('.', 'shellcmd')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200591 call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200592 call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
593 let root = has('win32') ? 'C:\\' : '/'
594 let l = getcompletion(root, 'shellcmd')
595 let expected = map(filter(glob(root . '*', 0, 1),
596 \ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
597 call assert_equal(expected, l)
598
Bram Moolenaarb650b982016-08-05 20:35:13 +0200599 if has('cscope')
600 let l = getcompletion('', 'cscope')
601 let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
602 call assert_equal(cmds, l)
603 " using cmdline completion must not change the result
604 call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
605 let l = getcompletion('', 'cscope')
606 call assert_equal(cmds, l)
607 let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
608 let l = getcompletion('find ', 'cscope')
609 call assert_equal(keys, l)
610 endif
611
Bram Moolenaar7522f692016-08-06 14:12:50 +0200612 if has('signs')
613 sign define Testing linehl=Comment
614 let l = getcompletion('', 'sign')
615 let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
616 call assert_equal(cmds, l)
617 " using cmdline completion must not change the result
618 call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
619 let l = getcompletion('', 'sign')
620 call assert_equal(cmds, l)
621 let l = getcompletion('list ', 'sign')
622 call assert_equal(['Testing'], l)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000623 let l = getcompletion('de*', 'sign')
624 call assert_equal(['define'], l)
625 let l = getcompletion('p?', 'sign')
626 call assert_equal(['place'], l)
627 let l = getcompletion('j.', 'sign')
628 call assert_equal(['jump'], l)
Bram Moolenaar7522f692016-08-06 14:12:50 +0200629 endif
630
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200631 " Command line completion tests
632 let l = getcompletion('cd ', 'cmdline')
633 call assert_true(index(l, 'samples/') >= 0)
634 let l = getcompletion('cd NoMatch', 'cmdline')
635 call assert_equal([], l)
636 let l = getcompletion('let v:n', 'cmdline')
637 call assert_true(index(l, 'v:null') >= 0)
638 let l = getcompletion('let v:notexists', 'cmdline')
639 call assert_equal([], l)
640 let l = getcompletion('call tag', 'cmdline')
641 call assert_true(index(l, 'taglist(') >= 0)
642 let l = getcompletion('call paint', 'cmdline')
643 call assert_equal([], l)
zeertzjqe4c79d32023-08-15 22:41:53 +0200644 let l = getcompletion('autocmd BufEnter * map <bu', 'cmdline')
645 call assert_equal(['<buffer>'], l)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +0200646
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100647 func T(a, c, p)
ii144785fe02021-11-21 12:13:56 +0000648 let g:cmdline_compl_params = [a:a, a:c, a:p]
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100649 return "oneA\noneB\noneC"
650 endfunc
651 command -nargs=1 -complete=custom,T MyCmd
652 let l = getcompletion('MyCmd ', 'cmdline')
653 call assert_equal(['oneA', 'oneB', 'oneC'], l)
ii144785fe02021-11-21 12:13:56 +0000654 call assert_equal(['', 'MyCmd ', 6], g:cmdline_compl_params)
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100655
656 delcommand MyCmd
657 delfunc T
ii144785fe02021-11-21 12:13:56 +0000658 unlet g:cmdline_compl_params
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +0100659
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200660 " For others test if the name is recognized.
Bram Moolenaar62fe66f2018-05-22 16:58:47 +0200661 let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200662 if has('cmdline_hist')
663 call add(names, 'history')
664 endif
665 if has('gettext')
666 call add(names, 'locale')
667 endif
668 if has('profile')
669 call add(names, 'syntime')
670 endif
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200671
672 set tags=Xtags
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100673 call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags', 'D')
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200674
675 for name in names
676 let matchcount = len(getcompletion('', name))
677 call assert_true(matchcount >= 0, 'No matches for ' . name)
678 endfor
679
Bram Moolenaar0331faf2019-06-15 18:40:37 +0200680 set tags&
Bram Moolenaarc1fb7632016-07-17 23:34:21 +0200681
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000682 edit a~b
683 enew
684 call assert_equal(['a~b'], getcompletion('a~', 'buffer'))
685 bw a~b
686
687 if has('unix')
688 edit Xtest\
689 enew
690 call assert_equal(['Xtest\'], getcompletion('Xtest\', 'buffer'))
691 bw Xtest\
692 endif
693
Bram Moolenaarb7e24832020-06-24 13:37:35 +0200694 call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200695 call assert_fails('call getcompletion("", "burp")', 'E475:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100696 call assert_fails('call getcompletion("abc", [])', 'E1174:')
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200697endfunc
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200698
Bram Moolenaarc32949b2023-01-04 15:56:51 +0000699func Test_multibyte_expression()
Bram Moolenaar2468add2023-01-04 18:59:57 +0000700 " Get a dialog in the GUI
701 CheckNotGui
702
Bram Moolenaarc32949b2023-01-04 15:56:51 +0000703 " This was using uninitialized memory.
704 let lines =<< trim END
705 set verbose=6
706 norm @=ٷ
707 qall!
708 END
709 call writefile(lines, 'XmultiScript', 'D')
710 call RunVim('', '', '-u NONE -n -e -s -S XmultiScript')
711endfunc
712
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +0000713" Test for getcompletion() with "fuzzy" in 'wildoptions'
714func Test_getcompletion_wildoptions()
715 let save_wildoptions = &wildoptions
716 set wildoptions&
717 let l = getcompletion('space', 'option')
718 call assert_equal([], l)
719 let l = getcompletion('ier', 'command')
720 call assert_equal([], l)
721 set wildoptions=fuzzy
722 let l = getcompletion('space', 'option')
723 call assert_true(index(l, 'backspace') >= 0)
724 let l = getcompletion('ier', 'command')
725 call assert_true(index(l, 'compiler') >= 0)
726 let &wildoptions = save_wildoptions
727endfunc
728
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000729func Test_complete_autoload_error()
730 let save_rtp = &rtp
731 let lines =<< trim END
732 vim9script
733 export def Complete(..._): string
734 return 'match'
735 enddef
736 echo this will cause an error
737 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100738 call mkdir('Xdir/autoload', 'pR')
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000739 call writefile(lines, 'Xdir/autoload/script.vim')
740 exe 'set rtp+=' .. getcwd() .. '/Xdir'
741
742 let lines =<< trim END
743 vim9script
744 import autoload 'script.vim'
745 command -nargs=* -complete=custom,script.Complete Cmd eval 0 + 0
746 &wildcharm = char2nr("\<Tab>")
747 feedkeys(":Cmd \<Tab>", 'xt')
748 END
749 call v9.CheckScriptFailure(lines, 'E121: Undefined variable: this')
750
751 let &rtp = save_rtp
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000752endfunc
753
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100754func Test_fullcommand()
755 let tests = {
756 \ '': '',
757 \ ':': '',
758 \ ':::': '',
759 \ ':::5': '',
760 \ 'not_a_cmd': '',
761 \ 'Check': '',
762 \ 'syntax': 'syntax',
763 \ ':syntax': 'syntax',
764 \ '::::syntax': 'syntax',
765 \ 'sy': 'syntax',
766 \ 'syn': 'syntax',
767 \ 'synt': 'syntax',
768 \ ':sy': 'syntax',
769 \ '::::sy': 'syntax',
770 \ 'match': 'match',
771 \ '2match': 'match',
772 \ '3match': 'match',
773 \ 'aboveleft': 'aboveleft',
774 \ 'abo': 'aboveleft',
Bram Moolenaaraa534142022-09-15 21:46:02 +0100775 \ 'en': 'endif',
776 \ 'end': 'endif',
777 \ 'endi': 'endif',
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100778 \ 's': 'substitute',
779 \ '5s': 'substitute',
780 \ ':5s': 'substitute',
781 \ "'<,'>s": 'substitute',
782 \ ":'<,'>s": 'substitute',
LemonBoycc766a82022-04-04 15:46:58 +0100783 \ 'CheckLin': 'CheckLinux',
784 \ 'CheckLinux': 'CheckLinux',
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100785 \ }
786
787 for [in, want] in items(tests)
788 call assert_equal(want, fullcommand(in))
789 endfor
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +0200790 call assert_equal('', fullcommand(test_null_string()))
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100791
792 call assert_equal('syntax', 'syn'->fullcommand())
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200793
794 command -buffer BufferLocalCommand :
795 command GlobalCommand :
796 call assert_equal('GlobalCommand', fullcommand('GlobalCom'))
797 call assert_equal('BufferLocalCommand', fullcommand('BufferL'))
798 delcommand BufferLocalCommand
799 delcommand GlobalCommand
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100800endfunc
801
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200802func Test_shellcmd_completion()
803 let save_path = $PATH
804
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100805 call mkdir('Xpathdir/Xpathsubdir', 'pR')
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200806 call writefile([''], 'Xpathdir/Xfile.exe')
807 call setfperm('Xpathdir/Xfile.exe', 'rwx------')
808
809 " Set PATH to example directory without trailing slash.
810 let $PATH = getcwd() . '/Xpathdir'
811
812 " Test for the ":!<TAB>" case. Previously, this would include subdirs of
813 " dirs in the PATH, even though they won't be executed. We check that only
814 " subdirs of the PWD and executables from the PATH are included in the
815 " suggestions.
816 let actual = getcompletion('X', 'shellcmd')
817 let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
818 call insert(expected, 'Xfile.exe')
819 call assert_equal(expected, actual)
820
Bram Moolenaar6ab9e422018-07-28 19:20:13 +0200821 let $PATH = save_path
822endfunc
823
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200824func Test_expand_star_star()
Bram Moolenaarbf630112023-05-19 21:41:02 +0100825 call mkdir('a/b/c', 'pR')
826 call writefile(['asdfasdf'], 'a/b/c/fileXname')
827 call feedkeys(":find a/**/fileXname\<Tab>\<CR>", 'xt')
828 call assert_equal('find a/b/c/fileXname', @:)
Bram Moolenaar1773ddf2016-08-28 13:38:54 +0200829 bwipe!
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +0200830endfunc
Bram Moolenaar21efc362016-12-03 14:05:49 +0100831
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100832func Test_cmdline_paste()
Bram Moolenaar21efc362016-12-03 14:05:49 +0100833 let @a = "def"
834 call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
835 call assert_equal('"abc def ghi', @:)
836
837 new
838 call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
839
840 call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
841 call assert_equal('"aaa asdf bbb', @:)
842
843 call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
844 call assert_equal('"aaa /tmp/some bbb', @:)
845
Bram Moolenaare2c8d832018-05-01 19:24:03 +0200846 call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
847 call assert_equal('"aaa '.getline(1).' bbb', @:)
848
Bram Moolenaar21efc362016-12-03 14:05:49 +0100849 set incsearch
850 call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
851 call assert_equal('"aaa verylongword bbb', @:)
852
853 call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
854 call assert_equal('"aaa a;b-c*d bbb', @:)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100855
856 call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
857 call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
Bram Moolenaar21efc362016-12-03 14:05:49 +0100858 bwipe!
Bram Moolenaar72532d32018-04-07 19:09:09 +0200859
860 " Error while typing a command used to cause that it was not executed
861 " in the end.
862 new
863 try
864 call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
865 catch /^Vim\%((\a\+)\)\=:E32/
866 " ignore error E32
867 endtry
868 call assert_equal("Xtestfile", bufname("%"))
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100869
Bram Moolenaar578fe942020-02-27 21:32:51 +0100870 " Try to paste an invalid register using <C-R>
871 call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
872 call assert_equal('"onetwo', @:)
873
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100874 " Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
Bram Moolenaar578fe942020-02-27 21:32:51 +0100875 let @a = "xy\<C-H>z"
876 call feedkeys(":\"\<C-R>a\<CR>", 'xt')
877 call assert_equal('"xz', @:)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100878 call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
879 call assert_equal("\"xy\<C-H>z", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +0100880 call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
881 call assert_equal("\"xy\<C-H>z", @:)
882
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100883 " Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
884 let @a = "xy\<C-V>z"
885 call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
886 call assert_equal('"xyz', @:)
887 call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
888 call assert_equal("\"xy\<C-V>z", @:)
889
Bram Moolenaar578fe942020-02-27 21:32:51 +0100890 call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
891
Bram Moolenaar72532d32018-04-07 19:09:09 +0200892 bwipe!
Bram Moolenaar21efc362016-12-03 14:05:49 +0100893endfunc
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100894
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100895func Test_cmdline_remove_char()
896 let encoding_save = &encoding
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100897
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100898 for e in ['utf8', 'latin1']
899 exe 'set encoding=' . e
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100900
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100901 call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
902 call assert_equal('"abc ef', @:, e)
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100903
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100904 call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
905 call assert_equal('"abcdef', @:)
906
907 call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
908 call assert_equal('"abc ghi', @:, e)
909
910 call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
911 call assert_equal('"def', @:, e)
Bram Moolenaaref02f162022-05-07 10:49:10 +0100912
913 " This was going before the start in latin1.
914 call feedkeys(": \<C-W>\<CR>", 'tx')
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100915 endfor
916
917 let &encoding = encoding_save
918endfunc
919
920func Test_cmdline_keymap_ctrl_hat()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200921 CheckFeature keymap
Bram Moolenaar59cb0412019-12-18 22:26:31 +0100922
923 set keymap=esperanto
924 call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
925 call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
926 set keymap=
Bram Moolenaareaaa9bb2016-12-09 18:42:20 +0100927endfunc
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100928
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100929func Test_illegal_address1()
Bram Moolenaarfe38b492016-12-11 21:34:23 +0100930 new
931 2;'(
932 2;')
933 quit
934endfunc
Bram Moolenaarba47b512017-01-24 21:18:19 +0100935
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100936func Test_illegal_address2()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100937 call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim', 'D')
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100938 new
939 source Xtest.vim
940 " Trigger calling validate_cursor()
941 diffsp Xtest.vim
942 quit!
943 bwipe!
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +0100944endfunc
945
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +0100946func Test_mark_from_line_zero()
947 " this was reading past the end of the first (empty) line
948 new
949 norm oxxxx
950 call assert_fails("0;'(", 'E20:')
951 bwipe!
952endfunc
953
Bram Moolenaarba47b512017-01-24 21:18:19 +0100954func Test_cmdline_complete_wildoptions()
955 help
956 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
957 let a = join(sort(split(@:)),' ')
958 set wildoptions=tagfile
959 call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
960 let b = join(sort(split(@:)),' ')
961 call assert_equal(a, b)
962 bw!
963endfunc
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +0100964
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200965func Test_cmdline_complete_user_cmd()
966 command! -complete=color -nargs=1 Foo :
967 call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
968 call assert_equal('"Foo blue', @:)
969 call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
970 call assert_equal('"Foo blue', @:)
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +0000971 call feedkeys(":Foo a b\<Tab>\<Home>\"\<cr>", 'tx')
972 call assert_equal('"Foo a blue', @:)
973 call feedkeys(":Foo b\\\<Tab>\<Home>\"\<cr>", 'tx')
974 call assert_equal('"Foo b\', @:)
975 call feedkeys(":Foo b\\x\<Tab>\<Home>\"\<cr>", 'tx')
976 call assert_equal('"Foo b\x', @:)
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200977 delcommand Foo
Bram Moolenaar300175f2022-08-21 18:38:21 +0100978
979 redraw
980 call assert_equal('~', Screenline(&lines - 1))
981 command! FooOne :
982 command! FooTwo :
983
984 set nowildmenu
985 call feedkeys(":Foo\<Tab>\<Home>\"\<cr>", 'tx')
986 call assert_equal('"FooOne', @:)
987 call assert_equal('~', Screenline(&lines - 1))
988
989 call feedkeys(":Foo\<S-Tab>\<Home>\"\<cr>", 'tx')
990 call assert_equal('"FooTwo', @:)
991 call assert_equal('~', Screenline(&lines - 1))
992
993 delcommand FooOne
994 delcommand FooTwo
995 set wildmenu&
Bram Moolenaara33ddbb2017-03-29 21:30:04 +0200996endfunc
997
Bram Moolenaarc2842ad2022-07-26 17:23:47 +0100998func Test_complete_user_cmd()
999 command FooBar echo 'global'
1000 command -buffer FooBar echo 'local'
1001 call feedkeys(":Foo\<C-A>\<Home>\"\<CR>", 'tx')
1002 call assert_equal('"FooBar', @:)
1003
1004 delcommand -buffer FooBar
1005 delcommand FooBar
1006endfunc
1007
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001008func s:ScriptLocalFunction()
1009 echo 'yes'
1010endfunc
1011
1012func Test_cmdline_complete_user_func()
1013 call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
naohiro ono5aec7552021-08-19 21:20:41 +02001014 call assert_match('"func Test_cmdline_complete_user_', @:)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001015 call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
1016 call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001017
1018 " g: prefix also works
1019 call feedkeys(":echo g:Test_cmdline_complete_user_f\<Tab>\<Home>\"\<cr>", 'tx')
1020 call assert_match('"echo g:Test_cmdline_complete_user_func', @:)
Bram Moolenaar069f9082021-08-13 17:48:25 +02001021
1022 " using g: prefix does not result in just "g:" matches from a lambda
1023 let Fx = { a -> a }
1024 call feedkeys(":echo g:\<Tab>\<Home>\"\<cr>", 'tx')
1025 call assert_match('"echo g:[A-Z]', @:)
naohiro ono5aec7552021-08-19 21:20:41 +02001026
1027 " existence of script-local dict function does not break user function name
1028 " completion
1029 function s:a_dict_func() dict
1030 endfunction
1031 call feedkeys(":call Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
1032 call assert_match('"call Test_cmdline_complete_user_', @:)
1033 delfunction s:a_dict_func
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001034endfunc
1035
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001036func Test_cmdline_complete_user_names()
1037 if has('unix') && executable('whoami')
1038 let whoami = systemlist('whoami')[0]
1039 let first_letter = whoami[0]
1040 if len(first_letter) > 0
1041 " Trying completion of :e ~x where x is the first letter of
1042 " the user name should complete to at least the user name.
1043 call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
1044 call assert_match('^"e \~.*\<' . whoami . '\>', @:)
1045 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001046 elseif has('win32')
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001047 " Just in case: check that the system has an Administrator account.
1048 let names = system('net user')
1049 if names =~ 'Administrator'
1050 " Trying completion of :e ~A should complete to Administrator.
Bram Moolenaar346d2a32019-01-27 20:43:41 +01001051 " There could be other names starting with "A" before Administrator.
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001052 call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
Bram Moolenaar346d2a32019-01-27 20:43:41 +01001053 call assert_match('^"e \~.*Administrator', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001054 endif
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001055 else
1056 throw 'Skipped: does not work on this platform'
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001057 endif
1058endfunc
1059
Bram Moolenaar297610b2019-12-27 17:20:55 +01001060func Test_cmdline_complete_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001061 CheckExecutable whoami
1062 call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
1063 call assert_match('^".*\<whoami\>', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +01001064endfunc
1065
Bram Moolenaar8b633132020-03-20 18:20:51 +01001066func Test_cmdline_complete_languages()
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001067 let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
1068 call assert_equal(lang, v:lc_time)
1069
1070 let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
1071 call assert_equal(lang, v:ctype)
1072
1073 let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
1074 call assert_equal(lang, v:collate)
1075
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001076 let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001077 call assert_equal(lang, v:lang)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001078
1079 call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001080 call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001081
Bram Moolenaarec680282020-06-12 19:35:32 +02001082 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001083
Bram Moolenaarec680282020-06-12 19:35:32 +02001084 call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
1085 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001086
Bram Moolenaarec680282020-06-12 19:35:32 +02001087 call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
1088 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001089
Bram Moolenaarec680282020-06-12 19:35:32 +02001090 call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
1091 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001092
1093 call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
1094 call assert_match('^"language .*\<' . lang . '\>', @:)
Bram Moolenaar5f8f2d32018-06-19 19:09:09 +02001095endfunc
1096
Bram Moolenaar297610b2019-12-27 17:20:55 +01001097func Test_cmdline_complete_env_variable()
1098 let $X_VIM_TEST_COMPLETE_ENV = 'foo'
Bram Moolenaar297610b2019-12-27 17:20:55 +01001099 call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
1100 call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
Bram Moolenaar297610b2019-12-27 17:20:55 +01001101 unlet $X_VIM_TEST_COMPLETE_ENV
1102endfunc
1103
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001104func Test_cmdline_complete_expression()
1105 let g:SomeVar = 'blah'
1106 for cmd in ['exe', 'echo', 'echon', 'echomsg']
1107 call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
1108 call assert_match('"' .. cmd .. ' SomeVar', @:)
1109 call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
1110 call assert_match('"' .. cmd .. ' foo SomeVar', @:)
1111 endfor
1112 unlet g:SomeVar
1113endfunc
1114
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001115func Test_cmdline_complete_argopt()
1116 " completion for ++opt=arg for file commands
1117 call assert_equal('fileformat=', getcompletion('edit ++', 'cmdline')[0])
1118 call assert_equal('encoding=', getcompletion('read ++e', 'cmdline')[0])
1119 call assert_equal('edit', getcompletion('read ++bin ++edi', 'cmdline')[0])
1120
1121 call assert_equal(['fileformat='], getcompletion('edit ++ff', 'cmdline'))
Yee Cheng Chin209ec902023-10-17 10:56:25 +02001122 " Test ++ff in the middle of the cmdline
1123 call feedkeys(":edit ++ff zz\<Left>\<Left>\<Left>\<C-A>\<C-B>\"\<CR>", 'xt')
1124 call assert_equal("\"edit ++fileformat= zz", @:)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001125
1126 call assert_equal('dos', getcompletion('write ++ff=d', 'cmdline')[0])
1127 call assert_equal('mac', getcompletion('args ++fileformat=m', 'cmdline')[0])
1128 call assert_equal('utf-8', getcompletion('split ++enc=ut*-8', 'cmdline')[0])
1129 call assert_equal('latin1', getcompletion('tabedit ++encoding=lati', 'cmdline')[0])
1130 call assert_equal('keep', getcompletion('edit ++bad=k', 'cmdline')[0])
1131
1132 call assert_equal([], getcompletion('edit ++bogus=', 'cmdline'))
1133
1134 " completion should skip the ++opt and continue
1135 call writefile([], 'Xaaaaa.txt', 'D')
1136 call feedkeys(":split ++enc=latin1 Xaaa\<C-A>\<C-B>\"\<CR>", 'xt')
1137 call assert_equal('"split ++enc=latin1 Xaaaaa.txt', @:)
1138
1139 if has('terminal')
1140 " completion for terminal's [options]
1141 call assert_equal('close', getcompletion('terminal ++cl*e', 'cmdline')[0])
1142 call assert_equal('hidden', getcompletion('terminal ++open ++hidd', 'cmdline')[0])
1143 call assert_equal('term', getcompletion('terminal ++kill=ter', 'cmdline')[0])
1144
1145 call assert_equal([], getcompletion('terminal ++bogus=', 'cmdline'))
1146
1147 " :terminal completion should skip the ++opt when considering what is the
1148 " first option, which is a list of shell commands, unlike second option
1149 " onwards.
1150 let first_param = getcompletion('terminal ', 'cmdline')
1151 let second_param = getcompletion('terminal foo ', 'cmdline')
1152 let skipped_opt_param = getcompletion('terminal ++close ', 'cmdline')
1153 call assert_equal(first_param, skipped_opt_param)
1154 call assert_notequal(first_param, second_param)
1155 endif
1156endfunc
1157
Bram Moolenaar47016f52021-08-26 16:39:58 +02001158" Unique function name for completion below
1159func s:WeirdFunc()
1160 echo 'weird'
1161endfunc
1162
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001163" Test for various command-line completion
1164func Test_cmdline_complete_various()
1165 " completion for a command starting with a comment
1166 call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
1167 call assert_equal("\" :|\"\<C-A>", @:)
1168
1169 " completion for a range followed by a comment
1170 call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
1171 call assert_equal("\"1,2\"\<C-A>", @:)
1172
1173 " completion for :k command
1174 call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
1175 call assert_equal("\"ka\<C-A>", @:)
1176
1177 " completion for short version of the :s command
1178 call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
1179 call assert_equal("\"sI \<C-A>", @:)
1180
1181 " completion for :write command
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001182 call mkdir('Xcwdir', 'R')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001183 call writefile(['one'], 'Xcwdir/Xfile1')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001184 let save_cwd = getcwd()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001185 cd Xcwdir
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001186 call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
1187 call assert_equal("\"w >> Xfile1", @:)
1188 call chdir(save_cwd)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001189
1190 " completion for :w ! and :r ! commands
1191 call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
1192 call assert_equal("\"w !invalid_xyz_cmd", @:)
1193 call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
1194 call assert_equal("\"r !invalid_xyz_cmd", @:)
1195
1196 " completion for :>> and :<< commands
1197 call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
1198 call assert_equal("\">>>\<C-A>", @:)
1199 call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
1200 call assert_equal("\"<<<\<C-A>", @:)
1201
1202 " completion for command with +cmd argument
1203 call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
1204 call assert_equal("\"buffer +/pat Xabc", @:)
1205 call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
1206 call assert_equal("\"buffer +/pat\<C-A>", @:)
1207
1208 " completion for a command with a trailing comment
1209 call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
1210 call assert_equal("\"ls \" comment\<C-A>", @:)
1211
1212 " completion for a command with a trailing command
1213 call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
1214 call assert_equal("\"ls | ls", @:)
1215
1216 " completion for a command with an CTRL-V escaped argument
1217 call feedkeys(":ls \<C-V>\<C-V>a\<C-A>\<C-B>\"\<CR>", 'xt')
1218 call assert_equal("\"ls \<C-V>a\<C-A>", @:)
1219
1220 " completion for a command that doesn't take additional arguments
1221 call feedkeys(":all abc\<C-A>\<C-B>\"\<CR>", 'xt')
1222 call assert_equal("\"all abc\<C-A>", @:)
1223
zeertzjqd3de1782022-09-01 12:58:52 +01001224 " completion for :wincmd with :horizontal modifier
1225 call feedkeys(":horizontal wincm\<C-A>\<C-B>\"\<CR>", 'xt')
1226 call assert_equal("\"horizontal wincmd", @:)
1227
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001228 " completion for a command with a command modifier
1229 call feedkeys(":topleft new\<C-A>\<C-B>\"\<CR>", 'xt')
1230 call assert_equal("\"topleft new", @:)
1231
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001232 " completion for vim9 and legacy commands
1233 call feedkeys(":vim9 call strle\<C-A>\<C-B>\"\<CR>", 'xt')
1234 call assert_equal("\"vim9 call strlen(", @:)
1235 call feedkeys(":legac call strle\<C-A>\<C-B>\"\<CR>", 'xt')
1236 call assert_equal("\"legac call strlen(", @:)
1237
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001238 " completion for the :disassemble command
1239 call feedkeys(":disas deb\<C-A>\<C-B>\"\<CR>", 'xt')
1240 call assert_equal("\"disas debug", @:)
1241 call feedkeys(":disas pro\<C-A>\<C-B>\"\<CR>", 'xt')
1242 call assert_equal("\"disas profile", @:)
1243 call feedkeys(":disas debug Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
1244 call assert_equal("\"disas debug Test_cmdline_complete_various", @:)
1245 call feedkeys(":disas profile Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
1246 call assert_equal("\"disas profile Test_cmdline_complete_various", @:)
naohiro ono9aecf792021-08-28 15:56:06 +02001247 call feedkeys(":disas Test_cmdline_complete_var\<C-A>\<C-B>\"\<CR>", 'xt')
1248 call assert_equal("\"disas Test_cmdline_complete_various", @:)
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001249
Bram Moolenaar47016f52021-08-26 16:39:58 +02001250 call feedkeys(":disas s:WeirdF\<C-A>\<C-B>\"\<CR>", 'xt')
naohiro ono9aecf792021-08-28 15:56:06 +02001251 call assert_match('"disas <SNR>\d\+_WeirdFunc', @:)
Bram Moolenaar47016f52021-08-26 16:39:58 +02001252
naohiro onodfe04db2021-09-12 15:45:10 +02001253 call feedkeys(":disas \<S-Tab>\<C-B>\"\<CR>", 'xt')
1254 call assert_match('"disas <SNR>\d\+_', @:)
1255 call feedkeys(":disas debug \<S-Tab>\<C-B>\"\<CR>", 'xt')
1256 call assert_match('"disas debug <SNR>\d\+_', @:)
1257
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001258 " completion for the :match command
1259 call feedkeys(":match Search /pat/\<C-A>\<C-B>\"\<CR>", 'xt')
1260 call assert_equal("\"match Search /pat/\<C-A>", @:)
1261
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001262 " completion for the :doautocmd command
1263 call feedkeys(":doautocmd User MyCmd a.c\<C-A>\<C-B>\"\<CR>", 'xt')
1264 call assert_equal("\"doautocmd User MyCmd a.c\<C-A>", @:)
1265
Bram Moolenaarafe8cf62020-10-05 20:07:18 +02001266 " completion of autocmd group after comma
1267 call feedkeys(":doautocmd BufNew,BufEn\<C-A>\<C-B>\"\<CR>", 'xt')
1268 call assert_equal("\"doautocmd BufNew,BufEnter", @:)
1269
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001270 " completion of file name in :doautocmd
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001271 call writefile([], 'Xvarfile1', 'D')
1272 call writefile([], 'Xvarfile2', 'D')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001273 call feedkeys(":doautocmd BufEnter Xvarfi\<C-A>\<C-B>\"\<CR>", 'xt')
1274 call assert_equal("\"doautocmd BufEnter Xvarfile1 Xvarfile2", @:)
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001275
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001276 " completion for the :augroup command
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02001277 augroup XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001278 augroup END
1279 call feedkeys(":augroup X\<C-A>\<C-B>\"\<CR>", 'xt')
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02001280 call assert_equal("\"augroup XTest.test", @:)
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01001281
1282 " group name completion in :autocmd
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02001283 call feedkeys(":au X\<C-A>\<C-B>\"\<CR>", 'xt')
1284 call assert_equal("\"au XTest.test", @:)
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01001285 call feedkeys(":au XTest.test\<Tab>\<C-B>\"\<CR>", 'xt')
1286 call assert_equal("\"au XTest.test", @:)
1287
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02001288 augroup! XTest.test
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001289
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01001290 " autocmd pattern completion
1291 call feedkeys(":au BufEnter *.py\<Tab>\<C-B>\"\<CR>", 'xt')
1292 call assert_equal("\"au BufEnter *.py\t", @:)
1293
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001294 " completion for the :unlet command
1295 call feedkeys(":unlet one two\<C-A>\<C-B>\"\<CR>", 'xt')
1296 call assert_equal("\"unlet one two", @:)
1297
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001298 " completion for the :buffer command with curlies
Bram Moolenaar39c47c32021-10-17 18:05:26 +01001299 " FIXME: what should happen on MS-Windows?
1300 if !has('win32')
1301 edit \{someFile}
1302 call feedkeys(":buf someFile\<C-A>\<C-B>\"\<CR>", 'xt')
1303 call assert_equal("\"buf {someFile}", @:)
1304 bwipe {someFile}
1305 endif
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001306
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001307 " completion for the :bdelete command
1308 call feedkeys(":bdel a b c\<C-A>\<C-B>\"\<CR>", 'xt')
1309 call assert_equal("\"bdel a b c", @:)
1310
1311 " completion for the :mapclear command
1312 call feedkeys(":mapclear \<C-A>\<C-B>\"\<CR>", 'xt')
1313 call assert_equal("\"mapclear <buffer>", @:)
1314
1315 " completion for user defined commands with menu names
1316 menu Test.foo :ls<CR>
1317 com -nargs=* -complete=menu MyCmd
1318 call feedkeys(":MyCmd Te\<C-A>\<C-B>\"\<CR>", 'xt')
1319 call assert_equal('"MyCmd Test.', @:)
1320 delcom MyCmd
1321 unmenu Test
1322
1323 " completion for user defined commands with mappings
1324 mapclear
1325 map <F3> :ls<CR>
1326 com -nargs=* -complete=mapping MyCmd
1327 call feedkeys(":MyCmd <F\<C-A>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001328 call assert_equal('"MyCmd <F3> <F4>', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001329 mapclear
1330 delcom MyCmd
1331
Yee Cheng Chin54844852023-10-09 18:12:31 +02001332 " Prepare for path completion
1333 call mkdir('Xa b c', 'D')
1334 defer delete('Xcomma,foobar.txt')
1335 call writefile([], 'Xcomma,foobar.txt')
1336
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001337 " completion for :set path= with multiple backslashes
Yee Cheng Chin54844852023-10-09 18:12:31 +02001338 call feedkeys(':set path=Xa\\\ b' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1339 call assert_equal('"set path=Xa\\\ b\\\ c/', @:)
1340 set path&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001341
1342 " completion for :set dir= with a backslash
Yee Cheng Chin54844852023-10-09 18:12:31 +02001343 call feedkeys(':set dir=Xa\ b' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1344 call assert_equal('"set dir=Xa\ b\ c/', @:)
1345 set dir&
1346
1347 " completion for :set tags= / set dictionary= with escaped commas
1348 if has('win32')
1349 " In Windows backslashes are rounded up, so both '\,' and '\\,' escape to
1350 " '\,'
1351 call feedkeys(':set dictionary=Xcomma\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1352 call assert_equal('"set dictionary=Xcomma\,foobar.txt', @:)
1353
1354 call feedkeys(':set tags=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1355 call assert_equal('"set tags=Xcomma\,foobar.txt', @:)
1356
1357 call feedkeys(':set tags=Xcomma\\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1358 call assert_equal('"set tags=Xcomma\\\,foo', @:) " Didn't find a match
1359
1360 " completion for :set dictionary= with escaped commas (same behavior, but
1361 " different internal code path from 'set tags=' for escaping the output)
1362 call feedkeys(':set tags=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1363 call assert_equal('"set tags=Xcomma\,foobar.txt', @:)
1364 else
1365 " In other platforms, backslashes are rounded down (since '\,' itself will
1366 " be escaped into ','). As a result '\\,' and '\\\,' escape to '\,'.
1367 call feedkeys(':set tags=Xcomma\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1368 call assert_equal('"set tags=Xcomma\,foo', @:) " Didn't find a match
1369
1370 call feedkeys(':set tags=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1371 call assert_equal('"set tags=Xcomma\\,foobar.txt', @:)
1372
1373 call feedkeys(':set dictionary=Xcomma\\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1374 call assert_equal('"set dictionary=Xcomma\\,foobar.txt', @:)
1375
1376 " completion for :set dictionary= with escaped commas (same behavior, but
1377 " different internal code path from 'set tags=' for escaping the output)
1378 call feedkeys(':set dictionary=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1379 call assert_equal('"set dictionary=Xcomma\\,foobar.txt', @:)
1380 endif
1381 set tags&
1382 set dictionary&
1383
1384 " completion for :set makeprg= with no escaped commas
1385 call feedkeys(':set makeprg=Xcomma,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1386 call assert_equal('"set makeprg=Xcomma,foobar.txt', @:)
1387
1388 if !has('win32')
1389 " Cannot create file with backslash in file name in Windows, so only test
1390 " this elsewhere.
1391 defer delete('Xcomma\,fooslash.txt')
1392 call writefile([], 'Xcomma\,fooslash.txt')
1393 call feedkeys(':set makeprg=Xcomma\\,foo' .. "\<C-A>\<C-B>\"\<CR>", 'xt')
1394 call assert_equal('"set makeprg=Xcomma\\,fooslash.txt', @:)
1395 endif
1396 set makeprg&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001397
1398 " completion for the :py3 commands
1399 call feedkeys(":py3\<C-A>\<C-B>\"\<CR>", 'xt')
1400 call assert_equal('"py3 py3do py3file', @:)
1401
Bram Moolenaardf749a22021-03-28 15:29:43 +02001402 " completion for the :vim9 commands
1403 call feedkeys(":vim9\<C-A>\<C-B>\"\<CR>", 'xt')
1404 call assert_equal('"vim9cmd vim9script', @:)
1405
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001406 " redir @" is not the start of a comment. So complete after that
1407 call feedkeys(":redir @\" | cwin\t\<C-B>\"\<CR>", 'xt')
1408 call assert_equal('"redir @" | cwindow', @:)
1409
1410 " completion after a backtick
1411 call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
1412 call assert_equal('"e `a1b2c', @:)
1413
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001414 " completion for :language command with an invalid argument
1415 call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
1416 call assert_equal("\"language dummy \t", @:)
1417
1418 " completion for commands after a :global command
Bram Moolenaar8b633132020-03-20 18:20:51 +01001419 call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
1420 call assert_equal('"g/a\xb/clearjumps', @:)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001421
1422 " completion with ambiguous user defined commands
1423 com TCmd1 echo 'TCmd1'
1424 com TCmd2 echo 'TCmd2'
1425 call feedkeys(":TCmd \t\<C-B>\"\<CR>", 'xt')
1426 call assert_equal('"TCmd ', @:)
1427 delcom TCmd1
1428 delcom TCmd2
1429
1430 " completion after a range followed by a pipe (|) character
1431 call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
1432 call assert_equal('"1,10 | chistory', @:)
Bram Moolenaar9c929712020-09-07 22:05:28 +02001433
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001434 " completion after a :global command
1435 call feedkeys(":g/a/chist\t\<C-B>\"\<CR>", 'xt')
1436 call assert_equal('"g/a/chistory', @:)
1437 call feedkeys(":g/a\\/chist\t\<C-B>\"\<CR>", 'xt')
1438 call assert_equal("\"g/a\\/chist\t", @:)
1439
Bram Moolenaar9c929712020-09-07 22:05:28 +02001440 " use <Esc> as the 'wildchar' for completion
1441 set wildchar=<Esc>
1442 call feedkeys(":g/a\\xb/clearj\<Esc>\<C-B>\"\<CR>", 'xt')
1443 call assert_equal('"g/a\xb/clearjumps', @:)
1444 " pressing <esc> twice should cancel the command
1445 call feedkeys(":chist\<Esc>\<Esc>", 'xt')
1446 call assert_equal('"g/a\xb/clearjumps', @:)
1447 set wildchar&
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001448
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001449 if has('unix')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001450 " should be able to complete a file name that starts with a '~'.
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001451 call writefile([], '~Xtest')
1452 call feedkeys(":e \\~X\<Tab>\<C-B>\"\<CR>", 'xt')
1453 call assert_equal('"e \~Xtest', @:)
1454 call delete('~Xtest')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001455
1456 " should be able to complete a file name that has a '*'
1457 call writefile([], 'Xx*Yy')
1458 call feedkeys(":e Xx\*\<Tab>\<C-B>\"\<CR>", 'xt')
1459 call assert_equal('"e Xx\*Yy', @:)
1460 call delete('Xx*Yy')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001461
1462 " use a literal star
1463 call feedkeys(":e \\*\<Tab>\<C-B>\"\<CR>", 'xt')
1464 call assert_equal('"e \*', @:)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001465 endif
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001466
1467 call feedkeys(":py3f\<Tab>\<C-B>\"\<CR>", 'xt')
1468 call assert_equal('"py3file', @:)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001469endfunc
1470
zeertzjq094dd152023-06-15 22:51:57 +01001471" Test that expanding a pattern doesn't interfere with cmdline completion.
1472func Test_expand_during_cmdline_completion()
1473 func ExpandStuff()
1474 badd <script>:p:h/README.*
1475 call assert_equal(expand('<script>:p:h') .. '/README.txt', bufname('$'))
1476 $bwipe
1477 call assert_equal('README.txt', expand('README.*'))
1478 call assert_equal(['README.txt'], getcompletion('README.*', 'file'))
1479 endfunc
1480 augroup test_CmdlineChanged
1481 autocmd!
1482 autocmd CmdlineChanged * call ExpandStuff()
1483 augroup END
1484
1485 call feedkeys(":sign \<Tab>\<Tab>\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
1486 call assert_equal('"sign place', @:)
1487
1488 augroup test_CmdlineChanged
1489 au!
1490 augroup END
1491 augroup! test_CmdlineChanged
1492 delfunc ExpandStuff
1493endfunc
1494
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001495" Test for 'wildignorecase'
1496func Test_cmdline_wildignorecase()
1497 CheckUnix
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001498 call writefile([], 'XTEST', 'D')
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001499 set wildignorecase
1500 call feedkeys(":e xt\<Tab>\<C-B>\"\<CR>", 'xt')
1501 call assert_equal('"e XTEST', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001502 call assert_equal(['XTEST'], getcompletion('xt', 'file'))
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001503 let g:Sline = ''
1504 call feedkeys(":e xt\<C-d>\<F4>\<C-B>\"\<CR>", 'xt')
1505 call assert_equal('"e xt', @:)
1506 call assert_equal('XTEST', g:Sline)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001507 set wildignorecase&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001508endfunc
1509
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001510func Test_cmdline_write_alternatefile()
1511 new
1512 call setline('.', ['one', 'two'])
1513 f foo.txt
1514 new
1515 f #-A
1516 call assert_equal('foo.txt-A', expand('%'))
1517 f #<-B.txt
1518 call assert_equal('foo-B.txt', expand('%'))
1519 f %<
1520 call assert_equal('foo-B', expand('%'))
1521 new
Bram Moolenaare2e40752020-09-04 21:18:46 +02001522 call assert_fails('f #<', 'E95:')
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02001523 bw!
1524 f foo-B.txt
1525 f %<-A
1526 call assert_equal('foo-B-A', expand('%'))
1527 bw!
1528 bw!
1529endfunc
1530
Bram Moolenaarf5724372022-09-02 19:45:15 +01001531func Test_cmdline_expand_cur_alt_file()
1532 enew
1533 file http://some.com/file.txt
1534 call feedkeys(":e %\<Tab>\<C-B>\"\<CR>", 'xt')
1535 call assert_equal('"e http://some.com/file.txt', @:)
1536 edit another
1537 call feedkeys(":e #\<Tab>\<C-B>\"\<CR>", 'xt')
1538 call assert_equal('"e http://some.com/file.txt', @:)
1539 bwipe
1540 bwipe http://some.com/file.txt
1541endfunc
1542
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001543" using a leading backslash here
1544set cpo+=C
1545
1546func Test_cmdline_search_range()
1547 new
1548 call setline(1, ['a', 'b', 'c', 'd'])
1549 /d
1550 1,\/s/b/B/
1551 call assert_equal('B', getline(2))
1552
1553 /a
1554 $
1555 \?,4s/c/C/
1556 call assert_equal('C', getline(3))
1557
1558 call setline(1, ['a', 'b', 'c', 'd'])
1559 %s/c/c/
1560 1,\&s/b/B/
1561 call assert_equal('B', getline(2))
1562
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001563 let @/ = 'apple'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001564 call assert_fails('\/print', ['E486:.*apple'])
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001565
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01001566 bwipe!
1567endfunc
1568
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001569" Test for the tick mark (') in an excmd range
1570func Test_tick_mark_in_range()
1571 " If only the tick is passed as a range and no command is specified, there
1572 " should not be an error
1573 call feedkeys(":'\<CR>", 'xt')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001574 call assert_equal("'", @:)
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001575 call assert_fails("',print", 'E78:')
1576endfunc
1577
1578" Test for using a line number followed by a search pattern as range
1579func Test_lnum_and_pattern_as_range()
1580 new
1581 call setline(1, ['foo 1', 'foo 2', 'foo 3'])
1582 let @" = ''
1583 2/foo/yank
1584 call assert_equal("foo 3\n", @")
1585 call assert_equal(1, line('.'))
1586 close!
1587endfunc
1588
Bram Moolenaar65189a12017-02-06 22:22:17 +01001589" Tests for getcmdline(), getcmdpos() and getcmdtype()
1590func Check_cmdline(cmdtype)
1591 call assert_equal('MyCmd a', getcmdline())
1592 call assert_equal(8, getcmdpos())
1593 call assert_equal(a:cmdtype, getcmdtype())
1594 return ''
1595endfunc
1596
Bram Moolenaar96e38a82019-09-09 18:35:33 +02001597set cpo&
1598
Bram Moolenaar65189a12017-02-06 22:22:17 +01001599func Test_getcmdtype()
1600 call feedkeys(":MyCmd a\<C-R>=Check_cmdline(':')\<CR>\<Esc>", "xt")
1601
1602 let cmdtype = ''
1603 debuggreedy
1604 call feedkeys(":debug echo 'test'\<CR>", "t")
1605 call feedkeys("let cmdtype = \<C-R>=string(getcmdtype())\<CR>\<CR>", "t")
1606 call feedkeys("cont\<CR>", "xt")
1607 0debuggreedy
1608 call assert_equal('>', cmdtype)
1609
1610 call feedkeys("/MyCmd a\<C-R>=Check_cmdline('/')\<CR>\<Esc>", "xt")
1611 call feedkeys("?MyCmd a\<C-R>=Check_cmdline('?')\<CR>\<Esc>", "xt")
1612
1613 call feedkeys(":call input('Answer?')\<CR>", "t")
Bram Moolenaar31eb1392017-02-09 21:44:03 +01001614 call feedkeys("MyCmd a\<C-R>=Check_cmdline('@')\<CR>\<C-C>", "xt")
Bram Moolenaar65189a12017-02-06 22:22:17 +01001615
1616 call feedkeys(":insert\<CR>MyCmd a\<C-R>=Check_cmdline('-')\<CR>\<Esc>", "xt")
1617
1618 cnoremap <expr> <F6> Check_cmdline('=')
1619 call feedkeys("a\<C-R>=MyCmd a\<F6>\<Esc>\<Esc>", "xt")
1620 cunmap <F6>
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001621
1622 call assert_equal('', getcmdline())
Bram Moolenaar65189a12017-02-06 22:22:17 +01001623endfunc
1624
Bram Moolenaar52604f22017-04-07 16:17:39 +02001625func Test_verbosefile()
1626 set verbosefile=Xlog
1627 echomsg 'foo'
1628 echomsg 'bar'
1629 set verbosefile=
1630 let log = readfile('Xlog')
1631 call assert_match("foo\nbar", join(log, "\n"))
1632 call delete('Xlog')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001633
1634 call mkdir('Xdir', 'D')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001635 call assert_fails('set verbosefile=Xdir', ['E484:.*Xdir', 'E474:'])
Bram Moolenaar52604f22017-04-07 16:17:39 +02001636endfunc
1637
Bram Moolenaar4facea32019-10-12 20:17:40 +02001638func Test_verbose_option()
1639 CheckScreendump
1640
1641 let lines =<< trim [SCRIPT]
1642 command DoSomething echo 'hello' |set ts=4 |let v = '123' |echo v
1643 call feedkeys("\r", 't') " for the hit-enter prompt
1644 set verbose=20
1645 [SCRIPT]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001646 call writefile(lines, 'XTest_verbose', 'D')
Bram Moolenaar4facea32019-10-12 20:17:40 +02001647
1648 let buf = RunVimInTerminal('-S XTest_verbose', {'rows': 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001649 call TermWait(buf, 50)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001650 call term_sendkeys(buf, ":DoSomething\<CR>")
1651 call VerifyScreenDump(buf, 'Test_verbose_option_1', {})
1652
1653 " clean up
1654 call StopVimInTerminal(buf)
Bram Moolenaar4facea32019-10-12 20:17:40 +02001655endfunc
1656
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001657func Test_setcmdpos()
1658 func InsertTextAtPos(text, pos)
1659 call assert_equal(0, setcmdpos(a:pos))
1660 return a:text
1661 endfunc
1662
1663 " setcmdpos() with position in the middle of the command line.
1664 call feedkeys(":\"12\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1665 call assert_equal('"1ab2', @:)
1666
1667 call feedkeys(":\"12\<C-R>\<C-R>=InsertTextAtPos('a', 3)\<CR>b\<CR>", 'xt')
1668 call assert_equal('"1b2a', @:)
1669
1670 " setcmdpos() with position beyond the end of the command line.
1671 call feedkeys(":\"12\<C-B>\<C-R>=InsertTextAtPos('a', 10)\<CR>b\<CR>", 'xt')
1672 call assert_equal('"12ab', @:)
1673
1674 " setcmdpos() returns 1 when not editing the command line.
Bram Moolenaar196b4662019-09-06 21:34:30 +02001675 call assert_equal(1, 3->setcmdpos())
Bram Moolenaarff3be4f2018-05-12 13:18:46 +02001676endfunc
1677
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001678func Test_cmdline_overstrike()
Bram Moolenaar30276f22019-01-24 17:59:39 +01001679 let encodings = ['latin1', 'utf8']
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001680 let encoding_save = &encoding
1681
1682 for e in encodings
1683 exe 'set encoding=' . e
1684
1685 " Test overstrike in the middle of the command line.
1686 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001687 call assert_equal('"0ab1cd4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001688
1689 " Test overstrike going beyond end of command line.
1690 call feedkeys(":\"01234\<home>\<right>\<right>ab\<right>\<insert>cdefgh\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001691 call assert_equal('"0ab1cdefgh', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001692
1693 " Test toggling insert/overstrike a few times.
1694 call feedkeys(":\"01234\<home>\<right>ab\<right>\<insert>cd\<right>\<insert>ef\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001695 call assert_equal('"ab0cd3ef4', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001696 endfor
1697
Bram Moolenaar30276f22019-01-24 17:59:39 +01001698 " Test overstrike with multi-byte characters.
1699 call feedkeys(":\"テキストエディタ\<home>\<right>\<right>ab\<right>\<insert>cd\<enter>", 'xt')
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001700 call assert_equal('"テabキcdエディタ', @:, e)
Bram Moolenaarc0676ba2018-12-31 21:03:02 +01001701
1702 let &encoding = encoding_save
1703endfunc
Bram Moolenaara046b372019-09-15 17:26:07 +02001704
Bram Moolenaar52410572019-10-27 05:12:45 +01001705func Test_buffers_lastused()
1706 " check that buffers are sorted by time when wildmode has lastused
1707 call test_settime(1550020000) " middle
1708 edit bufa
1709 enew
1710 call test_settime(1550030000) " newest
1711 edit bufb
1712 enew
1713 call test_settime(1550010000) " oldest
1714 edit bufc
1715 enew
1716 call test_settime(0)
1717 enew
1718
1719 call assert_equal(['bufa', 'bufb', 'bufc'],
1720 \ getcompletion('', 'buffer'))
1721
1722 let save_wildmode = &wildmode
1723 set wildmode=full:lastused
1724
1725 let cap = "\<c-r>=execute('let X=getcmdline()')\<cr>"
1726 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1727 call assert_equal('b bufb', X)
1728 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1729 call assert_equal('b bufa', X)
1730 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1731 call assert_equal('b bufc', X)
1732 enew
1733
1734 edit other
1735 call feedkeys(":b \<tab>" .. cap .. "\<esc>", 'xt')
1736 call assert_equal('b bufb', X)
1737 call feedkeys(":b \<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1738 call assert_equal('b bufa', X)
1739 call feedkeys(":b \<tab>\<tab>\<tab>" .. cap .. "\<esc>", 'xt')
1740 call assert_equal('b bufc', X)
1741 enew
1742
1743 let &wildmode = save_wildmode
1744
1745 bwipeout bufa
1746 bwipeout bufb
1747 bwipeout bufc
1748endfunc
Bram Moolenaar85db5472019-12-04 15:11:08 +01001749
Bram Moolenaar479950f2020-01-19 15:45:17 +01001750func Test_cmdlineclear_tabenter()
1751 CheckScreendump
1752
1753 let lines =<< trim [SCRIPT]
1754 call setline(1, range(30))
1755 [SCRIPT]
1756
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001757 call writefile(lines, 'XtestCmdlineClearTabenter', 'D')
Bram Moolenaar479950f2020-01-19 15:45:17 +01001758 let buf = RunVimInTerminal('-S XtestCmdlineClearTabenter', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001759 call TermWait(buf, 25)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001760 " in one tab make the command line higher with CTRL-W -
1761 call term_sendkeys(buf, ":tabnew\<cr>\<C-w>-\<C-w>-gtgt")
1762 call VerifyScreenDump(buf, 'Test_cmdlineclear_tabenter', {})
1763
1764 call StopVimInTerminal(buf)
Bram Moolenaar479950f2020-01-19 15:45:17 +01001765endfunc
1766
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001767" Test for expanding special keywords in cmdline
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001768func Test_cmdline_expand_special()
1769 %bwipe!
Bram Moolenaarb8bd2e62021-08-21 17:13:14 +02001770 call assert_fails('e #', 'E194:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001771 call assert_fails('e <afile>', 'E495:')
1772 call assert_fails('e <abuf>', 'E496:')
1773 call assert_fails('e <amatch>', 'E497:')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001774
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001775 call writefile([], 'Xfile.cpp', 'D')
1776 call writefile([], 'Xfile.java', 'D')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02001777 new Xfile.cpp
1778 call feedkeys(":e %:r\<C-A>\<C-B>\"\<CR>", 'xt')
1779 call assert_equal('"e Xfile.cpp Xfile.java', @:)
1780 close
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001781endfunc
1782
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001783" Test for backtick expression in the command line
1784func Test_cmd_backtick()
1785 %argd
1786 argadd `=['a', 'b', 'c']`
1787 call assert_equal(['a', 'b', 'c'], argv())
1788 %argd
Dominique Pellebfb2bb12021-08-14 21:11:51 +02001789
1790 argadd `echo abc def`
1791 call assert_equal(['abc def'], argv())
1792 %argd
Bram Moolenaarf0cee192020-02-16 13:33:56 +01001793endfunc
1794
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001795" Test for the :! command
1796func Test_cmd_bang()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001797 CheckUnix
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001798
1799 let lines =<< trim [SCRIPT]
1800 " Test for no previous command
1801 call assert_fails('!!', 'E34:')
1802 set nomore
1803 " Test for cmdline expansion with :!
1804 call setline(1, 'foo!')
1805 silent !echo <cWORD> > Xfile.out
1806 call assert_equal(['foo!'], readfile('Xfile.out'))
1807 " Test for using previous command
1808 silent !echo \! !
1809 call assert_equal(['! echo foo!'], readfile('Xfile.out'))
1810 call writefile(v:errors, 'Xresult')
1811 call delete('Xfile.out')
1812 qall!
1813 [SCRIPT]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001814 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001815 if RunVim([], [], '--clean -S Xscript')
1816 call assert_equal([], readfile('Xresult'))
1817 endif
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001818 call delete('Xresult')
1819endfunc
1820
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001821" Test error: "E135: *Filter* Autocommands must not change current buffer"
1822func Test_cmd_bang_E135()
1823 new
1824 call setline(1, ['a', 'b', 'c', 'd'])
1825 augroup test_cmd_filter_E135
1826 au!
1827 autocmd FilterReadPost * help
1828 augroup END
1829 call assert_fails('2,3!echo "x"', 'E135:')
1830
1831 augroup test_cmd_filter_E135
1832 au!
1833 augroup END
zeertzjq094dd152023-06-15 22:51:57 +01001834 augroup! test_cmd_filter_E135
Bram Moolenaare0c3c3d2020-06-04 22:46:04 +02001835 %bwipe!
1836endfunc
1837
shane.xb.qian4e7590e2022-11-08 21:40:04 +00001838func Test_cmd_bang_args()
1839 new
1840 :.!
1841 call assert_equal(0, v:shell_error)
1842
1843 " Note that below there is one space char after the '!'. This caused a
1844 " shell error in the past, see https://github.com/vim/vim/issues/11495.
1845 :.!
1846 call assert_equal(0, v:shell_error)
1847 bwipe!
1848
1849 CheckUnix
1850 :.!pwd
1851 call assert_equal(0, v:shell_error)
1852 :.! pwd
1853 call assert_equal(0, v:shell_error)
1854
1855 " Note there is one space after 'pwd'.
1856 :.! pwd
1857 call assert_equal(0, v:shell_error)
1858
1859 " Note there are two spaces after 'pwd'.
1860 :.! pwd
1861 call assert_equal(0, v:shell_error)
1862 :.!ls ~
1863 call assert_equal(0, v:shell_error)
1864
1865 " Note there is one space char after '~'.
1866 :.!ls ~
1867 call assert_equal(0, v:shell_error)
1868
1869 " Note there are two spaces after '~'.
1870 :.!ls ~
1871 call assert_equal(0, v:shell_error)
1872
1873 :.!echo "foo"
1874 call assert_equal(getline('.'), "foo")
1875 :.!echo "foo "
1876 call assert_equal(getline('.'), "foo ")
1877 :.!echo " foo "
1878 call assert_equal(getline('.'), " foo ")
1879 :.!echo " foo "
1880 call assert_equal(getline('.'), " foo ")
1881
1882 %bwipe!
1883endfunc
1884
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001885" Test for using ~ for home directory in cmdline completion matches
1886func Test_cmdline_expand_home()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001887 call mkdir('Xexpdir', 'R')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001888 call writefile([], 'Xexpdir/Xfile1')
1889 call writefile([], 'Xexpdir/Xfile2')
1890 cd Xexpdir
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001891 let save_HOME = $HOME
1892 let $HOME = getcwd()
1893 call feedkeys(":e ~/\<C-A>\<C-B>\"\<CR>", 'xt')
1894 call assert_equal('"e ~/Xfile1 ~/Xfile2', @:)
1895 let $HOME = save_HOME
1896 cd ..
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001897endfunc
1898
Bram Moolenaar578fe942020-02-27 21:32:51 +01001899" Test for using CTRL-\ CTRL-G in the command line to go back to normal mode
1900" or insert mode (when 'insertmode' is set)
1901func Test_cmdline_ctrl_g()
1902 new
1903 call setline(1, 'abc')
1904 call cursor(1, 3)
1905 " If command line is entered from insert mode, using C-\ C-G should back to
1906 " insert mode
1907 call feedkeys("i\<C-O>:\<C-\>\<C-G>xy", 'xt')
1908 call assert_equal('abxyc', getline(1))
1909 call assert_equal(4, col('.'))
1910
1911 " If command line is entered in 'insertmode', using C-\ C-G should back to
1912 " 'insertmode'
1913 call feedkeys(":set im\<cr>\<C-L>:\<C-\>\<C-G>12\<C-L>:set noim\<cr>", 'xt')
1914 call assert_equal('ab12xyc', getline(1))
1915 close!
1916endfunc
1917
Bram Moolenaar578fe942020-02-27 21:32:51 +01001918" Test for 'wildmode'
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001919func Wildmode_tests()
Bram Moolenaar578fe942020-02-27 21:32:51 +01001920 func T(a, c, p)
1921 return "oneA\noneB\noneC"
1922 endfunc
1923 command -nargs=1 -complete=custom,T MyCmd
1924
Bram Moolenaar578fe942020-02-27 21:32:51 +01001925 set nowildmenu
1926 set wildmode=full,list
1927 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001928 call feedkeys(":MyCmd \t\t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001929 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001930 call assert_equal('"MyCmd oneA', @:)
1931
1932 set wildmode=longest,full
1933 call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
1934 call assert_equal('"MyCmd one', @:)
1935 call feedkeys(":MyCmd o\t\t\t\t\<C-B>\"\<CR>", 'xt')
1936 call assert_equal('"MyCmd oneC', @:)
1937
1938 set wildmode=longest
1939 call feedkeys(":MyCmd one\t\t\<C-B>\"\<CR>", 'xt')
1940 call assert_equal('"MyCmd one', @:)
1941
1942 set wildmode=list:longest
1943 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001944 call feedkeys(":MyCmd \t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001945 call assert_equal('oneA oneB oneC', g:Sline)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001946 call assert_equal('"MyCmd one', @:)
1947
1948 set wildmode=""
1949 call feedkeys(":MyCmd \t\t\<C-B>\"\<CR>", 'xt')
1950 call assert_equal('"MyCmd oneA', @:)
1951
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001952 " Test for wildmode=longest with 'fileignorecase' set
1953 set wildmode=longest
1954 set fileignorecase
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001955 argadd AAA AAAA AAAAA
1956 call feedkeys(":buffer a\t\<C-B>\"\<CR>", 'xt')
1957 call assert_equal('"buffer AAA', @:)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001958 set fileignorecase&
1959
1960 " Test for listing files with wildmode=list
1961 set wildmode=list
1962 let g:Sline = ''
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001963 call feedkeys(":b A\t\t\<F4>\<C-B>\"\<CR>", 'xt')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001964 call assert_equal('AAA AAAA AAAAA', g:Sline)
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001965 call assert_equal('"b A', @:)
1966
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001967 " when using longest completion match, matches shorter than the argument
1968 " should be ignored (happens with :help)
1969 set wildmode=longest,full
1970 set wildmenu
1971 call feedkeys(":help a*\t\<C-B>\"\<CR>", 'xt')
1972 call assert_equal('"help a', @:)
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00001973 " non existing file
1974 call feedkeys(":e a1b2y3z4\t\<C-B>\"\<CR>", 'xt')
1975 call assert_equal('"e a1b2y3z4', @:)
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +00001976 set wildmenu&
1977
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001978 " Test for longest file name completion with 'fileignorecase'
1979 " On MS-Windows, file names are case insensitive.
1980 if has('unix')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01001981 call writefile([], 'XTESTfoo', 'D')
1982 call writefile([], 'Xtestbar', 'D')
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001983 set nofileignorecase
1984 call feedkeys(":e XT\<Tab>\<C-B>\"\<CR>", 'xt')
1985 call assert_equal('"e XTESTfoo', @:)
1986 call feedkeys(":e Xt\<Tab>\<C-B>\"\<CR>", 'xt')
1987 call assert_equal('"e Xtestbar', @:)
1988 set fileignorecase
1989 call feedkeys(":e XT\<Tab>\<C-B>\"\<CR>", 'xt')
1990 call assert_equal('"e Xtest', @:)
1991 call feedkeys(":e Xt\<Tab>\<C-B>\"\<CR>", 'xt')
1992 call assert_equal('"e Xtest', @:)
1993 set fileignorecase&
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001994 endif
1995
Bram Moolenaar24ebd832020-03-16 21:25:24 +01001996 %argdelete
Bram Moolenaar578fe942020-02-27 21:32:51 +01001997 delcommand MyCmd
1998 delfunc T
Bram Moolenaar578fe942020-02-27 21:32:51 +01001999 set wildmode&
Bram Moolenaar24ebd832020-03-16 21:25:24 +01002000 %bwipe!
Bram Moolenaar578fe942020-02-27 21:32:51 +01002001endfunc
2002
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00002003func Test_wildmode()
2004 " Test with utf-8 encoding
2005 call Wildmode_tests()
2006
2007 " Test with latin1 encoding
2008 let save_encoding = &encoding
2009 set encoding=latin1
2010 call Wildmode_tests()
2011 let &encoding = save_encoding
2012endfunc
2013
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00002014func Test_custom_complete_autoload()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002015 call mkdir('Xcustdir/autoload', 'pR')
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00002016 let save_rtp = &rtp
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002017 exe 'set rtp=' .. getcwd() .. '/Xcustdir'
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00002018 let lines =<< trim END
2019 func vim8#Complete(a, c, p)
2020 return "oneA\noneB\noneC"
2021 endfunc
2022 END
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002023 call writefile(lines, 'Xcustdir/autoload/vim8.vim')
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00002024
2025 command -nargs=1 -complete=custom,vim8#Complete MyCmd
2026 set nowildmenu
2027 set wildmode=full,list
2028 call feedkeys(":MyCmd \<C-A>\<C-B>\"\<CR>", 'xt')
2029 call assert_equal('"MyCmd oneA oneB oneC', @:)
2030
2031 let &rtp = save_rtp
2032 set wildmode& wildmenu&
2033 delcommand MyCmd
Bram Moolenaarda6d42c2022-03-17 11:46:55 +00002034endfunc
2035
Bram Moolenaar578fe942020-02-27 21:32:51 +01002036" Test for interrupting the command-line completion
2037func Test_interrupt_compl()
2038 func F(lead, cmdl, p)
2039 if a:lead =~ 'tw'
2040 call interrupt()
2041 return
2042 endif
2043 return "one\ntwo\nthree"
2044 endfunc
2045 command -nargs=1 -complete=custom,F Tcmd
2046
2047 set nowildmenu
2048 set wildmode=full
2049 let interrupted = 0
2050 try
2051 call feedkeys(":Tcmd tw\<Tab>\<C-B>\"\<CR>", 'xt')
2052 catch /^Vim:Interrupt$/
2053 let interrupted = 1
2054 endtry
2055 call assert_equal(1, interrupted)
2056
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002057 let interrupted = 0
2058 try
2059 call feedkeys(":Tcmd tw\<C-d>\<C-B>\"\<CR>", 'xt')
2060 catch /^Vim:Interrupt$/
2061 let interrupted = 1
2062 endtry
2063 call assert_equal(1, interrupted)
2064
Bram Moolenaar578fe942020-02-27 21:32:51 +01002065 delcommand Tcmd
2066 delfunc F
2067 set wildmode&
2068endfunc
2069
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01002070" Test for moving the cursor on the : command line
Bram Moolenaar578fe942020-02-27 21:32:51 +01002071func Test_cmdline_edit()
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01002072 let str = ":one two\<C-U>"
2073 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01002074 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01002075 let str ..= "\<Left>five\<Right>"
2076 let str ..= "\<Home>two "
2077 let str ..= "\<C-Left>one "
2078 let str ..= "\<C-Right> three"
2079 let str ..= "\<End>\<S-Left>four "
2080 let str ..= "\<S-Right> six"
2081 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
2082 call feedkeys(str, 'xt')
2083 call assert_equal("\"one two three four five six seven", @:)
2084endfunc
2085
2086" Test for moving the cursor on the / command line in 'rightleft' mode
2087func Test_cmdline_edit_rightleft()
2088 CheckFeature rightleft
2089 set rightleft
2090 set rightleftcmd=search
2091 let str = "/one two\<C-U>"
2092 let str ..= "one two\<C-W>\<C-W>"
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01002093 let str ..= "four\<BS>\<C-H>\<Del>\<kDel>"
Bram Moolenaard30ae2f2020-02-29 14:23:58 +01002094 let str ..= "\<Right>five\<Left>"
2095 let str ..= "\<Home>two "
2096 let str ..= "\<C-Right>one "
2097 let str ..= "\<C-Left> three"
2098 let str ..= "\<End>\<S-Right>four "
2099 let str ..= "\<S-Left> six"
2100 let str ..= "\<C-B>\"\<C-E> seven\<CR>"
2101 call assert_fails("call feedkeys(str, 'xt')", 'E486:')
2102 call assert_equal("\"one two three four five six seven", @/)
2103 set rightleftcmd&
2104 set rightleft&
2105endfunc
2106
2107" Test for using <C-\>e in the command line to evaluate an expression
2108func Test_cmdline_expr()
2109 " Evaluate an expression from the beginning of a command line
2110 call feedkeys(":abc\<C-B>\<C-\>e\"\\\"hello\"\<CR>\<CR>", 'xt')
2111 call assert_equal('"hello', @:)
2112
2113 " Use an invalid expression for <C-\>e
2114 call assert_beeps('call feedkeys(":\<C-\>einvalid\<CR>", "tx")')
2115
2116 " Insert literal <CTRL-\> in the command line
2117 call feedkeys(":\"e \<C-\>\<C-Y>\<CR>", 'xt')
2118 call assert_equal("\"e \<C-\>\<C-Y>", @:)
Bram Moolenaar578fe942020-02-27 21:32:51 +01002119endfunc
2120
Bram Moolenaar6046ade2022-06-22 13:51:54 +01002121" This was making the insert position negative
2122func Test_cmdline_expr_register()
2123 exe "sil! norm! ?\<C-\>e0\<C-R>0\<Esc>?\<C-\>e0\<CR>"
2124endfunc
2125
Bram Moolenaar0546d7d2020-03-01 16:53:09 +01002126" Test for 'imcmdline' and 'imsearch'
2127" This test doesn't actually test the input method functionality.
2128func Test_cmdline_inputmethod()
2129 new
2130 call setline(1, ['', 'abc', ''])
2131 set imcmdline
2132
2133 call feedkeys(":\"abc\<CR>", 'xt')
2134 call assert_equal("\"abc", @:)
2135 call feedkeys(":\"\<C-^>abc\<C-^>\<CR>", 'xt')
2136 call assert_equal("\"abc", @:)
2137 call feedkeys("/abc\<CR>", 'xt')
2138 call assert_equal([2, 1], [line('.'), col('.')])
2139 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
2140 call assert_equal([2, 1], [line('.'), col('.')])
2141
2142 set imsearch=2
2143 call cursor(1, 1)
2144 call feedkeys("/abc\<CR>", 'xt')
2145 call assert_equal([2, 1], [line('.'), col('.')])
2146 call cursor(1, 1)
2147 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
2148 call assert_equal([2, 1], [line('.'), col('.')])
2149 set imdisable
2150 call feedkeys("/\<C-^>abc\<C-^>\<CR>", 'xt')
2151 call assert_equal([2, 1], [line('.'), col('.')])
2152 set imdisable&
2153 set imsearch&
2154
2155 set imcmdline&
2156 %bwipe!
2157endfunc
2158
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +01002159" Test for using CTRL-_ in the command line with 'allowrevins'
2160func Test_cmdline_revins()
2161 CheckNotMSWindows
2162 CheckFeature rightleft
2163 call feedkeys(":\"abc\<c-_>\<cr>", 'xt')
2164 call assert_equal("\"abc\<c-_>", @:)
2165 set allowrevins
2166 call feedkeys(":\"abc\<c-_>xyz\<c-_>\<CR>", 'xt')
2167 call assert_equal('"abcñèæ', @:)
2168 set allowrevins&
2169endfunc
2170
2171" Test for typing UTF-8 composing characters in the command line
2172func Test_cmdline_composing_chars()
2173 call feedkeys(":\"\<C-V>u3046\<C-V>u3099\<CR>", 'xt')
2174 call assert_equal('"ゔ', @:)
2175endfunc
2176
Bram Moolenaar0e717042020-04-27 19:29:01 +02002177" test that ";" works to find a match at the start of the first line
2178func Test_zero_line_search()
2179 new
2180 call setline(1, ["1, pattern", "2, ", "3, pattern"])
2181 call cursor(1,1)
2182 0;/pattern/d
2183 call assert_equal(["2, ", "3, pattern"], getline(1,'$'))
2184 q!
2185endfunc
2186
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02002187func Test_read_shellcmd()
2188 CheckUnix
2189 if executable('ls')
2190 " There should be ls in the $PATH
2191 call feedkeys(":r! l\<c-a>\<c-b>\"\<cr>", 'tx')
2192 call assert_match('^"r! .*\<ls\>', @:)
2193 endif
2194
2195 if executable('rm')
2196 call feedkeys(":r! ++enc=utf-8 r\<c-a>\<c-b>\"\<cr>", 'tx')
2197 call assert_notmatch('^"r!.*\<runtest.vim\>', @:)
2198 call assert_match('^"r!.*\<rm\>', @:)
Bram Moolenaar743d0622020-07-03 18:15:06 +02002199
2200 call feedkeys(":r ++enc=utf-8 !rm\<c-a>\<c-b>\"\<cr>", 'tx')
2201 call assert_notmatch('^"r.*\<runtest.vim\>', @:)
2202 call assert_match('^"r ++enc\S\+ !.*\<rm\>', @:)
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02002203 endif
2204endfunc
2205
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002206" Test for going up and down the directory tree using 'wildmenu'
2207func Test_wildmenu_dirstack()
2208 CheckUnix
2209 %bw!
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002210 call mkdir('Xwildmenu/dir2/dir3/dir4', 'pR')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002211 call writefile([], 'Xwildmenu/file1_1.txt')
2212 call writefile([], 'Xwildmenu/file1_2.txt')
2213 call writefile([], 'Xwildmenu/dir2/file2_1.txt')
2214 call writefile([], 'Xwildmenu/dir2/file2_2.txt')
2215 call writefile([], 'Xwildmenu/dir2/dir3/file3_1.txt')
2216 call writefile([], 'Xwildmenu/dir2/dir3/file3_2.txt')
2217 call writefile([], 'Xwildmenu/dir2/dir3/dir4/file4_1.txt')
2218 call writefile([], 'Xwildmenu/dir2/dir3/dir4/file4_2.txt')
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002219 set wildmenu
2220
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002221 cd Xwildmenu/dir2/dir3/dir4
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002222 call feedkeys(":e \<Tab>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002223 call assert_equal('"e file4_1.txt', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002224 call feedkeys(":e \<Tab>\<Up>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002225 call assert_equal('"e ../dir4/', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002226 call feedkeys(":e \<Tab>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002227 call assert_equal('"e ../../dir3/', @:)
2228 call feedkeys(":e \<Tab>\<Up>\<Up>\<Up>\<C-B>\"\<CR>", 'xt')
2229 call assert_equal('"e ../../../dir2/', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002230 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002231 call assert_equal('"e ../../dir3/dir4/', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002232 call feedkeys(":e \<Tab>\<Up>\<Up>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002233 call assert_equal('"e ../../dir3/dir4/file4_1.txt', @:)
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002234 cd -
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002235 call feedkeys(":e Xwildmenu/\<Tab>\<Down>\<Down>\<Down>\<C-B>\"\<CR>", 'xt')
2236 call assert_equal('"e Xwildmenu/dir2/dir3/dir4/file4_1.txt', @:)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002237
Bram Moolenaar4b2ce122020-11-21 21:41:41 +01002238 set wildmenu&
2239endfunc
2240
obcat71c6f7a2021-05-13 20:23:10 +02002241" Test for recalling newer or older cmdline from history with <Up>, <Down>,
Yegappan Lakshmanan155b0882022-03-17 13:03:09 +00002242" <S-Up>, <S-Down>, <PageUp>, <PageDown>, <kPageUp>, <kPageDown>, <C-p>, or
2243" <C-n>.
obcat71c6f7a2021-05-13 20:23:10 +02002244func Test_recalling_cmdline()
2245 CheckFeature cmdline_hist
2246
2247 let g:cmdlines = []
2248 cnoremap <Plug>(save-cmdline) <Cmd>let g:cmdlines += [getcmdline()]<CR>
2249
2250 let histories = [
Yegappan Lakshmanan155b0882022-03-17 13:03:09 +00002251 \ #{name: 'cmd', enter: ':', exit: "\<Esc>"},
2252 \ #{name: 'search', enter: '/', exit: "\<Esc>"},
2253 \ #{name: 'expr', enter: ":\<C-r>=", exit: "\<Esc>\<Esc>"},
2254 \ #{name: 'input', enter: ":call input('')\<CR>", exit: "\<CR>"},
obcat71c6f7a2021-05-13 20:23:10 +02002255 "\ TODO: {'name': 'debug', ...}
2256 \]
2257 let keypairs = [
Yegappan Lakshmanan155b0882022-03-17 13:03:09 +00002258 \ #{older: "\<Up>", newer: "\<Down>", prefixmatch: v:true},
2259 \ #{older: "\<S-Up>", newer: "\<S-Down>", prefixmatch: v:false},
2260 \ #{older: "\<PageUp>", newer: "\<PageDown>", prefixmatch: v:false},
2261 \ #{older: "\<kPageUp>", newer: "\<kPageDown>", prefixmatch: v:false},
2262 \ #{older: "\<C-p>", newer: "\<C-n>", prefixmatch: v:false},
obcat71c6f7a2021-05-13 20:23:10 +02002263 \]
2264 let prefix = 'vi'
2265 for h in histories
2266 call histadd(h.name, 'vim')
2267 call histadd(h.name, 'virtue')
2268 call histadd(h.name, 'Virgo')
2269 call histadd(h.name, 'vogue')
2270 call histadd(h.name, 'emacs')
2271 for k in keypairs
2272 let g:cmdlines = []
2273 let keyseqs = h.enter
2274 \ .. prefix
2275 \ .. repeat(k.older .. "\<Plug>(save-cmdline)", 2)
2276 \ .. repeat(k.newer .. "\<Plug>(save-cmdline)", 2)
2277 \ .. h.exit
2278 call feedkeys(keyseqs, 'xt')
2279 call histdel(h.name, -1) " delete the history added by feedkeys above
2280 let expect = k.prefixmatch
2281 \ ? ['virtue', 'vim', 'virtue', prefix]
2282 \ : ['emacs', 'vogue', 'emacs', prefix]
2283 call assert_equal(expect, g:cmdlines)
2284 endfor
2285 endfor
2286
2287 unlet g:cmdlines
2288 cunmap <Plug>(save-cmdline)
2289endfunc
2290
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002291func Test_cmd_map_cmdlineChanged()
2292 let g:log = []
2293 cnoremap <F1> l<Cmd><CR>s
zeertzjq094dd152023-06-15 22:51:57 +01002294 augroup test_CmdlineChanged
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002295 autocmd!
2296 autocmd CmdlineChanged : let g:log += [getcmdline()]
2297 augroup END
2298
2299 call feedkeys(":\<F1>\<CR>", 'xt')
2300 call assert_equal(['l', 'ls'], g:log)
2301
Bram Moolenaar796139a2021-05-18 21:38:45 +02002302 let @b = 'b'
2303 cnoremap <F1> a<C-R>b
2304 let g:log = []
2305 call feedkeys(":\<F1>\<CR>", 'xt')
2306 call assert_equal(['a', 'ab'], g:log)
2307
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002308 unlet g:log
2309 cunmap <F1>
zeertzjq094dd152023-06-15 22:51:57 +01002310 augroup test_CmdlineChanged
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002311 autocmd!
2312 augroup END
zeertzjq094dd152023-06-15 22:51:57 +01002313 augroup! test_CmdlineChanged
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002314endfunc
2315
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002316" Test for the 'suffixes' option
2317func Test_suffixes_opt()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002318 call writefile([], 'Xsuffile', 'D')
2319 call writefile([], 'Xsuffile.c', 'D')
2320 call writefile([], 'Xsuffile.o', 'D')
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002321 set suffixes=
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002322 call feedkeys(":e Xsuffi*\<C-A>\<C-B>\"\<CR>", 'xt')
2323 call assert_equal('"e Xsuffile Xsuffile.c Xsuffile.o', @:)
2324 call feedkeys(":e Xsuffi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2325 call assert_equal('"e Xsuffile.c', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002326 set suffixes=.c
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002327 call feedkeys(":e Xsuffi*\<C-A>\<C-B>\"\<CR>", 'xt')
2328 call assert_equal('"e Xsuffile Xsuffile.o Xsuffile.c', @:)
2329 call feedkeys(":e Xsuffi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2330 call assert_equal('"e Xsuffile.o', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002331 set suffixes=,,
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002332 call feedkeys(":e Xsuffi*\<C-A>\<C-B>\"\<CR>", 'xt')
2333 call assert_equal('"e Xsuffile.c Xsuffile.o Xsuffile', @:)
2334 call feedkeys(":e Xsuffi*\<Tab>\<Tab>\<C-B>\"\<CR>", 'xt')
2335 call assert_equal('"e Xsuffile.o', @:)
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002336 set suffixes&
Yegappan Lakshmanan9773db62022-02-14 11:10:59 +00002337 " Test for getcompletion() with different patterns
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002338 call assert_equal(['Xsuffile', 'Xsuffile.c', 'Xsuffile.o'], getcompletion('Xsuffile', 'file'))
2339 call assert_equal(['Xsuffile'], getcompletion('Xsuffile$', 'file'))
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002340endfunc
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02002341
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002342" Test for using a popup menu for the command line completion matches
2343" (wildoptions=pum)
2344func Test_wildmenu_pum()
2345 CheckRunVimInTerminal
2346
2347 let commands =<< trim [CODE]
2348 set wildmenu
2349 set wildoptions=pum
2350 set shm+=I
2351 set noruler
2352 set noshowcmd
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002353
2354 func CmdCompl(a, b, c)
2355 return repeat(['aaaa'], 120)
2356 endfunc
2357 command -nargs=* -complete=customlist,CmdCompl Tcmd
Bram Moolenaar481acb12022-02-11 18:51:45 +00002358
2359 func MyStatusLine() abort
2360 return 'status'
2361 endfunc
2362 func SetupStatusline()
2363 set statusline=%!MyStatusLine()
2364 set laststatus=2
2365 endfunc
Bram Moolenaare4835bf2022-02-14 19:17:53 +00002366
2367 func MyTabLine()
2368 return 'my tab line'
2369 endfunc
2370 func SetupTabline()
2371 set statusline=
2372 set tabline=%!MyTabLine()
2373 set showtabline=2
2374 endfunc
Bram Moolenaar5c52be42022-02-27 14:28:31 +00002375
2376 func DoFeedKeys()
2377 let &wildcharm = char2nr("\t")
2378 call feedkeys(":edit $VIMRUNTIME/\<Tab>\<Left>\<C-U>ab\<Tab>")
2379 endfunc
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002380 [CODE]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002381 call writefile(commands, 'Xtest', 'D')
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002382
2383 let buf = RunVimInTerminal('-S Xtest', #{rows: 10})
2384
2385 call term_sendkeys(buf, ":sign \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002386 call VerifyScreenDump(buf, 'Test_wildmenu_pum_01', {})
2387
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002388 " going down the popup menu using <Down>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002389 call term_sendkeys(buf, "\<Down>\<Down>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002390 call VerifyScreenDump(buf, 'Test_wildmenu_pum_02', {})
2391
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002392 " going down the popup menu using <C-N>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002393 call term_sendkeys(buf, "\<C-N>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002394 call VerifyScreenDump(buf, 'Test_wildmenu_pum_03', {})
2395
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002396 " going up the popup menu using <C-P>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002397 call term_sendkeys(buf, "\<C-P>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002398 call VerifyScreenDump(buf, 'Test_wildmenu_pum_04', {})
2399
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002400 " going up the popup menu using <Up>
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002401 call term_sendkeys(buf, "\<Up>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002402 call VerifyScreenDump(buf, 'Test_wildmenu_pum_05', {})
2403
2404 " pressing <C-E> should end completion and go back to the original match
2405 call term_sendkeys(buf, "\<C-E>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002406 call VerifyScreenDump(buf, 'Test_wildmenu_pum_06', {})
2407
2408 " pressing <C-Y> should select the current match and end completion
2409 call term_sendkeys(buf, "\<Tab>\<C-P>\<C-P>\<C-Y>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002410 call VerifyScreenDump(buf, 'Test_wildmenu_pum_07', {})
2411
2412 " With 'wildmode' set to 'longest,full', completing a match should display
2413 " the longest match, the wildmenu should not be displayed.
2414 call term_sendkeys(buf, ":\<C-U>set wildmode=longest,full\<CR>")
2415 call TermWait(buf)
2416 call term_sendkeys(buf, ":sign u\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002417 call VerifyScreenDump(buf, 'Test_wildmenu_pum_08', {})
2418
2419 " pressing <Tab> should display the wildmenu
2420 call term_sendkeys(buf, "\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002421 call VerifyScreenDump(buf, 'Test_wildmenu_pum_09', {})
2422
2423 " pressing <Tab> second time should select the next entry in the menu
2424 call term_sendkeys(buf, "\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002425 call VerifyScreenDump(buf, 'Test_wildmenu_pum_10', {})
2426
2427 call term_sendkeys(buf, ":\<C-U>set wildmode=full\<CR>")
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002428 " showing popup menu in different columns in the cmdline
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002429 call term_sendkeys(buf, ":sign define \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002430 call VerifyScreenDump(buf, 'Test_wildmenu_pum_11', {})
2431
2432 call term_sendkeys(buf, " \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002433 call VerifyScreenDump(buf, 'Test_wildmenu_pum_12', {})
2434
2435 call term_sendkeys(buf, " \<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002436 call VerifyScreenDump(buf, 'Test_wildmenu_pum_13', {})
2437
2438 " Directory name completion
Dominique Pellefebe1382022-09-14 12:51:49 +01002439 call mkdir('Xnamedir/XdirA/XdirB', 'pR')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002440 call writefile([], 'Xnamedir/XfileA')
2441 call writefile([], 'Xnamedir/XdirA/XfileB')
2442 call writefile([], 'Xnamedir/XdirA/XdirB/XfileC')
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002443
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002444 call term_sendkeys(buf, "\<C-U>e Xnamedi\<Tab>\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002445 call VerifyScreenDump(buf, 'Test_wildmenu_pum_14', {})
2446
2447 " Pressing <Right> on a directory name should go into that directory
2448 call term_sendkeys(buf, "\<Right>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002449 call VerifyScreenDump(buf, 'Test_wildmenu_pum_15', {})
2450
2451 " Pressing <Left> on a directory name should go to the parent directory
2452 call term_sendkeys(buf, "\<Left>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002453 call VerifyScreenDump(buf, 'Test_wildmenu_pum_16', {})
2454
2455 " Pressing <C-A> when the popup menu is displayed should list all the
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002456 " matches but the popup menu should still remain
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002457 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-A>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002458 call VerifyScreenDump(buf, 'Test_wildmenu_pum_17', {})
2459
2460 " Pressing <C-D> when the popup menu is displayed should remove the popup
2461 " menu
2462 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-D>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002463 call VerifyScreenDump(buf, 'Test_wildmenu_pum_18', {})
2464
2465 " Pressing <S-Tab> should open the popup menu with the last entry selected
2466 call term_sendkeys(buf, "\<C-U>\<CR>:sign \<S-Tab>\<C-P>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002467 call VerifyScreenDump(buf, 'Test_wildmenu_pum_19', {})
2468
2469 " Pressing <Esc> should close the popup menu and cancel the cmd line
2470 call term_sendkeys(buf, "\<C-U>\<CR>:sign \<Tab>\<Esc>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002471 call VerifyScreenDump(buf, 'Test_wildmenu_pum_20', {})
2472
2473 " Typing a character when the popup is open, should close the popup
2474 call term_sendkeys(buf, ":sign \<Tab>x")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002475 call VerifyScreenDump(buf, 'Test_wildmenu_pum_21', {})
2476
2477 " When the popup is open, entering the cmdline window should close the popup
2478 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<C-F>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002479 call VerifyScreenDump(buf, 'Test_wildmenu_pum_22', {})
2480 call term_sendkeys(buf, ":q\<CR>")
2481
2482 " After the last popup menu item, <C-N> should show the original string
2483 call term_sendkeys(buf, ":sign u\<Tab>\<C-N>\<C-N>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002484 call VerifyScreenDump(buf, 'Test_wildmenu_pum_23', {})
2485
2486 " Use the popup menu for the command name
2487 call term_sendkeys(buf, "\<C-U>bu\<Tab>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002488 call VerifyScreenDump(buf, 'Test_wildmenu_pum_24', {})
2489
2490 " Pressing the left arrow should remove the popup menu
2491 call term_sendkeys(buf, "\<Left>\<Left>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002492 call VerifyScreenDump(buf, 'Test_wildmenu_pum_25', {})
2493
2494 " Pressing <BS> should remove the popup menu and erase the last character
2495 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<BS>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002496 call VerifyScreenDump(buf, 'Test_wildmenu_pum_26', {})
2497
2498 " Pressing <C-W> should remove the popup menu and erase the previous word
2499 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<C-W>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002500 call VerifyScreenDump(buf, 'Test_wildmenu_pum_27', {})
2501
2502 " Pressing <C-U> should remove the popup menu and erase the entire line
2503 call term_sendkeys(buf, "\<C-E>\<C-U>sign \<Tab>\<C-U>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002504 call VerifyScreenDump(buf, 'Test_wildmenu_pum_28', {})
2505
2506 " Using <C-E> to cancel the popup menu and then pressing <Up> should recall
2507 " the cmdline from history
2508 call term_sendkeys(buf, "sign xyz\<Esc>:sign \<Tab>\<C-E>\<Up>")
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002509 call VerifyScreenDump(buf, 'Test_wildmenu_pum_29', {})
2510
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002511 " Check "list" still works
2512 call term_sendkeys(buf, "\<C-U>set wildmode=longest,list\<CR>")
2513 call term_sendkeys(buf, ":cn\<Tab>")
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002514 call VerifyScreenDump(buf, 'Test_wildmenu_pum_30', {})
2515 call term_sendkeys(buf, "s")
Bram Moolenaar73a16c22022-02-08 17:40:36 +00002516 call VerifyScreenDump(buf, 'Test_wildmenu_pum_31', {})
2517
rbtnn68cc2b82022-02-09 11:55:47 +00002518 " Tests a directory name contained full-width characters.
Dominique Pellefebe1382022-09-14 12:51:49 +01002519 call mkdir('Xnamedir/あいう', 'p')
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002520 call writefile([], 'Xnamedir/あいう/abc')
2521 call writefile([], 'Xnamedir/あいう/xyz')
2522 call writefile([], 'Xnamedir/あいう/123')
rbtnn68cc2b82022-02-09 11:55:47 +00002523
2524 call term_sendkeys(buf, "\<C-U>set wildmode&\<CR>")
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002525 call term_sendkeys(buf, ":\<C-U>e Xnamedir/あいう/\<Tab>")
rbtnn68cc2b82022-02-09 11:55:47 +00002526 call VerifyScreenDump(buf, 'Test_wildmenu_pum_32', {})
2527
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002528 " Pressing <C-A> when the popup menu is displayed should list all the
2529 " matches and pressing a key after that should remove the popup menu
2530 call term_sendkeys(buf, "\<C-U>set wildmode=full\<CR>")
2531 call term_sendkeys(buf, ":sign \<Tab>\<C-A>x")
2532 call VerifyScreenDump(buf, 'Test_wildmenu_pum_33', {})
2533
2534 " Pressing <C-A> when the popup menu is displayed should list all the
2535 " matches and pressing <Left> after that should move the cursor
2536 call term_sendkeys(buf, "\<C-U>abc\<Esc>")
2537 call term_sendkeys(buf, ":sign \<Tab>\<C-A>\<Left>")
2538 call VerifyScreenDump(buf, 'Test_wildmenu_pum_34', {})
2539
2540 " When <C-A> displays a lot of matches (screen scrolls), all the matches
2541 " should be displayed correctly on the screen.
2542 call term_sendkeys(buf, "\<End>\<C-U>Tcmd \<Tab>\<C-A>\<Left>\<Left>")
2543 call VerifyScreenDump(buf, 'Test_wildmenu_pum_35', {})
2544
2545 " After using <C-A> to expand all the filename matches, pressing <Up>
2546 " should not open the popup menu again.
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002547 call term_sendkeys(buf, "\<C-E>\<C-U>:cd Xnamedir/XdirA\<CR>")
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002548 call term_sendkeys(buf, ":e \<Tab>\<C-A>\<Up>")
2549 call VerifyScreenDump(buf, 'Test_wildmenu_pum_36', {})
2550 call term_sendkeys(buf, "\<C-E>\<C-U>:cd -\<CR>")
2551
2552 " After using <C-A> to expand all the matches, pressing <S-Tab> used to
2553 " crash Vim
2554 call term_sendkeys(buf, ":sign \<Tab>\<C-A>\<S-Tab>")
2555 call VerifyScreenDump(buf, 'Test_wildmenu_pum_37', {})
2556
Bram Moolenaar414acd32022-02-10 21:09:45 +00002557 " After removing the pum the command line is redrawn
2558 call term_sendkeys(buf, ":edit foo\<CR>")
2559 call term_sendkeys(buf, ":edit bar\<CR>")
2560 call term_sendkeys(buf, ":ls\<CR>")
2561 call term_sendkeys(buf, ":com\<Tab> ")
2562 call VerifyScreenDump(buf, 'Test_wildmenu_pum_38', {})
Bram Moolenaar481acb12022-02-11 18:51:45 +00002563 call term_sendkeys(buf, "\<C-U>\<CR>")
2564
2565 " Esc still works to abort the command when 'statusline' is set
2566 call term_sendkeys(buf, ":call SetupStatusline()\<CR>")
2567 call term_sendkeys(buf, ":si\<Tab>")
2568 call term_sendkeys(buf, "\<Esc>")
2569 call VerifyScreenDump(buf, 'Test_wildmenu_pum_39', {})
Bram Moolenaar414acd32022-02-10 21:09:45 +00002570
Bram Moolenaare4835bf2022-02-14 19:17:53 +00002571 " Esc still works to abort the command when 'tabline' is set
2572 call term_sendkeys(buf, ":call SetupTabline()\<CR>")
2573 call term_sendkeys(buf, ":si\<Tab>")
2574 call term_sendkeys(buf, "\<Esc>")
2575 call VerifyScreenDump(buf, 'Test_wildmenu_pum_40', {})
2576
Bram Moolenaar5c52be42022-02-27 14:28:31 +00002577 " popup is cleared also when 'lazyredraw' is set
2578 call term_sendkeys(buf, ":set showtabline=1 laststatus=1 lazyredraw\<CR>")
2579 call term_sendkeys(buf, ":call DoFeedKeys()\<CR>")
2580 call VerifyScreenDump(buf, 'Test_wildmenu_pum_41', {})
2581 call term_sendkeys(buf, "\<Esc>")
2582
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00002583 " Pressing <PageDown> should scroll the menu downward
2584 call term_sendkeys(buf, ":sign \<Tab>\<PageDown>")
2585 call VerifyScreenDump(buf, 'Test_wildmenu_pum_42', {})
2586 call term_sendkeys(buf, "\<PageDown>")
2587 call VerifyScreenDump(buf, 'Test_wildmenu_pum_43', {})
2588 call term_sendkeys(buf, "\<PageDown>")
2589 call VerifyScreenDump(buf, 'Test_wildmenu_pum_44', {})
2590 call term_sendkeys(buf, "\<PageDown>")
2591 call VerifyScreenDump(buf, 'Test_wildmenu_pum_45', {})
2592 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<Down>\<Down>\<PageDown>")
2593 call VerifyScreenDump(buf, 'Test_wildmenu_pum_46', {})
2594
2595 " Pressing <PageUp> should scroll the menu upward
2596 call term_sendkeys(buf, "\<C-U>sign \<Tab>\<PageUp>")
2597 call VerifyScreenDump(buf, 'Test_wildmenu_pum_47', {})
2598 call term_sendkeys(buf, "\<PageUp>")
2599 call VerifyScreenDump(buf, 'Test_wildmenu_pum_48', {})
2600 call term_sendkeys(buf, "\<PageUp>")
2601 call VerifyScreenDump(buf, 'Test_wildmenu_pum_49', {})
2602 call term_sendkeys(buf, "\<PageUp>")
2603 call VerifyScreenDump(buf, 'Test_wildmenu_pum_50', {})
2604
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002605 " pressing <C-E> to end completion should work in middle of the line too
2606 call term_sendkeys(buf, "\<Esc>:set wildchazz\<Left>\<Left>\<Tab>")
2607 call VerifyScreenDump(buf, 'Test_wildmenu_pum_51', {})
2608 call term_sendkeys(buf, "\<C-E>")
2609 call VerifyScreenDump(buf, 'Test_wildmenu_pum_52', {})
2610
2611 " pressing <C-Y> should select the current match and end completion
2612 call term_sendkeys(buf, "\<Esc>:set wildchazz\<Left>\<Left>\<Tab>\<C-Y>")
2613 call VerifyScreenDump(buf, 'Test_wildmenu_pum_53', {})
2614
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002615 call term_sendkeys(buf, "\<C-U>\<CR>")
2616 call StopVimInTerminal(buf)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002617endfunc
2618
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002619" Test for wildmenumode() with the cmdline popup menu
2620func Test_wildmenumode_with_pum()
2621 set wildmenu
2622 set wildoptions=pum
2623 cnoremap <expr> <F2> wildmenumode()
2624 call feedkeys(":sign \<Tab>\<F2>\<F2>\<C-B>\"\<CR>", 'xt')
2625 call assert_equal('"sign define10', @:)
2626 call feedkeys(":sign \<Tab>\<C-A>\<F2>\<C-B>\"\<CR>", 'xt')
2627 call assert_equal('"sign define jump list place undefine unplace0', @:)
2628 call feedkeys(":sign \<Tab>\<C-E>\<F2>\<C-B>\"\<CR>", 'xt')
2629 call assert_equal('"sign 0', @:)
2630 call feedkeys(":sign \<Tab>\<C-Y>\<F2>\<C-B>\"\<CR>", 'xt')
2631 call assert_equal('"sign define0', @:)
2632 set nowildmenu wildoptions&
2633 cunmap <F2>
2634endfunc
2635
Bram Moolenaar11a57df2022-04-11 19:38:56 +01002636func Test_wildmenu_with_pum_foldexpr()
2637 CheckRunVimInTerminal
2638
2639 let lines =<< trim END
2640 call setline(1, ['folded one', 'folded two', 'some more text'])
2641 func MyFoldText()
2642 return 'foo'
2643 endfunc
2644 set foldtext=MyFoldText() wildoptions=pum
2645 normal ggzfj
2646 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002647 call writefile(lines, 'Xpumfold', 'D')
Bram Moolenaar11a57df2022-04-11 19:38:56 +01002648 let buf = RunVimInTerminal('-S Xpumfold', #{rows: 10})
2649 call term_sendkeys(buf, ":set\<Tab>")
2650 call VerifyScreenDump(buf, 'Test_wildmenu_with_pum_foldexpr_1', {})
2651
2652 call term_sendkeys(buf, "\<Esc>")
2653 call VerifyScreenDump(buf, 'Test_wildmenu_with_pum_foldexpr_2', {})
2654
2655 call StopVimInTerminal(buf)
Bram Moolenaar11a57df2022-04-11 19:38:56 +01002656endfunc
2657
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01002658" Test for opening the cmdline completion popup menu from the terminal window.
2659" The popup menu should be positioned correctly over the status line of the
2660" bottom-most window.
2661func Test_wildmenu_pum_from_terminal()
2662 CheckRunVimInTerminal
2663 let python = PythonProg()
2664 call CheckPython(python)
2665
2666 %bw!
2667 let cmds = ['set wildmenu wildoptions=pum']
2668 let pcmd = python .. ' -c "import sys; sys.stdout.write(sys.stdin.read())"'
2669 call add(cmds, "call term_start('" .. pcmd .. "')")
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002670 call writefile(cmds, 'Xtest', 'D')
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01002671 let buf = RunVimInTerminal('-S Xtest', #{rows: 10})
2672 call term_sendkeys(buf, "\r\r\r")
2673 call term_wait(buf)
2674 call term_sendkeys(buf, "\<C-W>:sign \<Tab>")
2675 call term_wait(buf)
2676 call VerifyScreenDump(buf, 'Test_wildmenu_pum_term_01', {})
2677 call term_wait(buf)
2678 call StopVimInTerminal(buf)
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01002679endfunc
2680
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002681func Test_wildmenu_pum_odd_wildchar()
Bram Moolenaarb9603f62022-12-08 15:44:22 +00002682 CheckRunVimInTerminal
2683
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002684 " Test odd wildchar interactions with pum. Make sure they behave properly
2685 " and don't lead to memory corruption due to improperly cleaned up memory.
Bram Moolenaar038e6d22022-12-08 12:00:50 +00002686 let lines =<< trim END
2687 set wildoptions=pum
2688 set wildchar=<C-E>
2689 END
2690 call writefile(lines, 'XwildmenuTest', 'D')
2691 let buf = RunVimInTerminal('-S XwildmenuTest', #{rows: 10})
2692
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002693 call term_sendkeys(buf, ":\<C-E>")
2694 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
Bram Moolenaar038e6d22022-12-08 12:00:50 +00002695
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002696 " <C-E> being a wildchar takes priority over its original functionality
2697 call term_sendkeys(buf, "\<C-E>")
2698 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_2', {})
2699
2700 call term_sendkeys(buf, "\<Esc>")
2701 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
2702
2703 " Escape key can be wildchar too. Double-<Esc> is hard-coded to escape
2704 " command-line, and we need to make sure to clean up properly.
2705 call term_sendkeys(buf, ":set wildchar=<Esc>\<CR>")
2706 call term_sendkeys(buf, ":\<Esc>")
2707 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
2708
2709 call term_sendkeys(buf, "\<Esc>")
2710 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
2711
2712 " <C-\> can also be wildchar. <C-\><C-N> however will still escape cmdline
2713 " and we again need to make sure we clean up properly.
2714 call term_sendkeys(buf, ":set wildchar=<C-\\>\<CR>")
2715 call term_sendkeys(buf, ":\<C-\>\<C-\>")
2716 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_1', {})
2717
2718 call term_sendkeys(buf, "\<C-N>")
2719 call VerifyScreenDump(buf, 'Test_wildmenu_pum_odd_wildchar_3', {})
2720
2721 call StopVimInTerminal(buf)
Bram Moolenaar038e6d22022-12-08 12:00:50 +00002722endfunc
2723
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002724" Test for completion after a :substitute command followed by a pipe (|)
2725" character
2726func Test_cmdline_complete_substitute()
2727 call feedkeys(":s | \t\<C-B>\"\<CR>", 'xt')
2728 call assert_equal("\"s | \t", @:)
2729 call feedkeys(":s/ | \t\<C-B>\"\<CR>", 'xt')
2730 call assert_equal("\"s/ | \t", @:)
2731 call feedkeys(":s/one | \t\<C-B>\"\<CR>", 'xt')
2732 call assert_equal("\"s/one | \t", @:)
2733 call feedkeys(":s/one/ | \t\<C-B>\"\<CR>", 'xt')
2734 call assert_equal("\"s/one/ | \t", @:)
2735 call feedkeys(":s/one/two | \t\<C-B>\"\<CR>", 'xt')
2736 call assert_equal("\"s/one/two | \t", @:)
2737 call feedkeys(":s/one/two/ | chist\t\<C-B>\"\<CR>", 'xt')
2738 call assert_equal('"s/one/two/ | chistory', @:)
2739 call feedkeys(":s/one/two/g \t\<C-B>\"\<CR>", 'xt')
2740 call assert_equal("\"s/one/two/g \t", @:)
2741 call feedkeys(":s/one/two/g | chist\t\<C-B>\"\<CR>", 'xt')
2742 call assert_equal("\"s/one/two/g | chistory", @:)
2743 call feedkeys(":s/one/t\\/ | \t\<C-B>\"\<CR>", 'xt')
2744 call assert_equal("\"s/one/t\\/ | \t", @:)
2745 call feedkeys(":s/one/t\"o/ | chist\t\<C-B>\"\<CR>", 'xt')
2746 call assert_equal('"s/one/t"o/ | chistory', @:)
2747 call feedkeys(":s/one/t|o/ | chist\t\<C-B>\"\<CR>", 'xt')
2748 call assert_equal('"s/one/t|o/ | chistory', @:)
2749 call feedkeys(":&\t\<C-B>\"\<CR>", 'xt')
2750 call assert_equal("\"&\t", @:)
2751endfunc
2752
2753" Test for the :dlist command completion
2754func Test_cmdline_complete_dlist()
2755 call feedkeys(":dlist 10 /pat/ a\<C-A>\<C-B>\"\<CR>", 'xt')
2756 call assert_equal("\"dlist 10 /pat/ a\<C-A>", @:)
2757 call feedkeys(":dlist 10 /pat/ \t\<C-B>\"\<CR>", 'xt')
2758 call assert_equal("\"dlist 10 /pat/ \t", @:)
2759 call feedkeys(":dlist 10 /pa\\t/\t\<C-B>\"\<CR>", 'xt')
2760 call assert_equal("\"dlist 10 /pa\\t/\t", @:)
2761 call feedkeys(":dlist 10 /pat\\\t\<C-B>\"\<CR>", 'xt')
2762 call assert_equal("\"dlist 10 /pat\\\t", @:)
2763 call feedkeys(":dlist 10 /pat/ | chist\<Tab>\<C-B>\"\<CR>", 'xt')
2764 call assert_equal("\"dlist 10 /pat/ | chistory", @:)
2765endfunc
2766
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002767" argument list (only for :argdel) fuzzy completion
2768func Test_fuzzy_completion_arglist()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002769 argadd change.py count.py charge.py
2770 set wildoptions&
2771 call feedkeys(":argdel cge\<C-A>\<C-B>\"\<CR>", 'tx')
2772 call assert_equal('"argdel cge', @:)
2773 set wildoptions=fuzzy
2774 call feedkeys(":argdel cge\<C-A>\<C-B>\"\<CR>", 'tx')
2775 call assert_equal('"argdel change.py charge.py', @:)
2776 %argdelete
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002777 set wildoptions&
2778endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002779
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002780" autocmd group name fuzzy completion
2781func Test_fuzzy_completion_autocmd()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002782 set wildoptions&
2783 augroup MyFuzzyGroup
2784 augroup END
2785 call feedkeys(":augroup mfg\<Tab>\<C-B>\"\<CR>", 'tx')
2786 call assert_equal('"augroup mfg', @:)
2787 call feedkeys(":augroup My*p\<Tab>\<C-B>\"\<CR>", 'tx')
2788 call assert_equal('"augroup MyFuzzyGroup', @:)
2789 set wildoptions=fuzzy
2790 call feedkeys(":augroup mfg\<Tab>\<C-B>\"\<CR>", 'tx')
2791 call assert_equal('"augroup MyFuzzyGroup', @:)
2792 call feedkeys(":augroup My*p\<Tab>\<C-B>\"\<CR>", 'tx')
2793 call assert_equal('"augroup My*p', @:)
2794 augroup! MyFuzzyGroup
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002795 set wildoptions&
2796endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002797
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002798" buffer name fuzzy completion
2799func Test_fuzzy_completion_bufname()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002800 set wildoptions&
Bram Moolenaar5ac4b1a2022-08-06 10:28:19 +01002801 " Use a long name to reduce the risk of matching a random directory name
2802 edit SomeRandomFileWithLetters.txt
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002803 enew
Bram Moolenaar5ac4b1a2022-08-06 10:28:19 +01002804 call feedkeys(":b SRFWL\<Tab>\<C-B>\"\<CR>", 'tx')
2805 call assert_equal('"b SRFWL', @:)
2806 call feedkeys(":b S*FileWithLetters.txt\<Tab>\<C-B>\"\<CR>", 'tx')
2807 call assert_equal('"b SomeRandomFileWithLetters.txt', @:)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002808 set wildoptions=fuzzy
Bram Moolenaar5ac4b1a2022-08-06 10:28:19 +01002809 call feedkeys(":b SRFWL\<Tab>\<C-B>\"\<CR>", 'tx')
2810 call assert_equal('"b SomeRandomFileWithLetters.txt', @:)
2811 call feedkeys(":b S*FileWithLetters.txt\<Tab>\<C-B>\"\<CR>", 'tx')
2812 call assert_equal('"b S*FileWithLetters.txt', @:)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002813 %bw!
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002814 set wildoptions&
2815endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002816
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002817" buffer name (full path) fuzzy completion
2818func Test_fuzzy_completion_bufname_fullpath()
2819 CheckUnix
2820 set wildoptions&
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01002821 call mkdir('Xcmd/Xstate/Xfile.js', 'pR')
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002822 edit Xcmd/Xstate/Xfile.js
2823 cd Xcmd/Xstate
2824 enew
2825 call feedkeys(":b CmdStateFile\<Tab>\<C-B>\"\<CR>", 'tx')
2826 call assert_equal('"b CmdStateFile', @:)
2827 set wildoptions=fuzzy
2828 call feedkeys(":b CmdStateFile\<Tab>\<C-B>\"\<CR>", 'tx')
2829 call assert_match('Xcmd/Xstate/Xfile.js$', @:)
2830 cd -
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002831 set wildoptions&
2832endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002833
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002834" :behave suboptions fuzzy completion
2835func Test_fuzzy_completion_behave()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002836 set wildoptions&
2837 call feedkeys(":behave xm\<Tab>\<C-B>\"\<CR>", 'tx')
2838 call assert_equal('"behave xm', @:)
2839 call feedkeys(":behave xt*m\<Tab>\<C-B>\"\<CR>", 'tx')
2840 call assert_equal('"behave xterm', @:)
2841 set wildoptions=fuzzy
2842 call feedkeys(":behave xm\<Tab>\<C-B>\"\<CR>", 'tx')
2843 call assert_equal('"behave xterm', @:)
2844 call feedkeys(":behave xt*m\<Tab>\<C-B>\"\<CR>", 'tx')
2845 call assert_equal('"behave xt*m', @:)
2846 let g:Sline = ''
2847 call feedkeys(":behave win\<C-D>\<F4>\<C-B>\"\<CR>", 'tx')
2848 call assert_equal('mswin', g:Sline)
2849 call assert_equal('"behave win', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002850 set wildoptions&
2851endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002852
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002853" " colorscheme name fuzzy completion - NOT supported
2854" func Test_fuzzy_completion_colorscheme()
2855" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002856
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002857" built-in command name fuzzy completion
2858func Test_fuzzy_completion_cmdname()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002859 set wildoptions&
2860 call feedkeys(":sbwin\<Tab>\<C-B>\"\<CR>", 'tx')
2861 call assert_equal('"sbwin', @:)
2862 call feedkeys(":sbr*d\<Tab>\<C-B>\"\<CR>", 'tx')
2863 call assert_equal('"sbrewind', @:)
2864 set wildoptions=fuzzy
2865 call feedkeys(":sbwin\<Tab>\<C-B>\"\<CR>", 'tx')
2866 call assert_equal('"sbrewind', @:)
2867 call feedkeys(":sbr*d\<Tab>\<C-B>\"\<CR>", 'tx')
2868 call assert_equal('"sbr*d', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002869 set wildoptions&
2870endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002871
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002872" " compiler name fuzzy completion - NOT supported
2873" func Test_fuzzy_completion_compiler()
2874" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002875
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002876" :cscope suboptions fuzzy completion
2877func Test_fuzzy_completion_cscope()
2878 CheckFeature cscope
2879 set wildoptions&
2880 call feedkeys(":cscope ret\<Tab>\<C-B>\"\<CR>", 'tx')
2881 call assert_equal('"cscope ret', @:)
2882 call feedkeys(":cscope re*t\<Tab>\<C-B>\"\<CR>", 'tx')
2883 call assert_equal('"cscope reset', @:)
2884 set wildoptions=fuzzy
2885 call feedkeys(":cscope ret\<Tab>\<C-B>\"\<CR>", 'tx')
2886 call assert_equal('"cscope reset', @:)
2887 call feedkeys(":cscope re*t\<Tab>\<C-B>\"\<CR>", 'tx')
2888 call assert_equal('"cscope re*t', @:)
2889 set wildoptions&
2890endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002891
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002892" :diffget/:diffput buffer name fuzzy completion
2893func Test_fuzzy_completion_diff()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002894 new SomeBuffer
2895 diffthis
2896 new OtherBuffer
2897 diffthis
2898 set wildoptions&
2899 call feedkeys(":diffget sbuf\<Tab>\<C-B>\"\<CR>", 'tx')
2900 call assert_equal('"diffget sbuf', @:)
2901 call feedkeys(":diffput sbuf\<Tab>\<C-B>\"\<CR>", 'tx')
2902 call assert_equal('"diffput sbuf', @:)
2903 set wildoptions=fuzzy
2904 call feedkeys(":diffget sbuf\<Tab>\<C-B>\"\<CR>", 'tx')
2905 call assert_equal('"diffget SomeBuffer', @:)
2906 call feedkeys(":diffput sbuf\<Tab>\<C-B>\"\<CR>", 'tx')
2907 call assert_equal('"diffput SomeBuffer', @:)
2908 %bw!
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002909 set wildoptions&
2910endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002911
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002912" " directory name fuzzy completion - NOT supported
2913" func Test_fuzzy_completion_dirname()
2914" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002915
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002916" environment variable name fuzzy completion
2917func Test_fuzzy_completion_env()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002918 set wildoptions&
2919 call feedkeys(":echo $VUT\<Tab>\<C-B>\"\<CR>", 'tx')
2920 call assert_equal('"echo $VUT', @:)
2921 set wildoptions=fuzzy
2922 call feedkeys(":echo $VUT\<Tab>\<C-B>\"\<CR>", 'tx')
2923 call assert_equal('"echo $VIMRUNTIME', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002924 set wildoptions&
2925endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002926
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002927" autocmd event fuzzy completion
2928func Test_fuzzy_completion_autocmd_event()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002929 set wildoptions&
2930 call feedkeys(":autocmd BWout\<Tab>\<C-B>\"\<CR>", 'tx')
2931 call assert_equal('"autocmd BWout', @:)
2932 set wildoptions=fuzzy
2933 call feedkeys(":autocmd BWout\<Tab>\<C-B>\"\<CR>", 'tx')
2934 call assert_equal('"autocmd BufWipeout', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002935 set wildoptions&
2936endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002937
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002938" vim expression fuzzy completion
2939func Test_fuzzy_completion_expr()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002940 let g:PerPlaceCount = 10
2941 set wildoptions&
2942 call feedkeys(":let c = ppc\<Tab>\<C-B>\"\<CR>", 'tx')
2943 call assert_equal('"let c = ppc', @:)
2944 set wildoptions=fuzzy
2945 call feedkeys(":let c = ppc\<Tab>\<C-B>\"\<CR>", 'tx')
2946 call assert_equal('"let c = PerPlaceCount', @:)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002947 set wildoptions&
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002948endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002949
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002950" " file name fuzzy completion - NOT supported
2951" func Test_fuzzy_completion_filename()
2952" endfunc
2953
2954" " files in path fuzzy completion - NOT supported
2955" func Test_fuzzy_completion_filesinpath()
2956" endfunc
2957
2958" " filetype name fuzzy completion - NOT supported
2959" func Test_fuzzy_completion_filetype()
2960" endfunc
2961
2962" user defined function name completion
2963func Test_fuzzy_completion_userdefined_func()
2964 set wildoptions&
2965 call feedkeys(":call Test_f_u_f\<Tab>\<C-B>\"\<CR>", 'tx')
2966 call assert_equal('"call Test_f_u_f', @:)
2967 set wildoptions=fuzzy
2968 call feedkeys(":call Test_f_u_f\<Tab>\<C-B>\"\<CR>", 'tx')
2969 call assert_equal('"call Test_fuzzy_completion_userdefined_func()', @:)
2970 set wildoptions&
2971endfunc
2972
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002973" <SNR> functions should be sorted to the end
2974func Test_fuzzy_completion_userdefined_snr_func()
2975 func s:Sendmail()
2976 endfunc
2977 func SendSomemail()
2978 endfunc
2979 func S1e2n3dmail()
2980 endfunc
2981 set wildoptions=fuzzy
2982 call feedkeys(":call sendmail\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +00002983 call assert_match('"call SendSomemail() S1e2n3dmail() <SNR>\d\+_Sendmail()', @:)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002984 set wildoptions&
2985 delfunc s:Sendmail
2986 delfunc SendSomemail
2987 delfunc S1e2n3dmail
2988endfunc
2989
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002990" user defined command name completion
2991func Test_fuzzy_completion_userdefined_cmd()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002992 set wildoptions&
2993 call feedkeys(":MsFeat\<Tab>\<C-B>\"\<CR>", 'tx')
2994 call assert_equal('"MsFeat', @:)
2995 set wildoptions=fuzzy
2996 call feedkeys(":MsFeat\<Tab>\<C-B>\"\<CR>", 'tx')
2997 call assert_equal('"MissingFeature', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002998 set wildoptions&
2999endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003000
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003001" " :help tag fuzzy completion - NOT supported
3002" func Test_fuzzy_completion_helptag()
3003" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003004
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003005" highlight group name fuzzy completion
3006func Test_fuzzy_completion_hlgroup()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003007 set wildoptions&
3008 call feedkeys(":highlight SKey\<Tab>\<C-B>\"\<CR>", 'tx')
3009 call assert_equal('"highlight SKey', @:)
3010 call feedkeys(":highlight Sp*Key\<Tab>\<C-B>\"\<CR>", 'tx')
3011 call assert_equal('"highlight SpecialKey', @:)
3012 set wildoptions=fuzzy
3013 call feedkeys(":highlight SKey\<Tab>\<C-B>\"\<CR>", 'tx')
3014 call assert_equal('"highlight SpecialKey', @:)
3015 call feedkeys(":highlight Sp*Key\<Tab>\<C-B>\"\<CR>", 'tx')
3016 call assert_equal('"highlight Sp*Key', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003017 set wildoptions&
3018endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003019
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003020" :history suboptions fuzzy completion
3021func Test_fuzzy_completion_history()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003022 set wildoptions&
3023 call feedkeys(":history dg\<Tab>\<C-B>\"\<CR>", 'tx')
3024 call assert_equal('"history dg', @:)
3025 call feedkeys(":history se*h\<Tab>\<C-B>\"\<CR>", 'tx')
3026 call assert_equal('"history search', @:)
3027 set wildoptions=fuzzy
3028 call feedkeys(":history dg\<Tab>\<C-B>\"\<CR>", 'tx')
3029 call assert_equal('"history debug', @:)
3030 call feedkeys(":history se*h\<Tab>\<C-B>\"\<CR>", 'tx')
3031 call assert_equal('"history se*h', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003032 set wildoptions&
3033endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003034
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003035" :language locale name fuzzy completion
3036func Test_fuzzy_completion_lang()
3037 CheckUnix
3038 set wildoptions&
3039 call feedkeys(":lang psx\<Tab>\<C-B>\"\<CR>", 'tx')
3040 call assert_equal('"lang psx', @:)
3041 set wildoptions=fuzzy
3042 call feedkeys(":lang psx\<Tab>\<C-B>\"\<CR>", 'tx')
3043 call assert_equal('"lang POSIX', @:)
3044 set wildoptions&
3045endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003046
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003047" :mapclear buffer argument fuzzy completion
3048func Test_fuzzy_completion_mapclear()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003049 set wildoptions&
3050 call feedkeys(":mapclear buf\<Tab>\<C-B>\"\<CR>", 'tx')
3051 call assert_equal('"mapclear buf', @:)
3052 set wildoptions=fuzzy
3053 call feedkeys(":mapclear buf\<Tab>\<C-B>\"\<CR>", 'tx')
3054 call assert_equal('"mapclear <buffer>', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003055 set wildoptions&
3056endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003057
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003058" map name fuzzy completion
3059func Test_fuzzy_completion_mapname()
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003060 " test regex completion works
3061 set wildoptions=fuzzy
3062 call feedkeys(":cnoremap <ex\<Tab> <esc> \<Tab>\<C-B>\"\<CR>", 'tx')
3063 call assert_equal("\"cnoremap <expr> <esc> \<Tab>", @:)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003064 nmap <plug>MyLongMap :p<CR>
3065 call feedkeys(":nmap MLM\<Tab>\<C-B>\"\<CR>", 'tx')
3066 call assert_equal("\"nmap <Plug>MyLongMap", @:)
3067 call feedkeys(":nmap MLM \<Tab>\<C-B>\"\<CR>", 'tx')
3068 call assert_equal("\"nmap MLM \t", @:)
3069 call feedkeys(":nmap <F2> one two \<Tab>\<C-B>\"\<CR>", 'tx')
3070 call assert_equal("\"nmap <F2> one two \t", @:)
3071 " duplicate entries should be removed
3072 vmap <plug>MyLongMap :<C-U>#<CR>
3073 call feedkeys(":nmap MLM\<Tab>\<C-B>\"\<CR>", 'tx')
3074 call assert_equal("\"nmap <Plug>MyLongMap", @:)
3075 nunmap <plug>MyLongMap
3076 vunmap <plug>MyLongMap
3077 call feedkeys(":nmap ABC\<Tab>\<C-B>\"\<CR>", 'tx')
3078 call assert_equal("\"nmap ABC\t", @:)
3079 " results should be sorted by best match
3080 nmap <Plug>format :
3081 nmap <Plug>goformat :
3082 nmap <Plug>TestFOrmat :
3083 nmap <Plug>fendoff :
3084 nmap <Plug>state :
3085 nmap <Plug>FendingOff :
3086 call feedkeys(":nmap <Plug>fo\<C-A>\<C-B>\"\<CR>", 'tx')
3087 call assert_equal("\"nmap <Plug>format <Plug>TestFOrmat <Plug>FendingOff <Plug>goformat <Plug>fendoff", @:)
3088 nunmap <Plug>format
3089 nunmap <Plug>goformat
3090 nunmap <Plug>TestFOrmat
3091 nunmap <Plug>fendoff
3092 nunmap <Plug>state
3093 nunmap <Plug>FendingOff
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003094 set wildoptions&
3095endfunc
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003096
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003097" abbreviation fuzzy completion
3098func Test_fuzzy_completion_abbr()
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003099 set wildoptions=fuzzy
3100 call feedkeys(":iabbr wait\<Tab>\<C-B>\"\<CR>", 'tx')
3101 call assert_equal("\"iabbr <nowait>", @:)
3102 iabbr WaitForCompletion WFC
3103 call feedkeys(":iabbr fcl\<Tab>\<C-B>\"\<CR>", 'tx')
3104 call assert_equal("\"iabbr WaitForCompletion", @:)
3105 call feedkeys(":iabbr a1z\<Tab>\<C-B>\"\<CR>", 'tx')
3106 call assert_equal("\"iabbr a1z\t", @:)
zeertzjq145a6af2023-01-22 12:41:55 +00003107
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003108 iunabbrev WaitForCompletion
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" menu name fuzzy completion
3113func Test_fuzzy_completion_menu()
zeertzjq145a6af2023-01-22 12:41:55 +00003114 CheckFeature menu
3115
3116 source $VIMRUNTIME/menu.vim
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003117 set wildoptions&
3118 call feedkeys(":menu pup\<Tab>\<C-B>\"\<CR>", 'tx')
3119 call assert_equal('"menu pup', @:)
3120 set wildoptions=fuzzy
3121 call feedkeys(":menu pup\<Tab>\<C-B>\"\<CR>", 'tx')
3122 call assert_equal('"menu PopUp.', @:)
zeertzjq145a6af2023-01-22 12:41:55 +00003123
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003124 set wildoptions&
zeertzjq145a6af2023-01-22 12:41:55 +00003125 source $VIMRUNTIME/delmenu.vim
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003126endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003127
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003128" :messages suboptions fuzzy completion
3129func Test_fuzzy_completion_messages()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003130 set wildoptions&
3131 call feedkeys(":messages clr\<Tab>\<C-B>\"\<CR>", 'tx')
3132 call assert_equal('"messages clr', @:)
3133 set wildoptions=fuzzy
3134 call feedkeys(":messages clr\<Tab>\<C-B>\"\<CR>", 'tx')
3135 call assert_equal('"messages clear', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003136 set wildoptions&
3137endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003138
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003139" :set option name fuzzy completion
3140func Test_fuzzy_completion_option()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003141 set wildoptions&
3142 call feedkeys(":set brkopt\<Tab>\<C-B>\"\<CR>", 'tx')
3143 call assert_equal('"set brkopt', @:)
3144 set wildoptions=fuzzy
3145 call feedkeys(":set brkopt\<Tab>\<C-B>\"\<CR>", 'tx')
3146 call assert_equal('"set breakindentopt', @:)
3147 set wildoptions&
3148 call feedkeys(":set fixeol\<Tab>\<C-B>\"\<CR>", 'tx')
3149 call assert_equal('"set fixendofline', @:)
3150 set wildoptions=fuzzy
3151 call feedkeys(":set fixeol\<Tab>\<C-B>\"\<CR>", 'tx')
3152 call assert_equal('"set fixendofline', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003153 set wildoptions&
3154endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003155
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003156" :set <term_option>
3157func Test_fuzzy_completion_term_option()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003158 set wildoptions&
3159 call feedkeys(":set t_E\<Tab>\<C-B>\"\<CR>", 'tx')
3160 call assert_equal('"set t_EC', @:)
3161 call feedkeys(":set <t_E\<Tab>\<C-B>\"\<CR>", 'tx')
3162 call assert_equal('"set <t_EC>', @:)
3163 set wildoptions=fuzzy
3164 call feedkeys(":set t_E\<Tab>\<C-B>\"\<CR>", 'tx')
3165 call assert_equal('"set t_EC', @:)
3166 call feedkeys(":set <t_E\<Tab>\<C-B>\"\<CR>", 'tx')
3167 call assert_equal('"set <t_EC>', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003168 set wildoptions&
3169endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003170
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003171" " :packadd directory name fuzzy completion - NOT supported
3172" func Test_fuzzy_completion_packadd()
3173" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003174
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003175" " shell command name fuzzy completion - NOT supported
3176" func Test_fuzzy_completion_shellcmd()
3177" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003178
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003179" :sign suboptions fuzzy completion
3180func Test_fuzzy_completion_sign()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003181 set wildoptions&
3182 call feedkeys(":sign ufe\<Tab>\<C-B>\"\<CR>", 'tx')
3183 call assert_equal('"sign ufe', @:)
3184 set wildoptions=fuzzy
3185 call feedkeys(":sign ufe\<Tab>\<C-B>\"\<CR>", 'tx')
3186 call assert_equal('"sign undefine', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003187 set wildoptions&
3188endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003189
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003190" :syntax suboptions fuzzy completion
3191func Test_fuzzy_completion_syntax_cmd()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003192 set wildoptions&
3193 call feedkeys(":syntax kwd\<Tab>\<C-B>\"\<CR>", 'tx')
3194 call assert_equal('"syntax kwd', @:)
3195 set wildoptions=fuzzy
3196 call feedkeys(":syntax kwd\<Tab>\<C-B>\"\<CR>", 'tx')
3197 call assert_equal('"syntax keyword', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003198 set wildoptions&
3199endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003200
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003201" syntax group name fuzzy completion
3202func Test_fuzzy_completion_syntax_group()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003203 set wildoptions&
3204 call feedkeys(":syntax list mpar\<Tab>\<C-B>\"\<CR>", 'tx')
3205 call assert_equal('"syntax list mpar', @:)
3206 set wildoptions=fuzzy
3207 call feedkeys(":syntax list mpar\<Tab>\<C-B>\"\<CR>", 'tx')
3208 call assert_equal('"syntax list MatchParen', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003209 set wildoptions&
3210endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003211
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003212" :syntime suboptions fuzzy completion
3213func Test_fuzzy_completion_syntime()
3214 CheckFeature profile
3215 set wildoptions&
3216 call feedkeys(":syntime clr\<Tab>\<C-B>\"\<CR>", 'tx')
3217 call assert_equal('"syntime clr', @:)
3218 set wildoptions=fuzzy
3219 call feedkeys(":syntime clr\<Tab>\<C-B>\"\<CR>", 'tx')
3220 call assert_equal('"syntime clear', @:)
3221 set wildoptions&
3222endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003223
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003224" " tag name fuzzy completion - NOT supported
3225" func Test_fuzzy_completion_tagname()
3226" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003227
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003228" " tag name and file fuzzy completion - NOT supported
3229" func Test_fuzzy_completion_tagfile()
3230" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003231
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003232" " user names fuzzy completion - how to test this functionality?
3233" func Test_fuzzy_completion_username()
3234" endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003235
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003236" user defined variable name fuzzy completion
3237func Test_fuzzy_completion_userdefined_var()
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003238 let g:SomeVariable=10
3239 set wildoptions&
3240 call feedkeys(":let SVar\<Tab>\<C-B>\"\<CR>", 'tx')
3241 call assert_equal('"let SVar', @:)
3242 set wildoptions=fuzzy
3243 call feedkeys(":let SVar\<Tab>\<C-B>\"\<CR>", 'tx')
3244 call assert_equal('"let SomeVariable', @:)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003245 set wildoptions&
3246endfunc
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003247
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003248" Test for sorting the results by the best match
3249func Test_fuzzy_completion_cmd_sort_results()
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003250 %bw!
3251 command T123format :
3252 command T123goformat :
3253 command T123TestFOrmat :
3254 command T123fendoff :
3255 command T123state :
3256 command T123FendingOff :
3257 set wildoptions=fuzzy
3258 call feedkeys(":T123fo\<C-A>\<C-B>\"\<CR>", 'tx')
3259 call assert_equal('"T123format T123TestFOrmat T123FendingOff T123goformat T123fendoff', @:)
3260 delcommand T123format
3261 delcommand T123goformat
3262 delcommand T123TestFOrmat
3263 delcommand T123fendoff
3264 delcommand T123state
3265 delcommand T123FendingOff
3266 %bw
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003267 set wildoptions&
3268endfunc
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003269
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003270" Test for fuzzy completion of a command with lower case letters and a number
3271func Test_fuzzy_completion_cmd_alnum()
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00003272 command Foo2Bar :
3273 set wildoptions=fuzzy
3274 call feedkeys(":foo2\<Tab>\<C-B>\"\<CR>", 'tx')
3275 call assert_equal('"Foo2Bar', @:)
3276 call feedkeys(":foo\<Tab>\<C-B>\"\<CR>", 'tx')
3277 call assert_equal('"Foo2Bar', @:)
3278 call feedkeys(":bar\<Tab>\<C-B>\"\<CR>", 'tx')
3279 call assert_equal('"Foo2Bar', @:)
3280 delcommand Foo2Bar
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003281 set wildoptions&
3282endfunc
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00003283
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003284" Test for command completion for a command starting with 'k'
3285func Test_fuzzy_completion_cmd_k()
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003286 command KillKillKill :
3287 set wildoptions&
3288 call feedkeys(":killkill\<Tab>\<C-B>\"\<CR>", 'tx')
3289 call assert_equal("\"killkill\<Tab>", @:)
3290 set wildoptions=fuzzy
3291 call feedkeys(":killkill\<Tab>\<C-B>\"\<CR>", 'tx')
3292 call assert_equal('"KillKillKill', @:)
3293 delcom KillKillKill
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003294 set wildoptions&
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003295endfunc
3296
3297" Test for fuzzy completion for user defined custom completion function
3298func Test_fuzzy_completion_custom_func()
3299 func Tcompl(a, c, p)
3300 return "format\ngoformat\nTestFOrmat\nfendoff\nstate"
3301 endfunc
3302 command -nargs=* -complete=custom,Tcompl Fuzzy :
3303 set wildoptions&
3304 call feedkeys(":Fuzzy fo\<C-A>\<C-B>\"\<CR>", 'tx')
3305 call assert_equal("\"Fuzzy format", @:)
3306 call feedkeys(":Fuzzy xy\<Tab>\<C-B>\"\<CR>", 'tx')
3307 call assert_equal("\"Fuzzy xy", @:)
3308 call feedkeys(":Fuzzy ttt\<C-A>\<C-B>\"\<CR>", 'tx')
3309 call assert_equal("\"Fuzzy ttt", @:)
3310 set wildoptions=fuzzy
3311 call feedkeys(":Fuzzy \<C-A>\<C-B>\"\<CR>", 'tx')
3312 call assert_equal("\"Fuzzy format goformat TestFOrmat fendoff state", @:)
3313 call feedkeys(":Fuzzy fo\<C-A>\<C-B>\"\<CR>", 'tx')
3314 call assert_equal("\"Fuzzy format TestFOrmat goformat fendoff", @:)
3315 call feedkeys(":Fuzzy xy\<Tab>\<C-B>\"\<CR>", 'tx')
3316 call assert_equal("\"Fuzzy xy", @:)
3317 call feedkeys(":Fuzzy ttt\<C-A>\<C-B>\"\<CR>", 'tx')
3318 call assert_equal("\"Fuzzy TestFOrmat", @:)
3319 delcom Fuzzy
3320 set wildoptions&
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003321endfunc
3322
Yee Cheng Chin209ec902023-10-17 10:56:25 +02003323" Test for fuzzy completion in the middle of a cmdline instead of at the end
3324func Test_fuzzy_completion_in_middle()
3325 set wildoptions=fuzzy
3326 call feedkeys(":set ildar wrap\<Left>\<Left>\<Left>\<Left>\<Left>\<C-A>\<C-B>\"\<CR>", 'tx')
3327 call assert_equal("\"set wildchar wildcharm wrap", @:)
3328
3329 call feedkeys(":args ++odng zz\<Left>\<Left>\<Left>\<C-A>\<C-B>\"\<CR>", 'tx')
3330 call assert_equal("\"args ++encoding= zz", @:)
3331 set wildoptions&
3332endfunc
3333
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003334" Test for :breakadd argument completion
3335func Test_cmdline_complete_breakadd()
3336 call feedkeys(":breakadd \<C-A>\<C-B>\"\<CR>", 'tx')
3337 call assert_equal("\"breakadd expr file func here", @:)
3338 call feedkeys(":breakadd \<Tab>\<C-B>\"\<CR>", 'tx')
3339 call assert_equal("\"breakadd expr", @:)
3340 call feedkeys(":breakadd \<Tab>\<C-B>\"\<CR>", 'tx')
3341 call assert_equal("\"breakadd expr", @:)
3342 call feedkeys(":breakadd he\<Tab>\<C-B>\"\<CR>", 'tx')
3343 call assert_equal("\"breakadd here", @:)
3344 call feedkeys(":breakadd he\<Tab>\<C-B>\"\<CR>", 'tx')
3345 call assert_equal("\"breakadd here", @:)
3346 call feedkeys(":breakadd abc\<Tab>\<C-B>\"\<CR>", 'tx')
3347 call assert_equal("\"breakadd abc", @:)
3348 call assert_equal(['expr', 'file', 'func', 'here'], getcompletion('', 'breakpoint'))
3349 let l = getcompletion('not', 'breakpoint')
3350 call assert_equal([], l)
3351
3352 " Test for :breakadd file [lnum] <file>
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01003353 call writefile([], 'Xscript', 'D')
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003354 call feedkeys(":breakadd file Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3355 call assert_equal("\"breakadd file Xscript", @:)
3356 call feedkeys(":breakadd file Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3357 call assert_equal("\"breakadd file Xscript", @:)
3358 call feedkeys(":breakadd file 20 Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3359 call assert_equal("\"breakadd file 20 Xscript", @:)
3360 call feedkeys(":breakadd file 20 Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3361 call assert_equal("\"breakadd file 20 Xscript", @:)
3362 call feedkeys(":breakadd file 20x Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3363 call assert_equal("\"breakadd file 20x Xsc\t", @:)
3364 call feedkeys(":breakadd file 20\<Tab>\<C-B>\"\<CR>", 'tx')
3365 call assert_equal("\"breakadd file 20\t", @:)
3366 call feedkeys(":breakadd file 20x\<Tab>\<C-B>\"\<CR>", 'tx')
3367 call assert_equal("\"breakadd file 20x\t", @:)
3368 call feedkeys(":breakadd file Xscript \<Tab>\<C-B>\"\<CR>", 'tx')
3369 call assert_equal("\"breakadd file Xscript ", @:)
3370 call feedkeys(":breakadd file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3371 call assert_equal("\"breakadd file X1B2C3", @:)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003372
3373 " Test for :breakadd func [lnum] <function>
3374 func Xbreak_func()
3375 endfunc
3376 call feedkeys(":breakadd func Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3377 call assert_equal("\"breakadd func Xbreak_func", @:)
3378 call feedkeys(":breakadd func Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3379 call assert_equal("\"breakadd func Xbreak_func", @:)
3380 call feedkeys(":breakadd func 20 Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3381 call assert_equal("\"breakadd func 20 Xbreak_func", @:)
3382 call feedkeys(":breakadd func 20 Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3383 call assert_equal("\"breakadd func 20 Xbreak_func", @:)
3384 call feedkeys(":breakadd func 20x Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3385 call assert_equal("\"breakadd func 20x Xbr\t", @:)
3386 call feedkeys(":breakadd func 20\<Tab>\<C-B>\"\<CR>", 'tx')
3387 call assert_equal("\"breakadd func 20\t", @:)
3388 call feedkeys(":breakadd func 20x\<Tab>\<C-B>\"\<CR>", 'tx')
3389 call assert_equal("\"breakadd func 20x\t", @:)
3390 call feedkeys(":breakadd func Xbreak_func \<Tab>\<C-B>\"\<CR>", 'tx')
3391 call assert_equal("\"breakadd func Xbreak_func ", @:)
3392 call feedkeys(":breakadd func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3393 call assert_equal("\"breakadd func X1B2C3", @:)
3394 delfunc Xbreak_func
3395
3396 " Test for :breakadd expr <expression>
3397 let g:Xtest_var = 10
3398 call feedkeys(":breakadd expr Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3399 call assert_equal("\"breakadd expr Xtest_var", @:)
3400 call feedkeys(":breakadd expr Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3401 call assert_equal("\"breakadd expr Xtest_var", @:)
3402 call feedkeys(":breakadd expr Xtest_var \<Tab>\<C-B>\"\<CR>", 'tx')
3403 call assert_equal("\"breakadd expr Xtest_var ", @:)
3404 call feedkeys(":breakadd expr X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3405 call assert_equal("\"breakadd expr X1B2C3", @:)
3406 unlet g:Xtest_var
3407
3408 " Test for :breakadd here
3409 call feedkeys(":breakadd here Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3410 call assert_equal("\"breakadd here Xtest", @:)
3411 call feedkeys(":breakadd here Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3412 call assert_equal("\"breakadd here Xtest", @:)
3413 call feedkeys(":breakadd here \<Tab>\<C-B>\"\<CR>", 'tx')
3414 call assert_equal("\"breakadd here ", @:)
3415endfunc
3416
3417" Test for :breakdel argument completion
3418func Test_cmdline_complete_breakdel()
3419 call feedkeys(":breakdel \<C-A>\<C-B>\"\<CR>", 'tx')
3420 call assert_equal("\"breakdel file func here", @:)
3421 call feedkeys(":breakdel \<Tab>\<C-B>\"\<CR>", 'tx')
3422 call assert_equal("\"breakdel file", @:)
3423 call feedkeys(":breakdel \<Tab>\<C-B>\"\<CR>", 'tx')
3424 call assert_equal("\"breakdel file", @:)
3425 call feedkeys(":breakdel he\<Tab>\<C-B>\"\<CR>", 'tx')
3426 call assert_equal("\"breakdel here", @:)
3427 call feedkeys(":breakdel he\<Tab>\<C-B>\"\<CR>", 'tx')
3428 call assert_equal("\"breakdel here", @:)
3429 call feedkeys(":breakdel abc\<Tab>\<C-B>\"\<CR>", 'tx')
3430 call assert_equal("\"breakdel abc", @:)
3431
3432 " Test for :breakdel file [lnum] <file>
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01003433 call writefile([], 'Xscript', 'D')
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003434 call feedkeys(":breakdel file Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3435 call assert_equal("\"breakdel file Xscript", @:)
3436 call feedkeys(":breakdel file Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3437 call assert_equal("\"breakdel file Xscript", @:)
3438 call feedkeys(":breakdel file 20 Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3439 call assert_equal("\"breakdel file 20 Xscript", @:)
3440 call feedkeys(":breakdel file 20 Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3441 call assert_equal("\"breakdel file 20 Xscript", @:)
3442 call feedkeys(":breakdel file 20x Xsc\<Tab>\<C-B>\"\<CR>", 'tx')
3443 call assert_equal("\"breakdel file 20x Xsc\t", @:)
3444 call feedkeys(":breakdel file 20\<Tab>\<C-B>\"\<CR>", 'tx')
3445 call assert_equal("\"breakdel file 20\t", @:)
3446 call feedkeys(":breakdel file 20x\<Tab>\<C-B>\"\<CR>", 'tx')
3447 call assert_equal("\"breakdel file 20x\t", @:)
3448 call feedkeys(":breakdel file Xscript \<Tab>\<C-B>\"\<CR>", 'tx')
3449 call assert_equal("\"breakdel file Xscript ", @:)
3450 call feedkeys(":breakdel file X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3451 call assert_equal("\"breakdel file X1B2C3", @:)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003452
3453 " Test for :breakdel func [lnum] <function>
3454 func Xbreak_func()
3455 endfunc
3456 call feedkeys(":breakdel func Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3457 call assert_equal("\"breakdel func Xbreak_func", @:)
3458 call feedkeys(":breakdel func Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3459 call assert_equal("\"breakdel func Xbreak_func", @:)
3460 call feedkeys(":breakdel func 20 Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3461 call assert_equal("\"breakdel func 20 Xbreak_func", @:)
3462 call feedkeys(":breakdel func 20 Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3463 call assert_equal("\"breakdel func 20 Xbreak_func", @:)
3464 call feedkeys(":breakdel func 20x Xbr\<Tab>\<C-B>\"\<CR>", 'tx')
3465 call assert_equal("\"breakdel func 20x Xbr\t", @:)
3466 call feedkeys(":breakdel func 20\<Tab>\<C-B>\"\<CR>", 'tx')
3467 call assert_equal("\"breakdel func 20\t", @:)
3468 call feedkeys(":breakdel func 20x\<Tab>\<C-B>\"\<CR>", 'tx')
3469 call assert_equal("\"breakdel func 20x\t", @:)
3470 call feedkeys(":breakdel func Xbreak_func \<Tab>\<C-B>\"\<CR>", 'tx')
3471 call assert_equal("\"breakdel func Xbreak_func ", @:)
3472 call feedkeys(":breakdel func X1B2C3\<Tab>\<C-B>\"\<CR>", 'tx')
3473 call assert_equal("\"breakdel func X1B2C3", @:)
3474 delfunc Xbreak_func
3475
3476 " Test for :breakdel here
3477 call feedkeys(":breakdel here Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3478 call assert_equal("\"breakdel here Xtest", @:)
3479 call feedkeys(":breakdel here Xtest\<Tab>\<C-B>\"\<CR>", 'tx')
3480 call assert_equal("\"breakdel here Xtest", @:)
3481 call feedkeys(":breakdel here \<Tab>\<C-B>\"\<CR>", 'tx')
3482 call assert_equal("\"breakdel here ", @:)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003483endfunc
3484
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003485" Test for :scriptnames argument completion
3486func Test_cmdline_complete_scriptnames()
3487 set wildmenu
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01003488 call writefile(['let a = 1'], 'Xa1b2c3.vim', 'D')
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003489 source Xa1b2c3.vim
3490 call feedkeys(":script \<Tab>\<Left>\<Left>\<C-B>\"\<CR>", 'tx')
3491 call assert_match("\"script .*Xa1b2c3.vim$", @:)
3492 call feedkeys(":script \<Tab>\<Left>\<Left>\<C-B>\"\<CR>", 'tx')
3493 call assert_match("\"script .*Xa1b2c3.vim$", @:)
3494 call feedkeys(":script b2c3\<Tab>\<C-B>\"\<CR>", 'tx')
3495 call assert_equal("\"script b2c3", @:)
3496 call feedkeys(":script 2\<Tab>\<C-B>\"\<CR>", 'tx')
3497 call assert_match("\"script 2\<Tab>$", @:)
3498 call feedkeys(":script \<Tab>\<Left>\<Left> \<Tab>\<C-B>\"\<CR>", 'tx')
3499 call assert_match("\"script .*Xa1b2c3.vim $", @:)
3500 call feedkeys(":script \<Tab>\<Left>\<C-B>\"\<CR>", 'tx')
3501 call assert_equal("\"script ", @:)
3502 call assert_match('Xa1b2c3.vim$', getcompletion('.*Xa1b2.*', 'scriptnames')[0])
3503 call assert_equal([], getcompletion('Xa1b2', 'scriptnames'))
3504 new
3505 call feedkeys(":script \<Tab>\<Left>\<Left>\<CR>", 'tx')
3506 call assert_equal('Xa1b2c3.vim', fnamemodify(@%, ':t'))
3507 bw!
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003508 set wildmenu&
3509endfunc
3510
Bram Moolenaard8893442022-05-06 20:38:47 +01003511" this was going over the end of IObuff
3512func Test_report_error_with_composing()
3513 let caught = 'no'
3514 try
3515 exe repeat('0', 987) .. "0\xdd\x80\xdd\x80\xdd\x80\xdd\x80"
3516 catch /E492:/
3517 let caught = 'yes'
3518 endtry
3519 call assert_equal('yes', caught)
3520endfunc
3521
Yegappan Lakshmanan5e877ba2022-03-25 21:19:26 +00003522" Test for expanding 2-letter and 3-letter :substitute command arguments.
3523" These commands don't accept an argument.
3524func Test_cmdline_complete_substitute_short()
3525 for cmd in ['sc', 'sce', 'scg', 'sci', 'scI', 'scn', 'scp', 'scl',
3526 \ 'sgc', 'sge', 'sg', 'sgi', 'sgI', 'sgn', 'sgp', 'sgl', 'sgr',
3527 \ 'sic', 'sie', 'si', 'siI', 'sin', 'sip', 'sir',
3528 \ 'sIc', 'sIe', 'sIg', 'sIi', 'sI', 'sIn', 'sIp', 'sIl', 'sIr',
3529 \ 'src', 'srg', 'sri', 'srI', 'srn', 'srp', 'srl', 'sr']
3530 call feedkeys(':' .. cmd .. " \<Tab>\<C-B>\"\<CR>", 'tx')
3531 call assert_equal('"' .. cmd .. " \<Tab>", @:)
3532 endfor
3533endfunc
3534
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01003535" Test for :! shell command argument completion
3536func Test_cmdline_complete_bang_cmd_argument()
3537 set wildoptions=fuzzy
3538 call feedkeys(":!vim test_cmdline.\<Tab>\<C-B>\"\<CR>", 'xt')
3539 call assert_equal('"!vim test_cmdline.vim', @:)
3540 set wildoptions&
3541 call feedkeys(":!vim test_cmdline.\<Tab>\<C-B>\"\<CR>", 'xt')
3542 call assert_equal('"!vim test_cmdline.vim', @:)
3543endfunc
3544
zeertzjq961b2e52023-04-17 15:53:24 +01003545func Call_cmd_funcs()
3546 return string([getcmdpos(), getcmdscreenpos(), getcmdcompltype()])
Shougo Matsushita79d599b2022-05-07 12:48:29 +01003547endfunc
3548
3549func Test_screenpos_and_completion()
zeertzjq961b2e52023-04-17 15:53:24 +01003550 call assert_equal(0, getcmdpos())
3551 call assert_equal(0, getcmdscreenpos())
3552 call assert_equal('', getcmdcompltype())
3553
3554 cnoremap <expr> <F2> string([getcmdpos(), getcmdscreenpos(), getcmdcompltype()])
3555 call feedkeys(":let a\<F2>\<C-B>\"\<CR>", "xt")
3556 call assert_equal("\"let a[6, 7, 'var']", @:)
3557 call feedkeys(":quit \<F2>\<C-B>\"\<CR>", "xt")
3558 call assert_equal("\"quit [6, 7, '']", @:)
3559 call feedkeys(":nosuchcommand \<F2>\<C-B>\"\<CR>", "xt")
3560 call assert_equal("\"nosuchcommand [15, 16, '']", @:)
3561 cunmap <F2>
Shougo Matsushita79d599b2022-05-07 12:48:29 +01003562endfunc
3563
Bram Moolenaar51f0bfb2022-05-17 20:11:02 +01003564func Test_recursive_register()
3565 let @= = ''
3566 silent! ?e/
3567 let caught = 'no'
3568 try
3569 normal //
3570 catch /E169:/
3571 let caught = 'yes'
3572 endtry
3573 call assert_equal('yes', caught)
3574endfunc
3575
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003576func Test_long_error_message()
3577 " the error should be truncated, not overrun IObuff
3578 silent! norm Q00000000000000     000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000                                                                                                                                                                                                                        
3579endfunc
3580
zeertzjq6791adc2022-07-26 20:42:25 +01003581func Test_cmdline_redraw_tabline()
3582 CheckRunVimInTerminal
3583
3584 let lines =<< trim END
3585 set showtabline=2
3586 autocmd CmdlineEnter * set tabline=foo
3587 END
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01003588 call writefile(lines, 'Xcmdline_redraw_tabline', 'D')
zeertzjq6791adc2022-07-26 20:42:25 +01003589 let buf = RunVimInTerminal('-S Xcmdline_redraw_tabline', #{rows: 6})
3590 call term_sendkeys(buf, ':')
3591 call WaitForAssert({-> assert_match('^foo', term_getline(buf, 1))})
3592
3593 call StopVimInTerminal(buf)
zeertzjq6791adc2022-07-26 20:42:25 +01003594endfunc
3595
zeertzjqb82a2ab2022-08-21 14:33:57 +01003596func Test_wildmenu_pum_disable_while_shown()
3597 set wildoptions=pum
3598 set wildmenu
3599 cnoremap <F2> <Cmd>set nowildmenu<CR>
3600 call feedkeys(":sign \<Tab>\<F2>\<Esc>", 'tx')
3601 call assert_equal(0, pumvisible())
3602 cunmap <F2>
3603 set wildoptions& wildmenu&
3604endfunc
3605
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003606func Test_setcmdline()
3607 func SetText(text, pos)
zeertzjqac6cd312023-04-12 16:21:14 +01003608 call assert_equal(0, setcmdline(test_null_string()))
3609 call assert_equal('', getcmdline())
3610 call assert_equal(1, getcmdpos())
3611
3612 call assert_equal(0, setcmdline(''[: -1]))
3613 call assert_equal('', getcmdline())
3614 call assert_equal(1, getcmdpos())
3615
zeertzjq54acb902022-08-29 16:21:25 +01003616 autocmd CmdlineChanged * let g:cmdtype = expand('<afile>')
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003617 call assert_equal(0, setcmdline(a:text))
3618 call assert_equal(a:text, getcmdline())
3619 call assert_equal(len(a:text) + 1, getcmdpos())
zeertzjq54acb902022-08-29 16:21:25 +01003620 call assert_equal(getcmdtype(), g:cmdtype)
3621 unlet g:cmdtype
3622 autocmd! CmdlineChanged
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003623
3624 call assert_equal(0, setcmdline(a:text, a:pos))
3625 call assert_equal(a:text, getcmdline())
3626 call assert_equal(a:pos, getcmdpos())
3627
3628 call assert_fails('call setcmdline("' .. a:text .. '", -1)', 'E487:')
Yegappan Lakshmanan25f1e552022-08-28 17:25:04 +01003629 call assert_fails('call setcmdline({}, 0)', 'E1174:')
3630 call assert_fails('call setcmdline("' .. a:text .. '", {})', 'E1210:')
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003631
3632 return ''
3633 endfunc
3634
3635 call feedkeys(":\<C-R>=SetText('set rtp?', 2)\<CR>\<CR>", 'xt')
3636 call assert_equal('set rtp?', @:)
3637
zeertzjq54acb902022-08-29 16:21:25 +01003638 call feedkeys(":let g:str = input('? ')\<CR>", 't')
3639 call feedkeys("\<C-R>=SetText('foo', 4)\<CR>\<CR>", 'xt')
3640 call assert_equal('foo', g:str)
3641 unlet g:str
3642
3643 delfunc SetText
3644
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003645 " setcmdline() returns 1 when not editing the command line.
3646 call assert_equal(1, 'foo'->setcmdline())
3647
3648 " Called in custom function
3649 func CustomComplete(A, L, P)
3650 call assert_equal(0, setcmdline("DoCmd "))
3651 return "January\nFebruary\nMars\n"
3652 endfunc
3653
3654 com! -nargs=* -complete=custom,CustomComplete DoCmd :
3655 call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
3656 call assert_equal('"DoCmd January February Mars', @:)
zeertzjq54acb902022-08-29 16:21:25 +01003657 delcom DoCmd
3658 delfunc CustomComplete
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01003659
3660 " Called in <expr>
3661 cnoremap <expr>a setcmdline('let foo=')
3662 call feedkeys(":a\<CR>", 'tx')
3663 call assert_equal('let foo=0', @:)
3664 cunmap a
3665endfunc
3666
Sean Dewarfc8a6012023-04-17 16:41:20 +01003667func Test_rulerformat_position()
3668 CheckScreendump
3669
3670 let buf = RunVimInTerminal('', #{rows: 2, cols: 20})
3671 call term_sendkeys(buf, ":set ruler rulerformat=longish\<CR>")
3672 call term_sendkeys(buf, ":set laststatus=0 winwidth=1\<CR>")
3673 call term_sendkeys(buf, "\<C-W>v\<C-W>|\<C-W>p")
3674 call VerifyScreenDump(buf, 'Test_rulerformat_position', {})
3675
3676 " clean up
3677 call StopVimInTerminal(buf)
3678endfunc
3679
zeertzjqe4c79d32023-08-15 22:41:53 +02003680func Test_getcompletion_usercmd()
Christian Brabandt8ef1fbc2023-07-17 20:09:37 +02003681 command! -nargs=* -complete=command TestCompletion echo <q-args>
Christian Brabandt8ef1fbc2023-07-17 20:09:37 +02003682
zeertzjqe4c79d32023-08-15 22:41:53 +02003683 call assert_equal(getcompletion('', 'cmdline'),
3684 \ getcompletion('TestCompletion ', 'cmdline'))
3685 call assert_equal(['<buffer>'],
3686 \ getcompletion('TestCompletion map <bu', 'cmdline'))
3687
Christian Brabandt8ef1fbc2023-07-17 20:09:37 +02003688 delcom TestCompletion
Christian Brabandt8ef1fbc2023-07-17 20:09:37 +02003689endfunc
zeertzjqe4c79d32023-08-15 22:41:53 +02003690
Shougo Matsushita92997dd2023-08-20 20:55:55 +02003691func Test_custom_completion()
3692 func CustomComplete1(lead, line, pos)
3693 return "a\nb\nc"
3694 endfunc
3695 func CustomComplete2(lead, line, pos)
3696 return ['a', 'b']->filter({ _, val -> val->stridx(a:lead) == 0 })
3697 endfunc
3698 func Check_custom_completion()
3699 call assert_equal('custom,CustomComplete1', getcmdcompltype())
3700 return ''
3701 endfunc
3702 func Check_customlist_completion()
3703 call assert_equal('customlist,CustomComplete2', getcmdcompltype())
3704 return ''
3705 endfunc
3706
3707 command -nargs=1 -complete=custom,CustomComplete1 Test1 echo
3708 command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
3709
3710 call feedkeys(":Test1 \<C-R>=Check_custom_completion()\<CR>\<Esc>", "xt")
3711 call feedkeys(":Test2 \<C-R>=Check_customlist_completion()\<CR>\<Esc>", "xt")
3712
3713 call assert_fails("call getcompletion('', 'custom')", 'E475:')
3714 call assert_fails("call getcompletion('', 'customlist')", 'E475:')
3715
3716 call assert_equal(getcompletion('', 'custom,CustomComplete1'), ['a', 'b', 'c'])
3717 call assert_equal(getcompletion('', 'customlist,CustomComplete2'), ['a', 'b'])
3718 call assert_equal(getcompletion('b', 'customlist,CustomComplete2'), ['b'])
3719
3720 delcom Test1
3721 delcom Test2
3722
3723 delfunc CustomComplete1
3724 delfunc CustomComplete2
3725 delfunc Check_custom_completion
3726 delfunc Check_customlist_completion
3727endfunc
3728
zeertzjq28a23602023-09-29 19:58:35 +02003729func Test_custom_completion_with_glob()
3730 func TestGlobComplete(A, L, P)
3731 return split(glob('Xglob*'), "\n")
3732 endfunc
3733
3734 command -nargs=* -complete=customlist,TestGlobComplete TestGlobComplete :
3735 call writefile([], 'Xglob1', 'D')
3736 call writefile([], 'Xglob2', 'D')
3737
3738 call feedkeys(":TestGlobComplete \<Tab> \<Tab>\<C-N> \<Tab>\<C-P>;\<C-B>\"\<CR>", 'xt')
3739 call assert_equal('"TestGlobComplete Xglob1 Xglob2 ;', @:)
3740
3741 delcommand TestGlobComplete
3742 delfunc TestGlobComplete
3743endfunc
3744
Bram Moolenaar309976e2019-12-05 18:16:33 +01003745" vim: shiftwidth=2 sts=2 expandtab