blob: b59610c02ec200e633339f0ce169eb03daee89c9 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000018static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000019static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020020static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020022#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000023static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020025#endif
26
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000027// "compl_match_array" points the currently displayed list of entries in the
28// popup menu. It is NULL when there is no popup menu.
29static pumitem_T *compl_match_array = NULL;
30static int compl_match_arraysize;
31// First column in cmdline of the matched item for completion.
32static int compl_startcol;
33static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000034
zeertzjqc51a3762022-12-10 10:22:29 +000035#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000038 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
39 * context.
40 */
41 static int
42cmdline_fuzzy_completion_supported(expand_T *xp)
43{
44 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
45 && xp->xp_context != EXPAND_BOOL_SETTINGS
46 && xp->xp_context != EXPAND_COLORS
47 && xp->xp_context != EXPAND_COMPILER
48 && xp->xp_context != EXPAND_DIRECTORIES
49 && xp->xp_context != EXPAND_FILES
50 && xp->xp_context != EXPAND_FILES_IN_PATH
51 && xp->xp_context != EXPAND_FILETYPE
52 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000053 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020054 && xp->xp_context != EXPAND_STRING_SETTING
55 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OWNSYNTAX
57 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000058 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_SHELLCMD
60 && xp->xp_context != EXPAND_TAGS
61 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_USER_LIST);
63}
64
65/*
66 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000067 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
68 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000069 */
70 int
71cmdline_fuzzy_complete(char_u *fuzzystr)
72{
73 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
74}
75
76/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000077 * sort function for the completion matches.
78 * <SNR> functions should be sorted to the end.
79 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020080 static int
81sort_func_compare(const void *s1, const void *s2)
82{
83 char_u *p1 = *(char_u **)s1;
84 char_u *p2 = *(char_u **)s2;
85
86 if (*p1 != '<' && *p2 == '<') return -1;
87 if (*p1 == '<' && *p2 != '<') return 1;
88 return STRCMP(p1, p2);
89}
Bram Moolenaar66b51422019-08-18 21:44:12 +020090
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000091/*
92 * Escape special characters in the cmdline completion matches.
93 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020094 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000095wildescape(
96 expand_T *xp,
97 char_u *str,
98 int numfiles,
99 char_u **files)
100{
101 char_u *p;
102 int vse_what = xp->xp_context == EXPAND_BUFFERS
103 ? VSE_BUFFER : VSE_NONE;
104
105 if (xp->xp_context == EXPAND_FILES
106 || xp->xp_context == EXPAND_FILES_IN_PATH
107 || xp->xp_context == EXPAND_SHELLCMD
108 || xp->xp_context == EXPAND_BUFFERS
109 || xp->xp_context == EXPAND_DIRECTORIES)
110 {
111 // Insert a backslash into a file name before a space, \, %, #
112 // and wildmatch characters, except '~'.
113 for (int i = 0; i < numfiles; ++i)
114 {
115 // for ":set path=" we need to escape spaces twice
116 if (xp->xp_backslash == XP_BS_THREE)
117 {
118 p = vim_strsave_escaped(files[i], (char_u *)" ");
119 if (p != NULL)
120 {
121 vim_free(files[i]);
122 files[i] = p;
123#if defined(BACKSLASH_IN_FILENAME)
124 p = vim_strsave_escaped(files[i], (char_u *)" ");
125 if (p != NULL)
126 {
127 vim_free(files[i]);
128 files[i] = p;
129 }
130#endif
131 }
132 }
133#ifdef BACKSLASH_IN_FILENAME
134 p = vim_strsave_fnameescape(files[i], vse_what);
135#else
136 p = vim_strsave_fnameescape(files[i],
137 xp->xp_shell ? VSE_SHELL : vse_what);
138#endif
139 if (p != NULL)
140 {
141 vim_free(files[i]);
142 files[i] = p;
143 }
144
145 // If 'str' starts with "\~", replace "~" at start of
146 // files[i] with "\~".
147 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
148 escape_fname(&files[i]);
149 }
150 xp->xp_backslash = XP_BS_NONE;
151
152 // If the first file starts with a '+' escape it. Otherwise it
153 // could be seen as "+cmd".
154 if (*files[0] == '+')
155 escape_fname(&files[0]);
156 }
157 else if (xp->xp_context == EXPAND_TAGS)
158 {
159 // Insert a backslash before characters in a tag name that
160 // would terminate the ":tag" command.
161 for (int i = 0; i < numfiles; ++i)
162 {
163 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
164 if (p != NULL)
165 {
166 vim_free(files[i]);
167 files[i] = p;
168 }
169 }
170 }
171}
172
173/*
174 * Escape special characters in the cmdline completion matches.
175 */
176 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200177ExpandEscape(
178 expand_T *xp,
179 char_u *str,
180 int numfiles,
181 char_u **files,
182 int options)
183{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200184 // May change home directory back to "~"
185 if (options & WILD_HOME_REPLACE)
186 tilde_replace(str, numfiles, files);
187
188 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000189 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200190}
191
192/*
193 * Return FAIL if this is not an appropriate context in which to do
194 * completion of anything, return OK if it is (even if there are no matches).
195 * For the caller, this means that the character is just passed through like a
196 * normal character (instead of being expanded). This allows :s/^I^D etc.
197 */
198 int
199nextwild(
200 expand_T *xp,
201 int type,
202 int options, // extra options for ExpandOne()
203 int escape) // if TRUE, escape the returned matches
204{
205 cmdline_info_T *ccline = get_cmdline_info();
206 int i, j;
207 char_u *p1;
208 char_u *p2;
209 int difflen;
210 int v;
211
212 if (xp->xp_numfiles == -1)
213 {
214 set_expand_context(xp);
215 cmd_showtail = expand_showtail(xp);
216 }
217
218 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
219 {
220 beep_flush();
221 return OK; // Something illegal on command line
222 }
223 if (xp->xp_context == EXPAND_NOTHING)
224 {
225 // Caller can use the character as a normal char instead
226 return FAIL;
227 }
228
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000229 // If cmd_silent is set then don't show the dots, because redrawcmd() below
230 // won't remove them.
231 if (!cmd_silent)
232 {
233 msg_puts("..."); // show that we are busy
234 out_flush();
235 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200236
237 i = (int)(xp->xp_pattern - ccline->cmdbuff);
238 xp->xp_pattern_len = ccline->cmdpos - i;
239
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000240 if (type == WILD_NEXT || type == WILD_PREV
241 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200242 {
243 // Get next/previous match for a previous expanded pattern.
244 p2 = ExpandOne(xp, NULL, NULL, 0, type);
245 }
246 else
247 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000248 if (cmdline_fuzzy_completion_supported(xp))
249 // If fuzzy matching, don't modify the search string
250 p1 = vim_strsave(xp->xp_pattern);
251 else
252 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
253
Bram Moolenaar66b51422019-08-18 21:44:12 +0200254 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000255 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200256 p2 = NULL;
257 else
258 {
259 int use_options = options |
260 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
261 if (escape)
262 use_options |= WILD_ESCAPE;
263
264 if (p_wic)
265 use_options += WILD_ICASE;
266 p2 = ExpandOne(xp, p1,
267 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
268 use_options, type);
269 vim_free(p1);
270 // longest match: make sure it is not shorter, happens with :help
271 if (p2 != NULL && type == WILD_LONGEST)
272 {
273 for (j = 0; j < xp->xp_pattern_len; ++j)
274 if (ccline->cmdbuff[i + j] == '*'
275 || ccline->cmdbuff[i + j] == '?')
276 break;
277 if ((int)STRLEN(p2) < j)
278 VIM_CLEAR(p2);
279 }
280 }
281 }
282
283 if (p2 != NULL && !got_int)
284 {
285 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
286 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
287 {
288 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
289 xp->xp_pattern = ccline->cmdbuff + i;
290 }
291 else
292 v = OK;
293 if (v == OK)
294 {
295 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
296 &ccline->cmdbuff[ccline->cmdpos],
297 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
298 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
299 ccline->cmdlen += difflen;
300 ccline->cmdpos += difflen;
301 }
302 }
303 vim_free(p2);
304
305 redrawcmd();
306 cursorcmd();
307
308 // When expanding a ":map" command and no matches are found, assume that
309 // the key is supposed to be inserted literally
310 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
311 return FAIL;
312
313 if (xp->xp_numfiles <= 0 && p2 == NULL)
314 beep_flush();
315 else if (xp->xp_numfiles == 1)
316 // free expanded pattern
317 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
318
319 return OK;
320}
321
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000322/*
323 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000324 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000325 */
326 static int
327cmdline_pum_create(
328 cmdline_info_T *ccline,
329 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000330 char_u **matches,
331 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000332 int showtail)
333{
334 int i;
335 int columns;
336
337 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000338 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000339 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000340 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000341 {
zeertzjqc51a3762022-12-10 10:22:29 +0000342 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000343 compl_match_array[i].pum_info = NULL;
344 compl_match_array[i].pum_extra = NULL;
345 compl_match_array[i].pum_kind = NULL;
346 }
347
348 // Compute the popup menu starting column
349 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
350 columns = vim_strsize(xp->xp_pattern);
351 if (showtail)
352 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000353 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000354 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000355 }
356 if (columns >= compl_startcol)
357 compl_startcol = 0;
358 else
359 compl_startcol -= columns;
360
361 // no default selection
362 compl_selected = -1;
363
364 cmdline_pum_display();
365
366 return EXPAND_OK;
367}
368
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000369/*
370 * Display the cmdline completion matches in a popup menu
371 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000372 void
373cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000374{
375 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
376}
377
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000378/*
379 * Returns TRUE if the cmdline completion popup menu is being displayed.
380 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000381 int
382cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000383{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100384 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000385}
386
387/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000388 * Remove the cmdline completion popup menu (if present), free the list of
389 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000390 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000391 void
392cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000393{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000394 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100395 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000396
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000397 pum_undisplay();
398 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000399 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000401 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000402 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100403
404 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
405 // as a side effect.
406 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407}
408
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000409 void
410cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000411{
412 cmdline_pum_remove();
413 wildmenu_cleanup(cclp);
414}
415
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000416/*
417 * Returns the starting column number to use for the cmdline completion popup
418 * menu.
419 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000420 int
421cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000422{
423 return compl_startcol;
424}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000425
Bram Moolenaar66b51422019-08-18 21:44:12 +0200426/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000427 * Return the number of characters that should be skipped in a status match.
428 * These are backslashes used for escaping. Do show backslashes in help tags.
429 */
430 static int
431skip_status_match_char(expand_T *xp, char_u *s)
432{
433 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
434#ifdef FEAT_MENU
435 || ((xp->xp_context == EXPAND_MENUS
436 || xp->xp_context == EXPAND_MENUNAMES)
437 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
438#endif
439 )
440 {
441#ifndef BACKSLASH_IN_FILENAME
442 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
443 return 2;
444#endif
445 return 1;
446 }
447 return 0;
448}
449
450/*
451 * Get the length of an item as it will be shown in the status line.
452 */
453 static int
454status_match_len(expand_T *xp, char_u *s)
455{
456 int len = 0;
457
458#ifdef FEAT_MENU
459 int emenu = xp->xp_context == EXPAND_MENUS
460 || xp->xp_context == EXPAND_MENUNAMES;
461
462 // Check for menu separators - replace with '|'.
463 if (emenu && menu_is_separator(s))
464 return 1;
465#endif
466
467 while (*s != NUL)
468 {
469 s += skip_status_match_char(xp, s);
470 len += ptr2cells(s);
471 MB_PTR_ADV(s);
472 }
473
474 return len;
475}
476
477/*
478 * Show wildchar matches in the status line.
479 * Show at least the "match" item.
480 * We start at item 'first_match' in the list and show all matches that fit.
481 *
482 * If inversion is possible we use it. Else '=' characters are used.
483 */
484 static void
485win_redr_status_matches(
486 expand_T *xp,
487 int num_matches,
488 char_u **matches, // list of matches
489 int match,
490 int showtail)
491{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000492 int row;
493 char_u *buf;
494 int len;
495 int clen; // length in screen cells
496 int fillchar;
497 int attr;
498 int i;
499 int highlight = TRUE;
500 char_u *selstart = NULL;
501 int selstart_col = 0;
502 char_u *selend = NULL;
503 static int first_match = 0;
504 int add_left = FALSE;
505 char_u *s;
506#ifdef FEAT_MENU
507 int emenu;
508#endif
509 int l;
510
511 if (matches == NULL) // interrupted completion?
512 return;
513
514 if (has_mbyte)
515 buf = alloc(Columns * MB_MAXBYTES + 1);
516 else
517 buf = alloc(Columns + 1);
518 if (buf == NULL)
519 return;
520
521 if (match == -1) // don't show match but original text
522 {
523 match = 0;
524 highlight = FALSE;
525 }
526 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000527 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000528 if (match == 0)
529 first_match = 0;
530 else if (match < first_match)
531 {
532 // jumping left, as far as we can go
533 first_match = match;
534 add_left = TRUE;
535 }
536 else
537 {
538 // check if match fits on the screen
539 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000540 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000541 if (first_match > 0)
542 clen += 2;
543 // jumping right, put match at the left
544 if ((long)clen > Columns)
545 {
546 first_match = match;
547 // if showing the last match, we can add some on the left
548 clen = 2;
549 for (i = match; i < num_matches; ++i)
550 {
zeertzjqc51a3762022-12-10 10:22:29 +0000551 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000552 if ((long)clen >= Columns)
553 break;
554 }
555 if (i == num_matches)
556 add_left = TRUE;
557 }
558 }
559 if (add_left)
560 while (first_match > 0)
561 {
zeertzjqc51a3762022-12-10 10:22:29 +0000562 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000563 if ((long)clen >= Columns)
564 break;
565 --first_match;
566 }
567
568 fillchar = fillchar_status(&attr, curwin);
569
570 if (first_match == 0)
571 {
572 *buf = NUL;
573 len = 0;
574 }
575 else
576 {
577 STRCPY(buf, "< ");
578 len = 2;
579 }
580 clen = len;
581
582 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000583 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000584 {
585 if (i == match)
586 {
587 selstart = buf + len;
588 selstart_col = clen;
589 }
590
zeertzjqc51a3762022-12-10 10:22:29 +0000591 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000592 // Check for menu separators - replace with '|'
593#ifdef FEAT_MENU
594 emenu = (xp->xp_context == EXPAND_MENUS
595 || xp->xp_context == EXPAND_MENUNAMES);
596 if (emenu && menu_is_separator(s))
597 {
598 STRCPY(buf + len, transchar('|'));
599 l = (int)STRLEN(buf + len);
600 len += l;
601 clen += l;
602 }
603 else
604#endif
605 for ( ; *s != NUL; ++s)
606 {
607 s += skip_status_match_char(xp, s);
608 clen += ptr2cells(s);
609 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
610 {
611 STRNCPY(buf + len, s, l);
612 s += l - 1;
613 len += l;
614 }
615 else
616 {
617 STRCPY(buf + len, transchar_byte(*s));
618 len += (int)STRLEN(buf + len);
619 }
620 }
621 if (i == match)
622 selend = buf + len;
623
624 *(buf + len++) = ' ';
625 *(buf + len++) = ' ';
626 clen += 2;
627 if (++i == num_matches)
628 break;
629 }
630
631 if (i != num_matches)
632 {
633 *(buf + len++) = '>';
634 ++clen;
635 }
636
637 buf[len] = NUL;
638
639 row = cmdline_row - 1;
640 if (row >= 0)
641 {
642 if (wild_menu_showing == 0)
643 {
644 if (msg_scrolled > 0)
645 {
646 // Put the wildmenu just above the command line. If there is
647 // no room, scroll the screen one line up.
648 if (cmdline_row == Rows - 1)
649 {
650 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
651 ++msg_scrolled;
652 }
653 else
654 {
655 ++cmdline_row;
656 ++row;
657 }
658 wild_menu_showing = WM_SCROLLED;
659 }
660 else
661 {
662 // Create status line if needed by setting 'laststatus' to 2.
663 // Set 'winminheight' to zero to avoid that the window is
664 // resized.
665 if (lastwin->w_status_height == 0)
666 {
667 save_p_ls = p_ls;
668 save_p_wmh = p_wmh;
669 p_ls = 2;
670 p_wmh = 0;
671 last_status(FALSE);
672 }
673 wild_menu_showing = WM_SHOWN;
674 }
675 }
676
677 screen_puts(buf, row, 0, attr);
678 if (selstart != NULL && highlight)
679 {
680 *selend = NUL;
681 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
682 }
683
684 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
685 }
686
687 win_redraw_last_status(topframe);
688 vim_free(buf);
689}
690
691/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000692 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200693 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000694 */
695 static char_u *
696get_next_or_prev_match(
697 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200698 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000699{
zeertzjqe9ef3472023-08-17 23:57:05 +0200700 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000701 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000702
703 if (xp->xp_numfiles <= 0)
704 return NULL;
705
706 if (mode == WILD_PREV)
707 {
708 if (findex == -1)
709 findex = xp->xp_numfiles;
710 --findex;
711 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000712 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000713 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000714 else if (mode == WILD_PAGEUP)
715 {
716 if (findex == 0)
717 // at the first entry, don't select any entries
718 findex = -1;
719 else if (findex == -1)
720 // no entry is selected. select the last entry
721 findex = xp->xp_numfiles - 1;
722 else
723 {
724 // go up by the pum height
725 ht = pum_get_height();
726 if (ht > 3)
727 ht -= 2;
728 findex -= ht;
729 if (findex < 0)
730 // few entries left, select the first entry
731 findex = 0;
732 }
733 }
734 else // mode == WILD_PAGEDOWN
735 {
736 if (findex == xp->xp_numfiles - 1)
737 // at the last entry, don't select any entries
738 findex = -1;
739 else if (findex == -1)
740 // no entry is selected. select the first entry
741 findex = 0;
742 else
743 {
744 // go down by the pum height
745 ht = pum_get_height();
746 if (ht > 3)
747 ht -= 2;
748 findex += ht;
749 if (findex >= xp->xp_numfiles)
750 // few entries left, select the last entry
751 findex = xp->xp_numfiles - 1;
752 }
753 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000754
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000755 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000756 if (findex < 0)
757 {
zeertzjq28a23602023-09-29 19:58:35 +0200758 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000759 findex = xp->xp_numfiles - 1;
760 else
761 findex = -1;
762 }
763 if (findex >= xp->xp_numfiles)
764 {
zeertzjq28a23602023-09-29 19:58:35 +0200765 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000766 findex = 0;
767 else
768 findex = -1;
769 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000770 if (compl_match_array)
771 {
772 compl_selected = findex;
773 cmdline_pum_display();
774 }
775 else if (p_wmnu)
776 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
777 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200778 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000779
780 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200781 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000782
783 return vim_strsave(xp->xp_files[findex]);
784}
785
786/*
787 * Start the command-line expansion and get the matches.
788 */
789 static char_u *
790ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
791{
792 int non_suf_match; // number without matching suffix
793 int i;
794 char_u *ss = NULL;
795
796 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000797 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000798 options) == FAIL)
799 {
800#ifdef FNAME_ILLEGAL
801 // Illegal file name has been silently skipped. But when there
802 // are wildcards, the real problem is that there was no match,
803 // causing the pattern to be added, which has illegal characters.
804 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
805 semsg(_(e_no_match_str_2), str);
806#endif
807 }
808 else if (xp->xp_numfiles == 0)
809 {
810 if (!(options & WILD_SILENT))
811 semsg(_(e_no_match_str_2), str);
812 }
813 else
814 {
815 // Escape the matches for use on the command line.
816 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
817
818 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000819 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000820 {
821 if (xp->xp_numfiles)
822 non_suf_match = xp->xp_numfiles;
823 else
824 non_suf_match = 1;
825 if ((xp->xp_context == EXPAND_FILES
826 || xp->xp_context == EXPAND_DIRECTORIES)
827 && xp->xp_numfiles > 1)
828 {
829 // More than one match; check suffix.
830 // The files will have been sorted on matching suffix in
831 // expand_wildcards, only need to check the first two.
832 non_suf_match = 0;
833 for (i = 0; i < 2; ++i)
834 if (match_suffix(xp->xp_files[i]))
835 ++non_suf_match;
836 }
837 if (non_suf_match != 1)
838 {
839 // Can we ever get here unless it's while expanding
840 // interactively? If not, we can get rid of this all
841 // together. Don't really want to wait for this message
842 // (and possibly have to hit return to continue!).
843 if (!(options & WILD_SILENT))
844 emsg(_(e_too_many_file_names));
845 else if (!(options & WILD_NO_BEEP))
846 beep_flush();
847 }
848 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
849 ss = vim_strsave(xp->xp_files[0]);
850 }
851 }
852
853 return ss;
854}
855
856/*
857 * Return the longest common part in the list of cmdline completion matches.
858 */
859 static char_u *
860find_longest_match(expand_T *xp, int options)
861{
862 long_u len;
863 int mb_len = 1;
864 int c0, ci;
865 int i;
866 char_u *ss;
867
868 for (len = 0; xp->xp_files[0][len]; len += mb_len)
869 {
870 if (has_mbyte)
871 {
872 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
873 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
874 }
875 else
876 c0 = xp->xp_files[0][len];
877 for (i = 1; i < xp->xp_numfiles; ++i)
878 {
879 if (has_mbyte)
880 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
881 else
882 ci = xp->xp_files[i][len];
883 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
884 || xp->xp_context == EXPAND_FILES
885 || xp->xp_context == EXPAND_SHELLCMD
886 || xp->xp_context == EXPAND_BUFFERS))
887 {
888 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
889 break;
890 }
891 else if (c0 != ci)
892 break;
893 }
894 if (i < xp->xp_numfiles)
895 {
896 if (!(options & WILD_NO_BEEP))
897 vim_beep(BO_WILD);
898 break;
899 }
900 }
901
902 ss = alloc(len + 1);
903 if (ss)
904 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
905
906 return ss;
907}
908
909/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000910 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200911 * Chars that should not be expanded must be preceded with a backslash.
912 * Return a pointer to allocated memory containing the new string.
913 * Return NULL for failure.
914 *
915 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200916 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
917 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200918 *
919 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
920 * is WILD_EXPAND_FREE or WILD_ALL.
921 *
922 * mode = WILD_FREE: just free previously expanded matches
923 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
924 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
925 * mode = WILD_NEXT: use next match in multiple match, wrap to first
926 * mode = WILD_PREV: use previous match in multiple match, wrap to first
927 * mode = WILD_ALL: return all matches concatenated
928 * mode = WILD_LONGEST: return longest matched part
929 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000930 * mode = WILD_APPLY: apply the item selected in the cmdline completion
931 * popup menu and close the menu.
932 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
933 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200934 *
935 * options = WILD_LIST_NOTFOUND: list entries without a match
936 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
937 * options = WILD_USE_NL: Use '\n' for WILD_ALL
938 * options = WILD_NO_BEEP: Don't beep for multiple matches
939 * options = WILD_ADD_SLASH: add a slash after directory names
940 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
941 * options = WILD_SILENT: don't print warning messages
942 * options = WILD_ESCAPE: put backslash before special chars
943 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200944 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200945 *
946 * The variables xp->xp_context and xp->xp_backslash must have been set!
947 */
948 char_u *
949ExpandOne(
950 expand_T *xp,
951 char_u *str,
952 char_u *orig, // allocated copy of original of expanded string
953 int options,
954 int mode)
955{
956 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200957 int orig_saved = FALSE;
958 int i;
959 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200960
961 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000962 if (mode == WILD_NEXT || mode == WILD_PREV
963 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +0200964 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200965
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000966 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +0200967 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000968 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +0200969 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +0200970 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +0200971 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000972
Bram Moolenaar66b51422019-08-18 21:44:12 +0200973 // free old names
974 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
975 {
976 FreeWild(xp->xp_numfiles, xp->xp_files);
977 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +0200978 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000979
980 // The entries from xp_files may be used in the PUM, remove it.
981 if (compl_match_array != NULL)
982 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200983 }
zeertzjqe9ef3472023-08-17 23:57:05 +0200984 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200985
986 if (mode == WILD_FREE) // only release file name
987 return NULL;
988
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000989 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200990 {
zeertzjq28a23602023-09-29 19:58:35 +0200991 vim_free(xp->xp_orig);
992 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200993 orig_saved = TRUE;
994
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000995 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200996 }
997
998 // Find longest common part
999 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1000 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001001 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001002 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001003 }
1004
Bram Moolenaar57e95172022-08-20 19:26:14 +01001005 // Concatenate all matching names. Unless interrupted, this can be slow
1006 // and the result probably won't be used.
1007 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001008 {
1009 len = 0;
1010 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001011 {
1012 if (i > 0)
1013 {
1014 if (xp->xp_prefix == XP_PREFIX_NO)
1015 len += 2; // prefix "no"
1016 else if (xp->xp_prefix == XP_PREFIX_INV)
1017 len += 3; // prefix "inv"
1018 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001019 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001020 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001021 ss = alloc(len);
1022 if (ss != NULL)
1023 {
1024 *ss = NUL;
1025 for (i = 0; i < xp->xp_numfiles; ++i)
1026 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001027 if (i > 0)
1028 {
1029 if (xp->xp_prefix == XP_PREFIX_NO)
1030 STRCAT(ss, "no");
1031 else if (xp->xp_prefix == XP_PREFIX_INV)
1032 STRCAT(ss, "inv");
1033 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001034 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001035
Bram Moolenaar66b51422019-08-18 21:44:12 +02001036 if (i != xp->xp_numfiles - 1)
1037 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1038 }
1039 }
1040 }
1041
1042 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1043 ExpandCleanup(xp);
1044
zeertzjq28a23602023-09-29 19:58:35 +02001045 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 if (!orig_saved)
1047 vim_free(orig);
1048
1049 return ss;
1050}
1051
1052/*
1053 * Prepare an expand structure for use.
1054 */
1055 void
1056ExpandInit(expand_T *xp)
1057{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001058 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001059 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001060 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001061 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001062}
1063
1064/*
1065 * Cleanup an expand structure after use.
1066 */
1067 void
1068ExpandCleanup(expand_T *xp)
1069{
1070 if (xp->xp_numfiles >= 0)
1071 {
1072 FreeWild(xp->xp_numfiles, xp->xp_files);
1073 xp->xp_numfiles = -1;
1074 }
zeertzjq28a23602023-09-29 19:58:35 +02001075 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076}
1077
1078/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001079 * Display one line of completion matches. Multiple matches are displayed in
1080 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001081 * matches - list of completion match names
1082 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001083 * lines - number of output lines
1084 * linenr - line number of matches to display
1085 * maxlen - maximum number of characters in each line
1086 * showtail - display only the tail of the full path of a file name
1087 * dir_attr - highlight attribute to use for directory names
1088 */
1089 static void
1090showmatches_oneline(
1091 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001092 char_u **matches,
1093 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001094 int lines,
1095 int linenr,
1096 int maxlen,
1097 int showtail,
1098 int dir_attr)
1099{
1100 int i, j;
1101 int isdir;
1102 int lastlen;
1103 char_u *p;
1104
1105 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001106 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001107 {
1108 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1109 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001110 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1111 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001112 msg_advance(maxlen + 1);
1113 msg_puts((char *)p);
1114 msg_advance(maxlen + 3);
1115 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1116 break;
1117 }
1118 for (i = maxlen - lastlen; --i >= 0; )
1119 msg_putchar(' ');
1120 if (xp->xp_context == EXPAND_FILES
1121 || xp->xp_context == EXPAND_SHELLCMD
1122 || xp->xp_context == EXPAND_BUFFERS)
1123 {
1124 // highlight directories
1125 if (xp->xp_numfiles != -1)
1126 {
1127 char_u *halved_slash;
1128 char_u *exp_path;
1129 char_u *path;
1130
1131 // Expansion was done before and special characters
1132 // were escaped, need to halve backslashes. Also
1133 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001134 exp_path = expand_env_save_opt(matches[j], TRUE);
1135 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001136 halved_slash = backslash_halve_save(path);
1137 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001138 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001139 vim_free(exp_path);
1140 if (halved_slash != path)
1141 vim_free(halved_slash);
1142 }
1143 else
1144 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001145 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001146 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001147 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001148 else
1149 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001150 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001151 TRUE);
1152 p = NameBuff;
1153 }
1154 }
1155 else
1156 {
1157 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001158 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001159 }
1160 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1161 }
1162 if (msg_col > 0) // when not wrapped around
1163 {
1164 msg_clr_eos();
1165 msg_putchar('\n');
1166 }
1167 out_flush(); // show one line at a time
1168}
1169
1170/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001171 * Show all matches for completion on the command line.
1172 * Returns EXPAND_NOTHING when the character that triggered expansion should
1173 * be inserted like a normal character.
1174 */
1175 int
1176showmatches(expand_T *xp, int wildmenu UNUSED)
1177{
1178 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001179 int numMatches;
1180 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001181 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001182 int maxlen;
1183 int lines;
1184 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001185 int attr;
1186 int showtail;
1187
1188 if (xp->xp_numfiles == -1)
1189 {
1190 set_expand_context(xp);
1191 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001192 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001193 showtail = expand_showtail(xp);
1194 if (i != EXPAND_OK)
1195 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001196 }
1197 else
1198 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001199 numMatches = xp->xp_numfiles;
1200 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001201 showtail = cmd_showtail;
1202 }
1203
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001204 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001205 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001206 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001207
Bram Moolenaar66b51422019-08-18 21:44:12 +02001208 if (!wildmenu)
1209 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001210 msg_didany = FALSE; // lines_left will be set
1211 msg_start(); // prepare for paging
1212 msg_putchar('\n');
1213 out_flush();
1214 cmdline_row = msg_row;
1215 msg_didany = FALSE; // lines_left will be set again
1216 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001217 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001218
1219 if (got_int)
1220 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001221 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001222 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 else
1224 {
1225 // find the length of the longest file name
1226 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001227 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001228 {
1229 if (!showtail && (xp->xp_context == EXPAND_FILES
1230 || xp->xp_context == EXPAND_SHELLCMD
1231 || xp->xp_context == EXPAND_BUFFERS))
1232 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001233 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001234 j = vim_strsize(NameBuff);
1235 }
1236 else
zeertzjqc51a3762022-12-10 10:22:29 +00001237 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001238 if (j > maxlen)
1239 maxlen = j;
1240 }
1241
1242 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001243 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001244 else
1245 {
1246 // compute the number of columns and lines for the listing
1247 maxlen += 2; // two spaces between file names
1248 columns = ((int)Columns + 2) / maxlen;
1249 if (columns < 1)
1250 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001251 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001252 }
1253
1254 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1255
1256 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1257 {
1258 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1259 msg_clr_eos();
1260 msg_advance(maxlen - 3);
1261 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1262 }
1263
1264 // list the files line by line
1265 for (i = 0; i < lines; ++i)
1266 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001267 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001268 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001269 if (got_int)
1270 {
1271 got_int = FALSE;
1272 break;
1273 }
1274 }
1275
1276 // we redraw the command below the lines that we have just listed
1277 // This is a bit tricky, but it saves a lot of screen updating.
1278 cmdline_row = msg_row; // will put it back later
1279 }
1280
1281 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001282 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001283
1284 return EXPAND_OK;
1285}
1286
1287/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001288 * gettail() version for showmatches() and win_redr_status_matches():
1289 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001290 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001291 static char_u *
1292showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001293{
1294 char_u *p;
1295 char_u *t = s;
1296 int had_sep = FALSE;
1297
1298 for (p = s; *p != NUL; )
1299 {
1300 if (vim_ispathsep(*p)
1301#ifdef BACKSLASH_IN_FILENAME
1302 && !rem_backslash(p)
1303#endif
1304 )
1305 had_sep = TRUE;
1306 else if (had_sep)
1307 {
1308 t = p;
1309 had_sep = FALSE;
1310 }
1311 MB_PTR_ADV(p);
1312 }
1313 return t;
1314}
1315
1316/*
1317 * Return TRUE if we only need to show the tail of completion matches.
1318 * When not completing file names or there is a wildcard in the path FALSE is
1319 * returned.
1320 */
1321 static int
1322expand_showtail(expand_T *xp)
1323{
1324 char_u *s;
1325 char_u *end;
1326
1327 // When not completing file names a "/" may mean something different.
1328 if (xp->xp_context != EXPAND_FILES
1329 && xp->xp_context != EXPAND_SHELLCMD
1330 && xp->xp_context != EXPAND_DIRECTORIES)
1331 return FALSE;
1332
1333 end = gettail(xp->xp_pattern);
1334 if (end == xp->xp_pattern) // there is no path separator
1335 return FALSE;
1336
1337 for (s = xp->xp_pattern; s < end; s++)
1338 {
1339 // Skip escaped wildcards. Only when the backslash is not a path
1340 // separator, on DOS the '*' "path\*\file" must not be skipped.
1341 if (rem_backslash(s))
1342 ++s;
1343 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1344 return FALSE;
1345 }
1346 return TRUE;
1347}
1348
1349/*
1350 * Prepare a string for expansion.
1351 * When expanding file names: The string will be used with expand_wildcards().
1352 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1353 * When expanding other names: The string will be used with regcomp(). Copy
1354 * the name into allocated memory and prepend "^".
1355 */
1356 char_u *
1357addstar(
1358 char_u *fname,
1359 int len,
1360 int context) // EXPAND_FILES etc.
1361{
1362 char_u *retval;
1363 int i, j;
1364 int new_len;
1365 char_u *tail;
1366 int ends_in_star;
1367
1368 if (context != EXPAND_FILES
1369 && context != EXPAND_FILES_IN_PATH
1370 && context != EXPAND_SHELLCMD
1371 && context != EXPAND_DIRECTORIES)
1372 {
1373 // Matching will be done internally (on something other than files).
1374 // So we convert the file-matching-type wildcards into our kind for
1375 // use with vim_regcomp(). First work out how long it will be:
1376
1377 // For help tags the translation is done in find_help_tags().
1378 // For a tag pattern starting with "/" no translation is needed.
1379 if (context == EXPAND_HELP
1380 || context == EXPAND_COLORS
1381 || context == EXPAND_COMPILER
1382 || context == EXPAND_OWNSYNTAX
1383 || context == EXPAND_FILETYPE
1384 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001385 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001386 || ((context == EXPAND_TAGS_LISTFILES
1387 || context == EXPAND_TAGS)
1388 && fname[0] == '/'))
1389 retval = vim_strnsave(fname, len);
1390 else
1391 {
1392 new_len = len + 2; // +2 for '^' at start, NUL at end
1393 for (i = 0; i < len; i++)
1394 {
1395 if (fname[i] == '*' || fname[i] == '~')
1396 new_len++; // '*' needs to be replaced by ".*"
1397 // '~' needs to be replaced by "\~"
1398
1399 // Buffer names are like file names. "." should be literal
1400 if (context == EXPAND_BUFFERS && fname[i] == '.')
1401 new_len++; // "." becomes "\."
1402
1403 // Custom expansion takes care of special things, match
1404 // backslashes literally (perhaps also for other types?)
1405 if ((context == EXPAND_USER_DEFINED
1406 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1407 new_len++; // '\' becomes "\\"
1408 }
1409 retval = alloc(new_len);
1410 if (retval != NULL)
1411 {
1412 retval[0] = '^';
1413 j = 1;
1414 for (i = 0; i < len; i++, j++)
1415 {
1416 // Skip backslash. But why? At least keep it for custom
1417 // expansion.
1418 if (context != EXPAND_USER_DEFINED
1419 && context != EXPAND_USER_LIST
1420 && fname[i] == '\\'
1421 && ++i == len)
1422 break;
1423
1424 switch (fname[i])
1425 {
1426 case '*': retval[j++] = '.';
1427 break;
1428 case '~': retval[j++] = '\\';
1429 break;
1430 case '?': retval[j] = '.';
1431 continue;
1432 case '.': if (context == EXPAND_BUFFERS)
1433 retval[j++] = '\\';
1434 break;
1435 case '\\': if (context == EXPAND_USER_DEFINED
1436 || context == EXPAND_USER_LIST)
1437 retval[j++] = '\\';
1438 break;
1439 }
1440 retval[j] = fname[i];
1441 }
1442 retval[j] = NUL;
1443 }
1444 }
1445 }
1446 else
1447 {
1448 retval = alloc(len + 4);
1449 if (retval != NULL)
1450 {
1451 vim_strncpy(retval, fname, len);
1452
1453 // Don't add a star to *, ~, ~user, $var or `cmd`.
1454 // * would become **, which walks the whole tree.
1455 // ~ would be at the start of the file name, but not the tail.
1456 // $ could be anywhere in the tail.
1457 // ` could be anywhere in the file name.
1458 // When the name ends in '$' don't add a star, remove the '$'.
1459 tail = gettail(retval);
1460 ends_in_star = (len > 0 && retval[len - 1] == '*');
1461#ifndef BACKSLASH_IN_FILENAME
1462 for (i = len - 2; i >= 0; --i)
1463 {
1464 if (retval[i] != '\\')
1465 break;
1466 ends_in_star = !ends_in_star;
1467 }
1468#endif
1469 if ((*retval != '~' || tail != retval)
1470 && !ends_in_star
1471 && vim_strchr(tail, '$') == NULL
1472 && vim_strchr(retval, '`') == NULL)
1473 retval[len++] = '*';
1474 else if (len > 0 && retval[len - 1] == '$')
1475 --len;
1476 retval[len] = NUL;
1477 }
1478 }
1479 return retval;
1480}
1481
1482/*
1483 * Must parse the command line so far to work out what context we are in.
1484 * Completion can then be done based on that context.
1485 * This routine sets the variables:
1486 * xp->xp_pattern The start of the pattern to be expanded within
1487 * the command line (ends at the cursor).
1488 * xp->xp_context The type of thing to expand. Will be one of:
1489 *
1490 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1491 * the command line, like an unknown command. Caller
1492 * should beep.
1493 * EXPAND_NOTHING Unrecognised context for completion, use char like
1494 * a normal char, rather than for completion. eg
1495 * :s/^I/
1496 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1497 * it.
1498 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1499 * EXPAND_FILES After command with EX_XFILE set, or after setting
1500 * with P_EXPAND set. eg :e ^I, :w>>^I
1501 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001502 * when we know only directories are of interest.
1503 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001504 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1505 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1506 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1507 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1508 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1509 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1510 * EXPAND_EVENTS Complete event names
1511 * EXPAND_SYNTAX Complete :syntax command arguments
1512 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1513 * EXPAND_AUGROUP Complete autocommand group names
1514 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1515 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1516 * eg :unmap a^I , :cunab x^I
1517 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1518 * eg :call sub^I
1519 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1520 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1521 * names in expressions, eg :while s^I
1522 * EXPAND_ENV_VARS Complete environment variable names
1523 * EXPAND_USER Complete user names
1524 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001525 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001526set_expand_context(expand_T *xp)
1527{
1528 cmdline_info_T *ccline = get_cmdline_info();
1529
1530 // only expansion for ':', '>' and '=' command-lines
1531 if (ccline->cmdfirstc != ':'
1532#ifdef FEAT_EVAL
1533 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1534 && !ccline->input_fn
1535#endif
1536 )
1537 {
1538 xp->xp_context = EXPAND_NOTHING;
1539 return;
1540 }
1541 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1542}
1543
Bram Moolenaard0190392019-08-23 21:17:35 +02001544/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001545 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1546 * For user defined commands, the completion context is set in 'xp' and the
1547 * completion flags in 'complp'.
1548 *
1549 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001550 */
1551 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001552set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001553{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001554 char_u *p = NULL;
1555 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001556 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001557
1558 // Isolate the command and search for it in the command table.
1559 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001560 // - the 'k' command can directly be followed by any character, but do
1561 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1562 // find matches anywhere in the command name, do this only for command
1563 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001564 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001565 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001566 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001567 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001568 p = cmd + 1;
1569 }
1570 else
1571 {
1572 p = cmd;
1573 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1574 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001575 // A user command may contain digits.
1576 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1577 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001578 while (ASCII_ISALNUM(*p) || *p == '*')
1579 ++p;
1580 // for python 3.x: ":py3*" commands completion
1581 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1582 {
1583 ++p;
1584 while (ASCII_ISALPHA(*p) || *p == '*')
1585 ++p;
1586 }
1587 // check for non-alpha command
1588 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1589 ++p;
1590 len = (int)(p - cmd);
1591
1592 if (len == 0)
1593 {
1594 xp->xp_context = EXPAND_UNSUCCESSFUL;
1595 return NULL;
1596 }
1597
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001598 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001599
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001600 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001601 // Also when doing fuzzy expansion for non-shell commands, support
1602 // alphanumeric characters.
1603 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1604 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001605 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1606 ++p;
1607 }
1608
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001609 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001610 // character, complete the command name.
1611 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1612 return NULL;
1613
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001614 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001615 {
1616 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1617 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001618 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001619 p = cmd + 1;
1620 }
1621 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1622 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001623 eap->cmd = cmd;
1624 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001625 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001626 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001627 }
1628 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001629 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001630 {
1631 // Not still touching the command and it was an illegal one
1632 xp->xp_context = EXPAND_UNSUCCESSFUL;
1633 return NULL;
1634 }
1635
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001636 return p;
1637}
Bram Moolenaard0190392019-08-23 21:17:35 +02001638
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001639/*
1640 * Set the completion context for a command argument with wild card characters.
1641 */
1642 static void
1643set_context_for_wildcard_arg(
1644 exarg_T *eap,
1645 char_u *arg,
1646 int usefilter,
1647 expand_T *xp,
1648 int *complp)
1649{
1650 char_u *p;
1651 int c;
1652 int in_quote = FALSE;
1653 char_u *bow = NULL; // Beginning of word
1654 int len = 0;
1655
1656 // Allow spaces within back-quotes to count as part of the argument
1657 // being expanded.
1658 xp->xp_pattern = skipwhite(arg);
1659 p = xp->xp_pattern;
1660 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001661 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001662 if (has_mbyte)
1663 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001664 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001665 c = *p;
1666 if (c == '\\' && p[1] != NUL)
1667 ++p;
1668 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001669 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001670 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001671 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001672 xp->xp_pattern = p;
1673 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001674 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001675 in_quote = !in_quote;
1676 }
1677 // An argument can contain just about everything, except
1678 // characters that end the command and white space.
1679 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001680#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001681 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001682#endif
1683 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001684 {
1685 len = 0; // avoid getting stuck when space is in 'isfname'
1686 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001687 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001688 if (has_mbyte)
1689 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001690 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001691 c = *p;
1692 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001693 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001694 if (has_mbyte)
1695 len = (*mb_ptr2len)(p);
1696 else
1697 len = 1;
1698 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001699 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001700 if (in_quote)
1701 bow = p;
1702 else
1703 xp->xp_pattern = p;
1704 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001705 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001706 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001707 }
1708
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001709 // If we are still inside the quotes, and we passed a space, just
1710 // expand from there.
1711 if (bow != NULL && in_quote)
1712 xp->xp_pattern = bow;
1713 xp->xp_context = EXPAND_FILES;
1714
1715 // For a shell command more chars need to be escaped.
1716 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1717 {
1718#ifndef BACKSLASH_IN_FILENAME
1719 xp->xp_shell = TRUE;
1720#endif
1721 // When still after the command name expand executables.
1722 if (xp->xp_pattern == skipwhite(arg))
1723 xp->xp_context = EXPAND_SHELLCMD;
1724 }
1725
1726 // Check for environment variable.
1727 if (*xp->xp_pattern == '$')
1728 {
1729 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1730 if (!vim_isIDc(*p))
1731 break;
1732 if (*p == NUL)
1733 {
1734 xp->xp_context = EXPAND_ENV_VARS;
1735 ++xp->xp_pattern;
1736 // Avoid that the assignment uses EXPAND_FILES again.
1737 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1738 *complp = EXPAND_ENV_VARS;
1739 }
1740 }
1741 // Check for user names.
1742 if (*xp->xp_pattern == '~')
1743 {
1744 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1745 ;
1746 // Complete ~user only if it partially matches a user name.
1747 // A full match ~user<Tab> will be replaced by user's home
1748 // directory i.e. something like ~user<Tab> -> /home/user/
1749 if (*p == NUL && p > xp->xp_pattern + 1
1750 && match_user(xp->xp_pattern + 1) >= 1)
1751 {
1752 xp->xp_context = EXPAND_USER;
1753 ++xp->xp_pattern;
1754 }
1755 }
1756}
1757
1758/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001759 * Set the completion context for the :filter command. Returns a pointer to the
1760 * next command after the :filter command.
1761 */
1762 static char_u *
1763set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1764{
1765 if (*arg != NUL)
1766 arg = skip_vimgrep_pat(arg, NULL, NULL);
1767 if (arg == NULL || *arg == NUL)
1768 {
1769 xp->xp_context = EXPAND_NOTHING;
1770 return NULL;
1771 }
1772 return skipwhite(arg);
1773}
1774
1775#ifdef FEAT_SEARCH_EXTRA
1776/*
1777 * Set the completion context for the :match command. Returns a pointer to the
1778 * next command after the :match command.
1779 */
1780 static char_u *
1781set_context_in_match_cmd(expand_T *xp, char_u *arg)
1782{
1783 if (*arg == NUL || !ends_excmd(*arg))
1784 {
1785 // also complete "None"
1786 set_context_in_echohl_cmd(xp, arg);
1787 arg = skipwhite(skiptowhite(arg));
1788 if (*arg != NUL)
1789 {
1790 xp->xp_context = EXPAND_NOTHING;
1791 arg = skip_regexp(arg + 1, *arg, magic_isset());
1792 }
1793 }
1794 return find_nextcmd(arg);
1795}
1796#endif
1797
1798/*
1799 * Returns a pointer to the next command after a :global or a :v command.
1800 * Returns NULL if there is no next command.
1801 */
1802 static char_u *
1803find_cmd_after_global_cmd(char_u *arg)
1804{
1805 int delim;
1806
1807 delim = *arg; // get the delimiter
1808 if (delim)
1809 ++arg; // skip delimiter if there is one
1810
1811 while (arg[0] != NUL && arg[0] != delim)
1812 {
1813 if (arg[0] == '\\' && arg[1] != NUL)
1814 ++arg;
1815 ++arg;
1816 }
1817 if (arg[0] != NUL)
1818 return arg + 1;
1819
1820 return NULL;
1821}
1822
1823/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001824 * Returns a pointer to the next command after a :substitute or a :& command.
1825 * Returns NULL if there is no next command.
1826 */
1827 static char_u *
1828find_cmd_after_substitute_cmd(char_u *arg)
1829{
1830 int delim;
1831
1832 delim = *arg;
1833 if (delim)
1834 {
1835 // skip "from" part
1836 ++arg;
1837 arg = skip_regexp(arg, delim, magic_isset());
1838
1839 if (arg[0] != NUL && arg[0] == delim)
1840 {
1841 // skip "to" part
1842 ++arg;
1843 while (arg[0] != NUL && arg[0] != delim)
1844 {
1845 if (arg[0] == '\\' && arg[1] != NUL)
1846 ++arg;
1847 ++arg;
1848 }
1849 if (arg[0] != NUL) // skip delimiter
1850 ++arg;
1851 }
1852 }
1853 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1854 ++arg;
1855 if (arg[0] != NUL)
1856 return arg;
1857
1858 return NULL;
1859}
1860
1861/*
1862 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1863 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1864 * Returns NULL if there is no next command.
1865 */
1866 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001867find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001868{
1869 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001870 if (*arg != '/')
1871 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001872
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001873 // Match regexp, not just whole words
1874 for (++arg; *arg && *arg != '/'; arg++)
1875 if (*arg == '\\' && arg[1] != NUL)
1876 arg++;
1877 if (*arg)
1878 {
1879 arg = skipwhite(arg + 1);
1880
1881 // Check for trailing illegal characters
1882 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1883 xp->xp_context = EXPAND_NOTHING;
1884 else
1885 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001886 }
1887
1888 return NULL;
1889}
1890
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001891#ifdef FEAT_EVAL
1892/*
1893 * Set the completion context for the :unlet command. Always returns NULL.
1894 */
1895 static char_u *
1896set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1897{
1898 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1899 arg = xp->xp_pattern + 1;
1900
1901 xp->xp_context = EXPAND_USER_VARS;
1902 xp->xp_pattern = arg;
1903
1904 if (*xp->xp_pattern == '$')
1905 {
1906 xp->xp_context = EXPAND_ENV_VARS;
1907 ++xp->xp_pattern;
1908 }
1909
1910 return NULL;
1911}
1912#endif
1913
1914#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1915/*
1916 * Set the completion context for the :language command. Always returns NULL.
1917 */
1918 static char_u *
1919set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1920{
1921 char_u *p;
1922
1923 p = skiptowhite(arg);
1924 if (*p == NUL)
1925 {
1926 xp->xp_context = EXPAND_LANGUAGE;
1927 xp->xp_pattern = arg;
1928 }
1929 else
1930 {
1931 if ( STRNCMP(arg, "messages", p - arg) == 0
1932 || STRNCMP(arg, "ctype", p - arg) == 0
1933 || STRNCMP(arg, "time", p - arg) == 0
1934 || STRNCMP(arg, "collate", p - arg) == 0)
1935 {
1936 xp->xp_context = EXPAND_LOCALES;
1937 xp->xp_pattern = skipwhite(p);
1938 }
1939 else
1940 xp->xp_context = EXPAND_NOTHING;
1941 }
1942
1943 return NULL;
1944}
1945#endif
1946
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001947#ifdef FEAT_EVAL
1948static enum
1949{
1950 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001951 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1952 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001953} breakpt_expand_what;
1954
1955/*
1956 * Set the completion context for the :breakadd command. Always returns NULL.
1957 */
1958 static char_u *
1959set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1960{
1961 char_u *p;
1962 char_u *subcmd_start;
1963
1964 xp->xp_context = EXPAND_BREAKPOINT;
1965 xp->xp_pattern = arg;
1966
1967 if (cmdidx == CMD_breakadd)
1968 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001969 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001970 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001971 else
1972 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001973
1974 p = skipwhite(arg);
1975 if (*p == NUL)
1976 return NULL;
1977 subcmd_start = p;
1978
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001979 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001980 {
1981 // :breakadd file [lnum] <filename>
1982 // :breakadd func [lnum] <funcname>
1983 p += 4;
1984 p = skipwhite(p);
1985
1986 // skip line number (if specified)
1987 if (VIM_ISDIGIT(*p))
1988 {
1989 p = skipdigits(p);
1990 if (*p != ' ')
1991 {
1992 xp->xp_context = EXPAND_NOTHING;
1993 return NULL;
1994 }
1995 p = skipwhite(p);
1996 }
1997 if (STRNCMP("file", subcmd_start, 4) == 0)
1998 xp->xp_context = EXPAND_FILES;
1999 else
2000 xp->xp_context = EXPAND_USER_FUNC;
2001 xp->xp_pattern = p;
2002 }
2003 else if (STRNCMP("expr ", p, 5) == 0)
2004 {
2005 // :breakadd expr <expression>
2006 xp->xp_context = EXPAND_EXPRESSION;
2007 xp->xp_pattern = skipwhite(p + 5);
2008 }
2009
2010 return NULL;
2011}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002012
2013 static char_u *
2014set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2015{
2016 char_u *p;
2017
2018 xp->xp_context = EXPAND_NOTHING;
2019 xp->xp_pattern = NULL;
2020
2021 p = skipwhite(arg);
2022 if (VIM_ISDIGIT(*p))
2023 return NULL;
2024
2025 xp->xp_context = EXPAND_SCRIPTNAMES;
2026 xp->xp_pattern = p;
2027
2028 return NULL;
2029}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002030#endif
2031
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002032/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002033 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2034 * The argument to the command is 'arg' and the argument flags is 'argt'.
2035 * For user-defined commands and for environment variables, 'compl' has the
2036 * completion type.
2037 * Returns a pointer to the next command. Returns NULL if there is no next
2038 * command.
2039 */
2040 static char_u *
2041set_context_by_cmdname(
2042 char_u *cmd,
2043 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002044 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002045 char_u *arg,
2046 long argt,
2047 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002048 int forceit)
2049{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002050 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002051 {
2052 case CMD_find:
2053 case CMD_sfind:
2054 case CMD_tabfind:
2055 if (xp->xp_context == EXPAND_FILES)
2056 xp->xp_context = EXPAND_FILES_IN_PATH;
2057 break;
2058 case CMD_cd:
2059 case CMD_chdir:
2060 case CMD_tcd:
2061 case CMD_tchdir:
2062 case CMD_lcd:
2063 case CMD_lchdir:
2064 if (xp->xp_context == EXPAND_FILES)
2065 xp->xp_context = EXPAND_DIRECTORIES;
2066 break;
2067 case CMD_help:
2068 xp->xp_context = EXPAND_HELP;
2069 xp->xp_pattern = arg;
2070 break;
2071
2072 // Command modifiers: return the argument.
2073 // Also for commands with an argument that is a command.
2074 case CMD_aboveleft:
2075 case CMD_argdo:
2076 case CMD_belowright:
2077 case CMD_botright:
2078 case CMD_browse:
2079 case CMD_bufdo:
2080 case CMD_cdo:
2081 case CMD_cfdo:
2082 case CMD_confirm:
2083 case CMD_debug:
2084 case CMD_folddoclosed:
2085 case CMD_folddoopen:
2086 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002087 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002088 case CMD_keepalt:
2089 case CMD_keepjumps:
2090 case CMD_keepmarks:
2091 case CMD_keeppatterns:
2092 case CMD_ldo:
2093 case CMD_leftabove:
2094 case CMD_lfdo:
2095 case CMD_lockmarks:
2096 case CMD_noautocmd:
2097 case CMD_noswapfile:
2098 case CMD_rightbelow:
2099 case CMD_sandbox:
2100 case CMD_silent:
2101 case CMD_tab:
2102 case CMD_tabdo:
2103 case CMD_topleft:
2104 case CMD_verbose:
2105 case CMD_vertical:
2106 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002107 case CMD_vim9cmd:
2108 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002109 return arg;
2110
2111 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002112 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002113
2114#ifdef FEAT_SEARCH_EXTRA
2115 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002116 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002117#endif
2118
2119 // All completion for the +cmdline_compl feature goes here.
2120
2121 case CMD_command:
2122 return set_context_in_user_cmd(xp, arg);
2123
2124 case CMD_delcommand:
2125 xp->xp_context = EXPAND_USER_COMMANDS;
2126 xp->xp_pattern = arg;
2127 break;
2128
2129 case CMD_global:
2130 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002131 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002132 case CMD_and:
2133 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002134 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002135 case CMD_isearch:
2136 case CMD_dsearch:
2137 case CMD_ilist:
2138 case CMD_dlist:
2139 case CMD_ijump:
2140 case CMD_psearch:
2141 case CMD_djump:
2142 case CMD_isplit:
2143 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002144 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002145 case CMD_autocmd:
2146 return set_context_in_autocmd(xp, arg, FALSE);
2147 case CMD_doautocmd:
2148 case CMD_doautoall:
2149 return set_context_in_autocmd(xp, arg, TRUE);
2150 case CMD_set:
2151 set_context_in_set_cmd(xp, arg, 0);
2152 break;
2153 case CMD_setglobal:
2154 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2155 break;
2156 case CMD_setlocal:
2157 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2158 break;
2159 case CMD_tag:
2160 case CMD_stag:
2161 case CMD_ptag:
2162 case CMD_ltag:
2163 case CMD_tselect:
2164 case CMD_stselect:
2165 case CMD_ptselect:
2166 case CMD_tjump:
2167 case CMD_stjump:
2168 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002169 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002170 xp->xp_context = EXPAND_TAGS_LISTFILES;
2171 else
2172 xp->xp_context = EXPAND_TAGS;
2173 xp->xp_pattern = arg;
2174 break;
2175 case CMD_augroup:
2176 xp->xp_context = EXPAND_AUGROUP;
2177 xp->xp_pattern = arg;
2178 break;
2179#ifdef FEAT_SYN_HL
2180 case CMD_syntax:
2181 set_context_in_syntax_cmd(xp, arg);
2182 break;
2183#endif
2184#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002185 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002186 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002187 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002188 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002189 case CMD_if:
2190 case CMD_elseif:
2191 case CMD_while:
2192 case CMD_for:
2193 case CMD_echo:
2194 case CMD_echon:
2195 case CMD_execute:
2196 case CMD_echomsg:
2197 case CMD_echoerr:
2198 case CMD_call:
2199 case CMD_return:
2200 case CMD_cexpr:
2201 case CMD_caddexpr:
2202 case CMD_cgetexpr:
2203 case CMD_lexpr:
2204 case CMD_laddexpr:
2205 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002206 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002207 break;
2208
2209 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002210 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002211 case CMD_function:
2212 case CMD_delfunction:
2213 xp->xp_context = EXPAND_USER_FUNC;
2214 xp->xp_pattern = arg;
2215 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002216 case CMD_disassemble:
2217 set_context_in_disassemble_cmd(xp, arg);
2218 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002219
2220 case CMD_echohl:
2221 set_context_in_echohl_cmd(xp, arg);
2222 break;
2223#endif
2224 case CMD_highlight:
2225 set_context_in_highlight_cmd(xp, arg);
2226 break;
2227#ifdef FEAT_CSCOPE
2228 case CMD_cscope:
2229 case CMD_lcscope:
2230 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002231 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002232 break;
2233#endif
2234#ifdef FEAT_SIGNS
2235 case CMD_sign:
2236 set_context_in_sign_cmd(xp, arg);
2237 break;
2238#endif
2239 case CMD_bdelete:
2240 case CMD_bwipeout:
2241 case CMD_bunload:
2242 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2243 arg = xp->xp_pattern + 1;
2244 // FALLTHROUGH
2245 case CMD_buffer:
2246 case CMD_sbuffer:
2247 case CMD_checktime:
2248 xp->xp_context = EXPAND_BUFFERS;
2249 xp->xp_pattern = arg;
2250 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002251#ifdef FEAT_DIFF
2252 case CMD_diffget:
2253 case CMD_diffput:
2254 // If current buffer is in diff mode, complete buffer names
2255 // which are in diff mode, and different than current buffer.
2256 xp->xp_context = EXPAND_DIFF_BUFFERS;
2257 xp->xp_pattern = arg;
2258 break;
2259#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002260 case CMD_USER:
2261 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002262 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2263 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002264
2265 case CMD_map: case CMD_noremap:
2266 case CMD_nmap: case CMD_nnoremap:
2267 case CMD_vmap: case CMD_vnoremap:
2268 case CMD_omap: case CMD_onoremap:
2269 case CMD_imap: case CMD_inoremap:
2270 case CMD_cmap: case CMD_cnoremap:
2271 case CMD_lmap: case CMD_lnoremap:
2272 case CMD_smap: case CMD_snoremap:
2273 case CMD_tmap: case CMD_tnoremap:
2274 case CMD_xmap: case CMD_xnoremap:
2275 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002276 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002277 case CMD_unmap:
2278 case CMD_nunmap:
2279 case CMD_vunmap:
2280 case CMD_ounmap:
2281 case CMD_iunmap:
2282 case CMD_cunmap:
2283 case CMD_lunmap:
2284 case CMD_sunmap:
2285 case CMD_tunmap:
2286 case CMD_xunmap:
2287 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002288 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002289 case CMD_mapclear:
2290 case CMD_nmapclear:
2291 case CMD_vmapclear:
2292 case CMD_omapclear:
2293 case CMD_imapclear:
2294 case CMD_cmapclear:
2295 case CMD_lmapclear:
2296 case CMD_smapclear:
2297 case CMD_tmapclear:
2298 case CMD_xmapclear:
2299 xp->xp_context = EXPAND_MAPCLEAR;
2300 xp->xp_pattern = arg;
2301 break;
2302
2303 case CMD_abbreviate: case CMD_noreabbrev:
2304 case CMD_cabbrev: case CMD_cnoreabbrev:
2305 case CMD_iabbrev: case CMD_inoreabbrev:
2306 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002307 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002308 case CMD_unabbreviate:
2309 case CMD_cunabbrev:
2310 case CMD_iunabbrev:
2311 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002312 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002313#ifdef FEAT_MENU
2314 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2315 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2316 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2317 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2318 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2319 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2320 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2321 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2322 case CMD_tmenu: case CMD_tunmenu:
2323 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2324 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2325#endif
2326
2327 case CMD_colorscheme:
2328 xp->xp_context = EXPAND_COLORS;
2329 xp->xp_pattern = arg;
2330 break;
2331
2332 case CMD_compiler:
2333 xp->xp_context = EXPAND_COMPILER;
2334 xp->xp_pattern = arg;
2335 break;
2336
2337 case CMD_ownsyntax:
2338 xp->xp_context = EXPAND_OWNSYNTAX;
2339 xp->xp_pattern = arg;
2340 break;
2341
2342 case CMD_setfiletype:
2343 xp->xp_context = EXPAND_FILETYPE;
2344 xp->xp_pattern = arg;
2345 break;
2346
2347 case CMD_packadd:
2348 xp->xp_context = EXPAND_PACKADD;
2349 xp->xp_pattern = arg;
2350 break;
2351
zeertzjqb0d45ec2023-01-25 15:04:22 +00002352 case CMD_runtime:
2353 set_context_in_runtime_cmd(xp, arg);
2354 break;
2355
Bram Moolenaard0190392019-08-23 21:17:35 +02002356#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2357 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002358 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002359#endif
2360#if defined(FEAT_PROFILE)
2361 case CMD_profile:
2362 set_context_in_profile_cmd(xp, arg);
2363 break;
2364#endif
2365 case CMD_behave:
2366 xp->xp_context = EXPAND_BEHAVE;
2367 xp->xp_pattern = arg;
2368 break;
2369
2370 case CMD_messages:
2371 xp->xp_context = EXPAND_MESSAGES;
2372 xp->xp_pattern = arg;
2373 break;
2374
2375 case CMD_history:
2376 xp->xp_context = EXPAND_HISTORY;
2377 xp->xp_pattern = arg;
2378 break;
2379#if defined(FEAT_PROFILE)
2380 case CMD_syntime:
2381 xp->xp_context = EXPAND_SYNTIME;
2382 xp->xp_pattern = arg;
2383 break;
2384#endif
2385
2386 case CMD_argdelete:
2387 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2388 arg = xp->xp_pattern + 1;
2389 xp->xp_context = EXPAND_ARGLIST;
2390 xp->xp_pattern = arg;
2391 break;
2392
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002393#ifdef FEAT_EVAL
2394 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002395 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002396 case CMD_breakdel:
2397 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002398
2399 case CMD_scriptnames:
2400 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002401#endif
2402
Bram Moolenaard0190392019-08-23 21:17:35 +02002403 default:
2404 break;
2405 }
2406 return NULL;
2407}
2408
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002409/*
2410 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2411 * we don't need/want deleted. Maybe this could be done better if we didn't
2412 * repeat all this stuff. The only problem is that they may not stay
2413 * perfectly compatible with each other, but then the command line syntax
2414 * probably won't change that much -- webb.
2415 */
2416 static char_u *
2417set_one_cmd_context(
2418 expand_T *xp,
2419 char_u *buff) // buffer for command string
2420{
2421 char_u *p;
2422 char_u *cmd, *arg;
2423 int len = 0;
2424 exarg_T ea;
2425 int compl = EXPAND_NOTHING;
2426 int forceit = FALSE;
2427 int usefilter = FALSE; // filter instead of file name
2428
2429 ExpandInit(xp);
2430 xp->xp_pattern = buff;
2431 xp->xp_line = buff;
2432 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2433 ea.argt = 0;
2434
2435 // 1. skip comment lines and leading space, colons or bars
2436 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2437 ;
2438 xp->xp_pattern = cmd;
2439
2440 if (*cmd == NUL)
2441 return NULL;
2442 if (*cmd == '"') // ignore comment lines
2443 {
2444 xp->xp_context = EXPAND_NOTHING;
2445 return NULL;
2446 }
2447
2448 // 3. Skip over the range to find the command.
2449 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2450 xp->xp_pattern = cmd;
2451 if (*cmd == NUL)
2452 return NULL;
2453 if (*cmd == '"')
2454 {
2455 xp->xp_context = EXPAND_NOTHING;
2456 return NULL;
2457 }
2458
2459 if (*cmd == '|' || *cmd == '\n')
2460 return cmd + 1; // There's another command
2461
2462 // Get the command index.
2463 p = set_cmd_index(cmd, &ea, xp, &compl);
2464 if (p == NULL)
2465 return NULL;
2466
2467 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2468
2469 if (*p == '!') // forced commands
2470 {
2471 forceit = TRUE;
2472 ++p;
2473 }
2474
2475 // 6. parse arguments
2476 if (!IS_USER_CMDIDX(ea.cmdidx))
2477 ea.argt = excmd_get_argt(ea.cmdidx);
2478
2479 arg = skipwhite(p);
2480
2481 // Skip over ++argopt argument
2482 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2483 {
2484 p = arg;
2485 while (*p && !vim_isspace(*p))
2486 MB_PTR_ADV(p);
2487 arg = skipwhite(p);
2488 }
2489
2490 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2491 {
2492 if (*arg == '>') // append
2493 {
2494 if (*++arg == '>')
2495 ++arg;
2496 arg = skipwhite(arg);
2497 }
2498 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2499 {
2500 ++arg;
2501 usefilter = TRUE;
2502 }
2503 }
2504
2505 if (ea.cmdidx == CMD_read)
2506 {
2507 usefilter = forceit; // :r! filter if forced
2508 if (*arg == '!') // :r !filter
2509 {
2510 ++arg;
2511 usefilter = TRUE;
2512 }
2513 }
2514
2515 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2516 {
2517 while (*arg == *cmd) // allow any number of '>' or '<'
2518 ++arg;
2519 arg = skipwhite(arg);
2520 }
2521
2522 // Does command allow "+command"?
2523 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2524 {
2525 // Check if we're in the +command
2526 p = arg + 1;
2527 arg = skip_cmd_arg(arg, FALSE);
2528
2529 // Still touching the command after '+'?
2530 if (*arg == NUL)
2531 return p;
2532
2533 // Skip space(s) after +command to get to the real argument
2534 arg = skipwhite(arg);
2535 }
2536
2537
2538 // Check for '|' to separate commands and '"' to start comments.
2539 // Don't do this for ":read !cmd" and ":write !cmd".
2540 if ((ea.argt & EX_TRLBAR) && !usefilter)
2541 {
2542 p = arg;
2543 // ":redir @" is not the start of a comment
2544 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2545 p += 2;
2546 while (*p)
2547 {
2548 if (*p == Ctrl_V)
2549 {
2550 if (p[1] != NUL)
2551 ++p;
2552 }
2553 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2554 || *p == '|' || *p == '\n')
2555 {
2556 if (*(p - 1) != '\\')
2557 {
2558 if (*p == '|' || *p == '\n')
2559 return p + 1;
2560 return NULL; // It's a comment
2561 }
2562 }
2563 MB_PTR_ADV(p);
2564 }
2565 }
2566
2567 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2568 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2569 // no arguments allowed but there is something
2570 return NULL;
2571
2572 // Find start of last argument (argument just before cursor):
2573 p = buff;
2574 xp->xp_pattern = p;
2575 len = (int)STRLEN(buff);
2576 while (*p && p < buff + len)
2577 {
2578 if (*p == ' ' || *p == TAB)
2579 {
2580 // argument starts after a space
2581 xp->xp_pattern = ++p;
2582 }
2583 else
2584 {
2585 if (*p == '\\' && *(p + 1) != NUL)
2586 ++p; // skip over escaped character
2587 MB_PTR_ADV(p);
2588 }
2589 }
2590
2591 if (ea.argt & EX_XFILE)
2592 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2593
2594 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002595 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002596 forceit);
2597}
2598
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002599/*
2600 * Set the completion context in 'xp' for command 'str'
2601 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002602 void
2603set_cmd_context(
2604 expand_T *xp,
2605 char_u *str, // start of command line
2606 int len, // length of command line (excl. NUL)
2607 int col, // position of cursor
2608 int use_ccline UNUSED) // use ccline for info
2609{
2610#ifdef FEAT_EVAL
2611 cmdline_info_T *ccline = get_cmdline_info();
2612#endif
2613 int old_char = NUL;
2614 char_u *nextcomm;
2615
2616 // Avoid a UMR warning from Purify, only save the character if it has been
2617 // written before.
2618 if (col < len)
2619 old_char = str[col];
2620 str[col] = NUL;
2621 nextcomm = str;
2622
2623#ifdef FEAT_EVAL
2624 if (use_ccline && ccline->cmdfirstc == '=')
2625 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002626 // pass CMD_SIZE because there is no real command
2627 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002628 }
2629 else if (use_ccline && ccline->input_fn)
2630 {
2631 xp->xp_context = ccline->xp_context;
2632 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002633 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002634 }
2635 else
2636#endif
2637 while (nextcomm != NULL)
2638 nextcomm = set_one_cmd_context(xp, nextcomm);
2639
2640 // Store the string here so that call_user_expand_func() can get to them
2641 // easily.
2642 xp->xp_line = str;
2643 xp->xp_col = col;
2644
2645 str[col] = old_char;
2646}
2647
2648/*
2649 * Expand the command line "str" from context "xp".
2650 * "xp" must have been set by set_cmd_context().
2651 * xp->xp_pattern points into "str", to where the text that is to be expanded
2652 * starts.
2653 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2654 * cursor.
2655 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2656 * key that triggered expansion literally.
2657 * Returns EXPAND_OK otherwise.
2658 */
2659 int
2660expand_cmdline(
2661 expand_T *xp,
2662 char_u *str, // start of command line
2663 int col, // position of cursor
2664 int *matchcount, // return: nr of matches
2665 char_u ***matches) // return: array of pointers to matches
2666{
2667 char_u *file_str = NULL;
2668 int options = WILD_ADD_SLASH|WILD_SILENT;
2669
2670 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2671 {
2672 beep_flush();
2673 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2674 }
2675 if (xp->xp_context == EXPAND_NOTHING)
2676 {
2677 // Caller can use the character as a normal char instead
2678 return EXPAND_NOTHING;
2679 }
2680
2681 // add star to file name, or convert to regexp if not exp. files.
2682 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002683 if (cmdline_fuzzy_completion_supported(xp))
2684 // If fuzzy matching, don't modify the search string
2685 file_str = vim_strsave(xp->xp_pattern);
2686 else
2687 {
2688 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2689 if (file_str == NULL)
2690 return EXPAND_UNSUCCESSFUL;
2691 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002692
2693 if (p_wic)
2694 options += WILD_ICASE;
2695
2696 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002697 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002698 {
2699 *matchcount = 0;
2700 *matches = NULL;
2701 }
2702 vim_free(file_str);
2703
2704 return EXPAND_OK;
2705}
2706
Bram Moolenaar66b51422019-08-18 21:44:12 +02002707/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002708 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002709 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002710 */
2711 static int
2712expand_files_and_dirs(
2713 expand_T *xp,
2714 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002715 char_u ***matches,
2716 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002717 int flags,
2718 int options)
2719{
2720 int free_pat = FALSE;
2721 int i;
2722 int ret;
2723
2724 // for ":set path=" and ":set tags=" halve backslashes for escaped
2725 // space
2726 if (xp->xp_backslash != XP_BS_NONE)
2727 {
2728 free_pat = TRUE;
2729 pat = vim_strsave(pat);
2730 for (i = 0; pat[i]; ++i)
2731 if (pat[i] == '\\')
2732 {
2733 if (xp->xp_backslash == XP_BS_THREE
2734 && pat[i + 1] == '\\'
2735 && pat[i + 2] == '\\'
2736 && pat[i + 3] == ' ')
2737 STRMOVE(pat + i, pat + i + 3);
2738 if (xp->xp_backslash == XP_BS_ONE
2739 && pat[i + 1] == ' ')
2740 STRMOVE(pat + i, pat + i + 1);
2741 }
2742 }
2743
2744 if (xp->xp_context == EXPAND_FILES)
2745 flags |= EW_FILE;
2746 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2747 flags |= (EW_FILE | EW_PATH);
2748 else
2749 flags = (flags | EW_DIR) & ~EW_FILE;
2750 if (options & WILD_ICASE)
2751 flags |= EW_ICASE;
2752
2753 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002754 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002755 if (free_pat)
2756 vim_free(pat);
2757#ifdef BACKSLASH_IN_FILENAME
2758 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2759 {
2760 int j;
2761
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002762 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002763 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002764 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002765
2766 while (*ptr != NUL)
2767 {
2768 if (p_csl[0] == 's' && *ptr == '\\')
2769 *ptr = '/';
2770 else if (p_csl[0] == 'b' && *ptr == '/')
2771 *ptr = '\\';
2772 ptr += (*mb_ptr2len)(ptr);
2773 }
2774 }
2775 }
2776#endif
2777 return ret;
2778}
2779
2780/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002781 * Function given to ExpandGeneric() to obtain the possible arguments of the
2782 * ":behave {mswin,xterm}" command.
2783 */
2784 static char_u *
2785get_behave_arg(expand_T *xp UNUSED, int idx)
2786{
2787 if (idx == 0)
2788 return (char_u *)"mswin";
2789 if (idx == 1)
2790 return (char_u *)"xterm";
2791 return NULL;
2792}
2793
K.Takata161b6ac2022-11-14 15:31:07 +00002794#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002795/*
2796 * Function given to ExpandGeneric() to obtain the possible arguments of the
2797 * ":breakadd {expr, file, func, here}" command.
2798 * ":breakdel {func, file, here}" command.
2799 */
2800 static char_u *
2801get_breakadd_arg(expand_T *xp UNUSED, int idx)
2802{
2803 char *opts[] = {"expr", "file", "func", "here"};
2804
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002805 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002806 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002807 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002808 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2809 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002810 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2811 {
2812 // breakdel {func, file, here}
2813 if (idx <= 2)
2814 return (char_u *)opts[idx + 1];
2815 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002816 else
2817 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002818 // profdel {func, file}
2819 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002820 return (char_u *)opts[idx + 1];
2821 }
2822 }
2823 return NULL;
2824}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002825
2826/*
2827 * Function given to ExpandGeneric() to obtain the possible arguments for the
2828 * ":scriptnames" command.
2829 */
2830 static char_u *
2831get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2832{
2833 scriptitem_T *si;
2834
2835 if (!SCRIPT_ID_VALID(idx + 1))
2836 return NULL;
2837
2838 si = SCRIPT_ITEM(idx + 1);
2839 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2840 return NameBuff;
2841}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002842#endif
2843
Bram Moolenaard0190392019-08-23 21:17:35 +02002844/*
2845 * Function given to ExpandGeneric() to obtain the possible arguments of the
2846 * ":messages {clear}" command.
2847 */
2848 static char_u *
2849get_messages_arg(expand_T *xp UNUSED, int idx)
2850{
2851 if (idx == 0)
2852 return (char_u *)"clear";
2853 return NULL;
2854}
2855
2856 static char_u *
2857get_mapclear_arg(expand_T *xp UNUSED, int idx)
2858{
2859 if (idx == 0)
2860 return (char_u *)"<buffer>";
2861 return NULL;
2862}
2863
2864/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002865 * Do the expansion based on xp->xp_context and 'rmp'.
2866 */
2867 static int
2868ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002869 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002870 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002871 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002872 char_u ***matches,
2873 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002874{
2875 static struct expgen
2876 {
2877 int context;
2878 char_u *((*func)(expand_T *, int));
2879 int ic;
2880 int escaped;
2881 } tab[] =
2882 {
2883 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2884 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2885 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2886 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2887 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2888 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2889 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2890 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2891 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2892 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002893#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002894 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2895 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2896 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2897 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2898 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002899#endif
2900#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002901 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2902 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002903#endif
2904#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002905 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002906#endif
2907#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002908 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002909#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002910 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2911 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2912 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002913#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002914 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002915#endif
2916#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002917 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002918#endif
2919#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002920 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002921#endif
2922#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002923 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2924 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002925#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002926 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2927 {EXPAND_USER, get_users, TRUE, FALSE},
2928 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002929#ifdef FEAT_EVAL
2930 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002931 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002932#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002933 };
2934 int i;
2935 int ret = FAIL;
2936
2937 // Find a context in the table and call the ExpandGeneric() with the
2938 // right function to do the expansion.
2939 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2940 {
2941 if (xp->xp_context == tab[i].context)
2942 {
2943 if (tab[i].ic)
2944 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002945 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2946 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002947 break;
2948 }
2949 }
2950
2951 return ret;
2952}
2953
2954/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002955 * Map wild expand options to flags for expand_wildcards()
2956 */
2957 static int
2958map_wildopts_to_ewflags(int options)
2959{
2960 int flags;
2961
2962 flags = EW_DIR; // include directories
2963 if (options & WILD_LIST_NOTFOUND)
2964 flags |= EW_NOTFOUND;
2965 if (options & WILD_ADD_SLASH)
2966 flags |= EW_ADDSLASH;
2967 if (options & WILD_KEEP_ALL)
2968 flags |= EW_KEEPALL;
2969 if (options & WILD_SILENT)
2970 flags |= EW_SILENT;
2971 if (options & WILD_NOERROR)
2972 flags |= EW_NOERROR;
2973 if (options & WILD_ALLLINKS)
2974 flags |= EW_ALLLINKS;
2975
2976 return flags;
2977}
2978
2979/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002980 * Do the expansion based on xp->xp_context and "pat".
2981 */
2982 static int
2983ExpandFromContext(
2984 expand_T *xp,
2985 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002986 char_u ***matches,
2987 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002988 int options) // WILD_ flags
2989{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002990 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002991 int ret;
2992 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002993 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002994 int fuzzy = cmdline_fuzzy_complete(pat)
2995 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002996
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002997 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002998
2999 if (xp->xp_context == EXPAND_FILES
3000 || xp->xp_context == EXPAND_DIRECTORIES
3001 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003002 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3003 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003004
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003005 *matches = (char_u **)"";
3006 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003007 if (xp->xp_context == EXPAND_HELP)
3008 {
3009 // With an empty argument we would get all the help tags, which is
3010 // very slow. Get matches for "help" instead.
3011 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003012 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003013 {
3014#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003015 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003016#endif
3017 return OK;
3018 }
3019 return FAIL;
3020 }
3021
Bram Moolenaar66b51422019-08-18 21:44:12 +02003022 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003023 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003024 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003025 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003026 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003027 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003028#ifdef FEAT_DIFF
3029 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003030 return ExpandBufnames(pat, numMatches, matches,
3031 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003032#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003033 if (xp->xp_context == EXPAND_TAGS
3034 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003035 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3036 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003037 if (xp->xp_context == EXPAND_COLORS)
3038 {
3039 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003040 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003041 directories);
3042 }
3043 if (xp->xp_context == EXPAND_COMPILER)
3044 {
3045 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003046 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003047 }
3048 if (xp->xp_context == EXPAND_OWNSYNTAX)
3049 {
3050 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003051 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003052 }
3053 if (xp->xp_context == EXPAND_FILETYPE)
3054 {
3055 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003056 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003057 }
K.Takata161b6ac2022-11-14 15:31:07 +00003058#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003059 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003060 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003061#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003062 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003063 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003064 if (xp->xp_context == EXPAND_RUNTIME)
3065 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003066
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003067 // When expanding a function name starting with s:, match the <SNR>nr_
3068 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003069 if ((xp->xp_context == EXPAND_USER_FUNC
3070 || xp->xp_context == EXPAND_DISASSEMBLE)
3071 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003072 {
3073 int len = (int)STRLEN(pat) + 20;
3074
3075 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003076 if (tofree == NULL)
3077 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003078 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003079 pat = tofree;
3080 }
3081
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003082 if (!fuzzy)
3083 {
3084 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3085 if (regmatch.regprog == NULL)
3086 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003087
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003088 // set ignore-case according to p_ic, p_scs and pat
3089 regmatch.rm_ic = ignorecase(pat);
3090 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003091
3092 if (xp->xp_context == EXPAND_SETTINGS
3093 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003094 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003095 else if (xp->xp_context == EXPAND_STRING_SETTING)
3096 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3097 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3098 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003099 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003100 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003101#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003103 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003104#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003105 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003106 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003107
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003108 if (!fuzzy)
3109 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003110 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111
3112 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113}
3114
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115/*
3116 * Expand a list of names.
3117 *
3118 * Generic function for command line completion. It calls a function to
3119 * obtain strings, one by one. The strings are matched against a regexp
3120 * program. Matching strings are copied into an array, which is returned.
3121 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003122 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3123 * is used.
3124 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125 * Returns OK when no problems encountered, FAIL for error (out of memory).
3126 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003127 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003129 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130 expand_T *xp,
3131 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003132 char_u ***matches,
3133 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003134 char_u *((*func)(expand_T *, int)),
3135 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003136 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137{
3138 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003139 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003141 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003142 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003143 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003144 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003145 int sort_matches = FALSE;
3146 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003148 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003149 *matches = NULL;
3150 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003151
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003152 if (!fuzzy)
3153 ga_init2(&ga, sizeof(char *), 30);
3154 else
3155 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3156
3157 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003158 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003159 str = (*func)(xp, i);
3160 if (str == NULL) // end of list
3161 break;
3162 if (*str == NUL) // skip empty strings
3163 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003164
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003165 if (xp->xp_pattern[0] != NUL)
3166 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003167 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003168 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003169 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003170 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003171 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003172 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003173 }
3174 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003175 else
3176 match = TRUE;
3177
3178 if (!match)
3179 continue;
3180
3181 if (escaped)
3182 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3183 else
3184 str = vim_strsave(str);
3185 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003186 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003187 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003188 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003189 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003190 return FAIL;
3191 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003192 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003193 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003194 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003195
3196 if (ga_grow(&ga, 1) == FAIL)
3197 {
3198 vim_free(str);
3199 break;
3200 }
3201
3202 if (fuzzy)
3203 {
3204 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3205 fuzmatch->idx = ga.ga_len;
3206 fuzmatch->str = str;
3207 fuzmatch->score = score;
3208 }
3209 else
3210 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3211
K.Takata161b6ac2022-11-14 15:31:07 +00003212#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003213 if (func == get_menu_names)
3214 {
3215 // test for separator added by get_menu_names()
3216 str += STRLEN(str) - 1;
3217 if (*str == '\001')
3218 *str = '.';
3219 }
K.Takata161b6ac2022-11-14 15:31:07 +00003220#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003221
3222 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003223 }
3224
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003225 if (ga.ga_len == 0)
3226 return OK;
3227
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003228 // sort the matches when using regular expression matching and sorting
3229 // applies to the completion context. Menus and scriptnames should be kept
3230 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003231 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003232 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003233 && xp->xp_context != EXPAND_MENUS
3234 && xp->xp_context != EXPAND_SCRIPTNAMES)
3235 sort_matches = TRUE;
3236
3237 // <SNR> functions should be sorted to the end.
3238 if (xp->xp_context == EXPAND_EXPRESSION
3239 || xp->xp_context == EXPAND_FUNCTIONS
3240 || xp->xp_context == EXPAND_USER_FUNC
3241 || xp->xp_context == EXPAND_DISASSEMBLE)
3242 funcsort = TRUE;
3243
3244 // Sort the matches.
3245 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003247 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003248 // <SNR> functions should be sorted to the end.
3249 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3250 sort_func_compare);
3251 else
3252 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3253 }
3254
3255 if (!fuzzy)
3256 {
3257 *matches = ga.ga_data;
3258 *numMatches = ga.ga_len;
3259 }
3260 else
3261 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003262 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3263 funcsort) == FAIL)
3264 return FAIL;
3265 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003266 }
3267
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003268#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003269 // Reset the variables used for special highlight names expansion, so that
3270 // they don't show up when getting normal highlight names by ID.
3271 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003272#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003273
Bram Moolenaar66b51422019-08-18 21:44:12 +02003274 return OK;
3275}
3276
3277/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003278 * Expand shell command matches in one directory of $PATH.
3279 */
3280 static void
3281expand_shellcmd_onedir(
3282 char_u *buf,
3283 char_u *s,
3284 size_t l,
3285 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003286 char_u ***matches,
3287 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003288 int flags,
3289 hashtab_T *ht,
3290 garray_T *gap)
3291{
3292 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003293 hash_T hash;
3294 hashitem_T *hi;
3295
3296 vim_strncpy(buf, s, l);
3297 add_pathsep(buf);
3298 l = STRLEN(buf);
3299 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3300
3301 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003302 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003303 if (ret != OK)
3304 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003305
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003306 if (ga_grow(gap, *numMatches) == FAIL)
3307 {
3308 FreeWild(*numMatches, *matches);
3309 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003310 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003311
3312 for (int i = 0; i < *numMatches; ++i)
3313 {
3314 char_u *name = (*matches)[i];
3315
3316 if (STRLEN(name) > l)
3317 {
3318 // Check if this name was already found.
3319 hash = hash_hash(name + l);
3320 hi = hash_lookup(ht, name + l, hash);
3321 if (HASHITEM_EMPTY(hi))
3322 {
3323 // Remove the path that was prepended.
3324 STRMOVE(name, name + l);
3325 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3326 hash_add_item(ht, hi, name, hash);
3327 name = NULL;
3328 }
3329 }
3330 vim_free(name);
3331 }
3332 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003333}
3334
3335/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003336 * Complete a shell command.
3337 * Returns FAIL or OK;
3338 */
3339 static int
3340expand_shellcmd(
3341 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003342 char_u ***matches, // return: array with matches
3343 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003344 int flagsarg) // EW_ flags
3345{
3346 char_u *pat;
3347 int i;
3348 char_u *path = NULL;
3349 int mustfree = FALSE;
3350 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003351 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003352 size_t l;
3353 char_u *s, *e;
3354 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003355 int did_curdir = FALSE;
3356 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003357
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003358 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003359 if (buf == NULL)
3360 return FAIL;
3361
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003362 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003363 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003364 if (pat == NULL)
3365 {
3366 vim_free(buf);
3367 return FAIL;
3368 }
3369
Bram Moolenaar66b51422019-08-18 21:44:12 +02003370 for (i = 0; pat[i]; ++i)
3371 if (pat[i] == '\\' && pat[i + 1] == ' ')
3372 STRMOVE(pat + i, pat + i + 1);
3373
3374 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3375
3376 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3377 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3378 path = (char_u *)".";
3379 else
3380 {
3381 // For an absolute name we don't use $PATH.
3382 if (!mch_isFullName(pat))
3383 path = vim_getenv((char_u *)"PATH", &mustfree);
3384 if (path == NULL)
3385 path = (char_u *)"";
3386 }
3387
3388 // Go over all directories in $PATH. Expand matches in that directory and
3389 // collect them in "ga". When "." is not in $PATH also expand for the
3390 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003391 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003392 hash_init(&found_ht);
3393 for (s = path; ; s = e)
3394 {
K.Takata161b6ac2022-11-14 15:31:07 +00003395#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003396 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003397#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003398 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003399#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003400 if (e == NULL)
3401 e = s + STRLEN(s);
3402
3403 if (*s == NUL)
3404 {
3405 if (did_curdir)
3406 break;
3407 // Find directories in the current directory, path is empty.
3408 did_curdir = TRUE;
3409 flags |= EW_DIR;
3410 }
3411 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3412 {
3413 did_curdir = TRUE;
3414 flags |= EW_DIR;
3415 }
3416 else
3417 // Do not match directories inside a $PATH item.
3418 flags &= ~EW_DIR;
3419
3420 l = e - s;
3421 if (l > MAXPATHL - 5)
3422 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003423
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003424 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003425 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003426
Bram Moolenaar66b51422019-08-18 21:44:12 +02003427 if (*e != NUL)
3428 ++e;
3429 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003430 *matches = ga.ga_data;
3431 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003432
3433 vim_free(buf);
3434 vim_free(pat);
3435 if (mustfree)
3436 vim_free(path);
3437 hash_clear(&found_ht);
3438 return OK;
3439}
3440
K.Takata161b6ac2022-11-14 15:31:07 +00003441#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003442/*
3443 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003444 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003445 */
3446 static void *
3447call_user_expand_func(
3448 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003449 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003450{
3451 cmdline_info_T *ccline = get_cmdline_info();
3452 int keep = 0;
3453 typval_T args[4];
3454 sctx_T save_current_sctx = current_sctx;
3455 char_u *pat = NULL;
3456 void *ret;
3457
3458 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3459 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003460
3461 if (ccline->cmdbuff != NULL)
3462 {
3463 keep = ccline->cmdbuff[ccline->cmdlen];
3464 ccline->cmdbuff[ccline->cmdlen] = 0;
3465 }
3466
3467 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3468
3469 args[0].v_type = VAR_STRING;
3470 args[0].vval.v_string = pat;
3471 args[1].v_type = VAR_STRING;
3472 args[1].vval.v_string = xp->xp_line;
3473 args[2].v_type = VAR_NUMBER;
3474 args[2].vval.v_number = xp->xp_col;
3475 args[3].v_type = VAR_UNKNOWN;
3476
3477 current_sctx = xp->xp_script_ctx;
3478
3479 ret = user_expand_func(xp->xp_arg, 3, args);
3480
3481 current_sctx = save_current_sctx;
3482 if (ccline->cmdbuff != NULL)
3483 ccline->cmdbuff[ccline->cmdlen] = keep;
3484
3485 vim_free(pat);
3486 return ret;
3487}
3488
3489/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003490 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3491 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003492 */
3493 static int
3494ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003495 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003496 expand_T *xp,
3497 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003498 char_u ***matches,
3499 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003500{
3501 char_u *retstr;
3502 char_u *s;
3503 char_u *e;
3504 int keep;
3505 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003506 int fuzzy;
3507 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003508 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003509
3510 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003511 *matches = NULL;
3512 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003513
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003514 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003515 if (retstr == NULL)
3516 return FAIL;
3517
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003518 if (!fuzzy)
3519 ga_init2(&ga, sizeof(char *), 3);
3520 else
3521 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3522
Bram Moolenaar66b51422019-08-18 21:44:12 +02003523 for (s = retstr; *s != NUL; s = e)
3524 {
3525 e = vim_strchr(s, '\n');
3526 if (e == NULL)
3527 e = s + STRLEN(s);
3528 keep = *e;
3529 *e = NUL;
3530
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003531 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003532 {
3533 if (!fuzzy)
3534 match = vim_regexec(regmatch, s, (colnr_T)0);
3535 else
3536 {
3537 score = fuzzy_match_str(s, pat);
3538 match = (score != 0);
3539 }
3540 }
3541 else
3542 match = TRUE; // match everything
3543
Bram Moolenaar66b51422019-08-18 21:44:12 +02003544 *e = keep;
3545
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003546 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003547 {
3548 if (ga_grow(&ga, 1) == FAIL)
3549 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003550 if (!fuzzy)
3551 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3552 else
3553 {
3554 fuzmatch_str_T *fuzmatch =
3555 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003556 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003557 fuzmatch->str = vim_strnsave(s, e - s);
3558 fuzmatch->score = score;
3559 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003560 ++ga.ga_len;
3561 }
3562
3563 if (*e != NUL)
3564 ++e;
3565 }
3566 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003567
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003568 if (ga.ga_len == 0)
3569 return OK;
3570
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003571 if (!fuzzy)
3572 {
3573 *matches = ga.ga_data;
3574 *numMatches = ga.ga_len;
3575 }
3576 else
3577 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003578 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3579 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003580 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003581 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003582 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003583 return OK;
3584}
3585
3586/*
3587 * Expand names with a list returned by a function defined by the user.
3588 */
3589 static int
3590ExpandUserList(
3591 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003592 char_u ***matches,
3593 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003594{
3595 list_T *retlist;
3596 listitem_T *li;
3597 garray_T ga;
3598
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003599 *matches = NULL;
3600 *numMatches = 0;
3601 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003602 if (retlist == NULL)
3603 return FAIL;
3604
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003605 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003606 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003607 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003608 {
3609 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3610 continue; // Skip non-string items and empty strings
3611
3612 if (ga_grow(&ga, 1) == FAIL)
3613 break;
3614
3615 ((char_u **)ga.ga_data)[ga.ga_len] =
3616 vim_strsave(li->li_tv.vval.v_string);
3617 ++ga.ga_len;
3618 }
3619 list_unref(retlist);
3620
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003621 *matches = ga.ga_data;
3622 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003623 return OK;
3624}
K.Takata161b6ac2022-11-14 15:31:07 +00003625#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003626
3627/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628 * Expand "file" for all comma-separated directories in "path".
3629 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003630 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003631 */
3632 void
3633globpath(
3634 char_u *path,
3635 char_u *file,
3636 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003637 int expand_options,
3638 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003639{
3640 expand_T xpc;
3641 char_u *buf;
3642 int i;
3643 int num_p;
3644 char_u **p;
3645
3646 buf = alloc(MAXPATHL);
3647 if (buf == NULL)
3648 return;
3649
3650 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003651 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003652
3653 // Loop over all entries in {path}.
3654 while (*path != NUL)
3655 {
3656 // Copy one item of the path to buf[] and concatenate the file name.
3657 copy_option_part(&path, buf, MAXPATHL, ",");
3658 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3659 {
K.Takata161b6ac2022-11-14 15:31:07 +00003660#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003661 // Using the platform's path separator (\) makes vim incorrectly
3662 // treat it as an escape character, use '/' instead.
3663 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3664 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003665#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003666 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003667#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003668 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003669 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003670 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3671 {
3672 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3673
3674 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003675 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003676 for (i = 0; i < num_p; ++i)
3677 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003678 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003679 ++ga->ga_len;
3680 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003681 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003682 }
3683 }
3684 }
3685
3686 vim_free(buf);
3687}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688
Bram Moolenaareadee482020-09-04 15:37:31 +02003689/*
3690 * Translate some keys pressed when 'wildmenu' is used.
3691 */
3692 int
3693wildmenu_translate_key(
3694 cmdline_info_T *cclp,
3695 int key,
3696 expand_T *xp,
3697 int did_wild_list)
3698{
3699 int c = key;
3700
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003701 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003702 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003703 // When the popup menu is used for cmdline completion:
3704 // Up : go to the previous item in the menu
3705 // Down : go to the next item in the menu
3706 // Left : go to the parent directory
3707 // Right: list the files in the selected directory
3708 switch (c)
3709 {
3710 case K_UP: c = K_LEFT; break;
3711 case K_DOWN: c = K_RIGHT; break;
3712 case K_LEFT: c = K_UP; break;
3713 case K_RIGHT: c = K_DOWN; break;
3714 default: break;
3715 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003716 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003717
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003718 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003719 {
3720 if (c == K_LEFT)
3721 c = Ctrl_P;
3722 else if (c == K_RIGHT)
3723 c = Ctrl_N;
3724 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003725
Bram Moolenaareadee482020-09-04 15:37:31 +02003726 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003727 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003728 && cclp->cmdpos > 1
3729 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3730 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3731 && (c == '\n' || c == '\r' || c == K_KENTER))
3732 c = K_DOWN;
3733
3734 return c;
3735}
3736
3737/*
3738 * Delete characters on the command line, from "from" to the current
3739 * position.
3740 */
3741 static void
3742cmdline_del(cmdline_info_T *cclp, int from)
3743{
3744 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3745 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3746 cclp->cmdlen -= cclp->cmdpos - from;
3747 cclp->cmdpos = from;
3748}
3749
3750/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003751 * Handle a key pressed when the wild menu for the menu names
3752 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003753 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003754 static int
3755wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003756{
Bram Moolenaareadee482020-09-04 15:37:31 +02003757 int i;
3758 int j;
3759
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003760 // Hitting <Down> after "emenu Name.": complete submenu
3761 if (key == K_DOWN && cclp->cmdpos > 0
3762 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003763 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003764 key = p_wc;
3765 KeyTyped = TRUE; // in case the key was mapped
3766 }
3767 else if (key == K_UP)
3768 {
3769 // Hitting <Up>: Remove one submenu name in front of the
3770 // cursor
3771 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003772
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003773 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3774 i = 0;
3775 while (--j > 0)
3776 {
3777 // check for start of menu name
3778 if (cclp->cmdbuff[j] == ' '
3779 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003780 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003781 i = j + 1;
3782 break;
3783 }
3784 // check for start of submenu name
3785 if (cclp->cmdbuff[j] == '.'
3786 && cclp->cmdbuff[j - 1] != '\\')
3787 {
3788 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003789 {
3790 i = j + 1;
3791 break;
3792 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003793 else
3794 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003795 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003796 }
3797 if (i > 0)
3798 cmdline_del(cclp, i);
3799 key = p_wc;
3800 KeyTyped = TRUE; // in case the key was mapped
3801 xp->xp_context = EXPAND_NOTHING;
3802 }
3803
3804 return key;
3805}
3806
3807/*
3808 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3809 * directory names (EXPAND_DIRECTORIES) or shell command names
3810 * (EXPAND_SHELLCMD) is displayed.
3811 */
3812 static int
3813wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3814{
3815 int i;
3816 int j;
3817 char_u upseg[5];
3818
3819 upseg[0] = PATHSEP;
3820 upseg[1] = '.';
3821 upseg[2] = '.';
3822 upseg[3] = PATHSEP;
3823 upseg[4] = NUL;
3824
3825 if (key == K_DOWN
3826 && cclp->cmdpos > 0
3827 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3828 && (cclp->cmdpos < 3
3829 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3830 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3831 {
3832 // go down a directory
3833 key = p_wc;
3834 KeyTyped = TRUE; // in case the key was mapped
3835 }
3836 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3837 {
3838 // If in a direct ancestor, strip off one ../ to go down
3839 int found = FALSE;
3840
3841 j = cclp->cmdpos;
3842 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3843 while (--j > i)
3844 {
3845 if (has_mbyte)
3846 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3847 if (vim_ispathsep(cclp->cmdbuff[j]))
3848 {
3849 found = TRUE;
3850 break;
3851 }
3852 }
3853 if (found
3854 && cclp->cmdbuff[j - 1] == '.'
3855 && cclp->cmdbuff[j - 2] == '.'
3856 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3857 {
3858 cmdline_del(cclp, j - 2);
3859 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003860 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003861 }
3862 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003863 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003864 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003865 // go up a directory
3866 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003867
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003868 j = cclp->cmdpos - 1;
3869 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3870 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003871 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003872 if (has_mbyte)
3873 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3874 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003875#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003876 && vim_strchr((char_u *)" *?[{`$%#",
3877 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003878#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003879 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003880 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003881 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003882 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003883 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003884 break;
3885 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003886 else
3887 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003888 }
3889 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003890
3891 if (!found)
3892 j = i;
3893 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3894 j += 4;
3895 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3896 && j == i)
3897 j += 3;
3898 else
3899 j = 0;
3900 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003901 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003902 // TODO this is only for DOS/UNIX systems - need to put in
3903 // machine-specific stuff here and in upseg init
3904 cmdline_del(cclp, j);
3905 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003906 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003907 else if (cclp->cmdpos > i)
3908 cmdline_del(cclp, i);
3909
3910 // Now complete in the new directory. Set KeyTyped in case the
3911 // Up key came from a mapping.
3912 key = p_wc;
3913 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003914 }
3915
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003916 return key;
3917}
3918
3919/*
3920 * Handle a key pressed when the wild menu is displayed
3921 */
3922 int
3923wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3924{
3925 if (xp->xp_context == EXPAND_MENUNAMES)
3926 return wildmenu_process_key_menunames(cclp, key, xp);
3927 else if ((xp->xp_context == EXPAND_FILES
3928 || xp->xp_context == EXPAND_DIRECTORIES
3929 || xp->xp_context == EXPAND_SHELLCMD))
3930 return wildmenu_process_key_filenames(cclp, key, xp);
3931
3932 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003933}
3934
3935/*
3936 * Free expanded names when finished walking through the matches
3937 */
3938 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003939wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003940{
3941 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02003942
3943 if (!p_wmnu || wild_menu_showing == 0)
3944 return;
3945
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003946#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003947 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02003948 if (cclp->input_fn)
3949 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003950#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003951
3952 if (wild_menu_showing == WM_SCROLLED)
3953 {
3954 // Entered command line, move it up
3955 cmdline_row--;
3956 redrawcmd();
3957 }
3958 else if (save_p_ls != -1)
3959 {
3960 // restore 'laststatus' and 'winminheight'
3961 p_ls = save_p_ls;
3962 p_wmh = save_p_wmh;
3963 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003964 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003965 redrawcmd();
3966 save_p_ls = -1;
3967 }
3968 else
3969 {
3970 win_redraw_last_status(topframe);
3971 redraw_statuslines();
3972 }
3973 KeyTyped = skt;
3974 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003975#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003976 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003977 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003978#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003979}
Bram Moolenaareadee482020-09-04 15:37:31 +02003980
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003981#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003982/*
3983 * "getcompletion()" function
3984 */
3985 void
3986f_getcompletion(typval_T *argvars, typval_T *rettv)
3987{
3988 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003989 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003990 expand_T xpc;
3991 int filtered = FALSE;
3992 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003993 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003994
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003995 if (in_vim9script()
3996 && (check_for_string_arg(argvars, 0) == FAIL
3997 || check_for_string_arg(argvars, 1) == FAIL
3998 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3999 return;
4000
ii144785fe02021-11-21 12:13:56 +00004001 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004002 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004003 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004004 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004005
4006 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004007 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004008
4009 if (p_wic)
4010 options |= WILD_ICASE;
4011
4012 // For filtered results, 'wildignore' is used
4013 if (!filtered)
4014 options |= WILD_KEEP_ALL;
4015
4016 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004017 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004018 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004019 int cmdline_len = (int)STRLEN(pat);
4020 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004021 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004022 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004023 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004024 else
4025 {
ii144785fe02021-11-21 12:13:56 +00004026 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004027 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004028 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004029
4030 xpc.xp_context = cmdcomplete_str_to_type(type);
4031 if (xpc.xp_context == EXPAND_NOTHING)
4032 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004033 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004034 return;
4035 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004036
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004037 if (xpc.xp_context == EXPAND_USER_DEFINED)
4038 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004039 // Must be "custom,funcname" pattern
4040 if (STRNCMP(type, "custom,", 7) != 0)
4041 {
4042 semsg(_(e_invalid_argument_str), type);
4043 return;
4044 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004045
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004046 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004047 }
4048
4049 if (xpc.xp_context == EXPAND_USER_LIST)
4050 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004051 // Must be "customlist,funcname" pattern
4052 if (STRNCMP(type, "customlist,", 11) != 0)
4053 {
4054 semsg(_(e_invalid_argument_str), type);
4055 return;
4056 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004057
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004058 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004059 }
4060
Bram Moolenaar66b51422019-08-18 21:44:12 +02004061# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004062 if (xpc.xp_context == EXPAND_MENUS)
4063 {
4064 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4065 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4066 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004067# endif
4068# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004069 if (xpc.xp_context == EXPAND_CSCOPE)
4070 {
4071 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4072 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4073 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004074# endif
4075# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004076 if (xpc.xp_context == EXPAND_SIGN)
4077 {
4078 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4079 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4080 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004081# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004082 if (xpc.xp_context == EXPAND_RUNTIME)
4083 {
4084 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4085 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4086 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004087 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004088
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004089 if (cmdline_fuzzy_completion_supported(&xpc))
4090 // when fuzzy matching, don't modify the search string
4091 pat = vim_strsave(xpc.xp_pattern);
4092 else
4093 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4094
Bram Moolenaar93a10962022-06-16 11:42:09 +01004095 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004096 {
4097 int i;
4098
4099 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4100
4101 for (i = 0; i < xpc.xp_numfiles; i++)
4102 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4103 }
4104 vim_free(pat);
4105 ExpandCleanup(&xpc);
4106}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004107#endif // FEAT_EVAL