blob: 20f3069ce21477326000b2af929737c26e0b52f9 [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 Lakshmanan24384302022-02-17 11:26:42 +000018static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000019static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020020static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020022#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000023static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020025#endif
26
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000027// "compl_match_array" points the currently displayed list of entries in the
28// popup menu. It is NULL when there is no popup menu.
29static pumitem_T *compl_match_array = NULL;
30static int compl_match_arraysize;
31// First column in cmdline of the matched item for completion.
32static int compl_startcol;
33static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000034
zeertzjqc51a3762022-12-10 10:22:29 +000035#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000038 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
39 * context.
40 */
41 static int
42cmdline_fuzzy_completion_supported(expand_T *xp)
43{
44 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
45 && xp->xp_context != EXPAND_BOOL_SETTINGS
46 && xp->xp_context != EXPAND_COLORS
47 && xp->xp_context != EXPAND_COMPILER
48 && xp->xp_context != EXPAND_DIRECTORIES
49 && xp->xp_context != EXPAND_FILES
50 && xp->xp_context != EXPAND_FILES_IN_PATH
51 && xp->xp_context != EXPAND_FILETYPE
52 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000053 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020054 && xp->xp_context != EXPAND_STRING_SETTING
55 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OWNSYNTAX
57 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000058 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_SHELLCMD
60 && xp->xp_context != EXPAND_TAGS
61 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_USER_LIST);
63}
64
65/*
66 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000067 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
68 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000069 */
70 int
71cmdline_fuzzy_complete(char_u *fuzzystr)
72{
73 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
74}
75
76/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000077 * sort function for the completion matches.
78 * <SNR> functions should be sorted to the end.
79 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020080 static int
81sort_func_compare(const void *s1, const void *s2)
82{
83 char_u *p1 = *(char_u **)s1;
84 char_u *p2 = *(char_u **)s2;
85
86 if (*p1 != '<' && *p2 == '<') return -1;
87 if (*p1 == '<' && *p2 != '<') return 1;
88 return STRCMP(p1, p2);
89}
Bram Moolenaar66b51422019-08-18 21:44:12 +020090
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000091/*
92 * Escape special characters in the cmdline completion matches.
93 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020094 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000095wildescape(
96 expand_T *xp,
97 char_u *str,
98 int numfiles,
99 char_u **files)
100{
101 char_u *p;
102 int vse_what = xp->xp_context == EXPAND_BUFFERS
103 ? VSE_BUFFER : VSE_NONE;
104
105 if (xp->xp_context == EXPAND_FILES
106 || xp->xp_context == EXPAND_FILES_IN_PATH
107 || xp->xp_context == EXPAND_SHELLCMD
108 || xp->xp_context == EXPAND_BUFFERS
109 || xp->xp_context == EXPAND_DIRECTORIES)
110 {
111 // Insert a backslash into a file name before a space, \, %, #
112 // and wildmatch characters, except '~'.
113 for (int i = 0; i < numfiles; ++i)
114 {
115 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200116 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000117 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200118 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
119 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000120 if (p != NULL)
121 {
122 vim_free(files[i]);
123 files[i] = p;
124#if defined(BACKSLASH_IN_FILENAME)
125 p = vim_strsave_escaped(files[i], (char_u *)" ");
126 if (p != NULL)
127 {
128 vim_free(files[i]);
129 files[i] = p;
130 }
131#endif
132 }
133 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200134 else if (xp->xp_backslash & XP_BS_COMMA)
135 {
136 if (vim_strchr(files[i], ',') != NULL)
137 {
138 p = vim_strsave_escaped(files[i], (char_u *)",");
139 if (p != NULL)
140 {
141 vim_free(files[i]);
142 files[i] = p;
143 }
144 }
145 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000146#ifdef BACKSLASH_IN_FILENAME
147 p = vim_strsave_fnameescape(files[i], vse_what);
148#else
149 p = vim_strsave_fnameescape(files[i],
150 xp->xp_shell ? VSE_SHELL : vse_what);
151#endif
152 if (p != NULL)
153 {
154 vim_free(files[i]);
155 files[i] = p;
156 }
157
158 // If 'str' starts with "\~", replace "~" at start of
159 // files[i] with "\~".
160 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
161 escape_fname(&files[i]);
162 }
163 xp->xp_backslash = XP_BS_NONE;
164
165 // If the first file starts with a '+' escape it. Otherwise it
166 // could be seen as "+cmd".
167 if (*files[0] == '+')
168 escape_fname(&files[0]);
169 }
170 else if (xp->xp_context == EXPAND_TAGS)
171 {
172 // Insert a backslash before characters in a tag name that
173 // would terminate the ":tag" command.
174 for (int i = 0; i < numfiles; ++i)
175 {
176 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
177 if (p != NULL)
178 {
179 vim_free(files[i]);
180 files[i] = p;
181 }
182 }
183 }
184}
185
186/*
187 * Escape special characters in the cmdline completion matches.
188 */
189 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200190ExpandEscape(
191 expand_T *xp,
192 char_u *str,
193 int numfiles,
194 char_u **files,
195 int options)
196{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200197 // May change home directory back to "~"
198 if (options & WILD_HOME_REPLACE)
199 tilde_replace(str, numfiles, files);
200
201 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000202 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200203}
204
205/*
206 * Return FAIL if this is not an appropriate context in which to do
207 * completion of anything, return OK if it is (even if there are no matches).
208 * For the caller, this means that the character is just passed through like a
209 * normal character (instead of being expanded). This allows :s/^I^D etc.
210 */
211 int
212nextwild(
213 expand_T *xp,
214 int type,
215 int options, // extra options for ExpandOne()
216 int escape) // if TRUE, escape the returned matches
217{
218 cmdline_info_T *ccline = get_cmdline_info();
219 int i, j;
220 char_u *p1;
221 char_u *p2;
222 int difflen;
223 int v;
224
225 if (xp->xp_numfiles == -1)
226 {
227 set_expand_context(xp);
228 cmd_showtail = expand_showtail(xp);
229 }
230
231 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
232 {
233 beep_flush();
234 return OK; // Something illegal on command line
235 }
236 if (xp->xp_context == EXPAND_NOTHING)
237 {
238 // Caller can use the character as a normal char instead
239 return FAIL;
240 }
241
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000242 // If cmd_silent is set then don't show the dots, because redrawcmd() below
243 // won't remove them.
244 if (!cmd_silent)
245 {
246 msg_puts("..."); // show that we are busy
247 out_flush();
248 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200249
250 i = (int)(xp->xp_pattern - ccline->cmdbuff);
251 xp->xp_pattern_len = ccline->cmdpos - i;
252
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000253 if (type == WILD_NEXT || type == WILD_PREV
254 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200255 {
256 // Get next/previous match for a previous expanded pattern.
257 p2 = ExpandOne(xp, NULL, NULL, 0, type);
258 }
259 else
260 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000261 if (cmdline_fuzzy_completion_supported(xp))
262 // If fuzzy matching, don't modify the search string
263 p1 = vim_strsave(xp->xp_pattern);
264 else
265 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
266
Bram Moolenaar66b51422019-08-18 21:44:12 +0200267 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000268 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200269 p2 = NULL;
270 else
271 {
272 int use_options = options |
273 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
274 if (escape)
275 use_options |= WILD_ESCAPE;
276
277 if (p_wic)
278 use_options += WILD_ICASE;
279 p2 = ExpandOne(xp, p1,
280 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
281 use_options, type);
282 vim_free(p1);
283 // longest match: make sure it is not shorter, happens with :help
284 if (p2 != NULL && type == WILD_LONGEST)
285 {
286 for (j = 0; j < xp->xp_pattern_len; ++j)
287 if (ccline->cmdbuff[i + j] == '*'
288 || ccline->cmdbuff[i + j] == '?')
289 break;
290 if ((int)STRLEN(p2) < j)
291 VIM_CLEAR(p2);
292 }
293 }
294 }
295
296 if (p2 != NULL && !got_int)
297 {
298 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
299 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
300 {
301 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
302 xp->xp_pattern = ccline->cmdbuff + i;
303 }
304 else
305 v = OK;
306 if (v == OK)
307 {
308 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
309 &ccline->cmdbuff[ccline->cmdpos],
310 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
311 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
312 ccline->cmdlen += difflen;
313 ccline->cmdpos += difflen;
314 }
315 }
316 vim_free(p2);
317
318 redrawcmd();
319 cursorcmd();
320
321 // When expanding a ":map" command and no matches are found, assume that
322 // the key is supposed to be inserted literally
323 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
324 return FAIL;
325
326 if (xp->xp_numfiles <= 0 && p2 == NULL)
327 beep_flush();
328 else if (xp->xp_numfiles == 1)
329 // free expanded pattern
330 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
331
332 return OK;
333}
334
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000335/*
336 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000337 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000338 */
339 static int
340cmdline_pum_create(
341 cmdline_info_T *ccline,
342 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000343 char_u **matches,
344 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000345 int showtail)
346{
347 int i;
348 int columns;
349
350 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000351 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000352 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000353 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000354 {
zeertzjqc51a3762022-12-10 10:22:29 +0000355 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 compl_match_array[i].pum_info = NULL;
357 compl_match_array[i].pum_extra = NULL;
358 compl_match_array[i].pum_kind = NULL;
359 }
360
361 // Compute the popup menu starting column
362 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
363 columns = vim_strsize(xp->xp_pattern);
364 if (showtail)
365 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000366 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000367 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000368 }
369 if (columns >= compl_startcol)
370 compl_startcol = 0;
371 else
372 compl_startcol -= columns;
373
374 // no default selection
375 compl_selected = -1;
376
377 cmdline_pum_display();
378
379 return EXPAND_OK;
380}
381
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000382/*
383 * Display the cmdline completion matches in a popup menu
384 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000385 void
386cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000387{
388 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
389}
390
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000391/*
392 * Returns TRUE if the cmdline completion popup menu is being displayed.
393 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000394 int
395cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000396{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100397 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000398}
399
400/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000401 * Remove the cmdline completion popup menu (if present), free the list of
402 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000403 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000404 void
405cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000406{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000407 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100408 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000409
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000410 pum_undisplay();
411 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000412 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000413 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000414 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000415 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100416
417 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
418 // as a side effect.
419 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000420}
421
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000422 void
423cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000424{
425 cmdline_pum_remove();
426 wildmenu_cleanup(cclp);
427}
428
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000429/*
430 * Returns the starting column number to use for the cmdline completion popup
431 * menu.
432 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000433 int
434cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000435{
436 return compl_startcol;
437}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000438
Bram Moolenaar66b51422019-08-18 21:44:12 +0200439/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000440 * Return the number of characters that should be skipped in a status match.
441 * These are backslashes used for escaping. Do show backslashes in help tags.
442 */
443 static int
444skip_status_match_char(expand_T *xp, char_u *s)
445{
446 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
447#ifdef FEAT_MENU
448 || ((xp->xp_context == EXPAND_MENUS
449 || xp->xp_context == EXPAND_MENUNAMES)
450 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
451#endif
452 )
453 {
454#ifndef BACKSLASH_IN_FILENAME
455 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
456 return 2;
457#endif
458 return 1;
459 }
460 return 0;
461}
462
463/*
464 * Get the length of an item as it will be shown in the status line.
465 */
466 static int
467status_match_len(expand_T *xp, char_u *s)
468{
469 int len = 0;
470
471#ifdef FEAT_MENU
472 int emenu = xp->xp_context == EXPAND_MENUS
473 || xp->xp_context == EXPAND_MENUNAMES;
474
475 // Check for menu separators - replace with '|'.
476 if (emenu && menu_is_separator(s))
477 return 1;
478#endif
479
480 while (*s != NUL)
481 {
482 s += skip_status_match_char(xp, s);
483 len += ptr2cells(s);
484 MB_PTR_ADV(s);
485 }
486
487 return len;
488}
489
490/*
491 * Show wildchar matches in the status line.
492 * Show at least the "match" item.
493 * We start at item 'first_match' in the list and show all matches that fit.
494 *
495 * If inversion is possible we use it. Else '=' characters are used.
496 */
497 static void
498win_redr_status_matches(
499 expand_T *xp,
500 int num_matches,
501 char_u **matches, // list of matches
502 int match,
503 int showtail)
504{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000505 int row;
506 char_u *buf;
507 int len;
508 int clen; // length in screen cells
509 int fillchar;
510 int attr;
511 int i;
512 int highlight = TRUE;
513 char_u *selstart = NULL;
514 int selstart_col = 0;
515 char_u *selend = NULL;
516 static int first_match = 0;
517 int add_left = FALSE;
518 char_u *s;
519#ifdef FEAT_MENU
520 int emenu;
521#endif
522 int l;
523
524 if (matches == NULL) // interrupted completion?
525 return;
526
527 if (has_mbyte)
528 buf = alloc(Columns * MB_MAXBYTES + 1);
529 else
530 buf = alloc(Columns + 1);
531 if (buf == NULL)
532 return;
533
534 if (match == -1) // don't show match but original text
535 {
536 match = 0;
537 highlight = FALSE;
538 }
539 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000540 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000541 if (match == 0)
542 first_match = 0;
543 else if (match < first_match)
544 {
545 // jumping left, as far as we can go
546 first_match = match;
547 add_left = TRUE;
548 }
549 else
550 {
551 // check if match fits on the screen
552 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000553 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000554 if (first_match > 0)
555 clen += 2;
556 // jumping right, put match at the left
557 if ((long)clen > Columns)
558 {
559 first_match = match;
560 // if showing the last match, we can add some on the left
561 clen = 2;
562 for (i = match; i < num_matches; ++i)
563 {
zeertzjqc51a3762022-12-10 10:22:29 +0000564 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000565 if ((long)clen >= Columns)
566 break;
567 }
568 if (i == num_matches)
569 add_left = TRUE;
570 }
571 }
572 if (add_left)
573 while (first_match > 0)
574 {
zeertzjqc51a3762022-12-10 10:22:29 +0000575 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000576 if ((long)clen >= Columns)
577 break;
578 --first_match;
579 }
580
581 fillchar = fillchar_status(&attr, curwin);
582
583 if (first_match == 0)
584 {
585 *buf = NUL;
586 len = 0;
587 }
588 else
589 {
590 STRCPY(buf, "< ");
591 len = 2;
592 }
593 clen = len;
594
595 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000596 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000597 {
598 if (i == match)
599 {
600 selstart = buf + len;
601 selstart_col = clen;
602 }
603
zeertzjqc51a3762022-12-10 10:22:29 +0000604 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000605 // Check for menu separators - replace with '|'
606#ifdef FEAT_MENU
607 emenu = (xp->xp_context == EXPAND_MENUS
608 || xp->xp_context == EXPAND_MENUNAMES);
609 if (emenu && menu_is_separator(s))
610 {
611 STRCPY(buf + len, transchar('|'));
612 l = (int)STRLEN(buf + len);
613 len += l;
614 clen += l;
615 }
616 else
617#endif
618 for ( ; *s != NUL; ++s)
619 {
620 s += skip_status_match_char(xp, s);
621 clen += ptr2cells(s);
622 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
623 {
624 STRNCPY(buf + len, s, l);
625 s += l - 1;
626 len += l;
627 }
628 else
629 {
630 STRCPY(buf + len, transchar_byte(*s));
631 len += (int)STRLEN(buf + len);
632 }
633 }
634 if (i == match)
635 selend = buf + len;
636
637 *(buf + len++) = ' ';
638 *(buf + len++) = ' ';
639 clen += 2;
640 if (++i == num_matches)
641 break;
642 }
643
644 if (i != num_matches)
645 {
646 *(buf + len++) = '>';
647 ++clen;
648 }
649
650 buf[len] = NUL;
651
652 row = cmdline_row - 1;
653 if (row >= 0)
654 {
655 if (wild_menu_showing == 0)
656 {
657 if (msg_scrolled > 0)
658 {
659 // Put the wildmenu just above the command line. If there is
660 // no room, scroll the screen one line up.
661 if (cmdline_row == Rows - 1)
662 {
663 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
664 ++msg_scrolled;
665 }
666 else
667 {
668 ++cmdline_row;
669 ++row;
670 }
671 wild_menu_showing = WM_SCROLLED;
672 }
673 else
674 {
675 // Create status line if needed by setting 'laststatus' to 2.
676 // Set 'winminheight' to zero to avoid that the window is
677 // resized.
678 if (lastwin->w_status_height == 0)
679 {
680 save_p_ls = p_ls;
681 save_p_wmh = p_wmh;
682 p_ls = 2;
683 p_wmh = 0;
684 last_status(FALSE);
685 }
686 wild_menu_showing = WM_SHOWN;
687 }
688 }
689
690 screen_puts(buf, row, 0, attr);
691 if (selstart != NULL && highlight)
692 {
693 *selend = NUL;
694 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
695 }
696
697 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
698 }
699
700 win_redraw_last_status(topframe);
701 vim_free(buf);
702}
703
704/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000705 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200706 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000707 */
708 static char_u *
709get_next_or_prev_match(
710 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200711 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000712{
zeertzjqe9ef3472023-08-17 23:57:05 +0200713 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000714 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000715
716 if (xp->xp_numfiles <= 0)
717 return NULL;
718
719 if (mode == WILD_PREV)
720 {
721 if (findex == -1)
722 findex = xp->xp_numfiles;
723 --findex;
724 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000725 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000726 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000727 else if (mode == WILD_PAGEUP)
728 {
729 if (findex == 0)
730 // at the first entry, don't select any entries
731 findex = -1;
732 else if (findex == -1)
733 // no entry is selected. select the last entry
734 findex = xp->xp_numfiles - 1;
735 else
736 {
737 // go up by the pum height
738 ht = pum_get_height();
739 if (ht > 3)
740 ht -= 2;
741 findex -= ht;
742 if (findex < 0)
743 // few entries left, select the first entry
744 findex = 0;
745 }
746 }
747 else // mode == WILD_PAGEDOWN
748 {
749 if (findex == xp->xp_numfiles - 1)
750 // at the last entry, don't select any entries
751 findex = -1;
752 else if (findex == -1)
753 // no entry is selected. select the first entry
754 findex = 0;
755 else
756 {
757 // go down by the pum height
758 ht = pum_get_height();
759 if (ht > 3)
760 ht -= 2;
761 findex += ht;
762 if (findex >= xp->xp_numfiles)
763 // few entries left, select the last entry
764 findex = xp->xp_numfiles - 1;
765 }
766 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000767
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000768 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000769 if (findex < 0)
770 {
zeertzjq28a23602023-09-29 19:58:35 +0200771 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000772 findex = xp->xp_numfiles - 1;
773 else
774 findex = -1;
775 }
776 if (findex >= xp->xp_numfiles)
777 {
zeertzjq28a23602023-09-29 19:58:35 +0200778 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000779 findex = 0;
780 else
781 findex = -1;
782 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000783 if (compl_match_array)
784 {
785 compl_selected = findex;
786 cmdline_pum_display();
787 }
788 else if (p_wmnu)
789 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
790 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200791 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000792
793 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200794 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000795
796 return vim_strsave(xp->xp_files[findex]);
797}
798
799/*
800 * Start the command-line expansion and get the matches.
801 */
802 static char_u *
803ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
804{
805 int non_suf_match; // number without matching suffix
806 int i;
807 char_u *ss = NULL;
808
809 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000810 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000811 options) == FAIL)
812 {
813#ifdef FNAME_ILLEGAL
814 // Illegal file name has been silently skipped. But when there
815 // are wildcards, the real problem is that there was no match,
816 // causing the pattern to be added, which has illegal characters.
817 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
818 semsg(_(e_no_match_str_2), str);
819#endif
820 }
821 else if (xp->xp_numfiles == 0)
822 {
823 if (!(options & WILD_SILENT))
824 semsg(_(e_no_match_str_2), str);
825 }
826 else
827 {
828 // Escape the matches for use on the command line.
829 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
830
831 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000832 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000833 {
834 if (xp->xp_numfiles)
835 non_suf_match = xp->xp_numfiles;
836 else
837 non_suf_match = 1;
838 if ((xp->xp_context == EXPAND_FILES
839 || xp->xp_context == EXPAND_DIRECTORIES)
840 && xp->xp_numfiles > 1)
841 {
842 // More than one match; check suffix.
843 // The files will have been sorted on matching suffix in
844 // expand_wildcards, only need to check the first two.
845 non_suf_match = 0;
846 for (i = 0; i < 2; ++i)
847 if (match_suffix(xp->xp_files[i]))
848 ++non_suf_match;
849 }
850 if (non_suf_match != 1)
851 {
852 // Can we ever get here unless it's while expanding
853 // interactively? If not, we can get rid of this all
854 // together. Don't really want to wait for this message
855 // (and possibly have to hit return to continue!).
856 if (!(options & WILD_SILENT))
857 emsg(_(e_too_many_file_names));
858 else if (!(options & WILD_NO_BEEP))
859 beep_flush();
860 }
861 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
862 ss = vim_strsave(xp->xp_files[0]);
863 }
864 }
865
866 return ss;
867}
868
869/*
870 * Return the longest common part in the list of cmdline completion matches.
871 */
872 static char_u *
873find_longest_match(expand_T *xp, int options)
874{
875 long_u len;
876 int mb_len = 1;
877 int c0, ci;
878 int i;
879 char_u *ss;
880
881 for (len = 0; xp->xp_files[0][len]; len += mb_len)
882 {
883 if (has_mbyte)
884 {
885 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
886 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
887 }
888 else
889 c0 = xp->xp_files[0][len];
890 for (i = 1; i < xp->xp_numfiles; ++i)
891 {
892 if (has_mbyte)
893 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
894 else
895 ci = xp->xp_files[i][len];
896 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
897 || xp->xp_context == EXPAND_FILES
898 || xp->xp_context == EXPAND_SHELLCMD
899 || xp->xp_context == EXPAND_BUFFERS))
900 {
901 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
902 break;
903 }
904 else if (c0 != ci)
905 break;
906 }
907 if (i < xp->xp_numfiles)
908 {
909 if (!(options & WILD_NO_BEEP))
910 vim_beep(BO_WILD);
911 break;
912 }
913 }
914
915 ss = alloc(len + 1);
916 if (ss)
917 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
918
919 return ss;
920}
921
922/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000923 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200924 * Chars that should not be expanded must be preceded with a backslash.
925 * Return a pointer to allocated memory containing the new string.
926 * Return NULL for failure.
927 *
928 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200929 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
930 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200931 *
932 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
933 * is WILD_EXPAND_FREE or WILD_ALL.
934 *
935 * mode = WILD_FREE: just free previously expanded matches
936 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
937 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
938 * mode = WILD_NEXT: use next match in multiple match, wrap to first
939 * mode = WILD_PREV: use previous match in multiple match, wrap to first
940 * mode = WILD_ALL: return all matches concatenated
941 * mode = WILD_LONGEST: return longest matched part
942 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000943 * mode = WILD_APPLY: apply the item selected in the cmdline completion
944 * popup menu and close the menu.
945 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
946 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200947 *
948 * options = WILD_LIST_NOTFOUND: list entries without a match
949 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
950 * options = WILD_USE_NL: Use '\n' for WILD_ALL
951 * options = WILD_NO_BEEP: Don't beep for multiple matches
952 * options = WILD_ADD_SLASH: add a slash after directory names
953 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
954 * options = WILD_SILENT: don't print warning messages
955 * options = WILD_ESCAPE: put backslash before special chars
956 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200957 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200958 *
959 * The variables xp->xp_context and xp->xp_backslash must have been set!
960 */
961 char_u *
962ExpandOne(
963 expand_T *xp,
964 char_u *str,
965 char_u *orig, // allocated copy of original of expanded string
966 int options,
967 int mode)
968{
969 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200970 int orig_saved = FALSE;
971 int i;
972 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200973
974 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000975 if (mode == WILD_NEXT || mode == WILD_PREV
976 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +0200977 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200978
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000979 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +0200980 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000981 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +0200982 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +0200983 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +0200984 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000985
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986 // free old names
987 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
988 {
989 FreeWild(xp->xp_numfiles, xp->xp_files);
990 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +0200991 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000992
993 // The entries from xp_files may be used in the PUM, remove it.
994 if (compl_match_array != NULL)
995 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200996 }
zeertzjqe9ef3472023-08-17 23:57:05 +0200997 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200998
999 if (mode == WILD_FREE) // only release file name
1000 return NULL;
1001
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001002 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001003 {
zeertzjq28a23602023-09-29 19:58:35 +02001004 vim_free(xp->xp_orig);
1005 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001006 orig_saved = TRUE;
1007
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001008 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001009 }
1010
1011 // Find longest common part
1012 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1013 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001014 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001015 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001016 }
1017
Bram Moolenaar57e95172022-08-20 19:26:14 +01001018 // Concatenate all matching names. Unless interrupted, this can be slow
1019 // and the result probably won't be used.
1020 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001021 {
1022 len = 0;
1023 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001024 {
1025 if (i > 0)
1026 {
1027 if (xp->xp_prefix == XP_PREFIX_NO)
1028 len += 2; // prefix "no"
1029 else if (xp->xp_prefix == XP_PREFIX_INV)
1030 len += 3; // prefix "inv"
1031 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001032 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001033 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001034 ss = alloc(len);
1035 if (ss != NULL)
1036 {
1037 *ss = NUL;
1038 for (i = 0; i < xp->xp_numfiles; ++i)
1039 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001040 if (i > 0)
1041 {
1042 if (xp->xp_prefix == XP_PREFIX_NO)
1043 STRCAT(ss, "no");
1044 else if (xp->xp_prefix == XP_PREFIX_INV)
1045 STRCAT(ss, "inv");
1046 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001047 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001048
Bram Moolenaar66b51422019-08-18 21:44:12 +02001049 if (i != xp->xp_numfiles - 1)
1050 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1051 }
1052 }
1053 }
1054
1055 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1056 ExpandCleanup(xp);
1057
zeertzjq28a23602023-09-29 19:58:35 +02001058 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001059 if (!orig_saved)
1060 vim_free(orig);
1061
1062 return ss;
1063}
1064
1065/*
1066 * Prepare an expand structure for use.
1067 */
1068 void
1069ExpandInit(expand_T *xp)
1070{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001071 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001072 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001073 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001074 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001075}
1076
1077/*
1078 * Cleanup an expand structure after use.
1079 */
1080 void
1081ExpandCleanup(expand_T *xp)
1082{
1083 if (xp->xp_numfiles >= 0)
1084 {
1085 FreeWild(xp->xp_numfiles, xp->xp_files);
1086 xp->xp_numfiles = -1;
1087 }
zeertzjq28a23602023-09-29 19:58:35 +02001088 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001089}
1090
1091/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001092 * Display one line of completion matches. Multiple matches are displayed in
1093 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001094 * matches - list of completion match names
1095 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001096 * lines - number of output lines
1097 * linenr - line number of matches to display
1098 * maxlen - maximum number of characters in each line
1099 * showtail - display only the tail of the full path of a file name
1100 * dir_attr - highlight attribute to use for directory names
1101 */
1102 static void
1103showmatches_oneline(
1104 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001105 char_u **matches,
1106 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001107 int lines,
1108 int linenr,
1109 int maxlen,
1110 int showtail,
1111 int dir_attr)
1112{
1113 int i, j;
1114 int isdir;
1115 int lastlen;
1116 char_u *p;
1117
1118 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001119 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001120 {
1121 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1122 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001123 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1124 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001125 msg_advance(maxlen + 1);
1126 msg_puts((char *)p);
1127 msg_advance(maxlen + 3);
1128 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1129 break;
1130 }
1131 for (i = maxlen - lastlen; --i >= 0; )
1132 msg_putchar(' ');
1133 if (xp->xp_context == EXPAND_FILES
1134 || xp->xp_context == EXPAND_SHELLCMD
1135 || xp->xp_context == EXPAND_BUFFERS)
1136 {
1137 // highlight directories
1138 if (xp->xp_numfiles != -1)
1139 {
1140 char_u *halved_slash;
1141 char_u *exp_path;
1142 char_u *path;
1143
1144 // Expansion was done before and special characters
1145 // were escaped, need to halve backslashes. Also
1146 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001147 exp_path = expand_env_save_opt(matches[j], TRUE);
1148 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001149 halved_slash = backslash_halve_save(path);
1150 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001151 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001152 vim_free(exp_path);
1153 if (halved_slash != path)
1154 vim_free(halved_slash);
1155 }
1156 else
1157 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001158 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001159 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001160 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001161 else
1162 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001163 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001164 TRUE);
1165 p = NameBuff;
1166 }
1167 }
1168 else
1169 {
1170 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001171 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001172 }
1173 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1174 }
1175 if (msg_col > 0) // when not wrapped around
1176 {
1177 msg_clr_eos();
1178 msg_putchar('\n');
1179 }
1180 out_flush(); // show one line at a time
1181}
1182
1183/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001184 * Show all matches for completion on the command line.
1185 * Returns EXPAND_NOTHING when the character that triggered expansion should
1186 * be inserted like a normal character.
1187 */
1188 int
1189showmatches(expand_T *xp, int wildmenu UNUSED)
1190{
1191 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001192 int numMatches;
1193 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001194 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001195 int maxlen;
1196 int lines;
1197 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001198 int attr;
1199 int showtail;
1200
1201 if (xp->xp_numfiles == -1)
1202 {
1203 set_expand_context(xp);
1204 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001205 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001206 showtail = expand_showtail(xp);
1207 if (i != EXPAND_OK)
1208 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001209 }
1210 else
1211 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001212 numMatches = xp->xp_numfiles;
1213 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001214 showtail = cmd_showtail;
1215 }
1216
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001217 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001218 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001219 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001220
Bram Moolenaar66b51422019-08-18 21:44:12 +02001221 if (!wildmenu)
1222 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 msg_didany = FALSE; // lines_left will be set
1224 msg_start(); // prepare for paging
1225 msg_putchar('\n');
1226 out_flush();
1227 cmdline_row = msg_row;
1228 msg_didany = FALSE; // lines_left will be set again
1229 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001230 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001231
1232 if (got_int)
1233 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001234 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001235 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001236 else
1237 {
1238 // find the length of the longest file name
1239 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001240 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001241 {
1242 if (!showtail && (xp->xp_context == EXPAND_FILES
1243 || xp->xp_context == EXPAND_SHELLCMD
1244 || xp->xp_context == EXPAND_BUFFERS))
1245 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001246 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001247 j = vim_strsize(NameBuff);
1248 }
1249 else
zeertzjqc51a3762022-12-10 10:22:29 +00001250 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001251 if (j > maxlen)
1252 maxlen = j;
1253 }
1254
1255 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001256 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 else
1258 {
1259 // compute the number of columns and lines for the listing
1260 maxlen += 2; // two spaces between file names
1261 columns = ((int)Columns + 2) / maxlen;
1262 if (columns < 1)
1263 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001264 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001265 }
1266
1267 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1268
1269 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1270 {
1271 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1272 msg_clr_eos();
1273 msg_advance(maxlen - 3);
1274 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1275 }
1276
1277 // list the files line by line
1278 for (i = 0; i < lines; ++i)
1279 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001280 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001281 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001282 if (got_int)
1283 {
1284 got_int = FALSE;
1285 break;
1286 }
1287 }
1288
1289 // we redraw the command below the lines that we have just listed
1290 // This is a bit tricky, but it saves a lot of screen updating.
1291 cmdline_row = msg_row; // will put it back later
1292 }
1293
1294 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001295 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001296
1297 return EXPAND_OK;
1298}
1299
1300/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001301 * gettail() version for showmatches() and win_redr_status_matches():
1302 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001303 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001304 static char_u *
1305showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001306{
1307 char_u *p;
1308 char_u *t = s;
1309 int had_sep = FALSE;
1310
1311 for (p = s; *p != NUL; )
1312 {
1313 if (vim_ispathsep(*p)
1314#ifdef BACKSLASH_IN_FILENAME
1315 && !rem_backslash(p)
1316#endif
1317 )
1318 had_sep = TRUE;
1319 else if (had_sep)
1320 {
1321 t = p;
1322 had_sep = FALSE;
1323 }
1324 MB_PTR_ADV(p);
1325 }
1326 return t;
1327}
1328
1329/*
1330 * Return TRUE if we only need to show the tail of completion matches.
1331 * When not completing file names or there is a wildcard in the path FALSE is
1332 * returned.
1333 */
1334 static int
1335expand_showtail(expand_T *xp)
1336{
1337 char_u *s;
1338 char_u *end;
1339
1340 // When not completing file names a "/" may mean something different.
1341 if (xp->xp_context != EXPAND_FILES
1342 && xp->xp_context != EXPAND_SHELLCMD
1343 && xp->xp_context != EXPAND_DIRECTORIES)
1344 return FALSE;
1345
1346 end = gettail(xp->xp_pattern);
1347 if (end == xp->xp_pattern) // there is no path separator
1348 return FALSE;
1349
1350 for (s = xp->xp_pattern; s < end; s++)
1351 {
1352 // Skip escaped wildcards. Only when the backslash is not a path
1353 // separator, on DOS the '*' "path\*\file" must not be skipped.
1354 if (rem_backslash(s))
1355 ++s;
1356 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1357 return FALSE;
1358 }
1359 return TRUE;
1360}
1361
1362/*
1363 * Prepare a string for expansion.
1364 * When expanding file names: The string will be used with expand_wildcards().
1365 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1366 * When expanding other names: The string will be used with regcomp(). Copy
1367 * the name into allocated memory and prepend "^".
1368 */
1369 char_u *
1370addstar(
1371 char_u *fname,
1372 int len,
1373 int context) // EXPAND_FILES etc.
1374{
1375 char_u *retval;
1376 int i, j;
1377 int new_len;
1378 char_u *tail;
1379 int ends_in_star;
1380
1381 if (context != EXPAND_FILES
1382 && context != EXPAND_FILES_IN_PATH
1383 && context != EXPAND_SHELLCMD
1384 && context != EXPAND_DIRECTORIES)
1385 {
1386 // Matching will be done internally (on something other than files).
1387 // So we convert the file-matching-type wildcards into our kind for
1388 // use with vim_regcomp(). First work out how long it will be:
1389
1390 // For help tags the translation is done in find_help_tags().
1391 // For a tag pattern starting with "/" no translation is needed.
1392 if (context == EXPAND_HELP
1393 || context == EXPAND_COLORS
1394 || context == EXPAND_COMPILER
1395 || context == EXPAND_OWNSYNTAX
1396 || context == EXPAND_FILETYPE
1397 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001398 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001399 || ((context == EXPAND_TAGS_LISTFILES
1400 || context == EXPAND_TAGS)
1401 && fname[0] == '/'))
1402 retval = vim_strnsave(fname, len);
1403 else
1404 {
1405 new_len = len + 2; // +2 for '^' at start, NUL at end
1406 for (i = 0; i < len; i++)
1407 {
1408 if (fname[i] == '*' || fname[i] == '~')
1409 new_len++; // '*' needs to be replaced by ".*"
1410 // '~' needs to be replaced by "\~"
1411
1412 // Buffer names are like file names. "." should be literal
1413 if (context == EXPAND_BUFFERS && fname[i] == '.')
1414 new_len++; // "." becomes "\."
1415
1416 // Custom expansion takes care of special things, match
1417 // backslashes literally (perhaps also for other types?)
1418 if ((context == EXPAND_USER_DEFINED
1419 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1420 new_len++; // '\' becomes "\\"
1421 }
1422 retval = alloc(new_len);
1423 if (retval != NULL)
1424 {
1425 retval[0] = '^';
1426 j = 1;
1427 for (i = 0; i < len; i++, j++)
1428 {
1429 // Skip backslash. But why? At least keep it for custom
1430 // expansion.
1431 if (context != EXPAND_USER_DEFINED
1432 && context != EXPAND_USER_LIST
1433 && fname[i] == '\\'
1434 && ++i == len)
1435 break;
1436
1437 switch (fname[i])
1438 {
1439 case '*': retval[j++] = '.';
1440 break;
1441 case '~': retval[j++] = '\\';
1442 break;
1443 case '?': retval[j] = '.';
1444 continue;
1445 case '.': if (context == EXPAND_BUFFERS)
1446 retval[j++] = '\\';
1447 break;
1448 case '\\': if (context == EXPAND_USER_DEFINED
1449 || context == EXPAND_USER_LIST)
1450 retval[j++] = '\\';
1451 break;
1452 }
1453 retval[j] = fname[i];
1454 }
1455 retval[j] = NUL;
1456 }
1457 }
1458 }
1459 else
1460 {
1461 retval = alloc(len + 4);
1462 if (retval != NULL)
1463 {
1464 vim_strncpy(retval, fname, len);
1465
1466 // Don't add a star to *, ~, ~user, $var or `cmd`.
1467 // * would become **, which walks the whole tree.
1468 // ~ would be at the start of the file name, but not the tail.
1469 // $ could be anywhere in the tail.
1470 // ` could be anywhere in the file name.
1471 // When the name ends in '$' don't add a star, remove the '$'.
1472 tail = gettail(retval);
1473 ends_in_star = (len > 0 && retval[len - 1] == '*');
1474#ifndef BACKSLASH_IN_FILENAME
1475 for (i = len - 2; i >= 0; --i)
1476 {
1477 if (retval[i] != '\\')
1478 break;
1479 ends_in_star = !ends_in_star;
1480 }
1481#endif
1482 if ((*retval != '~' || tail != retval)
1483 && !ends_in_star
1484 && vim_strchr(tail, '$') == NULL
1485 && vim_strchr(retval, '`') == NULL)
1486 retval[len++] = '*';
1487 else if (len > 0 && retval[len - 1] == '$')
1488 --len;
1489 retval[len] = NUL;
1490 }
1491 }
1492 return retval;
1493}
1494
1495/*
1496 * Must parse the command line so far to work out what context we are in.
1497 * Completion can then be done based on that context.
1498 * This routine sets the variables:
1499 * xp->xp_pattern The start of the pattern to be expanded within
1500 * the command line (ends at the cursor).
1501 * xp->xp_context The type of thing to expand. Will be one of:
1502 *
1503 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1504 * the command line, like an unknown command. Caller
1505 * should beep.
1506 * EXPAND_NOTHING Unrecognised context for completion, use char like
1507 * a normal char, rather than for completion. eg
1508 * :s/^I/
1509 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1510 * it.
1511 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1512 * EXPAND_FILES After command with EX_XFILE set, or after setting
1513 * with P_EXPAND set. eg :e ^I, :w>>^I
1514 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001515 * when we know only directories are of interest.
1516 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001517 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1518 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1519 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1520 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1521 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1522 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1523 * EXPAND_EVENTS Complete event names
1524 * EXPAND_SYNTAX Complete :syntax command arguments
1525 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1526 * EXPAND_AUGROUP Complete autocommand group names
1527 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1528 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1529 * eg :unmap a^I , :cunab x^I
1530 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1531 * eg :call sub^I
1532 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1533 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1534 * names in expressions, eg :while s^I
1535 * EXPAND_ENV_VARS Complete environment variable names
1536 * EXPAND_USER Complete user names
1537 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001538 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001539set_expand_context(expand_T *xp)
1540{
1541 cmdline_info_T *ccline = get_cmdline_info();
1542
1543 // only expansion for ':', '>' and '=' command-lines
1544 if (ccline->cmdfirstc != ':'
1545#ifdef FEAT_EVAL
1546 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1547 && !ccline->input_fn
1548#endif
1549 )
1550 {
1551 xp->xp_context = EXPAND_NOTHING;
1552 return;
1553 }
1554 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1555}
1556
Bram Moolenaard0190392019-08-23 21:17:35 +02001557/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001558 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1559 * For user defined commands, the completion context is set in 'xp' and the
1560 * completion flags in 'complp'.
1561 *
1562 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001563 */
1564 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001565set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001566{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001567 char_u *p = NULL;
1568 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001569 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001570
1571 // Isolate the command and search for it in the command table.
1572 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001573 // - the 'k' command can directly be followed by any character, but do
1574 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1575 // find matches anywhere in the command name, do this only for command
1576 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001577 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001578 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001579 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001580 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001581 p = cmd + 1;
1582 }
1583 else
1584 {
1585 p = cmd;
1586 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1587 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001588 // A user command may contain digits.
1589 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1590 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001591 while (ASCII_ISALNUM(*p) || *p == '*')
1592 ++p;
1593 // for python 3.x: ":py3*" commands completion
1594 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1595 {
1596 ++p;
1597 while (ASCII_ISALPHA(*p) || *p == '*')
1598 ++p;
1599 }
1600 // check for non-alpha command
1601 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1602 ++p;
1603 len = (int)(p - cmd);
1604
1605 if (len == 0)
1606 {
1607 xp->xp_context = EXPAND_UNSUCCESSFUL;
1608 return NULL;
1609 }
1610
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001611 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001612
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001613 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001614 // Also when doing fuzzy expansion for non-shell commands, support
1615 // alphanumeric characters.
1616 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1617 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001618 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1619 ++p;
1620 }
1621
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001622 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001623 // character, complete the command name.
1624 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1625 return NULL;
1626
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001627 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001628 {
1629 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1630 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001631 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001632 p = cmd + 1;
1633 }
1634 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1635 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001636 eap->cmd = cmd;
1637 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001638 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001639 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001640 }
1641 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001642 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001643 {
1644 // Not still touching the command and it was an illegal one
1645 xp->xp_context = EXPAND_UNSUCCESSFUL;
1646 return NULL;
1647 }
1648
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001649 return p;
1650}
Bram Moolenaard0190392019-08-23 21:17:35 +02001651
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001652/*
1653 * Set the completion context for a command argument with wild card characters.
1654 */
1655 static void
1656set_context_for_wildcard_arg(
1657 exarg_T *eap,
1658 char_u *arg,
1659 int usefilter,
1660 expand_T *xp,
1661 int *complp)
1662{
1663 char_u *p;
1664 int c;
1665 int in_quote = FALSE;
1666 char_u *bow = NULL; // Beginning of word
1667 int len = 0;
1668
1669 // Allow spaces within back-quotes to count as part of the argument
1670 // being expanded.
1671 xp->xp_pattern = skipwhite(arg);
1672 p = xp->xp_pattern;
1673 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001674 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001675 if (has_mbyte)
1676 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001678 c = *p;
1679 if (c == '\\' && p[1] != NUL)
1680 ++p;
1681 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001682 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001683 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001684 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685 xp->xp_pattern = p;
1686 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001687 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001688 in_quote = !in_quote;
1689 }
1690 // An argument can contain just about everything, except
1691 // characters that end the command and white space.
1692 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001693#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001694 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001695#endif
1696 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001697 {
1698 len = 0; // avoid getting stuck when space is in 'isfname'
1699 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001700 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001701 if (has_mbyte)
1702 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001703 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001704 c = *p;
1705 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001706 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001707 if (has_mbyte)
1708 len = (*mb_ptr2len)(p);
1709 else
1710 len = 1;
1711 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001712 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001713 if (in_quote)
1714 bow = p;
1715 else
1716 xp->xp_pattern = p;
1717 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001718 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001719 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001720 }
1721
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001722 // If we are still inside the quotes, and we passed a space, just
1723 // expand from there.
1724 if (bow != NULL && in_quote)
1725 xp->xp_pattern = bow;
1726 xp->xp_context = EXPAND_FILES;
1727
1728 // For a shell command more chars need to be escaped.
1729 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1730 {
1731#ifndef BACKSLASH_IN_FILENAME
1732 xp->xp_shell = TRUE;
1733#endif
1734 // When still after the command name expand executables.
1735 if (xp->xp_pattern == skipwhite(arg))
1736 xp->xp_context = EXPAND_SHELLCMD;
1737 }
1738
1739 // Check for environment variable.
1740 if (*xp->xp_pattern == '$')
1741 {
1742 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1743 if (!vim_isIDc(*p))
1744 break;
1745 if (*p == NUL)
1746 {
1747 xp->xp_context = EXPAND_ENV_VARS;
1748 ++xp->xp_pattern;
1749 // Avoid that the assignment uses EXPAND_FILES again.
1750 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1751 *complp = EXPAND_ENV_VARS;
1752 }
1753 }
1754 // Check for user names.
1755 if (*xp->xp_pattern == '~')
1756 {
1757 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1758 ;
1759 // Complete ~user only if it partially matches a user name.
1760 // A full match ~user<Tab> will be replaced by user's home
1761 // directory i.e. something like ~user<Tab> -> /home/user/
1762 if (*p == NUL && p > xp->xp_pattern + 1
1763 && match_user(xp->xp_pattern + 1) >= 1)
1764 {
1765 xp->xp_context = EXPAND_USER;
1766 ++xp->xp_pattern;
1767 }
1768 }
1769}
1770
1771/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001772 * Set the completion context for the :filter command. Returns a pointer to the
1773 * next command after the :filter command.
1774 */
1775 static char_u *
1776set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1777{
1778 if (*arg != NUL)
1779 arg = skip_vimgrep_pat(arg, NULL, NULL);
1780 if (arg == NULL || *arg == NUL)
1781 {
1782 xp->xp_context = EXPAND_NOTHING;
1783 return NULL;
1784 }
1785 return skipwhite(arg);
1786}
1787
1788#ifdef FEAT_SEARCH_EXTRA
1789/*
1790 * Set the completion context for the :match command. Returns a pointer to the
1791 * next command after the :match command.
1792 */
1793 static char_u *
1794set_context_in_match_cmd(expand_T *xp, char_u *arg)
1795{
1796 if (*arg == NUL || !ends_excmd(*arg))
1797 {
1798 // also complete "None"
1799 set_context_in_echohl_cmd(xp, arg);
1800 arg = skipwhite(skiptowhite(arg));
1801 if (*arg != NUL)
1802 {
1803 xp->xp_context = EXPAND_NOTHING;
1804 arg = skip_regexp(arg + 1, *arg, magic_isset());
1805 }
1806 }
1807 return find_nextcmd(arg);
1808}
1809#endif
1810
1811/*
1812 * Returns a pointer to the next command after a :global or a :v command.
1813 * Returns NULL if there is no next command.
1814 */
1815 static char_u *
1816find_cmd_after_global_cmd(char_u *arg)
1817{
1818 int delim;
1819
1820 delim = *arg; // get the delimiter
1821 if (delim)
1822 ++arg; // skip delimiter if there is one
1823
1824 while (arg[0] != NUL && arg[0] != delim)
1825 {
1826 if (arg[0] == '\\' && arg[1] != NUL)
1827 ++arg;
1828 ++arg;
1829 }
1830 if (arg[0] != NUL)
1831 return arg + 1;
1832
1833 return NULL;
1834}
1835
1836/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001837 * Returns a pointer to the next command after a :substitute or a :& command.
1838 * Returns NULL if there is no next command.
1839 */
1840 static char_u *
1841find_cmd_after_substitute_cmd(char_u *arg)
1842{
1843 int delim;
1844
1845 delim = *arg;
1846 if (delim)
1847 {
1848 // skip "from" part
1849 ++arg;
1850 arg = skip_regexp(arg, delim, magic_isset());
1851
1852 if (arg[0] != NUL && arg[0] == delim)
1853 {
1854 // skip "to" part
1855 ++arg;
1856 while (arg[0] != NUL && arg[0] != delim)
1857 {
1858 if (arg[0] == '\\' && arg[1] != NUL)
1859 ++arg;
1860 ++arg;
1861 }
1862 if (arg[0] != NUL) // skip delimiter
1863 ++arg;
1864 }
1865 }
1866 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1867 ++arg;
1868 if (arg[0] != NUL)
1869 return arg;
1870
1871 return NULL;
1872}
1873
1874/*
1875 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1876 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1877 * Returns NULL if there is no next command.
1878 */
1879 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001880find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001881{
1882 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001883 if (*arg != '/')
1884 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001885
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001886 // Match regexp, not just whole words
1887 for (++arg; *arg && *arg != '/'; arg++)
1888 if (*arg == '\\' && arg[1] != NUL)
1889 arg++;
1890 if (*arg)
1891 {
1892 arg = skipwhite(arg + 1);
1893
1894 // Check for trailing illegal characters
1895 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1896 xp->xp_context = EXPAND_NOTHING;
1897 else
1898 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001899 }
1900
1901 return NULL;
1902}
1903
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001904#ifdef FEAT_EVAL
1905/*
1906 * Set the completion context for the :unlet command. Always returns NULL.
1907 */
1908 static char_u *
1909set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1910{
1911 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1912 arg = xp->xp_pattern + 1;
1913
1914 xp->xp_context = EXPAND_USER_VARS;
1915 xp->xp_pattern = arg;
1916
1917 if (*xp->xp_pattern == '$')
1918 {
1919 xp->xp_context = EXPAND_ENV_VARS;
1920 ++xp->xp_pattern;
1921 }
1922
1923 return NULL;
1924}
1925#endif
1926
1927#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1928/*
1929 * Set the completion context for the :language command. Always returns NULL.
1930 */
1931 static char_u *
1932set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1933{
1934 char_u *p;
1935
1936 p = skiptowhite(arg);
1937 if (*p == NUL)
1938 {
1939 xp->xp_context = EXPAND_LANGUAGE;
1940 xp->xp_pattern = arg;
1941 }
1942 else
1943 {
1944 if ( STRNCMP(arg, "messages", p - arg) == 0
1945 || STRNCMP(arg, "ctype", p - arg) == 0
1946 || STRNCMP(arg, "time", p - arg) == 0
1947 || STRNCMP(arg, "collate", p - arg) == 0)
1948 {
1949 xp->xp_context = EXPAND_LOCALES;
1950 xp->xp_pattern = skipwhite(p);
1951 }
1952 else
1953 xp->xp_context = EXPAND_NOTHING;
1954 }
1955
1956 return NULL;
1957}
1958#endif
1959
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001960#ifdef FEAT_EVAL
1961static enum
1962{
1963 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001964 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1965 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001966} breakpt_expand_what;
1967
1968/*
1969 * Set the completion context for the :breakadd command. Always returns NULL.
1970 */
1971 static char_u *
1972set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1973{
1974 char_u *p;
1975 char_u *subcmd_start;
1976
1977 xp->xp_context = EXPAND_BREAKPOINT;
1978 xp->xp_pattern = arg;
1979
1980 if (cmdidx == CMD_breakadd)
1981 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001982 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001983 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001984 else
1985 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001986
1987 p = skipwhite(arg);
1988 if (*p == NUL)
1989 return NULL;
1990 subcmd_start = p;
1991
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001992 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001993 {
1994 // :breakadd file [lnum] <filename>
1995 // :breakadd func [lnum] <funcname>
1996 p += 4;
1997 p = skipwhite(p);
1998
1999 // skip line number (if specified)
2000 if (VIM_ISDIGIT(*p))
2001 {
2002 p = skipdigits(p);
2003 if (*p != ' ')
2004 {
2005 xp->xp_context = EXPAND_NOTHING;
2006 return NULL;
2007 }
2008 p = skipwhite(p);
2009 }
2010 if (STRNCMP("file", subcmd_start, 4) == 0)
2011 xp->xp_context = EXPAND_FILES;
2012 else
2013 xp->xp_context = EXPAND_USER_FUNC;
2014 xp->xp_pattern = p;
2015 }
2016 else if (STRNCMP("expr ", p, 5) == 0)
2017 {
2018 // :breakadd expr <expression>
2019 xp->xp_context = EXPAND_EXPRESSION;
2020 xp->xp_pattern = skipwhite(p + 5);
2021 }
2022
2023 return NULL;
2024}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002025
2026 static char_u *
2027set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2028{
2029 char_u *p;
2030
2031 xp->xp_context = EXPAND_NOTHING;
2032 xp->xp_pattern = NULL;
2033
2034 p = skipwhite(arg);
2035 if (VIM_ISDIGIT(*p))
2036 return NULL;
2037
2038 xp->xp_context = EXPAND_SCRIPTNAMES;
2039 xp->xp_pattern = p;
2040
2041 return NULL;
2042}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002043#endif
2044
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002045/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002046 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2047 * The argument to the command is 'arg' and the argument flags is 'argt'.
2048 * For user-defined commands and for environment variables, 'compl' has the
2049 * completion type.
2050 * Returns a pointer to the next command. Returns NULL if there is no next
2051 * command.
2052 */
2053 static char_u *
2054set_context_by_cmdname(
2055 char_u *cmd,
2056 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002057 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002058 char_u *arg,
2059 long argt,
2060 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002061 int forceit)
2062{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002063 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002064 {
2065 case CMD_find:
2066 case CMD_sfind:
2067 case CMD_tabfind:
2068 if (xp->xp_context == EXPAND_FILES)
2069 xp->xp_context = EXPAND_FILES_IN_PATH;
2070 break;
2071 case CMD_cd:
2072 case CMD_chdir:
2073 case CMD_tcd:
2074 case CMD_tchdir:
2075 case CMD_lcd:
2076 case CMD_lchdir:
2077 if (xp->xp_context == EXPAND_FILES)
2078 xp->xp_context = EXPAND_DIRECTORIES;
2079 break;
2080 case CMD_help:
2081 xp->xp_context = EXPAND_HELP;
2082 xp->xp_pattern = arg;
2083 break;
2084
2085 // Command modifiers: return the argument.
2086 // Also for commands with an argument that is a command.
2087 case CMD_aboveleft:
2088 case CMD_argdo:
2089 case CMD_belowright:
2090 case CMD_botright:
2091 case CMD_browse:
2092 case CMD_bufdo:
2093 case CMD_cdo:
2094 case CMD_cfdo:
2095 case CMD_confirm:
2096 case CMD_debug:
2097 case CMD_folddoclosed:
2098 case CMD_folddoopen:
2099 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002100 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002101 case CMD_keepalt:
2102 case CMD_keepjumps:
2103 case CMD_keepmarks:
2104 case CMD_keeppatterns:
2105 case CMD_ldo:
2106 case CMD_leftabove:
2107 case CMD_lfdo:
2108 case CMD_lockmarks:
2109 case CMD_noautocmd:
2110 case CMD_noswapfile:
2111 case CMD_rightbelow:
2112 case CMD_sandbox:
2113 case CMD_silent:
2114 case CMD_tab:
2115 case CMD_tabdo:
2116 case CMD_topleft:
2117 case CMD_verbose:
2118 case CMD_vertical:
2119 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002120 case CMD_vim9cmd:
2121 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002122 return arg;
2123
2124 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002125 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002126
2127#ifdef FEAT_SEARCH_EXTRA
2128 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002129 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002130#endif
2131
2132 // All completion for the +cmdline_compl feature goes here.
2133
2134 case CMD_command:
2135 return set_context_in_user_cmd(xp, arg);
2136
2137 case CMD_delcommand:
2138 xp->xp_context = EXPAND_USER_COMMANDS;
2139 xp->xp_pattern = arg;
2140 break;
2141
2142 case CMD_global:
2143 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002144 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002145 case CMD_and:
2146 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002147 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002148 case CMD_isearch:
2149 case CMD_dsearch:
2150 case CMD_ilist:
2151 case CMD_dlist:
2152 case CMD_ijump:
2153 case CMD_psearch:
2154 case CMD_djump:
2155 case CMD_isplit:
2156 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002157 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002158 case CMD_autocmd:
2159 return set_context_in_autocmd(xp, arg, FALSE);
2160 case CMD_doautocmd:
2161 case CMD_doautoall:
2162 return set_context_in_autocmd(xp, arg, TRUE);
2163 case CMD_set:
2164 set_context_in_set_cmd(xp, arg, 0);
2165 break;
2166 case CMD_setglobal:
2167 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2168 break;
2169 case CMD_setlocal:
2170 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2171 break;
2172 case CMD_tag:
2173 case CMD_stag:
2174 case CMD_ptag:
2175 case CMD_ltag:
2176 case CMD_tselect:
2177 case CMD_stselect:
2178 case CMD_ptselect:
2179 case CMD_tjump:
2180 case CMD_stjump:
2181 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002182 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002183 xp->xp_context = EXPAND_TAGS_LISTFILES;
2184 else
2185 xp->xp_context = EXPAND_TAGS;
2186 xp->xp_pattern = arg;
2187 break;
2188 case CMD_augroup:
2189 xp->xp_context = EXPAND_AUGROUP;
2190 xp->xp_pattern = arg;
2191 break;
2192#ifdef FEAT_SYN_HL
2193 case CMD_syntax:
2194 set_context_in_syntax_cmd(xp, arg);
2195 break;
2196#endif
2197#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002198 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002199 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002200 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002201 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002202 case CMD_if:
2203 case CMD_elseif:
2204 case CMD_while:
2205 case CMD_for:
2206 case CMD_echo:
2207 case CMD_echon:
2208 case CMD_execute:
2209 case CMD_echomsg:
2210 case CMD_echoerr:
2211 case CMD_call:
2212 case CMD_return:
2213 case CMD_cexpr:
2214 case CMD_caddexpr:
2215 case CMD_cgetexpr:
2216 case CMD_lexpr:
2217 case CMD_laddexpr:
2218 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002219 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002220 break;
2221
2222 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002223 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002224 case CMD_function:
2225 case CMD_delfunction:
2226 xp->xp_context = EXPAND_USER_FUNC;
2227 xp->xp_pattern = arg;
2228 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002229 case CMD_disassemble:
2230 set_context_in_disassemble_cmd(xp, arg);
2231 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002232
2233 case CMD_echohl:
2234 set_context_in_echohl_cmd(xp, arg);
2235 break;
2236#endif
2237 case CMD_highlight:
2238 set_context_in_highlight_cmd(xp, arg);
2239 break;
2240#ifdef FEAT_CSCOPE
2241 case CMD_cscope:
2242 case CMD_lcscope:
2243 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002244 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002245 break;
2246#endif
2247#ifdef FEAT_SIGNS
2248 case CMD_sign:
2249 set_context_in_sign_cmd(xp, arg);
2250 break;
2251#endif
2252 case CMD_bdelete:
2253 case CMD_bwipeout:
2254 case CMD_bunload:
2255 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2256 arg = xp->xp_pattern + 1;
2257 // FALLTHROUGH
2258 case CMD_buffer:
2259 case CMD_sbuffer:
2260 case CMD_checktime:
2261 xp->xp_context = EXPAND_BUFFERS;
2262 xp->xp_pattern = arg;
2263 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002264#ifdef FEAT_DIFF
2265 case CMD_diffget:
2266 case CMD_diffput:
2267 // If current buffer is in diff mode, complete buffer names
2268 // which are in diff mode, and different than current buffer.
2269 xp->xp_context = EXPAND_DIFF_BUFFERS;
2270 xp->xp_pattern = arg;
2271 break;
2272#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002273 case CMD_USER:
2274 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002275 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2276 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002277
2278 case CMD_map: case CMD_noremap:
2279 case CMD_nmap: case CMD_nnoremap:
2280 case CMD_vmap: case CMD_vnoremap:
2281 case CMD_omap: case CMD_onoremap:
2282 case CMD_imap: case CMD_inoremap:
2283 case CMD_cmap: case CMD_cnoremap:
2284 case CMD_lmap: case CMD_lnoremap:
2285 case CMD_smap: case CMD_snoremap:
2286 case CMD_tmap: case CMD_tnoremap:
2287 case CMD_xmap: case CMD_xnoremap:
2288 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002289 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002290 case CMD_unmap:
2291 case CMD_nunmap:
2292 case CMD_vunmap:
2293 case CMD_ounmap:
2294 case CMD_iunmap:
2295 case CMD_cunmap:
2296 case CMD_lunmap:
2297 case CMD_sunmap:
2298 case CMD_tunmap:
2299 case CMD_xunmap:
2300 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002301 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002302 case CMD_mapclear:
2303 case CMD_nmapclear:
2304 case CMD_vmapclear:
2305 case CMD_omapclear:
2306 case CMD_imapclear:
2307 case CMD_cmapclear:
2308 case CMD_lmapclear:
2309 case CMD_smapclear:
2310 case CMD_tmapclear:
2311 case CMD_xmapclear:
2312 xp->xp_context = EXPAND_MAPCLEAR;
2313 xp->xp_pattern = arg;
2314 break;
2315
2316 case CMD_abbreviate: case CMD_noreabbrev:
2317 case CMD_cabbrev: case CMD_cnoreabbrev:
2318 case CMD_iabbrev: case CMD_inoreabbrev:
2319 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002320 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002321 case CMD_unabbreviate:
2322 case CMD_cunabbrev:
2323 case CMD_iunabbrev:
2324 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002325 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002326#ifdef FEAT_MENU
2327 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2328 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2329 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2330 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2331 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2332 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2333 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2334 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2335 case CMD_tmenu: case CMD_tunmenu:
2336 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2337 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2338#endif
2339
2340 case CMD_colorscheme:
2341 xp->xp_context = EXPAND_COLORS;
2342 xp->xp_pattern = arg;
2343 break;
2344
2345 case CMD_compiler:
2346 xp->xp_context = EXPAND_COMPILER;
2347 xp->xp_pattern = arg;
2348 break;
2349
2350 case CMD_ownsyntax:
2351 xp->xp_context = EXPAND_OWNSYNTAX;
2352 xp->xp_pattern = arg;
2353 break;
2354
2355 case CMD_setfiletype:
2356 xp->xp_context = EXPAND_FILETYPE;
2357 xp->xp_pattern = arg;
2358 break;
2359
2360 case CMD_packadd:
2361 xp->xp_context = EXPAND_PACKADD;
2362 xp->xp_pattern = arg;
2363 break;
2364
zeertzjqb0d45ec2023-01-25 15:04:22 +00002365 case CMD_runtime:
2366 set_context_in_runtime_cmd(xp, arg);
2367 break;
2368
Bram Moolenaard0190392019-08-23 21:17:35 +02002369#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2370 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002371 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002372#endif
2373#if defined(FEAT_PROFILE)
2374 case CMD_profile:
2375 set_context_in_profile_cmd(xp, arg);
2376 break;
2377#endif
2378 case CMD_behave:
2379 xp->xp_context = EXPAND_BEHAVE;
2380 xp->xp_pattern = arg;
2381 break;
2382
2383 case CMD_messages:
2384 xp->xp_context = EXPAND_MESSAGES;
2385 xp->xp_pattern = arg;
2386 break;
2387
2388 case CMD_history:
2389 xp->xp_context = EXPAND_HISTORY;
2390 xp->xp_pattern = arg;
2391 break;
2392#if defined(FEAT_PROFILE)
2393 case CMD_syntime:
2394 xp->xp_context = EXPAND_SYNTIME;
2395 xp->xp_pattern = arg;
2396 break;
2397#endif
2398
2399 case CMD_argdelete:
2400 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2401 arg = xp->xp_pattern + 1;
2402 xp->xp_context = EXPAND_ARGLIST;
2403 xp->xp_pattern = arg;
2404 break;
2405
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002406#ifdef FEAT_EVAL
2407 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002408 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002409 case CMD_breakdel:
2410 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002411
2412 case CMD_scriptnames:
2413 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002414#endif
2415
Bram Moolenaard0190392019-08-23 21:17:35 +02002416 default:
2417 break;
2418 }
2419 return NULL;
2420}
2421
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002422/*
2423 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2424 * we don't need/want deleted. Maybe this could be done better if we didn't
2425 * repeat all this stuff. The only problem is that they may not stay
2426 * perfectly compatible with each other, but then the command line syntax
2427 * probably won't change that much -- webb.
2428 */
2429 static char_u *
2430set_one_cmd_context(
2431 expand_T *xp,
2432 char_u *buff) // buffer for command string
2433{
2434 char_u *p;
2435 char_u *cmd, *arg;
2436 int len = 0;
2437 exarg_T ea;
2438 int compl = EXPAND_NOTHING;
2439 int forceit = FALSE;
2440 int usefilter = FALSE; // filter instead of file name
2441
2442 ExpandInit(xp);
2443 xp->xp_pattern = buff;
2444 xp->xp_line = buff;
2445 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2446 ea.argt = 0;
2447
2448 // 1. skip comment lines and leading space, colons or bars
2449 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2450 ;
2451 xp->xp_pattern = cmd;
2452
2453 if (*cmd == NUL)
2454 return NULL;
2455 if (*cmd == '"') // ignore comment lines
2456 {
2457 xp->xp_context = EXPAND_NOTHING;
2458 return NULL;
2459 }
2460
2461 // 3. Skip over the range to find the command.
2462 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2463 xp->xp_pattern = cmd;
2464 if (*cmd == NUL)
2465 return NULL;
2466 if (*cmd == '"')
2467 {
2468 xp->xp_context = EXPAND_NOTHING;
2469 return NULL;
2470 }
2471
2472 if (*cmd == '|' || *cmd == '\n')
2473 return cmd + 1; // There's another command
2474
2475 // Get the command index.
2476 p = set_cmd_index(cmd, &ea, xp, &compl);
2477 if (p == NULL)
2478 return NULL;
2479
2480 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2481
2482 if (*p == '!') // forced commands
2483 {
2484 forceit = TRUE;
2485 ++p;
2486 }
2487
2488 // 6. parse arguments
2489 if (!IS_USER_CMDIDX(ea.cmdidx))
2490 ea.argt = excmd_get_argt(ea.cmdidx);
2491
2492 arg = skipwhite(p);
2493
2494 // Skip over ++argopt argument
2495 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2496 {
2497 p = arg;
2498 while (*p && !vim_isspace(*p))
2499 MB_PTR_ADV(p);
2500 arg = skipwhite(p);
2501 }
2502
2503 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2504 {
2505 if (*arg == '>') // append
2506 {
2507 if (*++arg == '>')
2508 ++arg;
2509 arg = skipwhite(arg);
2510 }
2511 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2512 {
2513 ++arg;
2514 usefilter = TRUE;
2515 }
2516 }
2517
2518 if (ea.cmdidx == CMD_read)
2519 {
2520 usefilter = forceit; // :r! filter if forced
2521 if (*arg == '!') // :r !filter
2522 {
2523 ++arg;
2524 usefilter = TRUE;
2525 }
2526 }
2527
2528 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2529 {
2530 while (*arg == *cmd) // allow any number of '>' or '<'
2531 ++arg;
2532 arg = skipwhite(arg);
2533 }
2534
2535 // Does command allow "+command"?
2536 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2537 {
2538 // Check if we're in the +command
2539 p = arg + 1;
2540 arg = skip_cmd_arg(arg, FALSE);
2541
2542 // Still touching the command after '+'?
2543 if (*arg == NUL)
2544 return p;
2545
2546 // Skip space(s) after +command to get to the real argument
2547 arg = skipwhite(arg);
2548 }
2549
2550
2551 // Check for '|' to separate commands and '"' to start comments.
2552 // Don't do this for ":read !cmd" and ":write !cmd".
2553 if ((ea.argt & EX_TRLBAR) && !usefilter)
2554 {
2555 p = arg;
2556 // ":redir @" is not the start of a comment
2557 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2558 p += 2;
2559 while (*p)
2560 {
2561 if (*p == Ctrl_V)
2562 {
2563 if (p[1] != NUL)
2564 ++p;
2565 }
2566 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2567 || *p == '|' || *p == '\n')
2568 {
2569 if (*(p - 1) != '\\')
2570 {
2571 if (*p == '|' || *p == '\n')
2572 return p + 1;
2573 return NULL; // It's a comment
2574 }
2575 }
2576 MB_PTR_ADV(p);
2577 }
2578 }
2579
2580 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2581 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2582 // no arguments allowed but there is something
2583 return NULL;
2584
2585 // Find start of last argument (argument just before cursor):
2586 p = buff;
2587 xp->xp_pattern = p;
2588 len = (int)STRLEN(buff);
2589 while (*p && p < buff + len)
2590 {
2591 if (*p == ' ' || *p == TAB)
2592 {
2593 // argument starts after a space
2594 xp->xp_pattern = ++p;
2595 }
2596 else
2597 {
2598 if (*p == '\\' && *(p + 1) != NUL)
2599 ++p; // skip over escaped character
2600 MB_PTR_ADV(p);
2601 }
2602 }
2603
2604 if (ea.argt & EX_XFILE)
2605 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2606
2607 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002608 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002609 forceit);
2610}
2611
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002612/*
2613 * Set the completion context in 'xp' for command 'str'
2614 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002615 void
2616set_cmd_context(
2617 expand_T *xp,
2618 char_u *str, // start of command line
2619 int len, // length of command line (excl. NUL)
2620 int col, // position of cursor
2621 int use_ccline UNUSED) // use ccline for info
2622{
2623#ifdef FEAT_EVAL
2624 cmdline_info_T *ccline = get_cmdline_info();
2625#endif
2626 int old_char = NUL;
2627 char_u *nextcomm;
2628
2629 // Avoid a UMR warning from Purify, only save the character if it has been
2630 // written before.
2631 if (col < len)
2632 old_char = str[col];
2633 str[col] = NUL;
2634 nextcomm = str;
2635
2636#ifdef FEAT_EVAL
2637 if (use_ccline && ccline->cmdfirstc == '=')
2638 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002639 // pass CMD_SIZE because there is no real command
2640 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002641 }
2642 else if (use_ccline && ccline->input_fn)
2643 {
2644 xp->xp_context = ccline->xp_context;
2645 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002646 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002647 }
2648 else
2649#endif
2650 while (nextcomm != NULL)
2651 nextcomm = set_one_cmd_context(xp, nextcomm);
2652
2653 // Store the string here so that call_user_expand_func() can get to them
2654 // easily.
2655 xp->xp_line = str;
2656 xp->xp_col = col;
2657
2658 str[col] = old_char;
2659}
2660
2661/*
2662 * Expand the command line "str" from context "xp".
2663 * "xp" must have been set by set_cmd_context().
2664 * xp->xp_pattern points into "str", to where the text that is to be expanded
2665 * starts.
2666 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2667 * cursor.
2668 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2669 * key that triggered expansion literally.
2670 * Returns EXPAND_OK otherwise.
2671 */
2672 int
2673expand_cmdline(
2674 expand_T *xp,
2675 char_u *str, // start of command line
2676 int col, // position of cursor
2677 int *matchcount, // return: nr of matches
2678 char_u ***matches) // return: array of pointers to matches
2679{
2680 char_u *file_str = NULL;
2681 int options = WILD_ADD_SLASH|WILD_SILENT;
2682
2683 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2684 {
2685 beep_flush();
2686 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2687 }
2688 if (xp->xp_context == EXPAND_NOTHING)
2689 {
2690 // Caller can use the character as a normal char instead
2691 return EXPAND_NOTHING;
2692 }
2693
2694 // add star to file name, or convert to regexp if not exp. files.
2695 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002696 if (cmdline_fuzzy_completion_supported(xp))
2697 // If fuzzy matching, don't modify the search string
2698 file_str = vim_strsave(xp->xp_pattern);
2699 else
2700 {
2701 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2702 if (file_str == NULL)
2703 return EXPAND_UNSUCCESSFUL;
2704 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002705
2706 if (p_wic)
2707 options += WILD_ICASE;
2708
2709 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002710 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002711 {
2712 *matchcount = 0;
2713 *matches = NULL;
2714 }
2715 vim_free(file_str);
2716
2717 return EXPAND_OK;
2718}
2719
Bram Moolenaar66b51422019-08-18 21:44:12 +02002720/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002721 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002722 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002723 */
2724 static int
2725expand_files_and_dirs(
2726 expand_T *xp,
2727 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002728 char_u ***matches,
2729 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002730 int flags,
2731 int options)
2732{
2733 int free_pat = FALSE;
2734 int i;
2735 int ret;
2736
2737 // for ":set path=" and ":set tags=" halve backslashes for escaped
2738 // space
2739 if (xp->xp_backslash != XP_BS_NONE)
2740 {
2741 free_pat = TRUE;
2742 pat = vim_strsave(pat);
2743 for (i = 0; pat[i]; ++i)
2744 if (pat[i] == '\\')
2745 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002746 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002747 && pat[i + 1] == '\\'
2748 && pat[i + 2] == '\\'
2749 && pat[i + 3] == ' ')
2750 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002751 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002752 && pat[i + 1] == ' ')
2753 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002754 else if ((xp->xp_backslash & XP_BS_COMMA)
2755 && pat[i + 1] == '\\'
2756 && pat[i + 2] == ',')
2757 STRMOVE(pat + i, pat + i + 2);
2758#ifdef BACKSLASH_IN_FILENAME
2759 else if ((xp->xp_backslash & XP_BS_COMMA)
2760 && pat[i + 1] == ',')
2761 STRMOVE(pat + i, pat + i + 1);
2762#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002763 }
2764 }
2765
2766 if (xp->xp_context == EXPAND_FILES)
2767 flags |= EW_FILE;
2768 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2769 flags |= (EW_FILE | EW_PATH);
2770 else
2771 flags = (flags | EW_DIR) & ~EW_FILE;
2772 if (options & WILD_ICASE)
2773 flags |= EW_ICASE;
2774
2775 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002776 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002777 if (free_pat)
2778 vim_free(pat);
2779#ifdef BACKSLASH_IN_FILENAME
2780 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2781 {
2782 int j;
2783
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002784 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002785 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002786 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002787
2788 while (*ptr != NUL)
2789 {
2790 if (p_csl[0] == 's' && *ptr == '\\')
2791 *ptr = '/';
2792 else if (p_csl[0] == 'b' && *ptr == '/')
2793 *ptr = '\\';
2794 ptr += (*mb_ptr2len)(ptr);
2795 }
2796 }
2797 }
2798#endif
2799 return ret;
2800}
2801
2802/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002803 * Function given to ExpandGeneric() to obtain the possible arguments of the
2804 * ":behave {mswin,xterm}" command.
2805 */
2806 static char_u *
2807get_behave_arg(expand_T *xp UNUSED, int idx)
2808{
2809 if (idx == 0)
2810 return (char_u *)"mswin";
2811 if (idx == 1)
2812 return (char_u *)"xterm";
2813 return NULL;
2814}
2815
K.Takata161b6ac2022-11-14 15:31:07 +00002816#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002817/*
2818 * Function given to ExpandGeneric() to obtain the possible arguments of the
2819 * ":breakadd {expr, file, func, here}" command.
2820 * ":breakdel {func, file, here}" command.
2821 */
2822 static char_u *
2823get_breakadd_arg(expand_T *xp UNUSED, int idx)
2824{
2825 char *opts[] = {"expr", "file", "func", "here"};
2826
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002827 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002828 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002829 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002830 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2831 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002832 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2833 {
2834 // breakdel {func, file, here}
2835 if (idx <= 2)
2836 return (char_u *)opts[idx + 1];
2837 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002838 else
2839 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002840 // profdel {func, file}
2841 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002842 return (char_u *)opts[idx + 1];
2843 }
2844 }
2845 return NULL;
2846}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002847
2848/*
2849 * Function given to ExpandGeneric() to obtain the possible arguments for the
2850 * ":scriptnames" command.
2851 */
2852 static char_u *
2853get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2854{
2855 scriptitem_T *si;
2856
2857 if (!SCRIPT_ID_VALID(idx + 1))
2858 return NULL;
2859
2860 si = SCRIPT_ITEM(idx + 1);
2861 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2862 return NameBuff;
2863}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002864#endif
2865
Bram Moolenaard0190392019-08-23 21:17:35 +02002866/*
2867 * Function given to ExpandGeneric() to obtain the possible arguments of the
2868 * ":messages {clear}" command.
2869 */
2870 static char_u *
2871get_messages_arg(expand_T *xp UNUSED, int idx)
2872{
2873 if (idx == 0)
2874 return (char_u *)"clear";
2875 return NULL;
2876}
2877
2878 static char_u *
2879get_mapclear_arg(expand_T *xp UNUSED, int idx)
2880{
2881 if (idx == 0)
2882 return (char_u *)"<buffer>";
2883 return NULL;
2884}
2885
2886/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002887 * Do the expansion based on xp->xp_context and 'rmp'.
2888 */
2889 static int
2890ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002891 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002892 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002893 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002894 char_u ***matches,
2895 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002896{
2897 static struct expgen
2898 {
2899 int context;
2900 char_u *((*func)(expand_T *, int));
2901 int ic;
2902 int escaped;
2903 } tab[] =
2904 {
2905 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2906 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2907 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2908 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2909 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2910 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2911 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2912 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2913 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2914 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002915#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002916 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2917 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2918 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2919 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2920 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002921#endif
2922#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002923 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2924 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002925#endif
2926#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002927 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002928#endif
2929#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002930 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002931#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002932 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2933 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2934 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002935#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002936 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002937#endif
2938#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002939 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002940#endif
2941#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002942 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002943#endif
2944#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002945 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2946 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002947#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002948 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2949 {EXPAND_USER, get_users, TRUE, FALSE},
2950 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002951#ifdef FEAT_EVAL
2952 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002953 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002954#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002955 };
2956 int i;
2957 int ret = FAIL;
2958
2959 // Find a context in the table and call the ExpandGeneric() with the
2960 // right function to do the expansion.
2961 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2962 {
2963 if (xp->xp_context == tab[i].context)
2964 {
2965 if (tab[i].ic)
2966 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002967 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2968 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002969 break;
2970 }
2971 }
2972
2973 return ret;
2974}
2975
2976/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002977 * Map wild expand options to flags for expand_wildcards()
2978 */
2979 static int
2980map_wildopts_to_ewflags(int options)
2981{
2982 int flags;
2983
2984 flags = EW_DIR; // include directories
2985 if (options & WILD_LIST_NOTFOUND)
2986 flags |= EW_NOTFOUND;
2987 if (options & WILD_ADD_SLASH)
2988 flags |= EW_ADDSLASH;
2989 if (options & WILD_KEEP_ALL)
2990 flags |= EW_KEEPALL;
2991 if (options & WILD_SILENT)
2992 flags |= EW_SILENT;
2993 if (options & WILD_NOERROR)
2994 flags |= EW_NOERROR;
2995 if (options & WILD_ALLLINKS)
2996 flags |= EW_ALLLINKS;
2997
2998 return flags;
2999}
3000
3001/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003002 * Do the expansion based on xp->xp_context and "pat".
3003 */
3004 static int
3005ExpandFromContext(
3006 expand_T *xp,
3007 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003008 char_u ***matches,
3009 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003010 int options) // WILD_ flags
3011{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003012 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003013 int ret;
3014 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003015 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003016 int fuzzy = cmdline_fuzzy_complete(pat)
3017 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003018
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003019 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003020
3021 if (xp->xp_context == EXPAND_FILES
3022 || xp->xp_context == EXPAND_DIRECTORIES
3023 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003024 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3025 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003026
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003027 *matches = (char_u **)"";
3028 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003029 if (xp->xp_context == EXPAND_HELP)
3030 {
3031 // With an empty argument we would get all the help tags, which is
3032 // very slow. Get matches for "help" instead.
3033 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003034 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003035 {
3036#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003037 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003038#endif
3039 return OK;
3040 }
3041 return FAIL;
3042 }
3043
Bram Moolenaar66b51422019-08-18 21:44:12 +02003044 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003045 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003046 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003047 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003048 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003049 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003050#ifdef FEAT_DIFF
3051 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003052 return ExpandBufnames(pat, numMatches, matches,
3053 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003054#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003055 if (xp->xp_context == EXPAND_TAGS
3056 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003057 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3058 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003059 if (xp->xp_context == EXPAND_COLORS)
3060 {
3061 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003062 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003063 directories);
3064 }
3065 if (xp->xp_context == EXPAND_COMPILER)
3066 {
3067 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003068 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003069 }
3070 if (xp->xp_context == EXPAND_OWNSYNTAX)
3071 {
3072 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003073 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003074 }
3075 if (xp->xp_context == EXPAND_FILETYPE)
3076 {
3077 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003078 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003079 }
K.Takata161b6ac2022-11-14 15:31:07 +00003080#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003081 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003082 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003083#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003084 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003085 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003086 if (xp->xp_context == EXPAND_RUNTIME)
3087 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003088
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003089 // When expanding a function name starting with s:, match the <SNR>nr_
3090 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003091 if ((xp->xp_context == EXPAND_USER_FUNC
3092 || xp->xp_context == EXPAND_DISASSEMBLE)
3093 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003094 {
3095 int len = (int)STRLEN(pat) + 20;
3096
3097 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003098 if (tofree == NULL)
3099 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003100 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003101 pat = tofree;
3102 }
3103
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003104 if (!fuzzy)
3105 {
3106 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3107 if (regmatch.regprog == NULL)
3108 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003109
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003110 // set ignore-case according to p_ic, p_scs and pat
3111 regmatch.rm_ic = ignorecase(pat);
3112 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113
3114 if (xp->xp_context == EXPAND_SETTINGS
3115 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003116 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003117 else if (xp->xp_context == EXPAND_STRING_SETTING)
3118 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3119 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3120 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003121 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003122 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003123#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003124 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003125 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003126#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003127 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003128 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003129
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003130 if (!fuzzy)
3131 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003132 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133
3134 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003135}
3136
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137/*
3138 * Expand a list of names.
3139 *
3140 * Generic function for command line completion. It calls a function to
3141 * obtain strings, one by one. The strings are matched against a regexp
3142 * program. Matching strings are copied into an array, which is returned.
3143 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003144 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3145 * is used.
3146 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147 * Returns OK when no problems encountered, FAIL for error (out of memory).
3148 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003149 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003150ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003151 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003152 expand_T *xp,
3153 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003154 char_u ***matches,
3155 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003156 char_u *((*func)(expand_T *, int)),
3157 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003158 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003159{
3160 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003161 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003162 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003163 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003164 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003165 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003166 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003167 int sort_matches = FALSE;
3168 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003169
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003170 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003171 *matches = NULL;
3172 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003173
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003174 if (!fuzzy)
3175 ga_init2(&ga, sizeof(char *), 30);
3176 else
3177 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3178
3179 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003180 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003181 str = (*func)(xp, i);
3182 if (str == NULL) // end of list
3183 break;
3184 if (*str == NUL) // skip empty strings
3185 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003186
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003187 if (xp->xp_pattern[0] != NUL)
3188 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003189 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003190 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003191 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003192 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003193 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003194 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003195 }
3196 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003197 else
3198 match = TRUE;
3199
3200 if (!match)
3201 continue;
3202
3203 if (escaped)
3204 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3205 else
3206 str = vim_strsave(str);
3207 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003208 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003209 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003210 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003211 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003212 return FAIL;
3213 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003214 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003215 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003216 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003217
3218 if (ga_grow(&ga, 1) == FAIL)
3219 {
3220 vim_free(str);
3221 break;
3222 }
3223
3224 if (fuzzy)
3225 {
3226 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3227 fuzmatch->idx = ga.ga_len;
3228 fuzmatch->str = str;
3229 fuzmatch->score = score;
3230 }
3231 else
3232 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3233
K.Takata161b6ac2022-11-14 15:31:07 +00003234#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003235 if (func == get_menu_names)
3236 {
3237 // test for separator added by get_menu_names()
3238 str += STRLEN(str) - 1;
3239 if (*str == '\001')
3240 *str = '.';
3241 }
K.Takata161b6ac2022-11-14 15:31:07 +00003242#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003243
3244 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003245 }
3246
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003247 if (ga.ga_len == 0)
3248 return OK;
3249
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003250 // sort the matches when using regular expression matching and sorting
3251 // applies to the completion context. Menus and scriptnames should be kept
3252 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003253 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003254 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003255 && xp->xp_context != EXPAND_MENUS
3256 && xp->xp_context != EXPAND_SCRIPTNAMES)
3257 sort_matches = TRUE;
3258
3259 // <SNR> functions should be sorted to the end.
3260 if (xp->xp_context == EXPAND_EXPRESSION
3261 || xp->xp_context == EXPAND_FUNCTIONS
3262 || xp->xp_context == EXPAND_USER_FUNC
3263 || xp->xp_context == EXPAND_DISASSEMBLE)
3264 funcsort = TRUE;
3265
3266 // Sort the matches.
3267 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003268 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003269 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003270 // <SNR> functions should be sorted to the end.
3271 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3272 sort_func_compare);
3273 else
3274 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3275 }
3276
3277 if (!fuzzy)
3278 {
3279 *matches = ga.ga_data;
3280 *numMatches = ga.ga_len;
3281 }
3282 else
3283 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003284 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3285 funcsort) == FAIL)
3286 return FAIL;
3287 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003288 }
3289
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003290#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003291 // Reset the variables used for special highlight names expansion, so that
3292 // they don't show up when getting normal highlight names by ID.
3293 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003294#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003295
Bram Moolenaar66b51422019-08-18 21:44:12 +02003296 return OK;
3297}
3298
3299/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003300 * Expand shell command matches in one directory of $PATH.
3301 */
3302 static void
3303expand_shellcmd_onedir(
3304 char_u *buf,
3305 char_u *s,
3306 size_t l,
3307 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003308 char_u ***matches,
3309 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003310 int flags,
3311 hashtab_T *ht,
3312 garray_T *gap)
3313{
3314 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003315 hash_T hash;
3316 hashitem_T *hi;
3317
3318 vim_strncpy(buf, s, l);
3319 add_pathsep(buf);
3320 l = STRLEN(buf);
3321 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3322
3323 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003324 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003325 if (ret != OK)
3326 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003327
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003328 if (ga_grow(gap, *numMatches) == FAIL)
3329 {
3330 FreeWild(*numMatches, *matches);
3331 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003332 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003333
3334 for (int i = 0; i < *numMatches; ++i)
3335 {
3336 char_u *name = (*matches)[i];
3337
3338 if (STRLEN(name) > l)
3339 {
3340 // Check if this name was already found.
3341 hash = hash_hash(name + l);
3342 hi = hash_lookup(ht, name + l, hash);
3343 if (HASHITEM_EMPTY(hi))
3344 {
3345 // Remove the path that was prepended.
3346 STRMOVE(name, name + l);
3347 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3348 hash_add_item(ht, hi, name, hash);
3349 name = NULL;
3350 }
3351 }
3352 vim_free(name);
3353 }
3354 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003355}
3356
3357/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003358 * Complete a shell command.
3359 * Returns FAIL or OK;
3360 */
3361 static int
3362expand_shellcmd(
3363 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003364 char_u ***matches, // return: array with matches
3365 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003366 int flagsarg) // EW_ flags
3367{
3368 char_u *pat;
3369 int i;
3370 char_u *path = NULL;
3371 int mustfree = FALSE;
3372 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003373 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003374 size_t l;
3375 char_u *s, *e;
3376 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003377 int did_curdir = FALSE;
3378 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003379
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003380 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003381 if (buf == NULL)
3382 return FAIL;
3383
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003384 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003385 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003386 if (pat == NULL)
3387 {
3388 vim_free(buf);
3389 return FAIL;
3390 }
3391
Bram Moolenaar66b51422019-08-18 21:44:12 +02003392 for (i = 0; pat[i]; ++i)
3393 if (pat[i] == '\\' && pat[i + 1] == ' ')
3394 STRMOVE(pat + i, pat + i + 1);
3395
3396 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3397
3398 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3399 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3400 path = (char_u *)".";
3401 else
3402 {
3403 // For an absolute name we don't use $PATH.
3404 if (!mch_isFullName(pat))
3405 path = vim_getenv((char_u *)"PATH", &mustfree);
3406 if (path == NULL)
3407 path = (char_u *)"";
3408 }
3409
3410 // Go over all directories in $PATH. Expand matches in that directory and
3411 // collect them in "ga". When "." is not in $PATH also expand for the
3412 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003413 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003414 hash_init(&found_ht);
3415 for (s = path; ; s = e)
3416 {
K.Takata161b6ac2022-11-14 15:31:07 +00003417#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003418 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003419#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003420 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003421#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003422 if (e == NULL)
3423 e = s + STRLEN(s);
3424
3425 if (*s == NUL)
3426 {
3427 if (did_curdir)
3428 break;
3429 // Find directories in the current directory, path is empty.
3430 did_curdir = TRUE;
3431 flags |= EW_DIR;
3432 }
3433 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3434 {
3435 did_curdir = TRUE;
3436 flags |= EW_DIR;
3437 }
3438 else
3439 // Do not match directories inside a $PATH item.
3440 flags &= ~EW_DIR;
3441
3442 l = e - s;
3443 if (l > MAXPATHL - 5)
3444 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003445
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003446 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003447 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003448
Bram Moolenaar66b51422019-08-18 21:44:12 +02003449 if (*e != NUL)
3450 ++e;
3451 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003452 *matches = ga.ga_data;
3453 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003454
3455 vim_free(buf);
3456 vim_free(pat);
3457 if (mustfree)
3458 vim_free(path);
3459 hash_clear(&found_ht);
3460 return OK;
3461}
3462
K.Takata161b6ac2022-11-14 15:31:07 +00003463#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003464/*
3465 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003466 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003467 */
3468 static void *
3469call_user_expand_func(
3470 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003471 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472{
3473 cmdline_info_T *ccline = get_cmdline_info();
3474 int keep = 0;
3475 typval_T args[4];
3476 sctx_T save_current_sctx = current_sctx;
3477 char_u *pat = NULL;
3478 void *ret;
3479
3480 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3481 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003482
3483 if (ccline->cmdbuff != NULL)
3484 {
3485 keep = ccline->cmdbuff[ccline->cmdlen];
3486 ccline->cmdbuff[ccline->cmdlen] = 0;
3487 }
3488
3489 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3490
3491 args[0].v_type = VAR_STRING;
3492 args[0].vval.v_string = pat;
3493 args[1].v_type = VAR_STRING;
3494 args[1].vval.v_string = xp->xp_line;
3495 args[2].v_type = VAR_NUMBER;
3496 args[2].vval.v_number = xp->xp_col;
3497 args[3].v_type = VAR_UNKNOWN;
3498
3499 current_sctx = xp->xp_script_ctx;
3500
3501 ret = user_expand_func(xp->xp_arg, 3, args);
3502
3503 current_sctx = save_current_sctx;
3504 if (ccline->cmdbuff != NULL)
3505 ccline->cmdbuff[ccline->cmdlen] = keep;
3506
3507 vim_free(pat);
3508 return ret;
3509}
3510
3511/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003512 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3513 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003514 */
3515 static int
3516ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003517 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003518 expand_T *xp,
3519 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003520 char_u ***matches,
3521 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003522{
3523 char_u *retstr;
3524 char_u *s;
3525 char_u *e;
3526 int keep;
3527 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003528 int fuzzy;
3529 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003530 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003531
3532 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003533 *matches = NULL;
3534 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003535
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003536 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003537 if (retstr == NULL)
3538 return FAIL;
3539
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003540 if (!fuzzy)
3541 ga_init2(&ga, sizeof(char *), 3);
3542 else
3543 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3544
Bram Moolenaar66b51422019-08-18 21:44:12 +02003545 for (s = retstr; *s != NUL; s = e)
3546 {
3547 e = vim_strchr(s, '\n');
3548 if (e == NULL)
3549 e = s + STRLEN(s);
3550 keep = *e;
3551 *e = NUL;
3552
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003553 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003554 {
3555 if (!fuzzy)
3556 match = vim_regexec(regmatch, s, (colnr_T)0);
3557 else
3558 {
3559 score = fuzzy_match_str(s, pat);
3560 match = (score != 0);
3561 }
3562 }
3563 else
3564 match = TRUE; // match everything
3565
Bram Moolenaar66b51422019-08-18 21:44:12 +02003566 *e = keep;
3567
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003568 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003569 {
3570 if (ga_grow(&ga, 1) == FAIL)
3571 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003572 if (!fuzzy)
3573 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3574 else
3575 {
3576 fuzmatch_str_T *fuzmatch =
3577 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003578 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003579 fuzmatch->str = vim_strnsave(s, e - s);
3580 fuzmatch->score = score;
3581 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003582 ++ga.ga_len;
3583 }
3584
3585 if (*e != NUL)
3586 ++e;
3587 }
3588 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003589
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003590 if (ga.ga_len == 0)
3591 return OK;
3592
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003593 if (!fuzzy)
3594 {
3595 *matches = ga.ga_data;
3596 *numMatches = ga.ga_len;
3597 }
3598 else
3599 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003600 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3601 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003602 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003603 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003604 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003605 return OK;
3606}
3607
3608/*
3609 * Expand names with a list returned by a function defined by the user.
3610 */
3611 static int
3612ExpandUserList(
3613 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003614 char_u ***matches,
3615 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003616{
3617 list_T *retlist;
3618 listitem_T *li;
3619 garray_T ga;
3620
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003621 *matches = NULL;
3622 *numMatches = 0;
3623 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003624 if (retlist == NULL)
3625 return FAIL;
3626
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003627 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003629 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003630 {
3631 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3632 continue; // Skip non-string items and empty strings
3633
3634 if (ga_grow(&ga, 1) == FAIL)
3635 break;
3636
3637 ((char_u **)ga.ga_data)[ga.ga_len] =
3638 vim_strsave(li->li_tv.vval.v_string);
3639 ++ga.ga_len;
3640 }
3641 list_unref(retlist);
3642
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003643 *matches = ga.ga_data;
3644 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003645 return OK;
3646}
K.Takata161b6ac2022-11-14 15:31:07 +00003647#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003648
3649/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003650 * Expand "file" for all comma-separated directories in "path".
3651 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003652 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003653 */
3654 void
3655globpath(
3656 char_u *path,
3657 char_u *file,
3658 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003659 int expand_options,
3660 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003661{
3662 expand_T xpc;
3663 char_u *buf;
3664 int i;
3665 int num_p;
3666 char_u **p;
3667
3668 buf = alloc(MAXPATHL);
3669 if (buf == NULL)
3670 return;
3671
3672 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003673 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003674
3675 // Loop over all entries in {path}.
3676 while (*path != NUL)
3677 {
3678 // Copy one item of the path to buf[] and concatenate the file name.
3679 copy_option_part(&path, buf, MAXPATHL, ",");
3680 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3681 {
K.Takata161b6ac2022-11-14 15:31:07 +00003682#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003683 // Using the platform's path separator (\) makes vim incorrectly
3684 // treat it as an escape character, use '/' instead.
3685 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3686 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003687#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003689#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003690 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003691 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003692 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3693 {
3694 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3695
3696 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003697 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003698 for (i = 0; i < num_p; ++i)
3699 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003700 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003701 ++ga->ga_len;
3702 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003703 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003704 }
3705 }
3706 }
3707
3708 vim_free(buf);
3709}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003710
Bram Moolenaareadee482020-09-04 15:37:31 +02003711/*
3712 * Translate some keys pressed when 'wildmenu' is used.
3713 */
3714 int
3715wildmenu_translate_key(
3716 cmdline_info_T *cclp,
3717 int key,
3718 expand_T *xp,
3719 int did_wild_list)
3720{
3721 int c = key;
3722
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003723 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003724 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003725 // When the popup menu is used for cmdline completion:
3726 // Up : go to the previous item in the menu
3727 // Down : go to the next item in the menu
3728 // Left : go to the parent directory
3729 // Right: list the files in the selected directory
3730 switch (c)
3731 {
3732 case K_UP: c = K_LEFT; break;
3733 case K_DOWN: c = K_RIGHT; break;
3734 case K_LEFT: c = K_UP; break;
3735 case K_RIGHT: c = K_DOWN; break;
3736 default: break;
3737 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003738 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003739
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003740 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003741 {
3742 if (c == K_LEFT)
3743 c = Ctrl_P;
3744 else if (c == K_RIGHT)
3745 c = Ctrl_N;
3746 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003747
Bram Moolenaareadee482020-09-04 15:37:31 +02003748 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003749 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003750 && cclp->cmdpos > 1
3751 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3752 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3753 && (c == '\n' || c == '\r' || c == K_KENTER))
3754 c = K_DOWN;
3755
3756 return c;
3757}
3758
3759/*
3760 * Delete characters on the command line, from "from" to the current
3761 * position.
3762 */
3763 static void
3764cmdline_del(cmdline_info_T *cclp, int from)
3765{
3766 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3767 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3768 cclp->cmdlen -= cclp->cmdpos - from;
3769 cclp->cmdpos = from;
3770}
3771
3772/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003773 * Handle a key pressed when the wild menu for the menu names
3774 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003775 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003776 static int
3777wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003778{
Bram Moolenaareadee482020-09-04 15:37:31 +02003779 int i;
3780 int j;
3781
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003782 // Hitting <Down> after "emenu Name.": complete submenu
3783 if (key == K_DOWN && cclp->cmdpos > 0
3784 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003785 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003786 key = p_wc;
3787 KeyTyped = TRUE; // in case the key was mapped
3788 }
3789 else if (key == K_UP)
3790 {
3791 // Hitting <Up>: Remove one submenu name in front of the
3792 // cursor
3793 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003794
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003795 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3796 i = 0;
3797 while (--j > 0)
3798 {
3799 // check for start of menu name
3800 if (cclp->cmdbuff[j] == ' '
3801 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003802 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003803 i = j + 1;
3804 break;
3805 }
3806 // check for start of submenu name
3807 if (cclp->cmdbuff[j] == '.'
3808 && cclp->cmdbuff[j - 1] != '\\')
3809 {
3810 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003811 {
3812 i = j + 1;
3813 break;
3814 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003815 else
3816 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003817 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003818 }
3819 if (i > 0)
3820 cmdline_del(cclp, i);
3821 key = p_wc;
3822 KeyTyped = TRUE; // in case the key was mapped
3823 xp->xp_context = EXPAND_NOTHING;
3824 }
3825
3826 return key;
3827}
3828
3829/*
3830 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3831 * directory names (EXPAND_DIRECTORIES) or shell command names
3832 * (EXPAND_SHELLCMD) is displayed.
3833 */
3834 static int
3835wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3836{
3837 int i;
3838 int j;
3839 char_u upseg[5];
3840
3841 upseg[0] = PATHSEP;
3842 upseg[1] = '.';
3843 upseg[2] = '.';
3844 upseg[3] = PATHSEP;
3845 upseg[4] = NUL;
3846
3847 if (key == K_DOWN
3848 && cclp->cmdpos > 0
3849 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3850 && (cclp->cmdpos < 3
3851 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3852 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3853 {
3854 // go down a directory
3855 key = p_wc;
3856 KeyTyped = TRUE; // in case the key was mapped
3857 }
3858 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3859 {
3860 // If in a direct ancestor, strip off one ../ to go down
3861 int found = FALSE;
3862
3863 j = cclp->cmdpos;
3864 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3865 while (--j > i)
3866 {
3867 if (has_mbyte)
3868 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3869 if (vim_ispathsep(cclp->cmdbuff[j]))
3870 {
3871 found = TRUE;
3872 break;
3873 }
3874 }
3875 if (found
3876 && cclp->cmdbuff[j - 1] == '.'
3877 && cclp->cmdbuff[j - 2] == '.'
3878 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3879 {
3880 cmdline_del(cclp, j - 2);
3881 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003882 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003883 }
3884 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003885 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003886 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003887 // go up a directory
3888 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003889
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003890 j = cclp->cmdpos - 1;
3891 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3892 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003893 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003894 if (has_mbyte)
3895 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3896 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003897#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003898 && vim_strchr((char_u *)" *?[{`$%#",
3899 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003900#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003901 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003902 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003903 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003904 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003905 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003906 break;
3907 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003908 else
3909 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003910 }
3911 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003912
3913 if (!found)
3914 j = i;
3915 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3916 j += 4;
3917 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3918 && j == i)
3919 j += 3;
3920 else
3921 j = 0;
3922 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003923 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003924 // TODO this is only for DOS/UNIX systems - need to put in
3925 // machine-specific stuff here and in upseg init
3926 cmdline_del(cclp, j);
3927 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003928 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003929 else if (cclp->cmdpos > i)
3930 cmdline_del(cclp, i);
3931
3932 // Now complete in the new directory. Set KeyTyped in case the
3933 // Up key came from a mapping.
3934 key = p_wc;
3935 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003936 }
3937
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003938 return key;
3939}
3940
3941/*
3942 * Handle a key pressed when the wild menu is displayed
3943 */
3944 int
3945wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3946{
3947 if (xp->xp_context == EXPAND_MENUNAMES)
3948 return wildmenu_process_key_menunames(cclp, key, xp);
3949 else if ((xp->xp_context == EXPAND_FILES
3950 || xp->xp_context == EXPAND_DIRECTORIES
3951 || xp->xp_context == EXPAND_SHELLCMD))
3952 return wildmenu_process_key_filenames(cclp, key, xp);
3953
3954 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003955}
3956
3957/*
3958 * Free expanded names when finished walking through the matches
3959 */
3960 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003961wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003962{
3963 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02003964
3965 if (!p_wmnu || wild_menu_showing == 0)
3966 return;
3967
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003968#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003969 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02003970 if (cclp->input_fn)
3971 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003972#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003973
3974 if (wild_menu_showing == WM_SCROLLED)
3975 {
3976 // Entered command line, move it up
3977 cmdline_row--;
3978 redrawcmd();
3979 }
3980 else if (save_p_ls != -1)
3981 {
3982 // restore 'laststatus' and 'winminheight'
3983 p_ls = save_p_ls;
3984 p_wmh = save_p_wmh;
3985 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003986 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003987 redrawcmd();
3988 save_p_ls = -1;
3989 }
3990 else
3991 {
3992 win_redraw_last_status(topframe);
3993 redraw_statuslines();
3994 }
3995 KeyTyped = skt;
3996 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003997#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003998 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003999 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004000#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004001}
Bram Moolenaareadee482020-09-04 15:37:31 +02004002
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004003#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004004/*
4005 * "getcompletion()" function
4006 */
4007 void
4008f_getcompletion(typval_T *argvars, typval_T *rettv)
4009{
4010 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004011 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004012 expand_T xpc;
4013 int filtered = FALSE;
4014 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004015 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004016
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004017 if (in_vim9script()
4018 && (check_for_string_arg(argvars, 0) == FAIL
4019 || check_for_string_arg(argvars, 1) == FAIL
4020 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4021 return;
4022
ii144785fe02021-11-21 12:13:56 +00004023 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004024 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004025 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004026 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004027
4028 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004029 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004030
4031 if (p_wic)
4032 options |= WILD_ICASE;
4033
4034 // For filtered results, 'wildignore' is used
4035 if (!filtered)
4036 options |= WILD_KEEP_ALL;
4037
4038 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004039 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004040 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004041 int cmdline_len = (int)STRLEN(pat);
4042 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004043 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004044 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004045 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004046 else
4047 {
ii144785fe02021-11-21 12:13:56 +00004048 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004049 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004050 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004051
4052 xpc.xp_context = cmdcomplete_str_to_type(type);
4053 if (xpc.xp_context == EXPAND_NOTHING)
4054 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004055 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004056 return;
4057 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004058
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004059 if (xpc.xp_context == EXPAND_USER_DEFINED)
4060 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004061 // Must be "custom,funcname" pattern
4062 if (STRNCMP(type, "custom,", 7) != 0)
4063 {
4064 semsg(_(e_invalid_argument_str), type);
4065 return;
4066 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004067
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004068 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004069 }
4070
4071 if (xpc.xp_context == EXPAND_USER_LIST)
4072 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004073 // Must be "customlist,funcname" pattern
4074 if (STRNCMP(type, "customlist,", 11) != 0)
4075 {
4076 semsg(_(e_invalid_argument_str), type);
4077 return;
4078 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004079
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004080 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004081 }
4082
Bram Moolenaar66b51422019-08-18 21:44:12 +02004083# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004084 if (xpc.xp_context == EXPAND_MENUS)
4085 {
4086 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4087 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4088 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004089# endif
4090# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004091 if (xpc.xp_context == EXPAND_CSCOPE)
4092 {
4093 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4094 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4095 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004096# endif
4097# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004098 if (xpc.xp_context == EXPAND_SIGN)
4099 {
4100 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4101 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4102 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004103# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004104 if (xpc.xp_context == EXPAND_RUNTIME)
4105 {
4106 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4107 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4108 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004109 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004110
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004111 if (cmdline_fuzzy_completion_supported(&xpc))
4112 // when fuzzy matching, don't modify the search string
4113 pat = vim_strsave(xpc.xp_pattern);
4114 else
4115 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4116
Bram Moolenaar93a10962022-06-16 11:42:09 +01004117 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004118 {
4119 int i;
4120
4121 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4122
4123 for (i = 0; i < xpc.xp_numfiles; i++)
4124 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4125 }
4126 vim_free(pat);
4127 ExpandCleanup(&xpc);
4128}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004129#endif // FEAT_EVAL