blob: ca669c07980e3ad7a52fd975b170bfc97847e91d [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}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001712
1713 static char_u *
1714set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1715{
1716 char_u *p;
1717
1718 xp->xp_context = EXPAND_NOTHING;
1719 xp->xp_pattern = NULL;
1720
1721 p = skipwhite(arg);
1722 if (VIM_ISDIGIT(*p))
1723 return NULL;
1724
1725 xp->xp_context = EXPAND_SCRIPTNAMES;
1726 xp->xp_pattern = p;
1727
1728 return NULL;
1729}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001730#endif
1731
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001732/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001733 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1734 * The argument to the command is 'arg' and the argument flags is 'argt'.
1735 * For user-defined commands and for environment variables, 'compl' has the
1736 * completion type.
1737 * Returns a pointer to the next command. Returns NULL if there is no next
1738 * command.
1739 */
1740 static char_u *
1741set_context_by_cmdname(
1742 char_u *cmd,
1743 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001744 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001745 char_u *arg,
1746 long argt,
1747 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001748 int forceit)
1749{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001750 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001751 {
1752 case CMD_find:
1753 case CMD_sfind:
1754 case CMD_tabfind:
1755 if (xp->xp_context == EXPAND_FILES)
1756 xp->xp_context = EXPAND_FILES_IN_PATH;
1757 break;
1758 case CMD_cd:
1759 case CMD_chdir:
1760 case CMD_tcd:
1761 case CMD_tchdir:
1762 case CMD_lcd:
1763 case CMD_lchdir:
1764 if (xp->xp_context == EXPAND_FILES)
1765 xp->xp_context = EXPAND_DIRECTORIES;
1766 break;
1767 case CMD_help:
1768 xp->xp_context = EXPAND_HELP;
1769 xp->xp_pattern = arg;
1770 break;
1771
1772 // Command modifiers: return the argument.
1773 // Also for commands with an argument that is a command.
1774 case CMD_aboveleft:
1775 case CMD_argdo:
1776 case CMD_belowright:
1777 case CMD_botright:
1778 case CMD_browse:
1779 case CMD_bufdo:
1780 case CMD_cdo:
1781 case CMD_cfdo:
1782 case CMD_confirm:
1783 case CMD_debug:
1784 case CMD_folddoclosed:
1785 case CMD_folddoopen:
1786 case CMD_hide:
1787 case CMD_keepalt:
1788 case CMD_keepjumps:
1789 case CMD_keepmarks:
1790 case CMD_keeppatterns:
1791 case CMD_ldo:
1792 case CMD_leftabove:
1793 case CMD_lfdo:
1794 case CMD_lockmarks:
1795 case CMD_noautocmd:
1796 case CMD_noswapfile:
1797 case CMD_rightbelow:
1798 case CMD_sandbox:
1799 case CMD_silent:
1800 case CMD_tab:
1801 case CMD_tabdo:
1802 case CMD_topleft:
1803 case CMD_verbose:
1804 case CMD_vertical:
1805 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001806 case CMD_vim9cmd:
1807 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001808 return arg;
1809
1810 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001811 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001812
1813#ifdef FEAT_SEARCH_EXTRA
1814 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001815 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001816#endif
1817
1818 // All completion for the +cmdline_compl feature goes here.
1819
1820 case CMD_command:
1821 return set_context_in_user_cmd(xp, arg);
1822
1823 case CMD_delcommand:
1824 xp->xp_context = EXPAND_USER_COMMANDS;
1825 xp->xp_pattern = arg;
1826 break;
1827
1828 case CMD_global:
1829 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001830 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001831 case CMD_and:
1832 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001833 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001834 case CMD_isearch:
1835 case CMD_dsearch:
1836 case CMD_ilist:
1837 case CMD_dlist:
1838 case CMD_ijump:
1839 case CMD_psearch:
1840 case CMD_djump:
1841 case CMD_isplit:
1842 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001843 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001844 case CMD_autocmd:
1845 return set_context_in_autocmd(xp, arg, FALSE);
1846 case CMD_doautocmd:
1847 case CMD_doautoall:
1848 return set_context_in_autocmd(xp, arg, TRUE);
1849 case CMD_set:
1850 set_context_in_set_cmd(xp, arg, 0);
1851 break;
1852 case CMD_setglobal:
1853 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1854 break;
1855 case CMD_setlocal:
1856 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1857 break;
1858 case CMD_tag:
1859 case CMD_stag:
1860 case CMD_ptag:
1861 case CMD_ltag:
1862 case CMD_tselect:
1863 case CMD_stselect:
1864 case CMD_ptselect:
1865 case CMD_tjump:
1866 case CMD_stjump:
1867 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001868 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001869 xp->xp_context = EXPAND_TAGS_LISTFILES;
1870 else
1871 xp->xp_context = EXPAND_TAGS;
1872 xp->xp_pattern = arg;
1873 break;
1874 case CMD_augroup:
1875 xp->xp_context = EXPAND_AUGROUP;
1876 xp->xp_pattern = arg;
1877 break;
1878#ifdef FEAT_SYN_HL
1879 case CMD_syntax:
1880 set_context_in_syntax_cmd(xp, arg);
1881 break;
1882#endif
1883#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001884 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001885 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001886 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001887 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001888 case CMD_if:
1889 case CMD_elseif:
1890 case CMD_while:
1891 case CMD_for:
1892 case CMD_echo:
1893 case CMD_echon:
1894 case CMD_execute:
1895 case CMD_echomsg:
1896 case CMD_echoerr:
1897 case CMD_call:
1898 case CMD_return:
1899 case CMD_cexpr:
1900 case CMD_caddexpr:
1901 case CMD_cgetexpr:
1902 case CMD_lexpr:
1903 case CMD_laddexpr:
1904 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001905 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001906 break;
1907
1908 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001909 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001910 case CMD_function:
1911 case CMD_delfunction:
1912 xp->xp_context = EXPAND_USER_FUNC;
1913 xp->xp_pattern = arg;
1914 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001915 case CMD_disassemble:
1916 set_context_in_disassemble_cmd(xp, arg);
1917 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001918
1919 case CMD_echohl:
1920 set_context_in_echohl_cmd(xp, arg);
1921 break;
1922#endif
1923 case CMD_highlight:
1924 set_context_in_highlight_cmd(xp, arg);
1925 break;
1926#ifdef FEAT_CSCOPE
1927 case CMD_cscope:
1928 case CMD_lcscope:
1929 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001930 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001931 break;
1932#endif
1933#ifdef FEAT_SIGNS
1934 case CMD_sign:
1935 set_context_in_sign_cmd(xp, arg);
1936 break;
1937#endif
1938 case CMD_bdelete:
1939 case CMD_bwipeout:
1940 case CMD_bunload:
1941 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1942 arg = xp->xp_pattern + 1;
1943 // FALLTHROUGH
1944 case CMD_buffer:
1945 case CMD_sbuffer:
1946 case CMD_checktime:
1947 xp->xp_context = EXPAND_BUFFERS;
1948 xp->xp_pattern = arg;
1949 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001950#ifdef FEAT_DIFF
1951 case CMD_diffget:
1952 case CMD_diffput:
1953 // If current buffer is in diff mode, complete buffer names
1954 // which are in diff mode, and different than current buffer.
1955 xp->xp_context = EXPAND_DIFF_BUFFERS;
1956 xp->xp_pattern = arg;
1957 break;
1958#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001959 case CMD_USER:
1960 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001961 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1962 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001963
1964 case CMD_map: case CMD_noremap:
1965 case CMD_nmap: case CMD_nnoremap:
1966 case CMD_vmap: case CMD_vnoremap:
1967 case CMD_omap: case CMD_onoremap:
1968 case CMD_imap: case CMD_inoremap:
1969 case CMD_cmap: case CMD_cnoremap:
1970 case CMD_lmap: case CMD_lnoremap:
1971 case CMD_smap: case CMD_snoremap:
1972 case CMD_tmap: case CMD_tnoremap:
1973 case CMD_xmap: case CMD_xnoremap:
1974 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001975 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001976 case CMD_unmap:
1977 case CMD_nunmap:
1978 case CMD_vunmap:
1979 case CMD_ounmap:
1980 case CMD_iunmap:
1981 case CMD_cunmap:
1982 case CMD_lunmap:
1983 case CMD_sunmap:
1984 case CMD_tunmap:
1985 case CMD_xunmap:
1986 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001987 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001988 case CMD_mapclear:
1989 case CMD_nmapclear:
1990 case CMD_vmapclear:
1991 case CMD_omapclear:
1992 case CMD_imapclear:
1993 case CMD_cmapclear:
1994 case CMD_lmapclear:
1995 case CMD_smapclear:
1996 case CMD_tmapclear:
1997 case CMD_xmapclear:
1998 xp->xp_context = EXPAND_MAPCLEAR;
1999 xp->xp_pattern = arg;
2000 break;
2001
2002 case CMD_abbreviate: case CMD_noreabbrev:
2003 case CMD_cabbrev: case CMD_cnoreabbrev:
2004 case CMD_iabbrev: case CMD_inoreabbrev:
2005 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002006 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002007 case CMD_unabbreviate:
2008 case CMD_cunabbrev:
2009 case CMD_iunabbrev:
2010 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002011 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002012#ifdef FEAT_MENU
2013 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2014 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2015 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2016 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2017 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2018 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2019 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2020 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2021 case CMD_tmenu: case CMD_tunmenu:
2022 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2023 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2024#endif
2025
2026 case CMD_colorscheme:
2027 xp->xp_context = EXPAND_COLORS;
2028 xp->xp_pattern = arg;
2029 break;
2030
2031 case CMD_compiler:
2032 xp->xp_context = EXPAND_COMPILER;
2033 xp->xp_pattern = arg;
2034 break;
2035
2036 case CMD_ownsyntax:
2037 xp->xp_context = EXPAND_OWNSYNTAX;
2038 xp->xp_pattern = arg;
2039 break;
2040
2041 case CMD_setfiletype:
2042 xp->xp_context = EXPAND_FILETYPE;
2043 xp->xp_pattern = arg;
2044 break;
2045
2046 case CMD_packadd:
2047 xp->xp_context = EXPAND_PACKADD;
2048 xp->xp_pattern = arg;
2049 break;
2050
2051#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2052 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002053 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002054#endif
2055#if defined(FEAT_PROFILE)
2056 case CMD_profile:
2057 set_context_in_profile_cmd(xp, arg);
2058 break;
2059#endif
2060 case CMD_behave:
2061 xp->xp_context = EXPAND_BEHAVE;
2062 xp->xp_pattern = arg;
2063 break;
2064
2065 case CMD_messages:
2066 xp->xp_context = EXPAND_MESSAGES;
2067 xp->xp_pattern = arg;
2068 break;
2069
2070 case CMD_history:
2071 xp->xp_context = EXPAND_HISTORY;
2072 xp->xp_pattern = arg;
2073 break;
2074#if defined(FEAT_PROFILE)
2075 case CMD_syntime:
2076 xp->xp_context = EXPAND_SYNTIME;
2077 xp->xp_pattern = arg;
2078 break;
2079#endif
2080
2081 case CMD_argdelete:
2082 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2083 arg = xp->xp_pattern + 1;
2084 xp->xp_context = EXPAND_ARGLIST;
2085 xp->xp_pattern = arg;
2086 break;
2087
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002088#ifdef FEAT_EVAL
2089 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002090 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002091 case CMD_breakdel:
2092 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002093
2094 case CMD_scriptnames:
2095 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002096#endif
2097
Bram Moolenaard0190392019-08-23 21:17:35 +02002098 default:
2099 break;
2100 }
2101 return NULL;
2102}
2103
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002104/*
2105 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2106 * we don't need/want deleted. Maybe this could be done better if we didn't
2107 * repeat all this stuff. The only problem is that they may not stay
2108 * perfectly compatible with each other, but then the command line syntax
2109 * probably won't change that much -- webb.
2110 */
2111 static char_u *
2112set_one_cmd_context(
2113 expand_T *xp,
2114 char_u *buff) // buffer for command string
2115{
2116 char_u *p;
2117 char_u *cmd, *arg;
2118 int len = 0;
2119 exarg_T ea;
2120 int compl = EXPAND_NOTHING;
2121 int forceit = FALSE;
2122 int usefilter = FALSE; // filter instead of file name
2123
2124 ExpandInit(xp);
2125 xp->xp_pattern = buff;
2126 xp->xp_line = buff;
2127 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2128 ea.argt = 0;
2129
2130 // 1. skip comment lines and leading space, colons or bars
2131 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2132 ;
2133 xp->xp_pattern = cmd;
2134
2135 if (*cmd == NUL)
2136 return NULL;
2137 if (*cmd == '"') // ignore comment lines
2138 {
2139 xp->xp_context = EXPAND_NOTHING;
2140 return NULL;
2141 }
2142
2143 // 3. Skip over the range to find the command.
2144 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2145 xp->xp_pattern = cmd;
2146 if (*cmd == NUL)
2147 return NULL;
2148 if (*cmd == '"')
2149 {
2150 xp->xp_context = EXPAND_NOTHING;
2151 return NULL;
2152 }
2153
2154 if (*cmd == '|' || *cmd == '\n')
2155 return cmd + 1; // There's another command
2156
2157 // Get the command index.
2158 p = set_cmd_index(cmd, &ea, xp, &compl);
2159 if (p == NULL)
2160 return NULL;
2161
2162 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2163
2164 if (*p == '!') // forced commands
2165 {
2166 forceit = TRUE;
2167 ++p;
2168 }
2169
2170 // 6. parse arguments
2171 if (!IS_USER_CMDIDX(ea.cmdidx))
2172 ea.argt = excmd_get_argt(ea.cmdidx);
2173
2174 arg = skipwhite(p);
2175
2176 // Skip over ++argopt argument
2177 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2178 {
2179 p = arg;
2180 while (*p && !vim_isspace(*p))
2181 MB_PTR_ADV(p);
2182 arg = skipwhite(p);
2183 }
2184
2185 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2186 {
2187 if (*arg == '>') // append
2188 {
2189 if (*++arg == '>')
2190 ++arg;
2191 arg = skipwhite(arg);
2192 }
2193 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2194 {
2195 ++arg;
2196 usefilter = TRUE;
2197 }
2198 }
2199
2200 if (ea.cmdidx == CMD_read)
2201 {
2202 usefilter = forceit; // :r! filter if forced
2203 if (*arg == '!') // :r !filter
2204 {
2205 ++arg;
2206 usefilter = TRUE;
2207 }
2208 }
2209
2210 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2211 {
2212 while (*arg == *cmd) // allow any number of '>' or '<'
2213 ++arg;
2214 arg = skipwhite(arg);
2215 }
2216
2217 // Does command allow "+command"?
2218 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2219 {
2220 // Check if we're in the +command
2221 p = arg + 1;
2222 arg = skip_cmd_arg(arg, FALSE);
2223
2224 // Still touching the command after '+'?
2225 if (*arg == NUL)
2226 return p;
2227
2228 // Skip space(s) after +command to get to the real argument
2229 arg = skipwhite(arg);
2230 }
2231
2232
2233 // Check for '|' to separate commands and '"' to start comments.
2234 // Don't do this for ":read !cmd" and ":write !cmd".
2235 if ((ea.argt & EX_TRLBAR) && !usefilter)
2236 {
2237 p = arg;
2238 // ":redir @" is not the start of a comment
2239 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2240 p += 2;
2241 while (*p)
2242 {
2243 if (*p == Ctrl_V)
2244 {
2245 if (p[1] != NUL)
2246 ++p;
2247 }
2248 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2249 || *p == '|' || *p == '\n')
2250 {
2251 if (*(p - 1) != '\\')
2252 {
2253 if (*p == '|' || *p == '\n')
2254 return p + 1;
2255 return NULL; // It's a comment
2256 }
2257 }
2258 MB_PTR_ADV(p);
2259 }
2260 }
2261
2262 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2263 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2264 // no arguments allowed but there is something
2265 return NULL;
2266
2267 // Find start of last argument (argument just before cursor):
2268 p = buff;
2269 xp->xp_pattern = p;
2270 len = (int)STRLEN(buff);
2271 while (*p && p < buff + len)
2272 {
2273 if (*p == ' ' || *p == TAB)
2274 {
2275 // argument starts after a space
2276 xp->xp_pattern = ++p;
2277 }
2278 else
2279 {
2280 if (*p == '\\' && *(p + 1) != NUL)
2281 ++p; // skip over escaped character
2282 MB_PTR_ADV(p);
2283 }
2284 }
2285
2286 if (ea.argt & EX_XFILE)
2287 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2288
2289 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002290 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002291 forceit);
2292}
2293
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002294/*
2295 * Set the completion context in 'xp' for command 'str'
2296 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002297 void
2298set_cmd_context(
2299 expand_T *xp,
2300 char_u *str, // start of command line
2301 int len, // length of command line (excl. NUL)
2302 int col, // position of cursor
2303 int use_ccline UNUSED) // use ccline for info
2304{
2305#ifdef FEAT_EVAL
2306 cmdline_info_T *ccline = get_cmdline_info();
2307#endif
2308 int old_char = NUL;
2309 char_u *nextcomm;
2310
2311 // Avoid a UMR warning from Purify, only save the character if it has been
2312 // written before.
2313 if (col < len)
2314 old_char = str[col];
2315 str[col] = NUL;
2316 nextcomm = str;
2317
2318#ifdef FEAT_EVAL
2319 if (use_ccline && ccline->cmdfirstc == '=')
2320 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002321 // pass CMD_SIZE because there is no real command
2322 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002323 }
2324 else if (use_ccline && ccline->input_fn)
2325 {
2326 xp->xp_context = ccline->xp_context;
2327 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002328 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002329 }
2330 else
2331#endif
2332 while (nextcomm != NULL)
2333 nextcomm = set_one_cmd_context(xp, nextcomm);
2334
2335 // Store the string here so that call_user_expand_func() can get to them
2336 // easily.
2337 xp->xp_line = str;
2338 xp->xp_col = col;
2339
2340 str[col] = old_char;
2341}
2342
2343/*
2344 * Expand the command line "str" from context "xp".
2345 * "xp" must have been set by set_cmd_context().
2346 * xp->xp_pattern points into "str", to where the text that is to be expanded
2347 * starts.
2348 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2349 * cursor.
2350 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2351 * key that triggered expansion literally.
2352 * Returns EXPAND_OK otherwise.
2353 */
2354 int
2355expand_cmdline(
2356 expand_T *xp,
2357 char_u *str, // start of command line
2358 int col, // position of cursor
2359 int *matchcount, // return: nr of matches
2360 char_u ***matches) // return: array of pointers to matches
2361{
2362 char_u *file_str = NULL;
2363 int options = WILD_ADD_SLASH|WILD_SILENT;
2364
2365 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2366 {
2367 beep_flush();
2368 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2369 }
2370 if (xp->xp_context == EXPAND_NOTHING)
2371 {
2372 // Caller can use the character as a normal char instead
2373 return EXPAND_NOTHING;
2374 }
2375
2376 // add star to file name, or convert to regexp if not exp. files.
2377 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002378 if (cmdline_fuzzy_completion_supported(xp))
2379 // If fuzzy matching, don't modify the search string
2380 file_str = vim_strsave(xp->xp_pattern);
2381 else
2382 {
2383 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2384 if (file_str == NULL)
2385 return EXPAND_UNSUCCESSFUL;
2386 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002387
2388 if (p_wic)
2389 options += WILD_ICASE;
2390
2391 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002392 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002393 {
2394 *matchcount = 0;
2395 *matches = NULL;
2396 }
2397 vim_free(file_str);
2398
2399 return EXPAND_OK;
2400}
2401
Bram Moolenaar66b51422019-08-18 21:44:12 +02002402/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002403 * Expand file or directory names.
2404 */
2405 static int
2406expand_files_and_dirs(
2407 expand_T *xp,
2408 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002409 char_u ***matches,
2410 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002411 int flags,
2412 int options)
2413{
2414 int free_pat = FALSE;
2415 int i;
2416 int ret;
2417
2418 // for ":set path=" and ":set tags=" halve backslashes for escaped
2419 // space
2420 if (xp->xp_backslash != XP_BS_NONE)
2421 {
2422 free_pat = TRUE;
2423 pat = vim_strsave(pat);
2424 for (i = 0; pat[i]; ++i)
2425 if (pat[i] == '\\')
2426 {
2427 if (xp->xp_backslash == XP_BS_THREE
2428 && pat[i + 1] == '\\'
2429 && pat[i + 2] == '\\'
2430 && pat[i + 3] == ' ')
2431 STRMOVE(pat + i, pat + i + 3);
2432 if (xp->xp_backslash == XP_BS_ONE
2433 && pat[i + 1] == ' ')
2434 STRMOVE(pat + i, pat + i + 1);
2435 }
2436 }
2437
2438 if (xp->xp_context == EXPAND_FILES)
2439 flags |= EW_FILE;
2440 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2441 flags |= (EW_FILE | EW_PATH);
2442 else
2443 flags = (flags | EW_DIR) & ~EW_FILE;
2444 if (options & WILD_ICASE)
2445 flags |= EW_ICASE;
2446
2447 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002448 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002449 if (free_pat)
2450 vim_free(pat);
2451#ifdef BACKSLASH_IN_FILENAME
2452 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2453 {
2454 int j;
2455
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002456 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002457 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002458 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002459
2460 while (*ptr != NUL)
2461 {
2462 if (p_csl[0] == 's' && *ptr == '\\')
2463 *ptr = '/';
2464 else if (p_csl[0] == 'b' && *ptr == '/')
2465 *ptr = '\\';
2466 ptr += (*mb_ptr2len)(ptr);
2467 }
2468 }
2469 }
2470#endif
2471 return ret;
2472}
2473
2474/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002475 * Function given to ExpandGeneric() to obtain the possible arguments of the
2476 * ":behave {mswin,xterm}" command.
2477 */
2478 static char_u *
2479get_behave_arg(expand_T *xp UNUSED, int idx)
2480{
2481 if (idx == 0)
2482 return (char_u *)"mswin";
2483 if (idx == 1)
2484 return (char_u *)"xterm";
2485 return NULL;
2486}
2487
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002488# ifdef FEAT_EVAL
2489/*
2490 * Function given to ExpandGeneric() to obtain the possible arguments of the
2491 * ":breakadd {expr, file, func, here}" command.
2492 * ":breakdel {func, file, here}" command.
2493 */
2494 static char_u *
2495get_breakadd_arg(expand_T *xp UNUSED, int idx)
2496{
2497 char *opts[] = {"expr", "file", "func", "here"};
2498
2499 if (idx >=0 && idx <= 3)
2500 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002501 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002502 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2503 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002504 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2505 {
2506 // breakdel {func, file, here}
2507 if (idx <= 2)
2508 return (char_u *)opts[idx + 1];
2509 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002510 else
2511 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002512 // profdel {func, file}
2513 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002514 return (char_u *)opts[idx + 1];
2515 }
2516 }
2517 return NULL;
2518}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002519
2520/*
2521 * Function given to ExpandGeneric() to obtain the possible arguments for the
2522 * ":scriptnames" command.
2523 */
2524 static char_u *
2525get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2526{
2527 scriptitem_T *si;
2528
2529 if (!SCRIPT_ID_VALID(idx + 1))
2530 return NULL;
2531
2532 si = SCRIPT_ITEM(idx + 1);
2533 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2534 return NameBuff;
2535}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002536#endif
2537
Bram Moolenaard0190392019-08-23 21:17:35 +02002538/*
2539 * Function given to ExpandGeneric() to obtain the possible arguments of the
2540 * ":messages {clear}" command.
2541 */
2542 static char_u *
2543get_messages_arg(expand_T *xp UNUSED, int idx)
2544{
2545 if (idx == 0)
2546 return (char_u *)"clear";
2547 return NULL;
2548}
2549
2550 static char_u *
2551get_mapclear_arg(expand_T *xp UNUSED, int idx)
2552{
2553 if (idx == 0)
2554 return (char_u *)"<buffer>";
2555 return NULL;
2556}
2557
2558/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002559 * Do the expansion based on xp->xp_context and 'rmp'.
2560 */
2561 static int
2562ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002563 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002564 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002565 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002566 char_u ***matches,
2567 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002568{
2569 static struct expgen
2570 {
2571 int context;
2572 char_u *((*func)(expand_T *, int));
2573 int ic;
2574 int escaped;
2575 } tab[] =
2576 {
2577 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2578 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2579 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2580 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2581 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2582 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2583 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2584 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2585 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2586 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002587#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002588 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2589 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2590 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2591 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2592 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002593#endif
2594#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002595 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2596 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002597#endif
2598#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002599 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002600#endif
2601#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002602 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002603#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002604 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2605 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2606 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002607#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002608 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002609#endif
2610#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002611 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002612#endif
2613#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002614 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002615#endif
2616#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002617 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2618 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002619#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002620 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2621 {EXPAND_USER, get_users, TRUE, FALSE},
2622 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002623#ifdef FEAT_EVAL
2624 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002625 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002626#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002627 };
2628 int i;
2629 int ret = FAIL;
2630
2631 // Find a context in the table and call the ExpandGeneric() with the
2632 // right function to do the expansion.
2633 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2634 {
2635 if (xp->xp_context == tab[i].context)
2636 {
2637 if (tab[i].ic)
2638 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002639 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2640 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002641 break;
2642 }
2643 }
2644
2645 return ret;
2646}
2647
2648/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002649 * Map wild expand options to flags for expand_wildcards()
2650 */
2651 static int
2652map_wildopts_to_ewflags(int options)
2653{
2654 int flags;
2655
2656 flags = EW_DIR; // include directories
2657 if (options & WILD_LIST_NOTFOUND)
2658 flags |= EW_NOTFOUND;
2659 if (options & WILD_ADD_SLASH)
2660 flags |= EW_ADDSLASH;
2661 if (options & WILD_KEEP_ALL)
2662 flags |= EW_KEEPALL;
2663 if (options & WILD_SILENT)
2664 flags |= EW_SILENT;
2665 if (options & WILD_NOERROR)
2666 flags |= EW_NOERROR;
2667 if (options & WILD_ALLLINKS)
2668 flags |= EW_ALLLINKS;
2669
2670 return flags;
2671}
2672
2673/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002674 * Do the expansion based on xp->xp_context and "pat".
2675 */
2676 static int
2677ExpandFromContext(
2678 expand_T *xp,
2679 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002680 char_u ***matches,
2681 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002682 int options) // WILD_ flags
2683{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002684 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002685 int ret;
2686 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002687 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002688 int fuzzy = cmdline_fuzzy_complete(pat)
2689 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002690
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002691 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002692
2693 if (xp->xp_context == EXPAND_FILES
2694 || xp->xp_context == EXPAND_DIRECTORIES
2695 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002696 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2697 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002698
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002699 *matches = (char_u **)"";
2700 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002701 if (xp->xp_context == EXPAND_HELP)
2702 {
2703 // With an empty argument we would get all the help tags, which is
2704 // very slow. Get matches for "help" instead.
2705 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002706 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002707 {
2708#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002709 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002710#endif
2711 return OK;
2712 }
2713 return FAIL;
2714 }
2715
Bram Moolenaar66b51422019-08-18 21:44:12 +02002716 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002717 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002718 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002719 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002720 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002721 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002722#ifdef FEAT_DIFF
2723 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002724 return ExpandBufnames(pat, numMatches, matches,
2725 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002726#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002727 if (xp->xp_context == EXPAND_TAGS
2728 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002729 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2730 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002731 if (xp->xp_context == EXPAND_COLORS)
2732 {
2733 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002734 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002735 directories);
2736 }
2737 if (xp->xp_context == EXPAND_COMPILER)
2738 {
2739 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002740 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002741 }
2742 if (xp->xp_context == EXPAND_OWNSYNTAX)
2743 {
2744 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002745 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002746 }
2747 if (xp->xp_context == EXPAND_FILETYPE)
2748 {
2749 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002750 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002751 }
2752# if defined(FEAT_EVAL)
2753 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002754 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002755# endif
2756 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002757 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002758
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002759 // When expanding a function name starting with s:, match the <SNR>nr_
2760 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002761 if ((xp->xp_context == EXPAND_USER_FUNC
2762 || xp->xp_context == EXPAND_DISASSEMBLE)
2763 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002764 {
2765 int len = (int)STRLEN(pat) + 20;
2766
2767 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002768 if (tofree == NULL)
2769 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002770 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002771 pat = tofree;
2772 }
2773
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002774 if (!fuzzy)
2775 {
2776 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2777 if (regmatch.regprog == NULL)
2778 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002779
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002780 // set ignore-case according to p_ic, p_scs and pat
2781 regmatch.rm_ic = ignorecase(pat);
2782 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002783
2784 if (xp->xp_context == EXPAND_SETTINGS
2785 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002786 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002787 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002788 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002789# if defined(FEAT_EVAL)
2790 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002791 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002792# endif
2793 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002794 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002795
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002796 if (!fuzzy)
2797 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002798 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002799
2800 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002801}
2802
Bram Moolenaar66b51422019-08-18 21:44:12 +02002803/*
2804 * Expand a list of names.
2805 *
2806 * Generic function for command line completion. It calls a function to
2807 * obtain strings, one by one. The strings are matched against a regexp
2808 * program. Matching strings are copied into an array, which is returned.
2809 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002810 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2811 * is used.
2812 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002813 * Returns OK when no problems encountered, FAIL for error (out of memory).
2814 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002815 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002816ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002817 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002818 expand_T *xp,
2819 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002820 char_u ***matches,
2821 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002822 char_u *((*func)(expand_T *, int)),
2823 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002824 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002825{
2826 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002827 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002828 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002829 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002830 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002831 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002832 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002833 int sort_matches = FALSE;
2834 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002835
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002836 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002837 *matches = NULL;
2838 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002839
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002840 if (!fuzzy)
2841 ga_init2(&ga, sizeof(char *), 30);
2842 else
2843 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2844
2845 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002846 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002847 str = (*func)(xp, i);
2848 if (str == NULL) // end of list
2849 break;
2850 if (*str == NUL) // skip empty strings
2851 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002852
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002853 if (xp->xp_pattern[0] != NUL)
2854 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002855 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002856 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002857 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002858 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002859 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002860 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002861 }
2862 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002863 else
2864 match = TRUE;
2865
2866 if (!match)
2867 continue;
2868
2869 if (escaped)
2870 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2871 else
2872 str = vim_strsave(str);
2873 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002874 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002875 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002876 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002877 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002878 return FAIL;
2879 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002880
2881 for (i = 0; i < ga.ga_len; ++i)
2882 {
2883 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[i];
2884 vim_free(fuzmatch->str);
2885 }
2886 ga_clear(&ga);
2887 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002888 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002889
2890 if (ga_grow(&ga, 1) == FAIL)
2891 {
2892 vim_free(str);
2893 break;
2894 }
2895
2896 if (fuzzy)
2897 {
2898 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2899 fuzmatch->idx = ga.ga_len;
2900 fuzmatch->str = str;
2901 fuzmatch->score = score;
2902 }
2903 else
2904 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2905
2906# ifdef FEAT_MENU
2907 if (func == get_menu_names)
2908 {
2909 // test for separator added by get_menu_names()
2910 str += STRLEN(str) - 1;
2911 if (*str == '\001')
2912 *str = '.';
2913 }
2914# endif
2915
2916 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002917 }
2918
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002919 if (ga.ga_len == 0)
2920 return OK;
2921
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002922 // sort the matches when using regular expression matching and sorting
2923 // applies to the completion context. Menus and scriptnames should be kept
2924 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002925 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002926 && xp->xp_context != EXPAND_MENUS
2927 && xp->xp_context != EXPAND_SCRIPTNAMES)
2928 sort_matches = TRUE;
2929
2930 // <SNR> functions should be sorted to the end.
2931 if (xp->xp_context == EXPAND_EXPRESSION
2932 || xp->xp_context == EXPAND_FUNCTIONS
2933 || xp->xp_context == EXPAND_USER_FUNC
2934 || xp->xp_context == EXPAND_DISASSEMBLE)
2935 funcsort = TRUE;
2936
2937 // Sort the matches.
2938 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002939 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002940 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002941 // <SNR> functions should be sorted to the end.
2942 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2943 sort_func_compare);
2944 else
2945 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2946 }
2947
2948 if (!fuzzy)
2949 {
2950 *matches = ga.ga_data;
2951 *numMatches = ga.ga_len;
2952 }
2953 else
2954 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002955 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2956 funcsort) == FAIL)
2957 return FAIL;
2958 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002959 }
2960
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002961#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002962 // Reset the variables used for special highlight names expansion, so that
2963 // they don't show up when getting normal highlight names by ID.
2964 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002965#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002966
Bram Moolenaar66b51422019-08-18 21:44:12 +02002967 return OK;
2968}
2969
2970/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002971 * Expand shell command matches in one directory of $PATH.
2972 */
2973 static void
2974expand_shellcmd_onedir(
2975 char_u *buf,
2976 char_u *s,
2977 size_t l,
2978 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002979 char_u ***matches,
2980 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002981 int flags,
2982 hashtab_T *ht,
2983 garray_T *gap)
2984{
2985 int ret;
2986 int i;
2987 hash_T hash;
2988 hashitem_T *hi;
2989
2990 vim_strncpy(buf, s, l);
2991 add_pathsep(buf);
2992 l = STRLEN(buf);
2993 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2994
2995 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002996 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002997 if (ret == OK)
2998 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002999 if (ga_grow(gap, *numMatches) == FAIL)
3000 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003001 else
3002 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003003 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003004 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003005 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003006
3007 if (STRLEN(name) > l)
3008 {
3009 // Check if this name was already found.
3010 hash = hash_hash(name + l);
3011 hi = hash_lookup(ht, name + l, hash);
3012 if (HASHITEM_EMPTY(hi))
3013 {
3014 // Remove the path that was prepended.
3015 STRMOVE(name, name + l);
3016 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3017 hash_add_item(ht, hi, name, hash);
3018 name = NULL;
3019 }
3020 }
3021 vim_free(name);
3022 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003023 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003024 }
3025 }
3026}
3027
3028/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003029 * Complete a shell command.
3030 * Returns FAIL or OK;
3031 */
3032 static int
3033expand_shellcmd(
3034 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003035 char_u ***matches, // return: array with matches
3036 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003037 int flagsarg) // EW_ flags
3038{
3039 char_u *pat;
3040 int i;
3041 char_u *path = NULL;
3042 int mustfree = FALSE;
3043 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003044 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003045 size_t l;
3046 char_u *s, *e;
3047 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003048 int did_curdir = FALSE;
3049 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003050
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003051 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003052 if (buf == NULL)
3053 return FAIL;
3054
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003055 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003056 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003057 if (pat == NULL)
3058 {
3059 vim_free(buf);
3060 return FAIL;
3061 }
3062
Bram Moolenaar66b51422019-08-18 21:44:12 +02003063 for (i = 0; pat[i]; ++i)
3064 if (pat[i] == '\\' && pat[i + 1] == ' ')
3065 STRMOVE(pat + i, pat + i + 1);
3066
3067 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3068
3069 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3070 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3071 path = (char_u *)".";
3072 else
3073 {
3074 // For an absolute name we don't use $PATH.
3075 if (!mch_isFullName(pat))
3076 path = vim_getenv((char_u *)"PATH", &mustfree);
3077 if (path == NULL)
3078 path = (char_u *)"";
3079 }
3080
3081 // Go over all directories in $PATH. Expand matches in that directory and
3082 // collect them in "ga". When "." is not in $PATH also expand for the
3083 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003084 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003085 hash_init(&found_ht);
3086 for (s = path; ; s = e)
3087 {
3088# if defined(MSWIN)
3089 e = vim_strchr(s, ';');
3090# else
3091 e = vim_strchr(s, ':');
3092# endif
3093 if (e == NULL)
3094 e = s + STRLEN(s);
3095
3096 if (*s == NUL)
3097 {
3098 if (did_curdir)
3099 break;
3100 // Find directories in the current directory, path is empty.
3101 did_curdir = TRUE;
3102 flags |= EW_DIR;
3103 }
3104 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3105 {
3106 did_curdir = TRUE;
3107 flags |= EW_DIR;
3108 }
3109 else
3110 // Do not match directories inside a $PATH item.
3111 flags &= ~EW_DIR;
3112
3113 l = e - s;
3114 if (l > MAXPATHL - 5)
3115 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003116
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003117 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003118 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003119
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120 if (*e != NUL)
3121 ++e;
3122 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003123 *matches = ga.ga_data;
3124 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125
3126 vim_free(buf);
3127 vim_free(pat);
3128 if (mustfree)
3129 vim_free(path);
3130 hash_clear(&found_ht);
3131 return OK;
3132}
3133
3134# if defined(FEAT_EVAL)
3135/*
3136 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003137 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138 */
3139 static void *
3140call_user_expand_func(
3141 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003142 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143{
3144 cmdline_info_T *ccline = get_cmdline_info();
3145 int keep = 0;
3146 typval_T args[4];
3147 sctx_T save_current_sctx = current_sctx;
3148 char_u *pat = NULL;
3149 void *ret;
3150
3151 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3152 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003153
3154 if (ccline->cmdbuff != NULL)
3155 {
3156 keep = ccline->cmdbuff[ccline->cmdlen];
3157 ccline->cmdbuff[ccline->cmdlen] = 0;
3158 }
3159
3160 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3161
3162 args[0].v_type = VAR_STRING;
3163 args[0].vval.v_string = pat;
3164 args[1].v_type = VAR_STRING;
3165 args[1].vval.v_string = xp->xp_line;
3166 args[2].v_type = VAR_NUMBER;
3167 args[2].vval.v_number = xp->xp_col;
3168 args[3].v_type = VAR_UNKNOWN;
3169
3170 current_sctx = xp->xp_script_ctx;
3171
3172 ret = user_expand_func(xp->xp_arg, 3, args);
3173
3174 current_sctx = save_current_sctx;
3175 if (ccline->cmdbuff != NULL)
3176 ccline->cmdbuff[ccline->cmdlen] = keep;
3177
3178 vim_free(pat);
3179 return ret;
3180}
3181
3182/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003183 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3184 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003185 */
3186 static int
3187ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003188 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003189 expand_T *xp,
3190 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003191 char_u ***matches,
3192 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193{
3194 char_u *retstr;
3195 char_u *s;
3196 char_u *e;
3197 int keep;
3198 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003199 int fuzzy;
3200 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003201 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003202
3203 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003204 *matches = NULL;
3205 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003206
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003207 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003208 if (retstr == NULL)
3209 return FAIL;
3210
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003211 if (!fuzzy)
3212 ga_init2(&ga, sizeof(char *), 3);
3213 else
3214 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3215
Bram Moolenaar66b51422019-08-18 21:44:12 +02003216 for (s = retstr; *s != NUL; s = e)
3217 {
3218 e = vim_strchr(s, '\n');
3219 if (e == NULL)
3220 e = s + STRLEN(s);
3221 keep = *e;
3222 *e = NUL;
3223
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003224 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003225 {
3226 if (!fuzzy)
3227 match = vim_regexec(regmatch, s, (colnr_T)0);
3228 else
3229 {
3230 score = fuzzy_match_str(s, pat);
3231 match = (score != 0);
3232 }
3233 }
3234 else
3235 match = TRUE; // match everything
3236
Bram Moolenaar66b51422019-08-18 21:44:12 +02003237 *e = keep;
3238
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003239 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003240 {
3241 if (ga_grow(&ga, 1) == FAIL)
3242 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003243 if (!fuzzy)
3244 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3245 else
3246 {
3247 fuzmatch_str_T *fuzmatch =
3248 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003249 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003250 fuzmatch->str = vim_strnsave(s, e - s);
3251 fuzmatch->score = score;
3252 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253 ++ga.ga_len;
3254 }
3255
3256 if (*e != NUL)
3257 ++e;
3258 }
3259 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003260
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003261 if (ga.ga_len == 0)
3262 return OK;
3263
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003264 if (!fuzzy)
3265 {
3266 *matches = ga.ga_data;
3267 *numMatches = ga.ga_len;
3268 }
3269 else
3270 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003271 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3272 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003273 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003274 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003275 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003276 return OK;
3277}
3278
3279/*
3280 * Expand names with a list returned by a function defined by the user.
3281 */
3282 static int
3283ExpandUserList(
3284 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003285 char_u ***matches,
3286 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003287{
3288 list_T *retlist;
3289 listitem_T *li;
3290 garray_T ga;
3291
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003292 *matches = NULL;
3293 *numMatches = 0;
3294 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003295 if (retlist == NULL)
3296 return FAIL;
3297
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003298 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003299 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003300 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003301 {
3302 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3303 continue; // Skip non-string items and empty strings
3304
3305 if (ga_grow(&ga, 1) == FAIL)
3306 break;
3307
3308 ((char_u **)ga.ga_data)[ga.ga_len] =
3309 vim_strsave(li->li_tv.vval.v_string);
3310 ++ga.ga_len;
3311 }
3312 list_unref(retlist);
3313
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003314 *matches = ga.ga_data;
3315 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003316 return OK;
3317}
3318# endif
3319
3320/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003321 * Expand "file" for all comma-separated directories in "path".
3322 * Adds the matches to "ga". Caller must init "ga".
3323 */
3324 void
3325globpath(
3326 char_u *path,
3327 char_u *file,
3328 garray_T *ga,
3329 int expand_options)
3330{
3331 expand_T xpc;
3332 char_u *buf;
3333 int i;
3334 int num_p;
3335 char_u **p;
3336
3337 buf = alloc(MAXPATHL);
3338 if (buf == NULL)
3339 return;
3340
3341 ExpandInit(&xpc);
3342 xpc.xp_context = EXPAND_FILES;
3343
3344 // Loop over all entries in {path}.
3345 while (*path != NUL)
3346 {
3347 // Copy one item of the path to buf[] and concatenate the file name.
3348 copy_option_part(&path, buf, MAXPATHL, ",");
3349 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3350 {
3351# if defined(MSWIN)
3352 // Using the platform's path separator (\) makes vim incorrectly
3353 // treat it as an escape character, use '/' instead.
3354 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3355 STRCAT(buf, "/");
3356# else
3357 add_pathsep(buf);
3358# endif
3359 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003360 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3362 {
3363 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3364
3365 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003366 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003367 for (i = 0; i < num_p; ++i)
3368 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003369 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003370 ++ga->ga_len;
3371 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003372 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003373 }
3374 }
3375 }
3376
3377 vim_free(buf);
3378}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003379
Bram Moolenaareadee482020-09-04 15:37:31 +02003380#ifdef FEAT_WILDMENU
3381
3382/*
3383 * Translate some keys pressed when 'wildmenu' is used.
3384 */
3385 int
3386wildmenu_translate_key(
3387 cmdline_info_T *cclp,
3388 int key,
3389 expand_T *xp,
3390 int did_wild_list)
3391{
3392 int c = key;
3393
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003394#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003395 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003396 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003397 // When the popup menu is used for cmdline completion:
3398 // Up : go to the previous item in the menu
3399 // Down : go to the next item in the menu
3400 // Left : go to the parent directory
3401 // Right: list the files in the selected directory
3402 switch (c)
3403 {
3404 case K_UP: c = K_LEFT; break;
3405 case K_DOWN: c = K_RIGHT; break;
3406 case K_LEFT: c = K_UP; break;
3407 case K_RIGHT: c = K_DOWN; break;
3408 default: break;
3409 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003410 }
3411#endif
3412
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003413 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003414 {
3415 if (c == K_LEFT)
3416 c = Ctrl_P;
3417 else if (c == K_RIGHT)
3418 c = Ctrl_N;
3419 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003420
Bram Moolenaareadee482020-09-04 15:37:31 +02003421 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003422 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003423 && cclp->cmdpos > 1
3424 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3425 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3426 && (c == '\n' || c == '\r' || c == K_KENTER))
3427 c = K_DOWN;
3428
3429 return c;
3430}
3431
3432/*
3433 * Delete characters on the command line, from "from" to the current
3434 * position.
3435 */
3436 static void
3437cmdline_del(cmdline_info_T *cclp, int from)
3438{
3439 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3440 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3441 cclp->cmdlen -= cclp->cmdpos - from;
3442 cclp->cmdpos = from;
3443}
3444
3445/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003446 * Handle a key pressed when the wild menu for the menu names
3447 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003448 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003449 static int
3450wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003451{
Bram Moolenaareadee482020-09-04 15:37:31 +02003452 int i;
3453 int j;
3454
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003455 // Hitting <Down> after "emenu Name.": complete submenu
3456 if (key == K_DOWN && cclp->cmdpos > 0
3457 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003458 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003459 key = p_wc;
3460 KeyTyped = TRUE; // in case the key was mapped
3461 }
3462 else if (key == K_UP)
3463 {
3464 // Hitting <Up>: Remove one submenu name in front of the
3465 // cursor
3466 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003467
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003468 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3469 i = 0;
3470 while (--j > 0)
3471 {
3472 // check for start of menu name
3473 if (cclp->cmdbuff[j] == ' '
3474 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003475 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003476 i = j + 1;
3477 break;
3478 }
3479 // check for start of submenu name
3480 if (cclp->cmdbuff[j] == '.'
3481 && cclp->cmdbuff[j - 1] != '\\')
3482 {
3483 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003484 {
3485 i = j + 1;
3486 break;
3487 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003488 else
3489 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003490 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003491 }
3492 if (i > 0)
3493 cmdline_del(cclp, i);
3494 key = p_wc;
3495 KeyTyped = TRUE; // in case the key was mapped
3496 xp->xp_context = EXPAND_NOTHING;
3497 }
3498
3499 return key;
3500}
3501
3502/*
3503 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3504 * directory names (EXPAND_DIRECTORIES) or shell command names
3505 * (EXPAND_SHELLCMD) is displayed.
3506 */
3507 static int
3508wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3509{
3510 int i;
3511 int j;
3512 char_u upseg[5];
3513
3514 upseg[0] = PATHSEP;
3515 upseg[1] = '.';
3516 upseg[2] = '.';
3517 upseg[3] = PATHSEP;
3518 upseg[4] = NUL;
3519
3520 if (key == K_DOWN
3521 && cclp->cmdpos > 0
3522 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3523 && (cclp->cmdpos < 3
3524 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3525 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3526 {
3527 // go down a directory
3528 key = p_wc;
3529 KeyTyped = TRUE; // in case the key was mapped
3530 }
3531 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3532 {
3533 // If in a direct ancestor, strip off one ../ to go down
3534 int found = FALSE;
3535
3536 j = cclp->cmdpos;
3537 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3538 while (--j > i)
3539 {
3540 if (has_mbyte)
3541 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3542 if (vim_ispathsep(cclp->cmdbuff[j]))
3543 {
3544 found = TRUE;
3545 break;
3546 }
3547 }
3548 if (found
3549 && cclp->cmdbuff[j - 1] == '.'
3550 && cclp->cmdbuff[j - 2] == '.'
3551 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3552 {
3553 cmdline_del(cclp, j - 2);
3554 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003555 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003556 }
3557 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003558 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003559 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003560 // go up a directory
3561 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003562
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003563 j = cclp->cmdpos - 1;
3564 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3565 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003566 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003567 if (has_mbyte)
3568 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3569 if (vim_ispathsep(cclp->cmdbuff[j])
3570# ifdef BACKSLASH_IN_FILENAME
3571 && vim_strchr((char_u *)" *?[{`$%#",
3572 cclp->cmdbuff[j + 1]) == NULL
3573# endif
3574 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003575 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003576 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003577 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003578 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003579 break;
3580 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003581 else
3582 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003583 }
3584 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003585
3586 if (!found)
3587 j = i;
3588 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3589 j += 4;
3590 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3591 && j == i)
3592 j += 3;
3593 else
3594 j = 0;
3595 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003596 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003597 // TODO this is only for DOS/UNIX systems - need to put in
3598 // machine-specific stuff here and in upseg init
3599 cmdline_del(cclp, j);
3600 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003601 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003602 else if (cclp->cmdpos > i)
3603 cmdline_del(cclp, i);
3604
3605 // Now complete in the new directory. Set KeyTyped in case the
3606 // Up key came from a mapping.
3607 key = p_wc;
3608 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003609 }
3610
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003611 return key;
3612}
3613
3614/*
3615 * Handle a key pressed when the wild menu is displayed
3616 */
3617 int
3618wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3619{
3620 if (xp->xp_context == EXPAND_MENUNAMES)
3621 return wildmenu_process_key_menunames(cclp, key, xp);
3622 else if ((xp->xp_context == EXPAND_FILES
3623 || xp->xp_context == EXPAND_DIRECTORIES
3624 || xp->xp_context == EXPAND_SHELLCMD))
3625 return wildmenu_process_key_filenames(cclp, key, xp);
3626
3627 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003628}
3629
3630/*
3631 * Free expanded names when finished walking through the matches
3632 */
3633 void
3634wildmenu_cleanup(cmdline_info_T *cclp)
3635{
3636 int skt = KeyTyped;
3637 int old_RedrawingDisabled = RedrawingDisabled;
3638
3639 if (!p_wmnu || wild_menu_showing == 0)
3640 return;
3641
3642 if (cclp->input_fn)
3643 RedrawingDisabled = 0;
3644
3645 if (wild_menu_showing == WM_SCROLLED)
3646 {
3647 // Entered command line, move it up
3648 cmdline_row--;
3649 redrawcmd();
3650 }
3651 else if (save_p_ls != -1)
3652 {
3653 // restore 'laststatus' and 'winminheight'
3654 p_ls = save_p_ls;
3655 p_wmh = save_p_wmh;
3656 last_status(FALSE);
3657 update_screen(VALID); // redraw the screen NOW
3658 redrawcmd();
3659 save_p_ls = -1;
3660 }
3661 else
3662 {
3663 win_redraw_last_status(topframe);
3664 redraw_statuslines();
3665 }
3666 KeyTyped = skt;
3667 wild_menu_showing = 0;
3668 if (cclp->input_fn)
3669 RedrawingDisabled = old_RedrawingDisabled;
3670}
3671#endif
3672
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003673#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003674/*
3675 * "getcompletion()" function
3676 */
3677 void
3678f_getcompletion(typval_T *argvars, typval_T *rettv)
3679{
3680 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003681 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003682 expand_T xpc;
3683 int filtered = FALSE;
3684 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003685 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003686
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003687 if (in_vim9script()
3688 && (check_for_string_arg(argvars, 0) == FAIL
3689 || check_for_string_arg(argvars, 1) == FAIL
3690 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3691 return;
3692
ii144785fe02021-11-21 12:13:56 +00003693 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003694 if (argvars[1].v_type != VAR_STRING)
3695 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003696 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003697 return;
3698 }
3699 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003700
3701 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003702 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003703
3704 if (p_wic)
3705 options |= WILD_ICASE;
3706
3707 // For filtered results, 'wildignore' is used
3708 if (!filtered)
3709 options |= WILD_KEEP_ALL;
3710
3711 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003712 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003713 {
ii144785fe02021-11-21 12:13:56 +00003714 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003715 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003716 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003717 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003718 else
3719 {
ii144785fe02021-11-21 12:13:56 +00003720 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003721 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3722
3723 xpc.xp_context = cmdcomplete_str_to_type(type);
3724 if (xpc.xp_context == EXPAND_NOTHING)
3725 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003726 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003727 return;
3728 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003729
3730# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003731 if (xpc.xp_context == EXPAND_MENUS)
3732 {
3733 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3734 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3735 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003736# endif
3737# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003738 if (xpc.xp_context == EXPAND_CSCOPE)
3739 {
3740 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3741 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3742 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003743# endif
3744# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003745 if (xpc.xp_context == EXPAND_SIGN)
3746 {
3747 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3748 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3749 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003750# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003751 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003752
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00003753 if (cmdline_fuzzy_completion_supported(&xpc))
3754 // when fuzzy matching, don't modify the search string
3755 pat = vim_strsave(xpc.xp_pattern);
3756 else
3757 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3758
Bram Moolenaar66b51422019-08-18 21:44:12 +02003759 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3760 {
3761 int i;
3762
3763 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3764
3765 for (i = 0; i < xpc.xp_numfiles; i++)
3766 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3767 }
3768 vim_free(pat);
3769 ExpandCleanup(&xpc);
3770}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003771#endif // FEAT_EVAL