blob: 40778cc219f0e24d33c27e2f0b32f837e0c78ff0 [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 Lakshmananafd4ae32022-02-27 21:03:21 +000018static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000020 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000022static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000026static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020028#endif
29
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000030// "compl_match_array" points the currently displayed list of entries in the
31// popup menu. It is NULL when there is no popup menu.
32static pumitem_T *compl_match_array = NULL;
33static int compl_match_arraysize;
34// First column in cmdline of the matched item for completion.
35static int compl_startcol;
36static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
zeertzjqc51a3762022-12-10 10:22:29 +000038#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
52 && xp->xp_context != EXPAND_FILES
53 && xp->xp_context != EXPAND_FILES_IN_PATH
54 && xp->xp_context != EXPAND_FILETYPE
55 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
57 && xp->xp_context != EXPAND_OWNSYNTAX
58 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000059 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060 && xp->xp_context != EXPAND_SHELLCMD
61 && xp->xp_context != EXPAND_TAGS
62 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_USER_LIST);
64}
65
66/*
67 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000068 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
69 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000070 */
71 int
72cmdline_fuzzy_complete(char_u *fuzzystr)
73{
74 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
75}
76
77/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000078 * sort function for the completion matches.
79 * <SNR> functions should be sorted to the end.
80 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020081 static int
82sort_func_compare(const void *s1, const void *s2)
83{
84 char_u *p1 = *(char_u **)s1;
85 char_u *p2 = *(char_u **)s2;
86
87 if (*p1 != '<' && *p2 == '<') return -1;
88 if (*p1 == '<' && *p2 != '<') return 1;
89 return STRCMP(p1, p2);
90}
Bram Moolenaar66b51422019-08-18 21:44:12 +020091
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000092/*
93 * Escape special characters in the cmdline completion matches.
94 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020095 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000096wildescape(
97 expand_T *xp,
98 char_u *str,
99 int numfiles,
100 char_u **files)
101{
102 char_u *p;
103 int vse_what = xp->xp_context == EXPAND_BUFFERS
104 ? VSE_BUFFER : VSE_NONE;
105
106 if (xp->xp_context == EXPAND_FILES
107 || xp->xp_context == EXPAND_FILES_IN_PATH
108 || xp->xp_context == EXPAND_SHELLCMD
109 || xp->xp_context == EXPAND_BUFFERS
110 || xp->xp_context == EXPAND_DIRECTORIES)
111 {
112 // Insert a backslash into a file name before a space, \, %, #
113 // and wildmatch characters, except '~'.
114 for (int i = 0; i < numfiles; ++i)
115 {
116 // for ":set path=" we need to escape spaces twice
117 if (xp->xp_backslash == XP_BS_THREE)
118 {
119 p = vim_strsave_escaped(files[i], (char_u *)" ");
120 if (p != NULL)
121 {
122 vim_free(files[i]);
123 files[i] = p;
124#if defined(BACKSLASH_IN_FILENAME)
125 p = vim_strsave_escaped(files[i], (char_u *)" ");
126 if (p != NULL)
127 {
128 vim_free(files[i]);
129 files[i] = p;
130 }
131#endif
132 }
133 }
134#ifdef BACKSLASH_IN_FILENAME
135 p = vim_strsave_fnameescape(files[i], vse_what);
136#else
137 p = vim_strsave_fnameescape(files[i],
138 xp->xp_shell ? VSE_SHELL : vse_what);
139#endif
140 if (p != NULL)
141 {
142 vim_free(files[i]);
143 files[i] = p;
144 }
145
146 // If 'str' starts with "\~", replace "~" at start of
147 // files[i] with "\~".
148 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
149 escape_fname(&files[i]);
150 }
151 xp->xp_backslash = XP_BS_NONE;
152
153 // If the first file starts with a '+' escape it. Otherwise it
154 // could be seen as "+cmd".
155 if (*files[0] == '+')
156 escape_fname(&files[0]);
157 }
158 else if (xp->xp_context == EXPAND_TAGS)
159 {
160 // Insert a backslash before characters in a tag name that
161 // would terminate the ":tag" command.
162 for (int i = 0; i < numfiles; ++i)
163 {
164 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
165 if (p != NULL)
166 {
167 vim_free(files[i]);
168 files[i] = p;
169 }
170 }
171 }
172}
173
174/*
175 * Escape special characters in the cmdline completion matches.
176 */
177 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200178ExpandEscape(
179 expand_T *xp,
180 char_u *str,
181 int numfiles,
182 char_u **files,
183 int options)
184{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200185 // May change home directory back to "~"
186 if (options & WILD_HOME_REPLACE)
187 tilde_replace(str, numfiles, files);
188
189 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000190 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200191}
192
193/*
194 * Return FAIL if this is not an appropriate context in which to do
195 * completion of anything, return OK if it is (even if there are no matches).
196 * For the caller, this means that the character is just passed through like a
197 * normal character (instead of being expanded). This allows :s/^I^D etc.
198 */
199 int
200nextwild(
201 expand_T *xp,
202 int type,
203 int options, // extra options for ExpandOne()
204 int escape) // if TRUE, escape the returned matches
205{
206 cmdline_info_T *ccline = get_cmdline_info();
207 int i, j;
208 char_u *p1;
209 char_u *p2;
210 int difflen;
211 int v;
212
213 if (xp->xp_numfiles == -1)
214 {
215 set_expand_context(xp);
216 cmd_showtail = expand_showtail(xp);
217 }
218
219 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
220 {
221 beep_flush();
222 return OK; // Something illegal on command line
223 }
224 if (xp->xp_context == EXPAND_NOTHING)
225 {
226 // Caller can use the character as a normal char instead
227 return FAIL;
228 }
229
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000230 // If cmd_silent is set then don't show the dots, because redrawcmd() below
231 // won't remove them.
232 if (!cmd_silent)
233 {
234 msg_puts("..."); // show that we are busy
235 out_flush();
236 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200237
238 i = (int)(xp->xp_pattern - ccline->cmdbuff);
239 xp->xp_pattern_len = ccline->cmdpos - i;
240
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000241 if (type == WILD_NEXT || type == WILD_PREV
242 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200243 {
244 // Get next/previous match for a previous expanded pattern.
245 p2 = ExpandOne(xp, NULL, NULL, 0, type);
246 }
247 else
248 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000249 if (cmdline_fuzzy_completion_supported(xp))
250 // If fuzzy matching, don't modify the search string
251 p1 = vim_strsave(xp->xp_pattern);
252 else
253 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
254
Bram Moolenaar66b51422019-08-18 21:44:12 +0200255 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000256 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200257 p2 = NULL;
258 else
259 {
260 int use_options = options |
261 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
262 if (escape)
263 use_options |= WILD_ESCAPE;
264
265 if (p_wic)
266 use_options += WILD_ICASE;
267 p2 = ExpandOne(xp, p1,
268 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
269 use_options, type);
270 vim_free(p1);
271 // longest match: make sure it is not shorter, happens with :help
272 if (p2 != NULL && type == WILD_LONGEST)
273 {
274 for (j = 0; j < xp->xp_pattern_len; ++j)
275 if (ccline->cmdbuff[i + j] == '*'
276 || ccline->cmdbuff[i + j] == '?')
277 break;
278 if ((int)STRLEN(p2) < j)
279 VIM_CLEAR(p2);
280 }
281 }
282 }
283
284 if (p2 != NULL && !got_int)
285 {
286 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
287 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
288 {
289 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
290 xp->xp_pattern = ccline->cmdbuff + i;
291 }
292 else
293 v = OK;
294 if (v == OK)
295 {
296 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
297 &ccline->cmdbuff[ccline->cmdpos],
298 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
299 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
300 ccline->cmdlen += difflen;
301 ccline->cmdpos += difflen;
302 }
303 }
304 vim_free(p2);
305
306 redrawcmd();
307 cursorcmd();
308
309 // When expanding a ":map" command and no matches are found, assume that
310 // the key is supposed to be inserted literally
311 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
312 return FAIL;
313
314 if (xp->xp_numfiles <= 0 && p2 == NULL)
315 beep_flush();
316 else if (xp->xp_numfiles == 1)
317 // free expanded pattern
318 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
319
320 return OK;
321}
322
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000323/*
324 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000325 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000326 */
327 static int
328cmdline_pum_create(
329 cmdline_info_T *ccline,
330 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000331 char_u **matches,
332 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000333 int showtail)
334{
335 int i;
336 int columns;
337
338 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000339 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000340 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000341 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000342 {
zeertzjqc51a3762022-12-10 10:22:29 +0000343 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000344 compl_match_array[i].pum_info = NULL;
345 compl_match_array[i].pum_extra = NULL;
346 compl_match_array[i].pum_kind = NULL;
347 }
348
349 // Compute the popup menu starting column
350 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
351 columns = vim_strsize(xp->xp_pattern);
352 if (showtail)
353 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000354 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 }
357 if (columns >= compl_startcol)
358 compl_startcol = 0;
359 else
360 compl_startcol -= columns;
361
362 // no default selection
363 compl_selected = -1;
364
365 cmdline_pum_display();
366
367 return EXPAND_OK;
368}
369
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000370/*
371 * Display the cmdline completion matches in a popup menu
372 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000373 void
374cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000375{
376 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
377}
378
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000379/*
380 * Returns TRUE if the cmdline completion popup menu is being displayed.
381 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000382 int
383cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000384{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100385 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000386}
387
388/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000389 * Remove the cmdline completion popup menu (if present), free the list of
390 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000391 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000392 void
393cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000394{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000395 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100396 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000397
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000398 pum_undisplay();
399 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000400 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000401 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000402 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000403 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100404
405 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
406 // as a side effect.
407 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000408}
409
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000410 void
411cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000412{
413 cmdline_pum_remove();
414 wildmenu_cleanup(cclp);
415}
416
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000417/*
418 * Returns the starting column number to use for the cmdline completion popup
419 * menu.
420 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000421 int
422cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423{
424 return compl_startcol;
425}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426
Bram Moolenaar66b51422019-08-18 21:44:12 +0200427/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000428 * Return the number of characters that should be skipped in a status match.
429 * These are backslashes used for escaping. Do show backslashes in help tags.
430 */
431 static int
432skip_status_match_char(expand_T *xp, char_u *s)
433{
434 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
435#ifdef FEAT_MENU
436 || ((xp->xp_context == EXPAND_MENUS
437 || xp->xp_context == EXPAND_MENUNAMES)
438 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
439#endif
440 )
441 {
442#ifndef BACKSLASH_IN_FILENAME
443 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
444 return 2;
445#endif
446 return 1;
447 }
448 return 0;
449}
450
451/*
452 * Get the length of an item as it will be shown in the status line.
453 */
454 static int
455status_match_len(expand_T *xp, char_u *s)
456{
457 int len = 0;
458
459#ifdef FEAT_MENU
460 int emenu = xp->xp_context == EXPAND_MENUS
461 || xp->xp_context == EXPAND_MENUNAMES;
462
463 // Check for menu separators - replace with '|'.
464 if (emenu && menu_is_separator(s))
465 return 1;
466#endif
467
468 while (*s != NUL)
469 {
470 s += skip_status_match_char(xp, s);
471 len += ptr2cells(s);
472 MB_PTR_ADV(s);
473 }
474
475 return len;
476}
477
478/*
479 * Show wildchar matches in the status line.
480 * Show at least the "match" item.
481 * We start at item 'first_match' in the list and show all matches that fit.
482 *
483 * If inversion is possible we use it. Else '=' characters are used.
484 */
485 static void
486win_redr_status_matches(
487 expand_T *xp,
488 int num_matches,
489 char_u **matches, // list of matches
490 int match,
491 int showtail)
492{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000493 int row;
494 char_u *buf;
495 int len;
496 int clen; // length in screen cells
497 int fillchar;
498 int attr;
499 int i;
500 int highlight = TRUE;
501 char_u *selstart = NULL;
502 int selstart_col = 0;
503 char_u *selend = NULL;
504 static int first_match = 0;
505 int add_left = FALSE;
506 char_u *s;
507#ifdef FEAT_MENU
508 int emenu;
509#endif
510 int l;
511
512 if (matches == NULL) // interrupted completion?
513 return;
514
515 if (has_mbyte)
516 buf = alloc(Columns * MB_MAXBYTES + 1);
517 else
518 buf = alloc(Columns + 1);
519 if (buf == NULL)
520 return;
521
522 if (match == -1) // don't show match but original text
523 {
524 match = 0;
525 highlight = FALSE;
526 }
527 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000528 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000529 if (match == 0)
530 first_match = 0;
531 else if (match < first_match)
532 {
533 // jumping left, as far as we can go
534 first_match = match;
535 add_left = TRUE;
536 }
537 else
538 {
539 // check if match fits on the screen
540 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000541 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000542 if (first_match > 0)
543 clen += 2;
544 // jumping right, put match at the left
545 if ((long)clen > Columns)
546 {
547 first_match = match;
548 // if showing the last match, we can add some on the left
549 clen = 2;
550 for (i = match; i < num_matches; ++i)
551 {
zeertzjqc51a3762022-12-10 10:22:29 +0000552 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000553 if ((long)clen >= Columns)
554 break;
555 }
556 if (i == num_matches)
557 add_left = TRUE;
558 }
559 }
560 if (add_left)
561 while (first_match > 0)
562 {
zeertzjqc51a3762022-12-10 10:22:29 +0000563 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000564 if ((long)clen >= Columns)
565 break;
566 --first_match;
567 }
568
569 fillchar = fillchar_status(&attr, curwin);
570
571 if (first_match == 0)
572 {
573 *buf = NUL;
574 len = 0;
575 }
576 else
577 {
578 STRCPY(buf, "< ");
579 len = 2;
580 }
581 clen = len;
582
583 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000584 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000585 {
586 if (i == match)
587 {
588 selstart = buf + len;
589 selstart_col = clen;
590 }
591
zeertzjqc51a3762022-12-10 10:22:29 +0000592 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000593 // Check for menu separators - replace with '|'
594#ifdef FEAT_MENU
595 emenu = (xp->xp_context == EXPAND_MENUS
596 || xp->xp_context == EXPAND_MENUNAMES);
597 if (emenu && menu_is_separator(s))
598 {
599 STRCPY(buf + len, transchar('|'));
600 l = (int)STRLEN(buf + len);
601 len += l;
602 clen += l;
603 }
604 else
605#endif
606 for ( ; *s != NUL; ++s)
607 {
608 s += skip_status_match_char(xp, s);
609 clen += ptr2cells(s);
610 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
611 {
612 STRNCPY(buf + len, s, l);
613 s += l - 1;
614 len += l;
615 }
616 else
617 {
618 STRCPY(buf + len, transchar_byte(*s));
619 len += (int)STRLEN(buf + len);
620 }
621 }
622 if (i == match)
623 selend = buf + len;
624
625 *(buf + len++) = ' ';
626 *(buf + len++) = ' ';
627 clen += 2;
628 if (++i == num_matches)
629 break;
630 }
631
632 if (i != num_matches)
633 {
634 *(buf + len++) = '>';
635 ++clen;
636 }
637
638 buf[len] = NUL;
639
640 row = cmdline_row - 1;
641 if (row >= 0)
642 {
643 if (wild_menu_showing == 0)
644 {
645 if (msg_scrolled > 0)
646 {
647 // Put the wildmenu just above the command line. If there is
648 // no room, scroll the screen one line up.
649 if (cmdline_row == Rows - 1)
650 {
651 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
652 ++msg_scrolled;
653 }
654 else
655 {
656 ++cmdline_row;
657 ++row;
658 }
659 wild_menu_showing = WM_SCROLLED;
660 }
661 else
662 {
663 // Create status line if needed by setting 'laststatus' to 2.
664 // Set 'winminheight' to zero to avoid that the window is
665 // resized.
666 if (lastwin->w_status_height == 0)
667 {
668 save_p_ls = p_ls;
669 save_p_wmh = p_wmh;
670 p_ls = 2;
671 p_wmh = 0;
672 last_status(FALSE);
673 }
674 wild_menu_showing = WM_SHOWN;
675 }
676 }
677
678 screen_puts(buf, row, 0, attr);
679 if (selstart != NULL && highlight)
680 {
681 *selend = NUL;
682 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
683 }
684
685 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
686 }
687
688 win_redraw_last_status(topframe);
689 vim_free(buf);
690}
691
692/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000693 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200694 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000695 */
696 static char_u *
697get_next_or_prev_match(
698 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200699 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000700{
zeertzjqe9ef3472023-08-17 23:57:05 +0200701 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000702 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000703
704 if (xp->xp_numfiles <= 0)
705 return NULL;
706
707 if (mode == WILD_PREV)
708 {
709 if (findex == -1)
710 findex = xp->xp_numfiles;
711 --findex;
712 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000713 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000714 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000715 else if (mode == WILD_PAGEUP)
716 {
717 if (findex == 0)
718 // at the first entry, don't select any entries
719 findex = -1;
720 else if (findex == -1)
721 // no entry is selected. select the last entry
722 findex = xp->xp_numfiles - 1;
723 else
724 {
725 // go up by the pum height
726 ht = pum_get_height();
727 if (ht > 3)
728 ht -= 2;
729 findex -= ht;
730 if (findex < 0)
731 // few entries left, select the first entry
732 findex = 0;
733 }
734 }
735 else // mode == WILD_PAGEDOWN
736 {
737 if (findex == xp->xp_numfiles - 1)
738 // at the last entry, don't select any entries
739 findex = -1;
740 else if (findex == -1)
741 // no entry is selected. select the first entry
742 findex = 0;
743 else
744 {
745 // go down by the pum height
746 ht = pum_get_height();
747 if (ht > 3)
748 ht -= 2;
749 findex += ht;
750 if (findex >= xp->xp_numfiles)
751 // few entries left, select the last entry
752 findex = xp->xp_numfiles - 1;
753 }
754 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000755
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000756 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000757 if (findex < 0)
758 {
zeertzjq28a23602023-09-29 19:58:35 +0200759 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000760 findex = xp->xp_numfiles - 1;
761 else
762 findex = -1;
763 }
764 if (findex >= xp->xp_numfiles)
765 {
zeertzjq28a23602023-09-29 19:58:35 +0200766 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000767 findex = 0;
768 else
769 findex = -1;
770 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000771 if (compl_match_array)
772 {
773 compl_selected = findex;
774 cmdline_pum_display();
775 }
776 else if (p_wmnu)
777 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
778 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200779 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000780
781 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200782 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000783
784 return vim_strsave(xp->xp_files[findex]);
785}
786
787/*
788 * Start the command-line expansion and get the matches.
789 */
790 static char_u *
791ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
792{
793 int non_suf_match; // number without matching suffix
794 int i;
795 char_u *ss = NULL;
796
797 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000798 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000799 options) == FAIL)
800 {
801#ifdef FNAME_ILLEGAL
802 // Illegal file name has been silently skipped. But when there
803 // are wildcards, the real problem is that there was no match,
804 // causing the pattern to be added, which has illegal characters.
805 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
806 semsg(_(e_no_match_str_2), str);
807#endif
808 }
809 else if (xp->xp_numfiles == 0)
810 {
811 if (!(options & WILD_SILENT))
812 semsg(_(e_no_match_str_2), str);
813 }
814 else
815 {
816 // Escape the matches for use on the command line.
817 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
818
819 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000820 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000821 {
822 if (xp->xp_numfiles)
823 non_suf_match = xp->xp_numfiles;
824 else
825 non_suf_match = 1;
826 if ((xp->xp_context == EXPAND_FILES
827 || xp->xp_context == EXPAND_DIRECTORIES)
828 && xp->xp_numfiles > 1)
829 {
830 // More than one match; check suffix.
831 // The files will have been sorted on matching suffix in
832 // expand_wildcards, only need to check the first two.
833 non_suf_match = 0;
834 for (i = 0; i < 2; ++i)
835 if (match_suffix(xp->xp_files[i]))
836 ++non_suf_match;
837 }
838 if (non_suf_match != 1)
839 {
840 // Can we ever get here unless it's while expanding
841 // interactively? If not, we can get rid of this all
842 // together. Don't really want to wait for this message
843 // (and possibly have to hit return to continue!).
844 if (!(options & WILD_SILENT))
845 emsg(_(e_too_many_file_names));
846 else if (!(options & WILD_NO_BEEP))
847 beep_flush();
848 }
849 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
850 ss = vim_strsave(xp->xp_files[0]);
851 }
852 }
853
854 return ss;
855}
856
857/*
858 * Return the longest common part in the list of cmdline completion matches.
859 */
860 static char_u *
861find_longest_match(expand_T *xp, int options)
862{
863 long_u len;
864 int mb_len = 1;
865 int c0, ci;
866 int i;
867 char_u *ss;
868
869 for (len = 0; xp->xp_files[0][len]; len += mb_len)
870 {
871 if (has_mbyte)
872 {
873 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
874 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
875 }
876 else
877 c0 = xp->xp_files[0][len];
878 for (i = 1; i < xp->xp_numfiles; ++i)
879 {
880 if (has_mbyte)
881 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
882 else
883 ci = xp->xp_files[i][len];
884 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
885 || xp->xp_context == EXPAND_FILES
886 || xp->xp_context == EXPAND_SHELLCMD
887 || xp->xp_context == EXPAND_BUFFERS))
888 {
889 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
890 break;
891 }
892 else if (c0 != ci)
893 break;
894 }
895 if (i < xp->xp_numfiles)
896 {
897 if (!(options & WILD_NO_BEEP))
898 vim_beep(BO_WILD);
899 break;
900 }
901 }
902
903 ss = alloc(len + 1);
904 if (ss)
905 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
906
907 return ss;
908}
909
910/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000911 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200912 * Chars that should not be expanded must be preceded with a backslash.
913 * Return a pointer to allocated memory containing the new string.
914 * Return NULL for failure.
915 *
916 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200917 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
918 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200919 *
920 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
921 * is WILD_EXPAND_FREE or WILD_ALL.
922 *
923 * mode = WILD_FREE: just free previously expanded matches
924 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
925 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
926 * mode = WILD_NEXT: use next match in multiple match, wrap to first
927 * mode = WILD_PREV: use previous match in multiple match, wrap to first
928 * mode = WILD_ALL: return all matches concatenated
929 * mode = WILD_LONGEST: return longest matched part
930 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000931 * mode = WILD_APPLY: apply the item selected in the cmdline completion
932 * popup menu and close the menu.
933 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
934 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200935 *
936 * options = WILD_LIST_NOTFOUND: list entries without a match
937 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
938 * options = WILD_USE_NL: Use '\n' for WILD_ALL
939 * options = WILD_NO_BEEP: Don't beep for multiple matches
940 * options = WILD_ADD_SLASH: add a slash after directory names
941 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
942 * options = WILD_SILENT: don't print warning messages
943 * options = WILD_ESCAPE: put backslash before special chars
944 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200945 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200946 *
947 * The variables xp->xp_context and xp->xp_backslash must have been set!
948 */
949 char_u *
950ExpandOne(
951 expand_T *xp,
952 char_u *str,
953 char_u *orig, // allocated copy of original of expanded string
954 int options,
955 int mode)
956{
957 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200958 int orig_saved = FALSE;
959 int i;
960 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200961
962 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000963 if (mode == WILD_NEXT || mode == WILD_PREV
964 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +0200965 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200966
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000967 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +0200968 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000969 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +0200970 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +0200971 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +0200972 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000973
Bram Moolenaar66b51422019-08-18 21:44:12 +0200974 // free old names
975 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
976 {
977 FreeWild(xp->xp_numfiles, xp->xp_files);
978 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +0200979 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000980
981 // The entries from xp_files may be used in the PUM, remove it.
982 if (compl_match_array != NULL)
983 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200984 }
zeertzjqe9ef3472023-08-17 23:57:05 +0200985 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986
987 if (mode == WILD_FREE) // only release file name
988 return NULL;
989
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000990 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200991 {
zeertzjq28a23602023-09-29 19:58:35 +0200992 vim_free(xp->xp_orig);
993 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200994 orig_saved = TRUE;
995
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000996 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200997 }
998
999 // Find longest common part
1000 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1001 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001002 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001003 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001004 }
1005
Bram Moolenaar57e95172022-08-20 19:26:14 +01001006 // Concatenate all matching names. Unless interrupted, this can be slow
1007 // and the result probably won't be used.
1008 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001009 {
1010 len = 0;
1011 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001012 {
1013 if (i > 0)
1014 {
1015 if (xp->xp_prefix == XP_PREFIX_NO)
1016 len += 2; // prefix "no"
1017 else if (xp->xp_prefix == XP_PREFIX_INV)
1018 len += 3; // prefix "inv"
1019 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001020 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001021 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001022 ss = alloc(len);
1023 if (ss != NULL)
1024 {
1025 *ss = NUL;
1026 for (i = 0; i < xp->xp_numfiles; ++i)
1027 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001028 if (i > 0)
1029 {
1030 if (xp->xp_prefix == XP_PREFIX_NO)
1031 STRCAT(ss, "no");
1032 else if (xp->xp_prefix == XP_PREFIX_INV)
1033 STRCAT(ss, "inv");
1034 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001035 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001036
Bram Moolenaar66b51422019-08-18 21:44:12 +02001037 if (i != xp->xp_numfiles - 1)
1038 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1039 }
1040 }
1041 }
1042
1043 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1044 ExpandCleanup(xp);
1045
zeertzjq28a23602023-09-29 19:58:35 +02001046 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001047 if (!orig_saved)
1048 vim_free(orig);
1049
1050 return ss;
1051}
1052
1053/*
1054 * Prepare an expand structure for use.
1055 */
1056 void
1057ExpandInit(expand_T *xp)
1058{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001059 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001060 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001061 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001062 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001063}
1064
1065/*
1066 * Cleanup an expand structure after use.
1067 */
1068 void
1069ExpandCleanup(expand_T *xp)
1070{
1071 if (xp->xp_numfiles >= 0)
1072 {
1073 FreeWild(xp->xp_numfiles, xp->xp_files);
1074 xp->xp_numfiles = -1;
1075 }
zeertzjq28a23602023-09-29 19:58:35 +02001076 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001077}
1078
1079/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001080 * Display one line of completion matches. Multiple matches are displayed in
1081 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001082 * matches - list of completion match names
1083 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001084 * lines - number of output lines
1085 * linenr - line number of matches to display
1086 * maxlen - maximum number of characters in each line
1087 * showtail - display only the tail of the full path of a file name
1088 * dir_attr - highlight attribute to use for directory names
1089 */
1090 static void
1091showmatches_oneline(
1092 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001093 char_u **matches,
1094 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001095 int lines,
1096 int linenr,
1097 int maxlen,
1098 int showtail,
1099 int dir_attr)
1100{
1101 int i, j;
1102 int isdir;
1103 int lastlen;
1104 char_u *p;
1105
1106 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001107 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001108 {
1109 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1110 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001111 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1112 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001113 msg_advance(maxlen + 1);
1114 msg_puts((char *)p);
1115 msg_advance(maxlen + 3);
1116 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1117 break;
1118 }
1119 for (i = maxlen - lastlen; --i >= 0; )
1120 msg_putchar(' ');
1121 if (xp->xp_context == EXPAND_FILES
1122 || xp->xp_context == EXPAND_SHELLCMD
1123 || xp->xp_context == EXPAND_BUFFERS)
1124 {
1125 // highlight directories
1126 if (xp->xp_numfiles != -1)
1127 {
1128 char_u *halved_slash;
1129 char_u *exp_path;
1130 char_u *path;
1131
1132 // Expansion was done before and special characters
1133 // were escaped, need to halve backslashes. Also
1134 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001135 exp_path = expand_env_save_opt(matches[j], TRUE);
1136 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001137 halved_slash = backslash_halve_save(path);
1138 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001139 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001140 vim_free(exp_path);
1141 if (halved_slash != path)
1142 vim_free(halved_slash);
1143 }
1144 else
1145 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001146 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001147 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001148 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001149 else
1150 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001151 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001152 TRUE);
1153 p = NameBuff;
1154 }
1155 }
1156 else
1157 {
1158 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001159 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001160 }
1161 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1162 }
1163 if (msg_col > 0) // when not wrapped around
1164 {
1165 msg_clr_eos();
1166 msg_putchar('\n');
1167 }
1168 out_flush(); // show one line at a time
1169}
1170
1171/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001172 * Show all matches for completion on the command line.
1173 * Returns EXPAND_NOTHING when the character that triggered expansion should
1174 * be inserted like a normal character.
1175 */
1176 int
1177showmatches(expand_T *xp, int wildmenu UNUSED)
1178{
1179 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001180 int numMatches;
1181 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001182 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001183 int maxlen;
1184 int lines;
1185 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001186 int attr;
1187 int showtail;
1188
1189 if (xp->xp_numfiles == -1)
1190 {
1191 set_expand_context(xp);
1192 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001193 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001194 showtail = expand_showtail(xp);
1195 if (i != EXPAND_OK)
1196 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001197 }
1198 else
1199 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001200 numMatches = xp->xp_numfiles;
1201 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001202 showtail = cmd_showtail;
1203 }
1204
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001205 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001206 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001207 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001208
Bram Moolenaar66b51422019-08-18 21:44:12 +02001209 if (!wildmenu)
1210 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001211 msg_didany = FALSE; // lines_left will be set
1212 msg_start(); // prepare for paging
1213 msg_putchar('\n');
1214 out_flush();
1215 cmdline_row = msg_row;
1216 msg_didany = FALSE; // lines_left will be set again
1217 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001218 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001219
1220 if (got_int)
1221 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001222 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001223 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001224 else
1225 {
1226 // find the length of the longest file name
1227 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001228 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001229 {
1230 if (!showtail && (xp->xp_context == EXPAND_FILES
1231 || xp->xp_context == EXPAND_SHELLCMD
1232 || xp->xp_context == EXPAND_BUFFERS))
1233 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001234 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001235 j = vim_strsize(NameBuff);
1236 }
1237 else
zeertzjqc51a3762022-12-10 10:22:29 +00001238 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001239 if (j > maxlen)
1240 maxlen = j;
1241 }
1242
1243 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001244 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001245 else
1246 {
1247 // compute the number of columns and lines for the listing
1248 maxlen += 2; // two spaces between file names
1249 columns = ((int)Columns + 2) / maxlen;
1250 if (columns < 1)
1251 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001252 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001253 }
1254
1255 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1256
1257 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1258 {
1259 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1260 msg_clr_eos();
1261 msg_advance(maxlen - 3);
1262 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1263 }
1264
1265 // list the files line by line
1266 for (i = 0; i < lines; ++i)
1267 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001268 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001269 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001270 if (got_int)
1271 {
1272 got_int = FALSE;
1273 break;
1274 }
1275 }
1276
1277 // we redraw the command below the lines that we have just listed
1278 // This is a bit tricky, but it saves a lot of screen updating.
1279 cmdline_row = msg_row; // will put it back later
1280 }
1281
1282 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001283 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001284
1285 return EXPAND_OK;
1286}
1287
1288/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001289 * gettail() version for showmatches() and win_redr_status_matches():
1290 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001291 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001292 static char_u *
1293showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001294{
1295 char_u *p;
1296 char_u *t = s;
1297 int had_sep = FALSE;
1298
1299 for (p = s; *p != NUL; )
1300 {
1301 if (vim_ispathsep(*p)
1302#ifdef BACKSLASH_IN_FILENAME
1303 && !rem_backslash(p)
1304#endif
1305 )
1306 had_sep = TRUE;
1307 else if (had_sep)
1308 {
1309 t = p;
1310 had_sep = FALSE;
1311 }
1312 MB_PTR_ADV(p);
1313 }
1314 return t;
1315}
1316
1317/*
1318 * Return TRUE if we only need to show the tail of completion matches.
1319 * When not completing file names or there is a wildcard in the path FALSE is
1320 * returned.
1321 */
1322 static int
1323expand_showtail(expand_T *xp)
1324{
1325 char_u *s;
1326 char_u *end;
1327
1328 // When not completing file names a "/" may mean something different.
1329 if (xp->xp_context != EXPAND_FILES
1330 && xp->xp_context != EXPAND_SHELLCMD
1331 && xp->xp_context != EXPAND_DIRECTORIES)
1332 return FALSE;
1333
1334 end = gettail(xp->xp_pattern);
1335 if (end == xp->xp_pattern) // there is no path separator
1336 return FALSE;
1337
1338 for (s = xp->xp_pattern; s < end; s++)
1339 {
1340 // Skip escaped wildcards. Only when the backslash is not a path
1341 // separator, on DOS the '*' "path\*\file" must not be skipped.
1342 if (rem_backslash(s))
1343 ++s;
1344 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1345 return FALSE;
1346 }
1347 return TRUE;
1348}
1349
1350/*
1351 * Prepare a string for expansion.
1352 * When expanding file names: The string will be used with expand_wildcards().
1353 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1354 * When expanding other names: The string will be used with regcomp(). Copy
1355 * the name into allocated memory and prepend "^".
1356 */
1357 char_u *
1358addstar(
1359 char_u *fname,
1360 int len,
1361 int context) // EXPAND_FILES etc.
1362{
1363 char_u *retval;
1364 int i, j;
1365 int new_len;
1366 char_u *tail;
1367 int ends_in_star;
1368
1369 if (context != EXPAND_FILES
1370 && context != EXPAND_FILES_IN_PATH
1371 && context != EXPAND_SHELLCMD
1372 && context != EXPAND_DIRECTORIES)
1373 {
1374 // Matching will be done internally (on something other than files).
1375 // So we convert the file-matching-type wildcards into our kind for
1376 // use with vim_regcomp(). First work out how long it will be:
1377
1378 // For help tags the translation is done in find_help_tags().
1379 // For a tag pattern starting with "/" no translation is needed.
1380 if (context == EXPAND_HELP
1381 || context == EXPAND_COLORS
1382 || context == EXPAND_COMPILER
1383 || context == EXPAND_OWNSYNTAX
1384 || context == EXPAND_FILETYPE
1385 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001386 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001387 || ((context == EXPAND_TAGS_LISTFILES
1388 || context == EXPAND_TAGS)
1389 && fname[0] == '/'))
1390 retval = vim_strnsave(fname, len);
1391 else
1392 {
1393 new_len = len + 2; // +2 for '^' at start, NUL at end
1394 for (i = 0; i < len; i++)
1395 {
1396 if (fname[i] == '*' || fname[i] == '~')
1397 new_len++; // '*' needs to be replaced by ".*"
1398 // '~' needs to be replaced by "\~"
1399
1400 // Buffer names are like file names. "." should be literal
1401 if (context == EXPAND_BUFFERS && fname[i] == '.')
1402 new_len++; // "." becomes "\."
1403
1404 // Custom expansion takes care of special things, match
1405 // backslashes literally (perhaps also for other types?)
1406 if ((context == EXPAND_USER_DEFINED
1407 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1408 new_len++; // '\' becomes "\\"
1409 }
1410 retval = alloc(new_len);
1411 if (retval != NULL)
1412 {
1413 retval[0] = '^';
1414 j = 1;
1415 for (i = 0; i < len; i++, j++)
1416 {
1417 // Skip backslash. But why? At least keep it for custom
1418 // expansion.
1419 if (context != EXPAND_USER_DEFINED
1420 && context != EXPAND_USER_LIST
1421 && fname[i] == '\\'
1422 && ++i == len)
1423 break;
1424
1425 switch (fname[i])
1426 {
1427 case '*': retval[j++] = '.';
1428 break;
1429 case '~': retval[j++] = '\\';
1430 break;
1431 case '?': retval[j] = '.';
1432 continue;
1433 case '.': if (context == EXPAND_BUFFERS)
1434 retval[j++] = '\\';
1435 break;
1436 case '\\': if (context == EXPAND_USER_DEFINED
1437 || context == EXPAND_USER_LIST)
1438 retval[j++] = '\\';
1439 break;
1440 }
1441 retval[j] = fname[i];
1442 }
1443 retval[j] = NUL;
1444 }
1445 }
1446 }
1447 else
1448 {
1449 retval = alloc(len + 4);
1450 if (retval != NULL)
1451 {
1452 vim_strncpy(retval, fname, len);
1453
1454 // Don't add a star to *, ~, ~user, $var or `cmd`.
1455 // * would become **, which walks the whole tree.
1456 // ~ would be at the start of the file name, but not the tail.
1457 // $ could be anywhere in the tail.
1458 // ` could be anywhere in the file name.
1459 // When the name ends in '$' don't add a star, remove the '$'.
1460 tail = gettail(retval);
1461 ends_in_star = (len > 0 && retval[len - 1] == '*');
1462#ifndef BACKSLASH_IN_FILENAME
1463 for (i = len - 2; i >= 0; --i)
1464 {
1465 if (retval[i] != '\\')
1466 break;
1467 ends_in_star = !ends_in_star;
1468 }
1469#endif
1470 if ((*retval != '~' || tail != retval)
1471 && !ends_in_star
1472 && vim_strchr(tail, '$') == NULL
1473 && vim_strchr(retval, '`') == NULL)
1474 retval[len++] = '*';
1475 else if (len > 0 && retval[len - 1] == '$')
1476 --len;
1477 retval[len] = NUL;
1478 }
1479 }
1480 return retval;
1481}
1482
1483/*
1484 * Must parse the command line so far to work out what context we are in.
1485 * Completion can then be done based on that context.
1486 * This routine sets the variables:
1487 * xp->xp_pattern The start of the pattern to be expanded within
1488 * the command line (ends at the cursor).
1489 * xp->xp_context The type of thing to expand. Will be one of:
1490 *
1491 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1492 * the command line, like an unknown command. Caller
1493 * should beep.
1494 * EXPAND_NOTHING Unrecognised context for completion, use char like
1495 * a normal char, rather than for completion. eg
1496 * :s/^I/
1497 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1498 * it.
1499 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1500 * EXPAND_FILES After command with EX_XFILE set, or after setting
1501 * with P_EXPAND set. eg :e ^I, :w>>^I
1502 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001503 * when we know only directories are of interest.
1504 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001505 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1506 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1507 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1508 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1509 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1510 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1511 * EXPAND_EVENTS Complete event names
1512 * EXPAND_SYNTAX Complete :syntax command arguments
1513 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1514 * EXPAND_AUGROUP Complete autocommand group names
1515 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1516 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1517 * eg :unmap a^I , :cunab x^I
1518 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1519 * eg :call sub^I
1520 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1521 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1522 * names in expressions, eg :while s^I
1523 * EXPAND_ENV_VARS Complete environment variable names
1524 * EXPAND_USER Complete user names
1525 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001526 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001527set_expand_context(expand_T *xp)
1528{
1529 cmdline_info_T *ccline = get_cmdline_info();
1530
1531 // only expansion for ':', '>' and '=' command-lines
1532 if (ccline->cmdfirstc != ':'
1533#ifdef FEAT_EVAL
1534 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1535 && !ccline->input_fn
1536#endif
1537 )
1538 {
1539 xp->xp_context = EXPAND_NOTHING;
1540 return;
1541 }
1542 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1543}
1544
Bram Moolenaard0190392019-08-23 21:17:35 +02001545/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001546 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1547 * For user defined commands, the completion context is set in 'xp' and the
1548 * completion flags in 'complp'.
1549 *
1550 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001551 */
1552 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001553set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001554{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001555 char_u *p = NULL;
1556 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001557 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001558
1559 // Isolate the command and search for it in the command table.
1560 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001561 // - the 'k' command can directly be followed by any character, but do
1562 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1563 // find matches anywhere in the command name, do this only for command
1564 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001565 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001566 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001567 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001568 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001569 p = cmd + 1;
1570 }
1571 else
1572 {
1573 p = cmd;
1574 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1575 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001576 // A user command may contain digits.
1577 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1578 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001579 while (ASCII_ISALNUM(*p) || *p == '*')
1580 ++p;
1581 // for python 3.x: ":py3*" commands completion
1582 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1583 {
1584 ++p;
1585 while (ASCII_ISALPHA(*p) || *p == '*')
1586 ++p;
1587 }
1588 // check for non-alpha command
1589 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1590 ++p;
1591 len = (int)(p - cmd);
1592
1593 if (len == 0)
1594 {
1595 xp->xp_context = EXPAND_UNSUCCESSFUL;
1596 return NULL;
1597 }
1598
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001599 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001600
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001601 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001602 // Also when doing fuzzy expansion for non-shell commands, support
1603 // alphanumeric characters.
1604 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1605 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001606 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1607 ++p;
1608 }
1609
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001610 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001611 // character, complete the command name.
1612 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1613 return NULL;
1614
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001615 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001616 {
1617 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1618 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001619 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001620 p = cmd + 1;
1621 }
1622 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1623 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001624 eap->cmd = cmd;
1625 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001626 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001627 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001628 }
1629 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001630 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001631 {
1632 // Not still touching the command and it was an illegal one
1633 xp->xp_context = EXPAND_UNSUCCESSFUL;
1634 return NULL;
1635 }
1636
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001637 return p;
1638}
Bram Moolenaard0190392019-08-23 21:17:35 +02001639
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001640/*
1641 * Set the completion context for a command argument with wild card characters.
1642 */
1643 static void
1644set_context_for_wildcard_arg(
1645 exarg_T *eap,
1646 char_u *arg,
1647 int usefilter,
1648 expand_T *xp,
1649 int *complp)
1650{
1651 char_u *p;
1652 int c;
1653 int in_quote = FALSE;
1654 char_u *bow = NULL; // Beginning of word
1655 int len = 0;
1656
1657 // Allow spaces within back-quotes to count as part of the argument
1658 // being expanded.
1659 xp->xp_pattern = skipwhite(arg);
1660 p = xp->xp_pattern;
1661 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001662 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001663 if (has_mbyte)
1664 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001665 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 c = *p;
1667 if (c == '\\' && p[1] != NUL)
1668 ++p;
1669 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001670 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001671 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001672 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001673 xp->xp_pattern = p;
1674 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001675 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001676 in_quote = !in_quote;
1677 }
1678 // An argument can contain just about everything, except
1679 // characters that end the command and white space.
1680 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001681#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001682 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001683#endif
1684 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685 {
1686 len = 0; // avoid getting stuck when space is in 'isfname'
1687 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001688 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001689 if (has_mbyte)
1690 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001691 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001692 c = *p;
1693 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001694 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001695 if (has_mbyte)
1696 len = (*mb_ptr2len)(p);
1697 else
1698 len = 1;
1699 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001700 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001701 if (in_quote)
1702 bow = p;
1703 else
1704 xp->xp_pattern = p;
1705 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001706 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001707 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001708 }
1709
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001710 // If we are still inside the quotes, and we passed a space, just
1711 // expand from there.
1712 if (bow != NULL && in_quote)
1713 xp->xp_pattern = bow;
1714 xp->xp_context = EXPAND_FILES;
1715
1716 // For a shell command more chars need to be escaped.
1717 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1718 {
1719#ifndef BACKSLASH_IN_FILENAME
1720 xp->xp_shell = TRUE;
1721#endif
1722 // When still after the command name expand executables.
1723 if (xp->xp_pattern == skipwhite(arg))
1724 xp->xp_context = EXPAND_SHELLCMD;
1725 }
1726
1727 // Check for environment variable.
1728 if (*xp->xp_pattern == '$')
1729 {
1730 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1731 if (!vim_isIDc(*p))
1732 break;
1733 if (*p == NUL)
1734 {
1735 xp->xp_context = EXPAND_ENV_VARS;
1736 ++xp->xp_pattern;
1737 // Avoid that the assignment uses EXPAND_FILES again.
1738 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1739 *complp = EXPAND_ENV_VARS;
1740 }
1741 }
1742 // Check for user names.
1743 if (*xp->xp_pattern == '~')
1744 {
1745 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1746 ;
1747 // Complete ~user only if it partially matches a user name.
1748 // A full match ~user<Tab> will be replaced by user's home
1749 // directory i.e. something like ~user<Tab> -> /home/user/
1750 if (*p == NUL && p > xp->xp_pattern + 1
1751 && match_user(xp->xp_pattern + 1) >= 1)
1752 {
1753 xp->xp_context = EXPAND_USER;
1754 ++xp->xp_pattern;
1755 }
1756 }
1757}
1758
1759/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001760 * Set the completion context for the :filter command. Returns a pointer to the
1761 * next command after the :filter command.
1762 */
1763 static char_u *
1764set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1765{
1766 if (*arg != NUL)
1767 arg = skip_vimgrep_pat(arg, NULL, NULL);
1768 if (arg == NULL || *arg == NUL)
1769 {
1770 xp->xp_context = EXPAND_NOTHING;
1771 return NULL;
1772 }
1773 return skipwhite(arg);
1774}
1775
1776#ifdef FEAT_SEARCH_EXTRA
1777/*
1778 * Set the completion context for the :match command. Returns a pointer to the
1779 * next command after the :match command.
1780 */
1781 static char_u *
1782set_context_in_match_cmd(expand_T *xp, char_u *arg)
1783{
1784 if (*arg == NUL || !ends_excmd(*arg))
1785 {
1786 // also complete "None"
1787 set_context_in_echohl_cmd(xp, arg);
1788 arg = skipwhite(skiptowhite(arg));
1789 if (*arg != NUL)
1790 {
1791 xp->xp_context = EXPAND_NOTHING;
1792 arg = skip_regexp(arg + 1, *arg, magic_isset());
1793 }
1794 }
1795 return find_nextcmd(arg);
1796}
1797#endif
1798
1799/*
1800 * Returns a pointer to the next command after a :global or a :v command.
1801 * Returns NULL if there is no next command.
1802 */
1803 static char_u *
1804find_cmd_after_global_cmd(char_u *arg)
1805{
1806 int delim;
1807
1808 delim = *arg; // get the delimiter
1809 if (delim)
1810 ++arg; // skip delimiter if there is one
1811
1812 while (arg[0] != NUL && arg[0] != delim)
1813 {
1814 if (arg[0] == '\\' && arg[1] != NUL)
1815 ++arg;
1816 ++arg;
1817 }
1818 if (arg[0] != NUL)
1819 return arg + 1;
1820
1821 return NULL;
1822}
1823
1824/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001825 * Returns a pointer to the next command after a :substitute or a :& command.
1826 * Returns NULL if there is no next command.
1827 */
1828 static char_u *
1829find_cmd_after_substitute_cmd(char_u *arg)
1830{
1831 int delim;
1832
1833 delim = *arg;
1834 if (delim)
1835 {
1836 // skip "from" part
1837 ++arg;
1838 arg = skip_regexp(arg, delim, magic_isset());
1839
1840 if (arg[0] != NUL && arg[0] == delim)
1841 {
1842 // skip "to" part
1843 ++arg;
1844 while (arg[0] != NUL && arg[0] != delim)
1845 {
1846 if (arg[0] == '\\' && arg[1] != NUL)
1847 ++arg;
1848 ++arg;
1849 }
1850 if (arg[0] != NUL) // skip delimiter
1851 ++arg;
1852 }
1853 }
1854 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1855 ++arg;
1856 if (arg[0] != NUL)
1857 return arg;
1858
1859 return NULL;
1860}
1861
1862/*
1863 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1864 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1865 * Returns NULL if there is no next command.
1866 */
1867 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001868find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001869{
1870 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001871 if (*arg != '/')
1872 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001873
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001874 // Match regexp, not just whole words
1875 for (++arg; *arg && *arg != '/'; arg++)
1876 if (*arg == '\\' && arg[1] != NUL)
1877 arg++;
1878 if (*arg)
1879 {
1880 arg = skipwhite(arg + 1);
1881
1882 // Check for trailing illegal characters
1883 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1884 xp->xp_context = EXPAND_NOTHING;
1885 else
1886 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001887 }
1888
1889 return NULL;
1890}
1891
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001892#ifdef FEAT_EVAL
1893/*
1894 * Set the completion context for the :unlet command. Always returns NULL.
1895 */
1896 static char_u *
1897set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1898{
1899 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1900 arg = xp->xp_pattern + 1;
1901
1902 xp->xp_context = EXPAND_USER_VARS;
1903 xp->xp_pattern = arg;
1904
1905 if (*xp->xp_pattern == '$')
1906 {
1907 xp->xp_context = EXPAND_ENV_VARS;
1908 ++xp->xp_pattern;
1909 }
1910
1911 return NULL;
1912}
1913#endif
1914
1915#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1916/*
1917 * Set the completion context for the :language command. Always returns NULL.
1918 */
1919 static char_u *
1920set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1921{
1922 char_u *p;
1923
1924 p = skiptowhite(arg);
1925 if (*p == NUL)
1926 {
1927 xp->xp_context = EXPAND_LANGUAGE;
1928 xp->xp_pattern = arg;
1929 }
1930 else
1931 {
1932 if ( STRNCMP(arg, "messages", p - arg) == 0
1933 || STRNCMP(arg, "ctype", p - arg) == 0
1934 || STRNCMP(arg, "time", p - arg) == 0
1935 || STRNCMP(arg, "collate", p - arg) == 0)
1936 {
1937 xp->xp_context = EXPAND_LOCALES;
1938 xp->xp_pattern = skipwhite(p);
1939 }
1940 else
1941 xp->xp_context = EXPAND_NOTHING;
1942 }
1943
1944 return NULL;
1945}
1946#endif
1947
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001948#ifdef FEAT_EVAL
1949static enum
1950{
1951 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001952 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1953 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001954} breakpt_expand_what;
1955
1956/*
1957 * Set the completion context for the :breakadd command. Always returns NULL.
1958 */
1959 static char_u *
1960set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1961{
1962 char_u *p;
1963 char_u *subcmd_start;
1964
1965 xp->xp_context = EXPAND_BREAKPOINT;
1966 xp->xp_pattern = arg;
1967
1968 if (cmdidx == CMD_breakadd)
1969 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001970 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001971 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001972 else
1973 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001974
1975 p = skipwhite(arg);
1976 if (*p == NUL)
1977 return NULL;
1978 subcmd_start = p;
1979
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001980 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001981 {
1982 // :breakadd file [lnum] <filename>
1983 // :breakadd func [lnum] <funcname>
1984 p += 4;
1985 p = skipwhite(p);
1986
1987 // skip line number (if specified)
1988 if (VIM_ISDIGIT(*p))
1989 {
1990 p = skipdigits(p);
1991 if (*p != ' ')
1992 {
1993 xp->xp_context = EXPAND_NOTHING;
1994 return NULL;
1995 }
1996 p = skipwhite(p);
1997 }
1998 if (STRNCMP("file", subcmd_start, 4) == 0)
1999 xp->xp_context = EXPAND_FILES;
2000 else
2001 xp->xp_context = EXPAND_USER_FUNC;
2002 xp->xp_pattern = p;
2003 }
2004 else if (STRNCMP("expr ", p, 5) == 0)
2005 {
2006 // :breakadd expr <expression>
2007 xp->xp_context = EXPAND_EXPRESSION;
2008 xp->xp_pattern = skipwhite(p + 5);
2009 }
2010
2011 return NULL;
2012}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002013
2014 static char_u *
2015set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2016{
2017 char_u *p;
2018
2019 xp->xp_context = EXPAND_NOTHING;
2020 xp->xp_pattern = NULL;
2021
2022 p = skipwhite(arg);
2023 if (VIM_ISDIGIT(*p))
2024 return NULL;
2025
2026 xp->xp_context = EXPAND_SCRIPTNAMES;
2027 xp->xp_pattern = p;
2028
2029 return NULL;
2030}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002031#endif
2032
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002033/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002034 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2035 * The argument to the command is 'arg' and the argument flags is 'argt'.
2036 * For user-defined commands and for environment variables, 'compl' has the
2037 * completion type.
2038 * Returns a pointer to the next command. Returns NULL if there is no next
2039 * command.
2040 */
2041 static char_u *
2042set_context_by_cmdname(
2043 char_u *cmd,
2044 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002045 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002046 char_u *arg,
2047 long argt,
2048 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002049 int forceit)
2050{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002051 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002052 {
2053 case CMD_find:
2054 case CMD_sfind:
2055 case CMD_tabfind:
2056 if (xp->xp_context == EXPAND_FILES)
2057 xp->xp_context = EXPAND_FILES_IN_PATH;
2058 break;
2059 case CMD_cd:
2060 case CMD_chdir:
2061 case CMD_tcd:
2062 case CMD_tchdir:
2063 case CMD_lcd:
2064 case CMD_lchdir:
2065 if (xp->xp_context == EXPAND_FILES)
2066 xp->xp_context = EXPAND_DIRECTORIES;
2067 break;
2068 case CMD_help:
2069 xp->xp_context = EXPAND_HELP;
2070 xp->xp_pattern = arg;
2071 break;
2072
2073 // Command modifiers: return the argument.
2074 // Also for commands with an argument that is a command.
2075 case CMD_aboveleft:
2076 case CMD_argdo:
2077 case CMD_belowright:
2078 case CMD_botright:
2079 case CMD_browse:
2080 case CMD_bufdo:
2081 case CMD_cdo:
2082 case CMD_cfdo:
2083 case CMD_confirm:
2084 case CMD_debug:
2085 case CMD_folddoclosed:
2086 case CMD_folddoopen:
2087 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002088 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002089 case CMD_keepalt:
2090 case CMD_keepjumps:
2091 case CMD_keepmarks:
2092 case CMD_keeppatterns:
2093 case CMD_ldo:
2094 case CMD_leftabove:
2095 case CMD_lfdo:
2096 case CMD_lockmarks:
2097 case CMD_noautocmd:
2098 case CMD_noswapfile:
2099 case CMD_rightbelow:
2100 case CMD_sandbox:
2101 case CMD_silent:
2102 case CMD_tab:
2103 case CMD_tabdo:
2104 case CMD_topleft:
2105 case CMD_verbose:
2106 case CMD_vertical:
2107 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002108 case CMD_vim9cmd:
2109 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002110 return arg;
2111
2112 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002113 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002114
2115#ifdef FEAT_SEARCH_EXTRA
2116 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002117 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002118#endif
2119
2120 // All completion for the +cmdline_compl feature goes here.
2121
2122 case CMD_command:
2123 return set_context_in_user_cmd(xp, arg);
2124
2125 case CMD_delcommand:
2126 xp->xp_context = EXPAND_USER_COMMANDS;
2127 xp->xp_pattern = arg;
2128 break;
2129
2130 case CMD_global:
2131 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002132 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002133 case CMD_and:
2134 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002135 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002136 case CMD_isearch:
2137 case CMD_dsearch:
2138 case CMD_ilist:
2139 case CMD_dlist:
2140 case CMD_ijump:
2141 case CMD_psearch:
2142 case CMD_djump:
2143 case CMD_isplit:
2144 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002145 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002146 case CMD_autocmd:
2147 return set_context_in_autocmd(xp, arg, FALSE);
2148 case CMD_doautocmd:
2149 case CMD_doautoall:
2150 return set_context_in_autocmd(xp, arg, TRUE);
2151 case CMD_set:
2152 set_context_in_set_cmd(xp, arg, 0);
2153 break;
2154 case CMD_setglobal:
2155 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2156 break;
2157 case CMD_setlocal:
2158 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2159 break;
2160 case CMD_tag:
2161 case CMD_stag:
2162 case CMD_ptag:
2163 case CMD_ltag:
2164 case CMD_tselect:
2165 case CMD_stselect:
2166 case CMD_ptselect:
2167 case CMD_tjump:
2168 case CMD_stjump:
2169 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002170 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002171 xp->xp_context = EXPAND_TAGS_LISTFILES;
2172 else
2173 xp->xp_context = EXPAND_TAGS;
2174 xp->xp_pattern = arg;
2175 break;
2176 case CMD_augroup:
2177 xp->xp_context = EXPAND_AUGROUP;
2178 xp->xp_pattern = arg;
2179 break;
2180#ifdef FEAT_SYN_HL
2181 case CMD_syntax:
2182 set_context_in_syntax_cmd(xp, arg);
2183 break;
2184#endif
2185#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002186 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002187 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002188 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002189 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002190 case CMD_if:
2191 case CMD_elseif:
2192 case CMD_while:
2193 case CMD_for:
2194 case CMD_echo:
2195 case CMD_echon:
2196 case CMD_execute:
2197 case CMD_echomsg:
2198 case CMD_echoerr:
2199 case CMD_call:
2200 case CMD_return:
2201 case CMD_cexpr:
2202 case CMD_caddexpr:
2203 case CMD_cgetexpr:
2204 case CMD_lexpr:
2205 case CMD_laddexpr:
2206 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002207 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002208 break;
2209
2210 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002211 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002212 case CMD_function:
2213 case CMD_delfunction:
2214 xp->xp_context = EXPAND_USER_FUNC;
2215 xp->xp_pattern = arg;
2216 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002217 case CMD_disassemble:
2218 set_context_in_disassemble_cmd(xp, arg);
2219 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002220
2221 case CMD_echohl:
2222 set_context_in_echohl_cmd(xp, arg);
2223 break;
2224#endif
2225 case CMD_highlight:
2226 set_context_in_highlight_cmd(xp, arg);
2227 break;
2228#ifdef FEAT_CSCOPE
2229 case CMD_cscope:
2230 case CMD_lcscope:
2231 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002232 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002233 break;
2234#endif
2235#ifdef FEAT_SIGNS
2236 case CMD_sign:
2237 set_context_in_sign_cmd(xp, arg);
2238 break;
2239#endif
2240 case CMD_bdelete:
2241 case CMD_bwipeout:
2242 case CMD_bunload:
2243 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2244 arg = xp->xp_pattern + 1;
2245 // FALLTHROUGH
2246 case CMD_buffer:
2247 case CMD_sbuffer:
2248 case CMD_checktime:
2249 xp->xp_context = EXPAND_BUFFERS;
2250 xp->xp_pattern = arg;
2251 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002252#ifdef FEAT_DIFF
2253 case CMD_diffget:
2254 case CMD_diffput:
2255 // If current buffer is in diff mode, complete buffer names
2256 // which are in diff mode, and different than current buffer.
2257 xp->xp_context = EXPAND_DIFF_BUFFERS;
2258 xp->xp_pattern = arg;
2259 break;
2260#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002261 case CMD_USER:
2262 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002263 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2264 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002265
2266 case CMD_map: case CMD_noremap:
2267 case CMD_nmap: case CMD_nnoremap:
2268 case CMD_vmap: case CMD_vnoremap:
2269 case CMD_omap: case CMD_onoremap:
2270 case CMD_imap: case CMD_inoremap:
2271 case CMD_cmap: case CMD_cnoremap:
2272 case CMD_lmap: case CMD_lnoremap:
2273 case CMD_smap: case CMD_snoremap:
2274 case CMD_tmap: case CMD_tnoremap:
2275 case CMD_xmap: case CMD_xnoremap:
2276 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002277 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002278 case CMD_unmap:
2279 case CMD_nunmap:
2280 case CMD_vunmap:
2281 case CMD_ounmap:
2282 case CMD_iunmap:
2283 case CMD_cunmap:
2284 case CMD_lunmap:
2285 case CMD_sunmap:
2286 case CMD_tunmap:
2287 case CMD_xunmap:
2288 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002289 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002290 case CMD_mapclear:
2291 case CMD_nmapclear:
2292 case CMD_vmapclear:
2293 case CMD_omapclear:
2294 case CMD_imapclear:
2295 case CMD_cmapclear:
2296 case CMD_lmapclear:
2297 case CMD_smapclear:
2298 case CMD_tmapclear:
2299 case CMD_xmapclear:
2300 xp->xp_context = EXPAND_MAPCLEAR;
2301 xp->xp_pattern = arg;
2302 break;
2303
2304 case CMD_abbreviate: case CMD_noreabbrev:
2305 case CMD_cabbrev: case CMD_cnoreabbrev:
2306 case CMD_iabbrev: case CMD_inoreabbrev:
2307 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002308 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002309 case CMD_unabbreviate:
2310 case CMD_cunabbrev:
2311 case CMD_iunabbrev:
2312 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002313 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002314#ifdef FEAT_MENU
2315 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2316 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2317 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2318 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2319 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2320 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2321 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2322 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2323 case CMD_tmenu: case CMD_tunmenu:
2324 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2325 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2326#endif
2327
2328 case CMD_colorscheme:
2329 xp->xp_context = EXPAND_COLORS;
2330 xp->xp_pattern = arg;
2331 break;
2332
2333 case CMD_compiler:
2334 xp->xp_context = EXPAND_COMPILER;
2335 xp->xp_pattern = arg;
2336 break;
2337
2338 case CMD_ownsyntax:
2339 xp->xp_context = EXPAND_OWNSYNTAX;
2340 xp->xp_pattern = arg;
2341 break;
2342
2343 case CMD_setfiletype:
2344 xp->xp_context = EXPAND_FILETYPE;
2345 xp->xp_pattern = arg;
2346 break;
2347
2348 case CMD_packadd:
2349 xp->xp_context = EXPAND_PACKADD;
2350 xp->xp_pattern = arg;
2351 break;
2352
zeertzjqb0d45ec2023-01-25 15:04:22 +00002353 case CMD_runtime:
2354 set_context_in_runtime_cmd(xp, arg);
2355 break;
2356
Bram Moolenaard0190392019-08-23 21:17:35 +02002357#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2358 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002359 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002360#endif
2361#if defined(FEAT_PROFILE)
2362 case CMD_profile:
2363 set_context_in_profile_cmd(xp, arg);
2364 break;
2365#endif
2366 case CMD_behave:
2367 xp->xp_context = EXPAND_BEHAVE;
2368 xp->xp_pattern = arg;
2369 break;
2370
2371 case CMD_messages:
2372 xp->xp_context = EXPAND_MESSAGES;
2373 xp->xp_pattern = arg;
2374 break;
2375
2376 case CMD_history:
2377 xp->xp_context = EXPAND_HISTORY;
2378 xp->xp_pattern = arg;
2379 break;
2380#if defined(FEAT_PROFILE)
2381 case CMD_syntime:
2382 xp->xp_context = EXPAND_SYNTIME;
2383 xp->xp_pattern = arg;
2384 break;
2385#endif
2386
2387 case CMD_argdelete:
2388 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2389 arg = xp->xp_pattern + 1;
2390 xp->xp_context = EXPAND_ARGLIST;
2391 xp->xp_pattern = arg;
2392 break;
2393
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002394#ifdef FEAT_EVAL
2395 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002396 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002397 case CMD_breakdel:
2398 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002399
2400 case CMD_scriptnames:
2401 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002402#endif
2403
Bram Moolenaard0190392019-08-23 21:17:35 +02002404 default:
2405 break;
2406 }
2407 return NULL;
2408}
2409
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002410/*
2411 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2412 * we don't need/want deleted. Maybe this could be done better if we didn't
2413 * repeat all this stuff. The only problem is that they may not stay
2414 * perfectly compatible with each other, but then the command line syntax
2415 * probably won't change that much -- webb.
2416 */
2417 static char_u *
2418set_one_cmd_context(
2419 expand_T *xp,
2420 char_u *buff) // buffer for command string
2421{
2422 char_u *p;
2423 char_u *cmd, *arg;
2424 int len = 0;
2425 exarg_T ea;
2426 int compl = EXPAND_NOTHING;
2427 int forceit = FALSE;
2428 int usefilter = FALSE; // filter instead of file name
2429
2430 ExpandInit(xp);
2431 xp->xp_pattern = buff;
2432 xp->xp_line = buff;
2433 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2434 ea.argt = 0;
2435
2436 // 1. skip comment lines and leading space, colons or bars
2437 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2438 ;
2439 xp->xp_pattern = cmd;
2440
2441 if (*cmd == NUL)
2442 return NULL;
2443 if (*cmd == '"') // ignore comment lines
2444 {
2445 xp->xp_context = EXPAND_NOTHING;
2446 return NULL;
2447 }
2448
2449 // 3. Skip over the range to find the command.
2450 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2451 xp->xp_pattern = cmd;
2452 if (*cmd == NUL)
2453 return NULL;
2454 if (*cmd == '"')
2455 {
2456 xp->xp_context = EXPAND_NOTHING;
2457 return NULL;
2458 }
2459
2460 if (*cmd == '|' || *cmd == '\n')
2461 return cmd + 1; // There's another command
2462
2463 // Get the command index.
2464 p = set_cmd_index(cmd, &ea, xp, &compl);
2465 if (p == NULL)
2466 return NULL;
2467
2468 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2469
2470 if (*p == '!') // forced commands
2471 {
2472 forceit = TRUE;
2473 ++p;
2474 }
2475
2476 // 6. parse arguments
2477 if (!IS_USER_CMDIDX(ea.cmdidx))
2478 ea.argt = excmd_get_argt(ea.cmdidx);
2479
2480 arg = skipwhite(p);
2481
2482 // Skip over ++argopt argument
2483 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2484 {
2485 p = arg;
2486 while (*p && !vim_isspace(*p))
2487 MB_PTR_ADV(p);
2488 arg = skipwhite(p);
2489 }
2490
2491 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2492 {
2493 if (*arg == '>') // append
2494 {
2495 if (*++arg == '>')
2496 ++arg;
2497 arg = skipwhite(arg);
2498 }
2499 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2500 {
2501 ++arg;
2502 usefilter = TRUE;
2503 }
2504 }
2505
2506 if (ea.cmdidx == CMD_read)
2507 {
2508 usefilter = forceit; // :r! filter if forced
2509 if (*arg == '!') // :r !filter
2510 {
2511 ++arg;
2512 usefilter = TRUE;
2513 }
2514 }
2515
2516 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2517 {
2518 while (*arg == *cmd) // allow any number of '>' or '<'
2519 ++arg;
2520 arg = skipwhite(arg);
2521 }
2522
2523 // Does command allow "+command"?
2524 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2525 {
2526 // Check if we're in the +command
2527 p = arg + 1;
2528 arg = skip_cmd_arg(arg, FALSE);
2529
2530 // Still touching the command after '+'?
2531 if (*arg == NUL)
2532 return p;
2533
2534 // Skip space(s) after +command to get to the real argument
2535 arg = skipwhite(arg);
2536 }
2537
2538
2539 // Check for '|' to separate commands and '"' to start comments.
2540 // Don't do this for ":read !cmd" and ":write !cmd".
2541 if ((ea.argt & EX_TRLBAR) && !usefilter)
2542 {
2543 p = arg;
2544 // ":redir @" is not the start of a comment
2545 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2546 p += 2;
2547 while (*p)
2548 {
2549 if (*p == Ctrl_V)
2550 {
2551 if (p[1] != NUL)
2552 ++p;
2553 }
2554 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2555 || *p == '|' || *p == '\n')
2556 {
2557 if (*(p - 1) != '\\')
2558 {
2559 if (*p == '|' || *p == '\n')
2560 return p + 1;
2561 return NULL; // It's a comment
2562 }
2563 }
2564 MB_PTR_ADV(p);
2565 }
2566 }
2567
2568 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2569 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2570 // no arguments allowed but there is something
2571 return NULL;
2572
2573 // Find start of last argument (argument just before cursor):
2574 p = buff;
2575 xp->xp_pattern = p;
2576 len = (int)STRLEN(buff);
2577 while (*p && p < buff + len)
2578 {
2579 if (*p == ' ' || *p == TAB)
2580 {
2581 // argument starts after a space
2582 xp->xp_pattern = ++p;
2583 }
2584 else
2585 {
2586 if (*p == '\\' && *(p + 1) != NUL)
2587 ++p; // skip over escaped character
2588 MB_PTR_ADV(p);
2589 }
2590 }
2591
2592 if (ea.argt & EX_XFILE)
2593 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2594
2595 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002596 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002597 forceit);
2598}
2599
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002600/*
2601 * Set the completion context in 'xp' for command 'str'
2602 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002603 void
2604set_cmd_context(
2605 expand_T *xp,
2606 char_u *str, // start of command line
2607 int len, // length of command line (excl. NUL)
2608 int col, // position of cursor
2609 int use_ccline UNUSED) // use ccline for info
2610{
2611#ifdef FEAT_EVAL
2612 cmdline_info_T *ccline = get_cmdline_info();
2613#endif
2614 int old_char = NUL;
2615 char_u *nextcomm;
2616
2617 // Avoid a UMR warning from Purify, only save the character if it has been
2618 // written before.
2619 if (col < len)
2620 old_char = str[col];
2621 str[col] = NUL;
2622 nextcomm = str;
2623
2624#ifdef FEAT_EVAL
2625 if (use_ccline && ccline->cmdfirstc == '=')
2626 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002627 // pass CMD_SIZE because there is no real command
2628 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002629 }
2630 else if (use_ccline && ccline->input_fn)
2631 {
2632 xp->xp_context = ccline->xp_context;
2633 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002634 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002635 }
2636 else
2637#endif
2638 while (nextcomm != NULL)
2639 nextcomm = set_one_cmd_context(xp, nextcomm);
2640
2641 // Store the string here so that call_user_expand_func() can get to them
2642 // easily.
2643 xp->xp_line = str;
2644 xp->xp_col = col;
2645
2646 str[col] = old_char;
2647}
2648
2649/*
2650 * Expand the command line "str" from context "xp".
2651 * "xp" must have been set by set_cmd_context().
2652 * xp->xp_pattern points into "str", to where the text that is to be expanded
2653 * starts.
2654 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2655 * cursor.
2656 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2657 * key that triggered expansion literally.
2658 * Returns EXPAND_OK otherwise.
2659 */
2660 int
2661expand_cmdline(
2662 expand_T *xp,
2663 char_u *str, // start of command line
2664 int col, // position of cursor
2665 int *matchcount, // return: nr of matches
2666 char_u ***matches) // return: array of pointers to matches
2667{
2668 char_u *file_str = NULL;
2669 int options = WILD_ADD_SLASH|WILD_SILENT;
2670
2671 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2672 {
2673 beep_flush();
2674 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2675 }
2676 if (xp->xp_context == EXPAND_NOTHING)
2677 {
2678 // Caller can use the character as a normal char instead
2679 return EXPAND_NOTHING;
2680 }
2681
2682 // add star to file name, or convert to regexp if not exp. files.
2683 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002684 if (cmdline_fuzzy_completion_supported(xp))
2685 // If fuzzy matching, don't modify the search string
2686 file_str = vim_strsave(xp->xp_pattern);
2687 else
2688 {
2689 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2690 if (file_str == NULL)
2691 return EXPAND_UNSUCCESSFUL;
2692 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002693
2694 if (p_wic)
2695 options += WILD_ICASE;
2696
2697 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002698 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002699 {
2700 *matchcount = 0;
2701 *matches = NULL;
2702 }
2703 vim_free(file_str);
2704
2705 return EXPAND_OK;
2706}
2707
Bram Moolenaar66b51422019-08-18 21:44:12 +02002708/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002709 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002710 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002711 */
2712 static int
2713expand_files_and_dirs(
2714 expand_T *xp,
2715 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002716 char_u ***matches,
2717 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002718 int flags,
2719 int options)
2720{
2721 int free_pat = FALSE;
2722 int i;
2723 int ret;
2724
2725 // for ":set path=" and ":set tags=" halve backslashes for escaped
2726 // space
2727 if (xp->xp_backslash != XP_BS_NONE)
2728 {
2729 free_pat = TRUE;
2730 pat = vim_strsave(pat);
2731 for (i = 0; pat[i]; ++i)
2732 if (pat[i] == '\\')
2733 {
2734 if (xp->xp_backslash == XP_BS_THREE
2735 && pat[i + 1] == '\\'
2736 && pat[i + 2] == '\\'
2737 && pat[i + 3] == ' ')
2738 STRMOVE(pat + i, pat + i + 3);
2739 if (xp->xp_backslash == XP_BS_ONE
2740 && pat[i + 1] == ' ')
2741 STRMOVE(pat + i, pat + i + 1);
2742 }
2743 }
2744
2745 if (xp->xp_context == EXPAND_FILES)
2746 flags |= EW_FILE;
2747 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2748 flags |= (EW_FILE | EW_PATH);
2749 else
2750 flags = (flags | EW_DIR) & ~EW_FILE;
2751 if (options & WILD_ICASE)
2752 flags |= EW_ICASE;
2753
2754 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002755 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002756 if (free_pat)
2757 vim_free(pat);
2758#ifdef BACKSLASH_IN_FILENAME
2759 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2760 {
2761 int j;
2762
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002763 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002764 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002765 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002766
2767 while (*ptr != NUL)
2768 {
2769 if (p_csl[0] == 's' && *ptr == '\\')
2770 *ptr = '/';
2771 else if (p_csl[0] == 'b' && *ptr == '/')
2772 *ptr = '\\';
2773 ptr += (*mb_ptr2len)(ptr);
2774 }
2775 }
2776 }
2777#endif
2778 return ret;
2779}
2780
2781/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002782 * Function given to ExpandGeneric() to obtain the possible arguments of the
2783 * ":behave {mswin,xterm}" command.
2784 */
2785 static char_u *
2786get_behave_arg(expand_T *xp UNUSED, int idx)
2787{
2788 if (idx == 0)
2789 return (char_u *)"mswin";
2790 if (idx == 1)
2791 return (char_u *)"xterm";
2792 return NULL;
2793}
2794
K.Takata161b6ac2022-11-14 15:31:07 +00002795#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002796/*
2797 * Function given to ExpandGeneric() to obtain the possible arguments of the
2798 * ":breakadd {expr, file, func, here}" command.
2799 * ":breakdel {func, file, here}" command.
2800 */
2801 static char_u *
2802get_breakadd_arg(expand_T *xp UNUSED, int idx)
2803{
2804 char *opts[] = {"expr", "file", "func", "here"};
2805
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002806 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002807 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002808 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002809 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2810 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002811 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2812 {
2813 // breakdel {func, file, here}
2814 if (idx <= 2)
2815 return (char_u *)opts[idx + 1];
2816 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002817 else
2818 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002819 // profdel {func, file}
2820 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002821 return (char_u *)opts[idx + 1];
2822 }
2823 }
2824 return NULL;
2825}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002826
2827/*
2828 * Function given to ExpandGeneric() to obtain the possible arguments for the
2829 * ":scriptnames" command.
2830 */
2831 static char_u *
2832get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2833{
2834 scriptitem_T *si;
2835
2836 if (!SCRIPT_ID_VALID(idx + 1))
2837 return NULL;
2838
2839 si = SCRIPT_ITEM(idx + 1);
2840 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2841 return NameBuff;
2842}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002843#endif
2844
Bram Moolenaard0190392019-08-23 21:17:35 +02002845/*
2846 * Function given to ExpandGeneric() to obtain the possible arguments of the
2847 * ":messages {clear}" command.
2848 */
2849 static char_u *
2850get_messages_arg(expand_T *xp UNUSED, int idx)
2851{
2852 if (idx == 0)
2853 return (char_u *)"clear";
2854 return NULL;
2855}
2856
2857 static char_u *
2858get_mapclear_arg(expand_T *xp UNUSED, int idx)
2859{
2860 if (idx == 0)
2861 return (char_u *)"<buffer>";
2862 return NULL;
2863}
2864
2865/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002866 * Do the expansion based on xp->xp_context and 'rmp'.
2867 */
2868 static int
2869ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002870 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002871 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002872 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002873 char_u ***matches,
2874 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002875{
2876 static struct expgen
2877 {
2878 int context;
2879 char_u *((*func)(expand_T *, int));
2880 int ic;
2881 int escaped;
2882 } tab[] =
2883 {
2884 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2885 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2886 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2887 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2888 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2889 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2890 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2891 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2892 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2893 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002894#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002895 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2896 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2897 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2898 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2899 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002900#endif
2901#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002902 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2903 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002904#endif
2905#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002906 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002907#endif
2908#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002909 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002910#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002911 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2912 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2913 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002914#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002915 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002916#endif
2917#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002918 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002919#endif
2920#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002921 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002922#endif
2923#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002924 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2925 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002926#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002927 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2928 {EXPAND_USER, get_users, TRUE, FALSE},
2929 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002930#ifdef FEAT_EVAL
2931 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002932 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002933#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002934 };
2935 int i;
2936 int ret = FAIL;
2937
2938 // Find a context in the table and call the ExpandGeneric() with the
2939 // right function to do the expansion.
2940 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2941 {
2942 if (xp->xp_context == tab[i].context)
2943 {
2944 if (tab[i].ic)
2945 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002946 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2947 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002948 break;
2949 }
2950 }
2951
2952 return ret;
2953}
2954
2955/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002956 * Map wild expand options to flags for expand_wildcards()
2957 */
2958 static int
2959map_wildopts_to_ewflags(int options)
2960{
2961 int flags;
2962
2963 flags = EW_DIR; // include directories
2964 if (options & WILD_LIST_NOTFOUND)
2965 flags |= EW_NOTFOUND;
2966 if (options & WILD_ADD_SLASH)
2967 flags |= EW_ADDSLASH;
2968 if (options & WILD_KEEP_ALL)
2969 flags |= EW_KEEPALL;
2970 if (options & WILD_SILENT)
2971 flags |= EW_SILENT;
2972 if (options & WILD_NOERROR)
2973 flags |= EW_NOERROR;
2974 if (options & WILD_ALLLINKS)
2975 flags |= EW_ALLLINKS;
2976
2977 return flags;
2978}
2979
2980/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002981 * Do the expansion based on xp->xp_context and "pat".
2982 */
2983 static int
2984ExpandFromContext(
2985 expand_T *xp,
2986 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002987 char_u ***matches,
2988 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002989 int options) // WILD_ flags
2990{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002991 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002992 int ret;
2993 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002994 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002995 int fuzzy = cmdline_fuzzy_complete(pat)
2996 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002997
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002998 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002999
3000 if (xp->xp_context == EXPAND_FILES
3001 || xp->xp_context == EXPAND_DIRECTORIES
3002 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003003 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3004 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003005
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003006 *matches = (char_u **)"";
3007 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003008 if (xp->xp_context == EXPAND_HELP)
3009 {
3010 // With an empty argument we would get all the help tags, which is
3011 // very slow. Get matches for "help" instead.
3012 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003013 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003014 {
3015#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003016 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003017#endif
3018 return OK;
3019 }
3020 return FAIL;
3021 }
3022
Bram Moolenaar66b51422019-08-18 21:44:12 +02003023 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003024 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003025 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003026 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003027 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003028 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003029#ifdef FEAT_DIFF
3030 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003031 return ExpandBufnames(pat, numMatches, matches,
3032 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003033#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003034 if (xp->xp_context == EXPAND_TAGS
3035 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003036 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3037 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003038 if (xp->xp_context == EXPAND_COLORS)
3039 {
3040 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003041 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003042 directories);
3043 }
3044 if (xp->xp_context == EXPAND_COMPILER)
3045 {
3046 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003047 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003048 }
3049 if (xp->xp_context == EXPAND_OWNSYNTAX)
3050 {
3051 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003052 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003053 }
3054 if (xp->xp_context == EXPAND_FILETYPE)
3055 {
3056 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003057 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003058 }
K.Takata161b6ac2022-11-14 15:31:07 +00003059#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003060 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003061 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003062#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003063 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003064 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003065 if (xp->xp_context == EXPAND_RUNTIME)
3066 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003067
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003068 // When expanding a function name starting with s:, match the <SNR>nr_
3069 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003070 if ((xp->xp_context == EXPAND_USER_FUNC
3071 || xp->xp_context == EXPAND_DISASSEMBLE)
3072 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003073 {
3074 int len = (int)STRLEN(pat) + 20;
3075
3076 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003077 if (tofree == NULL)
3078 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003079 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003080 pat = tofree;
3081 }
3082
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003083 if (!fuzzy)
3084 {
3085 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3086 if (regmatch.regprog == NULL)
3087 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003088
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003089 // set ignore-case according to p_ic, p_scs and pat
3090 regmatch.rm_ic = ignorecase(pat);
3091 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003092
3093 if (xp->xp_context == EXPAND_SETTINGS
3094 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003095 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003096 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003097 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003098#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003099 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003100 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003101#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003103 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003105 if (!fuzzy)
3106 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003107 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003108
3109 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110}
3111
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112/*
3113 * Expand a list of names.
3114 *
3115 * Generic function for command line completion. It calls a function to
3116 * obtain strings, one by one. The strings are matched against a regexp
3117 * program. Matching strings are copied into an array, which is returned.
3118 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003119 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3120 * is used.
3121 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003122 * Returns OK when no problems encountered, FAIL for error (out of memory).
3123 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003124 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003126 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003127 expand_T *xp,
3128 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003129 char_u ***matches,
3130 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003131 char_u *((*func)(expand_T *, int)),
3132 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003133 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003134{
3135 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003136 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003138 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003139 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003140 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003141 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003142 int sort_matches = FALSE;
3143 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003144
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003145 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003146 *matches = NULL;
3147 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003148
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003149 if (!fuzzy)
3150 ga_init2(&ga, sizeof(char *), 30);
3151 else
3152 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3153
3154 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003155 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003156 str = (*func)(xp, i);
3157 if (str == NULL) // end of list
3158 break;
3159 if (*str == NUL) // skip empty strings
3160 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003161
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003162 if (xp->xp_pattern[0] != NUL)
3163 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003164 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003165 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003166 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003167 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003168 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003169 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003170 }
3171 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003172 else
3173 match = TRUE;
3174
3175 if (!match)
3176 continue;
3177
3178 if (escaped)
3179 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3180 else
3181 str = vim_strsave(str);
3182 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003183 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003184 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003185 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003186 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003187 return FAIL;
3188 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003189 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003190 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003191 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003192
3193 if (ga_grow(&ga, 1) == FAIL)
3194 {
3195 vim_free(str);
3196 break;
3197 }
3198
3199 if (fuzzy)
3200 {
3201 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3202 fuzmatch->idx = ga.ga_len;
3203 fuzmatch->str = str;
3204 fuzmatch->score = score;
3205 }
3206 else
3207 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3208
K.Takata161b6ac2022-11-14 15:31:07 +00003209#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003210 if (func == get_menu_names)
3211 {
3212 // test for separator added by get_menu_names()
3213 str += STRLEN(str) - 1;
3214 if (*str == '\001')
3215 *str = '.';
3216 }
K.Takata161b6ac2022-11-14 15:31:07 +00003217#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003218
3219 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003220 }
3221
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003222 if (ga.ga_len == 0)
3223 return OK;
3224
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003225 // sort the matches when using regular expression matching and sorting
3226 // applies to the completion context. Menus and scriptnames should be kept
3227 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003228 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003229 && xp->xp_context != EXPAND_MENUS
3230 && xp->xp_context != EXPAND_SCRIPTNAMES)
3231 sort_matches = TRUE;
3232
3233 // <SNR> functions should be sorted to the end.
3234 if (xp->xp_context == EXPAND_EXPRESSION
3235 || xp->xp_context == EXPAND_FUNCTIONS
3236 || xp->xp_context == EXPAND_USER_FUNC
3237 || xp->xp_context == EXPAND_DISASSEMBLE)
3238 funcsort = TRUE;
3239
3240 // Sort the matches.
3241 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003242 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003243 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003244 // <SNR> functions should be sorted to the end.
3245 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3246 sort_func_compare);
3247 else
3248 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3249 }
3250
3251 if (!fuzzy)
3252 {
3253 *matches = ga.ga_data;
3254 *numMatches = ga.ga_len;
3255 }
3256 else
3257 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003258 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3259 funcsort) == FAIL)
3260 return FAIL;
3261 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003262 }
3263
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003264#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003265 // Reset the variables used for special highlight names expansion, so that
3266 // they don't show up when getting normal highlight names by ID.
3267 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003268#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003269
Bram Moolenaar66b51422019-08-18 21:44:12 +02003270 return OK;
3271}
3272
3273/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003274 * Expand shell command matches in one directory of $PATH.
3275 */
3276 static void
3277expand_shellcmd_onedir(
3278 char_u *buf,
3279 char_u *s,
3280 size_t l,
3281 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003282 char_u ***matches,
3283 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003284 int flags,
3285 hashtab_T *ht,
3286 garray_T *gap)
3287{
3288 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003289 hash_T hash;
3290 hashitem_T *hi;
3291
3292 vim_strncpy(buf, s, l);
3293 add_pathsep(buf);
3294 l = STRLEN(buf);
3295 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3296
3297 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003298 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003299 if (ret != OK)
3300 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003301
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003302 if (ga_grow(gap, *numMatches) == FAIL)
3303 {
3304 FreeWild(*numMatches, *matches);
3305 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003306 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003307
3308 for (int i = 0; i < *numMatches; ++i)
3309 {
3310 char_u *name = (*matches)[i];
3311
3312 if (STRLEN(name) > l)
3313 {
3314 // Check if this name was already found.
3315 hash = hash_hash(name + l);
3316 hi = hash_lookup(ht, name + l, hash);
3317 if (HASHITEM_EMPTY(hi))
3318 {
3319 // Remove the path that was prepended.
3320 STRMOVE(name, name + l);
3321 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3322 hash_add_item(ht, hi, name, hash);
3323 name = NULL;
3324 }
3325 }
3326 vim_free(name);
3327 }
3328 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003329}
3330
3331/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003332 * Complete a shell command.
3333 * Returns FAIL or OK;
3334 */
3335 static int
3336expand_shellcmd(
3337 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003338 char_u ***matches, // return: array with matches
3339 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003340 int flagsarg) // EW_ flags
3341{
3342 char_u *pat;
3343 int i;
3344 char_u *path = NULL;
3345 int mustfree = FALSE;
3346 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003347 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003348 size_t l;
3349 char_u *s, *e;
3350 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003351 int did_curdir = FALSE;
3352 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003354 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003355 if (buf == NULL)
3356 return FAIL;
3357
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003358 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003359 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003360 if (pat == NULL)
3361 {
3362 vim_free(buf);
3363 return FAIL;
3364 }
3365
Bram Moolenaar66b51422019-08-18 21:44:12 +02003366 for (i = 0; pat[i]; ++i)
3367 if (pat[i] == '\\' && pat[i + 1] == ' ')
3368 STRMOVE(pat + i, pat + i + 1);
3369
3370 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3371
3372 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3373 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3374 path = (char_u *)".";
3375 else
3376 {
3377 // For an absolute name we don't use $PATH.
3378 if (!mch_isFullName(pat))
3379 path = vim_getenv((char_u *)"PATH", &mustfree);
3380 if (path == NULL)
3381 path = (char_u *)"";
3382 }
3383
3384 // Go over all directories in $PATH. Expand matches in that directory and
3385 // collect them in "ga". When "." is not in $PATH also expand for the
3386 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003387 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003388 hash_init(&found_ht);
3389 for (s = path; ; s = e)
3390 {
K.Takata161b6ac2022-11-14 15:31:07 +00003391#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003392 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003393#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003394 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003395#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003396 if (e == NULL)
3397 e = s + STRLEN(s);
3398
3399 if (*s == NUL)
3400 {
3401 if (did_curdir)
3402 break;
3403 // Find directories in the current directory, path is empty.
3404 did_curdir = TRUE;
3405 flags |= EW_DIR;
3406 }
3407 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3408 {
3409 did_curdir = TRUE;
3410 flags |= EW_DIR;
3411 }
3412 else
3413 // Do not match directories inside a $PATH item.
3414 flags &= ~EW_DIR;
3415
3416 l = e - s;
3417 if (l > MAXPATHL - 5)
3418 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003419
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003420 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003421 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003422
Bram Moolenaar66b51422019-08-18 21:44:12 +02003423 if (*e != NUL)
3424 ++e;
3425 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003426 *matches = ga.ga_data;
3427 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003428
3429 vim_free(buf);
3430 vim_free(pat);
3431 if (mustfree)
3432 vim_free(path);
3433 hash_clear(&found_ht);
3434 return OK;
3435}
3436
K.Takata161b6ac2022-11-14 15:31:07 +00003437#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003438/*
3439 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003440 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003441 */
3442 static void *
3443call_user_expand_func(
3444 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003445 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003446{
3447 cmdline_info_T *ccline = get_cmdline_info();
3448 int keep = 0;
3449 typval_T args[4];
3450 sctx_T save_current_sctx = current_sctx;
3451 char_u *pat = NULL;
3452 void *ret;
3453
3454 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3455 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003456
3457 if (ccline->cmdbuff != NULL)
3458 {
3459 keep = ccline->cmdbuff[ccline->cmdlen];
3460 ccline->cmdbuff[ccline->cmdlen] = 0;
3461 }
3462
3463 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3464
3465 args[0].v_type = VAR_STRING;
3466 args[0].vval.v_string = pat;
3467 args[1].v_type = VAR_STRING;
3468 args[1].vval.v_string = xp->xp_line;
3469 args[2].v_type = VAR_NUMBER;
3470 args[2].vval.v_number = xp->xp_col;
3471 args[3].v_type = VAR_UNKNOWN;
3472
3473 current_sctx = xp->xp_script_ctx;
3474
3475 ret = user_expand_func(xp->xp_arg, 3, args);
3476
3477 current_sctx = save_current_sctx;
3478 if (ccline->cmdbuff != NULL)
3479 ccline->cmdbuff[ccline->cmdlen] = keep;
3480
3481 vim_free(pat);
3482 return ret;
3483}
3484
3485/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003486 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3487 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003488 */
3489 static int
3490ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003491 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003492 expand_T *xp,
3493 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003494 char_u ***matches,
3495 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003496{
3497 char_u *retstr;
3498 char_u *s;
3499 char_u *e;
3500 int keep;
3501 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003502 int fuzzy;
3503 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003504 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003505
3506 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003507 *matches = NULL;
3508 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003509
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003510 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003511 if (retstr == NULL)
3512 return FAIL;
3513
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003514 if (!fuzzy)
3515 ga_init2(&ga, sizeof(char *), 3);
3516 else
3517 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3518
Bram Moolenaar66b51422019-08-18 21:44:12 +02003519 for (s = retstr; *s != NUL; s = e)
3520 {
3521 e = vim_strchr(s, '\n');
3522 if (e == NULL)
3523 e = s + STRLEN(s);
3524 keep = *e;
3525 *e = NUL;
3526
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003527 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003528 {
3529 if (!fuzzy)
3530 match = vim_regexec(regmatch, s, (colnr_T)0);
3531 else
3532 {
3533 score = fuzzy_match_str(s, pat);
3534 match = (score != 0);
3535 }
3536 }
3537 else
3538 match = TRUE; // match everything
3539
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540 *e = keep;
3541
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003542 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003543 {
3544 if (ga_grow(&ga, 1) == FAIL)
3545 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003546 if (!fuzzy)
3547 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3548 else
3549 {
3550 fuzmatch_str_T *fuzmatch =
3551 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003552 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003553 fuzmatch->str = vim_strnsave(s, e - s);
3554 fuzmatch->score = score;
3555 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003556 ++ga.ga_len;
3557 }
3558
3559 if (*e != NUL)
3560 ++e;
3561 }
3562 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003563
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003564 if (ga.ga_len == 0)
3565 return OK;
3566
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003567 if (!fuzzy)
3568 {
3569 *matches = ga.ga_data;
3570 *numMatches = ga.ga_len;
3571 }
3572 else
3573 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003574 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3575 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003576 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003577 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003578 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003579 return OK;
3580}
3581
3582/*
3583 * Expand names with a list returned by a function defined by the user.
3584 */
3585 static int
3586ExpandUserList(
3587 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003588 char_u ***matches,
3589 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003590{
3591 list_T *retlist;
3592 listitem_T *li;
3593 garray_T ga;
3594
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003595 *matches = NULL;
3596 *numMatches = 0;
3597 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003598 if (retlist == NULL)
3599 return FAIL;
3600
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003601 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003602 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003603 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003604 {
3605 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3606 continue; // Skip non-string items and empty strings
3607
3608 if (ga_grow(&ga, 1) == FAIL)
3609 break;
3610
3611 ((char_u **)ga.ga_data)[ga.ga_len] =
3612 vim_strsave(li->li_tv.vval.v_string);
3613 ++ga.ga_len;
3614 }
3615 list_unref(retlist);
3616
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003617 *matches = ga.ga_data;
3618 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003619 return OK;
3620}
K.Takata161b6ac2022-11-14 15:31:07 +00003621#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003622
3623/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003624 * Expand "file" for all comma-separated directories in "path".
3625 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003626 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003627 */
3628 void
3629globpath(
3630 char_u *path,
3631 char_u *file,
3632 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003633 int expand_options,
3634 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003635{
3636 expand_T xpc;
3637 char_u *buf;
3638 int i;
3639 int num_p;
3640 char_u **p;
3641
3642 buf = alloc(MAXPATHL);
3643 if (buf == NULL)
3644 return;
3645
3646 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003647 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003648
3649 // Loop over all entries in {path}.
3650 while (*path != NUL)
3651 {
3652 // Copy one item of the path to buf[] and concatenate the file name.
3653 copy_option_part(&path, buf, MAXPATHL, ",");
3654 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3655 {
K.Takata161b6ac2022-11-14 15:31:07 +00003656#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003657 // Using the platform's path separator (\) makes vim incorrectly
3658 // treat it as an escape character, use '/' instead.
3659 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3660 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003661#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003662 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003663#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003664 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003665 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003666 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3667 {
3668 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3669
3670 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003671 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003672 for (i = 0; i < num_p; ++i)
3673 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003674 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003675 ++ga->ga_len;
3676 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003677 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003678 }
3679 }
3680 }
3681
3682 vim_free(buf);
3683}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003684
Bram Moolenaareadee482020-09-04 15:37:31 +02003685/*
3686 * Translate some keys pressed when 'wildmenu' is used.
3687 */
3688 int
3689wildmenu_translate_key(
3690 cmdline_info_T *cclp,
3691 int key,
3692 expand_T *xp,
3693 int did_wild_list)
3694{
3695 int c = key;
3696
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003697 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003698 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003699 // When the popup menu is used for cmdline completion:
3700 // Up : go to the previous item in the menu
3701 // Down : go to the next item in the menu
3702 // Left : go to the parent directory
3703 // Right: list the files in the selected directory
3704 switch (c)
3705 {
3706 case K_UP: c = K_LEFT; break;
3707 case K_DOWN: c = K_RIGHT; break;
3708 case K_LEFT: c = K_UP; break;
3709 case K_RIGHT: c = K_DOWN; break;
3710 default: break;
3711 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003712 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003713
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003714 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003715 {
3716 if (c == K_LEFT)
3717 c = Ctrl_P;
3718 else if (c == K_RIGHT)
3719 c = Ctrl_N;
3720 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003721
Bram Moolenaareadee482020-09-04 15:37:31 +02003722 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003723 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003724 && cclp->cmdpos > 1
3725 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3726 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3727 && (c == '\n' || c == '\r' || c == K_KENTER))
3728 c = K_DOWN;
3729
3730 return c;
3731}
3732
3733/*
3734 * Delete characters on the command line, from "from" to the current
3735 * position.
3736 */
3737 static void
3738cmdline_del(cmdline_info_T *cclp, int from)
3739{
3740 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3741 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3742 cclp->cmdlen -= cclp->cmdpos - from;
3743 cclp->cmdpos = from;
3744}
3745
3746/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003747 * Handle a key pressed when the wild menu for the menu names
3748 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003749 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003750 static int
3751wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003752{
Bram Moolenaareadee482020-09-04 15:37:31 +02003753 int i;
3754 int j;
3755
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003756 // Hitting <Down> after "emenu Name.": complete submenu
3757 if (key == K_DOWN && cclp->cmdpos > 0
3758 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003759 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003760 key = p_wc;
3761 KeyTyped = TRUE; // in case the key was mapped
3762 }
3763 else if (key == K_UP)
3764 {
3765 // Hitting <Up>: Remove one submenu name in front of the
3766 // cursor
3767 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003768
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003769 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3770 i = 0;
3771 while (--j > 0)
3772 {
3773 // check for start of menu name
3774 if (cclp->cmdbuff[j] == ' '
3775 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003776 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003777 i = j + 1;
3778 break;
3779 }
3780 // check for start of submenu name
3781 if (cclp->cmdbuff[j] == '.'
3782 && cclp->cmdbuff[j - 1] != '\\')
3783 {
3784 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003785 {
3786 i = j + 1;
3787 break;
3788 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003789 else
3790 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003791 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003792 }
3793 if (i > 0)
3794 cmdline_del(cclp, i);
3795 key = p_wc;
3796 KeyTyped = TRUE; // in case the key was mapped
3797 xp->xp_context = EXPAND_NOTHING;
3798 }
3799
3800 return key;
3801}
3802
3803/*
3804 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3805 * directory names (EXPAND_DIRECTORIES) or shell command names
3806 * (EXPAND_SHELLCMD) is displayed.
3807 */
3808 static int
3809wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3810{
3811 int i;
3812 int j;
3813 char_u upseg[5];
3814
3815 upseg[0] = PATHSEP;
3816 upseg[1] = '.';
3817 upseg[2] = '.';
3818 upseg[3] = PATHSEP;
3819 upseg[4] = NUL;
3820
3821 if (key == K_DOWN
3822 && cclp->cmdpos > 0
3823 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3824 && (cclp->cmdpos < 3
3825 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3826 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3827 {
3828 // go down a directory
3829 key = p_wc;
3830 KeyTyped = TRUE; // in case the key was mapped
3831 }
3832 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3833 {
3834 // If in a direct ancestor, strip off one ../ to go down
3835 int found = FALSE;
3836
3837 j = cclp->cmdpos;
3838 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3839 while (--j > i)
3840 {
3841 if (has_mbyte)
3842 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3843 if (vim_ispathsep(cclp->cmdbuff[j]))
3844 {
3845 found = TRUE;
3846 break;
3847 }
3848 }
3849 if (found
3850 && cclp->cmdbuff[j - 1] == '.'
3851 && cclp->cmdbuff[j - 2] == '.'
3852 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3853 {
3854 cmdline_del(cclp, j - 2);
3855 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003856 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003857 }
3858 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003859 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003860 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003861 // go up a directory
3862 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003863
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003864 j = cclp->cmdpos - 1;
3865 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3866 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003867 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003868 if (has_mbyte)
3869 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3870 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003871#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003872 && vim_strchr((char_u *)" *?[{`$%#",
3873 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003874#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003875 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003876 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003877 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003878 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003879 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003880 break;
3881 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003882 else
3883 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003884 }
3885 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003886
3887 if (!found)
3888 j = i;
3889 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3890 j += 4;
3891 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3892 && j == i)
3893 j += 3;
3894 else
3895 j = 0;
3896 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003897 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003898 // TODO this is only for DOS/UNIX systems - need to put in
3899 // machine-specific stuff here and in upseg init
3900 cmdline_del(cclp, j);
3901 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003902 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003903 else if (cclp->cmdpos > i)
3904 cmdline_del(cclp, i);
3905
3906 // Now complete in the new directory. Set KeyTyped in case the
3907 // Up key came from a mapping.
3908 key = p_wc;
3909 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003910 }
3911
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003912 return key;
3913}
3914
3915/*
3916 * Handle a key pressed when the wild menu is displayed
3917 */
3918 int
3919wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3920{
3921 if (xp->xp_context == EXPAND_MENUNAMES)
3922 return wildmenu_process_key_menunames(cclp, key, xp);
3923 else if ((xp->xp_context == EXPAND_FILES
3924 || xp->xp_context == EXPAND_DIRECTORIES
3925 || xp->xp_context == EXPAND_SHELLCMD))
3926 return wildmenu_process_key_filenames(cclp, key, xp);
3927
3928 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003929}
3930
3931/*
3932 * Free expanded names when finished walking through the matches
3933 */
3934 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003935wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003936{
3937 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02003938
3939 if (!p_wmnu || wild_menu_showing == 0)
3940 return;
3941
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003942#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003943 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02003944 if (cclp->input_fn)
3945 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003946#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003947
3948 if (wild_menu_showing == WM_SCROLLED)
3949 {
3950 // Entered command line, move it up
3951 cmdline_row--;
3952 redrawcmd();
3953 }
3954 else if (save_p_ls != -1)
3955 {
3956 // restore 'laststatus' and 'winminheight'
3957 p_ls = save_p_ls;
3958 p_wmh = save_p_wmh;
3959 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003960 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003961 redrawcmd();
3962 save_p_ls = -1;
3963 }
3964 else
3965 {
3966 win_redraw_last_status(topframe);
3967 redraw_statuslines();
3968 }
3969 KeyTyped = skt;
3970 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003971#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003972 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003973 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003974#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003975}
Bram Moolenaareadee482020-09-04 15:37:31 +02003976
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003977#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003978/*
3979 * "getcompletion()" function
3980 */
3981 void
3982f_getcompletion(typval_T *argvars, typval_T *rettv)
3983{
3984 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003985 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003986 expand_T xpc;
3987 int filtered = FALSE;
3988 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003989 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003990
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003991 if (in_vim9script()
3992 && (check_for_string_arg(argvars, 0) == FAIL
3993 || check_for_string_arg(argvars, 1) == FAIL
3994 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3995 return;
3996
ii144785fe02021-11-21 12:13:56 +00003997 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003998 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003999 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004000 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004001
4002 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004003 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004004
4005 if (p_wic)
4006 options |= WILD_ICASE;
4007
4008 // For filtered results, 'wildignore' is used
4009 if (!filtered)
4010 options |= WILD_KEEP_ALL;
4011
4012 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004013 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004014 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004015 int cmdline_len = (int)STRLEN(pat);
4016 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004017 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004018 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004019 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004020 else
4021 {
ii144785fe02021-11-21 12:13:56 +00004022 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004023 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004024 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004025
4026 xpc.xp_context = cmdcomplete_str_to_type(type);
4027 if (xpc.xp_context == EXPAND_NOTHING)
4028 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004029 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004030 return;
4031 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004032
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004033 if (xpc.xp_context == EXPAND_USER_DEFINED)
4034 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004035 // Must be "custom,funcname" pattern
4036 if (STRNCMP(type, "custom,", 7) != 0)
4037 {
4038 semsg(_(e_invalid_argument_str), type);
4039 return;
4040 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004041
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004042 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004043 }
4044
4045 if (xpc.xp_context == EXPAND_USER_LIST)
4046 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004047 // Must be "customlist,funcname" pattern
4048 if (STRNCMP(type, "customlist,", 11) != 0)
4049 {
4050 semsg(_(e_invalid_argument_str), type);
4051 return;
4052 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004053
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004054 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004055 }
4056
Bram Moolenaar66b51422019-08-18 21:44:12 +02004057# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004058 if (xpc.xp_context == EXPAND_MENUS)
4059 {
4060 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4061 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4062 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004063# endif
4064# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004065 if (xpc.xp_context == EXPAND_CSCOPE)
4066 {
4067 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4068 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4069 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004070# endif
4071# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004072 if (xpc.xp_context == EXPAND_SIGN)
4073 {
4074 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4075 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4076 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004077# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004078 if (xpc.xp_context == EXPAND_RUNTIME)
4079 {
4080 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4081 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4082 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004083 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004084
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004085 if (cmdline_fuzzy_completion_supported(&xpc))
4086 // when fuzzy matching, don't modify the search string
4087 pat = vim_strsave(xpc.xp_pattern);
4088 else
4089 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4090
Bram Moolenaar93a10962022-06-16 11:42:09 +01004091 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004092 {
4093 int i;
4094
4095 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4096
4097 for (i = 0; i < xpc.xp_numfiles; i++)
4098 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4099 }
4100 vim_free(pat);
4101 ExpandCleanup(&xpc);
4102}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004103#endif // FEAT_EVAL