blob: 0267d28e0f7127e5656cdec82931f20e4974c5a1 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000018static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000019static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020020static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020022#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000023static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020025#endif
26
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000027// "compl_match_array" points the currently displayed list of entries in the
28// popup menu. It is NULL when there is no popup menu.
29static pumitem_T *compl_match_array = NULL;
30static int compl_match_arraysize;
31// First column in cmdline of the matched item for completion.
32static int compl_startcol;
33static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000034
zeertzjqc51a3762022-12-10 10:22:29 +000035#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000038 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
39 * context.
40 */
41 static int
42cmdline_fuzzy_completion_supported(expand_T *xp)
43{
44 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
45 && xp->xp_context != EXPAND_BOOL_SETTINGS
46 && xp->xp_context != EXPAND_COLORS
47 && xp->xp_context != EXPAND_COMPILER
48 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020049 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000050 && xp->xp_context != EXPAND_FILES
51 && xp->xp_context != EXPAND_FILES_IN_PATH
52 && xp->xp_context != EXPAND_FILETYPE
53 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010054 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000055 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020056 && xp->xp_context != EXPAND_STRING_SETTING
57 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000058 && xp->xp_context != EXPAND_OWNSYNTAX
59 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000060 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000061 && xp->xp_context != EXPAND_SHELLCMD
62 && xp->xp_context != EXPAND_TAGS
63 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064 && xp->xp_context != EXPAND_USER_LIST);
65}
66
67/*
68 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000069 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
70 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000071 */
72 int
73cmdline_fuzzy_complete(char_u *fuzzystr)
74{
75 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
76}
77
78/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000079 * sort function for the completion matches.
80 * <SNR> functions should be sorted to the end.
81 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020082 static int
83sort_func_compare(const void *s1, const void *s2)
84{
85 char_u *p1 = *(char_u **)s1;
86 char_u *p2 = *(char_u **)s2;
87
88 if (*p1 != '<' && *p2 == '<') return -1;
89 if (*p1 == '<' && *p2 != '<') return 1;
90 return STRCMP(p1, p2);
91}
Bram Moolenaar66b51422019-08-18 21:44:12 +020092
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000093/*
94 * Escape special characters in the cmdline completion matches.
95 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020096 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000097wildescape(
98 expand_T *xp,
99 char_u *str,
100 int numfiles,
101 char_u **files)
102{
103 char_u *p;
104 int vse_what = xp->xp_context == EXPAND_BUFFERS
105 ? VSE_BUFFER : VSE_NONE;
106
107 if (xp->xp_context == EXPAND_FILES
108 || xp->xp_context == EXPAND_FILES_IN_PATH
109 || xp->xp_context == EXPAND_SHELLCMD
110 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200111 || xp->xp_context == EXPAND_DIRECTORIES
112 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000113 {
114 // Insert a backslash into a file name before a space, \, %, #
115 // and wildmatch characters, except '~'.
116 for (int i = 0; i < numfiles; ++i)
117 {
118 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200119 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000120 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200121 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
122 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000123 if (p != NULL)
124 {
125 vim_free(files[i]);
126 files[i] = p;
127#if defined(BACKSLASH_IN_FILENAME)
128 p = vim_strsave_escaped(files[i], (char_u *)" ");
129 if (p != NULL)
130 {
131 vim_free(files[i]);
132 files[i] = p;
133 }
134#endif
135 }
136 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200137 else if (xp->xp_backslash & XP_BS_COMMA)
138 {
139 if (vim_strchr(files[i], ',') != NULL)
140 {
141 p = vim_strsave_escaped(files[i], (char_u *)",");
142 if (p != NULL)
143 {
144 vim_free(files[i]);
145 files[i] = p;
146 }
147 }
148 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000149#ifdef BACKSLASH_IN_FILENAME
150 p = vim_strsave_fnameescape(files[i], vse_what);
151#else
152 p = vim_strsave_fnameescape(files[i],
153 xp->xp_shell ? VSE_SHELL : vse_what);
154#endif
155 if (p != NULL)
156 {
157 vim_free(files[i]);
158 files[i] = p;
159 }
160
161 // If 'str' starts with "\~", replace "~" at start of
162 // files[i] with "\~".
163 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
164 escape_fname(&files[i]);
165 }
166 xp->xp_backslash = XP_BS_NONE;
167
168 // If the first file starts with a '+' escape it. Otherwise it
169 // could be seen as "+cmd".
170 if (*files[0] == '+')
171 escape_fname(&files[0]);
172 }
173 else if (xp->xp_context == EXPAND_TAGS)
174 {
175 // Insert a backslash before characters in a tag name that
176 // would terminate the ":tag" command.
177 for (int i = 0; i < numfiles; ++i)
178 {
179 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
180 if (p != NULL)
181 {
182 vim_free(files[i]);
183 files[i] = p;
184 }
185 }
186 }
187}
188
189/*
190 * Escape special characters in the cmdline completion matches.
191 */
192 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200193ExpandEscape(
194 expand_T *xp,
195 char_u *str,
196 int numfiles,
197 char_u **files,
198 int options)
199{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200200 // May change home directory back to "~"
201 if (options & WILD_HOME_REPLACE)
202 tilde_replace(str, numfiles, files);
203
204 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000205 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200206}
207
208/*
209 * Return FAIL if this is not an appropriate context in which to do
210 * completion of anything, return OK if it is (even if there are no matches).
211 * For the caller, this means that the character is just passed through like a
212 * normal character (instead of being expanded). This allows :s/^I^D etc.
213 */
214 int
215nextwild(
216 expand_T *xp,
217 int type,
218 int options, // extra options for ExpandOne()
219 int escape) // if TRUE, escape the returned matches
220{
221 cmdline_info_T *ccline = get_cmdline_info();
222 int i, j;
223 char_u *p1;
224 char_u *p2;
225 int difflen;
226 int v;
227
228 if (xp->xp_numfiles == -1)
229 {
230 set_expand_context(xp);
231 cmd_showtail = expand_showtail(xp);
232 }
233
234 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
235 {
236 beep_flush();
237 return OK; // Something illegal on command line
238 }
239 if (xp->xp_context == EXPAND_NOTHING)
240 {
241 // Caller can use the character as a normal char instead
242 return FAIL;
243 }
244
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000245 // If cmd_silent is set then don't show the dots, because redrawcmd() below
246 // won't remove them.
247 if (!cmd_silent)
248 {
249 msg_puts("..."); // show that we are busy
250 out_flush();
251 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200252
253 i = (int)(xp->xp_pattern - ccline->cmdbuff);
254 xp->xp_pattern_len = ccline->cmdpos - i;
255
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000256 if (type == WILD_NEXT || type == WILD_PREV
257 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200258 {
259 // Get next/previous match for a previous expanded pattern.
260 p2 = ExpandOne(xp, NULL, NULL, 0, type);
261 }
262 else
263 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000264 if (cmdline_fuzzy_completion_supported(xp))
265 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200266 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000267 else
268 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
269
Bram Moolenaar66b51422019-08-18 21:44:12 +0200270 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000271 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200272 p2 = NULL;
273 else
274 {
275 int use_options = options |
276 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
277 if (escape)
278 use_options |= WILD_ESCAPE;
279
280 if (p_wic)
281 use_options += WILD_ICASE;
282 p2 = ExpandOne(xp, p1,
283 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
284 use_options, type);
285 vim_free(p1);
286 // longest match: make sure it is not shorter, happens with :help
287 if (p2 != NULL && type == WILD_LONGEST)
288 {
289 for (j = 0; j < xp->xp_pattern_len; ++j)
290 if (ccline->cmdbuff[i + j] == '*'
291 || ccline->cmdbuff[i + j] == '?')
292 break;
293 if ((int)STRLEN(p2) < j)
294 VIM_CLEAR(p2);
295 }
296 }
297 }
298
299 if (p2 != NULL && !got_int)
300 {
301 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
302 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
303 {
304 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
305 xp->xp_pattern = ccline->cmdbuff + i;
306 }
307 else
308 v = OK;
309 if (v == OK)
310 {
311 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
312 &ccline->cmdbuff[ccline->cmdpos],
313 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
314 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
315 ccline->cmdlen += difflen;
316 ccline->cmdpos += difflen;
317 }
318 }
319 vim_free(p2);
320
321 redrawcmd();
322 cursorcmd();
323
324 // When expanding a ":map" command and no matches are found, assume that
325 // the key is supposed to be inserted literally
326 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
327 return FAIL;
328
329 if (xp->xp_numfiles <= 0 && p2 == NULL)
330 beep_flush();
331 else if (xp->xp_numfiles == 1)
332 // free expanded pattern
333 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
334
335 return OK;
336}
337
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000338/*
339 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000340 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000341 */
342 static int
343cmdline_pum_create(
344 cmdline_info_T *ccline,
345 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000346 char_u **matches,
347 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000348 int showtail)
349{
350 int i;
351 int columns;
352
353 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000354 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000355 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000356 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000357 {
zeertzjqc51a3762022-12-10 10:22:29 +0000358 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000359 compl_match_array[i].pum_info = NULL;
360 compl_match_array[i].pum_extra = NULL;
361 compl_match_array[i].pum_kind = NULL;
362 }
363
364 // Compute the popup menu starting column
365 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
366 columns = vim_strsize(xp->xp_pattern);
367 if (showtail)
368 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000369 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000370 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000371 }
372 if (columns >= compl_startcol)
373 compl_startcol = 0;
374 else
375 compl_startcol -= columns;
376
377 // no default selection
378 compl_selected = -1;
379
380 cmdline_pum_display();
381
382 return EXPAND_OK;
383}
384
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000385/*
386 * Display the cmdline completion matches in a popup menu
387 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000388 void
389cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000390{
391 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
392}
393
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000394/*
395 * Returns TRUE if the cmdline completion popup menu is being displayed.
396 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000397 int
398cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000399{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100400 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000401}
402
403/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000404 * Remove the cmdline completion popup menu (if present), free the list of
405 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000406 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000407 void
408cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000409{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000410 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100411 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000412
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000413 pum_undisplay();
414 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000415 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000416 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000417 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000418 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100419
420 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
421 // as a side effect.
422 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423}
424
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000425 void
426cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000427{
428 cmdline_pum_remove();
429 wildmenu_cleanup(cclp);
430}
431
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000432/*
433 * Returns the starting column number to use for the cmdline completion popup
434 * menu.
435 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000436 int
437cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000438{
439 return compl_startcol;
440}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000441
Bram Moolenaar66b51422019-08-18 21:44:12 +0200442/*
zeertzjqd8c93402024-06-17 18:25:32 +0200443 * Returns the current cmdline completion pattern.
444 */
445 char_u *
446cmdline_compl_pattern(void)
447{
448 expand_T *xp = get_cmdline_info()->xpc;
449
450 return xp == NULL ? NULL : xp->xp_orig;
451}
452
453/*
454 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
455 */
456 int
457cmdline_compl_is_fuzzy(void)
458{
459 expand_T *xp = get_cmdline_info()->xpc;
460
461 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
462}
463
464/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000465 * Return the number of characters that should be skipped in a status match.
466 * These are backslashes used for escaping. Do show backslashes in help tags.
467 */
468 static int
469skip_status_match_char(expand_T *xp, char_u *s)
470{
471 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
472#ifdef FEAT_MENU
473 || ((xp->xp_context == EXPAND_MENUS
474 || xp->xp_context == EXPAND_MENUNAMES)
475 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
476#endif
477 )
478 {
479#ifndef BACKSLASH_IN_FILENAME
480 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
481 return 2;
482#endif
483 return 1;
484 }
485 return 0;
486}
487
488/*
489 * Get the length of an item as it will be shown in the status line.
490 */
491 static int
492status_match_len(expand_T *xp, char_u *s)
493{
494 int len = 0;
495
496#ifdef FEAT_MENU
497 int emenu = xp->xp_context == EXPAND_MENUS
498 || xp->xp_context == EXPAND_MENUNAMES;
499
500 // Check for menu separators - replace with '|'.
501 if (emenu && menu_is_separator(s))
502 return 1;
503#endif
504
505 while (*s != NUL)
506 {
507 s += skip_status_match_char(xp, s);
508 len += ptr2cells(s);
509 MB_PTR_ADV(s);
510 }
511
512 return len;
513}
514
515/*
516 * Show wildchar matches in the status line.
517 * Show at least the "match" item.
518 * We start at item 'first_match' in the list and show all matches that fit.
519 *
520 * If inversion is possible we use it. Else '=' characters are used.
521 */
522 static void
523win_redr_status_matches(
524 expand_T *xp,
525 int num_matches,
526 char_u **matches, // list of matches
527 int match,
528 int showtail)
529{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000530 int row;
531 char_u *buf;
532 int len;
533 int clen; // length in screen cells
534 int fillchar;
535 int attr;
536 int i;
537 int highlight = TRUE;
538 char_u *selstart = NULL;
539 int selstart_col = 0;
540 char_u *selend = NULL;
541 static int first_match = 0;
542 int add_left = FALSE;
543 char_u *s;
544#ifdef FEAT_MENU
545 int emenu;
546#endif
547 int l;
548
549 if (matches == NULL) // interrupted completion?
550 return;
551
552 if (has_mbyte)
553 buf = alloc(Columns * MB_MAXBYTES + 1);
554 else
555 buf = alloc(Columns + 1);
556 if (buf == NULL)
557 return;
558
559 if (match == -1) // don't show match but original text
560 {
561 match = 0;
562 highlight = FALSE;
563 }
564 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000565 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000566 if (match == 0)
567 first_match = 0;
568 else if (match < first_match)
569 {
570 // jumping left, as far as we can go
571 first_match = match;
572 add_left = TRUE;
573 }
574 else
575 {
576 // check if match fits on the screen
577 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000578 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000579 if (first_match > 0)
580 clen += 2;
581 // jumping right, put match at the left
582 if ((long)clen > Columns)
583 {
584 first_match = match;
585 // if showing the last match, we can add some on the left
586 clen = 2;
587 for (i = match; i < num_matches; ++i)
588 {
zeertzjqc51a3762022-12-10 10:22:29 +0000589 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000590 if ((long)clen >= Columns)
591 break;
592 }
593 if (i == num_matches)
594 add_left = TRUE;
595 }
596 }
597 if (add_left)
598 while (first_match > 0)
599 {
zeertzjqc51a3762022-12-10 10:22:29 +0000600 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000601 if ((long)clen >= Columns)
602 break;
603 --first_match;
604 }
605
606 fillchar = fillchar_status(&attr, curwin);
607
608 if (first_match == 0)
609 {
610 *buf = NUL;
611 len = 0;
612 }
613 else
614 {
615 STRCPY(buf, "< ");
616 len = 2;
617 }
618 clen = len;
619
620 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000621 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000622 {
623 if (i == match)
624 {
625 selstart = buf + len;
626 selstart_col = clen;
627 }
628
zeertzjqc51a3762022-12-10 10:22:29 +0000629 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000630 // Check for menu separators - replace with '|'
631#ifdef FEAT_MENU
632 emenu = (xp->xp_context == EXPAND_MENUS
633 || xp->xp_context == EXPAND_MENUNAMES);
634 if (emenu && menu_is_separator(s))
635 {
636 STRCPY(buf + len, transchar('|'));
637 l = (int)STRLEN(buf + len);
638 len += l;
639 clen += l;
640 }
641 else
642#endif
643 for ( ; *s != NUL; ++s)
644 {
645 s += skip_status_match_char(xp, s);
646 clen += ptr2cells(s);
647 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
648 {
649 STRNCPY(buf + len, s, l);
650 s += l - 1;
651 len += l;
652 }
653 else
654 {
655 STRCPY(buf + len, transchar_byte(*s));
656 len += (int)STRLEN(buf + len);
657 }
658 }
659 if (i == match)
660 selend = buf + len;
661
662 *(buf + len++) = ' ';
663 *(buf + len++) = ' ';
664 clen += 2;
665 if (++i == num_matches)
666 break;
667 }
668
669 if (i != num_matches)
670 {
671 *(buf + len++) = '>';
672 ++clen;
673 }
674
675 buf[len] = NUL;
676
677 row = cmdline_row - 1;
678 if (row >= 0)
679 {
680 if (wild_menu_showing == 0)
681 {
682 if (msg_scrolled > 0)
683 {
684 // Put the wildmenu just above the command line. If there is
685 // no room, scroll the screen one line up.
686 if (cmdline_row == Rows - 1)
687 {
688 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
689 ++msg_scrolled;
690 }
691 else
692 {
693 ++cmdline_row;
694 ++row;
695 }
696 wild_menu_showing = WM_SCROLLED;
697 }
698 else
699 {
700 // Create status line if needed by setting 'laststatus' to 2.
701 // Set 'winminheight' to zero to avoid that the window is
702 // resized.
703 if (lastwin->w_status_height == 0)
704 {
705 save_p_ls = p_ls;
706 save_p_wmh = p_wmh;
707 p_ls = 2;
708 p_wmh = 0;
709 last_status(FALSE);
710 }
711 wild_menu_showing = WM_SHOWN;
712 }
713 }
714
715 screen_puts(buf, row, 0, attr);
716 if (selstart != NULL && highlight)
717 {
718 *selend = NUL;
719 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
720 }
721
722 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
723 }
724
725 win_redraw_last_status(topframe);
726 vim_free(buf);
727}
728
729/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000730 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200731 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000732 */
733 static char_u *
734get_next_or_prev_match(
735 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200736 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000737{
zeertzjqe9ef3472023-08-17 23:57:05 +0200738 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000739 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000740
741 if (xp->xp_numfiles <= 0)
742 return NULL;
743
744 if (mode == WILD_PREV)
745 {
746 if (findex == -1)
747 findex = xp->xp_numfiles;
748 --findex;
749 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000750 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000751 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000752 else if (mode == WILD_PAGEUP)
753 {
754 if (findex == 0)
755 // at the first entry, don't select any entries
756 findex = -1;
757 else if (findex == -1)
758 // no entry is selected. select the last entry
759 findex = xp->xp_numfiles - 1;
760 else
761 {
762 // go up by the pum height
763 ht = pum_get_height();
764 if (ht > 3)
765 ht -= 2;
766 findex -= ht;
767 if (findex < 0)
768 // few entries left, select the first entry
769 findex = 0;
770 }
771 }
772 else // mode == WILD_PAGEDOWN
773 {
774 if (findex == xp->xp_numfiles - 1)
775 // at the last entry, don't select any entries
776 findex = -1;
777 else if (findex == -1)
778 // no entry is selected. select the first entry
779 findex = 0;
780 else
781 {
782 // go down by the pum height
783 ht = pum_get_height();
784 if (ht > 3)
785 ht -= 2;
786 findex += ht;
787 if (findex >= xp->xp_numfiles)
788 // few entries left, select the last entry
789 findex = xp->xp_numfiles - 1;
790 }
791 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000792
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000793 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000794 if (findex < 0)
795 {
zeertzjq28a23602023-09-29 19:58:35 +0200796 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000797 findex = xp->xp_numfiles - 1;
798 else
799 findex = -1;
800 }
801 if (findex >= xp->xp_numfiles)
802 {
zeertzjq28a23602023-09-29 19:58:35 +0200803 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000804 findex = 0;
805 else
806 findex = -1;
807 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000808 if (compl_match_array)
809 {
810 compl_selected = findex;
811 cmdline_pum_display();
812 }
813 else if (p_wmnu)
814 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
815 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200816 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000817
818 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200819 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000820
821 return vim_strsave(xp->xp_files[findex]);
822}
823
824/*
825 * Start the command-line expansion and get the matches.
826 */
827 static char_u *
828ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
829{
830 int non_suf_match; // number without matching suffix
831 int i;
832 char_u *ss = NULL;
833
834 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000835 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000836 options) == FAIL)
837 {
838#ifdef FNAME_ILLEGAL
839 // Illegal file name has been silently skipped. But when there
840 // are wildcards, the real problem is that there was no match,
841 // causing the pattern to be added, which has illegal characters.
842 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
843 semsg(_(e_no_match_str_2), str);
844#endif
845 }
846 else if (xp->xp_numfiles == 0)
847 {
848 if (!(options & WILD_SILENT))
849 semsg(_(e_no_match_str_2), str);
850 }
851 else
852 {
853 // Escape the matches for use on the command line.
854 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
855
856 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000857 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000858 {
859 if (xp->xp_numfiles)
860 non_suf_match = xp->xp_numfiles;
861 else
862 non_suf_match = 1;
863 if ((xp->xp_context == EXPAND_FILES
864 || xp->xp_context == EXPAND_DIRECTORIES)
865 && xp->xp_numfiles > 1)
866 {
867 // More than one match; check suffix.
868 // The files will have been sorted on matching suffix in
869 // expand_wildcards, only need to check the first two.
870 non_suf_match = 0;
871 for (i = 0; i < 2; ++i)
872 if (match_suffix(xp->xp_files[i]))
873 ++non_suf_match;
874 }
875 if (non_suf_match != 1)
876 {
877 // Can we ever get here unless it's while expanding
878 // interactively? If not, we can get rid of this all
879 // together. Don't really want to wait for this message
880 // (and possibly have to hit return to continue!).
881 if (!(options & WILD_SILENT))
882 emsg(_(e_too_many_file_names));
883 else if (!(options & WILD_NO_BEEP))
884 beep_flush();
885 }
886 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
887 ss = vim_strsave(xp->xp_files[0]);
888 }
889 }
890
891 return ss;
892}
893
894/*
895 * Return the longest common part in the list of cmdline completion matches.
896 */
897 static char_u *
898find_longest_match(expand_T *xp, int options)
899{
900 long_u len;
901 int mb_len = 1;
902 int c0, ci;
903 int i;
904 char_u *ss;
905
906 for (len = 0; xp->xp_files[0][len]; len += mb_len)
907 {
908 if (has_mbyte)
909 {
910 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
911 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
912 }
913 else
914 c0 = xp->xp_files[0][len];
915 for (i = 1; i < xp->xp_numfiles; ++i)
916 {
917 if (has_mbyte)
918 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
919 else
920 ci = xp->xp_files[i][len];
921 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
922 || xp->xp_context == EXPAND_FILES
923 || xp->xp_context == EXPAND_SHELLCMD
924 || xp->xp_context == EXPAND_BUFFERS))
925 {
926 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
927 break;
928 }
929 else if (c0 != ci)
930 break;
931 }
932 if (i < xp->xp_numfiles)
933 {
934 if (!(options & WILD_NO_BEEP))
935 vim_beep(BO_WILD);
936 break;
937 }
938 }
939
940 ss = alloc(len + 1);
941 if (ss)
942 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
943
944 return ss;
945}
946
947/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000948 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200949 * Chars that should not be expanded must be preceded with a backslash.
950 * Return a pointer to allocated memory containing the new string.
951 * Return NULL for failure.
952 *
953 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200954 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
955 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200956 *
957 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
958 * is WILD_EXPAND_FREE or WILD_ALL.
959 *
960 * mode = WILD_FREE: just free previously expanded matches
961 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
962 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
963 * mode = WILD_NEXT: use next match in multiple match, wrap to first
964 * mode = WILD_PREV: use previous match in multiple match, wrap to first
965 * mode = WILD_ALL: return all matches concatenated
966 * mode = WILD_LONGEST: return longest matched part
967 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000968 * mode = WILD_APPLY: apply the item selected in the cmdline completion
969 * popup menu and close the menu.
970 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
971 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200972 *
973 * options = WILD_LIST_NOTFOUND: list entries without a match
974 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
975 * options = WILD_USE_NL: Use '\n' for WILD_ALL
976 * options = WILD_NO_BEEP: Don't beep for multiple matches
977 * options = WILD_ADD_SLASH: add a slash after directory names
978 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
979 * options = WILD_SILENT: don't print warning messages
980 * options = WILD_ESCAPE: put backslash before special chars
981 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200982 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200983 *
984 * The variables xp->xp_context and xp->xp_backslash must have been set!
985 */
986 char_u *
987ExpandOne(
988 expand_T *xp,
989 char_u *str,
990 char_u *orig, // allocated copy of original of expanded string
991 int options,
992 int mode)
993{
994 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200995 int orig_saved = FALSE;
996 int i;
997 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200998
999 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001000 if (mode == WILD_NEXT || mode == WILD_PREV
1001 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001002 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001003
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001004 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001005 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001006 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001007 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001008 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001009 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001010
Bram Moolenaar66b51422019-08-18 21:44:12 +02001011 // free old names
1012 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1013 {
1014 FreeWild(xp->xp_numfiles, xp->xp_files);
1015 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001016 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001017
1018 // The entries from xp_files may be used in the PUM, remove it.
1019 if (compl_match_array != NULL)
1020 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001021 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001022 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001023
1024 if (mode == WILD_FREE) // only release file name
1025 return NULL;
1026
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001027 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001028 {
zeertzjq28a23602023-09-29 19:58:35 +02001029 vim_free(xp->xp_orig);
1030 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001031 orig_saved = TRUE;
1032
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001033 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001034 }
1035
1036 // Find longest common part
1037 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1038 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001039 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001040 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001041 }
1042
Bram Moolenaar57e95172022-08-20 19:26:14 +01001043 // Concatenate all matching names. Unless interrupted, this can be slow
1044 // and the result probably won't be used.
1045 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 {
1047 len = 0;
1048 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001049 {
1050 if (i > 0)
1051 {
1052 if (xp->xp_prefix == XP_PREFIX_NO)
1053 len += 2; // prefix "no"
1054 else if (xp->xp_prefix == XP_PREFIX_INV)
1055 len += 3; // prefix "inv"
1056 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001057 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001058 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001059 ss = alloc(len);
1060 if (ss != NULL)
1061 {
1062 *ss = NUL;
1063 for (i = 0; i < xp->xp_numfiles; ++i)
1064 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001065 if (i > 0)
1066 {
1067 if (xp->xp_prefix == XP_PREFIX_NO)
1068 STRCAT(ss, "no");
1069 else if (xp->xp_prefix == XP_PREFIX_INV)
1070 STRCAT(ss, "inv");
1071 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001072 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001073
Bram Moolenaar66b51422019-08-18 21:44:12 +02001074 if (i != xp->xp_numfiles - 1)
1075 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1076 }
1077 }
1078 }
1079
1080 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1081 ExpandCleanup(xp);
1082
zeertzjq28a23602023-09-29 19:58:35 +02001083 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001084 if (!orig_saved)
1085 vim_free(orig);
1086
1087 return ss;
1088}
1089
1090/*
1091 * Prepare an expand structure for use.
1092 */
1093 void
1094ExpandInit(expand_T *xp)
1095{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001096 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001097 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001098 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001099 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001100}
1101
1102/*
1103 * Cleanup an expand structure after use.
1104 */
1105 void
1106ExpandCleanup(expand_T *xp)
1107{
1108 if (xp->xp_numfiles >= 0)
1109 {
1110 FreeWild(xp->xp_numfiles, xp->xp_files);
1111 xp->xp_numfiles = -1;
1112 }
zeertzjq28a23602023-09-29 19:58:35 +02001113 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001114}
1115
1116/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001117 * Display one line of completion matches. Multiple matches are displayed in
1118 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001119 * matches - list of completion match names
1120 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001121 * lines - number of output lines
1122 * linenr - line number of matches to display
1123 * maxlen - maximum number of characters in each line
1124 * showtail - display only the tail of the full path of a file name
1125 * dir_attr - highlight attribute to use for directory names
1126 */
1127 static void
1128showmatches_oneline(
1129 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001130 char_u **matches,
1131 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001132 int lines,
1133 int linenr,
1134 int maxlen,
1135 int showtail,
1136 int dir_attr)
1137{
1138 int i, j;
1139 int isdir;
1140 int lastlen;
1141 char_u *p;
1142
1143 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001144 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001145 {
1146 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1147 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001148 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1149 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001150 msg_advance(maxlen + 1);
1151 msg_puts((char *)p);
1152 msg_advance(maxlen + 3);
1153 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1154 break;
1155 }
1156 for (i = maxlen - lastlen; --i >= 0; )
1157 msg_putchar(' ');
1158 if (xp->xp_context == EXPAND_FILES
1159 || xp->xp_context == EXPAND_SHELLCMD
1160 || xp->xp_context == EXPAND_BUFFERS)
1161 {
1162 // highlight directories
1163 if (xp->xp_numfiles != -1)
1164 {
1165 char_u *halved_slash;
1166 char_u *exp_path;
1167 char_u *path;
1168
1169 // Expansion was done before and special characters
1170 // were escaped, need to halve backslashes. Also
1171 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001172 exp_path = expand_env_save_opt(matches[j], TRUE);
1173 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001174 halved_slash = backslash_halve_save(path);
1175 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001176 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001177 vim_free(exp_path);
1178 if (halved_slash != path)
1179 vim_free(halved_slash);
1180 }
1181 else
1182 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001183 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001184 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001185 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001186 else
1187 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001188 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001189 TRUE);
1190 p = NameBuff;
1191 }
1192 }
1193 else
1194 {
1195 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001196 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001197 }
1198 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1199 }
1200 if (msg_col > 0) // when not wrapped around
1201 {
1202 msg_clr_eos();
1203 msg_putchar('\n');
1204 }
1205 out_flush(); // show one line at a time
1206}
1207
1208/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001209 * Show all matches for completion on the command line.
1210 * Returns EXPAND_NOTHING when the character that triggered expansion should
1211 * be inserted like a normal character.
1212 */
1213 int
1214showmatches(expand_T *xp, int wildmenu UNUSED)
1215{
1216 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001217 int numMatches;
1218 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001219 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001220 int maxlen;
1221 int lines;
1222 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 int attr;
1224 int showtail;
1225
1226 if (xp->xp_numfiles == -1)
1227 {
1228 set_expand_context(xp);
1229 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001230 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001231 showtail = expand_showtail(xp);
1232 if (i != EXPAND_OK)
1233 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001234 }
1235 else
1236 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001237 numMatches = xp->xp_numfiles;
1238 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001239 showtail = cmd_showtail;
1240 }
1241
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001242 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001243 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001244 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001245
Bram Moolenaar66b51422019-08-18 21:44:12 +02001246 if (!wildmenu)
1247 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001248 msg_didany = FALSE; // lines_left will be set
1249 msg_start(); // prepare for paging
1250 msg_putchar('\n');
1251 out_flush();
1252 cmdline_row = msg_row;
1253 msg_didany = FALSE; // lines_left will be set again
1254 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001255 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001256
1257 if (got_int)
1258 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001259 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001260 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001261 else
1262 {
1263 // find the length of the longest file name
1264 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001265 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001266 {
1267 if (!showtail && (xp->xp_context == EXPAND_FILES
1268 || xp->xp_context == EXPAND_SHELLCMD
1269 || xp->xp_context == EXPAND_BUFFERS))
1270 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001271 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001272 j = vim_strsize(NameBuff);
1273 }
1274 else
zeertzjqc51a3762022-12-10 10:22:29 +00001275 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001276 if (j > maxlen)
1277 maxlen = j;
1278 }
1279
1280 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001281 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001282 else
1283 {
1284 // compute the number of columns and lines for the listing
1285 maxlen += 2; // two spaces between file names
1286 columns = ((int)Columns + 2) / maxlen;
1287 if (columns < 1)
1288 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001289 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001290 }
1291
1292 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1293
1294 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1295 {
1296 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1297 msg_clr_eos();
1298 msg_advance(maxlen - 3);
1299 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1300 }
1301
1302 // list the files line by line
1303 for (i = 0; i < lines; ++i)
1304 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001305 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001306 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001307 if (got_int)
1308 {
1309 got_int = FALSE;
1310 break;
1311 }
1312 }
1313
1314 // we redraw the command below the lines that we have just listed
1315 // This is a bit tricky, but it saves a lot of screen updating.
1316 cmdline_row = msg_row; // will put it back later
1317 }
1318
1319 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001320 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001321
1322 return EXPAND_OK;
1323}
1324
1325/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001326 * gettail() version for showmatches() and win_redr_status_matches():
1327 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001328 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001329 static char_u *
1330showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001331{
1332 char_u *p;
1333 char_u *t = s;
1334 int had_sep = FALSE;
1335
1336 for (p = s; *p != NUL; )
1337 {
1338 if (vim_ispathsep(*p)
1339#ifdef BACKSLASH_IN_FILENAME
1340 && !rem_backslash(p)
1341#endif
1342 )
1343 had_sep = TRUE;
1344 else if (had_sep)
1345 {
1346 t = p;
1347 had_sep = FALSE;
1348 }
1349 MB_PTR_ADV(p);
1350 }
1351 return t;
1352}
1353
1354/*
1355 * Return TRUE if we only need to show the tail of completion matches.
1356 * When not completing file names or there is a wildcard in the path FALSE is
1357 * returned.
1358 */
1359 static int
1360expand_showtail(expand_T *xp)
1361{
1362 char_u *s;
1363 char_u *end;
1364
1365 // When not completing file names a "/" may mean something different.
1366 if (xp->xp_context != EXPAND_FILES
1367 && xp->xp_context != EXPAND_SHELLCMD
1368 && xp->xp_context != EXPAND_DIRECTORIES)
1369 return FALSE;
1370
1371 end = gettail(xp->xp_pattern);
1372 if (end == xp->xp_pattern) // there is no path separator
1373 return FALSE;
1374
1375 for (s = xp->xp_pattern; s < end; s++)
1376 {
1377 // Skip escaped wildcards. Only when the backslash is not a path
1378 // separator, on DOS the '*' "path\*\file" must not be skipped.
1379 if (rem_backslash(s))
1380 ++s;
1381 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1382 return FALSE;
1383 }
1384 return TRUE;
1385}
1386
1387/*
1388 * Prepare a string for expansion.
1389 * When expanding file names: The string will be used with expand_wildcards().
1390 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1391 * When expanding other names: The string will be used with regcomp(). Copy
1392 * the name into allocated memory and prepend "^".
1393 */
1394 char_u *
1395addstar(
1396 char_u *fname,
1397 int len,
1398 int context) // EXPAND_FILES etc.
1399{
1400 char_u *retval;
1401 int i, j;
1402 int new_len;
1403 char_u *tail;
1404 int ends_in_star;
1405
1406 if (context != EXPAND_FILES
1407 && context != EXPAND_FILES_IN_PATH
1408 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001409 && context != EXPAND_DIRECTORIES
1410 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001411 {
1412 // Matching will be done internally (on something other than files).
1413 // So we convert the file-matching-type wildcards into our kind for
1414 // use with vim_regcomp(). First work out how long it will be:
1415
1416 // For help tags the translation is done in find_help_tags().
1417 // For a tag pattern starting with "/" no translation is needed.
1418 if (context == EXPAND_HELP
1419 || context == EXPAND_COLORS
1420 || context == EXPAND_COMPILER
1421 || context == EXPAND_OWNSYNTAX
1422 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001423 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001424 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001425 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001426 || ((context == EXPAND_TAGS_LISTFILES
1427 || context == EXPAND_TAGS)
1428 && fname[0] == '/'))
1429 retval = vim_strnsave(fname, len);
1430 else
1431 {
1432 new_len = len + 2; // +2 for '^' at start, NUL at end
1433 for (i = 0; i < len; i++)
1434 {
1435 if (fname[i] == '*' || fname[i] == '~')
1436 new_len++; // '*' needs to be replaced by ".*"
1437 // '~' needs to be replaced by "\~"
1438
1439 // Buffer names are like file names. "." should be literal
1440 if (context == EXPAND_BUFFERS && fname[i] == '.')
1441 new_len++; // "." becomes "\."
1442
1443 // Custom expansion takes care of special things, match
1444 // backslashes literally (perhaps also for other types?)
1445 if ((context == EXPAND_USER_DEFINED
1446 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1447 new_len++; // '\' becomes "\\"
1448 }
1449 retval = alloc(new_len);
1450 if (retval != NULL)
1451 {
1452 retval[0] = '^';
1453 j = 1;
1454 for (i = 0; i < len; i++, j++)
1455 {
1456 // Skip backslash. But why? At least keep it for custom
1457 // expansion.
1458 if (context != EXPAND_USER_DEFINED
1459 && context != EXPAND_USER_LIST
1460 && fname[i] == '\\'
1461 && ++i == len)
1462 break;
1463
1464 switch (fname[i])
1465 {
1466 case '*': retval[j++] = '.';
1467 break;
1468 case '~': retval[j++] = '\\';
1469 break;
1470 case '?': retval[j] = '.';
1471 continue;
1472 case '.': if (context == EXPAND_BUFFERS)
1473 retval[j++] = '\\';
1474 break;
1475 case '\\': if (context == EXPAND_USER_DEFINED
1476 || context == EXPAND_USER_LIST)
1477 retval[j++] = '\\';
1478 break;
1479 }
1480 retval[j] = fname[i];
1481 }
1482 retval[j] = NUL;
1483 }
1484 }
1485 }
1486 else
1487 {
1488 retval = alloc(len + 4);
1489 if (retval != NULL)
1490 {
1491 vim_strncpy(retval, fname, len);
1492
1493 // Don't add a star to *, ~, ~user, $var or `cmd`.
1494 // * would become **, which walks the whole tree.
1495 // ~ would be at the start of the file name, but not the tail.
1496 // $ could be anywhere in the tail.
1497 // ` could be anywhere in the file name.
1498 // When the name ends in '$' don't add a star, remove the '$'.
1499 tail = gettail(retval);
1500 ends_in_star = (len > 0 && retval[len - 1] == '*');
1501#ifndef BACKSLASH_IN_FILENAME
1502 for (i = len - 2; i >= 0; --i)
1503 {
1504 if (retval[i] != '\\')
1505 break;
1506 ends_in_star = !ends_in_star;
1507 }
1508#endif
1509 if ((*retval != '~' || tail != retval)
1510 && !ends_in_star
1511 && vim_strchr(tail, '$') == NULL
1512 && vim_strchr(retval, '`') == NULL)
1513 retval[len++] = '*';
1514 else if (len > 0 && retval[len - 1] == '$')
1515 --len;
1516 retval[len] = NUL;
1517 }
1518 }
1519 return retval;
1520}
1521
1522/*
1523 * Must parse the command line so far to work out what context we are in.
1524 * Completion can then be done based on that context.
1525 * This routine sets the variables:
1526 * xp->xp_pattern The start of the pattern to be expanded within
1527 * the command line (ends at the cursor).
1528 * xp->xp_context The type of thing to expand. Will be one of:
1529 *
1530 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1531 * the command line, like an unknown command. Caller
1532 * should beep.
1533 * EXPAND_NOTHING Unrecognised context for completion, use char like
1534 * a normal char, rather than for completion. eg
1535 * :s/^I/
1536 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1537 * it.
1538 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1539 * EXPAND_FILES After command with EX_XFILE set, or after setting
1540 * with P_EXPAND set. eg :e ^I, :w>>^I
1541 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001542 * when we know only directories are of interest.
1543 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001544 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1545 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1546 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1547 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1548 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1549 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1550 * EXPAND_EVENTS Complete event names
1551 * EXPAND_SYNTAX Complete :syntax command arguments
1552 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1553 * EXPAND_AUGROUP Complete autocommand group names
1554 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1555 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1556 * eg :unmap a^I , :cunab x^I
1557 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1558 * eg :call sub^I
1559 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1560 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1561 * names in expressions, eg :while s^I
1562 * EXPAND_ENV_VARS Complete environment variable names
1563 * EXPAND_USER Complete user names
1564 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001565 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001566set_expand_context(expand_T *xp)
1567{
1568 cmdline_info_T *ccline = get_cmdline_info();
1569
1570 // only expansion for ':', '>' and '=' command-lines
1571 if (ccline->cmdfirstc != ':'
1572#ifdef FEAT_EVAL
1573 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1574 && !ccline->input_fn
1575#endif
1576 )
1577 {
1578 xp->xp_context = EXPAND_NOTHING;
1579 return;
1580 }
1581 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1582}
1583
Bram Moolenaard0190392019-08-23 21:17:35 +02001584/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001585 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1586 * For user defined commands, the completion context is set in 'xp' and the
1587 * completion flags in 'complp'.
1588 *
1589 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001590 */
1591 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001592set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001593{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001594 char_u *p = NULL;
1595 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001596 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001597
1598 // Isolate the command and search for it in the command table.
1599 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001600 // - the 'k' command can directly be followed by any character, but do
1601 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1602 // find matches anywhere in the command name, do this only for command
1603 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001604 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001605 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001606 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001607 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001608 p = cmd + 1;
1609 }
1610 else
1611 {
1612 p = cmd;
1613 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1614 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001615 // A user command may contain digits.
1616 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1617 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001618 while (ASCII_ISALNUM(*p) || *p == '*')
1619 ++p;
1620 // for python 3.x: ":py3*" commands completion
1621 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1622 {
1623 ++p;
1624 while (ASCII_ISALPHA(*p) || *p == '*')
1625 ++p;
1626 }
1627 // check for non-alpha command
1628 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1629 ++p;
1630 len = (int)(p - cmd);
1631
1632 if (len == 0)
1633 {
1634 xp->xp_context = EXPAND_UNSUCCESSFUL;
1635 return NULL;
1636 }
1637
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001638 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001639
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001640 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001641 // Also when doing fuzzy expansion for non-shell commands, support
1642 // alphanumeric characters.
1643 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1644 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001645 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1646 ++p;
1647 }
1648
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001649 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001650 // character, complete the command name.
1651 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1652 return NULL;
1653
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001654 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001655 {
1656 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1657 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001658 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001659 p = cmd + 1;
1660 }
1661 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1662 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001663 eap->cmd = cmd;
1664 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001665 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001667 }
1668 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001670 {
1671 // Not still touching the command and it was an illegal one
1672 xp->xp_context = EXPAND_UNSUCCESSFUL;
1673 return NULL;
1674 }
1675
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001676 return p;
1677}
Bram Moolenaard0190392019-08-23 21:17:35 +02001678
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001679/*
1680 * Set the completion context for a command argument with wild card characters.
1681 */
1682 static void
1683set_context_for_wildcard_arg(
1684 exarg_T *eap,
1685 char_u *arg,
1686 int usefilter,
1687 expand_T *xp,
1688 int *complp)
1689{
1690 char_u *p;
1691 int c;
1692 int in_quote = FALSE;
1693 char_u *bow = NULL; // Beginning of word
1694 int len = 0;
1695
1696 // Allow spaces within back-quotes to count as part of the argument
1697 // being expanded.
1698 xp->xp_pattern = skipwhite(arg);
1699 p = xp->xp_pattern;
1700 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001701 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001702 if (has_mbyte)
1703 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001705 c = *p;
1706 if (c == '\\' && p[1] != NUL)
1707 ++p;
1708 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001709 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001710 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001711 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001712 xp->xp_pattern = p;
1713 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001714 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001715 in_quote = !in_quote;
1716 }
1717 // An argument can contain just about everything, except
1718 // characters that end the command and white space.
1719 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001720#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001721 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001722#endif
1723 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001724 {
1725 len = 0; // avoid getting stuck when space is in 'isfname'
1726 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001727 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001728 if (has_mbyte)
1729 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001730 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001731 c = *p;
1732 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001733 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001734 if (has_mbyte)
1735 len = (*mb_ptr2len)(p);
1736 else
1737 len = 1;
1738 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001739 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001740 if (in_quote)
1741 bow = p;
1742 else
1743 xp->xp_pattern = p;
1744 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001745 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001746 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001747 }
1748
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001749 // If we are still inside the quotes, and we passed a space, just
1750 // expand from there.
1751 if (bow != NULL && in_quote)
1752 xp->xp_pattern = bow;
1753 xp->xp_context = EXPAND_FILES;
1754
1755 // For a shell command more chars need to be escaped.
1756 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1757 {
1758#ifndef BACKSLASH_IN_FILENAME
1759 xp->xp_shell = TRUE;
1760#endif
1761 // When still after the command name expand executables.
1762 if (xp->xp_pattern == skipwhite(arg))
1763 xp->xp_context = EXPAND_SHELLCMD;
1764 }
1765
1766 // Check for environment variable.
1767 if (*xp->xp_pattern == '$')
1768 {
1769 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1770 if (!vim_isIDc(*p))
1771 break;
1772 if (*p == NUL)
1773 {
1774 xp->xp_context = EXPAND_ENV_VARS;
1775 ++xp->xp_pattern;
1776 // Avoid that the assignment uses EXPAND_FILES again.
1777 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1778 *complp = EXPAND_ENV_VARS;
1779 }
1780 }
1781 // Check for user names.
1782 if (*xp->xp_pattern == '~')
1783 {
1784 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1785 ;
1786 // Complete ~user only if it partially matches a user name.
1787 // A full match ~user<Tab> will be replaced by user's home
1788 // directory i.e. something like ~user<Tab> -> /home/user/
1789 if (*p == NUL && p > xp->xp_pattern + 1
1790 && match_user(xp->xp_pattern + 1) >= 1)
1791 {
1792 xp->xp_context = EXPAND_USER;
1793 ++xp->xp_pattern;
1794 }
1795 }
1796}
1797
1798/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001799 * Set the completion context for the "++opt=arg" argument. Always returns
1800 * NULL.
1801 */
1802 static char_u *
1803set_context_in_argopt(expand_T *xp, char_u *arg)
1804{
1805 char_u *p;
1806
1807 p = vim_strchr(arg, '=');
1808 if (p == NULL)
1809 xp->xp_pattern = arg;
1810 else
1811 xp->xp_pattern = p + 1;
1812
1813 xp->xp_context = EXPAND_ARGOPT;
1814 return NULL;
1815}
1816
1817#ifdef FEAT_TERMINAL
1818/*
1819 * Set the completion context for :terminal's [options]. Always returns NULL.
1820 */
1821 static char_u *
1822set_context_in_terminalopt(expand_T *xp, char_u *arg)
1823{
1824 char_u *p;
1825
1826 p = vim_strchr(arg, '=');
1827 if (p == NULL)
1828 xp->xp_pattern = arg;
1829 else
1830 xp->xp_pattern = p + 1;
1831
1832 xp->xp_context = EXPAND_TERMINALOPT;
1833 return NULL;
1834}
1835#endif
1836
1837/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001838 * Set the completion context for the :filter command. Returns a pointer to the
1839 * next command after the :filter command.
1840 */
1841 static char_u *
1842set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1843{
1844 if (*arg != NUL)
1845 arg = skip_vimgrep_pat(arg, NULL, NULL);
1846 if (arg == NULL || *arg == NUL)
1847 {
1848 xp->xp_context = EXPAND_NOTHING;
1849 return NULL;
1850 }
1851 return skipwhite(arg);
1852}
1853
1854#ifdef FEAT_SEARCH_EXTRA
1855/*
1856 * Set the completion context for the :match command. Returns a pointer to the
1857 * next command after the :match command.
1858 */
1859 static char_u *
1860set_context_in_match_cmd(expand_T *xp, char_u *arg)
1861{
1862 if (*arg == NUL || !ends_excmd(*arg))
1863 {
1864 // also complete "None"
1865 set_context_in_echohl_cmd(xp, arg);
1866 arg = skipwhite(skiptowhite(arg));
1867 if (*arg != NUL)
1868 {
1869 xp->xp_context = EXPAND_NOTHING;
1870 arg = skip_regexp(arg + 1, *arg, magic_isset());
1871 }
1872 }
1873 return find_nextcmd(arg);
1874}
1875#endif
1876
1877/*
1878 * Returns a pointer to the next command after a :global or a :v command.
1879 * Returns NULL if there is no next command.
1880 */
1881 static char_u *
1882find_cmd_after_global_cmd(char_u *arg)
1883{
1884 int delim;
1885
1886 delim = *arg; // get the delimiter
1887 if (delim)
1888 ++arg; // skip delimiter if there is one
1889
1890 while (arg[0] != NUL && arg[0] != delim)
1891 {
1892 if (arg[0] == '\\' && arg[1] != NUL)
1893 ++arg;
1894 ++arg;
1895 }
1896 if (arg[0] != NUL)
1897 return arg + 1;
1898
1899 return NULL;
1900}
1901
1902/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001903 * Returns a pointer to the next command after a :substitute or a :& command.
1904 * Returns NULL if there is no next command.
1905 */
1906 static char_u *
1907find_cmd_after_substitute_cmd(char_u *arg)
1908{
1909 int delim;
1910
1911 delim = *arg;
1912 if (delim)
1913 {
1914 // skip "from" part
1915 ++arg;
1916 arg = skip_regexp(arg, delim, magic_isset());
1917
1918 if (arg[0] != NUL && arg[0] == delim)
1919 {
1920 // skip "to" part
1921 ++arg;
1922 while (arg[0] != NUL && arg[0] != delim)
1923 {
1924 if (arg[0] == '\\' && arg[1] != NUL)
1925 ++arg;
1926 ++arg;
1927 }
1928 if (arg[0] != NUL) // skip delimiter
1929 ++arg;
1930 }
1931 }
1932 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1933 ++arg;
1934 if (arg[0] != NUL)
1935 return arg;
1936
1937 return NULL;
1938}
1939
1940/*
1941 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1942 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1943 * Returns NULL if there is no next command.
1944 */
1945 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001946find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001947{
1948 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001949 if (*arg != '/')
1950 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001951
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001952 // Match regexp, not just whole words
1953 for (++arg; *arg && *arg != '/'; arg++)
1954 if (*arg == '\\' && arg[1] != NUL)
1955 arg++;
1956 if (*arg)
1957 {
1958 arg = skipwhite(arg + 1);
1959
1960 // Check for trailing illegal characters
1961 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1962 xp->xp_context = EXPAND_NOTHING;
1963 else
1964 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001965 }
1966
1967 return NULL;
1968}
1969
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001970#ifdef FEAT_EVAL
1971/*
1972 * Set the completion context for the :unlet command. Always returns NULL.
1973 */
1974 static char_u *
1975set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1976{
1977 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1978 arg = xp->xp_pattern + 1;
1979
1980 xp->xp_context = EXPAND_USER_VARS;
1981 xp->xp_pattern = arg;
1982
1983 if (*xp->xp_pattern == '$')
1984 {
1985 xp->xp_context = EXPAND_ENV_VARS;
1986 ++xp->xp_pattern;
1987 }
1988
1989 return NULL;
1990}
1991#endif
1992
1993#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1994/*
1995 * Set the completion context for the :language command. Always returns NULL.
1996 */
1997 static char_u *
1998set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1999{
2000 char_u *p;
2001
2002 p = skiptowhite(arg);
2003 if (*p == NUL)
2004 {
2005 xp->xp_context = EXPAND_LANGUAGE;
2006 xp->xp_pattern = arg;
2007 }
2008 else
2009 {
2010 if ( STRNCMP(arg, "messages", p - arg) == 0
2011 || STRNCMP(arg, "ctype", p - arg) == 0
2012 || STRNCMP(arg, "time", p - arg) == 0
2013 || STRNCMP(arg, "collate", p - arg) == 0)
2014 {
2015 xp->xp_context = EXPAND_LOCALES;
2016 xp->xp_pattern = skipwhite(p);
2017 }
2018 else
2019 xp->xp_context = EXPAND_NOTHING;
2020 }
2021
2022 return NULL;
2023}
2024#endif
2025
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002026#ifdef FEAT_EVAL
2027static enum
2028{
2029 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002030 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2031 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002032} breakpt_expand_what;
2033
2034/*
2035 * Set the completion context for the :breakadd command. Always returns NULL.
2036 */
2037 static char_u *
2038set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2039{
2040 char_u *p;
2041 char_u *subcmd_start;
2042
2043 xp->xp_context = EXPAND_BREAKPOINT;
2044 xp->xp_pattern = arg;
2045
2046 if (cmdidx == CMD_breakadd)
2047 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002048 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002049 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002050 else
2051 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002052
2053 p = skipwhite(arg);
2054 if (*p == NUL)
2055 return NULL;
2056 subcmd_start = p;
2057
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002058 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002059 {
2060 // :breakadd file [lnum] <filename>
2061 // :breakadd func [lnum] <funcname>
2062 p += 4;
2063 p = skipwhite(p);
2064
2065 // skip line number (if specified)
2066 if (VIM_ISDIGIT(*p))
2067 {
2068 p = skipdigits(p);
2069 if (*p != ' ')
2070 {
2071 xp->xp_context = EXPAND_NOTHING;
2072 return NULL;
2073 }
2074 p = skipwhite(p);
2075 }
2076 if (STRNCMP("file", subcmd_start, 4) == 0)
2077 xp->xp_context = EXPAND_FILES;
2078 else
2079 xp->xp_context = EXPAND_USER_FUNC;
2080 xp->xp_pattern = p;
2081 }
2082 else if (STRNCMP("expr ", p, 5) == 0)
2083 {
2084 // :breakadd expr <expression>
2085 xp->xp_context = EXPAND_EXPRESSION;
2086 xp->xp_pattern = skipwhite(p + 5);
2087 }
2088
2089 return NULL;
2090}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002091
2092 static char_u *
2093set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2094{
2095 char_u *p;
2096
2097 xp->xp_context = EXPAND_NOTHING;
2098 xp->xp_pattern = NULL;
2099
2100 p = skipwhite(arg);
2101 if (VIM_ISDIGIT(*p))
2102 return NULL;
2103
2104 xp->xp_context = EXPAND_SCRIPTNAMES;
2105 xp->xp_pattern = p;
2106
2107 return NULL;
2108}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002109#endif
2110
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002111/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002112 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2113 * The argument to the command is 'arg' and the argument flags is 'argt'.
2114 * For user-defined commands and for environment variables, 'compl' has the
2115 * completion type.
2116 * Returns a pointer to the next command. Returns NULL if there is no next
2117 * command.
2118 */
2119 static char_u *
2120set_context_by_cmdname(
2121 char_u *cmd,
2122 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002123 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002124 char_u *arg,
2125 long argt,
2126 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002127 int forceit)
2128{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002129 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002130 {
2131 case CMD_find:
2132 case CMD_sfind:
2133 case CMD_tabfind:
2134 if (xp->xp_context == EXPAND_FILES)
2135 xp->xp_context = EXPAND_FILES_IN_PATH;
2136 break;
2137 case CMD_cd:
2138 case CMD_chdir:
2139 case CMD_tcd:
2140 case CMD_tchdir:
2141 case CMD_lcd:
2142 case CMD_lchdir:
2143 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002144 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002145 break;
2146 case CMD_help:
2147 xp->xp_context = EXPAND_HELP;
2148 xp->xp_pattern = arg;
2149 break;
2150
2151 // Command modifiers: return the argument.
2152 // Also for commands with an argument that is a command.
2153 case CMD_aboveleft:
2154 case CMD_argdo:
2155 case CMD_belowright:
2156 case CMD_botright:
2157 case CMD_browse:
2158 case CMD_bufdo:
2159 case CMD_cdo:
2160 case CMD_cfdo:
2161 case CMD_confirm:
2162 case CMD_debug:
2163 case CMD_folddoclosed:
2164 case CMD_folddoopen:
2165 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002166 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002167 case CMD_keepalt:
2168 case CMD_keepjumps:
2169 case CMD_keepmarks:
2170 case CMD_keeppatterns:
2171 case CMD_ldo:
2172 case CMD_leftabove:
2173 case CMD_lfdo:
2174 case CMD_lockmarks:
2175 case CMD_noautocmd:
2176 case CMD_noswapfile:
2177 case CMD_rightbelow:
2178 case CMD_sandbox:
2179 case CMD_silent:
2180 case CMD_tab:
2181 case CMD_tabdo:
2182 case CMD_topleft:
2183 case CMD_verbose:
2184 case CMD_vertical:
2185 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002186 case CMD_vim9cmd:
2187 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002188 return arg;
2189
2190 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002191 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002192
2193#ifdef FEAT_SEARCH_EXTRA
2194 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002195 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002196#endif
2197
2198 // All completion for the +cmdline_compl feature goes here.
2199
2200 case CMD_command:
2201 return set_context_in_user_cmd(xp, arg);
2202
2203 case CMD_delcommand:
2204 xp->xp_context = EXPAND_USER_COMMANDS;
2205 xp->xp_pattern = arg;
2206 break;
2207
2208 case CMD_global:
2209 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002210 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002211 case CMD_and:
2212 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002213 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002214 case CMD_isearch:
2215 case CMD_dsearch:
2216 case CMD_ilist:
2217 case CMD_dlist:
2218 case CMD_ijump:
2219 case CMD_psearch:
2220 case CMD_djump:
2221 case CMD_isplit:
2222 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002223 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002224 case CMD_autocmd:
2225 return set_context_in_autocmd(xp, arg, FALSE);
2226 case CMD_doautocmd:
2227 case CMD_doautoall:
2228 return set_context_in_autocmd(xp, arg, TRUE);
2229 case CMD_set:
2230 set_context_in_set_cmd(xp, arg, 0);
2231 break;
2232 case CMD_setglobal:
2233 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2234 break;
2235 case CMD_setlocal:
2236 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2237 break;
2238 case CMD_tag:
2239 case CMD_stag:
2240 case CMD_ptag:
2241 case CMD_ltag:
2242 case CMD_tselect:
2243 case CMD_stselect:
2244 case CMD_ptselect:
2245 case CMD_tjump:
2246 case CMD_stjump:
2247 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002248 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002249 xp->xp_context = EXPAND_TAGS_LISTFILES;
2250 else
2251 xp->xp_context = EXPAND_TAGS;
2252 xp->xp_pattern = arg;
2253 break;
2254 case CMD_augroup:
2255 xp->xp_context = EXPAND_AUGROUP;
2256 xp->xp_pattern = arg;
2257 break;
2258#ifdef FEAT_SYN_HL
2259 case CMD_syntax:
2260 set_context_in_syntax_cmd(xp, arg);
2261 break;
2262#endif
2263#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002264 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002265 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002266 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002267 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002268 case CMD_if:
2269 case CMD_elseif:
2270 case CMD_while:
2271 case CMD_for:
2272 case CMD_echo:
2273 case CMD_echon:
2274 case CMD_execute:
2275 case CMD_echomsg:
2276 case CMD_echoerr:
2277 case CMD_call:
2278 case CMD_return:
2279 case CMD_cexpr:
2280 case CMD_caddexpr:
2281 case CMD_cgetexpr:
2282 case CMD_lexpr:
2283 case CMD_laddexpr:
2284 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002285 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002286 break;
2287
2288 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002289 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002290 case CMD_function:
2291 case CMD_delfunction:
2292 xp->xp_context = EXPAND_USER_FUNC;
2293 xp->xp_pattern = arg;
2294 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002295 case CMD_disassemble:
2296 set_context_in_disassemble_cmd(xp, arg);
2297 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002298
2299 case CMD_echohl:
2300 set_context_in_echohl_cmd(xp, arg);
2301 break;
2302#endif
2303 case CMD_highlight:
2304 set_context_in_highlight_cmd(xp, arg);
2305 break;
2306#ifdef FEAT_CSCOPE
2307 case CMD_cscope:
2308 case CMD_lcscope:
2309 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002310 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002311 break;
2312#endif
2313#ifdef FEAT_SIGNS
2314 case CMD_sign:
2315 set_context_in_sign_cmd(xp, arg);
2316 break;
2317#endif
2318 case CMD_bdelete:
2319 case CMD_bwipeout:
2320 case CMD_bunload:
2321 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2322 arg = xp->xp_pattern + 1;
2323 // FALLTHROUGH
2324 case CMD_buffer:
2325 case CMD_sbuffer:
2326 case CMD_checktime:
2327 xp->xp_context = EXPAND_BUFFERS;
2328 xp->xp_pattern = arg;
2329 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002330#ifdef FEAT_DIFF
2331 case CMD_diffget:
2332 case CMD_diffput:
2333 // If current buffer is in diff mode, complete buffer names
2334 // which are in diff mode, and different than current buffer.
2335 xp->xp_context = EXPAND_DIFF_BUFFERS;
2336 xp->xp_pattern = arg;
2337 break;
2338#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002339 case CMD_USER:
2340 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002341 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2342 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002343
2344 case CMD_map: case CMD_noremap:
2345 case CMD_nmap: case CMD_nnoremap:
2346 case CMD_vmap: case CMD_vnoremap:
2347 case CMD_omap: case CMD_onoremap:
2348 case CMD_imap: case CMD_inoremap:
2349 case CMD_cmap: case CMD_cnoremap:
2350 case CMD_lmap: case CMD_lnoremap:
2351 case CMD_smap: case CMD_snoremap:
2352 case CMD_tmap: case CMD_tnoremap:
2353 case CMD_xmap: case CMD_xnoremap:
2354 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002355 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002356 case CMD_unmap:
2357 case CMD_nunmap:
2358 case CMD_vunmap:
2359 case CMD_ounmap:
2360 case CMD_iunmap:
2361 case CMD_cunmap:
2362 case CMD_lunmap:
2363 case CMD_sunmap:
2364 case CMD_tunmap:
2365 case CMD_xunmap:
2366 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002367 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002368 case CMD_mapclear:
2369 case CMD_nmapclear:
2370 case CMD_vmapclear:
2371 case CMD_omapclear:
2372 case CMD_imapclear:
2373 case CMD_cmapclear:
2374 case CMD_lmapclear:
2375 case CMD_smapclear:
2376 case CMD_tmapclear:
2377 case CMD_xmapclear:
2378 xp->xp_context = EXPAND_MAPCLEAR;
2379 xp->xp_pattern = arg;
2380 break;
2381
2382 case CMD_abbreviate: case CMD_noreabbrev:
2383 case CMD_cabbrev: case CMD_cnoreabbrev:
2384 case CMD_iabbrev: case CMD_inoreabbrev:
2385 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002386 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002387 case CMD_unabbreviate:
2388 case CMD_cunabbrev:
2389 case CMD_iunabbrev:
2390 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002391 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002392#ifdef FEAT_MENU
2393 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2394 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2395 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2396 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2397 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2398 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2399 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2400 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2401 case CMD_tmenu: case CMD_tunmenu:
2402 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2403 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2404#endif
2405
2406 case CMD_colorscheme:
2407 xp->xp_context = EXPAND_COLORS;
2408 xp->xp_pattern = arg;
2409 break;
2410
2411 case CMD_compiler:
2412 xp->xp_context = EXPAND_COMPILER;
2413 xp->xp_pattern = arg;
2414 break;
2415
2416 case CMD_ownsyntax:
2417 xp->xp_context = EXPAND_OWNSYNTAX;
2418 xp->xp_pattern = arg;
2419 break;
2420
2421 case CMD_setfiletype:
2422 xp->xp_context = EXPAND_FILETYPE;
2423 xp->xp_pattern = arg;
2424 break;
2425
2426 case CMD_packadd:
2427 xp->xp_context = EXPAND_PACKADD;
2428 xp->xp_pattern = arg;
2429 break;
2430
zeertzjqb0d45ec2023-01-25 15:04:22 +00002431 case CMD_runtime:
2432 set_context_in_runtime_cmd(xp, arg);
2433 break;
2434
Bram Moolenaard0190392019-08-23 21:17:35 +02002435#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2436 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002437 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002438#endif
2439#if defined(FEAT_PROFILE)
2440 case CMD_profile:
2441 set_context_in_profile_cmd(xp, arg);
2442 break;
2443#endif
2444 case CMD_behave:
2445 xp->xp_context = EXPAND_BEHAVE;
2446 xp->xp_pattern = arg;
2447 break;
2448
2449 case CMD_messages:
2450 xp->xp_context = EXPAND_MESSAGES;
2451 xp->xp_pattern = arg;
2452 break;
2453
2454 case CMD_history:
2455 xp->xp_context = EXPAND_HISTORY;
2456 xp->xp_pattern = arg;
2457 break;
2458#if defined(FEAT_PROFILE)
2459 case CMD_syntime:
2460 xp->xp_context = EXPAND_SYNTIME;
2461 xp->xp_pattern = arg;
2462 break;
2463#endif
2464
2465 case CMD_argdelete:
2466 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2467 arg = xp->xp_pattern + 1;
2468 xp->xp_context = EXPAND_ARGLIST;
2469 xp->xp_pattern = arg;
2470 break;
2471
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002472#ifdef FEAT_EVAL
2473 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002474 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002475 case CMD_breakdel:
2476 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002477
2478 case CMD_scriptnames:
2479 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002480#endif
2481
Bram Moolenaard0190392019-08-23 21:17:35 +02002482 default:
2483 break;
2484 }
2485 return NULL;
2486}
2487
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002488/*
2489 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2490 * we don't need/want deleted. Maybe this could be done better if we didn't
2491 * repeat all this stuff. The only problem is that they may not stay
2492 * perfectly compatible with each other, but then the command line syntax
2493 * probably won't change that much -- webb.
2494 */
2495 static char_u *
2496set_one_cmd_context(
2497 expand_T *xp,
2498 char_u *buff) // buffer for command string
2499{
2500 char_u *p;
2501 char_u *cmd, *arg;
2502 int len = 0;
2503 exarg_T ea;
2504 int compl = EXPAND_NOTHING;
2505 int forceit = FALSE;
2506 int usefilter = FALSE; // filter instead of file name
2507
2508 ExpandInit(xp);
2509 xp->xp_pattern = buff;
2510 xp->xp_line = buff;
2511 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2512 ea.argt = 0;
2513
2514 // 1. skip comment lines and leading space, colons or bars
2515 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2516 ;
2517 xp->xp_pattern = cmd;
2518
2519 if (*cmd == NUL)
2520 return NULL;
2521 if (*cmd == '"') // ignore comment lines
2522 {
2523 xp->xp_context = EXPAND_NOTHING;
2524 return NULL;
2525 }
2526
2527 // 3. Skip over the range to find the command.
2528 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2529 xp->xp_pattern = cmd;
2530 if (*cmd == NUL)
2531 return NULL;
2532 if (*cmd == '"')
2533 {
2534 xp->xp_context = EXPAND_NOTHING;
2535 return NULL;
2536 }
2537
2538 if (*cmd == '|' || *cmd == '\n')
2539 return cmd + 1; // There's another command
2540
2541 // Get the command index.
2542 p = set_cmd_index(cmd, &ea, xp, &compl);
2543 if (p == NULL)
2544 return NULL;
2545
2546 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2547
2548 if (*p == '!') // forced commands
2549 {
2550 forceit = TRUE;
2551 ++p;
2552 }
2553
2554 // 6. parse arguments
2555 if (!IS_USER_CMDIDX(ea.cmdidx))
2556 ea.argt = excmd_get_argt(ea.cmdidx);
2557
2558 arg = skipwhite(p);
2559
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002560 // Does command allow "++argopt" argument?
2561 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002562 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002563 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2564 {
2565 p = arg + 2;
2566 while (*p && !vim_isspace(*p))
2567 MB_PTR_ADV(p);
2568
2569 // Still touching the command after "++"?
2570 if (*p == NUL)
2571 {
2572 if (ea.argt & EX_ARGOPT)
2573 return set_context_in_argopt(xp, arg + 2);
2574#ifdef FEAT_TERMINAL
2575 if (ea.cmdidx == CMD_terminal)
2576 return set_context_in_terminalopt(xp, arg + 2);
2577#endif
2578 }
2579
2580 arg = skipwhite(p);
2581 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002582 }
2583
2584 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2585 {
2586 if (*arg == '>') // append
2587 {
2588 if (*++arg == '>')
2589 ++arg;
2590 arg = skipwhite(arg);
2591 }
2592 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2593 {
2594 ++arg;
2595 usefilter = TRUE;
2596 }
2597 }
2598
2599 if (ea.cmdidx == CMD_read)
2600 {
2601 usefilter = forceit; // :r! filter if forced
2602 if (*arg == '!') // :r !filter
2603 {
2604 ++arg;
2605 usefilter = TRUE;
2606 }
2607 }
2608
2609 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2610 {
2611 while (*arg == *cmd) // allow any number of '>' or '<'
2612 ++arg;
2613 arg = skipwhite(arg);
2614 }
2615
2616 // Does command allow "+command"?
2617 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2618 {
2619 // Check if we're in the +command
2620 p = arg + 1;
2621 arg = skip_cmd_arg(arg, FALSE);
2622
2623 // Still touching the command after '+'?
2624 if (*arg == NUL)
2625 return p;
2626
2627 // Skip space(s) after +command to get to the real argument
2628 arg = skipwhite(arg);
2629 }
2630
2631
2632 // Check for '|' to separate commands and '"' to start comments.
2633 // Don't do this for ":read !cmd" and ":write !cmd".
2634 if ((ea.argt & EX_TRLBAR) && !usefilter)
2635 {
2636 p = arg;
2637 // ":redir @" is not the start of a comment
2638 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2639 p += 2;
2640 while (*p)
2641 {
2642 if (*p == Ctrl_V)
2643 {
2644 if (p[1] != NUL)
2645 ++p;
2646 }
2647 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2648 || *p == '|' || *p == '\n')
2649 {
2650 if (*(p - 1) != '\\')
2651 {
2652 if (*p == '|' || *p == '\n')
2653 return p + 1;
2654 return NULL; // It's a comment
2655 }
2656 }
2657 MB_PTR_ADV(p);
2658 }
2659 }
2660
2661 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2662 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2663 // no arguments allowed but there is something
2664 return NULL;
2665
2666 // Find start of last argument (argument just before cursor):
2667 p = buff;
2668 xp->xp_pattern = p;
2669 len = (int)STRLEN(buff);
2670 while (*p && p < buff + len)
2671 {
2672 if (*p == ' ' || *p == TAB)
2673 {
2674 // argument starts after a space
2675 xp->xp_pattern = ++p;
2676 }
2677 else
2678 {
2679 if (*p == '\\' && *(p + 1) != NUL)
2680 ++p; // skip over escaped character
2681 MB_PTR_ADV(p);
2682 }
2683 }
2684
2685 if (ea.argt & EX_XFILE)
2686 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2687
2688 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002689 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002690 forceit);
2691}
2692
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002693/*
2694 * Set the completion context in 'xp' for command 'str'
2695 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002696 void
2697set_cmd_context(
2698 expand_T *xp,
2699 char_u *str, // start of command line
2700 int len, // length of command line (excl. NUL)
2701 int col, // position of cursor
2702 int use_ccline UNUSED) // use ccline for info
2703{
2704#ifdef FEAT_EVAL
2705 cmdline_info_T *ccline = get_cmdline_info();
2706#endif
2707 int old_char = NUL;
2708 char_u *nextcomm;
2709
2710 // Avoid a UMR warning from Purify, only save the character if it has been
2711 // written before.
2712 if (col < len)
2713 old_char = str[col];
2714 str[col] = NUL;
2715 nextcomm = str;
2716
2717#ifdef FEAT_EVAL
2718 if (use_ccline && ccline->cmdfirstc == '=')
2719 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002720 // pass CMD_SIZE because there is no real command
2721 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002722 }
2723 else if (use_ccline && ccline->input_fn)
2724 {
2725 xp->xp_context = ccline->xp_context;
2726 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002727 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002728 }
2729 else
2730#endif
2731 while (nextcomm != NULL)
2732 nextcomm = set_one_cmd_context(xp, nextcomm);
2733
2734 // Store the string here so that call_user_expand_func() can get to them
2735 // easily.
2736 xp->xp_line = str;
2737 xp->xp_col = col;
2738
2739 str[col] = old_char;
2740}
2741
2742/*
2743 * Expand the command line "str" from context "xp".
2744 * "xp" must have been set by set_cmd_context().
2745 * xp->xp_pattern points into "str", to where the text that is to be expanded
2746 * starts.
2747 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2748 * cursor.
2749 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2750 * key that triggered expansion literally.
2751 * Returns EXPAND_OK otherwise.
2752 */
2753 int
2754expand_cmdline(
2755 expand_T *xp,
2756 char_u *str, // start of command line
2757 int col, // position of cursor
2758 int *matchcount, // return: nr of matches
2759 char_u ***matches) // return: array of pointers to matches
2760{
2761 char_u *file_str = NULL;
2762 int options = WILD_ADD_SLASH|WILD_SILENT;
2763
2764 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2765 {
2766 beep_flush();
2767 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2768 }
2769 if (xp->xp_context == EXPAND_NOTHING)
2770 {
2771 // Caller can use the character as a normal char instead
2772 return EXPAND_NOTHING;
2773 }
2774
2775 // add star to file name, or convert to regexp if not exp. files.
2776 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002777 if (cmdline_fuzzy_completion_supported(xp))
2778 // If fuzzy matching, don't modify the search string
2779 file_str = vim_strsave(xp->xp_pattern);
2780 else
2781 {
2782 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2783 if (file_str == NULL)
2784 return EXPAND_UNSUCCESSFUL;
2785 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002786
2787 if (p_wic)
2788 options += WILD_ICASE;
2789
2790 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002791 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002792 {
2793 *matchcount = 0;
2794 *matches = NULL;
2795 }
2796 vim_free(file_str);
2797
2798 return EXPAND_OK;
2799}
2800
Bram Moolenaar66b51422019-08-18 21:44:12 +02002801/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002802 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002803 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002804 */
2805 static int
2806expand_files_and_dirs(
2807 expand_T *xp,
2808 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002809 char_u ***matches,
2810 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002811 int flags,
2812 int options)
2813{
2814 int free_pat = FALSE;
2815 int i;
2816 int ret;
2817
2818 // for ":set path=" and ":set tags=" halve backslashes for escaped
2819 // space
2820 if (xp->xp_backslash != XP_BS_NONE)
2821 {
2822 free_pat = TRUE;
2823 pat = vim_strsave(pat);
2824 for (i = 0; pat[i]; ++i)
2825 if (pat[i] == '\\')
2826 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002827 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002828 && pat[i + 1] == '\\'
2829 && pat[i + 2] == '\\'
2830 && pat[i + 3] == ' ')
2831 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002832 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002833 && pat[i + 1] == ' ')
2834 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002835 else if ((xp->xp_backslash & XP_BS_COMMA)
2836 && pat[i + 1] == '\\'
2837 && pat[i + 2] == ',')
2838 STRMOVE(pat + i, pat + i + 2);
2839#ifdef BACKSLASH_IN_FILENAME
2840 else if ((xp->xp_backslash & XP_BS_COMMA)
2841 && pat[i + 1] == ',')
2842 STRMOVE(pat + i, pat + i + 1);
2843#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002844 }
2845 }
2846
2847 if (xp->xp_context == EXPAND_FILES)
2848 flags |= EW_FILE;
2849 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2850 flags |= (EW_FILE | EW_PATH);
LemonBoya20bf692024-07-11 22:35:53 +02002851 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2852 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002853 else
2854 flags = (flags | EW_DIR) & ~EW_FILE;
2855 if (options & WILD_ICASE)
2856 flags |= EW_ICASE;
2857
2858 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002859 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002860 if (free_pat)
2861 vim_free(pat);
2862#ifdef BACKSLASH_IN_FILENAME
2863 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2864 {
2865 int j;
2866
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002867 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002868 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002869 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002870
2871 while (*ptr != NUL)
2872 {
2873 if (p_csl[0] == 's' && *ptr == '\\')
2874 *ptr = '/';
2875 else if (p_csl[0] == 'b' && *ptr == '/')
2876 *ptr = '\\';
2877 ptr += (*mb_ptr2len)(ptr);
2878 }
2879 }
2880 }
2881#endif
2882 return ret;
2883}
2884
2885/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002886 * Function given to ExpandGeneric() to obtain the possible arguments of the
2887 * ":behave {mswin,xterm}" command.
2888 */
2889 static char_u *
2890get_behave_arg(expand_T *xp UNUSED, int idx)
2891{
2892 if (idx == 0)
2893 return (char_u *)"mswin";
2894 if (idx == 1)
2895 return (char_u *)"xterm";
2896 return NULL;
2897}
2898
K.Takata161b6ac2022-11-14 15:31:07 +00002899#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002900/*
2901 * Function given to ExpandGeneric() to obtain the possible arguments of the
2902 * ":breakadd {expr, file, func, here}" command.
2903 * ":breakdel {func, file, here}" command.
2904 */
2905 static char_u *
2906get_breakadd_arg(expand_T *xp UNUSED, int idx)
2907{
2908 char *opts[] = {"expr", "file", "func", "here"};
2909
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002910 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002911 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002912 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002913 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2914 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002915 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2916 {
2917 // breakdel {func, file, here}
2918 if (idx <= 2)
2919 return (char_u *)opts[idx + 1];
2920 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002921 else
2922 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002923 // profdel {func, file}
2924 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002925 return (char_u *)opts[idx + 1];
2926 }
2927 }
2928 return NULL;
2929}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002930
2931/*
2932 * Function given to ExpandGeneric() to obtain the possible arguments for the
2933 * ":scriptnames" command.
2934 */
2935 static char_u *
2936get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2937{
2938 scriptitem_T *si;
2939
2940 if (!SCRIPT_ID_VALID(idx + 1))
2941 return NULL;
2942
2943 si = SCRIPT_ITEM(idx + 1);
2944 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2945 return NameBuff;
2946}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002947#endif
2948
Bram Moolenaard0190392019-08-23 21:17:35 +02002949/*
2950 * Function given to ExpandGeneric() to obtain the possible arguments of the
2951 * ":messages {clear}" command.
2952 */
2953 static char_u *
2954get_messages_arg(expand_T *xp UNUSED, int idx)
2955{
2956 if (idx == 0)
2957 return (char_u *)"clear";
2958 return NULL;
2959}
2960
2961 static char_u *
2962get_mapclear_arg(expand_T *xp UNUSED, int idx)
2963{
2964 if (idx == 0)
2965 return (char_u *)"<buffer>";
2966 return NULL;
2967}
2968
2969/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002970 * Do the expansion based on xp->xp_context and 'rmp'.
2971 */
2972 static int
2973ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002974 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002975 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002976 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002977 char_u ***matches,
2978 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002979{
2980 static struct expgen
2981 {
2982 int context;
2983 char_u *((*func)(expand_T *, int));
2984 int ic;
2985 int escaped;
2986 } tab[] =
2987 {
2988 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2989 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2990 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2991 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2992 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2993 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2994 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2995 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2996 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2997 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002998#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002999 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3000 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3001 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3002 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3003 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003004#endif
3005#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003006 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3007 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003008#endif
3009#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003010 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003011#endif
3012#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003013 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003014#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003015 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3016 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3017 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003018#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003019 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003020#endif
3021#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003022 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003023#endif
3024#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003025 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003026#endif
3027#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003028 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3029 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003030#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003031 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3032 {EXPAND_USER, get_users, TRUE, FALSE},
3033 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003034#ifdef FEAT_EVAL
3035 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003036 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003037#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003038 };
3039 int i;
3040 int ret = FAIL;
3041
3042 // Find a context in the table and call the ExpandGeneric() with the
3043 // right function to do the expansion.
3044 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3045 {
3046 if (xp->xp_context == tab[i].context)
3047 {
3048 if (tab[i].ic)
3049 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003050 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3051 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003052 break;
3053 }
3054 }
3055
3056 return ret;
3057}
3058
3059/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003060 * Map wild expand options to flags for expand_wildcards()
3061 */
3062 static int
3063map_wildopts_to_ewflags(int options)
3064{
3065 int flags;
3066
3067 flags = EW_DIR; // include directories
3068 if (options & WILD_LIST_NOTFOUND)
3069 flags |= EW_NOTFOUND;
3070 if (options & WILD_ADD_SLASH)
3071 flags |= EW_ADDSLASH;
3072 if (options & WILD_KEEP_ALL)
3073 flags |= EW_KEEPALL;
3074 if (options & WILD_SILENT)
3075 flags |= EW_SILENT;
3076 if (options & WILD_NOERROR)
3077 flags |= EW_NOERROR;
3078 if (options & WILD_ALLLINKS)
3079 flags |= EW_ALLLINKS;
3080
3081 return flags;
3082}
3083
3084/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003085 * Do the expansion based on xp->xp_context and "pat".
3086 */
3087 static int
3088ExpandFromContext(
3089 expand_T *xp,
3090 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003091 char_u ***matches,
3092 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003093 int options) // WILD_ flags
3094{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003095 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003096 int ret;
3097 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003098 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003099 int fuzzy = cmdline_fuzzy_complete(pat)
3100 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003101
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003102 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003103
3104 if (xp->xp_context == EXPAND_FILES
3105 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003106 || xp->xp_context == EXPAND_FILES_IN_PATH
3107 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003108 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3109 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003111 *matches = (char_u **)"";
3112 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113 if (xp->xp_context == EXPAND_HELP)
3114 {
3115 // With an empty argument we would get all the help tags, which is
3116 // very slow. Get matches for "help" instead.
3117 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003118 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003119 {
3120#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003121 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003122#endif
3123 return OK;
3124 }
3125 return FAIL;
3126 }
3127
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003129 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003131 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003132 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003133 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003134#ifdef FEAT_DIFF
3135 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003136 return ExpandBufnames(pat, numMatches, matches,
3137 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003138#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139 if (xp->xp_context == EXPAND_TAGS
3140 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003141 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3142 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143 if (xp->xp_context == EXPAND_COLORS)
3144 {
3145 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003146 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147 directories);
3148 }
3149 if (xp->xp_context == EXPAND_COMPILER)
3150 {
3151 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003152 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003153 }
3154 if (xp->xp_context == EXPAND_OWNSYNTAX)
3155 {
3156 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003157 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003158 }
3159 if (xp->xp_context == EXPAND_FILETYPE)
3160 {
3161 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003162 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003163 }
Doug Kearns81642d92024-01-04 22:37:44 +01003164#ifdef FEAT_KEYMAP
3165 if (xp->xp_context == EXPAND_KEYMAP)
3166 {
3167 char *directories[] = {"keymap", NULL};
3168 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3169 }
3170#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003171#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003172 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003173 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003174#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003175 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003176 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003177 if (xp->xp_context == EXPAND_RUNTIME)
3178 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003179
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003180 // When expanding a function name starting with s:, match the <SNR>nr_
3181 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003182 if ((xp->xp_context == EXPAND_USER_FUNC
3183 || xp->xp_context == EXPAND_DISASSEMBLE)
3184 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003185 {
3186 int len = (int)STRLEN(pat) + 20;
3187
3188 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003189 if (tofree == NULL)
3190 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003191 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003192 pat = tofree;
3193 }
3194
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003195 if (!fuzzy)
3196 {
3197 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3198 if (regmatch.regprog == NULL)
3199 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003200
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003201 // set ignore-case according to p_ic, p_scs and pat
3202 regmatch.rm_ic = ignorecase(pat);
3203 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003204
3205 if (xp->xp_context == EXPAND_SETTINGS
3206 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003207 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003208 else if (xp->xp_context == EXPAND_STRING_SETTING)
3209 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3210 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3211 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003212 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003213 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003214 else if (xp->xp_context == EXPAND_ARGOPT)
3215 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3216#if defined(FEAT_TERMINAL)
3217 else if (xp->xp_context == EXPAND_TERMINALOPT)
3218 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3219#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003220#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003221 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003222 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003223#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003224 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003225 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003226
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003227 if (!fuzzy)
3228 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003229 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003230
3231 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003232}
3233
Bram Moolenaar66b51422019-08-18 21:44:12 +02003234/*
3235 * Expand a list of names.
3236 *
3237 * Generic function for command line completion. It calls a function to
3238 * obtain strings, one by one. The strings are matched against a regexp
3239 * program. Matching strings are copied into an array, which is returned.
3240 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003241 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3242 * is used.
3243 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003244 * Returns OK when no problems encountered, FAIL for error (out of memory).
3245 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003246 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003247ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003248 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003249 expand_T *xp,
3250 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003251 char_u ***matches,
3252 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253 char_u *((*func)(expand_T *, int)),
3254 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003255 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003256{
3257 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003258 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003259 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003260 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003261 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003262 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003263 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003264 int sort_matches = FALSE;
3265 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003266
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003267 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003268 *matches = NULL;
3269 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003270
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003271 if (!fuzzy)
3272 ga_init2(&ga, sizeof(char *), 30);
3273 else
3274 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3275
3276 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003277 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003278 str = (*func)(xp, i);
3279 if (str == NULL) // end of list
3280 break;
3281 if (*str == NUL) // skip empty strings
3282 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003283
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003284 if (xp->xp_pattern[0] != NUL)
3285 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003286 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003287 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003288 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003289 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003290 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003291 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003292 }
3293 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003294 else
3295 match = TRUE;
3296
3297 if (!match)
3298 continue;
3299
3300 if (escaped)
3301 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3302 else
3303 str = vim_strsave(str);
3304 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003305 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003306 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003308 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003309 return FAIL;
3310 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003311 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003312 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003313 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003314
3315 if (ga_grow(&ga, 1) == FAIL)
3316 {
3317 vim_free(str);
3318 break;
3319 }
3320
3321 if (fuzzy)
3322 {
3323 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3324 fuzmatch->idx = ga.ga_len;
3325 fuzmatch->str = str;
3326 fuzmatch->score = score;
3327 }
3328 else
3329 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3330
K.Takata161b6ac2022-11-14 15:31:07 +00003331#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003332 if (func == get_menu_names)
3333 {
3334 // test for separator added by get_menu_names()
3335 str += STRLEN(str) - 1;
3336 if (*str == '\001')
3337 *str = '.';
3338 }
K.Takata161b6ac2022-11-14 15:31:07 +00003339#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003340
3341 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003342 }
3343
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003344 if (ga.ga_len == 0)
3345 return OK;
3346
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003347 // sort the matches when using regular expression matching and sorting
3348 // applies to the completion context. Menus and scriptnames should be kept
3349 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003350 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003351 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003352 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003353 && xp->xp_context != EXPAND_SCRIPTNAMES
3354 && xp->xp_context != EXPAND_ARGOPT
3355 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003356 sort_matches = TRUE;
3357
3358 // <SNR> functions should be sorted to the end.
3359 if (xp->xp_context == EXPAND_EXPRESSION
3360 || xp->xp_context == EXPAND_FUNCTIONS
3361 || xp->xp_context == EXPAND_USER_FUNC
3362 || xp->xp_context == EXPAND_DISASSEMBLE)
3363 funcsort = TRUE;
3364
3365 // Sort the matches.
3366 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003367 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003368 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003369 // <SNR> functions should be sorted to the end.
3370 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3371 sort_func_compare);
3372 else
3373 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3374 }
3375
3376 if (!fuzzy)
3377 {
3378 *matches = ga.ga_data;
3379 *numMatches = ga.ga_len;
3380 }
3381 else
3382 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003383 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3384 funcsort) == FAIL)
3385 return FAIL;
3386 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003387 }
3388
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003389#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003390 // Reset the variables used for special highlight names expansion, so that
3391 // they don't show up when getting normal highlight names by ID.
3392 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003393#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003394
Bram Moolenaar66b51422019-08-18 21:44:12 +02003395 return OK;
3396}
3397
3398/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003399 * Expand shell command matches in one directory of $PATH.
3400 */
3401 static void
3402expand_shellcmd_onedir(
3403 char_u *buf,
3404 char_u *s,
3405 size_t l,
3406 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003407 char_u ***matches,
3408 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003409 int flags,
3410 hashtab_T *ht,
3411 garray_T *gap)
3412{
3413 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003414 hash_T hash;
3415 hashitem_T *hi;
3416
3417 vim_strncpy(buf, s, l);
3418 add_pathsep(buf);
3419 l = STRLEN(buf);
3420 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3421
3422 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003423 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003424 if (ret != OK)
3425 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003426
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003427 if (ga_grow(gap, *numMatches) == FAIL)
3428 {
3429 FreeWild(*numMatches, *matches);
3430 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003431 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003432
3433 for (int i = 0; i < *numMatches; ++i)
3434 {
3435 char_u *name = (*matches)[i];
3436
3437 if (STRLEN(name) > l)
3438 {
3439 // Check if this name was already found.
3440 hash = hash_hash(name + l);
3441 hi = hash_lookup(ht, name + l, hash);
3442 if (HASHITEM_EMPTY(hi))
3443 {
3444 // Remove the path that was prepended.
3445 STRMOVE(name, name + l);
3446 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3447 hash_add_item(ht, hi, name, hash);
3448 name = NULL;
3449 }
3450 }
3451 vim_free(name);
3452 }
3453 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003454}
3455
3456/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003457 * Complete a shell command.
3458 * Returns FAIL or OK;
3459 */
3460 static int
3461expand_shellcmd(
3462 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003463 char_u ***matches, // return: array with matches
3464 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003465 int flagsarg) // EW_ flags
3466{
3467 char_u *pat;
3468 int i;
3469 char_u *path = NULL;
3470 int mustfree = FALSE;
3471 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003472 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003473 size_t l;
3474 char_u *s, *e;
3475 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003476 int did_curdir = FALSE;
3477 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003478
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003479 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480 if (buf == NULL)
3481 return FAIL;
3482
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003483 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003484 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003485 if (pat == NULL)
3486 {
3487 vim_free(buf);
3488 return FAIL;
3489 }
3490
Bram Moolenaar66b51422019-08-18 21:44:12 +02003491 for (i = 0; pat[i]; ++i)
3492 if (pat[i] == '\\' && pat[i + 1] == ' ')
3493 STRMOVE(pat + i, pat + i + 1);
3494
3495 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3496
3497 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3498 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3499 path = (char_u *)".";
3500 else
3501 {
3502 // For an absolute name we don't use $PATH.
3503 if (!mch_isFullName(pat))
3504 path = vim_getenv((char_u *)"PATH", &mustfree);
3505 if (path == NULL)
3506 path = (char_u *)"";
3507 }
3508
3509 // Go over all directories in $PATH. Expand matches in that directory and
3510 // collect them in "ga". When "." is not in $PATH also expand for the
3511 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003512 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003513 hash_init(&found_ht);
3514 for (s = path; ; s = e)
3515 {
K.Takata161b6ac2022-11-14 15:31:07 +00003516#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003517 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003518#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003519 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003520#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003521 if (e == NULL)
3522 e = s + STRLEN(s);
3523
3524 if (*s == NUL)
3525 {
3526 if (did_curdir)
3527 break;
3528 // Find directories in the current directory, path is empty.
3529 did_curdir = TRUE;
3530 flags |= EW_DIR;
3531 }
3532 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3533 {
3534 did_curdir = TRUE;
3535 flags |= EW_DIR;
3536 }
3537 else
3538 // Do not match directories inside a $PATH item.
3539 flags &= ~EW_DIR;
3540
3541 l = e - s;
3542 if (l > MAXPATHL - 5)
3543 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003544
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003545 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003546 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003547
Bram Moolenaar66b51422019-08-18 21:44:12 +02003548 if (*e != NUL)
3549 ++e;
3550 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003551 *matches = ga.ga_data;
3552 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003553
3554 vim_free(buf);
3555 vim_free(pat);
3556 if (mustfree)
3557 vim_free(path);
3558 hash_clear(&found_ht);
3559 return OK;
3560}
3561
K.Takata161b6ac2022-11-14 15:31:07 +00003562#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003563/*
3564 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003565 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003566 */
3567 static void *
3568call_user_expand_func(
3569 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003570 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003571{
3572 cmdline_info_T *ccline = get_cmdline_info();
3573 int keep = 0;
3574 typval_T args[4];
3575 sctx_T save_current_sctx = current_sctx;
3576 char_u *pat = NULL;
3577 void *ret;
3578
3579 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3580 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003581
3582 if (ccline->cmdbuff != NULL)
3583 {
3584 keep = ccline->cmdbuff[ccline->cmdlen];
3585 ccline->cmdbuff[ccline->cmdlen] = 0;
3586 }
3587
3588 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3589
3590 args[0].v_type = VAR_STRING;
3591 args[0].vval.v_string = pat;
3592 args[1].v_type = VAR_STRING;
3593 args[1].vval.v_string = xp->xp_line;
3594 args[2].v_type = VAR_NUMBER;
3595 args[2].vval.v_number = xp->xp_col;
3596 args[3].v_type = VAR_UNKNOWN;
3597
3598 current_sctx = xp->xp_script_ctx;
3599
3600 ret = user_expand_func(xp->xp_arg, 3, args);
3601
3602 current_sctx = save_current_sctx;
3603 if (ccline->cmdbuff != NULL)
3604 ccline->cmdbuff[ccline->cmdlen] = keep;
3605
3606 vim_free(pat);
3607 return ret;
3608}
3609
3610/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003611 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3612 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003613 */
3614 static int
3615ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003616 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003617 expand_T *xp,
3618 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003619 char_u ***matches,
3620 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003621{
3622 char_u *retstr;
3623 char_u *s;
3624 char_u *e;
3625 int keep;
3626 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003627 int fuzzy;
3628 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003629 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003630
3631 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003632 *matches = NULL;
3633 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003634
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003635 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003636 if (retstr == NULL)
3637 return FAIL;
3638
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003639 if (!fuzzy)
3640 ga_init2(&ga, sizeof(char *), 3);
3641 else
3642 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3643
Bram Moolenaar66b51422019-08-18 21:44:12 +02003644 for (s = retstr; *s != NUL; s = e)
3645 {
3646 e = vim_strchr(s, '\n');
3647 if (e == NULL)
3648 e = s + STRLEN(s);
3649 keep = *e;
3650 *e = NUL;
3651
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003652 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003653 {
3654 if (!fuzzy)
3655 match = vim_regexec(regmatch, s, (colnr_T)0);
3656 else
3657 {
3658 score = fuzzy_match_str(s, pat);
3659 match = (score != 0);
3660 }
3661 }
3662 else
3663 match = TRUE; // match everything
3664
Bram Moolenaar66b51422019-08-18 21:44:12 +02003665 *e = keep;
3666
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003667 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003668 {
3669 if (ga_grow(&ga, 1) == FAIL)
3670 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003671 if (!fuzzy)
3672 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3673 else
3674 {
3675 fuzmatch_str_T *fuzmatch =
3676 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003677 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003678 fuzmatch->str = vim_strnsave(s, e - s);
3679 fuzmatch->score = score;
3680 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003681 ++ga.ga_len;
3682 }
3683
3684 if (*e != NUL)
3685 ++e;
3686 }
3687 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003688
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003689 if (ga.ga_len == 0)
3690 return OK;
3691
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003692 if (!fuzzy)
3693 {
3694 *matches = ga.ga_data;
3695 *numMatches = ga.ga_len;
3696 }
3697 else
3698 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003699 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3700 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003701 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003702 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003703 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003704 return OK;
3705}
3706
3707/*
3708 * Expand names with a list returned by a function defined by the user.
3709 */
3710 static int
3711ExpandUserList(
3712 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003713 char_u ***matches,
3714 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003715{
3716 list_T *retlist;
3717 listitem_T *li;
3718 garray_T ga;
3719
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003720 *matches = NULL;
3721 *numMatches = 0;
3722 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003723 if (retlist == NULL)
3724 return FAIL;
3725
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003726 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003727 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003728 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003729 {
3730 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3731 continue; // Skip non-string items and empty strings
3732
3733 if (ga_grow(&ga, 1) == FAIL)
3734 break;
3735
3736 ((char_u **)ga.ga_data)[ga.ga_len] =
3737 vim_strsave(li->li_tv.vval.v_string);
3738 ++ga.ga_len;
3739 }
3740 list_unref(retlist);
3741
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003742 *matches = ga.ga_data;
3743 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003744 return OK;
3745}
K.Takata161b6ac2022-11-14 15:31:07 +00003746#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003747
3748/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003749 * Expand "file" for all comma-separated directories in "path".
3750 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003751 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003752 */
3753 void
3754globpath(
3755 char_u *path,
3756 char_u *file,
3757 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003758 int expand_options,
3759 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003760{
3761 expand_T xpc;
3762 char_u *buf;
3763 int i;
3764 int num_p;
3765 char_u **p;
3766
3767 buf = alloc(MAXPATHL);
3768 if (buf == NULL)
3769 return;
3770
3771 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003772 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003773
3774 // Loop over all entries in {path}.
3775 while (*path != NUL)
3776 {
3777 // Copy one item of the path to buf[] and concatenate the file name.
3778 copy_option_part(&path, buf, MAXPATHL, ",");
3779 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3780 {
K.Takata161b6ac2022-11-14 15:31:07 +00003781#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003782 // Using the platform's path separator (\) makes vim incorrectly
3783 // treat it as an escape character, use '/' instead.
3784 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3785 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003786#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003787 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003788#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003789 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003790 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003791 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3792 {
3793 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3794
3795 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003796 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003797 for (i = 0; i < num_p; ++i)
3798 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003799 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003800 ++ga->ga_len;
3801 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003802 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003803 }
3804 }
3805 }
3806
3807 vim_free(buf);
3808}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003809
Bram Moolenaareadee482020-09-04 15:37:31 +02003810/*
3811 * Translate some keys pressed when 'wildmenu' is used.
3812 */
3813 int
3814wildmenu_translate_key(
3815 cmdline_info_T *cclp,
3816 int key,
3817 expand_T *xp,
3818 int did_wild_list)
3819{
3820 int c = key;
3821
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003822 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003823 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003824 // When the popup menu is used for cmdline completion:
3825 // Up : go to the previous item in the menu
3826 // Down : go to the next item in the menu
3827 // Left : go to the parent directory
3828 // Right: list the files in the selected directory
3829 switch (c)
3830 {
3831 case K_UP: c = K_LEFT; break;
3832 case K_DOWN: c = K_RIGHT; break;
3833 case K_LEFT: c = K_UP; break;
3834 case K_RIGHT: c = K_DOWN; break;
3835 default: break;
3836 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003837 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003838
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003839 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003840 {
3841 if (c == K_LEFT)
3842 c = Ctrl_P;
3843 else if (c == K_RIGHT)
3844 c = Ctrl_N;
3845 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003846
Bram Moolenaareadee482020-09-04 15:37:31 +02003847 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003848 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003849 && cclp->cmdpos > 1
3850 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3851 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3852 && (c == '\n' || c == '\r' || c == K_KENTER))
3853 c = K_DOWN;
3854
3855 return c;
3856}
3857
3858/*
3859 * Delete characters on the command line, from "from" to the current
3860 * position.
3861 */
3862 static void
3863cmdline_del(cmdline_info_T *cclp, int from)
3864{
3865 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3866 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3867 cclp->cmdlen -= cclp->cmdpos - from;
3868 cclp->cmdpos = from;
3869}
3870
3871/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003872 * Handle a key pressed when the wild menu for the menu names
3873 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003874 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003875 static int
3876wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003877{
Bram Moolenaareadee482020-09-04 15:37:31 +02003878 int i;
3879 int j;
3880
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003881 // Hitting <Down> after "emenu Name.": complete submenu
3882 if (key == K_DOWN && cclp->cmdpos > 0
3883 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003884 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003885 key = p_wc;
3886 KeyTyped = TRUE; // in case the key was mapped
3887 }
3888 else if (key == K_UP)
3889 {
3890 // Hitting <Up>: Remove one submenu name in front of the
3891 // cursor
3892 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003893
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003894 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3895 i = 0;
3896 while (--j > 0)
3897 {
3898 // check for start of menu name
3899 if (cclp->cmdbuff[j] == ' '
3900 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003901 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003902 i = j + 1;
3903 break;
3904 }
3905 // check for start of submenu name
3906 if (cclp->cmdbuff[j] == '.'
3907 && cclp->cmdbuff[j - 1] != '\\')
3908 {
3909 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003910 {
3911 i = j + 1;
3912 break;
3913 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003914 else
3915 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003916 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003917 }
3918 if (i > 0)
3919 cmdline_del(cclp, i);
3920 key = p_wc;
3921 KeyTyped = TRUE; // in case the key was mapped
3922 xp->xp_context = EXPAND_NOTHING;
3923 }
3924
3925 return key;
3926}
3927
3928/*
3929 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3930 * directory names (EXPAND_DIRECTORIES) or shell command names
3931 * (EXPAND_SHELLCMD) is displayed.
3932 */
3933 static int
3934wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3935{
3936 int i;
3937 int j;
3938 char_u upseg[5];
3939
3940 upseg[0] = PATHSEP;
3941 upseg[1] = '.';
3942 upseg[2] = '.';
3943 upseg[3] = PATHSEP;
3944 upseg[4] = NUL;
3945
3946 if (key == K_DOWN
3947 && cclp->cmdpos > 0
3948 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3949 && (cclp->cmdpos < 3
3950 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3951 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3952 {
3953 // go down a directory
3954 key = p_wc;
3955 KeyTyped = TRUE; // in case the key was mapped
3956 }
3957 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3958 {
3959 // If in a direct ancestor, strip off one ../ to go down
3960 int found = FALSE;
3961
3962 j = cclp->cmdpos;
3963 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3964 while (--j > i)
3965 {
3966 if (has_mbyte)
3967 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3968 if (vim_ispathsep(cclp->cmdbuff[j]))
3969 {
3970 found = TRUE;
3971 break;
3972 }
3973 }
3974 if (found
3975 && cclp->cmdbuff[j - 1] == '.'
3976 && cclp->cmdbuff[j - 2] == '.'
3977 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3978 {
3979 cmdline_del(cclp, j - 2);
3980 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003981 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003982 }
3983 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003984 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003985 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003986 // go up a directory
3987 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003988
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003989 j = cclp->cmdpos - 1;
3990 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3991 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003992 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003993 if (has_mbyte)
3994 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3995 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003996#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003997 && vim_strchr((char_u *)" *?[{`$%#",
3998 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003999#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004000 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004001 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004002 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004003 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004004 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004005 break;
4006 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004007 else
4008 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004009 }
4010 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004011
4012 if (!found)
4013 j = i;
4014 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4015 j += 4;
4016 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4017 && j == i)
4018 j += 3;
4019 else
4020 j = 0;
4021 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004022 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004023 // TODO this is only for DOS/UNIX systems - need to put in
4024 // machine-specific stuff here and in upseg init
4025 cmdline_del(cclp, j);
4026 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004027 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004028 else if (cclp->cmdpos > i)
4029 cmdline_del(cclp, i);
4030
4031 // Now complete in the new directory. Set KeyTyped in case the
4032 // Up key came from a mapping.
4033 key = p_wc;
4034 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004035 }
4036
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004037 return key;
4038}
4039
4040/*
4041 * Handle a key pressed when the wild menu is displayed
4042 */
4043 int
4044wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4045{
4046 if (xp->xp_context == EXPAND_MENUNAMES)
4047 return wildmenu_process_key_menunames(cclp, key, xp);
4048 else if ((xp->xp_context == EXPAND_FILES
4049 || xp->xp_context == EXPAND_DIRECTORIES
4050 || xp->xp_context == EXPAND_SHELLCMD))
4051 return wildmenu_process_key_filenames(cclp, key, xp);
4052
4053 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004054}
4055
4056/*
4057 * Free expanded names when finished walking through the matches
4058 */
4059 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004060wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004061{
4062 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004063
4064 if (!p_wmnu || wild_menu_showing == 0)
4065 return;
4066
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004067#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004068 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004069 if (cclp->input_fn)
4070 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004071#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004072
4073 if (wild_menu_showing == WM_SCROLLED)
4074 {
4075 // Entered command line, move it up
4076 cmdline_row--;
4077 redrawcmd();
4078 }
4079 else if (save_p_ls != -1)
4080 {
4081 // restore 'laststatus' and 'winminheight'
4082 p_ls = save_p_ls;
4083 p_wmh = save_p_wmh;
4084 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004085 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004086 redrawcmd();
4087 save_p_ls = -1;
4088 }
4089 else
4090 {
4091 win_redraw_last_status(topframe);
4092 redraw_statuslines();
4093 }
4094 KeyTyped = skt;
4095 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004096#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004097 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004098 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004099#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004100}
Bram Moolenaareadee482020-09-04 15:37:31 +02004101
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004102#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004103/*
4104 * "getcompletion()" function
4105 */
4106 void
4107f_getcompletion(typval_T *argvars, typval_T *rettv)
4108{
4109 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004110 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004111 expand_T xpc;
4112 int filtered = FALSE;
4113 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004114 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004115
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004116 if (in_vim9script()
4117 && (check_for_string_arg(argvars, 0) == FAIL
4118 || check_for_string_arg(argvars, 1) == FAIL
4119 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4120 return;
4121
ii144785fe02021-11-21 12:13:56 +00004122 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004123 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004124 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004125 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004126
4127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004128 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004129
4130 if (p_wic)
4131 options |= WILD_ICASE;
4132
4133 // For filtered results, 'wildignore' is used
4134 if (!filtered)
4135 options |= WILD_KEEP_ALL;
4136
4137 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004138 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004139 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004140 int cmdline_len = (int)STRLEN(pat);
4141 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004142 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004143 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004144 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004145 else
4146 {
ii144785fe02021-11-21 12:13:56 +00004147 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004148 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004149 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004150
4151 xpc.xp_context = cmdcomplete_str_to_type(type);
4152 if (xpc.xp_context == EXPAND_NOTHING)
4153 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004154 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004155 return;
4156 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004157
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004158 if (xpc.xp_context == EXPAND_USER_DEFINED)
4159 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004160 // Must be "custom,funcname" pattern
4161 if (STRNCMP(type, "custom,", 7) != 0)
4162 {
4163 semsg(_(e_invalid_argument_str), type);
4164 return;
4165 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004166
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004167 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004168 }
4169
4170 if (xpc.xp_context == EXPAND_USER_LIST)
4171 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004172 // Must be "customlist,funcname" pattern
4173 if (STRNCMP(type, "customlist,", 11) != 0)
4174 {
4175 semsg(_(e_invalid_argument_str), type);
4176 return;
4177 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004178
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004179 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004180 }
4181
Bram Moolenaar66b51422019-08-18 21:44:12 +02004182# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004183 if (xpc.xp_context == EXPAND_MENUS)
4184 {
4185 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4186 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4187 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004188# endif
4189# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004190 if (xpc.xp_context == EXPAND_CSCOPE)
4191 {
4192 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4193 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4194 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004195# endif
4196# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004197 if (xpc.xp_context == EXPAND_SIGN)
4198 {
4199 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4200 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4201 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004202# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004203 if (xpc.xp_context == EXPAND_RUNTIME)
4204 {
4205 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4206 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4207 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004208 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004209
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004210 if (cmdline_fuzzy_completion_supported(&xpc))
4211 // when fuzzy matching, don't modify the search string
4212 pat = vim_strsave(xpc.xp_pattern);
4213 else
4214 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4215
Bram Moolenaar93a10962022-06-16 11:42:09 +01004216 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004217 {
4218 int i;
4219
4220 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4221
4222 for (i = 0; i < xpc.xp_numfiles; i++)
4223 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4224 }
4225 vim_free(pat);
4226 ExpandCleanup(&xpc);
4227}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004228#endif // FEAT_EVAL