blob: 228d4bc095919b744290d72929a56be017baefd5 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000018static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000020 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000022static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000026static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020028#endif
29
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000030// "compl_match_array" points the currently displayed list of entries in the
31// popup menu. It is NULL when there is no popup menu.
32static pumitem_T *compl_match_array = NULL;
33static int compl_match_arraysize;
34// First column in cmdline of the matched item for completion.
35static int compl_startcol;
36static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
Bram Moolenaard6e91382022-11-12 17:44:13 +000038#define SHOW_FILE_TEXT(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
52 && xp->xp_context != EXPAND_FILES
53 && xp->xp_context != EXPAND_FILES_IN_PATH
54 && xp->xp_context != EXPAND_FILETYPE
55 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
57 && xp->xp_context != EXPAND_OWNSYNTAX
58 && xp->xp_context != EXPAND_PACKADD
59 && 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
116 if (xp->xp_backslash == XP_BS_THREE)
117 {
118 p = vim_strsave_escaped(files[i], (char_u *)" ");
119 if (p != NULL)
120 {
121 vim_free(files[i]);
122 files[i] = p;
123#if defined(BACKSLASH_IN_FILENAME)
124 p = vim_strsave_escaped(files[i], (char_u *)" ");
125 if (p != NULL)
126 {
127 vim_free(files[i]);
128 files[i] = p;
129 }
130#endif
131 }
132 }
133#ifdef BACKSLASH_IN_FILENAME
134 p = vim_strsave_fnameescape(files[i], vse_what);
135#else
136 p = vim_strsave_fnameescape(files[i],
137 xp->xp_shell ? VSE_SHELL : vse_what);
138#endif
139 if (p != NULL)
140 {
141 vim_free(files[i]);
142 files[i] = p;
143 }
144
145 // If 'str' starts with "\~", replace "~" at start of
146 // files[i] with "\~".
147 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
148 escape_fname(&files[i]);
149 }
150 xp->xp_backslash = XP_BS_NONE;
151
152 // If the first file starts with a '+' escape it. Otherwise it
153 // could be seen as "+cmd".
154 if (*files[0] == '+')
155 escape_fname(&files[0]);
156 }
157 else if (xp->xp_context == EXPAND_TAGS)
158 {
159 // Insert a backslash before characters in a tag name that
160 // would terminate the ":tag" command.
161 for (int i = 0; i < numfiles; ++i)
162 {
163 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
164 if (p != NULL)
165 {
166 vim_free(files[i]);
167 files[i] = p;
168 }
169 }
170 }
171}
172
173/*
174 * Escape special characters in the cmdline completion matches.
175 */
176 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200177ExpandEscape(
178 expand_T *xp,
179 char_u *str,
180 int numfiles,
181 char_u **files,
182 int options)
183{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200184 // May change home directory back to "~"
185 if (options & WILD_HOME_REPLACE)
186 tilde_replace(str, numfiles, files);
187
188 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000189 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200190}
191
192/*
193 * Return FAIL if this is not an appropriate context in which to do
194 * completion of anything, return OK if it is (even if there are no matches).
195 * For the caller, this means that the character is just passed through like a
196 * normal character (instead of being expanded). This allows :s/^I^D etc.
197 */
198 int
199nextwild(
200 expand_T *xp,
201 int type,
202 int options, // extra options for ExpandOne()
203 int escape) // if TRUE, escape the returned matches
204{
205 cmdline_info_T *ccline = get_cmdline_info();
206 int i, j;
207 char_u *p1;
208 char_u *p2;
209 int difflen;
210 int v;
211
212 if (xp->xp_numfiles == -1)
213 {
214 set_expand_context(xp);
215 cmd_showtail = expand_showtail(xp);
216 }
217
218 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
219 {
220 beep_flush();
221 return OK; // Something illegal on command line
222 }
223 if (xp->xp_context == EXPAND_NOTHING)
224 {
225 // Caller can use the character as a normal char instead
226 return FAIL;
227 }
228
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000229 // If cmd_silent is set then don't show the dots, because redrawcmd() below
230 // won't remove them.
231 if (!cmd_silent)
232 {
233 msg_puts("..."); // show that we are busy
234 out_flush();
235 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200236
237 i = (int)(xp->xp_pattern - ccline->cmdbuff);
238 xp->xp_pattern_len = ccline->cmdpos - i;
239
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000240 if (type == WILD_NEXT || type == WILD_PREV
241 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200242 {
243 // Get next/previous match for a previous expanded pattern.
244 p2 = ExpandOne(xp, NULL, NULL, 0, type);
245 }
246 else
247 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000248 if (cmdline_fuzzy_completion_supported(xp))
249 // If fuzzy matching, don't modify the search string
250 p1 = vim_strsave(xp->xp_pattern);
251 else
252 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
253
Bram Moolenaar66b51422019-08-18 21:44:12 +0200254 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000255 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200256 p2 = NULL;
257 else
258 {
259 int use_options = options |
260 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
261 if (escape)
262 use_options |= WILD_ESCAPE;
263
264 if (p_wic)
265 use_options += WILD_ICASE;
266 p2 = ExpandOne(xp, p1,
267 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
268 use_options, type);
269 vim_free(p1);
270 // longest match: make sure it is not shorter, happens with :help
271 if (p2 != NULL && type == WILD_LONGEST)
272 {
273 for (j = 0; j < xp->xp_pattern_len; ++j)
274 if (ccline->cmdbuff[i + j] == '*'
275 || ccline->cmdbuff[i + j] == '?')
276 break;
277 if ((int)STRLEN(p2) < j)
278 VIM_CLEAR(p2);
279 }
280 }
281 }
282
283 if (p2 != NULL && !got_int)
284 {
285 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
286 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
287 {
288 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
289 xp->xp_pattern = ccline->cmdbuff + i;
290 }
291 else
292 v = OK;
293 if (v == OK)
294 {
295 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
296 &ccline->cmdbuff[ccline->cmdpos],
297 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
298 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
299 ccline->cmdlen += difflen;
300 ccline->cmdpos += difflen;
301 }
302 }
303 vim_free(p2);
304
305 redrawcmd();
306 cursorcmd();
307
308 // When expanding a ":map" command and no matches are found, assume that
309 // the key is supposed to be inserted literally
310 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
311 return FAIL;
312
313 if (xp->xp_numfiles <= 0 && p2 == NULL)
314 beep_flush();
315 else if (xp->xp_numfiles == 1)
316 // free expanded pattern
317 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
318
319 return OK;
320}
321
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000322/*
323 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000324 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000325 */
326 static int
327cmdline_pum_create(
328 cmdline_info_T *ccline,
329 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000330 char_u **matches,
331 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000332 int showtail)
333{
334 int i;
335 int columns;
336
337 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000338 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000339 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000340 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000341 {
342 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
343 compl_match_array[i].pum_info = NULL;
344 compl_match_array[i].pum_extra = NULL;
345 compl_match_array[i].pum_kind = NULL;
346 }
347
348 // Compute the popup menu starting column
349 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
350 columns = vim_strsize(xp->xp_pattern);
351 if (showtail)
352 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000353 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000354 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000355 }
356 if (columns >= compl_startcol)
357 compl_startcol = 0;
358 else
359 compl_startcol -= columns;
360
361 // no default selection
362 compl_selected = -1;
363
364 cmdline_pum_display();
365
366 return EXPAND_OK;
367}
368
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000369/*
370 * Display the cmdline completion matches in a popup menu
371 */
372void cmdline_pum_display(void)
373{
374 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
375}
376
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000377/*
378 * Returns TRUE if the cmdline completion popup menu is being displayed.
379 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000380int cmdline_pum_active(void)
381{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100382 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000383}
384
385/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000386 * Remove the cmdline completion popup menu (if present), free the list of
387 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000388 */
389void cmdline_pum_remove(void)
390{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000391 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100392 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000393
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000394 pum_undisplay();
395 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000396 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000397 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000398 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000399 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100400
401 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
402 // as a side effect.
403 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000404}
405
406void cmdline_pum_cleanup(cmdline_info_T *cclp)
407{
408 cmdline_pum_remove();
409 wildmenu_cleanup(cclp);
410}
411
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000412/*
413 * Returns the starting column number to use for the cmdline completion popup
414 * menu.
415 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000416int cmdline_compl_startcol(void)
417{
418 return compl_startcol;
419}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000420
Bram Moolenaar66b51422019-08-18 21:44:12 +0200421/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000422 * Return the number of characters that should be skipped in a status match.
423 * These are backslashes used for escaping. Do show backslashes in help tags.
424 */
425 static int
426skip_status_match_char(expand_T *xp, char_u *s)
427{
428 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
429#ifdef FEAT_MENU
430 || ((xp->xp_context == EXPAND_MENUS
431 || xp->xp_context == EXPAND_MENUNAMES)
432 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
433#endif
434 )
435 {
436#ifndef BACKSLASH_IN_FILENAME
437 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
438 return 2;
439#endif
440 return 1;
441 }
442 return 0;
443}
444
445/*
446 * Get the length of an item as it will be shown in the status line.
447 */
448 static int
449status_match_len(expand_T *xp, char_u *s)
450{
451 int len = 0;
452
453#ifdef FEAT_MENU
454 int emenu = xp->xp_context == EXPAND_MENUS
455 || xp->xp_context == EXPAND_MENUNAMES;
456
457 // Check for menu separators - replace with '|'.
458 if (emenu && menu_is_separator(s))
459 return 1;
460#endif
461
462 while (*s != NUL)
463 {
464 s += skip_status_match_char(xp, s);
465 len += ptr2cells(s);
466 MB_PTR_ADV(s);
467 }
468
469 return len;
470}
471
472/*
473 * Show wildchar matches in the status line.
474 * Show at least the "match" item.
475 * We start at item 'first_match' in the list and show all matches that fit.
476 *
477 * If inversion is possible we use it. Else '=' characters are used.
478 */
479 static void
480win_redr_status_matches(
481 expand_T *xp,
482 int num_matches,
483 char_u **matches, // list of matches
484 int match,
485 int showtail)
486{
487#define L_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
488 int row;
489 char_u *buf;
490 int len;
491 int clen; // length in screen cells
492 int fillchar;
493 int attr;
494 int i;
495 int highlight = TRUE;
496 char_u *selstart = NULL;
497 int selstart_col = 0;
498 char_u *selend = NULL;
499 static int first_match = 0;
500 int add_left = FALSE;
501 char_u *s;
502#ifdef FEAT_MENU
503 int emenu;
504#endif
505 int l;
506
507 if (matches == NULL) // interrupted completion?
508 return;
509
510 if (has_mbyte)
511 buf = alloc(Columns * MB_MAXBYTES + 1);
512 else
513 buf = alloc(Columns + 1);
514 if (buf == NULL)
515 return;
516
517 if (match == -1) // don't show match but original text
518 {
519 match = 0;
520 highlight = FALSE;
521 }
522 // count 1 for the ending ">"
523 clen = status_match_len(xp, L_MATCH(match)) + 3;
524 if (match == 0)
525 first_match = 0;
526 else if (match < first_match)
527 {
528 // jumping left, as far as we can go
529 first_match = match;
530 add_left = TRUE;
531 }
532 else
533 {
534 // check if match fits on the screen
535 for (i = first_match; i < match; ++i)
536 clen += status_match_len(xp, L_MATCH(i)) + 2;
537 if (first_match > 0)
538 clen += 2;
539 // jumping right, put match at the left
540 if ((long)clen > Columns)
541 {
542 first_match = match;
543 // if showing the last match, we can add some on the left
544 clen = 2;
545 for (i = match; i < num_matches; ++i)
546 {
547 clen += status_match_len(xp, L_MATCH(i)) + 2;
548 if ((long)clen >= Columns)
549 break;
550 }
551 if (i == num_matches)
552 add_left = TRUE;
553 }
554 }
555 if (add_left)
556 while (first_match > 0)
557 {
558 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
559 if ((long)clen >= Columns)
560 break;
561 --first_match;
562 }
563
564 fillchar = fillchar_status(&attr, curwin);
565
566 if (first_match == 0)
567 {
568 *buf = NUL;
569 len = 0;
570 }
571 else
572 {
573 STRCPY(buf, "< ");
574 len = 2;
575 }
576 clen = len;
577
578 i = first_match;
579 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
580 {
581 if (i == match)
582 {
583 selstart = buf + len;
584 selstart_col = clen;
585 }
586
587 s = L_MATCH(i);
588 // Check for menu separators - replace with '|'
589#ifdef FEAT_MENU
590 emenu = (xp->xp_context == EXPAND_MENUS
591 || xp->xp_context == EXPAND_MENUNAMES);
592 if (emenu && menu_is_separator(s))
593 {
594 STRCPY(buf + len, transchar('|'));
595 l = (int)STRLEN(buf + len);
596 len += l;
597 clen += l;
598 }
599 else
600#endif
601 for ( ; *s != NUL; ++s)
602 {
603 s += skip_status_match_char(xp, s);
604 clen += ptr2cells(s);
605 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
606 {
607 STRNCPY(buf + len, s, l);
608 s += l - 1;
609 len += l;
610 }
611 else
612 {
613 STRCPY(buf + len, transchar_byte(*s));
614 len += (int)STRLEN(buf + len);
615 }
616 }
617 if (i == match)
618 selend = buf + len;
619
620 *(buf + len++) = ' ';
621 *(buf + len++) = ' ';
622 clen += 2;
623 if (++i == num_matches)
624 break;
625 }
626
627 if (i != num_matches)
628 {
629 *(buf + len++) = '>';
630 ++clen;
631 }
632
633 buf[len] = NUL;
634
635 row = cmdline_row - 1;
636 if (row >= 0)
637 {
638 if (wild_menu_showing == 0)
639 {
640 if (msg_scrolled > 0)
641 {
642 // Put the wildmenu just above the command line. If there is
643 // no room, scroll the screen one line up.
644 if (cmdline_row == Rows - 1)
645 {
646 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
647 ++msg_scrolled;
648 }
649 else
650 {
651 ++cmdline_row;
652 ++row;
653 }
654 wild_menu_showing = WM_SCROLLED;
655 }
656 else
657 {
658 // Create status line if needed by setting 'laststatus' to 2.
659 // Set 'winminheight' to zero to avoid that the window is
660 // resized.
661 if (lastwin->w_status_height == 0)
662 {
663 save_p_ls = p_ls;
664 save_p_wmh = p_wmh;
665 p_ls = 2;
666 p_wmh = 0;
667 last_status(FALSE);
668 }
669 wild_menu_showing = WM_SHOWN;
670 }
671 }
672
673 screen_puts(buf, row, 0, attr);
674 if (selstart != NULL && highlight)
675 {
676 *selend = NUL;
677 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
678 }
679
680 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
681 }
682
683 win_redraw_last_status(topframe);
684 vim_free(buf);
685}
686
687/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000688 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000689 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000690 */
691 static char_u *
692get_next_or_prev_match(
693 int mode,
694 expand_T *xp,
695 int *p_findex,
696 char_u *orig_save)
697{
698 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000699 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000700
701 if (xp->xp_numfiles <= 0)
702 return NULL;
703
704 if (mode == WILD_PREV)
705 {
706 if (findex == -1)
707 findex = xp->xp_numfiles;
708 --findex;
709 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000710 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000711 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000712 else if (mode == WILD_PAGEUP)
713 {
714 if (findex == 0)
715 // at the first entry, don't select any entries
716 findex = -1;
717 else if (findex == -1)
718 // no entry is selected. select the last entry
719 findex = xp->xp_numfiles - 1;
720 else
721 {
722 // go up by the pum height
723 ht = pum_get_height();
724 if (ht > 3)
725 ht -= 2;
726 findex -= ht;
727 if (findex < 0)
728 // few entries left, select the first entry
729 findex = 0;
730 }
731 }
732 else // mode == WILD_PAGEDOWN
733 {
734 if (findex == xp->xp_numfiles - 1)
735 // at the last entry, don't select any entries
736 findex = -1;
737 else if (findex == -1)
738 // no entry is selected. select the first entry
739 findex = 0;
740 else
741 {
742 // go down by the pum height
743 ht = pum_get_height();
744 if (ht > 3)
745 ht -= 2;
746 findex += ht;
747 if (findex >= xp->xp_numfiles)
748 // few entries left, select the last entry
749 findex = xp->xp_numfiles - 1;
750 }
751 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000752
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000753 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000754 if (findex < 0)
755 {
756 if (orig_save == NULL)
757 findex = xp->xp_numfiles - 1;
758 else
759 findex = -1;
760 }
761 if (findex >= xp->xp_numfiles)
762 {
763 if (orig_save == NULL)
764 findex = 0;
765 else
766 findex = -1;
767 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000768 if (compl_match_array)
769 {
770 compl_selected = findex;
771 cmdline_pum_display();
772 }
773 else if (p_wmnu)
774 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
775 findex, cmd_showtail);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000776 *p_findex = findex;
777
778 if (findex == -1)
779 return vim_strsave(orig_save);
780
781 return vim_strsave(xp->xp_files[findex]);
782}
783
784/*
785 * Start the command-line expansion and get the matches.
786 */
787 static char_u *
788ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
789{
790 int non_suf_match; // number without matching suffix
791 int i;
792 char_u *ss = NULL;
793
794 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000795 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000796 options) == FAIL)
797 {
798#ifdef FNAME_ILLEGAL
799 // Illegal file name has been silently skipped. But when there
800 // are wildcards, the real problem is that there was no match,
801 // causing the pattern to be added, which has illegal characters.
802 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
803 semsg(_(e_no_match_str_2), str);
804#endif
805 }
806 else if (xp->xp_numfiles == 0)
807 {
808 if (!(options & WILD_SILENT))
809 semsg(_(e_no_match_str_2), str);
810 }
811 else
812 {
813 // Escape the matches for use on the command line.
814 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
815
816 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000817 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000818 {
819 if (xp->xp_numfiles)
820 non_suf_match = xp->xp_numfiles;
821 else
822 non_suf_match = 1;
823 if ((xp->xp_context == EXPAND_FILES
824 || xp->xp_context == EXPAND_DIRECTORIES)
825 && xp->xp_numfiles > 1)
826 {
827 // More than one match; check suffix.
828 // The files will have been sorted on matching suffix in
829 // expand_wildcards, only need to check the first two.
830 non_suf_match = 0;
831 for (i = 0; i < 2; ++i)
832 if (match_suffix(xp->xp_files[i]))
833 ++non_suf_match;
834 }
835 if (non_suf_match != 1)
836 {
837 // Can we ever get here unless it's while expanding
838 // interactively? If not, we can get rid of this all
839 // together. Don't really want to wait for this message
840 // (and possibly have to hit return to continue!).
841 if (!(options & WILD_SILENT))
842 emsg(_(e_too_many_file_names));
843 else if (!(options & WILD_NO_BEEP))
844 beep_flush();
845 }
846 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
847 ss = vim_strsave(xp->xp_files[0]);
848 }
849 }
850
851 return ss;
852}
853
854/*
855 * Return the longest common part in the list of cmdline completion matches.
856 */
857 static char_u *
858find_longest_match(expand_T *xp, int options)
859{
860 long_u len;
861 int mb_len = 1;
862 int c0, ci;
863 int i;
864 char_u *ss;
865
866 for (len = 0; xp->xp_files[0][len]; len += mb_len)
867 {
868 if (has_mbyte)
869 {
870 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
871 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
872 }
873 else
874 c0 = xp->xp_files[0][len];
875 for (i = 1; i < xp->xp_numfiles; ++i)
876 {
877 if (has_mbyte)
878 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
879 else
880 ci = xp->xp_files[i][len];
881 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
882 || xp->xp_context == EXPAND_FILES
883 || xp->xp_context == EXPAND_SHELLCMD
884 || xp->xp_context == EXPAND_BUFFERS))
885 {
886 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
887 break;
888 }
889 else if (c0 != ci)
890 break;
891 }
892 if (i < xp->xp_numfiles)
893 {
894 if (!(options & WILD_NO_BEEP))
895 vim_beep(BO_WILD);
896 break;
897 }
898 }
899
900 ss = alloc(len + 1);
901 if (ss)
902 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
903
904 return ss;
905}
906
907/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000908 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200909 * Chars that should not be expanded must be preceded with a backslash.
910 * Return a pointer to allocated memory containing the new string.
911 * Return NULL for failure.
912 *
913 * "orig" is the originally expanded string, copied to allocated memory. It
914 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
915 * WILD_PREV "orig" should be NULL.
916 *
917 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
918 * is WILD_EXPAND_FREE or WILD_ALL.
919 *
920 * mode = WILD_FREE: just free previously expanded matches
921 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
922 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
923 * mode = WILD_NEXT: use next match in multiple match, wrap to first
924 * mode = WILD_PREV: use previous match in multiple match, wrap to first
925 * mode = WILD_ALL: return all matches concatenated
926 * mode = WILD_LONGEST: return longest matched part
927 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000928 * mode = WILD_APPLY: apply the item selected in the cmdline completion
929 * popup menu and close the menu.
930 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
931 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200932 *
933 * options = WILD_LIST_NOTFOUND: list entries without a match
934 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
935 * options = WILD_USE_NL: Use '\n' for WILD_ALL
936 * options = WILD_NO_BEEP: Don't beep for multiple matches
937 * options = WILD_ADD_SLASH: add a slash after directory names
938 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
939 * options = WILD_SILENT: don't print warning messages
940 * options = WILD_ESCAPE: put backslash before special chars
941 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200942 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200943 *
944 * The variables xp->xp_context and xp->xp_backslash must have been set!
945 */
946 char_u *
947ExpandOne(
948 expand_T *xp,
949 char_u *str,
950 char_u *orig, // allocated copy of original of expanded string
951 int options,
952 int mode)
953{
954 char_u *ss = NULL;
955 static int findex;
956 static char_u *orig_save = NULL; // kept value of orig
957 int orig_saved = FALSE;
958 int i;
959 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200960
961 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000962 if (mode == WILD_NEXT || mode == WILD_PREV
963 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000964 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200965
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000966 if (mode == WILD_CANCEL)
967 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
968 else if (mode == WILD_APPLY)
969 ss = vim_strsave(findex == -1 ? (orig_save ?
970 orig_save : (char_u *)"") : xp->xp_files[findex]);
971
Bram Moolenaar66b51422019-08-18 21:44:12 +0200972 // free old names
973 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
974 {
975 FreeWild(xp->xp_numfiles, xp->xp_files);
976 xp->xp_numfiles = -1;
977 VIM_CLEAR(orig_save);
978 }
979 findex = 0;
980
981 if (mode == WILD_FREE) // only release file name
982 return NULL;
983
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000984 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200985 {
986 vim_free(orig_save);
987 orig_save = orig;
988 orig_saved = TRUE;
989
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000990 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200991 }
992
993 // Find longest common part
994 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
995 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000996 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200997 findex = -1; // next p_wc gets first one
998 }
999
Bram Moolenaar57e95172022-08-20 19:26:14 +01001000 // Concatenate all matching names. Unless interrupted, this can be slow
1001 // and the result probably won't be used.
1002 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001003 {
1004 len = 0;
1005 for (i = 0; i < xp->xp_numfiles; ++i)
1006 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
1007 ss = alloc(len);
1008 if (ss != NULL)
1009 {
1010 *ss = NUL;
1011 for (i = 0; i < xp->xp_numfiles; ++i)
1012 {
1013 STRCAT(ss, xp->xp_files[i]);
1014 if (i != xp->xp_numfiles - 1)
1015 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1016 }
1017 }
1018 }
1019
1020 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1021 ExpandCleanup(xp);
1022
1023 // Free "orig" if it wasn't stored in "orig_save".
1024 if (!orig_saved)
1025 vim_free(orig);
1026
1027 return ss;
1028}
1029
1030/*
1031 * Prepare an expand structure for use.
1032 */
1033 void
1034ExpandInit(expand_T *xp)
1035{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001036 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001037 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001038 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001039}
1040
1041/*
1042 * Cleanup an expand structure after use.
1043 */
1044 void
1045ExpandCleanup(expand_T *xp)
1046{
1047 if (xp->xp_numfiles >= 0)
1048 {
1049 FreeWild(xp->xp_numfiles, xp->xp_files);
1050 xp->xp_numfiles = -1;
1051 }
1052}
1053
1054/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001055 * Display one line of completion matches. Multiple matches are displayed in
1056 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001057 * matches - list of completion match names
1058 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001059 * lines - number of output lines
1060 * linenr - line number of matches to display
1061 * maxlen - maximum number of characters in each line
1062 * showtail - display only the tail of the full path of a file name
1063 * dir_attr - highlight attribute to use for directory names
1064 */
1065 static void
1066showmatches_oneline(
1067 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001068 char_u **matches,
1069 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001070 int lines,
1071 int linenr,
1072 int maxlen,
1073 int showtail,
1074 int dir_attr)
1075{
1076 int i, j;
1077 int isdir;
1078 int lastlen;
1079 char_u *p;
1080
1081 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001082 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001083 {
1084 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1085 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001086 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1087 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001088 msg_advance(maxlen + 1);
1089 msg_puts((char *)p);
1090 msg_advance(maxlen + 3);
1091 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1092 break;
1093 }
1094 for (i = maxlen - lastlen; --i >= 0; )
1095 msg_putchar(' ');
1096 if (xp->xp_context == EXPAND_FILES
1097 || xp->xp_context == EXPAND_SHELLCMD
1098 || xp->xp_context == EXPAND_BUFFERS)
1099 {
1100 // highlight directories
1101 if (xp->xp_numfiles != -1)
1102 {
1103 char_u *halved_slash;
1104 char_u *exp_path;
1105 char_u *path;
1106
1107 // Expansion was done before and special characters
1108 // were escaped, need to halve backslashes. Also
1109 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001110 exp_path = expand_env_save_opt(matches[j], TRUE);
1111 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001112 halved_slash = backslash_halve_save(path);
1113 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001114 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001115 vim_free(exp_path);
1116 if (halved_slash != path)
1117 vim_free(halved_slash);
1118 }
1119 else
1120 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001121 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001122 if (showtail)
1123 p = SHOW_FILE_TEXT(j);
1124 else
1125 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001126 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001127 TRUE);
1128 p = NameBuff;
1129 }
1130 }
1131 else
1132 {
1133 isdir = FALSE;
1134 p = SHOW_FILE_TEXT(j);
1135 }
1136 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1137 }
1138 if (msg_col > 0) // when not wrapped around
1139 {
1140 msg_clr_eos();
1141 msg_putchar('\n');
1142 }
1143 out_flush(); // show one line at a time
1144}
1145
1146/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001147 * Show all matches for completion on the command line.
1148 * Returns EXPAND_NOTHING when the character that triggered expansion should
1149 * be inserted like a normal character.
1150 */
1151 int
1152showmatches(expand_T *xp, int wildmenu UNUSED)
1153{
1154 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001155 int numMatches;
1156 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001157 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001158 int maxlen;
1159 int lines;
1160 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001161 int attr;
1162 int showtail;
1163
1164 if (xp->xp_numfiles == -1)
1165 {
1166 set_expand_context(xp);
1167 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001168 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001169 showtail = expand_showtail(xp);
1170 if (i != EXPAND_OK)
1171 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001172 }
1173 else
1174 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001175 numMatches = xp->xp_numfiles;
1176 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001177 showtail = cmd_showtail;
1178 }
1179
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001180 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001181 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001182 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001183
Bram Moolenaar66b51422019-08-18 21:44:12 +02001184 if (!wildmenu)
1185 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001186 msg_didany = FALSE; // lines_left will be set
1187 msg_start(); // prepare for paging
1188 msg_putchar('\n');
1189 out_flush();
1190 cmdline_row = msg_row;
1191 msg_didany = FALSE; // lines_left will be set again
1192 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001193 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001194
1195 if (got_int)
1196 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001197 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001198 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001199 else
1200 {
1201 // find the length of the longest file name
1202 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001203 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001204 {
1205 if (!showtail && (xp->xp_context == EXPAND_FILES
1206 || xp->xp_context == EXPAND_SHELLCMD
1207 || xp->xp_context == EXPAND_BUFFERS))
1208 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001209 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001210 j = vim_strsize(NameBuff);
1211 }
1212 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001213 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001214 if (j > maxlen)
1215 maxlen = j;
1216 }
1217
1218 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001219 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001220 else
1221 {
1222 // compute the number of columns and lines for the listing
1223 maxlen += 2; // two spaces between file names
1224 columns = ((int)Columns + 2) / maxlen;
1225 if (columns < 1)
1226 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001227 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001228 }
1229
1230 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1231
1232 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1233 {
1234 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1235 msg_clr_eos();
1236 msg_advance(maxlen - 3);
1237 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1238 }
1239
1240 // list the files line by line
1241 for (i = 0; i < lines; ++i)
1242 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001243 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001244 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001245 if (got_int)
1246 {
1247 got_int = FALSE;
1248 break;
1249 }
1250 }
1251
1252 // we redraw the command below the lines that we have just listed
1253 // This is a bit tricky, but it saves a lot of screen updating.
1254 cmdline_row = msg_row; // will put it back later
1255 }
1256
1257 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001258 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001259
1260 return EXPAND_OK;
1261}
1262
1263/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001264 * gettail() version for showmatches() and win_redr_status_matches():
1265 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001266 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001267 static char_u *
1268showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001269{
1270 char_u *p;
1271 char_u *t = s;
1272 int had_sep = FALSE;
1273
1274 for (p = s; *p != NUL; )
1275 {
1276 if (vim_ispathsep(*p)
1277#ifdef BACKSLASH_IN_FILENAME
1278 && !rem_backslash(p)
1279#endif
1280 )
1281 had_sep = TRUE;
1282 else if (had_sep)
1283 {
1284 t = p;
1285 had_sep = FALSE;
1286 }
1287 MB_PTR_ADV(p);
1288 }
1289 return t;
1290}
1291
1292/*
1293 * Return TRUE if we only need to show the tail of completion matches.
1294 * When not completing file names or there is a wildcard in the path FALSE is
1295 * returned.
1296 */
1297 static int
1298expand_showtail(expand_T *xp)
1299{
1300 char_u *s;
1301 char_u *end;
1302
1303 // When not completing file names a "/" may mean something different.
1304 if (xp->xp_context != EXPAND_FILES
1305 && xp->xp_context != EXPAND_SHELLCMD
1306 && xp->xp_context != EXPAND_DIRECTORIES)
1307 return FALSE;
1308
1309 end = gettail(xp->xp_pattern);
1310 if (end == xp->xp_pattern) // there is no path separator
1311 return FALSE;
1312
1313 for (s = xp->xp_pattern; s < end; s++)
1314 {
1315 // Skip escaped wildcards. Only when the backslash is not a path
1316 // separator, on DOS the '*' "path\*\file" must not be skipped.
1317 if (rem_backslash(s))
1318 ++s;
1319 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1320 return FALSE;
1321 }
1322 return TRUE;
1323}
1324
1325/*
1326 * Prepare a string for expansion.
1327 * When expanding file names: The string will be used with expand_wildcards().
1328 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1329 * When expanding other names: The string will be used with regcomp(). Copy
1330 * the name into allocated memory and prepend "^".
1331 */
1332 char_u *
1333addstar(
1334 char_u *fname,
1335 int len,
1336 int context) // EXPAND_FILES etc.
1337{
1338 char_u *retval;
1339 int i, j;
1340 int new_len;
1341 char_u *tail;
1342 int ends_in_star;
1343
1344 if (context != EXPAND_FILES
1345 && context != EXPAND_FILES_IN_PATH
1346 && context != EXPAND_SHELLCMD
1347 && context != EXPAND_DIRECTORIES)
1348 {
1349 // Matching will be done internally (on something other than files).
1350 // So we convert the file-matching-type wildcards into our kind for
1351 // use with vim_regcomp(). First work out how long it will be:
1352
1353 // For help tags the translation is done in find_help_tags().
1354 // For a tag pattern starting with "/" no translation is needed.
1355 if (context == EXPAND_HELP
1356 || context == EXPAND_COLORS
1357 || context == EXPAND_COMPILER
1358 || context == EXPAND_OWNSYNTAX
1359 || context == EXPAND_FILETYPE
1360 || context == EXPAND_PACKADD
1361 || ((context == EXPAND_TAGS_LISTFILES
1362 || context == EXPAND_TAGS)
1363 && fname[0] == '/'))
1364 retval = vim_strnsave(fname, len);
1365 else
1366 {
1367 new_len = len + 2; // +2 for '^' at start, NUL at end
1368 for (i = 0; i < len; i++)
1369 {
1370 if (fname[i] == '*' || fname[i] == '~')
1371 new_len++; // '*' needs to be replaced by ".*"
1372 // '~' needs to be replaced by "\~"
1373
1374 // Buffer names are like file names. "." should be literal
1375 if (context == EXPAND_BUFFERS && fname[i] == '.')
1376 new_len++; // "." becomes "\."
1377
1378 // Custom expansion takes care of special things, match
1379 // backslashes literally (perhaps also for other types?)
1380 if ((context == EXPAND_USER_DEFINED
1381 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1382 new_len++; // '\' becomes "\\"
1383 }
1384 retval = alloc(new_len);
1385 if (retval != NULL)
1386 {
1387 retval[0] = '^';
1388 j = 1;
1389 for (i = 0; i < len; i++, j++)
1390 {
1391 // Skip backslash. But why? At least keep it for custom
1392 // expansion.
1393 if (context != EXPAND_USER_DEFINED
1394 && context != EXPAND_USER_LIST
1395 && fname[i] == '\\'
1396 && ++i == len)
1397 break;
1398
1399 switch (fname[i])
1400 {
1401 case '*': retval[j++] = '.';
1402 break;
1403 case '~': retval[j++] = '\\';
1404 break;
1405 case '?': retval[j] = '.';
1406 continue;
1407 case '.': if (context == EXPAND_BUFFERS)
1408 retval[j++] = '\\';
1409 break;
1410 case '\\': if (context == EXPAND_USER_DEFINED
1411 || context == EXPAND_USER_LIST)
1412 retval[j++] = '\\';
1413 break;
1414 }
1415 retval[j] = fname[i];
1416 }
1417 retval[j] = NUL;
1418 }
1419 }
1420 }
1421 else
1422 {
1423 retval = alloc(len + 4);
1424 if (retval != NULL)
1425 {
1426 vim_strncpy(retval, fname, len);
1427
1428 // Don't add a star to *, ~, ~user, $var or `cmd`.
1429 // * would become **, which walks the whole tree.
1430 // ~ would be at the start of the file name, but not the tail.
1431 // $ could be anywhere in the tail.
1432 // ` could be anywhere in the file name.
1433 // When the name ends in '$' don't add a star, remove the '$'.
1434 tail = gettail(retval);
1435 ends_in_star = (len > 0 && retval[len - 1] == '*');
1436#ifndef BACKSLASH_IN_FILENAME
1437 for (i = len - 2; i >= 0; --i)
1438 {
1439 if (retval[i] != '\\')
1440 break;
1441 ends_in_star = !ends_in_star;
1442 }
1443#endif
1444 if ((*retval != '~' || tail != retval)
1445 && !ends_in_star
1446 && vim_strchr(tail, '$') == NULL
1447 && vim_strchr(retval, '`') == NULL)
1448 retval[len++] = '*';
1449 else if (len > 0 && retval[len - 1] == '$')
1450 --len;
1451 retval[len] = NUL;
1452 }
1453 }
1454 return retval;
1455}
1456
1457/*
1458 * Must parse the command line so far to work out what context we are in.
1459 * Completion can then be done based on that context.
1460 * This routine sets the variables:
1461 * xp->xp_pattern The start of the pattern to be expanded within
1462 * the command line (ends at the cursor).
1463 * xp->xp_context The type of thing to expand. Will be one of:
1464 *
1465 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1466 * the command line, like an unknown command. Caller
1467 * should beep.
1468 * EXPAND_NOTHING Unrecognised context for completion, use char like
1469 * a normal char, rather than for completion. eg
1470 * :s/^I/
1471 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1472 * it.
1473 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1474 * EXPAND_FILES After command with EX_XFILE set, or after setting
1475 * with P_EXPAND set. eg :e ^I, :w>>^I
1476 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1477 * when we know only directories are of interest. eg
1478 * :set dir=^I
1479 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1480 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1481 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1482 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1483 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1484 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1485 * EXPAND_EVENTS Complete event names
1486 * EXPAND_SYNTAX Complete :syntax command arguments
1487 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1488 * EXPAND_AUGROUP Complete autocommand group names
1489 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1490 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1491 * eg :unmap a^I , :cunab x^I
1492 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1493 * eg :call sub^I
1494 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1495 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1496 * names in expressions, eg :while s^I
1497 * EXPAND_ENV_VARS Complete environment variable names
1498 * EXPAND_USER Complete user names
1499 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001500 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001501set_expand_context(expand_T *xp)
1502{
1503 cmdline_info_T *ccline = get_cmdline_info();
1504
1505 // only expansion for ':', '>' and '=' command-lines
1506 if (ccline->cmdfirstc != ':'
1507#ifdef FEAT_EVAL
1508 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1509 && !ccline->input_fn
1510#endif
1511 )
1512 {
1513 xp->xp_context = EXPAND_NOTHING;
1514 return;
1515 }
1516 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1517}
1518
Bram Moolenaard0190392019-08-23 21:17:35 +02001519/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001520 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1521 * For user defined commands, the completion context is set in 'xp' and the
1522 * completion flags in 'complp'.
1523 *
1524 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001525 */
1526 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001527set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001528{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001529 char_u *p = NULL;
1530 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001531 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001532
1533 // Isolate the command and search for it in the command table.
1534 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001535 // - the 'k' command can directly be followed by any character, but do
1536 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1537 // find matches anywhere in the command name, do this only for command
1538 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001539 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001540 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001541 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001542 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001543 p = cmd + 1;
1544 }
1545 else
1546 {
1547 p = cmd;
1548 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1549 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001550 // A user command may contain digits.
1551 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1552 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001553 while (ASCII_ISALNUM(*p) || *p == '*')
1554 ++p;
1555 // for python 3.x: ":py3*" commands completion
1556 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1557 {
1558 ++p;
1559 while (ASCII_ISALPHA(*p) || *p == '*')
1560 ++p;
1561 }
1562 // check for non-alpha command
1563 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1564 ++p;
1565 len = (int)(p - cmd);
1566
1567 if (len == 0)
1568 {
1569 xp->xp_context = EXPAND_UNSUCCESSFUL;
1570 return NULL;
1571 }
1572
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001573 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001574
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001575 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001576 // Also when doing fuzzy expansion for non-shell commands, support
1577 // alphanumeric characters.
1578 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1579 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001580 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1581 ++p;
1582 }
1583
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001584 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001585 // character, complete the command name.
1586 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1587 return NULL;
1588
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001589 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001590 {
1591 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1592 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001593 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001594 p = cmd + 1;
1595 }
1596 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1597 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001598 eap->cmd = cmd;
1599 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001600 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001601 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001602 }
1603 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001604 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001605 {
1606 // Not still touching the command and it was an illegal one
1607 xp->xp_context = EXPAND_UNSUCCESSFUL;
1608 return NULL;
1609 }
1610
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001611 return p;
1612}
Bram Moolenaard0190392019-08-23 21:17:35 +02001613
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001614/*
1615 * Set the completion context for a command argument with wild card characters.
1616 */
1617 static void
1618set_context_for_wildcard_arg(
1619 exarg_T *eap,
1620 char_u *arg,
1621 int usefilter,
1622 expand_T *xp,
1623 int *complp)
1624{
1625 char_u *p;
1626 int c;
1627 int in_quote = FALSE;
1628 char_u *bow = NULL; // Beginning of word
1629 int len = 0;
1630
1631 // Allow spaces within back-quotes to count as part of the argument
1632 // being expanded.
1633 xp->xp_pattern = skipwhite(arg);
1634 p = xp->xp_pattern;
1635 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001636 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001637 if (has_mbyte)
1638 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001639 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001640 c = *p;
1641 if (c == '\\' && p[1] != NUL)
1642 ++p;
1643 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001644 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001645 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001646 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001647 xp->xp_pattern = p;
1648 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001649 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001650 in_quote = !in_quote;
1651 }
1652 // An argument can contain just about everything, except
1653 // characters that end the command and white space.
1654 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001655#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001656 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001657#endif
1658 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001659 {
1660 len = 0; // avoid getting stuck when space is in 'isfname'
1661 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001662 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001663 if (has_mbyte)
1664 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001665 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 c = *p;
1667 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001668 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 if (has_mbyte)
1670 len = (*mb_ptr2len)(p);
1671 else
1672 len = 1;
1673 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001674 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001675 if (in_quote)
1676 bow = p;
1677 else
1678 xp->xp_pattern = p;
1679 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001680 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001681 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001682 }
1683
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001684 // If we are still inside the quotes, and we passed a space, just
1685 // expand from there.
1686 if (bow != NULL && in_quote)
1687 xp->xp_pattern = bow;
1688 xp->xp_context = EXPAND_FILES;
1689
1690 // For a shell command more chars need to be escaped.
1691 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1692 {
1693#ifndef BACKSLASH_IN_FILENAME
1694 xp->xp_shell = TRUE;
1695#endif
1696 // When still after the command name expand executables.
1697 if (xp->xp_pattern == skipwhite(arg))
1698 xp->xp_context = EXPAND_SHELLCMD;
1699 }
1700
1701 // Check for environment variable.
1702 if (*xp->xp_pattern == '$')
1703 {
1704 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1705 if (!vim_isIDc(*p))
1706 break;
1707 if (*p == NUL)
1708 {
1709 xp->xp_context = EXPAND_ENV_VARS;
1710 ++xp->xp_pattern;
1711 // Avoid that the assignment uses EXPAND_FILES again.
1712 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1713 *complp = EXPAND_ENV_VARS;
1714 }
1715 }
1716 // Check for user names.
1717 if (*xp->xp_pattern == '~')
1718 {
1719 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1720 ;
1721 // Complete ~user only if it partially matches a user name.
1722 // A full match ~user<Tab> will be replaced by user's home
1723 // directory i.e. something like ~user<Tab> -> /home/user/
1724 if (*p == NUL && p > xp->xp_pattern + 1
1725 && match_user(xp->xp_pattern + 1) >= 1)
1726 {
1727 xp->xp_context = EXPAND_USER;
1728 ++xp->xp_pattern;
1729 }
1730 }
1731}
1732
1733/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001734 * Set the completion context for the :filter command. Returns a pointer to the
1735 * next command after the :filter command.
1736 */
1737 static char_u *
1738set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1739{
1740 if (*arg != NUL)
1741 arg = skip_vimgrep_pat(arg, NULL, NULL);
1742 if (arg == NULL || *arg == NUL)
1743 {
1744 xp->xp_context = EXPAND_NOTHING;
1745 return NULL;
1746 }
1747 return skipwhite(arg);
1748}
1749
1750#ifdef FEAT_SEARCH_EXTRA
1751/*
1752 * Set the completion context for the :match command. Returns a pointer to the
1753 * next command after the :match command.
1754 */
1755 static char_u *
1756set_context_in_match_cmd(expand_T *xp, char_u *arg)
1757{
1758 if (*arg == NUL || !ends_excmd(*arg))
1759 {
1760 // also complete "None"
1761 set_context_in_echohl_cmd(xp, arg);
1762 arg = skipwhite(skiptowhite(arg));
1763 if (*arg != NUL)
1764 {
1765 xp->xp_context = EXPAND_NOTHING;
1766 arg = skip_regexp(arg + 1, *arg, magic_isset());
1767 }
1768 }
1769 return find_nextcmd(arg);
1770}
1771#endif
1772
1773/*
1774 * Returns a pointer to the next command after a :global or a :v command.
1775 * Returns NULL if there is no next command.
1776 */
1777 static char_u *
1778find_cmd_after_global_cmd(char_u *arg)
1779{
1780 int delim;
1781
1782 delim = *arg; // get the delimiter
1783 if (delim)
1784 ++arg; // skip delimiter if there is one
1785
1786 while (arg[0] != NUL && arg[0] != delim)
1787 {
1788 if (arg[0] == '\\' && arg[1] != NUL)
1789 ++arg;
1790 ++arg;
1791 }
1792 if (arg[0] != NUL)
1793 return arg + 1;
1794
1795 return NULL;
1796}
1797
1798/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001799 * Returns a pointer to the next command after a :substitute or a :& command.
1800 * Returns NULL if there is no next command.
1801 */
1802 static char_u *
1803find_cmd_after_substitute_cmd(char_u *arg)
1804{
1805 int delim;
1806
1807 delim = *arg;
1808 if (delim)
1809 {
1810 // skip "from" part
1811 ++arg;
1812 arg = skip_regexp(arg, delim, magic_isset());
1813
1814 if (arg[0] != NUL && arg[0] == delim)
1815 {
1816 // skip "to" part
1817 ++arg;
1818 while (arg[0] != NUL && arg[0] != delim)
1819 {
1820 if (arg[0] == '\\' && arg[1] != NUL)
1821 ++arg;
1822 ++arg;
1823 }
1824 if (arg[0] != NUL) // skip delimiter
1825 ++arg;
1826 }
1827 }
1828 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1829 ++arg;
1830 if (arg[0] != NUL)
1831 return arg;
1832
1833 return NULL;
1834}
1835
1836/*
1837 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1838 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1839 * Returns NULL if there is no next command.
1840 */
1841 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001842find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001843{
1844 arg = skipwhite(skipdigits(arg)); // skip count
1845 if (*arg == '/') // Match regexp, not just whole words
1846 {
1847 for (++arg; *arg && *arg != '/'; arg++)
1848 if (*arg == '\\' && arg[1] != NUL)
1849 arg++;
1850 if (*arg)
1851 {
1852 arg = skipwhite(arg + 1);
1853
1854 // Check for trailing illegal characters
1855 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1856 xp->xp_context = EXPAND_NOTHING;
1857 else
1858 return arg;
1859 }
1860 }
1861
1862 return NULL;
1863}
1864
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001865#ifdef FEAT_EVAL
1866/*
1867 * Set the completion context for the :unlet command. Always returns NULL.
1868 */
1869 static char_u *
1870set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1871{
1872 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1873 arg = xp->xp_pattern + 1;
1874
1875 xp->xp_context = EXPAND_USER_VARS;
1876 xp->xp_pattern = arg;
1877
1878 if (*xp->xp_pattern == '$')
1879 {
1880 xp->xp_context = EXPAND_ENV_VARS;
1881 ++xp->xp_pattern;
1882 }
1883
1884 return NULL;
1885}
1886#endif
1887
1888#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1889/*
1890 * Set the completion context for the :language command. Always returns NULL.
1891 */
1892 static char_u *
1893set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1894{
1895 char_u *p;
1896
1897 p = skiptowhite(arg);
1898 if (*p == NUL)
1899 {
1900 xp->xp_context = EXPAND_LANGUAGE;
1901 xp->xp_pattern = arg;
1902 }
1903 else
1904 {
1905 if ( STRNCMP(arg, "messages", p - arg) == 0
1906 || STRNCMP(arg, "ctype", p - arg) == 0
1907 || STRNCMP(arg, "time", p - arg) == 0
1908 || STRNCMP(arg, "collate", p - arg) == 0)
1909 {
1910 xp->xp_context = EXPAND_LOCALES;
1911 xp->xp_pattern = skipwhite(p);
1912 }
1913 else
1914 xp->xp_context = EXPAND_NOTHING;
1915 }
1916
1917 return NULL;
1918}
1919#endif
1920
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001921#ifdef FEAT_EVAL
1922static enum
1923{
1924 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001925 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1926 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001927} breakpt_expand_what;
1928
1929/*
1930 * Set the completion context for the :breakadd command. Always returns NULL.
1931 */
1932 static char_u *
1933set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1934{
1935 char_u *p;
1936 char_u *subcmd_start;
1937
1938 xp->xp_context = EXPAND_BREAKPOINT;
1939 xp->xp_pattern = arg;
1940
1941 if (cmdidx == CMD_breakadd)
1942 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001943 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001944 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001945 else
1946 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001947
1948 p = skipwhite(arg);
1949 if (*p == NUL)
1950 return NULL;
1951 subcmd_start = p;
1952
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001953 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001954 {
1955 // :breakadd file [lnum] <filename>
1956 // :breakadd func [lnum] <funcname>
1957 p += 4;
1958 p = skipwhite(p);
1959
1960 // skip line number (if specified)
1961 if (VIM_ISDIGIT(*p))
1962 {
1963 p = skipdigits(p);
1964 if (*p != ' ')
1965 {
1966 xp->xp_context = EXPAND_NOTHING;
1967 return NULL;
1968 }
1969 p = skipwhite(p);
1970 }
1971 if (STRNCMP("file", subcmd_start, 4) == 0)
1972 xp->xp_context = EXPAND_FILES;
1973 else
1974 xp->xp_context = EXPAND_USER_FUNC;
1975 xp->xp_pattern = p;
1976 }
1977 else if (STRNCMP("expr ", p, 5) == 0)
1978 {
1979 // :breakadd expr <expression>
1980 xp->xp_context = EXPAND_EXPRESSION;
1981 xp->xp_pattern = skipwhite(p + 5);
1982 }
1983
1984 return NULL;
1985}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001986
1987 static char_u *
1988set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1989{
1990 char_u *p;
1991
1992 xp->xp_context = EXPAND_NOTHING;
1993 xp->xp_pattern = NULL;
1994
1995 p = skipwhite(arg);
1996 if (VIM_ISDIGIT(*p))
1997 return NULL;
1998
1999 xp->xp_context = EXPAND_SCRIPTNAMES;
2000 xp->xp_pattern = p;
2001
2002 return NULL;
2003}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002004#endif
2005
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002006/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002007 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2008 * The argument to the command is 'arg' and the argument flags is 'argt'.
2009 * For user-defined commands and for environment variables, 'compl' has the
2010 * completion type.
2011 * Returns a pointer to the next command. Returns NULL if there is no next
2012 * command.
2013 */
2014 static char_u *
2015set_context_by_cmdname(
2016 char_u *cmd,
2017 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002018 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002019 char_u *arg,
2020 long argt,
2021 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002022 int forceit)
2023{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002024 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002025 {
2026 case CMD_find:
2027 case CMD_sfind:
2028 case CMD_tabfind:
2029 if (xp->xp_context == EXPAND_FILES)
2030 xp->xp_context = EXPAND_FILES_IN_PATH;
2031 break;
2032 case CMD_cd:
2033 case CMD_chdir:
2034 case CMD_tcd:
2035 case CMD_tchdir:
2036 case CMD_lcd:
2037 case CMD_lchdir:
2038 if (xp->xp_context == EXPAND_FILES)
2039 xp->xp_context = EXPAND_DIRECTORIES;
2040 break;
2041 case CMD_help:
2042 xp->xp_context = EXPAND_HELP;
2043 xp->xp_pattern = arg;
2044 break;
2045
2046 // Command modifiers: return the argument.
2047 // Also for commands with an argument that is a command.
2048 case CMD_aboveleft:
2049 case CMD_argdo:
2050 case CMD_belowright:
2051 case CMD_botright:
2052 case CMD_browse:
2053 case CMD_bufdo:
2054 case CMD_cdo:
2055 case CMD_cfdo:
2056 case CMD_confirm:
2057 case CMD_debug:
2058 case CMD_folddoclosed:
2059 case CMD_folddoopen:
2060 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002061 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002062 case CMD_keepalt:
2063 case CMD_keepjumps:
2064 case CMD_keepmarks:
2065 case CMD_keeppatterns:
2066 case CMD_ldo:
2067 case CMD_leftabove:
2068 case CMD_lfdo:
2069 case CMD_lockmarks:
2070 case CMD_noautocmd:
2071 case CMD_noswapfile:
2072 case CMD_rightbelow:
2073 case CMD_sandbox:
2074 case CMD_silent:
2075 case CMD_tab:
2076 case CMD_tabdo:
2077 case CMD_topleft:
2078 case CMD_verbose:
2079 case CMD_vertical:
2080 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002081 case CMD_vim9cmd:
2082 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002083 return arg;
2084
2085 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002086 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002087
2088#ifdef FEAT_SEARCH_EXTRA
2089 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002090 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002091#endif
2092
2093 // All completion for the +cmdline_compl feature goes here.
2094
2095 case CMD_command:
2096 return set_context_in_user_cmd(xp, arg);
2097
2098 case CMD_delcommand:
2099 xp->xp_context = EXPAND_USER_COMMANDS;
2100 xp->xp_pattern = arg;
2101 break;
2102
2103 case CMD_global:
2104 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002105 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002106 case CMD_and:
2107 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002108 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002109 case CMD_isearch:
2110 case CMD_dsearch:
2111 case CMD_ilist:
2112 case CMD_dlist:
2113 case CMD_ijump:
2114 case CMD_psearch:
2115 case CMD_djump:
2116 case CMD_isplit:
2117 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002118 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002119 case CMD_autocmd:
2120 return set_context_in_autocmd(xp, arg, FALSE);
2121 case CMD_doautocmd:
2122 case CMD_doautoall:
2123 return set_context_in_autocmd(xp, arg, TRUE);
2124 case CMD_set:
2125 set_context_in_set_cmd(xp, arg, 0);
2126 break;
2127 case CMD_setglobal:
2128 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2129 break;
2130 case CMD_setlocal:
2131 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2132 break;
2133 case CMD_tag:
2134 case CMD_stag:
2135 case CMD_ptag:
2136 case CMD_ltag:
2137 case CMD_tselect:
2138 case CMD_stselect:
2139 case CMD_ptselect:
2140 case CMD_tjump:
2141 case CMD_stjump:
2142 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002143 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002144 xp->xp_context = EXPAND_TAGS_LISTFILES;
2145 else
2146 xp->xp_context = EXPAND_TAGS;
2147 xp->xp_pattern = arg;
2148 break;
2149 case CMD_augroup:
2150 xp->xp_context = EXPAND_AUGROUP;
2151 xp->xp_pattern = arg;
2152 break;
2153#ifdef FEAT_SYN_HL
2154 case CMD_syntax:
2155 set_context_in_syntax_cmd(xp, arg);
2156 break;
2157#endif
2158#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002159 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002160 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002161 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002162 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002163 case CMD_if:
2164 case CMD_elseif:
2165 case CMD_while:
2166 case CMD_for:
2167 case CMD_echo:
2168 case CMD_echon:
2169 case CMD_execute:
2170 case CMD_echomsg:
2171 case CMD_echoerr:
2172 case CMD_call:
2173 case CMD_return:
2174 case CMD_cexpr:
2175 case CMD_caddexpr:
2176 case CMD_cgetexpr:
2177 case CMD_lexpr:
2178 case CMD_laddexpr:
2179 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002180 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002181 break;
2182
2183 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002184 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002185 case CMD_function:
2186 case CMD_delfunction:
2187 xp->xp_context = EXPAND_USER_FUNC;
2188 xp->xp_pattern = arg;
2189 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002190 case CMD_disassemble:
2191 set_context_in_disassemble_cmd(xp, arg);
2192 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002193
2194 case CMD_echohl:
2195 set_context_in_echohl_cmd(xp, arg);
2196 break;
2197#endif
2198 case CMD_highlight:
2199 set_context_in_highlight_cmd(xp, arg);
2200 break;
2201#ifdef FEAT_CSCOPE
2202 case CMD_cscope:
2203 case CMD_lcscope:
2204 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002205 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002206 break;
2207#endif
2208#ifdef FEAT_SIGNS
2209 case CMD_sign:
2210 set_context_in_sign_cmd(xp, arg);
2211 break;
2212#endif
2213 case CMD_bdelete:
2214 case CMD_bwipeout:
2215 case CMD_bunload:
2216 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2217 arg = xp->xp_pattern + 1;
2218 // FALLTHROUGH
2219 case CMD_buffer:
2220 case CMD_sbuffer:
2221 case CMD_checktime:
2222 xp->xp_context = EXPAND_BUFFERS;
2223 xp->xp_pattern = arg;
2224 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002225#ifdef FEAT_DIFF
2226 case CMD_diffget:
2227 case CMD_diffput:
2228 // If current buffer is in diff mode, complete buffer names
2229 // which are in diff mode, and different than current buffer.
2230 xp->xp_context = EXPAND_DIFF_BUFFERS;
2231 xp->xp_pattern = arg;
2232 break;
2233#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002234 case CMD_USER:
2235 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002236 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2237 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002238
2239 case CMD_map: case CMD_noremap:
2240 case CMD_nmap: case CMD_nnoremap:
2241 case CMD_vmap: case CMD_vnoremap:
2242 case CMD_omap: case CMD_onoremap:
2243 case CMD_imap: case CMD_inoremap:
2244 case CMD_cmap: case CMD_cnoremap:
2245 case CMD_lmap: case CMD_lnoremap:
2246 case CMD_smap: case CMD_snoremap:
2247 case CMD_tmap: case CMD_tnoremap:
2248 case CMD_xmap: case CMD_xnoremap:
2249 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002250 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002251 case CMD_unmap:
2252 case CMD_nunmap:
2253 case CMD_vunmap:
2254 case CMD_ounmap:
2255 case CMD_iunmap:
2256 case CMD_cunmap:
2257 case CMD_lunmap:
2258 case CMD_sunmap:
2259 case CMD_tunmap:
2260 case CMD_xunmap:
2261 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002262 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002263 case CMD_mapclear:
2264 case CMD_nmapclear:
2265 case CMD_vmapclear:
2266 case CMD_omapclear:
2267 case CMD_imapclear:
2268 case CMD_cmapclear:
2269 case CMD_lmapclear:
2270 case CMD_smapclear:
2271 case CMD_tmapclear:
2272 case CMD_xmapclear:
2273 xp->xp_context = EXPAND_MAPCLEAR;
2274 xp->xp_pattern = arg;
2275 break;
2276
2277 case CMD_abbreviate: case CMD_noreabbrev:
2278 case CMD_cabbrev: case CMD_cnoreabbrev:
2279 case CMD_iabbrev: case CMD_inoreabbrev:
2280 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002281 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002282 case CMD_unabbreviate:
2283 case CMD_cunabbrev:
2284 case CMD_iunabbrev:
2285 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002286 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002287#ifdef FEAT_MENU
2288 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2289 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2290 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2291 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2292 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2293 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2294 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2295 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2296 case CMD_tmenu: case CMD_tunmenu:
2297 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2298 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2299#endif
2300
2301 case CMD_colorscheme:
2302 xp->xp_context = EXPAND_COLORS;
2303 xp->xp_pattern = arg;
2304 break;
2305
2306 case CMD_compiler:
2307 xp->xp_context = EXPAND_COMPILER;
2308 xp->xp_pattern = arg;
2309 break;
2310
2311 case CMD_ownsyntax:
2312 xp->xp_context = EXPAND_OWNSYNTAX;
2313 xp->xp_pattern = arg;
2314 break;
2315
2316 case CMD_setfiletype:
2317 xp->xp_context = EXPAND_FILETYPE;
2318 xp->xp_pattern = arg;
2319 break;
2320
2321 case CMD_packadd:
2322 xp->xp_context = EXPAND_PACKADD;
2323 xp->xp_pattern = arg;
2324 break;
2325
2326#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2327 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002328 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002329#endif
2330#if defined(FEAT_PROFILE)
2331 case CMD_profile:
2332 set_context_in_profile_cmd(xp, arg);
2333 break;
2334#endif
2335 case CMD_behave:
2336 xp->xp_context = EXPAND_BEHAVE;
2337 xp->xp_pattern = arg;
2338 break;
2339
2340 case CMD_messages:
2341 xp->xp_context = EXPAND_MESSAGES;
2342 xp->xp_pattern = arg;
2343 break;
2344
2345 case CMD_history:
2346 xp->xp_context = EXPAND_HISTORY;
2347 xp->xp_pattern = arg;
2348 break;
2349#if defined(FEAT_PROFILE)
2350 case CMD_syntime:
2351 xp->xp_context = EXPAND_SYNTIME;
2352 xp->xp_pattern = arg;
2353 break;
2354#endif
2355
2356 case CMD_argdelete:
2357 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2358 arg = xp->xp_pattern + 1;
2359 xp->xp_context = EXPAND_ARGLIST;
2360 xp->xp_pattern = arg;
2361 break;
2362
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002363#ifdef FEAT_EVAL
2364 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002365 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002366 case CMD_breakdel:
2367 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002368
2369 case CMD_scriptnames:
2370 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002371#endif
2372
Bram Moolenaard0190392019-08-23 21:17:35 +02002373 default:
2374 break;
2375 }
2376 return NULL;
2377}
2378
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002379/*
2380 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2381 * we don't need/want deleted. Maybe this could be done better if we didn't
2382 * repeat all this stuff. The only problem is that they may not stay
2383 * perfectly compatible with each other, but then the command line syntax
2384 * probably won't change that much -- webb.
2385 */
2386 static char_u *
2387set_one_cmd_context(
2388 expand_T *xp,
2389 char_u *buff) // buffer for command string
2390{
2391 char_u *p;
2392 char_u *cmd, *arg;
2393 int len = 0;
2394 exarg_T ea;
2395 int compl = EXPAND_NOTHING;
2396 int forceit = FALSE;
2397 int usefilter = FALSE; // filter instead of file name
2398
2399 ExpandInit(xp);
2400 xp->xp_pattern = buff;
2401 xp->xp_line = buff;
2402 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2403 ea.argt = 0;
2404
2405 // 1. skip comment lines and leading space, colons or bars
2406 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2407 ;
2408 xp->xp_pattern = cmd;
2409
2410 if (*cmd == NUL)
2411 return NULL;
2412 if (*cmd == '"') // ignore comment lines
2413 {
2414 xp->xp_context = EXPAND_NOTHING;
2415 return NULL;
2416 }
2417
2418 // 3. Skip over the range to find the command.
2419 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2420 xp->xp_pattern = cmd;
2421 if (*cmd == NUL)
2422 return NULL;
2423 if (*cmd == '"')
2424 {
2425 xp->xp_context = EXPAND_NOTHING;
2426 return NULL;
2427 }
2428
2429 if (*cmd == '|' || *cmd == '\n')
2430 return cmd + 1; // There's another command
2431
2432 // Get the command index.
2433 p = set_cmd_index(cmd, &ea, xp, &compl);
2434 if (p == NULL)
2435 return NULL;
2436
2437 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2438
2439 if (*p == '!') // forced commands
2440 {
2441 forceit = TRUE;
2442 ++p;
2443 }
2444
2445 // 6. parse arguments
2446 if (!IS_USER_CMDIDX(ea.cmdidx))
2447 ea.argt = excmd_get_argt(ea.cmdidx);
2448
2449 arg = skipwhite(p);
2450
2451 // Skip over ++argopt argument
2452 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2453 {
2454 p = arg;
2455 while (*p && !vim_isspace(*p))
2456 MB_PTR_ADV(p);
2457 arg = skipwhite(p);
2458 }
2459
2460 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2461 {
2462 if (*arg == '>') // append
2463 {
2464 if (*++arg == '>')
2465 ++arg;
2466 arg = skipwhite(arg);
2467 }
2468 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2469 {
2470 ++arg;
2471 usefilter = TRUE;
2472 }
2473 }
2474
2475 if (ea.cmdidx == CMD_read)
2476 {
2477 usefilter = forceit; // :r! filter if forced
2478 if (*arg == '!') // :r !filter
2479 {
2480 ++arg;
2481 usefilter = TRUE;
2482 }
2483 }
2484
2485 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2486 {
2487 while (*arg == *cmd) // allow any number of '>' or '<'
2488 ++arg;
2489 arg = skipwhite(arg);
2490 }
2491
2492 // Does command allow "+command"?
2493 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2494 {
2495 // Check if we're in the +command
2496 p = arg + 1;
2497 arg = skip_cmd_arg(arg, FALSE);
2498
2499 // Still touching the command after '+'?
2500 if (*arg == NUL)
2501 return p;
2502
2503 // Skip space(s) after +command to get to the real argument
2504 arg = skipwhite(arg);
2505 }
2506
2507
2508 // Check for '|' to separate commands and '"' to start comments.
2509 // Don't do this for ":read !cmd" and ":write !cmd".
2510 if ((ea.argt & EX_TRLBAR) && !usefilter)
2511 {
2512 p = arg;
2513 // ":redir @" is not the start of a comment
2514 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2515 p += 2;
2516 while (*p)
2517 {
2518 if (*p == Ctrl_V)
2519 {
2520 if (p[1] != NUL)
2521 ++p;
2522 }
2523 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2524 || *p == '|' || *p == '\n')
2525 {
2526 if (*(p - 1) != '\\')
2527 {
2528 if (*p == '|' || *p == '\n')
2529 return p + 1;
2530 return NULL; // It's a comment
2531 }
2532 }
2533 MB_PTR_ADV(p);
2534 }
2535 }
2536
2537 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2538 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2539 // no arguments allowed but there is something
2540 return NULL;
2541
2542 // Find start of last argument (argument just before cursor):
2543 p = buff;
2544 xp->xp_pattern = p;
2545 len = (int)STRLEN(buff);
2546 while (*p && p < buff + len)
2547 {
2548 if (*p == ' ' || *p == TAB)
2549 {
2550 // argument starts after a space
2551 xp->xp_pattern = ++p;
2552 }
2553 else
2554 {
2555 if (*p == '\\' && *(p + 1) != NUL)
2556 ++p; // skip over escaped character
2557 MB_PTR_ADV(p);
2558 }
2559 }
2560
2561 if (ea.argt & EX_XFILE)
2562 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2563
2564 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002565 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002566 forceit);
2567}
2568
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002569/*
2570 * Set the completion context in 'xp' for command 'str'
2571 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002572 void
2573set_cmd_context(
2574 expand_T *xp,
2575 char_u *str, // start of command line
2576 int len, // length of command line (excl. NUL)
2577 int col, // position of cursor
2578 int use_ccline UNUSED) // use ccline for info
2579{
2580#ifdef FEAT_EVAL
2581 cmdline_info_T *ccline = get_cmdline_info();
2582#endif
2583 int old_char = NUL;
2584 char_u *nextcomm;
2585
2586 // Avoid a UMR warning from Purify, only save the character if it has been
2587 // written before.
2588 if (col < len)
2589 old_char = str[col];
2590 str[col] = NUL;
2591 nextcomm = str;
2592
2593#ifdef FEAT_EVAL
2594 if (use_ccline && ccline->cmdfirstc == '=')
2595 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002596 // pass CMD_SIZE because there is no real command
2597 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002598 }
2599 else if (use_ccline && ccline->input_fn)
2600 {
2601 xp->xp_context = ccline->xp_context;
2602 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002603 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002604 }
2605 else
2606#endif
2607 while (nextcomm != NULL)
2608 nextcomm = set_one_cmd_context(xp, nextcomm);
2609
2610 // Store the string here so that call_user_expand_func() can get to them
2611 // easily.
2612 xp->xp_line = str;
2613 xp->xp_col = col;
2614
2615 str[col] = old_char;
2616}
2617
2618/*
2619 * Expand the command line "str" from context "xp".
2620 * "xp" must have been set by set_cmd_context().
2621 * xp->xp_pattern points into "str", to where the text that is to be expanded
2622 * starts.
2623 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2624 * cursor.
2625 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2626 * key that triggered expansion literally.
2627 * Returns EXPAND_OK otherwise.
2628 */
2629 int
2630expand_cmdline(
2631 expand_T *xp,
2632 char_u *str, // start of command line
2633 int col, // position of cursor
2634 int *matchcount, // return: nr of matches
2635 char_u ***matches) // return: array of pointers to matches
2636{
2637 char_u *file_str = NULL;
2638 int options = WILD_ADD_SLASH|WILD_SILENT;
2639
2640 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2641 {
2642 beep_flush();
2643 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2644 }
2645 if (xp->xp_context == EXPAND_NOTHING)
2646 {
2647 // Caller can use the character as a normal char instead
2648 return EXPAND_NOTHING;
2649 }
2650
2651 // add star to file name, or convert to regexp if not exp. files.
2652 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002653 if (cmdline_fuzzy_completion_supported(xp))
2654 // If fuzzy matching, don't modify the search string
2655 file_str = vim_strsave(xp->xp_pattern);
2656 else
2657 {
2658 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2659 if (file_str == NULL)
2660 return EXPAND_UNSUCCESSFUL;
2661 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002662
2663 if (p_wic)
2664 options += WILD_ICASE;
2665
2666 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002667 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002668 {
2669 *matchcount = 0;
2670 *matches = NULL;
2671 }
2672 vim_free(file_str);
2673
2674 return EXPAND_OK;
2675}
2676
Bram Moolenaar66b51422019-08-18 21:44:12 +02002677/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002678 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002679 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002680 */
2681 static int
2682expand_files_and_dirs(
2683 expand_T *xp,
2684 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002685 char_u ***matches,
2686 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002687 int flags,
2688 int options)
2689{
2690 int free_pat = FALSE;
2691 int i;
2692 int ret;
2693
2694 // for ":set path=" and ":set tags=" halve backslashes for escaped
2695 // space
2696 if (xp->xp_backslash != XP_BS_NONE)
2697 {
2698 free_pat = TRUE;
2699 pat = vim_strsave(pat);
2700 for (i = 0; pat[i]; ++i)
2701 if (pat[i] == '\\')
2702 {
2703 if (xp->xp_backslash == XP_BS_THREE
2704 && pat[i + 1] == '\\'
2705 && pat[i + 2] == '\\'
2706 && pat[i + 3] == ' ')
2707 STRMOVE(pat + i, pat + i + 3);
2708 if (xp->xp_backslash == XP_BS_ONE
2709 && pat[i + 1] == ' ')
2710 STRMOVE(pat + i, pat + i + 1);
2711 }
2712 }
2713
2714 if (xp->xp_context == EXPAND_FILES)
2715 flags |= EW_FILE;
2716 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2717 flags |= (EW_FILE | EW_PATH);
2718 else
2719 flags = (flags | EW_DIR) & ~EW_FILE;
2720 if (options & WILD_ICASE)
2721 flags |= EW_ICASE;
2722
2723 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002724 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002725 if (free_pat)
2726 vim_free(pat);
2727#ifdef BACKSLASH_IN_FILENAME
2728 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2729 {
2730 int j;
2731
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002732 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002733 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002734 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002735
2736 while (*ptr != NUL)
2737 {
2738 if (p_csl[0] == 's' && *ptr == '\\')
2739 *ptr = '/';
2740 else if (p_csl[0] == 'b' && *ptr == '/')
2741 *ptr = '\\';
2742 ptr += (*mb_ptr2len)(ptr);
2743 }
2744 }
2745 }
2746#endif
2747 return ret;
2748}
2749
2750/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002751 * Function given to ExpandGeneric() to obtain the possible arguments of the
2752 * ":behave {mswin,xterm}" command.
2753 */
2754 static char_u *
2755get_behave_arg(expand_T *xp UNUSED, int idx)
2756{
2757 if (idx == 0)
2758 return (char_u *)"mswin";
2759 if (idx == 1)
2760 return (char_u *)"xterm";
2761 return NULL;
2762}
2763
K.Takata161b6ac2022-11-14 15:31:07 +00002764#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002765/*
2766 * Function given to ExpandGeneric() to obtain the possible arguments of the
2767 * ":breakadd {expr, file, func, here}" command.
2768 * ":breakdel {func, file, here}" command.
2769 */
2770 static char_u *
2771get_breakadd_arg(expand_T *xp UNUSED, int idx)
2772{
2773 char *opts[] = {"expr", "file", "func", "here"};
2774
2775 if (idx >=0 && idx <= 3)
2776 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002777 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002778 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2779 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002780 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2781 {
2782 // breakdel {func, file, here}
2783 if (idx <= 2)
2784 return (char_u *)opts[idx + 1];
2785 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002786 else
2787 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002788 // profdel {func, file}
2789 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002790 return (char_u *)opts[idx + 1];
2791 }
2792 }
2793 return NULL;
2794}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002795
2796/*
2797 * Function given to ExpandGeneric() to obtain the possible arguments for the
2798 * ":scriptnames" command.
2799 */
2800 static char_u *
2801get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2802{
2803 scriptitem_T *si;
2804
2805 if (!SCRIPT_ID_VALID(idx + 1))
2806 return NULL;
2807
2808 si = SCRIPT_ITEM(idx + 1);
2809 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2810 return NameBuff;
2811}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002812#endif
2813
Bram Moolenaard0190392019-08-23 21:17:35 +02002814/*
2815 * Function given to ExpandGeneric() to obtain the possible arguments of the
2816 * ":messages {clear}" command.
2817 */
2818 static char_u *
2819get_messages_arg(expand_T *xp UNUSED, int idx)
2820{
2821 if (idx == 0)
2822 return (char_u *)"clear";
2823 return NULL;
2824}
2825
2826 static char_u *
2827get_mapclear_arg(expand_T *xp UNUSED, int idx)
2828{
2829 if (idx == 0)
2830 return (char_u *)"<buffer>";
2831 return NULL;
2832}
2833
2834/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002835 * Do the expansion based on xp->xp_context and 'rmp'.
2836 */
2837 static int
2838ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002839 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002840 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002841 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002842 char_u ***matches,
2843 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002844{
2845 static struct expgen
2846 {
2847 int context;
2848 char_u *((*func)(expand_T *, int));
2849 int ic;
2850 int escaped;
2851 } tab[] =
2852 {
2853 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2854 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2855 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2856 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2857 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2858 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2859 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2860 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2861 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2862 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002863#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002864 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2865 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2866 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2867 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2868 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002869#endif
2870#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002871 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2872 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002873#endif
2874#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002875 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002876#endif
2877#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002878 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002879#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002880 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2881 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2882 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002883#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002884 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002885#endif
2886#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002887 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002888#endif
2889#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002890 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002891#endif
2892#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002893 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2894 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002895#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002896 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2897 {EXPAND_USER, get_users, TRUE, FALSE},
2898 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002899#ifdef FEAT_EVAL
2900 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002901 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002902#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002903 };
2904 int i;
2905 int ret = FAIL;
2906
2907 // Find a context in the table and call the ExpandGeneric() with the
2908 // right function to do the expansion.
2909 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2910 {
2911 if (xp->xp_context == tab[i].context)
2912 {
2913 if (tab[i].ic)
2914 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002915 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2916 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002917 break;
2918 }
2919 }
2920
2921 return ret;
2922}
2923
2924/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002925 * Map wild expand options to flags for expand_wildcards()
2926 */
2927 static int
2928map_wildopts_to_ewflags(int options)
2929{
2930 int flags;
2931
2932 flags = EW_DIR; // include directories
2933 if (options & WILD_LIST_NOTFOUND)
2934 flags |= EW_NOTFOUND;
2935 if (options & WILD_ADD_SLASH)
2936 flags |= EW_ADDSLASH;
2937 if (options & WILD_KEEP_ALL)
2938 flags |= EW_KEEPALL;
2939 if (options & WILD_SILENT)
2940 flags |= EW_SILENT;
2941 if (options & WILD_NOERROR)
2942 flags |= EW_NOERROR;
2943 if (options & WILD_ALLLINKS)
2944 flags |= EW_ALLLINKS;
2945
2946 return flags;
2947}
2948
2949/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002950 * Do the expansion based on xp->xp_context and "pat".
2951 */
2952 static int
2953ExpandFromContext(
2954 expand_T *xp,
2955 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002956 char_u ***matches,
2957 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002958 int options) // WILD_ flags
2959{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002960 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002961 int ret;
2962 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002963 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002964 int fuzzy = cmdline_fuzzy_complete(pat)
2965 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002966
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002967 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002968
2969 if (xp->xp_context == EXPAND_FILES
2970 || xp->xp_context == EXPAND_DIRECTORIES
2971 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002972 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2973 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002974
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002975 *matches = (char_u **)"";
2976 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002977 if (xp->xp_context == EXPAND_HELP)
2978 {
2979 // With an empty argument we would get all the help tags, which is
2980 // very slow. Get matches for "help" instead.
2981 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002982 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002983 {
2984#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002985 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002986#endif
2987 return OK;
2988 }
2989 return FAIL;
2990 }
2991
Bram Moolenaar66b51422019-08-18 21:44:12 +02002992 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002993 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002994 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002995 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002996 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002997 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002998#ifdef FEAT_DIFF
2999 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003000 return ExpandBufnames(pat, numMatches, matches,
3001 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003002#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003003 if (xp->xp_context == EXPAND_TAGS
3004 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003005 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3006 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003007 if (xp->xp_context == EXPAND_COLORS)
3008 {
3009 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003010 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003011 directories);
3012 }
3013 if (xp->xp_context == EXPAND_COMPILER)
3014 {
3015 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003016 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003017 }
3018 if (xp->xp_context == EXPAND_OWNSYNTAX)
3019 {
3020 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003021 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003022 }
3023 if (xp->xp_context == EXPAND_FILETYPE)
3024 {
3025 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003026 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003027 }
K.Takata161b6ac2022-11-14 15:31:07 +00003028#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003029 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003030 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003031#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003032 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003033 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003034
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003035 // When expanding a function name starting with s:, match the <SNR>nr_
3036 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003037 if ((xp->xp_context == EXPAND_USER_FUNC
3038 || xp->xp_context == EXPAND_DISASSEMBLE)
3039 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003040 {
3041 int len = (int)STRLEN(pat) + 20;
3042
3043 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003044 if (tofree == NULL)
3045 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003046 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003047 pat = tofree;
3048 }
3049
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003050 if (!fuzzy)
3051 {
3052 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3053 if (regmatch.regprog == NULL)
3054 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003055
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003056 // set ignore-case according to p_ic, p_scs and pat
3057 regmatch.rm_ic = ignorecase(pat);
3058 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003059
3060 if (xp->xp_context == EXPAND_SETTINGS
3061 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003062 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003063 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003064 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003065#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003066 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003067 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003068#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003069 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003070 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003071
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003072 if (!fuzzy)
3073 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003074 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003075
3076 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003077}
3078
Bram Moolenaar66b51422019-08-18 21:44:12 +02003079/*
3080 * Expand a list of names.
3081 *
3082 * Generic function for command line completion. It calls a function to
3083 * obtain strings, one by one. The strings are matched against a regexp
3084 * program. Matching strings are copied into an array, which is returned.
3085 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003086 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3087 * is used.
3088 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003089 * Returns OK when no problems encountered, FAIL for error (out of memory).
3090 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003091 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003092ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003093 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003094 expand_T *xp,
3095 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003096 char_u ***matches,
3097 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003098 char_u *((*func)(expand_T *, int)),
3099 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003100 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003101{
3102 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003103 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003105 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003106 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003107 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003108 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003109 int sort_matches = FALSE;
3110 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003112 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003113 *matches = NULL;
3114 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003115
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003116 if (!fuzzy)
3117 ga_init2(&ga, sizeof(char *), 30);
3118 else
3119 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3120
3121 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003122 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003123 str = (*func)(xp, i);
3124 if (str == NULL) // end of list
3125 break;
3126 if (*str == NUL) // skip empty strings
3127 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003129 if (xp->xp_pattern[0] != NUL)
3130 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003131 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003132 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003133 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003134 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003135 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003136 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137 }
3138 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003139 else
3140 match = TRUE;
3141
3142 if (!match)
3143 continue;
3144
3145 if (escaped)
3146 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3147 else
3148 str = vim_strsave(str);
3149 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003150 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003151 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003152 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003153 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003154 return FAIL;
3155 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003156 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003157 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003158 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003159
3160 if (ga_grow(&ga, 1) == FAIL)
3161 {
3162 vim_free(str);
3163 break;
3164 }
3165
3166 if (fuzzy)
3167 {
3168 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3169 fuzmatch->idx = ga.ga_len;
3170 fuzmatch->str = str;
3171 fuzmatch->score = score;
3172 }
3173 else
3174 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3175
K.Takata161b6ac2022-11-14 15:31:07 +00003176#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003177 if (func == get_menu_names)
3178 {
3179 // test for separator added by get_menu_names()
3180 str += STRLEN(str) - 1;
3181 if (*str == '\001')
3182 *str = '.';
3183 }
K.Takata161b6ac2022-11-14 15:31:07 +00003184#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003185
3186 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003187 }
3188
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003189 if (ga.ga_len == 0)
3190 return OK;
3191
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003192 // sort the matches when using regular expression matching and sorting
3193 // applies to the completion context. Menus and scriptnames should be kept
3194 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003195 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003196 && xp->xp_context != EXPAND_MENUS
3197 && xp->xp_context != EXPAND_SCRIPTNAMES)
3198 sort_matches = TRUE;
3199
3200 // <SNR> functions should be sorted to the end.
3201 if (xp->xp_context == EXPAND_EXPRESSION
3202 || xp->xp_context == EXPAND_FUNCTIONS
3203 || xp->xp_context == EXPAND_USER_FUNC
3204 || xp->xp_context == EXPAND_DISASSEMBLE)
3205 funcsort = TRUE;
3206
3207 // Sort the matches.
3208 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003209 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003210 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003211 // <SNR> functions should be sorted to the end.
3212 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3213 sort_func_compare);
3214 else
3215 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3216 }
3217
3218 if (!fuzzy)
3219 {
3220 *matches = ga.ga_data;
3221 *numMatches = ga.ga_len;
3222 }
3223 else
3224 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003225 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3226 funcsort) == FAIL)
3227 return FAIL;
3228 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003229 }
3230
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003231#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003232 // Reset the variables used for special highlight names expansion, so that
3233 // they don't show up when getting normal highlight names by ID.
3234 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003235#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003236
Bram Moolenaar66b51422019-08-18 21:44:12 +02003237 return OK;
3238}
3239
3240/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003241 * Expand shell command matches in one directory of $PATH.
3242 */
3243 static void
3244expand_shellcmd_onedir(
3245 char_u *buf,
3246 char_u *s,
3247 size_t l,
3248 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003249 char_u ***matches,
3250 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003251 int flags,
3252 hashtab_T *ht,
3253 garray_T *gap)
3254{
3255 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003256 hash_T hash;
3257 hashitem_T *hi;
3258
3259 vim_strncpy(buf, s, l);
3260 add_pathsep(buf);
3261 l = STRLEN(buf);
3262 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3263
3264 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003265 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003266 if (ret != OK)
3267 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003268
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003269 if (ga_grow(gap, *numMatches) == FAIL)
3270 {
3271 FreeWild(*numMatches, *matches);
3272 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003273 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003274
3275 for (int i = 0; i < *numMatches; ++i)
3276 {
3277 char_u *name = (*matches)[i];
3278
3279 if (STRLEN(name) > l)
3280 {
3281 // Check if this name was already found.
3282 hash = hash_hash(name + l);
3283 hi = hash_lookup(ht, name + l, hash);
3284 if (HASHITEM_EMPTY(hi))
3285 {
3286 // Remove the path that was prepended.
3287 STRMOVE(name, name + l);
3288 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3289 hash_add_item(ht, hi, name, hash);
3290 name = NULL;
3291 }
3292 }
3293 vim_free(name);
3294 }
3295 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003296}
3297
3298/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003299 * Complete a shell command.
3300 * Returns FAIL or OK;
3301 */
3302 static int
3303expand_shellcmd(
3304 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003305 char_u ***matches, // return: array with matches
3306 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307 int flagsarg) // EW_ flags
3308{
3309 char_u *pat;
3310 int i;
3311 char_u *path = NULL;
3312 int mustfree = FALSE;
3313 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003314 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003315 size_t l;
3316 char_u *s, *e;
3317 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003318 int did_curdir = FALSE;
3319 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003320
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003321 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003322 if (buf == NULL)
3323 return FAIL;
3324
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003325 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003326 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003327 if (pat == NULL)
3328 {
3329 vim_free(buf);
3330 return FAIL;
3331 }
3332
Bram Moolenaar66b51422019-08-18 21:44:12 +02003333 for (i = 0; pat[i]; ++i)
3334 if (pat[i] == '\\' && pat[i + 1] == ' ')
3335 STRMOVE(pat + i, pat + i + 1);
3336
3337 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3338
3339 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3340 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3341 path = (char_u *)".";
3342 else
3343 {
3344 // For an absolute name we don't use $PATH.
3345 if (!mch_isFullName(pat))
3346 path = vim_getenv((char_u *)"PATH", &mustfree);
3347 if (path == NULL)
3348 path = (char_u *)"";
3349 }
3350
3351 // Go over all directories in $PATH. Expand matches in that directory and
3352 // collect them in "ga". When "." is not in $PATH also expand for the
3353 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003354 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003355 hash_init(&found_ht);
3356 for (s = path; ; s = e)
3357 {
K.Takata161b6ac2022-11-14 15:31:07 +00003358#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003359 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003360#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003362#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003363 if (e == NULL)
3364 e = s + STRLEN(s);
3365
3366 if (*s == NUL)
3367 {
3368 if (did_curdir)
3369 break;
3370 // Find directories in the current directory, path is empty.
3371 did_curdir = TRUE;
3372 flags |= EW_DIR;
3373 }
3374 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3375 {
3376 did_curdir = TRUE;
3377 flags |= EW_DIR;
3378 }
3379 else
3380 // Do not match directories inside a $PATH item.
3381 flags &= ~EW_DIR;
3382
3383 l = e - s;
3384 if (l > MAXPATHL - 5)
3385 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003386
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003387 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003388 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003389
Bram Moolenaar66b51422019-08-18 21:44:12 +02003390 if (*e != NUL)
3391 ++e;
3392 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003393 *matches = ga.ga_data;
3394 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003395
3396 vim_free(buf);
3397 vim_free(pat);
3398 if (mustfree)
3399 vim_free(path);
3400 hash_clear(&found_ht);
3401 return OK;
3402}
3403
K.Takata161b6ac2022-11-14 15:31:07 +00003404#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003405/*
3406 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003407 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003408 */
3409 static void *
3410call_user_expand_func(
3411 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003412 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003413{
3414 cmdline_info_T *ccline = get_cmdline_info();
3415 int keep = 0;
3416 typval_T args[4];
3417 sctx_T save_current_sctx = current_sctx;
3418 char_u *pat = NULL;
3419 void *ret;
3420
3421 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3422 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003423
3424 if (ccline->cmdbuff != NULL)
3425 {
3426 keep = ccline->cmdbuff[ccline->cmdlen];
3427 ccline->cmdbuff[ccline->cmdlen] = 0;
3428 }
3429
3430 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3431
3432 args[0].v_type = VAR_STRING;
3433 args[0].vval.v_string = pat;
3434 args[1].v_type = VAR_STRING;
3435 args[1].vval.v_string = xp->xp_line;
3436 args[2].v_type = VAR_NUMBER;
3437 args[2].vval.v_number = xp->xp_col;
3438 args[3].v_type = VAR_UNKNOWN;
3439
3440 current_sctx = xp->xp_script_ctx;
3441
3442 ret = user_expand_func(xp->xp_arg, 3, args);
3443
3444 current_sctx = save_current_sctx;
3445 if (ccline->cmdbuff != NULL)
3446 ccline->cmdbuff[ccline->cmdlen] = keep;
3447
3448 vim_free(pat);
3449 return ret;
3450}
3451
3452/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003453 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3454 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003455 */
3456 static int
3457ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003458 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003459 expand_T *xp,
3460 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003461 char_u ***matches,
3462 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003463{
3464 char_u *retstr;
3465 char_u *s;
3466 char_u *e;
3467 int keep;
3468 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003469 int fuzzy;
3470 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003471 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003472
3473 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003474 *matches = NULL;
3475 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003476
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003477 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003478 if (retstr == NULL)
3479 return FAIL;
3480
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003481 if (!fuzzy)
3482 ga_init2(&ga, sizeof(char *), 3);
3483 else
3484 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3485
Bram Moolenaar66b51422019-08-18 21:44:12 +02003486 for (s = retstr; *s != NUL; s = e)
3487 {
3488 e = vim_strchr(s, '\n');
3489 if (e == NULL)
3490 e = s + STRLEN(s);
3491 keep = *e;
3492 *e = NUL;
3493
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003494 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003495 {
3496 if (!fuzzy)
3497 match = vim_regexec(regmatch, s, (colnr_T)0);
3498 else
3499 {
3500 score = fuzzy_match_str(s, pat);
3501 match = (score != 0);
3502 }
3503 }
3504 else
3505 match = TRUE; // match everything
3506
Bram Moolenaar66b51422019-08-18 21:44:12 +02003507 *e = keep;
3508
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003509 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003510 {
3511 if (ga_grow(&ga, 1) == FAIL)
3512 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003513 if (!fuzzy)
3514 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3515 else
3516 {
3517 fuzmatch_str_T *fuzmatch =
3518 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003519 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003520 fuzmatch->str = vim_strnsave(s, e - s);
3521 fuzmatch->score = score;
3522 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003523 ++ga.ga_len;
3524 }
3525
3526 if (*e != NUL)
3527 ++e;
3528 }
3529 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003530
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003531 if (ga.ga_len == 0)
3532 return OK;
3533
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003534 if (!fuzzy)
3535 {
3536 *matches = ga.ga_data;
3537 *numMatches = ga.ga_len;
3538 }
3539 else
3540 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003541 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3542 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003543 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003544 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003545 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003546 return OK;
3547}
3548
3549/*
3550 * Expand names with a list returned by a function defined by the user.
3551 */
3552 static int
3553ExpandUserList(
3554 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003555 char_u ***matches,
3556 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003557{
3558 list_T *retlist;
3559 listitem_T *li;
3560 garray_T ga;
3561
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003562 *matches = NULL;
3563 *numMatches = 0;
3564 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003565 if (retlist == NULL)
3566 return FAIL;
3567
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003568 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003569 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003570 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003571 {
3572 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3573 continue; // Skip non-string items and empty strings
3574
3575 if (ga_grow(&ga, 1) == FAIL)
3576 break;
3577
3578 ((char_u **)ga.ga_data)[ga.ga_len] =
3579 vim_strsave(li->li_tv.vval.v_string);
3580 ++ga.ga_len;
3581 }
3582 list_unref(retlist);
3583
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003584 *matches = ga.ga_data;
3585 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003586 return OK;
3587}
K.Takata161b6ac2022-11-14 15:31:07 +00003588#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003589
3590/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003591 * Expand "file" for all comma-separated directories in "path".
3592 * Adds the matches to "ga". Caller must init "ga".
3593 */
3594 void
3595globpath(
3596 char_u *path,
3597 char_u *file,
3598 garray_T *ga,
3599 int expand_options)
3600{
3601 expand_T xpc;
3602 char_u *buf;
3603 int i;
3604 int num_p;
3605 char_u **p;
3606
3607 buf = alloc(MAXPATHL);
3608 if (buf == NULL)
3609 return;
3610
3611 ExpandInit(&xpc);
3612 xpc.xp_context = EXPAND_FILES;
3613
3614 // Loop over all entries in {path}.
3615 while (*path != NUL)
3616 {
3617 // Copy one item of the path to buf[] and concatenate the file name.
3618 copy_option_part(&path, buf, MAXPATHL, ",");
3619 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3620 {
K.Takata161b6ac2022-11-14 15:31:07 +00003621#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003622 // Using the platform's path separator (\) makes vim incorrectly
3623 // treat it as an escape character, use '/' instead.
3624 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3625 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003626#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003627 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003628#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003629 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003630 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003631 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3632 {
3633 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3634
3635 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003636 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003637 for (i = 0; i < num_p; ++i)
3638 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003639 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003640 ++ga->ga_len;
3641 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003642 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003643 }
3644 }
3645 }
3646
3647 vim_free(buf);
3648}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003649
Bram Moolenaareadee482020-09-04 15:37:31 +02003650/*
3651 * Translate some keys pressed when 'wildmenu' is used.
3652 */
3653 int
3654wildmenu_translate_key(
3655 cmdline_info_T *cclp,
3656 int key,
3657 expand_T *xp,
3658 int did_wild_list)
3659{
3660 int c = key;
3661
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003662 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003663 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003664 // When the popup menu is used for cmdline completion:
3665 // Up : go to the previous item in the menu
3666 // Down : go to the next item in the menu
3667 // Left : go to the parent directory
3668 // Right: list the files in the selected directory
3669 switch (c)
3670 {
3671 case K_UP: c = K_LEFT; break;
3672 case K_DOWN: c = K_RIGHT; break;
3673 case K_LEFT: c = K_UP; break;
3674 case K_RIGHT: c = K_DOWN; break;
3675 default: break;
3676 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003677 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003678
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003679 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003680 {
3681 if (c == K_LEFT)
3682 c = Ctrl_P;
3683 else if (c == K_RIGHT)
3684 c = Ctrl_N;
3685 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003686
Bram Moolenaareadee482020-09-04 15:37:31 +02003687 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003688 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003689 && cclp->cmdpos > 1
3690 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3691 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3692 && (c == '\n' || c == '\r' || c == K_KENTER))
3693 c = K_DOWN;
3694
3695 return c;
3696}
3697
3698/*
3699 * Delete characters on the command line, from "from" to the current
3700 * position.
3701 */
3702 static void
3703cmdline_del(cmdline_info_T *cclp, int from)
3704{
3705 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3706 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3707 cclp->cmdlen -= cclp->cmdpos - from;
3708 cclp->cmdpos = from;
3709}
3710
3711/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003712 * Handle a key pressed when the wild menu for the menu names
3713 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003714 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003715 static int
3716wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003717{
Bram Moolenaareadee482020-09-04 15:37:31 +02003718 int i;
3719 int j;
3720
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003721 // Hitting <Down> after "emenu Name.": complete submenu
3722 if (key == K_DOWN && cclp->cmdpos > 0
3723 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003724 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003725 key = p_wc;
3726 KeyTyped = TRUE; // in case the key was mapped
3727 }
3728 else if (key == K_UP)
3729 {
3730 // Hitting <Up>: Remove one submenu name in front of the
3731 // cursor
3732 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003733
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003734 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3735 i = 0;
3736 while (--j > 0)
3737 {
3738 // check for start of menu name
3739 if (cclp->cmdbuff[j] == ' '
3740 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003741 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003742 i = j + 1;
3743 break;
3744 }
3745 // check for start of submenu name
3746 if (cclp->cmdbuff[j] == '.'
3747 && cclp->cmdbuff[j - 1] != '\\')
3748 {
3749 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003750 {
3751 i = j + 1;
3752 break;
3753 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003754 else
3755 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003756 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003757 }
3758 if (i > 0)
3759 cmdline_del(cclp, i);
3760 key = p_wc;
3761 KeyTyped = TRUE; // in case the key was mapped
3762 xp->xp_context = EXPAND_NOTHING;
3763 }
3764
3765 return key;
3766}
3767
3768/*
3769 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3770 * directory names (EXPAND_DIRECTORIES) or shell command names
3771 * (EXPAND_SHELLCMD) is displayed.
3772 */
3773 static int
3774wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3775{
3776 int i;
3777 int j;
3778 char_u upseg[5];
3779
3780 upseg[0] = PATHSEP;
3781 upseg[1] = '.';
3782 upseg[2] = '.';
3783 upseg[3] = PATHSEP;
3784 upseg[4] = NUL;
3785
3786 if (key == K_DOWN
3787 && cclp->cmdpos > 0
3788 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3789 && (cclp->cmdpos < 3
3790 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3791 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3792 {
3793 // go down a directory
3794 key = p_wc;
3795 KeyTyped = TRUE; // in case the key was mapped
3796 }
3797 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3798 {
3799 // If in a direct ancestor, strip off one ../ to go down
3800 int found = FALSE;
3801
3802 j = cclp->cmdpos;
3803 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3804 while (--j > i)
3805 {
3806 if (has_mbyte)
3807 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3808 if (vim_ispathsep(cclp->cmdbuff[j]))
3809 {
3810 found = TRUE;
3811 break;
3812 }
3813 }
3814 if (found
3815 && cclp->cmdbuff[j - 1] == '.'
3816 && cclp->cmdbuff[j - 2] == '.'
3817 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3818 {
3819 cmdline_del(cclp, j - 2);
3820 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003821 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003822 }
3823 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003824 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003825 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003826 // go up a directory
3827 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003828
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003829 j = cclp->cmdpos - 1;
3830 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3831 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003832 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003833 if (has_mbyte)
3834 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3835 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003836#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003837 && vim_strchr((char_u *)" *?[{`$%#",
3838 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003839#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003840 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003841 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003842 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003843 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003844 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003845 break;
3846 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003847 else
3848 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003849 }
3850 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003851
3852 if (!found)
3853 j = i;
3854 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3855 j += 4;
3856 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3857 && j == i)
3858 j += 3;
3859 else
3860 j = 0;
3861 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003862 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003863 // TODO this is only for DOS/UNIX systems - need to put in
3864 // machine-specific stuff here and in upseg init
3865 cmdline_del(cclp, j);
3866 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003867 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003868 else if (cclp->cmdpos > i)
3869 cmdline_del(cclp, i);
3870
3871 // Now complete in the new directory. Set KeyTyped in case the
3872 // Up key came from a mapping.
3873 key = p_wc;
3874 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003875 }
3876
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003877 return key;
3878}
3879
3880/*
3881 * Handle a key pressed when the wild menu is displayed
3882 */
3883 int
3884wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3885{
3886 if (xp->xp_context == EXPAND_MENUNAMES)
3887 return wildmenu_process_key_menunames(cclp, key, xp);
3888 else if ((xp->xp_context == EXPAND_FILES
3889 || xp->xp_context == EXPAND_DIRECTORIES
3890 || xp->xp_context == EXPAND_SHELLCMD))
3891 return wildmenu_process_key_filenames(cclp, key, xp);
3892
3893 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003894}
3895
3896/*
3897 * Free expanded names when finished walking through the matches
3898 */
3899 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003900wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003901{
3902 int skt = KeyTyped;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003903#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003904 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003905#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003906
3907 if (!p_wmnu || wild_menu_showing == 0)
3908 return;
3909
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003910#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003911 if (cclp->input_fn)
3912 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003913#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003914
3915 if (wild_menu_showing == WM_SCROLLED)
3916 {
3917 // Entered command line, move it up
3918 cmdline_row--;
3919 redrawcmd();
3920 }
3921 else if (save_p_ls != -1)
3922 {
3923 // restore 'laststatus' and 'winminheight'
3924 p_ls = save_p_ls;
3925 p_wmh = save_p_wmh;
3926 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003927 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003928 redrawcmd();
3929 save_p_ls = -1;
3930 }
3931 else
3932 {
3933 win_redraw_last_status(topframe);
3934 redraw_statuslines();
3935 }
3936 KeyTyped = skt;
3937 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003938#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003939 if (cclp->input_fn)
3940 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003941#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003942}
Bram Moolenaareadee482020-09-04 15:37:31 +02003943
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003944#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003945/*
3946 * "getcompletion()" function
3947 */
3948 void
3949f_getcompletion(typval_T *argvars, typval_T *rettv)
3950{
3951 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003952 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003953 expand_T xpc;
3954 int filtered = FALSE;
3955 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003956 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003957
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003958 if (in_vim9script()
3959 && (check_for_string_arg(argvars, 0) == FAIL
3960 || check_for_string_arg(argvars, 1) == FAIL
3961 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3962 return;
3963
ii144785fe02021-11-21 12:13:56 +00003964 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003965 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003966 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003967 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003968
3969 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003970 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003971
3972 if (p_wic)
3973 options |= WILD_ICASE;
3974
3975 // For filtered results, 'wildignore' is used
3976 if (!filtered)
3977 options |= WILD_KEEP_ALL;
3978
3979 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003980 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003981 {
ii144785fe02021-11-21 12:13:56 +00003982 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003983 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003984 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003985 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003986 else
3987 {
ii144785fe02021-11-21 12:13:56 +00003988 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003989 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3990
3991 xpc.xp_context = cmdcomplete_str_to_type(type);
3992 if (xpc.xp_context == EXPAND_NOTHING)
3993 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003994 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003995 return;
3996 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003997
3998# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003999 if (xpc.xp_context == EXPAND_MENUS)
4000 {
4001 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4002 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4003 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004004# endif
4005# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004006 if (xpc.xp_context == EXPAND_CSCOPE)
4007 {
4008 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4009 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4010 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004011# endif
4012# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004013 if (xpc.xp_context == EXPAND_SIGN)
4014 {
4015 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4016 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4017 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004018# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004019 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004020
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004021 if (cmdline_fuzzy_completion_supported(&xpc))
4022 // when fuzzy matching, don't modify the search string
4023 pat = vim_strsave(xpc.xp_pattern);
4024 else
4025 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4026
Bram Moolenaar93a10962022-06-16 11:42:09 +01004027 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004028 {
4029 int i;
4030
4031 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4032
4033 for (i = 0; i < xpc.xp_numfiles; i++)
4034 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4035 }
4036 vim_free(pat);
4037 ExpandCleanup(&xpc);
4038}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004039#endif // FEAT_EVAL