blob: 8bccaa3bb0cedb955615a3b529b03c7beb4cff85 [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
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200263 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000264 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/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001772 * Set the completion context for the "++opt=arg" argument. Always returns
1773 * NULL.
1774 */
1775 static char_u *
1776set_context_in_argopt(expand_T *xp, char_u *arg)
1777{
1778 char_u *p;
1779
1780 p = vim_strchr(arg, '=');
1781 if (p == NULL)
1782 xp->xp_pattern = arg;
1783 else
1784 xp->xp_pattern = p + 1;
1785
1786 xp->xp_context = EXPAND_ARGOPT;
1787 return NULL;
1788}
1789
1790#ifdef FEAT_TERMINAL
1791/*
1792 * Set the completion context for :terminal's [options]. Always returns NULL.
1793 */
1794 static char_u *
1795set_context_in_terminalopt(expand_T *xp, char_u *arg)
1796{
1797 char_u *p;
1798
1799 p = vim_strchr(arg, '=');
1800 if (p == NULL)
1801 xp->xp_pattern = arg;
1802 else
1803 xp->xp_pattern = p + 1;
1804
1805 xp->xp_context = EXPAND_TERMINALOPT;
1806 return NULL;
1807}
1808#endif
1809
1810/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001811 * Set the completion context for the :filter command. Returns a pointer to the
1812 * next command after the :filter command.
1813 */
1814 static char_u *
1815set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1816{
1817 if (*arg != NUL)
1818 arg = skip_vimgrep_pat(arg, NULL, NULL);
1819 if (arg == NULL || *arg == NUL)
1820 {
1821 xp->xp_context = EXPAND_NOTHING;
1822 return NULL;
1823 }
1824 return skipwhite(arg);
1825}
1826
1827#ifdef FEAT_SEARCH_EXTRA
1828/*
1829 * Set the completion context for the :match command. Returns a pointer to the
1830 * next command after the :match command.
1831 */
1832 static char_u *
1833set_context_in_match_cmd(expand_T *xp, char_u *arg)
1834{
1835 if (*arg == NUL || !ends_excmd(*arg))
1836 {
1837 // also complete "None"
1838 set_context_in_echohl_cmd(xp, arg);
1839 arg = skipwhite(skiptowhite(arg));
1840 if (*arg != NUL)
1841 {
1842 xp->xp_context = EXPAND_NOTHING;
1843 arg = skip_regexp(arg + 1, *arg, magic_isset());
1844 }
1845 }
1846 return find_nextcmd(arg);
1847}
1848#endif
1849
1850/*
1851 * Returns a pointer to the next command after a :global or a :v command.
1852 * Returns NULL if there is no next command.
1853 */
1854 static char_u *
1855find_cmd_after_global_cmd(char_u *arg)
1856{
1857 int delim;
1858
1859 delim = *arg; // get the delimiter
1860 if (delim)
1861 ++arg; // skip delimiter if there is one
1862
1863 while (arg[0] != NUL && arg[0] != delim)
1864 {
1865 if (arg[0] == '\\' && arg[1] != NUL)
1866 ++arg;
1867 ++arg;
1868 }
1869 if (arg[0] != NUL)
1870 return arg + 1;
1871
1872 return NULL;
1873}
1874
1875/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001876 * Returns a pointer to the next command after a :substitute or a :& command.
1877 * Returns NULL if there is no next command.
1878 */
1879 static char_u *
1880find_cmd_after_substitute_cmd(char_u *arg)
1881{
1882 int delim;
1883
1884 delim = *arg;
1885 if (delim)
1886 {
1887 // skip "from" part
1888 ++arg;
1889 arg = skip_regexp(arg, delim, magic_isset());
1890
1891 if (arg[0] != NUL && arg[0] == delim)
1892 {
1893 // skip "to" part
1894 ++arg;
1895 while (arg[0] != NUL && arg[0] != delim)
1896 {
1897 if (arg[0] == '\\' && arg[1] != NUL)
1898 ++arg;
1899 ++arg;
1900 }
1901 if (arg[0] != NUL) // skip delimiter
1902 ++arg;
1903 }
1904 }
1905 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1906 ++arg;
1907 if (arg[0] != NUL)
1908 return arg;
1909
1910 return NULL;
1911}
1912
1913/*
1914 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1915 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1916 * Returns NULL if there is no next command.
1917 */
1918 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001919find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001920{
1921 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001922 if (*arg != '/')
1923 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001924
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001925 // Match regexp, not just whole words
1926 for (++arg; *arg && *arg != '/'; arg++)
1927 if (*arg == '\\' && arg[1] != NUL)
1928 arg++;
1929 if (*arg)
1930 {
1931 arg = skipwhite(arg + 1);
1932
1933 // Check for trailing illegal characters
1934 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1935 xp->xp_context = EXPAND_NOTHING;
1936 else
1937 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001938 }
1939
1940 return NULL;
1941}
1942
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001943#ifdef FEAT_EVAL
1944/*
1945 * Set the completion context for the :unlet command. Always returns NULL.
1946 */
1947 static char_u *
1948set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1949{
1950 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1951 arg = xp->xp_pattern + 1;
1952
1953 xp->xp_context = EXPAND_USER_VARS;
1954 xp->xp_pattern = arg;
1955
1956 if (*xp->xp_pattern == '$')
1957 {
1958 xp->xp_context = EXPAND_ENV_VARS;
1959 ++xp->xp_pattern;
1960 }
1961
1962 return NULL;
1963}
1964#endif
1965
1966#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1967/*
1968 * Set the completion context for the :language command. Always returns NULL.
1969 */
1970 static char_u *
1971set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1972{
1973 char_u *p;
1974
1975 p = skiptowhite(arg);
1976 if (*p == NUL)
1977 {
1978 xp->xp_context = EXPAND_LANGUAGE;
1979 xp->xp_pattern = arg;
1980 }
1981 else
1982 {
1983 if ( STRNCMP(arg, "messages", p - arg) == 0
1984 || STRNCMP(arg, "ctype", p - arg) == 0
1985 || STRNCMP(arg, "time", p - arg) == 0
1986 || STRNCMP(arg, "collate", p - arg) == 0)
1987 {
1988 xp->xp_context = EXPAND_LOCALES;
1989 xp->xp_pattern = skipwhite(p);
1990 }
1991 else
1992 xp->xp_context = EXPAND_NOTHING;
1993 }
1994
1995 return NULL;
1996}
1997#endif
1998
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001999#ifdef FEAT_EVAL
2000static enum
2001{
2002 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002003 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2004 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002005} breakpt_expand_what;
2006
2007/*
2008 * Set the completion context for the :breakadd command. Always returns NULL.
2009 */
2010 static char_u *
2011set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2012{
2013 char_u *p;
2014 char_u *subcmd_start;
2015
2016 xp->xp_context = EXPAND_BREAKPOINT;
2017 xp->xp_pattern = arg;
2018
2019 if (cmdidx == CMD_breakadd)
2020 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002021 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002022 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002023 else
2024 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002025
2026 p = skipwhite(arg);
2027 if (*p == NUL)
2028 return NULL;
2029 subcmd_start = p;
2030
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002031 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002032 {
2033 // :breakadd file [lnum] <filename>
2034 // :breakadd func [lnum] <funcname>
2035 p += 4;
2036 p = skipwhite(p);
2037
2038 // skip line number (if specified)
2039 if (VIM_ISDIGIT(*p))
2040 {
2041 p = skipdigits(p);
2042 if (*p != ' ')
2043 {
2044 xp->xp_context = EXPAND_NOTHING;
2045 return NULL;
2046 }
2047 p = skipwhite(p);
2048 }
2049 if (STRNCMP("file", subcmd_start, 4) == 0)
2050 xp->xp_context = EXPAND_FILES;
2051 else
2052 xp->xp_context = EXPAND_USER_FUNC;
2053 xp->xp_pattern = p;
2054 }
2055 else if (STRNCMP("expr ", p, 5) == 0)
2056 {
2057 // :breakadd expr <expression>
2058 xp->xp_context = EXPAND_EXPRESSION;
2059 xp->xp_pattern = skipwhite(p + 5);
2060 }
2061
2062 return NULL;
2063}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002064
2065 static char_u *
2066set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2067{
2068 char_u *p;
2069
2070 xp->xp_context = EXPAND_NOTHING;
2071 xp->xp_pattern = NULL;
2072
2073 p = skipwhite(arg);
2074 if (VIM_ISDIGIT(*p))
2075 return NULL;
2076
2077 xp->xp_context = EXPAND_SCRIPTNAMES;
2078 xp->xp_pattern = p;
2079
2080 return NULL;
2081}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002082#endif
2083
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002084/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002085 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2086 * The argument to the command is 'arg' and the argument flags is 'argt'.
2087 * For user-defined commands and for environment variables, 'compl' has the
2088 * completion type.
2089 * Returns a pointer to the next command. Returns NULL if there is no next
2090 * command.
2091 */
2092 static char_u *
2093set_context_by_cmdname(
2094 char_u *cmd,
2095 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002096 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002097 char_u *arg,
2098 long argt,
2099 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002100 int forceit)
2101{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002102 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002103 {
2104 case CMD_find:
2105 case CMD_sfind:
2106 case CMD_tabfind:
2107 if (xp->xp_context == EXPAND_FILES)
2108 xp->xp_context = EXPAND_FILES_IN_PATH;
2109 break;
2110 case CMD_cd:
2111 case CMD_chdir:
2112 case CMD_tcd:
2113 case CMD_tchdir:
2114 case CMD_lcd:
2115 case CMD_lchdir:
2116 if (xp->xp_context == EXPAND_FILES)
2117 xp->xp_context = EXPAND_DIRECTORIES;
2118 break;
2119 case CMD_help:
2120 xp->xp_context = EXPAND_HELP;
2121 xp->xp_pattern = arg;
2122 break;
2123
2124 // Command modifiers: return the argument.
2125 // Also for commands with an argument that is a command.
2126 case CMD_aboveleft:
2127 case CMD_argdo:
2128 case CMD_belowright:
2129 case CMD_botright:
2130 case CMD_browse:
2131 case CMD_bufdo:
2132 case CMD_cdo:
2133 case CMD_cfdo:
2134 case CMD_confirm:
2135 case CMD_debug:
2136 case CMD_folddoclosed:
2137 case CMD_folddoopen:
2138 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002139 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002140 case CMD_keepalt:
2141 case CMD_keepjumps:
2142 case CMD_keepmarks:
2143 case CMD_keeppatterns:
2144 case CMD_ldo:
2145 case CMD_leftabove:
2146 case CMD_lfdo:
2147 case CMD_lockmarks:
2148 case CMD_noautocmd:
2149 case CMD_noswapfile:
2150 case CMD_rightbelow:
2151 case CMD_sandbox:
2152 case CMD_silent:
2153 case CMD_tab:
2154 case CMD_tabdo:
2155 case CMD_topleft:
2156 case CMD_verbose:
2157 case CMD_vertical:
2158 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002159 case CMD_vim9cmd:
2160 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002161 return arg;
2162
2163 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002164 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002165
2166#ifdef FEAT_SEARCH_EXTRA
2167 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002168 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002169#endif
2170
2171 // All completion for the +cmdline_compl feature goes here.
2172
2173 case CMD_command:
2174 return set_context_in_user_cmd(xp, arg);
2175
2176 case CMD_delcommand:
2177 xp->xp_context = EXPAND_USER_COMMANDS;
2178 xp->xp_pattern = arg;
2179 break;
2180
2181 case CMD_global:
2182 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002183 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002184 case CMD_and:
2185 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002186 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002187 case CMD_isearch:
2188 case CMD_dsearch:
2189 case CMD_ilist:
2190 case CMD_dlist:
2191 case CMD_ijump:
2192 case CMD_psearch:
2193 case CMD_djump:
2194 case CMD_isplit:
2195 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002196 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002197 case CMD_autocmd:
2198 return set_context_in_autocmd(xp, arg, FALSE);
2199 case CMD_doautocmd:
2200 case CMD_doautoall:
2201 return set_context_in_autocmd(xp, arg, TRUE);
2202 case CMD_set:
2203 set_context_in_set_cmd(xp, arg, 0);
2204 break;
2205 case CMD_setglobal:
2206 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2207 break;
2208 case CMD_setlocal:
2209 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2210 break;
2211 case CMD_tag:
2212 case CMD_stag:
2213 case CMD_ptag:
2214 case CMD_ltag:
2215 case CMD_tselect:
2216 case CMD_stselect:
2217 case CMD_ptselect:
2218 case CMD_tjump:
2219 case CMD_stjump:
2220 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002221 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002222 xp->xp_context = EXPAND_TAGS_LISTFILES;
2223 else
2224 xp->xp_context = EXPAND_TAGS;
2225 xp->xp_pattern = arg;
2226 break;
2227 case CMD_augroup:
2228 xp->xp_context = EXPAND_AUGROUP;
2229 xp->xp_pattern = arg;
2230 break;
2231#ifdef FEAT_SYN_HL
2232 case CMD_syntax:
2233 set_context_in_syntax_cmd(xp, arg);
2234 break;
2235#endif
2236#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002237 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002238 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002239 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002240 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002241 case CMD_if:
2242 case CMD_elseif:
2243 case CMD_while:
2244 case CMD_for:
2245 case CMD_echo:
2246 case CMD_echon:
2247 case CMD_execute:
2248 case CMD_echomsg:
2249 case CMD_echoerr:
2250 case CMD_call:
2251 case CMD_return:
2252 case CMD_cexpr:
2253 case CMD_caddexpr:
2254 case CMD_cgetexpr:
2255 case CMD_lexpr:
2256 case CMD_laddexpr:
2257 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002258 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002259 break;
2260
2261 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002262 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002263 case CMD_function:
2264 case CMD_delfunction:
2265 xp->xp_context = EXPAND_USER_FUNC;
2266 xp->xp_pattern = arg;
2267 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002268 case CMD_disassemble:
2269 set_context_in_disassemble_cmd(xp, arg);
2270 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002271
2272 case CMD_echohl:
2273 set_context_in_echohl_cmd(xp, arg);
2274 break;
2275#endif
2276 case CMD_highlight:
2277 set_context_in_highlight_cmd(xp, arg);
2278 break;
2279#ifdef FEAT_CSCOPE
2280 case CMD_cscope:
2281 case CMD_lcscope:
2282 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002283 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002284 break;
2285#endif
2286#ifdef FEAT_SIGNS
2287 case CMD_sign:
2288 set_context_in_sign_cmd(xp, arg);
2289 break;
2290#endif
2291 case CMD_bdelete:
2292 case CMD_bwipeout:
2293 case CMD_bunload:
2294 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2295 arg = xp->xp_pattern + 1;
2296 // FALLTHROUGH
2297 case CMD_buffer:
2298 case CMD_sbuffer:
2299 case CMD_checktime:
2300 xp->xp_context = EXPAND_BUFFERS;
2301 xp->xp_pattern = arg;
2302 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002303#ifdef FEAT_DIFF
2304 case CMD_diffget:
2305 case CMD_diffput:
2306 // If current buffer is in diff mode, complete buffer names
2307 // which are in diff mode, and different than current buffer.
2308 xp->xp_context = EXPAND_DIFF_BUFFERS;
2309 xp->xp_pattern = arg;
2310 break;
2311#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002312 case CMD_USER:
2313 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002314 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2315 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002316
2317 case CMD_map: case CMD_noremap:
2318 case CMD_nmap: case CMD_nnoremap:
2319 case CMD_vmap: case CMD_vnoremap:
2320 case CMD_omap: case CMD_onoremap:
2321 case CMD_imap: case CMD_inoremap:
2322 case CMD_cmap: case CMD_cnoremap:
2323 case CMD_lmap: case CMD_lnoremap:
2324 case CMD_smap: case CMD_snoremap:
2325 case CMD_tmap: case CMD_tnoremap:
2326 case CMD_xmap: case CMD_xnoremap:
2327 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002328 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002329 case CMD_unmap:
2330 case CMD_nunmap:
2331 case CMD_vunmap:
2332 case CMD_ounmap:
2333 case CMD_iunmap:
2334 case CMD_cunmap:
2335 case CMD_lunmap:
2336 case CMD_sunmap:
2337 case CMD_tunmap:
2338 case CMD_xunmap:
2339 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002340 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002341 case CMD_mapclear:
2342 case CMD_nmapclear:
2343 case CMD_vmapclear:
2344 case CMD_omapclear:
2345 case CMD_imapclear:
2346 case CMD_cmapclear:
2347 case CMD_lmapclear:
2348 case CMD_smapclear:
2349 case CMD_tmapclear:
2350 case CMD_xmapclear:
2351 xp->xp_context = EXPAND_MAPCLEAR;
2352 xp->xp_pattern = arg;
2353 break;
2354
2355 case CMD_abbreviate: case CMD_noreabbrev:
2356 case CMD_cabbrev: case CMD_cnoreabbrev:
2357 case CMD_iabbrev: case CMD_inoreabbrev:
2358 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002359 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002360 case CMD_unabbreviate:
2361 case CMD_cunabbrev:
2362 case CMD_iunabbrev:
2363 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002364 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002365#ifdef FEAT_MENU
2366 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2367 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2368 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2369 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2370 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2371 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2372 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2373 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2374 case CMD_tmenu: case CMD_tunmenu:
2375 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2376 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2377#endif
2378
2379 case CMD_colorscheme:
2380 xp->xp_context = EXPAND_COLORS;
2381 xp->xp_pattern = arg;
2382 break;
2383
2384 case CMD_compiler:
2385 xp->xp_context = EXPAND_COMPILER;
2386 xp->xp_pattern = arg;
2387 break;
2388
2389 case CMD_ownsyntax:
2390 xp->xp_context = EXPAND_OWNSYNTAX;
2391 xp->xp_pattern = arg;
2392 break;
2393
2394 case CMD_setfiletype:
2395 xp->xp_context = EXPAND_FILETYPE;
2396 xp->xp_pattern = arg;
2397 break;
2398
2399 case CMD_packadd:
2400 xp->xp_context = EXPAND_PACKADD;
2401 xp->xp_pattern = arg;
2402 break;
2403
zeertzjqb0d45ec2023-01-25 15:04:22 +00002404 case CMD_runtime:
2405 set_context_in_runtime_cmd(xp, arg);
2406 break;
2407
Bram Moolenaard0190392019-08-23 21:17:35 +02002408#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2409 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002410 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002411#endif
2412#if defined(FEAT_PROFILE)
2413 case CMD_profile:
2414 set_context_in_profile_cmd(xp, arg);
2415 break;
2416#endif
2417 case CMD_behave:
2418 xp->xp_context = EXPAND_BEHAVE;
2419 xp->xp_pattern = arg;
2420 break;
2421
2422 case CMD_messages:
2423 xp->xp_context = EXPAND_MESSAGES;
2424 xp->xp_pattern = arg;
2425 break;
2426
2427 case CMD_history:
2428 xp->xp_context = EXPAND_HISTORY;
2429 xp->xp_pattern = arg;
2430 break;
2431#if defined(FEAT_PROFILE)
2432 case CMD_syntime:
2433 xp->xp_context = EXPAND_SYNTIME;
2434 xp->xp_pattern = arg;
2435 break;
2436#endif
2437
2438 case CMD_argdelete:
2439 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2440 arg = xp->xp_pattern + 1;
2441 xp->xp_context = EXPAND_ARGLIST;
2442 xp->xp_pattern = arg;
2443 break;
2444
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002445#ifdef FEAT_EVAL
2446 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002447 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002448 case CMD_breakdel:
2449 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002450
2451 case CMD_scriptnames:
2452 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002453#endif
2454
Bram Moolenaard0190392019-08-23 21:17:35 +02002455 default:
2456 break;
2457 }
2458 return NULL;
2459}
2460
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002461/*
2462 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2463 * we don't need/want deleted. Maybe this could be done better if we didn't
2464 * repeat all this stuff. The only problem is that they may not stay
2465 * perfectly compatible with each other, but then the command line syntax
2466 * probably won't change that much -- webb.
2467 */
2468 static char_u *
2469set_one_cmd_context(
2470 expand_T *xp,
2471 char_u *buff) // buffer for command string
2472{
2473 char_u *p;
2474 char_u *cmd, *arg;
2475 int len = 0;
2476 exarg_T ea;
2477 int compl = EXPAND_NOTHING;
2478 int forceit = FALSE;
2479 int usefilter = FALSE; // filter instead of file name
2480
2481 ExpandInit(xp);
2482 xp->xp_pattern = buff;
2483 xp->xp_line = buff;
2484 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2485 ea.argt = 0;
2486
2487 // 1. skip comment lines and leading space, colons or bars
2488 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2489 ;
2490 xp->xp_pattern = cmd;
2491
2492 if (*cmd == NUL)
2493 return NULL;
2494 if (*cmd == '"') // ignore comment lines
2495 {
2496 xp->xp_context = EXPAND_NOTHING;
2497 return NULL;
2498 }
2499
2500 // 3. Skip over the range to find the command.
2501 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2502 xp->xp_pattern = cmd;
2503 if (*cmd == NUL)
2504 return NULL;
2505 if (*cmd == '"')
2506 {
2507 xp->xp_context = EXPAND_NOTHING;
2508 return NULL;
2509 }
2510
2511 if (*cmd == '|' || *cmd == '\n')
2512 return cmd + 1; // There's another command
2513
2514 // Get the command index.
2515 p = set_cmd_index(cmd, &ea, xp, &compl);
2516 if (p == NULL)
2517 return NULL;
2518
2519 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2520
2521 if (*p == '!') // forced commands
2522 {
2523 forceit = TRUE;
2524 ++p;
2525 }
2526
2527 // 6. parse arguments
2528 if (!IS_USER_CMDIDX(ea.cmdidx))
2529 ea.argt = excmd_get_argt(ea.cmdidx);
2530
2531 arg = skipwhite(p);
2532
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002533 // Does command allow "++argopt" argument?
2534 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002535 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002536 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2537 {
2538 p = arg + 2;
2539 while (*p && !vim_isspace(*p))
2540 MB_PTR_ADV(p);
2541
2542 // Still touching the command after "++"?
2543 if (*p == NUL)
2544 {
2545 if (ea.argt & EX_ARGOPT)
2546 return set_context_in_argopt(xp, arg + 2);
2547#ifdef FEAT_TERMINAL
2548 if (ea.cmdidx == CMD_terminal)
2549 return set_context_in_terminalopt(xp, arg + 2);
2550#endif
2551 }
2552
2553 arg = skipwhite(p);
2554 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002555 }
2556
2557 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2558 {
2559 if (*arg == '>') // append
2560 {
2561 if (*++arg == '>')
2562 ++arg;
2563 arg = skipwhite(arg);
2564 }
2565 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2566 {
2567 ++arg;
2568 usefilter = TRUE;
2569 }
2570 }
2571
2572 if (ea.cmdidx == CMD_read)
2573 {
2574 usefilter = forceit; // :r! filter if forced
2575 if (*arg == '!') // :r !filter
2576 {
2577 ++arg;
2578 usefilter = TRUE;
2579 }
2580 }
2581
2582 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2583 {
2584 while (*arg == *cmd) // allow any number of '>' or '<'
2585 ++arg;
2586 arg = skipwhite(arg);
2587 }
2588
2589 // Does command allow "+command"?
2590 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2591 {
2592 // Check if we're in the +command
2593 p = arg + 1;
2594 arg = skip_cmd_arg(arg, FALSE);
2595
2596 // Still touching the command after '+'?
2597 if (*arg == NUL)
2598 return p;
2599
2600 // Skip space(s) after +command to get to the real argument
2601 arg = skipwhite(arg);
2602 }
2603
2604
2605 // Check for '|' to separate commands and '"' to start comments.
2606 // Don't do this for ":read !cmd" and ":write !cmd".
2607 if ((ea.argt & EX_TRLBAR) && !usefilter)
2608 {
2609 p = arg;
2610 // ":redir @" is not the start of a comment
2611 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2612 p += 2;
2613 while (*p)
2614 {
2615 if (*p == Ctrl_V)
2616 {
2617 if (p[1] != NUL)
2618 ++p;
2619 }
2620 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2621 || *p == '|' || *p == '\n')
2622 {
2623 if (*(p - 1) != '\\')
2624 {
2625 if (*p == '|' || *p == '\n')
2626 return p + 1;
2627 return NULL; // It's a comment
2628 }
2629 }
2630 MB_PTR_ADV(p);
2631 }
2632 }
2633
2634 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2635 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2636 // no arguments allowed but there is something
2637 return NULL;
2638
2639 // Find start of last argument (argument just before cursor):
2640 p = buff;
2641 xp->xp_pattern = p;
2642 len = (int)STRLEN(buff);
2643 while (*p && p < buff + len)
2644 {
2645 if (*p == ' ' || *p == TAB)
2646 {
2647 // argument starts after a space
2648 xp->xp_pattern = ++p;
2649 }
2650 else
2651 {
2652 if (*p == '\\' && *(p + 1) != NUL)
2653 ++p; // skip over escaped character
2654 MB_PTR_ADV(p);
2655 }
2656 }
2657
2658 if (ea.argt & EX_XFILE)
2659 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2660
2661 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002662 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002663 forceit);
2664}
2665
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002666/*
2667 * Set the completion context in 'xp' for command 'str'
2668 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002669 void
2670set_cmd_context(
2671 expand_T *xp,
2672 char_u *str, // start of command line
2673 int len, // length of command line (excl. NUL)
2674 int col, // position of cursor
2675 int use_ccline UNUSED) // use ccline for info
2676{
2677#ifdef FEAT_EVAL
2678 cmdline_info_T *ccline = get_cmdline_info();
2679#endif
2680 int old_char = NUL;
2681 char_u *nextcomm;
2682
2683 // Avoid a UMR warning from Purify, only save the character if it has been
2684 // written before.
2685 if (col < len)
2686 old_char = str[col];
2687 str[col] = NUL;
2688 nextcomm = str;
2689
2690#ifdef FEAT_EVAL
2691 if (use_ccline && ccline->cmdfirstc == '=')
2692 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002693 // pass CMD_SIZE because there is no real command
2694 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002695 }
2696 else if (use_ccline && ccline->input_fn)
2697 {
2698 xp->xp_context = ccline->xp_context;
2699 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002700 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002701 }
2702 else
2703#endif
2704 while (nextcomm != NULL)
2705 nextcomm = set_one_cmd_context(xp, nextcomm);
2706
2707 // Store the string here so that call_user_expand_func() can get to them
2708 // easily.
2709 xp->xp_line = str;
2710 xp->xp_col = col;
2711
2712 str[col] = old_char;
2713}
2714
2715/*
2716 * Expand the command line "str" from context "xp".
2717 * "xp" must have been set by set_cmd_context().
2718 * xp->xp_pattern points into "str", to where the text that is to be expanded
2719 * starts.
2720 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2721 * cursor.
2722 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2723 * key that triggered expansion literally.
2724 * Returns EXPAND_OK otherwise.
2725 */
2726 int
2727expand_cmdline(
2728 expand_T *xp,
2729 char_u *str, // start of command line
2730 int col, // position of cursor
2731 int *matchcount, // return: nr of matches
2732 char_u ***matches) // return: array of pointers to matches
2733{
2734 char_u *file_str = NULL;
2735 int options = WILD_ADD_SLASH|WILD_SILENT;
2736
2737 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2738 {
2739 beep_flush();
2740 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2741 }
2742 if (xp->xp_context == EXPAND_NOTHING)
2743 {
2744 // Caller can use the character as a normal char instead
2745 return EXPAND_NOTHING;
2746 }
2747
2748 // add star to file name, or convert to regexp if not exp. files.
2749 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002750 if (cmdline_fuzzy_completion_supported(xp))
2751 // If fuzzy matching, don't modify the search string
2752 file_str = vim_strsave(xp->xp_pattern);
2753 else
2754 {
2755 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2756 if (file_str == NULL)
2757 return EXPAND_UNSUCCESSFUL;
2758 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002759
2760 if (p_wic)
2761 options += WILD_ICASE;
2762
2763 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002764 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002765 {
2766 *matchcount = 0;
2767 *matches = NULL;
2768 }
2769 vim_free(file_str);
2770
2771 return EXPAND_OK;
2772}
2773
Bram Moolenaar66b51422019-08-18 21:44:12 +02002774/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002775 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002776 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002777 */
2778 static int
2779expand_files_and_dirs(
2780 expand_T *xp,
2781 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002782 char_u ***matches,
2783 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002784 int flags,
2785 int options)
2786{
2787 int free_pat = FALSE;
2788 int i;
2789 int ret;
2790
2791 // for ":set path=" and ":set tags=" halve backslashes for escaped
2792 // space
2793 if (xp->xp_backslash != XP_BS_NONE)
2794 {
2795 free_pat = TRUE;
2796 pat = vim_strsave(pat);
2797 for (i = 0; pat[i]; ++i)
2798 if (pat[i] == '\\')
2799 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002800 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002801 && pat[i + 1] == '\\'
2802 && pat[i + 2] == '\\'
2803 && pat[i + 3] == ' ')
2804 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002805 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002806 && pat[i + 1] == ' ')
2807 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002808 else if ((xp->xp_backslash & XP_BS_COMMA)
2809 && pat[i + 1] == '\\'
2810 && pat[i + 2] == ',')
2811 STRMOVE(pat + i, pat + i + 2);
2812#ifdef BACKSLASH_IN_FILENAME
2813 else if ((xp->xp_backslash & XP_BS_COMMA)
2814 && pat[i + 1] == ',')
2815 STRMOVE(pat + i, pat + i + 1);
2816#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002817 }
2818 }
2819
2820 if (xp->xp_context == EXPAND_FILES)
2821 flags |= EW_FILE;
2822 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2823 flags |= (EW_FILE | EW_PATH);
2824 else
2825 flags = (flags | EW_DIR) & ~EW_FILE;
2826 if (options & WILD_ICASE)
2827 flags |= EW_ICASE;
2828
2829 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002830 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002831 if (free_pat)
2832 vim_free(pat);
2833#ifdef BACKSLASH_IN_FILENAME
2834 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2835 {
2836 int j;
2837
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002838 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002839 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002840 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002841
2842 while (*ptr != NUL)
2843 {
2844 if (p_csl[0] == 's' && *ptr == '\\')
2845 *ptr = '/';
2846 else if (p_csl[0] == 'b' && *ptr == '/')
2847 *ptr = '\\';
2848 ptr += (*mb_ptr2len)(ptr);
2849 }
2850 }
2851 }
2852#endif
2853 return ret;
2854}
2855
2856/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002857 * Function given to ExpandGeneric() to obtain the possible arguments of the
2858 * ":behave {mswin,xterm}" command.
2859 */
2860 static char_u *
2861get_behave_arg(expand_T *xp UNUSED, int idx)
2862{
2863 if (idx == 0)
2864 return (char_u *)"mswin";
2865 if (idx == 1)
2866 return (char_u *)"xterm";
2867 return NULL;
2868}
2869
K.Takata161b6ac2022-11-14 15:31:07 +00002870#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002871/*
2872 * Function given to ExpandGeneric() to obtain the possible arguments of the
2873 * ":breakadd {expr, file, func, here}" command.
2874 * ":breakdel {func, file, here}" command.
2875 */
2876 static char_u *
2877get_breakadd_arg(expand_T *xp UNUSED, int idx)
2878{
2879 char *opts[] = {"expr", "file", "func", "here"};
2880
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002881 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002882 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002883 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002884 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2885 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002886 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2887 {
2888 // breakdel {func, file, here}
2889 if (idx <= 2)
2890 return (char_u *)opts[idx + 1];
2891 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002892 else
2893 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002894 // profdel {func, file}
2895 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002896 return (char_u *)opts[idx + 1];
2897 }
2898 }
2899 return NULL;
2900}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002901
2902/*
2903 * Function given to ExpandGeneric() to obtain the possible arguments for the
2904 * ":scriptnames" command.
2905 */
2906 static char_u *
2907get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2908{
2909 scriptitem_T *si;
2910
2911 if (!SCRIPT_ID_VALID(idx + 1))
2912 return NULL;
2913
2914 si = SCRIPT_ITEM(idx + 1);
2915 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2916 return NameBuff;
2917}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002918#endif
2919
Bram Moolenaard0190392019-08-23 21:17:35 +02002920/*
2921 * Function given to ExpandGeneric() to obtain the possible arguments of the
2922 * ":messages {clear}" command.
2923 */
2924 static char_u *
2925get_messages_arg(expand_T *xp UNUSED, int idx)
2926{
2927 if (idx == 0)
2928 return (char_u *)"clear";
2929 return NULL;
2930}
2931
2932 static char_u *
2933get_mapclear_arg(expand_T *xp UNUSED, int idx)
2934{
2935 if (idx == 0)
2936 return (char_u *)"<buffer>";
2937 return NULL;
2938}
2939
2940/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002941 * Do the expansion based on xp->xp_context and 'rmp'.
2942 */
2943 static int
2944ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002945 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002946 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002947 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002948 char_u ***matches,
2949 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002950{
2951 static struct expgen
2952 {
2953 int context;
2954 char_u *((*func)(expand_T *, int));
2955 int ic;
2956 int escaped;
2957 } tab[] =
2958 {
2959 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2960 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2961 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2962 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2963 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2964 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2965 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2966 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2967 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2968 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002969#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002970 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2971 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2972 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2973 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2974 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002975#endif
2976#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002977 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2978 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002979#endif
2980#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002981 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002982#endif
2983#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002984 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002985#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002986 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2987 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2988 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002989#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002990 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002991#endif
2992#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002993 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002994#endif
2995#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002996 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002997#endif
2998#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002999 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3000 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003001#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003002 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3003 {EXPAND_USER, get_users, TRUE, FALSE},
3004 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003005#ifdef FEAT_EVAL
3006 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003007 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003008#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003009 };
3010 int i;
3011 int ret = FAIL;
3012
3013 // Find a context in the table and call the ExpandGeneric() with the
3014 // right function to do the expansion.
3015 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3016 {
3017 if (xp->xp_context == tab[i].context)
3018 {
3019 if (tab[i].ic)
3020 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003021 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3022 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003023 break;
3024 }
3025 }
3026
3027 return ret;
3028}
3029
3030/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003031 * Map wild expand options to flags for expand_wildcards()
3032 */
3033 static int
3034map_wildopts_to_ewflags(int options)
3035{
3036 int flags;
3037
3038 flags = EW_DIR; // include directories
3039 if (options & WILD_LIST_NOTFOUND)
3040 flags |= EW_NOTFOUND;
3041 if (options & WILD_ADD_SLASH)
3042 flags |= EW_ADDSLASH;
3043 if (options & WILD_KEEP_ALL)
3044 flags |= EW_KEEPALL;
3045 if (options & WILD_SILENT)
3046 flags |= EW_SILENT;
3047 if (options & WILD_NOERROR)
3048 flags |= EW_NOERROR;
3049 if (options & WILD_ALLLINKS)
3050 flags |= EW_ALLLINKS;
3051
3052 return flags;
3053}
3054
3055/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003056 * Do the expansion based on xp->xp_context and "pat".
3057 */
3058 static int
3059ExpandFromContext(
3060 expand_T *xp,
3061 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003062 char_u ***matches,
3063 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003064 int options) // WILD_ flags
3065{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003066 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003067 int ret;
3068 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003069 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003070 int fuzzy = cmdline_fuzzy_complete(pat)
3071 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003072
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003073 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003074
3075 if (xp->xp_context == EXPAND_FILES
3076 || xp->xp_context == EXPAND_DIRECTORIES
3077 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003078 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3079 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003080
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003081 *matches = (char_u **)"";
3082 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003083 if (xp->xp_context == EXPAND_HELP)
3084 {
3085 // With an empty argument we would get all the help tags, which is
3086 // very slow. Get matches for "help" instead.
3087 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003088 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003089 {
3090#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003091 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003092#endif
3093 return OK;
3094 }
3095 return FAIL;
3096 }
3097
Bram Moolenaar66b51422019-08-18 21:44:12 +02003098 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003099 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003100 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003101 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003103 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003104#ifdef FEAT_DIFF
3105 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003106 return ExpandBufnames(pat, numMatches, matches,
3107 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003108#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003109 if (xp->xp_context == EXPAND_TAGS
3110 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003111 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3112 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113 if (xp->xp_context == EXPAND_COLORS)
3114 {
3115 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003116 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003117 directories);
3118 }
3119 if (xp->xp_context == EXPAND_COMPILER)
3120 {
3121 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003122 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003123 }
3124 if (xp->xp_context == EXPAND_OWNSYNTAX)
3125 {
3126 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003127 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128 }
3129 if (xp->xp_context == EXPAND_FILETYPE)
3130 {
3131 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003132 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133 }
K.Takata161b6ac2022-11-14 15:31:07 +00003134#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003135 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003136 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003137#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003139 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003140 if (xp->xp_context == EXPAND_RUNTIME)
3141 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003142
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003143 // When expanding a function name starting with s:, match the <SNR>nr_
3144 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003145 if ((xp->xp_context == EXPAND_USER_FUNC
3146 || xp->xp_context == EXPAND_DISASSEMBLE)
3147 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003148 {
3149 int len = (int)STRLEN(pat) + 20;
3150
3151 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003152 if (tofree == NULL)
3153 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003154 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003155 pat = tofree;
3156 }
3157
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003158 if (!fuzzy)
3159 {
3160 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3161 if (regmatch.regprog == NULL)
3162 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003163
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003164 // set ignore-case according to p_ic, p_scs and pat
3165 regmatch.rm_ic = ignorecase(pat);
3166 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003167
3168 if (xp->xp_context == EXPAND_SETTINGS
3169 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003170 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003171 else if (xp->xp_context == EXPAND_STRING_SETTING)
3172 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3173 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3174 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003175 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003176 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003177 else if (xp->xp_context == EXPAND_ARGOPT)
3178 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3179#if defined(FEAT_TERMINAL)
3180 else if (xp->xp_context == EXPAND_TERMINALOPT)
3181 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3182#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003183#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003184 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003185 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003186#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003187 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003188 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003189
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003190 if (!fuzzy)
3191 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003192 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193
3194 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003195}
3196
Bram Moolenaar66b51422019-08-18 21:44:12 +02003197/*
3198 * Expand a list of names.
3199 *
3200 * Generic function for command line completion. It calls a function to
3201 * obtain strings, one by one. The strings are matched against a regexp
3202 * program. Matching strings are copied into an array, which is returned.
3203 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003204 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3205 * is used.
3206 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003207 * Returns OK when no problems encountered, FAIL for error (out of memory).
3208 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003209 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003210ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003211 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003212 expand_T *xp,
3213 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003214 char_u ***matches,
3215 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003216 char_u *((*func)(expand_T *, int)),
3217 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003218 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003219{
3220 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003221 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003222 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003223 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003224 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003225 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003226 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003227 int sort_matches = FALSE;
3228 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003229
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003230 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003231 *matches = NULL;
3232 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003233
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003234 if (!fuzzy)
3235 ga_init2(&ga, sizeof(char *), 30);
3236 else
3237 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3238
3239 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003240 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003241 str = (*func)(xp, i);
3242 if (str == NULL) // end of list
3243 break;
3244 if (*str == NUL) // skip empty strings
3245 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003247 if (xp->xp_pattern[0] != NUL)
3248 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003249 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003250 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003251 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003252 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003253 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003254 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003255 }
3256 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003257 else
3258 match = TRUE;
3259
3260 if (!match)
3261 continue;
3262
3263 if (escaped)
3264 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3265 else
3266 str = vim_strsave(str);
3267 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003268 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003269 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003270 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003271 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003272 return FAIL;
3273 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003274 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003275 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003276 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003277
3278 if (ga_grow(&ga, 1) == FAIL)
3279 {
3280 vim_free(str);
3281 break;
3282 }
3283
3284 if (fuzzy)
3285 {
3286 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3287 fuzmatch->idx = ga.ga_len;
3288 fuzmatch->str = str;
3289 fuzmatch->score = score;
3290 }
3291 else
3292 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3293
K.Takata161b6ac2022-11-14 15:31:07 +00003294#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003295 if (func == get_menu_names)
3296 {
3297 // test for separator added by get_menu_names()
3298 str += STRLEN(str) - 1;
3299 if (*str == '\001')
3300 *str = '.';
3301 }
K.Takata161b6ac2022-11-14 15:31:07 +00003302#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003303
3304 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003305 }
3306
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003307 if (ga.ga_len == 0)
3308 return OK;
3309
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003310 // sort the matches when using regular expression matching and sorting
3311 // applies to the completion context. Menus and scriptnames should be kept
3312 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003313 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003314 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003315 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003316 && xp->xp_context != EXPAND_SCRIPTNAMES
3317 && xp->xp_context != EXPAND_ARGOPT
3318 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003319 sort_matches = TRUE;
3320
3321 // <SNR> functions should be sorted to the end.
3322 if (xp->xp_context == EXPAND_EXPRESSION
3323 || xp->xp_context == EXPAND_FUNCTIONS
3324 || xp->xp_context == EXPAND_USER_FUNC
3325 || xp->xp_context == EXPAND_DISASSEMBLE)
3326 funcsort = TRUE;
3327
3328 // Sort the matches.
3329 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003330 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003331 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003332 // <SNR> functions should be sorted to the end.
3333 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3334 sort_func_compare);
3335 else
3336 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3337 }
3338
3339 if (!fuzzy)
3340 {
3341 *matches = ga.ga_data;
3342 *numMatches = ga.ga_len;
3343 }
3344 else
3345 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003346 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3347 funcsort) == FAIL)
3348 return FAIL;
3349 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 }
3351
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003352#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353 // Reset the variables used for special highlight names expansion, so that
3354 // they don't show up when getting normal highlight names by ID.
3355 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003356#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003357
Bram Moolenaar66b51422019-08-18 21:44:12 +02003358 return OK;
3359}
3360
3361/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003362 * Expand shell command matches in one directory of $PATH.
3363 */
3364 static void
3365expand_shellcmd_onedir(
3366 char_u *buf,
3367 char_u *s,
3368 size_t l,
3369 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003370 char_u ***matches,
3371 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003372 int flags,
3373 hashtab_T *ht,
3374 garray_T *gap)
3375{
3376 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003377 hash_T hash;
3378 hashitem_T *hi;
3379
3380 vim_strncpy(buf, s, l);
3381 add_pathsep(buf);
3382 l = STRLEN(buf);
3383 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3384
3385 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003386 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003387 if (ret != OK)
3388 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003389
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003390 if (ga_grow(gap, *numMatches) == FAIL)
3391 {
3392 FreeWild(*numMatches, *matches);
3393 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003394 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003395
3396 for (int i = 0; i < *numMatches; ++i)
3397 {
3398 char_u *name = (*matches)[i];
3399
3400 if (STRLEN(name) > l)
3401 {
3402 // Check if this name was already found.
3403 hash = hash_hash(name + l);
3404 hi = hash_lookup(ht, name + l, hash);
3405 if (HASHITEM_EMPTY(hi))
3406 {
3407 // Remove the path that was prepended.
3408 STRMOVE(name, name + l);
3409 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3410 hash_add_item(ht, hi, name, hash);
3411 name = NULL;
3412 }
3413 }
3414 vim_free(name);
3415 }
3416 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003417}
3418
3419/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003420 * Complete a shell command.
3421 * Returns FAIL or OK;
3422 */
3423 static int
3424expand_shellcmd(
3425 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003426 char_u ***matches, // return: array with matches
3427 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003428 int flagsarg) // EW_ flags
3429{
3430 char_u *pat;
3431 int i;
3432 char_u *path = NULL;
3433 int mustfree = FALSE;
3434 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003435 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003436 size_t l;
3437 char_u *s, *e;
3438 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003439 int did_curdir = FALSE;
3440 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003441
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003442 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003443 if (buf == NULL)
3444 return FAIL;
3445
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003446 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003447 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003448 if (pat == NULL)
3449 {
3450 vim_free(buf);
3451 return FAIL;
3452 }
3453
Bram Moolenaar66b51422019-08-18 21:44:12 +02003454 for (i = 0; pat[i]; ++i)
3455 if (pat[i] == '\\' && pat[i + 1] == ' ')
3456 STRMOVE(pat + i, pat + i + 1);
3457
3458 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3459
3460 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3461 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3462 path = (char_u *)".";
3463 else
3464 {
3465 // For an absolute name we don't use $PATH.
3466 if (!mch_isFullName(pat))
3467 path = vim_getenv((char_u *)"PATH", &mustfree);
3468 if (path == NULL)
3469 path = (char_u *)"";
3470 }
3471
3472 // Go over all directories in $PATH. Expand matches in that directory and
3473 // collect them in "ga". When "." is not in $PATH also expand for the
3474 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003475 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003476 hash_init(&found_ht);
3477 for (s = path; ; s = e)
3478 {
K.Takata161b6ac2022-11-14 15:31:07 +00003479#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003481#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003482 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003483#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003484 if (e == NULL)
3485 e = s + STRLEN(s);
3486
3487 if (*s == NUL)
3488 {
3489 if (did_curdir)
3490 break;
3491 // Find directories in the current directory, path is empty.
3492 did_curdir = TRUE;
3493 flags |= EW_DIR;
3494 }
3495 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3496 {
3497 did_curdir = TRUE;
3498 flags |= EW_DIR;
3499 }
3500 else
3501 // Do not match directories inside a $PATH item.
3502 flags &= ~EW_DIR;
3503
3504 l = e - s;
3505 if (l > MAXPATHL - 5)
3506 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003507
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003508 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003509 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003510
Bram Moolenaar66b51422019-08-18 21:44:12 +02003511 if (*e != NUL)
3512 ++e;
3513 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003514 *matches = ga.ga_data;
3515 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003516
3517 vim_free(buf);
3518 vim_free(pat);
3519 if (mustfree)
3520 vim_free(path);
3521 hash_clear(&found_ht);
3522 return OK;
3523}
3524
K.Takata161b6ac2022-11-14 15:31:07 +00003525#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003526/*
3527 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003528 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003529 */
3530 static void *
3531call_user_expand_func(
3532 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003533 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003534{
3535 cmdline_info_T *ccline = get_cmdline_info();
3536 int keep = 0;
3537 typval_T args[4];
3538 sctx_T save_current_sctx = current_sctx;
3539 char_u *pat = NULL;
3540 void *ret;
3541
3542 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3543 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003544
3545 if (ccline->cmdbuff != NULL)
3546 {
3547 keep = ccline->cmdbuff[ccline->cmdlen];
3548 ccline->cmdbuff[ccline->cmdlen] = 0;
3549 }
3550
3551 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3552
3553 args[0].v_type = VAR_STRING;
3554 args[0].vval.v_string = pat;
3555 args[1].v_type = VAR_STRING;
3556 args[1].vval.v_string = xp->xp_line;
3557 args[2].v_type = VAR_NUMBER;
3558 args[2].vval.v_number = xp->xp_col;
3559 args[3].v_type = VAR_UNKNOWN;
3560
3561 current_sctx = xp->xp_script_ctx;
3562
3563 ret = user_expand_func(xp->xp_arg, 3, args);
3564
3565 current_sctx = save_current_sctx;
3566 if (ccline->cmdbuff != NULL)
3567 ccline->cmdbuff[ccline->cmdlen] = keep;
3568
3569 vim_free(pat);
3570 return ret;
3571}
3572
3573/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003574 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3575 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003576 */
3577 static int
3578ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003579 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003580 expand_T *xp,
3581 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003582 char_u ***matches,
3583 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003584{
3585 char_u *retstr;
3586 char_u *s;
3587 char_u *e;
3588 int keep;
3589 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003590 int fuzzy;
3591 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003592 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003593
3594 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003595 *matches = NULL;
3596 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003597
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003598 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003599 if (retstr == NULL)
3600 return FAIL;
3601
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003602 if (!fuzzy)
3603 ga_init2(&ga, sizeof(char *), 3);
3604 else
3605 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3606
Bram Moolenaar66b51422019-08-18 21:44:12 +02003607 for (s = retstr; *s != NUL; s = e)
3608 {
3609 e = vim_strchr(s, '\n');
3610 if (e == NULL)
3611 e = s + STRLEN(s);
3612 keep = *e;
3613 *e = NUL;
3614
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003615 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003616 {
3617 if (!fuzzy)
3618 match = vim_regexec(regmatch, s, (colnr_T)0);
3619 else
3620 {
3621 score = fuzzy_match_str(s, pat);
3622 match = (score != 0);
3623 }
3624 }
3625 else
3626 match = TRUE; // match everything
3627
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628 *e = keep;
3629
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003630 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003631 {
3632 if (ga_grow(&ga, 1) == FAIL)
3633 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003634 if (!fuzzy)
3635 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3636 else
3637 {
3638 fuzmatch_str_T *fuzmatch =
3639 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003640 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003641 fuzmatch->str = vim_strnsave(s, e - s);
3642 fuzmatch->score = score;
3643 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003644 ++ga.ga_len;
3645 }
3646
3647 if (*e != NUL)
3648 ++e;
3649 }
3650 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003651
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003652 if (ga.ga_len == 0)
3653 return OK;
3654
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003655 if (!fuzzy)
3656 {
3657 *matches = ga.ga_data;
3658 *numMatches = ga.ga_len;
3659 }
3660 else
3661 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003662 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3663 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003664 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003665 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003666 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003667 return OK;
3668}
3669
3670/*
3671 * Expand names with a list returned by a function defined by the user.
3672 */
3673 static int
3674ExpandUserList(
3675 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003676 char_u ***matches,
3677 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003678{
3679 list_T *retlist;
3680 listitem_T *li;
3681 garray_T ga;
3682
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003683 *matches = NULL;
3684 *numMatches = 0;
3685 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003686 if (retlist == NULL)
3687 return FAIL;
3688
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003689 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003690 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003691 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003692 {
3693 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3694 continue; // Skip non-string items and empty strings
3695
3696 if (ga_grow(&ga, 1) == FAIL)
3697 break;
3698
3699 ((char_u **)ga.ga_data)[ga.ga_len] =
3700 vim_strsave(li->li_tv.vval.v_string);
3701 ++ga.ga_len;
3702 }
3703 list_unref(retlist);
3704
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003705 *matches = ga.ga_data;
3706 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003707 return OK;
3708}
K.Takata161b6ac2022-11-14 15:31:07 +00003709#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003710
3711/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003712 * Expand "file" for all comma-separated directories in "path".
3713 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003714 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003715 */
3716 void
3717globpath(
3718 char_u *path,
3719 char_u *file,
3720 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003721 int expand_options,
3722 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003723{
3724 expand_T xpc;
3725 char_u *buf;
3726 int i;
3727 int num_p;
3728 char_u **p;
3729
3730 buf = alloc(MAXPATHL);
3731 if (buf == NULL)
3732 return;
3733
3734 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003735 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003736
3737 // Loop over all entries in {path}.
3738 while (*path != NUL)
3739 {
3740 // Copy one item of the path to buf[] and concatenate the file name.
3741 copy_option_part(&path, buf, MAXPATHL, ",");
3742 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3743 {
K.Takata161b6ac2022-11-14 15:31:07 +00003744#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003745 // Using the platform's path separator (\) makes vim incorrectly
3746 // treat it as an escape character, use '/' instead.
3747 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3748 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003749#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003750 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003751#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003752 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003753 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003754 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3755 {
3756 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3757
3758 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003759 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003760 for (i = 0; i < num_p; ++i)
3761 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003762 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003763 ++ga->ga_len;
3764 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003765 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003766 }
3767 }
3768 }
3769
3770 vim_free(buf);
3771}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003772
Bram Moolenaareadee482020-09-04 15:37:31 +02003773/*
3774 * Translate some keys pressed when 'wildmenu' is used.
3775 */
3776 int
3777wildmenu_translate_key(
3778 cmdline_info_T *cclp,
3779 int key,
3780 expand_T *xp,
3781 int did_wild_list)
3782{
3783 int c = key;
3784
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003785 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003786 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003787 // When the popup menu is used for cmdline completion:
3788 // Up : go to the previous item in the menu
3789 // Down : go to the next item in the menu
3790 // Left : go to the parent directory
3791 // Right: list the files in the selected directory
3792 switch (c)
3793 {
3794 case K_UP: c = K_LEFT; break;
3795 case K_DOWN: c = K_RIGHT; break;
3796 case K_LEFT: c = K_UP; break;
3797 case K_RIGHT: c = K_DOWN; break;
3798 default: break;
3799 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003800 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003801
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003802 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003803 {
3804 if (c == K_LEFT)
3805 c = Ctrl_P;
3806 else if (c == K_RIGHT)
3807 c = Ctrl_N;
3808 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003809
Bram Moolenaareadee482020-09-04 15:37:31 +02003810 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003811 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003812 && cclp->cmdpos > 1
3813 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3814 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3815 && (c == '\n' || c == '\r' || c == K_KENTER))
3816 c = K_DOWN;
3817
3818 return c;
3819}
3820
3821/*
3822 * Delete characters on the command line, from "from" to the current
3823 * position.
3824 */
3825 static void
3826cmdline_del(cmdline_info_T *cclp, int from)
3827{
3828 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3829 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3830 cclp->cmdlen -= cclp->cmdpos - from;
3831 cclp->cmdpos = from;
3832}
3833
3834/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003835 * Handle a key pressed when the wild menu for the menu names
3836 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003837 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003838 static int
3839wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003840{
Bram Moolenaareadee482020-09-04 15:37:31 +02003841 int i;
3842 int j;
3843
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003844 // Hitting <Down> after "emenu Name.": complete submenu
3845 if (key == K_DOWN && cclp->cmdpos > 0
3846 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003847 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003848 key = p_wc;
3849 KeyTyped = TRUE; // in case the key was mapped
3850 }
3851 else if (key == K_UP)
3852 {
3853 // Hitting <Up>: Remove one submenu name in front of the
3854 // cursor
3855 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003856
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003857 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3858 i = 0;
3859 while (--j > 0)
3860 {
3861 // check for start of menu name
3862 if (cclp->cmdbuff[j] == ' '
3863 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003864 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003865 i = j + 1;
3866 break;
3867 }
3868 // check for start of submenu name
3869 if (cclp->cmdbuff[j] == '.'
3870 && cclp->cmdbuff[j - 1] != '\\')
3871 {
3872 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003873 {
3874 i = j + 1;
3875 break;
3876 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003877 else
3878 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003879 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003880 }
3881 if (i > 0)
3882 cmdline_del(cclp, i);
3883 key = p_wc;
3884 KeyTyped = TRUE; // in case the key was mapped
3885 xp->xp_context = EXPAND_NOTHING;
3886 }
3887
3888 return key;
3889}
3890
3891/*
3892 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3893 * directory names (EXPAND_DIRECTORIES) or shell command names
3894 * (EXPAND_SHELLCMD) is displayed.
3895 */
3896 static int
3897wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3898{
3899 int i;
3900 int j;
3901 char_u upseg[5];
3902
3903 upseg[0] = PATHSEP;
3904 upseg[1] = '.';
3905 upseg[2] = '.';
3906 upseg[3] = PATHSEP;
3907 upseg[4] = NUL;
3908
3909 if (key == K_DOWN
3910 && cclp->cmdpos > 0
3911 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3912 && (cclp->cmdpos < 3
3913 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3914 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3915 {
3916 // go down a directory
3917 key = p_wc;
3918 KeyTyped = TRUE; // in case the key was mapped
3919 }
3920 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3921 {
3922 // If in a direct ancestor, strip off one ../ to go down
3923 int found = FALSE;
3924
3925 j = cclp->cmdpos;
3926 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3927 while (--j > i)
3928 {
3929 if (has_mbyte)
3930 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3931 if (vim_ispathsep(cclp->cmdbuff[j]))
3932 {
3933 found = TRUE;
3934 break;
3935 }
3936 }
3937 if (found
3938 && cclp->cmdbuff[j - 1] == '.'
3939 && cclp->cmdbuff[j - 2] == '.'
3940 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3941 {
3942 cmdline_del(cclp, j - 2);
3943 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003944 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003945 }
3946 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003947 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003948 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003949 // go up a directory
3950 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003951
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003952 j = cclp->cmdpos - 1;
3953 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3954 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003955 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003956 if (has_mbyte)
3957 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3958 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003959#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003960 && vim_strchr((char_u *)" *?[{`$%#",
3961 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003962#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003963 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003964 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003965 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003966 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003967 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003968 break;
3969 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003970 else
3971 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003972 }
3973 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003974
3975 if (!found)
3976 j = i;
3977 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3978 j += 4;
3979 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3980 && j == i)
3981 j += 3;
3982 else
3983 j = 0;
3984 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003985 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003986 // TODO this is only for DOS/UNIX systems - need to put in
3987 // machine-specific stuff here and in upseg init
3988 cmdline_del(cclp, j);
3989 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003990 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003991 else if (cclp->cmdpos > i)
3992 cmdline_del(cclp, i);
3993
3994 // Now complete in the new directory. Set KeyTyped in case the
3995 // Up key came from a mapping.
3996 key = p_wc;
3997 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003998 }
3999
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004000 return key;
4001}
4002
4003/*
4004 * Handle a key pressed when the wild menu is displayed
4005 */
4006 int
4007wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4008{
4009 if (xp->xp_context == EXPAND_MENUNAMES)
4010 return wildmenu_process_key_menunames(cclp, key, xp);
4011 else if ((xp->xp_context == EXPAND_FILES
4012 || xp->xp_context == EXPAND_DIRECTORIES
4013 || xp->xp_context == EXPAND_SHELLCMD))
4014 return wildmenu_process_key_filenames(cclp, key, xp);
4015
4016 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004017}
4018
4019/*
4020 * Free expanded names when finished walking through the matches
4021 */
4022 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004023wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004024{
4025 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004026
4027 if (!p_wmnu || wild_menu_showing == 0)
4028 return;
4029
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004030#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004031 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004032 if (cclp->input_fn)
4033 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004034#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004035
4036 if (wild_menu_showing == WM_SCROLLED)
4037 {
4038 // Entered command line, move it up
4039 cmdline_row--;
4040 redrawcmd();
4041 }
4042 else if (save_p_ls != -1)
4043 {
4044 // restore 'laststatus' and 'winminheight'
4045 p_ls = save_p_ls;
4046 p_wmh = save_p_wmh;
4047 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004048 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004049 redrawcmd();
4050 save_p_ls = -1;
4051 }
4052 else
4053 {
4054 win_redraw_last_status(topframe);
4055 redraw_statuslines();
4056 }
4057 KeyTyped = skt;
4058 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004059#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004060 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004061 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004062#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004063}
Bram Moolenaareadee482020-09-04 15:37:31 +02004064
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004065#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004066/*
4067 * "getcompletion()" function
4068 */
4069 void
4070f_getcompletion(typval_T *argvars, typval_T *rettv)
4071{
4072 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004073 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004074 expand_T xpc;
4075 int filtered = FALSE;
4076 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004077 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004078
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004079 if (in_vim9script()
4080 && (check_for_string_arg(argvars, 0) == FAIL
4081 || check_for_string_arg(argvars, 1) == FAIL
4082 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4083 return;
4084
ii144785fe02021-11-21 12:13:56 +00004085 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004086 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004087 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004088 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004089
4090 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004091 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004092
4093 if (p_wic)
4094 options |= WILD_ICASE;
4095
4096 // For filtered results, 'wildignore' is used
4097 if (!filtered)
4098 options |= WILD_KEEP_ALL;
4099
4100 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004101 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004102 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004103 int cmdline_len = (int)STRLEN(pat);
4104 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004105 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004106 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004107 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004108 else
4109 {
ii144785fe02021-11-21 12:13:56 +00004110 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004111 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004112 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004113
4114 xpc.xp_context = cmdcomplete_str_to_type(type);
4115 if (xpc.xp_context == EXPAND_NOTHING)
4116 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004117 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004118 return;
4119 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004120
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004121 if (xpc.xp_context == EXPAND_USER_DEFINED)
4122 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004123 // Must be "custom,funcname" pattern
4124 if (STRNCMP(type, "custom,", 7) != 0)
4125 {
4126 semsg(_(e_invalid_argument_str), type);
4127 return;
4128 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004129
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004130 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004131 }
4132
4133 if (xpc.xp_context == EXPAND_USER_LIST)
4134 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004135 // Must be "customlist,funcname" pattern
4136 if (STRNCMP(type, "customlist,", 11) != 0)
4137 {
4138 semsg(_(e_invalid_argument_str), type);
4139 return;
4140 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004141
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004142 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004143 }
4144
Bram Moolenaar66b51422019-08-18 21:44:12 +02004145# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004146 if (xpc.xp_context == EXPAND_MENUS)
4147 {
4148 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4149 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4150 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004151# endif
4152# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004153 if (xpc.xp_context == EXPAND_CSCOPE)
4154 {
4155 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4156 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4157 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004158# endif
4159# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004160 if (xpc.xp_context == EXPAND_SIGN)
4161 {
4162 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4163 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4164 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004165# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004166 if (xpc.xp_context == EXPAND_RUNTIME)
4167 {
4168 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4169 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4170 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004171 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004172
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004173 if (cmdline_fuzzy_completion_supported(&xpc))
4174 // when fuzzy matching, don't modify the search string
4175 pat = vim_strsave(xpc.xp_pattern);
4176 else
4177 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4178
Bram Moolenaar93a10962022-06-16 11:42:09 +01004179 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004180 {
4181 int i;
4182
4183 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4184
4185 for (i = 0; i < xpc.xp_numfiles; i++)
4186 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4187 }
4188 vim_free(pat);
4189 ExpandCleanup(&xpc);
4190}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004191#endif // FEAT_EVAL