blob: 87953375d58feb4be52319ca20d61eb51f1423e7 [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;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100381 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000382
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000383 pum_undisplay();
384 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000385 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000386 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000387 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000388 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100389
390 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
391 // as a side effect.
392 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000393}
394
395void cmdline_pum_cleanup(cmdline_info_T *cclp)
396{
397 cmdline_pum_remove();
398 wildmenu_cleanup(cclp);
399}
400
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000401/*
402 * Returns the starting column number to use for the cmdline completion popup
403 * menu.
404 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000405int cmdline_compl_startcol(void)
406{
407 return compl_startcol;
408}
409#endif
410
Bram Moolenaar66b51422019-08-18 21:44:12 +0200411/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000412 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000413 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000414 */
415 static char_u *
416get_next_or_prev_match(
417 int mode,
418 expand_T *xp,
419 int *p_findex,
420 char_u *orig_save)
421{
422 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000423 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000424
425 if (xp->xp_numfiles <= 0)
426 return NULL;
427
428 if (mode == WILD_PREV)
429 {
430 if (findex == -1)
431 findex = xp->xp_numfiles;
432 --findex;
433 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000434 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000435 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000436 else if (mode == WILD_PAGEUP)
437 {
438 if (findex == 0)
439 // at the first entry, don't select any entries
440 findex = -1;
441 else if (findex == -1)
442 // no entry is selected. select the last entry
443 findex = xp->xp_numfiles - 1;
444 else
445 {
446 // go up by the pum height
447 ht = pum_get_height();
448 if (ht > 3)
449 ht -= 2;
450 findex -= ht;
451 if (findex < 0)
452 // few entries left, select the first entry
453 findex = 0;
454 }
455 }
456 else // mode == WILD_PAGEDOWN
457 {
458 if (findex == xp->xp_numfiles - 1)
459 // at the last entry, don't select any entries
460 findex = -1;
461 else if (findex == -1)
462 // no entry is selected. select the first entry
463 findex = 0;
464 else
465 {
466 // go down by the pum height
467 ht = pum_get_height();
468 if (ht > 3)
469 ht -= 2;
470 findex += ht;
471 if (findex >= xp->xp_numfiles)
472 // few entries left, select the last entry
473 findex = xp->xp_numfiles - 1;
474 }
475 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000476
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000477 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000478 if (findex < 0)
479 {
480 if (orig_save == NULL)
481 findex = xp->xp_numfiles - 1;
482 else
483 findex = -1;
484 }
485 if (findex >= xp->xp_numfiles)
486 {
487 if (orig_save == NULL)
488 findex = 0;
489 else
490 findex = -1;
491 }
492#ifdef FEAT_WILDMENU
493 if (compl_match_array)
494 {
495 compl_selected = findex;
496 cmdline_pum_display();
497 }
498 else if (p_wmnu)
499 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
500 findex, cmd_showtail);
501#endif
502 *p_findex = findex;
503
504 if (findex == -1)
505 return vim_strsave(orig_save);
506
507 return vim_strsave(xp->xp_files[findex]);
508}
509
510/*
511 * Start the command-line expansion and get the matches.
512 */
513 static char_u *
514ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
515{
516 int non_suf_match; // number without matching suffix
517 int i;
518 char_u *ss = NULL;
519
520 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000521 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000522 options) == FAIL)
523 {
524#ifdef FNAME_ILLEGAL
525 // Illegal file name has been silently skipped. But when there
526 // are wildcards, the real problem is that there was no match,
527 // causing the pattern to be added, which has illegal characters.
528 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
529 semsg(_(e_no_match_str_2), str);
530#endif
531 }
532 else if (xp->xp_numfiles == 0)
533 {
534 if (!(options & WILD_SILENT))
535 semsg(_(e_no_match_str_2), str);
536 }
537 else
538 {
539 // Escape the matches for use on the command line.
540 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
541
542 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000543 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000544 {
545 if (xp->xp_numfiles)
546 non_suf_match = xp->xp_numfiles;
547 else
548 non_suf_match = 1;
549 if ((xp->xp_context == EXPAND_FILES
550 || xp->xp_context == EXPAND_DIRECTORIES)
551 && xp->xp_numfiles > 1)
552 {
553 // More than one match; check suffix.
554 // The files will have been sorted on matching suffix in
555 // expand_wildcards, only need to check the first two.
556 non_suf_match = 0;
557 for (i = 0; i < 2; ++i)
558 if (match_suffix(xp->xp_files[i]))
559 ++non_suf_match;
560 }
561 if (non_suf_match != 1)
562 {
563 // Can we ever get here unless it's while expanding
564 // interactively? If not, we can get rid of this all
565 // together. Don't really want to wait for this message
566 // (and possibly have to hit return to continue!).
567 if (!(options & WILD_SILENT))
568 emsg(_(e_too_many_file_names));
569 else if (!(options & WILD_NO_BEEP))
570 beep_flush();
571 }
572 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
573 ss = vim_strsave(xp->xp_files[0]);
574 }
575 }
576
577 return ss;
578}
579
580/*
581 * Return the longest common part in the list of cmdline completion matches.
582 */
583 static char_u *
584find_longest_match(expand_T *xp, int options)
585{
586 long_u len;
587 int mb_len = 1;
588 int c0, ci;
589 int i;
590 char_u *ss;
591
592 for (len = 0; xp->xp_files[0][len]; len += mb_len)
593 {
594 if (has_mbyte)
595 {
596 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
597 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
598 }
599 else
600 c0 = xp->xp_files[0][len];
601 for (i = 1; i < xp->xp_numfiles; ++i)
602 {
603 if (has_mbyte)
604 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
605 else
606 ci = xp->xp_files[i][len];
607 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
608 || xp->xp_context == EXPAND_FILES
609 || xp->xp_context == EXPAND_SHELLCMD
610 || xp->xp_context == EXPAND_BUFFERS))
611 {
612 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
613 break;
614 }
615 else if (c0 != ci)
616 break;
617 }
618 if (i < xp->xp_numfiles)
619 {
620 if (!(options & WILD_NO_BEEP))
621 vim_beep(BO_WILD);
622 break;
623 }
624 }
625
626 ss = alloc(len + 1);
627 if (ss)
628 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
629
630 return ss;
631}
632
633/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000634 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200635 * Chars that should not be expanded must be preceded with a backslash.
636 * Return a pointer to allocated memory containing the new string.
637 * Return NULL for failure.
638 *
639 * "orig" is the originally expanded string, copied to allocated memory. It
640 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
641 * WILD_PREV "orig" should be NULL.
642 *
643 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
644 * is WILD_EXPAND_FREE or WILD_ALL.
645 *
646 * mode = WILD_FREE: just free previously expanded matches
647 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
648 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
649 * mode = WILD_NEXT: use next match in multiple match, wrap to first
650 * mode = WILD_PREV: use previous match in multiple match, wrap to first
651 * mode = WILD_ALL: return all matches concatenated
652 * mode = WILD_LONGEST: return longest matched part
653 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000654 * mode = WILD_APPLY: apply the item selected in the cmdline completion
655 * popup menu and close the menu.
656 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
657 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200658 *
659 * options = WILD_LIST_NOTFOUND: list entries without a match
660 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
661 * options = WILD_USE_NL: Use '\n' for WILD_ALL
662 * options = WILD_NO_BEEP: Don't beep for multiple matches
663 * options = WILD_ADD_SLASH: add a slash after directory names
664 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
665 * options = WILD_SILENT: don't print warning messages
666 * options = WILD_ESCAPE: put backslash before special chars
667 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200668 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200669 *
670 * The variables xp->xp_context and xp->xp_backslash must have been set!
671 */
672 char_u *
673ExpandOne(
674 expand_T *xp,
675 char_u *str,
676 char_u *orig, // allocated copy of original of expanded string
677 int options,
678 int mode)
679{
680 char_u *ss = NULL;
681 static int findex;
682 static char_u *orig_save = NULL; // kept value of orig
683 int orig_saved = FALSE;
684 int i;
685 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200686
687 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000688 if (mode == WILD_NEXT || mode == WILD_PREV
689 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000690 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200691
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000692 if (mode == WILD_CANCEL)
693 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
694 else if (mode == WILD_APPLY)
695 ss = vim_strsave(findex == -1 ? (orig_save ?
696 orig_save : (char_u *)"") : xp->xp_files[findex]);
697
Bram Moolenaar66b51422019-08-18 21:44:12 +0200698 // free old names
699 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
700 {
701 FreeWild(xp->xp_numfiles, xp->xp_files);
702 xp->xp_numfiles = -1;
703 VIM_CLEAR(orig_save);
704 }
705 findex = 0;
706
707 if (mode == WILD_FREE) // only release file name
708 return NULL;
709
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000710 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200711 {
712 vim_free(orig_save);
713 orig_save = orig;
714 orig_saved = TRUE;
715
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000716 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200717 }
718
719 // Find longest common part
720 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
721 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000722 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200723 findex = -1; // next p_wc gets first one
724 }
725
726 // Concatenate all matching names
727 if (mode == WILD_ALL && xp->xp_numfiles > 0)
728 {
729 len = 0;
730 for (i = 0; i < xp->xp_numfiles; ++i)
731 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
732 ss = alloc(len);
733 if (ss != NULL)
734 {
735 *ss = NUL;
736 for (i = 0; i < xp->xp_numfiles; ++i)
737 {
738 STRCAT(ss, xp->xp_files[i]);
739 if (i != xp->xp_numfiles - 1)
740 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
741 }
742 }
743 }
744
745 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
746 ExpandCleanup(xp);
747
748 // Free "orig" if it wasn't stored in "orig_save".
749 if (!orig_saved)
750 vim_free(orig);
751
752 return ss;
753}
754
755/*
756 * Prepare an expand structure for use.
757 */
758 void
759ExpandInit(expand_T *xp)
760{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200761 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200762 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200763 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200764}
765
766/*
767 * Cleanup an expand structure after use.
768 */
769 void
770ExpandCleanup(expand_T *xp)
771{
772 if (xp->xp_numfiles >= 0)
773 {
774 FreeWild(xp->xp_numfiles, xp->xp_files);
775 xp->xp_numfiles = -1;
776 }
777}
778
779/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000780 * Display one line of completion matches. Multiple matches are displayed in
781 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000782 * matches - list of completion match names
783 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000784 * lines - number of output lines
785 * linenr - line number of matches to display
786 * maxlen - maximum number of characters in each line
787 * showtail - display only the tail of the full path of a file name
788 * dir_attr - highlight attribute to use for directory names
789 */
790 static void
791showmatches_oneline(
792 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000793 char_u **matches,
794 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000795 int lines,
796 int linenr,
797 int maxlen,
798 int showtail,
799 int dir_attr)
800{
801 int i, j;
802 int isdir;
803 int lastlen;
804 char_u *p;
805
806 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000807 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000808 {
809 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
810 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000811 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
812 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000813 msg_advance(maxlen + 1);
814 msg_puts((char *)p);
815 msg_advance(maxlen + 3);
816 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
817 break;
818 }
819 for (i = maxlen - lastlen; --i >= 0; )
820 msg_putchar(' ');
821 if (xp->xp_context == EXPAND_FILES
822 || xp->xp_context == EXPAND_SHELLCMD
823 || xp->xp_context == EXPAND_BUFFERS)
824 {
825 // highlight directories
826 if (xp->xp_numfiles != -1)
827 {
828 char_u *halved_slash;
829 char_u *exp_path;
830 char_u *path;
831
832 // Expansion was done before and special characters
833 // were escaped, need to halve backslashes. Also
834 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000835 exp_path = expand_env_save_opt(matches[j], TRUE);
836 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000837 halved_slash = backslash_halve_save(path);
838 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000839 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000840 vim_free(exp_path);
841 if (halved_slash != path)
842 vim_free(halved_slash);
843 }
844 else
845 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000846 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000847 if (showtail)
848 p = SHOW_FILE_TEXT(j);
849 else
850 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000851 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000852 TRUE);
853 p = NameBuff;
854 }
855 }
856 else
857 {
858 isdir = FALSE;
859 p = SHOW_FILE_TEXT(j);
860 }
861 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
862 }
863 if (msg_col > 0) // when not wrapped around
864 {
865 msg_clr_eos();
866 msg_putchar('\n');
867 }
868 out_flush(); // show one line at a time
869}
870
871/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200872 * Show all matches for completion on the command line.
873 * Returns EXPAND_NOTHING when the character that triggered expansion should
874 * be inserted like a normal character.
875 */
876 int
877showmatches(expand_T *xp, int wildmenu UNUSED)
878{
879 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000880 int numMatches;
881 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000882 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200883 int maxlen;
884 int lines;
885 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200886 int attr;
887 int showtail;
888
889 if (xp->xp_numfiles == -1)
890 {
891 set_expand_context(xp);
892 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000893 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200894 showtail = expand_showtail(xp);
895 if (i != EXPAND_OK)
896 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200897 }
898 else
899 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000900 numMatches = xp->xp_numfiles;
901 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200902 showtail = cmd_showtail;
903 }
904
905#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000906 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000907 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000908 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000909#endif
910
911#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200912 if (!wildmenu)
913 {
914#endif
915 msg_didany = FALSE; // lines_left will be set
916 msg_start(); // prepare for paging
917 msg_putchar('\n');
918 out_flush();
919 cmdline_row = msg_row;
920 msg_didany = FALSE; // lines_left will be set again
921 msg_start(); // prepare for paging
922#ifdef FEAT_WILDMENU
923 }
924#endif
925
926 if (got_int)
927 got_int = FALSE; // only int. the completion, not the cmd line
928#ifdef FEAT_WILDMENU
929 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000930 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200931#endif
932 else
933 {
934 // find the length of the longest file name
935 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000936 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200937 {
938 if (!showtail && (xp->xp_context == EXPAND_FILES
939 || xp->xp_context == EXPAND_SHELLCMD
940 || xp->xp_context == EXPAND_BUFFERS))
941 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000942 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200943 j = vim_strsize(NameBuff);
944 }
945 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000946 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200947 if (j > maxlen)
948 maxlen = j;
949 }
950
951 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000952 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200953 else
954 {
955 // compute the number of columns and lines for the listing
956 maxlen += 2; // two spaces between file names
957 columns = ((int)Columns + 2) / maxlen;
958 if (columns < 1)
959 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000960 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200961 }
962
963 attr = HL_ATTR(HLF_D); // find out highlighting for directories
964
965 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
966 {
967 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
968 msg_clr_eos();
969 msg_advance(maxlen - 3);
970 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
971 }
972
973 // list the files line by line
974 for (i = 0; i < lines; ++i)
975 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000976 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000977 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200978 if (got_int)
979 {
980 got_int = FALSE;
981 break;
982 }
983 }
984
985 // we redraw the command below the lines that we have just listed
986 // This is a bit tricky, but it saves a lot of screen updating.
987 cmdline_row = msg_row; // will put it back later
988 }
989
990 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000991 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200992
993 return EXPAND_OK;
994}
995
996/*
997 * Private gettail for showmatches() (and win_redr_status_matches()):
998 * Find tail of file name path, but ignore trailing "/".
999 */
1000 char_u *
1001sm_gettail(char_u *s)
1002{
1003 char_u *p;
1004 char_u *t = s;
1005 int had_sep = FALSE;
1006
1007 for (p = s; *p != NUL; )
1008 {
1009 if (vim_ispathsep(*p)
1010#ifdef BACKSLASH_IN_FILENAME
1011 && !rem_backslash(p)
1012#endif
1013 )
1014 had_sep = TRUE;
1015 else if (had_sep)
1016 {
1017 t = p;
1018 had_sep = FALSE;
1019 }
1020 MB_PTR_ADV(p);
1021 }
1022 return t;
1023}
1024
1025/*
1026 * Return TRUE if we only need to show the tail of completion matches.
1027 * When not completing file names or there is a wildcard in the path FALSE is
1028 * returned.
1029 */
1030 static int
1031expand_showtail(expand_T *xp)
1032{
1033 char_u *s;
1034 char_u *end;
1035
1036 // When not completing file names a "/" may mean something different.
1037 if (xp->xp_context != EXPAND_FILES
1038 && xp->xp_context != EXPAND_SHELLCMD
1039 && xp->xp_context != EXPAND_DIRECTORIES)
1040 return FALSE;
1041
1042 end = gettail(xp->xp_pattern);
1043 if (end == xp->xp_pattern) // there is no path separator
1044 return FALSE;
1045
1046 for (s = xp->xp_pattern; s < end; s++)
1047 {
1048 // Skip escaped wildcards. Only when the backslash is not a path
1049 // separator, on DOS the '*' "path\*\file" must not be skipped.
1050 if (rem_backslash(s))
1051 ++s;
1052 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1053 return FALSE;
1054 }
1055 return TRUE;
1056}
1057
1058/*
1059 * Prepare a string for expansion.
1060 * When expanding file names: The string will be used with expand_wildcards().
1061 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1062 * When expanding other names: The string will be used with regcomp(). Copy
1063 * the name into allocated memory and prepend "^".
1064 */
1065 char_u *
1066addstar(
1067 char_u *fname,
1068 int len,
1069 int context) // EXPAND_FILES etc.
1070{
1071 char_u *retval;
1072 int i, j;
1073 int new_len;
1074 char_u *tail;
1075 int ends_in_star;
1076
1077 if (context != EXPAND_FILES
1078 && context != EXPAND_FILES_IN_PATH
1079 && context != EXPAND_SHELLCMD
1080 && context != EXPAND_DIRECTORIES)
1081 {
1082 // Matching will be done internally (on something other than files).
1083 // So we convert the file-matching-type wildcards into our kind for
1084 // use with vim_regcomp(). First work out how long it will be:
1085
1086 // For help tags the translation is done in find_help_tags().
1087 // For a tag pattern starting with "/" no translation is needed.
1088 if (context == EXPAND_HELP
1089 || context == EXPAND_COLORS
1090 || context == EXPAND_COMPILER
1091 || context == EXPAND_OWNSYNTAX
1092 || context == EXPAND_FILETYPE
1093 || context == EXPAND_PACKADD
1094 || ((context == EXPAND_TAGS_LISTFILES
1095 || context == EXPAND_TAGS)
1096 && fname[0] == '/'))
1097 retval = vim_strnsave(fname, len);
1098 else
1099 {
1100 new_len = len + 2; // +2 for '^' at start, NUL at end
1101 for (i = 0; i < len; i++)
1102 {
1103 if (fname[i] == '*' || fname[i] == '~')
1104 new_len++; // '*' needs to be replaced by ".*"
1105 // '~' needs to be replaced by "\~"
1106
1107 // Buffer names are like file names. "." should be literal
1108 if (context == EXPAND_BUFFERS && fname[i] == '.')
1109 new_len++; // "." becomes "\."
1110
1111 // Custom expansion takes care of special things, match
1112 // backslashes literally (perhaps also for other types?)
1113 if ((context == EXPAND_USER_DEFINED
1114 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1115 new_len++; // '\' becomes "\\"
1116 }
1117 retval = alloc(new_len);
1118 if (retval != NULL)
1119 {
1120 retval[0] = '^';
1121 j = 1;
1122 for (i = 0; i < len; i++, j++)
1123 {
1124 // Skip backslash. But why? At least keep it for custom
1125 // expansion.
1126 if (context != EXPAND_USER_DEFINED
1127 && context != EXPAND_USER_LIST
1128 && fname[i] == '\\'
1129 && ++i == len)
1130 break;
1131
1132 switch (fname[i])
1133 {
1134 case '*': retval[j++] = '.';
1135 break;
1136 case '~': retval[j++] = '\\';
1137 break;
1138 case '?': retval[j] = '.';
1139 continue;
1140 case '.': if (context == EXPAND_BUFFERS)
1141 retval[j++] = '\\';
1142 break;
1143 case '\\': if (context == EXPAND_USER_DEFINED
1144 || context == EXPAND_USER_LIST)
1145 retval[j++] = '\\';
1146 break;
1147 }
1148 retval[j] = fname[i];
1149 }
1150 retval[j] = NUL;
1151 }
1152 }
1153 }
1154 else
1155 {
1156 retval = alloc(len + 4);
1157 if (retval != NULL)
1158 {
1159 vim_strncpy(retval, fname, len);
1160
1161 // Don't add a star to *, ~, ~user, $var or `cmd`.
1162 // * would become **, which walks the whole tree.
1163 // ~ would be at the start of the file name, but not the tail.
1164 // $ could be anywhere in the tail.
1165 // ` could be anywhere in the file name.
1166 // When the name ends in '$' don't add a star, remove the '$'.
1167 tail = gettail(retval);
1168 ends_in_star = (len > 0 && retval[len - 1] == '*');
1169#ifndef BACKSLASH_IN_FILENAME
1170 for (i = len - 2; i >= 0; --i)
1171 {
1172 if (retval[i] != '\\')
1173 break;
1174 ends_in_star = !ends_in_star;
1175 }
1176#endif
1177 if ((*retval != '~' || tail != retval)
1178 && !ends_in_star
1179 && vim_strchr(tail, '$') == NULL
1180 && vim_strchr(retval, '`') == NULL)
1181 retval[len++] = '*';
1182 else if (len > 0 && retval[len - 1] == '$')
1183 --len;
1184 retval[len] = NUL;
1185 }
1186 }
1187 return retval;
1188}
1189
1190/*
1191 * Must parse the command line so far to work out what context we are in.
1192 * Completion can then be done based on that context.
1193 * This routine sets the variables:
1194 * xp->xp_pattern The start of the pattern to be expanded within
1195 * the command line (ends at the cursor).
1196 * xp->xp_context The type of thing to expand. Will be one of:
1197 *
1198 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1199 * the command line, like an unknown command. Caller
1200 * should beep.
1201 * EXPAND_NOTHING Unrecognised context for completion, use char like
1202 * a normal char, rather than for completion. eg
1203 * :s/^I/
1204 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1205 * it.
1206 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1207 * EXPAND_FILES After command with EX_XFILE set, or after setting
1208 * with P_EXPAND set. eg :e ^I, :w>>^I
1209 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1210 * when we know only directories are of interest. eg
1211 * :set dir=^I
1212 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1213 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1214 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1215 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1216 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1217 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1218 * EXPAND_EVENTS Complete event names
1219 * EXPAND_SYNTAX Complete :syntax command arguments
1220 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1221 * EXPAND_AUGROUP Complete autocommand group names
1222 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1223 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1224 * eg :unmap a^I , :cunab x^I
1225 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1226 * eg :call sub^I
1227 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1228 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1229 * names in expressions, eg :while s^I
1230 * EXPAND_ENV_VARS Complete environment variable names
1231 * EXPAND_USER Complete user names
1232 */
1233 static void
1234set_expand_context(expand_T *xp)
1235{
1236 cmdline_info_T *ccline = get_cmdline_info();
1237
1238 // only expansion for ':', '>' and '=' command-lines
1239 if (ccline->cmdfirstc != ':'
1240#ifdef FEAT_EVAL
1241 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1242 && !ccline->input_fn
1243#endif
1244 )
1245 {
1246 xp->xp_context = EXPAND_NOTHING;
1247 return;
1248 }
1249 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1250}
1251
Bram Moolenaard0190392019-08-23 21:17:35 +02001252/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001253 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1254 * For user defined commands, the completion context is set in 'xp' and the
1255 * completion flags in 'complp'.
1256 *
1257 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001258 */
1259 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001260set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001261{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001262 char_u *p = NULL;
1263 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001264 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001265
1266 // Isolate the command and search for it in the command table.
1267 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001268 // - the 'k' command can directly be followed by any character, but do
1269 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1270 // find matches anywhere in the command name, do this only for command
1271 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001272 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001273 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001274 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001275 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001276 p = cmd + 1;
1277 }
1278 else
1279 {
1280 p = cmd;
1281 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1282 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001283 // A user command may contain digits.
1284 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1285 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001286 while (ASCII_ISALNUM(*p) || *p == '*')
1287 ++p;
1288 // for python 3.x: ":py3*" commands completion
1289 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1290 {
1291 ++p;
1292 while (ASCII_ISALPHA(*p) || *p == '*')
1293 ++p;
1294 }
1295 // check for non-alpha command
1296 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1297 ++p;
1298 len = (int)(p - cmd);
1299
1300 if (len == 0)
1301 {
1302 xp->xp_context = EXPAND_UNSUCCESSFUL;
1303 return NULL;
1304 }
1305
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001306 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001307
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001308 // User defined commands support alphanumeric characters.
1309 // Also when doing fuzzy expansion, support alphanumeric characters.
1310 if ((cmd[0] >= 'A' && cmd[0] <= 'Z') || (fuzzy && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001311 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1312 ++p;
1313 }
1314
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001315 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001316 // character, complete the command name.
1317 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1318 return NULL;
1319
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001320 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001321 {
1322 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1323 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001324 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001325 p = cmd + 1;
1326 }
1327 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1328 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001329 eap->cmd = cmd;
1330 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001331 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001332 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001333 }
1334 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001335 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001336 {
1337 // Not still touching the command and it was an illegal one
1338 xp->xp_context = EXPAND_UNSUCCESSFUL;
1339 return NULL;
1340 }
1341
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001342 return p;
1343}
Bram Moolenaard0190392019-08-23 21:17:35 +02001344
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001345/*
1346 * Set the completion context for a command argument with wild card characters.
1347 */
1348 static void
1349set_context_for_wildcard_arg(
1350 exarg_T *eap,
1351 char_u *arg,
1352 int usefilter,
1353 expand_T *xp,
1354 int *complp)
1355{
1356 char_u *p;
1357 int c;
1358 int in_quote = FALSE;
1359 char_u *bow = NULL; // Beginning of word
1360 int len = 0;
1361
1362 // Allow spaces within back-quotes to count as part of the argument
1363 // being expanded.
1364 xp->xp_pattern = skipwhite(arg);
1365 p = xp->xp_pattern;
1366 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001367 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001368 if (has_mbyte)
1369 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001370 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001371 c = *p;
1372 if (c == '\\' && p[1] != NUL)
1373 ++p;
1374 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001375 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001376 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001377 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001378 xp->xp_pattern = p;
1379 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001380 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001381 in_quote = !in_quote;
1382 }
1383 // An argument can contain just about everything, except
1384 // characters that end the command and white space.
1385 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001386#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001387 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001388#endif
1389 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001390 {
1391 len = 0; // avoid getting stuck when space is in 'isfname'
1392 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001393 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001394 if (has_mbyte)
1395 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001396 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001397 c = *p;
1398 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001399 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001400 if (has_mbyte)
1401 len = (*mb_ptr2len)(p);
1402 else
1403 len = 1;
1404 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001405 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001406 if (in_quote)
1407 bow = p;
1408 else
1409 xp->xp_pattern = p;
1410 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001411 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001412 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001413 }
1414
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001415 // If we are still inside the quotes, and we passed a space, just
1416 // expand from there.
1417 if (bow != NULL && in_quote)
1418 xp->xp_pattern = bow;
1419 xp->xp_context = EXPAND_FILES;
1420
1421 // For a shell command more chars need to be escaped.
1422 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1423 {
1424#ifndef BACKSLASH_IN_FILENAME
1425 xp->xp_shell = TRUE;
1426#endif
1427 // When still after the command name expand executables.
1428 if (xp->xp_pattern == skipwhite(arg))
1429 xp->xp_context = EXPAND_SHELLCMD;
1430 }
1431
1432 // Check for environment variable.
1433 if (*xp->xp_pattern == '$')
1434 {
1435 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1436 if (!vim_isIDc(*p))
1437 break;
1438 if (*p == NUL)
1439 {
1440 xp->xp_context = EXPAND_ENV_VARS;
1441 ++xp->xp_pattern;
1442 // Avoid that the assignment uses EXPAND_FILES again.
1443 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1444 *complp = EXPAND_ENV_VARS;
1445 }
1446 }
1447 // Check for user names.
1448 if (*xp->xp_pattern == '~')
1449 {
1450 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1451 ;
1452 // Complete ~user only if it partially matches a user name.
1453 // A full match ~user<Tab> will be replaced by user's home
1454 // directory i.e. something like ~user<Tab> -> /home/user/
1455 if (*p == NUL && p > xp->xp_pattern + 1
1456 && match_user(xp->xp_pattern + 1) >= 1)
1457 {
1458 xp->xp_context = EXPAND_USER;
1459 ++xp->xp_pattern;
1460 }
1461 }
1462}
1463
1464/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001465 * Set the completion context for the :filter command. Returns a pointer to the
1466 * next command after the :filter command.
1467 */
1468 static char_u *
1469set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1470{
1471 if (*arg != NUL)
1472 arg = skip_vimgrep_pat(arg, NULL, NULL);
1473 if (arg == NULL || *arg == NUL)
1474 {
1475 xp->xp_context = EXPAND_NOTHING;
1476 return NULL;
1477 }
1478 return skipwhite(arg);
1479}
1480
1481#ifdef FEAT_SEARCH_EXTRA
1482/*
1483 * Set the completion context for the :match command. Returns a pointer to the
1484 * next command after the :match command.
1485 */
1486 static char_u *
1487set_context_in_match_cmd(expand_T *xp, char_u *arg)
1488{
1489 if (*arg == NUL || !ends_excmd(*arg))
1490 {
1491 // also complete "None"
1492 set_context_in_echohl_cmd(xp, arg);
1493 arg = skipwhite(skiptowhite(arg));
1494 if (*arg != NUL)
1495 {
1496 xp->xp_context = EXPAND_NOTHING;
1497 arg = skip_regexp(arg + 1, *arg, magic_isset());
1498 }
1499 }
1500 return find_nextcmd(arg);
1501}
1502#endif
1503
1504/*
1505 * Returns a pointer to the next command after a :global or a :v command.
1506 * Returns NULL if there is no next command.
1507 */
1508 static char_u *
1509find_cmd_after_global_cmd(char_u *arg)
1510{
1511 int delim;
1512
1513 delim = *arg; // get the delimiter
1514 if (delim)
1515 ++arg; // skip delimiter if there is one
1516
1517 while (arg[0] != NUL && arg[0] != delim)
1518 {
1519 if (arg[0] == '\\' && arg[1] != NUL)
1520 ++arg;
1521 ++arg;
1522 }
1523 if (arg[0] != NUL)
1524 return arg + 1;
1525
1526 return NULL;
1527}
1528
1529/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001530 * Returns a pointer to the next command after a :substitute or a :& command.
1531 * Returns NULL if there is no next command.
1532 */
1533 static char_u *
1534find_cmd_after_substitute_cmd(char_u *arg)
1535{
1536 int delim;
1537
1538 delim = *arg;
1539 if (delim)
1540 {
1541 // skip "from" part
1542 ++arg;
1543 arg = skip_regexp(arg, delim, magic_isset());
1544
1545 if (arg[0] != NUL && arg[0] == delim)
1546 {
1547 // skip "to" part
1548 ++arg;
1549 while (arg[0] != NUL && arg[0] != delim)
1550 {
1551 if (arg[0] == '\\' && arg[1] != NUL)
1552 ++arg;
1553 ++arg;
1554 }
1555 if (arg[0] != NUL) // skip delimiter
1556 ++arg;
1557 }
1558 }
1559 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1560 ++arg;
1561 if (arg[0] != NUL)
1562 return arg;
1563
1564 return NULL;
1565}
1566
1567/*
1568 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1569 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1570 * Returns NULL if there is no next command.
1571 */
1572 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001573find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001574{
1575 arg = skipwhite(skipdigits(arg)); // skip count
1576 if (*arg == '/') // Match regexp, not just whole words
1577 {
1578 for (++arg; *arg && *arg != '/'; arg++)
1579 if (*arg == '\\' && arg[1] != NUL)
1580 arg++;
1581 if (*arg)
1582 {
1583 arg = skipwhite(arg + 1);
1584
1585 // Check for trailing illegal characters
1586 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1587 xp->xp_context = EXPAND_NOTHING;
1588 else
1589 return arg;
1590 }
1591 }
1592
1593 return NULL;
1594}
1595
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001596#ifdef FEAT_EVAL
1597/*
1598 * Set the completion context for the :unlet command. Always returns NULL.
1599 */
1600 static char_u *
1601set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1602{
1603 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1604 arg = xp->xp_pattern + 1;
1605
1606 xp->xp_context = EXPAND_USER_VARS;
1607 xp->xp_pattern = arg;
1608
1609 if (*xp->xp_pattern == '$')
1610 {
1611 xp->xp_context = EXPAND_ENV_VARS;
1612 ++xp->xp_pattern;
1613 }
1614
1615 return NULL;
1616}
1617#endif
1618
1619#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1620/*
1621 * Set the completion context for the :language command. Always returns NULL.
1622 */
1623 static char_u *
1624set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1625{
1626 char_u *p;
1627
1628 p = skiptowhite(arg);
1629 if (*p == NUL)
1630 {
1631 xp->xp_context = EXPAND_LANGUAGE;
1632 xp->xp_pattern = arg;
1633 }
1634 else
1635 {
1636 if ( STRNCMP(arg, "messages", p - arg) == 0
1637 || STRNCMP(arg, "ctype", p - arg) == 0
1638 || STRNCMP(arg, "time", p - arg) == 0
1639 || STRNCMP(arg, "collate", p - arg) == 0)
1640 {
1641 xp->xp_context = EXPAND_LOCALES;
1642 xp->xp_pattern = skipwhite(p);
1643 }
1644 else
1645 xp->xp_context = EXPAND_NOTHING;
1646 }
1647
1648 return NULL;
1649}
1650#endif
1651
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001652#ifdef FEAT_EVAL
1653static enum
1654{
1655 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001656 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1657 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001658} breakpt_expand_what;
1659
1660/*
1661 * Set the completion context for the :breakadd command. Always returns NULL.
1662 */
1663 static char_u *
1664set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1665{
1666 char_u *p;
1667 char_u *subcmd_start;
1668
1669 xp->xp_context = EXPAND_BREAKPOINT;
1670 xp->xp_pattern = arg;
1671
1672 if (cmdidx == CMD_breakadd)
1673 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001674 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001675 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001676 else
1677 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001678
1679 p = skipwhite(arg);
1680 if (*p == NUL)
1681 return NULL;
1682 subcmd_start = p;
1683
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001684 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001685 {
1686 // :breakadd file [lnum] <filename>
1687 // :breakadd func [lnum] <funcname>
1688 p += 4;
1689 p = skipwhite(p);
1690
1691 // skip line number (if specified)
1692 if (VIM_ISDIGIT(*p))
1693 {
1694 p = skipdigits(p);
1695 if (*p != ' ')
1696 {
1697 xp->xp_context = EXPAND_NOTHING;
1698 return NULL;
1699 }
1700 p = skipwhite(p);
1701 }
1702 if (STRNCMP("file", subcmd_start, 4) == 0)
1703 xp->xp_context = EXPAND_FILES;
1704 else
1705 xp->xp_context = EXPAND_USER_FUNC;
1706 xp->xp_pattern = p;
1707 }
1708 else if (STRNCMP("expr ", p, 5) == 0)
1709 {
1710 // :breakadd expr <expression>
1711 xp->xp_context = EXPAND_EXPRESSION;
1712 xp->xp_pattern = skipwhite(p + 5);
1713 }
1714
1715 return NULL;
1716}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001717
1718 static char_u *
1719set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1720{
1721 char_u *p;
1722
1723 xp->xp_context = EXPAND_NOTHING;
1724 xp->xp_pattern = NULL;
1725
1726 p = skipwhite(arg);
1727 if (VIM_ISDIGIT(*p))
1728 return NULL;
1729
1730 xp->xp_context = EXPAND_SCRIPTNAMES;
1731 xp->xp_pattern = p;
1732
1733 return NULL;
1734}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001735#endif
1736
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001737/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001738 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1739 * The argument to the command is 'arg' and the argument flags is 'argt'.
1740 * For user-defined commands and for environment variables, 'compl' has the
1741 * completion type.
1742 * Returns a pointer to the next command. Returns NULL if there is no next
1743 * command.
1744 */
1745 static char_u *
1746set_context_by_cmdname(
1747 char_u *cmd,
1748 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001749 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001750 char_u *arg,
1751 long argt,
1752 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001753 int forceit)
1754{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001755 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001756 {
1757 case CMD_find:
1758 case CMD_sfind:
1759 case CMD_tabfind:
1760 if (xp->xp_context == EXPAND_FILES)
1761 xp->xp_context = EXPAND_FILES_IN_PATH;
1762 break;
1763 case CMD_cd:
1764 case CMD_chdir:
1765 case CMD_tcd:
1766 case CMD_tchdir:
1767 case CMD_lcd:
1768 case CMD_lchdir:
1769 if (xp->xp_context == EXPAND_FILES)
1770 xp->xp_context = EXPAND_DIRECTORIES;
1771 break;
1772 case CMD_help:
1773 xp->xp_context = EXPAND_HELP;
1774 xp->xp_pattern = arg;
1775 break;
1776
1777 // Command modifiers: return the argument.
1778 // Also for commands with an argument that is a command.
1779 case CMD_aboveleft:
1780 case CMD_argdo:
1781 case CMD_belowright:
1782 case CMD_botright:
1783 case CMD_browse:
1784 case CMD_bufdo:
1785 case CMD_cdo:
1786 case CMD_cfdo:
1787 case CMD_confirm:
1788 case CMD_debug:
1789 case CMD_folddoclosed:
1790 case CMD_folddoopen:
1791 case CMD_hide:
1792 case CMD_keepalt:
1793 case CMD_keepjumps:
1794 case CMD_keepmarks:
1795 case CMD_keeppatterns:
1796 case CMD_ldo:
1797 case CMD_leftabove:
1798 case CMD_lfdo:
1799 case CMD_lockmarks:
1800 case CMD_noautocmd:
1801 case CMD_noswapfile:
1802 case CMD_rightbelow:
1803 case CMD_sandbox:
1804 case CMD_silent:
1805 case CMD_tab:
1806 case CMD_tabdo:
1807 case CMD_topleft:
1808 case CMD_verbose:
1809 case CMD_vertical:
1810 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001811 case CMD_vim9cmd:
1812 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001813 return arg;
1814
1815 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001816 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001817
1818#ifdef FEAT_SEARCH_EXTRA
1819 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001820 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001821#endif
1822
1823 // All completion for the +cmdline_compl feature goes here.
1824
1825 case CMD_command:
1826 return set_context_in_user_cmd(xp, arg);
1827
1828 case CMD_delcommand:
1829 xp->xp_context = EXPAND_USER_COMMANDS;
1830 xp->xp_pattern = arg;
1831 break;
1832
1833 case CMD_global:
1834 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001835 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001836 case CMD_and:
1837 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001838 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001839 case CMD_isearch:
1840 case CMD_dsearch:
1841 case CMD_ilist:
1842 case CMD_dlist:
1843 case CMD_ijump:
1844 case CMD_psearch:
1845 case CMD_djump:
1846 case CMD_isplit:
1847 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001848 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001849 case CMD_autocmd:
1850 return set_context_in_autocmd(xp, arg, FALSE);
1851 case CMD_doautocmd:
1852 case CMD_doautoall:
1853 return set_context_in_autocmd(xp, arg, TRUE);
1854 case CMD_set:
1855 set_context_in_set_cmd(xp, arg, 0);
1856 break;
1857 case CMD_setglobal:
1858 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1859 break;
1860 case CMD_setlocal:
1861 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1862 break;
1863 case CMD_tag:
1864 case CMD_stag:
1865 case CMD_ptag:
1866 case CMD_ltag:
1867 case CMD_tselect:
1868 case CMD_stselect:
1869 case CMD_ptselect:
1870 case CMD_tjump:
1871 case CMD_stjump:
1872 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001873 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001874 xp->xp_context = EXPAND_TAGS_LISTFILES;
1875 else
1876 xp->xp_context = EXPAND_TAGS;
1877 xp->xp_pattern = arg;
1878 break;
1879 case CMD_augroup:
1880 xp->xp_context = EXPAND_AUGROUP;
1881 xp->xp_pattern = arg;
1882 break;
1883#ifdef FEAT_SYN_HL
1884 case CMD_syntax:
1885 set_context_in_syntax_cmd(xp, arg);
1886 break;
1887#endif
1888#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001889 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001890 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001891 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001892 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001893 case CMD_if:
1894 case CMD_elseif:
1895 case CMD_while:
1896 case CMD_for:
1897 case CMD_echo:
1898 case CMD_echon:
1899 case CMD_execute:
1900 case CMD_echomsg:
1901 case CMD_echoerr:
1902 case CMD_call:
1903 case CMD_return:
1904 case CMD_cexpr:
1905 case CMD_caddexpr:
1906 case CMD_cgetexpr:
1907 case CMD_lexpr:
1908 case CMD_laddexpr:
1909 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001910 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001911 break;
1912
1913 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001914 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001915 case CMD_function:
1916 case CMD_delfunction:
1917 xp->xp_context = EXPAND_USER_FUNC;
1918 xp->xp_pattern = arg;
1919 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001920 case CMD_disassemble:
1921 set_context_in_disassemble_cmd(xp, arg);
1922 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001923
1924 case CMD_echohl:
1925 set_context_in_echohl_cmd(xp, arg);
1926 break;
1927#endif
1928 case CMD_highlight:
1929 set_context_in_highlight_cmd(xp, arg);
1930 break;
1931#ifdef FEAT_CSCOPE
1932 case CMD_cscope:
1933 case CMD_lcscope:
1934 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001935 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001936 break;
1937#endif
1938#ifdef FEAT_SIGNS
1939 case CMD_sign:
1940 set_context_in_sign_cmd(xp, arg);
1941 break;
1942#endif
1943 case CMD_bdelete:
1944 case CMD_bwipeout:
1945 case CMD_bunload:
1946 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1947 arg = xp->xp_pattern + 1;
1948 // FALLTHROUGH
1949 case CMD_buffer:
1950 case CMD_sbuffer:
1951 case CMD_checktime:
1952 xp->xp_context = EXPAND_BUFFERS;
1953 xp->xp_pattern = arg;
1954 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001955#ifdef FEAT_DIFF
1956 case CMD_diffget:
1957 case CMD_diffput:
1958 // If current buffer is in diff mode, complete buffer names
1959 // which are in diff mode, and different than current buffer.
1960 xp->xp_context = EXPAND_DIFF_BUFFERS;
1961 xp->xp_pattern = arg;
1962 break;
1963#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001964 case CMD_USER:
1965 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001966 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1967 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001968
1969 case CMD_map: case CMD_noremap:
1970 case CMD_nmap: case CMD_nnoremap:
1971 case CMD_vmap: case CMD_vnoremap:
1972 case CMD_omap: case CMD_onoremap:
1973 case CMD_imap: case CMD_inoremap:
1974 case CMD_cmap: case CMD_cnoremap:
1975 case CMD_lmap: case CMD_lnoremap:
1976 case CMD_smap: case CMD_snoremap:
1977 case CMD_tmap: case CMD_tnoremap:
1978 case CMD_xmap: case CMD_xnoremap:
1979 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001980 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001981 case CMD_unmap:
1982 case CMD_nunmap:
1983 case CMD_vunmap:
1984 case CMD_ounmap:
1985 case CMD_iunmap:
1986 case CMD_cunmap:
1987 case CMD_lunmap:
1988 case CMD_sunmap:
1989 case CMD_tunmap:
1990 case CMD_xunmap:
1991 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001992 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001993 case CMD_mapclear:
1994 case CMD_nmapclear:
1995 case CMD_vmapclear:
1996 case CMD_omapclear:
1997 case CMD_imapclear:
1998 case CMD_cmapclear:
1999 case CMD_lmapclear:
2000 case CMD_smapclear:
2001 case CMD_tmapclear:
2002 case CMD_xmapclear:
2003 xp->xp_context = EXPAND_MAPCLEAR;
2004 xp->xp_pattern = arg;
2005 break;
2006
2007 case CMD_abbreviate: case CMD_noreabbrev:
2008 case CMD_cabbrev: case CMD_cnoreabbrev:
2009 case CMD_iabbrev: case CMD_inoreabbrev:
2010 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002011 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002012 case CMD_unabbreviate:
2013 case CMD_cunabbrev:
2014 case CMD_iunabbrev:
2015 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002016 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002017#ifdef FEAT_MENU
2018 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2019 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2020 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2021 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2022 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2023 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2024 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2025 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2026 case CMD_tmenu: case CMD_tunmenu:
2027 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2028 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2029#endif
2030
2031 case CMD_colorscheme:
2032 xp->xp_context = EXPAND_COLORS;
2033 xp->xp_pattern = arg;
2034 break;
2035
2036 case CMD_compiler:
2037 xp->xp_context = EXPAND_COMPILER;
2038 xp->xp_pattern = arg;
2039 break;
2040
2041 case CMD_ownsyntax:
2042 xp->xp_context = EXPAND_OWNSYNTAX;
2043 xp->xp_pattern = arg;
2044 break;
2045
2046 case CMD_setfiletype:
2047 xp->xp_context = EXPAND_FILETYPE;
2048 xp->xp_pattern = arg;
2049 break;
2050
2051 case CMD_packadd:
2052 xp->xp_context = EXPAND_PACKADD;
2053 xp->xp_pattern = arg;
2054 break;
2055
2056#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2057 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002058 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002059#endif
2060#if defined(FEAT_PROFILE)
2061 case CMD_profile:
2062 set_context_in_profile_cmd(xp, arg);
2063 break;
2064#endif
2065 case CMD_behave:
2066 xp->xp_context = EXPAND_BEHAVE;
2067 xp->xp_pattern = arg;
2068 break;
2069
2070 case CMD_messages:
2071 xp->xp_context = EXPAND_MESSAGES;
2072 xp->xp_pattern = arg;
2073 break;
2074
2075 case CMD_history:
2076 xp->xp_context = EXPAND_HISTORY;
2077 xp->xp_pattern = arg;
2078 break;
2079#if defined(FEAT_PROFILE)
2080 case CMD_syntime:
2081 xp->xp_context = EXPAND_SYNTIME;
2082 xp->xp_pattern = arg;
2083 break;
2084#endif
2085
2086 case CMD_argdelete:
2087 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2088 arg = xp->xp_pattern + 1;
2089 xp->xp_context = EXPAND_ARGLIST;
2090 xp->xp_pattern = arg;
2091 break;
2092
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002093#ifdef FEAT_EVAL
2094 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002095 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002096 case CMD_breakdel:
2097 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002098
2099 case CMD_scriptnames:
2100 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002101#endif
2102
Bram Moolenaard0190392019-08-23 21:17:35 +02002103 default:
2104 break;
2105 }
2106 return NULL;
2107}
2108
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002109/*
2110 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2111 * we don't need/want deleted. Maybe this could be done better if we didn't
2112 * repeat all this stuff. The only problem is that they may not stay
2113 * perfectly compatible with each other, but then the command line syntax
2114 * probably won't change that much -- webb.
2115 */
2116 static char_u *
2117set_one_cmd_context(
2118 expand_T *xp,
2119 char_u *buff) // buffer for command string
2120{
2121 char_u *p;
2122 char_u *cmd, *arg;
2123 int len = 0;
2124 exarg_T ea;
2125 int compl = EXPAND_NOTHING;
2126 int forceit = FALSE;
2127 int usefilter = FALSE; // filter instead of file name
2128
2129 ExpandInit(xp);
2130 xp->xp_pattern = buff;
2131 xp->xp_line = buff;
2132 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2133 ea.argt = 0;
2134
2135 // 1. skip comment lines and leading space, colons or bars
2136 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2137 ;
2138 xp->xp_pattern = cmd;
2139
2140 if (*cmd == NUL)
2141 return NULL;
2142 if (*cmd == '"') // ignore comment lines
2143 {
2144 xp->xp_context = EXPAND_NOTHING;
2145 return NULL;
2146 }
2147
2148 // 3. Skip over the range to find the command.
2149 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2150 xp->xp_pattern = cmd;
2151 if (*cmd == NUL)
2152 return NULL;
2153 if (*cmd == '"')
2154 {
2155 xp->xp_context = EXPAND_NOTHING;
2156 return NULL;
2157 }
2158
2159 if (*cmd == '|' || *cmd == '\n')
2160 return cmd + 1; // There's another command
2161
2162 // Get the command index.
2163 p = set_cmd_index(cmd, &ea, xp, &compl);
2164 if (p == NULL)
2165 return NULL;
2166
2167 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2168
2169 if (*p == '!') // forced commands
2170 {
2171 forceit = TRUE;
2172 ++p;
2173 }
2174
2175 // 6. parse arguments
2176 if (!IS_USER_CMDIDX(ea.cmdidx))
2177 ea.argt = excmd_get_argt(ea.cmdidx);
2178
2179 arg = skipwhite(p);
2180
2181 // Skip over ++argopt argument
2182 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2183 {
2184 p = arg;
2185 while (*p && !vim_isspace(*p))
2186 MB_PTR_ADV(p);
2187 arg = skipwhite(p);
2188 }
2189
2190 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2191 {
2192 if (*arg == '>') // append
2193 {
2194 if (*++arg == '>')
2195 ++arg;
2196 arg = skipwhite(arg);
2197 }
2198 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2199 {
2200 ++arg;
2201 usefilter = TRUE;
2202 }
2203 }
2204
2205 if (ea.cmdidx == CMD_read)
2206 {
2207 usefilter = forceit; // :r! filter if forced
2208 if (*arg == '!') // :r !filter
2209 {
2210 ++arg;
2211 usefilter = TRUE;
2212 }
2213 }
2214
2215 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2216 {
2217 while (*arg == *cmd) // allow any number of '>' or '<'
2218 ++arg;
2219 arg = skipwhite(arg);
2220 }
2221
2222 // Does command allow "+command"?
2223 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2224 {
2225 // Check if we're in the +command
2226 p = arg + 1;
2227 arg = skip_cmd_arg(arg, FALSE);
2228
2229 // Still touching the command after '+'?
2230 if (*arg == NUL)
2231 return p;
2232
2233 // Skip space(s) after +command to get to the real argument
2234 arg = skipwhite(arg);
2235 }
2236
2237
2238 // Check for '|' to separate commands and '"' to start comments.
2239 // Don't do this for ":read !cmd" and ":write !cmd".
2240 if ((ea.argt & EX_TRLBAR) && !usefilter)
2241 {
2242 p = arg;
2243 // ":redir @" is not the start of a comment
2244 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2245 p += 2;
2246 while (*p)
2247 {
2248 if (*p == Ctrl_V)
2249 {
2250 if (p[1] != NUL)
2251 ++p;
2252 }
2253 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2254 || *p == '|' || *p == '\n')
2255 {
2256 if (*(p - 1) != '\\')
2257 {
2258 if (*p == '|' || *p == '\n')
2259 return p + 1;
2260 return NULL; // It's a comment
2261 }
2262 }
2263 MB_PTR_ADV(p);
2264 }
2265 }
2266
2267 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2268 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2269 // no arguments allowed but there is something
2270 return NULL;
2271
2272 // Find start of last argument (argument just before cursor):
2273 p = buff;
2274 xp->xp_pattern = p;
2275 len = (int)STRLEN(buff);
2276 while (*p && p < buff + len)
2277 {
2278 if (*p == ' ' || *p == TAB)
2279 {
2280 // argument starts after a space
2281 xp->xp_pattern = ++p;
2282 }
2283 else
2284 {
2285 if (*p == '\\' && *(p + 1) != NUL)
2286 ++p; // skip over escaped character
2287 MB_PTR_ADV(p);
2288 }
2289 }
2290
2291 if (ea.argt & EX_XFILE)
2292 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2293
2294 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002295 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002296 forceit);
2297}
2298
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002299/*
2300 * Set the completion context in 'xp' for command 'str'
2301 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002302 void
2303set_cmd_context(
2304 expand_T *xp,
2305 char_u *str, // start of command line
2306 int len, // length of command line (excl. NUL)
2307 int col, // position of cursor
2308 int use_ccline UNUSED) // use ccline for info
2309{
2310#ifdef FEAT_EVAL
2311 cmdline_info_T *ccline = get_cmdline_info();
2312#endif
2313 int old_char = NUL;
2314 char_u *nextcomm;
2315
2316 // Avoid a UMR warning from Purify, only save the character if it has been
2317 // written before.
2318 if (col < len)
2319 old_char = str[col];
2320 str[col] = NUL;
2321 nextcomm = str;
2322
2323#ifdef FEAT_EVAL
2324 if (use_ccline && ccline->cmdfirstc == '=')
2325 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002326 // pass CMD_SIZE because there is no real command
2327 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002328 }
2329 else if (use_ccline && ccline->input_fn)
2330 {
2331 xp->xp_context = ccline->xp_context;
2332 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002333 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002334 }
2335 else
2336#endif
2337 while (nextcomm != NULL)
2338 nextcomm = set_one_cmd_context(xp, nextcomm);
2339
2340 // Store the string here so that call_user_expand_func() can get to them
2341 // easily.
2342 xp->xp_line = str;
2343 xp->xp_col = col;
2344
2345 str[col] = old_char;
2346}
2347
2348/*
2349 * Expand the command line "str" from context "xp".
2350 * "xp" must have been set by set_cmd_context().
2351 * xp->xp_pattern points into "str", to where the text that is to be expanded
2352 * starts.
2353 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2354 * cursor.
2355 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2356 * key that triggered expansion literally.
2357 * Returns EXPAND_OK otherwise.
2358 */
2359 int
2360expand_cmdline(
2361 expand_T *xp,
2362 char_u *str, // start of command line
2363 int col, // position of cursor
2364 int *matchcount, // return: nr of matches
2365 char_u ***matches) // return: array of pointers to matches
2366{
2367 char_u *file_str = NULL;
2368 int options = WILD_ADD_SLASH|WILD_SILENT;
2369
2370 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2371 {
2372 beep_flush();
2373 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2374 }
2375 if (xp->xp_context == EXPAND_NOTHING)
2376 {
2377 // Caller can use the character as a normal char instead
2378 return EXPAND_NOTHING;
2379 }
2380
2381 // add star to file name, or convert to regexp if not exp. files.
2382 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002383 if (cmdline_fuzzy_completion_supported(xp))
2384 // If fuzzy matching, don't modify the search string
2385 file_str = vim_strsave(xp->xp_pattern);
2386 else
2387 {
2388 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2389 if (file_str == NULL)
2390 return EXPAND_UNSUCCESSFUL;
2391 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002392
2393 if (p_wic)
2394 options += WILD_ICASE;
2395
2396 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002397 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002398 {
2399 *matchcount = 0;
2400 *matches = NULL;
2401 }
2402 vim_free(file_str);
2403
2404 return EXPAND_OK;
2405}
2406
Bram Moolenaar66b51422019-08-18 21:44:12 +02002407/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002408 * Expand file or directory names.
2409 */
2410 static int
2411expand_files_and_dirs(
2412 expand_T *xp,
2413 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002414 char_u ***matches,
2415 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002416 int flags,
2417 int options)
2418{
2419 int free_pat = FALSE;
2420 int i;
2421 int ret;
2422
2423 // for ":set path=" and ":set tags=" halve backslashes for escaped
2424 // space
2425 if (xp->xp_backslash != XP_BS_NONE)
2426 {
2427 free_pat = TRUE;
2428 pat = vim_strsave(pat);
2429 for (i = 0; pat[i]; ++i)
2430 if (pat[i] == '\\')
2431 {
2432 if (xp->xp_backslash == XP_BS_THREE
2433 && pat[i + 1] == '\\'
2434 && pat[i + 2] == '\\'
2435 && pat[i + 3] == ' ')
2436 STRMOVE(pat + i, pat + i + 3);
2437 if (xp->xp_backslash == XP_BS_ONE
2438 && pat[i + 1] == ' ')
2439 STRMOVE(pat + i, pat + i + 1);
2440 }
2441 }
2442
2443 if (xp->xp_context == EXPAND_FILES)
2444 flags |= EW_FILE;
2445 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2446 flags |= (EW_FILE | EW_PATH);
2447 else
2448 flags = (flags | EW_DIR) & ~EW_FILE;
2449 if (options & WILD_ICASE)
2450 flags |= EW_ICASE;
2451
2452 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002453 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002454 if (free_pat)
2455 vim_free(pat);
2456#ifdef BACKSLASH_IN_FILENAME
2457 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2458 {
2459 int j;
2460
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002461 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002462 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002463 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002464
2465 while (*ptr != NUL)
2466 {
2467 if (p_csl[0] == 's' && *ptr == '\\')
2468 *ptr = '/';
2469 else if (p_csl[0] == 'b' && *ptr == '/')
2470 *ptr = '\\';
2471 ptr += (*mb_ptr2len)(ptr);
2472 }
2473 }
2474 }
2475#endif
2476 return ret;
2477}
2478
2479/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002480 * Function given to ExpandGeneric() to obtain the possible arguments of the
2481 * ":behave {mswin,xterm}" command.
2482 */
2483 static char_u *
2484get_behave_arg(expand_T *xp UNUSED, int idx)
2485{
2486 if (idx == 0)
2487 return (char_u *)"mswin";
2488 if (idx == 1)
2489 return (char_u *)"xterm";
2490 return NULL;
2491}
2492
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002493# ifdef FEAT_EVAL
2494/*
2495 * Function given to ExpandGeneric() to obtain the possible arguments of the
2496 * ":breakadd {expr, file, func, here}" command.
2497 * ":breakdel {func, file, here}" command.
2498 */
2499 static char_u *
2500get_breakadd_arg(expand_T *xp UNUSED, int idx)
2501{
2502 char *opts[] = {"expr", "file", "func", "here"};
2503
2504 if (idx >=0 && idx <= 3)
2505 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002506 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002507 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2508 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002509 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2510 {
2511 // breakdel {func, file, here}
2512 if (idx <= 2)
2513 return (char_u *)opts[idx + 1];
2514 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002515 else
2516 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002517 // profdel {func, file}
2518 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002519 return (char_u *)opts[idx + 1];
2520 }
2521 }
2522 return NULL;
2523}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002524
2525/*
2526 * Function given to ExpandGeneric() to obtain the possible arguments for the
2527 * ":scriptnames" command.
2528 */
2529 static char_u *
2530get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2531{
2532 scriptitem_T *si;
2533
2534 if (!SCRIPT_ID_VALID(idx + 1))
2535 return NULL;
2536
2537 si = SCRIPT_ITEM(idx + 1);
2538 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2539 return NameBuff;
2540}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002541#endif
2542
Bram Moolenaard0190392019-08-23 21:17:35 +02002543/*
2544 * Function given to ExpandGeneric() to obtain the possible arguments of the
2545 * ":messages {clear}" command.
2546 */
2547 static char_u *
2548get_messages_arg(expand_T *xp UNUSED, int idx)
2549{
2550 if (idx == 0)
2551 return (char_u *)"clear";
2552 return NULL;
2553}
2554
2555 static char_u *
2556get_mapclear_arg(expand_T *xp UNUSED, int idx)
2557{
2558 if (idx == 0)
2559 return (char_u *)"<buffer>";
2560 return NULL;
2561}
2562
2563/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002564 * Do the expansion based on xp->xp_context and 'rmp'.
2565 */
2566 static int
2567ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002568 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002569 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002570 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002571 char_u ***matches,
2572 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002573{
2574 static struct expgen
2575 {
2576 int context;
2577 char_u *((*func)(expand_T *, int));
2578 int ic;
2579 int escaped;
2580 } tab[] =
2581 {
2582 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2583 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2584 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2585 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2586 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2587 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2588 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2589 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2590 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2591 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002592#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002593 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2594 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2595 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2596 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2597 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002598#endif
2599#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002600 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2601 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002602#endif
2603#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002604 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002605#endif
2606#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002607 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002608#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002609 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2610 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2611 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002612#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002613 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002614#endif
2615#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002616 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002617#endif
2618#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002619 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002620#endif
2621#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002622 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2623 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002624#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002625 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2626 {EXPAND_USER, get_users, TRUE, FALSE},
2627 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002628#ifdef FEAT_EVAL
2629 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002630 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002631#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002632 };
2633 int i;
2634 int ret = FAIL;
2635
2636 // Find a context in the table and call the ExpandGeneric() with the
2637 // right function to do the expansion.
2638 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2639 {
2640 if (xp->xp_context == tab[i].context)
2641 {
2642 if (tab[i].ic)
2643 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002644 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2645 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002646 break;
2647 }
2648 }
2649
2650 return ret;
2651}
2652
2653/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002654 * Map wild expand options to flags for expand_wildcards()
2655 */
2656 static int
2657map_wildopts_to_ewflags(int options)
2658{
2659 int flags;
2660
2661 flags = EW_DIR; // include directories
2662 if (options & WILD_LIST_NOTFOUND)
2663 flags |= EW_NOTFOUND;
2664 if (options & WILD_ADD_SLASH)
2665 flags |= EW_ADDSLASH;
2666 if (options & WILD_KEEP_ALL)
2667 flags |= EW_KEEPALL;
2668 if (options & WILD_SILENT)
2669 flags |= EW_SILENT;
2670 if (options & WILD_NOERROR)
2671 flags |= EW_NOERROR;
2672 if (options & WILD_ALLLINKS)
2673 flags |= EW_ALLLINKS;
2674
2675 return flags;
2676}
2677
2678/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002679 * Do the expansion based on xp->xp_context and "pat".
2680 */
2681 static int
2682ExpandFromContext(
2683 expand_T *xp,
2684 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002685 char_u ***matches,
2686 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002687 int options) // WILD_ flags
2688{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002689 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002690 int ret;
2691 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002692 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002693 int fuzzy = cmdline_fuzzy_complete(pat)
2694 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002695
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002696 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002697
2698 if (xp->xp_context == EXPAND_FILES
2699 || xp->xp_context == EXPAND_DIRECTORIES
2700 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002701 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2702 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002703
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002704 *matches = (char_u **)"";
2705 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002706 if (xp->xp_context == EXPAND_HELP)
2707 {
2708 // With an empty argument we would get all the help tags, which is
2709 // very slow. Get matches for "help" instead.
2710 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002711 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002712 {
2713#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002714 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002715#endif
2716 return OK;
2717 }
2718 return FAIL;
2719 }
2720
Bram Moolenaar66b51422019-08-18 21:44:12 +02002721 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002722 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002723 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002724 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002725 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002726 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002727#ifdef FEAT_DIFF
2728 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002729 return ExpandBufnames(pat, numMatches, matches,
2730 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002731#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002732 if (xp->xp_context == EXPAND_TAGS
2733 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002734 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2735 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002736 if (xp->xp_context == EXPAND_COLORS)
2737 {
2738 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002739 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002740 directories);
2741 }
2742 if (xp->xp_context == EXPAND_COMPILER)
2743 {
2744 char *directories[] = {"compiler", 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_OWNSYNTAX)
2748 {
2749 char *directories[] = {"syntax", 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 (xp->xp_context == EXPAND_FILETYPE)
2753 {
2754 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002755 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002756 }
2757# if defined(FEAT_EVAL)
2758 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002759 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002760# endif
2761 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002762 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002763
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002764 // When expanding a function name starting with s:, match the <SNR>nr_
2765 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002766 if ((xp->xp_context == EXPAND_USER_FUNC
2767 || xp->xp_context == EXPAND_DISASSEMBLE)
2768 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002769 {
2770 int len = (int)STRLEN(pat) + 20;
2771
2772 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002773 if (tofree == NULL)
2774 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002775 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002776 pat = tofree;
2777 }
2778
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002779 if (!fuzzy)
2780 {
2781 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2782 if (regmatch.regprog == NULL)
2783 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002784
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002785 // set ignore-case according to p_ic, p_scs and pat
2786 regmatch.rm_ic = ignorecase(pat);
2787 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002788
2789 if (xp->xp_context == EXPAND_SETTINGS
2790 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002791 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002792 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002793 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002794# if defined(FEAT_EVAL)
2795 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002796 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002797# endif
2798 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002799 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002800
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002801 if (!fuzzy)
2802 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002803 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002804
2805 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002806}
2807
Bram Moolenaar66b51422019-08-18 21:44:12 +02002808/*
2809 * Expand a list of names.
2810 *
2811 * Generic function for command line completion. It calls a function to
2812 * obtain strings, one by one. The strings are matched against a regexp
2813 * program. Matching strings are copied into an array, which is returned.
2814 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002815 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2816 * is used.
2817 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002818 * Returns OK when no problems encountered, FAIL for error (out of memory).
2819 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002820 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002821ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002822 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002823 expand_T *xp,
2824 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002825 char_u ***matches,
2826 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002827 char_u *((*func)(expand_T *, int)),
2828 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002829 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002830{
2831 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002832 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002833 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002834 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002835 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002836 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002837 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002838 int sort_matches = FALSE;
2839 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002840
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002841 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002842 *matches = NULL;
2843 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002844
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002845 if (!fuzzy)
2846 ga_init2(&ga, sizeof(char *), 30);
2847 else
2848 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2849
2850 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002851 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002852 str = (*func)(xp, i);
2853 if (str == NULL) // end of list
2854 break;
2855 if (*str == NUL) // skip empty strings
2856 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002857
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002858 if (xp->xp_pattern[0] != NUL)
2859 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002860 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002861 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002862 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002863 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002864 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002865 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002866 }
2867 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002868 else
2869 match = TRUE;
2870
2871 if (!match)
2872 continue;
2873
2874 if (escaped)
2875 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2876 else
2877 str = vim_strsave(str);
2878 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002879 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002880 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002881 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002882 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002883 return FAIL;
2884 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01002885 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002886 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002887 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002888
2889 if (ga_grow(&ga, 1) == FAIL)
2890 {
2891 vim_free(str);
2892 break;
2893 }
2894
2895 if (fuzzy)
2896 {
2897 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2898 fuzmatch->idx = ga.ga_len;
2899 fuzmatch->str = str;
2900 fuzmatch->score = score;
2901 }
2902 else
2903 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2904
2905# ifdef FEAT_MENU
2906 if (func == get_menu_names)
2907 {
2908 // test for separator added by get_menu_names()
2909 str += STRLEN(str) - 1;
2910 if (*str == '\001')
2911 *str = '.';
2912 }
2913# endif
2914
2915 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002916 }
2917
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002918 if (ga.ga_len == 0)
2919 return OK;
2920
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002921 // sort the matches when using regular expression matching and sorting
2922 // applies to the completion context. Menus and scriptnames should be kept
2923 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002924 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002925 && xp->xp_context != EXPAND_MENUS
2926 && xp->xp_context != EXPAND_SCRIPTNAMES)
2927 sort_matches = TRUE;
2928
2929 // <SNR> functions should be sorted to the end.
2930 if (xp->xp_context == EXPAND_EXPRESSION
2931 || xp->xp_context == EXPAND_FUNCTIONS
2932 || xp->xp_context == EXPAND_USER_FUNC
2933 || xp->xp_context == EXPAND_DISASSEMBLE)
2934 funcsort = TRUE;
2935
2936 // Sort the matches.
2937 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002938 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002939 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002940 // <SNR> functions should be sorted to the end.
2941 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2942 sort_func_compare);
2943 else
2944 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2945 }
2946
2947 if (!fuzzy)
2948 {
2949 *matches = ga.ga_data;
2950 *numMatches = ga.ga_len;
2951 }
2952 else
2953 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002954 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2955 funcsort) == FAIL)
2956 return FAIL;
2957 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002958 }
2959
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002960#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002961 // Reset the variables used for special highlight names expansion, so that
2962 // they don't show up when getting normal highlight names by ID.
2963 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002964#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002965
Bram Moolenaar66b51422019-08-18 21:44:12 +02002966 return OK;
2967}
2968
2969/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002970 * Expand shell command matches in one directory of $PATH.
2971 */
2972 static void
2973expand_shellcmd_onedir(
2974 char_u *buf,
2975 char_u *s,
2976 size_t l,
2977 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002978 char_u ***matches,
2979 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002980 int flags,
2981 hashtab_T *ht,
2982 garray_T *gap)
2983{
2984 int ret;
2985 int i;
2986 hash_T hash;
2987 hashitem_T *hi;
2988
2989 vim_strncpy(buf, s, l);
2990 add_pathsep(buf);
2991 l = STRLEN(buf);
2992 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2993
2994 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002995 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002996 if (ret == OK)
2997 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002998 if (ga_grow(gap, *numMatches) == FAIL)
2999 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003000 else
3001 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003002 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003003 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003004 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003005
3006 if (STRLEN(name) > l)
3007 {
3008 // Check if this name was already found.
3009 hash = hash_hash(name + l);
3010 hi = hash_lookup(ht, name + l, hash);
3011 if (HASHITEM_EMPTY(hi))
3012 {
3013 // Remove the path that was prepended.
3014 STRMOVE(name, name + l);
3015 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3016 hash_add_item(ht, hi, name, hash);
3017 name = NULL;
3018 }
3019 }
3020 vim_free(name);
3021 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003022 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003023 }
3024 }
3025}
3026
3027/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003028 * Complete a shell command.
3029 * Returns FAIL or OK;
3030 */
3031 static int
3032expand_shellcmd(
3033 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003034 char_u ***matches, // return: array with matches
3035 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003036 int flagsarg) // EW_ flags
3037{
3038 char_u *pat;
3039 int i;
3040 char_u *path = NULL;
3041 int mustfree = FALSE;
3042 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003043 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003044 size_t l;
3045 char_u *s, *e;
3046 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003047 int did_curdir = FALSE;
3048 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003049
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003050 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003051 if (buf == NULL)
3052 return FAIL;
3053
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003054 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003055 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003056 if (pat == NULL)
3057 {
3058 vim_free(buf);
3059 return FAIL;
3060 }
3061
Bram Moolenaar66b51422019-08-18 21:44:12 +02003062 for (i = 0; pat[i]; ++i)
3063 if (pat[i] == '\\' && pat[i + 1] == ' ')
3064 STRMOVE(pat + i, pat + i + 1);
3065
3066 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3067
3068 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3069 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3070 path = (char_u *)".";
3071 else
3072 {
3073 // For an absolute name we don't use $PATH.
3074 if (!mch_isFullName(pat))
3075 path = vim_getenv((char_u *)"PATH", &mustfree);
3076 if (path == NULL)
3077 path = (char_u *)"";
3078 }
3079
3080 // Go over all directories in $PATH. Expand matches in that directory and
3081 // collect them in "ga". When "." is not in $PATH also expand for the
3082 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003083 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003084 hash_init(&found_ht);
3085 for (s = path; ; s = e)
3086 {
3087# if defined(MSWIN)
3088 e = vim_strchr(s, ';');
3089# else
3090 e = vim_strchr(s, ':');
3091# endif
3092 if (e == NULL)
3093 e = s + STRLEN(s);
3094
3095 if (*s == NUL)
3096 {
3097 if (did_curdir)
3098 break;
3099 // Find directories in the current directory, path is empty.
3100 did_curdir = TRUE;
3101 flags |= EW_DIR;
3102 }
3103 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3104 {
3105 did_curdir = TRUE;
3106 flags |= EW_DIR;
3107 }
3108 else
3109 // Do not match directories inside a $PATH item.
3110 flags &= ~EW_DIR;
3111
3112 l = e - s;
3113 if (l > MAXPATHL - 5)
3114 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003116 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003117 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003118
Bram Moolenaar66b51422019-08-18 21:44:12 +02003119 if (*e != NUL)
3120 ++e;
3121 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003122 *matches = ga.ga_data;
3123 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003124
3125 vim_free(buf);
3126 vim_free(pat);
3127 if (mustfree)
3128 vim_free(path);
3129 hash_clear(&found_ht);
3130 return OK;
3131}
3132
3133# if defined(FEAT_EVAL)
3134/*
3135 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003136 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137 */
3138 static void *
3139call_user_expand_func(
3140 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003141 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003142{
3143 cmdline_info_T *ccline = get_cmdline_info();
3144 int keep = 0;
3145 typval_T args[4];
3146 sctx_T save_current_sctx = current_sctx;
3147 char_u *pat = NULL;
3148 void *ret;
3149
3150 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3151 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003152
3153 if (ccline->cmdbuff != NULL)
3154 {
3155 keep = ccline->cmdbuff[ccline->cmdlen];
3156 ccline->cmdbuff[ccline->cmdlen] = 0;
3157 }
3158
3159 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3160
3161 args[0].v_type = VAR_STRING;
3162 args[0].vval.v_string = pat;
3163 args[1].v_type = VAR_STRING;
3164 args[1].vval.v_string = xp->xp_line;
3165 args[2].v_type = VAR_NUMBER;
3166 args[2].vval.v_number = xp->xp_col;
3167 args[3].v_type = VAR_UNKNOWN;
3168
3169 current_sctx = xp->xp_script_ctx;
3170
3171 ret = user_expand_func(xp->xp_arg, 3, args);
3172
3173 current_sctx = save_current_sctx;
3174 if (ccline->cmdbuff != NULL)
3175 ccline->cmdbuff[ccline->cmdlen] = keep;
3176
3177 vim_free(pat);
3178 return ret;
3179}
3180
3181/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003182 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3183 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003184 */
3185 static int
3186ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003187 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003188 expand_T *xp,
3189 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003190 char_u ***matches,
3191 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003192{
3193 char_u *retstr;
3194 char_u *s;
3195 char_u *e;
3196 int keep;
3197 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003198 int fuzzy;
3199 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003200 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003201
3202 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003203 *matches = NULL;
3204 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003205
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003206 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003207 if (retstr == NULL)
3208 return FAIL;
3209
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003210 if (!fuzzy)
3211 ga_init2(&ga, sizeof(char *), 3);
3212 else
3213 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3214
Bram Moolenaar66b51422019-08-18 21:44:12 +02003215 for (s = retstr; *s != NUL; s = e)
3216 {
3217 e = vim_strchr(s, '\n');
3218 if (e == NULL)
3219 e = s + STRLEN(s);
3220 keep = *e;
3221 *e = NUL;
3222
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003223 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003224 {
3225 if (!fuzzy)
3226 match = vim_regexec(regmatch, s, (colnr_T)0);
3227 else
3228 {
3229 score = fuzzy_match_str(s, pat);
3230 match = (score != 0);
3231 }
3232 }
3233 else
3234 match = TRUE; // match everything
3235
Bram Moolenaar66b51422019-08-18 21:44:12 +02003236 *e = keep;
3237
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003238 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003239 {
3240 if (ga_grow(&ga, 1) == FAIL)
3241 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003242 if (!fuzzy)
3243 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3244 else
3245 {
3246 fuzmatch_str_T *fuzmatch =
3247 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003248 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003249 fuzmatch->str = vim_strnsave(s, e - s);
3250 fuzmatch->score = score;
3251 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003252 ++ga.ga_len;
3253 }
3254
3255 if (*e != NUL)
3256 ++e;
3257 }
3258 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003259
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003260 if (ga.ga_len == 0)
3261 return OK;
3262
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003263 if (!fuzzy)
3264 {
3265 *matches = ga.ga_data;
3266 *numMatches = ga.ga_len;
3267 }
3268 else
3269 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003270 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3271 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003272 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003273 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003274 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003275 return OK;
3276}
3277
3278/*
3279 * Expand names with a list returned by a function defined by the user.
3280 */
3281 static int
3282ExpandUserList(
3283 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003284 char_u ***matches,
3285 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003286{
3287 list_T *retlist;
3288 listitem_T *li;
3289 garray_T ga;
3290
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003291 *matches = NULL;
3292 *numMatches = 0;
3293 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003294 if (retlist == NULL)
3295 return FAIL;
3296
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003297 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003298 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003299 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003300 {
3301 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3302 continue; // Skip non-string items and empty strings
3303
3304 if (ga_grow(&ga, 1) == FAIL)
3305 break;
3306
3307 ((char_u **)ga.ga_data)[ga.ga_len] =
3308 vim_strsave(li->li_tv.vval.v_string);
3309 ++ga.ga_len;
3310 }
3311 list_unref(retlist);
3312
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003313 *matches = ga.ga_data;
3314 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003315 return OK;
3316}
3317# endif
3318
3319/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003320 * Expand "file" for all comma-separated directories in "path".
3321 * Adds the matches to "ga". Caller must init "ga".
3322 */
3323 void
3324globpath(
3325 char_u *path,
3326 char_u *file,
3327 garray_T *ga,
3328 int expand_options)
3329{
3330 expand_T xpc;
3331 char_u *buf;
3332 int i;
3333 int num_p;
3334 char_u **p;
3335
3336 buf = alloc(MAXPATHL);
3337 if (buf == NULL)
3338 return;
3339
3340 ExpandInit(&xpc);
3341 xpc.xp_context = EXPAND_FILES;
3342
3343 // Loop over all entries in {path}.
3344 while (*path != NUL)
3345 {
3346 // Copy one item of the path to buf[] and concatenate the file name.
3347 copy_option_part(&path, buf, MAXPATHL, ",");
3348 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3349 {
3350# if defined(MSWIN)
3351 // Using the platform's path separator (\) makes vim incorrectly
3352 // treat it as an escape character, use '/' instead.
3353 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3354 STRCAT(buf, "/");
3355# else
3356 add_pathsep(buf);
3357# endif
3358 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003359 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3361 {
3362 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3363
3364 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003365 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003366 for (i = 0; i < num_p; ++i)
3367 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003368 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003369 ++ga->ga_len;
3370 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003371 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003372 }
3373 }
3374 }
3375
3376 vim_free(buf);
3377}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003378
Bram Moolenaareadee482020-09-04 15:37:31 +02003379#ifdef FEAT_WILDMENU
3380
3381/*
3382 * Translate some keys pressed when 'wildmenu' is used.
3383 */
3384 int
3385wildmenu_translate_key(
3386 cmdline_info_T *cclp,
3387 int key,
3388 expand_T *xp,
3389 int did_wild_list)
3390{
3391 int c = key;
3392
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003393#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003394 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003395 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003396 // When the popup menu is used for cmdline completion:
3397 // Up : go to the previous item in the menu
3398 // Down : go to the next item in the menu
3399 // Left : go to the parent directory
3400 // Right: list the files in the selected directory
3401 switch (c)
3402 {
3403 case K_UP: c = K_LEFT; break;
3404 case K_DOWN: c = K_RIGHT; break;
3405 case K_LEFT: c = K_UP; break;
3406 case K_RIGHT: c = K_DOWN; break;
3407 default: break;
3408 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003409 }
3410#endif
3411
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003412 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003413 {
3414 if (c == K_LEFT)
3415 c = Ctrl_P;
3416 else if (c == K_RIGHT)
3417 c = Ctrl_N;
3418 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003419
Bram Moolenaareadee482020-09-04 15:37:31 +02003420 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003421 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003422 && cclp->cmdpos > 1
3423 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3424 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3425 && (c == '\n' || c == '\r' || c == K_KENTER))
3426 c = K_DOWN;
3427
3428 return c;
3429}
3430
3431/*
3432 * Delete characters on the command line, from "from" to the current
3433 * position.
3434 */
3435 static void
3436cmdline_del(cmdline_info_T *cclp, int from)
3437{
3438 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3439 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3440 cclp->cmdlen -= cclp->cmdpos - from;
3441 cclp->cmdpos = from;
3442}
3443
3444/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003445 * Handle a key pressed when the wild menu for the menu names
3446 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003447 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003448 static int
3449wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003450{
Bram Moolenaareadee482020-09-04 15:37:31 +02003451 int i;
3452 int j;
3453
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003454 // Hitting <Down> after "emenu Name.": complete submenu
3455 if (key == K_DOWN && cclp->cmdpos > 0
3456 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003457 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003458 key = p_wc;
3459 KeyTyped = TRUE; // in case the key was mapped
3460 }
3461 else if (key == K_UP)
3462 {
3463 // Hitting <Up>: Remove one submenu name in front of the
3464 // cursor
3465 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003466
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003467 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3468 i = 0;
3469 while (--j > 0)
3470 {
3471 // check for start of menu name
3472 if (cclp->cmdbuff[j] == ' '
3473 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003474 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003475 i = j + 1;
3476 break;
3477 }
3478 // check for start of submenu name
3479 if (cclp->cmdbuff[j] == '.'
3480 && cclp->cmdbuff[j - 1] != '\\')
3481 {
3482 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003483 {
3484 i = j + 1;
3485 break;
3486 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003487 else
3488 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003489 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003490 }
3491 if (i > 0)
3492 cmdline_del(cclp, i);
3493 key = p_wc;
3494 KeyTyped = TRUE; // in case the key was mapped
3495 xp->xp_context = EXPAND_NOTHING;
3496 }
3497
3498 return key;
3499}
3500
3501/*
3502 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3503 * directory names (EXPAND_DIRECTORIES) or shell command names
3504 * (EXPAND_SHELLCMD) is displayed.
3505 */
3506 static int
3507wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3508{
3509 int i;
3510 int j;
3511 char_u upseg[5];
3512
3513 upseg[0] = PATHSEP;
3514 upseg[1] = '.';
3515 upseg[2] = '.';
3516 upseg[3] = PATHSEP;
3517 upseg[4] = NUL;
3518
3519 if (key == K_DOWN
3520 && cclp->cmdpos > 0
3521 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3522 && (cclp->cmdpos < 3
3523 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3524 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3525 {
3526 // go down a directory
3527 key = p_wc;
3528 KeyTyped = TRUE; // in case the key was mapped
3529 }
3530 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3531 {
3532 // If in a direct ancestor, strip off one ../ to go down
3533 int found = FALSE;
3534
3535 j = cclp->cmdpos;
3536 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3537 while (--j > i)
3538 {
3539 if (has_mbyte)
3540 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3541 if (vim_ispathsep(cclp->cmdbuff[j]))
3542 {
3543 found = TRUE;
3544 break;
3545 }
3546 }
3547 if (found
3548 && cclp->cmdbuff[j - 1] == '.'
3549 && cclp->cmdbuff[j - 2] == '.'
3550 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3551 {
3552 cmdline_del(cclp, j - 2);
3553 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003554 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003555 }
3556 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003557 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003558 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003559 // go up a directory
3560 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003561
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003562 j = cclp->cmdpos - 1;
3563 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3564 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003565 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003566 if (has_mbyte)
3567 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3568 if (vim_ispathsep(cclp->cmdbuff[j])
3569# ifdef BACKSLASH_IN_FILENAME
3570 && vim_strchr((char_u *)" *?[{`$%#",
3571 cclp->cmdbuff[j + 1]) == NULL
3572# endif
3573 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003574 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003575 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003576 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003577 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003578 break;
3579 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003580 else
3581 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003582 }
3583 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003584
3585 if (!found)
3586 j = i;
3587 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3588 j += 4;
3589 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3590 && j == i)
3591 j += 3;
3592 else
3593 j = 0;
3594 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003595 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003596 // TODO this is only for DOS/UNIX systems - need to put in
3597 // machine-specific stuff here and in upseg init
3598 cmdline_del(cclp, j);
3599 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003600 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003601 else if (cclp->cmdpos > i)
3602 cmdline_del(cclp, i);
3603
3604 // Now complete in the new directory. Set KeyTyped in case the
3605 // Up key came from a mapping.
3606 key = p_wc;
3607 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003608 }
3609
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003610 return key;
3611}
3612
3613/*
3614 * Handle a key pressed when the wild menu is displayed
3615 */
3616 int
3617wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3618{
3619 if (xp->xp_context == EXPAND_MENUNAMES)
3620 return wildmenu_process_key_menunames(cclp, key, xp);
3621 else if ((xp->xp_context == EXPAND_FILES
3622 || xp->xp_context == EXPAND_DIRECTORIES
3623 || xp->xp_context == EXPAND_SHELLCMD))
3624 return wildmenu_process_key_filenames(cclp, key, xp);
3625
3626 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003627}
3628
3629/*
3630 * Free expanded names when finished walking through the matches
3631 */
3632 void
3633wildmenu_cleanup(cmdline_info_T *cclp)
3634{
3635 int skt = KeyTyped;
3636 int old_RedrawingDisabled = RedrawingDisabled;
3637
3638 if (!p_wmnu || wild_menu_showing == 0)
3639 return;
3640
3641 if (cclp->input_fn)
3642 RedrawingDisabled = 0;
3643
3644 if (wild_menu_showing == WM_SCROLLED)
3645 {
3646 // Entered command line, move it up
3647 cmdline_row--;
3648 redrawcmd();
3649 }
3650 else if (save_p_ls != -1)
3651 {
3652 // restore 'laststatus' and 'winminheight'
3653 p_ls = save_p_ls;
3654 p_wmh = save_p_wmh;
3655 last_status(FALSE);
3656 update_screen(VALID); // redraw the screen NOW
3657 redrawcmd();
3658 save_p_ls = -1;
3659 }
3660 else
3661 {
3662 win_redraw_last_status(topframe);
3663 redraw_statuslines();
3664 }
3665 KeyTyped = skt;
3666 wild_menu_showing = 0;
3667 if (cclp->input_fn)
3668 RedrawingDisabled = old_RedrawingDisabled;
3669}
3670#endif
3671
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003672#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003673/*
3674 * "getcompletion()" function
3675 */
3676 void
3677f_getcompletion(typval_T *argvars, typval_T *rettv)
3678{
3679 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003680 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003681 expand_T xpc;
3682 int filtered = FALSE;
3683 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003684 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003685
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003686 if (in_vim9script()
3687 && (check_for_string_arg(argvars, 0) == FAIL
3688 || check_for_string_arg(argvars, 1) == FAIL
3689 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3690 return;
3691
ii144785fe02021-11-21 12:13:56 +00003692 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003693 if (argvars[1].v_type != VAR_STRING)
3694 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003695 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003696 return;
3697 }
3698 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003699
3700 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003701 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003702
3703 if (p_wic)
3704 options |= WILD_ICASE;
3705
3706 // For filtered results, 'wildignore' is used
3707 if (!filtered)
3708 options |= WILD_KEEP_ALL;
3709
3710 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003711 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003712 {
ii144785fe02021-11-21 12:13:56 +00003713 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003714 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003715 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003716 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003717 else
3718 {
ii144785fe02021-11-21 12:13:56 +00003719 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003720 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3721
3722 xpc.xp_context = cmdcomplete_str_to_type(type);
3723 if (xpc.xp_context == EXPAND_NOTHING)
3724 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003725 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003726 return;
3727 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003728
3729# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003730 if (xpc.xp_context == EXPAND_MENUS)
3731 {
3732 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3733 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3734 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003735# endif
3736# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003737 if (xpc.xp_context == EXPAND_CSCOPE)
3738 {
3739 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3740 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3741 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003742# endif
3743# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003744 if (xpc.xp_context == EXPAND_SIGN)
3745 {
3746 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3747 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3748 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003749# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003750 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003751
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00003752 if (cmdline_fuzzy_completion_supported(&xpc))
3753 // when fuzzy matching, don't modify the search string
3754 pat = vim_strsave(xpc.xp_pattern);
3755 else
3756 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3757
Bram Moolenaar66b51422019-08-18 21:44:12 +02003758 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3759 {
3760 int i;
3761
3762 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3763
3764 for (i = 0; i < xpc.xp_numfiles; i++)
3765 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3766 }
3767 vim_free(pat);
3768 ExpandCleanup(&xpc);
3769}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003770#endif // FEAT_EVAL