blob: e5b96fbe9c4a7b43e2f0be661bb8bca3ae56e5e8 [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
zeertzjqe9ef3472023-08-17 23:57:05 +0200694 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000695 */
696 static char_u *
697get_next_or_prev_match(
698 int mode,
699 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000700 char_u *orig_save)
701{
zeertzjqe9ef3472023-08-17 23:57:05 +0200702 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000703 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000704
705 if (xp->xp_numfiles <= 0)
706 return NULL;
707
708 if (mode == WILD_PREV)
709 {
710 if (findex == -1)
711 findex = xp->xp_numfiles;
712 --findex;
713 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000714 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000715 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000716 else if (mode == WILD_PAGEUP)
717 {
718 if (findex == 0)
719 // at the first entry, don't select any entries
720 findex = -1;
721 else if (findex == -1)
722 // no entry is selected. select the last entry
723 findex = xp->xp_numfiles - 1;
724 else
725 {
726 // go up by the pum height
727 ht = pum_get_height();
728 if (ht > 3)
729 ht -= 2;
730 findex -= ht;
731 if (findex < 0)
732 // few entries left, select the first entry
733 findex = 0;
734 }
735 }
736 else // mode == WILD_PAGEDOWN
737 {
738 if (findex == xp->xp_numfiles - 1)
739 // at the last entry, don't select any entries
740 findex = -1;
741 else if (findex == -1)
742 // no entry is selected. select the first entry
743 findex = 0;
744 else
745 {
746 // go down by the pum height
747 ht = pum_get_height();
748 if (ht > 3)
749 ht -= 2;
750 findex += ht;
751 if (findex >= xp->xp_numfiles)
752 // few entries left, select the last entry
753 findex = xp->xp_numfiles - 1;
754 }
755 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000756
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000757 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000758 if (findex < 0)
759 {
760 if (orig_save == NULL)
761 findex = xp->xp_numfiles - 1;
762 else
763 findex = -1;
764 }
765 if (findex >= xp->xp_numfiles)
766 {
767 if (orig_save == NULL)
768 findex = 0;
769 else
770 findex = -1;
771 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000772 if (compl_match_array)
773 {
774 compl_selected = findex;
775 cmdline_pum_display();
776 }
777 else if (p_wmnu)
778 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
779 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200780 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000781
782 if (findex == -1)
783 return vim_strsave(orig_save);
784
785 return vim_strsave(xp->xp_files[findex]);
786}
787
788/*
789 * Start the command-line expansion and get the matches.
790 */
791 static char_u *
792ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
793{
794 int non_suf_match; // number without matching suffix
795 int i;
796 char_u *ss = NULL;
797
798 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000799 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000800 options) == FAIL)
801 {
802#ifdef FNAME_ILLEGAL
803 // Illegal file name has been silently skipped. But when there
804 // are wildcards, the real problem is that there was no match,
805 // causing the pattern to be added, which has illegal characters.
806 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
807 semsg(_(e_no_match_str_2), str);
808#endif
809 }
810 else if (xp->xp_numfiles == 0)
811 {
812 if (!(options & WILD_SILENT))
813 semsg(_(e_no_match_str_2), str);
814 }
815 else
816 {
817 // Escape the matches for use on the command line.
818 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
819
820 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000821 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000822 {
823 if (xp->xp_numfiles)
824 non_suf_match = xp->xp_numfiles;
825 else
826 non_suf_match = 1;
827 if ((xp->xp_context == EXPAND_FILES
828 || xp->xp_context == EXPAND_DIRECTORIES)
829 && xp->xp_numfiles > 1)
830 {
831 // More than one match; check suffix.
832 // The files will have been sorted on matching suffix in
833 // expand_wildcards, only need to check the first two.
834 non_suf_match = 0;
835 for (i = 0; i < 2; ++i)
836 if (match_suffix(xp->xp_files[i]))
837 ++non_suf_match;
838 }
839 if (non_suf_match != 1)
840 {
841 // Can we ever get here unless it's while expanding
842 // interactively? If not, we can get rid of this all
843 // together. Don't really want to wait for this message
844 // (and possibly have to hit return to continue!).
845 if (!(options & WILD_SILENT))
846 emsg(_(e_too_many_file_names));
847 else if (!(options & WILD_NO_BEEP))
848 beep_flush();
849 }
850 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
851 ss = vim_strsave(xp->xp_files[0]);
852 }
853 }
854
855 return ss;
856}
857
858/*
859 * Return the longest common part in the list of cmdline completion matches.
860 */
861 static char_u *
862find_longest_match(expand_T *xp, int options)
863{
864 long_u len;
865 int mb_len = 1;
866 int c0, ci;
867 int i;
868 char_u *ss;
869
870 for (len = 0; xp->xp_files[0][len]; len += mb_len)
871 {
872 if (has_mbyte)
873 {
874 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
875 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
876 }
877 else
878 c0 = xp->xp_files[0][len];
879 for (i = 1; i < xp->xp_numfiles; ++i)
880 {
881 if (has_mbyte)
882 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
883 else
884 ci = xp->xp_files[i][len];
885 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
886 || xp->xp_context == EXPAND_FILES
887 || xp->xp_context == EXPAND_SHELLCMD
888 || xp->xp_context == EXPAND_BUFFERS))
889 {
890 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
891 break;
892 }
893 else if (c0 != ci)
894 break;
895 }
896 if (i < xp->xp_numfiles)
897 {
898 if (!(options & WILD_NO_BEEP))
899 vim_beep(BO_WILD);
900 break;
901 }
902 }
903
904 ss = alloc(len + 1);
905 if (ss)
906 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
907
908 return ss;
909}
910
911/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000912 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200913 * Chars that should not be expanded must be preceded with a backslash.
914 * Return a pointer to allocated memory containing the new string.
915 * Return NULL for failure.
916 *
917 * "orig" is the originally expanded string, copied to allocated memory. It
918 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
919 * WILD_PREV "orig" should be NULL.
920 *
921 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
922 * is WILD_EXPAND_FREE or WILD_ALL.
923 *
924 * mode = WILD_FREE: just free previously expanded matches
925 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
926 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
927 * mode = WILD_NEXT: use next match in multiple match, wrap to first
928 * mode = WILD_PREV: use previous match in multiple match, wrap to first
929 * mode = WILD_ALL: return all matches concatenated
930 * mode = WILD_LONGEST: return longest matched part
931 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000932 * mode = WILD_APPLY: apply the item selected in the cmdline completion
933 * popup menu and close the menu.
934 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
935 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200936 *
937 * options = WILD_LIST_NOTFOUND: list entries without a match
938 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
939 * options = WILD_USE_NL: Use '\n' for WILD_ALL
940 * options = WILD_NO_BEEP: Don't beep for multiple matches
941 * options = WILD_ADD_SLASH: add a slash after directory names
942 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
943 * options = WILD_SILENT: don't print warning messages
944 * options = WILD_ESCAPE: put backslash before special chars
945 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200946 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200947 *
948 * The variables xp->xp_context and xp->xp_backslash must have been set!
949 */
950 char_u *
951ExpandOne(
952 expand_T *xp,
953 char_u *str,
954 char_u *orig, // allocated copy of original of expanded string
955 int options,
956 int mode)
957{
958 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200959 static char_u *orig_save = NULL; // kept value of orig
960 int orig_saved = FALSE;
961 int i;
962 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200963
964 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000965 if (mode == WILD_NEXT || mode == WILD_PREV
966 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjqe9ef3472023-08-17 23:57:05 +0200967 return get_next_or_prev_match(mode, xp, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200968
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000969 if (mode == WILD_CANCEL)
970 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
971 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +0200972 ss = vim_strsave(xp->xp_selected == -1
zeertzjq094dd152023-06-15 22:51:57 +0100973 ? (orig_save ? orig_save : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +0200974 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000975
Bram Moolenaar66b51422019-08-18 21:44:12 +0200976 // free old names
977 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
978 {
979 FreeWild(xp->xp_numfiles, xp->xp_files);
980 xp->xp_numfiles = -1;
981 VIM_CLEAR(orig_save);
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000982
983 // The entries from xp_files may be used in the PUM, remove it.
984 if (compl_match_array != NULL)
985 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986 }
zeertzjqe9ef3472023-08-17 23:57:05 +0200987 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200988
989 if (mode == WILD_FREE) // only release file name
990 return NULL;
991
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000992 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200993 {
994 vim_free(orig_save);
995 orig_save = orig;
996 orig_saved = TRUE;
997
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000998 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200999 }
1000
1001 // Find longest common part
1002 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1003 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001004 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001005 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001006 }
1007
Bram Moolenaar57e95172022-08-20 19:26:14 +01001008 // Concatenate all matching names. Unless interrupted, this can be slow
1009 // and the result probably won't be used.
1010 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001011 {
1012 len = 0;
1013 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001014 {
1015 if (i > 0)
1016 {
1017 if (xp->xp_prefix == XP_PREFIX_NO)
1018 len += 2; // prefix "no"
1019 else if (xp->xp_prefix == XP_PREFIX_INV)
1020 len += 3; // prefix "inv"
1021 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001022 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001023 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001024 ss = alloc(len);
1025 if (ss != NULL)
1026 {
1027 *ss = NUL;
1028 for (i = 0; i < xp->xp_numfiles; ++i)
1029 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001030 if (i > 0)
1031 {
1032 if (xp->xp_prefix == XP_PREFIX_NO)
1033 STRCAT(ss, "no");
1034 else if (xp->xp_prefix == XP_PREFIX_INV)
1035 STRCAT(ss, "inv");
1036 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001037 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001038
Bram Moolenaar66b51422019-08-18 21:44:12 +02001039 if (i != xp->xp_numfiles - 1)
1040 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1041 }
1042 }
1043 }
1044
1045 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1046 ExpandCleanup(xp);
1047
1048 // Free "orig" if it wasn't stored in "orig_save".
1049 if (!orig_saved)
1050 vim_free(orig);
1051
1052 return ss;
1053}
1054
1055/*
1056 * Prepare an expand structure for use.
1057 */
1058 void
1059ExpandInit(expand_T *xp)
1060{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001061 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001062 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001063 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001064 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001065}
1066
1067/*
1068 * Cleanup an expand structure after use.
1069 */
1070 void
1071ExpandCleanup(expand_T *xp)
1072{
1073 if (xp->xp_numfiles >= 0)
1074 {
1075 FreeWild(xp->xp_numfiles, xp->xp_files);
1076 xp->xp_numfiles = -1;
1077 }
1078}
1079
1080/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001081 * Display one line of completion matches. Multiple matches are displayed in
1082 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001083 * matches - list of completion match names
1084 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001085 * lines - number of output lines
1086 * linenr - line number of matches to display
1087 * maxlen - maximum number of characters in each line
1088 * showtail - display only the tail of the full path of a file name
1089 * dir_attr - highlight attribute to use for directory names
1090 */
1091 static void
1092showmatches_oneline(
1093 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001094 char_u **matches,
1095 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001096 int lines,
1097 int linenr,
1098 int maxlen,
1099 int showtail,
1100 int dir_attr)
1101{
1102 int i, j;
1103 int isdir;
1104 int lastlen;
1105 char_u *p;
1106
1107 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001108 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001109 {
1110 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1111 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001112 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1113 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001114 msg_advance(maxlen + 1);
1115 msg_puts((char *)p);
1116 msg_advance(maxlen + 3);
1117 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1118 break;
1119 }
1120 for (i = maxlen - lastlen; --i >= 0; )
1121 msg_putchar(' ');
1122 if (xp->xp_context == EXPAND_FILES
1123 || xp->xp_context == EXPAND_SHELLCMD
1124 || xp->xp_context == EXPAND_BUFFERS)
1125 {
1126 // highlight directories
1127 if (xp->xp_numfiles != -1)
1128 {
1129 char_u *halved_slash;
1130 char_u *exp_path;
1131 char_u *path;
1132
1133 // Expansion was done before and special characters
1134 // were escaped, need to halve backslashes. Also
1135 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001136 exp_path = expand_env_save_opt(matches[j], TRUE);
1137 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001138 halved_slash = backslash_halve_save(path);
1139 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001140 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001141 vim_free(exp_path);
1142 if (halved_slash != path)
1143 vim_free(halved_slash);
1144 }
1145 else
1146 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001147 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001148 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001149 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001150 else
1151 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001152 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001153 TRUE);
1154 p = NameBuff;
1155 }
1156 }
1157 else
1158 {
1159 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001160 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001161 }
1162 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1163 }
1164 if (msg_col > 0) // when not wrapped around
1165 {
1166 msg_clr_eos();
1167 msg_putchar('\n');
1168 }
1169 out_flush(); // show one line at a time
1170}
1171
1172/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001173 * Show all matches for completion on the command line.
1174 * Returns EXPAND_NOTHING when the character that triggered expansion should
1175 * be inserted like a normal character.
1176 */
1177 int
1178showmatches(expand_T *xp, int wildmenu UNUSED)
1179{
1180 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001181 int numMatches;
1182 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001183 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001184 int maxlen;
1185 int lines;
1186 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001187 int attr;
1188 int showtail;
1189
1190 if (xp->xp_numfiles == -1)
1191 {
1192 set_expand_context(xp);
1193 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001194 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001195 showtail = expand_showtail(xp);
1196 if (i != EXPAND_OK)
1197 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001198 }
1199 else
1200 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001201 numMatches = xp->xp_numfiles;
1202 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001203 showtail = cmd_showtail;
1204 }
1205
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001206 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001207 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001208 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001209
Bram Moolenaar66b51422019-08-18 21:44:12 +02001210 if (!wildmenu)
1211 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001212 msg_didany = FALSE; // lines_left will be set
1213 msg_start(); // prepare for paging
1214 msg_putchar('\n');
1215 out_flush();
1216 cmdline_row = msg_row;
1217 msg_didany = FALSE; // lines_left will be set again
1218 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001219 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001220
1221 if (got_int)
1222 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001224 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001225 else
1226 {
1227 // find the length of the longest file name
1228 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001229 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001230 {
1231 if (!showtail && (xp->xp_context == EXPAND_FILES
1232 || xp->xp_context == EXPAND_SHELLCMD
1233 || xp->xp_context == EXPAND_BUFFERS))
1234 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001235 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001236 j = vim_strsize(NameBuff);
1237 }
1238 else
zeertzjqc51a3762022-12-10 10:22:29 +00001239 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001240 if (j > maxlen)
1241 maxlen = j;
1242 }
1243
1244 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001245 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001246 else
1247 {
1248 // compute the number of columns and lines for the listing
1249 maxlen += 2; // two spaces between file names
1250 columns = ((int)Columns + 2) / maxlen;
1251 if (columns < 1)
1252 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001253 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001254 }
1255
1256 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1257
1258 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1259 {
1260 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1261 msg_clr_eos();
1262 msg_advance(maxlen - 3);
1263 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1264 }
1265
1266 // list the files line by line
1267 for (i = 0; i < lines; ++i)
1268 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001269 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001270 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001271 if (got_int)
1272 {
1273 got_int = FALSE;
1274 break;
1275 }
1276 }
1277
1278 // we redraw the command below the lines that we have just listed
1279 // This is a bit tricky, but it saves a lot of screen updating.
1280 cmdline_row = msg_row; // will put it back later
1281 }
1282
1283 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001284 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001285
1286 return EXPAND_OK;
1287}
1288
1289/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001290 * gettail() version for showmatches() and win_redr_status_matches():
1291 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001292 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001293 static char_u *
1294showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001295{
1296 char_u *p;
1297 char_u *t = s;
1298 int had_sep = FALSE;
1299
1300 for (p = s; *p != NUL; )
1301 {
1302 if (vim_ispathsep(*p)
1303#ifdef BACKSLASH_IN_FILENAME
1304 && !rem_backslash(p)
1305#endif
1306 )
1307 had_sep = TRUE;
1308 else if (had_sep)
1309 {
1310 t = p;
1311 had_sep = FALSE;
1312 }
1313 MB_PTR_ADV(p);
1314 }
1315 return t;
1316}
1317
1318/*
1319 * Return TRUE if we only need to show the tail of completion matches.
1320 * When not completing file names or there is a wildcard in the path FALSE is
1321 * returned.
1322 */
1323 static int
1324expand_showtail(expand_T *xp)
1325{
1326 char_u *s;
1327 char_u *end;
1328
1329 // When not completing file names a "/" may mean something different.
1330 if (xp->xp_context != EXPAND_FILES
1331 && xp->xp_context != EXPAND_SHELLCMD
1332 && xp->xp_context != EXPAND_DIRECTORIES)
1333 return FALSE;
1334
1335 end = gettail(xp->xp_pattern);
1336 if (end == xp->xp_pattern) // there is no path separator
1337 return FALSE;
1338
1339 for (s = xp->xp_pattern; s < end; s++)
1340 {
1341 // Skip escaped wildcards. Only when the backslash is not a path
1342 // separator, on DOS the '*' "path\*\file" must not be skipped.
1343 if (rem_backslash(s))
1344 ++s;
1345 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1346 return FALSE;
1347 }
1348 return TRUE;
1349}
1350
1351/*
1352 * Prepare a string for expansion.
1353 * When expanding file names: The string will be used with expand_wildcards().
1354 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1355 * When expanding other names: The string will be used with regcomp(). Copy
1356 * the name into allocated memory and prepend "^".
1357 */
1358 char_u *
1359addstar(
1360 char_u *fname,
1361 int len,
1362 int context) // EXPAND_FILES etc.
1363{
1364 char_u *retval;
1365 int i, j;
1366 int new_len;
1367 char_u *tail;
1368 int ends_in_star;
1369
1370 if (context != EXPAND_FILES
1371 && context != EXPAND_FILES_IN_PATH
1372 && context != EXPAND_SHELLCMD
1373 && context != EXPAND_DIRECTORIES)
1374 {
1375 // Matching will be done internally (on something other than files).
1376 // So we convert the file-matching-type wildcards into our kind for
1377 // use with vim_regcomp(). First work out how long it will be:
1378
1379 // For help tags the translation is done in find_help_tags().
1380 // For a tag pattern starting with "/" no translation is needed.
1381 if (context == EXPAND_HELP
1382 || context == EXPAND_COLORS
1383 || context == EXPAND_COMPILER
1384 || context == EXPAND_OWNSYNTAX
1385 || context == EXPAND_FILETYPE
1386 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001387 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001388 || ((context == EXPAND_TAGS_LISTFILES
1389 || context == EXPAND_TAGS)
1390 && fname[0] == '/'))
1391 retval = vim_strnsave(fname, len);
1392 else
1393 {
1394 new_len = len + 2; // +2 for '^' at start, NUL at end
1395 for (i = 0; i < len; i++)
1396 {
1397 if (fname[i] == '*' || fname[i] == '~')
1398 new_len++; // '*' needs to be replaced by ".*"
1399 // '~' needs to be replaced by "\~"
1400
1401 // Buffer names are like file names. "." should be literal
1402 if (context == EXPAND_BUFFERS && fname[i] == '.')
1403 new_len++; // "." becomes "\."
1404
1405 // Custom expansion takes care of special things, match
1406 // backslashes literally (perhaps also for other types?)
1407 if ((context == EXPAND_USER_DEFINED
1408 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1409 new_len++; // '\' becomes "\\"
1410 }
1411 retval = alloc(new_len);
1412 if (retval != NULL)
1413 {
1414 retval[0] = '^';
1415 j = 1;
1416 for (i = 0; i < len; i++, j++)
1417 {
1418 // Skip backslash. But why? At least keep it for custom
1419 // expansion.
1420 if (context != EXPAND_USER_DEFINED
1421 && context != EXPAND_USER_LIST
1422 && fname[i] == '\\'
1423 && ++i == len)
1424 break;
1425
1426 switch (fname[i])
1427 {
1428 case '*': retval[j++] = '.';
1429 break;
1430 case '~': retval[j++] = '\\';
1431 break;
1432 case '?': retval[j] = '.';
1433 continue;
1434 case '.': if (context == EXPAND_BUFFERS)
1435 retval[j++] = '\\';
1436 break;
1437 case '\\': if (context == EXPAND_USER_DEFINED
1438 || context == EXPAND_USER_LIST)
1439 retval[j++] = '\\';
1440 break;
1441 }
1442 retval[j] = fname[i];
1443 }
1444 retval[j] = NUL;
1445 }
1446 }
1447 }
1448 else
1449 {
1450 retval = alloc(len + 4);
1451 if (retval != NULL)
1452 {
1453 vim_strncpy(retval, fname, len);
1454
1455 // Don't add a star to *, ~, ~user, $var or `cmd`.
1456 // * would become **, which walks the whole tree.
1457 // ~ would be at the start of the file name, but not the tail.
1458 // $ could be anywhere in the tail.
1459 // ` could be anywhere in the file name.
1460 // When the name ends in '$' don't add a star, remove the '$'.
1461 tail = gettail(retval);
1462 ends_in_star = (len > 0 && retval[len - 1] == '*');
1463#ifndef BACKSLASH_IN_FILENAME
1464 for (i = len - 2; i >= 0; --i)
1465 {
1466 if (retval[i] != '\\')
1467 break;
1468 ends_in_star = !ends_in_star;
1469 }
1470#endif
1471 if ((*retval != '~' || tail != retval)
1472 && !ends_in_star
1473 && vim_strchr(tail, '$') == NULL
1474 && vim_strchr(retval, '`') == NULL)
1475 retval[len++] = '*';
1476 else if (len > 0 && retval[len - 1] == '$')
1477 --len;
1478 retval[len] = NUL;
1479 }
1480 }
1481 return retval;
1482}
1483
1484/*
1485 * Must parse the command line so far to work out what context we are in.
1486 * Completion can then be done based on that context.
1487 * This routine sets the variables:
1488 * xp->xp_pattern The start of the pattern to be expanded within
1489 * the command line (ends at the cursor).
1490 * xp->xp_context The type of thing to expand. Will be one of:
1491 *
1492 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1493 * the command line, like an unknown command. Caller
1494 * should beep.
1495 * EXPAND_NOTHING Unrecognised context for completion, use char like
1496 * a normal char, rather than for completion. eg
1497 * :s/^I/
1498 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1499 * it.
1500 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1501 * EXPAND_FILES After command with EX_XFILE set, or after setting
1502 * with P_EXPAND set. eg :e ^I, :w>>^I
1503 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001504 * when we know only directories are of interest.
1505 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001506 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1507 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1508 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1509 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1510 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1511 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1512 * EXPAND_EVENTS Complete event names
1513 * EXPAND_SYNTAX Complete :syntax command arguments
1514 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1515 * EXPAND_AUGROUP Complete autocommand group names
1516 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1517 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1518 * eg :unmap a^I , :cunab x^I
1519 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1520 * eg :call sub^I
1521 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1522 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1523 * names in expressions, eg :while s^I
1524 * EXPAND_ENV_VARS Complete environment variable names
1525 * EXPAND_USER Complete user names
1526 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001527 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001528set_expand_context(expand_T *xp)
1529{
1530 cmdline_info_T *ccline = get_cmdline_info();
1531
1532 // only expansion for ':', '>' and '=' command-lines
1533 if (ccline->cmdfirstc != ':'
1534#ifdef FEAT_EVAL
1535 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1536 && !ccline->input_fn
1537#endif
1538 )
1539 {
1540 xp->xp_context = EXPAND_NOTHING;
1541 return;
1542 }
1543 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1544}
1545
Bram Moolenaard0190392019-08-23 21:17:35 +02001546/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001547 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1548 * For user defined commands, the completion context is set in 'xp' and the
1549 * completion flags in 'complp'.
1550 *
1551 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001552 */
1553 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001554set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001555{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001556 char_u *p = NULL;
1557 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001558 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001559
1560 // Isolate the command and search for it in the command table.
1561 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001562 // - the 'k' command can directly be followed by any character, but do
1563 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1564 // find matches anywhere in the command name, do this only for command
1565 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001566 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001567 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001568 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001569 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001570 p = cmd + 1;
1571 }
1572 else
1573 {
1574 p = cmd;
1575 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1576 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001577 // A user command may contain digits.
1578 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1579 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001580 while (ASCII_ISALNUM(*p) || *p == '*')
1581 ++p;
1582 // for python 3.x: ":py3*" commands completion
1583 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1584 {
1585 ++p;
1586 while (ASCII_ISALPHA(*p) || *p == '*')
1587 ++p;
1588 }
1589 // check for non-alpha command
1590 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1591 ++p;
1592 len = (int)(p - cmd);
1593
1594 if (len == 0)
1595 {
1596 xp->xp_context = EXPAND_UNSUCCESSFUL;
1597 return NULL;
1598 }
1599
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001600 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001601
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001602 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001603 // Also when doing fuzzy expansion for non-shell commands, support
1604 // alphanumeric characters.
1605 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1606 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001607 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1608 ++p;
1609 }
1610
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001611 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001612 // character, complete the command name.
1613 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1614 return NULL;
1615
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001616 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001617 {
1618 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1619 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001620 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001621 p = cmd + 1;
1622 }
1623 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1624 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001625 eap->cmd = cmd;
1626 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001627 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001628 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001629 }
1630 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001631 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001632 {
1633 // Not still touching the command and it was an illegal one
1634 xp->xp_context = EXPAND_UNSUCCESSFUL;
1635 return NULL;
1636 }
1637
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001638 return p;
1639}
Bram Moolenaard0190392019-08-23 21:17:35 +02001640
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001641/*
1642 * Set the completion context for a command argument with wild card characters.
1643 */
1644 static void
1645set_context_for_wildcard_arg(
1646 exarg_T *eap,
1647 char_u *arg,
1648 int usefilter,
1649 expand_T *xp,
1650 int *complp)
1651{
1652 char_u *p;
1653 int c;
1654 int in_quote = FALSE;
1655 char_u *bow = NULL; // Beginning of word
1656 int len = 0;
1657
1658 // Allow spaces within back-quotes to count as part of the argument
1659 // being expanded.
1660 xp->xp_pattern = skipwhite(arg);
1661 p = xp->xp_pattern;
1662 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001663 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001664 if (has_mbyte)
1665 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001666 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001667 c = *p;
1668 if (c == '\\' && p[1] != NUL)
1669 ++p;
1670 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001671 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001672 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001673 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001674 xp->xp_pattern = p;
1675 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001676 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001677 in_quote = !in_quote;
1678 }
1679 // An argument can contain just about everything, except
1680 // characters that end the command and white space.
1681 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001682#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001683 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001684#endif
1685 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001686 {
1687 len = 0; // avoid getting stuck when space is in 'isfname'
1688 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001689 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001690 if (has_mbyte)
1691 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001692 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001693 c = *p;
1694 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001695 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001696 if (has_mbyte)
1697 len = (*mb_ptr2len)(p);
1698 else
1699 len = 1;
1700 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001701 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001702 if (in_quote)
1703 bow = p;
1704 else
1705 xp->xp_pattern = p;
1706 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001707 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001708 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001709 }
1710
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001711 // If we are still inside the quotes, and we passed a space, just
1712 // expand from there.
1713 if (bow != NULL && in_quote)
1714 xp->xp_pattern = bow;
1715 xp->xp_context = EXPAND_FILES;
1716
1717 // For a shell command more chars need to be escaped.
1718 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1719 {
1720#ifndef BACKSLASH_IN_FILENAME
1721 xp->xp_shell = TRUE;
1722#endif
1723 // When still after the command name expand executables.
1724 if (xp->xp_pattern == skipwhite(arg))
1725 xp->xp_context = EXPAND_SHELLCMD;
1726 }
1727
1728 // Check for environment variable.
1729 if (*xp->xp_pattern == '$')
1730 {
1731 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1732 if (!vim_isIDc(*p))
1733 break;
1734 if (*p == NUL)
1735 {
1736 xp->xp_context = EXPAND_ENV_VARS;
1737 ++xp->xp_pattern;
1738 // Avoid that the assignment uses EXPAND_FILES again.
1739 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1740 *complp = EXPAND_ENV_VARS;
1741 }
1742 }
1743 // Check for user names.
1744 if (*xp->xp_pattern == '~')
1745 {
1746 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1747 ;
1748 // Complete ~user only if it partially matches a user name.
1749 // A full match ~user<Tab> will be replaced by user's home
1750 // directory i.e. something like ~user<Tab> -> /home/user/
1751 if (*p == NUL && p > xp->xp_pattern + 1
1752 && match_user(xp->xp_pattern + 1) >= 1)
1753 {
1754 xp->xp_context = EXPAND_USER;
1755 ++xp->xp_pattern;
1756 }
1757 }
1758}
1759
1760/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001761 * Set the completion context for the :filter command. Returns a pointer to the
1762 * next command after the :filter command.
1763 */
1764 static char_u *
1765set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1766{
1767 if (*arg != NUL)
1768 arg = skip_vimgrep_pat(arg, NULL, NULL);
1769 if (arg == NULL || *arg == NUL)
1770 {
1771 xp->xp_context = EXPAND_NOTHING;
1772 return NULL;
1773 }
1774 return skipwhite(arg);
1775}
1776
1777#ifdef FEAT_SEARCH_EXTRA
1778/*
1779 * Set the completion context for the :match command. Returns a pointer to the
1780 * next command after the :match command.
1781 */
1782 static char_u *
1783set_context_in_match_cmd(expand_T *xp, char_u *arg)
1784{
1785 if (*arg == NUL || !ends_excmd(*arg))
1786 {
1787 // also complete "None"
1788 set_context_in_echohl_cmd(xp, arg);
1789 arg = skipwhite(skiptowhite(arg));
1790 if (*arg != NUL)
1791 {
1792 xp->xp_context = EXPAND_NOTHING;
1793 arg = skip_regexp(arg + 1, *arg, magic_isset());
1794 }
1795 }
1796 return find_nextcmd(arg);
1797}
1798#endif
1799
1800/*
1801 * Returns a pointer to the next command after a :global or a :v command.
1802 * Returns NULL if there is no next command.
1803 */
1804 static char_u *
1805find_cmd_after_global_cmd(char_u *arg)
1806{
1807 int delim;
1808
1809 delim = *arg; // get the delimiter
1810 if (delim)
1811 ++arg; // skip delimiter if there is one
1812
1813 while (arg[0] != NUL && arg[0] != delim)
1814 {
1815 if (arg[0] == '\\' && arg[1] != NUL)
1816 ++arg;
1817 ++arg;
1818 }
1819 if (arg[0] != NUL)
1820 return arg + 1;
1821
1822 return NULL;
1823}
1824
1825/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001826 * Returns a pointer to the next command after a :substitute or a :& command.
1827 * Returns NULL if there is no next command.
1828 */
1829 static char_u *
1830find_cmd_after_substitute_cmd(char_u *arg)
1831{
1832 int delim;
1833
1834 delim = *arg;
1835 if (delim)
1836 {
1837 // skip "from" part
1838 ++arg;
1839 arg = skip_regexp(arg, delim, magic_isset());
1840
1841 if (arg[0] != NUL && arg[0] == delim)
1842 {
1843 // skip "to" part
1844 ++arg;
1845 while (arg[0] != NUL && arg[0] != delim)
1846 {
1847 if (arg[0] == '\\' && arg[1] != NUL)
1848 ++arg;
1849 ++arg;
1850 }
1851 if (arg[0] != NUL) // skip delimiter
1852 ++arg;
1853 }
1854 }
1855 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1856 ++arg;
1857 if (arg[0] != NUL)
1858 return arg;
1859
1860 return NULL;
1861}
1862
1863/*
1864 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1865 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1866 * Returns NULL if there is no next command.
1867 */
1868 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001869find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001870{
1871 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001872 if (*arg != '/')
1873 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001874
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001875 // Match regexp, not just whole words
1876 for (++arg; *arg && *arg != '/'; arg++)
1877 if (*arg == '\\' && arg[1] != NUL)
1878 arg++;
1879 if (*arg)
1880 {
1881 arg = skipwhite(arg + 1);
1882
1883 // Check for trailing illegal characters
1884 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1885 xp->xp_context = EXPAND_NOTHING;
1886 else
1887 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001888 }
1889
1890 return NULL;
1891}
1892
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001893#ifdef FEAT_EVAL
1894/*
1895 * Set the completion context for the :unlet command. Always returns NULL.
1896 */
1897 static char_u *
1898set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1899{
1900 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1901 arg = xp->xp_pattern + 1;
1902
1903 xp->xp_context = EXPAND_USER_VARS;
1904 xp->xp_pattern = arg;
1905
1906 if (*xp->xp_pattern == '$')
1907 {
1908 xp->xp_context = EXPAND_ENV_VARS;
1909 ++xp->xp_pattern;
1910 }
1911
1912 return NULL;
1913}
1914#endif
1915
1916#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1917/*
1918 * Set the completion context for the :language command. Always returns NULL.
1919 */
1920 static char_u *
1921set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1922{
1923 char_u *p;
1924
1925 p = skiptowhite(arg);
1926 if (*p == NUL)
1927 {
1928 xp->xp_context = EXPAND_LANGUAGE;
1929 xp->xp_pattern = arg;
1930 }
1931 else
1932 {
1933 if ( STRNCMP(arg, "messages", p - arg) == 0
1934 || STRNCMP(arg, "ctype", p - arg) == 0
1935 || STRNCMP(arg, "time", p - arg) == 0
1936 || STRNCMP(arg, "collate", p - arg) == 0)
1937 {
1938 xp->xp_context = EXPAND_LOCALES;
1939 xp->xp_pattern = skipwhite(p);
1940 }
1941 else
1942 xp->xp_context = EXPAND_NOTHING;
1943 }
1944
1945 return NULL;
1946}
1947#endif
1948
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001949#ifdef FEAT_EVAL
1950static enum
1951{
1952 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001953 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1954 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001955} breakpt_expand_what;
1956
1957/*
1958 * Set the completion context for the :breakadd command. Always returns NULL.
1959 */
1960 static char_u *
1961set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1962{
1963 char_u *p;
1964 char_u *subcmd_start;
1965
1966 xp->xp_context = EXPAND_BREAKPOINT;
1967 xp->xp_pattern = arg;
1968
1969 if (cmdidx == CMD_breakadd)
1970 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001971 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001972 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001973 else
1974 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001975
1976 p = skipwhite(arg);
1977 if (*p == NUL)
1978 return NULL;
1979 subcmd_start = p;
1980
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001981 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001982 {
1983 // :breakadd file [lnum] <filename>
1984 // :breakadd func [lnum] <funcname>
1985 p += 4;
1986 p = skipwhite(p);
1987
1988 // skip line number (if specified)
1989 if (VIM_ISDIGIT(*p))
1990 {
1991 p = skipdigits(p);
1992 if (*p != ' ')
1993 {
1994 xp->xp_context = EXPAND_NOTHING;
1995 return NULL;
1996 }
1997 p = skipwhite(p);
1998 }
1999 if (STRNCMP("file", subcmd_start, 4) == 0)
2000 xp->xp_context = EXPAND_FILES;
2001 else
2002 xp->xp_context = EXPAND_USER_FUNC;
2003 xp->xp_pattern = p;
2004 }
2005 else if (STRNCMP("expr ", p, 5) == 0)
2006 {
2007 // :breakadd expr <expression>
2008 xp->xp_context = EXPAND_EXPRESSION;
2009 xp->xp_pattern = skipwhite(p + 5);
2010 }
2011
2012 return NULL;
2013}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002014
2015 static char_u *
2016set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2017{
2018 char_u *p;
2019
2020 xp->xp_context = EXPAND_NOTHING;
2021 xp->xp_pattern = NULL;
2022
2023 p = skipwhite(arg);
2024 if (VIM_ISDIGIT(*p))
2025 return NULL;
2026
2027 xp->xp_context = EXPAND_SCRIPTNAMES;
2028 xp->xp_pattern = p;
2029
2030 return NULL;
2031}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002032#endif
2033
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002034/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002035 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2036 * The argument to the command is 'arg' and the argument flags is 'argt'.
2037 * For user-defined commands and for environment variables, 'compl' has the
2038 * completion type.
2039 * Returns a pointer to the next command. Returns NULL if there is no next
2040 * command.
2041 */
2042 static char_u *
2043set_context_by_cmdname(
2044 char_u *cmd,
2045 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002046 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002047 char_u *arg,
2048 long argt,
2049 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002050 int forceit)
2051{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002052 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002053 {
2054 case CMD_find:
2055 case CMD_sfind:
2056 case CMD_tabfind:
2057 if (xp->xp_context == EXPAND_FILES)
2058 xp->xp_context = EXPAND_FILES_IN_PATH;
2059 break;
2060 case CMD_cd:
2061 case CMD_chdir:
2062 case CMD_tcd:
2063 case CMD_tchdir:
2064 case CMD_lcd:
2065 case CMD_lchdir:
2066 if (xp->xp_context == EXPAND_FILES)
2067 xp->xp_context = EXPAND_DIRECTORIES;
2068 break;
2069 case CMD_help:
2070 xp->xp_context = EXPAND_HELP;
2071 xp->xp_pattern = arg;
2072 break;
2073
2074 // Command modifiers: return the argument.
2075 // Also for commands with an argument that is a command.
2076 case CMD_aboveleft:
2077 case CMD_argdo:
2078 case CMD_belowright:
2079 case CMD_botright:
2080 case CMD_browse:
2081 case CMD_bufdo:
2082 case CMD_cdo:
2083 case CMD_cfdo:
2084 case CMD_confirm:
2085 case CMD_debug:
2086 case CMD_folddoclosed:
2087 case CMD_folddoopen:
2088 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002089 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002090 case CMD_keepalt:
2091 case CMD_keepjumps:
2092 case CMD_keepmarks:
2093 case CMD_keeppatterns:
2094 case CMD_ldo:
2095 case CMD_leftabove:
2096 case CMD_lfdo:
2097 case CMD_lockmarks:
2098 case CMD_noautocmd:
2099 case CMD_noswapfile:
2100 case CMD_rightbelow:
2101 case CMD_sandbox:
2102 case CMD_silent:
2103 case CMD_tab:
2104 case CMD_tabdo:
2105 case CMD_topleft:
2106 case CMD_verbose:
2107 case CMD_vertical:
2108 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002109 case CMD_vim9cmd:
2110 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002111 return arg;
2112
2113 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002114 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002115
2116#ifdef FEAT_SEARCH_EXTRA
2117 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002118 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002119#endif
2120
2121 // All completion for the +cmdline_compl feature goes here.
2122
2123 case CMD_command:
2124 return set_context_in_user_cmd(xp, arg);
2125
2126 case CMD_delcommand:
2127 xp->xp_context = EXPAND_USER_COMMANDS;
2128 xp->xp_pattern = arg;
2129 break;
2130
2131 case CMD_global:
2132 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002133 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002134 case CMD_and:
2135 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002136 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002137 case CMD_isearch:
2138 case CMD_dsearch:
2139 case CMD_ilist:
2140 case CMD_dlist:
2141 case CMD_ijump:
2142 case CMD_psearch:
2143 case CMD_djump:
2144 case CMD_isplit:
2145 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002146 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002147 case CMD_autocmd:
2148 return set_context_in_autocmd(xp, arg, FALSE);
2149 case CMD_doautocmd:
2150 case CMD_doautoall:
2151 return set_context_in_autocmd(xp, arg, TRUE);
2152 case CMD_set:
2153 set_context_in_set_cmd(xp, arg, 0);
2154 break;
2155 case CMD_setglobal:
2156 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2157 break;
2158 case CMD_setlocal:
2159 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2160 break;
2161 case CMD_tag:
2162 case CMD_stag:
2163 case CMD_ptag:
2164 case CMD_ltag:
2165 case CMD_tselect:
2166 case CMD_stselect:
2167 case CMD_ptselect:
2168 case CMD_tjump:
2169 case CMD_stjump:
2170 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002171 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002172 xp->xp_context = EXPAND_TAGS_LISTFILES;
2173 else
2174 xp->xp_context = EXPAND_TAGS;
2175 xp->xp_pattern = arg;
2176 break;
2177 case CMD_augroup:
2178 xp->xp_context = EXPAND_AUGROUP;
2179 xp->xp_pattern = arg;
2180 break;
2181#ifdef FEAT_SYN_HL
2182 case CMD_syntax:
2183 set_context_in_syntax_cmd(xp, arg);
2184 break;
2185#endif
2186#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002187 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002188 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002189 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002190 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002191 case CMD_if:
2192 case CMD_elseif:
2193 case CMD_while:
2194 case CMD_for:
2195 case CMD_echo:
2196 case CMD_echon:
2197 case CMD_execute:
2198 case CMD_echomsg:
2199 case CMD_echoerr:
2200 case CMD_call:
2201 case CMD_return:
2202 case CMD_cexpr:
2203 case CMD_caddexpr:
2204 case CMD_cgetexpr:
2205 case CMD_lexpr:
2206 case CMD_laddexpr:
2207 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002208 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002209 break;
2210
2211 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002212 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002213 case CMD_function:
2214 case CMD_delfunction:
2215 xp->xp_context = EXPAND_USER_FUNC;
2216 xp->xp_pattern = arg;
2217 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002218 case CMD_disassemble:
2219 set_context_in_disassemble_cmd(xp, arg);
2220 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002221
2222 case CMD_echohl:
2223 set_context_in_echohl_cmd(xp, arg);
2224 break;
2225#endif
2226 case CMD_highlight:
2227 set_context_in_highlight_cmd(xp, arg);
2228 break;
2229#ifdef FEAT_CSCOPE
2230 case CMD_cscope:
2231 case CMD_lcscope:
2232 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002233 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002234 break;
2235#endif
2236#ifdef FEAT_SIGNS
2237 case CMD_sign:
2238 set_context_in_sign_cmd(xp, arg);
2239 break;
2240#endif
2241 case CMD_bdelete:
2242 case CMD_bwipeout:
2243 case CMD_bunload:
2244 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2245 arg = xp->xp_pattern + 1;
2246 // FALLTHROUGH
2247 case CMD_buffer:
2248 case CMD_sbuffer:
2249 case CMD_checktime:
2250 xp->xp_context = EXPAND_BUFFERS;
2251 xp->xp_pattern = arg;
2252 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002253#ifdef FEAT_DIFF
2254 case CMD_diffget:
2255 case CMD_diffput:
2256 // If current buffer is in diff mode, complete buffer names
2257 // which are in diff mode, and different than current buffer.
2258 xp->xp_context = EXPAND_DIFF_BUFFERS;
2259 xp->xp_pattern = arg;
2260 break;
2261#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002262 case CMD_USER:
2263 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002264 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2265 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002266
2267 case CMD_map: case CMD_noremap:
2268 case CMD_nmap: case CMD_nnoremap:
2269 case CMD_vmap: case CMD_vnoremap:
2270 case CMD_omap: case CMD_onoremap:
2271 case CMD_imap: case CMD_inoremap:
2272 case CMD_cmap: case CMD_cnoremap:
2273 case CMD_lmap: case CMD_lnoremap:
2274 case CMD_smap: case CMD_snoremap:
2275 case CMD_tmap: case CMD_tnoremap:
2276 case CMD_xmap: case CMD_xnoremap:
2277 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002278 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002279 case CMD_unmap:
2280 case CMD_nunmap:
2281 case CMD_vunmap:
2282 case CMD_ounmap:
2283 case CMD_iunmap:
2284 case CMD_cunmap:
2285 case CMD_lunmap:
2286 case CMD_sunmap:
2287 case CMD_tunmap:
2288 case CMD_xunmap:
2289 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002290 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002291 case CMD_mapclear:
2292 case CMD_nmapclear:
2293 case CMD_vmapclear:
2294 case CMD_omapclear:
2295 case CMD_imapclear:
2296 case CMD_cmapclear:
2297 case CMD_lmapclear:
2298 case CMD_smapclear:
2299 case CMD_tmapclear:
2300 case CMD_xmapclear:
2301 xp->xp_context = EXPAND_MAPCLEAR;
2302 xp->xp_pattern = arg;
2303 break;
2304
2305 case CMD_abbreviate: case CMD_noreabbrev:
2306 case CMD_cabbrev: case CMD_cnoreabbrev:
2307 case CMD_iabbrev: case CMD_inoreabbrev:
2308 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002309 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002310 case CMD_unabbreviate:
2311 case CMD_cunabbrev:
2312 case CMD_iunabbrev:
2313 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002314 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002315#ifdef FEAT_MENU
2316 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2317 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2318 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2319 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2320 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2321 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2322 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2323 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2324 case CMD_tmenu: case CMD_tunmenu:
2325 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2326 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2327#endif
2328
2329 case CMD_colorscheme:
2330 xp->xp_context = EXPAND_COLORS;
2331 xp->xp_pattern = arg;
2332 break;
2333
2334 case CMD_compiler:
2335 xp->xp_context = EXPAND_COMPILER;
2336 xp->xp_pattern = arg;
2337 break;
2338
2339 case CMD_ownsyntax:
2340 xp->xp_context = EXPAND_OWNSYNTAX;
2341 xp->xp_pattern = arg;
2342 break;
2343
2344 case CMD_setfiletype:
2345 xp->xp_context = EXPAND_FILETYPE;
2346 xp->xp_pattern = arg;
2347 break;
2348
2349 case CMD_packadd:
2350 xp->xp_context = EXPAND_PACKADD;
2351 xp->xp_pattern = arg;
2352 break;
2353
zeertzjqb0d45ec2023-01-25 15:04:22 +00002354 case CMD_runtime:
2355 set_context_in_runtime_cmd(xp, arg);
2356 break;
2357
Bram Moolenaard0190392019-08-23 21:17:35 +02002358#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2359 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002360 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002361#endif
2362#if defined(FEAT_PROFILE)
2363 case CMD_profile:
2364 set_context_in_profile_cmd(xp, arg);
2365 break;
2366#endif
2367 case CMD_behave:
2368 xp->xp_context = EXPAND_BEHAVE;
2369 xp->xp_pattern = arg;
2370 break;
2371
2372 case CMD_messages:
2373 xp->xp_context = EXPAND_MESSAGES;
2374 xp->xp_pattern = arg;
2375 break;
2376
2377 case CMD_history:
2378 xp->xp_context = EXPAND_HISTORY;
2379 xp->xp_pattern = arg;
2380 break;
2381#if defined(FEAT_PROFILE)
2382 case CMD_syntime:
2383 xp->xp_context = EXPAND_SYNTIME;
2384 xp->xp_pattern = arg;
2385 break;
2386#endif
2387
2388 case CMD_argdelete:
2389 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2390 arg = xp->xp_pattern + 1;
2391 xp->xp_context = EXPAND_ARGLIST;
2392 xp->xp_pattern = arg;
2393 break;
2394
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002395#ifdef FEAT_EVAL
2396 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002397 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002398 case CMD_breakdel:
2399 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002400
2401 case CMD_scriptnames:
2402 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002403#endif
2404
Bram Moolenaard0190392019-08-23 21:17:35 +02002405 default:
2406 break;
2407 }
2408 return NULL;
2409}
2410
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002411/*
2412 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2413 * we don't need/want deleted. Maybe this could be done better if we didn't
2414 * repeat all this stuff. The only problem is that they may not stay
2415 * perfectly compatible with each other, but then the command line syntax
2416 * probably won't change that much -- webb.
2417 */
2418 static char_u *
2419set_one_cmd_context(
2420 expand_T *xp,
2421 char_u *buff) // buffer for command string
2422{
2423 char_u *p;
2424 char_u *cmd, *arg;
2425 int len = 0;
2426 exarg_T ea;
2427 int compl = EXPAND_NOTHING;
2428 int forceit = FALSE;
2429 int usefilter = FALSE; // filter instead of file name
2430
2431 ExpandInit(xp);
2432 xp->xp_pattern = buff;
2433 xp->xp_line = buff;
2434 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2435 ea.argt = 0;
2436
2437 // 1. skip comment lines and leading space, colons or bars
2438 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2439 ;
2440 xp->xp_pattern = cmd;
2441
2442 if (*cmd == NUL)
2443 return NULL;
2444 if (*cmd == '"') // ignore comment lines
2445 {
2446 xp->xp_context = EXPAND_NOTHING;
2447 return NULL;
2448 }
2449
2450 // 3. Skip over the range to find the command.
2451 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2452 xp->xp_pattern = cmd;
2453 if (*cmd == NUL)
2454 return NULL;
2455 if (*cmd == '"')
2456 {
2457 xp->xp_context = EXPAND_NOTHING;
2458 return NULL;
2459 }
2460
2461 if (*cmd == '|' || *cmd == '\n')
2462 return cmd + 1; // There's another command
2463
2464 // Get the command index.
2465 p = set_cmd_index(cmd, &ea, xp, &compl);
2466 if (p == NULL)
2467 return NULL;
2468
2469 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2470
2471 if (*p == '!') // forced commands
2472 {
2473 forceit = TRUE;
2474 ++p;
2475 }
2476
2477 // 6. parse arguments
2478 if (!IS_USER_CMDIDX(ea.cmdidx))
2479 ea.argt = excmd_get_argt(ea.cmdidx);
2480
2481 arg = skipwhite(p);
2482
2483 // Skip over ++argopt argument
2484 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2485 {
2486 p = arg;
2487 while (*p && !vim_isspace(*p))
2488 MB_PTR_ADV(p);
2489 arg = skipwhite(p);
2490 }
2491
2492 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2493 {
2494 if (*arg == '>') // append
2495 {
2496 if (*++arg == '>')
2497 ++arg;
2498 arg = skipwhite(arg);
2499 }
2500 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2501 {
2502 ++arg;
2503 usefilter = TRUE;
2504 }
2505 }
2506
2507 if (ea.cmdidx == CMD_read)
2508 {
2509 usefilter = forceit; // :r! filter if forced
2510 if (*arg == '!') // :r !filter
2511 {
2512 ++arg;
2513 usefilter = TRUE;
2514 }
2515 }
2516
2517 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2518 {
2519 while (*arg == *cmd) // allow any number of '>' or '<'
2520 ++arg;
2521 arg = skipwhite(arg);
2522 }
2523
2524 // Does command allow "+command"?
2525 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2526 {
2527 // Check if we're in the +command
2528 p = arg + 1;
2529 arg = skip_cmd_arg(arg, FALSE);
2530
2531 // Still touching the command after '+'?
2532 if (*arg == NUL)
2533 return p;
2534
2535 // Skip space(s) after +command to get to the real argument
2536 arg = skipwhite(arg);
2537 }
2538
2539
2540 // Check for '|' to separate commands and '"' to start comments.
2541 // Don't do this for ":read !cmd" and ":write !cmd".
2542 if ((ea.argt & EX_TRLBAR) && !usefilter)
2543 {
2544 p = arg;
2545 // ":redir @" is not the start of a comment
2546 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2547 p += 2;
2548 while (*p)
2549 {
2550 if (*p == Ctrl_V)
2551 {
2552 if (p[1] != NUL)
2553 ++p;
2554 }
2555 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2556 || *p == '|' || *p == '\n')
2557 {
2558 if (*(p - 1) != '\\')
2559 {
2560 if (*p == '|' || *p == '\n')
2561 return p + 1;
2562 return NULL; // It's a comment
2563 }
2564 }
2565 MB_PTR_ADV(p);
2566 }
2567 }
2568
2569 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2570 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2571 // no arguments allowed but there is something
2572 return NULL;
2573
2574 // Find start of last argument (argument just before cursor):
2575 p = buff;
2576 xp->xp_pattern = p;
2577 len = (int)STRLEN(buff);
2578 while (*p && p < buff + len)
2579 {
2580 if (*p == ' ' || *p == TAB)
2581 {
2582 // argument starts after a space
2583 xp->xp_pattern = ++p;
2584 }
2585 else
2586 {
2587 if (*p == '\\' && *(p + 1) != NUL)
2588 ++p; // skip over escaped character
2589 MB_PTR_ADV(p);
2590 }
2591 }
2592
2593 if (ea.argt & EX_XFILE)
2594 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2595
2596 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002597 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002598 forceit);
2599}
2600
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002601/*
2602 * Set the completion context in 'xp' for command 'str'
2603 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002604 void
2605set_cmd_context(
2606 expand_T *xp,
2607 char_u *str, // start of command line
2608 int len, // length of command line (excl. NUL)
2609 int col, // position of cursor
2610 int use_ccline UNUSED) // use ccline for info
2611{
2612#ifdef FEAT_EVAL
2613 cmdline_info_T *ccline = get_cmdline_info();
2614#endif
2615 int old_char = NUL;
2616 char_u *nextcomm;
2617
2618 // Avoid a UMR warning from Purify, only save the character if it has been
2619 // written before.
2620 if (col < len)
2621 old_char = str[col];
2622 str[col] = NUL;
2623 nextcomm = str;
2624
2625#ifdef FEAT_EVAL
2626 if (use_ccline && ccline->cmdfirstc == '=')
2627 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002628 // pass CMD_SIZE because there is no real command
2629 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002630 }
2631 else if (use_ccline && ccline->input_fn)
2632 {
2633 xp->xp_context = ccline->xp_context;
2634 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002635 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002636 }
2637 else
2638#endif
2639 while (nextcomm != NULL)
2640 nextcomm = set_one_cmd_context(xp, nextcomm);
2641
2642 // Store the string here so that call_user_expand_func() can get to them
2643 // easily.
2644 xp->xp_line = str;
2645 xp->xp_col = col;
2646
2647 str[col] = old_char;
2648}
2649
2650/*
2651 * Expand the command line "str" from context "xp".
2652 * "xp" must have been set by set_cmd_context().
2653 * xp->xp_pattern points into "str", to where the text that is to be expanded
2654 * starts.
2655 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2656 * cursor.
2657 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2658 * key that triggered expansion literally.
2659 * Returns EXPAND_OK otherwise.
2660 */
2661 int
2662expand_cmdline(
2663 expand_T *xp,
2664 char_u *str, // start of command line
2665 int col, // position of cursor
2666 int *matchcount, // return: nr of matches
2667 char_u ***matches) // return: array of pointers to matches
2668{
2669 char_u *file_str = NULL;
2670 int options = WILD_ADD_SLASH|WILD_SILENT;
2671
2672 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2673 {
2674 beep_flush();
2675 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2676 }
2677 if (xp->xp_context == EXPAND_NOTHING)
2678 {
2679 // Caller can use the character as a normal char instead
2680 return EXPAND_NOTHING;
2681 }
2682
2683 // add star to file name, or convert to regexp if not exp. files.
2684 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002685 if (cmdline_fuzzy_completion_supported(xp))
2686 // If fuzzy matching, don't modify the search string
2687 file_str = vim_strsave(xp->xp_pattern);
2688 else
2689 {
2690 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2691 if (file_str == NULL)
2692 return EXPAND_UNSUCCESSFUL;
2693 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002694
2695 if (p_wic)
2696 options += WILD_ICASE;
2697
2698 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002699 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002700 {
2701 *matchcount = 0;
2702 *matches = NULL;
2703 }
2704 vim_free(file_str);
2705
2706 return EXPAND_OK;
2707}
2708
Bram Moolenaar66b51422019-08-18 21:44:12 +02002709/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002710 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002711 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002712 */
2713 static int
2714expand_files_and_dirs(
2715 expand_T *xp,
2716 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002717 char_u ***matches,
2718 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002719 int flags,
2720 int options)
2721{
2722 int free_pat = FALSE;
2723 int i;
2724 int ret;
2725
2726 // for ":set path=" and ":set tags=" halve backslashes for escaped
2727 // space
2728 if (xp->xp_backslash != XP_BS_NONE)
2729 {
2730 free_pat = TRUE;
2731 pat = vim_strsave(pat);
2732 for (i = 0; pat[i]; ++i)
2733 if (pat[i] == '\\')
2734 {
2735 if (xp->xp_backslash == XP_BS_THREE
2736 && pat[i + 1] == '\\'
2737 && pat[i + 2] == '\\'
2738 && pat[i + 3] == ' ')
2739 STRMOVE(pat + i, pat + i + 3);
2740 if (xp->xp_backslash == XP_BS_ONE
2741 && pat[i + 1] == ' ')
2742 STRMOVE(pat + i, pat + i + 1);
2743 }
2744 }
2745
2746 if (xp->xp_context == EXPAND_FILES)
2747 flags |= EW_FILE;
2748 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2749 flags |= (EW_FILE | EW_PATH);
2750 else
2751 flags = (flags | EW_DIR) & ~EW_FILE;
2752 if (options & WILD_ICASE)
2753 flags |= EW_ICASE;
2754
2755 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002756 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002757 if (free_pat)
2758 vim_free(pat);
2759#ifdef BACKSLASH_IN_FILENAME
2760 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2761 {
2762 int j;
2763
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002764 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002765 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002766 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002767
2768 while (*ptr != NUL)
2769 {
2770 if (p_csl[0] == 's' && *ptr == '\\')
2771 *ptr = '/';
2772 else if (p_csl[0] == 'b' && *ptr == '/')
2773 *ptr = '\\';
2774 ptr += (*mb_ptr2len)(ptr);
2775 }
2776 }
2777 }
2778#endif
2779 return ret;
2780}
2781
2782/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002783 * Function given to ExpandGeneric() to obtain the possible arguments of the
2784 * ":behave {mswin,xterm}" command.
2785 */
2786 static char_u *
2787get_behave_arg(expand_T *xp UNUSED, int idx)
2788{
2789 if (idx == 0)
2790 return (char_u *)"mswin";
2791 if (idx == 1)
2792 return (char_u *)"xterm";
2793 return NULL;
2794}
2795
K.Takata161b6ac2022-11-14 15:31:07 +00002796#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002797/*
2798 * Function given to ExpandGeneric() to obtain the possible arguments of the
2799 * ":breakadd {expr, file, func, here}" command.
2800 * ":breakdel {func, file, here}" command.
2801 */
2802 static char_u *
2803get_breakadd_arg(expand_T *xp UNUSED, int idx)
2804{
2805 char *opts[] = {"expr", "file", "func", "here"};
2806
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002807 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002808 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002809 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002810 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2811 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002812 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2813 {
2814 // breakdel {func, file, here}
2815 if (idx <= 2)
2816 return (char_u *)opts[idx + 1];
2817 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002818 else
2819 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002820 // profdel {func, file}
2821 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002822 return (char_u *)opts[idx + 1];
2823 }
2824 }
2825 return NULL;
2826}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002827
2828/*
2829 * Function given to ExpandGeneric() to obtain the possible arguments for the
2830 * ":scriptnames" command.
2831 */
2832 static char_u *
2833get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2834{
2835 scriptitem_T *si;
2836
2837 if (!SCRIPT_ID_VALID(idx + 1))
2838 return NULL;
2839
2840 si = SCRIPT_ITEM(idx + 1);
2841 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2842 return NameBuff;
2843}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002844#endif
2845
Bram Moolenaard0190392019-08-23 21:17:35 +02002846/*
2847 * Function given to ExpandGeneric() to obtain the possible arguments of the
2848 * ":messages {clear}" command.
2849 */
2850 static char_u *
2851get_messages_arg(expand_T *xp UNUSED, int idx)
2852{
2853 if (idx == 0)
2854 return (char_u *)"clear";
2855 return NULL;
2856}
2857
2858 static char_u *
2859get_mapclear_arg(expand_T *xp UNUSED, int idx)
2860{
2861 if (idx == 0)
2862 return (char_u *)"<buffer>";
2863 return NULL;
2864}
2865
2866/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002867 * Do the expansion based on xp->xp_context and 'rmp'.
2868 */
2869 static int
2870ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002871 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002872 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002873 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002874 char_u ***matches,
2875 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002876{
2877 static struct expgen
2878 {
2879 int context;
2880 char_u *((*func)(expand_T *, int));
2881 int ic;
2882 int escaped;
2883 } tab[] =
2884 {
2885 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2886 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2887 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2888 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2889 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2890 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2891 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2892 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2893 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2894 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002895#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002896 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2897 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2898 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2899 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2900 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002901#endif
2902#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002903 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2904 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002905#endif
2906#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002907 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002908#endif
2909#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002910 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002911#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002912 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2913 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2914 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002915#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002916 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002917#endif
2918#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002919 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002920#endif
2921#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002922 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002923#endif
2924#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002925 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2926 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002927#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002928 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2929 {EXPAND_USER, get_users, TRUE, FALSE},
2930 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002931#ifdef FEAT_EVAL
2932 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002933 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002934#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002935 };
2936 int i;
2937 int ret = FAIL;
2938
2939 // Find a context in the table and call the ExpandGeneric() with the
2940 // right function to do the expansion.
2941 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2942 {
2943 if (xp->xp_context == tab[i].context)
2944 {
2945 if (tab[i].ic)
2946 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002947 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2948 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002949 break;
2950 }
2951 }
2952
2953 return ret;
2954}
2955
2956/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002957 * Map wild expand options to flags for expand_wildcards()
2958 */
2959 static int
2960map_wildopts_to_ewflags(int options)
2961{
2962 int flags;
2963
2964 flags = EW_DIR; // include directories
2965 if (options & WILD_LIST_NOTFOUND)
2966 flags |= EW_NOTFOUND;
2967 if (options & WILD_ADD_SLASH)
2968 flags |= EW_ADDSLASH;
2969 if (options & WILD_KEEP_ALL)
2970 flags |= EW_KEEPALL;
2971 if (options & WILD_SILENT)
2972 flags |= EW_SILENT;
2973 if (options & WILD_NOERROR)
2974 flags |= EW_NOERROR;
2975 if (options & WILD_ALLLINKS)
2976 flags |= EW_ALLLINKS;
2977
2978 return flags;
2979}
2980
2981/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002982 * Do the expansion based on xp->xp_context and "pat".
2983 */
2984 static int
2985ExpandFromContext(
2986 expand_T *xp,
2987 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002988 char_u ***matches,
2989 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002990 int options) // WILD_ flags
2991{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002992 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002993 int ret;
2994 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002995 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002996 int fuzzy = cmdline_fuzzy_complete(pat)
2997 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002998
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002999 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003000
3001 if (xp->xp_context == EXPAND_FILES
3002 || xp->xp_context == EXPAND_DIRECTORIES
3003 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003004 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3005 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003006
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003007 *matches = (char_u **)"";
3008 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003009 if (xp->xp_context == EXPAND_HELP)
3010 {
3011 // With an empty argument we would get all the help tags, which is
3012 // very slow. Get matches for "help" instead.
3013 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003014 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003015 {
3016#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003017 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003018#endif
3019 return OK;
3020 }
3021 return FAIL;
3022 }
3023
Bram Moolenaar66b51422019-08-18 21:44:12 +02003024 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003025 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003026 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003027 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003028 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003029 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003030#ifdef FEAT_DIFF
3031 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003032 return ExpandBufnames(pat, numMatches, matches,
3033 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003034#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003035 if (xp->xp_context == EXPAND_TAGS
3036 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003037 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3038 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003039 if (xp->xp_context == EXPAND_COLORS)
3040 {
3041 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003042 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003043 directories);
3044 }
3045 if (xp->xp_context == EXPAND_COMPILER)
3046 {
3047 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003048 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003049 }
3050 if (xp->xp_context == EXPAND_OWNSYNTAX)
3051 {
3052 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003053 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003054 }
3055 if (xp->xp_context == EXPAND_FILETYPE)
3056 {
3057 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003058 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003059 }
K.Takata161b6ac2022-11-14 15:31:07 +00003060#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003061 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003062 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003063#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003064 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003065 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003066 if (xp->xp_context == EXPAND_RUNTIME)
3067 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003068
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003069 // When expanding a function name starting with s:, match the <SNR>nr_
3070 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003071 if ((xp->xp_context == EXPAND_USER_FUNC
3072 || xp->xp_context == EXPAND_DISASSEMBLE)
3073 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003074 {
3075 int len = (int)STRLEN(pat) + 20;
3076
3077 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003078 if (tofree == NULL)
3079 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003080 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003081 pat = tofree;
3082 }
3083
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003084 if (!fuzzy)
3085 {
3086 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3087 if (regmatch.regprog == NULL)
3088 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003089
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003090 // set ignore-case according to p_ic, p_scs and pat
3091 regmatch.rm_ic = ignorecase(pat);
3092 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003093
3094 if (xp->xp_context == EXPAND_SETTINGS
3095 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003096 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003097 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003098 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003099#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003100 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003101 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003102#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003103 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003104 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003105
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003106 if (!fuzzy)
3107 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003108 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003109
3110 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111}
3112
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113/*
3114 * Expand a list of names.
3115 *
3116 * Generic function for command line completion. It calls a function to
3117 * obtain strings, one by one. The strings are matched against a regexp
3118 * program. Matching strings are copied into an array, which is returned.
3119 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003120 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3121 * is used.
3122 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003123 * Returns OK when no problems encountered, FAIL for error (out of memory).
3124 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003125 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003126ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003127 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128 expand_T *xp,
3129 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003130 char_u ***matches,
3131 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003132 char_u *((*func)(expand_T *, int)),
3133 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003134 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003135{
3136 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003137 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003139 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003140 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003141 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003142 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003143 int sort_matches = FALSE;
3144 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003145
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003146 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003147 *matches = NULL;
3148 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003149
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003150 if (!fuzzy)
3151 ga_init2(&ga, sizeof(char *), 30);
3152 else
3153 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3154
3155 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003156 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003157 str = (*func)(xp, i);
3158 if (str == NULL) // end of list
3159 break;
3160 if (*str == NUL) // skip empty strings
3161 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003162
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003163 if (xp->xp_pattern[0] != NUL)
3164 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003165 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003166 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003167 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003168 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003169 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003170 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003171 }
3172 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003173 else
3174 match = TRUE;
3175
3176 if (!match)
3177 continue;
3178
3179 if (escaped)
3180 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3181 else
3182 str = vim_strsave(str);
3183 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003184 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003185 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003186 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003187 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003188 return FAIL;
3189 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003190 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003191 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003192 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003193
3194 if (ga_grow(&ga, 1) == FAIL)
3195 {
3196 vim_free(str);
3197 break;
3198 }
3199
3200 if (fuzzy)
3201 {
3202 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3203 fuzmatch->idx = ga.ga_len;
3204 fuzmatch->str = str;
3205 fuzmatch->score = score;
3206 }
3207 else
3208 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3209
K.Takata161b6ac2022-11-14 15:31:07 +00003210#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003211 if (func == get_menu_names)
3212 {
3213 // test for separator added by get_menu_names()
3214 str += STRLEN(str) - 1;
3215 if (*str == '\001')
3216 *str = '.';
3217 }
K.Takata161b6ac2022-11-14 15:31:07 +00003218#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003219
3220 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003221 }
3222
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003223 if (ga.ga_len == 0)
3224 return OK;
3225
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003226 // sort the matches when using regular expression matching and sorting
3227 // applies to the completion context. Menus and scriptnames should be kept
3228 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003229 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003230 && xp->xp_context != EXPAND_MENUS
3231 && xp->xp_context != EXPAND_SCRIPTNAMES)
3232 sort_matches = TRUE;
3233
3234 // <SNR> functions should be sorted to the end.
3235 if (xp->xp_context == EXPAND_EXPRESSION
3236 || xp->xp_context == EXPAND_FUNCTIONS
3237 || xp->xp_context == EXPAND_USER_FUNC
3238 || xp->xp_context == EXPAND_DISASSEMBLE)
3239 funcsort = TRUE;
3240
3241 // Sort the matches.
3242 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003243 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003244 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003245 // <SNR> functions should be sorted to the end.
3246 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3247 sort_func_compare);
3248 else
3249 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3250 }
3251
3252 if (!fuzzy)
3253 {
3254 *matches = ga.ga_data;
3255 *numMatches = ga.ga_len;
3256 }
3257 else
3258 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003259 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3260 funcsort) == FAIL)
3261 return FAIL;
3262 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003263 }
3264
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003265#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003266 // Reset the variables used for special highlight names expansion, so that
3267 // they don't show up when getting normal highlight names by ID.
3268 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003269#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003270
Bram Moolenaar66b51422019-08-18 21:44:12 +02003271 return OK;
3272}
3273
3274/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003275 * Expand shell command matches in one directory of $PATH.
3276 */
3277 static void
3278expand_shellcmd_onedir(
3279 char_u *buf,
3280 char_u *s,
3281 size_t l,
3282 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003283 char_u ***matches,
3284 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003285 int flags,
3286 hashtab_T *ht,
3287 garray_T *gap)
3288{
3289 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003290 hash_T hash;
3291 hashitem_T *hi;
3292
3293 vim_strncpy(buf, s, l);
3294 add_pathsep(buf);
3295 l = STRLEN(buf);
3296 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3297
3298 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003299 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003300 if (ret != OK)
3301 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003302
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003303 if (ga_grow(gap, *numMatches) == FAIL)
3304 {
3305 FreeWild(*numMatches, *matches);
3306 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003307 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003308
3309 for (int i = 0; i < *numMatches; ++i)
3310 {
3311 char_u *name = (*matches)[i];
3312
3313 if (STRLEN(name) > l)
3314 {
3315 // Check if this name was already found.
3316 hash = hash_hash(name + l);
3317 hi = hash_lookup(ht, name + l, hash);
3318 if (HASHITEM_EMPTY(hi))
3319 {
3320 // Remove the path that was prepended.
3321 STRMOVE(name, name + l);
3322 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3323 hash_add_item(ht, hi, name, hash);
3324 name = NULL;
3325 }
3326 }
3327 vim_free(name);
3328 }
3329 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003330}
3331
3332/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003333 * Complete a shell command.
3334 * Returns FAIL or OK;
3335 */
3336 static int
3337expand_shellcmd(
3338 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003339 char_u ***matches, // return: array with matches
3340 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003341 int flagsarg) // EW_ flags
3342{
3343 char_u *pat;
3344 int i;
3345 char_u *path = NULL;
3346 int mustfree = FALSE;
3347 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003348 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003349 size_t l;
3350 char_u *s, *e;
3351 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003352 int did_curdir = FALSE;
3353 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003355 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003356 if (buf == NULL)
3357 return FAIL;
3358
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003359 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003361 if (pat == NULL)
3362 {
3363 vim_free(buf);
3364 return FAIL;
3365 }
3366
Bram Moolenaar66b51422019-08-18 21:44:12 +02003367 for (i = 0; pat[i]; ++i)
3368 if (pat[i] == '\\' && pat[i + 1] == ' ')
3369 STRMOVE(pat + i, pat + i + 1);
3370
3371 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3372
3373 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3374 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3375 path = (char_u *)".";
3376 else
3377 {
3378 // For an absolute name we don't use $PATH.
3379 if (!mch_isFullName(pat))
3380 path = vim_getenv((char_u *)"PATH", &mustfree);
3381 if (path == NULL)
3382 path = (char_u *)"";
3383 }
3384
3385 // Go over all directories in $PATH. Expand matches in that directory and
3386 // collect them in "ga". When "." is not in $PATH also expand for the
3387 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003388 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003389 hash_init(&found_ht);
3390 for (s = path; ; s = e)
3391 {
K.Takata161b6ac2022-11-14 15:31:07 +00003392#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003393 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003394#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003395 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003396#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003397 if (e == NULL)
3398 e = s + STRLEN(s);
3399
3400 if (*s == NUL)
3401 {
3402 if (did_curdir)
3403 break;
3404 // Find directories in the current directory, path is empty.
3405 did_curdir = TRUE;
3406 flags |= EW_DIR;
3407 }
3408 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3409 {
3410 did_curdir = TRUE;
3411 flags |= EW_DIR;
3412 }
3413 else
3414 // Do not match directories inside a $PATH item.
3415 flags &= ~EW_DIR;
3416
3417 l = e - s;
3418 if (l > MAXPATHL - 5)
3419 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003420
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003421 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003422 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003423
Bram Moolenaar66b51422019-08-18 21:44:12 +02003424 if (*e != NUL)
3425 ++e;
3426 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003427 *matches = ga.ga_data;
3428 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003429
3430 vim_free(buf);
3431 vim_free(pat);
3432 if (mustfree)
3433 vim_free(path);
3434 hash_clear(&found_ht);
3435 return OK;
3436}
3437
K.Takata161b6ac2022-11-14 15:31:07 +00003438#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003439/*
3440 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003441 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003442 */
3443 static void *
3444call_user_expand_func(
3445 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003446 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003447{
3448 cmdline_info_T *ccline = get_cmdline_info();
3449 int keep = 0;
3450 typval_T args[4];
3451 sctx_T save_current_sctx = current_sctx;
3452 char_u *pat = NULL;
3453 void *ret;
3454
3455 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3456 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003457
3458 if (ccline->cmdbuff != NULL)
3459 {
3460 keep = ccline->cmdbuff[ccline->cmdlen];
3461 ccline->cmdbuff[ccline->cmdlen] = 0;
3462 }
3463
3464 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3465
3466 args[0].v_type = VAR_STRING;
3467 args[0].vval.v_string = pat;
3468 args[1].v_type = VAR_STRING;
3469 args[1].vval.v_string = xp->xp_line;
3470 args[2].v_type = VAR_NUMBER;
3471 args[2].vval.v_number = xp->xp_col;
3472 args[3].v_type = VAR_UNKNOWN;
3473
3474 current_sctx = xp->xp_script_ctx;
3475
3476 ret = user_expand_func(xp->xp_arg, 3, args);
3477
3478 current_sctx = save_current_sctx;
3479 if (ccline->cmdbuff != NULL)
3480 ccline->cmdbuff[ccline->cmdlen] = keep;
3481
3482 vim_free(pat);
3483 return ret;
3484}
3485
3486/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003487 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3488 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003489 */
3490 static int
3491ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003492 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003493 expand_T *xp,
3494 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003495 char_u ***matches,
3496 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003497{
3498 char_u *retstr;
3499 char_u *s;
3500 char_u *e;
3501 int keep;
3502 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003503 int fuzzy;
3504 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003505 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003506
3507 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003508 *matches = NULL;
3509 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003510
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003511 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003512 if (retstr == NULL)
3513 return FAIL;
3514
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003515 if (!fuzzy)
3516 ga_init2(&ga, sizeof(char *), 3);
3517 else
3518 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3519
Bram Moolenaar66b51422019-08-18 21:44:12 +02003520 for (s = retstr; *s != NUL; s = e)
3521 {
3522 e = vim_strchr(s, '\n');
3523 if (e == NULL)
3524 e = s + STRLEN(s);
3525 keep = *e;
3526 *e = NUL;
3527
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003528 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003529 {
3530 if (!fuzzy)
3531 match = vim_regexec(regmatch, s, (colnr_T)0);
3532 else
3533 {
3534 score = fuzzy_match_str(s, pat);
3535 match = (score != 0);
3536 }
3537 }
3538 else
3539 match = TRUE; // match everything
3540
Bram Moolenaar66b51422019-08-18 21:44:12 +02003541 *e = keep;
3542
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003543 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003544 {
3545 if (ga_grow(&ga, 1) == FAIL)
3546 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003547 if (!fuzzy)
3548 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3549 else
3550 {
3551 fuzmatch_str_T *fuzmatch =
3552 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003553 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003554 fuzmatch->str = vim_strnsave(s, e - s);
3555 fuzmatch->score = score;
3556 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003557 ++ga.ga_len;
3558 }
3559
3560 if (*e != NUL)
3561 ++e;
3562 }
3563 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003564
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003565 if (ga.ga_len == 0)
3566 return OK;
3567
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003568 if (!fuzzy)
3569 {
3570 *matches = ga.ga_data;
3571 *numMatches = ga.ga_len;
3572 }
3573 else
3574 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003575 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3576 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003577 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003578 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003579 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003580 return OK;
3581}
3582
3583/*
3584 * Expand names with a list returned by a function defined by the user.
3585 */
3586 static int
3587ExpandUserList(
3588 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003589 char_u ***matches,
3590 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003591{
3592 list_T *retlist;
3593 listitem_T *li;
3594 garray_T ga;
3595
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003596 *matches = NULL;
3597 *numMatches = 0;
3598 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003599 if (retlist == NULL)
3600 return FAIL;
3601
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003602 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003603 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003604 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003605 {
3606 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3607 continue; // Skip non-string items and empty strings
3608
3609 if (ga_grow(&ga, 1) == FAIL)
3610 break;
3611
3612 ((char_u **)ga.ga_data)[ga.ga_len] =
3613 vim_strsave(li->li_tv.vval.v_string);
3614 ++ga.ga_len;
3615 }
3616 list_unref(retlist);
3617
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003618 *matches = ga.ga_data;
3619 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003620 return OK;
3621}
K.Takata161b6ac2022-11-14 15:31:07 +00003622#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003623
3624/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003625 * Expand "file" for all comma-separated directories in "path".
3626 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003627 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628 */
3629 void
3630globpath(
3631 char_u *path,
3632 char_u *file,
3633 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003634 int expand_options,
3635 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003636{
3637 expand_T xpc;
3638 char_u *buf;
3639 int i;
3640 int num_p;
3641 char_u **p;
3642
3643 buf = alloc(MAXPATHL);
3644 if (buf == NULL)
3645 return;
3646
3647 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003648 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003649
3650 // Loop over all entries in {path}.
3651 while (*path != NUL)
3652 {
3653 // Copy one item of the path to buf[] and concatenate the file name.
3654 copy_option_part(&path, buf, MAXPATHL, ",");
3655 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3656 {
K.Takata161b6ac2022-11-14 15:31:07 +00003657#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003658 // Using the platform's path separator (\) makes vim incorrectly
3659 // treat it as an escape character, use '/' instead.
3660 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3661 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003662#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003663 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003664#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003665 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003666 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003667 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3668 {
3669 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3670
3671 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003672 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003673 for (i = 0; i < num_p; ++i)
3674 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003675 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003676 ++ga->ga_len;
3677 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003678 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003679 }
3680 }
3681 }
3682
3683 vim_free(buf);
3684}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003685
Bram Moolenaareadee482020-09-04 15:37:31 +02003686/*
3687 * Translate some keys pressed when 'wildmenu' is used.
3688 */
3689 int
3690wildmenu_translate_key(
3691 cmdline_info_T *cclp,
3692 int key,
3693 expand_T *xp,
3694 int did_wild_list)
3695{
3696 int c = key;
3697
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003698 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003699 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003700 // When the popup menu is used for cmdline completion:
3701 // Up : go to the previous item in the menu
3702 // Down : go to the next item in the menu
3703 // Left : go to the parent directory
3704 // Right: list the files in the selected directory
3705 switch (c)
3706 {
3707 case K_UP: c = K_LEFT; break;
3708 case K_DOWN: c = K_RIGHT; break;
3709 case K_LEFT: c = K_UP; break;
3710 case K_RIGHT: c = K_DOWN; break;
3711 default: break;
3712 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003713 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003714
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003715 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003716 {
3717 if (c == K_LEFT)
3718 c = Ctrl_P;
3719 else if (c == K_RIGHT)
3720 c = Ctrl_N;
3721 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003722
Bram Moolenaareadee482020-09-04 15:37:31 +02003723 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003724 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003725 && cclp->cmdpos > 1
3726 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3727 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3728 && (c == '\n' || c == '\r' || c == K_KENTER))
3729 c = K_DOWN;
3730
3731 return c;
3732}
3733
3734/*
3735 * Delete characters on the command line, from "from" to the current
3736 * position.
3737 */
3738 static void
3739cmdline_del(cmdline_info_T *cclp, int from)
3740{
3741 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3742 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3743 cclp->cmdlen -= cclp->cmdpos - from;
3744 cclp->cmdpos = from;
3745}
3746
3747/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003748 * Handle a key pressed when the wild menu for the menu names
3749 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003750 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003751 static int
3752wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003753{
Bram Moolenaareadee482020-09-04 15:37:31 +02003754 int i;
3755 int j;
3756
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003757 // Hitting <Down> after "emenu Name.": complete submenu
3758 if (key == K_DOWN && cclp->cmdpos > 0
3759 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003760 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003761 key = p_wc;
3762 KeyTyped = TRUE; // in case the key was mapped
3763 }
3764 else if (key == K_UP)
3765 {
3766 // Hitting <Up>: Remove one submenu name in front of the
3767 // cursor
3768 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003769
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003770 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3771 i = 0;
3772 while (--j > 0)
3773 {
3774 // check for start of menu name
3775 if (cclp->cmdbuff[j] == ' '
3776 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003777 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003778 i = j + 1;
3779 break;
3780 }
3781 // check for start of submenu name
3782 if (cclp->cmdbuff[j] == '.'
3783 && cclp->cmdbuff[j - 1] != '\\')
3784 {
3785 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003786 {
3787 i = j + 1;
3788 break;
3789 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003790 else
3791 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003792 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003793 }
3794 if (i > 0)
3795 cmdline_del(cclp, i);
3796 key = p_wc;
3797 KeyTyped = TRUE; // in case the key was mapped
3798 xp->xp_context = EXPAND_NOTHING;
3799 }
3800
3801 return key;
3802}
3803
3804/*
3805 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3806 * directory names (EXPAND_DIRECTORIES) or shell command names
3807 * (EXPAND_SHELLCMD) is displayed.
3808 */
3809 static int
3810wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3811{
3812 int i;
3813 int j;
3814 char_u upseg[5];
3815
3816 upseg[0] = PATHSEP;
3817 upseg[1] = '.';
3818 upseg[2] = '.';
3819 upseg[3] = PATHSEP;
3820 upseg[4] = NUL;
3821
3822 if (key == K_DOWN
3823 && cclp->cmdpos > 0
3824 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3825 && (cclp->cmdpos < 3
3826 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3827 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3828 {
3829 // go down a directory
3830 key = p_wc;
3831 KeyTyped = TRUE; // in case the key was mapped
3832 }
3833 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3834 {
3835 // If in a direct ancestor, strip off one ../ to go down
3836 int found = FALSE;
3837
3838 j = cclp->cmdpos;
3839 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3840 while (--j > i)
3841 {
3842 if (has_mbyte)
3843 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3844 if (vim_ispathsep(cclp->cmdbuff[j]))
3845 {
3846 found = TRUE;
3847 break;
3848 }
3849 }
3850 if (found
3851 && cclp->cmdbuff[j - 1] == '.'
3852 && cclp->cmdbuff[j - 2] == '.'
3853 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3854 {
3855 cmdline_del(cclp, j - 2);
3856 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003857 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003858 }
3859 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003860 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003861 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003862 // go up a directory
3863 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003864
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003865 j = cclp->cmdpos - 1;
3866 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3867 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003868 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003869 if (has_mbyte)
3870 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3871 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003872#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003873 && vim_strchr((char_u *)" *?[{`$%#",
3874 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003875#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003876 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003877 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003878 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003879 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003880 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003881 break;
3882 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003883 else
3884 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003885 }
3886 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003887
3888 if (!found)
3889 j = i;
3890 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3891 j += 4;
3892 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3893 && j == i)
3894 j += 3;
3895 else
3896 j = 0;
3897 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003898 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003899 // TODO this is only for DOS/UNIX systems - need to put in
3900 // machine-specific stuff here and in upseg init
3901 cmdline_del(cclp, j);
3902 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003903 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003904 else if (cclp->cmdpos > i)
3905 cmdline_del(cclp, i);
3906
3907 // Now complete in the new directory. Set KeyTyped in case the
3908 // Up key came from a mapping.
3909 key = p_wc;
3910 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003911 }
3912
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003913 return key;
3914}
3915
3916/*
3917 * Handle a key pressed when the wild menu is displayed
3918 */
3919 int
3920wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3921{
3922 if (xp->xp_context == EXPAND_MENUNAMES)
3923 return wildmenu_process_key_menunames(cclp, key, xp);
3924 else if ((xp->xp_context == EXPAND_FILES
3925 || xp->xp_context == EXPAND_DIRECTORIES
3926 || xp->xp_context == EXPAND_SHELLCMD))
3927 return wildmenu_process_key_filenames(cclp, key, xp);
3928
3929 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003930}
3931
3932/*
3933 * Free expanded names when finished walking through the matches
3934 */
3935 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003936wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003937{
3938 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02003939
3940 if (!p_wmnu || wild_menu_showing == 0)
3941 return;
3942
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003943#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003944 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02003945 if (cclp->input_fn)
3946 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003947#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003948
3949 if (wild_menu_showing == WM_SCROLLED)
3950 {
3951 // Entered command line, move it up
3952 cmdline_row--;
3953 redrawcmd();
3954 }
3955 else if (save_p_ls != -1)
3956 {
3957 // restore 'laststatus' and 'winminheight'
3958 p_ls = save_p_ls;
3959 p_wmh = save_p_wmh;
3960 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003961 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003962 redrawcmd();
3963 save_p_ls = -1;
3964 }
3965 else
3966 {
3967 win_redraw_last_status(topframe);
3968 redraw_statuslines();
3969 }
3970 KeyTyped = skt;
3971 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003972#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003973 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003974 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003975#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003976}
Bram Moolenaareadee482020-09-04 15:37:31 +02003977
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003978#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003979/*
3980 * "getcompletion()" function
3981 */
3982 void
3983f_getcompletion(typval_T *argvars, typval_T *rettv)
3984{
3985 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003986 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003987 expand_T xpc;
3988 int filtered = FALSE;
3989 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003990 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003991
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003992 if (in_vim9script()
3993 && (check_for_string_arg(argvars, 0) == FAIL
3994 || check_for_string_arg(argvars, 1) == FAIL
3995 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3996 return;
3997
ii144785fe02021-11-21 12:13:56 +00003998 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003999 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004000 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004001 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004002
4003 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004004 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004005
4006 if (p_wic)
4007 options |= WILD_ICASE;
4008
4009 // For filtered results, 'wildignore' is used
4010 if (!filtered)
4011 options |= WILD_KEEP_ALL;
4012
4013 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004014 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004015 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004016 int cmdline_len = (int)STRLEN(pat);
4017 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004018 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004019 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004020 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004021 else
4022 {
ii144785fe02021-11-21 12:13:56 +00004023 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004024 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004025 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004026
4027 xpc.xp_context = cmdcomplete_str_to_type(type);
4028 if (xpc.xp_context == EXPAND_NOTHING)
4029 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004030 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004031 return;
4032 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004033
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004034 if (xpc.xp_context == EXPAND_USER_DEFINED)
4035 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004036 // Must be "custom,funcname" pattern
4037 if (STRNCMP(type, "custom,", 7) != 0)
4038 {
4039 semsg(_(e_invalid_argument_str), type);
4040 return;
4041 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004042
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004043 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004044 }
4045
4046 if (xpc.xp_context == EXPAND_USER_LIST)
4047 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004048 // Must be "customlist,funcname" pattern
4049 if (STRNCMP(type, "customlist,", 11) != 0)
4050 {
4051 semsg(_(e_invalid_argument_str), type);
4052 return;
4053 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004054
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004055 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004056 }
4057
Bram Moolenaar66b51422019-08-18 21:44:12 +02004058# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004059 if (xpc.xp_context == EXPAND_MENUS)
4060 {
4061 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4062 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4063 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004064# endif
4065# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004066 if (xpc.xp_context == EXPAND_CSCOPE)
4067 {
4068 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4069 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4070 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004071# endif
4072# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004073 if (xpc.xp_context == EXPAND_SIGN)
4074 {
4075 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4076 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4077 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004078# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004079 if (xpc.xp_context == EXPAND_RUNTIME)
4080 {
4081 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4082 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4083 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004084 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004085
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004086 if (cmdline_fuzzy_completion_supported(&xpc))
4087 // when fuzzy matching, don't modify the search string
4088 pat = vim_strsave(xpc.xp_pattern);
4089 else
4090 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4091
Bram Moolenaar93a10962022-06-16 11:42:09 +01004092 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004093 {
4094 int i;
4095
4096 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4097
4098 for (i = 0; i < xpc.xp_numfiles; i++)
4099 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4100 }
4101 vim_free(pat);
4102 ExpandCleanup(&xpc);
4103}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004104#endif // FEAT_EVAL