blob: 4f12625388ce0c69e1cc30f159ffdd44696e0b9a [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000018static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000020 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000022static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000026static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020028#endif
29
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000030// "compl_match_array" points the currently displayed list of entries in the
31// popup menu. It is NULL when there is no popup menu.
32static pumitem_T *compl_match_array = NULL;
33static int compl_match_arraysize;
34// First column in cmdline of the matched item for completion.
35static int compl_startcol;
36static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
Bram Moolenaard6e91382022-11-12 17:44:13 +000038#define SHOW_FILE_TEXT(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
52 && xp->xp_context != EXPAND_FILES
53 && xp->xp_context != EXPAND_FILES_IN_PATH
54 && xp->xp_context != EXPAND_FILETYPE
55 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
57 && xp->xp_context != EXPAND_OWNSYNTAX
58 && xp->xp_context != EXPAND_PACKADD
59 && xp->xp_context != EXPAND_SHELLCMD
60 && xp->xp_context != EXPAND_TAGS
61 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_USER_LIST);
63}
64
65/*
66 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000067 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
68 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000069 */
70 int
71cmdline_fuzzy_complete(char_u *fuzzystr)
72{
73 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
74}
75
76/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000077 * sort function for the completion matches.
78 * <SNR> functions should be sorted to the end.
79 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020080 static int
81sort_func_compare(const void *s1, const void *s2)
82{
83 char_u *p1 = *(char_u **)s1;
84 char_u *p2 = *(char_u **)s2;
85
86 if (*p1 != '<' && *p2 == '<') return -1;
87 if (*p1 == '<' && *p2 != '<') return 1;
88 return STRCMP(p1, p2);
89}
Bram Moolenaar66b51422019-08-18 21:44:12 +020090
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000091/*
92 * Escape special characters in the cmdline completion matches.
93 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020094 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000095wildescape(
96 expand_T *xp,
97 char_u *str,
98 int numfiles,
99 char_u **files)
100{
101 char_u *p;
102 int vse_what = xp->xp_context == EXPAND_BUFFERS
103 ? VSE_BUFFER : VSE_NONE;
104
105 if (xp->xp_context == EXPAND_FILES
106 || xp->xp_context == EXPAND_FILES_IN_PATH
107 || xp->xp_context == EXPAND_SHELLCMD
108 || xp->xp_context == EXPAND_BUFFERS
109 || xp->xp_context == EXPAND_DIRECTORIES)
110 {
111 // Insert a backslash into a file name before a space, \, %, #
112 // and wildmatch characters, except '~'.
113 for (int i = 0; i < numfiles; ++i)
114 {
115 // for ":set path=" we need to escape spaces twice
116 if (xp->xp_backslash == XP_BS_THREE)
117 {
118 p = vim_strsave_escaped(files[i], (char_u *)" ");
119 if (p != NULL)
120 {
121 vim_free(files[i]);
122 files[i] = p;
123#if defined(BACKSLASH_IN_FILENAME)
124 p = vim_strsave_escaped(files[i], (char_u *)" ");
125 if (p != NULL)
126 {
127 vim_free(files[i]);
128 files[i] = p;
129 }
130#endif
131 }
132 }
133#ifdef BACKSLASH_IN_FILENAME
134 p = vim_strsave_fnameescape(files[i], vse_what);
135#else
136 p = vim_strsave_fnameescape(files[i],
137 xp->xp_shell ? VSE_SHELL : vse_what);
138#endif
139 if (p != NULL)
140 {
141 vim_free(files[i]);
142 files[i] = p;
143 }
144
145 // If 'str' starts with "\~", replace "~" at start of
146 // files[i] with "\~".
147 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
148 escape_fname(&files[i]);
149 }
150 xp->xp_backslash = XP_BS_NONE;
151
152 // If the first file starts with a '+' escape it. Otherwise it
153 // could be seen as "+cmd".
154 if (*files[0] == '+')
155 escape_fname(&files[0]);
156 }
157 else if (xp->xp_context == EXPAND_TAGS)
158 {
159 // Insert a backslash before characters in a tag name that
160 // would terminate the ":tag" command.
161 for (int i = 0; i < numfiles; ++i)
162 {
163 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
164 if (p != NULL)
165 {
166 vim_free(files[i]);
167 files[i] = p;
168 }
169 }
170 }
171}
172
173/*
174 * Escape special characters in the cmdline completion matches.
175 */
176 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200177ExpandEscape(
178 expand_T *xp,
179 char_u *str,
180 int numfiles,
181 char_u **files,
182 int options)
183{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200184 // May change home directory back to "~"
185 if (options & WILD_HOME_REPLACE)
186 tilde_replace(str, numfiles, files);
187
188 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000189 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200190}
191
192/*
193 * Return FAIL if this is not an appropriate context in which to do
194 * completion of anything, return OK if it is (even if there are no matches).
195 * For the caller, this means that the character is just passed through like a
196 * normal character (instead of being expanded). This allows :s/^I^D etc.
197 */
198 int
199nextwild(
200 expand_T *xp,
201 int type,
202 int options, // extra options for ExpandOne()
203 int escape) // if TRUE, escape the returned matches
204{
205 cmdline_info_T *ccline = get_cmdline_info();
206 int i, j;
207 char_u *p1;
208 char_u *p2;
209 int difflen;
210 int v;
211
212 if (xp->xp_numfiles == -1)
213 {
214 set_expand_context(xp);
215 cmd_showtail = expand_showtail(xp);
216 }
217
218 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
219 {
220 beep_flush();
221 return OK; // Something illegal on command line
222 }
223 if (xp->xp_context == EXPAND_NOTHING)
224 {
225 // Caller can use the character as a normal char instead
226 return FAIL;
227 }
228
229 msg_puts("..."); // show that we are busy
230 out_flush();
231
232 i = (int)(xp->xp_pattern - ccline->cmdbuff);
233 xp->xp_pattern_len = ccline->cmdpos - i;
234
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000235 if (type == WILD_NEXT || type == WILD_PREV
236 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200237 {
238 // Get next/previous match for a previous expanded pattern.
239 p2 = ExpandOne(xp, NULL, NULL, 0, type);
240 }
241 else
242 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000243 if (cmdline_fuzzy_completion_supported(xp))
244 // If fuzzy matching, don't modify the search string
245 p1 = vim_strsave(xp->xp_pattern);
246 else
247 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
248
Bram Moolenaar66b51422019-08-18 21:44:12 +0200249 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000250 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200251 p2 = NULL;
252 else
253 {
254 int use_options = options |
255 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
256 if (escape)
257 use_options |= WILD_ESCAPE;
258
259 if (p_wic)
260 use_options += WILD_ICASE;
261 p2 = ExpandOne(xp, p1,
262 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
263 use_options, type);
264 vim_free(p1);
265 // longest match: make sure it is not shorter, happens with :help
266 if (p2 != NULL && type == WILD_LONGEST)
267 {
268 for (j = 0; j < xp->xp_pattern_len; ++j)
269 if (ccline->cmdbuff[i + j] == '*'
270 || ccline->cmdbuff[i + j] == '?')
271 break;
272 if ((int)STRLEN(p2) < j)
273 VIM_CLEAR(p2);
274 }
275 }
276 }
277
278 if (p2 != NULL && !got_int)
279 {
280 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
281 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
282 {
283 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
284 xp->xp_pattern = ccline->cmdbuff + i;
285 }
286 else
287 v = OK;
288 if (v == OK)
289 {
290 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
291 &ccline->cmdbuff[ccline->cmdpos],
292 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
293 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
294 ccline->cmdlen += difflen;
295 ccline->cmdpos += difflen;
296 }
297 }
298 vim_free(p2);
299
300 redrawcmd();
301 cursorcmd();
302
303 // When expanding a ":map" command and no matches are found, assume that
304 // the key is supposed to be inserted literally
305 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
306 return FAIL;
307
308 if (xp->xp_numfiles <= 0 && p2 == NULL)
309 beep_flush();
310 else if (xp->xp_numfiles == 1)
311 // free expanded pattern
312 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
313
314 return OK;
315}
316
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000317/*
318 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000319 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000320 */
321 static int
322cmdline_pum_create(
323 cmdline_info_T *ccline,
324 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000325 char_u **matches,
326 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000327 int showtail)
328{
329 int i;
330 int columns;
331
332 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000333 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000334 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000335 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000336 {
337 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
338 compl_match_array[i].pum_info = NULL;
339 compl_match_array[i].pum_extra = NULL;
340 compl_match_array[i].pum_kind = NULL;
341 }
342
343 // Compute the popup menu starting column
344 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
345 columns = vim_strsize(xp->xp_pattern);
346 if (showtail)
347 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000348 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000349 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000350 }
351 if (columns >= compl_startcol)
352 compl_startcol = 0;
353 else
354 compl_startcol -= columns;
355
356 // no default selection
357 compl_selected = -1;
358
359 cmdline_pum_display();
360
361 return EXPAND_OK;
362}
363
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000364/*
365 * Display the cmdline completion matches in a popup menu
366 */
367void cmdline_pum_display(void)
368{
369 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
370}
371
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000372/*
373 * Returns TRUE if the cmdline completion popup menu is being displayed.
374 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000375int cmdline_pum_active(void)
376{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100377 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000378}
379
380/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000381 * Remove the cmdline completion popup menu (if present), free the list of
382 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000383 */
384void cmdline_pum_remove(void)
385{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000386 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100387 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000388
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000389 pum_undisplay();
390 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000391 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000392 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000393 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000394 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100395
396 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
397 // as a side effect.
398 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000399}
400
401void cmdline_pum_cleanup(cmdline_info_T *cclp)
402{
403 cmdline_pum_remove();
404 wildmenu_cleanup(cclp);
405}
406
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000407/*
408 * Returns the starting column number to use for the cmdline completion popup
409 * menu.
410 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000411int cmdline_compl_startcol(void)
412{
413 return compl_startcol;
414}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000415
Bram Moolenaar66b51422019-08-18 21:44:12 +0200416/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000417 * Return the number of characters that should be skipped in a status match.
418 * These are backslashes used for escaping. Do show backslashes in help tags.
419 */
420 static int
421skip_status_match_char(expand_T *xp, char_u *s)
422{
423 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
424#ifdef FEAT_MENU
425 || ((xp->xp_context == EXPAND_MENUS
426 || xp->xp_context == EXPAND_MENUNAMES)
427 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
428#endif
429 )
430 {
431#ifndef BACKSLASH_IN_FILENAME
432 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
433 return 2;
434#endif
435 return 1;
436 }
437 return 0;
438}
439
440/*
441 * Get the length of an item as it will be shown in the status line.
442 */
443 static int
444status_match_len(expand_T *xp, char_u *s)
445{
446 int len = 0;
447
448#ifdef FEAT_MENU
449 int emenu = xp->xp_context == EXPAND_MENUS
450 || xp->xp_context == EXPAND_MENUNAMES;
451
452 // Check for menu separators - replace with '|'.
453 if (emenu && menu_is_separator(s))
454 return 1;
455#endif
456
457 while (*s != NUL)
458 {
459 s += skip_status_match_char(xp, s);
460 len += ptr2cells(s);
461 MB_PTR_ADV(s);
462 }
463
464 return len;
465}
466
467/*
468 * Show wildchar matches in the status line.
469 * Show at least the "match" item.
470 * We start at item 'first_match' in the list and show all matches that fit.
471 *
472 * If inversion is possible we use it. Else '=' characters are used.
473 */
474 static void
475win_redr_status_matches(
476 expand_T *xp,
477 int num_matches,
478 char_u **matches, // list of matches
479 int match,
480 int showtail)
481{
482#define L_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
483 int row;
484 char_u *buf;
485 int len;
486 int clen; // length in screen cells
487 int fillchar;
488 int attr;
489 int i;
490 int highlight = TRUE;
491 char_u *selstart = NULL;
492 int selstart_col = 0;
493 char_u *selend = NULL;
494 static int first_match = 0;
495 int add_left = FALSE;
496 char_u *s;
497#ifdef FEAT_MENU
498 int emenu;
499#endif
500 int l;
501
502 if (matches == NULL) // interrupted completion?
503 return;
504
505 if (has_mbyte)
506 buf = alloc(Columns * MB_MAXBYTES + 1);
507 else
508 buf = alloc(Columns + 1);
509 if (buf == NULL)
510 return;
511
512 if (match == -1) // don't show match but original text
513 {
514 match = 0;
515 highlight = FALSE;
516 }
517 // count 1 for the ending ">"
518 clen = status_match_len(xp, L_MATCH(match)) + 3;
519 if (match == 0)
520 first_match = 0;
521 else if (match < first_match)
522 {
523 // jumping left, as far as we can go
524 first_match = match;
525 add_left = TRUE;
526 }
527 else
528 {
529 // check if match fits on the screen
530 for (i = first_match; i < match; ++i)
531 clen += status_match_len(xp, L_MATCH(i)) + 2;
532 if (first_match > 0)
533 clen += 2;
534 // jumping right, put match at the left
535 if ((long)clen > Columns)
536 {
537 first_match = match;
538 // if showing the last match, we can add some on the left
539 clen = 2;
540 for (i = match; i < num_matches; ++i)
541 {
542 clen += status_match_len(xp, L_MATCH(i)) + 2;
543 if ((long)clen >= Columns)
544 break;
545 }
546 if (i == num_matches)
547 add_left = TRUE;
548 }
549 }
550 if (add_left)
551 while (first_match > 0)
552 {
553 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
554 if ((long)clen >= Columns)
555 break;
556 --first_match;
557 }
558
559 fillchar = fillchar_status(&attr, curwin);
560
561 if (first_match == 0)
562 {
563 *buf = NUL;
564 len = 0;
565 }
566 else
567 {
568 STRCPY(buf, "< ");
569 len = 2;
570 }
571 clen = len;
572
573 i = first_match;
574 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
575 {
576 if (i == match)
577 {
578 selstart = buf + len;
579 selstart_col = clen;
580 }
581
582 s = L_MATCH(i);
583 // Check for menu separators - replace with '|'
584#ifdef FEAT_MENU
585 emenu = (xp->xp_context == EXPAND_MENUS
586 || xp->xp_context == EXPAND_MENUNAMES);
587 if (emenu && menu_is_separator(s))
588 {
589 STRCPY(buf + len, transchar('|'));
590 l = (int)STRLEN(buf + len);
591 len += l;
592 clen += l;
593 }
594 else
595#endif
596 for ( ; *s != NUL; ++s)
597 {
598 s += skip_status_match_char(xp, s);
599 clen += ptr2cells(s);
600 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
601 {
602 STRNCPY(buf + len, s, l);
603 s += l - 1;
604 len += l;
605 }
606 else
607 {
608 STRCPY(buf + len, transchar_byte(*s));
609 len += (int)STRLEN(buf + len);
610 }
611 }
612 if (i == match)
613 selend = buf + len;
614
615 *(buf + len++) = ' ';
616 *(buf + len++) = ' ';
617 clen += 2;
618 if (++i == num_matches)
619 break;
620 }
621
622 if (i != num_matches)
623 {
624 *(buf + len++) = '>';
625 ++clen;
626 }
627
628 buf[len] = NUL;
629
630 row = cmdline_row - 1;
631 if (row >= 0)
632 {
633 if (wild_menu_showing == 0)
634 {
635 if (msg_scrolled > 0)
636 {
637 // Put the wildmenu just above the command line. If there is
638 // no room, scroll the screen one line up.
639 if (cmdline_row == Rows - 1)
640 {
641 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
642 ++msg_scrolled;
643 }
644 else
645 {
646 ++cmdline_row;
647 ++row;
648 }
649 wild_menu_showing = WM_SCROLLED;
650 }
651 else
652 {
653 // Create status line if needed by setting 'laststatus' to 2.
654 // Set 'winminheight' to zero to avoid that the window is
655 // resized.
656 if (lastwin->w_status_height == 0)
657 {
658 save_p_ls = p_ls;
659 save_p_wmh = p_wmh;
660 p_ls = 2;
661 p_wmh = 0;
662 last_status(FALSE);
663 }
664 wild_menu_showing = WM_SHOWN;
665 }
666 }
667
668 screen_puts(buf, row, 0, attr);
669 if (selstart != NULL && highlight)
670 {
671 *selend = NUL;
672 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
673 }
674
675 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
676 }
677
678 win_redraw_last_status(topframe);
679 vim_free(buf);
680}
681
682/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000683 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000684 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000685 */
686 static char_u *
687get_next_or_prev_match(
688 int mode,
689 expand_T *xp,
690 int *p_findex,
691 char_u *orig_save)
692{
693 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000694 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000695
696 if (xp->xp_numfiles <= 0)
697 return NULL;
698
699 if (mode == WILD_PREV)
700 {
701 if (findex == -1)
702 findex = xp->xp_numfiles;
703 --findex;
704 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000705 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000706 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000707 else if (mode == WILD_PAGEUP)
708 {
709 if (findex == 0)
710 // at the first entry, don't select any entries
711 findex = -1;
712 else if (findex == -1)
713 // no entry is selected. select the last entry
714 findex = xp->xp_numfiles - 1;
715 else
716 {
717 // go up by the pum height
718 ht = pum_get_height();
719 if (ht > 3)
720 ht -= 2;
721 findex -= ht;
722 if (findex < 0)
723 // few entries left, select the first entry
724 findex = 0;
725 }
726 }
727 else // mode == WILD_PAGEDOWN
728 {
729 if (findex == xp->xp_numfiles - 1)
730 // at the last entry, don't select any entries
731 findex = -1;
732 else if (findex == -1)
733 // no entry is selected. select the first entry
734 findex = 0;
735 else
736 {
737 // go down by the pum height
738 ht = pum_get_height();
739 if (ht > 3)
740 ht -= 2;
741 findex += ht;
742 if (findex >= xp->xp_numfiles)
743 // few entries left, select the last entry
744 findex = xp->xp_numfiles - 1;
745 }
746 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000747
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000748 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000749 if (findex < 0)
750 {
751 if (orig_save == NULL)
752 findex = xp->xp_numfiles - 1;
753 else
754 findex = -1;
755 }
756 if (findex >= xp->xp_numfiles)
757 {
758 if (orig_save == NULL)
759 findex = 0;
760 else
761 findex = -1;
762 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000763 if (compl_match_array)
764 {
765 compl_selected = findex;
766 cmdline_pum_display();
767 }
768 else if (p_wmnu)
769 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
770 findex, cmd_showtail);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000771 *p_findex = findex;
772
773 if (findex == -1)
774 return vim_strsave(orig_save);
775
776 return vim_strsave(xp->xp_files[findex]);
777}
778
779/*
780 * Start the command-line expansion and get the matches.
781 */
782 static char_u *
783ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
784{
785 int non_suf_match; // number without matching suffix
786 int i;
787 char_u *ss = NULL;
788
789 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000790 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000791 options) == FAIL)
792 {
793#ifdef FNAME_ILLEGAL
794 // Illegal file name has been silently skipped. But when there
795 // are wildcards, the real problem is that there was no match,
796 // causing the pattern to be added, which has illegal characters.
797 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
798 semsg(_(e_no_match_str_2), str);
799#endif
800 }
801 else if (xp->xp_numfiles == 0)
802 {
803 if (!(options & WILD_SILENT))
804 semsg(_(e_no_match_str_2), str);
805 }
806 else
807 {
808 // Escape the matches for use on the command line.
809 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
810
811 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000812 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000813 {
814 if (xp->xp_numfiles)
815 non_suf_match = xp->xp_numfiles;
816 else
817 non_suf_match = 1;
818 if ((xp->xp_context == EXPAND_FILES
819 || xp->xp_context == EXPAND_DIRECTORIES)
820 && xp->xp_numfiles > 1)
821 {
822 // More than one match; check suffix.
823 // The files will have been sorted on matching suffix in
824 // expand_wildcards, only need to check the first two.
825 non_suf_match = 0;
826 for (i = 0; i < 2; ++i)
827 if (match_suffix(xp->xp_files[i]))
828 ++non_suf_match;
829 }
830 if (non_suf_match != 1)
831 {
832 // Can we ever get here unless it's while expanding
833 // interactively? If not, we can get rid of this all
834 // together. Don't really want to wait for this message
835 // (and possibly have to hit return to continue!).
836 if (!(options & WILD_SILENT))
837 emsg(_(e_too_many_file_names));
838 else if (!(options & WILD_NO_BEEP))
839 beep_flush();
840 }
841 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
842 ss = vim_strsave(xp->xp_files[0]);
843 }
844 }
845
846 return ss;
847}
848
849/*
850 * Return the longest common part in the list of cmdline completion matches.
851 */
852 static char_u *
853find_longest_match(expand_T *xp, int options)
854{
855 long_u len;
856 int mb_len = 1;
857 int c0, ci;
858 int i;
859 char_u *ss;
860
861 for (len = 0; xp->xp_files[0][len]; len += mb_len)
862 {
863 if (has_mbyte)
864 {
865 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
866 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
867 }
868 else
869 c0 = xp->xp_files[0][len];
870 for (i = 1; i < xp->xp_numfiles; ++i)
871 {
872 if (has_mbyte)
873 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
874 else
875 ci = xp->xp_files[i][len];
876 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
877 || xp->xp_context == EXPAND_FILES
878 || xp->xp_context == EXPAND_SHELLCMD
879 || xp->xp_context == EXPAND_BUFFERS))
880 {
881 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
882 break;
883 }
884 else if (c0 != ci)
885 break;
886 }
887 if (i < xp->xp_numfiles)
888 {
889 if (!(options & WILD_NO_BEEP))
890 vim_beep(BO_WILD);
891 break;
892 }
893 }
894
895 ss = alloc(len + 1);
896 if (ss)
897 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
898
899 return ss;
900}
901
902/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000903 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200904 * Chars that should not be expanded must be preceded with a backslash.
905 * Return a pointer to allocated memory containing the new string.
906 * Return NULL for failure.
907 *
908 * "orig" is the originally expanded string, copied to allocated memory. It
909 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
910 * WILD_PREV "orig" should be NULL.
911 *
912 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
913 * is WILD_EXPAND_FREE or WILD_ALL.
914 *
915 * mode = WILD_FREE: just free previously expanded matches
916 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
917 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
918 * mode = WILD_NEXT: use next match in multiple match, wrap to first
919 * mode = WILD_PREV: use previous match in multiple match, wrap to first
920 * mode = WILD_ALL: return all matches concatenated
921 * mode = WILD_LONGEST: return longest matched part
922 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000923 * mode = WILD_APPLY: apply the item selected in the cmdline completion
924 * popup menu and close the menu.
925 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
926 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200927 *
928 * options = WILD_LIST_NOTFOUND: list entries without a match
929 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
930 * options = WILD_USE_NL: Use '\n' for WILD_ALL
931 * options = WILD_NO_BEEP: Don't beep for multiple matches
932 * options = WILD_ADD_SLASH: add a slash after directory names
933 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
934 * options = WILD_SILENT: don't print warning messages
935 * options = WILD_ESCAPE: put backslash before special chars
936 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200937 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200938 *
939 * The variables xp->xp_context and xp->xp_backslash must have been set!
940 */
941 char_u *
942ExpandOne(
943 expand_T *xp,
944 char_u *str,
945 char_u *orig, // allocated copy of original of expanded string
946 int options,
947 int mode)
948{
949 char_u *ss = NULL;
950 static int findex;
951 static char_u *orig_save = NULL; // kept value of orig
952 int orig_saved = FALSE;
953 int i;
954 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200955
956 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000957 if (mode == WILD_NEXT || mode == WILD_PREV
958 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000959 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200960
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000961 if (mode == WILD_CANCEL)
962 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
963 else if (mode == WILD_APPLY)
964 ss = vim_strsave(findex == -1 ? (orig_save ?
965 orig_save : (char_u *)"") : xp->xp_files[findex]);
966
Bram Moolenaar66b51422019-08-18 21:44:12 +0200967 // free old names
968 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
969 {
970 FreeWild(xp->xp_numfiles, xp->xp_files);
971 xp->xp_numfiles = -1;
972 VIM_CLEAR(orig_save);
973 }
974 findex = 0;
975
976 if (mode == WILD_FREE) // only release file name
977 return NULL;
978
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000979 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200980 {
981 vim_free(orig_save);
982 orig_save = orig;
983 orig_saved = TRUE;
984
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000985 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986 }
987
988 // Find longest common part
989 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
990 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000991 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200992 findex = -1; // next p_wc gets first one
993 }
994
Bram Moolenaar57e95172022-08-20 19:26:14 +0100995 // Concatenate all matching names. Unless interrupted, this can be slow
996 // and the result probably won't be used.
997 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200998 {
999 len = 0;
1000 for (i = 0; i < xp->xp_numfiles; ++i)
1001 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
1002 ss = alloc(len);
1003 if (ss != NULL)
1004 {
1005 *ss = NUL;
1006 for (i = 0; i < xp->xp_numfiles; ++i)
1007 {
1008 STRCAT(ss, xp->xp_files[i]);
1009 if (i != xp->xp_numfiles - 1)
1010 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1011 }
1012 }
1013 }
1014
1015 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1016 ExpandCleanup(xp);
1017
1018 // Free "orig" if it wasn't stored in "orig_save".
1019 if (!orig_saved)
1020 vim_free(orig);
1021
1022 return ss;
1023}
1024
1025/*
1026 * Prepare an expand structure for use.
1027 */
1028 void
1029ExpandInit(expand_T *xp)
1030{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001031 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001032 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001034}
1035
1036/*
1037 * Cleanup an expand structure after use.
1038 */
1039 void
1040ExpandCleanup(expand_T *xp)
1041{
1042 if (xp->xp_numfiles >= 0)
1043 {
1044 FreeWild(xp->xp_numfiles, xp->xp_files);
1045 xp->xp_numfiles = -1;
1046 }
1047}
1048
1049/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001050 * Display one line of completion matches. Multiple matches are displayed in
1051 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001052 * matches - list of completion match names
1053 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001054 * lines - number of output lines
1055 * linenr - line number of matches to display
1056 * maxlen - maximum number of characters in each line
1057 * showtail - display only the tail of the full path of a file name
1058 * dir_attr - highlight attribute to use for directory names
1059 */
1060 static void
1061showmatches_oneline(
1062 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001063 char_u **matches,
1064 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001065 int lines,
1066 int linenr,
1067 int maxlen,
1068 int showtail,
1069 int dir_attr)
1070{
1071 int i, j;
1072 int isdir;
1073 int lastlen;
1074 char_u *p;
1075
1076 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001077 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001078 {
1079 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1080 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001081 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1082 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001083 msg_advance(maxlen + 1);
1084 msg_puts((char *)p);
1085 msg_advance(maxlen + 3);
1086 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1087 break;
1088 }
1089 for (i = maxlen - lastlen; --i >= 0; )
1090 msg_putchar(' ');
1091 if (xp->xp_context == EXPAND_FILES
1092 || xp->xp_context == EXPAND_SHELLCMD
1093 || xp->xp_context == EXPAND_BUFFERS)
1094 {
1095 // highlight directories
1096 if (xp->xp_numfiles != -1)
1097 {
1098 char_u *halved_slash;
1099 char_u *exp_path;
1100 char_u *path;
1101
1102 // Expansion was done before and special characters
1103 // were escaped, need to halve backslashes. Also
1104 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001105 exp_path = expand_env_save_opt(matches[j], TRUE);
1106 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001107 halved_slash = backslash_halve_save(path);
1108 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001109 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001110 vim_free(exp_path);
1111 if (halved_slash != path)
1112 vim_free(halved_slash);
1113 }
1114 else
1115 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001116 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001117 if (showtail)
1118 p = SHOW_FILE_TEXT(j);
1119 else
1120 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001121 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001122 TRUE);
1123 p = NameBuff;
1124 }
1125 }
1126 else
1127 {
1128 isdir = FALSE;
1129 p = SHOW_FILE_TEXT(j);
1130 }
1131 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1132 }
1133 if (msg_col > 0) // when not wrapped around
1134 {
1135 msg_clr_eos();
1136 msg_putchar('\n');
1137 }
1138 out_flush(); // show one line at a time
1139}
1140
1141/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001142 * Show all matches for completion on the command line.
1143 * Returns EXPAND_NOTHING when the character that triggered expansion should
1144 * be inserted like a normal character.
1145 */
1146 int
1147showmatches(expand_T *xp, int wildmenu UNUSED)
1148{
1149 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001150 int numMatches;
1151 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001152 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001153 int maxlen;
1154 int lines;
1155 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001156 int attr;
1157 int showtail;
1158
1159 if (xp->xp_numfiles == -1)
1160 {
1161 set_expand_context(xp);
1162 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001163 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001164 showtail = expand_showtail(xp);
1165 if (i != EXPAND_OK)
1166 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001167 }
1168 else
1169 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001170 numMatches = xp->xp_numfiles;
1171 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001172 showtail = cmd_showtail;
1173 }
1174
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001175 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001176 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001177 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001178
Bram Moolenaar66b51422019-08-18 21:44:12 +02001179 if (!wildmenu)
1180 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001181 msg_didany = FALSE; // lines_left will be set
1182 msg_start(); // prepare for paging
1183 msg_putchar('\n');
1184 out_flush();
1185 cmdline_row = msg_row;
1186 msg_didany = FALSE; // lines_left will be set again
1187 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001188 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001189
1190 if (got_int)
1191 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001192 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001193 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001194 else
1195 {
1196 // find the length of the longest file name
1197 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001198 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001199 {
1200 if (!showtail && (xp->xp_context == EXPAND_FILES
1201 || xp->xp_context == EXPAND_SHELLCMD
1202 || xp->xp_context == EXPAND_BUFFERS))
1203 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001204 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001205 j = vim_strsize(NameBuff);
1206 }
1207 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001208 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001209 if (j > maxlen)
1210 maxlen = j;
1211 }
1212
1213 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001214 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001215 else
1216 {
1217 // compute the number of columns and lines for the listing
1218 maxlen += 2; // two spaces between file names
1219 columns = ((int)Columns + 2) / maxlen;
1220 if (columns < 1)
1221 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001222 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 }
1224
1225 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1226
1227 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1228 {
1229 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1230 msg_clr_eos();
1231 msg_advance(maxlen - 3);
1232 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1233 }
1234
1235 // list the files line by line
1236 for (i = 0; i < lines; ++i)
1237 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001238 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001239 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001240 if (got_int)
1241 {
1242 got_int = FALSE;
1243 break;
1244 }
1245 }
1246
1247 // we redraw the command below the lines that we have just listed
1248 // This is a bit tricky, but it saves a lot of screen updating.
1249 cmdline_row = msg_row; // will put it back later
1250 }
1251
1252 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001253 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001254
1255 return EXPAND_OK;
1256}
1257
1258/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001259 * gettail() version for showmatches() and win_redr_status_matches():
1260 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001261 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001262 static char_u *
1263showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001264{
1265 char_u *p;
1266 char_u *t = s;
1267 int had_sep = FALSE;
1268
1269 for (p = s; *p != NUL; )
1270 {
1271 if (vim_ispathsep(*p)
1272#ifdef BACKSLASH_IN_FILENAME
1273 && !rem_backslash(p)
1274#endif
1275 )
1276 had_sep = TRUE;
1277 else if (had_sep)
1278 {
1279 t = p;
1280 had_sep = FALSE;
1281 }
1282 MB_PTR_ADV(p);
1283 }
1284 return t;
1285}
1286
1287/*
1288 * Return TRUE if we only need to show the tail of completion matches.
1289 * When not completing file names or there is a wildcard in the path FALSE is
1290 * returned.
1291 */
1292 static int
1293expand_showtail(expand_T *xp)
1294{
1295 char_u *s;
1296 char_u *end;
1297
1298 // When not completing file names a "/" may mean something different.
1299 if (xp->xp_context != EXPAND_FILES
1300 && xp->xp_context != EXPAND_SHELLCMD
1301 && xp->xp_context != EXPAND_DIRECTORIES)
1302 return FALSE;
1303
1304 end = gettail(xp->xp_pattern);
1305 if (end == xp->xp_pattern) // there is no path separator
1306 return FALSE;
1307
1308 for (s = xp->xp_pattern; s < end; s++)
1309 {
1310 // Skip escaped wildcards. Only when the backslash is not a path
1311 // separator, on DOS the '*' "path\*\file" must not be skipped.
1312 if (rem_backslash(s))
1313 ++s;
1314 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1315 return FALSE;
1316 }
1317 return TRUE;
1318}
1319
1320/*
1321 * Prepare a string for expansion.
1322 * When expanding file names: The string will be used with expand_wildcards().
1323 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1324 * When expanding other names: The string will be used with regcomp(). Copy
1325 * the name into allocated memory and prepend "^".
1326 */
1327 char_u *
1328addstar(
1329 char_u *fname,
1330 int len,
1331 int context) // EXPAND_FILES etc.
1332{
1333 char_u *retval;
1334 int i, j;
1335 int new_len;
1336 char_u *tail;
1337 int ends_in_star;
1338
1339 if (context != EXPAND_FILES
1340 && context != EXPAND_FILES_IN_PATH
1341 && context != EXPAND_SHELLCMD
1342 && context != EXPAND_DIRECTORIES)
1343 {
1344 // Matching will be done internally (on something other than files).
1345 // So we convert the file-matching-type wildcards into our kind for
1346 // use with vim_regcomp(). First work out how long it will be:
1347
1348 // For help tags the translation is done in find_help_tags().
1349 // For a tag pattern starting with "/" no translation is needed.
1350 if (context == EXPAND_HELP
1351 || context == EXPAND_COLORS
1352 || context == EXPAND_COMPILER
1353 || context == EXPAND_OWNSYNTAX
1354 || context == EXPAND_FILETYPE
1355 || context == EXPAND_PACKADD
1356 || ((context == EXPAND_TAGS_LISTFILES
1357 || context == EXPAND_TAGS)
1358 && fname[0] == '/'))
1359 retval = vim_strnsave(fname, len);
1360 else
1361 {
1362 new_len = len + 2; // +2 for '^' at start, NUL at end
1363 for (i = 0; i < len; i++)
1364 {
1365 if (fname[i] == '*' || fname[i] == '~')
1366 new_len++; // '*' needs to be replaced by ".*"
1367 // '~' needs to be replaced by "\~"
1368
1369 // Buffer names are like file names. "." should be literal
1370 if (context == EXPAND_BUFFERS && fname[i] == '.')
1371 new_len++; // "." becomes "\."
1372
1373 // Custom expansion takes care of special things, match
1374 // backslashes literally (perhaps also for other types?)
1375 if ((context == EXPAND_USER_DEFINED
1376 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1377 new_len++; // '\' becomes "\\"
1378 }
1379 retval = alloc(new_len);
1380 if (retval != NULL)
1381 {
1382 retval[0] = '^';
1383 j = 1;
1384 for (i = 0; i < len; i++, j++)
1385 {
1386 // Skip backslash. But why? At least keep it for custom
1387 // expansion.
1388 if (context != EXPAND_USER_DEFINED
1389 && context != EXPAND_USER_LIST
1390 && fname[i] == '\\'
1391 && ++i == len)
1392 break;
1393
1394 switch (fname[i])
1395 {
1396 case '*': retval[j++] = '.';
1397 break;
1398 case '~': retval[j++] = '\\';
1399 break;
1400 case '?': retval[j] = '.';
1401 continue;
1402 case '.': if (context == EXPAND_BUFFERS)
1403 retval[j++] = '\\';
1404 break;
1405 case '\\': if (context == EXPAND_USER_DEFINED
1406 || context == EXPAND_USER_LIST)
1407 retval[j++] = '\\';
1408 break;
1409 }
1410 retval[j] = fname[i];
1411 }
1412 retval[j] = NUL;
1413 }
1414 }
1415 }
1416 else
1417 {
1418 retval = alloc(len + 4);
1419 if (retval != NULL)
1420 {
1421 vim_strncpy(retval, fname, len);
1422
1423 // Don't add a star to *, ~, ~user, $var or `cmd`.
1424 // * would become **, which walks the whole tree.
1425 // ~ would be at the start of the file name, but not the tail.
1426 // $ could be anywhere in the tail.
1427 // ` could be anywhere in the file name.
1428 // When the name ends in '$' don't add a star, remove the '$'.
1429 tail = gettail(retval);
1430 ends_in_star = (len > 0 && retval[len - 1] == '*');
1431#ifndef BACKSLASH_IN_FILENAME
1432 for (i = len - 2; i >= 0; --i)
1433 {
1434 if (retval[i] != '\\')
1435 break;
1436 ends_in_star = !ends_in_star;
1437 }
1438#endif
1439 if ((*retval != '~' || tail != retval)
1440 && !ends_in_star
1441 && vim_strchr(tail, '$') == NULL
1442 && vim_strchr(retval, '`') == NULL)
1443 retval[len++] = '*';
1444 else if (len > 0 && retval[len - 1] == '$')
1445 --len;
1446 retval[len] = NUL;
1447 }
1448 }
1449 return retval;
1450}
1451
1452/*
1453 * Must parse the command line so far to work out what context we are in.
1454 * Completion can then be done based on that context.
1455 * This routine sets the variables:
1456 * xp->xp_pattern The start of the pattern to be expanded within
1457 * the command line (ends at the cursor).
1458 * xp->xp_context The type of thing to expand. Will be one of:
1459 *
1460 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1461 * the command line, like an unknown command. Caller
1462 * should beep.
1463 * EXPAND_NOTHING Unrecognised context for completion, use char like
1464 * a normal char, rather than for completion. eg
1465 * :s/^I/
1466 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1467 * it.
1468 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1469 * EXPAND_FILES After command with EX_XFILE set, or after setting
1470 * with P_EXPAND set. eg :e ^I, :w>>^I
1471 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1472 * when we know only directories are of interest. eg
1473 * :set dir=^I
1474 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1475 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1476 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1477 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1478 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1479 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1480 * EXPAND_EVENTS Complete event names
1481 * EXPAND_SYNTAX Complete :syntax command arguments
1482 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1483 * EXPAND_AUGROUP Complete autocommand group names
1484 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1485 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1486 * eg :unmap a^I , :cunab x^I
1487 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1488 * eg :call sub^I
1489 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1490 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1491 * names in expressions, eg :while s^I
1492 * EXPAND_ENV_VARS Complete environment variable names
1493 * EXPAND_USER Complete user names
1494 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001495 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001496set_expand_context(expand_T *xp)
1497{
1498 cmdline_info_T *ccline = get_cmdline_info();
1499
1500 // only expansion for ':', '>' and '=' command-lines
1501 if (ccline->cmdfirstc != ':'
1502#ifdef FEAT_EVAL
1503 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1504 && !ccline->input_fn
1505#endif
1506 )
1507 {
1508 xp->xp_context = EXPAND_NOTHING;
1509 return;
1510 }
1511 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1512}
1513
Bram Moolenaard0190392019-08-23 21:17:35 +02001514/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001515 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1516 * For user defined commands, the completion context is set in 'xp' and the
1517 * completion flags in 'complp'.
1518 *
1519 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001520 */
1521 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001522set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001523{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001524 char_u *p = NULL;
1525 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001526 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001527
1528 // Isolate the command and search for it in the command table.
1529 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001530 // - the 'k' command can directly be followed by any character, but do
1531 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1532 // find matches anywhere in the command name, do this only for command
1533 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001534 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001535 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001536 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001537 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001538 p = cmd + 1;
1539 }
1540 else
1541 {
1542 p = cmd;
1543 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1544 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001545 // A user command may contain digits.
1546 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1547 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001548 while (ASCII_ISALNUM(*p) || *p == '*')
1549 ++p;
1550 // for python 3.x: ":py3*" commands completion
1551 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1552 {
1553 ++p;
1554 while (ASCII_ISALPHA(*p) || *p == '*')
1555 ++p;
1556 }
1557 // check for non-alpha command
1558 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1559 ++p;
1560 len = (int)(p - cmd);
1561
1562 if (len == 0)
1563 {
1564 xp->xp_context = EXPAND_UNSUCCESSFUL;
1565 return NULL;
1566 }
1567
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001568 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001569
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001570 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001571 // Also when doing fuzzy expansion for non-shell commands, support
1572 // alphanumeric characters.
1573 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1574 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001575 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1576 ++p;
1577 }
1578
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001579 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001580 // character, complete the command name.
1581 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1582 return NULL;
1583
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001584 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001585 {
1586 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1587 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001588 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001589 p = cmd + 1;
1590 }
1591 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1592 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001593 eap->cmd = cmd;
1594 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001595 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001596 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001597 }
1598 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001599 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001600 {
1601 // Not still touching the command and it was an illegal one
1602 xp->xp_context = EXPAND_UNSUCCESSFUL;
1603 return NULL;
1604 }
1605
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001606 return p;
1607}
Bram Moolenaard0190392019-08-23 21:17:35 +02001608
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001609/*
1610 * Set the completion context for a command argument with wild card characters.
1611 */
1612 static void
1613set_context_for_wildcard_arg(
1614 exarg_T *eap,
1615 char_u *arg,
1616 int usefilter,
1617 expand_T *xp,
1618 int *complp)
1619{
1620 char_u *p;
1621 int c;
1622 int in_quote = FALSE;
1623 char_u *bow = NULL; // Beginning of word
1624 int len = 0;
1625
1626 // Allow spaces within back-quotes to count as part of the argument
1627 // being expanded.
1628 xp->xp_pattern = skipwhite(arg);
1629 p = xp->xp_pattern;
1630 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001631 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001632 if (has_mbyte)
1633 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001634 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001635 c = *p;
1636 if (c == '\\' && p[1] != NUL)
1637 ++p;
1638 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001639 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001640 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001641 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001642 xp->xp_pattern = p;
1643 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001644 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001645 in_quote = !in_quote;
1646 }
1647 // An argument can contain just about everything, except
1648 // characters that end the command and white space.
1649 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001650#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001651 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001652#endif
1653 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001654 {
1655 len = 0; // avoid getting stuck when space is in 'isfname'
1656 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001657 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001658 if (has_mbyte)
1659 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001660 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001661 c = *p;
1662 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001663 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001664 if (has_mbyte)
1665 len = (*mb_ptr2len)(p);
1666 else
1667 len = 1;
1668 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001669 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001670 if (in_quote)
1671 bow = p;
1672 else
1673 xp->xp_pattern = p;
1674 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001675 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001676 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 }
1678
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001679 // If we are still inside the quotes, and we passed a space, just
1680 // expand from there.
1681 if (bow != NULL && in_quote)
1682 xp->xp_pattern = bow;
1683 xp->xp_context = EXPAND_FILES;
1684
1685 // For a shell command more chars need to be escaped.
1686 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1687 {
1688#ifndef BACKSLASH_IN_FILENAME
1689 xp->xp_shell = TRUE;
1690#endif
1691 // When still after the command name expand executables.
1692 if (xp->xp_pattern == skipwhite(arg))
1693 xp->xp_context = EXPAND_SHELLCMD;
1694 }
1695
1696 // Check for environment variable.
1697 if (*xp->xp_pattern == '$')
1698 {
1699 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1700 if (!vim_isIDc(*p))
1701 break;
1702 if (*p == NUL)
1703 {
1704 xp->xp_context = EXPAND_ENV_VARS;
1705 ++xp->xp_pattern;
1706 // Avoid that the assignment uses EXPAND_FILES again.
1707 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1708 *complp = EXPAND_ENV_VARS;
1709 }
1710 }
1711 // Check for user names.
1712 if (*xp->xp_pattern == '~')
1713 {
1714 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1715 ;
1716 // Complete ~user only if it partially matches a user name.
1717 // A full match ~user<Tab> will be replaced by user's home
1718 // directory i.e. something like ~user<Tab> -> /home/user/
1719 if (*p == NUL && p > xp->xp_pattern + 1
1720 && match_user(xp->xp_pattern + 1) >= 1)
1721 {
1722 xp->xp_context = EXPAND_USER;
1723 ++xp->xp_pattern;
1724 }
1725 }
1726}
1727
1728/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001729 * Set the completion context for the :filter command. Returns a pointer to the
1730 * next command after the :filter command.
1731 */
1732 static char_u *
1733set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1734{
1735 if (*arg != NUL)
1736 arg = skip_vimgrep_pat(arg, NULL, NULL);
1737 if (arg == NULL || *arg == NUL)
1738 {
1739 xp->xp_context = EXPAND_NOTHING;
1740 return NULL;
1741 }
1742 return skipwhite(arg);
1743}
1744
1745#ifdef FEAT_SEARCH_EXTRA
1746/*
1747 * Set the completion context for the :match command. Returns a pointer to the
1748 * next command after the :match command.
1749 */
1750 static char_u *
1751set_context_in_match_cmd(expand_T *xp, char_u *arg)
1752{
1753 if (*arg == NUL || !ends_excmd(*arg))
1754 {
1755 // also complete "None"
1756 set_context_in_echohl_cmd(xp, arg);
1757 arg = skipwhite(skiptowhite(arg));
1758 if (*arg != NUL)
1759 {
1760 xp->xp_context = EXPAND_NOTHING;
1761 arg = skip_regexp(arg + 1, *arg, magic_isset());
1762 }
1763 }
1764 return find_nextcmd(arg);
1765}
1766#endif
1767
1768/*
1769 * Returns a pointer to the next command after a :global or a :v command.
1770 * Returns NULL if there is no next command.
1771 */
1772 static char_u *
1773find_cmd_after_global_cmd(char_u *arg)
1774{
1775 int delim;
1776
1777 delim = *arg; // get the delimiter
1778 if (delim)
1779 ++arg; // skip delimiter if there is one
1780
1781 while (arg[0] != NUL && arg[0] != delim)
1782 {
1783 if (arg[0] == '\\' && arg[1] != NUL)
1784 ++arg;
1785 ++arg;
1786 }
1787 if (arg[0] != NUL)
1788 return arg + 1;
1789
1790 return NULL;
1791}
1792
1793/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001794 * Returns a pointer to the next command after a :substitute or a :& command.
1795 * Returns NULL if there is no next command.
1796 */
1797 static char_u *
1798find_cmd_after_substitute_cmd(char_u *arg)
1799{
1800 int delim;
1801
1802 delim = *arg;
1803 if (delim)
1804 {
1805 // skip "from" part
1806 ++arg;
1807 arg = skip_regexp(arg, delim, magic_isset());
1808
1809 if (arg[0] != NUL && arg[0] == delim)
1810 {
1811 // skip "to" part
1812 ++arg;
1813 while (arg[0] != NUL && arg[0] != delim)
1814 {
1815 if (arg[0] == '\\' && arg[1] != NUL)
1816 ++arg;
1817 ++arg;
1818 }
1819 if (arg[0] != NUL) // skip delimiter
1820 ++arg;
1821 }
1822 }
1823 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1824 ++arg;
1825 if (arg[0] != NUL)
1826 return arg;
1827
1828 return NULL;
1829}
1830
1831/*
1832 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1833 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1834 * Returns NULL if there is no next command.
1835 */
1836 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001837find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001838{
1839 arg = skipwhite(skipdigits(arg)); // skip count
1840 if (*arg == '/') // Match regexp, not just whole words
1841 {
1842 for (++arg; *arg && *arg != '/'; arg++)
1843 if (*arg == '\\' && arg[1] != NUL)
1844 arg++;
1845 if (*arg)
1846 {
1847 arg = skipwhite(arg + 1);
1848
1849 // Check for trailing illegal characters
1850 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1851 xp->xp_context = EXPAND_NOTHING;
1852 else
1853 return arg;
1854 }
1855 }
1856
1857 return NULL;
1858}
1859
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001860#ifdef FEAT_EVAL
1861/*
1862 * Set the completion context for the :unlet command. Always returns NULL.
1863 */
1864 static char_u *
1865set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1866{
1867 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1868 arg = xp->xp_pattern + 1;
1869
1870 xp->xp_context = EXPAND_USER_VARS;
1871 xp->xp_pattern = arg;
1872
1873 if (*xp->xp_pattern == '$')
1874 {
1875 xp->xp_context = EXPAND_ENV_VARS;
1876 ++xp->xp_pattern;
1877 }
1878
1879 return NULL;
1880}
1881#endif
1882
1883#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1884/*
1885 * Set the completion context for the :language command. Always returns NULL.
1886 */
1887 static char_u *
1888set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1889{
1890 char_u *p;
1891
1892 p = skiptowhite(arg);
1893 if (*p == NUL)
1894 {
1895 xp->xp_context = EXPAND_LANGUAGE;
1896 xp->xp_pattern = arg;
1897 }
1898 else
1899 {
1900 if ( STRNCMP(arg, "messages", p - arg) == 0
1901 || STRNCMP(arg, "ctype", p - arg) == 0
1902 || STRNCMP(arg, "time", p - arg) == 0
1903 || STRNCMP(arg, "collate", p - arg) == 0)
1904 {
1905 xp->xp_context = EXPAND_LOCALES;
1906 xp->xp_pattern = skipwhite(p);
1907 }
1908 else
1909 xp->xp_context = EXPAND_NOTHING;
1910 }
1911
1912 return NULL;
1913}
1914#endif
1915
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001916#ifdef FEAT_EVAL
1917static enum
1918{
1919 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001920 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1921 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001922} breakpt_expand_what;
1923
1924/*
1925 * Set the completion context for the :breakadd command. Always returns NULL.
1926 */
1927 static char_u *
1928set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1929{
1930 char_u *p;
1931 char_u *subcmd_start;
1932
1933 xp->xp_context = EXPAND_BREAKPOINT;
1934 xp->xp_pattern = arg;
1935
1936 if (cmdidx == CMD_breakadd)
1937 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001938 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001939 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001940 else
1941 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001942
1943 p = skipwhite(arg);
1944 if (*p == NUL)
1945 return NULL;
1946 subcmd_start = p;
1947
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001948 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001949 {
1950 // :breakadd file [lnum] <filename>
1951 // :breakadd func [lnum] <funcname>
1952 p += 4;
1953 p = skipwhite(p);
1954
1955 // skip line number (if specified)
1956 if (VIM_ISDIGIT(*p))
1957 {
1958 p = skipdigits(p);
1959 if (*p != ' ')
1960 {
1961 xp->xp_context = EXPAND_NOTHING;
1962 return NULL;
1963 }
1964 p = skipwhite(p);
1965 }
1966 if (STRNCMP("file", subcmd_start, 4) == 0)
1967 xp->xp_context = EXPAND_FILES;
1968 else
1969 xp->xp_context = EXPAND_USER_FUNC;
1970 xp->xp_pattern = p;
1971 }
1972 else if (STRNCMP("expr ", p, 5) == 0)
1973 {
1974 // :breakadd expr <expression>
1975 xp->xp_context = EXPAND_EXPRESSION;
1976 xp->xp_pattern = skipwhite(p + 5);
1977 }
1978
1979 return NULL;
1980}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001981
1982 static char_u *
1983set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1984{
1985 char_u *p;
1986
1987 xp->xp_context = EXPAND_NOTHING;
1988 xp->xp_pattern = NULL;
1989
1990 p = skipwhite(arg);
1991 if (VIM_ISDIGIT(*p))
1992 return NULL;
1993
1994 xp->xp_context = EXPAND_SCRIPTNAMES;
1995 xp->xp_pattern = p;
1996
1997 return NULL;
1998}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001999#endif
2000
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002001/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002002 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2003 * The argument to the command is 'arg' and the argument flags is 'argt'.
2004 * For user-defined commands and for environment variables, 'compl' has the
2005 * completion type.
2006 * Returns a pointer to the next command. Returns NULL if there is no next
2007 * command.
2008 */
2009 static char_u *
2010set_context_by_cmdname(
2011 char_u *cmd,
2012 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002013 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002014 char_u *arg,
2015 long argt,
2016 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002017 int forceit)
2018{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002019 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002020 {
2021 case CMD_find:
2022 case CMD_sfind:
2023 case CMD_tabfind:
2024 if (xp->xp_context == EXPAND_FILES)
2025 xp->xp_context = EXPAND_FILES_IN_PATH;
2026 break;
2027 case CMD_cd:
2028 case CMD_chdir:
2029 case CMD_tcd:
2030 case CMD_tchdir:
2031 case CMD_lcd:
2032 case CMD_lchdir:
2033 if (xp->xp_context == EXPAND_FILES)
2034 xp->xp_context = EXPAND_DIRECTORIES;
2035 break;
2036 case CMD_help:
2037 xp->xp_context = EXPAND_HELP;
2038 xp->xp_pattern = arg;
2039 break;
2040
2041 // Command modifiers: return the argument.
2042 // Also for commands with an argument that is a command.
2043 case CMD_aboveleft:
2044 case CMD_argdo:
2045 case CMD_belowright:
2046 case CMD_botright:
2047 case CMD_browse:
2048 case CMD_bufdo:
2049 case CMD_cdo:
2050 case CMD_cfdo:
2051 case CMD_confirm:
2052 case CMD_debug:
2053 case CMD_folddoclosed:
2054 case CMD_folddoopen:
2055 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002056 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002057 case CMD_keepalt:
2058 case CMD_keepjumps:
2059 case CMD_keepmarks:
2060 case CMD_keeppatterns:
2061 case CMD_ldo:
2062 case CMD_leftabove:
2063 case CMD_lfdo:
2064 case CMD_lockmarks:
2065 case CMD_noautocmd:
2066 case CMD_noswapfile:
2067 case CMD_rightbelow:
2068 case CMD_sandbox:
2069 case CMD_silent:
2070 case CMD_tab:
2071 case CMD_tabdo:
2072 case CMD_topleft:
2073 case CMD_verbose:
2074 case CMD_vertical:
2075 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002076 case CMD_vim9cmd:
2077 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002078 return arg;
2079
2080 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002081 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002082
2083#ifdef FEAT_SEARCH_EXTRA
2084 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002085 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002086#endif
2087
2088 // All completion for the +cmdline_compl feature goes here.
2089
2090 case CMD_command:
2091 return set_context_in_user_cmd(xp, arg);
2092
2093 case CMD_delcommand:
2094 xp->xp_context = EXPAND_USER_COMMANDS;
2095 xp->xp_pattern = arg;
2096 break;
2097
2098 case CMD_global:
2099 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002100 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002101 case CMD_and:
2102 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002103 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002104 case CMD_isearch:
2105 case CMD_dsearch:
2106 case CMD_ilist:
2107 case CMD_dlist:
2108 case CMD_ijump:
2109 case CMD_psearch:
2110 case CMD_djump:
2111 case CMD_isplit:
2112 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002113 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002114 case CMD_autocmd:
2115 return set_context_in_autocmd(xp, arg, FALSE);
2116 case CMD_doautocmd:
2117 case CMD_doautoall:
2118 return set_context_in_autocmd(xp, arg, TRUE);
2119 case CMD_set:
2120 set_context_in_set_cmd(xp, arg, 0);
2121 break;
2122 case CMD_setglobal:
2123 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2124 break;
2125 case CMD_setlocal:
2126 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2127 break;
2128 case CMD_tag:
2129 case CMD_stag:
2130 case CMD_ptag:
2131 case CMD_ltag:
2132 case CMD_tselect:
2133 case CMD_stselect:
2134 case CMD_ptselect:
2135 case CMD_tjump:
2136 case CMD_stjump:
2137 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002138 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002139 xp->xp_context = EXPAND_TAGS_LISTFILES;
2140 else
2141 xp->xp_context = EXPAND_TAGS;
2142 xp->xp_pattern = arg;
2143 break;
2144 case CMD_augroup:
2145 xp->xp_context = EXPAND_AUGROUP;
2146 xp->xp_pattern = arg;
2147 break;
2148#ifdef FEAT_SYN_HL
2149 case CMD_syntax:
2150 set_context_in_syntax_cmd(xp, arg);
2151 break;
2152#endif
2153#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002154 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002155 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002156 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002157 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002158 case CMD_if:
2159 case CMD_elseif:
2160 case CMD_while:
2161 case CMD_for:
2162 case CMD_echo:
2163 case CMD_echon:
2164 case CMD_execute:
2165 case CMD_echomsg:
2166 case CMD_echoerr:
2167 case CMD_call:
2168 case CMD_return:
2169 case CMD_cexpr:
2170 case CMD_caddexpr:
2171 case CMD_cgetexpr:
2172 case CMD_lexpr:
2173 case CMD_laddexpr:
2174 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002175 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002176 break;
2177
2178 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002179 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002180 case CMD_function:
2181 case CMD_delfunction:
2182 xp->xp_context = EXPAND_USER_FUNC;
2183 xp->xp_pattern = arg;
2184 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002185 case CMD_disassemble:
2186 set_context_in_disassemble_cmd(xp, arg);
2187 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002188
2189 case CMD_echohl:
2190 set_context_in_echohl_cmd(xp, arg);
2191 break;
2192#endif
2193 case CMD_highlight:
2194 set_context_in_highlight_cmd(xp, arg);
2195 break;
2196#ifdef FEAT_CSCOPE
2197 case CMD_cscope:
2198 case CMD_lcscope:
2199 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002200 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002201 break;
2202#endif
2203#ifdef FEAT_SIGNS
2204 case CMD_sign:
2205 set_context_in_sign_cmd(xp, arg);
2206 break;
2207#endif
2208 case CMD_bdelete:
2209 case CMD_bwipeout:
2210 case CMD_bunload:
2211 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2212 arg = xp->xp_pattern + 1;
2213 // FALLTHROUGH
2214 case CMD_buffer:
2215 case CMD_sbuffer:
2216 case CMD_checktime:
2217 xp->xp_context = EXPAND_BUFFERS;
2218 xp->xp_pattern = arg;
2219 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002220#ifdef FEAT_DIFF
2221 case CMD_diffget:
2222 case CMD_diffput:
2223 // If current buffer is in diff mode, complete buffer names
2224 // which are in diff mode, and different than current buffer.
2225 xp->xp_context = EXPAND_DIFF_BUFFERS;
2226 xp->xp_pattern = arg;
2227 break;
2228#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002229 case CMD_USER:
2230 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002231 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2232 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002233
2234 case CMD_map: case CMD_noremap:
2235 case CMD_nmap: case CMD_nnoremap:
2236 case CMD_vmap: case CMD_vnoremap:
2237 case CMD_omap: case CMD_onoremap:
2238 case CMD_imap: case CMD_inoremap:
2239 case CMD_cmap: case CMD_cnoremap:
2240 case CMD_lmap: case CMD_lnoremap:
2241 case CMD_smap: case CMD_snoremap:
2242 case CMD_tmap: case CMD_tnoremap:
2243 case CMD_xmap: case CMD_xnoremap:
2244 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002245 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002246 case CMD_unmap:
2247 case CMD_nunmap:
2248 case CMD_vunmap:
2249 case CMD_ounmap:
2250 case CMD_iunmap:
2251 case CMD_cunmap:
2252 case CMD_lunmap:
2253 case CMD_sunmap:
2254 case CMD_tunmap:
2255 case CMD_xunmap:
2256 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002257 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002258 case CMD_mapclear:
2259 case CMD_nmapclear:
2260 case CMD_vmapclear:
2261 case CMD_omapclear:
2262 case CMD_imapclear:
2263 case CMD_cmapclear:
2264 case CMD_lmapclear:
2265 case CMD_smapclear:
2266 case CMD_tmapclear:
2267 case CMD_xmapclear:
2268 xp->xp_context = EXPAND_MAPCLEAR;
2269 xp->xp_pattern = arg;
2270 break;
2271
2272 case CMD_abbreviate: case CMD_noreabbrev:
2273 case CMD_cabbrev: case CMD_cnoreabbrev:
2274 case CMD_iabbrev: case CMD_inoreabbrev:
2275 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002276 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002277 case CMD_unabbreviate:
2278 case CMD_cunabbrev:
2279 case CMD_iunabbrev:
2280 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002281 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002282#ifdef FEAT_MENU
2283 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2284 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2285 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2286 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2287 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2288 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2289 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2290 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2291 case CMD_tmenu: case CMD_tunmenu:
2292 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2293 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2294#endif
2295
2296 case CMD_colorscheme:
2297 xp->xp_context = EXPAND_COLORS;
2298 xp->xp_pattern = arg;
2299 break;
2300
2301 case CMD_compiler:
2302 xp->xp_context = EXPAND_COMPILER;
2303 xp->xp_pattern = arg;
2304 break;
2305
2306 case CMD_ownsyntax:
2307 xp->xp_context = EXPAND_OWNSYNTAX;
2308 xp->xp_pattern = arg;
2309 break;
2310
2311 case CMD_setfiletype:
2312 xp->xp_context = EXPAND_FILETYPE;
2313 xp->xp_pattern = arg;
2314 break;
2315
2316 case CMD_packadd:
2317 xp->xp_context = EXPAND_PACKADD;
2318 xp->xp_pattern = arg;
2319 break;
2320
2321#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2322 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002323 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002324#endif
2325#if defined(FEAT_PROFILE)
2326 case CMD_profile:
2327 set_context_in_profile_cmd(xp, arg);
2328 break;
2329#endif
2330 case CMD_behave:
2331 xp->xp_context = EXPAND_BEHAVE;
2332 xp->xp_pattern = arg;
2333 break;
2334
2335 case CMD_messages:
2336 xp->xp_context = EXPAND_MESSAGES;
2337 xp->xp_pattern = arg;
2338 break;
2339
2340 case CMD_history:
2341 xp->xp_context = EXPAND_HISTORY;
2342 xp->xp_pattern = arg;
2343 break;
2344#if defined(FEAT_PROFILE)
2345 case CMD_syntime:
2346 xp->xp_context = EXPAND_SYNTIME;
2347 xp->xp_pattern = arg;
2348 break;
2349#endif
2350
2351 case CMD_argdelete:
2352 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2353 arg = xp->xp_pattern + 1;
2354 xp->xp_context = EXPAND_ARGLIST;
2355 xp->xp_pattern = arg;
2356 break;
2357
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002358#ifdef FEAT_EVAL
2359 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002360 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002361 case CMD_breakdel:
2362 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002363
2364 case CMD_scriptnames:
2365 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002366#endif
2367
Bram Moolenaard0190392019-08-23 21:17:35 +02002368 default:
2369 break;
2370 }
2371 return NULL;
2372}
2373
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002374/*
2375 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2376 * we don't need/want deleted. Maybe this could be done better if we didn't
2377 * repeat all this stuff. The only problem is that they may not stay
2378 * perfectly compatible with each other, but then the command line syntax
2379 * probably won't change that much -- webb.
2380 */
2381 static char_u *
2382set_one_cmd_context(
2383 expand_T *xp,
2384 char_u *buff) // buffer for command string
2385{
2386 char_u *p;
2387 char_u *cmd, *arg;
2388 int len = 0;
2389 exarg_T ea;
2390 int compl = EXPAND_NOTHING;
2391 int forceit = FALSE;
2392 int usefilter = FALSE; // filter instead of file name
2393
2394 ExpandInit(xp);
2395 xp->xp_pattern = buff;
2396 xp->xp_line = buff;
2397 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2398 ea.argt = 0;
2399
2400 // 1. skip comment lines and leading space, colons or bars
2401 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2402 ;
2403 xp->xp_pattern = cmd;
2404
2405 if (*cmd == NUL)
2406 return NULL;
2407 if (*cmd == '"') // ignore comment lines
2408 {
2409 xp->xp_context = EXPAND_NOTHING;
2410 return NULL;
2411 }
2412
2413 // 3. Skip over the range to find the command.
2414 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2415 xp->xp_pattern = cmd;
2416 if (*cmd == NUL)
2417 return NULL;
2418 if (*cmd == '"')
2419 {
2420 xp->xp_context = EXPAND_NOTHING;
2421 return NULL;
2422 }
2423
2424 if (*cmd == '|' || *cmd == '\n')
2425 return cmd + 1; // There's another command
2426
2427 // Get the command index.
2428 p = set_cmd_index(cmd, &ea, xp, &compl);
2429 if (p == NULL)
2430 return NULL;
2431
2432 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2433
2434 if (*p == '!') // forced commands
2435 {
2436 forceit = TRUE;
2437 ++p;
2438 }
2439
2440 // 6. parse arguments
2441 if (!IS_USER_CMDIDX(ea.cmdidx))
2442 ea.argt = excmd_get_argt(ea.cmdidx);
2443
2444 arg = skipwhite(p);
2445
2446 // Skip over ++argopt argument
2447 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2448 {
2449 p = arg;
2450 while (*p && !vim_isspace(*p))
2451 MB_PTR_ADV(p);
2452 arg = skipwhite(p);
2453 }
2454
2455 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2456 {
2457 if (*arg == '>') // append
2458 {
2459 if (*++arg == '>')
2460 ++arg;
2461 arg = skipwhite(arg);
2462 }
2463 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2464 {
2465 ++arg;
2466 usefilter = TRUE;
2467 }
2468 }
2469
2470 if (ea.cmdidx == CMD_read)
2471 {
2472 usefilter = forceit; // :r! filter if forced
2473 if (*arg == '!') // :r !filter
2474 {
2475 ++arg;
2476 usefilter = TRUE;
2477 }
2478 }
2479
2480 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2481 {
2482 while (*arg == *cmd) // allow any number of '>' or '<'
2483 ++arg;
2484 arg = skipwhite(arg);
2485 }
2486
2487 // Does command allow "+command"?
2488 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2489 {
2490 // Check if we're in the +command
2491 p = arg + 1;
2492 arg = skip_cmd_arg(arg, FALSE);
2493
2494 // Still touching the command after '+'?
2495 if (*arg == NUL)
2496 return p;
2497
2498 // Skip space(s) after +command to get to the real argument
2499 arg = skipwhite(arg);
2500 }
2501
2502
2503 // Check for '|' to separate commands and '"' to start comments.
2504 // Don't do this for ":read !cmd" and ":write !cmd".
2505 if ((ea.argt & EX_TRLBAR) && !usefilter)
2506 {
2507 p = arg;
2508 // ":redir @" is not the start of a comment
2509 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2510 p += 2;
2511 while (*p)
2512 {
2513 if (*p == Ctrl_V)
2514 {
2515 if (p[1] != NUL)
2516 ++p;
2517 }
2518 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2519 || *p == '|' || *p == '\n')
2520 {
2521 if (*(p - 1) != '\\')
2522 {
2523 if (*p == '|' || *p == '\n')
2524 return p + 1;
2525 return NULL; // It's a comment
2526 }
2527 }
2528 MB_PTR_ADV(p);
2529 }
2530 }
2531
2532 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2533 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2534 // no arguments allowed but there is something
2535 return NULL;
2536
2537 // Find start of last argument (argument just before cursor):
2538 p = buff;
2539 xp->xp_pattern = p;
2540 len = (int)STRLEN(buff);
2541 while (*p && p < buff + len)
2542 {
2543 if (*p == ' ' || *p == TAB)
2544 {
2545 // argument starts after a space
2546 xp->xp_pattern = ++p;
2547 }
2548 else
2549 {
2550 if (*p == '\\' && *(p + 1) != NUL)
2551 ++p; // skip over escaped character
2552 MB_PTR_ADV(p);
2553 }
2554 }
2555
2556 if (ea.argt & EX_XFILE)
2557 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2558
2559 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002560 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002561 forceit);
2562}
2563
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002564/*
2565 * Set the completion context in 'xp' for command 'str'
2566 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002567 void
2568set_cmd_context(
2569 expand_T *xp,
2570 char_u *str, // start of command line
2571 int len, // length of command line (excl. NUL)
2572 int col, // position of cursor
2573 int use_ccline UNUSED) // use ccline for info
2574{
2575#ifdef FEAT_EVAL
2576 cmdline_info_T *ccline = get_cmdline_info();
2577#endif
2578 int old_char = NUL;
2579 char_u *nextcomm;
2580
2581 // Avoid a UMR warning from Purify, only save the character if it has been
2582 // written before.
2583 if (col < len)
2584 old_char = str[col];
2585 str[col] = NUL;
2586 nextcomm = str;
2587
2588#ifdef FEAT_EVAL
2589 if (use_ccline && ccline->cmdfirstc == '=')
2590 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002591 // pass CMD_SIZE because there is no real command
2592 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002593 }
2594 else if (use_ccline && ccline->input_fn)
2595 {
2596 xp->xp_context = ccline->xp_context;
2597 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002598 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002599 }
2600 else
2601#endif
2602 while (nextcomm != NULL)
2603 nextcomm = set_one_cmd_context(xp, nextcomm);
2604
2605 // Store the string here so that call_user_expand_func() can get to them
2606 // easily.
2607 xp->xp_line = str;
2608 xp->xp_col = col;
2609
2610 str[col] = old_char;
2611}
2612
2613/*
2614 * Expand the command line "str" from context "xp".
2615 * "xp" must have been set by set_cmd_context().
2616 * xp->xp_pattern points into "str", to where the text that is to be expanded
2617 * starts.
2618 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2619 * cursor.
2620 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2621 * key that triggered expansion literally.
2622 * Returns EXPAND_OK otherwise.
2623 */
2624 int
2625expand_cmdline(
2626 expand_T *xp,
2627 char_u *str, // start of command line
2628 int col, // position of cursor
2629 int *matchcount, // return: nr of matches
2630 char_u ***matches) // return: array of pointers to matches
2631{
2632 char_u *file_str = NULL;
2633 int options = WILD_ADD_SLASH|WILD_SILENT;
2634
2635 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2636 {
2637 beep_flush();
2638 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2639 }
2640 if (xp->xp_context == EXPAND_NOTHING)
2641 {
2642 // Caller can use the character as a normal char instead
2643 return EXPAND_NOTHING;
2644 }
2645
2646 // add star to file name, or convert to regexp if not exp. files.
2647 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002648 if (cmdline_fuzzy_completion_supported(xp))
2649 // If fuzzy matching, don't modify the search string
2650 file_str = vim_strsave(xp->xp_pattern);
2651 else
2652 {
2653 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2654 if (file_str == NULL)
2655 return EXPAND_UNSUCCESSFUL;
2656 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002657
2658 if (p_wic)
2659 options += WILD_ICASE;
2660
2661 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002662 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002663 {
2664 *matchcount = 0;
2665 *matches = NULL;
2666 }
2667 vim_free(file_str);
2668
2669 return EXPAND_OK;
2670}
2671
Bram Moolenaar66b51422019-08-18 21:44:12 +02002672/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002673 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002674 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002675 */
2676 static int
2677expand_files_and_dirs(
2678 expand_T *xp,
2679 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002680 char_u ***matches,
2681 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002682 int flags,
2683 int options)
2684{
2685 int free_pat = FALSE;
2686 int i;
2687 int ret;
2688
2689 // for ":set path=" and ":set tags=" halve backslashes for escaped
2690 // space
2691 if (xp->xp_backslash != XP_BS_NONE)
2692 {
2693 free_pat = TRUE;
2694 pat = vim_strsave(pat);
2695 for (i = 0; pat[i]; ++i)
2696 if (pat[i] == '\\')
2697 {
2698 if (xp->xp_backslash == XP_BS_THREE
2699 && pat[i + 1] == '\\'
2700 && pat[i + 2] == '\\'
2701 && pat[i + 3] == ' ')
2702 STRMOVE(pat + i, pat + i + 3);
2703 if (xp->xp_backslash == XP_BS_ONE
2704 && pat[i + 1] == ' ')
2705 STRMOVE(pat + i, pat + i + 1);
2706 }
2707 }
2708
2709 if (xp->xp_context == EXPAND_FILES)
2710 flags |= EW_FILE;
2711 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2712 flags |= (EW_FILE | EW_PATH);
2713 else
2714 flags = (flags | EW_DIR) & ~EW_FILE;
2715 if (options & WILD_ICASE)
2716 flags |= EW_ICASE;
2717
2718 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002719 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002720 if (free_pat)
2721 vim_free(pat);
2722#ifdef BACKSLASH_IN_FILENAME
2723 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2724 {
2725 int j;
2726
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002727 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002728 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002729 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002730
2731 while (*ptr != NUL)
2732 {
2733 if (p_csl[0] == 's' && *ptr == '\\')
2734 *ptr = '/';
2735 else if (p_csl[0] == 'b' && *ptr == '/')
2736 *ptr = '\\';
2737 ptr += (*mb_ptr2len)(ptr);
2738 }
2739 }
2740 }
2741#endif
2742 return ret;
2743}
2744
2745/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002746 * Function given to ExpandGeneric() to obtain the possible arguments of the
2747 * ":behave {mswin,xterm}" command.
2748 */
2749 static char_u *
2750get_behave_arg(expand_T *xp UNUSED, int idx)
2751{
2752 if (idx == 0)
2753 return (char_u *)"mswin";
2754 if (idx == 1)
2755 return (char_u *)"xterm";
2756 return NULL;
2757}
2758
K.Takata161b6ac2022-11-14 15:31:07 +00002759#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002760/*
2761 * Function given to ExpandGeneric() to obtain the possible arguments of the
2762 * ":breakadd {expr, file, func, here}" command.
2763 * ":breakdel {func, file, here}" command.
2764 */
2765 static char_u *
2766get_breakadd_arg(expand_T *xp UNUSED, int idx)
2767{
2768 char *opts[] = {"expr", "file", "func", "here"};
2769
2770 if (idx >=0 && idx <= 3)
2771 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002772 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002773 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2774 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002775 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2776 {
2777 // breakdel {func, file, here}
2778 if (idx <= 2)
2779 return (char_u *)opts[idx + 1];
2780 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002781 else
2782 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002783 // profdel {func, file}
2784 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002785 return (char_u *)opts[idx + 1];
2786 }
2787 }
2788 return NULL;
2789}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002790
2791/*
2792 * Function given to ExpandGeneric() to obtain the possible arguments for the
2793 * ":scriptnames" command.
2794 */
2795 static char_u *
2796get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2797{
2798 scriptitem_T *si;
2799
2800 if (!SCRIPT_ID_VALID(idx + 1))
2801 return NULL;
2802
2803 si = SCRIPT_ITEM(idx + 1);
2804 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2805 return NameBuff;
2806}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002807#endif
2808
Bram Moolenaard0190392019-08-23 21:17:35 +02002809/*
2810 * Function given to ExpandGeneric() to obtain the possible arguments of the
2811 * ":messages {clear}" command.
2812 */
2813 static char_u *
2814get_messages_arg(expand_T *xp UNUSED, int idx)
2815{
2816 if (idx == 0)
2817 return (char_u *)"clear";
2818 return NULL;
2819}
2820
2821 static char_u *
2822get_mapclear_arg(expand_T *xp UNUSED, int idx)
2823{
2824 if (idx == 0)
2825 return (char_u *)"<buffer>";
2826 return NULL;
2827}
2828
2829/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002830 * Do the expansion based on xp->xp_context and 'rmp'.
2831 */
2832 static int
2833ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002834 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002835 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002836 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002837 char_u ***matches,
2838 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002839{
2840 static struct expgen
2841 {
2842 int context;
2843 char_u *((*func)(expand_T *, int));
2844 int ic;
2845 int escaped;
2846 } tab[] =
2847 {
2848 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2849 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2850 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2851 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2852 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2853 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2854 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2855 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2856 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2857 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002858#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002859 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2860 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2861 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2862 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2863 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002864#endif
2865#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002866 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2867 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002868#endif
2869#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002870 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002871#endif
2872#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002873 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002874#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002875 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2876 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2877 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002878#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002879 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002880#endif
2881#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002882 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002883#endif
2884#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002885 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002886#endif
2887#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002888 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2889 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002890#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002891 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2892 {EXPAND_USER, get_users, TRUE, FALSE},
2893 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002894#ifdef FEAT_EVAL
2895 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002896 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002897#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002898 };
2899 int i;
2900 int ret = FAIL;
2901
2902 // Find a context in the table and call the ExpandGeneric() with the
2903 // right function to do the expansion.
2904 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2905 {
2906 if (xp->xp_context == tab[i].context)
2907 {
2908 if (tab[i].ic)
2909 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002910 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2911 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002912 break;
2913 }
2914 }
2915
2916 return ret;
2917}
2918
2919/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002920 * Map wild expand options to flags for expand_wildcards()
2921 */
2922 static int
2923map_wildopts_to_ewflags(int options)
2924{
2925 int flags;
2926
2927 flags = EW_DIR; // include directories
2928 if (options & WILD_LIST_NOTFOUND)
2929 flags |= EW_NOTFOUND;
2930 if (options & WILD_ADD_SLASH)
2931 flags |= EW_ADDSLASH;
2932 if (options & WILD_KEEP_ALL)
2933 flags |= EW_KEEPALL;
2934 if (options & WILD_SILENT)
2935 flags |= EW_SILENT;
2936 if (options & WILD_NOERROR)
2937 flags |= EW_NOERROR;
2938 if (options & WILD_ALLLINKS)
2939 flags |= EW_ALLLINKS;
2940
2941 return flags;
2942}
2943
2944/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002945 * Do the expansion based on xp->xp_context and "pat".
2946 */
2947 static int
2948ExpandFromContext(
2949 expand_T *xp,
2950 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002951 char_u ***matches,
2952 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002953 int options) // WILD_ flags
2954{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002955 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002956 int ret;
2957 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002958 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002959 int fuzzy = cmdline_fuzzy_complete(pat)
2960 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002961
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002962 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002963
2964 if (xp->xp_context == EXPAND_FILES
2965 || xp->xp_context == EXPAND_DIRECTORIES
2966 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002967 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2968 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002969
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002970 *matches = (char_u **)"";
2971 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002972 if (xp->xp_context == EXPAND_HELP)
2973 {
2974 // With an empty argument we would get all the help tags, which is
2975 // very slow. Get matches for "help" instead.
2976 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002977 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002978 {
2979#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002980 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002981#endif
2982 return OK;
2983 }
2984 return FAIL;
2985 }
2986
Bram Moolenaar66b51422019-08-18 21:44:12 +02002987 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002988 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002989 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002990 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002991 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002992 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002993#ifdef FEAT_DIFF
2994 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002995 return ExpandBufnames(pat, numMatches, matches,
2996 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002997#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002998 if (xp->xp_context == EXPAND_TAGS
2999 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003000 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3001 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003002 if (xp->xp_context == EXPAND_COLORS)
3003 {
3004 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003005 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003006 directories);
3007 }
3008 if (xp->xp_context == EXPAND_COMPILER)
3009 {
3010 char *directories[] = {"compiler", 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 (xp->xp_context == EXPAND_OWNSYNTAX)
3014 {
3015 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003016 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003017 }
3018 if (xp->xp_context == EXPAND_FILETYPE)
3019 {
3020 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003021 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003022 }
K.Takata161b6ac2022-11-14 15:31:07 +00003023#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003024 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003025 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003026#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003027 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003028 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003029
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003030 // When expanding a function name starting with s:, match the <SNR>nr_
3031 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003032 if ((xp->xp_context == EXPAND_USER_FUNC
3033 || xp->xp_context == EXPAND_DISASSEMBLE)
3034 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003035 {
3036 int len = (int)STRLEN(pat) + 20;
3037
3038 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003039 if (tofree == NULL)
3040 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003041 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003042 pat = tofree;
3043 }
3044
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003045 if (!fuzzy)
3046 {
3047 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3048 if (regmatch.regprog == NULL)
3049 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003050
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003051 // set ignore-case according to p_ic, p_scs and pat
3052 regmatch.rm_ic = ignorecase(pat);
3053 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003054
3055 if (xp->xp_context == EXPAND_SETTINGS
3056 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003057 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003058 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003059 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003060#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003061 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003062 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003063#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003064 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003065 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003066
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003067 if (!fuzzy)
3068 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003069 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003070
3071 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003072}
3073
Bram Moolenaar66b51422019-08-18 21:44:12 +02003074/*
3075 * Expand a list of names.
3076 *
3077 * Generic function for command line completion. It calls a function to
3078 * obtain strings, one by one. The strings are matched against a regexp
3079 * program. Matching strings are copied into an array, which is returned.
3080 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003081 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3082 * is used.
3083 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003084 * Returns OK when no problems encountered, FAIL for error (out of memory).
3085 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003086 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003087ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003088 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003089 expand_T *xp,
3090 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003091 char_u ***matches,
3092 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003093 char_u *((*func)(expand_T *, int)),
3094 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003095 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003096{
3097 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003098 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003099 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003100 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003101 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003102 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003103 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003104 int sort_matches = FALSE;
3105 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003106
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003107 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003108 *matches = NULL;
3109 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003110
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003111 if (!fuzzy)
3112 ga_init2(&ga, sizeof(char *), 30);
3113 else
3114 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3115
3116 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003117 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003118 str = (*func)(xp, i);
3119 if (str == NULL) // end of list
3120 break;
3121 if (*str == NUL) // skip empty strings
3122 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003123
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003124 if (xp->xp_pattern[0] != NUL)
3125 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003126 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003127 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003128 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003129 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003130 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003131 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003132 }
3133 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003134 else
3135 match = TRUE;
3136
3137 if (!match)
3138 continue;
3139
3140 if (escaped)
3141 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3142 else
3143 str = vim_strsave(str);
3144 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003145 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003146 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003148 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003149 return FAIL;
3150 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003151 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003152 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003153 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003154
3155 if (ga_grow(&ga, 1) == FAIL)
3156 {
3157 vim_free(str);
3158 break;
3159 }
3160
3161 if (fuzzy)
3162 {
3163 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3164 fuzmatch->idx = ga.ga_len;
3165 fuzmatch->str = str;
3166 fuzmatch->score = score;
3167 }
3168 else
3169 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3170
K.Takata161b6ac2022-11-14 15:31:07 +00003171#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003172 if (func == get_menu_names)
3173 {
3174 // test for separator added by get_menu_names()
3175 str += STRLEN(str) - 1;
3176 if (*str == '\001')
3177 *str = '.';
3178 }
K.Takata161b6ac2022-11-14 15:31:07 +00003179#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003180
3181 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003182 }
3183
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003184 if (ga.ga_len == 0)
3185 return OK;
3186
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003187 // sort the matches when using regular expression matching and sorting
3188 // applies to the completion context. Menus and scriptnames should be kept
3189 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003190 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003191 && xp->xp_context != EXPAND_MENUS
3192 && xp->xp_context != EXPAND_SCRIPTNAMES)
3193 sort_matches = TRUE;
3194
3195 // <SNR> functions should be sorted to the end.
3196 if (xp->xp_context == EXPAND_EXPRESSION
3197 || xp->xp_context == EXPAND_FUNCTIONS
3198 || xp->xp_context == EXPAND_USER_FUNC
3199 || xp->xp_context == EXPAND_DISASSEMBLE)
3200 funcsort = TRUE;
3201
3202 // Sort the matches.
3203 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003204 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003205 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003206 // <SNR> functions should be sorted to the end.
3207 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3208 sort_func_compare);
3209 else
3210 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3211 }
3212
3213 if (!fuzzy)
3214 {
3215 *matches = ga.ga_data;
3216 *numMatches = ga.ga_len;
3217 }
3218 else
3219 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003220 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3221 funcsort) == FAIL)
3222 return FAIL;
3223 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003224 }
3225
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003226#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003227 // Reset the variables used for special highlight names expansion, so that
3228 // they don't show up when getting normal highlight names by ID.
3229 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003230#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003231
Bram Moolenaar66b51422019-08-18 21:44:12 +02003232 return OK;
3233}
3234
3235/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003236 * Expand shell command matches in one directory of $PATH.
3237 */
3238 static void
3239expand_shellcmd_onedir(
3240 char_u *buf,
3241 char_u *s,
3242 size_t l,
3243 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003244 char_u ***matches,
3245 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003246 int flags,
3247 hashtab_T *ht,
3248 garray_T *gap)
3249{
3250 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003251 hash_T hash;
3252 hashitem_T *hi;
3253
3254 vim_strncpy(buf, s, l);
3255 add_pathsep(buf);
3256 l = STRLEN(buf);
3257 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3258
3259 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003260 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003261 if (ret != OK)
3262 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003263
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003264 if (ga_grow(gap, *numMatches) == FAIL)
3265 {
3266 FreeWild(*numMatches, *matches);
3267 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003268 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003269
3270 for (int i = 0; i < *numMatches; ++i)
3271 {
3272 char_u *name = (*matches)[i];
3273
3274 if (STRLEN(name) > l)
3275 {
3276 // Check if this name was already found.
3277 hash = hash_hash(name + l);
3278 hi = hash_lookup(ht, name + l, hash);
3279 if (HASHITEM_EMPTY(hi))
3280 {
3281 // Remove the path that was prepended.
3282 STRMOVE(name, name + l);
3283 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3284 hash_add_item(ht, hi, name, hash);
3285 name = NULL;
3286 }
3287 }
3288 vim_free(name);
3289 }
3290 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003291}
3292
3293/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003294 * Complete a shell command.
3295 * Returns FAIL or OK;
3296 */
3297 static int
3298expand_shellcmd(
3299 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003300 char_u ***matches, // return: array with matches
3301 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003302 int flagsarg) // EW_ flags
3303{
3304 char_u *pat;
3305 int i;
3306 char_u *path = NULL;
3307 int mustfree = FALSE;
3308 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003309 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003310 size_t l;
3311 char_u *s, *e;
3312 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003313 int did_curdir = FALSE;
3314 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003315
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003316 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003317 if (buf == NULL)
3318 return FAIL;
3319
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003320 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003321 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003322 if (pat == NULL)
3323 {
3324 vim_free(buf);
3325 return FAIL;
3326 }
3327
Bram Moolenaar66b51422019-08-18 21:44:12 +02003328 for (i = 0; pat[i]; ++i)
3329 if (pat[i] == '\\' && pat[i + 1] == ' ')
3330 STRMOVE(pat + i, pat + i + 1);
3331
3332 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3333
3334 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3335 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3336 path = (char_u *)".";
3337 else
3338 {
3339 // For an absolute name we don't use $PATH.
3340 if (!mch_isFullName(pat))
3341 path = vim_getenv((char_u *)"PATH", &mustfree);
3342 if (path == NULL)
3343 path = (char_u *)"";
3344 }
3345
3346 // Go over all directories in $PATH. Expand matches in that directory and
3347 // collect them in "ga". When "." is not in $PATH also expand for the
3348 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003349 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 hash_init(&found_ht);
3351 for (s = path; ; s = e)
3352 {
K.Takata161b6ac2022-11-14 15:31:07 +00003353#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003355#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003356 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003357#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003358 if (e == NULL)
3359 e = s + STRLEN(s);
3360
3361 if (*s == NUL)
3362 {
3363 if (did_curdir)
3364 break;
3365 // Find directories in the current directory, path is empty.
3366 did_curdir = TRUE;
3367 flags |= EW_DIR;
3368 }
3369 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3370 {
3371 did_curdir = TRUE;
3372 flags |= EW_DIR;
3373 }
3374 else
3375 // Do not match directories inside a $PATH item.
3376 flags &= ~EW_DIR;
3377
3378 l = e - s;
3379 if (l > MAXPATHL - 5)
3380 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003381
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003382 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003383 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003384
Bram Moolenaar66b51422019-08-18 21:44:12 +02003385 if (*e != NUL)
3386 ++e;
3387 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003388 *matches = ga.ga_data;
3389 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003390
3391 vim_free(buf);
3392 vim_free(pat);
3393 if (mustfree)
3394 vim_free(path);
3395 hash_clear(&found_ht);
3396 return OK;
3397}
3398
K.Takata161b6ac2022-11-14 15:31:07 +00003399#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003400/*
3401 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003402 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003403 */
3404 static void *
3405call_user_expand_func(
3406 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003407 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003408{
3409 cmdline_info_T *ccline = get_cmdline_info();
3410 int keep = 0;
3411 typval_T args[4];
3412 sctx_T save_current_sctx = current_sctx;
3413 char_u *pat = NULL;
3414 void *ret;
3415
3416 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3417 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003418
3419 if (ccline->cmdbuff != NULL)
3420 {
3421 keep = ccline->cmdbuff[ccline->cmdlen];
3422 ccline->cmdbuff[ccline->cmdlen] = 0;
3423 }
3424
3425 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3426
3427 args[0].v_type = VAR_STRING;
3428 args[0].vval.v_string = pat;
3429 args[1].v_type = VAR_STRING;
3430 args[1].vval.v_string = xp->xp_line;
3431 args[2].v_type = VAR_NUMBER;
3432 args[2].vval.v_number = xp->xp_col;
3433 args[3].v_type = VAR_UNKNOWN;
3434
3435 current_sctx = xp->xp_script_ctx;
3436
3437 ret = user_expand_func(xp->xp_arg, 3, args);
3438
3439 current_sctx = save_current_sctx;
3440 if (ccline->cmdbuff != NULL)
3441 ccline->cmdbuff[ccline->cmdlen] = keep;
3442
3443 vim_free(pat);
3444 return ret;
3445}
3446
3447/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003448 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3449 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003450 */
3451 static int
3452ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003453 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003454 expand_T *xp,
3455 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003456 char_u ***matches,
3457 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003458{
3459 char_u *retstr;
3460 char_u *s;
3461 char_u *e;
3462 int keep;
3463 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003464 int fuzzy;
3465 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003466 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003467
3468 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003469 *matches = NULL;
3470 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003471
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003472 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003473 if (retstr == NULL)
3474 return FAIL;
3475
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003476 if (!fuzzy)
3477 ga_init2(&ga, sizeof(char *), 3);
3478 else
3479 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3480
Bram Moolenaar66b51422019-08-18 21:44:12 +02003481 for (s = retstr; *s != NUL; s = e)
3482 {
3483 e = vim_strchr(s, '\n');
3484 if (e == NULL)
3485 e = s + STRLEN(s);
3486 keep = *e;
3487 *e = NUL;
3488
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003489 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003490 {
3491 if (!fuzzy)
3492 match = vim_regexec(regmatch, s, (colnr_T)0);
3493 else
3494 {
3495 score = fuzzy_match_str(s, pat);
3496 match = (score != 0);
3497 }
3498 }
3499 else
3500 match = TRUE; // match everything
3501
Bram Moolenaar66b51422019-08-18 21:44:12 +02003502 *e = keep;
3503
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003504 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003505 {
3506 if (ga_grow(&ga, 1) == FAIL)
3507 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003508 if (!fuzzy)
3509 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3510 else
3511 {
3512 fuzmatch_str_T *fuzmatch =
3513 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003514 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003515 fuzmatch->str = vim_strnsave(s, e - s);
3516 fuzmatch->score = score;
3517 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003518 ++ga.ga_len;
3519 }
3520
3521 if (*e != NUL)
3522 ++e;
3523 }
3524 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003525
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003526 if (ga.ga_len == 0)
3527 return OK;
3528
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003529 if (!fuzzy)
3530 {
3531 *matches = ga.ga_data;
3532 *numMatches = ga.ga_len;
3533 }
3534 else
3535 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003536 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3537 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003538 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003539 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003540 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003541 return OK;
3542}
3543
3544/*
3545 * Expand names with a list returned by a function defined by the user.
3546 */
3547 static int
3548ExpandUserList(
3549 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003550 char_u ***matches,
3551 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003552{
3553 list_T *retlist;
3554 listitem_T *li;
3555 garray_T ga;
3556
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003557 *matches = NULL;
3558 *numMatches = 0;
3559 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003560 if (retlist == NULL)
3561 return FAIL;
3562
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003563 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003564 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003565 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003566 {
3567 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3568 continue; // Skip non-string items and empty strings
3569
3570 if (ga_grow(&ga, 1) == FAIL)
3571 break;
3572
3573 ((char_u **)ga.ga_data)[ga.ga_len] =
3574 vim_strsave(li->li_tv.vval.v_string);
3575 ++ga.ga_len;
3576 }
3577 list_unref(retlist);
3578
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003579 *matches = ga.ga_data;
3580 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003581 return OK;
3582}
K.Takata161b6ac2022-11-14 15:31:07 +00003583#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003584
3585/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003586 * Expand "file" for all comma-separated directories in "path".
3587 * Adds the matches to "ga". Caller must init "ga".
3588 */
3589 void
3590globpath(
3591 char_u *path,
3592 char_u *file,
3593 garray_T *ga,
3594 int expand_options)
3595{
3596 expand_T xpc;
3597 char_u *buf;
3598 int i;
3599 int num_p;
3600 char_u **p;
3601
3602 buf = alloc(MAXPATHL);
3603 if (buf == NULL)
3604 return;
3605
3606 ExpandInit(&xpc);
3607 xpc.xp_context = EXPAND_FILES;
3608
3609 // Loop over all entries in {path}.
3610 while (*path != NUL)
3611 {
3612 // Copy one item of the path to buf[] and concatenate the file name.
3613 copy_option_part(&path, buf, MAXPATHL, ",");
3614 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3615 {
K.Takata161b6ac2022-11-14 15:31:07 +00003616#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003617 // Using the platform's path separator (\) makes vim incorrectly
3618 // treat it as an escape character, use '/' instead.
3619 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3620 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003621#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003622 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003623#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003624 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003625 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003626 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3627 {
3628 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3629
3630 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003631 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003632 for (i = 0; i < num_p; ++i)
3633 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003634 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003635 ++ga->ga_len;
3636 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003637 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003638 }
3639 }
3640 }
3641
3642 vim_free(buf);
3643}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003644
Bram Moolenaareadee482020-09-04 15:37:31 +02003645/*
3646 * Translate some keys pressed when 'wildmenu' is used.
3647 */
3648 int
3649wildmenu_translate_key(
3650 cmdline_info_T *cclp,
3651 int key,
3652 expand_T *xp,
3653 int did_wild_list)
3654{
3655 int c = key;
3656
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003657 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003658 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003659 // When the popup menu is used for cmdline completion:
3660 // Up : go to the previous item in the menu
3661 // Down : go to the next item in the menu
3662 // Left : go to the parent directory
3663 // Right: list the files in the selected directory
3664 switch (c)
3665 {
3666 case K_UP: c = K_LEFT; break;
3667 case K_DOWN: c = K_RIGHT; break;
3668 case K_LEFT: c = K_UP; break;
3669 case K_RIGHT: c = K_DOWN; break;
3670 default: break;
3671 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003672 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003673
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003674 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003675 {
3676 if (c == K_LEFT)
3677 c = Ctrl_P;
3678 else if (c == K_RIGHT)
3679 c = Ctrl_N;
3680 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003681
Bram Moolenaareadee482020-09-04 15:37:31 +02003682 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003683 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003684 && cclp->cmdpos > 1
3685 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3686 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3687 && (c == '\n' || c == '\r' || c == K_KENTER))
3688 c = K_DOWN;
3689
3690 return c;
3691}
3692
3693/*
3694 * Delete characters on the command line, from "from" to the current
3695 * position.
3696 */
3697 static void
3698cmdline_del(cmdline_info_T *cclp, int from)
3699{
3700 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3701 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3702 cclp->cmdlen -= cclp->cmdpos - from;
3703 cclp->cmdpos = from;
3704}
3705
3706/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003707 * Handle a key pressed when the wild menu for the menu names
3708 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003709 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003710 static int
3711wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003712{
Bram Moolenaareadee482020-09-04 15:37:31 +02003713 int i;
3714 int j;
3715
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003716 // Hitting <Down> after "emenu Name.": complete submenu
3717 if (key == K_DOWN && cclp->cmdpos > 0
3718 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003719 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003720 key = p_wc;
3721 KeyTyped = TRUE; // in case the key was mapped
3722 }
3723 else if (key == K_UP)
3724 {
3725 // Hitting <Up>: Remove one submenu name in front of the
3726 // cursor
3727 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003728
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003729 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3730 i = 0;
3731 while (--j > 0)
3732 {
3733 // check for start of menu name
3734 if (cclp->cmdbuff[j] == ' '
3735 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003736 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003737 i = j + 1;
3738 break;
3739 }
3740 // check for start of submenu name
3741 if (cclp->cmdbuff[j] == '.'
3742 && cclp->cmdbuff[j - 1] != '\\')
3743 {
3744 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003745 {
3746 i = j + 1;
3747 break;
3748 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003749 else
3750 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003751 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003752 }
3753 if (i > 0)
3754 cmdline_del(cclp, i);
3755 key = p_wc;
3756 KeyTyped = TRUE; // in case the key was mapped
3757 xp->xp_context = EXPAND_NOTHING;
3758 }
3759
3760 return key;
3761}
3762
3763/*
3764 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3765 * directory names (EXPAND_DIRECTORIES) or shell command names
3766 * (EXPAND_SHELLCMD) is displayed.
3767 */
3768 static int
3769wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3770{
3771 int i;
3772 int j;
3773 char_u upseg[5];
3774
3775 upseg[0] = PATHSEP;
3776 upseg[1] = '.';
3777 upseg[2] = '.';
3778 upseg[3] = PATHSEP;
3779 upseg[4] = NUL;
3780
3781 if (key == K_DOWN
3782 && cclp->cmdpos > 0
3783 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3784 && (cclp->cmdpos < 3
3785 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3786 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3787 {
3788 // go down a directory
3789 key = p_wc;
3790 KeyTyped = TRUE; // in case the key was mapped
3791 }
3792 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3793 {
3794 // If in a direct ancestor, strip off one ../ to go down
3795 int found = FALSE;
3796
3797 j = cclp->cmdpos;
3798 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3799 while (--j > i)
3800 {
3801 if (has_mbyte)
3802 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3803 if (vim_ispathsep(cclp->cmdbuff[j]))
3804 {
3805 found = TRUE;
3806 break;
3807 }
3808 }
3809 if (found
3810 && cclp->cmdbuff[j - 1] == '.'
3811 && cclp->cmdbuff[j - 2] == '.'
3812 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3813 {
3814 cmdline_del(cclp, j - 2);
3815 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003816 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003817 }
3818 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003819 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003820 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003821 // go up a directory
3822 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003823
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003824 j = cclp->cmdpos - 1;
3825 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3826 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003827 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003828 if (has_mbyte)
3829 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3830 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003831#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003832 && vim_strchr((char_u *)" *?[{`$%#",
3833 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003834#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003835 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003836 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003837 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003838 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003839 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003840 break;
3841 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003842 else
3843 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003844 }
3845 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003846
3847 if (!found)
3848 j = i;
3849 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3850 j += 4;
3851 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3852 && j == i)
3853 j += 3;
3854 else
3855 j = 0;
3856 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003857 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003858 // TODO this is only for DOS/UNIX systems - need to put in
3859 // machine-specific stuff here and in upseg init
3860 cmdline_del(cclp, j);
3861 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003862 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003863 else if (cclp->cmdpos > i)
3864 cmdline_del(cclp, i);
3865
3866 // Now complete in the new directory. Set KeyTyped in case the
3867 // Up key came from a mapping.
3868 key = p_wc;
3869 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003870 }
3871
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003872 return key;
3873}
3874
3875/*
3876 * Handle a key pressed when the wild menu is displayed
3877 */
3878 int
3879wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3880{
3881 if (xp->xp_context == EXPAND_MENUNAMES)
3882 return wildmenu_process_key_menunames(cclp, key, xp);
3883 else if ((xp->xp_context == EXPAND_FILES
3884 || xp->xp_context == EXPAND_DIRECTORIES
3885 || xp->xp_context == EXPAND_SHELLCMD))
3886 return wildmenu_process_key_filenames(cclp, key, xp);
3887
3888 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003889}
3890
3891/*
3892 * Free expanded names when finished walking through the matches
3893 */
3894 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003895wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003896{
3897 int skt = KeyTyped;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003898#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003899 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003900#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003901
3902 if (!p_wmnu || wild_menu_showing == 0)
3903 return;
3904
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003905#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003906 if (cclp->input_fn)
3907 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003908#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003909
3910 if (wild_menu_showing == WM_SCROLLED)
3911 {
3912 // Entered command line, move it up
3913 cmdline_row--;
3914 redrawcmd();
3915 }
3916 else if (save_p_ls != -1)
3917 {
3918 // restore 'laststatus' and 'winminheight'
3919 p_ls = save_p_ls;
3920 p_wmh = save_p_wmh;
3921 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003922 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003923 redrawcmd();
3924 save_p_ls = -1;
3925 }
3926 else
3927 {
3928 win_redraw_last_status(topframe);
3929 redraw_statuslines();
3930 }
3931 KeyTyped = skt;
3932 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003933#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003934 if (cclp->input_fn)
3935 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003936#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003937}
Bram Moolenaareadee482020-09-04 15:37:31 +02003938
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003939#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003940/*
3941 * "getcompletion()" function
3942 */
3943 void
3944f_getcompletion(typval_T *argvars, typval_T *rettv)
3945{
3946 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003947 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003948 expand_T xpc;
3949 int filtered = FALSE;
3950 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003951 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003952
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003953 if (in_vim9script()
3954 && (check_for_string_arg(argvars, 0) == FAIL
3955 || check_for_string_arg(argvars, 1) == FAIL
3956 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3957 return;
3958
ii144785fe02021-11-21 12:13:56 +00003959 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003960 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003961 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003962 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003963
3964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003965 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003966
3967 if (p_wic)
3968 options |= WILD_ICASE;
3969
3970 // For filtered results, 'wildignore' is used
3971 if (!filtered)
3972 options |= WILD_KEEP_ALL;
3973
3974 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003975 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003976 {
ii144785fe02021-11-21 12:13:56 +00003977 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003978 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003979 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003980 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003981 else
3982 {
ii144785fe02021-11-21 12:13:56 +00003983 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003984 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3985
3986 xpc.xp_context = cmdcomplete_str_to_type(type);
3987 if (xpc.xp_context == EXPAND_NOTHING)
3988 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003989 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003990 return;
3991 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003992
3993# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003994 if (xpc.xp_context == EXPAND_MENUS)
3995 {
3996 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3997 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3998 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003999# endif
4000# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004001 if (xpc.xp_context == EXPAND_CSCOPE)
4002 {
4003 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4004 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4005 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004006# endif
4007# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004008 if (xpc.xp_context == EXPAND_SIGN)
4009 {
4010 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4011 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4012 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004013# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004014 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004015
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004016 if (cmdline_fuzzy_completion_supported(&xpc))
4017 // when fuzzy matching, don't modify the search string
4018 pat = vim_strsave(xpc.xp_pattern);
4019 else
4020 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4021
Bram Moolenaar93a10962022-06-16 11:42:09 +01004022 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004023 {
4024 int i;
4025
4026 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4027
4028 for (i = 0; i < xpc.xp_numfiles; i++)
4029 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4030 }
4031 vim_free(pat);
4032 ExpandCleanup(&xpc);
4033}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004034#endif // FEAT_EVAL