blob: 2dbde4ef7751684b74c36a56eb124437d2cb98bf [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
95ExpandEscape(
96 expand_T *xp,
97 char_u *str,
98 int numfiles,
99 char_u **files,
100 int options)
101{
102 int i;
103 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100104 int vse_what = xp->xp_context == EXPAND_BUFFERS
105 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200106
107 // May change home directory back to "~"
108 if (options & WILD_HOME_REPLACE)
109 tilde_replace(str, numfiles, files);
110
111 if (options & WILD_ESCAPE)
112 {
113 if (xp->xp_context == EXPAND_FILES
114 || xp->xp_context == EXPAND_FILES_IN_PATH
115 || xp->xp_context == EXPAND_SHELLCMD
116 || xp->xp_context == EXPAND_BUFFERS
117 || xp->xp_context == EXPAND_DIRECTORIES)
118 {
119 // Insert a backslash into a file name before a space, \, %, #
120 // and wildmatch characters, except '~'.
121 for (i = 0; i < numfiles; ++i)
122 {
123 // for ":set path=" we need to escape spaces twice
124 if (xp->xp_backslash == XP_BS_THREE)
125 {
126 p = vim_strsave_escaped(files[i], (char_u *)" ");
127 if (p != NULL)
128 {
129 vim_free(files[i]);
130 files[i] = p;
131#if defined(BACKSLASH_IN_FILENAME)
132 p = vim_strsave_escaped(files[i], (char_u *)" ");
133 if (p != NULL)
134 {
135 vim_free(files[i]);
136 files[i] = p;
137 }
138#endif
139 }
140 }
141#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100142 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200143#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100144 p = vim_strsave_fnameescape(files[i],
145 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200146#endif
147 if (p != NULL)
148 {
149 vim_free(files[i]);
150 files[i] = p;
151 }
152
153 // If 'str' starts with "\~", replace "~" at start of
154 // files[i] with "\~".
155 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
156 escape_fname(&files[i]);
157 }
158 xp->xp_backslash = XP_BS_NONE;
159
160 // If the first file starts with a '+' escape it. Otherwise it
161 // could be seen as "+cmd".
162 if (*files[0] == '+')
163 escape_fname(&files[0]);
164 }
165 else if (xp->xp_context == EXPAND_TAGS)
166 {
167 // Insert a backslash before characters in a tag name that
168 // would terminate the ":tag" command.
169 for (i = 0; i < numfiles; ++i)
170 {
171 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
172 if (p != NULL)
173 {
174 vim_free(files[i]);
175 files[i] = p;
176 }
177 }
178 }
179 }
180}
181
182/*
183 * Return FAIL if this is not an appropriate context in which to do
184 * completion of anything, return OK if it is (even if there are no matches).
185 * For the caller, this means that the character is just passed through like a
186 * normal character (instead of being expanded). This allows :s/^I^D etc.
187 */
188 int
189nextwild(
190 expand_T *xp,
191 int type,
192 int options, // extra options for ExpandOne()
193 int escape) // if TRUE, escape the returned matches
194{
195 cmdline_info_T *ccline = get_cmdline_info();
196 int i, j;
197 char_u *p1;
198 char_u *p2;
199 int difflen;
200 int v;
201
202 if (xp->xp_numfiles == -1)
203 {
204 set_expand_context(xp);
205 cmd_showtail = expand_showtail(xp);
206 }
207
208 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
209 {
210 beep_flush();
211 return OK; // Something illegal on command line
212 }
213 if (xp->xp_context == EXPAND_NOTHING)
214 {
215 // Caller can use the character as a normal char instead
216 return FAIL;
217 }
218
219 msg_puts("..."); // show that we are busy
220 out_flush();
221
222 i = (int)(xp->xp_pattern - ccline->cmdbuff);
223 xp->xp_pattern_len = ccline->cmdpos - i;
224
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000225 if (type == WILD_NEXT || type == WILD_PREV
226 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200227 {
228 // Get next/previous match for a previous expanded pattern.
229 p2 = ExpandOne(xp, NULL, NULL, 0, type);
230 }
231 else
232 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000233 if (cmdline_fuzzy_completion_supported(xp))
234 // If fuzzy matching, don't modify the search string
235 p1 = vim_strsave(xp->xp_pattern);
236 else
237 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
238
Bram Moolenaar66b51422019-08-18 21:44:12 +0200239 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000240 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200241 p2 = NULL;
242 else
243 {
244 int use_options = options |
245 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
246 if (escape)
247 use_options |= WILD_ESCAPE;
248
249 if (p_wic)
250 use_options += WILD_ICASE;
251 p2 = ExpandOne(xp, p1,
252 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
253 use_options, type);
254 vim_free(p1);
255 // longest match: make sure it is not shorter, happens with :help
256 if (p2 != NULL && type == WILD_LONGEST)
257 {
258 for (j = 0; j < xp->xp_pattern_len; ++j)
259 if (ccline->cmdbuff[i + j] == '*'
260 || ccline->cmdbuff[i + j] == '?')
261 break;
262 if ((int)STRLEN(p2) < j)
263 VIM_CLEAR(p2);
264 }
265 }
266 }
267
268 if (p2 != NULL && !got_int)
269 {
270 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
271 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
272 {
273 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
274 xp->xp_pattern = ccline->cmdbuff + i;
275 }
276 else
277 v = OK;
278 if (v == OK)
279 {
280 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
281 &ccline->cmdbuff[ccline->cmdpos],
282 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
283 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
284 ccline->cmdlen += difflen;
285 ccline->cmdpos += difflen;
286 }
287 }
288 vim_free(p2);
289
290 redrawcmd();
291 cursorcmd();
292
293 // When expanding a ":map" command and no matches are found, assume that
294 // the key is supposed to be inserted literally
295 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
296 return FAIL;
297
298 if (xp->xp_numfiles <= 0 && p2 == NULL)
299 beep_flush();
300 else if (xp->xp_numfiles == 1)
301 // free expanded pattern
302 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
303
304 return OK;
305}
306
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000307/*
308 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000309 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000310 */
311 static int
312cmdline_pum_create(
313 cmdline_info_T *ccline,
314 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000315 char_u **matches,
316 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000317 int showtail)
318{
319 int i;
320 int columns;
321
322 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000323 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000324 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000325 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000326 {
327 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
328 compl_match_array[i].pum_info = NULL;
329 compl_match_array[i].pum_extra = NULL;
330 compl_match_array[i].pum_kind = NULL;
331 }
332
333 // Compute the popup menu starting column
334 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
335 columns = vim_strsize(xp->xp_pattern);
336 if (showtail)
337 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000338 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000339 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000340 }
341 if (columns >= compl_startcol)
342 compl_startcol = 0;
343 else
344 compl_startcol -= columns;
345
346 // no default selection
347 compl_selected = -1;
348
349 cmdline_pum_display();
350
351 return EXPAND_OK;
352}
353
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000354/*
355 * Display the cmdline completion matches in a popup menu
356 */
357void cmdline_pum_display(void)
358{
359 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
360}
361
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000362/*
363 * Returns TRUE if the cmdline completion popup menu is being displayed.
364 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000365int cmdline_pum_active(void)
366{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100367 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000368}
369
370/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000371 * Remove the cmdline completion popup menu (if present), free the list of
372 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000373 */
374void cmdline_pum_remove(void)
375{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000376 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100377 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000378
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000379 pum_undisplay();
380 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000381 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000382 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000383 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000384 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100385
386 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
387 // as a side effect.
388 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000389}
390
391void cmdline_pum_cleanup(cmdline_info_T *cclp)
392{
393 cmdline_pum_remove();
394 wildmenu_cleanup(cclp);
395}
396
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000397/*
398 * Returns the starting column number to use for the cmdline completion popup
399 * menu.
400 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000401int cmdline_compl_startcol(void)
402{
403 return compl_startcol;
404}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000405
Bram Moolenaar66b51422019-08-18 21:44:12 +0200406/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000407 * Return the number of characters that should be skipped in a status match.
408 * These are backslashes used for escaping. Do show backslashes in help tags.
409 */
410 static int
411skip_status_match_char(expand_T *xp, char_u *s)
412{
413 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
414#ifdef FEAT_MENU
415 || ((xp->xp_context == EXPAND_MENUS
416 || xp->xp_context == EXPAND_MENUNAMES)
417 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
418#endif
419 )
420 {
421#ifndef BACKSLASH_IN_FILENAME
422 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
423 return 2;
424#endif
425 return 1;
426 }
427 return 0;
428}
429
430/*
431 * Get the length of an item as it will be shown in the status line.
432 */
433 static int
434status_match_len(expand_T *xp, char_u *s)
435{
436 int len = 0;
437
438#ifdef FEAT_MENU
439 int emenu = xp->xp_context == EXPAND_MENUS
440 || xp->xp_context == EXPAND_MENUNAMES;
441
442 // Check for menu separators - replace with '|'.
443 if (emenu && menu_is_separator(s))
444 return 1;
445#endif
446
447 while (*s != NUL)
448 {
449 s += skip_status_match_char(xp, s);
450 len += ptr2cells(s);
451 MB_PTR_ADV(s);
452 }
453
454 return len;
455}
456
457/*
458 * Show wildchar matches in the status line.
459 * Show at least the "match" item.
460 * We start at item 'first_match' in the list and show all matches that fit.
461 *
462 * If inversion is possible we use it. Else '=' characters are used.
463 */
464 static void
465win_redr_status_matches(
466 expand_T *xp,
467 int num_matches,
468 char_u **matches, // list of matches
469 int match,
470 int showtail)
471{
472#define L_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
473 int row;
474 char_u *buf;
475 int len;
476 int clen; // length in screen cells
477 int fillchar;
478 int attr;
479 int i;
480 int highlight = TRUE;
481 char_u *selstart = NULL;
482 int selstart_col = 0;
483 char_u *selend = NULL;
484 static int first_match = 0;
485 int add_left = FALSE;
486 char_u *s;
487#ifdef FEAT_MENU
488 int emenu;
489#endif
490 int l;
491
492 if (matches == NULL) // interrupted completion?
493 return;
494
495 if (has_mbyte)
496 buf = alloc(Columns * MB_MAXBYTES + 1);
497 else
498 buf = alloc(Columns + 1);
499 if (buf == NULL)
500 return;
501
502 if (match == -1) // don't show match but original text
503 {
504 match = 0;
505 highlight = FALSE;
506 }
507 // count 1 for the ending ">"
508 clen = status_match_len(xp, L_MATCH(match)) + 3;
509 if (match == 0)
510 first_match = 0;
511 else if (match < first_match)
512 {
513 // jumping left, as far as we can go
514 first_match = match;
515 add_left = TRUE;
516 }
517 else
518 {
519 // check if match fits on the screen
520 for (i = first_match; i < match; ++i)
521 clen += status_match_len(xp, L_MATCH(i)) + 2;
522 if (first_match > 0)
523 clen += 2;
524 // jumping right, put match at the left
525 if ((long)clen > Columns)
526 {
527 first_match = match;
528 // if showing the last match, we can add some on the left
529 clen = 2;
530 for (i = match; i < num_matches; ++i)
531 {
532 clen += status_match_len(xp, L_MATCH(i)) + 2;
533 if ((long)clen >= Columns)
534 break;
535 }
536 if (i == num_matches)
537 add_left = TRUE;
538 }
539 }
540 if (add_left)
541 while (first_match > 0)
542 {
543 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
544 if ((long)clen >= Columns)
545 break;
546 --first_match;
547 }
548
549 fillchar = fillchar_status(&attr, curwin);
550
551 if (first_match == 0)
552 {
553 *buf = NUL;
554 len = 0;
555 }
556 else
557 {
558 STRCPY(buf, "< ");
559 len = 2;
560 }
561 clen = len;
562
563 i = first_match;
564 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
565 {
566 if (i == match)
567 {
568 selstart = buf + len;
569 selstart_col = clen;
570 }
571
572 s = L_MATCH(i);
573 // Check for menu separators - replace with '|'
574#ifdef FEAT_MENU
575 emenu = (xp->xp_context == EXPAND_MENUS
576 || xp->xp_context == EXPAND_MENUNAMES);
577 if (emenu && menu_is_separator(s))
578 {
579 STRCPY(buf + len, transchar('|'));
580 l = (int)STRLEN(buf + len);
581 len += l;
582 clen += l;
583 }
584 else
585#endif
586 for ( ; *s != NUL; ++s)
587 {
588 s += skip_status_match_char(xp, s);
589 clen += ptr2cells(s);
590 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
591 {
592 STRNCPY(buf + len, s, l);
593 s += l - 1;
594 len += l;
595 }
596 else
597 {
598 STRCPY(buf + len, transchar_byte(*s));
599 len += (int)STRLEN(buf + len);
600 }
601 }
602 if (i == match)
603 selend = buf + len;
604
605 *(buf + len++) = ' ';
606 *(buf + len++) = ' ';
607 clen += 2;
608 if (++i == num_matches)
609 break;
610 }
611
612 if (i != num_matches)
613 {
614 *(buf + len++) = '>';
615 ++clen;
616 }
617
618 buf[len] = NUL;
619
620 row = cmdline_row - 1;
621 if (row >= 0)
622 {
623 if (wild_menu_showing == 0)
624 {
625 if (msg_scrolled > 0)
626 {
627 // Put the wildmenu just above the command line. If there is
628 // no room, scroll the screen one line up.
629 if (cmdline_row == Rows - 1)
630 {
631 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
632 ++msg_scrolled;
633 }
634 else
635 {
636 ++cmdline_row;
637 ++row;
638 }
639 wild_menu_showing = WM_SCROLLED;
640 }
641 else
642 {
643 // Create status line if needed by setting 'laststatus' to 2.
644 // Set 'winminheight' to zero to avoid that the window is
645 // resized.
646 if (lastwin->w_status_height == 0)
647 {
648 save_p_ls = p_ls;
649 save_p_wmh = p_wmh;
650 p_ls = 2;
651 p_wmh = 0;
652 last_status(FALSE);
653 }
654 wild_menu_showing = WM_SHOWN;
655 }
656 }
657
658 screen_puts(buf, row, 0, attr);
659 if (selstart != NULL && highlight)
660 {
661 *selend = NUL;
662 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
663 }
664
665 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
666 }
667
668 win_redraw_last_status(topframe);
669 vim_free(buf);
670}
671
672/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000673 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000674 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000675 */
676 static char_u *
677get_next_or_prev_match(
678 int mode,
679 expand_T *xp,
680 int *p_findex,
681 char_u *orig_save)
682{
683 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000684 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000685
686 if (xp->xp_numfiles <= 0)
687 return NULL;
688
689 if (mode == WILD_PREV)
690 {
691 if (findex == -1)
692 findex = xp->xp_numfiles;
693 --findex;
694 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000695 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000696 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000697 else if (mode == WILD_PAGEUP)
698 {
699 if (findex == 0)
700 // at the first entry, don't select any entries
701 findex = -1;
702 else if (findex == -1)
703 // no entry is selected. select the last entry
704 findex = xp->xp_numfiles - 1;
705 else
706 {
707 // go up by the pum height
708 ht = pum_get_height();
709 if (ht > 3)
710 ht -= 2;
711 findex -= ht;
712 if (findex < 0)
713 // few entries left, select the first entry
714 findex = 0;
715 }
716 }
717 else // mode == WILD_PAGEDOWN
718 {
719 if (findex == xp->xp_numfiles - 1)
720 // at the last entry, don't select any entries
721 findex = -1;
722 else if (findex == -1)
723 // no entry is selected. select the first entry
724 findex = 0;
725 else
726 {
727 // go down by the pum height
728 ht = pum_get_height();
729 if (ht > 3)
730 ht -= 2;
731 findex += ht;
732 if (findex >= xp->xp_numfiles)
733 // few entries left, select the last entry
734 findex = xp->xp_numfiles - 1;
735 }
736 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000737
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000738 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000739 if (findex < 0)
740 {
741 if (orig_save == NULL)
742 findex = xp->xp_numfiles - 1;
743 else
744 findex = -1;
745 }
746 if (findex >= xp->xp_numfiles)
747 {
748 if (orig_save == NULL)
749 findex = 0;
750 else
751 findex = -1;
752 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000753 if (compl_match_array)
754 {
755 compl_selected = findex;
756 cmdline_pum_display();
757 }
758 else if (p_wmnu)
759 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
760 findex, cmd_showtail);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000761 *p_findex = findex;
762
763 if (findex == -1)
764 return vim_strsave(orig_save);
765
766 return vim_strsave(xp->xp_files[findex]);
767}
768
769/*
770 * Start the command-line expansion and get the matches.
771 */
772 static char_u *
773ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
774{
775 int non_suf_match; // number without matching suffix
776 int i;
777 char_u *ss = NULL;
778
779 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000780 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000781 options) == FAIL)
782 {
783#ifdef FNAME_ILLEGAL
784 // Illegal file name has been silently skipped. But when there
785 // are wildcards, the real problem is that there was no match,
786 // causing the pattern to be added, which has illegal characters.
787 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
788 semsg(_(e_no_match_str_2), str);
789#endif
790 }
791 else if (xp->xp_numfiles == 0)
792 {
793 if (!(options & WILD_SILENT))
794 semsg(_(e_no_match_str_2), str);
795 }
796 else
797 {
798 // Escape the matches for use on the command line.
799 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
800
801 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000802 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000803 {
804 if (xp->xp_numfiles)
805 non_suf_match = xp->xp_numfiles;
806 else
807 non_suf_match = 1;
808 if ((xp->xp_context == EXPAND_FILES
809 || xp->xp_context == EXPAND_DIRECTORIES)
810 && xp->xp_numfiles > 1)
811 {
812 // More than one match; check suffix.
813 // The files will have been sorted on matching suffix in
814 // expand_wildcards, only need to check the first two.
815 non_suf_match = 0;
816 for (i = 0; i < 2; ++i)
817 if (match_suffix(xp->xp_files[i]))
818 ++non_suf_match;
819 }
820 if (non_suf_match != 1)
821 {
822 // Can we ever get here unless it's while expanding
823 // interactively? If not, we can get rid of this all
824 // together. Don't really want to wait for this message
825 // (and possibly have to hit return to continue!).
826 if (!(options & WILD_SILENT))
827 emsg(_(e_too_many_file_names));
828 else if (!(options & WILD_NO_BEEP))
829 beep_flush();
830 }
831 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
832 ss = vim_strsave(xp->xp_files[0]);
833 }
834 }
835
836 return ss;
837}
838
839/*
840 * Return the longest common part in the list of cmdline completion matches.
841 */
842 static char_u *
843find_longest_match(expand_T *xp, int options)
844{
845 long_u len;
846 int mb_len = 1;
847 int c0, ci;
848 int i;
849 char_u *ss;
850
851 for (len = 0; xp->xp_files[0][len]; len += mb_len)
852 {
853 if (has_mbyte)
854 {
855 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
856 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
857 }
858 else
859 c0 = xp->xp_files[0][len];
860 for (i = 1; i < xp->xp_numfiles; ++i)
861 {
862 if (has_mbyte)
863 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
864 else
865 ci = xp->xp_files[i][len];
866 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
867 || xp->xp_context == EXPAND_FILES
868 || xp->xp_context == EXPAND_SHELLCMD
869 || xp->xp_context == EXPAND_BUFFERS))
870 {
871 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
872 break;
873 }
874 else if (c0 != ci)
875 break;
876 }
877 if (i < xp->xp_numfiles)
878 {
879 if (!(options & WILD_NO_BEEP))
880 vim_beep(BO_WILD);
881 break;
882 }
883 }
884
885 ss = alloc(len + 1);
886 if (ss)
887 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
888
889 return ss;
890}
891
892/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000893 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200894 * Chars that should not be expanded must be preceded with a backslash.
895 * Return a pointer to allocated memory containing the new string.
896 * Return NULL for failure.
897 *
898 * "orig" is the originally expanded string, copied to allocated memory. It
899 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
900 * WILD_PREV "orig" should be NULL.
901 *
902 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
903 * is WILD_EXPAND_FREE or WILD_ALL.
904 *
905 * mode = WILD_FREE: just free previously expanded matches
906 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
907 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
908 * mode = WILD_NEXT: use next match in multiple match, wrap to first
909 * mode = WILD_PREV: use previous match in multiple match, wrap to first
910 * mode = WILD_ALL: return all matches concatenated
911 * mode = WILD_LONGEST: return longest matched part
912 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000913 * mode = WILD_APPLY: apply the item selected in the cmdline completion
914 * popup menu and close the menu.
915 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
916 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200917 *
918 * options = WILD_LIST_NOTFOUND: list entries without a match
919 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
920 * options = WILD_USE_NL: Use '\n' for WILD_ALL
921 * options = WILD_NO_BEEP: Don't beep for multiple matches
922 * options = WILD_ADD_SLASH: add a slash after directory names
923 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
924 * options = WILD_SILENT: don't print warning messages
925 * options = WILD_ESCAPE: put backslash before special chars
926 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200927 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200928 *
929 * The variables xp->xp_context and xp->xp_backslash must have been set!
930 */
931 char_u *
932ExpandOne(
933 expand_T *xp,
934 char_u *str,
935 char_u *orig, // allocated copy of original of expanded string
936 int options,
937 int mode)
938{
939 char_u *ss = NULL;
940 static int findex;
941 static char_u *orig_save = NULL; // kept value of orig
942 int orig_saved = FALSE;
943 int i;
944 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200945
946 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000947 if (mode == WILD_NEXT || mode == WILD_PREV
948 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000949 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200950
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000951 if (mode == WILD_CANCEL)
952 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
953 else if (mode == WILD_APPLY)
954 ss = vim_strsave(findex == -1 ? (orig_save ?
955 orig_save : (char_u *)"") : xp->xp_files[findex]);
956
Bram Moolenaar66b51422019-08-18 21:44:12 +0200957 // free old names
958 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
959 {
960 FreeWild(xp->xp_numfiles, xp->xp_files);
961 xp->xp_numfiles = -1;
962 VIM_CLEAR(orig_save);
963 }
964 findex = 0;
965
966 if (mode == WILD_FREE) // only release file name
967 return NULL;
968
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000969 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200970 {
971 vim_free(orig_save);
972 orig_save = orig;
973 orig_saved = TRUE;
974
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000975 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200976 }
977
978 // Find longest common part
979 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
980 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000981 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200982 findex = -1; // next p_wc gets first one
983 }
984
Bram Moolenaar57e95172022-08-20 19:26:14 +0100985 // Concatenate all matching names. Unless interrupted, this can be slow
986 // and the result probably won't be used.
987 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200988 {
989 len = 0;
990 for (i = 0; i < xp->xp_numfiles; ++i)
991 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
992 ss = alloc(len);
993 if (ss != NULL)
994 {
995 *ss = NUL;
996 for (i = 0; i < xp->xp_numfiles; ++i)
997 {
998 STRCAT(ss, xp->xp_files[i]);
999 if (i != xp->xp_numfiles - 1)
1000 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1001 }
1002 }
1003 }
1004
1005 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1006 ExpandCleanup(xp);
1007
1008 // Free "orig" if it wasn't stored in "orig_save".
1009 if (!orig_saved)
1010 vim_free(orig);
1011
1012 return ss;
1013}
1014
1015/*
1016 * Prepare an expand structure for use.
1017 */
1018 void
1019ExpandInit(expand_T *xp)
1020{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001021 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001022 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001023 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001024}
1025
1026/*
1027 * Cleanup an expand structure after use.
1028 */
1029 void
1030ExpandCleanup(expand_T *xp)
1031{
1032 if (xp->xp_numfiles >= 0)
1033 {
1034 FreeWild(xp->xp_numfiles, xp->xp_files);
1035 xp->xp_numfiles = -1;
1036 }
1037}
1038
1039/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001040 * Display one line of completion matches. Multiple matches are displayed in
1041 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001042 * matches - list of completion match names
1043 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001044 * lines - number of output lines
1045 * linenr - line number of matches to display
1046 * maxlen - maximum number of characters in each line
1047 * showtail - display only the tail of the full path of a file name
1048 * dir_attr - highlight attribute to use for directory names
1049 */
1050 static void
1051showmatches_oneline(
1052 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001053 char_u **matches,
1054 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001055 int lines,
1056 int linenr,
1057 int maxlen,
1058 int showtail,
1059 int dir_attr)
1060{
1061 int i, j;
1062 int isdir;
1063 int lastlen;
1064 char_u *p;
1065
1066 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001067 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001068 {
1069 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1070 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001071 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1072 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001073 msg_advance(maxlen + 1);
1074 msg_puts((char *)p);
1075 msg_advance(maxlen + 3);
1076 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1077 break;
1078 }
1079 for (i = maxlen - lastlen; --i >= 0; )
1080 msg_putchar(' ');
1081 if (xp->xp_context == EXPAND_FILES
1082 || xp->xp_context == EXPAND_SHELLCMD
1083 || xp->xp_context == EXPAND_BUFFERS)
1084 {
1085 // highlight directories
1086 if (xp->xp_numfiles != -1)
1087 {
1088 char_u *halved_slash;
1089 char_u *exp_path;
1090 char_u *path;
1091
1092 // Expansion was done before and special characters
1093 // were escaped, need to halve backslashes. Also
1094 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001095 exp_path = expand_env_save_opt(matches[j], TRUE);
1096 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001097 halved_slash = backslash_halve_save(path);
1098 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001099 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001100 vim_free(exp_path);
1101 if (halved_slash != path)
1102 vim_free(halved_slash);
1103 }
1104 else
1105 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001106 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001107 if (showtail)
1108 p = SHOW_FILE_TEXT(j);
1109 else
1110 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001111 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001112 TRUE);
1113 p = NameBuff;
1114 }
1115 }
1116 else
1117 {
1118 isdir = FALSE;
1119 p = SHOW_FILE_TEXT(j);
1120 }
1121 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1122 }
1123 if (msg_col > 0) // when not wrapped around
1124 {
1125 msg_clr_eos();
1126 msg_putchar('\n');
1127 }
1128 out_flush(); // show one line at a time
1129}
1130
1131/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001132 * Show all matches for completion on the command line.
1133 * Returns EXPAND_NOTHING when the character that triggered expansion should
1134 * be inserted like a normal character.
1135 */
1136 int
1137showmatches(expand_T *xp, int wildmenu UNUSED)
1138{
1139 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001140 int numMatches;
1141 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001142 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001143 int maxlen;
1144 int lines;
1145 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001146 int attr;
1147 int showtail;
1148
1149 if (xp->xp_numfiles == -1)
1150 {
1151 set_expand_context(xp);
1152 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001153 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001154 showtail = expand_showtail(xp);
1155 if (i != EXPAND_OK)
1156 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001157 }
1158 else
1159 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001160 numMatches = xp->xp_numfiles;
1161 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001162 showtail = cmd_showtail;
1163 }
1164
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001165 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001166 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001167 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001168
Bram Moolenaar66b51422019-08-18 21:44:12 +02001169 if (!wildmenu)
1170 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001171 msg_didany = FALSE; // lines_left will be set
1172 msg_start(); // prepare for paging
1173 msg_putchar('\n');
1174 out_flush();
1175 cmdline_row = msg_row;
1176 msg_didany = FALSE; // lines_left will be set again
1177 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001178 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001179
1180 if (got_int)
1181 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001182 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001183 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001184 else
1185 {
1186 // find the length of the longest file name
1187 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001188 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001189 {
1190 if (!showtail && (xp->xp_context == EXPAND_FILES
1191 || xp->xp_context == EXPAND_SHELLCMD
1192 || xp->xp_context == EXPAND_BUFFERS))
1193 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001194 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001195 j = vim_strsize(NameBuff);
1196 }
1197 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001198 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001199 if (j > maxlen)
1200 maxlen = j;
1201 }
1202
1203 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001204 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001205 else
1206 {
1207 // compute the number of columns and lines for the listing
1208 maxlen += 2; // two spaces between file names
1209 columns = ((int)Columns + 2) / maxlen;
1210 if (columns < 1)
1211 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001212 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001213 }
1214
1215 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1216
1217 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1218 {
1219 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1220 msg_clr_eos();
1221 msg_advance(maxlen - 3);
1222 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1223 }
1224
1225 // list the files line by line
1226 for (i = 0; i < lines; ++i)
1227 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001228 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001229 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001230 if (got_int)
1231 {
1232 got_int = FALSE;
1233 break;
1234 }
1235 }
1236
1237 // we redraw the command below the lines that we have just listed
1238 // This is a bit tricky, but it saves a lot of screen updating.
1239 cmdline_row = msg_row; // will put it back later
1240 }
1241
1242 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001243 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001244
1245 return EXPAND_OK;
1246}
1247
1248/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001249 * gettail() version for showmatches() and win_redr_status_matches():
1250 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001251 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001252 static char_u *
1253showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001254{
1255 char_u *p;
1256 char_u *t = s;
1257 int had_sep = FALSE;
1258
1259 for (p = s; *p != NUL; )
1260 {
1261 if (vim_ispathsep(*p)
1262#ifdef BACKSLASH_IN_FILENAME
1263 && !rem_backslash(p)
1264#endif
1265 )
1266 had_sep = TRUE;
1267 else if (had_sep)
1268 {
1269 t = p;
1270 had_sep = FALSE;
1271 }
1272 MB_PTR_ADV(p);
1273 }
1274 return t;
1275}
1276
1277/*
1278 * Return TRUE if we only need to show the tail of completion matches.
1279 * When not completing file names or there is a wildcard in the path FALSE is
1280 * returned.
1281 */
1282 static int
1283expand_showtail(expand_T *xp)
1284{
1285 char_u *s;
1286 char_u *end;
1287
1288 // When not completing file names a "/" may mean something different.
1289 if (xp->xp_context != EXPAND_FILES
1290 && xp->xp_context != EXPAND_SHELLCMD
1291 && xp->xp_context != EXPAND_DIRECTORIES)
1292 return FALSE;
1293
1294 end = gettail(xp->xp_pattern);
1295 if (end == xp->xp_pattern) // there is no path separator
1296 return FALSE;
1297
1298 for (s = xp->xp_pattern; s < end; s++)
1299 {
1300 // Skip escaped wildcards. Only when the backslash is not a path
1301 // separator, on DOS the '*' "path\*\file" must not be skipped.
1302 if (rem_backslash(s))
1303 ++s;
1304 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1305 return FALSE;
1306 }
1307 return TRUE;
1308}
1309
1310/*
1311 * Prepare a string for expansion.
1312 * When expanding file names: The string will be used with expand_wildcards().
1313 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1314 * When expanding other names: The string will be used with regcomp(). Copy
1315 * the name into allocated memory and prepend "^".
1316 */
1317 char_u *
1318addstar(
1319 char_u *fname,
1320 int len,
1321 int context) // EXPAND_FILES etc.
1322{
1323 char_u *retval;
1324 int i, j;
1325 int new_len;
1326 char_u *tail;
1327 int ends_in_star;
1328
1329 if (context != EXPAND_FILES
1330 && context != EXPAND_FILES_IN_PATH
1331 && context != EXPAND_SHELLCMD
1332 && context != EXPAND_DIRECTORIES)
1333 {
1334 // Matching will be done internally (on something other than files).
1335 // So we convert the file-matching-type wildcards into our kind for
1336 // use with vim_regcomp(). First work out how long it will be:
1337
1338 // For help tags the translation is done in find_help_tags().
1339 // For a tag pattern starting with "/" no translation is needed.
1340 if (context == EXPAND_HELP
1341 || context == EXPAND_COLORS
1342 || context == EXPAND_COMPILER
1343 || context == EXPAND_OWNSYNTAX
1344 || context == EXPAND_FILETYPE
1345 || context == EXPAND_PACKADD
1346 || ((context == EXPAND_TAGS_LISTFILES
1347 || context == EXPAND_TAGS)
1348 && fname[0] == '/'))
1349 retval = vim_strnsave(fname, len);
1350 else
1351 {
1352 new_len = len + 2; // +2 for '^' at start, NUL at end
1353 for (i = 0; i < len; i++)
1354 {
1355 if (fname[i] == '*' || fname[i] == '~')
1356 new_len++; // '*' needs to be replaced by ".*"
1357 // '~' needs to be replaced by "\~"
1358
1359 // Buffer names are like file names. "." should be literal
1360 if (context == EXPAND_BUFFERS && fname[i] == '.')
1361 new_len++; // "." becomes "\."
1362
1363 // Custom expansion takes care of special things, match
1364 // backslashes literally (perhaps also for other types?)
1365 if ((context == EXPAND_USER_DEFINED
1366 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1367 new_len++; // '\' becomes "\\"
1368 }
1369 retval = alloc(new_len);
1370 if (retval != NULL)
1371 {
1372 retval[0] = '^';
1373 j = 1;
1374 for (i = 0; i < len; i++, j++)
1375 {
1376 // Skip backslash. But why? At least keep it for custom
1377 // expansion.
1378 if (context != EXPAND_USER_DEFINED
1379 && context != EXPAND_USER_LIST
1380 && fname[i] == '\\'
1381 && ++i == len)
1382 break;
1383
1384 switch (fname[i])
1385 {
1386 case '*': retval[j++] = '.';
1387 break;
1388 case '~': retval[j++] = '\\';
1389 break;
1390 case '?': retval[j] = '.';
1391 continue;
1392 case '.': if (context == EXPAND_BUFFERS)
1393 retval[j++] = '\\';
1394 break;
1395 case '\\': if (context == EXPAND_USER_DEFINED
1396 || context == EXPAND_USER_LIST)
1397 retval[j++] = '\\';
1398 break;
1399 }
1400 retval[j] = fname[i];
1401 }
1402 retval[j] = NUL;
1403 }
1404 }
1405 }
1406 else
1407 {
1408 retval = alloc(len + 4);
1409 if (retval != NULL)
1410 {
1411 vim_strncpy(retval, fname, len);
1412
1413 // Don't add a star to *, ~, ~user, $var or `cmd`.
1414 // * would become **, which walks the whole tree.
1415 // ~ would be at the start of the file name, but not the tail.
1416 // $ could be anywhere in the tail.
1417 // ` could be anywhere in the file name.
1418 // When the name ends in '$' don't add a star, remove the '$'.
1419 tail = gettail(retval);
1420 ends_in_star = (len > 0 && retval[len - 1] == '*');
1421#ifndef BACKSLASH_IN_FILENAME
1422 for (i = len - 2; i >= 0; --i)
1423 {
1424 if (retval[i] != '\\')
1425 break;
1426 ends_in_star = !ends_in_star;
1427 }
1428#endif
1429 if ((*retval != '~' || tail != retval)
1430 && !ends_in_star
1431 && vim_strchr(tail, '$') == NULL
1432 && vim_strchr(retval, '`') == NULL)
1433 retval[len++] = '*';
1434 else if (len > 0 && retval[len - 1] == '$')
1435 --len;
1436 retval[len] = NUL;
1437 }
1438 }
1439 return retval;
1440}
1441
1442/*
1443 * Must parse the command line so far to work out what context we are in.
1444 * Completion can then be done based on that context.
1445 * This routine sets the variables:
1446 * xp->xp_pattern The start of the pattern to be expanded within
1447 * the command line (ends at the cursor).
1448 * xp->xp_context The type of thing to expand. Will be one of:
1449 *
1450 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1451 * the command line, like an unknown command. Caller
1452 * should beep.
1453 * EXPAND_NOTHING Unrecognised context for completion, use char like
1454 * a normal char, rather than for completion. eg
1455 * :s/^I/
1456 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1457 * it.
1458 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1459 * EXPAND_FILES After command with EX_XFILE set, or after setting
1460 * with P_EXPAND set. eg :e ^I, :w>>^I
1461 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1462 * when we know only directories are of interest. eg
1463 * :set dir=^I
1464 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1465 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1466 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1467 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1468 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1469 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1470 * EXPAND_EVENTS Complete event names
1471 * EXPAND_SYNTAX Complete :syntax command arguments
1472 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1473 * EXPAND_AUGROUP Complete autocommand group names
1474 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1475 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1476 * eg :unmap a^I , :cunab x^I
1477 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1478 * eg :call sub^I
1479 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1480 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1481 * names in expressions, eg :while s^I
1482 * EXPAND_ENV_VARS Complete environment variable names
1483 * EXPAND_USER Complete user names
1484 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001485 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001486set_expand_context(expand_T *xp)
1487{
1488 cmdline_info_T *ccline = get_cmdline_info();
1489
1490 // only expansion for ':', '>' and '=' command-lines
1491 if (ccline->cmdfirstc != ':'
1492#ifdef FEAT_EVAL
1493 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1494 && !ccline->input_fn
1495#endif
1496 )
1497 {
1498 xp->xp_context = EXPAND_NOTHING;
1499 return;
1500 }
1501 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1502}
1503
Bram Moolenaard0190392019-08-23 21:17:35 +02001504/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001505 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1506 * For user defined commands, the completion context is set in 'xp' and the
1507 * completion flags in 'complp'.
1508 *
1509 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001510 */
1511 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001512set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001513{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001514 char_u *p = NULL;
1515 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001516 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001517
1518 // Isolate the command and search for it in the command table.
1519 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001520 // - the 'k' command can directly be followed by any character, but do
1521 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1522 // find matches anywhere in the command name, do this only for command
1523 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001524 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001525 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001526 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001527 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001528 p = cmd + 1;
1529 }
1530 else
1531 {
1532 p = cmd;
1533 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1534 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001535 // A user command may contain digits.
1536 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1537 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001538 while (ASCII_ISALNUM(*p) || *p == '*')
1539 ++p;
1540 // for python 3.x: ":py3*" commands completion
1541 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1542 {
1543 ++p;
1544 while (ASCII_ISALPHA(*p) || *p == '*')
1545 ++p;
1546 }
1547 // check for non-alpha command
1548 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1549 ++p;
1550 len = (int)(p - cmd);
1551
1552 if (len == 0)
1553 {
1554 xp->xp_context = EXPAND_UNSUCCESSFUL;
1555 return NULL;
1556 }
1557
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001558 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001559
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001560 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001561 // Also when doing fuzzy expansion for non-shell commands, support
1562 // alphanumeric characters.
1563 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1564 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001565 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1566 ++p;
1567 }
1568
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001569 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001570 // character, complete the command name.
1571 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1572 return NULL;
1573
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001574 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001575 {
1576 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1577 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001578 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001579 p = cmd + 1;
1580 }
1581 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1582 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001583 eap->cmd = cmd;
1584 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001585 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001586 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001587 }
1588 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001589 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001590 {
1591 // Not still touching the command and it was an illegal one
1592 xp->xp_context = EXPAND_UNSUCCESSFUL;
1593 return NULL;
1594 }
1595
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001596 return p;
1597}
Bram Moolenaard0190392019-08-23 21:17:35 +02001598
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001599/*
1600 * Set the completion context for a command argument with wild card characters.
1601 */
1602 static void
1603set_context_for_wildcard_arg(
1604 exarg_T *eap,
1605 char_u *arg,
1606 int usefilter,
1607 expand_T *xp,
1608 int *complp)
1609{
1610 char_u *p;
1611 int c;
1612 int in_quote = FALSE;
1613 char_u *bow = NULL; // Beginning of word
1614 int len = 0;
1615
1616 // Allow spaces within back-quotes to count as part of the argument
1617 // being expanded.
1618 xp->xp_pattern = skipwhite(arg);
1619 p = xp->xp_pattern;
1620 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001621 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001622 if (has_mbyte)
1623 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001624 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001625 c = *p;
1626 if (c == '\\' && p[1] != NUL)
1627 ++p;
1628 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001629 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001630 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001631 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001632 xp->xp_pattern = p;
1633 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001634 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001635 in_quote = !in_quote;
1636 }
1637 // An argument can contain just about everything, except
1638 // characters that end the command and white space.
1639 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001640#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001641 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001642#endif
1643 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001644 {
1645 len = 0; // avoid getting stuck when space is in 'isfname'
1646 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001647 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001648 if (has_mbyte)
1649 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001650 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001651 c = *p;
1652 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001653 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001654 if (has_mbyte)
1655 len = (*mb_ptr2len)(p);
1656 else
1657 len = 1;
1658 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001659 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001660 if (in_quote)
1661 bow = p;
1662 else
1663 xp->xp_pattern = p;
1664 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001665 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001667 }
1668
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 // If we are still inside the quotes, and we passed a space, just
1670 // expand from there.
1671 if (bow != NULL && in_quote)
1672 xp->xp_pattern = bow;
1673 xp->xp_context = EXPAND_FILES;
1674
1675 // For a shell command more chars need to be escaped.
1676 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1677 {
1678#ifndef BACKSLASH_IN_FILENAME
1679 xp->xp_shell = TRUE;
1680#endif
1681 // When still after the command name expand executables.
1682 if (xp->xp_pattern == skipwhite(arg))
1683 xp->xp_context = EXPAND_SHELLCMD;
1684 }
1685
1686 // Check for environment variable.
1687 if (*xp->xp_pattern == '$')
1688 {
1689 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1690 if (!vim_isIDc(*p))
1691 break;
1692 if (*p == NUL)
1693 {
1694 xp->xp_context = EXPAND_ENV_VARS;
1695 ++xp->xp_pattern;
1696 // Avoid that the assignment uses EXPAND_FILES again.
1697 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1698 *complp = EXPAND_ENV_VARS;
1699 }
1700 }
1701 // Check for user names.
1702 if (*xp->xp_pattern == '~')
1703 {
1704 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1705 ;
1706 // Complete ~user only if it partially matches a user name.
1707 // A full match ~user<Tab> will be replaced by user's home
1708 // directory i.e. something like ~user<Tab> -> /home/user/
1709 if (*p == NUL && p > xp->xp_pattern + 1
1710 && match_user(xp->xp_pattern + 1) >= 1)
1711 {
1712 xp->xp_context = EXPAND_USER;
1713 ++xp->xp_pattern;
1714 }
1715 }
1716}
1717
1718/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001719 * Set the completion context for the :filter command. Returns a pointer to the
1720 * next command after the :filter command.
1721 */
1722 static char_u *
1723set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1724{
1725 if (*arg != NUL)
1726 arg = skip_vimgrep_pat(arg, NULL, NULL);
1727 if (arg == NULL || *arg == NUL)
1728 {
1729 xp->xp_context = EXPAND_NOTHING;
1730 return NULL;
1731 }
1732 return skipwhite(arg);
1733}
1734
1735#ifdef FEAT_SEARCH_EXTRA
1736/*
1737 * Set the completion context for the :match command. Returns a pointer to the
1738 * next command after the :match command.
1739 */
1740 static char_u *
1741set_context_in_match_cmd(expand_T *xp, char_u *arg)
1742{
1743 if (*arg == NUL || !ends_excmd(*arg))
1744 {
1745 // also complete "None"
1746 set_context_in_echohl_cmd(xp, arg);
1747 arg = skipwhite(skiptowhite(arg));
1748 if (*arg != NUL)
1749 {
1750 xp->xp_context = EXPAND_NOTHING;
1751 arg = skip_regexp(arg + 1, *arg, magic_isset());
1752 }
1753 }
1754 return find_nextcmd(arg);
1755}
1756#endif
1757
1758/*
1759 * Returns a pointer to the next command after a :global or a :v command.
1760 * Returns NULL if there is no next command.
1761 */
1762 static char_u *
1763find_cmd_after_global_cmd(char_u *arg)
1764{
1765 int delim;
1766
1767 delim = *arg; // get the delimiter
1768 if (delim)
1769 ++arg; // skip delimiter if there is one
1770
1771 while (arg[0] != NUL && arg[0] != delim)
1772 {
1773 if (arg[0] == '\\' && arg[1] != NUL)
1774 ++arg;
1775 ++arg;
1776 }
1777 if (arg[0] != NUL)
1778 return arg + 1;
1779
1780 return NULL;
1781}
1782
1783/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001784 * Returns a pointer to the next command after a :substitute or a :& command.
1785 * Returns NULL if there is no next command.
1786 */
1787 static char_u *
1788find_cmd_after_substitute_cmd(char_u *arg)
1789{
1790 int delim;
1791
1792 delim = *arg;
1793 if (delim)
1794 {
1795 // skip "from" part
1796 ++arg;
1797 arg = skip_regexp(arg, delim, magic_isset());
1798
1799 if (arg[0] != NUL && arg[0] == delim)
1800 {
1801 // skip "to" part
1802 ++arg;
1803 while (arg[0] != NUL && arg[0] != delim)
1804 {
1805 if (arg[0] == '\\' && arg[1] != NUL)
1806 ++arg;
1807 ++arg;
1808 }
1809 if (arg[0] != NUL) // skip delimiter
1810 ++arg;
1811 }
1812 }
1813 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1814 ++arg;
1815 if (arg[0] != NUL)
1816 return arg;
1817
1818 return NULL;
1819}
1820
1821/*
1822 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1823 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1824 * Returns NULL if there is no next command.
1825 */
1826 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001827find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001828{
1829 arg = skipwhite(skipdigits(arg)); // skip count
1830 if (*arg == '/') // Match regexp, not just whole words
1831 {
1832 for (++arg; *arg && *arg != '/'; arg++)
1833 if (*arg == '\\' && arg[1] != NUL)
1834 arg++;
1835 if (*arg)
1836 {
1837 arg = skipwhite(arg + 1);
1838
1839 // Check for trailing illegal characters
1840 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1841 xp->xp_context = EXPAND_NOTHING;
1842 else
1843 return arg;
1844 }
1845 }
1846
1847 return NULL;
1848}
1849
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001850#ifdef FEAT_EVAL
1851/*
1852 * Set the completion context for the :unlet command. Always returns NULL.
1853 */
1854 static char_u *
1855set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1856{
1857 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1858 arg = xp->xp_pattern + 1;
1859
1860 xp->xp_context = EXPAND_USER_VARS;
1861 xp->xp_pattern = arg;
1862
1863 if (*xp->xp_pattern == '$')
1864 {
1865 xp->xp_context = EXPAND_ENV_VARS;
1866 ++xp->xp_pattern;
1867 }
1868
1869 return NULL;
1870}
1871#endif
1872
1873#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1874/*
1875 * Set the completion context for the :language command. Always returns NULL.
1876 */
1877 static char_u *
1878set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1879{
1880 char_u *p;
1881
1882 p = skiptowhite(arg);
1883 if (*p == NUL)
1884 {
1885 xp->xp_context = EXPAND_LANGUAGE;
1886 xp->xp_pattern = arg;
1887 }
1888 else
1889 {
1890 if ( STRNCMP(arg, "messages", p - arg) == 0
1891 || STRNCMP(arg, "ctype", p - arg) == 0
1892 || STRNCMP(arg, "time", p - arg) == 0
1893 || STRNCMP(arg, "collate", p - arg) == 0)
1894 {
1895 xp->xp_context = EXPAND_LOCALES;
1896 xp->xp_pattern = skipwhite(p);
1897 }
1898 else
1899 xp->xp_context = EXPAND_NOTHING;
1900 }
1901
1902 return NULL;
1903}
1904#endif
1905
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001906#ifdef FEAT_EVAL
1907static enum
1908{
1909 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001910 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1911 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001912} breakpt_expand_what;
1913
1914/*
1915 * Set the completion context for the :breakadd command. Always returns NULL.
1916 */
1917 static char_u *
1918set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1919{
1920 char_u *p;
1921 char_u *subcmd_start;
1922
1923 xp->xp_context = EXPAND_BREAKPOINT;
1924 xp->xp_pattern = arg;
1925
1926 if (cmdidx == CMD_breakadd)
1927 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001928 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001929 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001930 else
1931 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001932
1933 p = skipwhite(arg);
1934 if (*p == NUL)
1935 return NULL;
1936 subcmd_start = p;
1937
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001938 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001939 {
1940 // :breakadd file [lnum] <filename>
1941 // :breakadd func [lnum] <funcname>
1942 p += 4;
1943 p = skipwhite(p);
1944
1945 // skip line number (if specified)
1946 if (VIM_ISDIGIT(*p))
1947 {
1948 p = skipdigits(p);
1949 if (*p != ' ')
1950 {
1951 xp->xp_context = EXPAND_NOTHING;
1952 return NULL;
1953 }
1954 p = skipwhite(p);
1955 }
1956 if (STRNCMP("file", subcmd_start, 4) == 0)
1957 xp->xp_context = EXPAND_FILES;
1958 else
1959 xp->xp_context = EXPAND_USER_FUNC;
1960 xp->xp_pattern = p;
1961 }
1962 else if (STRNCMP("expr ", p, 5) == 0)
1963 {
1964 // :breakadd expr <expression>
1965 xp->xp_context = EXPAND_EXPRESSION;
1966 xp->xp_pattern = skipwhite(p + 5);
1967 }
1968
1969 return NULL;
1970}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001971
1972 static char_u *
1973set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1974{
1975 char_u *p;
1976
1977 xp->xp_context = EXPAND_NOTHING;
1978 xp->xp_pattern = NULL;
1979
1980 p = skipwhite(arg);
1981 if (VIM_ISDIGIT(*p))
1982 return NULL;
1983
1984 xp->xp_context = EXPAND_SCRIPTNAMES;
1985 xp->xp_pattern = p;
1986
1987 return NULL;
1988}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001989#endif
1990
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001991/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001992 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1993 * The argument to the command is 'arg' and the argument flags is 'argt'.
1994 * For user-defined commands and for environment variables, 'compl' has the
1995 * completion type.
1996 * Returns a pointer to the next command. Returns NULL if there is no next
1997 * command.
1998 */
1999 static char_u *
2000set_context_by_cmdname(
2001 char_u *cmd,
2002 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002003 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002004 char_u *arg,
2005 long argt,
2006 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002007 int forceit)
2008{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002009 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002010 {
2011 case CMD_find:
2012 case CMD_sfind:
2013 case CMD_tabfind:
2014 if (xp->xp_context == EXPAND_FILES)
2015 xp->xp_context = EXPAND_FILES_IN_PATH;
2016 break;
2017 case CMD_cd:
2018 case CMD_chdir:
2019 case CMD_tcd:
2020 case CMD_tchdir:
2021 case CMD_lcd:
2022 case CMD_lchdir:
2023 if (xp->xp_context == EXPAND_FILES)
2024 xp->xp_context = EXPAND_DIRECTORIES;
2025 break;
2026 case CMD_help:
2027 xp->xp_context = EXPAND_HELP;
2028 xp->xp_pattern = arg;
2029 break;
2030
2031 // Command modifiers: return the argument.
2032 // Also for commands with an argument that is a command.
2033 case CMD_aboveleft:
2034 case CMD_argdo:
2035 case CMD_belowright:
2036 case CMD_botright:
2037 case CMD_browse:
2038 case CMD_bufdo:
2039 case CMD_cdo:
2040 case CMD_cfdo:
2041 case CMD_confirm:
2042 case CMD_debug:
2043 case CMD_folddoclosed:
2044 case CMD_folddoopen:
2045 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002046 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002047 case CMD_keepalt:
2048 case CMD_keepjumps:
2049 case CMD_keepmarks:
2050 case CMD_keeppatterns:
2051 case CMD_ldo:
2052 case CMD_leftabove:
2053 case CMD_lfdo:
2054 case CMD_lockmarks:
2055 case CMD_noautocmd:
2056 case CMD_noswapfile:
2057 case CMD_rightbelow:
2058 case CMD_sandbox:
2059 case CMD_silent:
2060 case CMD_tab:
2061 case CMD_tabdo:
2062 case CMD_topleft:
2063 case CMD_verbose:
2064 case CMD_vertical:
2065 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002066 case CMD_vim9cmd:
2067 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002068 return arg;
2069
2070 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002071 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002072
2073#ifdef FEAT_SEARCH_EXTRA
2074 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002075 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002076#endif
2077
2078 // All completion for the +cmdline_compl feature goes here.
2079
2080 case CMD_command:
2081 return set_context_in_user_cmd(xp, arg);
2082
2083 case CMD_delcommand:
2084 xp->xp_context = EXPAND_USER_COMMANDS;
2085 xp->xp_pattern = arg;
2086 break;
2087
2088 case CMD_global:
2089 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002090 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002091 case CMD_and:
2092 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002093 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002094 case CMD_isearch:
2095 case CMD_dsearch:
2096 case CMD_ilist:
2097 case CMD_dlist:
2098 case CMD_ijump:
2099 case CMD_psearch:
2100 case CMD_djump:
2101 case CMD_isplit:
2102 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002103 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002104 case CMD_autocmd:
2105 return set_context_in_autocmd(xp, arg, FALSE);
2106 case CMD_doautocmd:
2107 case CMD_doautoall:
2108 return set_context_in_autocmd(xp, arg, TRUE);
2109 case CMD_set:
2110 set_context_in_set_cmd(xp, arg, 0);
2111 break;
2112 case CMD_setglobal:
2113 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2114 break;
2115 case CMD_setlocal:
2116 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2117 break;
2118 case CMD_tag:
2119 case CMD_stag:
2120 case CMD_ptag:
2121 case CMD_ltag:
2122 case CMD_tselect:
2123 case CMD_stselect:
2124 case CMD_ptselect:
2125 case CMD_tjump:
2126 case CMD_stjump:
2127 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002128 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002129 xp->xp_context = EXPAND_TAGS_LISTFILES;
2130 else
2131 xp->xp_context = EXPAND_TAGS;
2132 xp->xp_pattern = arg;
2133 break;
2134 case CMD_augroup:
2135 xp->xp_context = EXPAND_AUGROUP;
2136 xp->xp_pattern = arg;
2137 break;
2138#ifdef FEAT_SYN_HL
2139 case CMD_syntax:
2140 set_context_in_syntax_cmd(xp, arg);
2141 break;
2142#endif
2143#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002144 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002145 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002146 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002147 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002148 case CMD_if:
2149 case CMD_elseif:
2150 case CMD_while:
2151 case CMD_for:
2152 case CMD_echo:
2153 case CMD_echon:
2154 case CMD_execute:
2155 case CMD_echomsg:
2156 case CMD_echoerr:
2157 case CMD_call:
2158 case CMD_return:
2159 case CMD_cexpr:
2160 case CMD_caddexpr:
2161 case CMD_cgetexpr:
2162 case CMD_lexpr:
2163 case CMD_laddexpr:
2164 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002165 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002166 break;
2167
2168 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002169 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002170 case CMD_function:
2171 case CMD_delfunction:
2172 xp->xp_context = EXPAND_USER_FUNC;
2173 xp->xp_pattern = arg;
2174 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002175 case CMD_disassemble:
2176 set_context_in_disassemble_cmd(xp, arg);
2177 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002178
2179 case CMD_echohl:
2180 set_context_in_echohl_cmd(xp, arg);
2181 break;
2182#endif
2183 case CMD_highlight:
2184 set_context_in_highlight_cmd(xp, arg);
2185 break;
2186#ifdef FEAT_CSCOPE
2187 case CMD_cscope:
2188 case CMD_lcscope:
2189 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002190 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002191 break;
2192#endif
2193#ifdef FEAT_SIGNS
2194 case CMD_sign:
2195 set_context_in_sign_cmd(xp, arg);
2196 break;
2197#endif
2198 case CMD_bdelete:
2199 case CMD_bwipeout:
2200 case CMD_bunload:
2201 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2202 arg = xp->xp_pattern + 1;
2203 // FALLTHROUGH
2204 case CMD_buffer:
2205 case CMD_sbuffer:
2206 case CMD_checktime:
2207 xp->xp_context = EXPAND_BUFFERS;
2208 xp->xp_pattern = arg;
2209 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002210#ifdef FEAT_DIFF
2211 case CMD_diffget:
2212 case CMD_diffput:
2213 // If current buffer is in diff mode, complete buffer names
2214 // which are in diff mode, and different than current buffer.
2215 xp->xp_context = EXPAND_DIFF_BUFFERS;
2216 xp->xp_pattern = arg;
2217 break;
2218#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002219 case CMD_USER:
2220 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002221 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2222 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002223
2224 case CMD_map: case CMD_noremap:
2225 case CMD_nmap: case CMD_nnoremap:
2226 case CMD_vmap: case CMD_vnoremap:
2227 case CMD_omap: case CMD_onoremap:
2228 case CMD_imap: case CMD_inoremap:
2229 case CMD_cmap: case CMD_cnoremap:
2230 case CMD_lmap: case CMD_lnoremap:
2231 case CMD_smap: case CMD_snoremap:
2232 case CMD_tmap: case CMD_tnoremap:
2233 case CMD_xmap: case CMD_xnoremap:
2234 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002235 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002236 case CMD_unmap:
2237 case CMD_nunmap:
2238 case CMD_vunmap:
2239 case CMD_ounmap:
2240 case CMD_iunmap:
2241 case CMD_cunmap:
2242 case CMD_lunmap:
2243 case CMD_sunmap:
2244 case CMD_tunmap:
2245 case CMD_xunmap:
2246 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002247 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002248 case CMD_mapclear:
2249 case CMD_nmapclear:
2250 case CMD_vmapclear:
2251 case CMD_omapclear:
2252 case CMD_imapclear:
2253 case CMD_cmapclear:
2254 case CMD_lmapclear:
2255 case CMD_smapclear:
2256 case CMD_tmapclear:
2257 case CMD_xmapclear:
2258 xp->xp_context = EXPAND_MAPCLEAR;
2259 xp->xp_pattern = arg;
2260 break;
2261
2262 case CMD_abbreviate: case CMD_noreabbrev:
2263 case CMD_cabbrev: case CMD_cnoreabbrev:
2264 case CMD_iabbrev: case CMD_inoreabbrev:
2265 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002266 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002267 case CMD_unabbreviate:
2268 case CMD_cunabbrev:
2269 case CMD_iunabbrev:
2270 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002271 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002272#ifdef FEAT_MENU
2273 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2274 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2275 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2276 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2277 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2278 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2279 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2280 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2281 case CMD_tmenu: case CMD_tunmenu:
2282 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2283 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2284#endif
2285
2286 case CMD_colorscheme:
2287 xp->xp_context = EXPAND_COLORS;
2288 xp->xp_pattern = arg;
2289 break;
2290
2291 case CMD_compiler:
2292 xp->xp_context = EXPAND_COMPILER;
2293 xp->xp_pattern = arg;
2294 break;
2295
2296 case CMD_ownsyntax:
2297 xp->xp_context = EXPAND_OWNSYNTAX;
2298 xp->xp_pattern = arg;
2299 break;
2300
2301 case CMD_setfiletype:
2302 xp->xp_context = EXPAND_FILETYPE;
2303 xp->xp_pattern = arg;
2304 break;
2305
2306 case CMD_packadd:
2307 xp->xp_context = EXPAND_PACKADD;
2308 xp->xp_pattern = arg;
2309 break;
2310
2311#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2312 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002313 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002314#endif
2315#if defined(FEAT_PROFILE)
2316 case CMD_profile:
2317 set_context_in_profile_cmd(xp, arg);
2318 break;
2319#endif
2320 case CMD_behave:
2321 xp->xp_context = EXPAND_BEHAVE;
2322 xp->xp_pattern = arg;
2323 break;
2324
2325 case CMD_messages:
2326 xp->xp_context = EXPAND_MESSAGES;
2327 xp->xp_pattern = arg;
2328 break;
2329
2330 case CMD_history:
2331 xp->xp_context = EXPAND_HISTORY;
2332 xp->xp_pattern = arg;
2333 break;
2334#if defined(FEAT_PROFILE)
2335 case CMD_syntime:
2336 xp->xp_context = EXPAND_SYNTIME;
2337 xp->xp_pattern = arg;
2338 break;
2339#endif
2340
2341 case CMD_argdelete:
2342 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2343 arg = xp->xp_pattern + 1;
2344 xp->xp_context = EXPAND_ARGLIST;
2345 xp->xp_pattern = arg;
2346 break;
2347
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002348#ifdef FEAT_EVAL
2349 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002350 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002351 case CMD_breakdel:
2352 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002353
2354 case CMD_scriptnames:
2355 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002356#endif
2357
Bram Moolenaard0190392019-08-23 21:17:35 +02002358 default:
2359 break;
2360 }
2361 return NULL;
2362}
2363
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002364/*
2365 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2366 * we don't need/want deleted. Maybe this could be done better if we didn't
2367 * repeat all this stuff. The only problem is that they may not stay
2368 * perfectly compatible with each other, but then the command line syntax
2369 * probably won't change that much -- webb.
2370 */
2371 static char_u *
2372set_one_cmd_context(
2373 expand_T *xp,
2374 char_u *buff) // buffer for command string
2375{
2376 char_u *p;
2377 char_u *cmd, *arg;
2378 int len = 0;
2379 exarg_T ea;
2380 int compl = EXPAND_NOTHING;
2381 int forceit = FALSE;
2382 int usefilter = FALSE; // filter instead of file name
2383
2384 ExpandInit(xp);
2385 xp->xp_pattern = buff;
2386 xp->xp_line = buff;
2387 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2388 ea.argt = 0;
2389
2390 // 1. skip comment lines and leading space, colons or bars
2391 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2392 ;
2393 xp->xp_pattern = cmd;
2394
2395 if (*cmd == NUL)
2396 return NULL;
2397 if (*cmd == '"') // ignore comment lines
2398 {
2399 xp->xp_context = EXPAND_NOTHING;
2400 return NULL;
2401 }
2402
2403 // 3. Skip over the range to find the command.
2404 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2405 xp->xp_pattern = cmd;
2406 if (*cmd == NUL)
2407 return NULL;
2408 if (*cmd == '"')
2409 {
2410 xp->xp_context = EXPAND_NOTHING;
2411 return NULL;
2412 }
2413
2414 if (*cmd == '|' || *cmd == '\n')
2415 return cmd + 1; // There's another command
2416
2417 // Get the command index.
2418 p = set_cmd_index(cmd, &ea, xp, &compl);
2419 if (p == NULL)
2420 return NULL;
2421
2422 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2423
2424 if (*p == '!') // forced commands
2425 {
2426 forceit = TRUE;
2427 ++p;
2428 }
2429
2430 // 6. parse arguments
2431 if (!IS_USER_CMDIDX(ea.cmdidx))
2432 ea.argt = excmd_get_argt(ea.cmdidx);
2433
2434 arg = skipwhite(p);
2435
2436 // Skip over ++argopt argument
2437 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2438 {
2439 p = arg;
2440 while (*p && !vim_isspace(*p))
2441 MB_PTR_ADV(p);
2442 arg = skipwhite(p);
2443 }
2444
2445 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2446 {
2447 if (*arg == '>') // append
2448 {
2449 if (*++arg == '>')
2450 ++arg;
2451 arg = skipwhite(arg);
2452 }
2453 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2454 {
2455 ++arg;
2456 usefilter = TRUE;
2457 }
2458 }
2459
2460 if (ea.cmdidx == CMD_read)
2461 {
2462 usefilter = forceit; // :r! filter if forced
2463 if (*arg == '!') // :r !filter
2464 {
2465 ++arg;
2466 usefilter = TRUE;
2467 }
2468 }
2469
2470 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2471 {
2472 while (*arg == *cmd) // allow any number of '>' or '<'
2473 ++arg;
2474 arg = skipwhite(arg);
2475 }
2476
2477 // Does command allow "+command"?
2478 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2479 {
2480 // Check if we're in the +command
2481 p = arg + 1;
2482 arg = skip_cmd_arg(arg, FALSE);
2483
2484 // Still touching the command after '+'?
2485 if (*arg == NUL)
2486 return p;
2487
2488 // Skip space(s) after +command to get to the real argument
2489 arg = skipwhite(arg);
2490 }
2491
2492
2493 // Check for '|' to separate commands and '"' to start comments.
2494 // Don't do this for ":read !cmd" and ":write !cmd".
2495 if ((ea.argt & EX_TRLBAR) && !usefilter)
2496 {
2497 p = arg;
2498 // ":redir @" is not the start of a comment
2499 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2500 p += 2;
2501 while (*p)
2502 {
2503 if (*p == Ctrl_V)
2504 {
2505 if (p[1] != NUL)
2506 ++p;
2507 }
2508 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2509 || *p == '|' || *p == '\n')
2510 {
2511 if (*(p - 1) != '\\')
2512 {
2513 if (*p == '|' || *p == '\n')
2514 return p + 1;
2515 return NULL; // It's a comment
2516 }
2517 }
2518 MB_PTR_ADV(p);
2519 }
2520 }
2521
2522 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2523 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2524 // no arguments allowed but there is something
2525 return NULL;
2526
2527 // Find start of last argument (argument just before cursor):
2528 p = buff;
2529 xp->xp_pattern = p;
2530 len = (int)STRLEN(buff);
2531 while (*p && p < buff + len)
2532 {
2533 if (*p == ' ' || *p == TAB)
2534 {
2535 // argument starts after a space
2536 xp->xp_pattern = ++p;
2537 }
2538 else
2539 {
2540 if (*p == '\\' && *(p + 1) != NUL)
2541 ++p; // skip over escaped character
2542 MB_PTR_ADV(p);
2543 }
2544 }
2545
2546 if (ea.argt & EX_XFILE)
2547 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2548
2549 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002550 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002551 forceit);
2552}
2553
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002554/*
2555 * Set the completion context in 'xp' for command 'str'
2556 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002557 void
2558set_cmd_context(
2559 expand_T *xp,
2560 char_u *str, // start of command line
2561 int len, // length of command line (excl. NUL)
2562 int col, // position of cursor
2563 int use_ccline UNUSED) // use ccline for info
2564{
2565#ifdef FEAT_EVAL
2566 cmdline_info_T *ccline = get_cmdline_info();
2567#endif
2568 int old_char = NUL;
2569 char_u *nextcomm;
2570
2571 // Avoid a UMR warning from Purify, only save the character if it has been
2572 // written before.
2573 if (col < len)
2574 old_char = str[col];
2575 str[col] = NUL;
2576 nextcomm = str;
2577
2578#ifdef FEAT_EVAL
2579 if (use_ccline && ccline->cmdfirstc == '=')
2580 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002581 // pass CMD_SIZE because there is no real command
2582 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002583 }
2584 else if (use_ccline && ccline->input_fn)
2585 {
2586 xp->xp_context = ccline->xp_context;
2587 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002588 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002589 }
2590 else
2591#endif
2592 while (nextcomm != NULL)
2593 nextcomm = set_one_cmd_context(xp, nextcomm);
2594
2595 // Store the string here so that call_user_expand_func() can get to them
2596 // easily.
2597 xp->xp_line = str;
2598 xp->xp_col = col;
2599
2600 str[col] = old_char;
2601}
2602
2603/*
2604 * Expand the command line "str" from context "xp".
2605 * "xp" must have been set by set_cmd_context().
2606 * xp->xp_pattern points into "str", to where the text that is to be expanded
2607 * starts.
2608 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2609 * cursor.
2610 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2611 * key that triggered expansion literally.
2612 * Returns EXPAND_OK otherwise.
2613 */
2614 int
2615expand_cmdline(
2616 expand_T *xp,
2617 char_u *str, // start of command line
2618 int col, // position of cursor
2619 int *matchcount, // return: nr of matches
2620 char_u ***matches) // return: array of pointers to matches
2621{
2622 char_u *file_str = NULL;
2623 int options = WILD_ADD_SLASH|WILD_SILENT;
2624
2625 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2626 {
2627 beep_flush();
2628 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2629 }
2630 if (xp->xp_context == EXPAND_NOTHING)
2631 {
2632 // Caller can use the character as a normal char instead
2633 return EXPAND_NOTHING;
2634 }
2635
2636 // add star to file name, or convert to regexp if not exp. files.
2637 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002638 if (cmdline_fuzzy_completion_supported(xp))
2639 // If fuzzy matching, don't modify the search string
2640 file_str = vim_strsave(xp->xp_pattern);
2641 else
2642 {
2643 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2644 if (file_str == NULL)
2645 return EXPAND_UNSUCCESSFUL;
2646 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002647
2648 if (p_wic)
2649 options += WILD_ICASE;
2650
2651 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002652 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002653 {
2654 *matchcount = 0;
2655 *matches = NULL;
2656 }
2657 vim_free(file_str);
2658
2659 return EXPAND_OK;
2660}
2661
Bram Moolenaar66b51422019-08-18 21:44:12 +02002662/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002663 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002664 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002665 */
2666 static int
2667expand_files_and_dirs(
2668 expand_T *xp,
2669 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002670 char_u ***matches,
2671 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002672 int flags,
2673 int options)
2674{
2675 int free_pat = FALSE;
2676 int i;
2677 int ret;
2678
2679 // for ":set path=" and ":set tags=" halve backslashes for escaped
2680 // space
2681 if (xp->xp_backslash != XP_BS_NONE)
2682 {
2683 free_pat = TRUE;
2684 pat = vim_strsave(pat);
2685 for (i = 0; pat[i]; ++i)
2686 if (pat[i] == '\\')
2687 {
2688 if (xp->xp_backslash == XP_BS_THREE
2689 && pat[i + 1] == '\\'
2690 && pat[i + 2] == '\\'
2691 && pat[i + 3] == ' ')
2692 STRMOVE(pat + i, pat + i + 3);
2693 if (xp->xp_backslash == XP_BS_ONE
2694 && pat[i + 1] == ' ')
2695 STRMOVE(pat + i, pat + i + 1);
2696 }
2697 }
2698
2699 if (xp->xp_context == EXPAND_FILES)
2700 flags |= EW_FILE;
2701 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2702 flags |= (EW_FILE | EW_PATH);
2703 else
2704 flags = (flags | EW_DIR) & ~EW_FILE;
2705 if (options & WILD_ICASE)
2706 flags |= EW_ICASE;
2707
2708 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002709 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002710 if (free_pat)
2711 vim_free(pat);
2712#ifdef BACKSLASH_IN_FILENAME
2713 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2714 {
2715 int j;
2716
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002717 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002718 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002719 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002720
2721 while (*ptr != NUL)
2722 {
2723 if (p_csl[0] == 's' && *ptr == '\\')
2724 *ptr = '/';
2725 else if (p_csl[0] == 'b' && *ptr == '/')
2726 *ptr = '\\';
2727 ptr += (*mb_ptr2len)(ptr);
2728 }
2729 }
2730 }
2731#endif
2732 return ret;
2733}
2734
2735/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002736 * Function given to ExpandGeneric() to obtain the possible arguments of the
2737 * ":behave {mswin,xterm}" command.
2738 */
2739 static char_u *
2740get_behave_arg(expand_T *xp UNUSED, int idx)
2741{
2742 if (idx == 0)
2743 return (char_u *)"mswin";
2744 if (idx == 1)
2745 return (char_u *)"xterm";
2746 return NULL;
2747}
2748
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002749# ifdef FEAT_EVAL
2750/*
2751 * Function given to ExpandGeneric() to obtain the possible arguments of the
2752 * ":breakadd {expr, file, func, here}" command.
2753 * ":breakdel {func, file, here}" command.
2754 */
2755 static char_u *
2756get_breakadd_arg(expand_T *xp UNUSED, int idx)
2757{
2758 char *opts[] = {"expr", "file", "func", "here"};
2759
2760 if (idx >=0 && idx <= 3)
2761 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002762 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002763 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2764 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002765 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2766 {
2767 // breakdel {func, file, here}
2768 if (idx <= 2)
2769 return (char_u *)opts[idx + 1];
2770 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002771 else
2772 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002773 // profdel {func, file}
2774 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002775 return (char_u *)opts[idx + 1];
2776 }
2777 }
2778 return NULL;
2779}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002780
2781/*
2782 * Function given to ExpandGeneric() to obtain the possible arguments for the
2783 * ":scriptnames" command.
2784 */
2785 static char_u *
2786get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2787{
2788 scriptitem_T *si;
2789
2790 if (!SCRIPT_ID_VALID(idx + 1))
2791 return NULL;
2792
2793 si = SCRIPT_ITEM(idx + 1);
2794 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2795 return NameBuff;
2796}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002797#endif
2798
Bram Moolenaard0190392019-08-23 21:17:35 +02002799/*
2800 * Function given to ExpandGeneric() to obtain the possible arguments of the
2801 * ":messages {clear}" command.
2802 */
2803 static char_u *
2804get_messages_arg(expand_T *xp UNUSED, int idx)
2805{
2806 if (idx == 0)
2807 return (char_u *)"clear";
2808 return NULL;
2809}
2810
2811 static char_u *
2812get_mapclear_arg(expand_T *xp UNUSED, int idx)
2813{
2814 if (idx == 0)
2815 return (char_u *)"<buffer>";
2816 return NULL;
2817}
2818
2819/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002820 * Do the expansion based on xp->xp_context and 'rmp'.
2821 */
2822 static int
2823ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002824 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002825 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002826 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002827 char_u ***matches,
2828 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002829{
2830 static struct expgen
2831 {
2832 int context;
2833 char_u *((*func)(expand_T *, int));
2834 int ic;
2835 int escaped;
2836 } tab[] =
2837 {
2838 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2839 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2840 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2841 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2842 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2843 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2844 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2845 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2846 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2847 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002848#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002849 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2850 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2851 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2852 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2853 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002854#endif
2855#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002856 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2857 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002858#endif
2859#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002860 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002861#endif
2862#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002863 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002864#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002865 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2866 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2867 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002868#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002869 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002870#endif
2871#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002872 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002873#endif
2874#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002875 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002876#endif
2877#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002878 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2879 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002880#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002881 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2882 {EXPAND_USER, get_users, TRUE, FALSE},
2883 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002884#ifdef FEAT_EVAL
2885 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002886 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002887#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002888 };
2889 int i;
2890 int ret = FAIL;
2891
2892 // Find a context in the table and call the ExpandGeneric() with the
2893 // right function to do the expansion.
2894 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2895 {
2896 if (xp->xp_context == tab[i].context)
2897 {
2898 if (tab[i].ic)
2899 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002900 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2901 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002902 break;
2903 }
2904 }
2905
2906 return ret;
2907}
2908
2909/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002910 * Map wild expand options to flags for expand_wildcards()
2911 */
2912 static int
2913map_wildopts_to_ewflags(int options)
2914{
2915 int flags;
2916
2917 flags = EW_DIR; // include directories
2918 if (options & WILD_LIST_NOTFOUND)
2919 flags |= EW_NOTFOUND;
2920 if (options & WILD_ADD_SLASH)
2921 flags |= EW_ADDSLASH;
2922 if (options & WILD_KEEP_ALL)
2923 flags |= EW_KEEPALL;
2924 if (options & WILD_SILENT)
2925 flags |= EW_SILENT;
2926 if (options & WILD_NOERROR)
2927 flags |= EW_NOERROR;
2928 if (options & WILD_ALLLINKS)
2929 flags |= EW_ALLLINKS;
2930
2931 return flags;
2932}
2933
2934/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002935 * Do the expansion based on xp->xp_context and "pat".
2936 */
2937 static int
2938ExpandFromContext(
2939 expand_T *xp,
2940 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002941 char_u ***matches,
2942 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002943 int options) // WILD_ flags
2944{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002945 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002946 int ret;
2947 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002948 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002949 int fuzzy = cmdline_fuzzy_complete(pat)
2950 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002951
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002952 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002953
2954 if (xp->xp_context == EXPAND_FILES
2955 || xp->xp_context == EXPAND_DIRECTORIES
2956 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002957 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2958 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002959
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002960 *matches = (char_u **)"";
2961 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002962 if (xp->xp_context == EXPAND_HELP)
2963 {
2964 // With an empty argument we would get all the help tags, which is
2965 // very slow. Get matches for "help" instead.
2966 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002967 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002968 {
2969#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002970 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002971#endif
2972 return OK;
2973 }
2974 return FAIL;
2975 }
2976
Bram Moolenaar66b51422019-08-18 21:44:12 +02002977 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002978 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002979 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002980 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002981 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002982 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002983#ifdef FEAT_DIFF
2984 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002985 return ExpandBufnames(pat, numMatches, matches,
2986 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002987#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002988 if (xp->xp_context == EXPAND_TAGS
2989 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002990 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2991 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002992 if (xp->xp_context == EXPAND_COLORS)
2993 {
2994 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002995 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002996 directories);
2997 }
2998 if (xp->xp_context == EXPAND_COMPILER)
2999 {
3000 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003001 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003002 }
3003 if (xp->xp_context == EXPAND_OWNSYNTAX)
3004 {
3005 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003006 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003007 }
3008 if (xp->xp_context == EXPAND_FILETYPE)
3009 {
3010 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003011 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003012 }
3013# if defined(FEAT_EVAL)
3014 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003015 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003016# endif
3017 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003018 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003019
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003020 // When expanding a function name starting with s:, match the <SNR>nr_
3021 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003022 if ((xp->xp_context == EXPAND_USER_FUNC
3023 || xp->xp_context == EXPAND_DISASSEMBLE)
3024 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003025 {
3026 int len = (int)STRLEN(pat) + 20;
3027
3028 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003029 if (tofree == NULL)
3030 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003031 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003032 pat = tofree;
3033 }
3034
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003035 if (!fuzzy)
3036 {
3037 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3038 if (regmatch.regprog == NULL)
3039 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003040
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003041 // set ignore-case according to p_ic, p_scs and pat
3042 regmatch.rm_ic = ignorecase(pat);
3043 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003044
3045 if (xp->xp_context == EXPAND_SETTINGS
3046 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003047 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003048 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003049 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003050# if defined(FEAT_EVAL)
3051 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003052 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003053# endif
3054 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003055 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003056
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003057 if (!fuzzy)
3058 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003059 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003060
3061 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003062}
3063
Bram Moolenaar66b51422019-08-18 21:44:12 +02003064/*
3065 * Expand a list of names.
3066 *
3067 * Generic function for command line completion. It calls a function to
3068 * obtain strings, one by one. The strings are matched against a regexp
3069 * program. Matching strings are copied into an array, which is returned.
3070 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003071 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3072 * is used.
3073 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003074 * Returns OK when no problems encountered, FAIL for error (out of memory).
3075 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003076 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003077ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003078 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003079 expand_T *xp,
3080 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003081 char_u ***matches,
3082 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003083 char_u *((*func)(expand_T *, int)),
3084 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003085 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003086{
3087 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003088 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003089 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003090 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003091 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003092 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003093 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003094 int sort_matches = FALSE;
3095 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003096
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003097 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003098 *matches = NULL;
3099 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003100
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003101 if (!fuzzy)
3102 ga_init2(&ga, sizeof(char *), 30);
3103 else
3104 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3105
3106 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003107 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003108 str = (*func)(xp, i);
3109 if (str == NULL) // end of list
3110 break;
3111 if (*str == NUL) // skip empty strings
3112 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003114 if (xp->xp_pattern[0] != NUL)
3115 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003116 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003117 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003118 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003119 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003120 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003121 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003122 }
3123 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003124 else
3125 match = TRUE;
3126
3127 if (!match)
3128 continue;
3129
3130 if (escaped)
3131 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3132 else
3133 str = vim_strsave(str);
3134 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003135 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003136 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003138 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139 return FAIL;
3140 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003141 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003142 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003144
3145 if (ga_grow(&ga, 1) == FAIL)
3146 {
3147 vim_free(str);
3148 break;
3149 }
3150
3151 if (fuzzy)
3152 {
3153 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3154 fuzmatch->idx = ga.ga_len;
3155 fuzmatch->str = str;
3156 fuzmatch->score = score;
3157 }
3158 else
3159 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3160
3161# ifdef FEAT_MENU
3162 if (func == get_menu_names)
3163 {
3164 // test for separator added by get_menu_names()
3165 str += STRLEN(str) - 1;
3166 if (*str == '\001')
3167 *str = '.';
3168 }
3169# endif
3170
3171 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003172 }
3173
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003174 if (ga.ga_len == 0)
3175 return OK;
3176
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003177 // sort the matches when using regular expression matching and sorting
3178 // applies to the completion context. Menus and scriptnames should be kept
3179 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003180 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003181 && xp->xp_context != EXPAND_MENUS
3182 && xp->xp_context != EXPAND_SCRIPTNAMES)
3183 sort_matches = TRUE;
3184
3185 // <SNR> functions should be sorted to the end.
3186 if (xp->xp_context == EXPAND_EXPRESSION
3187 || xp->xp_context == EXPAND_FUNCTIONS
3188 || xp->xp_context == EXPAND_USER_FUNC
3189 || xp->xp_context == EXPAND_DISASSEMBLE)
3190 funcsort = TRUE;
3191
3192 // Sort the matches.
3193 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003194 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003195 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003196 // <SNR> functions should be sorted to the end.
3197 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3198 sort_func_compare);
3199 else
3200 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3201 }
3202
3203 if (!fuzzy)
3204 {
3205 *matches = ga.ga_data;
3206 *numMatches = ga.ga_len;
3207 }
3208 else
3209 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003210 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3211 funcsort) == FAIL)
3212 return FAIL;
3213 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003214 }
3215
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003216#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003217 // Reset the variables used for special highlight names expansion, so that
3218 // they don't show up when getting normal highlight names by ID.
3219 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003220#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003221
Bram Moolenaar66b51422019-08-18 21:44:12 +02003222 return OK;
3223}
3224
3225/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003226 * Expand shell command matches in one directory of $PATH.
3227 */
3228 static void
3229expand_shellcmd_onedir(
3230 char_u *buf,
3231 char_u *s,
3232 size_t l,
3233 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003234 char_u ***matches,
3235 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003236 int flags,
3237 hashtab_T *ht,
3238 garray_T *gap)
3239{
3240 int ret;
3241 int i;
3242 hash_T hash;
3243 hashitem_T *hi;
3244
3245 vim_strncpy(buf, s, l);
3246 add_pathsep(buf);
3247 l = STRLEN(buf);
3248 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3249
3250 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003251 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003252 if (ret == OK)
3253 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003254 if (ga_grow(gap, *numMatches) == FAIL)
3255 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003256 else
3257 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003258 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003259 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003260 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003261
3262 if (STRLEN(name) > l)
3263 {
3264 // Check if this name was already found.
3265 hash = hash_hash(name + l);
3266 hi = hash_lookup(ht, name + l, hash);
3267 if (HASHITEM_EMPTY(hi))
3268 {
3269 // Remove the path that was prepended.
3270 STRMOVE(name, name + l);
3271 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3272 hash_add_item(ht, hi, name, hash);
3273 name = NULL;
3274 }
3275 }
3276 vim_free(name);
3277 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003278 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003279 }
3280 }
3281}
3282
3283/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003284 * Complete a shell command.
3285 * Returns FAIL or OK;
3286 */
3287 static int
3288expand_shellcmd(
3289 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003290 char_u ***matches, // return: array with matches
3291 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003292 int flagsarg) // EW_ flags
3293{
3294 char_u *pat;
3295 int i;
3296 char_u *path = NULL;
3297 int mustfree = FALSE;
3298 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003299 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003300 size_t l;
3301 char_u *s, *e;
3302 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003303 int did_curdir = FALSE;
3304 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003305
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003306 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307 if (buf == NULL)
3308 return FAIL;
3309
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003310 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003311 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003312 if (pat == NULL)
3313 {
3314 vim_free(buf);
3315 return FAIL;
3316 }
3317
Bram Moolenaar66b51422019-08-18 21:44:12 +02003318 for (i = 0; pat[i]; ++i)
3319 if (pat[i] == '\\' && pat[i + 1] == ' ')
3320 STRMOVE(pat + i, pat + i + 1);
3321
3322 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3323
3324 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3325 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3326 path = (char_u *)".";
3327 else
3328 {
3329 // For an absolute name we don't use $PATH.
3330 if (!mch_isFullName(pat))
3331 path = vim_getenv((char_u *)"PATH", &mustfree);
3332 if (path == NULL)
3333 path = (char_u *)"";
3334 }
3335
3336 // Go over all directories in $PATH. Expand matches in that directory and
3337 // collect them in "ga". When "." is not in $PATH also expand for the
3338 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003339 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003340 hash_init(&found_ht);
3341 for (s = path; ; s = e)
3342 {
3343# if defined(MSWIN)
3344 e = vim_strchr(s, ';');
3345# else
3346 e = vim_strchr(s, ':');
3347# endif
3348 if (e == NULL)
3349 e = s + STRLEN(s);
3350
3351 if (*s == NUL)
3352 {
3353 if (did_curdir)
3354 break;
3355 // Find directories in the current directory, path is empty.
3356 did_curdir = TRUE;
3357 flags |= EW_DIR;
3358 }
3359 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3360 {
3361 did_curdir = TRUE;
3362 flags |= EW_DIR;
3363 }
3364 else
3365 // Do not match directories inside a $PATH item.
3366 flags &= ~EW_DIR;
3367
3368 l = e - s;
3369 if (l > MAXPATHL - 5)
3370 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003371
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003372 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003373 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003374
Bram Moolenaar66b51422019-08-18 21:44:12 +02003375 if (*e != NUL)
3376 ++e;
3377 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003378 *matches = ga.ga_data;
3379 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003380
3381 vim_free(buf);
3382 vim_free(pat);
3383 if (mustfree)
3384 vim_free(path);
3385 hash_clear(&found_ht);
3386 return OK;
3387}
3388
3389# if defined(FEAT_EVAL)
3390/*
3391 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003392 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003393 */
3394 static void *
3395call_user_expand_func(
3396 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003397 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003398{
3399 cmdline_info_T *ccline = get_cmdline_info();
3400 int keep = 0;
3401 typval_T args[4];
3402 sctx_T save_current_sctx = current_sctx;
3403 char_u *pat = NULL;
3404 void *ret;
3405
3406 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3407 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003408
3409 if (ccline->cmdbuff != NULL)
3410 {
3411 keep = ccline->cmdbuff[ccline->cmdlen];
3412 ccline->cmdbuff[ccline->cmdlen] = 0;
3413 }
3414
3415 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3416
3417 args[0].v_type = VAR_STRING;
3418 args[0].vval.v_string = pat;
3419 args[1].v_type = VAR_STRING;
3420 args[1].vval.v_string = xp->xp_line;
3421 args[2].v_type = VAR_NUMBER;
3422 args[2].vval.v_number = xp->xp_col;
3423 args[3].v_type = VAR_UNKNOWN;
3424
3425 current_sctx = xp->xp_script_ctx;
3426
3427 ret = user_expand_func(xp->xp_arg, 3, args);
3428
3429 current_sctx = save_current_sctx;
3430 if (ccline->cmdbuff != NULL)
3431 ccline->cmdbuff[ccline->cmdlen] = keep;
3432
3433 vim_free(pat);
3434 return ret;
3435}
3436
3437/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003438 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3439 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003440 */
3441 static int
3442ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003443 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003444 expand_T *xp,
3445 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003446 char_u ***matches,
3447 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003448{
3449 char_u *retstr;
3450 char_u *s;
3451 char_u *e;
3452 int keep;
3453 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003454 int fuzzy;
3455 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003456 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003457
3458 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003459 *matches = NULL;
3460 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003461
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003462 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003463 if (retstr == NULL)
3464 return FAIL;
3465
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003466 if (!fuzzy)
3467 ga_init2(&ga, sizeof(char *), 3);
3468 else
3469 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3470
Bram Moolenaar66b51422019-08-18 21:44:12 +02003471 for (s = retstr; *s != NUL; s = e)
3472 {
3473 e = vim_strchr(s, '\n');
3474 if (e == NULL)
3475 e = s + STRLEN(s);
3476 keep = *e;
3477 *e = NUL;
3478
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003479 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003480 {
3481 if (!fuzzy)
3482 match = vim_regexec(regmatch, s, (colnr_T)0);
3483 else
3484 {
3485 score = fuzzy_match_str(s, pat);
3486 match = (score != 0);
3487 }
3488 }
3489 else
3490 match = TRUE; // match everything
3491
Bram Moolenaar66b51422019-08-18 21:44:12 +02003492 *e = keep;
3493
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003494 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495 {
3496 if (ga_grow(&ga, 1) == FAIL)
3497 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003498 if (!fuzzy)
3499 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3500 else
3501 {
3502 fuzmatch_str_T *fuzmatch =
3503 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003504 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003505 fuzmatch->str = vim_strnsave(s, e - s);
3506 fuzmatch->score = score;
3507 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003508 ++ga.ga_len;
3509 }
3510
3511 if (*e != NUL)
3512 ++e;
3513 }
3514 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003515
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003516 if (ga.ga_len == 0)
3517 return OK;
3518
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003519 if (!fuzzy)
3520 {
3521 *matches = ga.ga_data;
3522 *numMatches = ga.ga_len;
3523 }
3524 else
3525 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003526 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3527 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003528 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003529 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003530 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003531 return OK;
3532}
3533
3534/*
3535 * Expand names with a list returned by a function defined by the user.
3536 */
3537 static int
3538ExpandUserList(
3539 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003540 char_u ***matches,
3541 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003542{
3543 list_T *retlist;
3544 listitem_T *li;
3545 garray_T ga;
3546
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003547 *matches = NULL;
3548 *numMatches = 0;
3549 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003550 if (retlist == NULL)
3551 return FAIL;
3552
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003553 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003554 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003555 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003556 {
3557 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3558 continue; // Skip non-string items and empty strings
3559
3560 if (ga_grow(&ga, 1) == FAIL)
3561 break;
3562
3563 ((char_u **)ga.ga_data)[ga.ga_len] =
3564 vim_strsave(li->li_tv.vval.v_string);
3565 ++ga.ga_len;
3566 }
3567 list_unref(retlist);
3568
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003569 *matches = ga.ga_data;
3570 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003571 return OK;
3572}
3573# endif
3574
3575/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003576 * Expand "file" for all comma-separated directories in "path".
3577 * Adds the matches to "ga". Caller must init "ga".
3578 */
3579 void
3580globpath(
3581 char_u *path,
3582 char_u *file,
3583 garray_T *ga,
3584 int expand_options)
3585{
3586 expand_T xpc;
3587 char_u *buf;
3588 int i;
3589 int num_p;
3590 char_u **p;
3591
3592 buf = alloc(MAXPATHL);
3593 if (buf == NULL)
3594 return;
3595
3596 ExpandInit(&xpc);
3597 xpc.xp_context = EXPAND_FILES;
3598
3599 // Loop over all entries in {path}.
3600 while (*path != NUL)
3601 {
3602 // Copy one item of the path to buf[] and concatenate the file name.
3603 copy_option_part(&path, buf, MAXPATHL, ",");
3604 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3605 {
3606# if defined(MSWIN)
3607 // Using the platform's path separator (\) makes vim incorrectly
3608 // treat it as an escape character, use '/' instead.
3609 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3610 STRCAT(buf, "/");
3611# else
3612 add_pathsep(buf);
3613# endif
3614 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003615 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003616 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3617 {
3618 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3619
3620 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003621 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003622 for (i = 0; i < num_p; ++i)
3623 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003624 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003625 ++ga->ga_len;
3626 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003627 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628 }
3629 }
3630 }
3631
3632 vim_free(buf);
3633}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003634
Bram Moolenaareadee482020-09-04 15:37:31 +02003635/*
3636 * Translate some keys pressed when 'wildmenu' is used.
3637 */
3638 int
3639wildmenu_translate_key(
3640 cmdline_info_T *cclp,
3641 int key,
3642 expand_T *xp,
3643 int did_wild_list)
3644{
3645 int c = key;
3646
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003647 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003648 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003649 // When the popup menu is used for cmdline completion:
3650 // Up : go to the previous item in the menu
3651 // Down : go to the next item in the menu
3652 // Left : go to the parent directory
3653 // Right: list the files in the selected directory
3654 switch (c)
3655 {
3656 case K_UP: c = K_LEFT; break;
3657 case K_DOWN: c = K_RIGHT; break;
3658 case K_LEFT: c = K_UP; break;
3659 case K_RIGHT: c = K_DOWN; break;
3660 default: break;
3661 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003662 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003663
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003664 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003665 {
3666 if (c == K_LEFT)
3667 c = Ctrl_P;
3668 else if (c == K_RIGHT)
3669 c = Ctrl_N;
3670 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003671
Bram Moolenaareadee482020-09-04 15:37:31 +02003672 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003673 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003674 && cclp->cmdpos > 1
3675 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3676 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3677 && (c == '\n' || c == '\r' || c == K_KENTER))
3678 c = K_DOWN;
3679
3680 return c;
3681}
3682
3683/*
3684 * Delete characters on the command line, from "from" to the current
3685 * position.
3686 */
3687 static void
3688cmdline_del(cmdline_info_T *cclp, int from)
3689{
3690 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3691 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3692 cclp->cmdlen -= cclp->cmdpos - from;
3693 cclp->cmdpos = from;
3694}
3695
3696/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003697 * Handle a key pressed when the wild menu for the menu names
3698 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003699 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003700 static int
3701wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003702{
Bram Moolenaareadee482020-09-04 15:37:31 +02003703 int i;
3704 int j;
3705
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003706 // Hitting <Down> after "emenu Name.": complete submenu
3707 if (key == K_DOWN && cclp->cmdpos > 0
3708 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003709 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003710 key = p_wc;
3711 KeyTyped = TRUE; // in case the key was mapped
3712 }
3713 else if (key == K_UP)
3714 {
3715 // Hitting <Up>: Remove one submenu name in front of the
3716 // cursor
3717 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003718
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003719 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3720 i = 0;
3721 while (--j > 0)
3722 {
3723 // check for start of menu name
3724 if (cclp->cmdbuff[j] == ' '
3725 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003726 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003727 i = j + 1;
3728 break;
3729 }
3730 // check for start of submenu name
3731 if (cclp->cmdbuff[j] == '.'
3732 && cclp->cmdbuff[j - 1] != '\\')
3733 {
3734 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003735 {
3736 i = j + 1;
3737 break;
3738 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003739 else
3740 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003741 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003742 }
3743 if (i > 0)
3744 cmdline_del(cclp, i);
3745 key = p_wc;
3746 KeyTyped = TRUE; // in case the key was mapped
3747 xp->xp_context = EXPAND_NOTHING;
3748 }
3749
3750 return key;
3751}
3752
3753/*
3754 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3755 * directory names (EXPAND_DIRECTORIES) or shell command names
3756 * (EXPAND_SHELLCMD) is displayed.
3757 */
3758 static int
3759wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3760{
3761 int i;
3762 int j;
3763 char_u upseg[5];
3764
3765 upseg[0] = PATHSEP;
3766 upseg[1] = '.';
3767 upseg[2] = '.';
3768 upseg[3] = PATHSEP;
3769 upseg[4] = NUL;
3770
3771 if (key == K_DOWN
3772 && cclp->cmdpos > 0
3773 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3774 && (cclp->cmdpos < 3
3775 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3776 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3777 {
3778 // go down a directory
3779 key = p_wc;
3780 KeyTyped = TRUE; // in case the key was mapped
3781 }
3782 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3783 {
3784 // If in a direct ancestor, strip off one ../ to go down
3785 int found = FALSE;
3786
3787 j = cclp->cmdpos;
3788 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3789 while (--j > i)
3790 {
3791 if (has_mbyte)
3792 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3793 if (vim_ispathsep(cclp->cmdbuff[j]))
3794 {
3795 found = TRUE;
3796 break;
3797 }
3798 }
3799 if (found
3800 && cclp->cmdbuff[j - 1] == '.'
3801 && cclp->cmdbuff[j - 2] == '.'
3802 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3803 {
3804 cmdline_del(cclp, j - 2);
3805 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003806 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003807 }
3808 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003809 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003810 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003811 // go up a directory
3812 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003813
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003814 j = cclp->cmdpos - 1;
3815 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3816 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003817 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003818 if (has_mbyte)
3819 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3820 if (vim_ispathsep(cclp->cmdbuff[j])
3821# ifdef BACKSLASH_IN_FILENAME
3822 && vim_strchr((char_u *)" *?[{`$%#",
3823 cclp->cmdbuff[j + 1]) == NULL
3824# endif
3825 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003826 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003827 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003828 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003829 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003830 break;
3831 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003832 else
3833 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003834 }
3835 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003836
3837 if (!found)
3838 j = i;
3839 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3840 j += 4;
3841 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3842 && j == i)
3843 j += 3;
3844 else
3845 j = 0;
3846 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003847 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003848 // TODO this is only for DOS/UNIX systems - need to put in
3849 // machine-specific stuff here and in upseg init
3850 cmdline_del(cclp, j);
3851 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003852 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003853 else if (cclp->cmdpos > i)
3854 cmdline_del(cclp, i);
3855
3856 // Now complete in the new directory. Set KeyTyped in case the
3857 // Up key came from a mapping.
3858 key = p_wc;
3859 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003860 }
3861
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003862 return key;
3863}
3864
3865/*
3866 * Handle a key pressed when the wild menu is displayed
3867 */
3868 int
3869wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3870{
3871 if (xp->xp_context == EXPAND_MENUNAMES)
3872 return wildmenu_process_key_menunames(cclp, key, xp);
3873 else if ((xp->xp_context == EXPAND_FILES
3874 || xp->xp_context == EXPAND_DIRECTORIES
3875 || xp->xp_context == EXPAND_SHELLCMD))
3876 return wildmenu_process_key_filenames(cclp, key, xp);
3877
3878 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003879}
3880
3881/*
3882 * Free expanded names when finished walking through the matches
3883 */
3884 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003885wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003886{
3887 int skt = KeyTyped;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003888#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003889 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003890#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003891
3892 if (!p_wmnu || wild_menu_showing == 0)
3893 return;
3894
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003895#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003896 if (cclp->input_fn)
3897 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003898#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003899
3900 if (wild_menu_showing == WM_SCROLLED)
3901 {
3902 // Entered command line, move it up
3903 cmdline_row--;
3904 redrawcmd();
3905 }
3906 else if (save_p_ls != -1)
3907 {
3908 // restore 'laststatus' and 'winminheight'
3909 p_ls = save_p_ls;
3910 p_wmh = save_p_wmh;
3911 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003912 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003913 redrawcmd();
3914 save_p_ls = -1;
3915 }
3916 else
3917 {
3918 win_redraw_last_status(topframe);
3919 redraw_statuslines();
3920 }
3921 KeyTyped = skt;
3922 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003923#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003924 if (cclp->input_fn)
3925 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003926#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003927}
Bram Moolenaareadee482020-09-04 15:37:31 +02003928
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003929#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003930/*
3931 * "getcompletion()" function
3932 */
3933 void
3934f_getcompletion(typval_T *argvars, typval_T *rettv)
3935{
3936 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003937 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003938 expand_T xpc;
3939 int filtered = FALSE;
3940 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003941 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003942
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003943 if (in_vim9script()
3944 && (check_for_string_arg(argvars, 0) == FAIL
3945 || check_for_string_arg(argvars, 1) == FAIL
3946 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3947 return;
3948
ii144785fe02021-11-21 12:13:56 +00003949 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003950 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003951 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003952 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003953
3954 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003955 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003956
3957 if (p_wic)
3958 options |= WILD_ICASE;
3959
3960 // For filtered results, 'wildignore' is used
3961 if (!filtered)
3962 options |= WILD_KEEP_ALL;
3963
3964 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003965 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003966 {
ii144785fe02021-11-21 12:13:56 +00003967 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003968 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003969 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003970 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003971 else
3972 {
ii144785fe02021-11-21 12:13:56 +00003973 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003974 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3975
3976 xpc.xp_context = cmdcomplete_str_to_type(type);
3977 if (xpc.xp_context == EXPAND_NOTHING)
3978 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003979 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003980 return;
3981 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003982
3983# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003984 if (xpc.xp_context == EXPAND_MENUS)
3985 {
3986 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3987 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3988 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003989# endif
3990# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003991 if (xpc.xp_context == EXPAND_CSCOPE)
3992 {
3993 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3994 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3995 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003996# endif
3997# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003998 if (xpc.xp_context == EXPAND_SIGN)
3999 {
4000 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4001 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4002 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004003# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004004 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004005
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004006 if (cmdline_fuzzy_completion_supported(&xpc))
4007 // when fuzzy matching, don't modify the search string
4008 pat = vim_strsave(xpc.xp_pattern);
4009 else
4010 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4011
Bram Moolenaar93a10962022-06-16 11:42:09 +01004012 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004013 {
4014 int i;
4015
4016 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4017
4018 for (i = 0; i < xpc.xp_numfiles; i++)
4019 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4020 }
4021 vim_free(pat);
4022 ExpandCleanup(&xpc);
4023}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004024#endif // FEAT_EVAL