blob: 8ca5ecddc399b1f727b7d979718024868833dff8 [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
zeertzjqc51a3762022-12-10 10:22:29 +000038#define SHOW_MATCH(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
roota6759382023-01-21 21:56:06 +000059 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060 && xp->xp_context != EXPAND_SHELLCMD
61 && xp->xp_context != EXPAND_TAGS
62 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_USER_LIST);
64}
65
66/*
67 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000068 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
69 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000070 */
71 int
72cmdline_fuzzy_complete(char_u *fuzzystr)
73{
74 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
75}
76
77/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000078 * sort function for the completion matches.
79 * <SNR> functions should be sorted to the end.
80 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020081 static int
82sort_func_compare(const void *s1, const void *s2)
83{
84 char_u *p1 = *(char_u **)s1;
85 char_u *p2 = *(char_u **)s2;
86
87 if (*p1 != '<' && *p2 == '<') return -1;
88 if (*p1 == '<' && *p2 != '<') return 1;
89 return STRCMP(p1, p2);
90}
Bram Moolenaar66b51422019-08-18 21:44:12 +020091
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000092/*
93 * Escape special characters in the cmdline completion matches.
94 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020095 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000096wildescape(
97 expand_T *xp,
98 char_u *str,
99 int numfiles,
100 char_u **files)
101{
102 char_u *p;
103 int vse_what = xp->xp_context == EXPAND_BUFFERS
104 ? VSE_BUFFER : VSE_NONE;
105
106 if (xp->xp_context == EXPAND_FILES
107 || xp->xp_context == EXPAND_FILES_IN_PATH
108 || xp->xp_context == EXPAND_SHELLCMD
109 || xp->xp_context == EXPAND_BUFFERS
110 || xp->xp_context == EXPAND_DIRECTORIES)
111 {
112 // Insert a backslash into a file name before a space, \, %, #
113 // and wildmatch characters, except '~'.
114 for (int i = 0; i < numfiles; ++i)
115 {
116 // for ":set path=" we need to escape spaces twice
117 if (xp->xp_backslash == XP_BS_THREE)
118 {
119 p = vim_strsave_escaped(files[i], (char_u *)" ");
120 if (p != NULL)
121 {
122 vim_free(files[i]);
123 files[i] = p;
124#if defined(BACKSLASH_IN_FILENAME)
125 p = vim_strsave_escaped(files[i], (char_u *)" ");
126 if (p != NULL)
127 {
128 vim_free(files[i]);
129 files[i] = p;
130 }
131#endif
132 }
133 }
134#ifdef BACKSLASH_IN_FILENAME
135 p = vim_strsave_fnameescape(files[i], vse_what);
136#else
137 p = vim_strsave_fnameescape(files[i],
138 xp->xp_shell ? VSE_SHELL : vse_what);
139#endif
140 if (p != NULL)
141 {
142 vim_free(files[i]);
143 files[i] = p;
144 }
145
146 // If 'str' starts with "\~", replace "~" at start of
147 // files[i] with "\~".
148 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
149 escape_fname(&files[i]);
150 }
151 xp->xp_backslash = XP_BS_NONE;
152
153 // If the first file starts with a '+' escape it. Otherwise it
154 // could be seen as "+cmd".
155 if (*files[0] == '+')
156 escape_fname(&files[0]);
157 }
158 else if (xp->xp_context == EXPAND_TAGS)
159 {
160 // Insert a backslash before characters in a tag name that
161 // would terminate the ":tag" command.
162 for (int i = 0; i < numfiles; ++i)
163 {
164 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
165 if (p != NULL)
166 {
167 vim_free(files[i]);
168 files[i] = p;
169 }
170 }
171 }
172}
173
174/*
175 * Escape special characters in the cmdline completion matches.
176 */
177 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200178ExpandEscape(
179 expand_T *xp,
180 char_u *str,
181 int numfiles,
182 char_u **files,
183 int options)
184{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200185 // May change home directory back to "~"
186 if (options & WILD_HOME_REPLACE)
187 tilde_replace(str, numfiles, files);
188
189 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000190 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200191}
192
193/*
194 * Return FAIL if this is not an appropriate context in which to do
195 * completion of anything, return OK if it is (even if there are no matches).
196 * For the caller, this means that the character is just passed through like a
197 * normal character (instead of being expanded). This allows :s/^I^D etc.
198 */
199 int
200nextwild(
201 expand_T *xp,
202 int type,
203 int options, // extra options for ExpandOne()
204 int escape) // if TRUE, escape the returned matches
205{
206 cmdline_info_T *ccline = get_cmdline_info();
207 int i, j;
208 char_u *p1;
209 char_u *p2;
210 int difflen;
211 int v;
212
213 if (xp->xp_numfiles == -1)
214 {
215 set_expand_context(xp);
216 cmd_showtail = expand_showtail(xp);
217 }
218
219 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
220 {
221 beep_flush();
222 return OK; // Something illegal on command line
223 }
224 if (xp->xp_context == EXPAND_NOTHING)
225 {
226 // Caller can use the character as a normal char instead
227 return FAIL;
228 }
229
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000230 // If cmd_silent is set then don't show the dots, because redrawcmd() below
231 // won't remove them.
232 if (!cmd_silent)
233 {
234 msg_puts("..."); // show that we are busy
235 out_flush();
236 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200237
238 i = (int)(xp->xp_pattern - ccline->cmdbuff);
239 xp->xp_pattern_len = ccline->cmdpos - i;
240
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000241 if (type == WILD_NEXT || type == WILD_PREV
242 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200243 {
244 // Get next/previous match for a previous expanded pattern.
245 p2 = ExpandOne(xp, NULL, NULL, 0, type);
246 }
247 else
248 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000249 if (cmdline_fuzzy_completion_supported(xp))
250 // If fuzzy matching, don't modify the search string
251 p1 = vim_strsave(xp->xp_pattern);
252 else
253 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
254
Bram Moolenaar66b51422019-08-18 21:44:12 +0200255 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000256 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200257 p2 = NULL;
258 else
259 {
260 int use_options = options |
261 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
262 if (escape)
263 use_options |= WILD_ESCAPE;
264
265 if (p_wic)
266 use_options += WILD_ICASE;
267 p2 = ExpandOne(xp, p1,
268 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
269 use_options, type);
270 vim_free(p1);
271 // longest match: make sure it is not shorter, happens with :help
272 if (p2 != NULL && type == WILD_LONGEST)
273 {
274 for (j = 0; j < xp->xp_pattern_len; ++j)
275 if (ccline->cmdbuff[i + j] == '*'
276 || ccline->cmdbuff[i + j] == '?')
277 break;
278 if ((int)STRLEN(p2) < j)
279 VIM_CLEAR(p2);
280 }
281 }
282 }
283
284 if (p2 != NULL && !got_int)
285 {
286 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
287 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
288 {
289 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
290 xp->xp_pattern = ccline->cmdbuff + i;
291 }
292 else
293 v = OK;
294 if (v == OK)
295 {
296 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
297 &ccline->cmdbuff[ccline->cmdpos],
298 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
299 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
300 ccline->cmdlen += difflen;
301 ccline->cmdpos += difflen;
302 }
303 }
304 vim_free(p2);
305
306 redrawcmd();
307 cursorcmd();
308
309 // When expanding a ":map" command and no matches are found, assume that
310 // the key is supposed to be inserted literally
311 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
312 return FAIL;
313
314 if (xp->xp_numfiles <= 0 && p2 == NULL)
315 beep_flush();
316 else if (xp->xp_numfiles == 1)
317 // free expanded pattern
318 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
319
320 return OK;
321}
322
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000323/*
324 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000325 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000326 */
327 static int
328cmdline_pum_create(
329 cmdline_info_T *ccline,
330 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000331 char_u **matches,
332 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000333 int showtail)
334{
335 int i;
336 int columns;
337
338 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000339 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000340 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000341 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000342 {
zeertzjqc51a3762022-12-10 10:22:29 +0000343 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000344 compl_match_array[i].pum_info = NULL;
345 compl_match_array[i].pum_extra = NULL;
346 compl_match_array[i].pum_kind = NULL;
347 }
348
349 // Compute the popup menu starting column
350 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
351 columns = vim_strsize(xp->xp_pattern);
352 if (showtail)
353 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000354 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 }
357 if (columns >= compl_startcol)
358 compl_startcol = 0;
359 else
360 compl_startcol -= columns;
361
362 // no default selection
363 compl_selected = -1;
364
365 cmdline_pum_display();
366
367 return EXPAND_OK;
368}
369
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000370/*
371 * Display the cmdline completion matches in a popup menu
372 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000373 void
374cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000375{
376 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
377}
378
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000379/*
380 * Returns TRUE if the cmdline completion popup menu is being displayed.
381 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000382 int
383cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000384{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100385 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000386}
387
388/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000389 * Remove the cmdline completion popup menu (if present), free the list of
390 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000391 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000392 void
393cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000394{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000395 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100396 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000397
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000398 pum_undisplay();
399 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000400 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000401 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000402 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000403 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100404
405 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
406 // as a side effect.
407 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000408}
409
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000410 void
411cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000412{
413 cmdline_pum_remove();
414 wildmenu_cleanup(cclp);
415}
416
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000417/*
418 * Returns the starting column number to use for the cmdline completion popup
419 * menu.
420 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000421 int
422cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423{
424 return compl_startcol;
425}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426
Bram Moolenaar66b51422019-08-18 21:44:12 +0200427/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000428 * Return the number of characters that should be skipped in a status match.
429 * These are backslashes used for escaping. Do show backslashes in help tags.
430 */
431 static int
432skip_status_match_char(expand_T *xp, char_u *s)
433{
434 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
435#ifdef FEAT_MENU
436 || ((xp->xp_context == EXPAND_MENUS
437 || xp->xp_context == EXPAND_MENUNAMES)
438 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
439#endif
440 )
441 {
442#ifndef BACKSLASH_IN_FILENAME
443 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
444 return 2;
445#endif
446 return 1;
447 }
448 return 0;
449}
450
451/*
452 * Get the length of an item as it will be shown in the status line.
453 */
454 static int
455status_match_len(expand_T *xp, char_u *s)
456{
457 int len = 0;
458
459#ifdef FEAT_MENU
460 int emenu = xp->xp_context == EXPAND_MENUS
461 || xp->xp_context == EXPAND_MENUNAMES;
462
463 // Check for menu separators - replace with '|'.
464 if (emenu && menu_is_separator(s))
465 return 1;
466#endif
467
468 while (*s != NUL)
469 {
470 s += skip_status_match_char(xp, s);
471 len += ptr2cells(s);
472 MB_PTR_ADV(s);
473 }
474
475 return len;
476}
477
478/*
479 * Show wildchar matches in the status line.
480 * Show at least the "match" item.
481 * We start at item 'first_match' in the list and show all matches that fit.
482 *
483 * If inversion is possible we use it. Else '=' characters are used.
484 */
485 static void
486win_redr_status_matches(
487 expand_T *xp,
488 int num_matches,
489 char_u **matches, // list of matches
490 int match,
491 int showtail)
492{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000493 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 ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000528 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000529 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)
zeertzjqc51a3762022-12-10 10:22:29 +0000541 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000542 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 {
zeertzjqc51a3762022-12-10 10:22:29 +0000552 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000553 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 {
zeertzjqc51a3762022-12-10 10:22:29 +0000563 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000564 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;
zeertzjqc51a3762022-12-10 10:22:29 +0000584 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000585 {
586 if (i == match)
587 {
588 selstart = buf + len;
589 selstart_col = clen;
590 }
591
zeertzjqc51a3762022-12-10 10:22:29 +0000592 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000593 // 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)
zeertzjqc51a3762022-12-10 10:22:29 +00001132 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001133 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;
zeertzjqc51a3762022-12-10 10:22:29 +00001143 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001144 }
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
zeertzjqc51a3762022-12-10 10:22:29 +00001222 j = vim_strsize(SHOW_MATCH(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
roota6759382023-01-21 21:56:06 +00001366 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001367 || context == EXPAND_COMPILER
1368 || context == EXPAND_OWNSYNTAX
1369 || context == EXPAND_FILETYPE
1370 || context == EXPAND_PACKADD
1371 || ((context == EXPAND_TAGS_LISTFILES
1372 || context == EXPAND_TAGS)
1373 && fname[0] == '/'))
1374 retval = vim_strnsave(fname, len);
1375 else
1376 {
1377 new_len = len + 2; // +2 for '^' at start, NUL at end
1378 for (i = 0; i < len; i++)
1379 {
1380 if (fname[i] == '*' || fname[i] == '~')
1381 new_len++; // '*' needs to be replaced by ".*"
1382 // '~' needs to be replaced by "\~"
1383
1384 // Buffer names are like file names. "." should be literal
1385 if (context == EXPAND_BUFFERS && fname[i] == '.')
1386 new_len++; // "." becomes "\."
1387
1388 // Custom expansion takes care of special things, match
1389 // backslashes literally (perhaps also for other types?)
1390 if ((context == EXPAND_USER_DEFINED
1391 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1392 new_len++; // '\' becomes "\\"
1393 }
1394 retval = alloc(new_len);
1395 if (retval != NULL)
1396 {
1397 retval[0] = '^';
1398 j = 1;
1399 for (i = 0; i < len; i++, j++)
1400 {
1401 // Skip backslash. But why? At least keep it for custom
1402 // expansion.
1403 if (context != EXPAND_USER_DEFINED
1404 && context != EXPAND_USER_LIST
1405 && fname[i] == '\\'
1406 && ++i == len)
1407 break;
1408
1409 switch (fname[i])
1410 {
1411 case '*': retval[j++] = '.';
1412 break;
1413 case '~': retval[j++] = '\\';
1414 break;
1415 case '?': retval[j] = '.';
1416 continue;
1417 case '.': if (context == EXPAND_BUFFERS)
1418 retval[j++] = '\\';
1419 break;
1420 case '\\': if (context == EXPAND_USER_DEFINED
1421 || context == EXPAND_USER_LIST)
1422 retval[j++] = '\\';
1423 break;
1424 }
1425 retval[j] = fname[i];
1426 }
1427 retval[j] = NUL;
1428 }
1429 }
1430 }
1431 else
1432 {
1433 retval = alloc(len + 4);
1434 if (retval != NULL)
1435 {
1436 vim_strncpy(retval, fname, len);
1437
1438 // Don't add a star to *, ~, ~user, $var or `cmd`.
1439 // * would become **, which walks the whole tree.
1440 // ~ would be at the start of the file name, but not the tail.
1441 // $ could be anywhere in the tail.
1442 // ` could be anywhere in the file name.
1443 // When the name ends in '$' don't add a star, remove the '$'.
1444 tail = gettail(retval);
1445 ends_in_star = (len > 0 && retval[len - 1] == '*');
1446#ifndef BACKSLASH_IN_FILENAME
1447 for (i = len - 2; i >= 0; --i)
1448 {
1449 if (retval[i] != '\\')
1450 break;
1451 ends_in_star = !ends_in_star;
1452 }
1453#endif
1454 if ((*retval != '~' || tail != retval)
1455 && !ends_in_star
1456 && vim_strchr(tail, '$') == NULL
1457 && vim_strchr(retval, '`') == NULL)
1458 retval[len++] = '*';
1459 else if (len > 0 && retval[len - 1] == '$')
1460 --len;
1461 retval[len] = NUL;
1462 }
1463 }
1464 return retval;
1465}
1466
1467/*
1468 * Must parse the command line so far to work out what context we are in.
1469 * Completion can then be done based on that context.
1470 * This routine sets the variables:
1471 * xp->xp_pattern The start of the pattern to be expanded within
1472 * the command line (ends at the cursor).
1473 * xp->xp_context The type of thing to expand. Will be one of:
1474 *
1475 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1476 * the command line, like an unknown command. Caller
1477 * should beep.
1478 * EXPAND_NOTHING Unrecognised context for completion, use char like
1479 * a normal char, rather than for completion. eg
1480 * :s/^I/
1481 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1482 * it.
1483 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1484 * EXPAND_FILES After command with EX_XFILE set, or after setting
1485 * with P_EXPAND set. eg :e ^I, :w>>^I
1486 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1487 * when we know only directories are of interest. eg
1488 * :set dir=^I
1489 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1490 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1491 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1492 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1493 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1494 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1495 * EXPAND_EVENTS Complete event names
1496 * EXPAND_SYNTAX Complete :syntax command arguments
1497 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1498 * EXPAND_AUGROUP Complete autocommand group names
1499 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1500 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1501 * eg :unmap a^I , :cunab x^I
1502 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1503 * eg :call sub^I
1504 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1505 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1506 * names in expressions, eg :while s^I
1507 * EXPAND_ENV_VARS Complete environment variable names
1508 * EXPAND_USER Complete user names
1509 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001510 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001511set_expand_context(expand_T *xp)
1512{
1513 cmdline_info_T *ccline = get_cmdline_info();
1514
1515 // only expansion for ':', '>' and '=' command-lines
1516 if (ccline->cmdfirstc != ':'
1517#ifdef FEAT_EVAL
1518 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1519 && !ccline->input_fn
1520#endif
1521 )
1522 {
1523 xp->xp_context = EXPAND_NOTHING;
1524 return;
1525 }
1526 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1527}
1528
Bram Moolenaard0190392019-08-23 21:17:35 +02001529/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001530 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1531 * For user defined commands, the completion context is set in 'xp' and the
1532 * completion flags in 'complp'.
1533 *
1534 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001535 */
1536 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001537set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001538{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001539 char_u *p = NULL;
1540 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001541 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001542
1543 // Isolate the command and search for it in the command table.
1544 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001545 // - the 'k' command can directly be followed by any character, but do
1546 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1547 // find matches anywhere in the command name, do this only for command
1548 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001549 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001550 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001551 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001552 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001553 p = cmd + 1;
1554 }
1555 else
1556 {
1557 p = cmd;
1558 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1559 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001560 // A user command may contain digits.
1561 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1562 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001563 while (ASCII_ISALNUM(*p) || *p == '*')
1564 ++p;
1565 // for python 3.x: ":py3*" commands completion
1566 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1567 {
1568 ++p;
1569 while (ASCII_ISALPHA(*p) || *p == '*')
1570 ++p;
1571 }
1572 // check for non-alpha command
1573 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1574 ++p;
1575 len = (int)(p - cmd);
1576
1577 if (len == 0)
1578 {
1579 xp->xp_context = EXPAND_UNSUCCESSFUL;
1580 return NULL;
1581 }
1582
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001583 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001584
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001585 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001586 // Also when doing fuzzy expansion for non-shell commands, support
1587 // alphanumeric characters.
1588 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1589 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001590 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1591 ++p;
1592 }
1593
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001594 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001595 // character, complete the command name.
1596 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1597 return NULL;
1598
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001599 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001600 {
1601 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1602 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001603 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001604 p = cmd + 1;
1605 }
1606 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1607 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001608 eap->cmd = cmd;
1609 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001610 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001611 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001612 }
1613 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001614 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001615 {
1616 // Not still touching the command and it was an illegal one
1617 xp->xp_context = EXPAND_UNSUCCESSFUL;
1618 return NULL;
1619 }
1620
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001621 return p;
1622}
Bram Moolenaard0190392019-08-23 21:17:35 +02001623
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001624/*
1625 * Set the completion context for a command argument with wild card characters.
1626 */
1627 static void
1628set_context_for_wildcard_arg(
1629 exarg_T *eap,
1630 char_u *arg,
1631 int usefilter,
1632 expand_T *xp,
1633 int *complp)
1634{
1635 char_u *p;
1636 int c;
1637 int in_quote = FALSE;
1638 char_u *bow = NULL; // Beginning of word
1639 int len = 0;
1640
1641 // Allow spaces within back-quotes to count as part of the argument
1642 // being expanded.
1643 xp->xp_pattern = skipwhite(arg);
1644 p = xp->xp_pattern;
1645 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001646 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001647 if (has_mbyte)
1648 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001649 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001650 c = *p;
1651 if (c == '\\' && p[1] != NUL)
1652 ++p;
1653 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001654 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001655 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001656 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001657 xp->xp_pattern = p;
1658 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001659 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001660 in_quote = !in_quote;
1661 }
1662 // An argument can contain just about everything, except
1663 // characters that end the command and white space.
1664 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001665#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001667#endif
1668 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 {
1670 len = 0; // avoid getting stuck when space is in 'isfname'
1671 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001672 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001673 if (has_mbyte)
1674 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001675 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001676 c = *p;
1677 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001678 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001679 if (has_mbyte)
1680 len = (*mb_ptr2len)(p);
1681 else
1682 len = 1;
1683 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001684 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685 if (in_quote)
1686 bow = p;
1687 else
1688 xp->xp_pattern = p;
1689 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001690 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001691 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001692 }
1693
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001694 // If we are still inside the quotes, and we passed a space, just
1695 // expand from there.
1696 if (bow != NULL && in_quote)
1697 xp->xp_pattern = bow;
1698 xp->xp_context = EXPAND_FILES;
1699
1700 // For a shell command more chars need to be escaped.
1701 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1702 {
1703#ifndef BACKSLASH_IN_FILENAME
1704 xp->xp_shell = TRUE;
1705#endif
1706 // When still after the command name expand executables.
1707 if (xp->xp_pattern == skipwhite(arg))
1708 xp->xp_context = EXPAND_SHELLCMD;
1709 }
1710
1711 // Check for environment variable.
1712 if (*xp->xp_pattern == '$')
1713 {
1714 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1715 if (!vim_isIDc(*p))
1716 break;
1717 if (*p == NUL)
1718 {
1719 xp->xp_context = EXPAND_ENV_VARS;
1720 ++xp->xp_pattern;
1721 // Avoid that the assignment uses EXPAND_FILES again.
1722 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1723 *complp = EXPAND_ENV_VARS;
1724 }
1725 }
1726 // Check for user names.
1727 if (*xp->xp_pattern == '~')
1728 {
1729 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1730 ;
1731 // Complete ~user only if it partially matches a user name.
1732 // A full match ~user<Tab> will be replaced by user's home
1733 // directory i.e. something like ~user<Tab> -> /home/user/
1734 if (*p == NUL && p > xp->xp_pattern + 1
1735 && match_user(xp->xp_pattern + 1) >= 1)
1736 {
1737 xp->xp_context = EXPAND_USER;
1738 ++xp->xp_pattern;
1739 }
1740 }
1741}
1742
1743/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001744 * Set the completion context for the :filter command. Returns a pointer to the
1745 * next command after the :filter command.
1746 */
1747 static char_u *
1748set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1749{
1750 if (*arg != NUL)
1751 arg = skip_vimgrep_pat(arg, NULL, NULL);
1752 if (arg == NULL || *arg == NUL)
1753 {
1754 xp->xp_context = EXPAND_NOTHING;
1755 return NULL;
1756 }
1757 return skipwhite(arg);
1758}
1759
1760#ifdef FEAT_SEARCH_EXTRA
1761/*
1762 * Set the completion context for the :match command. Returns a pointer to the
1763 * next command after the :match command.
1764 */
1765 static char_u *
1766set_context_in_match_cmd(expand_T *xp, char_u *arg)
1767{
1768 if (*arg == NUL || !ends_excmd(*arg))
1769 {
1770 // also complete "None"
1771 set_context_in_echohl_cmd(xp, arg);
1772 arg = skipwhite(skiptowhite(arg));
1773 if (*arg != NUL)
1774 {
1775 xp->xp_context = EXPAND_NOTHING;
1776 arg = skip_regexp(arg + 1, *arg, magic_isset());
1777 }
1778 }
1779 return find_nextcmd(arg);
1780}
1781#endif
1782
1783/*
1784 * Returns a pointer to the next command after a :global or a :v command.
1785 * Returns NULL if there is no next command.
1786 */
1787 static char_u *
1788find_cmd_after_global_cmd(char_u *arg)
1789{
1790 int delim;
1791
1792 delim = *arg; // get the delimiter
1793 if (delim)
1794 ++arg; // skip delimiter if there is one
1795
1796 while (arg[0] != NUL && arg[0] != delim)
1797 {
1798 if (arg[0] == '\\' && arg[1] != NUL)
1799 ++arg;
1800 ++arg;
1801 }
1802 if (arg[0] != NUL)
1803 return arg + 1;
1804
1805 return NULL;
1806}
1807
1808/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001809 * Returns a pointer to the next command after a :substitute or a :& command.
1810 * Returns NULL if there is no next command.
1811 */
1812 static char_u *
1813find_cmd_after_substitute_cmd(char_u *arg)
1814{
1815 int delim;
1816
1817 delim = *arg;
1818 if (delim)
1819 {
1820 // skip "from" part
1821 ++arg;
1822 arg = skip_regexp(arg, delim, magic_isset());
1823
1824 if (arg[0] != NUL && arg[0] == delim)
1825 {
1826 // skip "to" part
1827 ++arg;
1828 while (arg[0] != NUL && arg[0] != delim)
1829 {
1830 if (arg[0] == '\\' && arg[1] != NUL)
1831 ++arg;
1832 ++arg;
1833 }
1834 if (arg[0] != NUL) // skip delimiter
1835 ++arg;
1836 }
1837 }
1838 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1839 ++arg;
1840 if (arg[0] != NUL)
1841 return arg;
1842
1843 return NULL;
1844}
1845
1846/*
1847 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1848 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1849 * Returns NULL if there is no next command.
1850 */
1851 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001852find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001853{
1854 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001855 if (*arg != '/')
1856 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001857
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001858 // Match regexp, not just whole words
1859 for (++arg; *arg && *arg != '/'; arg++)
1860 if (*arg == '\\' && arg[1] != NUL)
1861 arg++;
1862 if (*arg)
1863 {
1864 arg = skipwhite(arg + 1);
1865
1866 // Check for trailing illegal characters
1867 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1868 xp->xp_context = EXPAND_NOTHING;
1869 else
1870 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001871 }
1872
1873 return NULL;
1874}
1875
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001876#ifdef FEAT_EVAL
1877/*
1878 * Set the completion context for the :unlet command. Always returns NULL.
1879 */
1880 static char_u *
1881set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1882{
1883 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1884 arg = xp->xp_pattern + 1;
1885
1886 xp->xp_context = EXPAND_USER_VARS;
1887 xp->xp_pattern = arg;
1888
1889 if (*xp->xp_pattern == '$')
1890 {
1891 xp->xp_context = EXPAND_ENV_VARS;
1892 ++xp->xp_pattern;
1893 }
1894
1895 return NULL;
1896}
1897#endif
1898
1899#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1900/*
1901 * Set the completion context for the :language command. Always returns NULL.
1902 */
1903 static char_u *
1904set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1905{
1906 char_u *p;
1907
1908 p = skiptowhite(arg);
1909 if (*p == NUL)
1910 {
1911 xp->xp_context = EXPAND_LANGUAGE;
1912 xp->xp_pattern = arg;
1913 }
1914 else
1915 {
1916 if ( STRNCMP(arg, "messages", p - arg) == 0
1917 || STRNCMP(arg, "ctype", p - arg) == 0
1918 || STRNCMP(arg, "time", p - arg) == 0
1919 || STRNCMP(arg, "collate", p - arg) == 0)
1920 {
1921 xp->xp_context = EXPAND_LOCALES;
1922 xp->xp_pattern = skipwhite(p);
1923 }
1924 else
1925 xp->xp_context = EXPAND_NOTHING;
1926 }
1927
1928 return NULL;
1929}
1930#endif
1931
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001932#ifdef FEAT_EVAL
1933static enum
1934{
1935 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001936 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1937 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001938} breakpt_expand_what;
1939
1940/*
1941 * Set the completion context for the :breakadd command. Always returns NULL.
1942 */
1943 static char_u *
1944set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1945{
1946 char_u *p;
1947 char_u *subcmd_start;
1948
1949 xp->xp_context = EXPAND_BREAKPOINT;
1950 xp->xp_pattern = arg;
1951
1952 if (cmdidx == CMD_breakadd)
1953 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001954 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001955 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001956 else
1957 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001958
1959 p = skipwhite(arg);
1960 if (*p == NUL)
1961 return NULL;
1962 subcmd_start = p;
1963
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001964 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001965 {
1966 // :breakadd file [lnum] <filename>
1967 // :breakadd func [lnum] <funcname>
1968 p += 4;
1969 p = skipwhite(p);
1970
1971 // skip line number (if specified)
1972 if (VIM_ISDIGIT(*p))
1973 {
1974 p = skipdigits(p);
1975 if (*p != ' ')
1976 {
1977 xp->xp_context = EXPAND_NOTHING;
1978 return NULL;
1979 }
1980 p = skipwhite(p);
1981 }
1982 if (STRNCMP("file", subcmd_start, 4) == 0)
1983 xp->xp_context = EXPAND_FILES;
1984 else
1985 xp->xp_context = EXPAND_USER_FUNC;
1986 xp->xp_pattern = p;
1987 }
1988 else if (STRNCMP("expr ", p, 5) == 0)
1989 {
1990 // :breakadd expr <expression>
1991 xp->xp_context = EXPAND_EXPRESSION;
1992 xp->xp_pattern = skipwhite(p + 5);
1993 }
1994
1995 return NULL;
1996}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001997
1998 static char_u *
1999set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2000{
2001 char_u *p;
2002
2003 xp->xp_context = EXPAND_NOTHING;
2004 xp->xp_pattern = NULL;
2005
2006 p = skipwhite(arg);
2007 if (VIM_ISDIGIT(*p))
2008 return NULL;
2009
2010 xp->xp_context = EXPAND_SCRIPTNAMES;
2011 xp->xp_pattern = p;
2012
2013 return NULL;
2014}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002015#endif
2016
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002017/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002018 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2019 * The argument to the command is 'arg' and the argument flags is 'argt'.
2020 * For user-defined commands and for environment variables, 'compl' has the
2021 * completion type.
2022 * Returns a pointer to the next command. Returns NULL if there is no next
2023 * command.
2024 */
2025 static char_u *
2026set_context_by_cmdname(
2027 char_u *cmd,
2028 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002029 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002030 char_u *arg,
2031 long argt,
2032 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002033 int forceit)
2034{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002035 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002036 {
2037 case CMD_find:
2038 case CMD_sfind:
2039 case CMD_tabfind:
2040 if (xp->xp_context == EXPAND_FILES)
2041 xp->xp_context = EXPAND_FILES_IN_PATH;
2042 break;
2043 case CMD_cd:
2044 case CMD_chdir:
2045 case CMD_tcd:
2046 case CMD_tchdir:
2047 case CMD_lcd:
2048 case CMD_lchdir:
2049 if (xp->xp_context == EXPAND_FILES)
2050 xp->xp_context = EXPAND_DIRECTORIES;
2051 break;
2052 case CMD_help:
2053 xp->xp_context = EXPAND_HELP;
2054 xp->xp_pattern = arg;
2055 break;
2056
2057 // Command modifiers: return the argument.
2058 // Also for commands with an argument that is a command.
2059 case CMD_aboveleft:
2060 case CMD_argdo:
2061 case CMD_belowright:
2062 case CMD_botright:
2063 case CMD_browse:
2064 case CMD_bufdo:
2065 case CMD_cdo:
2066 case CMD_cfdo:
2067 case CMD_confirm:
2068 case CMD_debug:
2069 case CMD_folddoclosed:
2070 case CMD_folddoopen:
2071 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002072 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002073 case CMD_keepalt:
2074 case CMD_keepjumps:
2075 case CMD_keepmarks:
2076 case CMD_keeppatterns:
2077 case CMD_ldo:
2078 case CMD_leftabove:
2079 case CMD_lfdo:
2080 case CMD_lockmarks:
2081 case CMD_noautocmd:
2082 case CMD_noswapfile:
2083 case CMD_rightbelow:
2084 case CMD_sandbox:
2085 case CMD_silent:
2086 case CMD_tab:
2087 case CMD_tabdo:
2088 case CMD_topleft:
2089 case CMD_verbose:
2090 case CMD_vertical:
2091 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002092 case CMD_vim9cmd:
2093 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002094 return arg;
2095
2096 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002097 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002098
2099#ifdef FEAT_SEARCH_EXTRA
2100 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002101 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002102#endif
2103
2104 // All completion for the +cmdline_compl feature goes here.
2105
2106 case CMD_command:
2107 return set_context_in_user_cmd(xp, arg);
2108
2109 case CMD_delcommand:
2110 xp->xp_context = EXPAND_USER_COMMANDS;
2111 xp->xp_pattern = arg;
2112 break;
2113
2114 case CMD_global:
2115 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002116 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002117 case CMD_and:
2118 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002119 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002120 case CMD_isearch:
2121 case CMD_dsearch:
2122 case CMD_ilist:
2123 case CMD_dlist:
2124 case CMD_ijump:
2125 case CMD_psearch:
2126 case CMD_djump:
2127 case CMD_isplit:
2128 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002129 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002130 case CMD_autocmd:
2131 return set_context_in_autocmd(xp, arg, FALSE);
2132 case CMD_doautocmd:
2133 case CMD_doautoall:
2134 return set_context_in_autocmd(xp, arg, TRUE);
2135 case CMD_set:
2136 set_context_in_set_cmd(xp, arg, 0);
2137 break;
2138 case CMD_setglobal:
2139 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2140 break;
2141 case CMD_setlocal:
2142 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2143 break;
2144 case CMD_tag:
2145 case CMD_stag:
2146 case CMD_ptag:
2147 case CMD_ltag:
2148 case CMD_tselect:
2149 case CMD_stselect:
2150 case CMD_ptselect:
2151 case CMD_tjump:
2152 case CMD_stjump:
2153 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002154 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002155 xp->xp_context = EXPAND_TAGS_LISTFILES;
2156 else
2157 xp->xp_context = EXPAND_TAGS;
2158 xp->xp_pattern = arg;
2159 break;
2160 case CMD_augroup:
2161 xp->xp_context = EXPAND_AUGROUP;
2162 xp->xp_pattern = arg;
2163 break;
2164#ifdef FEAT_SYN_HL
2165 case CMD_syntax:
2166 set_context_in_syntax_cmd(xp, arg);
2167 break;
2168#endif
2169#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002170 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002171 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002172 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002173 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002174 case CMD_if:
2175 case CMD_elseif:
2176 case CMD_while:
2177 case CMD_for:
2178 case CMD_echo:
2179 case CMD_echon:
2180 case CMD_execute:
2181 case CMD_echomsg:
2182 case CMD_echoerr:
2183 case CMD_call:
2184 case CMD_return:
2185 case CMD_cexpr:
2186 case CMD_caddexpr:
2187 case CMD_cgetexpr:
2188 case CMD_lexpr:
2189 case CMD_laddexpr:
2190 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002191 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002192 break;
2193
2194 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002195 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002196 case CMD_function:
2197 case CMD_delfunction:
2198 xp->xp_context = EXPAND_USER_FUNC;
2199 xp->xp_pattern = arg;
2200 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002201 case CMD_disassemble:
2202 set_context_in_disassemble_cmd(xp, arg);
2203 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002204
2205 case CMD_echohl:
2206 set_context_in_echohl_cmd(xp, arg);
2207 break;
2208#endif
2209 case CMD_highlight:
2210 set_context_in_highlight_cmd(xp, arg);
2211 break;
2212#ifdef FEAT_CSCOPE
2213 case CMD_cscope:
2214 case CMD_lcscope:
2215 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002216 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002217 break;
2218#endif
2219#ifdef FEAT_SIGNS
2220 case CMD_sign:
2221 set_context_in_sign_cmd(xp, arg);
2222 break;
2223#endif
2224 case CMD_bdelete:
2225 case CMD_bwipeout:
2226 case CMD_bunload:
2227 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2228 arg = xp->xp_pattern + 1;
2229 // FALLTHROUGH
2230 case CMD_buffer:
2231 case CMD_sbuffer:
2232 case CMD_checktime:
2233 xp->xp_context = EXPAND_BUFFERS;
2234 xp->xp_pattern = arg;
2235 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002236#ifdef FEAT_DIFF
2237 case CMD_diffget:
2238 case CMD_diffput:
2239 // If current buffer is in diff mode, complete buffer names
2240 // which are in diff mode, and different than current buffer.
2241 xp->xp_context = EXPAND_DIFF_BUFFERS;
2242 xp->xp_pattern = arg;
2243 break;
2244#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002245 case CMD_USER:
2246 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002247 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2248 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002249
2250 case CMD_map: case CMD_noremap:
2251 case CMD_nmap: case CMD_nnoremap:
2252 case CMD_vmap: case CMD_vnoremap:
2253 case CMD_omap: case CMD_onoremap:
2254 case CMD_imap: case CMD_inoremap:
2255 case CMD_cmap: case CMD_cnoremap:
2256 case CMD_lmap: case CMD_lnoremap:
2257 case CMD_smap: case CMD_snoremap:
2258 case CMD_tmap: case CMD_tnoremap:
2259 case CMD_xmap: case CMD_xnoremap:
2260 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002261 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002262 case CMD_unmap:
2263 case CMD_nunmap:
2264 case CMD_vunmap:
2265 case CMD_ounmap:
2266 case CMD_iunmap:
2267 case CMD_cunmap:
2268 case CMD_lunmap:
2269 case CMD_sunmap:
2270 case CMD_tunmap:
2271 case CMD_xunmap:
2272 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002273 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002274 case CMD_mapclear:
2275 case CMD_nmapclear:
2276 case CMD_vmapclear:
2277 case CMD_omapclear:
2278 case CMD_imapclear:
2279 case CMD_cmapclear:
2280 case CMD_lmapclear:
2281 case CMD_smapclear:
2282 case CMD_tmapclear:
2283 case CMD_xmapclear:
2284 xp->xp_context = EXPAND_MAPCLEAR;
2285 xp->xp_pattern = arg;
2286 break;
2287
2288 case CMD_abbreviate: case CMD_noreabbrev:
2289 case CMD_cabbrev: case CMD_cnoreabbrev:
2290 case CMD_iabbrev: case CMD_inoreabbrev:
2291 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002292 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002293 case CMD_unabbreviate:
2294 case CMD_cunabbrev:
2295 case CMD_iunabbrev:
2296 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002297 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002298#ifdef FEAT_MENU
2299 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2300 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2301 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2302 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2303 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2304 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2305 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2306 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2307 case CMD_tmenu: case CMD_tunmenu:
2308 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2309 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2310#endif
2311
2312 case CMD_colorscheme:
2313 xp->xp_context = EXPAND_COLORS;
2314 xp->xp_pattern = arg;
2315 break;
2316
roota6759382023-01-21 21:56:06 +00002317 case CMD_runtime:
zeertzjq3770f4c2023-01-22 18:38:51 +00002318 set_context_in_runtime_cmd(xp, arg);
roota6759382023-01-21 21:56:06 +00002319 break;
2320
Bram Moolenaard0190392019-08-23 21:17:35 +02002321 case CMD_compiler:
2322 xp->xp_context = EXPAND_COMPILER;
2323 xp->xp_pattern = arg;
2324 break;
2325
2326 case CMD_ownsyntax:
2327 xp->xp_context = EXPAND_OWNSYNTAX;
2328 xp->xp_pattern = arg;
2329 break;
2330
2331 case CMD_setfiletype:
2332 xp->xp_context = EXPAND_FILETYPE;
2333 xp->xp_pattern = arg;
2334 break;
2335
2336 case CMD_packadd:
2337 xp->xp_context = EXPAND_PACKADD;
2338 xp->xp_pattern = arg;
2339 break;
2340
2341#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2342 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002343 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002344#endif
2345#if defined(FEAT_PROFILE)
2346 case CMD_profile:
2347 set_context_in_profile_cmd(xp, arg);
2348 break;
2349#endif
2350 case CMD_behave:
2351 xp->xp_context = EXPAND_BEHAVE;
2352 xp->xp_pattern = arg;
2353 break;
2354
2355 case CMD_messages:
2356 xp->xp_context = EXPAND_MESSAGES;
2357 xp->xp_pattern = arg;
2358 break;
2359
2360 case CMD_history:
2361 xp->xp_context = EXPAND_HISTORY;
2362 xp->xp_pattern = arg;
2363 break;
2364#if defined(FEAT_PROFILE)
2365 case CMD_syntime:
2366 xp->xp_context = EXPAND_SYNTIME;
2367 xp->xp_pattern = arg;
2368 break;
2369#endif
2370
2371 case CMD_argdelete:
2372 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2373 arg = xp->xp_pattern + 1;
2374 xp->xp_context = EXPAND_ARGLIST;
2375 xp->xp_pattern = arg;
2376 break;
2377
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002378#ifdef FEAT_EVAL
2379 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002380 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002381 case CMD_breakdel:
2382 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002383
2384 case CMD_scriptnames:
2385 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002386#endif
2387
Bram Moolenaard0190392019-08-23 21:17:35 +02002388 default:
2389 break;
2390 }
2391 return NULL;
2392}
2393
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002394/*
2395 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2396 * we don't need/want deleted. Maybe this could be done better if we didn't
2397 * repeat all this stuff. The only problem is that they may not stay
2398 * perfectly compatible with each other, but then the command line syntax
2399 * probably won't change that much -- webb.
2400 */
2401 static char_u *
2402set_one_cmd_context(
2403 expand_T *xp,
2404 char_u *buff) // buffer for command string
2405{
2406 char_u *p;
2407 char_u *cmd, *arg;
2408 int len = 0;
2409 exarg_T ea;
2410 int compl = EXPAND_NOTHING;
2411 int forceit = FALSE;
2412 int usefilter = FALSE; // filter instead of file name
2413
2414 ExpandInit(xp);
2415 xp->xp_pattern = buff;
2416 xp->xp_line = buff;
2417 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2418 ea.argt = 0;
2419
2420 // 1. skip comment lines and leading space, colons or bars
2421 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2422 ;
2423 xp->xp_pattern = cmd;
2424
2425 if (*cmd == NUL)
2426 return NULL;
2427 if (*cmd == '"') // ignore comment lines
2428 {
2429 xp->xp_context = EXPAND_NOTHING;
2430 return NULL;
2431 }
2432
2433 // 3. Skip over the range to find the command.
2434 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2435 xp->xp_pattern = cmd;
2436 if (*cmd == NUL)
2437 return NULL;
2438 if (*cmd == '"')
2439 {
2440 xp->xp_context = EXPAND_NOTHING;
2441 return NULL;
2442 }
2443
2444 if (*cmd == '|' || *cmd == '\n')
2445 return cmd + 1; // There's another command
2446
2447 // Get the command index.
2448 p = set_cmd_index(cmd, &ea, xp, &compl);
2449 if (p == NULL)
2450 return NULL;
2451
2452 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2453
2454 if (*p == '!') // forced commands
2455 {
2456 forceit = TRUE;
2457 ++p;
2458 }
2459
2460 // 6. parse arguments
2461 if (!IS_USER_CMDIDX(ea.cmdidx))
2462 ea.argt = excmd_get_argt(ea.cmdidx);
2463
2464 arg = skipwhite(p);
2465
2466 // Skip over ++argopt argument
2467 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2468 {
2469 p = arg;
2470 while (*p && !vim_isspace(*p))
2471 MB_PTR_ADV(p);
2472 arg = skipwhite(p);
2473 }
2474
2475 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2476 {
2477 if (*arg == '>') // append
2478 {
2479 if (*++arg == '>')
2480 ++arg;
2481 arg = skipwhite(arg);
2482 }
2483 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2484 {
2485 ++arg;
2486 usefilter = TRUE;
2487 }
2488 }
2489
2490 if (ea.cmdidx == CMD_read)
2491 {
2492 usefilter = forceit; // :r! filter if forced
2493 if (*arg == '!') // :r !filter
2494 {
2495 ++arg;
2496 usefilter = TRUE;
2497 }
2498 }
2499
2500 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2501 {
2502 while (*arg == *cmd) // allow any number of '>' or '<'
2503 ++arg;
2504 arg = skipwhite(arg);
2505 }
2506
2507 // Does command allow "+command"?
2508 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2509 {
2510 // Check if we're in the +command
2511 p = arg + 1;
2512 arg = skip_cmd_arg(arg, FALSE);
2513
2514 // Still touching the command after '+'?
2515 if (*arg == NUL)
2516 return p;
2517
2518 // Skip space(s) after +command to get to the real argument
2519 arg = skipwhite(arg);
2520 }
2521
2522
2523 // Check for '|' to separate commands and '"' to start comments.
2524 // Don't do this for ":read !cmd" and ":write !cmd".
2525 if ((ea.argt & EX_TRLBAR) && !usefilter)
2526 {
2527 p = arg;
2528 // ":redir @" is not the start of a comment
2529 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2530 p += 2;
2531 while (*p)
2532 {
2533 if (*p == Ctrl_V)
2534 {
2535 if (p[1] != NUL)
2536 ++p;
2537 }
2538 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2539 || *p == '|' || *p == '\n')
2540 {
2541 if (*(p - 1) != '\\')
2542 {
2543 if (*p == '|' || *p == '\n')
2544 return p + 1;
2545 return NULL; // It's a comment
2546 }
2547 }
2548 MB_PTR_ADV(p);
2549 }
2550 }
2551
2552 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2553 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2554 // no arguments allowed but there is something
2555 return NULL;
2556
2557 // Find start of last argument (argument just before cursor):
2558 p = buff;
2559 xp->xp_pattern = p;
2560 len = (int)STRLEN(buff);
2561 while (*p && p < buff + len)
2562 {
2563 if (*p == ' ' || *p == TAB)
2564 {
2565 // argument starts after a space
2566 xp->xp_pattern = ++p;
2567 }
2568 else
2569 {
2570 if (*p == '\\' && *(p + 1) != NUL)
2571 ++p; // skip over escaped character
2572 MB_PTR_ADV(p);
2573 }
2574 }
2575
2576 if (ea.argt & EX_XFILE)
2577 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2578
2579 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002580 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002581 forceit);
2582}
2583
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002584/*
2585 * Set the completion context in 'xp' for command 'str'
2586 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002587 void
2588set_cmd_context(
2589 expand_T *xp,
2590 char_u *str, // start of command line
2591 int len, // length of command line (excl. NUL)
2592 int col, // position of cursor
2593 int use_ccline UNUSED) // use ccline for info
2594{
2595#ifdef FEAT_EVAL
2596 cmdline_info_T *ccline = get_cmdline_info();
2597#endif
2598 int old_char = NUL;
2599 char_u *nextcomm;
2600
2601 // Avoid a UMR warning from Purify, only save the character if it has been
2602 // written before.
2603 if (col < len)
2604 old_char = str[col];
2605 str[col] = NUL;
2606 nextcomm = str;
2607
2608#ifdef FEAT_EVAL
2609 if (use_ccline && ccline->cmdfirstc == '=')
2610 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002611 // pass CMD_SIZE because there is no real command
2612 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002613 }
2614 else if (use_ccline && ccline->input_fn)
2615 {
2616 xp->xp_context = ccline->xp_context;
2617 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002618 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002619 }
2620 else
2621#endif
2622 while (nextcomm != NULL)
2623 nextcomm = set_one_cmd_context(xp, nextcomm);
2624
2625 // Store the string here so that call_user_expand_func() can get to them
2626 // easily.
2627 xp->xp_line = str;
2628 xp->xp_col = col;
2629
2630 str[col] = old_char;
2631}
2632
2633/*
2634 * Expand the command line "str" from context "xp".
2635 * "xp" must have been set by set_cmd_context().
2636 * xp->xp_pattern points into "str", to where the text that is to be expanded
2637 * starts.
2638 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2639 * cursor.
2640 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2641 * key that triggered expansion literally.
2642 * Returns EXPAND_OK otherwise.
2643 */
2644 int
2645expand_cmdline(
2646 expand_T *xp,
2647 char_u *str, // start of command line
2648 int col, // position of cursor
2649 int *matchcount, // return: nr of matches
2650 char_u ***matches) // return: array of pointers to matches
2651{
2652 char_u *file_str = NULL;
2653 int options = WILD_ADD_SLASH|WILD_SILENT;
2654
2655 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2656 {
2657 beep_flush();
2658 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2659 }
2660 if (xp->xp_context == EXPAND_NOTHING)
2661 {
2662 // Caller can use the character as a normal char instead
2663 return EXPAND_NOTHING;
2664 }
2665
2666 // add star to file name, or convert to regexp if not exp. files.
2667 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002668 if (cmdline_fuzzy_completion_supported(xp))
2669 // If fuzzy matching, don't modify the search string
2670 file_str = vim_strsave(xp->xp_pattern);
2671 else
2672 {
2673 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2674 if (file_str == NULL)
2675 return EXPAND_UNSUCCESSFUL;
2676 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002677
2678 if (p_wic)
2679 options += WILD_ICASE;
2680
2681 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002682 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002683 {
2684 *matchcount = 0;
2685 *matches = NULL;
2686 }
2687 vim_free(file_str);
2688
2689 return EXPAND_OK;
2690}
2691
Bram Moolenaar66b51422019-08-18 21:44:12 +02002692/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002693 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002694 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002695 */
2696 static int
2697expand_files_and_dirs(
2698 expand_T *xp,
2699 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002700 char_u ***matches,
2701 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002702 int flags,
2703 int options)
2704{
2705 int free_pat = FALSE;
2706 int i;
2707 int ret;
2708
2709 // for ":set path=" and ":set tags=" halve backslashes for escaped
2710 // space
2711 if (xp->xp_backslash != XP_BS_NONE)
2712 {
2713 free_pat = TRUE;
2714 pat = vim_strsave(pat);
2715 for (i = 0; pat[i]; ++i)
2716 if (pat[i] == '\\')
2717 {
2718 if (xp->xp_backslash == XP_BS_THREE
2719 && pat[i + 1] == '\\'
2720 && pat[i + 2] == '\\'
2721 && pat[i + 3] == ' ')
2722 STRMOVE(pat + i, pat + i + 3);
2723 if (xp->xp_backslash == XP_BS_ONE
2724 && pat[i + 1] == ' ')
2725 STRMOVE(pat + i, pat + i + 1);
2726 }
2727 }
2728
2729 if (xp->xp_context == EXPAND_FILES)
2730 flags |= EW_FILE;
2731 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2732 flags |= (EW_FILE | EW_PATH);
2733 else
2734 flags = (flags | EW_DIR) & ~EW_FILE;
2735 if (options & WILD_ICASE)
2736 flags |= EW_ICASE;
2737
2738 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002739 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002740 if (free_pat)
2741 vim_free(pat);
2742#ifdef BACKSLASH_IN_FILENAME
2743 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2744 {
2745 int j;
2746
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002747 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002748 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002749 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002750
2751 while (*ptr != NUL)
2752 {
2753 if (p_csl[0] == 's' && *ptr == '\\')
2754 *ptr = '/';
2755 else if (p_csl[0] == 'b' && *ptr == '/')
2756 *ptr = '\\';
2757 ptr += (*mb_ptr2len)(ptr);
2758 }
2759 }
2760 }
2761#endif
2762 return ret;
2763}
2764
2765/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002766 * Function given to ExpandGeneric() to obtain the possible arguments of the
2767 * ":behave {mswin,xterm}" command.
2768 */
2769 static char_u *
2770get_behave_arg(expand_T *xp UNUSED, int idx)
2771{
2772 if (idx == 0)
2773 return (char_u *)"mswin";
2774 if (idx == 1)
2775 return (char_u *)"xterm";
2776 return NULL;
2777}
2778
K.Takata161b6ac2022-11-14 15:31:07 +00002779#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002780/*
2781 * Function given to ExpandGeneric() to obtain the possible arguments of the
2782 * ":breakadd {expr, file, func, here}" command.
2783 * ":breakdel {func, file, here}" command.
2784 */
2785 static char_u *
2786get_breakadd_arg(expand_T *xp UNUSED, int idx)
2787{
2788 char *opts[] = {"expr", "file", "func", "here"};
2789
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002790 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002791 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002792 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002793 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2794 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002795 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2796 {
2797 // breakdel {func, file, here}
2798 if (idx <= 2)
2799 return (char_u *)opts[idx + 1];
2800 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002801 else
2802 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002803 // profdel {func, file}
2804 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002805 return (char_u *)opts[idx + 1];
2806 }
2807 }
2808 return NULL;
2809}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002810
2811/*
2812 * Function given to ExpandGeneric() to obtain the possible arguments for the
2813 * ":scriptnames" command.
2814 */
2815 static char_u *
2816get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2817{
2818 scriptitem_T *si;
2819
2820 if (!SCRIPT_ID_VALID(idx + 1))
2821 return NULL;
2822
2823 si = SCRIPT_ITEM(idx + 1);
2824 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2825 return NameBuff;
2826}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002827#endif
2828
Bram Moolenaard0190392019-08-23 21:17:35 +02002829/*
2830 * Function given to ExpandGeneric() to obtain the possible arguments of the
2831 * ":messages {clear}" command.
2832 */
2833 static char_u *
2834get_messages_arg(expand_T *xp UNUSED, int idx)
2835{
2836 if (idx == 0)
2837 return (char_u *)"clear";
2838 return NULL;
2839}
2840
2841 static char_u *
2842get_mapclear_arg(expand_T *xp UNUSED, int idx)
2843{
2844 if (idx == 0)
2845 return (char_u *)"<buffer>";
2846 return NULL;
2847}
2848
2849/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002850 * Do the expansion based on xp->xp_context and 'rmp'.
2851 */
2852 static int
2853ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002854 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002855 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002856 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002857 char_u ***matches,
2858 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002859{
2860 static struct expgen
2861 {
2862 int context;
2863 char_u *((*func)(expand_T *, int));
2864 int ic;
2865 int escaped;
2866 } tab[] =
2867 {
2868 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2869 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2870 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2871 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2872 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2873 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2874 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2875 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2876 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2877 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002878#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002879 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2880 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2881 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2882 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2883 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002884#endif
2885#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002886 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2887 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002888#endif
2889#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002890 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002891#endif
2892#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002893 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002894#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002895 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2896 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2897 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002898#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002899 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002900#endif
2901#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002902 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002903#endif
2904#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002905 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002906#endif
2907#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002908 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2909 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002910#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002911 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2912 {EXPAND_USER, get_users, TRUE, FALSE},
2913 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002914#ifdef FEAT_EVAL
2915 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002916 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002917#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002918 };
2919 int i;
2920 int ret = FAIL;
2921
2922 // Find a context in the table and call the ExpandGeneric() with the
2923 // right function to do the expansion.
2924 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2925 {
2926 if (xp->xp_context == tab[i].context)
2927 {
2928 if (tab[i].ic)
2929 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002930 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2931 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002932 break;
2933 }
2934 }
2935
2936 return ret;
2937}
2938
2939/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002940 * Map wild expand options to flags for expand_wildcards()
2941 */
2942 static int
2943map_wildopts_to_ewflags(int options)
2944{
2945 int flags;
2946
2947 flags = EW_DIR; // include directories
2948 if (options & WILD_LIST_NOTFOUND)
2949 flags |= EW_NOTFOUND;
2950 if (options & WILD_ADD_SLASH)
2951 flags |= EW_ADDSLASH;
2952 if (options & WILD_KEEP_ALL)
2953 flags |= EW_KEEPALL;
2954 if (options & WILD_SILENT)
2955 flags |= EW_SILENT;
2956 if (options & WILD_NOERROR)
2957 flags |= EW_NOERROR;
2958 if (options & WILD_ALLLINKS)
2959 flags |= EW_ALLLINKS;
2960
2961 return flags;
2962}
2963
2964/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002965 * Do the expansion based on xp->xp_context and "pat".
2966 */
2967 static int
2968ExpandFromContext(
2969 expand_T *xp,
2970 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002971 char_u ***matches,
2972 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002973 int options) // WILD_ flags
2974{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002975 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002976 int ret;
2977 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002978 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002979 int fuzzy = cmdline_fuzzy_complete(pat)
2980 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002981
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002982 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002983
2984 if (xp->xp_context == EXPAND_FILES
2985 || xp->xp_context == EXPAND_DIRECTORIES
2986 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002987 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2988 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002989
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002990 *matches = (char_u **)"";
2991 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002992 if (xp->xp_context == EXPAND_HELP)
2993 {
2994 // With an empty argument we would get all the help tags, which is
2995 // very slow. Get matches for "help" instead.
2996 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002997 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002998 {
2999#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003000 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003001#endif
3002 return OK;
3003 }
3004 return FAIL;
3005 }
3006
Bram Moolenaar66b51422019-08-18 21:44:12 +02003007 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003008 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003009 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003010 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003011 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003012 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003013#ifdef FEAT_DIFF
3014 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003015 return ExpandBufnames(pat, numMatches, matches,
3016 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003017#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003018 if (xp->xp_context == EXPAND_TAGS
3019 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003020 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3021 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003022 if (xp->xp_context == EXPAND_COLORS)
3023 {
3024 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003025 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003026 directories);
3027 }
3028 if (xp->xp_context == EXPAND_COMPILER)
3029 {
3030 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003031 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003032 }
3033 if (xp->xp_context == EXPAND_OWNSYNTAX)
3034 {
3035 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003036 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003037 }
3038 if (xp->xp_context == EXPAND_FILETYPE)
3039 {
3040 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003041 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003042 }
K.Takata161b6ac2022-11-14 15:31:07 +00003043#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003044 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003045 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003046#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003047 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003048 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003049 if (xp->xp_context == EXPAND_RUNTIME)
3050 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003051
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003052 // When expanding a function name starting with s:, match the <SNR>nr_
3053 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003054 if ((xp->xp_context == EXPAND_USER_FUNC
3055 || xp->xp_context == EXPAND_DISASSEMBLE)
3056 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003057 {
3058 int len = (int)STRLEN(pat) + 20;
3059
3060 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003061 if (tofree == NULL)
3062 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003063 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003064 pat = tofree;
3065 }
3066
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003067 if (!fuzzy)
3068 {
3069 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3070 if (regmatch.regprog == NULL)
3071 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003072
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003073 // set ignore-case according to p_ic, p_scs and pat
3074 regmatch.rm_ic = ignorecase(pat);
3075 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003076
3077 if (xp->xp_context == EXPAND_SETTINGS
3078 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003079 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003080 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003081 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003082#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003083 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003084 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003085#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003086 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003087 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003088
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003089 if (!fuzzy)
3090 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003091 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003092
3093 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003094}
3095
Bram Moolenaar66b51422019-08-18 21:44:12 +02003096/*
3097 * Expand a list of names.
3098 *
3099 * Generic function for command line completion. It calls a function to
3100 * obtain strings, one by one. The strings are matched against a regexp
3101 * program. Matching strings are copied into an array, which is returned.
3102 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003103 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3104 * is used.
3105 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003106 * Returns OK when no problems encountered, FAIL for error (out of memory).
3107 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003108 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003109ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003110 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111 expand_T *xp,
3112 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003113 char_u ***matches,
3114 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115 char_u *((*func)(expand_T *, int)),
3116 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003117 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003118{
3119 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003120 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003121 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003122 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003123 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003124 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003125 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003126 int sort_matches = FALSE;
3127 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003129 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003130 *matches = NULL;
3131 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003132
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003133 if (!fuzzy)
3134 ga_init2(&ga, sizeof(char *), 30);
3135 else
3136 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3137
3138 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003140 str = (*func)(xp, i);
3141 if (str == NULL) // end of list
3142 break;
3143 if (*str == NUL) // skip empty strings
3144 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003145
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003146 if (xp->xp_pattern[0] != NUL)
3147 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003148 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003149 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003150 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003151 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003152 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003153 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003154 }
3155 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003156 else
3157 match = TRUE;
3158
3159 if (!match)
3160 continue;
3161
3162 if (escaped)
3163 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3164 else
3165 str = vim_strsave(str);
3166 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003167 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003168 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003169 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003170 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003171 return FAIL;
3172 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003173 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003174 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003175 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003176
3177 if (ga_grow(&ga, 1) == FAIL)
3178 {
3179 vim_free(str);
3180 break;
3181 }
3182
3183 if (fuzzy)
3184 {
3185 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3186 fuzmatch->idx = ga.ga_len;
3187 fuzmatch->str = str;
3188 fuzmatch->score = score;
3189 }
3190 else
3191 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3192
K.Takata161b6ac2022-11-14 15:31:07 +00003193#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003194 if (func == get_menu_names)
3195 {
3196 // test for separator added by get_menu_names()
3197 str += STRLEN(str) - 1;
3198 if (*str == '\001')
3199 *str = '.';
3200 }
K.Takata161b6ac2022-11-14 15:31:07 +00003201#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003202
3203 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003204 }
3205
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003206 if (ga.ga_len == 0)
3207 return OK;
3208
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003209 // sort the matches when using regular expression matching and sorting
3210 // applies to the completion context. Menus and scriptnames should be kept
3211 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003212 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003213 && xp->xp_context != EXPAND_MENUS
3214 && xp->xp_context != EXPAND_SCRIPTNAMES)
3215 sort_matches = TRUE;
3216
3217 // <SNR> functions should be sorted to the end.
3218 if (xp->xp_context == EXPAND_EXPRESSION
3219 || xp->xp_context == EXPAND_FUNCTIONS
3220 || xp->xp_context == EXPAND_USER_FUNC
3221 || xp->xp_context == EXPAND_DISASSEMBLE)
3222 funcsort = TRUE;
3223
3224 // Sort the matches.
3225 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003226 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003227 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003228 // <SNR> functions should be sorted to the end.
3229 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3230 sort_func_compare);
3231 else
3232 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3233 }
3234
3235 if (!fuzzy)
3236 {
3237 *matches = ga.ga_data;
3238 *numMatches = ga.ga_len;
3239 }
3240 else
3241 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003242 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3243 funcsort) == FAIL)
3244 return FAIL;
3245 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246 }
3247
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003248#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003249 // Reset the variables used for special highlight names expansion, so that
3250 // they don't show up when getting normal highlight names by ID.
3251 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003252#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003253
Bram Moolenaar66b51422019-08-18 21:44:12 +02003254 return OK;
3255}
3256
3257/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003258 * Expand shell command matches in one directory of $PATH.
3259 */
3260 static void
3261expand_shellcmd_onedir(
3262 char_u *buf,
3263 char_u *s,
3264 size_t l,
3265 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003266 char_u ***matches,
3267 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003268 int flags,
3269 hashtab_T *ht,
3270 garray_T *gap)
3271{
3272 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003273 hash_T hash;
3274 hashitem_T *hi;
3275
3276 vim_strncpy(buf, s, l);
3277 add_pathsep(buf);
3278 l = STRLEN(buf);
3279 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3280
3281 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003282 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003283 if (ret != OK)
3284 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003285
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003286 if (ga_grow(gap, *numMatches) == FAIL)
3287 {
3288 FreeWild(*numMatches, *matches);
3289 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003290 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003291
3292 for (int i = 0; i < *numMatches; ++i)
3293 {
3294 char_u *name = (*matches)[i];
3295
3296 if (STRLEN(name) > l)
3297 {
3298 // Check if this name was already found.
3299 hash = hash_hash(name + l);
3300 hi = hash_lookup(ht, name + l, hash);
3301 if (HASHITEM_EMPTY(hi))
3302 {
3303 // Remove the path that was prepended.
3304 STRMOVE(name, name + l);
3305 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3306 hash_add_item(ht, hi, name, hash);
3307 name = NULL;
3308 }
3309 }
3310 vim_free(name);
3311 }
3312 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003313}
3314
3315/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003316 * Complete a shell command.
3317 * Returns FAIL or OK;
3318 */
3319 static int
3320expand_shellcmd(
3321 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003322 char_u ***matches, // return: array with matches
3323 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003324 int flagsarg) // EW_ flags
3325{
3326 char_u *pat;
3327 int i;
3328 char_u *path = NULL;
3329 int mustfree = FALSE;
3330 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003331 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003332 size_t l;
3333 char_u *s, *e;
3334 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003335 int did_curdir = FALSE;
3336 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003337
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003338 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003339 if (buf == NULL)
3340 return FAIL;
3341
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003342 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003343 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003344 if (pat == NULL)
3345 {
3346 vim_free(buf);
3347 return FAIL;
3348 }
3349
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 for (i = 0; pat[i]; ++i)
3351 if (pat[i] == '\\' && pat[i + 1] == ' ')
3352 STRMOVE(pat + i, pat + i + 1);
3353
3354 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3355
3356 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3357 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3358 path = (char_u *)".";
3359 else
3360 {
3361 // For an absolute name we don't use $PATH.
3362 if (!mch_isFullName(pat))
3363 path = vim_getenv((char_u *)"PATH", &mustfree);
3364 if (path == NULL)
3365 path = (char_u *)"";
3366 }
3367
3368 // Go over all directories in $PATH. Expand matches in that directory and
3369 // collect them in "ga". When "." is not in $PATH also expand for the
3370 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003371 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003372 hash_init(&found_ht);
3373 for (s = path; ; s = e)
3374 {
K.Takata161b6ac2022-11-14 15:31:07 +00003375#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003376 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003377#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003378 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003379#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003380 if (e == NULL)
3381 e = s + STRLEN(s);
3382
3383 if (*s == NUL)
3384 {
3385 if (did_curdir)
3386 break;
3387 // Find directories in the current directory, path is empty.
3388 did_curdir = TRUE;
3389 flags |= EW_DIR;
3390 }
3391 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3392 {
3393 did_curdir = TRUE;
3394 flags |= EW_DIR;
3395 }
3396 else
3397 // Do not match directories inside a $PATH item.
3398 flags &= ~EW_DIR;
3399
3400 l = e - s;
3401 if (l > MAXPATHL - 5)
3402 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003403
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003404 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003405 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003406
Bram Moolenaar66b51422019-08-18 21:44:12 +02003407 if (*e != NUL)
3408 ++e;
3409 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003410 *matches = ga.ga_data;
3411 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003412
3413 vim_free(buf);
3414 vim_free(pat);
3415 if (mustfree)
3416 vim_free(path);
3417 hash_clear(&found_ht);
3418 return OK;
3419}
3420
K.Takata161b6ac2022-11-14 15:31:07 +00003421#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003422/*
3423 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003424 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003425 */
3426 static void *
3427call_user_expand_func(
3428 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003429 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003430{
3431 cmdline_info_T *ccline = get_cmdline_info();
3432 int keep = 0;
3433 typval_T args[4];
3434 sctx_T save_current_sctx = current_sctx;
3435 char_u *pat = NULL;
3436 void *ret;
3437
3438 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3439 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003440
3441 if (ccline->cmdbuff != NULL)
3442 {
3443 keep = ccline->cmdbuff[ccline->cmdlen];
3444 ccline->cmdbuff[ccline->cmdlen] = 0;
3445 }
3446
3447 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3448
3449 args[0].v_type = VAR_STRING;
3450 args[0].vval.v_string = pat;
3451 args[1].v_type = VAR_STRING;
3452 args[1].vval.v_string = xp->xp_line;
3453 args[2].v_type = VAR_NUMBER;
3454 args[2].vval.v_number = xp->xp_col;
3455 args[3].v_type = VAR_UNKNOWN;
3456
3457 current_sctx = xp->xp_script_ctx;
3458
3459 ret = user_expand_func(xp->xp_arg, 3, args);
3460
3461 current_sctx = save_current_sctx;
3462 if (ccline->cmdbuff != NULL)
3463 ccline->cmdbuff[ccline->cmdlen] = keep;
3464
3465 vim_free(pat);
3466 return ret;
3467}
3468
3469/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003470 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3471 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472 */
3473 static int
3474ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003475 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003476 expand_T *xp,
3477 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003478 char_u ***matches,
3479 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480{
3481 char_u *retstr;
3482 char_u *s;
3483 char_u *e;
3484 int keep;
3485 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003486 int fuzzy;
3487 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003488 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003489
3490 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003491 *matches = NULL;
3492 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003493
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003494 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495 if (retstr == NULL)
3496 return FAIL;
3497
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003498 if (!fuzzy)
3499 ga_init2(&ga, sizeof(char *), 3);
3500 else
3501 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3502
Bram Moolenaar66b51422019-08-18 21:44:12 +02003503 for (s = retstr; *s != NUL; s = e)
3504 {
3505 e = vim_strchr(s, '\n');
3506 if (e == NULL)
3507 e = s + STRLEN(s);
3508 keep = *e;
3509 *e = NUL;
3510
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003511 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003512 {
3513 if (!fuzzy)
3514 match = vim_regexec(regmatch, s, (colnr_T)0);
3515 else
3516 {
3517 score = fuzzy_match_str(s, pat);
3518 match = (score != 0);
3519 }
3520 }
3521 else
3522 match = TRUE; // match everything
3523
Bram Moolenaar66b51422019-08-18 21:44:12 +02003524 *e = keep;
3525
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003526 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003527 {
3528 if (ga_grow(&ga, 1) == FAIL)
3529 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003530 if (!fuzzy)
3531 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3532 else
3533 {
3534 fuzmatch_str_T *fuzmatch =
3535 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003536 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003537 fuzmatch->str = vim_strnsave(s, e - s);
3538 fuzmatch->score = score;
3539 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540 ++ga.ga_len;
3541 }
3542
3543 if (*e != NUL)
3544 ++e;
3545 }
3546 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003547
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003548 if (ga.ga_len == 0)
3549 return OK;
3550
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003551 if (!fuzzy)
3552 {
3553 *matches = ga.ga_data;
3554 *numMatches = ga.ga_len;
3555 }
3556 else
3557 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003558 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3559 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003560 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003561 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003562 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003563 return OK;
3564}
3565
3566/*
3567 * Expand names with a list returned by a function defined by the user.
3568 */
3569 static int
3570ExpandUserList(
3571 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003572 char_u ***matches,
3573 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003574{
3575 list_T *retlist;
3576 listitem_T *li;
3577 garray_T ga;
3578
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003579 *matches = NULL;
3580 *numMatches = 0;
3581 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003582 if (retlist == NULL)
3583 return FAIL;
3584
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003585 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003586 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003587 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003588 {
3589 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3590 continue; // Skip non-string items and empty strings
3591
3592 if (ga_grow(&ga, 1) == FAIL)
3593 break;
3594
3595 ((char_u **)ga.ga_data)[ga.ga_len] =
3596 vim_strsave(li->li_tv.vval.v_string);
3597 ++ga.ga_len;
3598 }
3599 list_unref(retlist);
3600
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003601 *matches = ga.ga_data;
3602 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003603 return OK;
3604}
K.Takata161b6ac2022-11-14 15:31:07 +00003605#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003606
3607/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003608 * Expand "file" for all comma-separated directories in "path".
3609 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003610 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003611 */
3612 void
3613globpath(
3614 char_u *path,
3615 char_u *file,
3616 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003617 int expand_options,
3618 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003619{
3620 expand_T xpc;
3621 char_u *buf;
3622 int i;
3623 int num_p;
3624 char_u **p;
3625
3626 buf = alloc(MAXPATHL);
3627 if (buf == NULL)
3628 return;
3629
3630 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003631 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003632
3633 // Loop over all entries in {path}.
3634 while (*path != NUL)
3635 {
3636 // Copy one item of the path to buf[] and concatenate the file name.
3637 copy_option_part(&path, buf, MAXPATHL, ",");
3638 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3639 {
K.Takata161b6ac2022-11-14 15:31:07 +00003640#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003641 // Using the platform's path separator (\) makes vim incorrectly
3642 // treat it as an escape character, use '/' instead.
3643 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3644 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003645#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003646 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003647#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003648 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003649 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003650 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3651 {
3652 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3653
3654 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003655 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003656 for (i = 0; i < num_p; ++i)
3657 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003658 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003659 ++ga->ga_len;
3660 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003661 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003662 }
3663 }
3664 }
3665
3666 vim_free(buf);
3667}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003668
Bram Moolenaareadee482020-09-04 15:37:31 +02003669/*
3670 * Translate some keys pressed when 'wildmenu' is used.
3671 */
3672 int
3673wildmenu_translate_key(
3674 cmdline_info_T *cclp,
3675 int key,
3676 expand_T *xp,
3677 int did_wild_list)
3678{
3679 int c = key;
3680
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003681 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003682 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003683 // When the popup menu is used for cmdline completion:
3684 // Up : go to the previous item in the menu
3685 // Down : go to the next item in the menu
3686 // Left : go to the parent directory
3687 // Right: list the files in the selected directory
3688 switch (c)
3689 {
3690 case K_UP: c = K_LEFT; break;
3691 case K_DOWN: c = K_RIGHT; break;
3692 case K_LEFT: c = K_UP; break;
3693 case K_RIGHT: c = K_DOWN; break;
3694 default: break;
3695 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003696 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003697
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003698 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003699 {
3700 if (c == K_LEFT)
3701 c = Ctrl_P;
3702 else if (c == K_RIGHT)
3703 c = Ctrl_N;
3704 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003705
Bram Moolenaareadee482020-09-04 15:37:31 +02003706 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003707 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003708 && cclp->cmdpos > 1
3709 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3710 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3711 && (c == '\n' || c == '\r' || c == K_KENTER))
3712 c = K_DOWN;
3713
3714 return c;
3715}
3716
3717/*
3718 * Delete characters on the command line, from "from" to the current
3719 * position.
3720 */
3721 static void
3722cmdline_del(cmdline_info_T *cclp, int from)
3723{
3724 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3725 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3726 cclp->cmdlen -= cclp->cmdpos - from;
3727 cclp->cmdpos = from;
3728}
3729
3730/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003731 * Handle a key pressed when the wild menu for the menu names
3732 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003733 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003734 static int
3735wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003736{
Bram Moolenaareadee482020-09-04 15:37:31 +02003737 int i;
3738 int j;
3739
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003740 // Hitting <Down> after "emenu Name.": complete submenu
3741 if (key == K_DOWN && cclp->cmdpos > 0
3742 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003743 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003744 key = p_wc;
3745 KeyTyped = TRUE; // in case the key was mapped
3746 }
3747 else if (key == K_UP)
3748 {
3749 // Hitting <Up>: Remove one submenu name in front of the
3750 // cursor
3751 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003752
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003753 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3754 i = 0;
3755 while (--j > 0)
3756 {
3757 // check for start of menu name
3758 if (cclp->cmdbuff[j] == ' '
3759 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003760 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003761 i = j + 1;
3762 break;
3763 }
3764 // check for start of submenu name
3765 if (cclp->cmdbuff[j] == '.'
3766 && cclp->cmdbuff[j - 1] != '\\')
3767 {
3768 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003769 {
3770 i = j + 1;
3771 break;
3772 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003773 else
3774 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003775 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003776 }
3777 if (i > 0)
3778 cmdline_del(cclp, i);
3779 key = p_wc;
3780 KeyTyped = TRUE; // in case the key was mapped
3781 xp->xp_context = EXPAND_NOTHING;
3782 }
3783
3784 return key;
3785}
3786
3787/*
3788 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3789 * directory names (EXPAND_DIRECTORIES) or shell command names
3790 * (EXPAND_SHELLCMD) is displayed.
3791 */
3792 static int
3793wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3794{
3795 int i;
3796 int j;
3797 char_u upseg[5];
3798
3799 upseg[0] = PATHSEP;
3800 upseg[1] = '.';
3801 upseg[2] = '.';
3802 upseg[3] = PATHSEP;
3803 upseg[4] = NUL;
3804
3805 if (key == K_DOWN
3806 && cclp->cmdpos > 0
3807 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3808 && (cclp->cmdpos < 3
3809 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3810 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3811 {
3812 // go down a directory
3813 key = p_wc;
3814 KeyTyped = TRUE; // in case the key was mapped
3815 }
3816 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3817 {
3818 // If in a direct ancestor, strip off one ../ to go down
3819 int found = FALSE;
3820
3821 j = cclp->cmdpos;
3822 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3823 while (--j > i)
3824 {
3825 if (has_mbyte)
3826 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3827 if (vim_ispathsep(cclp->cmdbuff[j]))
3828 {
3829 found = TRUE;
3830 break;
3831 }
3832 }
3833 if (found
3834 && cclp->cmdbuff[j - 1] == '.'
3835 && cclp->cmdbuff[j - 2] == '.'
3836 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3837 {
3838 cmdline_del(cclp, j - 2);
3839 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003840 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003841 }
3842 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003843 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003844 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003845 // go up a directory
3846 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003847
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003848 j = cclp->cmdpos - 1;
3849 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3850 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003851 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003852 if (has_mbyte)
3853 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3854 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003855#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003856 && vim_strchr((char_u *)" *?[{`$%#",
3857 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003858#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003859 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003860 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003861 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003862 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003863 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003864 break;
3865 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003866 else
3867 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003868 }
3869 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003870
3871 if (!found)
3872 j = i;
3873 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3874 j += 4;
3875 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3876 && j == i)
3877 j += 3;
3878 else
3879 j = 0;
3880 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003881 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003882 // TODO this is only for DOS/UNIX systems - need to put in
3883 // machine-specific stuff here and in upseg init
3884 cmdline_del(cclp, j);
3885 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003886 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003887 else if (cclp->cmdpos > i)
3888 cmdline_del(cclp, i);
3889
3890 // Now complete in the new directory. Set KeyTyped in case the
3891 // Up key came from a mapping.
3892 key = p_wc;
3893 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003894 }
3895
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003896 return key;
3897}
3898
3899/*
3900 * Handle a key pressed when the wild menu is displayed
3901 */
3902 int
3903wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3904{
3905 if (xp->xp_context == EXPAND_MENUNAMES)
3906 return wildmenu_process_key_menunames(cclp, key, xp);
3907 else if ((xp->xp_context == EXPAND_FILES
3908 || xp->xp_context == EXPAND_DIRECTORIES
3909 || xp->xp_context == EXPAND_SHELLCMD))
3910 return wildmenu_process_key_filenames(cclp, key, xp);
3911
3912 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003913}
3914
3915/*
3916 * Free expanded names when finished walking through the matches
3917 */
3918 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003919wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003920{
3921 int skt = KeyTyped;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003922#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003923 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003924#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003925
3926 if (!p_wmnu || wild_menu_showing == 0)
3927 return;
3928
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003929#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003930 if (cclp->input_fn)
3931 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003932#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003933
3934 if (wild_menu_showing == WM_SCROLLED)
3935 {
3936 // Entered command line, move it up
3937 cmdline_row--;
3938 redrawcmd();
3939 }
3940 else if (save_p_ls != -1)
3941 {
3942 // restore 'laststatus' and 'winminheight'
3943 p_ls = save_p_ls;
3944 p_wmh = save_p_wmh;
3945 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003946 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003947 redrawcmd();
3948 save_p_ls = -1;
3949 }
3950 else
3951 {
3952 win_redraw_last_status(topframe);
3953 redraw_statuslines();
3954 }
3955 KeyTyped = skt;
3956 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003957#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003958 if (cclp->input_fn)
3959 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003960#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003961}
Bram Moolenaareadee482020-09-04 15:37:31 +02003962
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003963#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003964/*
3965 * "getcompletion()" function
3966 */
3967 void
3968f_getcompletion(typval_T *argvars, typval_T *rettv)
3969{
3970 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003971 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003972 expand_T xpc;
3973 int filtered = FALSE;
3974 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003975 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003976
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003977 if (in_vim9script()
3978 && (check_for_string_arg(argvars, 0) == FAIL
3979 || check_for_string_arg(argvars, 1) == FAIL
3980 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3981 return;
3982
ii144785fe02021-11-21 12:13:56 +00003983 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003984 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003985 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003986 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003987
3988 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003989 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003990
3991 if (p_wic)
3992 options |= WILD_ICASE;
3993
3994 // For filtered results, 'wildignore' is used
3995 if (!filtered)
3996 options |= WILD_KEEP_ALL;
3997
3998 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003999 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004000 {
ii144785fe02021-11-21 12:13:56 +00004001 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004002 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00004003 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004004 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004005 else
4006 {
ii144785fe02021-11-21 12:13:56 +00004007 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004008 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4009
4010 xpc.xp_context = cmdcomplete_str_to_type(type);
4011 if (xpc.xp_context == EXPAND_NOTHING)
4012 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004013 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004014 return;
4015 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004016
4017# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004018 if (xpc.xp_context == EXPAND_MENUS)
4019 {
4020 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4021 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4022 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004023# endif
4024# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004025 if (xpc.xp_context == EXPAND_CSCOPE)
4026 {
4027 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4028 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4029 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004030# endif
4031# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004032 if (xpc.xp_context == EXPAND_SIGN)
4033 {
4034 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4035 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4036 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004037# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004038 if (xpc.xp_context == EXPAND_RUNTIME)
4039 {
4040 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4041 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4042 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004043 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004044
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004045 if (cmdline_fuzzy_completion_supported(&xpc))
4046 // when fuzzy matching, don't modify the search string
4047 pat = vim_strsave(xpc.xp_pattern);
4048 else
4049 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4050
Bram Moolenaar93a10962022-06-16 11:42:09 +01004051 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004052 {
4053 int i;
4054
4055 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4056
4057 for (i = 0; i < xpc.xp_numfiles; i++)
4058 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4059 }
4060 vim_free(pat);
4061 ExpandCleanup(&xpc);
4062}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004063#endif // FEAT_EVAL