blob: a62c6abf619af39681006ac6ffea241eca8f0a4a [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:
1779 case CMD_keepalt:
1780 case CMD_keepjumps:
1781 case CMD_keepmarks:
1782 case CMD_keeppatterns:
1783 case CMD_ldo:
1784 case CMD_leftabove:
1785 case CMD_lfdo:
1786 case CMD_lockmarks:
1787 case CMD_noautocmd:
1788 case CMD_noswapfile:
1789 case CMD_rightbelow:
1790 case CMD_sandbox:
1791 case CMD_silent:
1792 case CMD_tab:
1793 case CMD_tabdo:
1794 case CMD_topleft:
1795 case CMD_verbose:
1796 case CMD_vertical:
1797 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001798 case CMD_vim9cmd:
1799 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001800 return arg;
1801
1802 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001803 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001804
1805#ifdef FEAT_SEARCH_EXTRA
1806 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001807 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001808#endif
1809
1810 // All completion for the +cmdline_compl feature goes here.
1811
1812 case CMD_command:
1813 return set_context_in_user_cmd(xp, arg);
1814
1815 case CMD_delcommand:
1816 xp->xp_context = EXPAND_USER_COMMANDS;
1817 xp->xp_pattern = arg;
1818 break;
1819
1820 case CMD_global:
1821 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001822 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001823 case CMD_and:
1824 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001825 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001826 case CMD_isearch:
1827 case CMD_dsearch:
1828 case CMD_ilist:
1829 case CMD_dlist:
1830 case CMD_ijump:
1831 case CMD_psearch:
1832 case CMD_djump:
1833 case CMD_isplit:
1834 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001835 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001836 case CMD_autocmd:
1837 return set_context_in_autocmd(xp, arg, FALSE);
1838 case CMD_doautocmd:
1839 case CMD_doautoall:
1840 return set_context_in_autocmd(xp, arg, TRUE);
1841 case CMD_set:
1842 set_context_in_set_cmd(xp, arg, 0);
1843 break;
1844 case CMD_setglobal:
1845 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1846 break;
1847 case CMD_setlocal:
1848 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1849 break;
1850 case CMD_tag:
1851 case CMD_stag:
1852 case CMD_ptag:
1853 case CMD_ltag:
1854 case CMD_tselect:
1855 case CMD_stselect:
1856 case CMD_ptselect:
1857 case CMD_tjump:
1858 case CMD_stjump:
1859 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001860 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001861 xp->xp_context = EXPAND_TAGS_LISTFILES;
1862 else
1863 xp->xp_context = EXPAND_TAGS;
1864 xp->xp_pattern = arg;
1865 break;
1866 case CMD_augroup:
1867 xp->xp_context = EXPAND_AUGROUP;
1868 xp->xp_pattern = arg;
1869 break;
1870#ifdef FEAT_SYN_HL
1871 case CMD_syntax:
1872 set_context_in_syntax_cmd(xp, arg);
1873 break;
1874#endif
1875#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001876 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001877 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001878 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001879 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001880 case CMD_if:
1881 case CMD_elseif:
1882 case CMD_while:
1883 case CMD_for:
1884 case CMD_echo:
1885 case CMD_echon:
1886 case CMD_execute:
1887 case CMD_echomsg:
1888 case CMD_echoerr:
1889 case CMD_call:
1890 case CMD_return:
1891 case CMD_cexpr:
1892 case CMD_caddexpr:
1893 case CMD_cgetexpr:
1894 case CMD_lexpr:
1895 case CMD_laddexpr:
1896 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001897 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001898 break;
1899
1900 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001901 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001902 case CMD_function:
1903 case CMD_delfunction:
1904 xp->xp_context = EXPAND_USER_FUNC;
1905 xp->xp_pattern = arg;
1906 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001907 case CMD_disassemble:
1908 set_context_in_disassemble_cmd(xp, arg);
1909 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001910
1911 case CMD_echohl:
1912 set_context_in_echohl_cmd(xp, arg);
1913 break;
1914#endif
1915 case CMD_highlight:
1916 set_context_in_highlight_cmd(xp, arg);
1917 break;
1918#ifdef FEAT_CSCOPE
1919 case CMD_cscope:
1920 case CMD_lcscope:
1921 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001922 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001923 break;
1924#endif
1925#ifdef FEAT_SIGNS
1926 case CMD_sign:
1927 set_context_in_sign_cmd(xp, arg);
1928 break;
1929#endif
1930 case CMD_bdelete:
1931 case CMD_bwipeout:
1932 case CMD_bunload:
1933 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1934 arg = xp->xp_pattern + 1;
1935 // FALLTHROUGH
1936 case CMD_buffer:
1937 case CMD_sbuffer:
1938 case CMD_checktime:
1939 xp->xp_context = EXPAND_BUFFERS;
1940 xp->xp_pattern = arg;
1941 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001942#ifdef FEAT_DIFF
1943 case CMD_diffget:
1944 case CMD_diffput:
1945 // If current buffer is in diff mode, complete buffer names
1946 // which are in diff mode, and different than current buffer.
1947 xp->xp_context = EXPAND_DIFF_BUFFERS;
1948 xp->xp_pattern = arg;
1949 break;
1950#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001951 case CMD_USER:
1952 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001953 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1954 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001955
1956 case CMD_map: case CMD_noremap:
1957 case CMD_nmap: case CMD_nnoremap:
1958 case CMD_vmap: case CMD_vnoremap:
1959 case CMD_omap: case CMD_onoremap:
1960 case CMD_imap: case CMD_inoremap:
1961 case CMD_cmap: case CMD_cnoremap:
1962 case CMD_lmap: case CMD_lnoremap:
1963 case CMD_smap: case CMD_snoremap:
1964 case CMD_tmap: case CMD_tnoremap:
1965 case CMD_xmap: case CMD_xnoremap:
1966 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001967 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001968 case CMD_unmap:
1969 case CMD_nunmap:
1970 case CMD_vunmap:
1971 case CMD_ounmap:
1972 case CMD_iunmap:
1973 case CMD_cunmap:
1974 case CMD_lunmap:
1975 case CMD_sunmap:
1976 case CMD_tunmap:
1977 case CMD_xunmap:
1978 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001979 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001980 case CMD_mapclear:
1981 case CMD_nmapclear:
1982 case CMD_vmapclear:
1983 case CMD_omapclear:
1984 case CMD_imapclear:
1985 case CMD_cmapclear:
1986 case CMD_lmapclear:
1987 case CMD_smapclear:
1988 case CMD_tmapclear:
1989 case CMD_xmapclear:
1990 xp->xp_context = EXPAND_MAPCLEAR;
1991 xp->xp_pattern = arg;
1992 break;
1993
1994 case CMD_abbreviate: case CMD_noreabbrev:
1995 case CMD_cabbrev: case CMD_cnoreabbrev:
1996 case CMD_iabbrev: case CMD_inoreabbrev:
1997 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001998 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001999 case CMD_unabbreviate:
2000 case CMD_cunabbrev:
2001 case CMD_iunabbrev:
2002 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002003 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002004#ifdef FEAT_MENU
2005 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2006 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2007 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2008 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2009 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2010 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2011 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2012 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2013 case CMD_tmenu: case CMD_tunmenu:
2014 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2015 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2016#endif
2017
2018 case CMD_colorscheme:
2019 xp->xp_context = EXPAND_COLORS;
2020 xp->xp_pattern = arg;
2021 break;
2022
2023 case CMD_compiler:
2024 xp->xp_context = EXPAND_COMPILER;
2025 xp->xp_pattern = arg;
2026 break;
2027
2028 case CMD_ownsyntax:
2029 xp->xp_context = EXPAND_OWNSYNTAX;
2030 xp->xp_pattern = arg;
2031 break;
2032
2033 case CMD_setfiletype:
2034 xp->xp_context = EXPAND_FILETYPE;
2035 xp->xp_pattern = arg;
2036 break;
2037
2038 case CMD_packadd:
2039 xp->xp_context = EXPAND_PACKADD;
2040 xp->xp_pattern = arg;
2041 break;
2042
2043#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2044 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002045 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002046#endif
2047#if defined(FEAT_PROFILE)
2048 case CMD_profile:
2049 set_context_in_profile_cmd(xp, arg);
2050 break;
2051#endif
2052 case CMD_behave:
2053 xp->xp_context = EXPAND_BEHAVE;
2054 xp->xp_pattern = arg;
2055 break;
2056
2057 case CMD_messages:
2058 xp->xp_context = EXPAND_MESSAGES;
2059 xp->xp_pattern = arg;
2060 break;
2061
2062 case CMD_history:
2063 xp->xp_context = EXPAND_HISTORY;
2064 xp->xp_pattern = arg;
2065 break;
2066#if defined(FEAT_PROFILE)
2067 case CMD_syntime:
2068 xp->xp_context = EXPAND_SYNTIME;
2069 xp->xp_pattern = arg;
2070 break;
2071#endif
2072
2073 case CMD_argdelete:
2074 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2075 arg = xp->xp_pattern + 1;
2076 xp->xp_context = EXPAND_ARGLIST;
2077 xp->xp_pattern = arg;
2078 break;
2079
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002080#ifdef FEAT_EVAL
2081 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002082 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002083 case CMD_breakdel:
2084 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002085
2086 case CMD_scriptnames:
2087 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002088#endif
2089
Bram Moolenaard0190392019-08-23 21:17:35 +02002090 default:
2091 break;
2092 }
2093 return NULL;
2094}
2095
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002096/*
2097 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2098 * we don't need/want deleted. Maybe this could be done better if we didn't
2099 * repeat all this stuff. The only problem is that they may not stay
2100 * perfectly compatible with each other, but then the command line syntax
2101 * probably won't change that much -- webb.
2102 */
2103 static char_u *
2104set_one_cmd_context(
2105 expand_T *xp,
2106 char_u *buff) // buffer for command string
2107{
2108 char_u *p;
2109 char_u *cmd, *arg;
2110 int len = 0;
2111 exarg_T ea;
2112 int compl = EXPAND_NOTHING;
2113 int forceit = FALSE;
2114 int usefilter = FALSE; // filter instead of file name
2115
2116 ExpandInit(xp);
2117 xp->xp_pattern = buff;
2118 xp->xp_line = buff;
2119 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2120 ea.argt = 0;
2121
2122 // 1. skip comment lines and leading space, colons or bars
2123 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2124 ;
2125 xp->xp_pattern = cmd;
2126
2127 if (*cmd == NUL)
2128 return NULL;
2129 if (*cmd == '"') // ignore comment lines
2130 {
2131 xp->xp_context = EXPAND_NOTHING;
2132 return NULL;
2133 }
2134
2135 // 3. Skip over the range to find the command.
2136 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2137 xp->xp_pattern = cmd;
2138 if (*cmd == NUL)
2139 return NULL;
2140 if (*cmd == '"')
2141 {
2142 xp->xp_context = EXPAND_NOTHING;
2143 return NULL;
2144 }
2145
2146 if (*cmd == '|' || *cmd == '\n')
2147 return cmd + 1; // There's another command
2148
2149 // Get the command index.
2150 p = set_cmd_index(cmd, &ea, xp, &compl);
2151 if (p == NULL)
2152 return NULL;
2153
2154 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2155
2156 if (*p == '!') // forced commands
2157 {
2158 forceit = TRUE;
2159 ++p;
2160 }
2161
2162 // 6. parse arguments
2163 if (!IS_USER_CMDIDX(ea.cmdidx))
2164 ea.argt = excmd_get_argt(ea.cmdidx);
2165
2166 arg = skipwhite(p);
2167
2168 // Skip over ++argopt argument
2169 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2170 {
2171 p = arg;
2172 while (*p && !vim_isspace(*p))
2173 MB_PTR_ADV(p);
2174 arg = skipwhite(p);
2175 }
2176
2177 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2178 {
2179 if (*arg == '>') // append
2180 {
2181 if (*++arg == '>')
2182 ++arg;
2183 arg = skipwhite(arg);
2184 }
2185 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2186 {
2187 ++arg;
2188 usefilter = TRUE;
2189 }
2190 }
2191
2192 if (ea.cmdidx == CMD_read)
2193 {
2194 usefilter = forceit; // :r! filter if forced
2195 if (*arg == '!') // :r !filter
2196 {
2197 ++arg;
2198 usefilter = TRUE;
2199 }
2200 }
2201
2202 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2203 {
2204 while (*arg == *cmd) // allow any number of '>' or '<'
2205 ++arg;
2206 arg = skipwhite(arg);
2207 }
2208
2209 // Does command allow "+command"?
2210 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2211 {
2212 // Check if we're in the +command
2213 p = arg + 1;
2214 arg = skip_cmd_arg(arg, FALSE);
2215
2216 // Still touching the command after '+'?
2217 if (*arg == NUL)
2218 return p;
2219
2220 // Skip space(s) after +command to get to the real argument
2221 arg = skipwhite(arg);
2222 }
2223
2224
2225 // Check for '|' to separate commands and '"' to start comments.
2226 // Don't do this for ":read !cmd" and ":write !cmd".
2227 if ((ea.argt & EX_TRLBAR) && !usefilter)
2228 {
2229 p = arg;
2230 // ":redir @" is not the start of a comment
2231 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2232 p += 2;
2233 while (*p)
2234 {
2235 if (*p == Ctrl_V)
2236 {
2237 if (p[1] != NUL)
2238 ++p;
2239 }
2240 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2241 || *p == '|' || *p == '\n')
2242 {
2243 if (*(p - 1) != '\\')
2244 {
2245 if (*p == '|' || *p == '\n')
2246 return p + 1;
2247 return NULL; // It's a comment
2248 }
2249 }
2250 MB_PTR_ADV(p);
2251 }
2252 }
2253
2254 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2255 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2256 // no arguments allowed but there is something
2257 return NULL;
2258
2259 // Find start of last argument (argument just before cursor):
2260 p = buff;
2261 xp->xp_pattern = p;
2262 len = (int)STRLEN(buff);
2263 while (*p && p < buff + len)
2264 {
2265 if (*p == ' ' || *p == TAB)
2266 {
2267 // argument starts after a space
2268 xp->xp_pattern = ++p;
2269 }
2270 else
2271 {
2272 if (*p == '\\' && *(p + 1) != NUL)
2273 ++p; // skip over escaped character
2274 MB_PTR_ADV(p);
2275 }
2276 }
2277
2278 if (ea.argt & EX_XFILE)
2279 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2280
2281 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002282 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002283 forceit);
2284}
2285
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002286/*
2287 * Set the completion context in 'xp' for command 'str'
2288 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002289 void
2290set_cmd_context(
2291 expand_T *xp,
2292 char_u *str, // start of command line
2293 int len, // length of command line (excl. NUL)
2294 int col, // position of cursor
2295 int use_ccline UNUSED) // use ccline for info
2296{
2297#ifdef FEAT_EVAL
2298 cmdline_info_T *ccline = get_cmdline_info();
2299#endif
2300 int old_char = NUL;
2301 char_u *nextcomm;
2302
2303 // Avoid a UMR warning from Purify, only save the character if it has been
2304 // written before.
2305 if (col < len)
2306 old_char = str[col];
2307 str[col] = NUL;
2308 nextcomm = str;
2309
2310#ifdef FEAT_EVAL
2311 if (use_ccline && ccline->cmdfirstc == '=')
2312 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002313 // pass CMD_SIZE because there is no real command
2314 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002315 }
2316 else if (use_ccline && ccline->input_fn)
2317 {
2318 xp->xp_context = ccline->xp_context;
2319 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002320 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002321 }
2322 else
2323#endif
2324 while (nextcomm != NULL)
2325 nextcomm = set_one_cmd_context(xp, nextcomm);
2326
2327 // Store the string here so that call_user_expand_func() can get to them
2328 // easily.
2329 xp->xp_line = str;
2330 xp->xp_col = col;
2331
2332 str[col] = old_char;
2333}
2334
2335/*
2336 * Expand the command line "str" from context "xp".
2337 * "xp" must have been set by set_cmd_context().
2338 * xp->xp_pattern points into "str", to where the text that is to be expanded
2339 * starts.
2340 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2341 * cursor.
2342 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2343 * key that triggered expansion literally.
2344 * Returns EXPAND_OK otherwise.
2345 */
2346 int
2347expand_cmdline(
2348 expand_T *xp,
2349 char_u *str, // start of command line
2350 int col, // position of cursor
2351 int *matchcount, // return: nr of matches
2352 char_u ***matches) // return: array of pointers to matches
2353{
2354 char_u *file_str = NULL;
2355 int options = WILD_ADD_SLASH|WILD_SILENT;
2356
2357 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2358 {
2359 beep_flush();
2360 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2361 }
2362 if (xp->xp_context == EXPAND_NOTHING)
2363 {
2364 // Caller can use the character as a normal char instead
2365 return EXPAND_NOTHING;
2366 }
2367
2368 // add star to file name, or convert to regexp if not exp. files.
2369 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002370 if (cmdline_fuzzy_completion_supported(xp))
2371 // If fuzzy matching, don't modify the search string
2372 file_str = vim_strsave(xp->xp_pattern);
2373 else
2374 {
2375 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2376 if (file_str == NULL)
2377 return EXPAND_UNSUCCESSFUL;
2378 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002379
2380 if (p_wic)
2381 options += WILD_ICASE;
2382
2383 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002384 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002385 {
2386 *matchcount = 0;
2387 *matches = NULL;
2388 }
2389 vim_free(file_str);
2390
2391 return EXPAND_OK;
2392}
2393
Bram Moolenaar66b51422019-08-18 21:44:12 +02002394/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002395 * Expand file or directory names.
2396 */
2397 static int
2398expand_files_and_dirs(
2399 expand_T *xp,
2400 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002401 char_u ***matches,
2402 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002403 int flags,
2404 int options)
2405{
2406 int free_pat = FALSE;
2407 int i;
2408 int ret;
2409
2410 // for ":set path=" and ":set tags=" halve backslashes for escaped
2411 // space
2412 if (xp->xp_backslash != XP_BS_NONE)
2413 {
2414 free_pat = TRUE;
2415 pat = vim_strsave(pat);
2416 for (i = 0; pat[i]; ++i)
2417 if (pat[i] == '\\')
2418 {
2419 if (xp->xp_backslash == XP_BS_THREE
2420 && pat[i + 1] == '\\'
2421 && pat[i + 2] == '\\'
2422 && pat[i + 3] == ' ')
2423 STRMOVE(pat + i, pat + i + 3);
2424 if (xp->xp_backslash == XP_BS_ONE
2425 && pat[i + 1] == ' ')
2426 STRMOVE(pat + i, pat + i + 1);
2427 }
2428 }
2429
2430 if (xp->xp_context == EXPAND_FILES)
2431 flags |= EW_FILE;
2432 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2433 flags |= (EW_FILE | EW_PATH);
2434 else
2435 flags = (flags | EW_DIR) & ~EW_FILE;
2436 if (options & WILD_ICASE)
2437 flags |= EW_ICASE;
2438
2439 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002440 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002441 if (free_pat)
2442 vim_free(pat);
2443#ifdef BACKSLASH_IN_FILENAME
2444 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2445 {
2446 int j;
2447
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002448 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002449 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002450 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002451
2452 while (*ptr != NUL)
2453 {
2454 if (p_csl[0] == 's' && *ptr == '\\')
2455 *ptr = '/';
2456 else if (p_csl[0] == 'b' && *ptr == '/')
2457 *ptr = '\\';
2458 ptr += (*mb_ptr2len)(ptr);
2459 }
2460 }
2461 }
2462#endif
2463 return ret;
2464}
2465
2466/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002467 * Function given to ExpandGeneric() to obtain the possible arguments of the
2468 * ":behave {mswin,xterm}" command.
2469 */
2470 static char_u *
2471get_behave_arg(expand_T *xp UNUSED, int idx)
2472{
2473 if (idx == 0)
2474 return (char_u *)"mswin";
2475 if (idx == 1)
2476 return (char_u *)"xterm";
2477 return NULL;
2478}
2479
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002480# ifdef FEAT_EVAL
2481/*
2482 * Function given to ExpandGeneric() to obtain the possible arguments of the
2483 * ":breakadd {expr, file, func, here}" command.
2484 * ":breakdel {func, file, here}" command.
2485 */
2486 static char_u *
2487get_breakadd_arg(expand_T *xp UNUSED, int idx)
2488{
2489 char *opts[] = {"expr", "file", "func", "here"};
2490
2491 if (idx >=0 && idx <= 3)
2492 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002493 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002494 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2495 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002496 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2497 {
2498 // breakdel {func, file, here}
2499 if (idx <= 2)
2500 return (char_u *)opts[idx + 1];
2501 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002502 else
2503 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002504 // profdel {func, file}
2505 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002506 return (char_u *)opts[idx + 1];
2507 }
2508 }
2509 return NULL;
2510}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002511
2512/*
2513 * Function given to ExpandGeneric() to obtain the possible arguments for the
2514 * ":scriptnames" command.
2515 */
2516 static char_u *
2517get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2518{
2519 scriptitem_T *si;
2520
2521 if (!SCRIPT_ID_VALID(idx + 1))
2522 return NULL;
2523
2524 si = SCRIPT_ITEM(idx + 1);
2525 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2526 return NameBuff;
2527}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002528#endif
2529
Bram Moolenaard0190392019-08-23 21:17:35 +02002530/*
2531 * Function given to ExpandGeneric() to obtain the possible arguments of the
2532 * ":messages {clear}" command.
2533 */
2534 static char_u *
2535get_messages_arg(expand_T *xp UNUSED, int idx)
2536{
2537 if (idx == 0)
2538 return (char_u *)"clear";
2539 return NULL;
2540}
2541
2542 static char_u *
2543get_mapclear_arg(expand_T *xp UNUSED, int idx)
2544{
2545 if (idx == 0)
2546 return (char_u *)"<buffer>";
2547 return NULL;
2548}
2549
2550/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002551 * Do the expansion based on xp->xp_context and 'rmp'.
2552 */
2553 static int
2554ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002555 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002556 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002557 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002558 char_u ***matches,
2559 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002560{
2561 static struct expgen
2562 {
2563 int context;
2564 char_u *((*func)(expand_T *, int));
2565 int ic;
2566 int escaped;
2567 } tab[] =
2568 {
2569 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2570 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2571 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2572 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2573 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2574 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2575 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2576 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2577 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2578 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002579#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002580 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2581 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2582 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2583 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2584 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002585#endif
2586#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002587 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2588 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002589#endif
2590#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002591 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002592#endif
2593#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002594 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002595#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002596 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2597 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2598 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002599#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002600 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002601#endif
2602#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002603 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002604#endif
2605#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002606 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002607#endif
2608#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002609 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2610 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002611#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002612 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2613 {EXPAND_USER, get_users, TRUE, FALSE},
2614 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002615#ifdef FEAT_EVAL
2616 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002617 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002618#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002619 };
2620 int i;
2621 int ret = FAIL;
2622
2623 // Find a context in the table and call the ExpandGeneric() with the
2624 // right function to do the expansion.
2625 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2626 {
2627 if (xp->xp_context == tab[i].context)
2628 {
2629 if (tab[i].ic)
2630 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002631 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2632 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002633 break;
2634 }
2635 }
2636
2637 return ret;
2638}
2639
2640/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002641 * Map wild expand options to flags for expand_wildcards()
2642 */
2643 static int
2644map_wildopts_to_ewflags(int options)
2645{
2646 int flags;
2647
2648 flags = EW_DIR; // include directories
2649 if (options & WILD_LIST_NOTFOUND)
2650 flags |= EW_NOTFOUND;
2651 if (options & WILD_ADD_SLASH)
2652 flags |= EW_ADDSLASH;
2653 if (options & WILD_KEEP_ALL)
2654 flags |= EW_KEEPALL;
2655 if (options & WILD_SILENT)
2656 flags |= EW_SILENT;
2657 if (options & WILD_NOERROR)
2658 flags |= EW_NOERROR;
2659 if (options & WILD_ALLLINKS)
2660 flags |= EW_ALLLINKS;
2661
2662 return flags;
2663}
2664
2665/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002666 * Do the expansion based on xp->xp_context and "pat".
2667 */
2668 static int
2669ExpandFromContext(
2670 expand_T *xp,
2671 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002672 char_u ***matches,
2673 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002674 int options) // WILD_ flags
2675{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002676 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002677 int ret;
2678 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002679 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002680 int fuzzy = cmdline_fuzzy_complete(pat)
2681 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002682
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002683 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002684
2685 if (xp->xp_context == EXPAND_FILES
2686 || xp->xp_context == EXPAND_DIRECTORIES
2687 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002688 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2689 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002690
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002691 *matches = (char_u **)"";
2692 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002693 if (xp->xp_context == EXPAND_HELP)
2694 {
2695 // With an empty argument we would get all the help tags, which is
2696 // very slow. Get matches for "help" instead.
2697 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002698 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002699 {
2700#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002701 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002702#endif
2703 return OK;
2704 }
2705 return FAIL;
2706 }
2707
Bram Moolenaar66b51422019-08-18 21:44:12 +02002708 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002709 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002710 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002711 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002712 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002713 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002714#ifdef FEAT_DIFF
2715 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002716 return ExpandBufnames(pat, numMatches, matches,
2717 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002718#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002719 if (xp->xp_context == EXPAND_TAGS
2720 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002721 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2722 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002723 if (xp->xp_context == EXPAND_COLORS)
2724 {
2725 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002726 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002727 directories);
2728 }
2729 if (xp->xp_context == EXPAND_COMPILER)
2730 {
2731 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002732 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002733 }
2734 if (xp->xp_context == EXPAND_OWNSYNTAX)
2735 {
2736 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002737 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002738 }
2739 if (xp->xp_context == EXPAND_FILETYPE)
2740 {
2741 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002742 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002743 }
2744# if defined(FEAT_EVAL)
2745 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002746 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002747# endif
2748 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002749 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002750
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002751 // When expanding a function name starting with s:, match the <SNR>nr_
2752 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002753 if ((xp->xp_context == EXPAND_USER_FUNC
2754 || xp->xp_context == EXPAND_DISASSEMBLE)
2755 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002756 {
2757 int len = (int)STRLEN(pat) + 20;
2758
2759 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002760 if (tofree == NULL)
2761 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002762 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002763 pat = tofree;
2764 }
2765
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002766 if (!fuzzy)
2767 {
2768 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2769 if (regmatch.regprog == NULL)
2770 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002771
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002772 // set ignore-case according to p_ic, p_scs and pat
2773 regmatch.rm_ic = ignorecase(pat);
2774 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002775
2776 if (xp->xp_context == EXPAND_SETTINGS
2777 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01002778 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002779 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002780 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002781# if defined(FEAT_EVAL)
2782 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002783 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002784# endif
2785 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002786 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002787
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002788 if (!fuzzy)
2789 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002790 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002791
2792 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002793}
2794
Bram Moolenaar66b51422019-08-18 21:44:12 +02002795/*
2796 * Expand a list of names.
2797 *
2798 * Generic function for command line completion. It calls a function to
2799 * obtain strings, one by one. The strings are matched against a regexp
2800 * program. Matching strings are copied into an array, which is returned.
2801 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002802 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2803 * is used.
2804 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002805 * Returns OK when no problems encountered, FAIL for error (out of memory).
2806 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002807 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002808ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002809 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002810 expand_T *xp,
2811 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002812 char_u ***matches,
2813 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002814 char_u *((*func)(expand_T *, int)),
2815 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002816 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002817{
2818 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002819 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002820 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002821 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002822 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002823 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002824 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002825 int sort_matches = FALSE;
2826 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002827
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002828 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002829 *matches = NULL;
2830 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002831
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002832 if (!fuzzy)
2833 ga_init2(&ga, sizeof(char *), 30);
2834 else
2835 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2836
2837 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002838 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002839 str = (*func)(xp, i);
2840 if (str == NULL) // end of list
2841 break;
2842 if (*str == NUL) // skip empty strings
2843 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002844
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002845 if (xp->xp_pattern[0] != NUL)
2846 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002847 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002848 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002849 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002850 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002851 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002852 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002853 }
2854 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002855 else
2856 match = TRUE;
2857
2858 if (!match)
2859 continue;
2860
2861 if (escaped)
2862 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2863 else
2864 str = vim_strsave(str);
2865 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002866 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002867 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002868 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002869 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002870 return FAIL;
2871 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01002872 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002873 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002874 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002875
2876 if (ga_grow(&ga, 1) == FAIL)
2877 {
2878 vim_free(str);
2879 break;
2880 }
2881
2882 if (fuzzy)
2883 {
2884 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2885 fuzmatch->idx = ga.ga_len;
2886 fuzmatch->str = str;
2887 fuzmatch->score = score;
2888 }
2889 else
2890 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2891
2892# ifdef FEAT_MENU
2893 if (func == get_menu_names)
2894 {
2895 // test for separator added by get_menu_names()
2896 str += STRLEN(str) - 1;
2897 if (*str == '\001')
2898 *str = '.';
2899 }
2900# endif
2901
2902 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002903 }
2904
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002905 if (ga.ga_len == 0)
2906 return OK;
2907
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002908 // sort the matches when using regular expression matching and sorting
2909 // applies to the completion context. Menus and scriptnames should be kept
2910 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002911 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002912 && xp->xp_context != EXPAND_MENUS
2913 && xp->xp_context != EXPAND_SCRIPTNAMES)
2914 sort_matches = TRUE;
2915
2916 // <SNR> functions should be sorted to the end.
2917 if (xp->xp_context == EXPAND_EXPRESSION
2918 || xp->xp_context == EXPAND_FUNCTIONS
2919 || xp->xp_context == EXPAND_USER_FUNC
2920 || xp->xp_context == EXPAND_DISASSEMBLE)
2921 funcsort = TRUE;
2922
2923 // Sort the matches.
2924 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002925 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002926 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002927 // <SNR> functions should be sorted to the end.
2928 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2929 sort_func_compare);
2930 else
2931 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2932 }
2933
2934 if (!fuzzy)
2935 {
2936 *matches = ga.ga_data;
2937 *numMatches = ga.ga_len;
2938 }
2939 else
2940 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002941 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2942 funcsort) == FAIL)
2943 return FAIL;
2944 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002945 }
2946
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002947#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002948 // Reset the variables used for special highlight names expansion, so that
2949 // they don't show up when getting normal highlight names by ID.
2950 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002951#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002952
Bram Moolenaar66b51422019-08-18 21:44:12 +02002953 return OK;
2954}
2955
2956/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002957 * Expand shell command matches in one directory of $PATH.
2958 */
2959 static void
2960expand_shellcmd_onedir(
2961 char_u *buf,
2962 char_u *s,
2963 size_t l,
2964 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002965 char_u ***matches,
2966 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002967 int flags,
2968 hashtab_T *ht,
2969 garray_T *gap)
2970{
2971 int ret;
2972 int i;
2973 hash_T hash;
2974 hashitem_T *hi;
2975
2976 vim_strncpy(buf, s, l);
2977 add_pathsep(buf);
2978 l = STRLEN(buf);
2979 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2980
2981 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002982 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002983 if (ret == OK)
2984 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002985 if (ga_grow(gap, *numMatches) == FAIL)
2986 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002987 else
2988 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002989 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002990 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002991 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002992
2993 if (STRLEN(name) > l)
2994 {
2995 // Check if this name was already found.
2996 hash = hash_hash(name + l);
2997 hi = hash_lookup(ht, name + l, hash);
2998 if (HASHITEM_EMPTY(hi))
2999 {
3000 // Remove the path that was prepended.
3001 STRMOVE(name, name + l);
3002 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3003 hash_add_item(ht, hi, name, hash);
3004 name = NULL;
3005 }
3006 }
3007 vim_free(name);
3008 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003009 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003010 }
3011 }
3012}
3013
3014/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003015 * Complete a shell command.
3016 * Returns FAIL or OK;
3017 */
3018 static int
3019expand_shellcmd(
3020 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003021 char_u ***matches, // return: array with matches
3022 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003023 int flagsarg) // EW_ flags
3024{
3025 char_u *pat;
3026 int i;
3027 char_u *path = NULL;
3028 int mustfree = FALSE;
3029 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003030 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003031 size_t l;
3032 char_u *s, *e;
3033 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003034 int did_curdir = FALSE;
3035 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003036
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003037 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003038 if (buf == NULL)
3039 return FAIL;
3040
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003041 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003042 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003043 if (pat == NULL)
3044 {
3045 vim_free(buf);
3046 return FAIL;
3047 }
3048
Bram Moolenaar66b51422019-08-18 21:44:12 +02003049 for (i = 0; pat[i]; ++i)
3050 if (pat[i] == '\\' && pat[i + 1] == ' ')
3051 STRMOVE(pat + i, pat + i + 1);
3052
3053 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3054
3055 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3056 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3057 path = (char_u *)".";
3058 else
3059 {
3060 // For an absolute name we don't use $PATH.
3061 if (!mch_isFullName(pat))
3062 path = vim_getenv((char_u *)"PATH", &mustfree);
3063 if (path == NULL)
3064 path = (char_u *)"";
3065 }
3066
3067 // Go over all directories in $PATH. Expand matches in that directory and
3068 // collect them in "ga". When "." is not in $PATH also expand for the
3069 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003070 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003071 hash_init(&found_ht);
3072 for (s = path; ; s = e)
3073 {
3074# if defined(MSWIN)
3075 e = vim_strchr(s, ';');
3076# else
3077 e = vim_strchr(s, ':');
3078# endif
3079 if (e == NULL)
3080 e = s + STRLEN(s);
3081
3082 if (*s == NUL)
3083 {
3084 if (did_curdir)
3085 break;
3086 // Find directories in the current directory, path is empty.
3087 did_curdir = TRUE;
3088 flags |= EW_DIR;
3089 }
3090 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3091 {
3092 did_curdir = TRUE;
3093 flags |= EW_DIR;
3094 }
3095 else
3096 // Do not match directories inside a $PATH item.
3097 flags &= ~EW_DIR;
3098
3099 l = e - s;
3100 if (l > MAXPATHL - 5)
3101 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003103 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003104 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003105
Bram Moolenaar66b51422019-08-18 21:44:12 +02003106 if (*e != NUL)
3107 ++e;
3108 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003109 *matches = ga.ga_data;
3110 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111
3112 vim_free(buf);
3113 vim_free(pat);
3114 if (mustfree)
3115 vim_free(path);
3116 hash_clear(&found_ht);
3117 return OK;
3118}
3119
3120# if defined(FEAT_EVAL)
3121/*
3122 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003123 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003124 */
3125 static void *
3126call_user_expand_func(
3127 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003128 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003129{
3130 cmdline_info_T *ccline = get_cmdline_info();
3131 int keep = 0;
3132 typval_T args[4];
3133 sctx_T save_current_sctx = current_sctx;
3134 char_u *pat = NULL;
3135 void *ret;
3136
3137 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3138 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139
3140 if (ccline->cmdbuff != NULL)
3141 {
3142 keep = ccline->cmdbuff[ccline->cmdlen];
3143 ccline->cmdbuff[ccline->cmdlen] = 0;
3144 }
3145
3146 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3147
3148 args[0].v_type = VAR_STRING;
3149 args[0].vval.v_string = pat;
3150 args[1].v_type = VAR_STRING;
3151 args[1].vval.v_string = xp->xp_line;
3152 args[2].v_type = VAR_NUMBER;
3153 args[2].vval.v_number = xp->xp_col;
3154 args[3].v_type = VAR_UNKNOWN;
3155
3156 current_sctx = xp->xp_script_ctx;
3157
3158 ret = user_expand_func(xp->xp_arg, 3, args);
3159
3160 current_sctx = save_current_sctx;
3161 if (ccline->cmdbuff != NULL)
3162 ccline->cmdbuff[ccline->cmdlen] = keep;
3163
3164 vim_free(pat);
3165 return ret;
3166}
3167
3168/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003169 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3170 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003171 */
3172 static int
3173ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003174 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003175 expand_T *xp,
3176 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003177 char_u ***matches,
3178 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003179{
3180 char_u *retstr;
3181 char_u *s;
3182 char_u *e;
3183 int keep;
3184 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003185 int fuzzy;
3186 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003187 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003188
3189 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003190 *matches = NULL;
3191 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003192
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003193 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003194 if (retstr == NULL)
3195 return FAIL;
3196
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003197 if (!fuzzy)
3198 ga_init2(&ga, sizeof(char *), 3);
3199 else
3200 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3201
Bram Moolenaar66b51422019-08-18 21:44:12 +02003202 for (s = retstr; *s != NUL; s = e)
3203 {
3204 e = vim_strchr(s, '\n');
3205 if (e == NULL)
3206 e = s + STRLEN(s);
3207 keep = *e;
3208 *e = NUL;
3209
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003210 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003211 {
3212 if (!fuzzy)
3213 match = vim_regexec(regmatch, s, (colnr_T)0);
3214 else
3215 {
3216 score = fuzzy_match_str(s, pat);
3217 match = (score != 0);
3218 }
3219 }
3220 else
3221 match = TRUE; // match everything
3222
Bram Moolenaar66b51422019-08-18 21:44:12 +02003223 *e = keep;
3224
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003225 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003226 {
3227 if (ga_grow(&ga, 1) == FAIL)
3228 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003229 if (!fuzzy)
3230 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3231 else
3232 {
3233 fuzmatch_str_T *fuzmatch =
3234 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003235 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003236 fuzmatch->str = vim_strnsave(s, e - s);
3237 fuzmatch->score = score;
3238 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003239 ++ga.ga_len;
3240 }
3241
3242 if (*e != NUL)
3243 ++e;
3244 }
3245 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003246
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003247 if (ga.ga_len == 0)
3248 return OK;
3249
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003250 if (!fuzzy)
3251 {
3252 *matches = ga.ga_data;
3253 *numMatches = ga.ga_len;
3254 }
3255 else
3256 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003257 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3258 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003259 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003260 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003261 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003262 return OK;
3263}
3264
3265/*
3266 * Expand names with a list returned by a function defined by the user.
3267 */
3268 static int
3269ExpandUserList(
3270 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003271 char_u ***matches,
3272 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003273{
3274 list_T *retlist;
3275 listitem_T *li;
3276 garray_T ga;
3277
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003278 *matches = NULL;
3279 *numMatches = 0;
3280 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003281 if (retlist == NULL)
3282 return FAIL;
3283
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003284 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003285 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003286 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003287 {
3288 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3289 continue; // Skip non-string items and empty strings
3290
3291 if (ga_grow(&ga, 1) == FAIL)
3292 break;
3293
3294 ((char_u **)ga.ga_data)[ga.ga_len] =
3295 vim_strsave(li->li_tv.vval.v_string);
3296 ++ga.ga_len;
3297 }
3298 list_unref(retlist);
3299
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003300 *matches = ga.ga_data;
3301 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003302 return OK;
3303}
3304# endif
3305
3306/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307 * Expand "file" for all comma-separated directories in "path".
3308 * Adds the matches to "ga". Caller must init "ga".
3309 */
3310 void
3311globpath(
3312 char_u *path,
3313 char_u *file,
3314 garray_T *ga,
3315 int expand_options)
3316{
3317 expand_T xpc;
3318 char_u *buf;
3319 int i;
3320 int num_p;
3321 char_u **p;
3322
3323 buf = alloc(MAXPATHL);
3324 if (buf == NULL)
3325 return;
3326
3327 ExpandInit(&xpc);
3328 xpc.xp_context = EXPAND_FILES;
3329
3330 // Loop over all entries in {path}.
3331 while (*path != NUL)
3332 {
3333 // Copy one item of the path to buf[] and concatenate the file name.
3334 copy_option_part(&path, buf, MAXPATHL, ",");
3335 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3336 {
3337# if defined(MSWIN)
3338 // Using the platform's path separator (\) makes vim incorrectly
3339 // treat it as an escape character, use '/' instead.
3340 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3341 STRCAT(buf, "/");
3342# else
3343 add_pathsep(buf);
3344# endif
3345 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003346 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003347 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3348 {
3349 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3350
3351 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003352 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353 for (i = 0; i < num_p; ++i)
3354 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003355 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003356 ++ga->ga_len;
3357 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003358 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003359 }
3360 }
3361 }
3362
3363 vim_free(buf);
3364}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003365
Bram Moolenaareadee482020-09-04 15:37:31 +02003366/*
3367 * Translate some keys pressed when 'wildmenu' is used.
3368 */
3369 int
3370wildmenu_translate_key(
3371 cmdline_info_T *cclp,
3372 int key,
3373 expand_T *xp,
3374 int did_wild_list)
3375{
3376 int c = key;
3377
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003378 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003379 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003380 // When the popup menu is used for cmdline completion:
3381 // Up : go to the previous item in the menu
3382 // Down : go to the next item in the menu
3383 // Left : go to the parent directory
3384 // Right: list the files in the selected directory
3385 switch (c)
3386 {
3387 case K_UP: c = K_LEFT; break;
3388 case K_DOWN: c = K_RIGHT; break;
3389 case K_LEFT: c = K_UP; break;
3390 case K_RIGHT: c = K_DOWN; break;
3391 default: break;
3392 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003393 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003394
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003395 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003396 {
3397 if (c == K_LEFT)
3398 c = Ctrl_P;
3399 else if (c == K_RIGHT)
3400 c = Ctrl_N;
3401 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003402
Bram Moolenaareadee482020-09-04 15:37:31 +02003403 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003404 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003405 && cclp->cmdpos > 1
3406 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3407 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3408 && (c == '\n' || c == '\r' || c == K_KENTER))
3409 c = K_DOWN;
3410
3411 return c;
3412}
3413
3414/*
3415 * Delete characters on the command line, from "from" to the current
3416 * position.
3417 */
3418 static void
3419cmdline_del(cmdline_info_T *cclp, int from)
3420{
3421 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3422 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3423 cclp->cmdlen -= cclp->cmdpos - from;
3424 cclp->cmdpos = from;
3425}
3426
3427/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003428 * Handle a key pressed when the wild menu for the menu names
3429 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003430 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003431 static int
3432wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003433{
Bram Moolenaareadee482020-09-04 15:37:31 +02003434 int i;
3435 int j;
3436
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003437 // Hitting <Down> after "emenu Name.": complete submenu
3438 if (key == K_DOWN && cclp->cmdpos > 0
3439 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003440 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003441 key = p_wc;
3442 KeyTyped = TRUE; // in case the key was mapped
3443 }
3444 else if (key == K_UP)
3445 {
3446 // Hitting <Up>: Remove one submenu name in front of the
3447 // cursor
3448 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003449
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003450 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3451 i = 0;
3452 while (--j > 0)
3453 {
3454 // check for start of menu name
3455 if (cclp->cmdbuff[j] == ' '
3456 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003457 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003458 i = j + 1;
3459 break;
3460 }
3461 // check for start of submenu name
3462 if (cclp->cmdbuff[j] == '.'
3463 && cclp->cmdbuff[j - 1] != '\\')
3464 {
3465 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003466 {
3467 i = j + 1;
3468 break;
3469 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003470 else
3471 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003472 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003473 }
3474 if (i > 0)
3475 cmdline_del(cclp, i);
3476 key = p_wc;
3477 KeyTyped = TRUE; // in case the key was mapped
3478 xp->xp_context = EXPAND_NOTHING;
3479 }
3480
3481 return key;
3482}
3483
3484/*
3485 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3486 * directory names (EXPAND_DIRECTORIES) or shell command names
3487 * (EXPAND_SHELLCMD) is displayed.
3488 */
3489 static int
3490wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3491{
3492 int i;
3493 int j;
3494 char_u upseg[5];
3495
3496 upseg[0] = PATHSEP;
3497 upseg[1] = '.';
3498 upseg[2] = '.';
3499 upseg[3] = PATHSEP;
3500 upseg[4] = NUL;
3501
3502 if (key == K_DOWN
3503 && cclp->cmdpos > 0
3504 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3505 && (cclp->cmdpos < 3
3506 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3507 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3508 {
3509 // go down a directory
3510 key = p_wc;
3511 KeyTyped = TRUE; // in case the key was mapped
3512 }
3513 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3514 {
3515 // If in a direct ancestor, strip off one ../ to go down
3516 int found = FALSE;
3517
3518 j = cclp->cmdpos;
3519 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3520 while (--j > i)
3521 {
3522 if (has_mbyte)
3523 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3524 if (vim_ispathsep(cclp->cmdbuff[j]))
3525 {
3526 found = TRUE;
3527 break;
3528 }
3529 }
3530 if (found
3531 && cclp->cmdbuff[j - 1] == '.'
3532 && cclp->cmdbuff[j - 2] == '.'
3533 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3534 {
3535 cmdline_del(cclp, j - 2);
3536 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003537 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003538 }
3539 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003540 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003541 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003542 // go up a directory
3543 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003544
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003545 j = cclp->cmdpos - 1;
3546 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3547 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003548 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003549 if (has_mbyte)
3550 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3551 if (vim_ispathsep(cclp->cmdbuff[j])
3552# ifdef BACKSLASH_IN_FILENAME
3553 && vim_strchr((char_u *)" *?[{`$%#",
3554 cclp->cmdbuff[j + 1]) == NULL
3555# endif
3556 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003557 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003558 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003559 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003560 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003561 break;
3562 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003563 else
3564 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003565 }
3566 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003567
3568 if (!found)
3569 j = i;
3570 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3571 j += 4;
3572 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3573 && j == i)
3574 j += 3;
3575 else
3576 j = 0;
3577 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003578 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003579 // TODO this is only for DOS/UNIX systems - need to put in
3580 // machine-specific stuff here and in upseg init
3581 cmdline_del(cclp, j);
3582 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003583 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003584 else if (cclp->cmdpos > i)
3585 cmdline_del(cclp, i);
3586
3587 // Now complete in the new directory. Set KeyTyped in case the
3588 // Up key came from a mapping.
3589 key = p_wc;
3590 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003591 }
3592
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003593 return key;
3594}
3595
3596/*
3597 * Handle a key pressed when the wild menu is displayed
3598 */
3599 int
3600wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3601{
3602 if (xp->xp_context == EXPAND_MENUNAMES)
3603 return wildmenu_process_key_menunames(cclp, key, xp);
3604 else if ((xp->xp_context == EXPAND_FILES
3605 || xp->xp_context == EXPAND_DIRECTORIES
3606 || xp->xp_context == EXPAND_SHELLCMD))
3607 return wildmenu_process_key_filenames(cclp, key, xp);
3608
3609 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003610}
3611
3612/*
3613 * Free expanded names when finished walking through the matches
3614 */
3615 void
3616wildmenu_cleanup(cmdline_info_T *cclp)
3617{
3618 int skt = KeyTyped;
3619 int old_RedrawingDisabled = RedrawingDisabled;
3620
3621 if (!p_wmnu || wild_menu_showing == 0)
3622 return;
3623
3624 if (cclp->input_fn)
3625 RedrawingDisabled = 0;
3626
3627 if (wild_menu_showing == WM_SCROLLED)
3628 {
3629 // Entered command line, move it up
3630 cmdline_row--;
3631 redrawcmd();
3632 }
3633 else if (save_p_ls != -1)
3634 {
3635 // restore 'laststatus' and 'winminheight'
3636 p_ls = save_p_ls;
3637 p_wmh = save_p_wmh;
3638 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003639 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003640 redrawcmd();
3641 save_p_ls = -1;
3642 }
3643 else
3644 {
3645 win_redraw_last_status(topframe);
3646 redraw_statuslines();
3647 }
3648 KeyTyped = skt;
3649 wild_menu_showing = 0;
3650 if (cclp->input_fn)
3651 RedrawingDisabled = old_RedrawingDisabled;
3652}
Bram Moolenaareadee482020-09-04 15:37:31 +02003653
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003654#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003655/*
3656 * "getcompletion()" function
3657 */
3658 void
3659f_getcompletion(typval_T *argvars, typval_T *rettv)
3660{
3661 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003662 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003663 expand_T xpc;
3664 int filtered = FALSE;
3665 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003666 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003667
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003668 if (in_vim9script()
3669 && (check_for_string_arg(argvars, 0) == FAIL
3670 || check_for_string_arg(argvars, 1) == FAIL
3671 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3672 return;
3673
ii144785fe02021-11-21 12:13:56 +00003674 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003675 if (argvars[1].v_type != VAR_STRING)
3676 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003677 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003678 return;
3679 }
3680 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003681
3682 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003683 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003684
3685 if (p_wic)
3686 options |= WILD_ICASE;
3687
3688 // For filtered results, 'wildignore' is used
3689 if (!filtered)
3690 options |= WILD_KEEP_ALL;
3691
3692 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003693 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003694 {
ii144785fe02021-11-21 12:13:56 +00003695 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003696 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003697 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003698 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003699 else
3700 {
ii144785fe02021-11-21 12:13:56 +00003701 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003702 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3703
3704 xpc.xp_context = cmdcomplete_str_to_type(type);
3705 if (xpc.xp_context == EXPAND_NOTHING)
3706 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003707 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003708 return;
3709 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003710
3711# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003712 if (xpc.xp_context == EXPAND_MENUS)
3713 {
3714 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3715 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3716 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003717# endif
3718# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003719 if (xpc.xp_context == EXPAND_CSCOPE)
3720 {
3721 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3722 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3723 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003724# endif
3725# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003726 if (xpc.xp_context == EXPAND_SIGN)
3727 {
3728 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3729 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3730 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003731# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003732 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003733
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00003734 if (cmdline_fuzzy_completion_supported(&xpc))
3735 // when fuzzy matching, don't modify the search string
3736 pat = vim_strsave(xpc.xp_pattern);
3737 else
3738 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3739
Bram Moolenaar93a10962022-06-16 11:42:09 +01003740 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003741 {
3742 int i;
3743
3744 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3745
3746 for (i = 0; i < xpc.xp_numfiles; i++)
3747 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3748 }
3749 vim_free(pat);
3750 ExpandCleanup(&xpc);
3751}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003752#endif // FEAT_EVAL