blob: fb329a57d7f22318e90cfc8e51d5b62ddfab7103 [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
Bram Moolenaar57e95172022-08-20 19:26:14 +0100725 // Concatenate all matching names. Unless interrupted, this can be slow
726 // and the result probably won't be used.
727 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200728 {
729 len = 0;
730 for (i = 0; i < xp->xp_numfiles; ++i)
731 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
732 ss = alloc(len);
733 if (ss != NULL)
734 {
735 *ss = NUL;
736 for (i = 0; i < xp->xp_numfiles; ++i)
737 {
738 STRCAT(ss, xp->xp_files[i]);
739 if (i != xp->xp_numfiles - 1)
740 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
741 }
742 }
743 }
744
745 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
746 ExpandCleanup(xp);
747
748 // Free "orig" if it wasn't stored in "orig_save".
749 if (!orig_saved)
750 vim_free(orig);
751
752 return ss;
753}
754
755/*
756 * Prepare an expand structure for use.
757 */
758 void
759ExpandInit(expand_T *xp)
760{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200761 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200762 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200763 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200764}
765
766/*
767 * Cleanup an expand structure after use.
768 */
769 void
770ExpandCleanup(expand_T *xp)
771{
772 if (xp->xp_numfiles >= 0)
773 {
774 FreeWild(xp->xp_numfiles, xp->xp_files);
775 xp->xp_numfiles = -1;
776 }
777}
778
779/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000780 * Display one line of completion matches. Multiple matches are displayed in
781 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000782 * matches - list of completion match names
783 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000784 * lines - number of output lines
785 * linenr - line number of matches to display
786 * maxlen - maximum number of characters in each line
787 * showtail - display only the tail of the full path of a file name
788 * dir_attr - highlight attribute to use for directory names
789 */
790 static void
791showmatches_oneline(
792 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000793 char_u **matches,
794 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000795 int lines,
796 int linenr,
797 int maxlen,
798 int showtail,
799 int dir_attr)
800{
801 int i, j;
802 int isdir;
803 int lastlen;
804 char_u *p;
805
806 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000807 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000808 {
809 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
810 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000811 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
812 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000813 msg_advance(maxlen + 1);
814 msg_puts((char *)p);
815 msg_advance(maxlen + 3);
816 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
817 break;
818 }
819 for (i = maxlen - lastlen; --i >= 0; )
820 msg_putchar(' ');
821 if (xp->xp_context == EXPAND_FILES
822 || xp->xp_context == EXPAND_SHELLCMD
823 || xp->xp_context == EXPAND_BUFFERS)
824 {
825 // highlight directories
826 if (xp->xp_numfiles != -1)
827 {
828 char_u *halved_slash;
829 char_u *exp_path;
830 char_u *path;
831
832 // Expansion was done before and special characters
833 // were escaped, need to halve backslashes. Also
834 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000835 exp_path = expand_env_save_opt(matches[j], TRUE);
836 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000837 halved_slash = backslash_halve_save(path);
838 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000839 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000840 vim_free(exp_path);
841 if (halved_slash != path)
842 vim_free(halved_slash);
843 }
844 else
845 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000846 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000847 if (showtail)
848 p = SHOW_FILE_TEXT(j);
849 else
850 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000851 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000852 TRUE);
853 p = NameBuff;
854 }
855 }
856 else
857 {
858 isdir = FALSE;
859 p = SHOW_FILE_TEXT(j);
860 }
861 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
862 }
863 if (msg_col > 0) // when not wrapped around
864 {
865 msg_clr_eos();
866 msg_putchar('\n');
867 }
868 out_flush(); // show one line at a time
869}
870
871/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200872 * Show all matches for completion on the command line.
873 * Returns EXPAND_NOTHING when the character that triggered expansion should
874 * be inserted like a normal character.
875 */
876 int
877showmatches(expand_T *xp, int wildmenu UNUSED)
878{
879 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000880 int numMatches;
881 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000882 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200883 int maxlen;
884 int lines;
885 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200886 int attr;
887 int showtail;
888
889 if (xp->xp_numfiles == -1)
890 {
891 set_expand_context(xp);
892 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000893 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200894 showtail = expand_showtail(xp);
895 if (i != EXPAND_OK)
896 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200897 }
898 else
899 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000900 numMatches = xp->xp_numfiles;
901 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200902 showtail = cmd_showtail;
903 }
904
905#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000906 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000907 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000908 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000909#endif
910
911#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200912 if (!wildmenu)
913 {
914#endif
915 msg_didany = FALSE; // lines_left will be set
916 msg_start(); // prepare for paging
917 msg_putchar('\n');
918 out_flush();
919 cmdline_row = msg_row;
920 msg_didany = FALSE; // lines_left will be set again
921 msg_start(); // prepare for paging
922#ifdef FEAT_WILDMENU
923 }
924#endif
925
926 if (got_int)
927 got_int = FALSE; // only int. the completion, not the cmd line
928#ifdef FEAT_WILDMENU
929 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000930 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200931#endif
932 else
933 {
934 // find the length of the longest file name
935 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000936 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200937 {
938 if (!showtail && (xp->xp_context == EXPAND_FILES
939 || xp->xp_context == EXPAND_SHELLCMD
940 || xp->xp_context == EXPAND_BUFFERS))
941 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000942 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200943 j = vim_strsize(NameBuff);
944 }
945 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000946 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200947 if (j > maxlen)
948 maxlen = j;
949 }
950
951 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000952 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200953 else
954 {
955 // compute the number of columns and lines for the listing
956 maxlen += 2; // two spaces between file names
957 columns = ((int)Columns + 2) / maxlen;
958 if (columns < 1)
959 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000960 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200961 }
962
963 attr = HL_ATTR(HLF_D); // find out highlighting for directories
964
965 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
966 {
967 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
968 msg_clr_eos();
969 msg_advance(maxlen - 3);
970 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
971 }
972
973 // list the files line by line
974 for (i = 0; i < lines; ++i)
975 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000976 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000977 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200978 if (got_int)
979 {
980 got_int = FALSE;
981 break;
982 }
983 }
984
985 // we redraw the command below the lines that we have just listed
986 // This is a bit tricky, but it saves a lot of screen updating.
987 cmdline_row = msg_row; // will put it back later
988 }
989
990 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000991 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200992
993 return EXPAND_OK;
994}
995
996/*
997 * Private gettail for showmatches() (and win_redr_status_matches()):
998 * Find tail of file name path, but ignore trailing "/".
999 */
1000 char_u *
1001sm_gettail(char_u *s)
1002{
1003 char_u *p;
1004 char_u *t = s;
1005 int had_sep = FALSE;
1006
1007 for (p = s; *p != NUL; )
1008 {
1009 if (vim_ispathsep(*p)
1010#ifdef BACKSLASH_IN_FILENAME
1011 && !rem_backslash(p)
1012#endif
1013 )
1014 had_sep = TRUE;
1015 else if (had_sep)
1016 {
1017 t = p;
1018 had_sep = FALSE;
1019 }
1020 MB_PTR_ADV(p);
1021 }
1022 return t;
1023}
1024
1025/*
1026 * Return TRUE if we only need to show the tail of completion matches.
1027 * When not completing file names or there is a wildcard in the path FALSE is
1028 * returned.
1029 */
1030 static int
1031expand_showtail(expand_T *xp)
1032{
1033 char_u *s;
1034 char_u *end;
1035
1036 // When not completing file names a "/" may mean something different.
1037 if (xp->xp_context != EXPAND_FILES
1038 && xp->xp_context != EXPAND_SHELLCMD
1039 && xp->xp_context != EXPAND_DIRECTORIES)
1040 return FALSE;
1041
1042 end = gettail(xp->xp_pattern);
1043 if (end == xp->xp_pattern) // there is no path separator
1044 return FALSE;
1045
1046 for (s = xp->xp_pattern; s < end; s++)
1047 {
1048 // Skip escaped wildcards. Only when the backslash is not a path
1049 // separator, on DOS the '*' "path\*\file" must not be skipped.
1050 if (rem_backslash(s))
1051 ++s;
1052 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1053 return FALSE;
1054 }
1055 return TRUE;
1056}
1057
1058/*
1059 * Prepare a string for expansion.
1060 * When expanding file names: The string will be used with expand_wildcards().
1061 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1062 * When expanding other names: The string will be used with regcomp(). Copy
1063 * the name into allocated memory and prepend "^".
1064 */
1065 char_u *
1066addstar(
1067 char_u *fname,
1068 int len,
1069 int context) // EXPAND_FILES etc.
1070{
1071 char_u *retval;
1072 int i, j;
1073 int new_len;
1074 char_u *tail;
1075 int ends_in_star;
1076
1077 if (context != EXPAND_FILES
1078 && context != EXPAND_FILES_IN_PATH
1079 && context != EXPAND_SHELLCMD
1080 && context != EXPAND_DIRECTORIES)
1081 {
1082 // Matching will be done internally (on something other than files).
1083 // So we convert the file-matching-type wildcards into our kind for
1084 // use with vim_regcomp(). First work out how long it will be:
1085
1086 // For help tags the translation is done in find_help_tags().
1087 // For a tag pattern starting with "/" no translation is needed.
1088 if (context == EXPAND_HELP
1089 || context == EXPAND_COLORS
1090 || context == EXPAND_COMPILER
1091 || context == EXPAND_OWNSYNTAX
1092 || context == EXPAND_FILETYPE
1093 || context == EXPAND_PACKADD
1094 || ((context == EXPAND_TAGS_LISTFILES
1095 || context == EXPAND_TAGS)
1096 && fname[0] == '/'))
1097 retval = vim_strnsave(fname, len);
1098 else
1099 {
1100 new_len = len + 2; // +2 for '^' at start, NUL at end
1101 for (i = 0; i < len; i++)
1102 {
1103 if (fname[i] == '*' || fname[i] == '~')
1104 new_len++; // '*' needs to be replaced by ".*"
1105 // '~' needs to be replaced by "\~"
1106
1107 // Buffer names are like file names. "." should be literal
1108 if (context == EXPAND_BUFFERS && fname[i] == '.')
1109 new_len++; // "." becomes "\."
1110
1111 // Custom expansion takes care of special things, match
1112 // backslashes literally (perhaps also for other types?)
1113 if ((context == EXPAND_USER_DEFINED
1114 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1115 new_len++; // '\' becomes "\\"
1116 }
1117 retval = alloc(new_len);
1118 if (retval != NULL)
1119 {
1120 retval[0] = '^';
1121 j = 1;
1122 for (i = 0; i < len; i++, j++)
1123 {
1124 // Skip backslash. But why? At least keep it for custom
1125 // expansion.
1126 if (context != EXPAND_USER_DEFINED
1127 && context != EXPAND_USER_LIST
1128 && fname[i] == '\\'
1129 && ++i == len)
1130 break;
1131
1132 switch (fname[i])
1133 {
1134 case '*': retval[j++] = '.';
1135 break;
1136 case '~': retval[j++] = '\\';
1137 break;
1138 case '?': retval[j] = '.';
1139 continue;
1140 case '.': if (context == EXPAND_BUFFERS)
1141 retval[j++] = '\\';
1142 break;
1143 case '\\': if (context == EXPAND_USER_DEFINED
1144 || context == EXPAND_USER_LIST)
1145 retval[j++] = '\\';
1146 break;
1147 }
1148 retval[j] = fname[i];
1149 }
1150 retval[j] = NUL;
1151 }
1152 }
1153 }
1154 else
1155 {
1156 retval = alloc(len + 4);
1157 if (retval != NULL)
1158 {
1159 vim_strncpy(retval, fname, len);
1160
1161 // Don't add a star to *, ~, ~user, $var or `cmd`.
1162 // * would become **, which walks the whole tree.
1163 // ~ would be at the start of the file name, but not the tail.
1164 // $ could be anywhere in the tail.
1165 // ` could be anywhere in the file name.
1166 // When the name ends in '$' don't add a star, remove the '$'.
1167 tail = gettail(retval);
1168 ends_in_star = (len > 0 && retval[len - 1] == '*');
1169#ifndef BACKSLASH_IN_FILENAME
1170 for (i = len - 2; i >= 0; --i)
1171 {
1172 if (retval[i] != '\\')
1173 break;
1174 ends_in_star = !ends_in_star;
1175 }
1176#endif
1177 if ((*retval != '~' || tail != retval)
1178 && !ends_in_star
1179 && vim_strchr(tail, '$') == NULL
1180 && vim_strchr(retval, '`') == NULL)
1181 retval[len++] = '*';
1182 else if (len > 0 && retval[len - 1] == '$')
1183 --len;
1184 retval[len] = NUL;
1185 }
1186 }
1187 return retval;
1188}
1189
1190/*
1191 * Must parse the command line so far to work out what context we are in.
1192 * Completion can then be done based on that context.
1193 * This routine sets the variables:
1194 * xp->xp_pattern The start of the pattern to be expanded within
1195 * the command line (ends at the cursor).
1196 * xp->xp_context The type of thing to expand. Will be one of:
1197 *
1198 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1199 * the command line, like an unknown command. Caller
1200 * should beep.
1201 * EXPAND_NOTHING Unrecognised context for completion, use char like
1202 * a normal char, rather than for completion. eg
1203 * :s/^I/
1204 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1205 * it.
1206 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1207 * EXPAND_FILES After command with EX_XFILE set, or after setting
1208 * with P_EXPAND set. eg :e ^I, :w>>^I
1209 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1210 * when we know only directories are of interest. eg
1211 * :set dir=^I
1212 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1213 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1214 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1215 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1216 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1217 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1218 * EXPAND_EVENTS Complete event names
1219 * EXPAND_SYNTAX Complete :syntax command arguments
1220 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1221 * EXPAND_AUGROUP Complete autocommand group names
1222 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1223 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1224 * eg :unmap a^I , :cunab x^I
1225 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1226 * eg :call sub^I
1227 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1228 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1229 * names in expressions, eg :while s^I
1230 * EXPAND_ENV_VARS Complete environment variable names
1231 * EXPAND_USER Complete user names
1232 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001233 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001234set_expand_context(expand_T *xp)
1235{
1236 cmdline_info_T *ccline = get_cmdline_info();
1237
1238 // only expansion for ':', '>' and '=' command-lines
1239 if (ccline->cmdfirstc != ':'
1240#ifdef FEAT_EVAL
1241 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1242 && !ccline->input_fn
1243#endif
1244 )
1245 {
1246 xp->xp_context = EXPAND_NOTHING;
1247 return;
1248 }
1249 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1250}
1251
Bram Moolenaard0190392019-08-23 21:17:35 +02001252/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001253 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1254 * For user defined commands, the completion context is set in 'xp' and the
1255 * completion flags in 'complp'.
1256 *
1257 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001258 */
1259 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001260set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001261{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001262 char_u *p = NULL;
1263 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001264 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001265
1266 // Isolate the command and search for it in the command table.
1267 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001268 // - the 'k' command can directly be followed by any character, but do
1269 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1270 // find matches anywhere in the command name, do this only for command
1271 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001272 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001273 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001274 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001275 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001276 p = cmd + 1;
1277 }
1278 else
1279 {
1280 p = cmd;
1281 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1282 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001283 // A user command may contain digits.
1284 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1285 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001286 while (ASCII_ISALNUM(*p) || *p == '*')
1287 ++p;
1288 // for python 3.x: ":py3*" commands completion
1289 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1290 {
1291 ++p;
1292 while (ASCII_ISALPHA(*p) || *p == '*')
1293 ++p;
1294 }
1295 // check for non-alpha command
1296 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1297 ++p;
1298 len = (int)(p - cmd);
1299
1300 if (len == 0)
1301 {
1302 xp->xp_context = EXPAND_UNSUCCESSFUL;
1303 return NULL;
1304 }
1305
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001306 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001307
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001308 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001309 // Also when doing fuzzy expansion for non-shell commands, support
1310 // alphanumeric characters.
1311 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1312 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001313 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1314 ++p;
1315 }
1316
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001317 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001318 // character, complete the command name.
1319 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1320 return NULL;
1321
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001322 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001323 {
1324 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1325 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001326 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001327 p = cmd + 1;
1328 }
1329 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1330 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001331 eap->cmd = cmd;
1332 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001333 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001334 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001335 }
1336 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001337 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001338 {
1339 // Not still touching the command and it was an illegal one
1340 xp->xp_context = EXPAND_UNSUCCESSFUL;
1341 return NULL;
1342 }
1343
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001344 return p;
1345}
Bram Moolenaard0190392019-08-23 21:17:35 +02001346
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001347/*
1348 * Set the completion context for a command argument with wild card characters.
1349 */
1350 static void
1351set_context_for_wildcard_arg(
1352 exarg_T *eap,
1353 char_u *arg,
1354 int usefilter,
1355 expand_T *xp,
1356 int *complp)
1357{
1358 char_u *p;
1359 int c;
1360 int in_quote = FALSE;
1361 char_u *bow = NULL; // Beginning of word
1362 int len = 0;
1363
1364 // Allow spaces within back-quotes to count as part of the argument
1365 // being expanded.
1366 xp->xp_pattern = skipwhite(arg);
1367 p = xp->xp_pattern;
1368 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001369 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001370 if (has_mbyte)
1371 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001372 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001373 c = *p;
1374 if (c == '\\' && p[1] != NUL)
1375 ++p;
1376 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001377 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001378 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001379 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001380 xp->xp_pattern = p;
1381 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001382 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001383 in_quote = !in_quote;
1384 }
1385 // An argument can contain just about everything, except
1386 // characters that end the command and white space.
1387 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001388#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001389 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001390#endif
1391 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001392 {
1393 len = 0; // avoid getting stuck when space is in 'isfname'
1394 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001395 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001396 if (has_mbyte)
1397 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001398 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001399 c = *p;
1400 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001401 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001402 if (has_mbyte)
1403 len = (*mb_ptr2len)(p);
1404 else
1405 len = 1;
1406 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001407 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001408 if (in_quote)
1409 bow = p;
1410 else
1411 xp->xp_pattern = p;
1412 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001413 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001414 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001415 }
1416
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001417 // If we are still inside the quotes, and we passed a space, just
1418 // expand from there.
1419 if (bow != NULL && in_quote)
1420 xp->xp_pattern = bow;
1421 xp->xp_context = EXPAND_FILES;
1422
1423 // For a shell command more chars need to be escaped.
1424 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1425 {
1426#ifndef BACKSLASH_IN_FILENAME
1427 xp->xp_shell = TRUE;
1428#endif
1429 // When still after the command name expand executables.
1430 if (xp->xp_pattern == skipwhite(arg))
1431 xp->xp_context = EXPAND_SHELLCMD;
1432 }
1433
1434 // Check for environment variable.
1435 if (*xp->xp_pattern == '$')
1436 {
1437 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1438 if (!vim_isIDc(*p))
1439 break;
1440 if (*p == NUL)
1441 {
1442 xp->xp_context = EXPAND_ENV_VARS;
1443 ++xp->xp_pattern;
1444 // Avoid that the assignment uses EXPAND_FILES again.
1445 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1446 *complp = EXPAND_ENV_VARS;
1447 }
1448 }
1449 // Check for user names.
1450 if (*xp->xp_pattern == '~')
1451 {
1452 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1453 ;
1454 // Complete ~user only if it partially matches a user name.
1455 // A full match ~user<Tab> will be replaced by user's home
1456 // directory i.e. something like ~user<Tab> -> /home/user/
1457 if (*p == NUL && p > xp->xp_pattern + 1
1458 && match_user(xp->xp_pattern + 1) >= 1)
1459 {
1460 xp->xp_context = EXPAND_USER;
1461 ++xp->xp_pattern;
1462 }
1463 }
1464}
1465
1466/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001467 * Set the completion context for the :filter command. Returns a pointer to the
1468 * next command after the :filter command.
1469 */
1470 static char_u *
1471set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1472{
1473 if (*arg != NUL)
1474 arg = skip_vimgrep_pat(arg, NULL, NULL);
1475 if (arg == NULL || *arg == NUL)
1476 {
1477 xp->xp_context = EXPAND_NOTHING;
1478 return NULL;
1479 }
1480 return skipwhite(arg);
1481}
1482
1483#ifdef FEAT_SEARCH_EXTRA
1484/*
1485 * Set the completion context for the :match command. Returns a pointer to the
1486 * next command after the :match command.
1487 */
1488 static char_u *
1489set_context_in_match_cmd(expand_T *xp, char_u *arg)
1490{
1491 if (*arg == NUL || !ends_excmd(*arg))
1492 {
1493 // also complete "None"
1494 set_context_in_echohl_cmd(xp, arg);
1495 arg = skipwhite(skiptowhite(arg));
1496 if (*arg != NUL)
1497 {
1498 xp->xp_context = EXPAND_NOTHING;
1499 arg = skip_regexp(arg + 1, *arg, magic_isset());
1500 }
1501 }
1502 return find_nextcmd(arg);
1503}
1504#endif
1505
1506/*
1507 * Returns a pointer to the next command after a :global or a :v command.
1508 * Returns NULL if there is no next command.
1509 */
1510 static char_u *
1511find_cmd_after_global_cmd(char_u *arg)
1512{
1513 int delim;
1514
1515 delim = *arg; // get the delimiter
1516 if (delim)
1517 ++arg; // skip delimiter if there is one
1518
1519 while (arg[0] != NUL && arg[0] != delim)
1520 {
1521 if (arg[0] == '\\' && arg[1] != NUL)
1522 ++arg;
1523 ++arg;
1524 }
1525 if (arg[0] != NUL)
1526 return arg + 1;
1527
1528 return NULL;
1529}
1530
1531/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001532 * Returns a pointer to the next command after a :substitute or a :& command.
1533 * Returns NULL if there is no next command.
1534 */
1535 static char_u *
1536find_cmd_after_substitute_cmd(char_u *arg)
1537{
1538 int delim;
1539
1540 delim = *arg;
1541 if (delim)
1542 {
1543 // skip "from" part
1544 ++arg;
1545 arg = skip_regexp(arg, delim, magic_isset());
1546
1547 if (arg[0] != NUL && arg[0] == delim)
1548 {
1549 // skip "to" part
1550 ++arg;
1551 while (arg[0] != NUL && arg[0] != delim)
1552 {
1553 if (arg[0] == '\\' && arg[1] != NUL)
1554 ++arg;
1555 ++arg;
1556 }
1557 if (arg[0] != NUL) // skip delimiter
1558 ++arg;
1559 }
1560 }
1561 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1562 ++arg;
1563 if (arg[0] != NUL)
1564 return arg;
1565
1566 return NULL;
1567}
1568
1569/*
1570 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1571 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1572 * Returns NULL if there is no next command.
1573 */
1574 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001575find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001576{
1577 arg = skipwhite(skipdigits(arg)); // skip count
1578 if (*arg == '/') // Match regexp, not just whole words
1579 {
1580 for (++arg; *arg && *arg != '/'; arg++)
1581 if (*arg == '\\' && arg[1] != NUL)
1582 arg++;
1583 if (*arg)
1584 {
1585 arg = skipwhite(arg + 1);
1586
1587 // Check for trailing illegal characters
1588 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1589 xp->xp_context = EXPAND_NOTHING;
1590 else
1591 return arg;
1592 }
1593 }
1594
1595 return NULL;
1596}
1597
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001598#ifdef FEAT_EVAL
1599/*
1600 * Set the completion context for the :unlet command. Always returns NULL.
1601 */
1602 static char_u *
1603set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1604{
1605 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1606 arg = xp->xp_pattern + 1;
1607
1608 xp->xp_context = EXPAND_USER_VARS;
1609 xp->xp_pattern = arg;
1610
1611 if (*xp->xp_pattern == '$')
1612 {
1613 xp->xp_context = EXPAND_ENV_VARS;
1614 ++xp->xp_pattern;
1615 }
1616
1617 return NULL;
1618}
1619#endif
1620
1621#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1622/*
1623 * Set the completion context for the :language command. Always returns NULL.
1624 */
1625 static char_u *
1626set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1627{
1628 char_u *p;
1629
1630 p = skiptowhite(arg);
1631 if (*p == NUL)
1632 {
1633 xp->xp_context = EXPAND_LANGUAGE;
1634 xp->xp_pattern = arg;
1635 }
1636 else
1637 {
1638 if ( STRNCMP(arg, "messages", p - arg) == 0
1639 || STRNCMP(arg, "ctype", p - arg) == 0
1640 || STRNCMP(arg, "time", p - arg) == 0
1641 || STRNCMP(arg, "collate", p - arg) == 0)
1642 {
1643 xp->xp_context = EXPAND_LOCALES;
1644 xp->xp_pattern = skipwhite(p);
1645 }
1646 else
1647 xp->xp_context = EXPAND_NOTHING;
1648 }
1649
1650 return NULL;
1651}
1652#endif
1653
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001654#ifdef FEAT_EVAL
1655static enum
1656{
1657 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001658 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1659 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001660} breakpt_expand_what;
1661
1662/*
1663 * Set the completion context for the :breakadd command. Always returns NULL.
1664 */
1665 static char_u *
1666set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1667{
1668 char_u *p;
1669 char_u *subcmd_start;
1670
1671 xp->xp_context = EXPAND_BREAKPOINT;
1672 xp->xp_pattern = arg;
1673
1674 if (cmdidx == CMD_breakadd)
1675 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001676 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001677 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001678 else
1679 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001680
1681 p = skipwhite(arg);
1682 if (*p == NUL)
1683 return NULL;
1684 subcmd_start = p;
1685
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001686 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001687 {
1688 // :breakadd file [lnum] <filename>
1689 // :breakadd func [lnum] <funcname>
1690 p += 4;
1691 p = skipwhite(p);
1692
1693 // skip line number (if specified)
1694 if (VIM_ISDIGIT(*p))
1695 {
1696 p = skipdigits(p);
1697 if (*p != ' ')
1698 {
1699 xp->xp_context = EXPAND_NOTHING;
1700 return NULL;
1701 }
1702 p = skipwhite(p);
1703 }
1704 if (STRNCMP("file", subcmd_start, 4) == 0)
1705 xp->xp_context = EXPAND_FILES;
1706 else
1707 xp->xp_context = EXPAND_USER_FUNC;
1708 xp->xp_pattern = p;
1709 }
1710 else if (STRNCMP("expr ", p, 5) == 0)
1711 {
1712 // :breakadd expr <expression>
1713 xp->xp_context = EXPAND_EXPRESSION;
1714 xp->xp_pattern = skipwhite(p + 5);
1715 }
1716
1717 return NULL;
1718}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001719
1720 static char_u *
1721set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1722{
1723 char_u *p;
1724
1725 xp->xp_context = EXPAND_NOTHING;
1726 xp->xp_pattern = NULL;
1727
1728 p = skipwhite(arg);
1729 if (VIM_ISDIGIT(*p))
1730 return NULL;
1731
1732 xp->xp_context = EXPAND_SCRIPTNAMES;
1733 xp->xp_pattern = p;
1734
1735 return NULL;
1736}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001737#endif
1738
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001739/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001740 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1741 * The argument to the command is 'arg' and the argument flags is 'argt'.
1742 * For user-defined commands and for environment variables, 'compl' has the
1743 * completion type.
1744 * Returns a pointer to the next command. Returns NULL if there is no next
1745 * command.
1746 */
1747 static char_u *
1748set_context_by_cmdname(
1749 char_u *cmd,
1750 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001751 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001752 char_u *arg,
1753 long argt,
1754 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001755 int forceit)
1756{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001757 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001758 {
1759 case CMD_find:
1760 case CMD_sfind:
1761 case CMD_tabfind:
1762 if (xp->xp_context == EXPAND_FILES)
1763 xp->xp_context = EXPAND_FILES_IN_PATH;
1764 break;
1765 case CMD_cd:
1766 case CMD_chdir:
1767 case CMD_tcd:
1768 case CMD_tchdir:
1769 case CMD_lcd:
1770 case CMD_lchdir:
1771 if (xp->xp_context == EXPAND_FILES)
1772 xp->xp_context = EXPAND_DIRECTORIES;
1773 break;
1774 case CMD_help:
1775 xp->xp_context = EXPAND_HELP;
1776 xp->xp_pattern = arg;
1777 break;
1778
1779 // Command modifiers: return the argument.
1780 // Also for commands with an argument that is a command.
1781 case CMD_aboveleft:
1782 case CMD_argdo:
1783 case CMD_belowright:
1784 case CMD_botright:
1785 case CMD_browse:
1786 case CMD_bufdo:
1787 case CMD_cdo:
1788 case CMD_cfdo:
1789 case CMD_confirm:
1790 case CMD_debug:
1791 case CMD_folddoclosed:
1792 case CMD_folddoopen:
1793 case CMD_hide:
1794 case CMD_keepalt:
1795 case CMD_keepjumps:
1796 case CMD_keepmarks:
1797 case CMD_keeppatterns:
1798 case CMD_ldo:
1799 case CMD_leftabove:
1800 case CMD_lfdo:
1801 case CMD_lockmarks:
1802 case CMD_noautocmd:
1803 case CMD_noswapfile:
1804 case CMD_rightbelow:
1805 case CMD_sandbox:
1806 case CMD_silent:
1807 case CMD_tab:
1808 case CMD_tabdo:
1809 case CMD_topleft:
1810 case CMD_verbose:
1811 case CMD_vertical:
1812 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001813 case CMD_vim9cmd:
1814 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001815 return arg;
1816
1817 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001818 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001819
1820#ifdef FEAT_SEARCH_EXTRA
1821 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001822 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001823#endif
1824
1825 // All completion for the +cmdline_compl feature goes here.
1826
1827 case CMD_command:
1828 return set_context_in_user_cmd(xp, arg);
1829
1830 case CMD_delcommand:
1831 xp->xp_context = EXPAND_USER_COMMANDS;
1832 xp->xp_pattern = arg;
1833 break;
1834
1835 case CMD_global:
1836 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001837 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001838 case CMD_and:
1839 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001840 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001841 case CMD_isearch:
1842 case CMD_dsearch:
1843 case CMD_ilist:
1844 case CMD_dlist:
1845 case CMD_ijump:
1846 case CMD_psearch:
1847 case CMD_djump:
1848 case CMD_isplit:
1849 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001850 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001851 case CMD_autocmd:
1852 return set_context_in_autocmd(xp, arg, FALSE);
1853 case CMD_doautocmd:
1854 case CMD_doautoall:
1855 return set_context_in_autocmd(xp, arg, TRUE);
1856 case CMD_set:
1857 set_context_in_set_cmd(xp, arg, 0);
1858 break;
1859 case CMD_setglobal:
1860 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1861 break;
1862 case CMD_setlocal:
1863 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1864 break;
1865 case CMD_tag:
1866 case CMD_stag:
1867 case CMD_ptag:
1868 case CMD_ltag:
1869 case CMD_tselect:
1870 case CMD_stselect:
1871 case CMD_ptselect:
1872 case CMD_tjump:
1873 case CMD_stjump:
1874 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001875 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001876 xp->xp_context = EXPAND_TAGS_LISTFILES;
1877 else
1878 xp->xp_context = EXPAND_TAGS;
1879 xp->xp_pattern = arg;
1880 break;
1881 case CMD_augroup:
1882 xp->xp_context = EXPAND_AUGROUP;
1883 xp->xp_pattern = arg;
1884 break;
1885#ifdef FEAT_SYN_HL
1886 case CMD_syntax:
1887 set_context_in_syntax_cmd(xp, arg);
1888 break;
1889#endif
1890#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001891 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001892 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001893 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001894 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001895 case CMD_if:
1896 case CMD_elseif:
1897 case CMD_while:
1898 case CMD_for:
1899 case CMD_echo:
1900 case CMD_echon:
1901 case CMD_execute:
1902 case CMD_echomsg:
1903 case CMD_echoerr:
1904 case CMD_call:
1905 case CMD_return:
1906 case CMD_cexpr:
1907 case CMD_caddexpr:
1908 case CMD_cgetexpr:
1909 case CMD_lexpr:
1910 case CMD_laddexpr:
1911 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001912 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001913 break;
1914
1915 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001916 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001917 case CMD_function:
1918 case CMD_delfunction:
1919 xp->xp_context = EXPAND_USER_FUNC;
1920 xp->xp_pattern = arg;
1921 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001922 case CMD_disassemble:
1923 set_context_in_disassemble_cmd(xp, arg);
1924 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001925
1926 case CMD_echohl:
1927 set_context_in_echohl_cmd(xp, arg);
1928 break;
1929#endif
1930 case CMD_highlight:
1931 set_context_in_highlight_cmd(xp, arg);
1932 break;
1933#ifdef FEAT_CSCOPE
1934 case CMD_cscope:
1935 case CMD_lcscope:
1936 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001937 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001938 break;
1939#endif
1940#ifdef FEAT_SIGNS
1941 case CMD_sign:
1942 set_context_in_sign_cmd(xp, arg);
1943 break;
1944#endif
1945 case CMD_bdelete:
1946 case CMD_bwipeout:
1947 case CMD_bunload:
1948 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1949 arg = xp->xp_pattern + 1;
1950 // FALLTHROUGH
1951 case CMD_buffer:
1952 case CMD_sbuffer:
1953 case CMD_checktime:
1954 xp->xp_context = EXPAND_BUFFERS;
1955 xp->xp_pattern = arg;
1956 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001957#ifdef FEAT_DIFF
1958 case CMD_diffget:
1959 case CMD_diffput:
1960 // If current buffer is in diff mode, complete buffer names
1961 // which are in diff mode, and different than current buffer.
1962 xp->xp_context = EXPAND_DIFF_BUFFERS;
1963 xp->xp_pattern = arg;
1964 break;
1965#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001966 case CMD_USER:
1967 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001968 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1969 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001970
1971 case CMD_map: case CMD_noremap:
1972 case CMD_nmap: case CMD_nnoremap:
1973 case CMD_vmap: case CMD_vnoremap:
1974 case CMD_omap: case CMD_onoremap:
1975 case CMD_imap: case CMD_inoremap:
1976 case CMD_cmap: case CMD_cnoremap:
1977 case CMD_lmap: case CMD_lnoremap:
1978 case CMD_smap: case CMD_snoremap:
1979 case CMD_tmap: case CMD_tnoremap:
1980 case CMD_xmap: case CMD_xnoremap:
1981 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001982 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001983 case CMD_unmap:
1984 case CMD_nunmap:
1985 case CMD_vunmap:
1986 case CMD_ounmap:
1987 case CMD_iunmap:
1988 case CMD_cunmap:
1989 case CMD_lunmap:
1990 case CMD_sunmap:
1991 case CMD_tunmap:
1992 case CMD_xunmap:
1993 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001994 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001995 case CMD_mapclear:
1996 case CMD_nmapclear:
1997 case CMD_vmapclear:
1998 case CMD_omapclear:
1999 case CMD_imapclear:
2000 case CMD_cmapclear:
2001 case CMD_lmapclear:
2002 case CMD_smapclear:
2003 case CMD_tmapclear:
2004 case CMD_xmapclear:
2005 xp->xp_context = EXPAND_MAPCLEAR;
2006 xp->xp_pattern = arg;
2007 break;
2008
2009 case CMD_abbreviate: case CMD_noreabbrev:
2010 case CMD_cabbrev: case CMD_cnoreabbrev:
2011 case CMD_iabbrev: case CMD_inoreabbrev:
2012 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002013 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002014 case CMD_unabbreviate:
2015 case CMD_cunabbrev:
2016 case CMD_iunabbrev:
2017 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002018 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002019#ifdef FEAT_MENU
2020 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2021 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2022 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2023 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2024 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2025 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2026 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2027 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2028 case CMD_tmenu: case CMD_tunmenu:
2029 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2030 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2031#endif
2032
2033 case CMD_colorscheme:
2034 xp->xp_context = EXPAND_COLORS;
2035 xp->xp_pattern = arg;
2036 break;
2037
2038 case CMD_compiler:
2039 xp->xp_context = EXPAND_COMPILER;
2040 xp->xp_pattern = arg;
2041 break;
2042
2043 case CMD_ownsyntax:
2044 xp->xp_context = EXPAND_OWNSYNTAX;
2045 xp->xp_pattern = arg;
2046 break;
2047
2048 case CMD_setfiletype:
2049 xp->xp_context = EXPAND_FILETYPE;
2050 xp->xp_pattern = arg;
2051 break;
2052
2053 case CMD_packadd:
2054 xp->xp_context = EXPAND_PACKADD;
2055 xp->xp_pattern = arg;
2056 break;
2057
2058#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2059 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002060 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002061#endif
2062#if defined(FEAT_PROFILE)
2063 case CMD_profile:
2064 set_context_in_profile_cmd(xp, arg);
2065 break;
2066#endif
2067 case CMD_behave:
2068 xp->xp_context = EXPAND_BEHAVE;
2069 xp->xp_pattern = arg;
2070 break;
2071
2072 case CMD_messages:
2073 xp->xp_context = EXPAND_MESSAGES;
2074 xp->xp_pattern = arg;
2075 break;
2076
2077 case CMD_history:
2078 xp->xp_context = EXPAND_HISTORY;
2079 xp->xp_pattern = arg;
2080 break;
2081#if defined(FEAT_PROFILE)
2082 case CMD_syntime:
2083 xp->xp_context = EXPAND_SYNTIME;
2084 xp->xp_pattern = arg;
2085 break;
2086#endif
2087
2088 case CMD_argdelete:
2089 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2090 arg = xp->xp_pattern + 1;
2091 xp->xp_context = EXPAND_ARGLIST;
2092 xp->xp_pattern = arg;
2093 break;
2094
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002095#ifdef FEAT_EVAL
2096 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002097 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002098 case CMD_breakdel:
2099 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002100
2101 case CMD_scriptnames:
2102 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002103#endif
2104
Bram Moolenaard0190392019-08-23 21:17:35 +02002105 default:
2106 break;
2107 }
2108 return NULL;
2109}
2110
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002111/*
2112 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2113 * we don't need/want deleted. Maybe this could be done better if we didn't
2114 * repeat all this stuff. The only problem is that they may not stay
2115 * perfectly compatible with each other, but then the command line syntax
2116 * probably won't change that much -- webb.
2117 */
2118 static char_u *
2119set_one_cmd_context(
2120 expand_T *xp,
2121 char_u *buff) // buffer for command string
2122{
2123 char_u *p;
2124 char_u *cmd, *arg;
2125 int len = 0;
2126 exarg_T ea;
2127 int compl = EXPAND_NOTHING;
2128 int forceit = FALSE;
2129 int usefilter = FALSE; // filter instead of file name
2130
2131 ExpandInit(xp);
2132 xp->xp_pattern = buff;
2133 xp->xp_line = buff;
2134 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2135 ea.argt = 0;
2136
2137 // 1. skip comment lines and leading space, colons or bars
2138 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2139 ;
2140 xp->xp_pattern = cmd;
2141
2142 if (*cmd == NUL)
2143 return NULL;
2144 if (*cmd == '"') // ignore comment lines
2145 {
2146 xp->xp_context = EXPAND_NOTHING;
2147 return NULL;
2148 }
2149
2150 // 3. Skip over the range to find the command.
2151 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2152 xp->xp_pattern = cmd;
2153 if (*cmd == NUL)
2154 return NULL;
2155 if (*cmd == '"')
2156 {
2157 xp->xp_context = EXPAND_NOTHING;
2158 return NULL;
2159 }
2160
2161 if (*cmd == '|' || *cmd == '\n')
2162 return cmd + 1; // There's another command
2163
2164 // Get the command index.
2165 p = set_cmd_index(cmd, &ea, xp, &compl);
2166 if (p == NULL)
2167 return NULL;
2168
2169 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2170
2171 if (*p == '!') // forced commands
2172 {
2173 forceit = TRUE;
2174 ++p;
2175 }
2176
2177 // 6. parse arguments
2178 if (!IS_USER_CMDIDX(ea.cmdidx))
2179 ea.argt = excmd_get_argt(ea.cmdidx);
2180
2181 arg = skipwhite(p);
2182
2183 // Skip over ++argopt argument
2184 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2185 {
2186 p = arg;
2187 while (*p && !vim_isspace(*p))
2188 MB_PTR_ADV(p);
2189 arg = skipwhite(p);
2190 }
2191
2192 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2193 {
2194 if (*arg == '>') // append
2195 {
2196 if (*++arg == '>')
2197 ++arg;
2198 arg = skipwhite(arg);
2199 }
2200 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2201 {
2202 ++arg;
2203 usefilter = TRUE;
2204 }
2205 }
2206
2207 if (ea.cmdidx == CMD_read)
2208 {
2209 usefilter = forceit; // :r! filter if forced
2210 if (*arg == '!') // :r !filter
2211 {
2212 ++arg;
2213 usefilter = TRUE;
2214 }
2215 }
2216
2217 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2218 {
2219 while (*arg == *cmd) // allow any number of '>' or '<'
2220 ++arg;
2221 arg = skipwhite(arg);
2222 }
2223
2224 // Does command allow "+command"?
2225 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2226 {
2227 // Check if we're in the +command
2228 p = arg + 1;
2229 arg = skip_cmd_arg(arg, FALSE);
2230
2231 // Still touching the command after '+'?
2232 if (*arg == NUL)
2233 return p;
2234
2235 // Skip space(s) after +command to get to the real argument
2236 arg = skipwhite(arg);
2237 }
2238
2239
2240 // Check for '|' to separate commands and '"' to start comments.
2241 // Don't do this for ":read !cmd" and ":write !cmd".
2242 if ((ea.argt & EX_TRLBAR) && !usefilter)
2243 {
2244 p = arg;
2245 // ":redir @" is not the start of a comment
2246 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2247 p += 2;
2248 while (*p)
2249 {
2250 if (*p == Ctrl_V)
2251 {
2252 if (p[1] != NUL)
2253 ++p;
2254 }
2255 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2256 || *p == '|' || *p == '\n')
2257 {
2258 if (*(p - 1) != '\\')
2259 {
2260 if (*p == '|' || *p == '\n')
2261 return p + 1;
2262 return NULL; // It's a comment
2263 }
2264 }
2265 MB_PTR_ADV(p);
2266 }
2267 }
2268
2269 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2270 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2271 // no arguments allowed but there is something
2272 return NULL;
2273
2274 // Find start of last argument (argument just before cursor):
2275 p = buff;
2276 xp->xp_pattern = p;
2277 len = (int)STRLEN(buff);
2278 while (*p && p < buff + len)
2279 {
2280 if (*p == ' ' || *p == TAB)
2281 {
2282 // argument starts after a space
2283 xp->xp_pattern = ++p;
2284 }
2285 else
2286 {
2287 if (*p == '\\' && *(p + 1) != NUL)
2288 ++p; // skip over escaped character
2289 MB_PTR_ADV(p);
2290 }
2291 }
2292
2293 if (ea.argt & EX_XFILE)
2294 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2295
2296 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002297 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002298 forceit);
2299}
2300
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002301/*
2302 * Set the completion context in 'xp' for command 'str'
2303 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002304 void
2305set_cmd_context(
2306 expand_T *xp,
2307 char_u *str, // start of command line
2308 int len, // length of command line (excl. NUL)
2309 int col, // position of cursor
2310 int use_ccline UNUSED) // use ccline for info
2311{
2312#ifdef FEAT_EVAL
2313 cmdline_info_T *ccline = get_cmdline_info();
2314#endif
2315 int old_char = NUL;
2316 char_u *nextcomm;
2317
2318 // Avoid a UMR warning from Purify, only save the character if it has been
2319 // written before.
2320 if (col < len)
2321 old_char = str[col];
2322 str[col] = NUL;
2323 nextcomm = str;
2324
2325#ifdef FEAT_EVAL
2326 if (use_ccline && ccline->cmdfirstc == '=')
2327 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002328 // pass CMD_SIZE because there is no real command
2329 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002330 }
2331 else if (use_ccline && ccline->input_fn)
2332 {
2333 xp->xp_context = ccline->xp_context;
2334 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002335 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002336 }
2337 else
2338#endif
2339 while (nextcomm != NULL)
2340 nextcomm = set_one_cmd_context(xp, nextcomm);
2341
2342 // Store the string here so that call_user_expand_func() can get to them
2343 // easily.
2344 xp->xp_line = str;
2345 xp->xp_col = col;
2346
2347 str[col] = old_char;
2348}
2349
2350/*
2351 * Expand the command line "str" from context "xp".
2352 * "xp" must have been set by set_cmd_context().
2353 * xp->xp_pattern points into "str", to where the text that is to be expanded
2354 * starts.
2355 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2356 * cursor.
2357 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2358 * key that triggered expansion literally.
2359 * Returns EXPAND_OK otherwise.
2360 */
2361 int
2362expand_cmdline(
2363 expand_T *xp,
2364 char_u *str, // start of command line
2365 int col, // position of cursor
2366 int *matchcount, // return: nr of matches
2367 char_u ***matches) // return: array of pointers to matches
2368{
2369 char_u *file_str = NULL;
2370 int options = WILD_ADD_SLASH|WILD_SILENT;
2371
2372 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2373 {
2374 beep_flush();
2375 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2376 }
2377 if (xp->xp_context == EXPAND_NOTHING)
2378 {
2379 // Caller can use the character as a normal char instead
2380 return EXPAND_NOTHING;
2381 }
2382
2383 // add star to file name, or convert to regexp if not exp. files.
2384 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002385 if (cmdline_fuzzy_completion_supported(xp))
2386 // If fuzzy matching, don't modify the search string
2387 file_str = vim_strsave(xp->xp_pattern);
2388 else
2389 {
2390 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2391 if (file_str == NULL)
2392 return EXPAND_UNSUCCESSFUL;
2393 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002394
2395 if (p_wic)
2396 options += WILD_ICASE;
2397
2398 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002399 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002400 {
2401 *matchcount = 0;
2402 *matches = NULL;
2403 }
2404 vim_free(file_str);
2405
2406 return EXPAND_OK;
2407}
2408
Bram Moolenaar66b51422019-08-18 21:44:12 +02002409/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002410 * Expand file or directory names.
2411 */
2412 static int
2413expand_files_and_dirs(
2414 expand_T *xp,
2415 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002416 char_u ***matches,
2417 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002418 int flags,
2419 int options)
2420{
2421 int free_pat = FALSE;
2422 int i;
2423 int ret;
2424
2425 // for ":set path=" and ":set tags=" halve backslashes for escaped
2426 // space
2427 if (xp->xp_backslash != XP_BS_NONE)
2428 {
2429 free_pat = TRUE;
2430 pat = vim_strsave(pat);
2431 for (i = 0; pat[i]; ++i)
2432 if (pat[i] == '\\')
2433 {
2434 if (xp->xp_backslash == XP_BS_THREE
2435 && pat[i + 1] == '\\'
2436 && pat[i + 2] == '\\'
2437 && pat[i + 3] == ' ')
2438 STRMOVE(pat + i, pat + i + 3);
2439 if (xp->xp_backslash == XP_BS_ONE
2440 && pat[i + 1] == ' ')
2441 STRMOVE(pat + i, pat + i + 1);
2442 }
2443 }
2444
2445 if (xp->xp_context == EXPAND_FILES)
2446 flags |= EW_FILE;
2447 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2448 flags |= (EW_FILE | EW_PATH);
2449 else
2450 flags = (flags | EW_DIR) & ~EW_FILE;
2451 if (options & WILD_ICASE)
2452 flags |= EW_ICASE;
2453
2454 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002455 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002456 if (free_pat)
2457 vim_free(pat);
2458#ifdef BACKSLASH_IN_FILENAME
2459 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2460 {
2461 int j;
2462
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002463 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002464 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002465 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002466
2467 while (*ptr != NUL)
2468 {
2469 if (p_csl[0] == 's' && *ptr == '\\')
2470 *ptr = '/';
2471 else if (p_csl[0] == 'b' && *ptr == '/')
2472 *ptr = '\\';
2473 ptr += (*mb_ptr2len)(ptr);
2474 }
2475 }
2476 }
2477#endif
2478 return ret;
2479}
2480
2481/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002482 * Function given to ExpandGeneric() to obtain the possible arguments of the
2483 * ":behave {mswin,xterm}" command.
2484 */
2485 static char_u *
2486get_behave_arg(expand_T *xp UNUSED, int idx)
2487{
2488 if (idx == 0)
2489 return (char_u *)"mswin";
2490 if (idx == 1)
2491 return (char_u *)"xterm";
2492 return NULL;
2493}
2494
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002495# ifdef FEAT_EVAL
2496/*
2497 * Function given to ExpandGeneric() to obtain the possible arguments of the
2498 * ":breakadd {expr, file, func, here}" command.
2499 * ":breakdel {func, file, here}" command.
2500 */
2501 static char_u *
2502get_breakadd_arg(expand_T *xp UNUSED, int idx)
2503{
2504 char *opts[] = {"expr", "file", "func", "here"};
2505
2506 if (idx >=0 && idx <= 3)
2507 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002508 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002509 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2510 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002511 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2512 {
2513 // breakdel {func, file, here}
2514 if (idx <= 2)
2515 return (char_u *)opts[idx + 1];
2516 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002517 else
2518 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002519 // profdel {func, file}
2520 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002521 return (char_u *)opts[idx + 1];
2522 }
2523 }
2524 return NULL;
2525}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002526
2527/*
2528 * Function given to ExpandGeneric() to obtain the possible arguments for the
2529 * ":scriptnames" command.
2530 */
2531 static char_u *
2532get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2533{
2534 scriptitem_T *si;
2535
2536 if (!SCRIPT_ID_VALID(idx + 1))
2537 return NULL;
2538
2539 si = SCRIPT_ITEM(idx + 1);
2540 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2541 return NameBuff;
2542}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002543#endif
2544
Bram Moolenaard0190392019-08-23 21:17:35 +02002545/*
2546 * Function given to ExpandGeneric() to obtain the possible arguments of the
2547 * ":messages {clear}" command.
2548 */
2549 static char_u *
2550get_messages_arg(expand_T *xp UNUSED, int idx)
2551{
2552 if (idx == 0)
2553 return (char_u *)"clear";
2554 return NULL;
2555}
2556
2557 static char_u *
2558get_mapclear_arg(expand_T *xp UNUSED, int idx)
2559{
2560 if (idx == 0)
2561 return (char_u *)"<buffer>";
2562 return NULL;
2563}
2564
2565/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002566 * Do the expansion based on xp->xp_context and 'rmp'.
2567 */
2568 static int
2569ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002570 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002571 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002572 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002573 char_u ***matches,
2574 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002575{
2576 static struct expgen
2577 {
2578 int context;
2579 char_u *((*func)(expand_T *, int));
2580 int ic;
2581 int escaped;
2582 } tab[] =
2583 {
2584 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2585 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2586 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2587 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2588 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2589 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2590 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2591 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2592 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2593 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002594#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002595 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2596 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2597 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2598 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2599 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002600#endif
2601#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002602 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2603 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002604#endif
2605#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002606 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002607#endif
2608#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002609 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002610#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002611 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2612 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2613 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002614#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002615 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002616#endif
2617#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002618 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002619#endif
2620#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002621 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002622#endif
2623#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002624 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2625 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002626#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002627 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2628 {EXPAND_USER, get_users, TRUE, FALSE},
2629 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002630#ifdef FEAT_EVAL
2631 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002632 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002633#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002634 };
2635 int i;
2636 int ret = FAIL;
2637
2638 // Find a context in the table and call the ExpandGeneric() with the
2639 // right function to do the expansion.
2640 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2641 {
2642 if (xp->xp_context == tab[i].context)
2643 {
2644 if (tab[i].ic)
2645 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002646 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2647 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002648 break;
2649 }
2650 }
2651
2652 return ret;
2653}
2654
2655/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002656 * Map wild expand options to flags for expand_wildcards()
2657 */
2658 static int
2659map_wildopts_to_ewflags(int options)
2660{
2661 int flags;
2662
2663 flags = EW_DIR; // include directories
2664 if (options & WILD_LIST_NOTFOUND)
2665 flags |= EW_NOTFOUND;
2666 if (options & WILD_ADD_SLASH)
2667 flags |= EW_ADDSLASH;
2668 if (options & WILD_KEEP_ALL)
2669 flags |= EW_KEEPALL;
2670 if (options & WILD_SILENT)
2671 flags |= EW_SILENT;
2672 if (options & WILD_NOERROR)
2673 flags |= EW_NOERROR;
2674 if (options & WILD_ALLLINKS)
2675 flags |= EW_ALLLINKS;
2676
2677 return flags;
2678}
2679
2680/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002681 * Do the expansion based on xp->xp_context and "pat".
2682 */
2683 static int
2684ExpandFromContext(
2685 expand_T *xp,
2686 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002687 char_u ***matches,
2688 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002689 int options) // WILD_ flags
2690{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002691 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002692 int ret;
2693 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002694 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002695 int fuzzy = cmdline_fuzzy_complete(pat)
2696 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002697
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002698 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002699
2700 if (xp->xp_context == EXPAND_FILES
2701 || xp->xp_context == EXPAND_DIRECTORIES
2702 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002703 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2704 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002705
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002706 *matches = (char_u **)"";
2707 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002708 if (xp->xp_context == EXPAND_HELP)
2709 {
2710 // With an empty argument we would get all the help tags, which is
2711 // very slow. Get matches for "help" instead.
2712 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002713 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002714 {
2715#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002716 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002717#endif
2718 return OK;
2719 }
2720 return FAIL;
2721 }
2722
Bram Moolenaar66b51422019-08-18 21:44:12 +02002723 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002724 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002725 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002726 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002727 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002728 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002729#ifdef FEAT_DIFF
2730 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002731 return ExpandBufnames(pat, numMatches, matches,
2732 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002733#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002734 if (xp->xp_context == EXPAND_TAGS
2735 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002736 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2737 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002738 if (xp->xp_context == EXPAND_COLORS)
2739 {
2740 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002741 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002742 directories);
2743 }
2744 if (xp->xp_context == EXPAND_COMPILER)
2745 {
2746 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002747 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002748 }
2749 if (xp->xp_context == EXPAND_OWNSYNTAX)
2750 {
2751 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002752 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002753 }
2754 if (xp->xp_context == EXPAND_FILETYPE)
2755 {
2756 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002757 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002758 }
2759# if defined(FEAT_EVAL)
2760 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002761 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002762# endif
2763 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002764 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002765
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002766 // When expanding a function name starting with s:, match the <SNR>nr_
2767 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002768 if ((xp->xp_context == EXPAND_USER_FUNC
2769 || xp->xp_context == EXPAND_DISASSEMBLE)
2770 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002771 {
2772 int len = (int)STRLEN(pat) + 20;
2773
2774 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002775 if (tofree == NULL)
2776 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002777 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002778 pat = tofree;
2779 }
2780
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002781 if (!fuzzy)
2782 {
2783 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2784 if (regmatch.regprog == NULL)
2785 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002786
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002787 // set ignore-case according to p_ic, p_scs and pat
2788 regmatch.rm_ic = ignorecase(pat);
2789 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002790
2791 if (xp->xp_context == EXPAND_SETTINGS
2792 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01002793 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002794 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002795 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002796# if defined(FEAT_EVAL)
2797 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002798 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002799# endif
2800 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002801 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002802
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002803 if (!fuzzy)
2804 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002805 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002806
2807 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002808}
2809
Bram Moolenaar66b51422019-08-18 21:44:12 +02002810/*
2811 * Expand a list of names.
2812 *
2813 * Generic function for command line completion. It calls a function to
2814 * obtain strings, one by one. The strings are matched against a regexp
2815 * program. Matching strings are copied into an array, which is returned.
2816 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002817 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2818 * is used.
2819 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002820 * Returns OK when no problems encountered, FAIL for error (out of memory).
2821 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002822 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002823ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002824 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002825 expand_T *xp,
2826 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002827 char_u ***matches,
2828 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002829 char_u *((*func)(expand_T *, int)),
2830 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002831 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002832{
2833 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002834 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002835 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002836 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002837 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002838 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002839 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002840 int sort_matches = FALSE;
2841 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002842
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002843 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002844 *matches = NULL;
2845 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002846
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002847 if (!fuzzy)
2848 ga_init2(&ga, sizeof(char *), 30);
2849 else
2850 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2851
2852 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002853 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002854 str = (*func)(xp, i);
2855 if (str == NULL) // end of list
2856 break;
2857 if (*str == NUL) // skip empty strings
2858 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002859
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002860 if (xp->xp_pattern[0] != NUL)
2861 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002862 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002863 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002864 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002865 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002866 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002867 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002868 }
2869 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002870 else
2871 match = TRUE;
2872
2873 if (!match)
2874 continue;
2875
2876 if (escaped)
2877 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2878 else
2879 str = vim_strsave(str);
2880 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002881 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002882 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002883 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002884 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002885 return FAIL;
2886 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01002887 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002888 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002889 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002890
2891 if (ga_grow(&ga, 1) == FAIL)
2892 {
2893 vim_free(str);
2894 break;
2895 }
2896
2897 if (fuzzy)
2898 {
2899 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2900 fuzmatch->idx = ga.ga_len;
2901 fuzmatch->str = str;
2902 fuzmatch->score = score;
2903 }
2904 else
2905 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2906
2907# ifdef FEAT_MENU
2908 if (func == get_menu_names)
2909 {
2910 // test for separator added by get_menu_names()
2911 str += STRLEN(str) - 1;
2912 if (*str == '\001')
2913 *str = '.';
2914 }
2915# endif
2916
2917 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002918 }
2919
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002920 if (ga.ga_len == 0)
2921 return OK;
2922
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002923 // sort the matches when using regular expression matching and sorting
2924 // applies to the completion context. Menus and scriptnames should be kept
2925 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002926 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002927 && xp->xp_context != EXPAND_MENUS
2928 && xp->xp_context != EXPAND_SCRIPTNAMES)
2929 sort_matches = TRUE;
2930
2931 // <SNR> functions should be sorted to the end.
2932 if (xp->xp_context == EXPAND_EXPRESSION
2933 || xp->xp_context == EXPAND_FUNCTIONS
2934 || xp->xp_context == EXPAND_USER_FUNC
2935 || xp->xp_context == EXPAND_DISASSEMBLE)
2936 funcsort = TRUE;
2937
2938 // Sort the matches.
2939 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002940 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002941 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002942 // <SNR> functions should be sorted to the end.
2943 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2944 sort_func_compare);
2945 else
2946 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2947 }
2948
2949 if (!fuzzy)
2950 {
2951 *matches = ga.ga_data;
2952 *numMatches = ga.ga_len;
2953 }
2954 else
2955 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002956 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2957 funcsort) == FAIL)
2958 return FAIL;
2959 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002960 }
2961
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002962#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002963 // Reset the variables used for special highlight names expansion, so that
2964 // they don't show up when getting normal highlight names by ID.
2965 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002966#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002967
Bram Moolenaar66b51422019-08-18 21:44:12 +02002968 return OK;
2969}
2970
2971/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002972 * Expand shell command matches in one directory of $PATH.
2973 */
2974 static void
2975expand_shellcmd_onedir(
2976 char_u *buf,
2977 char_u *s,
2978 size_t l,
2979 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002980 char_u ***matches,
2981 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002982 int flags,
2983 hashtab_T *ht,
2984 garray_T *gap)
2985{
2986 int ret;
2987 int i;
2988 hash_T hash;
2989 hashitem_T *hi;
2990
2991 vim_strncpy(buf, s, l);
2992 add_pathsep(buf);
2993 l = STRLEN(buf);
2994 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2995
2996 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002997 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002998 if (ret == OK)
2999 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003000 if (ga_grow(gap, *numMatches) == FAIL)
3001 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003002 else
3003 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003004 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003005 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003006 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003007
3008 if (STRLEN(name) > l)
3009 {
3010 // Check if this name was already found.
3011 hash = hash_hash(name + l);
3012 hi = hash_lookup(ht, name + l, hash);
3013 if (HASHITEM_EMPTY(hi))
3014 {
3015 // Remove the path that was prepended.
3016 STRMOVE(name, name + l);
3017 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3018 hash_add_item(ht, hi, name, hash);
3019 name = NULL;
3020 }
3021 }
3022 vim_free(name);
3023 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003024 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003025 }
3026 }
3027}
3028
3029/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003030 * Complete a shell command.
3031 * Returns FAIL or OK;
3032 */
3033 static int
3034expand_shellcmd(
3035 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003036 char_u ***matches, // return: array with matches
3037 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003038 int flagsarg) // EW_ flags
3039{
3040 char_u *pat;
3041 int i;
3042 char_u *path = NULL;
3043 int mustfree = FALSE;
3044 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003045 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003046 size_t l;
3047 char_u *s, *e;
3048 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003049 int did_curdir = FALSE;
3050 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003051
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003052 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003053 if (buf == NULL)
3054 return FAIL;
3055
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003056 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003057 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003058 if (pat == NULL)
3059 {
3060 vim_free(buf);
3061 return FAIL;
3062 }
3063
Bram Moolenaar66b51422019-08-18 21:44:12 +02003064 for (i = 0; pat[i]; ++i)
3065 if (pat[i] == '\\' && pat[i + 1] == ' ')
3066 STRMOVE(pat + i, pat + i + 1);
3067
3068 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3069
3070 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3071 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3072 path = (char_u *)".";
3073 else
3074 {
3075 // For an absolute name we don't use $PATH.
3076 if (!mch_isFullName(pat))
3077 path = vim_getenv((char_u *)"PATH", &mustfree);
3078 if (path == NULL)
3079 path = (char_u *)"";
3080 }
3081
3082 // Go over all directories in $PATH. Expand matches in that directory and
3083 // collect them in "ga". When "." is not in $PATH also expand for the
3084 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003085 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003086 hash_init(&found_ht);
3087 for (s = path; ; s = e)
3088 {
3089# if defined(MSWIN)
3090 e = vim_strchr(s, ';');
3091# else
3092 e = vim_strchr(s, ':');
3093# endif
3094 if (e == NULL)
3095 e = s + STRLEN(s);
3096
3097 if (*s == NUL)
3098 {
3099 if (did_curdir)
3100 break;
3101 // Find directories in the current directory, path is empty.
3102 did_curdir = TRUE;
3103 flags |= EW_DIR;
3104 }
3105 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3106 {
3107 did_curdir = TRUE;
3108 flags |= EW_DIR;
3109 }
3110 else
3111 // Do not match directories inside a $PATH item.
3112 flags &= ~EW_DIR;
3113
3114 l = e - s;
3115 if (l > MAXPATHL - 5)
3116 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003117
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003118 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003119 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120
Bram Moolenaar66b51422019-08-18 21:44:12 +02003121 if (*e != NUL)
3122 ++e;
3123 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003124 *matches = ga.ga_data;
3125 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003126
3127 vim_free(buf);
3128 vim_free(pat);
3129 if (mustfree)
3130 vim_free(path);
3131 hash_clear(&found_ht);
3132 return OK;
3133}
3134
3135# if defined(FEAT_EVAL)
3136/*
3137 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003138 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139 */
3140 static void *
3141call_user_expand_func(
3142 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003143 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003144{
3145 cmdline_info_T *ccline = get_cmdline_info();
3146 int keep = 0;
3147 typval_T args[4];
3148 sctx_T save_current_sctx = current_sctx;
3149 char_u *pat = NULL;
3150 void *ret;
3151
3152 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3153 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003154
3155 if (ccline->cmdbuff != NULL)
3156 {
3157 keep = ccline->cmdbuff[ccline->cmdlen];
3158 ccline->cmdbuff[ccline->cmdlen] = 0;
3159 }
3160
3161 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3162
3163 args[0].v_type = VAR_STRING;
3164 args[0].vval.v_string = pat;
3165 args[1].v_type = VAR_STRING;
3166 args[1].vval.v_string = xp->xp_line;
3167 args[2].v_type = VAR_NUMBER;
3168 args[2].vval.v_number = xp->xp_col;
3169 args[3].v_type = VAR_UNKNOWN;
3170
3171 current_sctx = xp->xp_script_ctx;
3172
3173 ret = user_expand_func(xp->xp_arg, 3, args);
3174
3175 current_sctx = save_current_sctx;
3176 if (ccline->cmdbuff != NULL)
3177 ccline->cmdbuff[ccline->cmdlen] = keep;
3178
3179 vim_free(pat);
3180 return ret;
3181}
3182
3183/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003184 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3185 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003186 */
3187 static int
3188ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003189 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003190 expand_T *xp,
3191 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003192 char_u ***matches,
3193 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003194{
3195 char_u *retstr;
3196 char_u *s;
3197 char_u *e;
3198 int keep;
3199 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003200 int fuzzy;
3201 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003202 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003203
3204 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003205 *matches = NULL;
3206 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003207
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003208 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003209 if (retstr == NULL)
3210 return FAIL;
3211
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003212 if (!fuzzy)
3213 ga_init2(&ga, sizeof(char *), 3);
3214 else
3215 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3216
Bram Moolenaar66b51422019-08-18 21:44:12 +02003217 for (s = retstr; *s != NUL; s = e)
3218 {
3219 e = vim_strchr(s, '\n');
3220 if (e == NULL)
3221 e = s + STRLEN(s);
3222 keep = *e;
3223 *e = NUL;
3224
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003225 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003226 {
3227 if (!fuzzy)
3228 match = vim_regexec(regmatch, s, (colnr_T)0);
3229 else
3230 {
3231 score = fuzzy_match_str(s, pat);
3232 match = (score != 0);
3233 }
3234 }
3235 else
3236 match = TRUE; // match everything
3237
Bram Moolenaar66b51422019-08-18 21:44:12 +02003238 *e = keep;
3239
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003240 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003241 {
3242 if (ga_grow(&ga, 1) == FAIL)
3243 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003244 if (!fuzzy)
3245 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3246 else
3247 {
3248 fuzmatch_str_T *fuzmatch =
3249 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003250 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003251 fuzmatch->str = vim_strnsave(s, e - s);
3252 fuzmatch->score = score;
3253 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003254 ++ga.ga_len;
3255 }
3256
3257 if (*e != NUL)
3258 ++e;
3259 }
3260 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003261
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003262 if (ga.ga_len == 0)
3263 return OK;
3264
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003265 if (!fuzzy)
3266 {
3267 *matches = ga.ga_data;
3268 *numMatches = ga.ga_len;
3269 }
3270 else
3271 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003272 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3273 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003274 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003275 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003276 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003277 return OK;
3278}
3279
3280/*
3281 * Expand names with a list returned by a function defined by the user.
3282 */
3283 static int
3284ExpandUserList(
3285 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003286 char_u ***matches,
3287 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003288{
3289 list_T *retlist;
3290 listitem_T *li;
3291 garray_T ga;
3292
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003293 *matches = NULL;
3294 *numMatches = 0;
3295 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003296 if (retlist == NULL)
3297 return FAIL;
3298
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003299 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003300 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003301 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003302 {
3303 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3304 continue; // Skip non-string items and empty strings
3305
3306 if (ga_grow(&ga, 1) == FAIL)
3307 break;
3308
3309 ((char_u **)ga.ga_data)[ga.ga_len] =
3310 vim_strsave(li->li_tv.vval.v_string);
3311 ++ga.ga_len;
3312 }
3313 list_unref(retlist);
3314
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003315 *matches = ga.ga_data;
3316 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003317 return OK;
3318}
3319# endif
3320
3321/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003322 * Expand "file" for all comma-separated directories in "path".
3323 * Adds the matches to "ga". Caller must init "ga".
3324 */
3325 void
3326globpath(
3327 char_u *path,
3328 char_u *file,
3329 garray_T *ga,
3330 int expand_options)
3331{
3332 expand_T xpc;
3333 char_u *buf;
3334 int i;
3335 int num_p;
3336 char_u **p;
3337
3338 buf = alloc(MAXPATHL);
3339 if (buf == NULL)
3340 return;
3341
3342 ExpandInit(&xpc);
3343 xpc.xp_context = EXPAND_FILES;
3344
3345 // Loop over all entries in {path}.
3346 while (*path != NUL)
3347 {
3348 // Copy one item of the path to buf[] and concatenate the file name.
3349 copy_option_part(&path, buf, MAXPATHL, ",");
3350 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3351 {
3352# if defined(MSWIN)
3353 // Using the platform's path separator (\) makes vim incorrectly
3354 // treat it as an escape character, use '/' instead.
3355 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3356 STRCAT(buf, "/");
3357# else
3358 add_pathsep(buf);
3359# endif
3360 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003361 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003362 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3363 {
3364 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3365
3366 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003367 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003368 for (i = 0; i < num_p; ++i)
3369 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003370 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003371 ++ga->ga_len;
3372 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003373 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003374 }
3375 }
3376 }
3377
3378 vim_free(buf);
3379}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003380
Bram Moolenaareadee482020-09-04 15:37:31 +02003381#ifdef FEAT_WILDMENU
3382
3383/*
3384 * Translate some keys pressed when 'wildmenu' is used.
3385 */
3386 int
3387wildmenu_translate_key(
3388 cmdline_info_T *cclp,
3389 int key,
3390 expand_T *xp,
3391 int did_wild_list)
3392{
3393 int c = key;
3394
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003395#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003396 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003397 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003398 // When the popup menu is used for cmdline completion:
3399 // Up : go to the previous item in the menu
3400 // Down : go to the next item in the menu
3401 // Left : go to the parent directory
3402 // Right: list the files in the selected directory
3403 switch (c)
3404 {
3405 case K_UP: c = K_LEFT; break;
3406 case K_DOWN: c = K_RIGHT; break;
3407 case K_LEFT: c = K_UP; break;
3408 case K_RIGHT: c = K_DOWN; break;
3409 default: break;
3410 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003411 }
3412#endif
3413
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003414 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003415 {
3416 if (c == K_LEFT)
3417 c = Ctrl_P;
3418 else if (c == K_RIGHT)
3419 c = Ctrl_N;
3420 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003421
Bram Moolenaareadee482020-09-04 15:37:31 +02003422 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003423 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003424 && cclp->cmdpos > 1
3425 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3426 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3427 && (c == '\n' || c == '\r' || c == K_KENTER))
3428 c = K_DOWN;
3429
3430 return c;
3431}
3432
3433/*
3434 * Delete characters on the command line, from "from" to the current
3435 * position.
3436 */
3437 static void
3438cmdline_del(cmdline_info_T *cclp, int from)
3439{
3440 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3441 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3442 cclp->cmdlen -= cclp->cmdpos - from;
3443 cclp->cmdpos = from;
3444}
3445
3446/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003447 * Handle a key pressed when the wild menu for the menu names
3448 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003449 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003450 static int
3451wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003452{
Bram Moolenaareadee482020-09-04 15:37:31 +02003453 int i;
3454 int j;
3455
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003456 // Hitting <Down> after "emenu Name.": complete submenu
3457 if (key == K_DOWN && cclp->cmdpos > 0
3458 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003459 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003460 key = p_wc;
3461 KeyTyped = TRUE; // in case the key was mapped
3462 }
3463 else if (key == K_UP)
3464 {
3465 // Hitting <Up>: Remove one submenu name in front of the
3466 // cursor
3467 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003468
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003469 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3470 i = 0;
3471 while (--j > 0)
3472 {
3473 // check for start of menu name
3474 if (cclp->cmdbuff[j] == ' '
3475 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003476 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003477 i = j + 1;
3478 break;
3479 }
3480 // check for start of submenu name
3481 if (cclp->cmdbuff[j] == '.'
3482 && cclp->cmdbuff[j - 1] != '\\')
3483 {
3484 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003485 {
3486 i = j + 1;
3487 break;
3488 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003489 else
3490 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003491 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003492 }
3493 if (i > 0)
3494 cmdline_del(cclp, i);
3495 key = p_wc;
3496 KeyTyped = TRUE; // in case the key was mapped
3497 xp->xp_context = EXPAND_NOTHING;
3498 }
3499
3500 return key;
3501}
3502
3503/*
3504 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3505 * directory names (EXPAND_DIRECTORIES) or shell command names
3506 * (EXPAND_SHELLCMD) is displayed.
3507 */
3508 static int
3509wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3510{
3511 int i;
3512 int j;
3513 char_u upseg[5];
3514
3515 upseg[0] = PATHSEP;
3516 upseg[1] = '.';
3517 upseg[2] = '.';
3518 upseg[3] = PATHSEP;
3519 upseg[4] = NUL;
3520
3521 if (key == K_DOWN
3522 && cclp->cmdpos > 0
3523 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3524 && (cclp->cmdpos < 3
3525 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3526 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3527 {
3528 // go down a directory
3529 key = p_wc;
3530 KeyTyped = TRUE; // in case the key was mapped
3531 }
3532 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3533 {
3534 // If in a direct ancestor, strip off one ../ to go down
3535 int found = FALSE;
3536
3537 j = cclp->cmdpos;
3538 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3539 while (--j > i)
3540 {
3541 if (has_mbyte)
3542 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3543 if (vim_ispathsep(cclp->cmdbuff[j]))
3544 {
3545 found = TRUE;
3546 break;
3547 }
3548 }
3549 if (found
3550 && cclp->cmdbuff[j - 1] == '.'
3551 && cclp->cmdbuff[j - 2] == '.'
3552 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3553 {
3554 cmdline_del(cclp, j - 2);
3555 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003556 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003557 }
3558 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003559 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003560 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003561 // go up a directory
3562 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003563
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003564 j = cclp->cmdpos - 1;
3565 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3566 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003567 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003568 if (has_mbyte)
3569 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3570 if (vim_ispathsep(cclp->cmdbuff[j])
3571# ifdef BACKSLASH_IN_FILENAME
3572 && vim_strchr((char_u *)" *?[{`$%#",
3573 cclp->cmdbuff[j + 1]) == NULL
3574# endif
3575 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003576 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003577 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003578 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003579 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003580 break;
3581 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003582 else
3583 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003584 }
3585 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003586
3587 if (!found)
3588 j = i;
3589 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3590 j += 4;
3591 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3592 && j == i)
3593 j += 3;
3594 else
3595 j = 0;
3596 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003597 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003598 // TODO this is only for DOS/UNIX systems - need to put in
3599 // machine-specific stuff here and in upseg init
3600 cmdline_del(cclp, j);
3601 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003602 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003603 else if (cclp->cmdpos > i)
3604 cmdline_del(cclp, i);
3605
3606 // Now complete in the new directory. Set KeyTyped in case the
3607 // Up key came from a mapping.
3608 key = p_wc;
3609 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003610 }
3611
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003612 return key;
3613}
3614
3615/*
3616 * Handle a key pressed when the wild menu is displayed
3617 */
3618 int
3619wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3620{
3621 if (xp->xp_context == EXPAND_MENUNAMES)
3622 return wildmenu_process_key_menunames(cclp, key, xp);
3623 else if ((xp->xp_context == EXPAND_FILES
3624 || xp->xp_context == EXPAND_DIRECTORIES
3625 || xp->xp_context == EXPAND_SHELLCMD))
3626 return wildmenu_process_key_filenames(cclp, key, xp);
3627
3628 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003629}
3630
3631/*
3632 * Free expanded names when finished walking through the matches
3633 */
3634 void
3635wildmenu_cleanup(cmdline_info_T *cclp)
3636{
3637 int skt = KeyTyped;
3638 int old_RedrawingDisabled = RedrawingDisabled;
3639
3640 if (!p_wmnu || wild_menu_showing == 0)
3641 return;
3642
3643 if (cclp->input_fn)
3644 RedrawingDisabled = 0;
3645
3646 if (wild_menu_showing == WM_SCROLLED)
3647 {
3648 // Entered command line, move it up
3649 cmdline_row--;
3650 redrawcmd();
3651 }
3652 else if (save_p_ls != -1)
3653 {
3654 // restore 'laststatus' and 'winminheight'
3655 p_ls = save_p_ls;
3656 p_wmh = save_p_wmh;
3657 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003658 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003659 redrawcmd();
3660 save_p_ls = -1;
3661 }
3662 else
3663 {
3664 win_redraw_last_status(topframe);
3665 redraw_statuslines();
3666 }
3667 KeyTyped = skt;
3668 wild_menu_showing = 0;
3669 if (cclp->input_fn)
3670 RedrawingDisabled = old_RedrawingDisabled;
3671}
3672#endif
3673
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003674#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003675/*
3676 * "getcompletion()" function
3677 */
3678 void
3679f_getcompletion(typval_T *argvars, typval_T *rettv)
3680{
3681 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003682 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003683 expand_T xpc;
3684 int filtered = FALSE;
3685 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003686 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003687
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003688 if (in_vim9script()
3689 && (check_for_string_arg(argvars, 0) == FAIL
3690 || check_for_string_arg(argvars, 1) == FAIL
3691 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3692 return;
3693
ii144785fe02021-11-21 12:13:56 +00003694 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003695 if (argvars[1].v_type != VAR_STRING)
3696 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003697 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003698 return;
3699 }
3700 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003701
3702 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003703 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003704
3705 if (p_wic)
3706 options |= WILD_ICASE;
3707
3708 // For filtered results, 'wildignore' is used
3709 if (!filtered)
3710 options |= WILD_KEEP_ALL;
3711
3712 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003713 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003714 {
ii144785fe02021-11-21 12:13:56 +00003715 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003716 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003717 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003718 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003719 else
3720 {
ii144785fe02021-11-21 12:13:56 +00003721 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003722 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3723
3724 xpc.xp_context = cmdcomplete_str_to_type(type);
3725 if (xpc.xp_context == EXPAND_NOTHING)
3726 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003727 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003728 return;
3729 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003730
3731# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003732 if (xpc.xp_context == EXPAND_MENUS)
3733 {
3734 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3735 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3736 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003737# endif
3738# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003739 if (xpc.xp_context == EXPAND_CSCOPE)
3740 {
3741 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3742 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3743 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003744# endif
3745# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003746 if (xpc.xp_context == EXPAND_SIGN)
3747 {
3748 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3749 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3750 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003751# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003752 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003753
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00003754 if (cmdline_fuzzy_completion_supported(&xpc))
3755 // when fuzzy matching, don't modify the search string
3756 pat = vim_strsave(xpc.xp_pattern);
3757 else
3758 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3759
Bram Moolenaar93a10962022-06-16 11:42:09 +01003760 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003761 {
3762 int i;
3763
3764 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3765
3766 for (i = 0; i < xpc.xp_numfiles; i++)
3767 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3768 }
3769 vim_free(pat);
3770 ExpandCleanup(&xpc);
3771}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003772#endif // FEAT_EVAL