blob: 57bf47a1bac4634cdfca8e935bc359467481cbd7 [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// "compl_match_array" points the currently displayed list of entries in the
30// popup menu. It is NULL when there is no popup menu.
31static pumitem_T *compl_match_array = NULL;
32static int compl_match_arraysize;
33// First column in cmdline of the matched item for completion.
34static int compl_startcol;
35static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037#define SHOW_FILE_TEXT(m) (showtail ? sm_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000038
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000039/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000040 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
41 * context.
42 */
43 static int
44cmdline_fuzzy_completion_supported(expand_T *xp)
45{
46 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
47 && xp->xp_context != EXPAND_BOOL_SETTINGS
48 && xp->xp_context != EXPAND_COLORS
49 && xp->xp_context != EXPAND_COMPILER
50 && xp->xp_context != EXPAND_DIRECTORIES
51 && xp->xp_context != EXPAND_FILES
52 && xp->xp_context != EXPAND_FILES_IN_PATH
53 && xp->xp_context != EXPAND_FILETYPE
54 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000055 && xp->xp_context != EXPAND_OLD_SETTING
56 && xp->xp_context != EXPAND_OWNSYNTAX
57 && xp->xp_context != EXPAND_PACKADD
58 && xp->xp_context != EXPAND_SHELLCMD
59 && xp->xp_context != EXPAND_TAGS
60 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000061 && xp->xp_context != EXPAND_USER_LIST);
62}
63
64/*
65 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000066 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
67 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000068 */
69 int
70cmdline_fuzzy_complete(char_u *fuzzystr)
71{
72 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
73}
74
75/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000076 * sort function for the completion matches.
77 * <SNR> functions should be sorted to the end.
78 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020079 static int
80sort_func_compare(const void *s1, const void *s2)
81{
82 char_u *p1 = *(char_u **)s1;
83 char_u *p2 = *(char_u **)s2;
84
85 if (*p1 != '<' && *p2 == '<') return -1;
86 if (*p1 == '<' && *p2 != '<') return 1;
87 return STRCMP(p1, p2);
88}
Bram Moolenaar66b51422019-08-18 21:44:12 +020089
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000090/*
91 * Escape special characters in the cmdline completion matches.
92 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020093 static void
94ExpandEscape(
95 expand_T *xp,
96 char_u *str,
97 int numfiles,
98 char_u **files,
99 int options)
100{
101 int i;
102 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100103 int vse_what = xp->xp_context == EXPAND_BUFFERS
104 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200105
106 // May change home directory back to "~"
107 if (options & WILD_HOME_REPLACE)
108 tilde_replace(str, numfiles, files);
109
110 if (options & WILD_ESCAPE)
111 {
112 if (xp->xp_context == EXPAND_FILES
113 || xp->xp_context == EXPAND_FILES_IN_PATH
114 || xp->xp_context == EXPAND_SHELLCMD
115 || xp->xp_context == EXPAND_BUFFERS
116 || xp->xp_context == EXPAND_DIRECTORIES)
117 {
118 // Insert a backslash into a file name before a space, \, %, #
119 // and wildmatch characters, except '~'.
120 for (i = 0; i < numfiles; ++i)
121 {
122 // for ":set path=" we need to escape spaces twice
123 if (xp->xp_backslash == XP_BS_THREE)
124 {
125 p = vim_strsave_escaped(files[i], (char_u *)" ");
126 if (p != NULL)
127 {
128 vim_free(files[i]);
129 files[i] = p;
130#if defined(BACKSLASH_IN_FILENAME)
131 p = vim_strsave_escaped(files[i], (char_u *)" ");
132 if (p != NULL)
133 {
134 vim_free(files[i]);
135 files[i] = p;
136 }
137#endif
138 }
139 }
140#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100141 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200142#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100143 p = vim_strsave_fnameescape(files[i],
144 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200145#endif
146 if (p != NULL)
147 {
148 vim_free(files[i]);
149 files[i] = p;
150 }
151
152 // If 'str' starts with "\~", replace "~" at start of
153 // files[i] with "\~".
154 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
155 escape_fname(&files[i]);
156 }
157 xp->xp_backslash = XP_BS_NONE;
158
159 // If the first file starts with a '+' escape it. Otherwise it
160 // could be seen as "+cmd".
161 if (*files[0] == '+')
162 escape_fname(&files[0]);
163 }
164 else if (xp->xp_context == EXPAND_TAGS)
165 {
166 // Insert a backslash before characters in a tag name that
167 // would terminate the ":tag" command.
168 for (i = 0; i < numfiles; ++i)
169 {
170 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
171 if (p != NULL)
172 {
173 vim_free(files[i]);
174 files[i] = p;
175 }
176 }
177 }
178 }
179}
180
181/*
182 * Return FAIL if this is not an appropriate context in which to do
183 * completion of anything, return OK if it is (even if there are no matches).
184 * For the caller, this means that the character is just passed through like a
185 * normal character (instead of being expanded). This allows :s/^I^D etc.
186 */
187 int
188nextwild(
189 expand_T *xp,
190 int type,
191 int options, // extra options for ExpandOne()
192 int escape) // if TRUE, escape the returned matches
193{
194 cmdline_info_T *ccline = get_cmdline_info();
195 int i, j;
196 char_u *p1;
197 char_u *p2;
198 int difflen;
199 int v;
200
201 if (xp->xp_numfiles == -1)
202 {
203 set_expand_context(xp);
204 cmd_showtail = expand_showtail(xp);
205 }
206
207 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
208 {
209 beep_flush();
210 return OK; // Something illegal on command line
211 }
212 if (xp->xp_context == EXPAND_NOTHING)
213 {
214 // Caller can use the character as a normal char instead
215 return FAIL;
216 }
217
218 msg_puts("..."); // show that we are busy
219 out_flush();
220
221 i = (int)(xp->xp_pattern - ccline->cmdbuff);
222 xp->xp_pattern_len = ccline->cmdpos - i;
223
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000224 if (type == WILD_NEXT || type == WILD_PREV
225 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200226 {
227 // Get next/previous match for a previous expanded pattern.
228 p2 = ExpandOne(xp, NULL, NULL, 0, type);
229 }
230 else
231 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000232 if (cmdline_fuzzy_completion_supported(xp))
233 // If fuzzy matching, don't modify the search string
234 p1 = vim_strsave(xp->xp_pattern);
235 else
236 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
237
Bram Moolenaar66b51422019-08-18 21:44:12 +0200238 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000239 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200240 p2 = NULL;
241 else
242 {
243 int use_options = options |
244 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
245 if (escape)
246 use_options |= WILD_ESCAPE;
247
248 if (p_wic)
249 use_options += WILD_ICASE;
250 p2 = ExpandOne(xp, p1,
251 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
252 use_options, type);
253 vim_free(p1);
254 // longest match: make sure it is not shorter, happens with :help
255 if (p2 != NULL && type == WILD_LONGEST)
256 {
257 for (j = 0; j < xp->xp_pattern_len; ++j)
258 if (ccline->cmdbuff[i + j] == '*'
259 || ccline->cmdbuff[i + j] == '?')
260 break;
261 if ((int)STRLEN(p2) < j)
262 VIM_CLEAR(p2);
263 }
264 }
265 }
266
267 if (p2 != NULL && !got_int)
268 {
269 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
270 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
271 {
272 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
273 xp->xp_pattern = ccline->cmdbuff + i;
274 }
275 else
276 v = OK;
277 if (v == OK)
278 {
279 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
280 &ccline->cmdbuff[ccline->cmdpos],
281 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
282 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
283 ccline->cmdlen += difflen;
284 ccline->cmdpos += difflen;
285 }
286 }
287 vim_free(p2);
288
289 redrawcmd();
290 cursorcmd();
291
292 // When expanding a ":map" command and no matches are found, assume that
293 // the key is supposed to be inserted literally
294 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
295 return FAIL;
296
297 if (xp->xp_numfiles <= 0 && p2 == NULL)
298 beep_flush();
299 else if (xp->xp_numfiles == 1)
300 // free expanded pattern
301 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
302
303 return OK;
304}
305
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000306/*
307 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000308 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000309 */
310 static int
311cmdline_pum_create(
312 cmdline_info_T *ccline,
313 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000314 char_u **matches,
315 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000316 int showtail)
317{
318 int i;
319 int columns;
320
321 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000322 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000323 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000324 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000325 {
326 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
327 compl_match_array[i].pum_info = NULL;
328 compl_match_array[i].pum_extra = NULL;
329 compl_match_array[i].pum_kind = NULL;
330 }
331
332 // Compute the popup menu starting column
333 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
334 columns = vim_strsize(xp->xp_pattern);
335 if (showtail)
336 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000337 columns += vim_strsize(sm_gettail(matches[0]));
338 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000339 }
340 if (columns >= compl_startcol)
341 compl_startcol = 0;
342 else
343 compl_startcol -= columns;
344
345 // no default selection
346 compl_selected = -1;
347
348 cmdline_pum_display();
349
350 return EXPAND_OK;
351}
352
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000353/*
354 * Display the cmdline completion matches in a popup menu
355 */
356void cmdline_pum_display(void)
357{
358 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
359}
360
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000361/*
362 * Returns TRUE if the cmdline completion popup menu is being displayed.
363 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000364int cmdline_pum_active(void)
365{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100366 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000367}
368
369/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000370 * Remove the cmdline completion popup menu (if present), free the list of
371 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000372 */
373void cmdline_pum_remove(void)
374{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000375 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100376 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000377
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000378 pum_undisplay();
379 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000380 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000381 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000382 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000383 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100384
385 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
386 // as a side effect.
387 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000388}
389
390void cmdline_pum_cleanup(cmdline_info_T *cclp)
391{
392 cmdline_pum_remove();
393 wildmenu_cleanup(cclp);
394}
395
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000396/*
397 * Returns the starting column number to use for the cmdline completion popup
398 * menu.
399 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400int cmdline_compl_startcol(void)
401{
402 return compl_startcol;
403}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000404
Bram Moolenaar66b51422019-08-18 21:44:12 +0200405/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000406 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000407 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000408 */
409 static char_u *
410get_next_or_prev_match(
411 int mode,
412 expand_T *xp,
413 int *p_findex,
414 char_u *orig_save)
415{
416 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000417 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000418
419 if (xp->xp_numfiles <= 0)
420 return NULL;
421
422 if (mode == WILD_PREV)
423 {
424 if (findex == -1)
425 findex = xp->xp_numfiles;
426 --findex;
427 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000428 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000429 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000430 else if (mode == WILD_PAGEUP)
431 {
432 if (findex == 0)
433 // at the first entry, don't select any entries
434 findex = -1;
435 else if (findex == -1)
436 // no entry is selected. select the last entry
437 findex = xp->xp_numfiles - 1;
438 else
439 {
440 // go up by the pum height
441 ht = pum_get_height();
442 if (ht > 3)
443 ht -= 2;
444 findex -= ht;
445 if (findex < 0)
446 // few entries left, select the first entry
447 findex = 0;
448 }
449 }
450 else // mode == WILD_PAGEDOWN
451 {
452 if (findex == xp->xp_numfiles - 1)
453 // at the last entry, don't select any entries
454 findex = -1;
455 else if (findex == -1)
456 // no entry is selected. select the first entry
457 findex = 0;
458 else
459 {
460 // go down by the pum height
461 ht = pum_get_height();
462 if (ht > 3)
463 ht -= 2;
464 findex += ht;
465 if (findex >= xp->xp_numfiles)
466 // few entries left, select the last entry
467 findex = xp->xp_numfiles - 1;
468 }
469 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000470
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000471 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000472 if (findex < 0)
473 {
474 if (orig_save == NULL)
475 findex = xp->xp_numfiles - 1;
476 else
477 findex = -1;
478 }
479 if (findex >= xp->xp_numfiles)
480 {
481 if (orig_save == NULL)
482 findex = 0;
483 else
484 findex = -1;
485 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000486 if (compl_match_array)
487 {
488 compl_selected = findex;
489 cmdline_pum_display();
490 }
491 else if (p_wmnu)
492 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
493 findex, cmd_showtail);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000494 *p_findex = findex;
495
496 if (findex == -1)
497 return vim_strsave(orig_save);
498
499 return vim_strsave(xp->xp_files[findex]);
500}
501
502/*
503 * Start the command-line expansion and get the matches.
504 */
505 static char_u *
506ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
507{
508 int non_suf_match; // number without matching suffix
509 int i;
510 char_u *ss = NULL;
511
512 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000513 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000514 options) == FAIL)
515 {
516#ifdef FNAME_ILLEGAL
517 // Illegal file name has been silently skipped. But when there
518 // are wildcards, the real problem is that there was no match,
519 // causing the pattern to be added, which has illegal characters.
520 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
521 semsg(_(e_no_match_str_2), str);
522#endif
523 }
524 else if (xp->xp_numfiles == 0)
525 {
526 if (!(options & WILD_SILENT))
527 semsg(_(e_no_match_str_2), str);
528 }
529 else
530 {
531 // Escape the matches for use on the command line.
532 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
533
534 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000535 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000536 {
537 if (xp->xp_numfiles)
538 non_suf_match = xp->xp_numfiles;
539 else
540 non_suf_match = 1;
541 if ((xp->xp_context == EXPAND_FILES
542 || xp->xp_context == EXPAND_DIRECTORIES)
543 && xp->xp_numfiles > 1)
544 {
545 // More than one match; check suffix.
546 // The files will have been sorted on matching suffix in
547 // expand_wildcards, only need to check the first two.
548 non_suf_match = 0;
549 for (i = 0; i < 2; ++i)
550 if (match_suffix(xp->xp_files[i]))
551 ++non_suf_match;
552 }
553 if (non_suf_match != 1)
554 {
555 // Can we ever get here unless it's while expanding
556 // interactively? If not, we can get rid of this all
557 // together. Don't really want to wait for this message
558 // (and possibly have to hit return to continue!).
559 if (!(options & WILD_SILENT))
560 emsg(_(e_too_many_file_names));
561 else if (!(options & WILD_NO_BEEP))
562 beep_flush();
563 }
564 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
565 ss = vim_strsave(xp->xp_files[0]);
566 }
567 }
568
569 return ss;
570}
571
572/*
573 * Return the longest common part in the list of cmdline completion matches.
574 */
575 static char_u *
576find_longest_match(expand_T *xp, int options)
577{
578 long_u len;
579 int mb_len = 1;
580 int c0, ci;
581 int i;
582 char_u *ss;
583
584 for (len = 0; xp->xp_files[0][len]; len += mb_len)
585 {
586 if (has_mbyte)
587 {
588 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
589 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
590 }
591 else
592 c0 = xp->xp_files[0][len];
593 for (i = 1; i < xp->xp_numfiles; ++i)
594 {
595 if (has_mbyte)
596 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
597 else
598 ci = xp->xp_files[i][len];
599 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
600 || xp->xp_context == EXPAND_FILES
601 || xp->xp_context == EXPAND_SHELLCMD
602 || xp->xp_context == EXPAND_BUFFERS))
603 {
604 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
605 break;
606 }
607 else if (c0 != ci)
608 break;
609 }
610 if (i < xp->xp_numfiles)
611 {
612 if (!(options & WILD_NO_BEEP))
613 vim_beep(BO_WILD);
614 break;
615 }
616 }
617
618 ss = alloc(len + 1);
619 if (ss)
620 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
621
622 return ss;
623}
624
625/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000626 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200627 * Chars that should not be expanded must be preceded with a backslash.
628 * Return a pointer to allocated memory containing the new string.
629 * Return NULL for failure.
630 *
631 * "orig" is the originally expanded string, copied to allocated memory. It
632 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
633 * WILD_PREV "orig" should be NULL.
634 *
635 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
636 * is WILD_EXPAND_FREE or WILD_ALL.
637 *
638 * mode = WILD_FREE: just free previously expanded matches
639 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
640 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
641 * mode = WILD_NEXT: use next match in multiple match, wrap to first
642 * mode = WILD_PREV: use previous match in multiple match, wrap to first
643 * mode = WILD_ALL: return all matches concatenated
644 * mode = WILD_LONGEST: return longest matched part
645 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000646 * mode = WILD_APPLY: apply the item selected in the cmdline completion
647 * popup menu and close the menu.
648 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
649 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200650 *
651 * options = WILD_LIST_NOTFOUND: list entries without a match
652 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
653 * options = WILD_USE_NL: Use '\n' for WILD_ALL
654 * options = WILD_NO_BEEP: Don't beep for multiple matches
655 * options = WILD_ADD_SLASH: add a slash after directory names
656 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
657 * options = WILD_SILENT: don't print warning messages
658 * options = WILD_ESCAPE: put backslash before special chars
659 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200660 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200661 *
662 * The variables xp->xp_context and xp->xp_backslash must have been set!
663 */
664 char_u *
665ExpandOne(
666 expand_T *xp,
667 char_u *str,
668 char_u *orig, // allocated copy of original of expanded string
669 int options,
670 int mode)
671{
672 char_u *ss = NULL;
673 static int findex;
674 static char_u *orig_save = NULL; // kept value of orig
675 int orig_saved = FALSE;
676 int i;
677 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200678
679 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000680 if (mode == WILD_NEXT || mode == WILD_PREV
681 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000682 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200683
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000684 if (mode == WILD_CANCEL)
685 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
686 else if (mode == WILD_APPLY)
687 ss = vim_strsave(findex == -1 ? (orig_save ?
688 orig_save : (char_u *)"") : xp->xp_files[findex]);
689
Bram Moolenaar66b51422019-08-18 21:44:12 +0200690 // free old names
691 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
692 {
693 FreeWild(xp->xp_numfiles, xp->xp_files);
694 xp->xp_numfiles = -1;
695 VIM_CLEAR(orig_save);
696 }
697 findex = 0;
698
699 if (mode == WILD_FREE) // only release file name
700 return NULL;
701
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000702 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200703 {
704 vim_free(orig_save);
705 orig_save = orig;
706 orig_saved = TRUE;
707
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000708 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200709 }
710
711 // Find longest common part
712 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
713 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000714 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200715 findex = -1; // next p_wc gets first one
716 }
717
Bram Moolenaar57e95172022-08-20 19:26:14 +0100718 // Concatenate all matching names. Unless interrupted, this can be slow
719 // and the result probably won't be used.
720 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200721 {
722 len = 0;
723 for (i = 0; i < xp->xp_numfiles; ++i)
724 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
725 ss = alloc(len);
726 if (ss != NULL)
727 {
728 *ss = NUL;
729 for (i = 0; i < xp->xp_numfiles; ++i)
730 {
731 STRCAT(ss, xp->xp_files[i]);
732 if (i != xp->xp_numfiles - 1)
733 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
734 }
735 }
736 }
737
738 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
739 ExpandCleanup(xp);
740
741 // Free "orig" if it wasn't stored in "orig_save".
742 if (!orig_saved)
743 vim_free(orig);
744
745 return ss;
746}
747
748/*
749 * Prepare an expand structure for use.
750 */
751 void
752ExpandInit(expand_T *xp)
753{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200754 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200755 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200756 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200757}
758
759/*
760 * Cleanup an expand structure after use.
761 */
762 void
763ExpandCleanup(expand_T *xp)
764{
765 if (xp->xp_numfiles >= 0)
766 {
767 FreeWild(xp->xp_numfiles, xp->xp_files);
768 xp->xp_numfiles = -1;
769 }
770}
771
772/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000773 * Display one line of completion matches. Multiple matches are displayed in
774 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000775 * matches - list of completion match names
776 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000777 * lines - number of output lines
778 * linenr - line number of matches to display
779 * maxlen - maximum number of characters in each line
780 * showtail - display only the tail of the full path of a file name
781 * dir_attr - highlight attribute to use for directory names
782 */
783 static void
784showmatches_oneline(
785 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000786 char_u **matches,
787 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000788 int lines,
789 int linenr,
790 int maxlen,
791 int showtail,
792 int dir_attr)
793{
794 int i, j;
795 int isdir;
796 int lastlen;
797 char_u *p;
798
799 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000800 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000801 {
802 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
803 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000804 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
805 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000806 msg_advance(maxlen + 1);
807 msg_puts((char *)p);
808 msg_advance(maxlen + 3);
809 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
810 break;
811 }
812 for (i = maxlen - lastlen; --i >= 0; )
813 msg_putchar(' ');
814 if (xp->xp_context == EXPAND_FILES
815 || xp->xp_context == EXPAND_SHELLCMD
816 || xp->xp_context == EXPAND_BUFFERS)
817 {
818 // highlight directories
819 if (xp->xp_numfiles != -1)
820 {
821 char_u *halved_slash;
822 char_u *exp_path;
823 char_u *path;
824
825 // Expansion was done before and special characters
826 // were escaped, need to halve backslashes. Also
827 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000828 exp_path = expand_env_save_opt(matches[j], TRUE);
829 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000830 halved_slash = backslash_halve_save(path);
831 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000832 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000833 vim_free(exp_path);
834 if (halved_slash != path)
835 vim_free(halved_slash);
836 }
837 else
838 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000839 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000840 if (showtail)
841 p = SHOW_FILE_TEXT(j);
842 else
843 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000844 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000845 TRUE);
846 p = NameBuff;
847 }
848 }
849 else
850 {
851 isdir = FALSE;
852 p = SHOW_FILE_TEXT(j);
853 }
854 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
855 }
856 if (msg_col > 0) // when not wrapped around
857 {
858 msg_clr_eos();
859 msg_putchar('\n');
860 }
861 out_flush(); // show one line at a time
862}
863
864/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200865 * Show all matches for completion on the command line.
866 * Returns EXPAND_NOTHING when the character that triggered expansion should
867 * be inserted like a normal character.
868 */
869 int
870showmatches(expand_T *xp, int wildmenu UNUSED)
871{
872 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000873 int numMatches;
874 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000875 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200876 int maxlen;
877 int lines;
878 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200879 int attr;
880 int showtail;
881
882 if (xp->xp_numfiles == -1)
883 {
884 set_expand_context(xp);
885 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000886 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200887 showtail = expand_showtail(xp);
888 if (i != EXPAND_OK)
889 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200890 }
891 else
892 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000893 numMatches = xp->xp_numfiles;
894 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200895 showtail = cmd_showtail;
896 }
897
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000898 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000899 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000900 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000901
Bram Moolenaar66b51422019-08-18 21:44:12 +0200902 if (!wildmenu)
903 {
Bram Moolenaar66b51422019-08-18 21:44:12 +0200904 msg_didany = FALSE; // lines_left will be set
905 msg_start(); // prepare for paging
906 msg_putchar('\n');
907 out_flush();
908 cmdline_row = msg_row;
909 msg_didany = FALSE; // lines_left will be set again
910 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +0200911 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200912
913 if (got_int)
914 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +0200915 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000916 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200917 else
918 {
919 // find the length of the longest file name
920 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000921 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200922 {
923 if (!showtail && (xp->xp_context == EXPAND_FILES
924 || xp->xp_context == EXPAND_SHELLCMD
925 || xp->xp_context == EXPAND_BUFFERS))
926 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000927 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200928 j = vim_strsize(NameBuff);
929 }
930 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000931 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200932 if (j > maxlen)
933 maxlen = j;
934 }
935
936 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000937 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200938 else
939 {
940 // compute the number of columns and lines for the listing
941 maxlen += 2; // two spaces between file names
942 columns = ((int)Columns + 2) / maxlen;
943 if (columns < 1)
944 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000945 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200946 }
947
948 attr = HL_ATTR(HLF_D); // find out highlighting for directories
949
950 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
951 {
952 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
953 msg_clr_eos();
954 msg_advance(maxlen - 3);
955 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
956 }
957
958 // list the files line by line
959 for (i = 0; i < lines; ++i)
960 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000961 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000962 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200963 if (got_int)
964 {
965 got_int = FALSE;
966 break;
967 }
968 }
969
970 // we redraw the command below the lines that we have just listed
971 // This is a bit tricky, but it saves a lot of screen updating.
972 cmdline_row = msg_row; // will put it back later
973 }
974
975 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000976 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200977
978 return EXPAND_OK;
979}
980
981/*
982 * Private gettail for showmatches() (and win_redr_status_matches()):
983 * Find tail of file name path, but ignore trailing "/".
984 */
985 char_u *
986sm_gettail(char_u *s)
987{
988 char_u *p;
989 char_u *t = s;
990 int had_sep = FALSE;
991
992 for (p = s; *p != NUL; )
993 {
994 if (vim_ispathsep(*p)
995#ifdef BACKSLASH_IN_FILENAME
996 && !rem_backslash(p)
997#endif
998 )
999 had_sep = TRUE;
1000 else if (had_sep)
1001 {
1002 t = p;
1003 had_sep = FALSE;
1004 }
1005 MB_PTR_ADV(p);
1006 }
1007 return t;
1008}
1009
1010/*
1011 * Return TRUE if we only need to show the tail of completion matches.
1012 * When not completing file names or there is a wildcard in the path FALSE is
1013 * returned.
1014 */
1015 static int
1016expand_showtail(expand_T *xp)
1017{
1018 char_u *s;
1019 char_u *end;
1020
1021 // When not completing file names a "/" may mean something different.
1022 if (xp->xp_context != EXPAND_FILES
1023 && xp->xp_context != EXPAND_SHELLCMD
1024 && xp->xp_context != EXPAND_DIRECTORIES)
1025 return FALSE;
1026
1027 end = gettail(xp->xp_pattern);
1028 if (end == xp->xp_pattern) // there is no path separator
1029 return FALSE;
1030
1031 for (s = xp->xp_pattern; s < end; s++)
1032 {
1033 // Skip escaped wildcards. Only when the backslash is not a path
1034 // separator, on DOS the '*' "path\*\file" must not be skipped.
1035 if (rem_backslash(s))
1036 ++s;
1037 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1038 return FALSE;
1039 }
1040 return TRUE;
1041}
1042
1043/*
1044 * Prepare a string for expansion.
1045 * When expanding file names: The string will be used with expand_wildcards().
1046 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1047 * When expanding other names: The string will be used with regcomp(). Copy
1048 * the name into allocated memory and prepend "^".
1049 */
1050 char_u *
1051addstar(
1052 char_u *fname,
1053 int len,
1054 int context) // EXPAND_FILES etc.
1055{
1056 char_u *retval;
1057 int i, j;
1058 int new_len;
1059 char_u *tail;
1060 int ends_in_star;
1061
1062 if (context != EXPAND_FILES
1063 && context != EXPAND_FILES_IN_PATH
1064 && context != EXPAND_SHELLCMD
1065 && context != EXPAND_DIRECTORIES)
1066 {
1067 // Matching will be done internally (on something other than files).
1068 // So we convert the file-matching-type wildcards into our kind for
1069 // use with vim_regcomp(). First work out how long it will be:
1070
1071 // For help tags the translation is done in find_help_tags().
1072 // For a tag pattern starting with "/" no translation is needed.
1073 if (context == EXPAND_HELP
1074 || context == EXPAND_COLORS
1075 || context == EXPAND_COMPILER
1076 || context == EXPAND_OWNSYNTAX
1077 || context == EXPAND_FILETYPE
1078 || context == EXPAND_PACKADD
1079 || ((context == EXPAND_TAGS_LISTFILES
1080 || context == EXPAND_TAGS)
1081 && fname[0] == '/'))
1082 retval = vim_strnsave(fname, len);
1083 else
1084 {
1085 new_len = len + 2; // +2 for '^' at start, NUL at end
1086 for (i = 0; i < len; i++)
1087 {
1088 if (fname[i] == '*' || fname[i] == '~')
1089 new_len++; // '*' needs to be replaced by ".*"
1090 // '~' needs to be replaced by "\~"
1091
1092 // Buffer names are like file names. "." should be literal
1093 if (context == EXPAND_BUFFERS && fname[i] == '.')
1094 new_len++; // "." becomes "\."
1095
1096 // Custom expansion takes care of special things, match
1097 // backslashes literally (perhaps also for other types?)
1098 if ((context == EXPAND_USER_DEFINED
1099 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1100 new_len++; // '\' becomes "\\"
1101 }
1102 retval = alloc(new_len);
1103 if (retval != NULL)
1104 {
1105 retval[0] = '^';
1106 j = 1;
1107 for (i = 0; i < len; i++, j++)
1108 {
1109 // Skip backslash. But why? At least keep it for custom
1110 // expansion.
1111 if (context != EXPAND_USER_DEFINED
1112 && context != EXPAND_USER_LIST
1113 && fname[i] == '\\'
1114 && ++i == len)
1115 break;
1116
1117 switch (fname[i])
1118 {
1119 case '*': retval[j++] = '.';
1120 break;
1121 case '~': retval[j++] = '\\';
1122 break;
1123 case '?': retval[j] = '.';
1124 continue;
1125 case '.': if (context == EXPAND_BUFFERS)
1126 retval[j++] = '\\';
1127 break;
1128 case '\\': if (context == EXPAND_USER_DEFINED
1129 || context == EXPAND_USER_LIST)
1130 retval[j++] = '\\';
1131 break;
1132 }
1133 retval[j] = fname[i];
1134 }
1135 retval[j] = NUL;
1136 }
1137 }
1138 }
1139 else
1140 {
1141 retval = alloc(len + 4);
1142 if (retval != NULL)
1143 {
1144 vim_strncpy(retval, fname, len);
1145
1146 // Don't add a star to *, ~, ~user, $var or `cmd`.
1147 // * would become **, which walks the whole tree.
1148 // ~ would be at the start of the file name, but not the tail.
1149 // $ could be anywhere in the tail.
1150 // ` could be anywhere in the file name.
1151 // When the name ends in '$' don't add a star, remove the '$'.
1152 tail = gettail(retval);
1153 ends_in_star = (len > 0 && retval[len - 1] == '*');
1154#ifndef BACKSLASH_IN_FILENAME
1155 for (i = len - 2; i >= 0; --i)
1156 {
1157 if (retval[i] != '\\')
1158 break;
1159 ends_in_star = !ends_in_star;
1160 }
1161#endif
1162 if ((*retval != '~' || tail != retval)
1163 && !ends_in_star
1164 && vim_strchr(tail, '$') == NULL
1165 && vim_strchr(retval, '`') == NULL)
1166 retval[len++] = '*';
1167 else if (len > 0 && retval[len - 1] == '$')
1168 --len;
1169 retval[len] = NUL;
1170 }
1171 }
1172 return retval;
1173}
1174
1175/*
1176 * Must parse the command line so far to work out what context we are in.
1177 * Completion can then be done based on that context.
1178 * This routine sets the variables:
1179 * xp->xp_pattern The start of the pattern to be expanded within
1180 * the command line (ends at the cursor).
1181 * xp->xp_context The type of thing to expand. Will be one of:
1182 *
1183 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1184 * the command line, like an unknown command. Caller
1185 * should beep.
1186 * EXPAND_NOTHING Unrecognised context for completion, use char like
1187 * a normal char, rather than for completion. eg
1188 * :s/^I/
1189 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1190 * it.
1191 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1192 * EXPAND_FILES After command with EX_XFILE set, or after setting
1193 * with P_EXPAND set. eg :e ^I, :w>>^I
1194 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1195 * when we know only directories are of interest. eg
1196 * :set dir=^I
1197 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1198 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1199 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1200 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1201 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1202 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1203 * EXPAND_EVENTS Complete event names
1204 * EXPAND_SYNTAX Complete :syntax command arguments
1205 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1206 * EXPAND_AUGROUP Complete autocommand group names
1207 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1208 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1209 * eg :unmap a^I , :cunab x^I
1210 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1211 * eg :call sub^I
1212 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1213 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1214 * names in expressions, eg :while s^I
1215 * EXPAND_ENV_VARS Complete environment variable names
1216 * EXPAND_USER Complete user names
1217 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001218 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001219set_expand_context(expand_T *xp)
1220{
1221 cmdline_info_T *ccline = get_cmdline_info();
1222
1223 // only expansion for ':', '>' and '=' command-lines
1224 if (ccline->cmdfirstc != ':'
1225#ifdef FEAT_EVAL
1226 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1227 && !ccline->input_fn
1228#endif
1229 )
1230 {
1231 xp->xp_context = EXPAND_NOTHING;
1232 return;
1233 }
1234 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1235}
1236
Bram Moolenaard0190392019-08-23 21:17:35 +02001237/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001238 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1239 * For user defined commands, the completion context is set in 'xp' and the
1240 * completion flags in 'complp'.
1241 *
1242 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001243 */
1244 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001245set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001246{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001247 char_u *p = NULL;
1248 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001249 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001250
1251 // Isolate the command and search for it in the command table.
1252 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001253 // - the 'k' command can directly be followed by any character, but do
1254 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1255 // find matches anywhere in the command name, do this only for command
1256 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001257 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001258 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001259 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001260 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001261 p = cmd + 1;
1262 }
1263 else
1264 {
1265 p = cmd;
1266 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1267 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001268 // A user command may contain digits.
1269 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1270 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001271 while (ASCII_ISALNUM(*p) || *p == '*')
1272 ++p;
1273 // for python 3.x: ":py3*" commands completion
1274 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1275 {
1276 ++p;
1277 while (ASCII_ISALPHA(*p) || *p == '*')
1278 ++p;
1279 }
1280 // check for non-alpha command
1281 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1282 ++p;
1283 len = (int)(p - cmd);
1284
1285 if (len == 0)
1286 {
1287 xp->xp_context = EXPAND_UNSUCCESSFUL;
1288 return NULL;
1289 }
1290
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001291 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001292
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001293 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001294 // Also when doing fuzzy expansion for non-shell commands, support
1295 // alphanumeric characters.
1296 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1297 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001298 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1299 ++p;
1300 }
1301
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001302 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001303 // character, complete the command name.
1304 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1305 return NULL;
1306
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001307 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001308 {
1309 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1310 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001311 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001312 p = cmd + 1;
1313 }
1314 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1315 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001316 eap->cmd = cmd;
1317 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001318 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001319 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001320 }
1321 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001322 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001323 {
1324 // Not still touching the command and it was an illegal one
1325 xp->xp_context = EXPAND_UNSUCCESSFUL;
1326 return NULL;
1327 }
1328
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001329 return p;
1330}
Bram Moolenaard0190392019-08-23 21:17:35 +02001331
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001332/*
1333 * Set the completion context for a command argument with wild card characters.
1334 */
1335 static void
1336set_context_for_wildcard_arg(
1337 exarg_T *eap,
1338 char_u *arg,
1339 int usefilter,
1340 expand_T *xp,
1341 int *complp)
1342{
1343 char_u *p;
1344 int c;
1345 int in_quote = FALSE;
1346 char_u *bow = NULL; // Beginning of word
1347 int len = 0;
1348
1349 // Allow spaces within back-quotes to count as part of the argument
1350 // being expanded.
1351 xp->xp_pattern = skipwhite(arg);
1352 p = xp->xp_pattern;
1353 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001354 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001355 if (has_mbyte)
1356 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001357 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001358 c = *p;
1359 if (c == '\\' && p[1] != NUL)
1360 ++p;
1361 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001362 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001363 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001364 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001365 xp->xp_pattern = p;
1366 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001367 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001368 in_quote = !in_quote;
1369 }
1370 // An argument can contain just about everything, except
1371 // characters that end the command and white space.
1372 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001373#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001374 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001375#endif
1376 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001377 {
1378 len = 0; // avoid getting stuck when space is in 'isfname'
1379 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001380 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001381 if (has_mbyte)
1382 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001383 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001384 c = *p;
1385 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001386 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001387 if (has_mbyte)
1388 len = (*mb_ptr2len)(p);
1389 else
1390 len = 1;
1391 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001392 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001393 if (in_quote)
1394 bow = p;
1395 else
1396 xp->xp_pattern = p;
1397 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001398 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001399 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001400 }
1401
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001402 // If we are still inside the quotes, and we passed a space, just
1403 // expand from there.
1404 if (bow != NULL && in_quote)
1405 xp->xp_pattern = bow;
1406 xp->xp_context = EXPAND_FILES;
1407
1408 // For a shell command more chars need to be escaped.
1409 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1410 {
1411#ifndef BACKSLASH_IN_FILENAME
1412 xp->xp_shell = TRUE;
1413#endif
1414 // When still after the command name expand executables.
1415 if (xp->xp_pattern == skipwhite(arg))
1416 xp->xp_context = EXPAND_SHELLCMD;
1417 }
1418
1419 // Check for environment variable.
1420 if (*xp->xp_pattern == '$')
1421 {
1422 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1423 if (!vim_isIDc(*p))
1424 break;
1425 if (*p == NUL)
1426 {
1427 xp->xp_context = EXPAND_ENV_VARS;
1428 ++xp->xp_pattern;
1429 // Avoid that the assignment uses EXPAND_FILES again.
1430 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1431 *complp = EXPAND_ENV_VARS;
1432 }
1433 }
1434 // Check for user names.
1435 if (*xp->xp_pattern == '~')
1436 {
1437 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1438 ;
1439 // Complete ~user only if it partially matches a user name.
1440 // A full match ~user<Tab> will be replaced by user's home
1441 // directory i.e. something like ~user<Tab> -> /home/user/
1442 if (*p == NUL && p > xp->xp_pattern + 1
1443 && match_user(xp->xp_pattern + 1) >= 1)
1444 {
1445 xp->xp_context = EXPAND_USER;
1446 ++xp->xp_pattern;
1447 }
1448 }
1449}
1450
1451/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001452 * Set the completion context for the :filter command. Returns a pointer to the
1453 * next command after the :filter command.
1454 */
1455 static char_u *
1456set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1457{
1458 if (*arg != NUL)
1459 arg = skip_vimgrep_pat(arg, NULL, NULL);
1460 if (arg == NULL || *arg == NUL)
1461 {
1462 xp->xp_context = EXPAND_NOTHING;
1463 return NULL;
1464 }
1465 return skipwhite(arg);
1466}
1467
1468#ifdef FEAT_SEARCH_EXTRA
1469/*
1470 * Set the completion context for the :match command. Returns a pointer to the
1471 * next command after the :match command.
1472 */
1473 static char_u *
1474set_context_in_match_cmd(expand_T *xp, char_u *arg)
1475{
1476 if (*arg == NUL || !ends_excmd(*arg))
1477 {
1478 // also complete "None"
1479 set_context_in_echohl_cmd(xp, arg);
1480 arg = skipwhite(skiptowhite(arg));
1481 if (*arg != NUL)
1482 {
1483 xp->xp_context = EXPAND_NOTHING;
1484 arg = skip_regexp(arg + 1, *arg, magic_isset());
1485 }
1486 }
1487 return find_nextcmd(arg);
1488}
1489#endif
1490
1491/*
1492 * Returns a pointer to the next command after a :global or a :v command.
1493 * Returns NULL if there is no next command.
1494 */
1495 static char_u *
1496find_cmd_after_global_cmd(char_u *arg)
1497{
1498 int delim;
1499
1500 delim = *arg; // get the delimiter
1501 if (delim)
1502 ++arg; // skip delimiter if there is one
1503
1504 while (arg[0] != NUL && arg[0] != delim)
1505 {
1506 if (arg[0] == '\\' && arg[1] != NUL)
1507 ++arg;
1508 ++arg;
1509 }
1510 if (arg[0] != NUL)
1511 return arg + 1;
1512
1513 return NULL;
1514}
1515
1516/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001517 * Returns a pointer to the next command after a :substitute or a :& command.
1518 * Returns NULL if there is no next command.
1519 */
1520 static char_u *
1521find_cmd_after_substitute_cmd(char_u *arg)
1522{
1523 int delim;
1524
1525 delim = *arg;
1526 if (delim)
1527 {
1528 // skip "from" part
1529 ++arg;
1530 arg = skip_regexp(arg, delim, magic_isset());
1531
1532 if (arg[0] != NUL && arg[0] == delim)
1533 {
1534 // skip "to" part
1535 ++arg;
1536 while (arg[0] != NUL && arg[0] != delim)
1537 {
1538 if (arg[0] == '\\' && arg[1] != NUL)
1539 ++arg;
1540 ++arg;
1541 }
1542 if (arg[0] != NUL) // skip delimiter
1543 ++arg;
1544 }
1545 }
1546 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1547 ++arg;
1548 if (arg[0] != NUL)
1549 return arg;
1550
1551 return NULL;
1552}
1553
1554/*
1555 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1556 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1557 * Returns NULL if there is no next command.
1558 */
1559 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001560find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001561{
1562 arg = skipwhite(skipdigits(arg)); // skip count
1563 if (*arg == '/') // Match regexp, not just whole words
1564 {
1565 for (++arg; *arg && *arg != '/'; arg++)
1566 if (*arg == '\\' && arg[1] != NUL)
1567 arg++;
1568 if (*arg)
1569 {
1570 arg = skipwhite(arg + 1);
1571
1572 // Check for trailing illegal characters
1573 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1574 xp->xp_context = EXPAND_NOTHING;
1575 else
1576 return arg;
1577 }
1578 }
1579
1580 return NULL;
1581}
1582
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001583#ifdef FEAT_EVAL
1584/*
1585 * Set the completion context for the :unlet command. Always returns NULL.
1586 */
1587 static char_u *
1588set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1589{
1590 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1591 arg = xp->xp_pattern + 1;
1592
1593 xp->xp_context = EXPAND_USER_VARS;
1594 xp->xp_pattern = arg;
1595
1596 if (*xp->xp_pattern == '$')
1597 {
1598 xp->xp_context = EXPAND_ENV_VARS;
1599 ++xp->xp_pattern;
1600 }
1601
1602 return NULL;
1603}
1604#endif
1605
1606#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1607/*
1608 * Set the completion context for the :language command. Always returns NULL.
1609 */
1610 static char_u *
1611set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1612{
1613 char_u *p;
1614
1615 p = skiptowhite(arg);
1616 if (*p == NUL)
1617 {
1618 xp->xp_context = EXPAND_LANGUAGE;
1619 xp->xp_pattern = arg;
1620 }
1621 else
1622 {
1623 if ( STRNCMP(arg, "messages", p - arg) == 0
1624 || STRNCMP(arg, "ctype", p - arg) == 0
1625 || STRNCMP(arg, "time", p - arg) == 0
1626 || STRNCMP(arg, "collate", p - arg) == 0)
1627 {
1628 xp->xp_context = EXPAND_LOCALES;
1629 xp->xp_pattern = skipwhite(p);
1630 }
1631 else
1632 xp->xp_context = EXPAND_NOTHING;
1633 }
1634
1635 return NULL;
1636}
1637#endif
1638
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001639#ifdef FEAT_EVAL
1640static enum
1641{
1642 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001643 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1644 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001645} breakpt_expand_what;
1646
1647/*
1648 * Set the completion context for the :breakadd command. Always returns NULL.
1649 */
1650 static char_u *
1651set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1652{
1653 char_u *p;
1654 char_u *subcmd_start;
1655
1656 xp->xp_context = EXPAND_BREAKPOINT;
1657 xp->xp_pattern = arg;
1658
1659 if (cmdidx == CMD_breakadd)
1660 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001661 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001662 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001663 else
1664 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001665
1666 p = skipwhite(arg);
1667 if (*p == NUL)
1668 return NULL;
1669 subcmd_start = p;
1670
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001671 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001672 {
1673 // :breakadd file [lnum] <filename>
1674 // :breakadd func [lnum] <funcname>
1675 p += 4;
1676 p = skipwhite(p);
1677
1678 // skip line number (if specified)
1679 if (VIM_ISDIGIT(*p))
1680 {
1681 p = skipdigits(p);
1682 if (*p != ' ')
1683 {
1684 xp->xp_context = EXPAND_NOTHING;
1685 return NULL;
1686 }
1687 p = skipwhite(p);
1688 }
1689 if (STRNCMP("file", subcmd_start, 4) == 0)
1690 xp->xp_context = EXPAND_FILES;
1691 else
1692 xp->xp_context = EXPAND_USER_FUNC;
1693 xp->xp_pattern = p;
1694 }
1695 else if (STRNCMP("expr ", p, 5) == 0)
1696 {
1697 // :breakadd expr <expression>
1698 xp->xp_context = EXPAND_EXPRESSION;
1699 xp->xp_pattern = skipwhite(p + 5);
1700 }
1701
1702 return NULL;
1703}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001704
1705 static char_u *
1706set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1707{
1708 char_u *p;
1709
1710 xp->xp_context = EXPAND_NOTHING;
1711 xp->xp_pattern = NULL;
1712
1713 p = skipwhite(arg);
1714 if (VIM_ISDIGIT(*p))
1715 return NULL;
1716
1717 xp->xp_context = EXPAND_SCRIPTNAMES;
1718 xp->xp_pattern = p;
1719
1720 return NULL;
1721}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001722#endif
1723
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001724/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001725 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1726 * The argument to the command is 'arg' and the argument flags is 'argt'.
1727 * For user-defined commands and for environment variables, 'compl' has the
1728 * completion type.
1729 * Returns a pointer to the next command. Returns NULL if there is no next
1730 * command.
1731 */
1732 static char_u *
1733set_context_by_cmdname(
1734 char_u *cmd,
1735 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001736 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001737 char_u *arg,
1738 long argt,
1739 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001740 int forceit)
1741{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001742 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001743 {
1744 case CMD_find:
1745 case CMD_sfind:
1746 case CMD_tabfind:
1747 if (xp->xp_context == EXPAND_FILES)
1748 xp->xp_context = EXPAND_FILES_IN_PATH;
1749 break;
1750 case CMD_cd:
1751 case CMD_chdir:
1752 case CMD_tcd:
1753 case CMD_tchdir:
1754 case CMD_lcd:
1755 case CMD_lchdir:
1756 if (xp->xp_context == EXPAND_FILES)
1757 xp->xp_context = EXPAND_DIRECTORIES;
1758 break;
1759 case CMD_help:
1760 xp->xp_context = EXPAND_HELP;
1761 xp->xp_pattern = arg;
1762 break;
1763
1764 // Command modifiers: return the argument.
1765 // Also for commands with an argument that is a command.
1766 case CMD_aboveleft:
1767 case CMD_argdo:
1768 case CMD_belowright:
1769 case CMD_botright:
1770 case CMD_browse:
1771 case CMD_bufdo:
1772 case CMD_cdo:
1773 case CMD_cfdo:
1774 case CMD_confirm:
1775 case CMD_debug:
1776 case CMD_folddoclosed:
1777 case CMD_folddoopen:
1778 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01001779 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02001780 case CMD_keepalt:
1781 case CMD_keepjumps:
1782 case CMD_keepmarks:
1783 case CMD_keeppatterns:
1784 case CMD_ldo:
1785 case CMD_leftabove:
1786 case CMD_lfdo:
1787 case CMD_lockmarks:
1788 case CMD_noautocmd:
1789 case CMD_noswapfile:
1790 case CMD_rightbelow:
1791 case CMD_sandbox:
1792 case CMD_silent:
1793 case CMD_tab:
1794 case CMD_tabdo:
1795 case CMD_topleft:
1796 case CMD_verbose:
1797 case CMD_vertical:
1798 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001799 case CMD_vim9cmd:
1800 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001801 return arg;
1802
1803 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001804 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001805
1806#ifdef FEAT_SEARCH_EXTRA
1807 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001808 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001809#endif
1810
1811 // All completion for the +cmdline_compl feature goes here.
1812
1813 case CMD_command:
1814 return set_context_in_user_cmd(xp, arg);
1815
1816 case CMD_delcommand:
1817 xp->xp_context = EXPAND_USER_COMMANDS;
1818 xp->xp_pattern = arg;
1819 break;
1820
1821 case CMD_global:
1822 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001823 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001824 case CMD_and:
1825 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001826 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001827 case CMD_isearch:
1828 case CMD_dsearch:
1829 case CMD_ilist:
1830 case CMD_dlist:
1831 case CMD_ijump:
1832 case CMD_psearch:
1833 case CMD_djump:
1834 case CMD_isplit:
1835 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001836 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001837 case CMD_autocmd:
1838 return set_context_in_autocmd(xp, arg, FALSE);
1839 case CMD_doautocmd:
1840 case CMD_doautoall:
1841 return set_context_in_autocmd(xp, arg, TRUE);
1842 case CMD_set:
1843 set_context_in_set_cmd(xp, arg, 0);
1844 break;
1845 case CMD_setglobal:
1846 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1847 break;
1848 case CMD_setlocal:
1849 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1850 break;
1851 case CMD_tag:
1852 case CMD_stag:
1853 case CMD_ptag:
1854 case CMD_ltag:
1855 case CMD_tselect:
1856 case CMD_stselect:
1857 case CMD_ptselect:
1858 case CMD_tjump:
1859 case CMD_stjump:
1860 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001861 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001862 xp->xp_context = EXPAND_TAGS_LISTFILES;
1863 else
1864 xp->xp_context = EXPAND_TAGS;
1865 xp->xp_pattern = arg;
1866 break;
1867 case CMD_augroup:
1868 xp->xp_context = EXPAND_AUGROUP;
1869 xp->xp_pattern = arg;
1870 break;
1871#ifdef FEAT_SYN_HL
1872 case CMD_syntax:
1873 set_context_in_syntax_cmd(xp, arg);
1874 break;
1875#endif
1876#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001877 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001878 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001879 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001880 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001881 case CMD_if:
1882 case CMD_elseif:
1883 case CMD_while:
1884 case CMD_for:
1885 case CMD_echo:
1886 case CMD_echon:
1887 case CMD_execute:
1888 case CMD_echomsg:
1889 case CMD_echoerr:
1890 case CMD_call:
1891 case CMD_return:
1892 case CMD_cexpr:
1893 case CMD_caddexpr:
1894 case CMD_cgetexpr:
1895 case CMD_lexpr:
1896 case CMD_laddexpr:
1897 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001898 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001899 break;
1900
1901 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001902 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001903 case CMD_function:
1904 case CMD_delfunction:
1905 xp->xp_context = EXPAND_USER_FUNC;
1906 xp->xp_pattern = arg;
1907 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001908 case CMD_disassemble:
1909 set_context_in_disassemble_cmd(xp, arg);
1910 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001911
1912 case CMD_echohl:
1913 set_context_in_echohl_cmd(xp, arg);
1914 break;
1915#endif
1916 case CMD_highlight:
1917 set_context_in_highlight_cmd(xp, arg);
1918 break;
1919#ifdef FEAT_CSCOPE
1920 case CMD_cscope:
1921 case CMD_lcscope:
1922 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001923 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001924 break;
1925#endif
1926#ifdef FEAT_SIGNS
1927 case CMD_sign:
1928 set_context_in_sign_cmd(xp, arg);
1929 break;
1930#endif
1931 case CMD_bdelete:
1932 case CMD_bwipeout:
1933 case CMD_bunload:
1934 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1935 arg = xp->xp_pattern + 1;
1936 // FALLTHROUGH
1937 case CMD_buffer:
1938 case CMD_sbuffer:
1939 case CMD_checktime:
1940 xp->xp_context = EXPAND_BUFFERS;
1941 xp->xp_pattern = arg;
1942 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001943#ifdef FEAT_DIFF
1944 case CMD_diffget:
1945 case CMD_diffput:
1946 // If current buffer is in diff mode, complete buffer names
1947 // which are in diff mode, and different than current buffer.
1948 xp->xp_context = EXPAND_DIFF_BUFFERS;
1949 xp->xp_pattern = arg;
1950 break;
1951#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001952 case CMD_USER:
1953 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001954 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1955 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001956
1957 case CMD_map: case CMD_noremap:
1958 case CMD_nmap: case CMD_nnoremap:
1959 case CMD_vmap: case CMD_vnoremap:
1960 case CMD_omap: case CMD_onoremap:
1961 case CMD_imap: case CMD_inoremap:
1962 case CMD_cmap: case CMD_cnoremap:
1963 case CMD_lmap: case CMD_lnoremap:
1964 case CMD_smap: case CMD_snoremap:
1965 case CMD_tmap: case CMD_tnoremap:
1966 case CMD_xmap: case CMD_xnoremap:
1967 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001968 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001969 case CMD_unmap:
1970 case CMD_nunmap:
1971 case CMD_vunmap:
1972 case CMD_ounmap:
1973 case CMD_iunmap:
1974 case CMD_cunmap:
1975 case CMD_lunmap:
1976 case CMD_sunmap:
1977 case CMD_tunmap:
1978 case CMD_xunmap:
1979 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001980 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001981 case CMD_mapclear:
1982 case CMD_nmapclear:
1983 case CMD_vmapclear:
1984 case CMD_omapclear:
1985 case CMD_imapclear:
1986 case CMD_cmapclear:
1987 case CMD_lmapclear:
1988 case CMD_smapclear:
1989 case CMD_tmapclear:
1990 case CMD_xmapclear:
1991 xp->xp_context = EXPAND_MAPCLEAR;
1992 xp->xp_pattern = arg;
1993 break;
1994
1995 case CMD_abbreviate: case CMD_noreabbrev:
1996 case CMD_cabbrev: case CMD_cnoreabbrev:
1997 case CMD_iabbrev: case CMD_inoreabbrev:
1998 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001999 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002000 case CMD_unabbreviate:
2001 case CMD_cunabbrev:
2002 case CMD_iunabbrev:
2003 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002004 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002005#ifdef FEAT_MENU
2006 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2007 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2008 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2009 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2010 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2011 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2012 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2013 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2014 case CMD_tmenu: case CMD_tunmenu:
2015 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2016 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2017#endif
2018
2019 case CMD_colorscheme:
2020 xp->xp_context = EXPAND_COLORS;
2021 xp->xp_pattern = arg;
2022 break;
2023
2024 case CMD_compiler:
2025 xp->xp_context = EXPAND_COMPILER;
2026 xp->xp_pattern = arg;
2027 break;
2028
2029 case CMD_ownsyntax:
2030 xp->xp_context = EXPAND_OWNSYNTAX;
2031 xp->xp_pattern = arg;
2032 break;
2033
2034 case CMD_setfiletype:
2035 xp->xp_context = EXPAND_FILETYPE;
2036 xp->xp_pattern = arg;
2037 break;
2038
2039 case CMD_packadd:
2040 xp->xp_context = EXPAND_PACKADD;
2041 xp->xp_pattern = arg;
2042 break;
2043
2044#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2045 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002046 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002047#endif
2048#if defined(FEAT_PROFILE)
2049 case CMD_profile:
2050 set_context_in_profile_cmd(xp, arg);
2051 break;
2052#endif
2053 case CMD_behave:
2054 xp->xp_context = EXPAND_BEHAVE;
2055 xp->xp_pattern = arg;
2056 break;
2057
2058 case CMD_messages:
2059 xp->xp_context = EXPAND_MESSAGES;
2060 xp->xp_pattern = arg;
2061 break;
2062
2063 case CMD_history:
2064 xp->xp_context = EXPAND_HISTORY;
2065 xp->xp_pattern = arg;
2066 break;
2067#if defined(FEAT_PROFILE)
2068 case CMD_syntime:
2069 xp->xp_context = EXPAND_SYNTIME;
2070 xp->xp_pattern = arg;
2071 break;
2072#endif
2073
2074 case CMD_argdelete:
2075 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2076 arg = xp->xp_pattern + 1;
2077 xp->xp_context = EXPAND_ARGLIST;
2078 xp->xp_pattern = arg;
2079 break;
2080
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002081#ifdef FEAT_EVAL
2082 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002083 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002084 case CMD_breakdel:
2085 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002086
2087 case CMD_scriptnames:
2088 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002089#endif
2090
Bram Moolenaard0190392019-08-23 21:17:35 +02002091 default:
2092 break;
2093 }
2094 return NULL;
2095}
2096
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002097/*
2098 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2099 * we don't need/want deleted. Maybe this could be done better if we didn't
2100 * repeat all this stuff. The only problem is that they may not stay
2101 * perfectly compatible with each other, but then the command line syntax
2102 * probably won't change that much -- webb.
2103 */
2104 static char_u *
2105set_one_cmd_context(
2106 expand_T *xp,
2107 char_u *buff) // buffer for command string
2108{
2109 char_u *p;
2110 char_u *cmd, *arg;
2111 int len = 0;
2112 exarg_T ea;
2113 int compl = EXPAND_NOTHING;
2114 int forceit = FALSE;
2115 int usefilter = FALSE; // filter instead of file name
2116
2117 ExpandInit(xp);
2118 xp->xp_pattern = buff;
2119 xp->xp_line = buff;
2120 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2121 ea.argt = 0;
2122
2123 // 1. skip comment lines and leading space, colons or bars
2124 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2125 ;
2126 xp->xp_pattern = cmd;
2127
2128 if (*cmd == NUL)
2129 return NULL;
2130 if (*cmd == '"') // ignore comment lines
2131 {
2132 xp->xp_context = EXPAND_NOTHING;
2133 return NULL;
2134 }
2135
2136 // 3. Skip over the range to find the command.
2137 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2138 xp->xp_pattern = cmd;
2139 if (*cmd == NUL)
2140 return NULL;
2141 if (*cmd == '"')
2142 {
2143 xp->xp_context = EXPAND_NOTHING;
2144 return NULL;
2145 }
2146
2147 if (*cmd == '|' || *cmd == '\n')
2148 return cmd + 1; // There's another command
2149
2150 // Get the command index.
2151 p = set_cmd_index(cmd, &ea, xp, &compl);
2152 if (p == NULL)
2153 return NULL;
2154
2155 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2156
2157 if (*p == '!') // forced commands
2158 {
2159 forceit = TRUE;
2160 ++p;
2161 }
2162
2163 // 6. parse arguments
2164 if (!IS_USER_CMDIDX(ea.cmdidx))
2165 ea.argt = excmd_get_argt(ea.cmdidx);
2166
2167 arg = skipwhite(p);
2168
2169 // Skip over ++argopt argument
2170 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2171 {
2172 p = arg;
2173 while (*p && !vim_isspace(*p))
2174 MB_PTR_ADV(p);
2175 arg = skipwhite(p);
2176 }
2177
2178 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2179 {
2180 if (*arg == '>') // append
2181 {
2182 if (*++arg == '>')
2183 ++arg;
2184 arg = skipwhite(arg);
2185 }
2186 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2187 {
2188 ++arg;
2189 usefilter = TRUE;
2190 }
2191 }
2192
2193 if (ea.cmdidx == CMD_read)
2194 {
2195 usefilter = forceit; // :r! filter if forced
2196 if (*arg == '!') // :r !filter
2197 {
2198 ++arg;
2199 usefilter = TRUE;
2200 }
2201 }
2202
2203 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2204 {
2205 while (*arg == *cmd) // allow any number of '>' or '<'
2206 ++arg;
2207 arg = skipwhite(arg);
2208 }
2209
2210 // Does command allow "+command"?
2211 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2212 {
2213 // Check if we're in the +command
2214 p = arg + 1;
2215 arg = skip_cmd_arg(arg, FALSE);
2216
2217 // Still touching the command after '+'?
2218 if (*arg == NUL)
2219 return p;
2220
2221 // Skip space(s) after +command to get to the real argument
2222 arg = skipwhite(arg);
2223 }
2224
2225
2226 // Check for '|' to separate commands and '"' to start comments.
2227 // Don't do this for ":read !cmd" and ":write !cmd".
2228 if ((ea.argt & EX_TRLBAR) && !usefilter)
2229 {
2230 p = arg;
2231 // ":redir @" is not the start of a comment
2232 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2233 p += 2;
2234 while (*p)
2235 {
2236 if (*p == Ctrl_V)
2237 {
2238 if (p[1] != NUL)
2239 ++p;
2240 }
2241 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2242 || *p == '|' || *p == '\n')
2243 {
2244 if (*(p - 1) != '\\')
2245 {
2246 if (*p == '|' || *p == '\n')
2247 return p + 1;
2248 return NULL; // It's a comment
2249 }
2250 }
2251 MB_PTR_ADV(p);
2252 }
2253 }
2254
2255 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2256 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2257 // no arguments allowed but there is something
2258 return NULL;
2259
2260 // Find start of last argument (argument just before cursor):
2261 p = buff;
2262 xp->xp_pattern = p;
2263 len = (int)STRLEN(buff);
2264 while (*p && p < buff + len)
2265 {
2266 if (*p == ' ' || *p == TAB)
2267 {
2268 // argument starts after a space
2269 xp->xp_pattern = ++p;
2270 }
2271 else
2272 {
2273 if (*p == '\\' && *(p + 1) != NUL)
2274 ++p; // skip over escaped character
2275 MB_PTR_ADV(p);
2276 }
2277 }
2278
2279 if (ea.argt & EX_XFILE)
2280 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2281
2282 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002283 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002284 forceit);
2285}
2286
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002287/*
2288 * Set the completion context in 'xp' for command 'str'
2289 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002290 void
2291set_cmd_context(
2292 expand_T *xp,
2293 char_u *str, // start of command line
2294 int len, // length of command line (excl. NUL)
2295 int col, // position of cursor
2296 int use_ccline UNUSED) // use ccline for info
2297{
2298#ifdef FEAT_EVAL
2299 cmdline_info_T *ccline = get_cmdline_info();
2300#endif
2301 int old_char = NUL;
2302 char_u *nextcomm;
2303
2304 // Avoid a UMR warning from Purify, only save the character if it has been
2305 // written before.
2306 if (col < len)
2307 old_char = str[col];
2308 str[col] = NUL;
2309 nextcomm = str;
2310
2311#ifdef FEAT_EVAL
2312 if (use_ccline && ccline->cmdfirstc == '=')
2313 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002314 // pass CMD_SIZE because there is no real command
2315 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002316 }
2317 else if (use_ccline && ccline->input_fn)
2318 {
2319 xp->xp_context = ccline->xp_context;
2320 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002321 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002322 }
2323 else
2324#endif
2325 while (nextcomm != NULL)
2326 nextcomm = set_one_cmd_context(xp, nextcomm);
2327
2328 // Store the string here so that call_user_expand_func() can get to them
2329 // easily.
2330 xp->xp_line = str;
2331 xp->xp_col = col;
2332
2333 str[col] = old_char;
2334}
2335
2336/*
2337 * Expand the command line "str" from context "xp".
2338 * "xp" must have been set by set_cmd_context().
2339 * xp->xp_pattern points into "str", to where the text that is to be expanded
2340 * starts.
2341 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2342 * cursor.
2343 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2344 * key that triggered expansion literally.
2345 * Returns EXPAND_OK otherwise.
2346 */
2347 int
2348expand_cmdline(
2349 expand_T *xp,
2350 char_u *str, // start of command line
2351 int col, // position of cursor
2352 int *matchcount, // return: nr of matches
2353 char_u ***matches) // return: array of pointers to matches
2354{
2355 char_u *file_str = NULL;
2356 int options = WILD_ADD_SLASH|WILD_SILENT;
2357
2358 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2359 {
2360 beep_flush();
2361 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2362 }
2363 if (xp->xp_context == EXPAND_NOTHING)
2364 {
2365 // Caller can use the character as a normal char instead
2366 return EXPAND_NOTHING;
2367 }
2368
2369 // add star to file name, or convert to regexp if not exp. files.
2370 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002371 if (cmdline_fuzzy_completion_supported(xp))
2372 // If fuzzy matching, don't modify the search string
2373 file_str = vim_strsave(xp->xp_pattern);
2374 else
2375 {
2376 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2377 if (file_str == NULL)
2378 return EXPAND_UNSUCCESSFUL;
2379 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002380
2381 if (p_wic)
2382 options += WILD_ICASE;
2383
2384 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002385 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002386 {
2387 *matchcount = 0;
2388 *matches = NULL;
2389 }
2390 vim_free(file_str);
2391
2392 return EXPAND_OK;
2393}
2394
Bram Moolenaar66b51422019-08-18 21:44:12 +02002395/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002396 * Expand file or directory names.
2397 */
2398 static int
2399expand_files_and_dirs(
2400 expand_T *xp,
2401 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002402 char_u ***matches,
2403 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002404 int flags,
2405 int options)
2406{
2407 int free_pat = FALSE;
2408 int i;
2409 int ret;
2410
2411 // for ":set path=" and ":set tags=" halve backslashes for escaped
2412 // space
2413 if (xp->xp_backslash != XP_BS_NONE)
2414 {
2415 free_pat = TRUE;
2416 pat = vim_strsave(pat);
2417 for (i = 0; pat[i]; ++i)
2418 if (pat[i] == '\\')
2419 {
2420 if (xp->xp_backslash == XP_BS_THREE
2421 && pat[i + 1] == '\\'
2422 && pat[i + 2] == '\\'
2423 && pat[i + 3] == ' ')
2424 STRMOVE(pat + i, pat + i + 3);
2425 if (xp->xp_backslash == XP_BS_ONE
2426 && pat[i + 1] == ' ')
2427 STRMOVE(pat + i, pat + i + 1);
2428 }
2429 }
2430
2431 if (xp->xp_context == EXPAND_FILES)
2432 flags |= EW_FILE;
2433 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2434 flags |= (EW_FILE | EW_PATH);
2435 else
2436 flags = (flags | EW_DIR) & ~EW_FILE;
2437 if (options & WILD_ICASE)
2438 flags |= EW_ICASE;
2439
2440 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002441 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002442 if (free_pat)
2443 vim_free(pat);
2444#ifdef BACKSLASH_IN_FILENAME
2445 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2446 {
2447 int j;
2448
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002449 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002450 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002451 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002452
2453 while (*ptr != NUL)
2454 {
2455 if (p_csl[0] == 's' && *ptr == '\\')
2456 *ptr = '/';
2457 else if (p_csl[0] == 'b' && *ptr == '/')
2458 *ptr = '\\';
2459 ptr += (*mb_ptr2len)(ptr);
2460 }
2461 }
2462 }
2463#endif
2464 return ret;
2465}
2466
2467/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002468 * Function given to ExpandGeneric() to obtain the possible arguments of the
2469 * ":behave {mswin,xterm}" command.
2470 */
2471 static char_u *
2472get_behave_arg(expand_T *xp UNUSED, int idx)
2473{
2474 if (idx == 0)
2475 return (char_u *)"mswin";
2476 if (idx == 1)
2477 return (char_u *)"xterm";
2478 return NULL;
2479}
2480
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002481# ifdef FEAT_EVAL
2482/*
2483 * Function given to ExpandGeneric() to obtain the possible arguments of the
2484 * ":breakadd {expr, file, func, here}" command.
2485 * ":breakdel {func, file, here}" command.
2486 */
2487 static char_u *
2488get_breakadd_arg(expand_T *xp UNUSED, int idx)
2489{
2490 char *opts[] = {"expr", "file", "func", "here"};
2491
2492 if (idx >=0 && idx <= 3)
2493 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002494 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002495 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2496 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002497 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2498 {
2499 // breakdel {func, file, here}
2500 if (idx <= 2)
2501 return (char_u *)opts[idx + 1];
2502 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002503 else
2504 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002505 // profdel {func, file}
2506 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002507 return (char_u *)opts[idx + 1];
2508 }
2509 }
2510 return NULL;
2511}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002512
2513/*
2514 * Function given to ExpandGeneric() to obtain the possible arguments for the
2515 * ":scriptnames" command.
2516 */
2517 static char_u *
2518get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2519{
2520 scriptitem_T *si;
2521
2522 if (!SCRIPT_ID_VALID(idx + 1))
2523 return NULL;
2524
2525 si = SCRIPT_ITEM(idx + 1);
2526 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2527 return NameBuff;
2528}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002529#endif
2530
Bram Moolenaard0190392019-08-23 21:17:35 +02002531/*
2532 * Function given to ExpandGeneric() to obtain the possible arguments of the
2533 * ":messages {clear}" command.
2534 */
2535 static char_u *
2536get_messages_arg(expand_T *xp UNUSED, int idx)
2537{
2538 if (idx == 0)
2539 return (char_u *)"clear";
2540 return NULL;
2541}
2542
2543 static char_u *
2544get_mapclear_arg(expand_T *xp UNUSED, int idx)
2545{
2546 if (idx == 0)
2547 return (char_u *)"<buffer>";
2548 return NULL;
2549}
2550
2551/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002552 * Do the expansion based on xp->xp_context and 'rmp'.
2553 */
2554 static int
2555ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002556 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002557 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002558 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002559 char_u ***matches,
2560 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002561{
2562 static struct expgen
2563 {
2564 int context;
2565 char_u *((*func)(expand_T *, int));
2566 int ic;
2567 int escaped;
2568 } tab[] =
2569 {
2570 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2571 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2572 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2573 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2574 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2575 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2576 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2577 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2578 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2579 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002580#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002581 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2582 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2583 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2584 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2585 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002586#endif
2587#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002588 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2589 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002590#endif
2591#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002592 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002593#endif
2594#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002595 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002596#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002597 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2598 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2599 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002600#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002601 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002602#endif
2603#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002604 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002605#endif
2606#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002607 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002608#endif
2609#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002610 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2611 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002612#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002613 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2614 {EXPAND_USER, get_users, TRUE, FALSE},
2615 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002616#ifdef FEAT_EVAL
2617 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002618 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002619#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002620 };
2621 int i;
2622 int ret = FAIL;
2623
2624 // Find a context in the table and call the ExpandGeneric() with the
2625 // right function to do the expansion.
2626 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2627 {
2628 if (xp->xp_context == tab[i].context)
2629 {
2630 if (tab[i].ic)
2631 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002632 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2633 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002634 break;
2635 }
2636 }
2637
2638 return ret;
2639}
2640
2641/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002642 * Map wild expand options to flags for expand_wildcards()
2643 */
2644 static int
2645map_wildopts_to_ewflags(int options)
2646{
2647 int flags;
2648
2649 flags = EW_DIR; // include directories
2650 if (options & WILD_LIST_NOTFOUND)
2651 flags |= EW_NOTFOUND;
2652 if (options & WILD_ADD_SLASH)
2653 flags |= EW_ADDSLASH;
2654 if (options & WILD_KEEP_ALL)
2655 flags |= EW_KEEPALL;
2656 if (options & WILD_SILENT)
2657 flags |= EW_SILENT;
2658 if (options & WILD_NOERROR)
2659 flags |= EW_NOERROR;
2660 if (options & WILD_ALLLINKS)
2661 flags |= EW_ALLLINKS;
2662
2663 return flags;
2664}
2665
2666/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002667 * Do the expansion based on xp->xp_context and "pat".
2668 */
2669 static int
2670ExpandFromContext(
2671 expand_T *xp,
2672 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002673 char_u ***matches,
2674 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002675 int options) // WILD_ flags
2676{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002677 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002678 int ret;
2679 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002680 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002681 int fuzzy = cmdline_fuzzy_complete(pat)
2682 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002683
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002684 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002685
2686 if (xp->xp_context == EXPAND_FILES
2687 || xp->xp_context == EXPAND_DIRECTORIES
2688 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002689 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2690 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002691
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002692 *matches = (char_u **)"";
2693 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002694 if (xp->xp_context == EXPAND_HELP)
2695 {
2696 // With an empty argument we would get all the help tags, which is
2697 // very slow. Get matches for "help" instead.
2698 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002699 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002700 {
2701#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002702 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002703#endif
2704 return OK;
2705 }
2706 return FAIL;
2707 }
2708
Bram Moolenaar66b51422019-08-18 21:44:12 +02002709 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002710 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002711 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002712 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002713 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002714 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002715#ifdef FEAT_DIFF
2716 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002717 return ExpandBufnames(pat, numMatches, matches,
2718 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002719#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002720 if (xp->xp_context == EXPAND_TAGS
2721 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002722 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2723 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002724 if (xp->xp_context == EXPAND_COLORS)
2725 {
2726 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002727 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002728 directories);
2729 }
2730 if (xp->xp_context == EXPAND_COMPILER)
2731 {
2732 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002733 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002734 }
2735 if (xp->xp_context == EXPAND_OWNSYNTAX)
2736 {
2737 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002738 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002739 }
2740 if (xp->xp_context == EXPAND_FILETYPE)
2741 {
2742 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002743 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002744 }
2745# if defined(FEAT_EVAL)
2746 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002747 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002748# endif
2749 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002750 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002751
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002752 // When expanding a function name starting with s:, match the <SNR>nr_
2753 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002754 if ((xp->xp_context == EXPAND_USER_FUNC
2755 || xp->xp_context == EXPAND_DISASSEMBLE)
2756 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002757 {
2758 int len = (int)STRLEN(pat) + 20;
2759
2760 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002761 if (tofree == NULL)
2762 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002763 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002764 pat = tofree;
2765 }
2766
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002767 if (!fuzzy)
2768 {
2769 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2770 if (regmatch.regprog == NULL)
2771 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002772
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002773 // set ignore-case according to p_ic, p_scs and pat
2774 regmatch.rm_ic = ignorecase(pat);
2775 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002776
2777 if (xp->xp_context == EXPAND_SETTINGS
2778 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01002779 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002780 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002781 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002782# if defined(FEAT_EVAL)
2783 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002784 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002785# endif
2786 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002787 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002788
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002789 if (!fuzzy)
2790 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002791 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002792
2793 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002794}
2795
Bram Moolenaar66b51422019-08-18 21:44:12 +02002796/*
2797 * Expand a list of names.
2798 *
2799 * Generic function for command line completion. It calls a function to
2800 * obtain strings, one by one. The strings are matched against a regexp
2801 * program. Matching strings are copied into an array, which is returned.
2802 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002803 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2804 * is used.
2805 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002806 * Returns OK when no problems encountered, FAIL for error (out of memory).
2807 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002808 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002809ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002810 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002811 expand_T *xp,
2812 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002813 char_u ***matches,
2814 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002815 char_u *((*func)(expand_T *, int)),
2816 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002817 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002818{
2819 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002820 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002821 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002822 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002823 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002824 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002825 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002826 int sort_matches = FALSE;
2827 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002828
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002829 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002830 *matches = NULL;
2831 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002832
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002833 if (!fuzzy)
2834 ga_init2(&ga, sizeof(char *), 30);
2835 else
2836 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2837
2838 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002839 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002840 str = (*func)(xp, i);
2841 if (str == NULL) // end of list
2842 break;
2843 if (*str == NUL) // skip empty strings
2844 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002845
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002846 if (xp->xp_pattern[0] != NUL)
2847 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002848 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002849 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002850 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002851 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002852 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002853 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002854 }
2855 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002856 else
2857 match = TRUE;
2858
2859 if (!match)
2860 continue;
2861
2862 if (escaped)
2863 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2864 else
2865 str = vim_strsave(str);
2866 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002867 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002868 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002869 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002870 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002871 return FAIL;
2872 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01002873 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002874 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002875 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002876
2877 if (ga_grow(&ga, 1) == FAIL)
2878 {
2879 vim_free(str);
2880 break;
2881 }
2882
2883 if (fuzzy)
2884 {
2885 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2886 fuzmatch->idx = ga.ga_len;
2887 fuzmatch->str = str;
2888 fuzmatch->score = score;
2889 }
2890 else
2891 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2892
2893# ifdef FEAT_MENU
2894 if (func == get_menu_names)
2895 {
2896 // test for separator added by get_menu_names()
2897 str += STRLEN(str) - 1;
2898 if (*str == '\001')
2899 *str = '.';
2900 }
2901# endif
2902
2903 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002904 }
2905
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002906 if (ga.ga_len == 0)
2907 return OK;
2908
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002909 // sort the matches when using regular expression matching and sorting
2910 // applies to the completion context. Menus and scriptnames should be kept
2911 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002912 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002913 && xp->xp_context != EXPAND_MENUS
2914 && xp->xp_context != EXPAND_SCRIPTNAMES)
2915 sort_matches = TRUE;
2916
2917 // <SNR> functions should be sorted to the end.
2918 if (xp->xp_context == EXPAND_EXPRESSION
2919 || xp->xp_context == EXPAND_FUNCTIONS
2920 || xp->xp_context == EXPAND_USER_FUNC
2921 || xp->xp_context == EXPAND_DISASSEMBLE)
2922 funcsort = TRUE;
2923
2924 // Sort the matches.
2925 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002926 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002927 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002928 // <SNR> functions should be sorted to the end.
2929 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2930 sort_func_compare);
2931 else
2932 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2933 }
2934
2935 if (!fuzzy)
2936 {
2937 *matches = ga.ga_data;
2938 *numMatches = ga.ga_len;
2939 }
2940 else
2941 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002942 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2943 funcsort) == FAIL)
2944 return FAIL;
2945 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002946 }
2947
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002948#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002949 // Reset the variables used for special highlight names expansion, so that
2950 // they don't show up when getting normal highlight names by ID.
2951 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002952#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002953
Bram Moolenaar66b51422019-08-18 21:44:12 +02002954 return OK;
2955}
2956
2957/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002958 * Expand shell command matches in one directory of $PATH.
2959 */
2960 static void
2961expand_shellcmd_onedir(
2962 char_u *buf,
2963 char_u *s,
2964 size_t l,
2965 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002966 char_u ***matches,
2967 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002968 int flags,
2969 hashtab_T *ht,
2970 garray_T *gap)
2971{
2972 int ret;
2973 int i;
2974 hash_T hash;
2975 hashitem_T *hi;
2976
2977 vim_strncpy(buf, s, l);
2978 add_pathsep(buf);
2979 l = STRLEN(buf);
2980 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2981
2982 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002983 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002984 if (ret == OK)
2985 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002986 if (ga_grow(gap, *numMatches) == FAIL)
2987 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002988 else
2989 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002990 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002991 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002992 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002993
2994 if (STRLEN(name) > l)
2995 {
2996 // Check if this name was already found.
2997 hash = hash_hash(name + l);
2998 hi = hash_lookup(ht, name + l, hash);
2999 if (HASHITEM_EMPTY(hi))
3000 {
3001 // Remove the path that was prepended.
3002 STRMOVE(name, name + l);
3003 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3004 hash_add_item(ht, hi, name, hash);
3005 name = NULL;
3006 }
3007 }
3008 vim_free(name);
3009 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003010 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003011 }
3012 }
3013}
3014
3015/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003016 * Complete a shell command.
3017 * Returns FAIL or OK;
3018 */
3019 static int
3020expand_shellcmd(
3021 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003022 char_u ***matches, // return: array with matches
3023 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003024 int flagsarg) // EW_ flags
3025{
3026 char_u *pat;
3027 int i;
3028 char_u *path = NULL;
3029 int mustfree = FALSE;
3030 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003031 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003032 size_t l;
3033 char_u *s, *e;
3034 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003035 int did_curdir = FALSE;
3036 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003037
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003038 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003039 if (buf == NULL)
3040 return FAIL;
3041
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003042 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003043 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003044 if (pat == NULL)
3045 {
3046 vim_free(buf);
3047 return FAIL;
3048 }
3049
Bram Moolenaar66b51422019-08-18 21:44:12 +02003050 for (i = 0; pat[i]; ++i)
3051 if (pat[i] == '\\' && pat[i + 1] == ' ')
3052 STRMOVE(pat + i, pat + i + 1);
3053
3054 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3055
3056 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3057 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3058 path = (char_u *)".";
3059 else
3060 {
3061 // For an absolute name we don't use $PATH.
3062 if (!mch_isFullName(pat))
3063 path = vim_getenv((char_u *)"PATH", &mustfree);
3064 if (path == NULL)
3065 path = (char_u *)"";
3066 }
3067
3068 // Go over all directories in $PATH. Expand matches in that directory and
3069 // collect them in "ga". When "." is not in $PATH also expand for the
3070 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003071 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003072 hash_init(&found_ht);
3073 for (s = path; ; s = e)
3074 {
3075# if defined(MSWIN)
3076 e = vim_strchr(s, ';');
3077# else
3078 e = vim_strchr(s, ':');
3079# endif
3080 if (e == NULL)
3081 e = s + STRLEN(s);
3082
3083 if (*s == NUL)
3084 {
3085 if (did_curdir)
3086 break;
3087 // Find directories in the current directory, path is empty.
3088 did_curdir = TRUE;
3089 flags |= EW_DIR;
3090 }
3091 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3092 {
3093 did_curdir = TRUE;
3094 flags |= EW_DIR;
3095 }
3096 else
3097 // Do not match directories inside a $PATH item.
3098 flags &= ~EW_DIR;
3099
3100 l = e - s;
3101 if (l > MAXPATHL - 5)
3102 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003103
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003104 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003105 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003106
Bram Moolenaar66b51422019-08-18 21:44:12 +02003107 if (*e != NUL)
3108 ++e;
3109 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003110 *matches = ga.ga_data;
3111 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112
3113 vim_free(buf);
3114 vim_free(pat);
3115 if (mustfree)
3116 vim_free(path);
3117 hash_clear(&found_ht);
3118 return OK;
3119}
3120
3121# if defined(FEAT_EVAL)
3122/*
3123 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003124 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125 */
3126 static void *
3127call_user_expand_func(
3128 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003129 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130{
3131 cmdline_info_T *ccline = get_cmdline_info();
3132 int keep = 0;
3133 typval_T args[4];
3134 sctx_T save_current_sctx = current_sctx;
3135 char_u *pat = NULL;
3136 void *ret;
3137
3138 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3139 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140
3141 if (ccline->cmdbuff != NULL)
3142 {
3143 keep = ccline->cmdbuff[ccline->cmdlen];
3144 ccline->cmdbuff[ccline->cmdlen] = 0;
3145 }
3146
3147 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3148
3149 args[0].v_type = VAR_STRING;
3150 args[0].vval.v_string = pat;
3151 args[1].v_type = VAR_STRING;
3152 args[1].vval.v_string = xp->xp_line;
3153 args[2].v_type = VAR_NUMBER;
3154 args[2].vval.v_number = xp->xp_col;
3155 args[3].v_type = VAR_UNKNOWN;
3156
3157 current_sctx = xp->xp_script_ctx;
3158
3159 ret = user_expand_func(xp->xp_arg, 3, args);
3160
3161 current_sctx = save_current_sctx;
3162 if (ccline->cmdbuff != NULL)
3163 ccline->cmdbuff[ccline->cmdlen] = keep;
3164
3165 vim_free(pat);
3166 return ret;
3167}
3168
3169/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003170 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3171 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003172 */
3173 static int
3174ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003175 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003176 expand_T *xp,
3177 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003178 char_u ***matches,
3179 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003180{
3181 char_u *retstr;
3182 char_u *s;
3183 char_u *e;
3184 int keep;
3185 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003186 int fuzzy;
3187 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003188 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003189
3190 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003191 *matches = NULL;
3192 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003193
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003194 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003195 if (retstr == NULL)
3196 return FAIL;
3197
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003198 if (!fuzzy)
3199 ga_init2(&ga, sizeof(char *), 3);
3200 else
3201 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3202
Bram Moolenaar66b51422019-08-18 21:44:12 +02003203 for (s = retstr; *s != NUL; s = e)
3204 {
3205 e = vim_strchr(s, '\n');
3206 if (e == NULL)
3207 e = s + STRLEN(s);
3208 keep = *e;
3209 *e = NUL;
3210
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003211 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003212 {
3213 if (!fuzzy)
3214 match = vim_regexec(regmatch, s, (colnr_T)0);
3215 else
3216 {
3217 score = fuzzy_match_str(s, pat);
3218 match = (score != 0);
3219 }
3220 }
3221 else
3222 match = TRUE; // match everything
3223
Bram Moolenaar66b51422019-08-18 21:44:12 +02003224 *e = keep;
3225
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003226 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003227 {
3228 if (ga_grow(&ga, 1) == FAIL)
3229 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003230 if (!fuzzy)
3231 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3232 else
3233 {
3234 fuzmatch_str_T *fuzmatch =
3235 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003236 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003237 fuzmatch->str = vim_strnsave(s, e - s);
3238 fuzmatch->score = score;
3239 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003240 ++ga.ga_len;
3241 }
3242
3243 if (*e != NUL)
3244 ++e;
3245 }
3246 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003247
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003248 if (ga.ga_len == 0)
3249 return OK;
3250
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003251 if (!fuzzy)
3252 {
3253 *matches = ga.ga_data;
3254 *numMatches = ga.ga_len;
3255 }
3256 else
3257 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003258 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3259 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003260 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003261 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003262 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003263 return OK;
3264}
3265
3266/*
3267 * Expand names with a list returned by a function defined by the user.
3268 */
3269 static int
3270ExpandUserList(
3271 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003272 char_u ***matches,
3273 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003274{
3275 list_T *retlist;
3276 listitem_T *li;
3277 garray_T ga;
3278
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003279 *matches = NULL;
3280 *numMatches = 0;
3281 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003282 if (retlist == NULL)
3283 return FAIL;
3284
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003285 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003286 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003287 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003288 {
3289 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3290 continue; // Skip non-string items and empty strings
3291
3292 if (ga_grow(&ga, 1) == FAIL)
3293 break;
3294
3295 ((char_u **)ga.ga_data)[ga.ga_len] =
3296 vim_strsave(li->li_tv.vval.v_string);
3297 ++ga.ga_len;
3298 }
3299 list_unref(retlist);
3300
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003301 *matches = ga.ga_data;
3302 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003303 return OK;
3304}
3305# endif
3306
3307/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003308 * Expand "file" for all comma-separated directories in "path".
3309 * Adds the matches to "ga". Caller must init "ga".
3310 */
3311 void
3312globpath(
3313 char_u *path,
3314 char_u *file,
3315 garray_T *ga,
3316 int expand_options)
3317{
3318 expand_T xpc;
3319 char_u *buf;
3320 int i;
3321 int num_p;
3322 char_u **p;
3323
3324 buf = alloc(MAXPATHL);
3325 if (buf == NULL)
3326 return;
3327
3328 ExpandInit(&xpc);
3329 xpc.xp_context = EXPAND_FILES;
3330
3331 // Loop over all entries in {path}.
3332 while (*path != NUL)
3333 {
3334 // Copy one item of the path to buf[] and concatenate the file name.
3335 copy_option_part(&path, buf, MAXPATHL, ",");
3336 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3337 {
3338# if defined(MSWIN)
3339 // Using the platform's path separator (\) makes vim incorrectly
3340 // treat it as an escape character, use '/' instead.
3341 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3342 STRCAT(buf, "/");
3343# else
3344 add_pathsep(buf);
3345# endif
3346 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003347 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003348 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3349 {
3350 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3351
3352 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003353 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354 for (i = 0; i < num_p; ++i)
3355 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003356 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003357 ++ga->ga_len;
3358 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003359 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360 }
3361 }
3362 }
3363
3364 vim_free(buf);
3365}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003366
Bram Moolenaareadee482020-09-04 15:37:31 +02003367/*
3368 * Translate some keys pressed when 'wildmenu' is used.
3369 */
3370 int
3371wildmenu_translate_key(
3372 cmdline_info_T *cclp,
3373 int key,
3374 expand_T *xp,
3375 int did_wild_list)
3376{
3377 int c = key;
3378
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003379 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003380 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003381 // When the popup menu is used for cmdline completion:
3382 // Up : go to the previous item in the menu
3383 // Down : go to the next item in the menu
3384 // Left : go to the parent directory
3385 // Right: list the files in the selected directory
3386 switch (c)
3387 {
3388 case K_UP: c = K_LEFT; break;
3389 case K_DOWN: c = K_RIGHT; break;
3390 case K_LEFT: c = K_UP; break;
3391 case K_RIGHT: c = K_DOWN; break;
3392 default: break;
3393 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003394 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003395
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003396 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003397 {
3398 if (c == K_LEFT)
3399 c = Ctrl_P;
3400 else if (c == K_RIGHT)
3401 c = Ctrl_N;
3402 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003403
Bram Moolenaareadee482020-09-04 15:37:31 +02003404 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003405 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003406 && cclp->cmdpos > 1
3407 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3408 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3409 && (c == '\n' || c == '\r' || c == K_KENTER))
3410 c = K_DOWN;
3411
3412 return c;
3413}
3414
3415/*
3416 * Delete characters on the command line, from "from" to the current
3417 * position.
3418 */
3419 static void
3420cmdline_del(cmdline_info_T *cclp, int from)
3421{
3422 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3423 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3424 cclp->cmdlen -= cclp->cmdpos - from;
3425 cclp->cmdpos = from;
3426}
3427
3428/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003429 * Handle a key pressed when the wild menu for the menu names
3430 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003431 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003432 static int
3433wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003434{
Bram Moolenaareadee482020-09-04 15:37:31 +02003435 int i;
3436 int j;
3437
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003438 // Hitting <Down> after "emenu Name.": complete submenu
3439 if (key == K_DOWN && cclp->cmdpos > 0
3440 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003441 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003442 key = p_wc;
3443 KeyTyped = TRUE; // in case the key was mapped
3444 }
3445 else if (key == K_UP)
3446 {
3447 // Hitting <Up>: Remove one submenu name in front of the
3448 // cursor
3449 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003450
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003451 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3452 i = 0;
3453 while (--j > 0)
3454 {
3455 // check for start of menu name
3456 if (cclp->cmdbuff[j] == ' '
3457 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003458 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003459 i = j + 1;
3460 break;
3461 }
3462 // check for start of submenu name
3463 if (cclp->cmdbuff[j] == '.'
3464 && cclp->cmdbuff[j - 1] != '\\')
3465 {
3466 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003467 {
3468 i = j + 1;
3469 break;
3470 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003471 else
3472 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003473 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003474 }
3475 if (i > 0)
3476 cmdline_del(cclp, i);
3477 key = p_wc;
3478 KeyTyped = TRUE; // in case the key was mapped
3479 xp->xp_context = EXPAND_NOTHING;
3480 }
3481
3482 return key;
3483}
3484
3485/*
3486 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3487 * directory names (EXPAND_DIRECTORIES) or shell command names
3488 * (EXPAND_SHELLCMD) is displayed.
3489 */
3490 static int
3491wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3492{
3493 int i;
3494 int j;
3495 char_u upseg[5];
3496
3497 upseg[0] = PATHSEP;
3498 upseg[1] = '.';
3499 upseg[2] = '.';
3500 upseg[3] = PATHSEP;
3501 upseg[4] = NUL;
3502
3503 if (key == K_DOWN
3504 && cclp->cmdpos > 0
3505 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3506 && (cclp->cmdpos < 3
3507 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3508 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3509 {
3510 // go down a directory
3511 key = p_wc;
3512 KeyTyped = TRUE; // in case the key was mapped
3513 }
3514 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3515 {
3516 // If in a direct ancestor, strip off one ../ to go down
3517 int found = FALSE;
3518
3519 j = cclp->cmdpos;
3520 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3521 while (--j > i)
3522 {
3523 if (has_mbyte)
3524 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3525 if (vim_ispathsep(cclp->cmdbuff[j]))
3526 {
3527 found = TRUE;
3528 break;
3529 }
3530 }
3531 if (found
3532 && cclp->cmdbuff[j - 1] == '.'
3533 && cclp->cmdbuff[j - 2] == '.'
3534 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3535 {
3536 cmdline_del(cclp, j - 2);
3537 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003538 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003539 }
3540 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003541 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003542 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003543 // go up a directory
3544 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003545
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003546 j = cclp->cmdpos - 1;
3547 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3548 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003549 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003550 if (has_mbyte)
3551 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3552 if (vim_ispathsep(cclp->cmdbuff[j])
3553# ifdef BACKSLASH_IN_FILENAME
3554 && vim_strchr((char_u *)" *?[{`$%#",
3555 cclp->cmdbuff[j + 1]) == NULL
3556# endif
3557 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003558 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003559 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003560 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003561 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003562 break;
3563 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003564 else
3565 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003566 }
3567 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003568
3569 if (!found)
3570 j = i;
3571 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3572 j += 4;
3573 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3574 && j == i)
3575 j += 3;
3576 else
3577 j = 0;
3578 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003579 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003580 // TODO this is only for DOS/UNIX systems - need to put in
3581 // machine-specific stuff here and in upseg init
3582 cmdline_del(cclp, j);
3583 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003584 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003585 else if (cclp->cmdpos > i)
3586 cmdline_del(cclp, i);
3587
3588 // Now complete in the new directory. Set KeyTyped in case the
3589 // Up key came from a mapping.
3590 key = p_wc;
3591 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003592 }
3593
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003594 return key;
3595}
3596
3597/*
3598 * Handle a key pressed when the wild menu is displayed
3599 */
3600 int
3601wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3602{
3603 if (xp->xp_context == EXPAND_MENUNAMES)
3604 return wildmenu_process_key_menunames(cclp, key, xp);
3605 else if ((xp->xp_context == EXPAND_FILES
3606 || xp->xp_context == EXPAND_DIRECTORIES
3607 || xp->xp_context == EXPAND_SHELLCMD))
3608 return wildmenu_process_key_filenames(cclp, key, xp);
3609
3610 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003611}
3612
3613/*
3614 * Free expanded names when finished walking through the matches
3615 */
3616 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003617wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003618{
3619 int skt = KeyTyped;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003620#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003621 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003622#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003623
3624 if (!p_wmnu || wild_menu_showing == 0)
3625 return;
3626
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003627#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003628 if (cclp->input_fn)
3629 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003630#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003631
3632 if (wild_menu_showing == WM_SCROLLED)
3633 {
3634 // Entered command line, move it up
3635 cmdline_row--;
3636 redrawcmd();
3637 }
3638 else if (save_p_ls != -1)
3639 {
3640 // restore 'laststatus' and 'winminheight'
3641 p_ls = save_p_ls;
3642 p_wmh = save_p_wmh;
3643 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003644 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003645 redrawcmd();
3646 save_p_ls = -1;
3647 }
3648 else
3649 {
3650 win_redraw_last_status(topframe);
3651 redraw_statuslines();
3652 }
3653 KeyTyped = skt;
3654 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003655#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003656 if (cclp->input_fn)
3657 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003658#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003659}
Bram Moolenaareadee482020-09-04 15:37:31 +02003660
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003661#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003662/*
3663 * "getcompletion()" function
3664 */
3665 void
3666f_getcompletion(typval_T *argvars, typval_T *rettv)
3667{
3668 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003669 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003670 expand_T xpc;
3671 int filtered = FALSE;
3672 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003673 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003674
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003675 if (in_vim9script()
3676 && (check_for_string_arg(argvars, 0) == FAIL
3677 || check_for_string_arg(argvars, 1) == FAIL
3678 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3679 return;
3680
ii144785fe02021-11-21 12:13:56 +00003681 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003682 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003683 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003684 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003685
3686 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003687 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688
3689 if (p_wic)
3690 options |= WILD_ICASE;
3691
3692 // For filtered results, 'wildignore' is used
3693 if (!filtered)
3694 options |= WILD_KEEP_ALL;
3695
3696 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003697 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003698 {
ii144785fe02021-11-21 12:13:56 +00003699 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003700 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003701 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003702 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003703 else
3704 {
ii144785fe02021-11-21 12:13:56 +00003705 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003706 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3707
3708 xpc.xp_context = cmdcomplete_str_to_type(type);
3709 if (xpc.xp_context == EXPAND_NOTHING)
3710 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003711 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003712 return;
3713 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003714
3715# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003716 if (xpc.xp_context == EXPAND_MENUS)
3717 {
3718 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3719 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3720 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003721# endif
3722# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003723 if (xpc.xp_context == EXPAND_CSCOPE)
3724 {
3725 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3726 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3727 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003728# endif
3729# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003730 if (xpc.xp_context == EXPAND_SIGN)
3731 {
3732 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3733 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3734 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003735# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003736 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003737
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00003738 if (cmdline_fuzzy_completion_supported(&xpc))
3739 // when fuzzy matching, don't modify the search string
3740 pat = vim_strsave(xpc.xp_pattern);
3741 else
3742 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3743
Bram Moolenaar93a10962022-06-16 11:42:09 +01003744 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003745 {
3746 int i;
3747
3748 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3749
3750 for (i = 0; i < xpc.xp_numfiles; i++)
3751 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3752 }
3753 vim_free(pat);
3754 ExpandCleanup(&xpc);
3755}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003756#endif // FEAT_EVAL