blob: 3e2ecb373882628efa9346f96b068be31a713d90 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000018static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000020 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000022static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000026static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020028#endif
29
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000030// "compl_match_array" points the currently displayed list of entries in the
31// popup menu. It is NULL when there is no popup menu.
32static pumitem_T *compl_match_array = NULL;
33static int compl_match_arraysize;
34// First column in cmdline of the matched item for completion.
35static int compl_startcol;
36static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
Bram Moolenaard6e91382022-11-12 17:44:13 +000038#define SHOW_FILE_TEXT(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
52 && xp->xp_context != EXPAND_FILES
53 && xp->xp_context != EXPAND_FILES_IN_PATH
54 && xp->xp_context != EXPAND_FILETYPE
55 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
57 && xp->xp_context != EXPAND_OWNSYNTAX
58 && xp->xp_context != EXPAND_PACKADD
59 && xp->xp_context != EXPAND_SHELLCMD
60 && xp->xp_context != EXPAND_TAGS
61 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_USER_LIST);
63}
64
65/*
66 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000067 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
68 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000069 */
70 int
71cmdline_fuzzy_complete(char_u *fuzzystr)
72{
73 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
74}
75
76/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000077 * sort function for the completion matches.
78 * <SNR> functions should be sorted to the end.
79 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020080 static int
81sort_func_compare(const void *s1, const void *s2)
82{
83 char_u *p1 = *(char_u **)s1;
84 char_u *p2 = *(char_u **)s2;
85
86 if (*p1 != '<' && *p2 == '<') return -1;
87 if (*p1 == '<' && *p2 != '<') return 1;
88 return STRCMP(p1, p2);
89}
Bram Moolenaar66b51422019-08-18 21:44:12 +020090
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000091/*
92 * Escape special characters in the cmdline completion matches.
93 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020094 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000095wildescape(
96 expand_T *xp,
97 char_u *str,
98 int numfiles,
99 char_u **files)
100{
101 char_u *p;
102 int vse_what = xp->xp_context == EXPAND_BUFFERS
103 ? VSE_BUFFER : VSE_NONE;
104
105 if (xp->xp_context == EXPAND_FILES
106 || xp->xp_context == EXPAND_FILES_IN_PATH
107 || xp->xp_context == EXPAND_SHELLCMD
108 || xp->xp_context == EXPAND_BUFFERS
109 || xp->xp_context == EXPAND_DIRECTORIES)
110 {
111 // Insert a backslash into a file name before a space, \, %, #
112 // and wildmatch characters, except '~'.
113 for (int i = 0; i < numfiles; ++i)
114 {
115 // for ":set path=" we need to escape spaces twice
116 if (xp->xp_backslash == XP_BS_THREE)
117 {
118 p = vim_strsave_escaped(files[i], (char_u *)" ");
119 if (p != NULL)
120 {
121 vim_free(files[i]);
122 files[i] = p;
123#if defined(BACKSLASH_IN_FILENAME)
124 p = vim_strsave_escaped(files[i], (char_u *)" ");
125 if (p != NULL)
126 {
127 vim_free(files[i]);
128 files[i] = p;
129 }
130#endif
131 }
132 }
133#ifdef BACKSLASH_IN_FILENAME
134 p = vim_strsave_fnameescape(files[i], vse_what);
135#else
136 p = vim_strsave_fnameescape(files[i],
137 xp->xp_shell ? VSE_SHELL : vse_what);
138#endif
139 if (p != NULL)
140 {
141 vim_free(files[i]);
142 files[i] = p;
143 }
144
145 // If 'str' starts with "\~", replace "~" at start of
146 // files[i] with "\~".
147 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
148 escape_fname(&files[i]);
149 }
150 xp->xp_backslash = XP_BS_NONE;
151
152 // If the first file starts with a '+' escape it. Otherwise it
153 // could be seen as "+cmd".
154 if (*files[0] == '+')
155 escape_fname(&files[0]);
156 }
157 else if (xp->xp_context == EXPAND_TAGS)
158 {
159 // Insert a backslash before characters in a tag name that
160 // would terminate the ":tag" command.
161 for (int i = 0; i < numfiles; ++i)
162 {
163 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
164 if (p != NULL)
165 {
166 vim_free(files[i]);
167 files[i] = p;
168 }
169 }
170 }
171}
172
173/*
174 * Escape special characters in the cmdline completion matches.
175 */
176 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200177ExpandEscape(
178 expand_T *xp,
179 char_u *str,
180 int numfiles,
181 char_u **files,
182 int options)
183{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200184 // May change home directory back to "~"
185 if (options & WILD_HOME_REPLACE)
186 tilde_replace(str, numfiles, files);
187
188 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000189 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200190}
191
192/*
193 * Return FAIL if this is not an appropriate context in which to do
194 * completion of anything, return OK if it is (even if there are no matches).
195 * For the caller, this means that the character is just passed through like a
196 * normal character (instead of being expanded). This allows :s/^I^D etc.
197 */
198 int
199nextwild(
200 expand_T *xp,
201 int type,
202 int options, // extra options for ExpandOne()
203 int escape) // if TRUE, escape the returned matches
204{
205 cmdline_info_T *ccline = get_cmdline_info();
206 int i, j;
207 char_u *p1;
208 char_u *p2;
209 int difflen;
210 int v;
211
212 if (xp->xp_numfiles == -1)
213 {
214 set_expand_context(xp);
215 cmd_showtail = expand_showtail(xp);
216 }
217
218 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
219 {
220 beep_flush();
221 return OK; // Something illegal on command line
222 }
223 if (xp->xp_context == EXPAND_NOTHING)
224 {
225 // Caller can use the character as a normal char instead
226 return FAIL;
227 }
228
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000229 // If cmd_silent is set then don't show the dots, because redrawcmd() below
230 // won't remove them.
231 if (!cmd_silent)
232 {
233 msg_puts("..."); // show that we are busy
234 out_flush();
235 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200236
237 i = (int)(xp->xp_pattern - ccline->cmdbuff);
238 xp->xp_pattern_len = ccline->cmdpos - i;
239
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000240 if (type == WILD_NEXT || type == WILD_PREV
241 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200242 {
243 // Get next/previous match for a previous expanded pattern.
244 p2 = ExpandOne(xp, NULL, NULL, 0, type);
245 }
246 else
247 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000248 if (cmdline_fuzzy_completion_supported(xp))
249 // If fuzzy matching, don't modify the search string
250 p1 = vim_strsave(xp->xp_pattern);
251 else
252 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
253
Bram Moolenaar66b51422019-08-18 21:44:12 +0200254 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000255 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200256 p2 = NULL;
257 else
258 {
259 int use_options = options |
260 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
261 if (escape)
262 use_options |= WILD_ESCAPE;
263
264 if (p_wic)
265 use_options += WILD_ICASE;
266 p2 = ExpandOne(xp, p1,
267 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
268 use_options, type);
269 vim_free(p1);
270 // longest match: make sure it is not shorter, happens with :help
271 if (p2 != NULL && type == WILD_LONGEST)
272 {
273 for (j = 0; j < xp->xp_pattern_len; ++j)
274 if (ccline->cmdbuff[i + j] == '*'
275 || ccline->cmdbuff[i + j] == '?')
276 break;
277 if ((int)STRLEN(p2) < j)
278 VIM_CLEAR(p2);
279 }
280 }
281 }
282
283 if (p2 != NULL && !got_int)
284 {
285 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
286 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
287 {
288 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
289 xp->xp_pattern = ccline->cmdbuff + i;
290 }
291 else
292 v = OK;
293 if (v == OK)
294 {
295 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
296 &ccline->cmdbuff[ccline->cmdpos],
297 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
298 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
299 ccline->cmdlen += difflen;
300 ccline->cmdpos += difflen;
301 }
302 }
303 vim_free(p2);
304
305 redrawcmd();
306 cursorcmd();
307
308 // When expanding a ":map" command and no matches are found, assume that
309 // the key is supposed to be inserted literally
310 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
311 return FAIL;
312
313 if (xp->xp_numfiles <= 0 && p2 == NULL)
314 beep_flush();
315 else if (xp->xp_numfiles == 1)
316 // free expanded pattern
317 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
318
319 return OK;
320}
321
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000322/*
323 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000324 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000325 */
326 static int
327cmdline_pum_create(
328 cmdline_info_T *ccline,
329 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000330 char_u **matches,
331 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000332 int showtail)
333{
334 int i;
335 int columns;
336
337 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000338 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000339 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000340 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000341 {
342 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
343 compl_match_array[i].pum_info = NULL;
344 compl_match_array[i].pum_extra = NULL;
345 compl_match_array[i].pum_kind = NULL;
346 }
347
348 // Compute the popup menu starting column
349 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
350 columns = vim_strsize(xp->xp_pattern);
351 if (showtail)
352 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000353 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000354 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000355 }
356 if (columns >= compl_startcol)
357 compl_startcol = 0;
358 else
359 compl_startcol -= columns;
360
361 // no default selection
362 compl_selected = -1;
363
364 cmdline_pum_display();
365
366 return EXPAND_OK;
367}
368
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000369/*
370 * Display the cmdline completion matches in a popup menu
371 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000372 void
373cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000374{
375 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
376}
377
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000378/*
379 * Returns TRUE if the cmdline completion popup menu is being displayed.
380 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000381 int
382cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000383{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100384 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000385}
386
387/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000388 * Remove the cmdline completion popup menu (if present), free the list of
389 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000390 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000391 void
392cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000393{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000394 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100395 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000396
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000397 pum_undisplay();
398 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000399 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000401 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000402 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100403
404 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
405 // as a side effect.
406 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407}
408
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000409 void
410cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000411{
412 cmdline_pum_remove();
413 wildmenu_cleanup(cclp);
414}
415
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000416/*
417 * Returns the starting column number to use for the cmdline completion popup
418 * menu.
419 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000420 int
421cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000422{
423 return compl_startcol;
424}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000425
Bram Moolenaar66b51422019-08-18 21:44:12 +0200426/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000427 * Return the number of characters that should be skipped in a status match.
428 * These are backslashes used for escaping. Do show backslashes in help tags.
429 */
430 static int
431skip_status_match_char(expand_T *xp, char_u *s)
432{
433 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
434#ifdef FEAT_MENU
435 || ((xp->xp_context == EXPAND_MENUS
436 || xp->xp_context == EXPAND_MENUNAMES)
437 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
438#endif
439 )
440 {
441#ifndef BACKSLASH_IN_FILENAME
442 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
443 return 2;
444#endif
445 return 1;
446 }
447 return 0;
448}
449
450/*
451 * Get the length of an item as it will be shown in the status line.
452 */
453 static int
454status_match_len(expand_T *xp, char_u *s)
455{
456 int len = 0;
457
458#ifdef FEAT_MENU
459 int emenu = xp->xp_context == EXPAND_MENUS
460 || xp->xp_context == EXPAND_MENUNAMES;
461
462 // Check for menu separators - replace with '|'.
463 if (emenu && menu_is_separator(s))
464 return 1;
465#endif
466
467 while (*s != NUL)
468 {
469 s += skip_status_match_char(xp, s);
470 len += ptr2cells(s);
471 MB_PTR_ADV(s);
472 }
473
474 return len;
475}
476
477/*
478 * Show wildchar matches in the status line.
479 * Show at least the "match" item.
480 * We start at item 'first_match' in the list and show all matches that fit.
481 *
482 * If inversion is possible we use it. Else '=' characters are used.
483 */
484 static void
485win_redr_status_matches(
486 expand_T *xp,
487 int num_matches,
488 char_u **matches, // list of matches
489 int match,
490 int showtail)
491{
492#define L_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
493 int row;
494 char_u *buf;
495 int len;
496 int clen; // length in screen cells
497 int fillchar;
498 int attr;
499 int i;
500 int highlight = TRUE;
501 char_u *selstart = NULL;
502 int selstart_col = 0;
503 char_u *selend = NULL;
504 static int first_match = 0;
505 int add_left = FALSE;
506 char_u *s;
507#ifdef FEAT_MENU
508 int emenu;
509#endif
510 int l;
511
512 if (matches == NULL) // interrupted completion?
513 return;
514
515 if (has_mbyte)
516 buf = alloc(Columns * MB_MAXBYTES + 1);
517 else
518 buf = alloc(Columns + 1);
519 if (buf == NULL)
520 return;
521
522 if (match == -1) // don't show match but original text
523 {
524 match = 0;
525 highlight = FALSE;
526 }
527 // count 1 for the ending ">"
528 clen = status_match_len(xp, L_MATCH(match)) + 3;
529 if (match == 0)
530 first_match = 0;
531 else if (match < first_match)
532 {
533 // jumping left, as far as we can go
534 first_match = match;
535 add_left = TRUE;
536 }
537 else
538 {
539 // check if match fits on the screen
540 for (i = first_match; i < match; ++i)
541 clen += status_match_len(xp, L_MATCH(i)) + 2;
542 if (first_match > 0)
543 clen += 2;
544 // jumping right, put match at the left
545 if ((long)clen > Columns)
546 {
547 first_match = match;
548 // if showing the last match, we can add some on the left
549 clen = 2;
550 for (i = match; i < num_matches; ++i)
551 {
552 clen += status_match_len(xp, L_MATCH(i)) + 2;
553 if ((long)clen >= Columns)
554 break;
555 }
556 if (i == num_matches)
557 add_left = TRUE;
558 }
559 }
560 if (add_left)
561 while (first_match > 0)
562 {
563 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
564 if ((long)clen >= Columns)
565 break;
566 --first_match;
567 }
568
569 fillchar = fillchar_status(&attr, curwin);
570
571 if (first_match == 0)
572 {
573 *buf = NUL;
574 len = 0;
575 }
576 else
577 {
578 STRCPY(buf, "< ");
579 len = 2;
580 }
581 clen = len;
582
583 i = first_match;
584 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
585 {
586 if (i == match)
587 {
588 selstart = buf + len;
589 selstart_col = clen;
590 }
591
592 s = L_MATCH(i);
593 // Check for menu separators - replace with '|'
594#ifdef FEAT_MENU
595 emenu = (xp->xp_context == EXPAND_MENUS
596 || xp->xp_context == EXPAND_MENUNAMES);
597 if (emenu && menu_is_separator(s))
598 {
599 STRCPY(buf + len, transchar('|'));
600 l = (int)STRLEN(buf + len);
601 len += l;
602 clen += l;
603 }
604 else
605#endif
606 for ( ; *s != NUL; ++s)
607 {
608 s += skip_status_match_char(xp, s);
609 clen += ptr2cells(s);
610 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
611 {
612 STRNCPY(buf + len, s, l);
613 s += l - 1;
614 len += l;
615 }
616 else
617 {
618 STRCPY(buf + len, transchar_byte(*s));
619 len += (int)STRLEN(buf + len);
620 }
621 }
622 if (i == match)
623 selend = buf + len;
624
625 *(buf + len++) = ' ';
626 *(buf + len++) = ' ';
627 clen += 2;
628 if (++i == num_matches)
629 break;
630 }
631
632 if (i != num_matches)
633 {
634 *(buf + len++) = '>';
635 ++clen;
636 }
637
638 buf[len] = NUL;
639
640 row = cmdline_row - 1;
641 if (row >= 0)
642 {
643 if (wild_menu_showing == 0)
644 {
645 if (msg_scrolled > 0)
646 {
647 // Put the wildmenu just above the command line. If there is
648 // no room, scroll the screen one line up.
649 if (cmdline_row == Rows - 1)
650 {
651 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
652 ++msg_scrolled;
653 }
654 else
655 {
656 ++cmdline_row;
657 ++row;
658 }
659 wild_menu_showing = WM_SCROLLED;
660 }
661 else
662 {
663 // Create status line if needed by setting 'laststatus' to 2.
664 // Set 'winminheight' to zero to avoid that the window is
665 // resized.
666 if (lastwin->w_status_height == 0)
667 {
668 save_p_ls = p_ls;
669 save_p_wmh = p_wmh;
670 p_ls = 2;
671 p_wmh = 0;
672 last_status(FALSE);
673 }
674 wild_menu_showing = WM_SHOWN;
675 }
676 }
677
678 screen_puts(buf, row, 0, attr);
679 if (selstart != NULL && highlight)
680 {
681 *selend = NUL;
682 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
683 }
684
685 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
686 }
687
688 win_redraw_last_status(topframe);
689 vim_free(buf);
690}
691
692/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000693 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000694 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000695 */
696 static char_u *
697get_next_or_prev_match(
698 int mode,
699 expand_T *xp,
700 int *p_findex,
701 char_u *orig_save)
702{
703 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000704 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000705
706 if (xp->xp_numfiles <= 0)
707 return NULL;
708
709 if (mode == WILD_PREV)
710 {
711 if (findex == -1)
712 findex = xp->xp_numfiles;
713 --findex;
714 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000715 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000716 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000717 else if (mode == WILD_PAGEUP)
718 {
719 if (findex == 0)
720 // at the first entry, don't select any entries
721 findex = -1;
722 else if (findex == -1)
723 // no entry is selected. select the last entry
724 findex = xp->xp_numfiles - 1;
725 else
726 {
727 // go up by the pum height
728 ht = pum_get_height();
729 if (ht > 3)
730 ht -= 2;
731 findex -= ht;
732 if (findex < 0)
733 // few entries left, select the first entry
734 findex = 0;
735 }
736 }
737 else // mode == WILD_PAGEDOWN
738 {
739 if (findex == xp->xp_numfiles - 1)
740 // at the last entry, don't select any entries
741 findex = -1;
742 else if (findex == -1)
743 // no entry is selected. select the first entry
744 findex = 0;
745 else
746 {
747 // go down by the pum height
748 ht = pum_get_height();
749 if (ht > 3)
750 ht -= 2;
751 findex += ht;
752 if (findex >= xp->xp_numfiles)
753 // few entries left, select the last entry
754 findex = xp->xp_numfiles - 1;
755 }
756 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000757
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000758 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000759 if (findex < 0)
760 {
761 if (orig_save == NULL)
762 findex = xp->xp_numfiles - 1;
763 else
764 findex = -1;
765 }
766 if (findex >= xp->xp_numfiles)
767 {
768 if (orig_save == NULL)
769 findex = 0;
770 else
771 findex = -1;
772 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000773 if (compl_match_array)
774 {
775 compl_selected = findex;
776 cmdline_pum_display();
777 }
778 else if (p_wmnu)
779 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
780 findex, cmd_showtail);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000781 *p_findex = findex;
782
783 if (findex == -1)
784 return vim_strsave(orig_save);
785
786 return vim_strsave(xp->xp_files[findex]);
787}
788
789/*
790 * Start the command-line expansion and get the matches.
791 */
792 static char_u *
793ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
794{
795 int non_suf_match; // number without matching suffix
796 int i;
797 char_u *ss = NULL;
798
799 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000800 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000801 options) == FAIL)
802 {
803#ifdef FNAME_ILLEGAL
804 // Illegal file name has been silently skipped. But when there
805 // are wildcards, the real problem is that there was no match,
806 // causing the pattern to be added, which has illegal characters.
807 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
808 semsg(_(e_no_match_str_2), str);
809#endif
810 }
811 else if (xp->xp_numfiles == 0)
812 {
813 if (!(options & WILD_SILENT))
814 semsg(_(e_no_match_str_2), str);
815 }
816 else
817 {
818 // Escape the matches for use on the command line.
819 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
820
821 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000822 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000823 {
824 if (xp->xp_numfiles)
825 non_suf_match = xp->xp_numfiles;
826 else
827 non_suf_match = 1;
828 if ((xp->xp_context == EXPAND_FILES
829 || xp->xp_context == EXPAND_DIRECTORIES)
830 && xp->xp_numfiles > 1)
831 {
832 // More than one match; check suffix.
833 // The files will have been sorted on matching suffix in
834 // expand_wildcards, only need to check the first two.
835 non_suf_match = 0;
836 for (i = 0; i < 2; ++i)
837 if (match_suffix(xp->xp_files[i]))
838 ++non_suf_match;
839 }
840 if (non_suf_match != 1)
841 {
842 // Can we ever get here unless it's while expanding
843 // interactively? If not, we can get rid of this all
844 // together. Don't really want to wait for this message
845 // (and possibly have to hit return to continue!).
846 if (!(options & WILD_SILENT))
847 emsg(_(e_too_many_file_names));
848 else if (!(options & WILD_NO_BEEP))
849 beep_flush();
850 }
851 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
852 ss = vim_strsave(xp->xp_files[0]);
853 }
854 }
855
856 return ss;
857}
858
859/*
860 * Return the longest common part in the list of cmdline completion matches.
861 */
862 static char_u *
863find_longest_match(expand_T *xp, int options)
864{
865 long_u len;
866 int mb_len = 1;
867 int c0, ci;
868 int i;
869 char_u *ss;
870
871 for (len = 0; xp->xp_files[0][len]; len += mb_len)
872 {
873 if (has_mbyte)
874 {
875 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
876 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
877 }
878 else
879 c0 = xp->xp_files[0][len];
880 for (i = 1; i < xp->xp_numfiles; ++i)
881 {
882 if (has_mbyte)
883 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
884 else
885 ci = xp->xp_files[i][len];
886 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
887 || xp->xp_context == EXPAND_FILES
888 || xp->xp_context == EXPAND_SHELLCMD
889 || xp->xp_context == EXPAND_BUFFERS))
890 {
891 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
892 break;
893 }
894 else if (c0 != ci)
895 break;
896 }
897 if (i < xp->xp_numfiles)
898 {
899 if (!(options & WILD_NO_BEEP))
900 vim_beep(BO_WILD);
901 break;
902 }
903 }
904
905 ss = alloc(len + 1);
906 if (ss)
907 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
908
909 return ss;
910}
911
912/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000913 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200914 * Chars that should not be expanded must be preceded with a backslash.
915 * Return a pointer to allocated memory containing the new string.
916 * Return NULL for failure.
917 *
918 * "orig" is the originally expanded string, copied to allocated memory. It
919 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
920 * WILD_PREV "orig" should be NULL.
921 *
922 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
923 * is WILD_EXPAND_FREE or WILD_ALL.
924 *
925 * mode = WILD_FREE: just free previously expanded matches
926 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
927 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
928 * mode = WILD_NEXT: use next match in multiple match, wrap to first
929 * mode = WILD_PREV: use previous match in multiple match, wrap to first
930 * mode = WILD_ALL: return all matches concatenated
931 * mode = WILD_LONGEST: return longest matched part
932 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000933 * mode = WILD_APPLY: apply the item selected in the cmdline completion
934 * popup menu and close the menu.
935 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
936 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200937 *
938 * options = WILD_LIST_NOTFOUND: list entries without a match
939 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
940 * options = WILD_USE_NL: Use '\n' for WILD_ALL
941 * options = WILD_NO_BEEP: Don't beep for multiple matches
942 * options = WILD_ADD_SLASH: add a slash after directory names
943 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
944 * options = WILD_SILENT: don't print warning messages
945 * options = WILD_ESCAPE: put backslash before special chars
946 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200947 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200948 *
949 * The variables xp->xp_context and xp->xp_backslash must have been set!
950 */
951 char_u *
952ExpandOne(
953 expand_T *xp,
954 char_u *str,
955 char_u *orig, // allocated copy of original of expanded string
956 int options,
957 int mode)
958{
959 char_u *ss = NULL;
960 static int findex;
961 static char_u *orig_save = NULL; // kept value of orig
962 int orig_saved = FALSE;
963 int i;
964 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200965
966 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000967 if (mode == WILD_NEXT || mode == WILD_PREV
968 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000969 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200970
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000971 if (mode == WILD_CANCEL)
972 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
973 else if (mode == WILD_APPLY)
974 ss = vim_strsave(findex == -1 ? (orig_save ?
975 orig_save : (char_u *)"") : xp->xp_files[findex]);
976
Bram Moolenaar66b51422019-08-18 21:44:12 +0200977 // free old names
978 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
979 {
980 FreeWild(xp->xp_numfiles, xp->xp_files);
981 xp->xp_numfiles = -1;
982 VIM_CLEAR(orig_save);
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000983
984 // The entries from xp_files may be used in the PUM, remove it.
985 if (compl_match_array != NULL)
986 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200987 }
988 findex = 0;
989
990 if (mode == WILD_FREE) // only release file name
991 return NULL;
992
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000993 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200994 {
995 vim_free(orig_save);
996 orig_save = orig;
997 orig_saved = TRUE;
998
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000999 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001000 }
1001
1002 // Find longest common part
1003 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1004 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001005 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001006 findex = -1; // next p_wc gets first one
1007 }
1008
Bram Moolenaar57e95172022-08-20 19:26:14 +01001009 // Concatenate all matching names. Unless interrupted, this can be slow
1010 // and the result probably won't be used.
1011 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001012 {
1013 len = 0;
1014 for (i = 0; i < xp->xp_numfiles; ++i)
1015 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
1016 ss = alloc(len);
1017 if (ss != NULL)
1018 {
1019 *ss = NUL;
1020 for (i = 0; i < xp->xp_numfiles; ++i)
1021 {
1022 STRCAT(ss, xp->xp_files[i]);
1023 if (i != xp->xp_numfiles - 1)
1024 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1025 }
1026 }
1027 }
1028
1029 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1030 ExpandCleanup(xp);
1031
1032 // Free "orig" if it wasn't stored in "orig_save".
1033 if (!orig_saved)
1034 vim_free(orig);
1035
1036 return ss;
1037}
1038
1039/*
1040 * Prepare an expand structure for use.
1041 */
1042 void
1043ExpandInit(expand_T *xp)
1044{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001045 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001047 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001048}
1049
1050/*
1051 * Cleanup an expand structure after use.
1052 */
1053 void
1054ExpandCleanup(expand_T *xp)
1055{
1056 if (xp->xp_numfiles >= 0)
1057 {
1058 FreeWild(xp->xp_numfiles, xp->xp_files);
1059 xp->xp_numfiles = -1;
1060 }
1061}
1062
1063/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001064 * Display one line of completion matches. Multiple matches are displayed in
1065 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001066 * matches - list of completion match names
1067 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001068 * lines - number of output lines
1069 * linenr - line number of matches to display
1070 * maxlen - maximum number of characters in each line
1071 * showtail - display only the tail of the full path of a file name
1072 * dir_attr - highlight attribute to use for directory names
1073 */
1074 static void
1075showmatches_oneline(
1076 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001077 char_u **matches,
1078 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001079 int lines,
1080 int linenr,
1081 int maxlen,
1082 int showtail,
1083 int dir_attr)
1084{
1085 int i, j;
1086 int isdir;
1087 int lastlen;
1088 char_u *p;
1089
1090 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001091 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001092 {
1093 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1094 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001095 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1096 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001097 msg_advance(maxlen + 1);
1098 msg_puts((char *)p);
1099 msg_advance(maxlen + 3);
1100 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1101 break;
1102 }
1103 for (i = maxlen - lastlen; --i >= 0; )
1104 msg_putchar(' ');
1105 if (xp->xp_context == EXPAND_FILES
1106 || xp->xp_context == EXPAND_SHELLCMD
1107 || xp->xp_context == EXPAND_BUFFERS)
1108 {
1109 // highlight directories
1110 if (xp->xp_numfiles != -1)
1111 {
1112 char_u *halved_slash;
1113 char_u *exp_path;
1114 char_u *path;
1115
1116 // Expansion was done before and special characters
1117 // were escaped, need to halve backslashes. Also
1118 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001119 exp_path = expand_env_save_opt(matches[j], TRUE);
1120 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001121 halved_slash = backslash_halve_save(path);
1122 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001123 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001124 vim_free(exp_path);
1125 if (halved_slash != path)
1126 vim_free(halved_slash);
1127 }
1128 else
1129 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001130 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001131 if (showtail)
1132 p = SHOW_FILE_TEXT(j);
1133 else
1134 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001135 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001136 TRUE);
1137 p = NameBuff;
1138 }
1139 }
1140 else
1141 {
1142 isdir = FALSE;
1143 p = SHOW_FILE_TEXT(j);
1144 }
1145 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1146 }
1147 if (msg_col > 0) // when not wrapped around
1148 {
1149 msg_clr_eos();
1150 msg_putchar('\n');
1151 }
1152 out_flush(); // show one line at a time
1153}
1154
1155/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001156 * Show all matches for completion on the command line.
1157 * Returns EXPAND_NOTHING when the character that triggered expansion should
1158 * be inserted like a normal character.
1159 */
1160 int
1161showmatches(expand_T *xp, int wildmenu UNUSED)
1162{
1163 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001164 int numMatches;
1165 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001166 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001167 int maxlen;
1168 int lines;
1169 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001170 int attr;
1171 int showtail;
1172
1173 if (xp->xp_numfiles == -1)
1174 {
1175 set_expand_context(xp);
1176 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001177 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001178 showtail = expand_showtail(xp);
1179 if (i != EXPAND_OK)
1180 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001181 }
1182 else
1183 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001184 numMatches = xp->xp_numfiles;
1185 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001186 showtail = cmd_showtail;
1187 }
1188
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001189 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001190 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001191 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001192
Bram Moolenaar66b51422019-08-18 21:44:12 +02001193 if (!wildmenu)
1194 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001195 msg_didany = FALSE; // lines_left will be set
1196 msg_start(); // prepare for paging
1197 msg_putchar('\n');
1198 out_flush();
1199 cmdline_row = msg_row;
1200 msg_didany = FALSE; // lines_left will be set again
1201 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001202 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001203
1204 if (got_int)
1205 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001206 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001207 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001208 else
1209 {
1210 // find the length of the longest file name
1211 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001212 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001213 {
1214 if (!showtail && (xp->xp_context == EXPAND_FILES
1215 || xp->xp_context == EXPAND_SHELLCMD
1216 || xp->xp_context == EXPAND_BUFFERS))
1217 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001218 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001219 j = vim_strsize(NameBuff);
1220 }
1221 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001222 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 if (j > maxlen)
1224 maxlen = j;
1225 }
1226
1227 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001228 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001229 else
1230 {
1231 // compute the number of columns and lines for the listing
1232 maxlen += 2; // two spaces between file names
1233 columns = ((int)Columns + 2) / maxlen;
1234 if (columns < 1)
1235 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001236 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001237 }
1238
1239 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1240
1241 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1242 {
1243 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1244 msg_clr_eos();
1245 msg_advance(maxlen - 3);
1246 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1247 }
1248
1249 // list the files line by line
1250 for (i = 0; i < lines; ++i)
1251 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001252 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001253 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001254 if (got_int)
1255 {
1256 got_int = FALSE;
1257 break;
1258 }
1259 }
1260
1261 // we redraw the command below the lines that we have just listed
1262 // This is a bit tricky, but it saves a lot of screen updating.
1263 cmdline_row = msg_row; // will put it back later
1264 }
1265
1266 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001267 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001268
1269 return EXPAND_OK;
1270}
1271
1272/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001273 * gettail() version for showmatches() and win_redr_status_matches():
1274 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001275 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001276 static char_u *
1277showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001278{
1279 char_u *p;
1280 char_u *t = s;
1281 int had_sep = FALSE;
1282
1283 for (p = s; *p != NUL; )
1284 {
1285 if (vim_ispathsep(*p)
1286#ifdef BACKSLASH_IN_FILENAME
1287 && !rem_backslash(p)
1288#endif
1289 )
1290 had_sep = TRUE;
1291 else if (had_sep)
1292 {
1293 t = p;
1294 had_sep = FALSE;
1295 }
1296 MB_PTR_ADV(p);
1297 }
1298 return t;
1299}
1300
1301/*
1302 * Return TRUE if we only need to show the tail of completion matches.
1303 * When not completing file names or there is a wildcard in the path FALSE is
1304 * returned.
1305 */
1306 static int
1307expand_showtail(expand_T *xp)
1308{
1309 char_u *s;
1310 char_u *end;
1311
1312 // When not completing file names a "/" may mean something different.
1313 if (xp->xp_context != EXPAND_FILES
1314 && xp->xp_context != EXPAND_SHELLCMD
1315 && xp->xp_context != EXPAND_DIRECTORIES)
1316 return FALSE;
1317
1318 end = gettail(xp->xp_pattern);
1319 if (end == xp->xp_pattern) // there is no path separator
1320 return FALSE;
1321
1322 for (s = xp->xp_pattern; s < end; s++)
1323 {
1324 // Skip escaped wildcards. Only when the backslash is not a path
1325 // separator, on DOS the '*' "path\*\file" must not be skipped.
1326 if (rem_backslash(s))
1327 ++s;
1328 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1329 return FALSE;
1330 }
1331 return TRUE;
1332}
1333
1334/*
1335 * Prepare a string for expansion.
1336 * When expanding file names: The string will be used with expand_wildcards().
1337 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1338 * When expanding other names: The string will be used with regcomp(). Copy
1339 * the name into allocated memory and prepend "^".
1340 */
1341 char_u *
1342addstar(
1343 char_u *fname,
1344 int len,
1345 int context) // EXPAND_FILES etc.
1346{
1347 char_u *retval;
1348 int i, j;
1349 int new_len;
1350 char_u *tail;
1351 int ends_in_star;
1352
1353 if (context != EXPAND_FILES
1354 && context != EXPAND_FILES_IN_PATH
1355 && context != EXPAND_SHELLCMD
1356 && context != EXPAND_DIRECTORIES)
1357 {
1358 // Matching will be done internally (on something other than files).
1359 // So we convert the file-matching-type wildcards into our kind for
1360 // use with vim_regcomp(). First work out how long it will be:
1361
1362 // For help tags the translation is done in find_help_tags().
1363 // For a tag pattern starting with "/" no translation is needed.
1364 if (context == EXPAND_HELP
1365 || context == EXPAND_COLORS
1366 || context == EXPAND_COMPILER
1367 || context == EXPAND_OWNSYNTAX
1368 || context == EXPAND_FILETYPE
1369 || context == EXPAND_PACKADD
1370 || ((context == EXPAND_TAGS_LISTFILES
1371 || context == EXPAND_TAGS)
1372 && fname[0] == '/'))
1373 retval = vim_strnsave(fname, len);
1374 else
1375 {
1376 new_len = len + 2; // +2 for '^' at start, NUL at end
1377 for (i = 0; i < len; i++)
1378 {
1379 if (fname[i] == '*' || fname[i] == '~')
1380 new_len++; // '*' needs to be replaced by ".*"
1381 // '~' needs to be replaced by "\~"
1382
1383 // Buffer names are like file names. "." should be literal
1384 if (context == EXPAND_BUFFERS && fname[i] == '.')
1385 new_len++; // "." becomes "\."
1386
1387 // Custom expansion takes care of special things, match
1388 // backslashes literally (perhaps also for other types?)
1389 if ((context == EXPAND_USER_DEFINED
1390 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1391 new_len++; // '\' becomes "\\"
1392 }
1393 retval = alloc(new_len);
1394 if (retval != NULL)
1395 {
1396 retval[0] = '^';
1397 j = 1;
1398 for (i = 0; i < len; i++, j++)
1399 {
1400 // Skip backslash. But why? At least keep it for custom
1401 // expansion.
1402 if (context != EXPAND_USER_DEFINED
1403 && context != EXPAND_USER_LIST
1404 && fname[i] == '\\'
1405 && ++i == len)
1406 break;
1407
1408 switch (fname[i])
1409 {
1410 case '*': retval[j++] = '.';
1411 break;
1412 case '~': retval[j++] = '\\';
1413 break;
1414 case '?': retval[j] = '.';
1415 continue;
1416 case '.': if (context == EXPAND_BUFFERS)
1417 retval[j++] = '\\';
1418 break;
1419 case '\\': if (context == EXPAND_USER_DEFINED
1420 || context == EXPAND_USER_LIST)
1421 retval[j++] = '\\';
1422 break;
1423 }
1424 retval[j] = fname[i];
1425 }
1426 retval[j] = NUL;
1427 }
1428 }
1429 }
1430 else
1431 {
1432 retval = alloc(len + 4);
1433 if (retval != NULL)
1434 {
1435 vim_strncpy(retval, fname, len);
1436
1437 // Don't add a star to *, ~, ~user, $var or `cmd`.
1438 // * would become **, which walks the whole tree.
1439 // ~ would be at the start of the file name, but not the tail.
1440 // $ could be anywhere in the tail.
1441 // ` could be anywhere in the file name.
1442 // When the name ends in '$' don't add a star, remove the '$'.
1443 tail = gettail(retval);
1444 ends_in_star = (len > 0 && retval[len - 1] == '*');
1445#ifndef BACKSLASH_IN_FILENAME
1446 for (i = len - 2; i >= 0; --i)
1447 {
1448 if (retval[i] != '\\')
1449 break;
1450 ends_in_star = !ends_in_star;
1451 }
1452#endif
1453 if ((*retval != '~' || tail != retval)
1454 && !ends_in_star
1455 && vim_strchr(tail, '$') == NULL
1456 && vim_strchr(retval, '`') == NULL)
1457 retval[len++] = '*';
1458 else if (len > 0 && retval[len - 1] == '$')
1459 --len;
1460 retval[len] = NUL;
1461 }
1462 }
1463 return retval;
1464}
1465
1466/*
1467 * Must parse the command line so far to work out what context we are in.
1468 * Completion can then be done based on that context.
1469 * This routine sets the variables:
1470 * xp->xp_pattern The start of the pattern to be expanded within
1471 * the command line (ends at the cursor).
1472 * xp->xp_context The type of thing to expand. Will be one of:
1473 *
1474 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1475 * the command line, like an unknown command. Caller
1476 * should beep.
1477 * EXPAND_NOTHING Unrecognised context for completion, use char like
1478 * a normal char, rather than for completion. eg
1479 * :s/^I/
1480 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1481 * it.
1482 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1483 * EXPAND_FILES After command with EX_XFILE set, or after setting
1484 * with P_EXPAND set. eg :e ^I, :w>>^I
1485 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1486 * when we know only directories are of interest. eg
1487 * :set dir=^I
1488 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1489 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1490 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1491 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1492 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1493 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1494 * EXPAND_EVENTS Complete event names
1495 * EXPAND_SYNTAX Complete :syntax command arguments
1496 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1497 * EXPAND_AUGROUP Complete autocommand group names
1498 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1499 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1500 * eg :unmap a^I , :cunab x^I
1501 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1502 * eg :call sub^I
1503 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1504 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1505 * names in expressions, eg :while s^I
1506 * EXPAND_ENV_VARS Complete environment variable names
1507 * EXPAND_USER Complete user names
1508 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001509 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001510set_expand_context(expand_T *xp)
1511{
1512 cmdline_info_T *ccline = get_cmdline_info();
1513
1514 // only expansion for ':', '>' and '=' command-lines
1515 if (ccline->cmdfirstc != ':'
1516#ifdef FEAT_EVAL
1517 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1518 && !ccline->input_fn
1519#endif
1520 )
1521 {
1522 xp->xp_context = EXPAND_NOTHING;
1523 return;
1524 }
1525 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1526}
1527
Bram Moolenaard0190392019-08-23 21:17:35 +02001528/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001529 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1530 * For user defined commands, the completion context is set in 'xp' and the
1531 * completion flags in 'complp'.
1532 *
1533 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001534 */
1535 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001536set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001537{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001538 char_u *p = NULL;
1539 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001540 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001541
1542 // Isolate the command and search for it in the command table.
1543 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001544 // - the 'k' command can directly be followed by any character, but do
1545 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1546 // find matches anywhere in the command name, do this only for command
1547 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001548 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001549 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001550 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001551 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001552 p = cmd + 1;
1553 }
1554 else
1555 {
1556 p = cmd;
1557 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1558 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001559 // A user command may contain digits.
1560 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1561 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001562 while (ASCII_ISALNUM(*p) || *p == '*')
1563 ++p;
1564 // for python 3.x: ":py3*" commands completion
1565 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1566 {
1567 ++p;
1568 while (ASCII_ISALPHA(*p) || *p == '*')
1569 ++p;
1570 }
1571 // check for non-alpha command
1572 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1573 ++p;
1574 len = (int)(p - cmd);
1575
1576 if (len == 0)
1577 {
1578 xp->xp_context = EXPAND_UNSUCCESSFUL;
1579 return NULL;
1580 }
1581
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001582 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001583
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001584 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001585 // Also when doing fuzzy expansion for non-shell commands, support
1586 // alphanumeric characters.
1587 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1588 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001589 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1590 ++p;
1591 }
1592
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001593 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001594 // character, complete the command name.
1595 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1596 return NULL;
1597
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001598 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001599 {
1600 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1601 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001602 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001603 p = cmd + 1;
1604 }
1605 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1606 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001607 eap->cmd = cmd;
1608 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001609 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001610 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001611 }
1612 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001613 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001614 {
1615 // Not still touching the command and it was an illegal one
1616 xp->xp_context = EXPAND_UNSUCCESSFUL;
1617 return NULL;
1618 }
1619
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001620 return p;
1621}
Bram Moolenaard0190392019-08-23 21:17:35 +02001622
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001623/*
1624 * Set the completion context for a command argument with wild card characters.
1625 */
1626 static void
1627set_context_for_wildcard_arg(
1628 exarg_T *eap,
1629 char_u *arg,
1630 int usefilter,
1631 expand_T *xp,
1632 int *complp)
1633{
1634 char_u *p;
1635 int c;
1636 int in_quote = FALSE;
1637 char_u *bow = NULL; // Beginning of word
1638 int len = 0;
1639
1640 // Allow spaces within back-quotes to count as part of the argument
1641 // being expanded.
1642 xp->xp_pattern = skipwhite(arg);
1643 p = xp->xp_pattern;
1644 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001645 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001646 if (has_mbyte)
1647 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001648 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001649 c = *p;
1650 if (c == '\\' && p[1] != NUL)
1651 ++p;
1652 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001653 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001654 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001655 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001656 xp->xp_pattern = p;
1657 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001658 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001659 in_quote = !in_quote;
1660 }
1661 // An argument can contain just about everything, except
1662 // characters that end the command and white space.
1663 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001664#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001665 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001666#endif
1667 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001668 {
1669 len = 0; // avoid getting stuck when space is in 'isfname'
1670 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001671 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001672 if (has_mbyte)
1673 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001674 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001675 c = *p;
1676 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001678 if (has_mbyte)
1679 len = (*mb_ptr2len)(p);
1680 else
1681 len = 1;
1682 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001683 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001684 if (in_quote)
1685 bow = p;
1686 else
1687 xp->xp_pattern = p;
1688 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001689 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001690 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001691 }
1692
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001693 // If we are still inside the quotes, and we passed a space, just
1694 // expand from there.
1695 if (bow != NULL && in_quote)
1696 xp->xp_pattern = bow;
1697 xp->xp_context = EXPAND_FILES;
1698
1699 // For a shell command more chars need to be escaped.
1700 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1701 {
1702#ifndef BACKSLASH_IN_FILENAME
1703 xp->xp_shell = TRUE;
1704#endif
1705 // When still after the command name expand executables.
1706 if (xp->xp_pattern == skipwhite(arg))
1707 xp->xp_context = EXPAND_SHELLCMD;
1708 }
1709
1710 // Check for environment variable.
1711 if (*xp->xp_pattern == '$')
1712 {
1713 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1714 if (!vim_isIDc(*p))
1715 break;
1716 if (*p == NUL)
1717 {
1718 xp->xp_context = EXPAND_ENV_VARS;
1719 ++xp->xp_pattern;
1720 // Avoid that the assignment uses EXPAND_FILES again.
1721 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1722 *complp = EXPAND_ENV_VARS;
1723 }
1724 }
1725 // Check for user names.
1726 if (*xp->xp_pattern == '~')
1727 {
1728 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1729 ;
1730 // Complete ~user only if it partially matches a user name.
1731 // A full match ~user<Tab> will be replaced by user's home
1732 // directory i.e. something like ~user<Tab> -> /home/user/
1733 if (*p == NUL && p > xp->xp_pattern + 1
1734 && match_user(xp->xp_pattern + 1) >= 1)
1735 {
1736 xp->xp_context = EXPAND_USER;
1737 ++xp->xp_pattern;
1738 }
1739 }
1740}
1741
1742/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001743 * Set the completion context for the :filter command. Returns a pointer to the
1744 * next command after the :filter command.
1745 */
1746 static char_u *
1747set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1748{
1749 if (*arg != NUL)
1750 arg = skip_vimgrep_pat(arg, NULL, NULL);
1751 if (arg == NULL || *arg == NUL)
1752 {
1753 xp->xp_context = EXPAND_NOTHING;
1754 return NULL;
1755 }
1756 return skipwhite(arg);
1757}
1758
1759#ifdef FEAT_SEARCH_EXTRA
1760/*
1761 * Set the completion context for the :match command. Returns a pointer to the
1762 * next command after the :match command.
1763 */
1764 static char_u *
1765set_context_in_match_cmd(expand_T *xp, char_u *arg)
1766{
1767 if (*arg == NUL || !ends_excmd(*arg))
1768 {
1769 // also complete "None"
1770 set_context_in_echohl_cmd(xp, arg);
1771 arg = skipwhite(skiptowhite(arg));
1772 if (*arg != NUL)
1773 {
1774 xp->xp_context = EXPAND_NOTHING;
1775 arg = skip_regexp(arg + 1, *arg, magic_isset());
1776 }
1777 }
1778 return find_nextcmd(arg);
1779}
1780#endif
1781
1782/*
1783 * Returns a pointer to the next command after a :global or a :v command.
1784 * Returns NULL if there is no next command.
1785 */
1786 static char_u *
1787find_cmd_after_global_cmd(char_u *arg)
1788{
1789 int delim;
1790
1791 delim = *arg; // get the delimiter
1792 if (delim)
1793 ++arg; // skip delimiter if there is one
1794
1795 while (arg[0] != NUL && arg[0] != delim)
1796 {
1797 if (arg[0] == '\\' && arg[1] != NUL)
1798 ++arg;
1799 ++arg;
1800 }
1801 if (arg[0] != NUL)
1802 return arg + 1;
1803
1804 return NULL;
1805}
1806
1807/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001808 * Returns a pointer to the next command after a :substitute or a :& command.
1809 * Returns NULL if there is no next command.
1810 */
1811 static char_u *
1812find_cmd_after_substitute_cmd(char_u *arg)
1813{
1814 int delim;
1815
1816 delim = *arg;
1817 if (delim)
1818 {
1819 // skip "from" part
1820 ++arg;
1821 arg = skip_regexp(arg, delim, magic_isset());
1822
1823 if (arg[0] != NUL && arg[0] == delim)
1824 {
1825 // skip "to" part
1826 ++arg;
1827 while (arg[0] != NUL && arg[0] != delim)
1828 {
1829 if (arg[0] == '\\' && arg[1] != NUL)
1830 ++arg;
1831 ++arg;
1832 }
1833 if (arg[0] != NUL) // skip delimiter
1834 ++arg;
1835 }
1836 }
1837 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1838 ++arg;
1839 if (arg[0] != NUL)
1840 return arg;
1841
1842 return NULL;
1843}
1844
1845/*
1846 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1847 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1848 * Returns NULL if there is no next command.
1849 */
1850 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001851find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001852{
1853 arg = skipwhite(skipdigits(arg)); // skip count
1854 if (*arg == '/') // Match regexp, not just whole words
1855 {
1856 for (++arg; *arg && *arg != '/'; arg++)
1857 if (*arg == '\\' && arg[1] != NUL)
1858 arg++;
1859 if (*arg)
1860 {
1861 arg = skipwhite(arg + 1);
1862
1863 // Check for trailing illegal characters
1864 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1865 xp->xp_context = EXPAND_NOTHING;
1866 else
1867 return arg;
1868 }
1869 }
1870
1871 return NULL;
1872}
1873
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001874#ifdef FEAT_EVAL
1875/*
1876 * Set the completion context for the :unlet command. Always returns NULL.
1877 */
1878 static char_u *
1879set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1880{
1881 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1882 arg = xp->xp_pattern + 1;
1883
1884 xp->xp_context = EXPAND_USER_VARS;
1885 xp->xp_pattern = arg;
1886
1887 if (*xp->xp_pattern == '$')
1888 {
1889 xp->xp_context = EXPAND_ENV_VARS;
1890 ++xp->xp_pattern;
1891 }
1892
1893 return NULL;
1894}
1895#endif
1896
1897#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1898/*
1899 * Set the completion context for the :language command. Always returns NULL.
1900 */
1901 static char_u *
1902set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1903{
1904 char_u *p;
1905
1906 p = skiptowhite(arg);
1907 if (*p == NUL)
1908 {
1909 xp->xp_context = EXPAND_LANGUAGE;
1910 xp->xp_pattern = arg;
1911 }
1912 else
1913 {
1914 if ( STRNCMP(arg, "messages", p - arg) == 0
1915 || STRNCMP(arg, "ctype", p - arg) == 0
1916 || STRNCMP(arg, "time", p - arg) == 0
1917 || STRNCMP(arg, "collate", p - arg) == 0)
1918 {
1919 xp->xp_context = EXPAND_LOCALES;
1920 xp->xp_pattern = skipwhite(p);
1921 }
1922 else
1923 xp->xp_context = EXPAND_NOTHING;
1924 }
1925
1926 return NULL;
1927}
1928#endif
1929
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001930#ifdef FEAT_EVAL
1931static enum
1932{
1933 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001934 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1935 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001936} breakpt_expand_what;
1937
1938/*
1939 * Set the completion context for the :breakadd command. Always returns NULL.
1940 */
1941 static char_u *
1942set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1943{
1944 char_u *p;
1945 char_u *subcmd_start;
1946
1947 xp->xp_context = EXPAND_BREAKPOINT;
1948 xp->xp_pattern = arg;
1949
1950 if (cmdidx == CMD_breakadd)
1951 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001952 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001953 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001954 else
1955 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001956
1957 p = skipwhite(arg);
1958 if (*p == NUL)
1959 return NULL;
1960 subcmd_start = p;
1961
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001962 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001963 {
1964 // :breakadd file [lnum] <filename>
1965 // :breakadd func [lnum] <funcname>
1966 p += 4;
1967 p = skipwhite(p);
1968
1969 // skip line number (if specified)
1970 if (VIM_ISDIGIT(*p))
1971 {
1972 p = skipdigits(p);
1973 if (*p != ' ')
1974 {
1975 xp->xp_context = EXPAND_NOTHING;
1976 return NULL;
1977 }
1978 p = skipwhite(p);
1979 }
1980 if (STRNCMP("file", subcmd_start, 4) == 0)
1981 xp->xp_context = EXPAND_FILES;
1982 else
1983 xp->xp_context = EXPAND_USER_FUNC;
1984 xp->xp_pattern = p;
1985 }
1986 else if (STRNCMP("expr ", p, 5) == 0)
1987 {
1988 // :breakadd expr <expression>
1989 xp->xp_context = EXPAND_EXPRESSION;
1990 xp->xp_pattern = skipwhite(p + 5);
1991 }
1992
1993 return NULL;
1994}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001995
1996 static char_u *
1997set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
1998{
1999 char_u *p;
2000
2001 xp->xp_context = EXPAND_NOTHING;
2002 xp->xp_pattern = NULL;
2003
2004 p = skipwhite(arg);
2005 if (VIM_ISDIGIT(*p))
2006 return NULL;
2007
2008 xp->xp_context = EXPAND_SCRIPTNAMES;
2009 xp->xp_pattern = p;
2010
2011 return NULL;
2012}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002013#endif
2014
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002015/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002016 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2017 * The argument to the command is 'arg' and the argument flags is 'argt'.
2018 * For user-defined commands and for environment variables, 'compl' has the
2019 * completion type.
2020 * Returns a pointer to the next command. Returns NULL if there is no next
2021 * command.
2022 */
2023 static char_u *
2024set_context_by_cmdname(
2025 char_u *cmd,
2026 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002027 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002028 char_u *arg,
2029 long argt,
2030 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002031 int forceit)
2032{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002033 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002034 {
2035 case CMD_find:
2036 case CMD_sfind:
2037 case CMD_tabfind:
2038 if (xp->xp_context == EXPAND_FILES)
2039 xp->xp_context = EXPAND_FILES_IN_PATH;
2040 break;
2041 case CMD_cd:
2042 case CMD_chdir:
2043 case CMD_tcd:
2044 case CMD_tchdir:
2045 case CMD_lcd:
2046 case CMD_lchdir:
2047 if (xp->xp_context == EXPAND_FILES)
2048 xp->xp_context = EXPAND_DIRECTORIES;
2049 break;
2050 case CMD_help:
2051 xp->xp_context = EXPAND_HELP;
2052 xp->xp_pattern = arg;
2053 break;
2054
2055 // Command modifiers: return the argument.
2056 // Also for commands with an argument that is a command.
2057 case CMD_aboveleft:
2058 case CMD_argdo:
2059 case CMD_belowright:
2060 case CMD_botright:
2061 case CMD_browse:
2062 case CMD_bufdo:
2063 case CMD_cdo:
2064 case CMD_cfdo:
2065 case CMD_confirm:
2066 case CMD_debug:
2067 case CMD_folddoclosed:
2068 case CMD_folddoopen:
2069 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002070 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002071 case CMD_keepalt:
2072 case CMD_keepjumps:
2073 case CMD_keepmarks:
2074 case CMD_keeppatterns:
2075 case CMD_ldo:
2076 case CMD_leftabove:
2077 case CMD_lfdo:
2078 case CMD_lockmarks:
2079 case CMD_noautocmd:
2080 case CMD_noswapfile:
2081 case CMD_rightbelow:
2082 case CMD_sandbox:
2083 case CMD_silent:
2084 case CMD_tab:
2085 case CMD_tabdo:
2086 case CMD_topleft:
2087 case CMD_verbose:
2088 case CMD_vertical:
2089 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002090 case CMD_vim9cmd:
2091 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002092 return arg;
2093
2094 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002095 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002096
2097#ifdef FEAT_SEARCH_EXTRA
2098 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002099 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002100#endif
2101
2102 // All completion for the +cmdline_compl feature goes here.
2103
2104 case CMD_command:
2105 return set_context_in_user_cmd(xp, arg);
2106
2107 case CMD_delcommand:
2108 xp->xp_context = EXPAND_USER_COMMANDS;
2109 xp->xp_pattern = arg;
2110 break;
2111
2112 case CMD_global:
2113 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002114 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002115 case CMD_and:
2116 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002117 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002118 case CMD_isearch:
2119 case CMD_dsearch:
2120 case CMD_ilist:
2121 case CMD_dlist:
2122 case CMD_ijump:
2123 case CMD_psearch:
2124 case CMD_djump:
2125 case CMD_isplit:
2126 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002127 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002128 case CMD_autocmd:
2129 return set_context_in_autocmd(xp, arg, FALSE);
2130 case CMD_doautocmd:
2131 case CMD_doautoall:
2132 return set_context_in_autocmd(xp, arg, TRUE);
2133 case CMD_set:
2134 set_context_in_set_cmd(xp, arg, 0);
2135 break;
2136 case CMD_setglobal:
2137 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2138 break;
2139 case CMD_setlocal:
2140 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2141 break;
2142 case CMD_tag:
2143 case CMD_stag:
2144 case CMD_ptag:
2145 case CMD_ltag:
2146 case CMD_tselect:
2147 case CMD_stselect:
2148 case CMD_ptselect:
2149 case CMD_tjump:
2150 case CMD_stjump:
2151 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002152 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002153 xp->xp_context = EXPAND_TAGS_LISTFILES;
2154 else
2155 xp->xp_context = EXPAND_TAGS;
2156 xp->xp_pattern = arg;
2157 break;
2158 case CMD_augroup:
2159 xp->xp_context = EXPAND_AUGROUP;
2160 xp->xp_pattern = arg;
2161 break;
2162#ifdef FEAT_SYN_HL
2163 case CMD_syntax:
2164 set_context_in_syntax_cmd(xp, arg);
2165 break;
2166#endif
2167#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002168 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002169 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002170 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002171 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002172 case CMD_if:
2173 case CMD_elseif:
2174 case CMD_while:
2175 case CMD_for:
2176 case CMD_echo:
2177 case CMD_echon:
2178 case CMD_execute:
2179 case CMD_echomsg:
2180 case CMD_echoerr:
2181 case CMD_call:
2182 case CMD_return:
2183 case CMD_cexpr:
2184 case CMD_caddexpr:
2185 case CMD_cgetexpr:
2186 case CMD_lexpr:
2187 case CMD_laddexpr:
2188 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002189 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002190 break;
2191
2192 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002193 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002194 case CMD_function:
2195 case CMD_delfunction:
2196 xp->xp_context = EXPAND_USER_FUNC;
2197 xp->xp_pattern = arg;
2198 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002199 case CMD_disassemble:
2200 set_context_in_disassemble_cmd(xp, arg);
2201 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002202
2203 case CMD_echohl:
2204 set_context_in_echohl_cmd(xp, arg);
2205 break;
2206#endif
2207 case CMD_highlight:
2208 set_context_in_highlight_cmd(xp, arg);
2209 break;
2210#ifdef FEAT_CSCOPE
2211 case CMD_cscope:
2212 case CMD_lcscope:
2213 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002214 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002215 break;
2216#endif
2217#ifdef FEAT_SIGNS
2218 case CMD_sign:
2219 set_context_in_sign_cmd(xp, arg);
2220 break;
2221#endif
2222 case CMD_bdelete:
2223 case CMD_bwipeout:
2224 case CMD_bunload:
2225 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2226 arg = xp->xp_pattern + 1;
2227 // FALLTHROUGH
2228 case CMD_buffer:
2229 case CMD_sbuffer:
2230 case CMD_checktime:
2231 xp->xp_context = EXPAND_BUFFERS;
2232 xp->xp_pattern = arg;
2233 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002234#ifdef FEAT_DIFF
2235 case CMD_diffget:
2236 case CMD_diffput:
2237 // If current buffer is in diff mode, complete buffer names
2238 // which are in diff mode, and different than current buffer.
2239 xp->xp_context = EXPAND_DIFF_BUFFERS;
2240 xp->xp_pattern = arg;
2241 break;
2242#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002243 case CMD_USER:
2244 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002245 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2246 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002247
2248 case CMD_map: case CMD_noremap:
2249 case CMD_nmap: case CMD_nnoremap:
2250 case CMD_vmap: case CMD_vnoremap:
2251 case CMD_omap: case CMD_onoremap:
2252 case CMD_imap: case CMD_inoremap:
2253 case CMD_cmap: case CMD_cnoremap:
2254 case CMD_lmap: case CMD_lnoremap:
2255 case CMD_smap: case CMD_snoremap:
2256 case CMD_tmap: case CMD_tnoremap:
2257 case CMD_xmap: case CMD_xnoremap:
2258 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002259 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002260 case CMD_unmap:
2261 case CMD_nunmap:
2262 case CMD_vunmap:
2263 case CMD_ounmap:
2264 case CMD_iunmap:
2265 case CMD_cunmap:
2266 case CMD_lunmap:
2267 case CMD_sunmap:
2268 case CMD_tunmap:
2269 case CMD_xunmap:
2270 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002271 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002272 case CMD_mapclear:
2273 case CMD_nmapclear:
2274 case CMD_vmapclear:
2275 case CMD_omapclear:
2276 case CMD_imapclear:
2277 case CMD_cmapclear:
2278 case CMD_lmapclear:
2279 case CMD_smapclear:
2280 case CMD_tmapclear:
2281 case CMD_xmapclear:
2282 xp->xp_context = EXPAND_MAPCLEAR;
2283 xp->xp_pattern = arg;
2284 break;
2285
2286 case CMD_abbreviate: case CMD_noreabbrev:
2287 case CMD_cabbrev: case CMD_cnoreabbrev:
2288 case CMD_iabbrev: case CMD_inoreabbrev:
2289 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002290 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002291 case CMD_unabbreviate:
2292 case CMD_cunabbrev:
2293 case CMD_iunabbrev:
2294 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002295 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002296#ifdef FEAT_MENU
2297 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2298 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2299 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2300 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2301 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2302 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2303 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2304 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2305 case CMD_tmenu: case CMD_tunmenu:
2306 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2307 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2308#endif
2309
2310 case CMD_colorscheme:
2311 xp->xp_context = EXPAND_COLORS;
2312 xp->xp_pattern = arg;
2313 break;
2314
2315 case CMD_compiler:
2316 xp->xp_context = EXPAND_COMPILER;
2317 xp->xp_pattern = arg;
2318 break;
2319
2320 case CMD_ownsyntax:
2321 xp->xp_context = EXPAND_OWNSYNTAX;
2322 xp->xp_pattern = arg;
2323 break;
2324
2325 case CMD_setfiletype:
2326 xp->xp_context = EXPAND_FILETYPE;
2327 xp->xp_pattern = arg;
2328 break;
2329
2330 case CMD_packadd:
2331 xp->xp_context = EXPAND_PACKADD;
2332 xp->xp_pattern = arg;
2333 break;
2334
2335#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2336 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002337 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002338#endif
2339#if defined(FEAT_PROFILE)
2340 case CMD_profile:
2341 set_context_in_profile_cmd(xp, arg);
2342 break;
2343#endif
2344 case CMD_behave:
2345 xp->xp_context = EXPAND_BEHAVE;
2346 xp->xp_pattern = arg;
2347 break;
2348
2349 case CMD_messages:
2350 xp->xp_context = EXPAND_MESSAGES;
2351 xp->xp_pattern = arg;
2352 break;
2353
2354 case CMD_history:
2355 xp->xp_context = EXPAND_HISTORY;
2356 xp->xp_pattern = arg;
2357 break;
2358#if defined(FEAT_PROFILE)
2359 case CMD_syntime:
2360 xp->xp_context = EXPAND_SYNTIME;
2361 xp->xp_pattern = arg;
2362 break;
2363#endif
2364
2365 case CMD_argdelete:
2366 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2367 arg = xp->xp_pattern + 1;
2368 xp->xp_context = EXPAND_ARGLIST;
2369 xp->xp_pattern = arg;
2370 break;
2371
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002372#ifdef FEAT_EVAL
2373 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002374 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002375 case CMD_breakdel:
2376 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002377
2378 case CMD_scriptnames:
2379 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002380#endif
2381
Bram Moolenaard0190392019-08-23 21:17:35 +02002382 default:
2383 break;
2384 }
2385 return NULL;
2386}
2387
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002388/*
2389 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2390 * we don't need/want deleted. Maybe this could be done better if we didn't
2391 * repeat all this stuff. The only problem is that they may not stay
2392 * perfectly compatible with each other, but then the command line syntax
2393 * probably won't change that much -- webb.
2394 */
2395 static char_u *
2396set_one_cmd_context(
2397 expand_T *xp,
2398 char_u *buff) // buffer for command string
2399{
2400 char_u *p;
2401 char_u *cmd, *arg;
2402 int len = 0;
2403 exarg_T ea;
2404 int compl = EXPAND_NOTHING;
2405 int forceit = FALSE;
2406 int usefilter = FALSE; // filter instead of file name
2407
2408 ExpandInit(xp);
2409 xp->xp_pattern = buff;
2410 xp->xp_line = buff;
2411 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2412 ea.argt = 0;
2413
2414 // 1. skip comment lines and leading space, colons or bars
2415 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2416 ;
2417 xp->xp_pattern = cmd;
2418
2419 if (*cmd == NUL)
2420 return NULL;
2421 if (*cmd == '"') // ignore comment lines
2422 {
2423 xp->xp_context = EXPAND_NOTHING;
2424 return NULL;
2425 }
2426
2427 // 3. Skip over the range to find the command.
2428 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2429 xp->xp_pattern = cmd;
2430 if (*cmd == NUL)
2431 return NULL;
2432 if (*cmd == '"')
2433 {
2434 xp->xp_context = EXPAND_NOTHING;
2435 return NULL;
2436 }
2437
2438 if (*cmd == '|' || *cmd == '\n')
2439 return cmd + 1; // There's another command
2440
2441 // Get the command index.
2442 p = set_cmd_index(cmd, &ea, xp, &compl);
2443 if (p == NULL)
2444 return NULL;
2445
2446 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2447
2448 if (*p == '!') // forced commands
2449 {
2450 forceit = TRUE;
2451 ++p;
2452 }
2453
2454 // 6. parse arguments
2455 if (!IS_USER_CMDIDX(ea.cmdidx))
2456 ea.argt = excmd_get_argt(ea.cmdidx);
2457
2458 arg = skipwhite(p);
2459
2460 // Skip over ++argopt argument
2461 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2462 {
2463 p = arg;
2464 while (*p && !vim_isspace(*p))
2465 MB_PTR_ADV(p);
2466 arg = skipwhite(p);
2467 }
2468
2469 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2470 {
2471 if (*arg == '>') // append
2472 {
2473 if (*++arg == '>')
2474 ++arg;
2475 arg = skipwhite(arg);
2476 }
2477 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2478 {
2479 ++arg;
2480 usefilter = TRUE;
2481 }
2482 }
2483
2484 if (ea.cmdidx == CMD_read)
2485 {
2486 usefilter = forceit; // :r! filter if forced
2487 if (*arg == '!') // :r !filter
2488 {
2489 ++arg;
2490 usefilter = TRUE;
2491 }
2492 }
2493
2494 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2495 {
2496 while (*arg == *cmd) // allow any number of '>' or '<'
2497 ++arg;
2498 arg = skipwhite(arg);
2499 }
2500
2501 // Does command allow "+command"?
2502 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2503 {
2504 // Check if we're in the +command
2505 p = arg + 1;
2506 arg = skip_cmd_arg(arg, FALSE);
2507
2508 // Still touching the command after '+'?
2509 if (*arg == NUL)
2510 return p;
2511
2512 // Skip space(s) after +command to get to the real argument
2513 arg = skipwhite(arg);
2514 }
2515
2516
2517 // Check for '|' to separate commands and '"' to start comments.
2518 // Don't do this for ":read !cmd" and ":write !cmd".
2519 if ((ea.argt & EX_TRLBAR) && !usefilter)
2520 {
2521 p = arg;
2522 // ":redir @" is not the start of a comment
2523 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2524 p += 2;
2525 while (*p)
2526 {
2527 if (*p == Ctrl_V)
2528 {
2529 if (p[1] != NUL)
2530 ++p;
2531 }
2532 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2533 || *p == '|' || *p == '\n')
2534 {
2535 if (*(p - 1) != '\\')
2536 {
2537 if (*p == '|' || *p == '\n')
2538 return p + 1;
2539 return NULL; // It's a comment
2540 }
2541 }
2542 MB_PTR_ADV(p);
2543 }
2544 }
2545
2546 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2547 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2548 // no arguments allowed but there is something
2549 return NULL;
2550
2551 // Find start of last argument (argument just before cursor):
2552 p = buff;
2553 xp->xp_pattern = p;
2554 len = (int)STRLEN(buff);
2555 while (*p && p < buff + len)
2556 {
2557 if (*p == ' ' || *p == TAB)
2558 {
2559 // argument starts after a space
2560 xp->xp_pattern = ++p;
2561 }
2562 else
2563 {
2564 if (*p == '\\' && *(p + 1) != NUL)
2565 ++p; // skip over escaped character
2566 MB_PTR_ADV(p);
2567 }
2568 }
2569
2570 if (ea.argt & EX_XFILE)
2571 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2572
2573 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002574 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002575 forceit);
2576}
2577
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002578/*
2579 * Set the completion context in 'xp' for command 'str'
2580 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002581 void
2582set_cmd_context(
2583 expand_T *xp,
2584 char_u *str, // start of command line
2585 int len, // length of command line (excl. NUL)
2586 int col, // position of cursor
2587 int use_ccline UNUSED) // use ccline for info
2588{
2589#ifdef FEAT_EVAL
2590 cmdline_info_T *ccline = get_cmdline_info();
2591#endif
2592 int old_char = NUL;
2593 char_u *nextcomm;
2594
2595 // Avoid a UMR warning from Purify, only save the character if it has been
2596 // written before.
2597 if (col < len)
2598 old_char = str[col];
2599 str[col] = NUL;
2600 nextcomm = str;
2601
2602#ifdef FEAT_EVAL
2603 if (use_ccline && ccline->cmdfirstc == '=')
2604 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002605 // pass CMD_SIZE because there is no real command
2606 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002607 }
2608 else if (use_ccline && ccline->input_fn)
2609 {
2610 xp->xp_context = ccline->xp_context;
2611 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002612 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002613 }
2614 else
2615#endif
2616 while (nextcomm != NULL)
2617 nextcomm = set_one_cmd_context(xp, nextcomm);
2618
2619 // Store the string here so that call_user_expand_func() can get to them
2620 // easily.
2621 xp->xp_line = str;
2622 xp->xp_col = col;
2623
2624 str[col] = old_char;
2625}
2626
2627/*
2628 * Expand the command line "str" from context "xp".
2629 * "xp" must have been set by set_cmd_context().
2630 * xp->xp_pattern points into "str", to where the text that is to be expanded
2631 * starts.
2632 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2633 * cursor.
2634 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2635 * key that triggered expansion literally.
2636 * Returns EXPAND_OK otherwise.
2637 */
2638 int
2639expand_cmdline(
2640 expand_T *xp,
2641 char_u *str, // start of command line
2642 int col, // position of cursor
2643 int *matchcount, // return: nr of matches
2644 char_u ***matches) // return: array of pointers to matches
2645{
2646 char_u *file_str = NULL;
2647 int options = WILD_ADD_SLASH|WILD_SILENT;
2648
2649 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2650 {
2651 beep_flush();
2652 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2653 }
2654 if (xp->xp_context == EXPAND_NOTHING)
2655 {
2656 // Caller can use the character as a normal char instead
2657 return EXPAND_NOTHING;
2658 }
2659
2660 // add star to file name, or convert to regexp if not exp. files.
2661 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002662 if (cmdline_fuzzy_completion_supported(xp))
2663 // If fuzzy matching, don't modify the search string
2664 file_str = vim_strsave(xp->xp_pattern);
2665 else
2666 {
2667 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2668 if (file_str == NULL)
2669 return EXPAND_UNSUCCESSFUL;
2670 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002671
2672 if (p_wic)
2673 options += WILD_ICASE;
2674
2675 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002676 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002677 {
2678 *matchcount = 0;
2679 *matches = NULL;
2680 }
2681 vim_free(file_str);
2682
2683 return EXPAND_OK;
2684}
2685
Bram Moolenaar66b51422019-08-18 21:44:12 +02002686/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002687 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002688 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002689 */
2690 static int
2691expand_files_and_dirs(
2692 expand_T *xp,
2693 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002694 char_u ***matches,
2695 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002696 int flags,
2697 int options)
2698{
2699 int free_pat = FALSE;
2700 int i;
2701 int ret;
2702
2703 // for ":set path=" and ":set tags=" halve backslashes for escaped
2704 // space
2705 if (xp->xp_backslash != XP_BS_NONE)
2706 {
2707 free_pat = TRUE;
2708 pat = vim_strsave(pat);
2709 for (i = 0; pat[i]; ++i)
2710 if (pat[i] == '\\')
2711 {
2712 if (xp->xp_backslash == XP_BS_THREE
2713 && pat[i + 1] == '\\'
2714 && pat[i + 2] == '\\'
2715 && pat[i + 3] == ' ')
2716 STRMOVE(pat + i, pat + i + 3);
2717 if (xp->xp_backslash == XP_BS_ONE
2718 && pat[i + 1] == ' ')
2719 STRMOVE(pat + i, pat + i + 1);
2720 }
2721 }
2722
2723 if (xp->xp_context == EXPAND_FILES)
2724 flags |= EW_FILE;
2725 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2726 flags |= (EW_FILE | EW_PATH);
2727 else
2728 flags = (flags | EW_DIR) & ~EW_FILE;
2729 if (options & WILD_ICASE)
2730 flags |= EW_ICASE;
2731
2732 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002733 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002734 if (free_pat)
2735 vim_free(pat);
2736#ifdef BACKSLASH_IN_FILENAME
2737 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2738 {
2739 int j;
2740
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002741 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002742 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002743 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002744
2745 while (*ptr != NUL)
2746 {
2747 if (p_csl[0] == 's' && *ptr == '\\')
2748 *ptr = '/';
2749 else if (p_csl[0] == 'b' && *ptr == '/')
2750 *ptr = '\\';
2751 ptr += (*mb_ptr2len)(ptr);
2752 }
2753 }
2754 }
2755#endif
2756 return ret;
2757}
2758
2759/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002760 * Function given to ExpandGeneric() to obtain the possible arguments of the
2761 * ":behave {mswin,xterm}" command.
2762 */
2763 static char_u *
2764get_behave_arg(expand_T *xp UNUSED, int idx)
2765{
2766 if (idx == 0)
2767 return (char_u *)"mswin";
2768 if (idx == 1)
2769 return (char_u *)"xterm";
2770 return NULL;
2771}
2772
K.Takata161b6ac2022-11-14 15:31:07 +00002773#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002774/*
2775 * Function given to ExpandGeneric() to obtain the possible arguments of the
2776 * ":breakadd {expr, file, func, here}" command.
2777 * ":breakdel {func, file, here}" command.
2778 */
2779 static char_u *
2780get_breakadd_arg(expand_T *xp UNUSED, int idx)
2781{
2782 char *opts[] = {"expr", "file", "func", "here"};
2783
2784 if (idx >=0 && idx <= 3)
2785 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002786 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002787 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2788 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002789 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2790 {
2791 // breakdel {func, file, here}
2792 if (idx <= 2)
2793 return (char_u *)opts[idx + 1];
2794 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002795 else
2796 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002797 // profdel {func, file}
2798 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002799 return (char_u *)opts[idx + 1];
2800 }
2801 }
2802 return NULL;
2803}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002804
2805/*
2806 * Function given to ExpandGeneric() to obtain the possible arguments for the
2807 * ":scriptnames" command.
2808 */
2809 static char_u *
2810get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2811{
2812 scriptitem_T *si;
2813
2814 if (!SCRIPT_ID_VALID(idx + 1))
2815 return NULL;
2816
2817 si = SCRIPT_ITEM(idx + 1);
2818 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2819 return NameBuff;
2820}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002821#endif
2822
Bram Moolenaard0190392019-08-23 21:17:35 +02002823/*
2824 * Function given to ExpandGeneric() to obtain the possible arguments of the
2825 * ":messages {clear}" command.
2826 */
2827 static char_u *
2828get_messages_arg(expand_T *xp UNUSED, int idx)
2829{
2830 if (idx == 0)
2831 return (char_u *)"clear";
2832 return NULL;
2833}
2834
2835 static char_u *
2836get_mapclear_arg(expand_T *xp UNUSED, int idx)
2837{
2838 if (idx == 0)
2839 return (char_u *)"<buffer>";
2840 return NULL;
2841}
2842
2843/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002844 * Do the expansion based on xp->xp_context and 'rmp'.
2845 */
2846 static int
2847ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002848 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002849 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002850 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002851 char_u ***matches,
2852 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002853{
2854 static struct expgen
2855 {
2856 int context;
2857 char_u *((*func)(expand_T *, int));
2858 int ic;
2859 int escaped;
2860 } tab[] =
2861 {
2862 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2863 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2864 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2865 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2866 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2867 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2868 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2869 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2870 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2871 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002872#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002873 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2874 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2875 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2876 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2877 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002878#endif
2879#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002880 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2881 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002882#endif
2883#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002884 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002885#endif
2886#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002887 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002888#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002889 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2890 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2891 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002892#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002893 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002894#endif
2895#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002896 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002897#endif
2898#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002899 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002900#endif
2901#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002902 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2903 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002904#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002905 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2906 {EXPAND_USER, get_users, TRUE, FALSE},
2907 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002908#ifdef FEAT_EVAL
2909 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002910 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002911#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002912 };
2913 int i;
2914 int ret = FAIL;
2915
2916 // Find a context in the table and call the ExpandGeneric() with the
2917 // right function to do the expansion.
2918 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2919 {
2920 if (xp->xp_context == tab[i].context)
2921 {
2922 if (tab[i].ic)
2923 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002924 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2925 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002926 break;
2927 }
2928 }
2929
2930 return ret;
2931}
2932
2933/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002934 * Map wild expand options to flags for expand_wildcards()
2935 */
2936 static int
2937map_wildopts_to_ewflags(int options)
2938{
2939 int flags;
2940
2941 flags = EW_DIR; // include directories
2942 if (options & WILD_LIST_NOTFOUND)
2943 flags |= EW_NOTFOUND;
2944 if (options & WILD_ADD_SLASH)
2945 flags |= EW_ADDSLASH;
2946 if (options & WILD_KEEP_ALL)
2947 flags |= EW_KEEPALL;
2948 if (options & WILD_SILENT)
2949 flags |= EW_SILENT;
2950 if (options & WILD_NOERROR)
2951 flags |= EW_NOERROR;
2952 if (options & WILD_ALLLINKS)
2953 flags |= EW_ALLLINKS;
2954
2955 return flags;
2956}
2957
2958/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002959 * Do the expansion based on xp->xp_context and "pat".
2960 */
2961 static int
2962ExpandFromContext(
2963 expand_T *xp,
2964 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002965 char_u ***matches,
2966 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002967 int options) // WILD_ flags
2968{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002969 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002970 int ret;
2971 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002972 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002973 int fuzzy = cmdline_fuzzy_complete(pat)
2974 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002975
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002976 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002977
2978 if (xp->xp_context == EXPAND_FILES
2979 || xp->xp_context == EXPAND_DIRECTORIES
2980 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002981 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2982 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002983
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002984 *matches = (char_u **)"";
2985 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002986 if (xp->xp_context == EXPAND_HELP)
2987 {
2988 // With an empty argument we would get all the help tags, which is
2989 // very slow. Get matches for "help" instead.
2990 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002991 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002992 {
2993#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002994 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002995#endif
2996 return OK;
2997 }
2998 return FAIL;
2999 }
3000
Bram Moolenaar66b51422019-08-18 21:44:12 +02003001 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003002 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003003 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003004 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003005 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003006 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003007#ifdef FEAT_DIFF
3008 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003009 return ExpandBufnames(pat, numMatches, matches,
3010 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003011#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003012 if (xp->xp_context == EXPAND_TAGS
3013 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003014 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3015 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003016 if (xp->xp_context == EXPAND_COLORS)
3017 {
3018 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003019 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003020 directories);
3021 }
3022 if (xp->xp_context == EXPAND_COMPILER)
3023 {
3024 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003025 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003026 }
3027 if (xp->xp_context == EXPAND_OWNSYNTAX)
3028 {
3029 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003030 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003031 }
3032 if (xp->xp_context == EXPAND_FILETYPE)
3033 {
3034 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003035 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003036 }
K.Takata161b6ac2022-11-14 15:31:07 +00003037#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003038 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003039 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003040#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003041 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003042 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003043
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003044 // When expanding a function name starting with s:, match the <SNR>nr_
3045 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003046 if ((xp->xp_context == EXPAND_USER_FUNC
3047 || xp->xp_context == EXPAND_DISASSEMBLE)
3048 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003049 {
3050 int len = (int)STRLEN(pat) + 20;
3051
3052 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003053 if (tofree == NULL)
3054 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003055 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003056 pat = tofree;
3057 }
3058
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003059 if (!fuzzy)
3060 {
3061 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3062 if (regmatch.regprog == NULL)
3063 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003064
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003065 // set ignore-case according to p_ic, p_scs and pat
3066 regmatch.rm_ic = ignorecase(pat);
3067 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003068
3069 if (xp->xp_context == EXPAND_SETTINGS
3070 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003071 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003072 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003073 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003074#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003075 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003076 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003077#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003078 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003079 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003080
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003081 if (!fuzzy)
3082 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003083 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003084
3085 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003086}
3087
Bram Moolenaar66b51422019-08-18 21:44:12 +02003088/*
3089 * Expand a list of names.
3090 *
3091 * Generic function for command line completion. It calls a function to
3092 * obtain strings, one by one. The strings are matched against a regexp
3093 * program. Matching strings are copied into an array, which is returned.
3094 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003095 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3096 * is used.
3097 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003098 * Returns OK when no problems encountered, FAIL for error (out of memory).
3099 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003100 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003101ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003102 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003103 expand_T *xp,
3104 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003105 char_u ***matches,
3106 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003107 char_u *((*func)(expand_T *, int)),
3108 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003109 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110{
3111 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003112 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003114 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003115 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003116 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003117 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003118 int sort_matches = FALSE;
3119 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003121 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003122 *matches = NULL;
3123 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003124
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003125 if (!fuzzy)
3126 ga_init2(&ga, sizeof(char *), 30);
3127 else
3128 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3129
3130 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003131 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003132 str = (*func)(xp, i);
3133 if (str == NULL) // end of list
3134 break;
3135 if (*str == NUL) // skip empty strings
3136 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003138 if (xp->xp_pattern[0] != NUL)
3139 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003140 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003141 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003142 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003144 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003145 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003146 }
3147 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003148 else
3149 match = TRUE;
3150
3151 if (!match)
3152 continue;
3153
3154 if (escaped)
3155 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3156 else
3157 str = vim_strsave(str);
3158 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003159 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003160 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003161 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003162 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003163 return FAIL;
3164 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003165 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003166 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003167 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003168
3169 if (ga_grow(&ga, 1) == FAIL)
3170 {
3171 vim_free(str);
3172 break;
3173 }
3174
3175 if (fuzzy)
3176 {
3177 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3178 fuzmatch->idx = ga.ga_len;
3179 fuzmatch->str = str;
3180 fuzmatch->score = score;
3181 }
3182 else
3183 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3184
K.Takata161b6ac2022-11-14 15:31:07 +00003185#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003186 if (func == get_menu_names)
3187 {
3188 // test for separator added by get_menu_names()
3189 str += STRLEN(str) - 1;
3190 if (*str == '\001')
3191 *str = '.';
3192 }
K.Takata161b6ac2022-11-14 15:31:07 +00003193#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003194
3195 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003196 }
3197
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003198 if (ga.ga_len == 0)
3199 return OK;
3200
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003201 // sort the matches when using regular expression matching and sorting
3202 // applies to the completion context. Menus and scriptnames should be kept
3203 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003204 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003205 && xp->xp_context != EXPAND_MENUS
3206 && xp->xp_context != EXPAND_SCRIPTNAMES)
3207 sort_matches = TRUE;
3208
3209 // <SNR> functions should be sorted to the end.
3210 if (xp->xp_context == EXPAND_EXPRESSION
3211 || xp->xp_context == EXPAND_FUNCTIONS
3212 || xp->xp_context == EXPAND_USER_FUNC
3213 || xp->xp_context == EXPAND_DISASSEMBLE)
3214 funcsort = TRUE;
3215
3216 // Sort the matches.
3217 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003218 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003219 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003220 // <SNR> functions should be sorted to the end.
3221 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3222 sort_func_compare);
3223 else
3224 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3225 }
3226
3227 if (!fuzzy)
3228 {
3229 *matches = ga.ga_data;
3230 *numMatches = ga.ga_len;
3231 }
3232 else
3233 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003234 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3235 funcsort) == FAIL)
3236 return FAIL;
3237 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003238 }
3239
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003240#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003241 // Reset the variables used for special highlight names expansion, so that
3242 // they don't show up when getting normal highlight names by ID.
3243 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003244#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003245
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246 return OK;
3247}
3248
3249/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003250 * Expand shell command matches in one directory of $PATH.
3251 */
3252 static void
3253expand_shellcmd_onedir(
3254 char_u *buf,
3255 char_u *s,
3256 size_t l,
3257 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003258 char_u ***matches,
3259 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003260 int flags,
3261 hashtab_T *ht,
3262 garray_T *gap)
3263{
3264 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003265 hash_T hash;
3266 hashitem_T *hi;
3267
3268 vim_strncpy(buf, s, l);
3269 add_pathsep(buf);
3270 l = STRLEN(buf);
3271 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3272
3273 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003274 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003275 if (ret != OK)
3276 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003277
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003278 if (ga_grow(gap, *numMatches) == FAIL)
3279 {
3280 FreeWild(*numMatches, *matches);
3281 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003282 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003283
3284 for (int i = 0; i < *numMatches; ++i)
3285 {
3286 char_u *name = (*matches)[i];
3287
3288 if (STRLEN(name) > l)
3289 {
3290 // Check if this name was already found.
3291 hash = hash_hash(name + l);
3292 hi = hash_lookup(ht, name + l, hash);
3293 if (HASHITEM_EMPTY(hi))
3294 {
3295 // Remove the path that was prepended.
3296 STRMOVE(name, name + l);
3297 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3298 hash_add_item(ht, hi, name, hash);
3299 name = NULL;
3300 }
3301 }
3302 vim_free(name);
3303 }
3304 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003305}
3306
3307/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003308 * Complete a shell command.
3309 * Returns FAIL or OK;
3310 */
3311 static int
3312expand_shellcmd(
3313 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003314 char_u ***matches, // return: array with matches
3315 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003316 int flagsarg) // EW_ flags
3317{
3318 char_u *pat;
3319 int i;
3320 char_u *path = NULL;
3321 int mustfree = FALSE;
3322 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003323 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003324 size_t l;
3325 char_u *s, *e;
3326 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003327 int did_curdir = FALSE;
3328 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003329
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003330 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003331 if (buf == NULL)
3332 return FAIL;
3333
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003334 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003335 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003336 if (pat == NULL)
3337 {
3338 vim_free(buf);
3339 return FAIL;
3340 }
3341
Bram Moolenaar66b51422019-08-18 21:44:12 +02003342 for (i = 0; pat[i]; ++i)
3343 if (pat[i] == '\\' && pat[i + 1] == ' ')
3344 STRMOVE(pat + i, pat + i + 1);
3345
3346 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3347
3348 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3349 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3350 path = (char_u *)".";
3351 else
3352 {
3353 // For an absolute name we don't use $PATH.
3354 if (!mch_isFullName(pat))
3355 path = vim_getenv((char_u *)"PATH", &mustfree);
3356 if (path == NULL)
3357 path = (char_u *)"";
3358 }
3359
3360 // Go over all directories in $PATH. Expand matches in that directory and
3361 // collect them in "ga". When "." is not in $PATH also expand for the
3362 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003363 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003364 hash_init(&found_ht);
3365 for (s = path; ; s = e)
3366 {
K.Takata161b6ac2022-11-14 15:31:07 +00003367#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003368 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003369#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003370 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003371#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003372 if (e == NULL)
3373 e = s + STRLEN(s);
3374
3375 if (*s == NUL)
3376 {
3377 if (did_curdir)
3378 break;
3379 // Find directories in the current directory, path is empty.
3380 did_curdir = TRUE;
3381 flags |= EW_DIR;
3382 }
3383 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3384 {
3385 did_curdir = TRUE;
3386 flags |= EW_DIR;
3387 }
3388 else
3389 // Do not match directories inside a $PATH item.
3390 flags &= ~EW_DIR;
3391
3392 l = e - s;
3393 if (l > MAXPATHL - 5)
3394 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003395
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003396 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003397 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003398
Bram Moolenaar66b51422019-08-18 21:44:12 +02003399 if (*e != NUL)
3400 ++e;
3401 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003402 *matches = ga.ga_data;
3403 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003404
3405 vim_free(buf);
3406 vim_free(pat);
3407 if (mustfree)
3408 vim_free(path);
3409 hash_clear(&found_ht);
3410 return OK;
3411}
3412
K.Takata161b6ac2022-11-14 15:31:07 +00003413#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003414/*
3415 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003416 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003417 */
3418 static void *
3419call_user_expand_func(
3420 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003421 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003422{
3423 cmdline_info_T *ccline = get_cmdline_info();
3424 int keep = 0;
3425 typval_T args[4];
3426 sctx_T save_current_sctx = current_sctx;
3427 char_u *pat = NULL;
3428 void *ret;
3429
3430 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3431 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003432
3433 if (ccline->cmdbuff != NULL)
3434 {
3435 keep = ccline->cmdbuff[ccline->cmdlen];
3436 ccline->cmdbuff[ccline->cmdlen] = 0;
3437 }
3438
3439 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3440
3441 args[0].v_type = VAR_STRING;
3442 args[0].vval.v_string = pat;
3443 args[1].v_type = VAR_STRING;
3444 args[1].vval.v_string = xp->xp_line;
3445 args[2].v_type = VAR_NUMBER;
3446 args[2].vval.v_number = xp->xp_col;
3447 args[3].v_type = VAR_UNKNOWN;
3448
3449 current_sctx = xp->xp_script_ctx;
3450
3451 ret = user_expand_func(xp->xp_arg, 3, args);
3452
3453 current_sctx = save_current_sctx;
3454 if (ccline->cmdbuff != NULL)
3455 ccline->cmdbuff[ccline->cmdlen] = keep;
3456
3457 vim_free(pat);
3458 return ret;
3459}
3460
3461/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003462 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3463 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003464 */
3465 static int
3466ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003467 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003468 expand_T *xp,
3469 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003470 char_u ***matches,
3471 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472{
3473 char_u *retstr;
3474 char_u *s;
3475 char_u *e;
3476 int keep;
3477 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003478 int fuzzy;
3479 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003480 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003481
3482 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003483 *matches = NULL;
3484 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003485
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003486 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003487 if (retstr == NULL)
3488 return FAIL;
3489
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003490 if (!fuzzy)
3491 ga_init2(&ga, sizeof(char *), 3);
3492 else
3493 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3494
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495 for (s = retstr; *s != NUL; s = e)
3496 {
3497 e = vim_strchr(s, '\n');
3498 if (e == NULL)
3499 e = s + STRLEN(s);
3500 keep = *e;
3501 *e = NUL;
3502
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003503 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003504 {
3505 if (!fuzzy)
3506 match = vim_regexec(regmatch, s, (colnr_T)0);
3507 else
3508 {
3509 score = fuzzy_match_str(s, pat);
3510 match = (score != 0);
3511 }
3512 }
3513 else
3514 match = TRUE; // match everything
3515
Bram Moolenaar66b51422019-08-18 21:44:12 +02003516 *e = keep;
3517
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003518 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003519 {
3520 if (ga_grow(&ga, 1) == FAIL)
3521 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003522 if (!fuzzy)
3523 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3524 else
3525 {
3526 fuzmatch_str_T *fuzmatch =
3527 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003528 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003529 fuzmatch->str = vim_strnsave(s, e - s);
3530 fuzmatch->score = score;
3531 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003532 ++ga.ga_len;
3533 }
3534
3535 if (*e != NUL)
3536 ++e;
3537 }
3538 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003539
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003540 if (ga.ga_len == 0)
3541 return OK;
3542
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003543 if (!fuzzy)
3544 {
3545 *matches = ga.ga_data;
3546 *numMatches = ga.ga_len;
3547 }
3548 else
3549 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003550 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3551 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003552 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003553 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003554 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003555 return OK;
3556}
3557
3558/*
3559 * Expand names with a list returned by a function defined by the user.
3560 */
3561 static int
3562ExpandUserList(
3563 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003564 char_u ***matches,
3565 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003566{
3567 list_T *retlist;
3568 listitem_T *li;
3569 garray_T ga;
3570
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003571 *matches = NULL;
3572 *numMatches = 0;
3573 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003574 if (retlist == NULL)
3575 return FAIL;
3576
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003577 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003578 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003579 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003580 {
3581 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3582 continue; // Skip non-string items and empty strings
3583
3584 if (ga_grow(&ga, 1) == FAIL)
3585 break;
3586
3587 ((char_u **)ga.ga_data)[ga.ga_len] =
3588 vim_strsave(li->li_tv.vval.v_string);
3589 ++ga.ga_len;
3590 }
3591 list_unref(retlist);
3592
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003593 *matches = ga.ga_data;
3594 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003595 return OK;
3596}
K.Takata161b6ac2022-11-14 15:31:07 +00003597#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003598
3599/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003600 * Expand "file" for all comma-separated directories in "path".
3601 * Adds the matches to "ga". Caller must init "ga".
3602 */
3603 void
3604globpath(
3605 char_u *path,
3606 char_u *file,
3607 garray_T *ga,
3608 int expand_options)
3609{
3610 expand_T xpc;
3611 char_u *buf;
3612 int i;
3613 int num_p;
3614 char_u **p;
3615
3616 buf = alloc(MAXPATHL);
3617 if (buf == NULL)
3618 return;
3619
3620 ExpandInit(&xpc);
3621 xpc.xp_context = EXPAND_FILES;
3622
3623 // Loop over all entries in {path}.
3624 while (*path != NUL)
3625 {
3626 // Copy one item of the path to buf[] and concatenate the file name.
3627 copy_option_part(&path, buf, MAXPATHL, ",");
3628 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3629 {
K.Takata161b6ac2022-11-14 15:31:07 +00003630#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003631 // Using the platform's path separator (\) makes vim incorrectly
3632 // treat it as an escape character, use '/' instead.
3633 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3634 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003635#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003636 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003637#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003638 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003639 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003640 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3641 {
3642 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3643
3644 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003645 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003646 for (i = 0; i < num_p; ++i)
3647 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003648 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003649 ++ga->ga_len;
3650 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003651 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003652 }
3653 }
3654 }
3655
3656 vim_free(buf);
3657}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003658
Bram Moolenaareadee482020-09-04 15:37:31 +02003659/*
3660 * Translate some keys pressed when 'wildmenu' is used.
3661 */
3662 int
3663wildmenu_translate_key(
3664 cmdline_info_T *cclp,
3665 int key,
3666 expand_T *xp,
3667 int did_wild_list)
3668{
3669 int c = key;
3670
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003671 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003672 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003673 // When the popup menu is used for cmdline completion:
3674 // Up : go to the previous item in the menu
3675 // Down : go to the next item in the menu
3676 // Left : go to the parent directory
3677 // Right: list the files in the selected directory
3678 switch (c)
3679 {
3680 case K_UP: c = K_LEFT; break;
3681 case K_DOWN: c = K_RIGHT; break;
3682 case K_LEFT: c = K_UP; break;
3683 case K_RIGHT: c = K_DOWN; break;
3684 default: break;
3685 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003686 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003687
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003688 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003689 {
3690 if (c == K_LEFT)
3691 c = Ctrl_P;
3692 else if (c == K_RIGHT)
3693 c = Ctrl_N;
3694 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003695
Bram Moolenaareadee482020-09-04 15:37:31 +02003696 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003697 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003698 && cclp->cmdpos > 1
3699 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3700 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3701 && (c == '\n' || c == '\r' || c == K_KENTER))
3702 c = K_DOWN;
3703
3704 return c;
3705}
3706
3707/*
3708 * Delete characters on the command line, from "from" to the current
3709 * position.
3710 */
3711 static void
3712cmdline_del(cmdline_info_T *cclp, int from)
3713{
3714 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3715 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3716 cclp->cmdlen -= cclp->cmdpos - from;
3717 cclp->cmdpos = from;
3718}
3719
3720/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003721 * Handle a key pressed when the wild menu for the menu names
3722 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003723 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003724 static int
3725wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003726{
Bram Moolenaareadee482020-09-04 15:37:31 +02003727 int i;
3728 int j;
3729
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003730 // Hitting <Down> after "emenu Name.": complete submenu
3731 if (key == K_DOWN && cclp->cmdpos > 0
3732 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003733 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003734 key = p_wc;
3735 KeyTyped = TRUE; // in case the key was mapped
3736 }
3737 else if (key == K_UP)
3738 {
3739 // Hitting <Up>: Remove one submenu name in front of the
3740 // cursor
3741 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003742
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003743 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3744 i = 0;
3745 while (--j > 0)
3746 {
3747 // check for start of menu name
3748 if (cclp->cmdbuff[j] == ' '
3749 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003750 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003751 i = j + 1;
3752 break;
3753 }
3754 // check for start of submenu name
3755 if (cclp->cmdbuff[j] == '.'
3756 && cclp->cmdbuff[j - 1] != '\\')
3757 {
3758 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003759 {
3760 i = j + 1;
3761 break;
3762 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003763 else
3764 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003765 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003766 }
3767 if (i > 0)
3768 cmdline_del(cclp, i);
3769 key = p_wc;
3770 KeyTyped = TRUE; // in case the key was mapped
3771 xp->xp_context = EXPAND_NOTHING;
3772 }
3773
3774 return key;
3775}
3776
3777/*
3778 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3779 * directory names (EXPAND_DIRECTORIES) or shell command names
3780 * (EXPAND_SHELLCMD) is displayed.
3781 */
3782 static int
3783wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3784{
3785 int i;
3786 int j;
3787 char_u upseg[5];
3788
3789 upseg[0] = PATHSEP;
3790 upseg[1] = '.';
3791 upseg[2] = '.';
3792 upseg[3] = PATHSEP;
3793 upseg[4] = NUL;
3794
3795 if (key == K_DOWN
3796 && cclp->cmdpos > 0
3797 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3798 && (cclp->cmdpos < 3
3799 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3800 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3801 {
3802 // go down a directory
3803 key = p_wc;
3804 KeyTyped = TRUE; // in case the key was mapped
3805 }
3806 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3807 {
3808 // If in a direct ancestor, strip off one ../ to go down
3809 int found = FALSE;
3810
3811 j = cclp->cmdpos;
3812 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3813 while (--j > i)
3814 {
3815 if (has_mbyte)
3816 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3817 if (vim_ispathsep(cclp->cmdbuff[j]))
3818 {
3819 found = TRUE;
3820 break;
3821 }
3822 }
3823 if (found
3824 && cclp->cmdbuff[j - 1] == '.'
3825 && cclp->cmdbuff[j - 2] == '.'
3826 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3827 {
3828 cmdline_del(cclp, j - 2);
3829 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003830 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003831 }
3832 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003833 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003834 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003835 // go up a directory
3836 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003837
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003838 j = cclp->cmdpos - 1;
3839 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3840 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003841 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003842 if (has_mbyte)
3843 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3844 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003845#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003846 && vim_strchr((char_u *)" *?[{`$%#",
3847 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003848#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003849 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003850 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003851 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003852 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003853 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003854 break;
3855 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003856 else
3857 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003858 }
3859 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003860
3861 if (!found)
3862 j = i;
3863 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3864 j += 4;
3865 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3866 && j == i)
3867 j += 3;
3868 else
3869 j = 0;
3870 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003871 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003872 // TODO this is only for DOS/UNIX systems - need to put in
3873 // machine-specific stuff here and in upseg init
3874 cmdline_del(cclp, j);
3875 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003876 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003877 else if (cclp->cmdpos > i)
3878 cmdline_del(cclp, i);
3879
3880 // Now complete in the new directory. Set KeyTyped in case the
3881 // Up key came from a mapping.
3882 key = p_wc;
3883 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003884 }
3885
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003886 return key;
3887}
3888
3889/*
3890 * Handle a key pressed when the wild menu is displayed
3891 */
3892 int
3893wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3894{
3895 if (xp->xp_context == EXPAND_MENUNAMES)
3896 return wildmenu_process_key_menunames(cclp, key, xp);
3897 else if ((xp->xp_context == EXPAND_FILES
3898 || xp->xp_context == EXPAND_DIRECTORIES
3899 || xp->xp_context == EXPAND_SHELLCMD))
3900 return wildmenu_process_key_filenames(cclp, key, xp);
3901
3902 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003903}
3904
3905/*
3906 * Free expanded names when finished walking through the matches
3907 */
3908 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003909wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003910{
3911 int skt = KeyTyped;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003912#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003913 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003914#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003915
3916 if (!p_wmnu || wild_menu_showing == 0)
3917 return;
3918
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003919#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003920 if (cclp->input_fn)
3921 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003922#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003923
3924 if (wild_menu_showing == WM_SCROLLED)
3925 {
3926 // Entered command line, move it up
3927 cmdline_row--;
3928 redrawcmd();
3929 }
3930 else if (save_p_ls != -1)
3931 {
3932 // restore 'laststatus' and 'winminheight'
3933 p_ls = save_p_ls;
3934 p_wmh = save_p_wmh;
3935 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003936 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003937 redrawcmd();
3938 save_p_ls = -1;
3939 }
3940 else
3941 {
3942 win_redraw_last_status(topframe);
3943 redraw_statuslines();
3944 }
3945 KeyTyped = skt;
3946 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003947#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003948 if (cclp->input_fn)
3949 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003950#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003951}
Bram Moolenaareadee482020-09-04 15:37:31 +02003952
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003953#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003954/*
3955 * "getcompletion()" function
3956 */
3957 void
3958f_getcompletion(typval_T *argvars, typval_T *rettv)
3959{
3960 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003961 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003962 expand_T xpc;
3963 int filtered = FALSE;
3964 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003965 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003966
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003967 if (in_vim9script()
3968 && (check_for_string_arg(argvars, 0) == FAIL
3969 || check_for_string_arg(argvars, 1) == FAIL
3970 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3971 return;
3972
ii144785fe02021-11-21 12:13:56 +00003973 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003974 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003975 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003976 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003977
3978 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003979 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003980
3981 if (p_wic)
3982 options |= WILD_ICASE;
3983
3984 // For filtered results, 'wildignore' is used
3985 if (!filtered)
3986 options |= WILD_KEEP_ALL;
3987
3988 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003989 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003990 {
ii144785fe02021-11-21 12:13:56 +00003991 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003992 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003993 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003994 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003995 else
3996 {
ii144785fe02021-11-21 12:13:56 +00003997 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003998 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3999
4000 xpc.xp_context = cmdcomplete_str_to_type(type);
4001 if (xpc.xp_context == EXPAND_NOTHING)
4002 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004003 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004004 return;
4005 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004006
4007# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004008 if (xpc.xp_context == EXPAND_MENUS)
4009 {
4010 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4011 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4012 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004013# endif
4014# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004015 if (xpc.xp_context == EXPAND_CSCOPE)
4016 {
4017 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4018 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4019 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004020# endif
4021# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004022 if (xpc.xp_context == EXPAND_SIGN)
4023 {
4024 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4025 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4026 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004027# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004028 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004029
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004030 if (cmdline_fuzzy_completion_supported(&xpc))
4031 // when fuzzy matching, don't modify the search string
4032 pat = vim_strsave(xpc.xp_pattern);
4033 else
4034 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4035
Bram Moolenaar93a10962022-06-16 11:42:09 +01004036 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004037 {
4038 int i;
4039
4040 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4041
4042 for (i = 0; i < xpc.xp_numfiles; i++)
4043 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4044 }
4045 vim_free(pat);
4046 ExpandCleanup(&xpc);
4047}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004048#endif // FEAT_EVAL