blob: 5ba6b084d30872e94ec659af168bd3d1ea96b5e1 [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
18static void set_expand_context(expand_T *xp);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000019static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000020 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000021 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000022static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000026static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020028#endif
29
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000030#ifdef FEAT_WILDMENU
31// "compl_match_array" points the currently displayed list of entries in the
32// popup menu. It is NULL when there is no popup menu.
33static pumitem_T *compl_match_array = NULL;
34static int compl_match_arraysize;
35// First column in cmdline of the matched item for completion.
36static int compl_startcol;
37static int compl_selected;
38#endif
39
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040#define SHOW_FILE_TEXT(m) (showtail ? sm_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000041
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000042/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000043 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
44 * context.
45 */
46 static int
47cmdline_fuzzy_completion_supported(expand_T *xp)
48{
49 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
50 && xp->xp_context != EXPAND_BOOL_SETTINGS
51 && xp->xp_context != EXPAND_COLORS
52 && xp->xp_context != EXPAND_COMPILER
53 && xp->xp_context != EXPAND_DIRECTORIES
54 && xp->xp_context != EXPAND_FILES
55 && xp->xp_context != EXPAND_FILES_IN_PATH
56 && xp->xp_context != EXPAND_FILETYPE
57 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000058 && xp->xp_context != EXPAND_OLD_SETTING
59 && xp->xp_context != EXPAND_OWNSYNTAX
60 && xp->xp_context != EXPAND_PACKADD
61 && xp->xp_context != EXPAND_SHELLCMD
62 && xp->xp_context != EXPAND_TAGS
63 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064 && xp->xp_context != EXPAND_USER_LIST);
65}
66
67/*
68 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000069 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
70 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000071 */
72 int
73cmdline_fuzzy_complete(char_u *fuzzystr)
74{
75 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
76}
77
78/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000079 * sort function for the completion matches.
80 * <SNR> functions should be sorted to the end.
81 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020082 static int
83sort_func_compare(const void *s1, const void *s2)
84{
85 char_u *p1 = *(char_u **)s1;
86 char_u *p2 = *(char_u **)s2;
87
88 if (*p1 != '<' && *p2 == '<') return -1;
89 if (*p1 == '<' && *p2 != '<') return 1;
90 return STRCMP(p1, p2);
91}
Bram Moolenaar66b51422019-08-18 21:44:12 +020092
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000093/*
94 * Escape special characters in the cmdline completion matches.
95 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020096 static void
97ExpandEscape(
98 expand_T *xp,
99 char_u *str,
100 int numfiles,
101 char_u **files,
102 int options)
103{
104 int i;
105 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100106 int vse_what = xp->xp_context == EXPAND_BUFFERS
107 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200108
109 // May change home directory back to "~"
110 if (options & WILD_HOME_REPLACE)
111 tilde_replace(str, numfiles, files);
112
113 if (options & WILD_ESCAPE)
114 {
115 if (xp->xp_context == EXPAND_FILES
116 || xp->xp_context == EXPAND_FILES_IN_PATH
117 || xp->xp_context == EXPAND_SHELLCMD
118 || xp->xp_context == EXPAND_BUFFERS
119 || xp->xp_context == EXPAND_DIRECTORIES)
120 {
121 // Insert a backslash into a file name before a space, \, %, #
122 // and wildmatch characters, except '~'.
123 for (i = 0; i < numfiles; ++i)
124 {
125 // for ":set path=" we need to escape spaces twice
126 if (xp->xp_backslash == XP_BS_THREE)
127 {
128 p = vim_strsave_escaped(files[i], (char_u *)" ");
129 if (p != NULL)
130 {
131 vim_free(files[i]);
132 files[i] = p;
133#if defined(BACKSLASH_IN_FILENAME)
134 p = vim_strsave_escaped(files[i], (char_u *)" ");
135 if (p != NULL)
136 {
137 vim_free(files[i]);
138 files[i] = p;
139 }
140#endif
141 }
142 }
143#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100144 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200145#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100146 p = vim_strsave_fnameescape(files[i],
147 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200148#endif
149 if (p != NULL)
150 {
151 vim_free(files[i]);
152 files[i] = p;
153 }
154
155 // If 'str' starts with "\~", replace "~" at start of
156 // files[i] with "\~".
157 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
158 escape_fname(&files[i]);
159 }
160 xp->xp_backslash = XP_BS_NONE;
161
162 // If the first file starts with a '+' escape it. Otherwise it
163 // could be seen as "+cmd".
164 if (*files[0] == '+')
165 escape_fname(&files[0]);
166 }
167 else if (xp->xp_context == EXPAND_TAGS)
168 {
169 // Insert a backslash before characters in a tag name that
170 // would terminate the ":tag" command.
171 for (i = 0; i < numfiles; ++i)
172 {
173 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
174 if (p != NULL)
175 {
176 vim_free(files[i]);
177 files[i] = p;
178 }
179 }
180 }
181 }
182}
183
184/*
185 * Return FAIL if this is not an appropriate context in which to do
186 * completion of anything, return OK if it is (even if there are no matches).
187 * For the caller, this means that the character is just passed through like a
188 * normal character (instead of being expanded). This allows :s/^I^D etc.
189 */
190 int
191nextwild(
192 expand_T *xp,
193 int type,
194 int options, // extra options for ExpandOne()
195 int escape) // if TRUE, escape the returned matches
196{
197 cmdline_info_T *ccline = get_cmdline_info();
198 int i, j;
199 char_u *p1;
200 char_u *p2;
201 int difflen;
202 int v;
203
204 if (xp->xp_numfiles == -1)
205 {
206 set_expand_context(xp);
207 cmd_showtail = expand_showtail(xp);
208 }
209
210 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
211 {
212 beep_flush();
213 return OK; // Something illegal on command line
214 }
215 if (xp->xp_context == EXPAND_NOTHING)
216 {
217 // Caller can use the character as a normal char instead
218 return FAIL;
219 }
220
221 msg_puts("..."); // show that we are busy
222 out_flush();
223
224 i = (int)(xp->xp_pattern - ccline->cmdbuff);
225 xp->xp_pattern_len = ccline->cmdpos - i;
226
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000227 if (type == WILD_NEXT || type == WILD_PREV
228 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200229 {
230 // Get next/previous match for a previous expanded pattern.
231 p2 = ExpandOne(xp, NULL, NULL, 0, type);
232 }
233 else
234 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000235 if (cmdline_fuzzy_completion_supported(xp))
236 // If fuzzy matching, don't modify the search string
237 p1 = vim_strsave(xp->xp_pattern);
238 else
239 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
240
Bram Moolenaar66b51422019-08-18 21:44:12 +0200241 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000242 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200243 p2 = NULL;
244 else
245 {
246 int use_options = options |
247 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
248 if (escape)
249 use_options |= WILD_ESCAPE;
250
251 if (p_wic)
252 use_options += WILD_ICASE;
253 p2 = ExpandOne(xp, p1,
254 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
255 use_options, type);
256 vim_free(p1);
257 // longest match: make sure it is not shorter, happens with :help
258 if (p2 != NULL && type == WILD_LONGEST)
259 {
260 for (j = 0; j < xp->xp_pattern_len; ++j)
261 if (ccline->cmdbuff[i + j] == '*'
262 || ccline->cmdbuff[i + j] == '?')
263 break;
264 if ((int)STRLEN(p2) < j)
265 VIM_CLEAR(p2);
266 }
267 }
268 }
269
270 if (p2 != NULL && !got_int)
271 {
272 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
273 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
274 {
275 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
276 xp->xp_pattern = ccline->cmdbuff + i;
277 }
278 else
279 v = OK;
280 if (v == OK)
281 {
282 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
283 &ccline->cmdbuff[ccline->cmdpos],
284 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
285 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
286 ccline->cmdlen += difflen;
287 ccline->cmdpos += difflen;
288 }
289 }
290 vim_free(p2);
291
292 redrawcmd();
293 cursorcmd();
294
295 // When expanding a ":map" command and no matches are found, assume that
296 // the key is supposed to be inserted literally
297 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
298 return FAIL;
299
300 if (xp->xp_numfiles <= 0 && p2 == NULL)
301 beep_flush();
302 else if (xp->xp_numfiles == 1)
303 // free expanded pattern
304 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
305
306 return OK;
307}
308
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000309#if defined(FEAT_WILDMENU) || defined(PROTO)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000310
311/*
312 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000313 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000314 */
315 static int
316cmdline_pum_create(
317 cmdline_info_T *ccline,
318 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000319 char_u **matches,
320 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000321 int showtail)
322{
323 int i;
324 int columns;
325
326 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000327 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000328 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000329 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000330 {
331 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
332 compl_match_array[i].pum_info = NULL;
333 compl_match_array[i].pum_extra = NULL;
334 compl_match_array[i].pum_kind = NULL;
335 }
336
337 // Compute the popup menu starting column
338 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
339 columns = vim_strsize(xp->xp_pattern);
340 if (showtail)
341 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000342 columns += vim_strsize(sm_gettail(matches[0]));
343 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000344 }
345 if (columns >= compl_startcol)
346 compl_startcol = 0;
347 else
348 compl_startcol -= columns;
349
350 // no default selection
351 compl_selected = -1;
352
353 cmdline_pum_display();
354
355 return EXPAND_OK;
356}
357
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000358/*
359 * Display the cmdline completion matches in a popup menu
360 */
361void cmdline_pum_display(void)
362{
363 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
364}
365
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000366/*
367 * Returns TRUE if the cmdline completion popup menu is being displayed.
368 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000369int cmdline_pum_active(void)
370{
371 return p_wmnu && pum_visible() && compl_match_array != NULL;
372}
373
374/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000375 * Remove the cmdline completion popup menu (if present), free the list of
376 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000377 */
378void cmdline_pum_remove(void)
379{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000380 int save_p_lz = p_lz;
381
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000382 pum_undisplay();
383 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000384 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000385 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000386 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000387 redrawcmd();
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000388}
389
390void cmdline_pum_cleanup(cmdline_info_T *cclp)
391{
392 cmdline_pum_remove();
393 wildmenu_cleanup(cclp);
394}
395
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000396/*
397 * Returns the starting column number to use for the cmdline completion popup
398 * menu.
399 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400int cmdline_compl_startcol(void)
401{
402 return compl_startcol;
403}
404#endif
405
Bram Moolenaar66b51422019-08-18 21:44:12 +0200406/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000407 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000408 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000409 */
410 static char_u *
411get_next_or_prev_match(
412 int mode,
413 expand_T *xp,
414 int *p_findex,
415 char_u *orig_save)
416{
417 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000418 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000419
420 if (xp->xp_numfiles <= 0)
421 return NULL;
422
423 if (mode == WILD_PREV)
424 {
425 if (findex == -1)
426 findex = xp->xp_numfiles;
427 --findex;
428 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000429 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000430 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000431 else if (mode == WILD_PAGEUP)
432 {
433 if (findex == 0)
434 // at the first entry, don't select any entries
435 findex = -1;
436 else if (findex == -1)
437 // no entry is selected. select the last entry
438 findex = xp->xp_numfiles - 1;
439 else
440 {
441 // go up by the pum height
442 ht = pum_get_height();
443 if (ht > 3)
444 ht -= 2;
445 findex -= ht;
446 if (findex < 0)
447 // few entries left, select the first entry
448 findex = 0;
449 }
450 }
451 else // mode == WILD_PAGEDOWN
452 {
453 if (findex == xp->xp_numfiles - 1)
454 // at the last entry, don't select any entries
455 findex = -1;
456 else if (findex == -1)
457 // no entry is selected. select the first entry
458 findex = 0;
459 else
460 {
461 // go down by the pum height
462 ht = pum_get_height();
463 if (ht > 3)
464 ht -= 2;
465 findex += ht;
466 if (findex >= xp->xp_numfiles)
467 // few entries left, select the last entry
468 findex = xp->xp_numfiles - 1;
469 }
470 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000471
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000472 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000473 if (findex < 0)
474 {
475 if (orig_save == NULL)
476 findex = xp->xp_numfiles - 1;
477 else
478 findex = -1;
479 }
480 if (findex >= xp->xp_numfiles)
481 {
482 if (orig_save == NULL)
483 findex = 0;
484 else
485 findex = -1;
486 }
487#ifdef FEAT_WILDMENU
488 if (compl_match_array)
489 {
490 compl_selected = findex;
491 cmdline_pum_display();
492 }
493 else if (p_wmnu)
494 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
495 findex, cmd_showtail);
496#endif
497 *p_findex = findex;
498
499 if (findex == -1)
500 return vim_strsave(orig_save);
501
502 return vim_strsave(xp->xp_files[findex]);
503}
504
505/*
506 * Start the command-line expansion and get the matches.
507 */
508 static char_u *
509ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
510{
511 int non_suf_match; // number without matching suffix
512 int i;
513 char_u *ss = NULL;
514
515 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000516 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000517 options) == FAIL)
518 {
519#ifdef FNAME_ILLEGAL
520 // Illegal file name has been silently skipped. But when there
521 // are wildcards, the real problem is that there was no match,
522 // causing the pattern to be added, which has illegal characters.
523 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
524 semsg(_(e_no_match_str_2), str);
525#endif
526 }
527 else if (xp->xp_numfiles == 0)
528 {
529 if (!(options & WILD_SILENT))
530 semsg(_(e_no_match_str_2), str);
531 }
532 else
533 {
534 // Escape the matches for use on the command line.
535 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
536
537 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000538 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000539 {
540 if (xp->xp_numfiles)
541 non_suf_match = xp->xp_numfiles;
542 else
543 non_suf_match = 1;
544 if ((xp->xp_context == EXPAND_FILES
545 || xp->xp_context == EXPAND_DIRECTORIES)
546 && xp->xp_numfiles > 1)
547 {
548 // More than one match; check suffix.
549 // The files will have been sorted on matching suffix in
550 // expand_wildcards, only need to check the first two.
551 non_suf_match = 0;
552 for (i = 0; i < 2; ++i)
553 if (match_suffix(xp->xp_files[i]))
554 ++non_suf_match;
555 }
556 if (non_suf_match != 1)
557 {
558 // Can we ever get here unless it's while expanding
559 // interactively? If not, we can get rid of this all
560 // together. Don't really want to wait for this message
561 // (and possibly have to hit return to continue!).
562 if (!(options & WILD_SILENT))
563 emsg(_(e_too_many_file_names));
564 else if (!(options & WILD_NO_BEEP))
565 beep_flush();
566 }
567 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
568 ss = vim_strsave(xp->xp_files[0]);
569 }
570 }
571
572 return ss;
573}
574
575/*
576 * Return the longest common part in the list of cmdline completion matches.
577 */
578 static char_u *
579find_longest_match(expand_T *xp, int options)
580{
581 long_u len;
582 int mb_len = 1;
583 int c0, ci;
584 int i;
585 char_u *ss;
586
587 for (len = 0; xp->xp_files[0][len]; len += mb_len)
588 {
589 if (has_mbyte)
590 {
591 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
592 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
593 }
594 else
595 c0 = xp->xp_files[0][len];
596 for (i = 1; i < xp->xp_numfiles; ++i)
597 {
598 if (has_mbyte)
599 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
600 else
601 ci = xp->xp_files[i][len];
602 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
603 || xp->xp_context == EXPAND_FILES
604 || xp->xp_context == EXPAND_SHELLCMD
605 || xp->xp_context == EXPAND_BUFFERS))
606 {
607 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
608 break;
609 }
610 else if (c0 != ci)
611 break;
612 }
613 if (i < xp->xp_numfiles)
614 {
615 if (!(options & WILD_NO_BEEP))
616 vim_beep(BO_WILD);
617 break;
618 }
619 }
620
621 ss = alloc(len + 1);
622 if (ss)
623 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
624
625 return ss;
626}
627
628/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000629 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200630 * Chars that should not be expanded must be preceded with a backslash.
631 * Return a pointer to allocated memory containing the new string.
632 * Return NULL for failure.
633 *
634 * "orig" is the originally expanded string, copied to allocated memory. It
635 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
636 * WILD_PREV "orig" should be NULL.
637 *
638 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
639 * is WILD_EXPAND_FREE or WILD_ALL.
640 *
641 * mode = WILD_FREE: just free previously expanded matches
642 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
643 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
644 * mode = WILD_NEXT: use next match in multiple match, wrap to first
645 * mode = WILD_PREV: use previous match in multiple match, wrap to first
646 * mode = WILD_ALL: return all matches concatenated
647 * mode = WILD_LONGEST: return longest matched part
648 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000649 * mode = WILD_APPLY: apply the item selected in the cmdline completion
650 * popup menu and close the menu.
651 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
652 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200653 *
654 * options = WILD_LIST_NOTFOUND: list entries without a match
655 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
656 * options = WILD_USE_NL: Use '\n' for WILD_ALL
657 * options = WILD_NO_BEEP: Don't beep for multiple matches
658 * options = WILD_ADD_SLASH: add a slash after directory names
659 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
660 * options = WILD_SILENT: don't print warning messages
661 * options = WILD_ESCAPE: put backslash before special chars
662 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200663 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200664 *
665 * The variables xp->xp_context and xp->xp_backslash must have been set!
666 */
667 char_u *
668ExpandOne(
669 expand_T *xp,
670 char_u *str,
671 char_u *orig, // allocated copy of original of expanded string
672 int options,
673 int mode)
674{
675 char_u *ss = NULL;
676 static int findex;
677 static char_u *orig_save = NULL; // kept value of orig
678 int orig_saved = FALSE;
679 int i;
680 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200681
682 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000683 if (mode == WILD_NEXT || mode == WILD_PREV
684 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000685 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200686
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000687 if (mode == WILD_CANCEL)
688 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
689 else if (mode == WILD_APPLY)
690 ss = vim_strsave(findex == -1 ? (orig_save ?
691 orig_save : (char_u *)"") : xp->xp_files[findex]);
692
Bram Moolenaar66b51422019-08-18 21:44:12 +0200693 // free old names
694 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
695 {
696 FreeWild(xp->xp_numfiles, xp->xp_files);
697 xp->xp_numfiles = -1;
698 VIM_CLEAR(orig_save);
699 }
700 findex = 0;
701
702 if (mode == WILD_FREE) // only release file name
703 return NULL;
704
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000705 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200706 {
707 vim_free(orig_save);
708 orig_save = orig;
709 orig_saved = TRUE;
710
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000711 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200712 }
713
714 // Find longest common part
715 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
716 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000717 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200718 findex = -1; // next p_wc gets first one
719 }
720
721 // Concatenate all matching names
722 if (mode == WILD_ALL && xp->xp_numfiles > 0)
723 {
724 len = 0;
725 for (i = 0; i < xp->xp_numfiles; ++i)
726 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
727 ss = alloc(len);
728 if (ss != NULL)
729 {
730 *ss = NUL;
731 for (i = 0; i < xp->xp_numfiles; ++i)
732 {
733 STRCAT(ss, xp->xp_files[i]);
734 if (i != xp->xp_numfiles - 1)
735 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
736 }
737 }
738 }
739
740 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
741 ExpandCleanup(xp);
742
743 // Free "orig" if it wasn't stored in "orig_save".
744 if (!orig_saved)
745 vim_free(orig);
746
747 return ss;
748}
749
750/*
751 * Prepare an expand structure for use.
752 */
753 void
754ExpandInit(expand_T *xp)
755{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200756 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200757 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200758 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200759}
760
761/*
762 * Cleanup an expand structure after use.
763 */
764 void
765ExpandCleanup(expand_T *xp)
766{
767 if (xp->xp_numfiles >= 0)
768 {
769 FreeWild(xp->xp_numfiles, xp->xp_files);
770 xp->xp_numfiles = -1;
771 }
772}
773
774/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000775 * Display one line of completion matches. Multiple matches are displayed in
776 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000777 * matches - list of completion match names
778 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000779 * lines - number of output lines
780 * linenr - line number of matches to display
781 * maxlen - maximum number of characters in each line
782 * showtail - display only the tail of the full path of a file name
783 * dir_attr - highlight attribute to use for directory names
784 */
785 static void
786showmatches_oneline(
787 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000788 char_u **matches,
789 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000790 int lines,
791 int linenr,
792 int maxlen,
793 int showtail,
794 int dir_attr)
795{
796 int i, j;
797 int isdir;
798 int lastlen;
799 char_u *p;
800
801 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000802 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000803 {
804 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
805 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000806 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
807 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000808 msg_advance(maxlen + 1);
809 msg_puts((char *)p);
810 msg_advance(maxlen + 3);
811 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
812 break;
813 }
814 for (i = maxlen - lastlen; --i >= 0; )
815 msg_putchar(' ');
816 if (xp->xp_context == EXPAND_FILES
817 || xp->xp_context == EXPAND_SHELLCMD
818 || xp->xp_context == EXPAND_BUFFERS)
819 {
820 // highlight directories
821 if (xp->xp_numfiles != -1)
822 {
823 char_u *halved_slash;
824 char_u *exp_path;
825 char_u *path;
826
827 // Expansion was done before and special characters
828 // were escaped, need to halve backslashes. Also
829 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000830 exp_path = expand_env_save_opt(matches[j], TRUE);
831 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000832 halved_slash = backslash_halve_save(path);
833 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000834 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000835 vim_free(exp_path);
836 if (halved_slash != path)
837 vim_free(halved_slash);
838 }
839 else
840 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000841 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000842 if (showtail)
843 p = SHOW_FILE_TEXT(j);
844 else
845 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000846 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000847 TRUE);
848 p = NameBuff;
849 }
850 }
851 else
852 {
853 isdir = FALSE;
854 p = SHOW_FILE_TEXT(j);
855 }
856 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
857 }
858 if (msg_col > 0) // when not wrapped around
859 {
860 msg_clr_eos();
861 msg_putchar('\n');
862 }
863 out_flush(); // show one line at a time
864}
865
866/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200867 * Show all matches for completion on the command line.
868 * Returns EXPAND_NOTHING when the character that triggered expansion should
869 * be inserted like a normal character.
870 */
871 int
872showmatches(expand_T *xp, int wildmenu UNUSED)
873{
874 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000875 int numMatches;
876 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000877 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200878 int maxlen;
879 int lines;
880 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200881 int attr;
882 int showtail;
883
884 if (xp->xp_numfiles == -1)
885 {
886 set_expand_context(xp);
887 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000888 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200889 showtail = expand_showtail(xp);
890 if (i != EXPAND_OK)
891 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200892 }
893 else
894 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000895 numMatches = xp->xp_numfiles;
896 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200897 showtail = cmd_showtail;
898 }
899
900#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000901 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000902 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000903 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000904#endif
905
906#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200907 if (!wildmenu)
908 {
909#endif
910 msg_didany = FALSE; // lines_left will be set
911 msg_start(); // prepare for paging
912 msg_putchar('\n');
913 out_flush();
914 cmdline_row = msg_row;
915 msg_didany = FALSE; // lines_left will be set again
916 msg_start(); // prepare for paging
917#ifdef FEAT_WILDMENU
918 }
919#endif
920
921 if (got_int)
922 got_int = FALSE; // only int. the completion, not the cmd line
923#ifdef FEAT_WILDMENU
924 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000925 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200926#endif
927 else
928 {
929 // find the length of the longest file name
930 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000931 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200932 {
933 if (!showtail && (xp->xp_context == EXPAND_FILES
934 || xp->xp_context == EXPAND_SHELLCMD
935 || xp->xp_context == EXPAND_BUFFERS))
936 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000937 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200938 j = vim_strsize(NameBuff);
939 }
940 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000941 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200942 if (j > maxlen)
943 maxlen = j;
944 }
945
946 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000947 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200948 else
949 {
950 // compute the number of columns and lines for the listing
951 maxlen += 2; // two spaces between file names
952 columns = ((int)Columns + 2) / maxlen;
953 if (columns < 1)
954 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000955 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200956 }
957
958 attr = HL_ATTR(HLF_D); // find out highlighting for directories
959
960 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
961 {
962 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
963 msg_clr_eos();
964 msg_advance(maxlen - 3);
965 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
966 }
967
968 // list the files line by line
969 for (i = 0; i < lines; ++i)
970 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000971 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000972 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200973 if (got_int)
974 {
975 got_int = FALSE;
976 break;
977 }
978 }
979
980 // we redraw the command below the lines that we have just listed
981 // This is a bit tricky, but it saves a lot of screen updating.
982 cmdline_row = msg_row; // will put it back later
983 }
984
985 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000986 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200987
988 return EXPAND_OK;
989}
990
991/*
992 * Private gettail for showmatches() (and win_redr_status_matches()):
993 * Find tail of file name path, but ignore trailing "/".
994 */
995 char_u *
996sm_gettail(char_u *s)
997{
998 char_u *p;
999 char_u *t = s;
1000 int had_sep = FALSE;
1001
1002 for (p = s; *p != NUL; )
1003 {
1004 if (vim_ispathsep(*p)
1005#ifdef BACKSLASH_IN_FILENAME
1006 && !rem_backslash(p)
1007#endif
1008 )
1009 had_sep = TRUE;
1010 else if (had_sep)
1011 {
1012 t = p;
1013 had_sep = FALSE;
1014 }
1015 MB_PTR_ADV(p);
1016 }
1017 return t;
1018}
1019
1020/*
1021 * Return TRUE if we only need to show the tail of completion matches.
1022 * When not completing file names or there is a wildcard in the path FALSE is
1023 * returned.
1024 */
1025 static int
1026expand_showtail(expand_T *xp)
1027{
1028 char_u *s;
1029 char_u *end;
1030
1031 // When not completing file names a "/" may mean something different.
1032 if (xp->xp_context != EXPAND_FILES
1033 && xp->xp_context != EXPAND_SHELLCMD
1034 && xp->xp_context != EXPAND_DIRECTORIES)
1035 return FALSE;
1036
1037 end = gettail(xp->xp_pattern);
1038 if (end == xp->xp_pattern) // there is no path separator
1039 return FALSE;
1040
1041 for (s = xp->xp_pattern; s < end; s++)
1042 {
1043 // Skip escaped wildcards. Only when the backslash is not a path
1044 // separator, on DOS the '*' "path\*\file" must not be skipped.
1045 if (rem_backslash(s))
1046 ++s;
1047 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1048 return FALSE;
1049 }
1050 return TRUE;
1051}
1052
1053/*
1054 * Prepare a string for expansion.
1055 * When expanding file names: The string will be used with expand_wildcards().
1056 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1057 * When expanding other names: The string will be used with regcomp(). Copy
1058 * the name into allocated memory and prepend "^".
1059 */
1060 char_u *
1061addstar(
1062 char_u *fname,
1063 int len,
1064 int context) // EXPAND_FILES etc.
1065{
1066 char_u *retval;
1067 int i, j;
1068 int new_len;
1069 char_u *tail;
1070 int ends_in_star;
1071
1072 if (context != EXPAND_FILES
1073 && context != EXPAND_FILES_IN_PATH
1074 && context != EXPAND_SHELLCMD
1075 && context != EXPAND_DIRECTORIES)
1076 {
1077 // Matching will be done internally (on something other than files).
1078 // So we convert the file-matching-type wildcards into our kind for
1079 // use with vim_regcomp(). First work out how long it will be:
1080
1081 // For help tags the translation is done in find_help_tags().
1082 // For a tag pattern starting with "/" no translation is needed.
1083 if (context == EXPAND_HELP
1084 || context == EXPAND_COLORS
1085 || context == EXPAND_COMPILER
1086 || context == EXPAND_OWNSYNTAX
1087 || context == EXPAND_FILETYPE
1088 || context == EXPAND_PACKADD
1089 || ((context == EXPAND_TAGS_LISTFILES
1090 || context == EXPAND_TAGS)
1091 && fname[0] == '/'))
1092 retval = vim_strnsave(fname, len);
1093 else
1094 {
1095 new_len = len + 2; // +2 for '^' at start, NUL at end
1096 for (i = 0; i < len; i++)
1097 {
1098 if (fname[i] == '*' || fname[i] == '~')
1099 new_len++; // '*' needs to be replaced by ".*"
1100 // '~' needs to be replaced by "\~"
1101
1102 // Buffer names are like file names. "." should be literal
1103 if (context == EXPAND_BUFFERS && fname[i] == '.')
1104 new_len++; // "." becomes "\."
1105
1106 // Custom expansion takes care of special things, match
1107 // backslashes literally (perhaps also for other types?)
1108 if ((context == EXPAND_USER_DEFINED
1109 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1110 new_len++; // '\' becomes "\\"
1111 }
1112 retval = alloc(new_len);
1113 if (retval != NULL)
1114 {
1115 retval[0] = '^';
1116 j = 1;
1117 for (i = 0; i < len; i++, j++)
1118 {
1119 // Skip backslash. But why? At least keep it for custom
1120 // expansion.
1121 if (context != EXPAND_USER_DEFINED
1122 && context != EXPAND_USER_LIST
1123 && fname[i] == '\\'
1124 && ++i == len)
1125 break;
1126
1127 switch (fname[i])
1128 {
1129 case '*': retval[j++] = '.';
1130 break;
1131 case '~': retval[j++] = '\\';
1132 break;
1133 case '?': retval[j] = '.';
1134 continue;
1135 case '.': if (context == EXPAND_BUFFERS)
1136 retval[j++] = '\\';
1137 break;
1138 case '\\': if (context == EXPAND_USER_DEFINED
1139 || context == EXPAND_USER_LIST)
1140 retval[j++] = '\\';
1141 break;
1142 }
1143 retval[j] = fname[i];
1144 }
1145 retval[j] = NUL;
1146 }
1147 }
1148 }
1149 else
1150 {
1151 retval = alloc(len + 4);
1152 if (retval != NULL)
1153 {
1154 vim_strncpy(retval, fname, len);
1155
1156 // Don't add a star to *, ~, ~user, $var or `cmd`.
1157 // * would become **, which walks the whole tree.
1158 // ~ would be at the start of the file name, but not the tail.
1159 // $ could be anywhere in the tail.
1160 // ` could be anywhere in the file name.
1161 // When the name ends in '$' don't add a star, remove the '$'.
1162 tail = gettail(retval);
1163 ends_in_star = (len > 0 && retval[len - 1] == '*');
1164#ifndef BACKSLASH_IN_FILENAME
1165 for (i = len - 2; i >= 0; --i)
1166 {
1167 if (retval[i] != '\\')
1168 break;
1169 ends_in_star = !ends_in_star;
1170 }
1171#endif
1172 if ((*retval != '~' || tail != retval)
1173 && !ends_in_star
1174 && vim_strchr(tail, '$') == NULL
1175 && vim_strchr(retval, '`') == NULL)
1176 retval[len++] = '*';
1177 else if (len > 0 && retval[len - 1] == '$')
1178 --len;
1179 retval[len] = NUL;
1180 }
1181 }
1182 return retval;
1183}
1184
1185/*
1186 * Must parse the command line so far to work out what context we are in.
1187 * Completion can then be done based on that context.
1188 * This routine sets the variables:
1189 * xp->xp_pattern The start of the pattern to be expanded within
1190 * the command line (ends at the cursor).
1191 * xp->xp_context The type of thing to expand. Will be one of:
1192 *
1193 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1194 * the command line, like an unknown command. Caller
1195 * should beep.
1196 * EXPAND_NOTHING Unrecognised context for completion, use char like
1197 * a normal char, rather than for completion. eg
1198 * :s/^I/
1199 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1200 * it.
1201 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1202 * EXPAND_FILES After command with EX_XFILE set, or after setting
1203 * with P_EXPAND set. eg :e ^I, :w>>^I
1204 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1205 * when we know only directories are of interest. eg
1206 * :set dir=^I
1207 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1208 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1209 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1210 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1211 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1212 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1213 * EXPAND_EVENTS Complete event names
1214 * EXPAND_SYNTAX Complete :syntax command arguments
1215 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1216 * EXPAND_AUGROUP Complete autocommand group names
1217 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1218 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1219 * eg :unmap a^I , :cunab x^I
1220 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1221 * eg :call sub^I
1222 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1223 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1224 * names in expressions, eg :while s^I
1225 * EXPAND_ENV_VARS Complete environment variable names
1226 * EXPAND_USER Complete user names
1227 */
1228 static void
1229set_expand_context(expand_T *xp)
1230{
1231 cmdline_info_T *ccline = get_cmdline_info();
1232
1233 // only expansion for ':', '>' and '=' command-lines
1234 if (ccline->cmdfirstc != ':'
1235#ifdef FEAT_EVAL
1236 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1237 && !ccline->input_fn
1238#endif
1239 )
1240 {
1241 xp->xp_context = EXPAND_NOTHING;
1242 return;
1243 }
1244 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1245}
1246
Bram Moolenaard0190392019-08-23 21:17:35 +02001247/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001248 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1249 * For user defined commands, the completion context is set in 'xp' and the
1250 * completion flags in 'complp'.
1251 *
1252 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001253 */
1254 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001255set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001256{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001257 char_u *p = NULL;
1258 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001259 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001260
1261 // Isolate the command and search for it in the command table.
1262 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001263 // - the 'k' command can directly be followed by any character, but do
1264 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1265 // find matches anywhere in the command name, do this only for command
1266 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001267 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001268 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001269 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001270 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001271 p = cmd + 1;
1272 }
1273 else
1274 {
1275 p = cmd;
1276 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1277 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001278 // A user command may contain digits.
1279 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1280 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001281 while (ASCII_ISALNUM(*p) || *p == '*')
1282 ++p;
1283 // for python 3.x: ":py3*" commands completion
1284 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1285 {
1286 ++p;
1287 while (ASCII_ISALPHA(*p) || *p == '*')
1288 ++p;
1289 }
1290 // check for non-alpha command
1291 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1292 ++p;
1293 len = (int)(p - cmd);
1294
1295 if (len == 0)
1296 {
1297 xp->xp_context = EXPAND_UNSUCCESSFUL;
1298 return NULL;
1299 }
1300
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001301 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001302
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001303 // User defined commands support alphanumeric characters.
1304 // Also when doing fuzzy expansion, support alphanumeric characters.
1305 if ((cmd[0] >= 'A' && cmd[0] <= 'Z') || (fuzzy && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001306 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1307 ++p;
1308 }
1309
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001310 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001311 // character, complete the command name.
1312 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1313 return NULL;
1314
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001315 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001316 {
1317 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1318 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001319 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001320 p = cmd + 1;
1321 }
1322 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1323 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001324 eap->cmd = cmd;
1325 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001326 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001327 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001328 }
1329 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001330 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001331 {
1332 // Not still touching the command and it was an illegal one
1333 xp->xp_context = EXPAND_UNSUCCESSFUL;
1334 return NULL;
1335 }
1336
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001337 return p;
1338}
Bram Moolenaard0190392019-08-23 21:17:35 +02001339
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001340/*
1341 * Set the completion context for a command argument with wild card characters.
1342 */
1343 static void
1344set_context_for_wildcard_arg(
1345 exarg_T *eap,
1346 char_u *arg,
1347 int usefilter,
1348 expand_T *xp,
1349 int *complp)
1350{
1351 char_u *p;
1352 int c;
1353 int in_quote = FALSE;
1354 char_u *bow = NULL; // Beginning of word
1355 int len = 0;
1356
1357 // Allow spaces within back-quotes to count as part of the argument
1358 // being expanded.
1359 xp->xp_pattern = skipwhite(arg);
1360 p = xp->xp_pattern;
1361 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001362 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001363 if (has_mbyte)
1364 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001365 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001366 c = *p;
1367 if (c == '\\' && p[1] != NUL)
1368 ++p;
1369 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001370 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001371 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001372 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001373 xp->xp_pattern = p;
1374 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001375 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001376 in_quote = !in_quote;
1377 }
1378 // An argument can contain just about everything, except
1379 // characters that end the command and white space.
1380 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001381#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001382 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001383#endif
1384 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001385 {
1386 len = 0; // avoid getting stuck when space is in 'isfname'
1387 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001388 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001389 if (has_mbyte)
1390 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001391 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001392 c = *p;
1393 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001394 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001395 if (has_mbyte)
1396 len = (*mb_ptr2len)(p);
1397 else
1398 len = 1;
1399 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001400 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001401 if (in_quote)
1402 bow = p;
1403 else
1404 xp->xp_pattern = p;
1405 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001406 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001407 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001408 }
1409
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001410 // If we are still inside the quotes, and we passed a space, just
1411 // expand from there.
1412 if (bow != NULL && in_quote)
1413 xp->xp_pattern = bow;
1414 xp->xp_context = EXPAND_FILES;
1415
1416 // For a shell command more chars need to be escaped.
1417 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1418 {
1419#ifndef BACKSLASH_IN_FILENAME
1420 xp->xp_shell = TRUE;
1421#endif
1422 // When still after the command name expand executables.
1423 if (xp->xp_pattern == skipwhite(arg))
1424 xp->xp_context = EXPAND_SHELLCMD;
1425 }
1426
1427 // Check for environment variable.
1428 if (*xp->xp_pattern == '$')
1429 {
1430 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1431 if (!vim_isIDc(*p))
1432 break;
1433 if (*p == NUL)
1434 {
1435 xp->xp_context = EXPAND_ENV_VARS;
1436 ++xp->xp_pattern;
1437 // Avoid that the assignment uses EXPAND_FILES again.
1438 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1439 *complp = EXPAND_ENV_VARS;
1440 }
1441 }
1442 // Check for user names.
1443 if (*xp->xp_pattern == '~')
1444 {
1445 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1446 ;
1447 // Complete ~user only if it partially matches a user name.
1448 // A full match ~user<Tab> will be replaced by user's home
1449 // directory i.e. something like ~user<Tab> -> /home/user/
1450 if (*p == NUL && p > xp->xp_pattern + 1
1451 && match_user(xp->xp_pattern + 1) >= 1)
1452 {
1453 xp->xp_context = EXPAND_USER;
1454 ++xp->xp_pattern;
1455 }
1456 }
1457}
1458
1459/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001460 * Set the completion context for the :filter command. Returns a pointer to the
1461 * next command after the :filter command.
1462 */
1463 static char_u *
1464set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1465{
1466 if (*arg != NUL)
1467 arg = skip_vimgrep_pat(arg, NULL, NULL);
1468 if (arg == NULL || *arg == NUL)
1469 {
1470 xp->xp_context = EXPAND_NOTHING;
1471 return NULL;
1472 }
1473 return skipwhite(arg);
1474}
1475
1476#ifdef FEAT_SEARCH_EXTRA
1477/*
1478 * Set the completion context for the :match command. Returns a pointer to the
1479 * next command after the :match command.
1480 */
1481 static char_u *
1482set_context_in_match_cmd(expand_T *xp, char_u *arg)
1483{
1484 if (*arg == NUL || !ends_excmd(*arg))
1485 {
1486 // also complete "None"
1487 set_context_in_echohl_cmd(xp, arg);
1488 arg = skipwhite(skiptowhite(arg));
1489 if (*arg != NUL)
1490 {
1491 xp->xp_context = EXPAND_NOTHING;
1492 arg = skip_regexp(arg + 1, *arg, magic_isset());
1493 }
1494 }
1495 return find_nextcmd(arg);
1496}
1497#endif
1498
1499/*
1500 * Returns a pointer to the next command after a :global or a :v command.
1501 * Returns NULL if there is no next command.
1502 */
1503 static char_u *
1504find_cmd_after_global_cmd(char_u *arg)
1505{
1506 int delim;
1507
1508 delim = *arg; // get the delimiter
1509 if (delim)
1510 ++arg; // skip delimiter if there is one
1511
1512 while (arg[0] != NUL && arg[0] != delim)
1513 {
1514 if (arg[0] == '\\' && arg[1] != NUL)
1515 ++arg;
1516 ++arg;
1517 }
1518 if (arg[0] != NUL)
1519 return arg + 1;
1520
1521 return NULL;
1522}
1523
1524/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001525 * Returns a pointer to the next command after a :substitute or a :& command.
1526 * Returns NULL if there is no next command.
1527 */
1528 static char_u *
1529find_cmd_after_substitute_cmd(char_u *arg)
1530{
1531 int delim;
1532
1533 delim = *arg;
1534 if (delim)
1535 {
1536 // skip "from" part
1537 ++arg;
1538 arg = skip_regexp(arg, delim, magic_isset());
1539
1540 if (arg[0] != NUL && arg[0] == delim)
1541 {
1542 // skip "to" part
1543 ++arg;
1544 while (arg[0] != NUL && arg[0] != delim)
1545 {
1546 if (arg[0] == '\\' && arg[1] != NUL)
1547 ++arg;
1548 ++arg;
1549 }
1550 if (arg[0] != NUL) // skip delimiter
1551 ++arg;
1552 }
1553 }
1554 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1555 ++arg;
1556 if (arg[0] != NUL)
1557 return arg;
1558
1559 return NULL;
1560}
1561
1562/*
1563 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1564 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1565 * Returns NULL if there is no next command.
1566 */
1567 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001568find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001569{
1570 arg = skipwhite(skipdigits(arg)); // skip count
1571 if (*arg == '/') // Match regexp, not just whole words
1572 {
1573 for (++arg; *arg && *arg != '/'; arg++)
1574 if (*arg == '\\' && arg[1] != NUL)
1575 arg++;
1576 if (*arg)
1577 {
1578 arg = skipwhite(arg + 1);
1579
1580 // Check for trailing illegal characters
1581 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1582 xp->xp_context = EXPAND_NOTHING;
1583 else
1584 return arg;
1585 }
1586 }
1587
1588 return NULL;
1589}
1590
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001591#ifdef FEAT_EVAL
1592/*
1593 * Set the completion context for the :unlet command. Always returns NULL.
1594 */
1595 static char_u *
1596set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1597{
1598 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1599 arg = xp->xp_pattern + 1;
1600
1601 xp->xp_context = EXPAND_USER_VARS;
1602 xp->xp_pattern = arg;
1603
1604 if (*xp->xp_pattern == '$')
1605 {
1606 xp->xp_context = EXPAND_ENV_VARS;
1607 ++xp->xp_pattern;
1608 }
1609
1610 return NULL;
1611}
1612#endif
1613
1614#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1615/*
1616 * Set the completion context for the :language command. Always returns NULL.
1617 */
1618 static char_u *
1619set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1620{
1621 char_u *p;
1622
1623 p = skiptowhite(arg);
1624 if (*p == NUL)
1625 {
1626 xp->xp_context = EXPAND_LANGUAGE;
1627 xp->xp_pattern = arg;
1628 }
1629 else
1630 {
1631 if ( STRNCMP(arg, "messages", p - arg) == 0
1632 || STRNCMP(arg, "ctype", p - arg) == 0
1633 || STRNCMP(arg, "time", p - arg) == 0
1634 || STRNCMP(arg, "collate", p - arg) == 0)
1635 {
1636 xp->xp_context = EXPAND_LOCALES;
1637 xp->xp_pattern = skipwhite(p);
1638 }
1639 else
1640 xp->xp_context = EXPAND_NOTHING;
1641 }
1642
1643 return NULL;
1644}
1645#endif
1646
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001647#ifdef FEAT_EVAL
1648static enum
1649{
1650 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001651 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1652 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001653} breakpt_expand_what;
1654
1655/*
1656 * Set the completion context for the :breakadd command. Always returns NULL.
1657 */
1658 static char_u *
1659set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1660{
1661 char_u *p;
1662 char_u *subcmd_start;
1663
1664 xp->xp_context = EXPAND_BREAKPOINT;
1665 xp->xp_pattern = arg;
1666
1667 if (cmdidx == CMD_breakadd)
1668 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001669 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001670 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001671 else
1672 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001673
1674 p = skipwhite(arg);
1675 if (*p == NUL)
1676 return NULL;
1677 subcmd_start = p;
1678
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001679 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001680 {
1681 // :breakadd file [lnum] <filename>
1682 // :breakadd func [lnum] <funcname>
1683 p += 4;
1684 p = skipwhite(p);
1685
1686 // skip line number (if specified)
1687 if (VIM_ISDIGIT(*p))
1688 {
1689 p = skipdigits(p);
1690 if (*p != ' ')
1691 {
1692 xp->xp_context = EXPAND_NOTHING;
1693 return NULL;
1694 }
1695 p = skipwhite(p);
1696 }
1697 if (STRNCMP("file", subcmd_start, 4) == 0)
1698 xp->xp_context = EXPAND_FILES;
1699 else
1700 xp->xp_context = EXPAND_USER_FUNC;
1701 xp->xp_pattern = p;
1702 }
1703 else if (STRNCMP("expr ", p, 5) == 0)
1704 {
1705 // :breakadd expr <expression>
1706 xp->xp_context = EXPAND_EXPRESSION;
1707 xp->xp_pattern = skipwhite(p + 5);
1708 }
1709
1710 return NULL;
1711}
1712#endif
1713
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001714/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001715 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1716 * The argument to the command is 'arg' and the argument flags is 'argt'.
1717 * For user-defined commands and for environment variables, 'compl' has the
1718 * completion type.
1719 * Returns a pointer to the next command. Returns NULL if there is no next
1720 * command.
1721 */
1722 static char_u *
1723set_context_by_cmdname(
1724 char_u *cmd,
1725 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001726 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001727 char_u *arg,
1728 long argt,
1729 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001730 int forceit)
1731{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001732 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001733 {
1734 case CMD_find:
1735 case CMD_sfind:
1736 case CMD_tabfind:
1737 if (xp->xp_context == EXPAND_FILES)
1738 xp->xp_context = EXPAND_FILES_IN_PATH;
1739 break;
1740 case CMD_cd:
1741 case CMD_chdir:
1742 case CMD_tcd:
1743 case CMD_tchdir:
1744 case CMD_lcd:
1745 case CMD_lchdir:
1746 if (xp->xp_context == EXPAND_FILES)
1747 xp->xp_context = EXPAND_DIRECTORIES;
1748 break;
1749 case CMD_help:
1750 xp->xp_context = EXPAND_HELP;
1751 xp->xp_pattern = arg;
1752 break;
1753
1754 // Command modifiers: return the argument.
1755 // Also for commands with an argument that is a command.
1756 case CMD_aboveleft:
1757 case CMD_argdo:
1758 case CMD_belowright:
1759 case CMD_botright:
1760 case CMD_browse:
1761 case CMD_bufdo:
1762 case CMD_cdo:
1763 case CMD_cfdo:
1764 case CMD_confirm:
1765 case CMD_debug:
1766 case CMD_folddoclosed:
1767 case CMD_folddoopen:
1768 case CMD_hide:
1769 case CMD_keepalt:
1770 case CMD_keepjumps:
1771 case CMD_keepmarks:
1772 case CMD_keeppatterns:
1773 case CMD_ldo:
1774 case CMD_leftabove:
1775 case CMD_lfdo:
1776 case CMD_lockmarks:
1777 case CMD_noautocmd:
1778 case CMD_noswapfile:
1779 case CMD_rightbelow:
1780 case CMD_sandbox:
1781 case CMD_silent:
1782 case CMD_tab:
1783 case CMD_tabdo:
1784 case CMD_topleft:
1785 case CMD_verbose:
1786 case CMD_vertical:
1787 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001788 case CMD_vim9cmd:
1789 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001790 return arg;
1791
1792 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001793 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001794
1795#ifdef FEAT_SEARCH_EXTRA
1796 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001797 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001798#endif
1799
1800 // All completion for the +cmdline_compl feature goes here.
1801
1802 case CMD_command:
1803 return set_context_in_user_cmd(xp, arg);
1804
1805 case CMD_delcommand:
1806 xp->xp_context = EXPAND_USER_COMMANDS;
1807 xp->xp_pattern = arg;
1808 break;
1809
1810 case CMD_global:
1811 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001812 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001813 case CMD_and:
1814 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001815 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001816 case CMD_isearch:
1817 case CMD_dsearch:
1818 case CMD_ilist:
1819 case CMD_dlist:
1820 case CMD_ijump:
1821 case CMD_psearch:
1822 case CMD_djump:
1823 case CMD_isplit:
1824 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001825 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001826 case CMD_autocmd:
1827 return set_context_in_autocmd(xp, arg, FALSE);
1828 case CMD_doautocmd:
1829 case CMD_doautoall:
1830 return set_context_in_autocmd(xp, arg, TRUE);
1831 case CMD_set:
1832 set_context_in_set_cmd(xp, arg, 0);
1833 break;
1834 case CMD_setglobal:
1835 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1836 break;
1837 case CMD_setlocal:
1838 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1839 break;
1840 case CMD_tag:
1841 case CMD_stag:
1842 case CMD_ptag:
1843 case CMD_ltag:
1844 case CMD_tselect:
1845 case CMD_stselect:
1846 case CMD_ptselect:
1847 case CMD_tjump:
1848 case CMD_stjump:
1849 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001850 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001851 xp->xp_context = EXPAND_TAGS_LISTFILES;
1852 else
1853 xp->xp_context = EXPAND_TAGS;
1854 xp->xp_pattern = arg;
1855 break;
1856 case CMD_augroup:
1857 xp->xp_context = EXPAND_AUGROUP;
1858 xp->xp_pattern = arg;
1859 break;
1860#ifdef FEAT_SYN_HL
1861 case CMD_syntax:
1862 set_context_in_syntax_cmd(xp, arg);
1863 break;
1864#endif
1865#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001866 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001867 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001868 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001869 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001870 case CMD_if:
1871 case CMD_elseif:
1872 case CMD_while:
1873 case CMD_for:
1874 case CMD_echo:
1875 case CMD_echon:
1876 case CMD_execute:
1877 case CMD_echomsg:
1878 case CMD_echoerr:
1879 case CMD_call:
1880 case CMD_return:
1881 case CMD_cexpr:
1882 case CMD_caddexpr:
1883 case CMD_cgetexpr:
1884 case CMD_lexpr:
1885 case CMD_laddexpr:
1886 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001887 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001888 break;
1889
1890 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001891 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001892 case CMD_function:
1893 case CMD_delfunction:
1894 xp->xp_context = EXPAND_USER_FUNC;
1895 xp->xp_pattern = arg;
1896 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001897 case CMD_disassemble:
1898 set_context_in_disassemble_cmd(xp, arg);
1899 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001900
1901 case CMD_echohl:
1902 set_context_in_echohl_cmd(xp, arg);
1903 break;
1904#endif
1905 case CMD_highlight:
1906 set_context_in_highlight_cmd(xp, arg);
1907 break;
1908#ifdef FEAT_CSCOPE
1909 case CMD_cscope:
1910 case CMD_lcscope:
1911 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001912 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001913 break;
1914#endif
1915#ifdef FEAT_SIGNS
1916 case CMD_sign:
1917 set_context_in_sign_cmd(xp, arg);
1918 break;
1919#endif
1920 case CMD_bdelete:
1921 case CMD_bwipeout:
1922 case CMD_bunload:
1923 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1924 arg = xp->xp_pattern + 1;
1925 // FALLTHROUGH
1926 case CMD_buffer:
1927 case CMD_sbuffer:
1928 case CMD_checktime:
1929 xp->xp_context = EXPAND_BUFFERS;
1930 xp->xp_pattern = arg;
1931 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001932#ifdef FEAT_DIFF
1933 case CMD_diffget:
1934 case CMD_diffput:
1935 // If current buffer is in diff mode, complete buffer names
1936 // which are in diff mode, and different than current buffer.
1937 xp->xp_context = EXPAND_DIFF_BUFFERS;
1938 xp->xp_pattern = arg;
1939 break;
1940#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001941 case CMD_USER:
1942 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001943 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1944 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001945
1946 case CMD_map: case CMD_noremap:
1947 case CMD_nmap: case CMD_nnoremap:
1948 case CMD_vmap: case CMD_vnoremap:
1949 case CMD_omap: case CMD_onoremap:
1950 case CMD_imap: case CMD_inoremap:
1951 case CMD_cmap: case CMD_cnoremap:
1952 case CMD_lmap: case CMD_lnoremap:
1953 case CMD_smap: case CMD_snoremap:
1954 case CMD_tmap: case CMD_tnoremap:
1955 case CMD_xmap: case CMD_xnoremap:
1956 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001957 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001958 case CMD_unmap:
1959 case CMD_nunmap:
1960 case CMD_vunmap:
1961 case CMD_ounmap:
1962 case CMD_iunmap:
1963 case CMD_cunmap:
1964 case CMD_lunmap:
1965 case CMD_sunmap:
1966 case CMD_tunmap:
1967 case CMD_xunmap:
1968 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001969 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001970 case CMD_mapclear:
1971 case CMD_nmapclear:
1972 case CMD_vmapclear:
1973 case CMD_omapclear:
1974 case CMD_imapclear:
1975 case CMD_cmapclear:
1976 case CMD_lmapclear:
1977 case CMD_smapclear:
1978 case CMD_tmapclear:
1979 case CMD_xmapclear:
1980 xp->xp_context = EXPAND_MAPCLEAR;
1981 xp->xp_pattern = arg;
1982 break;
1983
1984 case CMD_abbreviate: case CMD_noreabbrev:
1985 case CMD_cabbrev: case CMD_cnoreabbrev:
1986 case CMD_iabbrev: case CMD_inoreabbrev:
1987 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001988 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001989 case CMD_unabbreviate:
1990 case CMD_cunabbrev:
1991 case CMD_iunabbrev:
1992 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001993 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001994#ifdef FEAT_MENU
1995 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1996 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1997 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1998 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1999 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2000 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2001 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2002 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2003 case CMD_tmenu: case CMD_tunmenu:
2004 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2005 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2006#endif
2007
2008 case CMD_colorscheme:
2009 xp->xp_context = EXPAND_COLORS;
2010 xp->xp_pattern = arg;
2011 break;
2012
2013 case CMD_compiler:
2014 xp->xp_context = EXPAND_COMPILER;
2015 xp->xp_pattern = arg;
2016 break;
2017
2018 case CMD_ownsyntax:
2019 xp->xp_context = EXPAND_OWNSYNTAX;
2020 xp->xp_pattern = arg;
2021 break;
2022
2023 case CMD_setfiletype:
2024 xp->xp_context = EXPAND_FILETYPE;
2025 xp->xp_pattern = arg;
2026 break;
2027
2028 case CMD_packadd:
2029 xp->xp_context = EXPAND_PACKADD;
2030 xp->xp_pattern = arg;
2031 break;
2032
2033#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2034 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002035 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002036#endif
2037#if defined(FEAT_PROFILE)
2038 case CMD_profile:
2039 set_context_in_profile_cmd(xp, arg);
2040 break;
2041#endif
2042 case CMD_behave:
2043 xp->xp_context = EXPAND_BEHAVE;
2044 xp->xp_pattern = arg;
2045 break;
2046
2047 case CMD_messages:
2048 xp->xp_context = EXPAND_MESSAGES;
2049 xp->xp_pattern = arg;
2050 break;
2051
2052 case CMD_history:
2053 xp->xp_context = EXPAND_HISTORY;
2054 xp->xp_pattern = arg;
2055 break;
2056#if defined(FEAT_PROFILE)
2057 case CMD_syntime:
2058 xp->xp_context = EXPAND_SYNTIME;
2059 xp->xp_pattern = arg;
2060 break;
2061#endif
2062
2063 case CMD_argdelete:
2064 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2065 arg = xp->xp_pattern + 1;
2066 xp->xp_context = EXPAND_ARGLIST;
2067 xp->xp_pattern = arg;
2068 break;
2069
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002070#ifdef FEAT_EVAL
2071 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002072 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002073 case CMD_breakdel:
2074 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
2075#endif
2076
Bram Moolenaard0190392019-08-23 21:17:35 +02002077 default:
2078 break;
2079 }
2080 return NULL;
2081}
2082
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002083/*
2084 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2085 * we don't need/want deleted. Maybe this could be done better if we didn't
2086 * repeat all this stuff. The only problem is that they may not stay
2087 * perfectly compatible with each other, but then the command line syntax
2088 * probably won't change that much -- webb.
2089 */
2090 static char_u *
2091set_one_cmd_context(
2092 expand_T *xp,
2093 char_u *buff) // buffer for command string
2094{
2095 char_u *p;
2096 char_u *cmd, *arg;
2097 int len = 0;
2098 exarg_T ea;
2099 int compl = EXPAND_NOTHING;
2100 int forceit = FALSE;
2101 int usefilter = FALSE; // filter instead of file name
2102
2103 ExpandInit(xp);
2104 xp->xp_pattern = buff;
2105 xp->xp_line = buff;
2106 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2107 ea.argt = 0;
2108
2109 // 1. skip comment lines and leading space, colons or bars
2110 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2111 ;
2112 xp->xp_pattern = cmd;
2113
2114 if (*cmd == NUL)
2115 return NULL;
2116 if (*cmd == '"') // ignore comment lines
2117 {
2118 xp->xp_context = EXPAND_NOTHING;
2119 return NULL;
2120 }
2121
2122 // 3. Skip over the range to find the command.
2123 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2124 xp->xp_pattern = cmd;
2125 if (*cmd == NUL)
2126 return NULL;
2127 if (*cmd == '"')
2128 {
2129 xp->xp_context = EXPAND_NOTHING;
2130 return NULL;
2131 }
2132
2133 if (*cmd == '|' || *cmd == '\n')
2134 return cmd + 1; // There's another command
2135
2136 // Get the command index.
2137 p = set_cmd_index(cmd, &ea, xp, &compl);
2138 if (p == NULL)
2139 return NULL;
2140
2141 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2142
2143 if (*p == '!') // forced commands
2144 {
2145 forceit = TRUE;
2146 ++p;
2147 }
2148
2149 // 6. parse arguments
2150 if (!IS_USER_CMDIDX(ea.cmdidx))
2151 ea.argt = excmd_get_argt(ea.cmdidx);
2152
2153 arg = skipwhite(p);
2154
2155 // Skip over ++argopt argument
2156 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2157 {
2158 p = arg;
2159 while (*p && !vim_isspace(*p))
2160 MB_PTR_ADV(p);
2161 arg = skipwhite(p);
2162 }
2163
2164 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2165 {
2166 if (*arg == '>') // append
2167 {
2168 if (*++arg == '>')
2169 ++arg;
2170 arg = skipwhite(arg);
2171 }
2172 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2173 {
2174 ++arg;
2175 usefilter = TRUE;
2176 }
2177 }
2178
2179 if (ea.cmdidx == CMD_read)
2180 {
2181 usefilter = forceit; // :r! filter if forced
2182 if (*arg == '!') // :r !filter
2183 {
2184 ++arg;
2185 usefilter = TRUE;
2186 }
2187 }
2188
2189 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2190 {
2191 while (*arg == *cmd) // allow any number of '>' or '<'
2192 ++arg;
2193 arg = skipwhite(arg);
2194 }
2195
2196 // Does command allow "+command"?
2197 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2198 {
2199 // Check if we're in the +command
2200 p = arg + 1;
2201 arg = skip_cmd_arg(arg, FALSE);
2202
2203 // Still touching the command after '+'?
2204 if (*arg == NUL)
2205 return p;
2206
2207 // Skip space(s) after +command to get to the real argument
2208 arg = skipwhite(arg);
2209 }
2210
2211
2212 // Check for '|' to separate commands and '"' to start comments.
2213 // Don't do this for ":read !cmd" and ":write !cmd".
2214 if ((ea.argt & EX_TRLBAR) && !usefilter)
2215 {
2216 p = arg;
2217 // ":redir @" is not the start of a comment
2218 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2219 p += 2;
2220 while (*p)
2221 {
2222 if (*p == Ctrl_V)
2223 {
2224 if (p[1] != NUL)
2225 ++p;
2226 }
2227 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2228 || *p == '|' || *p == '\n')
2229 {
2230 if (*(p - 1) != '\\')
2231 {
2232 if (*p == '|' || *p == '\n')
2233 return p + 1;
2234 return NULL; // It's a comment
2235 }
2236 }
2237 MB_PTR_ADV(p);
2238 }
2239 }
2240
2241 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2242 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2243 // no arguments allowed but there is something
2244 return NULL;
2245
2246 // Find start of last argument (argument just before cursor):
2247 p = buff;
2248 xp->xp_pattern = p;
2249 len = (int)STRLEN(buff);
2250 while (*p && p < buff + len)
2251 {
2252 if (*p == ' ' || *p == TAB)
2253 {
2254 // argument starts after a space
2255 xp->xp_pattern = ++p;
2256 }
2257 else
2258 {
2259 if (*p == '\\' && *(p + 1) != NUL)
2260 ++p; // skip over escaped character
2261 MB_PTR_ADV(p);
2262 }
2263 }
2264
2265 if (ea.argt & EX_XFILE)
2266 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2267
2268 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002269 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002270 forceit);
2271}
2272
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002273/*
2274 * Set the completion context in 'xp' for command 'str'
2275 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002276 void
2277set_cmd_context(
2278 expand_T *xp,
2279 char_u *str, // start of command line
2280 int len, // length of command line (excl. NUL)
2281 int col, // position of cursor
2282 int use_ccline UNUSED) // use ccline for info
2283{
2284#ifdef FEAT_EVAL
2285 cmdline_info_T *ccline = get_cmdline_info();
2286#endif
2287 int old_char = NUL;
2288 char_u *nextcomm;
2289
2290 // Avoid a UMR warning from Purify, only save the character if it has been
2291 // written before.
2292 if (col < len)
2293 old_char = str[col];
2294 str[col] = NUL;
2295 nextcomm = str;
2296
2297#ifdef FEAT_EVAL
2298 if (use_ccline && ccline->cmdfirstc == '=')
2299 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002300 // pass CMD_SIZE because there is no real command
2301 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002302 }
2303 else if (use_ccline && ccline->input_fn)
2304 {
2305 xp->xp_context = ccline->xp_context;
2306 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002307 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002308 }
2309 else
2310#endif
2311 while (nextcomm != NULL)
2312 nextcomm = set_one_cmd_context(xp, nextcomm);
2313
2314 // Store the string here so that call_user_expand_func() can get to them
2315 // easily.
2316 xp->xp_line = str;
2317 xp->xp_col = col;
2318
2319 str[col] = old_char;
2320}
2321
2322/*
2323 * Expand the command line "str" from context "xp".
2324 * "xp" must have been set by set_cmd_context().
2325 * xp->xp_pattern points into "str", to where the text that is to be expanded
2326 * starts.
2327 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2328 * cursor.
2329 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2330 * key that triggered expansion literally.
2331 * Returns EXPAND_OK otherwise.
2332 */
2333 int
2334expand_cmdline(
2335 expand_T *xp,
2336 char_u *str, // start of command line
2337 int col, // position of cursor
2338 int *matchcount, // return: nr of matches
2339 char_u ***matches) // return: array of pointers to matches
2340{
2341 char_u *file_str = NULL;
2342 int options = WILD_ADD_SLASH|WILD_SILENT;
2343
2344 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2345 {
2346 beep_flush();
2347 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2348 }
2349 if (xp->xp_context == EXPAND_NOTHING)
2350 {
2351 // Caller can use the character as a normal char instead
2352 return EXPAND_NOTHING;
2353 }
2354
2355 // add star to file name, or convert to regexp if not exp. files.
2356 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002357 if (cmdline_fuzzy_completion_supported(xp))
2358 // If fuzzy matching, don't modify the search string
2359 file_str = vim_strsave(xp->xp_pattern);
2360 else
2361 {
2362 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2363 if (file_str == NULL)
2364 return EXPAND_UNSUCCESSFUL;
2365 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002366
2367 if (p_wic)
2368 options += WILD_ICASE;
2369
2370 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002371 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002372 {
2373 *matchcount = 0;
2374 *matches = NULL;
2375 }
2376 vim_free(file_str);
2377
2378 return EXPAND_OK;
2379}
2380
Bram Moolenaar66b51422019-08-18 21:44:12 +02002381/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002382 * Expand file or directory names.
2383 */
2384 static int
2385expand_files_and_dirs(
2386 expand_T *xp,
2387 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002388 char_u ***matches,
2389 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002390 int flags,
2391 int options)
2392{
2393 int free_pat = FALSE;
2394 int i;
2395 int ret;
2396
2397 // for ":set path=" and ":set tags=" halve backslashes for escaped
2398 // space
2399 if (xp->xp_backslash != XP_BS_NONE)
2400 {
2401 free_pat = TRUE;
2402 pat = vim_strsave(pat);
2403 for (i = 0; pat[i]; ++i)
2404 if (pat[i] == '\\')
2405 {
2406 if (xp->xp_backslash == XP_BS_THREE
2407 && pat[i + 1] == '\\'
2408 && pat[i + 2] == '\\'
2409 && pat[i + 3] == ' ')
2410 STRMOVE(pat + i, pat + i + 3);
2411 if (xp->xp_backslash == XP_BS_ONE
2412 && pat[i + 1] == ' ')
2413 STRMOVE(pat + i, pat + i + 1);
2414 }
2415 }
2416
2417 if (xp->xp_context == EXPAND_FILES)
2418 flags |= EW_FILE;
2419 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2420 flags |= (EW_FILE | EW_PATH);
2421 else
2422 flags = (flags | EW_DIR) & ~EW_FILE;
2423 if (options & WILD_ICASE)
2424 flags |= EW_ICASE;
2425
2426 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002427 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002428 if (free_pat)
2429 vim_free(pat);
2430#ifdef BACKSLASH_IN_FILENAME
2431 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2432 {
2433 int j;
2434
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002435 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002436 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002437 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002438
2439 while (*ptr != NUL)
2440 {
2441 if (p_csl[0] == 's' && *ptr == '\\')
2442 *ptr = '/';
2443 else if (p_csl[0] == 'b' && *ptr == '/')
2444 *ptr = '\\';
2445 ptr += (*mb_ptr2len)(ptr);
2446 }
2447 }
2448 }
2449#endif
2450 return ret;
2451}
2452
2453/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002454 * Function given to ExpandGeneric() to obtain the possible arguments of the
2455 * ":behave {mswin,xterm}" command.
2456 */
2457 static char_u *
2458get_behave_arg(expand_T *xp UNUSED, int idx)
2459{
2460 if (idx == 0)
2461 return (char_u *)"mswin";
2462 if (idx == 1)
2463 return (char_u *)"xterm";
2464 return NULL;
2465}
2466
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002467# ifdef FEAT_EVAL
2468/*
2469 * Function given to ExpandGeneric() to obtain the possible arguments of the
2470 * ":breakadd {expr, file, func, here}" command.
2471 * ":breakdel {func, file, here}" command.
2472 */
2473 static char_u *
2474get_breakadd_arg(expand_T *xp UNUSED, int idx)
2475{
2476 char *opts[] = {"expr", "file", "func", "here"};
2477
2478 if (idx >=0 && idx <= 3)
2479 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002480 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002481 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2482 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002483 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2484 {
2485 // breakdel {func, file, here}
2486 if (idx <= 2)
2487 return (char_u *)opts[idx + 1];
2488 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002489 else
2490 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002491 // profdel {func, file}
2492 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002493 return (char_u *)opts[idx + 1];
2494 }
2495 }
2496 return NULL;
2497}
2498#endif
2499
Bram Moolenaard0190392019-08-23 21:17:35 +02002500/*
2501 * Function given to ExpandGeneric() to obtain the possible arguments of the
2502 * ":messages {clear}" command.
2503 */
2504 static char_u *
2505get_messages_arg(expand_T *xp UNUSED, int idx)
2506{
2507 if (idx == 0)
2508 return (char_u *)"clear";
2509 return NULL;
2510}
2511
2512 static char_u *
2513get_mapclear_arg(expand_T *xp UNUSED, int idx)
2514{
2515 if (idx == 0)
2516 return (char_u *)"<buffer>";
2517 return NULL;
2518}
2519
2520/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002521 * Do the expansion based on xp->xp_context and 'rmp'.
2522 */
2523 static int
2524ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002525 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002526 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002527 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002528 char_u ***matches,
2529 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002530{
2531 static struct expgen
2532 {
2533 int context;
2534 char_u *((*func)(expand_T *, int));
2535 int ic;
2536 int escaped;
2537 } tab[] =
2538 {
2539 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2540 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2541 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2542 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2543 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2544 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2545 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2546 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2547 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2548 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002549#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002550 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2551 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2552 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2553 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2554 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002555#endif
2556#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002557 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2558 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002559#endif
2560#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002561 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002562#endif
2563#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002564 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002565#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002566 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2567 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2568 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002569#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002570 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002571#endif
2572#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002573 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002574#endif
2575#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002576 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002577#endif
2578#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002579 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2580 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002581#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002582 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2583 {EXPAND_USER, get_users, TRUE, FALSE},
2584 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002585#ifdef FEAT_EVAL
2586 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
2587#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002588 };
2589 int i;
2590 int ret = FAIL;
2591
2592 // Find a context in the table and call the ExpandGeneric() with the
2593 // right function to do the expansion.
2594 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2595 {
2596 if (xp->xp_context == tab[i].context)
2597 {
2598 if (tab[i].ic)
2599 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002600 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2601 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002602 break;
2603 }
2604 }
2605
2606 return ret;
2607}
2608
2609/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002610 * Map wild expand options to flags for expand_wildcards()
2611 */
2612 static int
2613map_wildopts_to_ewflags(int options)
2614{
2615 int flags;
2616
2617 flags = EW_DIR; // include directories
2618 if (options & WILD_LIST_NOTFOUND)
2619 flags |= EW_NOTFOUND;
2620 if (options & WILD_ADD_SLASH)
2621 flags |= EW_ADDSLASH;
2622 if (options & WILD_KEEP_ALL)
2623 flags |= EW_KEEPALL;
2624 if (options & WILD_SILENT)
2625 flags |= EW_SILENT;
2626 if (options & WILD_NOERROR)
2627 flags |= EW_NOERROR;
2628 if (options & WILD_ALLLINKS)
2629 flags |= EW_ALLLINKS;
2630
2631 return flags;
2632}
2633
2634/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002635 * Do the expansion based on xp->xp_context and "pat".
2636 */
2637 static int
2638ExpandFromContext(
2639 expand_T *xp,
2640 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002641 char_u ***matches,
2642 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002643 int options) // WILD_ flags
2644{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002645 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002646 int ret;
2647 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002648 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002649 int fuzzy = cmdline_fuzzy_complete(pat)
2650 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002651
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002652 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002653
2654 if (xp->xp_context == EXPAND_FILES
2655 || xp->xp_context == EXPAND_DIRECTORIES
2656 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002657 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2658 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002659
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002660 *matches = (char_u **)"";
2661 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002662 if (xp->xp_context == EXPAND_HELP)
2663 {
2664 // With an empty argument we would get all the help tags, which is
2665 // very slow. Get matches for "help" instead.
2666 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002667 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002668 {
2669#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002670 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002671#endif
2672 return OK;
2673 }
2674 return FAIL;
2675 }
2676
Bram Moolenaar66b51422019-08-18 21:44:12 +02002677 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002678 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002679 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002680 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002681 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002682 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002683#ifdef FEAT_DIFF
2684 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002685 return ExpandBufnames(pat, numMatches, matches,
2686 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002687#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002688 if (xp->xp_context == EXPAND_TAGS
2689 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002690 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2691 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002692 if (xp->xp_context == EXPAND_COLORS)
2693 {
2694 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002695 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002696 directories);
2697 }
2698 if (xp->xp_context == EXPAND_COMPILER)
2699 {
2700 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002701 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002702 }
2703 if (xp->xp_context == EXPAND_OWNSYNTAX)
2704 {
2705 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002706 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002707 }
2708 if (xp->xp_context == EXPAND_FILETYPE)
2709 {
2710 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002711 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002712 }
2713# if defined(FEAT_EVAL)
2714 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002715 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002716# endif
2717 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002718 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002719
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002720 // When expanding a function name starting with s:, match the <SNR>nr_
2721 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002722 if ((xp->xp_context == EXPAND_USER_FUNC
2723 || xp->xp_context == EXPAND_DISASSEMBLE)
2724 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002725 {
2726 int len = (int)STRLEN(pat) + 20;
2727
2728 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002729 if (tofree == NULL)
2730 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002731 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002732 pat = tofree;
2733 }
2734
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002735 if (!fuzzy)
2736 {
2737 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2738 if (regmatch.regprog == NULL)
2739 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002740
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002741 // set ignore-case according to p_ic, p_scs and pat
2742 regmatch.rm_ic = ignorecase(pat);
2743 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002744
2745 if (xp->xp_context == EXPAND_SETTINGS
2746 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002747 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002748 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002749 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002750# if defined(FEAT_EVAL)
2751 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002752 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002753# endif
2754 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002755 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002756
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002757 if (!fuzzy)
2758 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002759 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002760
2761 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002762}
2763
Bram Moolenaar66b51422019-08-18 21:44:12 +02002764/*
2765 * Expand a list of names.
2766 *
2767 * Generic function for command line completion. It calls a function to
2768 * obtain strings, one by one. The strings are matched against a regexp
2769 * program. Matching strings are copied into an array, which is returned.
2770 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002771 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2772 * is used.
2773 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002774 * Returns OK when no problems encountered, FAIL for error (out of memory).
2775 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002776 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002777ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002778 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002779 expand_T *xp,
2780 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002781 char_u ***matches,
2782 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002783 char_u *((*func)(expand_T *, int)),
2784 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002785 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002786{
2787 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002788 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002789 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002790 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002791 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002792 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002793 int match;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002794
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002795 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002796 *matches = NULL;
2797 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002798
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002799 if (!fuzzy)
2800 ga_init2(&ga, sizeof(char *), 30);
2801 else
2802 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2803
2804 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002805 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002806 str = (*func)(xp, i);
2807 if (str == NULL) // end of list
2808 break;
2809 if (*str == NUL) // skip empty strings
2810 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002811
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002812 if (xp->xp_pattern[0] != NUL)
2813 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002814 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002815 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002816 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002817 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002818 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002819 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002820 }
2821 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002822 else
2823 match = TRUE;
2824
2825 if (!match)
2826 continue;
2827
2828 if (escaped)
2829 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2830 else
2831 str = vim_strsave(str);
2832 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002833 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002834 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002835 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002836 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002837 return FAIL;
2838 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002839
2840 for (i = 0; i < ga.ga_len; ++i)
2841 {
2842 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[i];
2843 vim_free(fuzmatch->str);
2844 }
2845 ga_clear(&ga);
2846 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002847 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002848
2849 if (ga_grow(&ga, 1) == FAIL)
2850 {
2851 vim_free(str);
2852 break;
2853 }
2854
2855 if (fuzzy)
2856 {
2857 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2858 fuzmatch->idx = ga.ga_len;
2859 fuzmatch->str = str;
2860 fuzmatch->score = score;
2861 }
2862 else
2863 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2864
2865# ifdef FEAT_MENU
2866 if (func == get_menu_names)
2867 {
2868 // test for separator added by get_menu_names()
2869 str += STRLEN(str) - 1;
2870 if (*str == '\001')
2871 *str = '.';
2872 }
2873# endif
2874
2875 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002876 }
2877
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002878 if (ga.ga_len == 0)
2879 return OK;
2880
Bram Moolenaar66b51422019-08-18 21:44:12 +02002881 // Sort the results. Keep menu's in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002882 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
2883 && xp->xp_context != EXPAND_MENUS)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002884 {
2885 if (xp->xp_context == EXPAND_EXPRESSION
2886 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002887 || xp->xp_context == EXPAND_USER_FUNC
2888 || xp->xp_context == EXPAND_DISASSEMBLE)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002889 // <SNR> functions should be sorted to the end.
2890 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2891 sort_func_compare);
2892 else
2893 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2894 }
2895
2896 if (!fuzzy)
2897 {
2898 *matches = ga.ga_data;
2899 *numMatches = ga.ga_len;
2900 }
2901 else
2902 {
2903 int funcsort = FALSE;
2904
2905 if (xp->xp_context == EXPAND_EXPRESSION
2906 || xp->xp_context == EXPAND_FUNCTIONS
2907 || xp->xp_context == EXPAND_USER_FUNC
2908 || xp->xp_context == EXPAND_DISASSEMBLE)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002909 // <SNR> functions should be sorted to the end.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002910 funcsort = TRUE;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002911
2912 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2913 funcsort) == FAIL)
2914 return FAIL;
2915 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002916 }
2917
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002918#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002919 // Reset the variables used for special highlight names expansion, so that
2920 // they don't show up when getting normal highlight names by ID.
2921 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002922#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002923
Bram Moolenaar66b51422019-08-18 21:44:12 +02002924 return OK;
2925}
2926
2927/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002928 * Expand shell command matches in one directory of $PATH.
2929 */
2930 static void
2931expand_shellcmd_onedir(
2932 char_u *buf,
2933 char_u *s,
2934 size_t l,
2935 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002936 char_u ***matches,
2937 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002938 int flags,
2939 hashtab_T *ht,
2940 garray_T *gap)
2941{
2942 int ret;
2943 int i;
2944 hash_T hash;
2945 hashitem_T *hi;
2946
2947 vim_strncpy(buf, s, l);
2948 add_pathsep(buf);
2949 l = STRLEN(buf);
2950 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2951
2952 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002953 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002954 if (ret == OK)
2955 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002956 if (ga_grow(gap, *numMatches) == FAIL)
2957 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002958 else
2959 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002960 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002961 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002962 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002963
2964 if (STRLEN(name) > l)
2965 {
2966 // Check if this name was already found.
2967 hash = hash_hash(name + l);
2968 hi = hash_lookup(ht, name + l, hash);
2969 if (HASHITEM_EMPTY(hi))
2970 {
2971 // Remove the path that was prepended.
2972 STRMOVE(name, name + l);
2973 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2974 hash_add_item(ht, hi, name, hash);
2975 name = NULL;
2976 }
2977 }
2978 vim_free(name);
2979 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002980 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002981 }
2982 }
2983}
2984
2985/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002986 * Complete a shell command.
2987 * Returns FAIL or OK;
2988 */
2989 static int
2990expand_shellcmd(
2991 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002992 char_u ***matches, // return: array with matches
2993 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02002994 int flagsarg) // EW_ flags
2995{
2996 char_u *pat;
2997 int i;
2998 char_u *path = NULL;
2999 int mustfree = FALSE;
3000 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003001 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003002 size_t l;
3003 char_u *s, *e;
3004 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003005 int did_curdir = FALSE;
3006 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003007
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003008 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003009 if (buf == NULL)
3010 return FAIL;
3011
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003012 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003013 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003014 if (pat == NULL)
3015 {
3016 vim_free(buf);
3017 return FAIL;
3018 }
3019
Bram Moolenaar66b51422019-08-18 21:44:12 +02003020 for (i = 0; pat[i]; ++i)
3021 if (pat[i] == '\\' && pat[i + 1] == ' ')
3022 STRMOVE(pat + i, pat + i + 1);
3023
3024 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3025
3026 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3027 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3028 path = (char_u *)".";
3029 else
3030 {
3031 // For an absolute name we don't use $PATH.
3032 if (!mch_isFullName(pat))
3033 path = vim_getenv((char_u *)"PATH", &mustfree);
3034 if (path == NULL)
3035 path = (char_u *)"";
3036 }
3037
3038 // Go over all directories in $PATH. Expand matches in that directory and
3039 // collect them in "ga". When "." is not in $PATH also expand for the
3040 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003041 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003042 hash_init(&found_ht);
3043 for (s = path; ; s = e)
3044 {
3045# if defined(MSWIN)
3046 e = vim_strchr(s, ';');
3047# else
3048 e = vim_strchr(s, ':');
3049# endif
3050 if (e == NULL)
3051 e = s + STRLEN(s);
3052
3053 if (*s == NUL)
3054 {
3055 if (did_curdir)
3056 break;
3057 // Find directories in the current directory, path is empty.
3058 did_curdir = TRUE;
3059 flags |= EW_DIR;
3060 }
3061 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3062 {
3063 did_curdir = TRUE;
3064 flags |= EW_DIR;
3065 }
3066 else
3067 // Do not match directories inside a $PATH item.
3068 flags &= ~EW_DIR;
3069
3070 l = e - s;
3071 if (l > MAXPATHL - 5)
3072 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003073
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003074 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003075 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003076
Bram Moolenaar66b51422019-08-18 21:44:12 +02003077 if (*e != NUL)
3078 ++e;
3079 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003080 *matches = ga.ga_data;
3081 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003082
3083 vim_free(buf);
3084 vim_free(pat);
3085 if (mustfree)
3086 vim_free(path);
3087 hash_clear(&found_ht);
3088 return OK;
3089}
3090
3091# if defined(FEAT_EVAL)
3092/*
3093 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003094 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003095 */
3096 static void *
3097call_user_expand_func(
3098 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003099 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003100{
3101 cmdline_info_T *ccline = get_cmdline_info();
3102 int keep = 0;
3103 typval_T args[4];
3104 sctx_T save_current_sctx = current_sctx;
3105 char_u *pat = NULL;
3106 void *ret;
3107
3108 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3109 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110
3111 if (ccline->cmdbuff != NULL)
3112 {
3113 keep = ccline->cmdbuff[ccline->cmdlen];
3114 ccline->cmdbuff[ccline->cmdlen] = 0;
3115 }
3116
3117 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3118
3119 args[0].v_type = VAR_STRING;
3120 args[0].vval.v_string = pat;
3121 args[1].v_type = VAR_STRING;
3122 args[1].vval.v_string = xp->xp_line;
3123 args[2].v_type = VAR_NUMBER;
3124 args[2].vval.v_number = xp->xp_col;
3125 args[3].v_type = VAR_UNKNOWN;
3126
3127 current_sctx = xp->xp_script_ctx;
3128
3129 ret = user_expand_func(xp->xp_arg, 3, args);
3130
3131 current_sctx = save_current_sctx;
3132 if (ccline->cmdbuff != NULL)
3133 ccline->cmdbuff[ccline->cmdlen] = keep;
3134
3135 vim_free(pat);
3136 return ret;
3137}
3138
3139/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003140 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3141 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003142 */
3143 static int
3144ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003145 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003146 expand_T *xp,
3147 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003148 char_u ***matches,
3149 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003150{
3151 char_u *retstr;
3152 char_u *s;
3153 char_u *e;
3154 int keep;
3155 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003156 int fuzzy;
3157 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003158 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003159
3160 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003161 *matches = NULL;
3162 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003163
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003164 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003165 if (retstr == NULL)
3166 return FAIL;
3167
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003168 if (!fuzzy)
3169 ga_init2(&ga, sizeof(char *), 3);
3170 else
3171 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3172
Bram Moolenaar66b51422019-08-18 21:44:12 +02003173 for (s = retstr; *s != NUL; s = e)
3174 {
3175 e = vim_strchr(s, '\n');
3176 if (e == NULL)
3177 e = s + STRLEN(s);
3178 keep = *e;
3179 *e = NUL;
3180
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003181 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003182 {
3183 if (!fuzzy)
3184 match = vim_regexec(regmatch, s, (colnr_T)0);
3185 else
3186 {
3187 score = fuzzy_match_str(s, pat);
3188 match = (score != 0);
3189 }
3190 }
3191 else
3192 match = TRUE; // match everything
3193
Bram Moolenaar66b51422019-08-18 21:44:12 +02003194 *e = keep;
3195
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003196 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003197 {
3198 if (ga_grow(&ga, 1) == FAIL)
3199 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003200 if (!fuzzy)
3201 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3202 else
3203 {
3204 fuzmatch_str_T *fuzmatch =
3205 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003206 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003207 fuzmatch->str = vim_strnsave(s, e - s);
3208 fuzmatch->score = score;
3209 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003210 ++ga.ga_len;
3211 }
3212
3213 if (*e != NUL)
3214 ++e;
3215 }
3216 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003217
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003218 if (ga.ga_len == 0)
3219 return OK;
3220
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003221 if (!fuzzy)
3222 {
3223 *matches = ga.ga_data;
3224 *numMatches = ga.ga_len;
3225 }
3226 else
3227 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003228 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3229 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003230 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003231 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003232 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003233 return OK;
3234}
3235
3236/*
3237 * Expand names with a list returned by a function defined by the user.
3238 */
3239 static int
3240ExpandUserList(
3241 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003242 char_u ***matches,
3243 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003244{
3245 list_T *retlist;
3246 listitem_T *li;
3247 garray_T ga;
3248
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003249 *matches = NULL;
3250 *numMatches = 0;
3251 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003252 if (retlist == NULL)
3253 return FAIL;
3254
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003255 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003256 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003257 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003258 {
3259 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3260 continue; // Skip non-string items and empty strings
3261
3262 if (ga_grow(&ga, 1) == FAIL)
3263 break;
3264
3265 ((char_u **)ga.ga_data)[ga.ga_len] =
3266 vim_strsave(li->li_tv.vval.v_string);
3267 ++ga.ga_len;
3268 }
3269 list_unref(retlist);
3270
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003271 *matches = ga.ga_data;
3272 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003273 return OK;
3274}
3275# endif
3276
3277/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003278 * Expand "file" for all comma-separated directories in "path".
3279 * Adds the matches to "ga". Caller must init "ga".
3280 */
3281 void
3282globpath(
3283 char_u *path,
3284 char_u *file,
3285 garray_T *ga,
3286 int expand_options)
3287{
3288 expand_T xpc;
3289 char_u *buf;
3290 int i;
3291 int num_p;
3292 char_u **p;
3293
3294 buf = alloc(MAXPATHL);
3295 if (buf == NULL)
3296 return;
3297
3298 ExpandInit(&xpc);
3299 xpc.xp_context = EXPAND_FILES;
3300
3301 // Loop over all entries in {path}.
3302 while (*path != NUL)
3303 {
3304 // Copy one item of the path to buf[] and concatenate the file name.
3305 copy_option_part(&path, buf, MAXPATHL, ",");
3306 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3307 {
3308# if defined(MSWIN)
3309 // Using the platform's path separator (\) makes vim incorrectly
3310 // treat it as an escape character, use '/' instead.
3311 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3312 STRCAT(buf, "/");
3313# else
3314 add_pathsep(buf);
3315# endif
3316 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003317 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003318 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3319 {
3320 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3321
3322 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003323 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003324 for (i = 0; i < num_p; ++i)
3325 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003326 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003327 ++ga->ga_len;
3328 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003329 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003330 }
3331 }
3332 }
3333
3334 vim_free(buf);
3335}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003336
Bram Moolenaareadee482020-09-04 15:37:31 +02003337#ifdef FEAT_WILDMENU
3338
3339/*
3340 * Translate some keys pressed when 'wildmenu' is used.
3341 */
3342 int
3343wildmenu_translate_key(
3344 cmdline_info_T *cclp,
3345 int key,
3346 expand_T *xp,
3347 int did_wild_list)
3348{
3349 int c = key;
3350
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003351#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003352 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003353 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003354 // When the popup menu is used for cmdline completion:
3355 // Up : go to the previous item in the menu
3356 // Down : go to the next item in the menu
3357 // Left : go to the parent directory
3358 // Right: list the files in the selected directory
3359 switch (c)
3360 {
3361 case K_UP: c = K_LEFT; break;
3362 case K_DOWN: c = K_RIGHT; break;
3363 case K_LEFT: c = K_UP; break;
3364 case K_RIGHT: c = K_DOWN; break;
3365 default: break;
3366 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003367 }
3368#endif
3369
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003370 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003371 {
3372 if (c == K_LEFT)
3373 c = Ctrl_P;
3374 else if (c == K_RIGHT)
3375 c = Ctrl_N;
3376 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003377
Bram Moolenaareadee482020-09-04 15:37:31 +02003378 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003379 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003380 && cclp->cmdpos > 1
3381 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3382 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3383 && (c == '\n' || c == '\r' || c == K_KENTER))
3384 c = K_DOWN;
3385
3386 return c;
3387}
3388
3389/*
3390 * Delete characters on the command line, from "from" to the current
3391 * position.
3392 */
3393 static void
3394cmdline_del(cmdline_info_T *cclp, int from)
3395{
3396 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3397 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3398 cclp->cmdlen -= cclp->cmdpos - from;
3399 cclp->cmdpos = from;
3400}
3401
3402/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003403 * Handle a key pressed when the wild menu for the menu names
3404 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003405 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003406 static int
3407wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003408{
Bram Moolenaareadee482020-09-04 15:37:31 +02003409 int i;
3410 int j;
3411
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003412 // Hitting <Down> after "emenu Name.": complete submenu
3413 if (key == K_DOWN && cclp->cmdpos > 0
3414 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003415 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003416 key = p_wc;
3417 KeyTyped = TRUE; // in case the key was mapped
3418 }
3419 else if (key == K_UP)
3420 {
3421 // Hitting <Up>: Remove one submenu name in front of the
3422 // cursor
3423 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003424
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003425 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3426 i = 0;
3427 while (--j > 0)
3428 {
3429 // check for start of menu name
3430 if (cclp->cmdbuff[j] == ' '
3431 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003432 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003433 i = j + 1;
3434 break;
3435 }
3436 // check for start of submenu name
3437 if (cclp->cmdbuff[j] == '.'
3438 && cclp->cmdbuff[j - 1] != '\\')
3439 {
3440 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003441 {
3442 i = j + 1;
3443 break;
3444 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003445 else
3446 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003447 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003448 }
3449 if (i > 0)
3450 cmdline_del(cclp, i);
3451 key = p_wc;
3452 KeyTyped = TRUE; // in case the key was mapped
3453 xp->xp_context = EXPAND_NOTHING;
3454 }
3455
3456 return key;
3457}
3458
3459/*
3460 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3461 * directory names (EXPAND_DIRECTORIES) or shell command names
3462 * (EXPAND_SHELLCMD) is displayed.
3463 */
3464 static int
3465wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3466{
3467 int i;
3468 int j;
3469 char_u upseg[5];
3470
3471 upseg[0] = PATHSEP;
3472 upseg[1] = '.';
3473 upseg[2] = '.';
3474 upseg[3] = PATHSEP;
3475 upseg[4] = NUL;
3476
3477 if (key == K_DOWN
3478 && cclp->cmdpos > 0
3479 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3480 && (cclp->cmdpos < 3
3481 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3482 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3483 {
3484 // go down a directory
3485 key = p_wc;
3486 KeyTyped = TRUE; // in case the key was mapped
3487 }
3488 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3489 {
3490 // If in a direct ancestor, strip off one ../ to go down
3491 int found = FALSE;
3492
3493 j = cclp->cmdpos;
3494 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3495 while (--j > i)
3496 {
3497 if (has_mbyte)
3498 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3499 if (vim_ispathsep(cclp->cmdbuff[j]))
3500 {
3501 found = TRUE;
3502 break;
3503 }
3504 }
3505 if (found
3506 && cclp->cmdbuff[j - 1] == '.'
3507 && cclp->cmdbuff[j - 2] == '.'
3508 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3509 {
3510 cmdline_del(cclp, j - 2);
3511 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003512 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003513 }
3514 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003515 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003516 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003517 // go up a directory
3518 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003519
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003520 j = cclp->cmdpos - 1;
3521 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3522 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003523 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003524 if (has_mbyte)
3525 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3526 if (vim_ispathsep(cclp->cmdbuff[j])
3527# ifdef BACKSLASH_IN_FILENAME
3528 && vim_strchr((char_u *)" *?[{`$%#",
3529 cclp->cmdbuff[j + 1]) == NULL
3530# endif
3531 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003532 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003533 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003534 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003535 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003536 break;
3537 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003538 else
3539 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003540 }
3541 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003542
3543 if (!found)
3544 j = i;
3545 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3546 j += 4;
3547 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3548 && j == i)
3549 j += 3;
3550 else
3551 j = 0;
3552 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003553 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003554 // TODO this is only for DOS/UNIX systems - need to put in
3555 // machine-specific stuff here and in upseg init
3556 cmdline_del(cclp, j);
3557 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003558 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003559 else if (cclp->cmdpos > i)
3560 cmdline_del(cclp, i);
3561
3562 // Now complete in the new directory. Set KeyTyped in case the
3563 // Up key came from a mapping.
3564 key = p_wc;
3565 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003566 }
3567
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003568 return key;
3569}
3570
3571/*
3572 * Handle a key pressed when the wild menu is displayed
3573 */
3574 int
3575wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3576{
3577 if (xp->xp_context == EXPAND_MENUNAMES)
3578 return wildmenu_process_key_menunames(cclp, key, xp);
3579 else if ((xp->xp_context == EXPAND_FILES
3580 || xp->xp_context == EXPAND_DIRECTORIES
3581 || xp->xp_context == EXPAND_SHELLCMD))
3582 return wildmenu_process_key_filenames(cclp, key, xp);
3583
3584 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003585}
3586
3587/*
3588 * Free expanded names when finished walking through the matches
3589 */
3590 void
3591wildmenu_cleanup(cmdline_info_T *cclp)
3592{
3593 int skt = KeyTyped;
3594 int old_RedrawingDisabled = RedrawingDisabled;
3595
3596 if (!p_wmnu || wild_menu_showing == 0)
3597 return;
3598
3599 if (cclp->input_fn)
3600 RedrawingDisabled = 0;
3601
3602 if (wild_menu_showing == WM_SCROLLED)
3603 {
3604 // Entered command line, move it up
3605 cmdline_row--;
3606 redrawcmd();
3607 }
3608 else if (save_p_ls != -1)
3609 {
3610 // restore 'laststatus' and 'winminheight'
3611 p_ls = save_p_ls;
3612 p_wmh = save_p_wmh;
3613 last_status(FALSE);
3614 update_screen(VALID); // redraw the screen NOW
3615 redrawcmd();
3616 save_p_ls = -1;
3617 }
3618 else
3619 {
3620 win_redraw_last_status(topframe);
3621 redraw_statuslines();
3622 }
3623 KeyTyped = skt;
3624 wild_menu_showing = 0;
3625 if (cclp->input_fn)
3626 RedrawingDisabled = old_RedrawingDisabled;
3627}
3628#endif
3629
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003630#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003631/*
3632 * "getcompletion()" function
3633 */
3634 void
3635f_getcompletion(typval_T *argvars, typval_T *rettv)
3636{
3637 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003638 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003639 expand_T xpc;
3640 int filtered = FALSE;
3641 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003642 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003643
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003644 if (in_vim9script()
3645 && (check_for_string_arg(argvars, 0) == FAIL
3646 || check_for_string_arg(argvars, 1) == FAIL
3647 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3648 return;
3649
ii144785fe02021-11-21 12:13:56 +00003650 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003651 if (argvars[1].v_type != VAR_STRING)
3652 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003653 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003654 return;
3655 }
3656 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003657
3658 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003659 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003660
3661 if (p_wic)
3662 options |= WILD_ICASE;
3663
3664 // For filtered results, 'wildignore' is used
3665 if (!filtered)
3666 options |= WILD_KEEP_ALL;
3667
3668 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003669 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003670 {
ii144785fe02021-11-21 12:13:56 +00003671 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003672 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003673 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003674 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003675 else
3676 {
ii144785fe02021-11-21 12:13:56 +00003677 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003678 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3679
3680 xpc.xp_context = cmdcomplete_str_to_type(type);
3681 if (xpc.xp_context == EXPAND_NOTHING)
3682 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003683 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003684 return;
3685 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003686
3687# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003688 if (xpc.xp_context == EXPAND_MENUS)
3689 {
3690 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3691 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3692 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003693# endif
3694# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003695 if (xpc.xp_context == EXPAND_CSCOPE)
3696 {
3697 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3698 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3699 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003700# endif
3701# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003702 if (xpc.xp_context == EXPAND_SIGN)
3703 {
3704 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3705 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3706 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003707# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003708 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003709
3710 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3711 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3712 {
3713 int i;
3714
3715 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3716
3717 for (i = 0; i < xpc.xp_numfiles; i++)
3718 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3719 }
3720 vim_free(pat);
3721 ExpandCleanup(&xpc);
3722}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003723#endif // FEAT_EVAL