blob: ff6aa0d0339e782660541a1e627b6e4c6197131e [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000018static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000020 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaar66b51422019-08-18 21:44:12 +020022static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000023static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020024#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000025static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000026static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020027#endif
28
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000029#ifdef FEAT_WILDMENU
30// "compl_match_array" points the currently displayed list of entries in the
31// popup menu. It is NULL when there is no popup menu.
32static pumitem_T *compl_match_array = NULL;
33static int compl_match_arraysize;
34// First column in cmdline of the matched item for completion.
35static int compl_startcol;
36static int compl_selected;
37#endif
38
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000039#define SHOW_FILE_TEXT(m) (showtail ? sm_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000040
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000041/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000042 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
43 * context.
44 */
45 static int
46cmdline_fuzzy_completion_supported(expand_T *xp)
47{
48 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
49 && xp->xp_context != EXPAND_BOOL_SETTINGS
50 && xp->xp_context != EXPAND_COLORS
51 && xp->xp_context != EXPAND_COMPILER
52 && xp->xp_context != EXPAND_DIRECTORIES
53 && xp->xp_context != EXPAND_FILES
54 && xp->xp_context != EXPAND_FILES_IN_PATH
55 && xp->xp_context != EXPAND_FILETYPE
56 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000057 && xp->xp_context != EXPAND_OLD_SETTING
58 && xp->xp_context != EXPAND_OWNSYNTAX
59 && xp->xp_context != EXPAND_PACKADD
60 && xp->xp_context != EXPAND_SHELLCMD
61 && xp->xp_context != EXPAND_TAGS
62 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_USER_LIST);
64}
65
66/*
67 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000068 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
69 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000070 */
71 int
72cmdline_fuzzy_complete(char_u *fuzzystr)
73{
74 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
75}
76
77/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000078 * sort function for the completion matches.
79 * <SNR> functions should be sorted to the end.
80 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020081 static int
82sort_func_compare(const void *s1, const void *s2)
83{
84 char_u *p1 = *(char_u **)s1;
85 char_u *p2 = *(char_u **)s2;
86
87 if (*p1 != '<' && *p2 == '<') return -1;
88 if (*p1 == '<' && *p2 != '<') return 1;
89 return STRCMP(p1, p2);
90}
Bram Moolenaar66b51422019-08-18 21:44:12 +020091
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000092/*
93 * Escape special characters in the cmdline completion matches.
94 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020095 static void
96ExpandEscape(
97 expand_T *xp,
98 char_u *str,
99 int numfiles,
100 char_u **files,
101 int options)
102{
103 int i;
104 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100105 int vse_what = xp->xp_context == EXPAND_BUFFERS
106 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200107
108 // May change home directory back to "~"
109 if (options & WILD_HOME_REPLACE)
110 tilde_replace(str, numfiles, files);
111
112 if (options & WILD_ESCAPE)
113 {
114 if (xp->xp_context == EXPAND_FILES
115 || xp->xp_context == EXPAND_FILES_IN_PATH
116 || xp->xp_context == EXPAND_SHELLCMD
117 || xp->xp_context == EXPAND_BUFFERS
118 || xp->xp_context == EXPAND_DIRECTORIES)
119 {
120 // Insert a backslash into a file name before a space, \, %, #
121 // and wildmatch characters, except '~'.
122 for (i = 0; i < numfiles; ++i)
123 {
124 // for ":set path=" we need to escape spaces twice
125 if (xp->xp_backslash == XP_BS_THREE)
126 {
127 p = vim_strsave_escaped(files[i], (char_u *)" ");
128 if (p != NULL)
129 {
130 vim_free(files[i]);
131 files[i] = p;
132#if defined(BACKSLASH_IN_FILENAME)
133 p = vim_strsave_escaped(files[i], (char_u *)" ");
134 if (p != NULL)
135 {
136 vim_free(files[i]);
137 files[i] = p;
138 }
139#endif
140 }
141 }
142#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100143 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200144#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100145 p = vim_strsave_fnameescape(files[i],
146 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200147#endif
148 if (p != NULL)
149 {
150 vim_free(files[i]);
151 files[i] = p;
152 }
153
154 // If 'str' starts with "\~", replace "~" at start of
155 // files[i] with "\~".
156 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
157 escape_fname(&files[i]);
158 }
159 xp->xp_backslash = XP_BS_NONE;
160
161 // If the first file starts with a '+' escape it. Otherwise it
162 // could be seen as "+cmd".
163 if (*files[0] == '+')
164 escape_fname(&files[0]);
165 }
166 else if (xp->xp_context == EXPAND_TAGS)
167 {
168 // Insert a backslash before characters in a tag name that
169 // would terminate the ":tag" command.
170 for (i = 0; i < numfiles; ++i)
171 {
172 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
173 if (p != NULL)
174 {
175 vim_free(files[i]);
176 files[i] = p;
177 }
178 }
179 }
180 }
181}
182
183/*
184 * Return FAIL if this is not an appropriate context in which to do
185 * completion of anything, return OK if it is (even if there are no matches).
186 * For the caller, this means that the character is just passed through like a
187 * normal character (instead of being expanded). This allows :s/^I^D etc.
188 */
189 int
190nextwild(
191 expand_T *xp,
192 int type,
193 int options, // extra options for ExpandOne()
194 int escape) // if TRUE, escape the returned matches
195{
196 cmdline_info_T *ccline = get_cmdline_info();
197 int i, j;
198 char_u *p1;
199 char_u *p2;
200 int difflen;
201 int v;
202
203 if (xp->xp_numfiles == -1)
204 {
205 set_expand_context(xp);
206 cmd_showtail = expand_showtail(xp);
207 }
208
209 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
210 {
211 beep_flush();
212 return OK; // Something illegal on command line
213 }
214 if (xp->xp_context == EXPAND_NOTHING)
215 {
216 // Caller can use the character as a normal char instead
217 return FAIL;
218 }
219
220 msg_puts("..."); // show that we are busy
221 out_flush();
222
223 i = (int)(xp->xp_pattern - ccline->cmdbuff);
224 xp->xp_pattern_len = ccline->cmdpos - i;
225
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000226 if (type == WILD_NEXT || type == WILD_PREV
227 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200228 {
229 // Get next/previous match for a previous expanded pattern.
230 p2 = ExpandOne(xp, NULL, NULL, 0, type);
231 }
232 else
233 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000234 if (cmdline_fuzzy_completion_supported(xp))
235 // If fuzzy matching, don't modify the search string
236 p1 = vim_strsave(xp->xp_pattern);
237 else
238 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
239
Bram Moolenaar66b51422019-08-18 21:44:12 +0200240 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000241 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200242 p2 = NULL;
243 else
244 {
245 int use_options = options |
246 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
247 if (escape)
248 use_options |= WILD_ESCAPE;
249
250 if (p_wic)
251 use_options += WILD_ICASE;
252 p2 = ExpandOne(xp, p1,
253 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
254 use_options, type);
255 vim_free(p1);
256 // longest match: make sure it is not shorter, happens with :help
257 if (p2 != NULL && type == WILD_LONGEST)
258 {
259 for (j = 0; j < xp->xp_pattern_len; ++j)
260 if (ccline->cmdbuff[i + j] == '*'
261 || ccline->cmdbuff[i + j] == '?')
262 break;
263 if ((int)STRLEN(p2) < j)
264 VIM_CLEAR(p2);
265 }
266 }
267 }
268
269 if (p2 != NULL && !got_int)
270 {
271 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
272 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
273 {
274 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
275 xp->xp_pattern = ccline->cmdbuff + i;
276 }
277 else
278 v = OK;
279 if (v == OK)
280 {
281 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
282 &ccline->cmdbuff[ccline->cmdpos],
283 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
284 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
285 ccline->cmdlen += difflen;
286 ccline->cmdpos += difflen;
287 }
288 }
289 vim_free(p2);
290
291 redrawcmd();
292 cursorcmd();
293
294 // When expanding a ":map" command and no matches are found, assume that
295 // the key is supposed to be inserted literally
296 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
297 return FAIL;
298
299 if (xp->xp_numfiles <= 0 && p2 == NULL)
300 beep_flush();
301 else if (xp->xp_numfiles == 1)
302 // free expanded pattern
303 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
304
305 return OK;
306}
307
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000308#if defined(FEAT_WILDMENU) || defined(PROTO)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000309
310/*
311 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000312 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000313 */
314 static int
315cmdline_pum_create(
316 cmdline_info_T *ccline,
317 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000318 char_u **matches,
319 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000320 int showtail)
321{
322 int i;
323 int columns;
324
325 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000326 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000327 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000328 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000329 {
330 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
331 compl_match_array[i].pum_info = NULL;
332 compl_match_array[i].pum_extra = NULL;
333 compl_match_array[i].pum_kind = NULL;
334 }
335
336 // Compute the popup menu starting column
337 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
338 columns = vim_strsize(xp->xp_pattern);
339 if (showtail)
340 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000341 columns += vim_strsize(sm_gettail(matches[0]));
342 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000343 }
344 if (columns >= compl_startcol)
345 compl_startcol = 0;
346 else
347 compl_startcol -= columns;
348
349 // no default selection
350 compl_selected = -1;
351
352 cmdline_pum_display();
353
354 return EXPAND_OK;
355}
356
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000357/*
358 * Display the cmdline completion matches in a popup menu
359 */
360void cmdline_pum_display(void)
361{
362 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
363}
364
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000365/*
366 * Returns TRUE if the cmdline completion popup menu is being displayed.
367 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000368int cmdline_pum_active(void)
369{
370 return p_wmnu && pum_visible() && compl_match_array != NULL;
371}
372
373/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000374 * Remove the cmdline completion popup menu (if present), free the list of
375 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000376 */
377void cmdline_pum_remove(void)
378{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000379 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100380 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000381
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000382 pum_undisplay();
383 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000384 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000385 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000386 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000387 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100388
389 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
390 // as a side effect.
391 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000392}
393
394void cmdline_pum_cleanup(cmdline_info_T *cclp)
395{
396 cmdline_pum_remove();
397 wildmenu_cleanup(cclp);
398}
399
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000400/*
401 * Returns the starting column number to use for the cmdline completion popup
402 * menu.
403 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000404int cmdline_compl_startcol(void)
405{
406 return compl_startcol;
407}
408#endif
409
Bram Moolenaar66b51422019-08-18 21:44:12 +0200410/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000411 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000412 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000413 */
414 static char_u *
415get_next_or_prev_match(
416 int mode,
417 expand_T *xp,
418 int *p_findex,
419 char_u *orig_save)
420{
421 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000422 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000423
424 if (xp->xp_numfiles <= 0)
425 return NULL;
426
427 if (mode == WILD_PREV)
428 {
429 if (findex == -1)
430 findex = xp->xp_numfiles;
431 --findex;
432 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000433 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000434 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000435 else if (mode == WILD_PAGEUP)
436 {
437 if (findex == 0)
438 // at the first entry, don't select any entries
439 findex = -1;
440 else if (findex == -1)
441 // no entry is selected. select the last entry
442 findex = xp->xp_numfiles - 1;
443 else
444 {
445 // go up by the pum height
446 ht = pum_get_height();
447 if (ht > 3)
448 ht -= 2;
449 findex -= ht;
450 if (findex < 0)
451 // few entries left, select the first entry
452 findex = 0;
453 }
454 }
455 else // mode == WILD_PAGEDOWN
456 {
457 if (findex == xp->xp_numfiles - 1)
458 // at the last entry, don't select any entries
459 findex = -1;
460 else if (findex == -1)
461 // no entry is selected. select the first entry
462 findex = 0;
463 else
464 {
465 // go down by the pum height
466 ht = pum_get_height();
467 if (ht > 3)
468 ht -= 2;
469 findex += ht;
470 if (findex >= xp->xp_numfiles)
471 // few entries left, select the last entry
472 findex = xp->xp_numfiles - 1;
473 }
474 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000475
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000476 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000477 if (findex < 0)
478 {
479 if (orig_save == NULL)
480 findex = xp->xp_numfiles - 1;
481 else
482 findex = -1;
483 }
484 if (findex >= xp->xp_numfiles)
485 {
486 if (orig_save == NULL)
487 findex = 0;
488 else
489 findex = -1;
490 }
491#ifdef FEAT_WILDMENU
492 if (compl_match_array)
493 {
494 compl_selected = findex;
495 cmdline_pum_display();
496 }
497 else if (p_wmnu)
498 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
499 findex, cmd_showtail);
500#endif
501 *p_findex = findex;
502
503 if (findex == -1)
504 return vim_strsave(orig_save);
505
506 return vim_strsave(xp->xp_files[findex]);
507}
508
509/*
510 * Start the command-line expansion and get the matches.
511 */
512 static char_u *
513ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
514{
515 int non_suf_match; // number without matching suffix
516 int i;
517 char_u *ss = NULL;
518
519 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000520 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000521 options) == FAIL)
522 {
523#ifdef FNAME_ILLEGAL
524 // Illegal file name has been silently skipped. But when there
525 // are wildcards, the real problem is that there was no match,
526 // causing the pattern to be added, which has illegal characters.
527 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
528 semsg(_(e_no_match_str_2), str);
529#endif
530 }
531 else if (xp->xp_numfiles == 0)
532 {
533 if (!(options & WILD_SILENT))
534 semsg(_(e_no_match_str_2), str);
535 }
536 else
537 {
538 // Escape the matches for use on the command line.
539 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
540
541 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000542 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000543 {
544 if (xp->xp_numfiles)
545 non_suf_match = xp->xp_numfiles;
546 else
547 non_suf_match = 1;
548 if ((xp->xp_context == EXPAND_FILES
549 || xp->xp_context == EXPAND_DIRECTORIES)
550 && xp->xp_numfiles > 1)
551 {
552 // More than one match; check suffix.
553 // The files will have been sorted on matching suffix in
554 // expand_wildcards, only need to check the first two.
555 non_suf_match = 0;
556 for (i = 0; i < 2; ++i)
557 if (match_suffix(xp->xp_files[i]))
558 ++non_suf_match;
559 }
560 if (non_suf_match != 1)
561 {
562 // Can we ever get here unless it's while expanding
563 // interactively? If not, we can get rid of this all
564 // together. Don't really want to wait for this message
565 // (and possibly have to hit return to continue!).
566 if (!(options & WILD_SILENT))
567 emsg(_(e_too_many_file_names));
568 else if (!(options & WILD_NO_BEEP))
569 beep_flush();
570 }
571 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
572 ss = vim_strsave(xp->xp_files[0]);
573 }
574 }
575
576 return ss;
577}
578
579/*
580 * Return the longest common part in the list of cmdline completion matches.
581 */
582 static char_u *
583find_longest_match(expand_T *xp, int options)
584{
585 long_u len;
586 int mb_len = 1;
587 int c0, ci;
588 int i;
589 char_u *ss;
590
591 for (len = 0; xp->xp_files[0][len]; len += mb_len)
592 {
593 if (has_mbyte)
594 {
595 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
596 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
597 }
598 else
599 c0 = xp->xp_files[0][len];
600 for (i = 1; i < xp->xp_numfiles; ++i)
601 {
602 if (has_mbyte)
603 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
604 else
605 ci = xp->xp_files[i][len];
606 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
607 || xp->xp_context == EXPAND_FILES
608 || xp->xp_context == EXPAND_SHELLCMD
609 || xp->xp_context == EXPAND_BUFFERS))
610 {
611 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
612 break;
613 }
614 else if (c0 != ci)
615 break;
616 }
617 if (i < xp->xp_numfiles)
618 {
619 if (!(options & WILD_NO_BEEP))
620 vim_beep(BO_WILD);
621 break;
622 }
623 }
624
625 ss = alloc(len + 1);
626 if (ss)
627 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
628
629 return ss;
630}
631
632/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000633 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200634 * Chars that should not be expanded must be preceded with a backslash.
635 * Return a pointer to allocated memory containing the new string.
636 * Return NULL for failure.
637 *
638 * "orig" is the originally expanded string, copied to allocated memory. It
639 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
640 * WILD_PREV "orig" should be NULL.
641 *
642 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
643 * is WILD_EXPAND_FREE or WILD_ALL.
644 *
645 * mode = WILD_FREE: just free previously expanded matches
646 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
647 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
648 * mode = WILD_NEXT: use next match in multiple match, wrap to first
649 * mode = WILD_PREV: use previous match in multiple match, wrap to first
650 * mode = WILD_ALL: return all matches concatenated
651 * mode = WILD_LONGEST: return longest matched part
652 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000653 * mode = WILD_APPLY: apply the item selected in the cmdline completion
654 * popup menu and close the menu.
655 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
656 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200657 *
658 * options = WILD_LIST_NOTFOUND: list entries without a match
659 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
660 * options = WILD_USE_NL: Use '\n' for WILD_ALL
661 * options = WILD_NO_BEEP: Don't beep for multiple matches
662 * options = WILD_ADD_SLASH: add a slash after directory names
663 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
664 * options = WILD_SILENT: don't print warning messages
665 * options = WILD_ESCAPE: put backslash before special chars
666 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200667 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200668 *
669 * The variables xp->xp_context and xp->xp_backslash must have been set!
670 */
671 char_u *
672ExpandOne(
673 expand_T *xp,
674 char_u *str,
675 char_u *orig, // allocated copy of original of expanded string
676 int options,
677 int mode)
678{
679 char_u *ss = NULL;
680 static int findex;
681 static char_u *orig_save = NULL; // kept value of orig
682 int orig_saved = FALSE;
683 int i;
684 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200685
686 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000687 if (mode == WILD_NEXT || mode == WILD_PREV
688 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000689 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200690
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000691 if (mode == WILD_CANCEL)
692 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
693 else if (mode == WILD_APPLY)
694 ss = vim_strsave(findex == -1 ? (orig_save ?
695 orig_save : (char_u *)"") : xp->xp_files[findex]);
696
Bram Moolenaar66b51422019-08-18 21:44:12 +0200697 // free old names
698 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
699 {
700 FreeWild(xp->xp_numfiles, xp->xp_files);
701 xp->xp_numfiles = -1;
702 VIM_CLEAR(orig_save);
703 }
704 findex = 0;
705
706 if (mode == WILD_FREE) // only release file name
707 return NULL;
708
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000709 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200710 {
711 vim_free(orig_save);
712 orig_save = orig;
713 orig_saved = TRUE;
714
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000715 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200716 }
717
718 // Find longest common part
719 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
720 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000721 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200722 findex = -1; // next p_wc gets first one
723 }
724
725 // Concatenate all matching names
726 if (mode == WILD_ALL && xp->xp_numfiles > 0)
727 {
728 len = 0;
729 for (i = 0; i < xp->xp_numfiles; ++i)
730 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
731 ss = alloc(len);
732 if (ss != NULL)
733 {
734 *ss = NUL;
735 for (i = 0; i < xp->xp_numfiles; ++i)
736 {
737 STRCAT(ss, xp->xp_files[i]);
738 if (i != xp->xp_numfiles - 1)
739 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
740 }
741 }
742 }
743
744 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
745 ExpandCleanup(xp);
746
747 // Free "orig" if it wasn't stored in "orig_save".
748 if (!orig_saved)
749 vim_free(orig);
750
751 return ss;
752}
753
754/*
755 * Prepare an expand structure for use.
756 */
757 void
758ExpandInit(expand_T *xp)
759{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200760 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200761 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200762 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200763}
764
765/*
766 * Cleanup an expand structure after use.
767 */
768 void
769ExpandCleanup(expand_T *xp)
770{
771 if (xp->xp_numfiles >= 0)
772 {
773 FreeWild(xp->xp_numfiles, xp->xp_files);
774 xp->xp_numfiles = -1;
775 }
776}
777
778/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000779 * Display one line of completion matches. Multiple matches are displayed in
780 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000781 * matches - list of completion match names
782 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000783 * lines - number of output lines
784 * linenr - line number of matches to display
785 * maxlen - maximum number of characters in each line
786 * showtail - display only the tail of the full path of a file name
787 * dir_attr - highlight attribute to use for directory names
788 */
789 static void
790showmatches_oneline(
791 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000792 char_u **matches,
793 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000794 int lines,
795 int linenr,
796 int maxlen,
797 int showtail,
798 int dir_attr)
799{
800 int i, j;
801 int isdir;
802 int lastlen;
803 char_u *p;
804
805 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000806 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000807 {
808 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
809 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000810 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
811 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000812 msg_advance(maxlen + 1);
813 msg_puts((char *)p);
814 msg_advance(maxlen + 3);
815 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
816 break;
817 }
818 for (i = maxlen - lastlen; --i >= 0; )
819 msg_putchar(' ');
820 if (xp->xp_context == EXPAND_FILES
821 || xp->xp_context == EXPAND_SHELLCMD
822 || xp->xp_context == EXPAND_BUFFERS)
823 {
824 // highlight directories
825 if (xp->xp_numfiles != -1)
826 {
827 char_u *halved_slash;
828 char_u *exp_path;
829 char_u *path;
830
831 // Expansion was done before and special characters
832 // were escaped, need to halve backslashes. Also
833 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000834 exp_path = expand_env_save_opt(matches[j], TRUE);
835 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000836 halved_slash = backslash_halve_save(path);
837 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000838 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000839 vim_free(exp_path);
840 if (halved_slash != path)
841 vim_free(halved_slash);
842 }
843 else
844 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000845 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000846 if (showtail)
847 p = SHOW_FILE_TEXT(j);
848 else
849 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000850 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000851 TRUE);
852 p = NameBuff;
853 }
854 }
855 else
856 {
857 isdir = FALSE;
858 p = SHOW_FILE_TEXT(j);
859 }
860 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
861 }
862 if (msg_col > 0) // when not wrapped around
863 {
864 msg_clr_eos();
865 msg_putchar('\n');
866 }
867 out_flush(); // show one line at a time
868}
869
870/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200871 * Show all matches for completion on the command line.
872 * Returns EXPAND_NOTHING when the character that triggered expansion should
873 * be inserted like a normal character.
874 */
875 int
876showmatches(expand_T *xp, int wildmenu UNUSED)
877{
878 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000879 int numMatches;
880 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000881 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200882 int maxlen;
883 int lines;
884 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200885 int attr;
886 int showtail;
887
888 if (xp->xp_numfiles == -1)
889 {
890 set_expand_context(xp);
891 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000892 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200893 showtail = expand_showtail(xp);
894 if (i != EXPAND_OK)
895 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200896 }
897 else
898 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000899 numMatches = xp->xp_numfiles;
900 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200901 showtail = cmd_showtail;
902 }
903
904#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000905 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000906 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000907 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000908#endif
909
910#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200911 if (!wildmenu)
912 {
913#endif
914 msg_didany = FALSE; // lines_left will be set
915 msg_start(); // prepare for paging
916 msg_putchar('\n');
917 out_flush();
918 cmdline_row = msg_row;
919 msg_didany = FALSE; // lines_left will be set again
920 msg_start(); // prepare for paging
921#ifdef FEAT_WILDMENU
922 }
923#endif
924
925 if (got_int)
926 got_int = FALSE; // only int. the completion, not the cmd line
927#ifdef FEAT_WILDMENU
928 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000929 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200930#endif
931 else
932 {
933 // find the length of the longest file name
934 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000935 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200936 {
937 if (!showtail && (xp->xp_context == EXPAND_FILES
938 || xp->xp_context == EXPAND_SHELLCMD
939 || xp->xp_context == EXPAND_BUFFERS))
940 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000941 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200942 j = vim_strsize(NameBuff);
943 }
944 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000945 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200946 if (j > maxlen)
947 maxlen = j;
948 }
949
950 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000951 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200952 else
953 {
954 // compute the number of columns and lines for the listing
955 maxlen += 2; // two spaces between file names
956 columns = ((int)Columns + 2) / maxlen;
957 if (columns < 1)
958 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000959 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200960 }
961
962 attr = HL_ATTR(HLF_D); // find out highlighting for directories
963
964 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
965 {
966 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
967 msg_clr_eos();
968 msg_advance(maxlen - 3);
969 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
970 }
971
972 // list the files line by line
973 for (i = 0; i < lines; ++i)
974 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000975 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000976 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200977 if (got_int)
978 {
979 got_int = FALSE;
980 break;
981 }
982 }
983
984 // we redraw the command below the lines that we have just listed
985 // This is a bit tricky, but it saves a lot of screen updating.
986 cmdline_row = msg_row; // will put it back later
987 }
988
989 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000990 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200991
992 return EXPAND_OK;
993}
994
995/*
996 * Private gettail for showmatches() (and win_redr_status_matches()):
997 * Find tail of file name path, but ignore trailing "/".
998 */
999 char_u *
1000sm_gettail(char_u *s)
1001{
1002 char_u *p;
1003 char_u *t = s;
1004 int had_sep = FALSE;
1005
1006 for (p = s; *p != NUL; )
1007 {
1008 if (vim_ispathsep(*p)
1009#ifdef BACKSLASH_IN_FILENAME
1010 && !rem_backslash(p)
1011#endif
1012 )
1013 had_sep = TRUE;
1014 else if (had_sep)
1015 {
1016 t = p;
1017 had_sep = FALSE;
1018 }
1019 MB_PTR_ADV(p);
1020 }
1021 return t;
1022}
1023
1024/*
1025 * Return TRUE if we only need to show the tail of completion matches.
1026 * When not completing file names or there is a wildcard in the path FALSE is
1027 * returned.
1028 */
1029 static int
1030expand_showtail(expand_T *xp)
1031{
1032 char_u *s;
1033 char_u *end;
1034
1035 // When not completing file names a "/" may mean something different.
1036 if (xp->xp_context != EXPAND_FILES
1037 && xp->xp_context != EXPAND_SHELLCMD
1038 && xp->xp_context != EXPAND_DIRECTORIES)
1039 return FALSE;
1040
1041 end = gettail(xp->xp_pattern);
1042 if (end == xp->xp_pattern) // there is no path separator
1043 return FALSE;
1044
1045 for (s = xp->xp_pattern; s < end; s++)
1046 {
1047 // Skip escaped wildcards. Only when the backslash is not a path
1048 // separator, on DOS the '*' "path\*\file" must not be skipped.
1049 if (rem_backslash(s))
1050 ++s;
1051 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1052 return FALSE;
1053 }
1054 return TRUE;
1055}
1056
1057/*
1058 * Prepare a string for expansion.
1059 * When expanding file names: The string will be used with expand_wildcards().
1060 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1061 * When expanding other names: The string will be used with regcomp(). Copy
1062 * the name into allocated memory and prepend "^".
1063 */
1064 char_u *
1065addstar(
1066 char_u *fname,
1067 int len,
1068 int context) // EXPAND_FILES etc.
1069{
1070 char_u *retval;
1071 int i, j;
1072 int new_len;
1073 char_u *tail;
1074 int ends_in_star;
1075
1076 if (context != EXPAND_FILES
1077 && context != EXPAND_FILES_IN_PATH
1078 && context != EXPAND_SHELLCMD
1079 && context != EXPAND_DIRECTORIES)
1080 {
1081 // Matching will be done internally (on something other than files).
1082 // So we convert the file-matching-type wildcards into our kind for
1083 // use with vim_regcomp(). First work out how long it will be:
1084
1085 // For help tags the translation is done in find_help_tags().
1086 // For a tag pattern starting with "/" no translation is needed.
1087 if (context == EXPAND_HELP
1088 || context == EXPAND_COLORS
1089 || context == EXPAND_COMPILER
1090 || context == EXPAND_OWNSYNTAX
1091 || context == EXPAND_FILETYPE
1092 || context == EXPAND_PACKADD
1093 || ((context == EXPAND_TAGS_LISTFILES
1094 || context == EXPAND_TAGS)
1095 && fname[0] == '/'))
1096 retval = vim_strnsave(fname, len);
1097 else
1098 {
1099 new_len = len + 2; // +2 for '^' at start, NUL at end
1100 for (i = 0; i < len; i++)
1101 {
1102 if (fname[i] == '*' || fname[i] == '~')
1103 new_len++; // '*' needs to be replaced by ".*"
1104 // '~' needs to be replaced by "\~"
1105
1106 // Buffer names are like file names. "." should be literal
1107 if (context == EXPAND_BUFFERS && fname[i] == '.')
1108 new_len++; // "." becomes "\."
1109
1110 // Custom expansion takes care of special things, match
1111 // backslashes literally (perhaps also for other types?)
1112 if ((context == EXPAND_USER_DEFINED
1113 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1114 new_len++; // '\' becomes "\\"
1115 }
1116 retval = alloc(new_len);
1117 if (retval != NULL)
1118 {
1119 retval[0] = '^';
1120 j = 1;
1121 for (i = 0; i < len; i++, j++)
1122 {
1123 // Skip backslash. But why? At least keep it for custom
1124 // expansion.
1125 if (context != EXPAND_USER_DEFINED
1126 && context != EXPAND_USER_LIST
1127 && fname[i] == '\\'
1128 && ++i == len)
1129 break;
1130
1131 switch (fname[i])
1132 {
1133 case '*': retval[j++] = '.';
1134 break;
1135 case '~': retval[j++] = '\\';
1136 break;
1137 case '?': retval[j] = '.';
1138 continue;
1139 case '.': if (context == EXPAND_BUFFERS)
1140 retval[j++] = '\\';
1141 break;
1142 case '\\': if (context == EXPAND_USER_DEFINED
1143 || context == EXPAND_USER_LIST)
1144 retval[j++] = '\\';
1145 break;
1146 }
1147 retval[j] = fname[i];
1148 }
1149 retval[j] = NUL;
1150 }
1151 }
1152 }
1153 else
1154 {
1155 retval = alloc(len + 4);
1156 if (retval != NULL)
1157 {
1158 vim_strncpy(retval, fname, len);
1159
1160 // Don't add a star to *, ~, ~user, $var or `cmd`.
1161 // * would become **, which walks the whole tree.
1162 // ~ would be at the start of the file name, but not the tail.
1163 // $ could be anywhere in the tail.
1164 // ` could be anywhere in the file name.
1165 // When the name ends in '$' don't add a star, remove the '$'.
1166 tail = gettail(retval);
1167 ends_in_star = (len > 0 && retval[len - 1] == '*');
1168#ifndef BACKSLASH_IN_FILENAME
1169 for (i = len - 2; i >= 0; --i)
1170 {
1171 if (retval[i] != '\\')
1172 break;
1173 ends_in_star = !ends_in_star;
1174 }
1175#endif
1176 if ((*retval != '~' || tail != retval)
1177 && !ends_in_star
1178 && vim_strchr(tail, '$') == NULL
1179 && vim_strchr(retval, '`') == NULL)
1180 retval[len++] = '*';
1181 else if (len > 0 && retval[len - 1] == '$')
1182 --len;
1183 retval[len] = NUL;
1184 }
1185 }
1186 return retval;
1187}
1188
1189/*
1190 * Must parse the command line so far to work out what context we are in.
1191 * Completion can then be done based on that context.
1192 * This routine sets the variables:
1193 * xp->xp_pattern The start of the pattern to be expanded within
1194 * the command line (ends at the cursor).
1195 * xp->xp_context The type of thing to expand. Will be one of:
1196 *
1197 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1198 * the command line, like an unknown command. Caller
1199 * should beep.
1200 * EXPAND_NOTHING Unrecognised context for completion, use char like
1201 * a normal char, rather than for completion. eg
1202 * :s/^I/
1203 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1204 * it.
1205 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1206 * EXPAND_FILES After command with EX_XFILE set, or after setting
1207 * with P_EXPAND set. eg :e ^I, :w>>^I
1208 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1209 * when we know only directories are of interest. eg
1210 * :set dir=^I
1211 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1212 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1213 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1214 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1215 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1216 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1217 * EXPAND_EVENTS Complete event names
1218 * EXPAND_SYNTAX Complete :syntax command arguments
1219 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1220 * EXPAND_AUGROUP Complete autocommand group names
1221 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1222 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1223 * eg :unmap a^I , :cunab x^I
1224 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1225 * eg :call sub^I
1226 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1227 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1228 * names in expressions, eg :while s^I
1229 * EXPAND_ENV_VARS Complete environment variable names
1230 * EXPAND_USER Complete user names
1231 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001232 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001233set_expand_context(expand_T *xp)
1234{
1235 cmdline_info_T *ccline = get_cmdline_info();
1236
1237 // only expansion for ':', '>' and '=' command-lines
1238 if (ccline->cmdfirstc != ':'
1239#ifdef FEAT_EVAL
1240 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1241 && !ccline->input_fn
1242#endif
1243 )
1244 {
1245 xp->xp_context = EXPAND_NOTHING;
1246 return;
1247 }
1248 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1249}
1250
Bram Moolenaard0190392019-08-23 21:17:35 +02001251/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001252 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1253 * For user defined commands, the completion context is set in 'xp' and the
1254 * completion flags in 'complp'.
1255 *
1256 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001257 */
1258 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001259set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001260{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001261 char_u *p = NULL;
1262 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001263 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001264
1265 // Isolate the command and search for it in the command table.
1266 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001267 // - the 'k' command can directly be followed by any character, but do
1268 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1269 // find matches anywhere in the command name, do this only for command
1270 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001271 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001272 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001273 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001274 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001275 p = cmd + 1;
1276 }
1277 else
1278 {
1279 p = cmd;
1280 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1281 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001282 // A user command may contain digits.
1283 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1284 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001285 while (ASCII_ISALNUM(*p) || *p == '*')
1286 ++p;
1287 // for python 3.x: ":py3*" commands completion
1288 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1289 {
1290 ++p;
1291 while (ASCII_ISALPHA(*p) || *p == '*')
1292 ++p;
1293 }
1294 // check for non-alpha command
1295 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1296 ++p;
1297 len = (int)(p - cmd);
1298
1299 if (len == 0)
1300 {
1301 xp->xp_context = EXPAND_UNSUCCESSFUL;
1302 return NULL;
1303 }
1304
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001305 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001306
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001307 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001308 // Also when doing fuzzy expansion for non-shell commands, support
1309 // alphanumeric characters.
1310 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1311 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001312 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1313 ++p;
1314 }
1315
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001316 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001317 // character, complete the command name.
1318 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1319 return NULL;
1320
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001321 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001322 {
1323 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1324 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001325 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001326 p = cmd + 1;
1327 }
1328 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1329 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001330 eap->cmd = cmd;
1331 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001332 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001333 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001334 }
1335 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001336 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001337 {
1338 // Not still touching the command and it was an illegal one
1339 xp->xp_context = EXPAND_UNSUCCESSFUL;
1340 return NULL;
1341 }
1342
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001343 return p;
1344}
Bram Moolenaard0190392019-08-23 21:17:35 +02001345
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001346/*
1347 * Set the completion context for a command argument with wild card characters.
1348 */
1349 static void
1350set_context_for_wildcard_arg(
1351 exarg_T *eap,
1352 char_u *arg,
1353 int usefilter,
1354 expand_T *xp,
1355 int *complp)
1356{
1357 char_u *p;
1358 int c;
1359 int in_quote = FALSE;
1360 char_u *bow = NULL; // Beginning of word
1361 int len = 0;
1362
1363 // Allow spaces within back-quotes to count as part of the argument
1364 // being expanded.
1365 xp->xp_pattern = skipwhite(arg);
1366 p = xp->xp_pattern;
1367 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001368 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001369 if (has_mbyte)
1370 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001371 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001372 c = *p;
1373 if (c == '\\' && p[1] != NUL)
1374 ++p;
1375 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001376 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001377 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001378 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001379 xp->xp_pattern = p;
1380 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001381 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001382 in_quote = !in_quote;
1383 }
1384 // An argument can contain just about everything, except
1385 // characters that end the command and white space.
1386 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001387#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001388 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001389#endif
1390 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001391 {
1392 len = 0; // avoid getting stuck when space is in 'isfname'
1393 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001394 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001395 if (has_mbyte)
1396 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001397 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001398 c = *p;
1399 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001400 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001401 if (has_mbyte)
1402 len = (*mb_ptr2len)(p);
1403 else
1404 len = 1;
1405 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001406 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001407 if (in_quote)
1408 bow = p;
1409 else
1410 xp->xp_pattern = p;
1411 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001412 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001413 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001414 }
1415
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001416 // If we are still inside the quotes, and we passed a space, just
1417 // expand from there.
1418 if (bow != NULL && in_quote)
1419 xp->xp_pattern = bow;
1420 xp->xp_context = EXPAND_FILES;
1421
1422 // For a shell command more chars need to be escaped.
1423 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1424 {
1425#ifndef BACKSLASH_IN_FILENAME
1426 xp->xp_shell = TRUE;
1427#endif
1428 // When still after the command name expand executables.
1429 if (xp->xp_pattern == skipwhite(arg))
1430 xp->xp_context = EXPAND_SHELLCMD;
1431 }
1432
1433 // Check for environment variable.
1434 if (*xp->xp_pattern == '$')
1435 {
1436 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1437 if (!vim_isIDc(*p))
1438 break;
1439 if (*p == NUL)
1440 {
1441 xp->xp_context = EXPAND_ENV_VARS;
1442 ++xp->xp_pattern;
1443 // Avoid that the assignment uses EXPAND_FILES again.
1444 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1445 *complp = EXPAND_ENV_VARS;
1446 }
1447 }
1448 // Check for user names.
1449 if (*xp->xp_pattern == '~')
1450 {
1451 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1452 ;
1453 // Complete ~user only if it partially matches a user name.
1454 // A full match ~user<Tab> will be replaced by user's home
1455 // directory i.e. something like ~user<Tab> -> /home/user/
1456 if (*p == NUL && p > xp->xp_pattern + 1
1457 && match_user(xp->xp_pattern + 1) >= 1)
1458 {
1459 xp->xp_context = EXPAND_USER;
1460 ++xp->xp_pattern;
1461 }
1462 }
1463}
1464
1465/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001466 * Set the completion context for the :filter command. Returns a pointer to the
1467 * next command after the :filter command.
1468 */
1469 static char_u *
1470set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1471{
1472 if (*arg != NUL)
1473 arg = skip_vimgrep_pat(arg, NULL, NULL);
1474 if (arg == NULL || *arg == NUL)
1475 {
1476 xp->xp_context = EXPAND_NOTHING;
1477 return NULL;
1478 }
1479 return skipwhite(arg);
1480}
1481
1482#ifdef FEAT_SEARCH_EXTRA
1483/*
1484 * Set the completion context for the :match command. Returns a pointer to the
1485 * next command after the :match command.
1486 */
1487 static char_u *
1488set_context_in_match_cmd(expand_T *xp, char_u *arg)
1489{
1490 if (*arg == NUL || !ends_excmd(*arg))
1491 {
1492 // also complete "None"
1493 set_context_in_echohl_cmd(xp, arg);
1494 arg = skipwhite(skiptowhite(arg));
1495 if (*arg != NUL)
1496 {
1497 xp->xp_context = EXPAND_NOTHING;
1498 arg = skip_regexp(arg + 1, *arg, magic_isset());
1499 }
1500 }
1501 return find_nextcmd(arg);
1502}
1503#endif
1504
1505/*
1506 * Returns a pointer to the next command after a :global or a :v command.
1507 * Returns NULL if there is no next command.
1508 */
1509 static char_u *
1510find_cmd_after_global_cmd(char_u *arg)
1511{
1512 int delim;
1513
1514 delim = *arg; // get the delimiter
1515 if (delim)
1516 ++arg; // skip delimiter if there is one
1517
1518 while (arg[0] != NUL && arg[0] != delim)
1519 {
1520 if (arg[0] == '\\' && arg[1] != NUL)
1521 ++arg;
1522 ++arg;
1523 }
1524 if (arg[0] != NUL)
1525 return arg + 1;
1526
1527 return NULL;
1528}
1529
1530/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001531 * Returns a pointer to the next command after a :substitute or a :& command.
1532 * Returns NULL if there is no next command.
1533 */
1534 static char_u *
1535find_cmd_after_substitute_cmd(char_u *arg)
1536{
1537 int delim;
1538
1539 delim = *arg;
1540 if (delim)
1541 {
1542 // skip "from" part
1543 ++arg;
1544 arg = skip_regexp(arg, delim, magic_isset());
1545
1546 if (arg[0] != NUL && arg[0] == delim)
1547 {
1548 // skip "to" part
1549 ++arg;
1550 while (arg[0] != NUL && arg[0] != delim)
1551 {
1552 if (arg[0] == '\\' && arg[1] != NUL)
1553 ++arg;
1554 ++arg;
1555 }
1556 if (arg[0] != NUL) // skip delimiter
1557 ++arg;
1558 }
1559 }
1560 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1561 ++arg;
1562 if (arg[0] != NUL)
1563 return arg;
1564
1565 return NULL;
1566}
1567
1568/*
1569 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1570 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1571 * Returns NULL if there is no next command.
1572 */
1573 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001574find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001575{
1576 arg = skipwhite(skipdigits(arg)); // skip count
1577 if (*arg == '/') // Match regexp, not just whole words
1578 {
1579 for (++arg; *arg && *arg != '/'; arg++)
1580 if (*arg == '\\' && arg[1] != NUL)
1581 arg++;
1582 if (*arg)
1583 {
1584 arg = skipwhite(arg + 1);
1585
1586 // Check for trailing illegal characters
1587 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1588 xp->xp_context = EXPAND_NOTHING;
1589 else
1590 return arg;
1591 }
1592 }
1593
1594 return NULL;
1595}
1596
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001597#ifdef FEAT_EVAL
1598/*
1599 * Set the completion context for the :unlet command. Always returns NULL.
1600 */
1601 static char_u *
1602set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1603{
1604 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1605 arg = xp->xp_pattern + 1;
1606
1607 xp->xp_context = EXPAND_USER_VARS;
1608 xp->xp_pattern = arg;
1609
1610 if (*xp->xp_pattern == '$')
1611 {
1612 xp->xp_context = EXPAND_ENV_VARS;
1613 ++xp->xp_pattern;
1614 }
1615
1616 return NULL;
1617}
1618#endif
1619
1620#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1621/*
1622 * Set the completion context for the :language command. Always returns NULL.
1623 */
1624 static char_u *
1625set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1626{
1627 char_u *p;
1628
1629 p = skiptowhite(arg);
1630 if (*p == NUL)
1631 {
1632 xp->xp_context = EXPAND_LANGUAGE;
1633 xp->xp_pattern = arg;
1634 }
1635 else
1636 {
1637 if ( STRNCMP(arg, "messages", p - arg) == 0
1638 || STRNCMP(arg, "ctype", p - arg) == 0
1639 || STRNCMP(arg, "time", p - arg) == 0
1640 || STRNCMP(arg, "collate", p - arg) == 0)
1641 {
1642 xp->xp_context = EXPAND_LOCALES;
1643 xp->xp_pattern = skipwhite(p);
1644 }
1645 else
1646 xp->xp_context = EXPAND_NOTHING;
1647 }
1648
1649 return NULL;
1650}
1651#endif
1652
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001653#ifdef FEAT_EVAL
1654static enum
1655{
1656 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001657 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1658 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001659} breakpt_expand_what;
1660
1661/*
1662 * Set the completion context for the :breakadd command. Always returns NULL.
1663 */
1664 static char_u *
1665set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1666{
1667 char_u *p;
1668 char_u *subcmd_start;
1669
1670 xp->xp_context = EXPAND_BREAKPOINT;
1671 xp->xp_pattern = arg;
1672
1673 if (cmdidx == CMD_breakadd)
1674 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001675 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001676 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001677 else
1678 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001679
1680 p = skipwhite(arg);
1681 if (*p == NUL)
1682 return NULL;
1683 subcmd_start = p;
1684
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001685 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001686 {
1687 // :breakadd file [lnum] <filename>
1688 // :breakadd func [lnum] <funcname>
1689 p += 4;
1690 p = skipwhite(p);
1691
1692 // skip line number (if specified)
1693 if (VIM_ISDIGIT(*p))
1694 {
1695 p = skipdigits(p);
1696 if (*p != ' ')
1697 {
1698 xp->xp_context = EXPAND_NOTHING;
1699 return NULL;
1700 }
1701 p = skipwhite(p);
1702 }
1703 if (STRNCMP("file", subcmd_start, 4) == 0)
1704 xp->xp_context = EXPAND_FILES;
1705 else
1706 xp->xp_context = EXPAND_USER_FUNC;
1707 xp->xp_pattern = p;
1708 }
1709 else if (STRNCMP("expr ", p, 5) == 0)
1710 {
1711 // :breakadd expr <expression>
1712 xp->xp_context = EXPAND_EXPRESSION;
1713 xp->xp_pattern = skipwhite(p + 5);
1714 }
1715
1716 return NULL;
1717}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001718
1719 static char_u *
1720set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1721{
1722 char_u *p;
1723
1724 xp->xp_context = EXPAND_NOTHING;
1725 xp->xp_pattern = NULL;
1726
1727 p = skipwhite(arg);
1728 if (VIM_ISDIGIT(*p))
1729 return NULL;
1730
1731 xp->xp_context = EXPAND_SCRIPTNAMES;
1732 xp->xp_pattern = p;
1733
1734 return NULL;
1735}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001736#endif
1737
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001738/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001739 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1740 * The argument to the command is 'arg' and the argument flags is 'argt'.
1741 * For user-defined commands and for environment variables, 'compl' has the
1742 * completion type.
1743 * Returns a pointer to the next command. Returns NULL if there is no next
1744 * command.
1745 */
1746 static char_u *
1747set_context_by_cmdname(
1748 char_u *cmd,
1749 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001750 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001751 char_u *arg,
1752 long argt,
1753 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001754 int forceit)
1755{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001756 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001757 {
1758 case CMD_find:
1759 case CMD_sfind:
1760 case CMD_tabfind:
1761 if (xp->xp_context == EXPAND_FILES)
1762 xp->xp_context = EXPAND_FILES_IN_PATH;
1763 break;
1764 case CMD_cd:
1765 case CMD_chdir:
1766 case CMD_tcd:
1767 case CMD_tchdir:
1768 case CMD_lcd:
1769 case CMD_lchdir:
1770 if (xp->xp_context == EXPAND_FILES)
1771 xp->xp_context = EXPAND_DIRECTORIES;
1772 break;
1773 case CMD_help:
1774 xp->xp_context = EXPAND_HELP;
1775 xp->xp_pattern = arg;
1776 break;
1777
1778 // Command modifiers: return the argument.
1779 // Also for commands with an argument that is a command.
1780 case CMD_aboveleft:
1781 case CMD_argdo:
1782 case CMD_belowright:
1783 case CMD_botright:
1784 case CMD_browse:
1785 case CMD_bufdo:
1786 case CMD_cdo:
1787 case CMD_cfdo:
1788 case CMD_confirm:
1789 case CMD_debug:
1790 case CMD_folddoclosed:
1791 case CMD_folddoopen:
1792 case CMD_hide:
1793 case CMD_keepalt:
1794 case CMD_keepjumps:
1795 case CMD_keepmarks:
1796 case CMD_keeppatterns:
1797 case CMD_ldo:
1798 case CMD_leftabove:
1799 case CMD_lfdo:
1800 case CMD_lockmarks:
1801 case CMD_noautocmd:
1802 case CMD_noswapfile:
1803 case CMD_rightbelow:
1804 case CMD_sandbox:
1805 case CMD_silent:
1806 case CMD_tab:
1807 case CMD_tabdo:
1808 case CMD_topleft:
1809 case CMD_verbose:
1810 case CMD_vertical:
1811 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001812 case CMD_vim9cmd:
1813 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001814 return arg;
1815
1816 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001817 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001818
1819#ifdef FEAT_SEARCH_EXTRA
1820 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001821 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001822#endif
1823
1824 // All completion for the +cmdline_compl feature goes here.
1825
1826 case CMD_command:
1827 return set_context_in_user_cmd(xp, arg);
1828
1829 case CMD_delcommand:
1830 xp->xp_context = EXPAND_USER_COMMANDS;
1831 xp->xp_pattern = arg;
1832 break;
1833
1834 case CMD_global:
1835 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001836 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001837 case CMD_and:
1838 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001839 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001840 case CMD_isearch:
1841 case CMD_dsearch:
1842 case CMD_ilist:
1843 case CMD_dlist:
1844 case CMD_ijump:
1845 case CMD_psearch:
1846 case CMD_djump:
1847 case CMD_isplit:
1848 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001849 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001850 case CMD_autocmd:
1851 return set_context_in_autocmd(xp, arg, FALSE);
1852 case CMD_doautocmd:
1853 case CMD_doautoall:
1854 return set_context_in_autocmd(xp, arg, TRUE);
1855 case CMD_set:
1856 set_context_in_set_cmd(xp, arg, 0);
1857 break;
1858 case CMD_setglobal:
1859 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1860 break;
1861 case CMD_setlocal:
1862 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1863 break;
1864 case CMD_tag:
1865 case CMD_stag:
1866 case CMD_ptag:
1867 case CMD_ltag:
1868 case CMD_tselect:
1869 case CMD_stselect:
1870 case CMD_ptselect:
1871 case CMD_tjump:
1872 case CMD_stjump:
1873 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001874 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001875 xp->xp_context = EXPAND_TAGS_LISTFILES;
1876 else
1877 xp->xp_context = EXPAND_TAGS;
1878 xp->xp_pattern = arg;
1879 break;
1880 case CMD_augroup:
1881 xp->xp_context = EXPAND_AUGROUP;
1882 xp->xp_pattern = arg;
1883 break;
1884#ifdef FEAT_SYN_HL
1885 case CMD_syntax:
1886 set_context_in_syntax_cmd(xp, arg);
1887 break;
1888#endif
1889#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001890 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001891 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001892 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001893 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001894 case CMD_if:
1895 case CMD_elseif:
1896 case CMD_while:
1897 case CMD_for:
1898 case CMD_echo:
1899 case CMD_echon:
1900 case CMD_execute:
1901 case CMD_echomsg:
1902 case CMD_echoerr:
1903 case CMD_call:
1904 case CMD_return:
1905 case CMD_cexpr:
1906 case CMD_caddexpr:
1907 case CMD_cgetexpr:
1908 case CMD_lexpr:
1909 case CMD_laddexpr:
1910 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001911 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001912 break;
1913
1914 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001915 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001916 case CMD_function:
1917 case CMD_delfunction:
1918 xp->xp_context = EXPAND_USER_FUNC;
1919 xp->xp_pattern = arg;
1920 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001921 case CMD_disassemble:
1922 set_context_in_disassemble_cmd(xp, arg);
1923 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001924
1925 case CMD_echohl:
1926 set_context_in_echohl_cmd(xp, arg);
1927 break;
1928#endif
1929 case CMD_highlight:
1930 set_context_in_highlight_cmd(xp, arg);
1931 break;
1932#ifdef FEAT_CSCOPE
1933 case CMD_cscope:
1934 case CMD_lcscope:
1935 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001936 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001937 break;
1938#endif
1939#ifdef FEAT_SIGNS
1940 case CMD_sign:
1941 set_context_in_sign_cmd(xp, arg);
1942 break;
1943#endif
1944 case CMD_bdelete:
1945 case CMD_bwipeout:
1946 case CMD_bunload:
1947 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1948 arg = xp->xp_pattern + 1;
1949 // FALLTHROUGH
1950 case CMD_buffer:
1951 case CMD_sbuffer:
1952 case CMD_checktime:
1953 xp->xp_context = EXPAND_BUFFERS;
1954 xp->xp_pattern = arg;
1955 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001956#ifdef FEAT_DIFF
1957 case CMD_diffget:
1958 case CMD_diffput:
1959 // If current buffer is in diff mode, complete buffer names
1960 // which are in diff mode, and different than current buffer.
1961 xp->xp_context = EXPAND_DIFF_BUFFERS;
1962 xp->xp_pattern = arg;
1963 break;
1964#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001965 case CMD_USER:
1966 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001967 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1968 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001969
1970 case CMD_map: case CMD_noremap:
1971 case CMD_nmap: case CMD_nnoremap:
1972 case CMD_vmap: case CMD_vnoremap:
1973 case CMD_omap: case CMD_onoremap:
1974 case CMD_imap: case CMD_inoremap:
1975 case CMD_cmap: case CMD_cnoremap:
1976 case CMD_lmap: case CMD_lnoremap:
1977 case CMD_smap: case CMD_snoremap:
1978 case CMD_tmap: case CMD_tnoremap:
1979 case CMD_xmap: case CMD_xnoremap:
1980 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001981 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001982 case CMD_unmap:
1983 case CMD_nunmap:
1984 case CMD_vunmap:
1985 case CMD_ounmap:
1986 case CMD_iunmap:
1987 case CMD_cunmap:
1988 case CMD_lunmap:
1989 case CMD_sunmap:
1990 case CMD_tunmap:
1991 case CMD_xunmap:
1992 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001993 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001994 case CMD_mapclear:
1995 case CMD_nmapclear:
1996 case CMD_vmapclear:
1997 case CMD_omapclear:
1998 case CMD_imapclear:
1999 case CMD_cmapclear:
2000 case CMD_lmapclear:
2001 case CMD_smapclear:
2002 case CMD_tmapclear:
2003 case CMD_xmapclear:
2004 xp->xp_context = EXPAND_MAPCLEAR;
2005 xp->xp_pattern = arg;
2006 break;
2007
2008 case CMD_abbreviate: case CMD_noreabbrev:
2009 case CMD_cabbrev: case CMD_cnoreabbrev:
2010 case CMD_iabbrev: case CMD_inoreabbrev:
2011 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002012 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002013 case CMD_unabbreviate:
2014 case CMD_cunabbrev:
2015 case CMD_iunabbrev:
2016 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002017 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002018#ifdef FEAT_MENU
2019 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2020 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2021 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2022 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2023 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2024 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2025 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2026 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2027 case CMD_tmenu: case CMD_tunmenu:
2028 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2029 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2030#endif
2031
2032 case CMD_colorscheme:
2033 xp->xp_context = EXPAND_COLORS;
2034 xp->xp_pattern = arg;
2035 break;
2036
2037 case CMD_compiler:
2038 xp->xp_context = EXPAND_COMPILER;
2039 xp->xp_pattern = arg;
2040 break;
2041
2042 case CMD_ownsyntax:
2043 xp->xp_context = EXPAND_OWNSYNTAX;
2044 xp->xp_pattern = arg;
2045 break;
2046
2047 case CMD_setfiletype:
2048 xp->xp_context = EXPAND_FILETYPE;
2049 xp->xp_pattern = arg;
2050 break;
2051
2052 case CMD_packadd:
2053 xp->xp_context = EXPAND_PACKADD;
2054 xp->xp_pattern = arg;
2055 break;
2056
2057#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2058 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002059 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002060#endif
2061#if defined(FEAT_PROFILE)
2062 case CMD_profile:
2063 set_context_in_profile_cmd(xp, arg);
2064 break;
2065#endif
2066 case CMD_behave:
2067 xp->xp_context = EXPAND_BEHAVE;
2068 xp->xp_pattern = arg;
2069 break;
2070
2071 case CMD_messages:
2072 xp->xp_context = EXPAND_MESSAGES;
2073 xp->xp_pattern = arg;
2074 break;
2075
2076 case CMD_history:
2077 xp->xp_context = EXPAND_HISTORY;
2078 xp->xp_pattern = arg;
2079 break;
2080#if defined(FEAT_PROFILE)
2081 case CMD_syntime:
2082 xp->xp_context = EXPAND_SYNTIME;
2083 xp->xp_pattern = arg;
2084 break;
2085#endif
2086
2087 case CMD_argdelete:
2088 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2089 arg = xp->xp_pattern + 1;
2090 xp->xp_context = EXPAND_ARGLIST;
2091 xp->xp_pattern = arg;
2092 break;
2093
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002094#ifdef FEAT_EVAL
2095 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002096 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002097 case CMD_breakdel:
2098 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002099
2100 case CMD_scriptnames:
2101 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002102#endif
2103
Bram Moolenaard0190392019-08-23 21:17:35 +02002104 default:
2105 break;
2106 }
2107 return NULL;
2108}
2109
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002110/*
2111 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2112 * we don't need/want deleted. Maybe this could be done better if we didn't
2113 * repeat all this stuff. The only problem is that they may not stay
2114 * perfectly compatible with each other, but then the command line syntax
2115 * probably won't change that much -- webb.
2116 */
2117 static char_u *
2118set_one_cmd_context(
2119 expand_T *xp,
2120 char_u *buff) // buffer for command string
2121{
2122 char_u *p;
2123 char_u *cmd, *arg;
2124 int len = 0;
2125 exarg_T ea;
2126 int compl = EXPAND_NOTHING;
2127 int forceit = FALSE;
2128 int usefilter = FALSE; // filter instead of file name
2129
2130 ExpandInit(xp);
2131 xp->xp_pattern = buff;
2132 xp->xp_line = buff;
2133 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2134 ea.argt = 0;
2135
2136 // 1. skip comment lines and leading space, colons or bars
2137 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2138 ;
2139 xp->xp_pattern = cmd;
2140
2141 if (*cmd == NUL)
2142 return NULL;
2143 if (*cmd == '"') // ignore comment lines
2144 {
2145 xp->xp_context = EXPAND_NOTHING;
2146 return NULL;
2147 }
2148
2149 // 3. Skip over the range to find the command.
2150 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2151 xp->xp_pattern = cmd;
2152 if (*cmd == NUL)
2153 return NULL;
2154 if (*cmd == '"')
2155 {
2156 xp->xp_context = EXPAND_NOTHING;
2157 return NULL;
2158 }
2159
2160 if (*cmd == '|' || *cmd == '\n')
2161 return cmd + 1; // There's another command
2162
2163 // Get the command index.
2164 p = set_cmd_index(cmd, &ea, xp, &compl);
2165 if (p == NULL)
2166 return NULL;
2167
2168 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2169
2170 if (*p == '!') // forced commands
2171 {
2172 forceit = TRUE;
2173 ++p;
2174 }
2175
2176 // 6. parse arguments
2177 if (!IS_USER_CMDIDX(ea.cmdidx))
2178 ea.argt = excmd_get_argt(ea.cmdidx);
2179
2180 arg = skipwhite(p);
2181
2182 // Skip over ++argopt argument
2183 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2184 {
2185 p = arg;
2186 while (*p && !vim_isspace(*p))
2187 MB_PTR_ADV(p);
2188 arg = skipwhite(p);
2189 }
2190
2191 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2192 {
2193 if (*arg == '>') // append
2194 {
2195 if (*++arg == '>')
2196 ++arg;
2197 arg = skipwhite(arg);
2198 }
2199 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2200 {
2201 ++arg;
2202 usefilter = TRUE;
2203 }
2204 }
2205
2206 if (ea.cmdidx == CMD_read)
2207 {
2208 usefilter = forceit; // :r! filter if forced
2209 if (*arg == '!') // :r !filter
2210 {
2211 ++arg;
2212 usefilter = TRUE;
2213 }
2214 }
2215
2216 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2217 {
2218 while (*arg == *cmd) // allow any number of '>' or '<'
2219 ++arg;
2220 arg = skipwhite(arg);
2221 }
2222
2223 // Does command allow "+command"?
2224 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2225 {
2226 // Check if we're in the +command
2227 p = arg + 1;
2228 arg = skip_cmd_arg(arg, FALSE);
2229
2230 // Still touching the command after '+'?
2231 if (*arg == NUL)
2232 return p;
2233
2234 // Skip space(s) after +command to get to the real argument
2235 arg = skipwhite(arg);
2236 }
2237
2238
2239 // Check for '|' to separate commands and '"' to start comments.
2240 // Don't do this for ":read !cmd" and ":write !cmd".
2241 if ((ea.argt & EX_TRLBAR) && !usefilter)
2242 {
2243 p = arg;
2244 // ":redir @" is not the start of a comment
2245 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2246 p += 2;
2247 while (*p)
2248 {
2249 if (*p == Ctrl_V)
2250 {
2251 if (p[1] != NUL)
2252 ++p;
2253 }
2254 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2255 || *p == '|' || *p == '\n')
2256 {
2257 if (*(p - 1) != '\\')
2258 {
2259 if (*p == '|' || *p == '\n')
2260 return p + 1;
2261 return NULL; // It's a comment
2262 }
2263 }
2264 MB_PTR_ADV(p);
2265 }
2266 }
2267
2268 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2269 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2270 // no arguments allowed but there is something
2271 return NULL;
2272
2273 // Find start of last argument (argument just before cursor):
2274 p = buff;
2275 xp->xp_pattern = p;
2276 len = (int)STRLEN(buff);
2277 while (*p && p < buff + len)
2278 {
2279 if (*p == ' ' || *p == TAB)
2280 {
2281 // argument starts after a space
2282 xp->xp_pattern = ++p;
2283 }
2284 else
2285 {
2286 if (*p == '\\' && *(p + 1) != NUL)
2287 ++p; // skip over escaped character
2288 MB_PTR_ADV(p);
2289 }
2290 }
2291
2292 if (ea.argt & EX_XFILE)
2293 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2294
2295 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002296 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002297 forceit);
2298}
2299
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002300/*
2301 * Set the completion context in 'xp' for command 'str'
2302 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002303 void
2304set_cmd_context(
2305 expand_T *xp,
2306 char_u *str, // start of command line
2307 int len, // length of command line (excl. NUL)
2308 int col, // position of cursor
2309 int use_ccline UNUSED) // use ccline for info
2310{
2311#ifdef FEAT_EVAL
2312 cmdline_info_T *ccline = get_cmdline_info();
2313#endif
2314 int old_char = NUL;
2315 char_u *nextcomm;
2316
2317 // Avoid a UMR warning from Purify, only save the character if it has been
2318 // written before.
2319 if (col < len)
2320 old_char = str[col];
2321 str[col] = NUL;
2322 nextcomm = str;
2323
2324#ifdef FEAT_EVAL
2325 if (use_ccline && ccline->cmdfirstc == '=')
2326 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002327 // pass CMD_SIZE because there is no real command
2328 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002329 }
2330 else if (use_ccline && ccline->input_fn)
2331 {
2332 xp->xp_context = ccline->xp_context;
2333 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002334 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002335 }
2336 else
2337#endif
2338 while (nextcomm != NULL)
2339 nextcomm = set_one_cmd_context(xp, nextcomm);
2340
2341 // Store the string here so that call_user_expand_func() can get to them
2342 // easily.
2343 xp->xp_line = str;
2344 xp->xp_col = col;
2345
2346 str[col] = old_char;
2347}
2348
2349/*
2350 * Expand the command line "str" from context "xp".
2351 * "xp" must have been set by set_cmd_context().
2352 * xp->xp_pattern points into "str", to where the text that is to be expanded
2353 * starts.
2354 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2355 * cursor.
2356 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2357 * key that triggered expansion literally.
2358 * Returns EXPAND_OK otherwise.
2359 */
2360 int
2361expand_cmdline(
2362 expand_T *xp,
2363 char_u *str, // start of command line
2364 int col, // position of cursor
2365 int *matchcount, // return: nr of matches
2366 char_u ***matches) // return: array of pointers to matches
2367{
2368 char_u *file_str = NULL;
2369 int options = WILD_ADD_SLASH|WILD_SILENT;
2370
2371 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2372 {
2373 beep_flush();
2374 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2375 }
2376 if (xp->xp_context == EXPAND_NOTHING)
2377 {
2378 // Caller can use the character as a normal char instead
2379 return EXPAND_NOTHING;
2380 }
2381
2382 // add star to file name, or convert to regexp if not exp. files.
2383 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002384 if (cmdline_fuzzy_completion_supported(xp))
2385 // If fuzzy matching, don't modify the search string
2386 file_str = vim_strsave(xp->xp_pattern);
2387 else
2388 {
2389 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2390 if (file_str == NULL)
2391 return EXPAND_UNSUCCESSFUL;
2392 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002393
2394 if (p_wic)
2395 options += WILD_ICASE;
2396
2397 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002398 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002399 {
2400 *matchcount = 0;
2401 *matches = NULL;
2402 }
2403 vim_free(file_str);
2404
2405 return EXPAND_OK;
2406}
2407
Bram Moolenaar66b51422019-08-18 21:44:12 +02002408/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002409 * Expand file or directory names.
2410 */
2411 static int
2412expand_files_and_dirs(
2413 expand_T *xp,
2414 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002415 char_u ***matches,
2416 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002417 int flags,
2418 int options)
2419{
2420 int free_pat = FALSE;
2421 int i;
2422 int ret;
2423
2424 // for ":set path=" and ":set tags=" halve backslashes for escaped
2425 // space
2426 if (xp->xp_backslash != XP_BS_NONE)
2427 {
2428 free_pat = TRUE;
2429 pat = vim_strsave(pat);
2430 for (i = 0; pat[i]; ++i)
2431 if (pat[i] == '\\')
2432 {
2433 if (xp->xp_backslash == XP_BS_THREE
2434 && pat[i + 1] == '\\'
2435 && pat[i + 2] == '\\'
2436 && pat[i + 3] == ' ')
2437 STRMOVE(pat + i, pat + i + 3);
2438 if (xp->xp_backslash == XP_BS_ONE
2439 && pat[i + 1] == ' ')
2440 STRMOVE(pat + i, pat + i + 1);
2441 }
2442 }
2443
2444 if (xp->xp_context == EXPAND_FILES)
2445 flags |= EW_FILE;
2446 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2447 flags |= (EW_FILE | EW_PATH);
2448 else
2449 flags = (flags | EW_DIR) & ~EW_FILE;
2450 if (options & WILD_ICASE)
2451 flags |= EW_ICASE;
2452
2453 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002454 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002455 if (free_pat)
2456 vim_free(pat);
2457#ifdef BACKSLASH_IN_FILENAME
2458 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2459 {
2460 int j;
2461
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002462 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002463 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002464 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002465
2466 while (*ptr != NUL)
2467 {
2468 if (p_csl[0] == 's' && *ptr == '\\')
2469 *ptr = '/';
2470 else if (p_csl[0] == 'b' && *ptr == '/')
2471 *ptr = '\\';
2472 ptr += (*mb_ptr2len)(ptr);
2473 }
2474 }
2475 }
2476#endif
2477 return ret;
2478}
2479
2480/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002481 * Function given to ExpandGeneric() to obtain the possible arguments of the
2482 * ":behave {mswin,xterm}" command.
2483 */
2484 static char_u *
2485get_behave_arg(expand_T *xp UNUSED, int idx)
2486{
2487 if (idx == 0)
2488 return (char_u *)"mswin";
2489 if (idx == 1)
2490 return (char_u *)"xterm";
2491 return NULL;
2492}
2493
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002494# ifdef FEAT_EVAL
2495/*
2496 * Function given to ExpandGeneric() to obtain the possible arguments of the
2497 * ":breakadd {expr, file, func, here}" command.
2498 * ":breakdel {func, file, here}" command.
2499 */
2500 static char_u *
2501get_breakadd_arg(expand_T *xp UNUSED, int idx)
2502{
2503 char *opts[] = {"expr", "file", "func", "here"};
2504
2505 if (idx >=0 && idx <= 3)
2506 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002507 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002508 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2509 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002510 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2511 {
2512 // breakdel {func, file, here}
2513 if (idx <= 2)
2514 return (char_u *)opts[idx + 1];
2515 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002516 else
2517 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002518 // profdel {func, file}
2519 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002520 return (char_u *)opts[idx + 1];
2521 }
2522 }
2523 return NULL;
2524}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002525
2526/*
2527 * Function given to ExpandGeneric() to obtain the possible arguments for the
2528 * ":scriptnames" command.
2529 */
2530 static char_u *
2531get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2532{
2533 scriptitem_T *si;
2534
2535 if (!SCRIPT_ID_VALID(idx + 1))
2536 return NULL;
2537
2538 si = SCRIPT_ITEM(idx + 1);
2539 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2540 return NameBuff;
2541}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002542#endif
2543
Bram Moolenaard0190392019-08-23 21:17:35 +02002544/*
2545 * Function given to ExpandGeneric() to obtain the possible arguments of the
2546 * ":messages {clear}" command.
2547 */
2548 static char_u *
2549get_messages_arg(expand_T *xp UNUSED, int idx)
2550{
2551 if (idx == 0)
2552 return (char_u *)"clear";
2553 return NULL;
2554}
2555
2556 static char_u *
2557get_mapclear_arg(expand_T *xp UNUSED, int idx)
2558{
2559 if (idx == 0)
2560 return (char_u *)"<buffer>";
2561 return NULL;
2562}
2563
2564/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002565 * Do the expansion based on xp->xp_context and 'rmp'.
2566 */
2567 static int
2568ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002569 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002570 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002571 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002572 char_u ***matches,
2573 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002574{
2575 static struct expgen
2576 {
2577 int context;
2578 char_u *((*func)(expand_T *, int));
2579 int ic;
2580 int escaped;
2581 } tab[] =
2582 {
2583 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2584 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2585 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2586 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2587 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2588 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2589 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2590 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2591 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2592 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002593#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002594 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2595 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2596 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2597 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2598 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002599#endif
2600#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002601 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2602 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002603#endif
2604#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002605 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002606#endif
2607#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002608 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002609#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002610 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2611 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2612 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002613#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002614 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002615#endif
2616#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002617 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002618#endif
2619#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002620 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002621#endif
2622#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002623 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2624 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002625#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002626 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2627 {EXPAND_USER, get_users, TRUE, FALSE},
2628 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002629#ifdef FEAT_EVAL
2630 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002631 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002632#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002633 };
2634 int i;
2635 int ret = FAIL;
2636
2637 // Find a context in the table and call the ExpandGeneric() with the
2638 // right function to do the expansion.
2639 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2640 {
2641 if (xp->xp_context == tab[i].context)
2642 {
2643 if (tab[i].ic)
2644 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002645 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2646 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002647 break;
2648 }
2649 }
2650
2651 return ret;
2652}
2653
2654/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002655 * Map wild expand options to flags for expand_wildcards()
2656 */
2657 static int
2658map_wildopts_to_ewflags(int options)
2659{
2660 int flags;
2661
2662 flags = EW_DIR; // include directories
2663 if (options & WILD_LIST_NOTFOUND)
2664 flags |= EW_NOTFOUND;
2665 if (options & WILD_ADD_SLASH)
2666 flags |= EW_ADDSLASH;
2667 if (options & WILD_KEEP_ALL)
2668 flags |= EW_KEEPALL;
2669 if (options & WILD_SILENT)
2670 flags |= EW_SILENT;
2671 if (options & WILD_NOERROR)
2672 flags |= EW_NOERROR;
2673 if (options & WILD_ALLLINKS)
2674 flags |= EW_ALLLINKS;
2675
2676 return flags;
2677}
2678
2679/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002680 * Do the expansion based on xp->xp_context and "pat".
2681 */
2682 static int
2683ExpandFromContext(
2684 expand_T *xp,
2685 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002686 char_u ***matches,
2687 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002688 int options) // WILD_ flags
2689{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002690 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002691 int ret;
2692 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002693 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002694 int fuzzy = cmdline_fuzzy_complete(pat)
2695 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002696
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002697 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002698
2699 if (xp->xp_context == EXPAND_FILES
2700 || xp->xp_context == EXPAND_DIRECTORIES
2701 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002702 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2703 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002704
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002705 *matches = (char_u **)"";
2706 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002707 if (xp->xp_context == EXPAND_HELP)
2708 {
2709 // With an empty argument we would get all the help tags, which is
2710 // very slow. Get matches for "help" instead.
2711 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002712 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002713 {
2714#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002715 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002716#endif
2717 return OK;
2718 }
2719 return FAIL;
2720 }
2721
Bram Moolenaar66b51422019-08-18 21:44:12 +02002722 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002723 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002724 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002725 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002726 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002727 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002728#ifdef FEAT_DIFF
2729 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002730 return ExpandBufnames(pat, numMatches, matches,
2731 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002732#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002733 if (xp->xp_context == EXPAND_TAGS
2734 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002735 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2736 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002737 if (xp->xp_context == EXPAND_COLORS)
2738 {
2739 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002740 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002741 directories);
2742 }
2743 if (xp->xp_context == EXPAND_COMPILER)
2744 {
2745 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002746 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002747 }
2748 if (xp->xp_context == EXPAND_OWNSYNTAX)
2749 {
2750 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002751 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002752 }
2753 if (xp->xp_context == EXPAND_FILETYPE)
2754 {
2755 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002756 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002757 }
2758# if defined(FEAT_EVAL)
2759 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002760 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002761# endif
2762 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002763 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002764
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002765 // When expanding a function name starting with s:, match the <SNR>nr_
2766 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002767 if ((xp->xp_context == EXPAND_USER_FUNC
2768 || xp->xp_context == EXPAND_DISASSEMBLE)
2769 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002770 {
2771 int len = (int)STRLEN(pat) + 20;
2772
2773 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002774 if (tofree == NULL)
2775 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002776 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002777 pat = tofree;
2778 }
2779
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002780 if (!fuzzy)
2781 {
2782 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2783 if (regmatch.regprog == NULL)
2784 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002785
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002786 // set ignore-case according to p_ic, p_scs and pat
2787 regmatch.rm_ic = ignorecase(pat);
2788 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002789
2790 if (xp->xp_context == EXPAND_SETTINGS
2791 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01002792 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002793 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002794 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002795# if defined(FEAT_EVAL)
2796 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002797 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002798# endif
2799 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002800 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002801
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002802 if (!fuzzy)
2803 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002804 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002805
2806 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002807}
2808
Bram Moolenaar66b51422019-08-18 21:44:12 +02002809/*
2810 * Expand a list of names.
2811 *
2812 * Generic function for command line completion. It calls a function to
2813 * obtain strings, one by one. The strings are matched against a regexp
2814 * program. Matching strings are copied into an array, which is returned.
2815 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002816 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2817 * is used.
2818 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002819 * Returns OK when no problems encountered, FAIL for error (out of memory).
2820 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002821 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002822ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002823 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002824 expand_T *xp,
2825 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002826 char_u ***matches,
2827 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002828 char_u *((*func)(expand_T *, int)),
2829 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002830 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002831{
2832 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002833 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002834 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002835 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002836 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002837 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002838 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002839 int sort_matches = FALSE;
2840 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002841
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002842 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002843 *matches = NULL;
2844 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002845
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002846 if (!fuzzy)
2847 ga_init2(&ga, sizeof(char *), 30);
2848 else
2849 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2850
2851 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002852 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002853 str = (*func)(xp, i);
2854 if (str == NULL) // end of list
2855 break;
2856 if (*str == NUL) // skip empty strings
2857 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002858
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002859 if (xp->xp_pattern[0] != NUL)
2860 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002861 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002862 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002863 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002864 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002865 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002866 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002867 }
2868 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002869 else
2870 match = TRUE;
2871
2872 if (!match)
2873 continue;
2874
2875 if (escaped)
2876 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2877 else
2878 str = vim_strsave(str);
2879 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002880 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002881 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002882 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002883 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002884 return FAIL;
2885 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01002886 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002887 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002888 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002889
2890 if (ga_grow(&ga, 1) == FAIL)
2891 {
2892 vim_free(str);
2893 break;
2894 }
2895
2896 if (fuzzy)
2897 {
2898 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2899 fuzmatch->idx = ga.ga_len;
2900 fuzmatch->str = str;
2901 fuzmatch->score = score;
2902 }
2903 else
2904 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2905
2906# ifdef FEAT_MENU
2907 if (func == get_menu_names)
2908 {
2909 // test for separator added by get_menu_names()
2910 str += STRLEN(str) - 1;
2911 if (*str == '\001')
2912 *str = '.';
2913 }
2914# endif
2915
2916 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002917 }
2918
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002919 if (ga.ga_len == 0)
2920 return OK;
2921
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002922 // sort the matches when using regular expression matching and sorting
2923 // applies to the completion context. Menus and scriptnames should be kept
2924 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002925 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002926 && xp->xp_context != EXPAND_MENUS
2927 && xp->xp_context != EXPAND_SCRIPTNAMES)
2928 sort_matches = TRUE;
2929
2930 // <SNR> functions should be sorted to the end.
2931 if (xp->xp_context == EXPAND_EXPRESSION
2932 || xp->xp_context == EXPAND_FUNCTIONS
2933 || xp->xp_context == EXPAND_USER_FUNC
2934 || xp->xp_context == EXPAND_DISASSEMBLE)
2935 funcsort = TRUE;
2936
2937 // Sort the matches.
2938 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002939 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002940 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002941 // <SNR> functions should be sorted to the end.
2942 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2943 sort_func_compare);
2944 else
2945 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2946 }
2947
2948 if (!fuzzy)
2949 {
2950 *matches = ga.ga_data;
2951 *numMatches = ga.ga_len;
2952 }
2953 else
2954 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002955 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2956 funcsort) == FAIL)
2957 return FAIL;
2958 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002959 }
2960
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002961#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002962 // Reset the variables used for special highlight names expansion, so that
2963 // they don't show up when getting normal highlight names by ID.
2964 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002965#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002966
Bram Moolenaar66b51422019-08-18 21:44:12 +02002967 return OK;
2968}
2969
2970/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002971 * Expand shell command matches in one directory of $PATH.
2972 */
2973 static void
2974expand_shellcmd_onedir(
2975 char_u *buf,
2976 char_u *s,
2977 size_t l,
2978 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002979 char_u ***matches,
2980 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002981 int flags,
2982 hashtab_T *ht,
2983 garray_T *gap)
2984{
2985 int ret;
2986 int i;
2987 hash_T hash;
2988 hashitem_T *hi;
2989
2990 vim_strncpy(buf, s, l);
2991 add_pathsep(buf);
2992 l = STRLEN(buf);
2993 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2994
2995 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002996 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002997 if (ret == OK)
2998 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002999 if (ga_grow(gap, *numMatches) == FAIL)
3000 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003001 else
3002 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003003 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003004 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003005 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003006
3007 if (STRLEN(name) > l)
3008 {
3009 // Check if this name was already found.
3010 hash = hash_hash(name + l);
3011 hi = hash_lookup(ht, name + l, hash);
3012 if (HASHITEM_EMPTY(hi))
3013 {
3014 // Remove the path that was prepended.
3015 STRMOVE(name, name + l);
3016 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3017 hash_add_item(ht, hi, name, hash);
3018 name = NULL;
3019 }
3020 }
3021 vim_free(name);
3022 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003023 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003024 }
3025 }
3026}
3027
3028/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003029 * Complete a shell command.
3030 * Returns FAIL or OK;
3031 */
3032 static int
3033expand_shellcmd(
3034 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003035 char_u ***matches, // return: array with matches
3036 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003037 int flagsarg) // EW_ flags
3038{
3039 char_u *pat;
3040 int i;
3041 char_u *path = NULL;
3042 int mustfree = FALSE;
3043 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003044 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003045 size_t l;
3046 char_u *s, *e;
3047 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003048 int did_curdir = FALSE;
3049 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003050
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003051 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003052 if (buf == NULL)
3053 return FAIL;
3054
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003055 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003056 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003057 if (pat == NULL)
3058 {
3059 vim_free(buf);
3060 return FAIL;
3061 }
3062
Bram Moolenaar66b51422019-08-18 21:44:12 +02003063 for (i = 0; pat[i]; ++i)
3064 if (pat[i] == '\\' && pat[i + 1] == ' ')
3065 STRMOVE(pat + i, pat + i + 1);
3066
3067 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3068
3069 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3070 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3071 path = (char_u *)".";
3072 else
3073 {
3074 // For an absolute name we don't use $PATH.
3075 if (!mch_isFullName(pat))
3076 path = vim_getenv((char_u *)"PATH", &mustfree);
3077 if (path == NULL)
3078 path = (char_u *)"";
3079 }
3080
3081 // Go over all directories in $PATH. Expand matches in that directory and
3082 // collect them in "ga". When "." is not in $PATH also expand for the
3083 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003084 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003085 hash_init(&found_ht);
3086 for (s = path; ; s = e)
3087 {
3088# if defined(MSWIN)
3089 e = vim_strchr(s, ';');
3090# else
3091 e = vim_strchr(s, ':');
3092# endif
3093 if (e == NULL)
3094 e = s + STRLEN(s);
3095
3096 if (*s == NUL)
3097 {
3098 if (did_curdir)
3099 break;
3100 // Find directories in the current directory, path is empty.
3101 did_curdir = TRUE;
3102 flags |= EW_DIR;
3103 }
3104 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3105 {
3106 did_curdir = TRUE;
3107 flags |= EW_DIR;
3108 }
3109 else
3110 // Do not match directories inside a $PATH item.
3111 flags &= ~EW_DIR;
3112
3113 l = e - s;
3114 if (l > MAXPATHL - 5)
3115 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003116
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003117 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003118 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003119
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120 if (*e != NUL)
3121 ++e;
3122 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003123 *matches = ga.ga_data;
3124 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125
3126 vim_free(buf);
3127 vim_free(pat);
3128 if (mustfree)
3129 vim_free(path);
3130 hash_clear(&found_ht);
3131 return OK;
3132}
3133
3134# if defined(FEAT_EVAL)
3135/*
3136 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003137 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138 */
3139 static void *
3140call_user_expand_func(
3141 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003142 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143{
3144 cmdline_info_T *ccline = get_cmdline_info();
3145 int keep = 0;
3146 typval_T args[4];
3147 sctx_T save_current_sctx = current_sctx;
3148 char_u *pat = NULL;
3149 void *ret;
3150
3151 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3152 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003153
3154 if (ccline->cmdbuff != NULL)
3155 {
3156 keep = ccline->cmdbuff[ccline->cmdlen];
3157 ccline->cmdbuff[ccline->cmdlen] = 0;
3158 }
3159
3160 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3161
3162 args[0].v_type = VAR_STRING;
3163 args[0].vval.v_string = pat;
3164 args[1].v_type = VAR_STRING;
3165 args[1].vval.v_string = xp->xp_line;
3166 args[2].v_type = VAR_NUMBER;
3167 args[2].vval.v_number = xp->xp_col;
3168 args[3].v_type = VAR_UNKNOWN;
3169
3170 current_sctx = xp->xp_script_ctx;
3171
3172 ret = user_expand_func(xp->xp_arg, 3, args);
3173
3174 current_sctx = save_current_sctx;
3175 if (ccline->cmdbuff != NULL)
3176 ccline->cmdbuff[ccline->cmdlen] = keep;
3177
3178 vim_free(pat);
3179 return ret;
3180}
3181
3182/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003183 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3184 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003185 */
3186 static int
3187ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003188 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003189 expand_T *xp,
3190 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003191 char_u ***matches,
3192 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193{
3194 char_u *retstr;
3195 char_u *s;
3196 char_u *e;
3197 int keep;
3198 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003199 int fuzzy;
3200 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003201 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003202
3203 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003204 *matches = NULL;
3205 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003206
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003207 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003208 if (retstr == NULL)
3209 return FAIL;
3210
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003211 if (!fuzzy)
3212 ga_init2(&ga, sizeof(char *), 3);
3213 else
3214 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3215
Bram Moolenaar66b51422019-08-18 21:44:12 +02003216 for (s = retstr; *s != NUL; s = e)
3217 {
3218 e = vim_strchr(s, '\n');
3219 if (e == NULL)
3220 e = s + STRLEN(s);
3221 keep = *e;
3222 *e = NUL;
3223
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003224 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003225 {
3226 if (!fuzzy)
3227 match = vim_regexec(regmatch, s, (colnr_T)0);
3228 else
3229 {
3230 score = fuzzy_match_str(s, pat);
3231 match = (score != 0);
3232 }
3233 }
3234 else
3235 match = TRUE; // match everything
3236
Bram Moolenaar66b51422019-08-18 21:44:12 +02003237 *e = keep;
3238
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003239 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003240 {
3241 if (ga_grow(&ga, 1) == FAIL)
3242 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003243 if (!fuzzy)
3244 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3245 else
3246 {
3247 fuzmatch_str_T *fuzmatch =
3248 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003249 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003250 fuzmatch->str = vim_strnsave(s, e - s);
3251 fuzmatch->score = score;
3252 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253 ++ga.ga_len;
3254 }
3255
3256 if (*e != NUL)
3257 ++e;
3258 }
3259 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003260
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003261 if (ga.ga_len == 0)
3262 return OK;
3263
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003264 if (!fuzzy)
3265 {
3266 *matches = ga.ga_data;
3267 *numMatches = ga.ga_len;
3268 }
3269 else
3270 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003271 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3272 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003273 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003274 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003275 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003276 return OK;
3277}
3278
3279/*
3280 * Expand names with a list returned by a function defined by the user.
3281 */
3282 static int
3283ExpandUserList(
3284 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003285 char_u ***matches,
3286 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003287{
3288 list_T *retlist;
3289 listitem_T *li;
3290 garray_T ga;
3291
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003292 *matches = NULL;
3293 *numMatches = 0;
3294 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003295 if (retlist == NULL)
3296 return FAIL;
3297
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003298 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003299 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003300 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003301 {
3302 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3303 continue; // Skip non-string items and empty strings
3304
3305 if (ga_grow(&ga, 1) == FAIL)
3306 break;
3307
3308 ((char_u **)ga.ga_data)[ga.ga_len] =
3309 vim_strsave(li->li_tv.vval.v_string);
3310 ++ga.ga_len;
3311 }
3312 list_unref(retlist);
3313
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003314 *matches = ga.ga_data;
3315 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003316 return OK;
3317}
3318# endif
3319
3320/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003321 * Expand "file" for all comma-separated directories in "path".
3322 * Adds the matches to "ga". Caller must init "ga".
3323 */
3324 void
3325globpath(
3326 char_u *path,
3327 char_u *file,
3328 garray_T *ga,
3329 int expand_options)
3330{
3331 expand_T xpc;
3332 char_u *buf;
3333 int i;
3334 int num_p;
3335 char_u **p;
3336
3337 buf = alloc(MAXPATHL);
3338 if (buf == NULL)
3339 return;
3340
3341 ExpandInit(&xpc);
3342 xpc.xp_context = EXPAND_FILES;
3343
3344 // Loop over all entries in {path}.
3345 while (*path != NUL)
3346 {
3347 // Copy one item of the path to buf[] and concatenate the file name.
3348 copy_option_part(&path, buf, MAXPATHL, ",");
3349 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3350 {
3351# if defined(MSWIN)
3352 // Using the platform's path separator (\) makes vim incorrectly
3353 // treat it as an escape character, use '/' instead.
3354 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3355 STRCAT(buf, "/");
3356# else
3357 add_pathsep(buf);
3358# endif
3359 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003360 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3362 {
3363 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3364
3365 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003366 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003367 for (i = 0; i < num_p; ++i)
3368 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003369 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003370 ++ga->ga_len;
3371 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003372 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003373 }
3374 }
3375 }
3376
3377 vim_free(buf);
3378}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003379
Bram Moolenaareadee482020-09-04 15:37:31 +02003380#ifdef FEAT_WILDMENU
3381
3382/*
3383 * Translate some keys pressed when 'wildmenu' is used.
3384 */
3385 int
3386wildmenu_translate_key(
3387 cmdline_info_T *cclp,
3388 int key,
3389 expand_T *xp,
3390 int did_wild_list)
3391{
3392 int c = key;
3393
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003394#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003395 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003396 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003397 // When the popup menu is used for cmdline completion:
3398 // Up : go to the previous item in the menu
3399 // Down : go to the next item in the menu
3400 // Left : go to the parent directory
3401 // Right: list the files in the selected directory
3402 switch (c)
3403 {
3404 case K_UP: c = K_LEFT; break;
3405 case K_DOWN: c = K_RIGHT; break;
3406 case K_LEFT: c = K_UP; break;
3407 case K_RIGHT: c = K_DOWN; break;
3408 default: break;
3409 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003410 }
3411#endif
3412
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003413 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003414 {
3415 if (c == K_LEFT)
3416 c = Ctrl_P;
3417 else if (c == K_RIGHT)
3418 c = Ctrl_N;
3419 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003420
Bram Moolenaareadee482020-09-04 15:37:31 +02003421 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003422 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003423 && cclp->cmdpos > 1
3424 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3425 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3426 && (c == '\n' || c == '\r' || c == K_KENTER))
3427 c = K_DOWN;
3428
3429 return c;
3430}
3431
3432/*
3433 * Delete characters on the command line, from "from" to the current
3434 * position.
3435 */
3436 static void
3437cmdline_del(cmdline_info_T *cclp, int from)
3438{
3439 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3440 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3441 cclp->cmdlen -= cclp->cmdpos - from;
3442 cclp->cmdpos = from;
3443}
3444
3445/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003446 * Handle a key pressed when the wild menu for the menu names
3447 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003448 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003449 static int
3450wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003451{
Bram Moolenaareadee482020-09-04 15:37:31 +02003452 int i;
3453 int j;
3454
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003455 // Hitting <Down> after "emenu Name.": complete submenu
3456 if (key == K_DOWN && cclp->cmdpos > 0
3457 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003458 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003459 key = p_wc;
3460 KeyTyped = TRUE; // in case the key was mapped
3461 }
3462 else if (key == K_UP)
3463 {
3464 // Hitting <Up>: Remove one submenu name in front of the
3465 // cursor
3466 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003467
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003468 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3469 i = 0;
3470 while (--j > 0)
3471 {
3472 // check for start of menu name
3473 if (cclp->cmdbuff[j] == ' '
3474 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003475 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003476 i = j + 1;
3477 break;
3478 }
3479 // check for start of submenu name
3480 if (cclp->cmdbuff[j] == '.'
3481 && cclp->cmdbuff[j - 1] != '\\')
3482 {
3483 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003484 {
3485 i = j + 1;
3486 break;
3487 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003488 else
3489 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003490 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003491 }
3492 if (i > 0)
3493 cmdline_del(cclp, i);
3494 key = p_wc;
3495 KeyTyped = TRUE; // in case the key was mapped
3496 xp->xp_context = EXPAND_NOTHING;
3497 }
3498
3499 return key;
3500}
3501
3502/*
3503 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3504 * directory names (EXPAND_DIRECTORIES) or shell command names
3505 * (EXPAND_SHELLCMD) is displayed.
3506 */
3507 static int
3508wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3509{
3510 int i;
3511 int j;
3512 char_u upseg[5];
3513
3514 upseg[0] = PATHSEP;
3515 upseg[1] = '.';
3516 upseg[2] = '.';
3517 upseg[3] = PATHSEP;
3518 upseg[4] = NUL;
3519
3520 if (key == K_DOWN
3521 && cclp->cmdpos > 0
3522 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3523 && (cclp->cmdpos < 3
3524 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3525 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3526 {
3527 // go down a directory
3528 key = p_wc;
3529 KeyTyped = TRUE; // in case the key was mapped
3530 }
3531 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3532 {
3533 // If in a direct ancestor, strip off one ../ to go down
3534 int found = FALSE;
3535
3536 j = cclp->cmdpos;
3537 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3538 while (--j > i)
3539 {
3540 if (has_mbyte)
3541 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3542 if (vim_ispathsep(cclp->cmdbuff[j]))
3543 {
3544 found = TRUE;
3545 break;
3546 }
3547 }
3548 if (found
3549 && cclp->cmdbuff[j - 1] == '.'
3550 && cclp->cmdbuff[j - 2] == '.'
3551 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3552 {
3553 cmdline_del(cclp, j - 2);
3554 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003555 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003556 }
3557 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003558 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003559 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003560 // go up a directory
3561 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003562
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003563 j = cclp->cmdpos - 1;
3564 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3565 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003566 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003567 if (has_mbyte)
3568 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3569 if (vim_ispathsep(cclp->cmdbuff[j])
3570# ifdef BACKSLASH_IN_FILENAME
3571 && vim_strchr((char_u *)" *?[{`$%#",
3572 cclp->cmdbuff[j + 1]) == NULL
3573# endif
3574 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003575 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003576 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003577 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003578 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003579 break;
3580 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003581 else
3582 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003583 }
3584 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003585
3586 if (!found)
3587 j = i;
3588 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3589 j += 4;
3590 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3591 && j == i)
3592 j += 3;
3593 else
3594 j = 0;
3595 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003596 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003597 // TODO this is only for DOS/UNIX systems - need to put in
3598 // machine-specific stuff here and in upseg init
3599 cmdline_del(cclp, j);
3600 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003601 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003602 else if (cclp->cmdpos > i)
3603 cmdline_del(cclp, i);
3604
3605 // Now complete in the new directory. Set KeyTyped in case the
3606 // Up key came from a mapping.
3607 key = p_wc;
3608 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003609 }
3610
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003611 return key;
3612}
3613
3614/*
3615 * Handle a key pressed when the wild menu is displayed
3616 */
3617 int
3618wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3619{
3620 if (xp->xp_context == EXPAND_MENUNAMES)
3621 return wildmenu_process_key_menunames(cclp, key, xp);
3622 else if ((xp->xp_context == EXPAND_FILES
3623 || xp->xp_context == EXPAND_DIRECTORIES
3624 || xp->xp_context == EXPAND_SHELLCMD))
3625 return wildmenu_process_key_filenames(cclp, key, xp);
3626
3627 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003628}
3629
3630/*
3631 * Free expanded names when finished walking through the matches
3632 */
3633 void
3634wildmenu_cleanup(cmdline_info_T *cclp)
3635{
3636 int skt = KeyTyped;
3637 int old_RedrawingDisabled = RedrawingDisabled;
3638
3639 if (!p_wmnu || wild_menu_showing == 0)
3640 return;
3641
3642 if (cclp->input_fn)
3643 RedrawingDisabled = 0;
3644
3645 if (wild_menu_showing == WM_SCROLLED)
3646 {
3647 // Entered command line, move it up
3648 cmdline_row--;
3649 redrawcmd();
3650 }
3651 else if (save_p_ls != -1)
3652 {
3653 // restore 'laststatus' and 'winminheight'
3654 p_ls = save_p_ls;
3655 p_wmh = save_p_wmh;
3656 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003657 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003658 redrawcmd();
3659 save_p_ls = -1;
3660 }
3661 else
3662 {
3663 win_redraw_last_status(topframe);
3664 redraw_statuslines();
3665 }
3666 KeyTyped = skt;
3667 wild_menu_showing = 0;
3668 if (cclp->input_fn)
3669 RedrawingDisabled = old_RedrawingDisabled;
3670}
3671#endif
3672
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003673#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003674/*
3675 * "getcompletion()" function
3676 */
3677 void
3678f_getcompletion(typval_T *argvars, typval_T *rettv)
3679{
3680 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003681 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003682 expand_T xpc;
3683 int filtered = FALSE;
3684 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003685 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003686
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003687 if (in_vim9script()
3688 && (check_for_string_arg(argvars, 0) == FAIL
3689 || check_for_string_arg(argvars, 1) == FAIL
3690 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3691 return;
3692
ii144785fe02021-11-21 12:13:56 +00003693 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003694 if (argvars[1].v_type != VAR_STRING)
3695 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003696 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003697 return;
3698 }
3699 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003700
3701 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003702 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003703
3704 if (p_wic)
3705 options |= WILD_ICASE;
3706
3707 // For filtered results, 'wildignore' is used
3708 if (!filtered)
3709 options |= WILD_KEEP_ALL;
3710
3711 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003712 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003713 {
ii144785fe02021-11-21 12:13:56 +00003714 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003715 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003716 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003717 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003718 else
3719 {
ii144785fe02021-11-21 12:13:56 +00003720 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003721 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3722
3723 xpc.xp_context = cmdcomplete_str_to_type(type);
3724 if (xpc.xp_context == EXPAND_NOTHING)
3725 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003726 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003727 return;
3728 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003729
3730# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003731 if (xpc.xp_context == EXPAND_MENUS)
3732 {
3733 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3734 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3735 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003736# endif
3737# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003738 if (xpc.xp_context == EXPAND_CSCOPE)
3739 {
3740 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3741 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3742 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003743# endif
3744# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003745 if (xpc.xp_context == EXPAND_SIGN)
3746 {
3747 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3748 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3749 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003750# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003751 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003752
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00003753 if (cmdline_fuzzy_completion_supported(&xpc))
3754 // when fuzzy matching, don't modify the search string
3755 pat = vim_strsave(xpc.xp_pattern);
3756 else
3757 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3758
Bram Moolenaar93a10962022-06-16 11:42:09 +01003759 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003760 {
3761 int i;
3762
3763 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3764
3765 for (i = 0; i < xpc.xp_numfiles; i++)
3766 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3767 }
3768 vim_free(pat);
3769 ExpandCleanup(&xpc);
3770}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003771#endif // FEAT_EVAL