blob: 09691066e09d5011b25376b7be4a3940e1619da5 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000018static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000020 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000022static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000026static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020028#endif
29
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000030// "compl_match_array" points the currently displayed list of entries in the
31// popup menu. It is NULL when there is no popup menu.
32static pumitem_T *compl_match_array = NULL;
33static int compl_match_arraysize;
34// First column in cmdline of the matched item for completion.
35static int compl_startcol;
36static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
zeertzjqc51a3762022-12-10 10:22:29 +000038#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
52 && xp->xp_context != EXPAND_FILES
53 && xp->xp_context != EXPAND_FILES_IN_PATH
54 && xp->xp_context != EXPAND_FILETYPE
55 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
57 && xp->xp_context != EXPAND_OWNSYNTAX
58 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000059 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060 && xp->xp_context != EXPAND_SHELLCMD
61 && xp->xp_context != EXPAND_TAGS
62 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_USER_LIST);
64}
65
66/*
67 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000068 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
69 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000070 */
71 int
72cmdline_fuzzy_complete(char_u *fuzzystr)
73{
74 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
75}
76
77/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000078 * sort function for the completion matches.
79 * <SNR> functions should be sorted to the end.
80 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020081 static int
82sort_func_compare(const void *s1, const void *s2)
83{
84 char_u *p1 = *(char_u **)s1;
85 char_u *p2 = *(char_u **)s2;
86
87 if (*p1 != '<' && *p2 == '<') return -1;
88 if (*p1 == '<' && *p2 != '<') return 1;
89 return STRCMP(p1, p2);
90}
Bram Moolenaar66b51422019-08-18 21:44:12 +020091
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000092/*
93 * Escape special characters in the cmdline completion matches.
94 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020095 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000096wildescape(
97 expand_T *xp,
98 char_u *str,
99 int numfiles,
100 char_u **files)
101{
102 char_u *p;
103 int vse_what = xp->xp_context == EXPAND_BUFFERS
104 ? VSE_BUFFER : VSE_NONE;
105
106 if (xp->xp_context == EXPAND_FILES
107 || xp->xp_context == EXPAND_FILES_IN_PATH
108 || xp->xp_context == EXPAND_SHELLCMD
109 || xp->xp_context == EXPAND_BUFFERS
110 || xp->xp_context == EXPAND_DIRECTORIES)
111 {
112 // Insert a backslash into a file name before a space, \, %, #
113 // and wildmatch characters, except '~'.
114 for (int i = 0; i < numfiles; ++i)
115 {
116 // for ":set path=" we need to escape spaces twice
117 if (xp->xp_backslash == XP_BS_THREE)
118 {
119 p = vim_strsave_escaped(files[i], (char_u *)" ");
120 if (p != NULL)
121 {
122 vim_free(files[i]);
123 files[i] = p;
124#if defined(BACKSLASH_IN_FILENAME)
125 p = vim_strsave_escaped(files[i], (char_u *)" ");
126 if (p != NULL)
127 {
128 vim_free(files[i]);
129 files[i] = p;
130 }
131#endif
132 }
133 }
134#ifdef BACKSLASH_IN_FILENAME
135 p = vim_strsave_fnameescape(files[i], vse_what);
136#else
137 p = vim_strsave_fnameescape(files[i],
138 xp->xp_shell ? VSE_SHELL : vse_what);
139#endif
140 if (p != NULL)
141 {
142 vim_free(files[i]);
143 files[i] = p;
144 }
145
146 // If 'str' starts with "\~", replace "~" at start of
147 // files[i] with "\~".
148 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
149 escape_fname(&files[i]);
150 }
151 xp->xp_backslash = XP_BS_NONE;
152
153 // If the first file starts with a '+' escape it. Otherwise it
154 // could be seen as "+cmd".
155 if (*files[0] == '+')
156 escape_fname(&files[0]);
157 }
158 else if (xp->xp_context == EXPAND_TAGS)
159 {
160 // Insert a backslash before characters in a tag name that
161 // would terminate the ":tag" command.
162 for (int i = 0; i < numfiles; ++i)
163 {
164 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
165 if (p != NULL)
166 {
167 vim_free(files[i]);
168 files[i] = p;
169 }
170 }
171 }
172}
173
174/*
175 * Escape special characters in the cmdline completion matches.
176 */
177 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200178ExpandEscape(
179 expand_T *xp,
180 char_u *str,
181 int numfiles,
182 char_u **files,
183 int options)
184{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200185 // May change home directory back to "~"
186 if (options & WILD_HOME_REPLACE)
187 tilde_replace(str, numfiles, files);
188
189 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000190 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200191}
192
193/*
194 * Return FAIL if this is not an appropriate context in which to do
195 * completion of anything, return OK if it is (even if there are no matches).
196 * For the caller, this means that the character is just passed through like a
197 * normal character (instead of being expanded). This allows :s/^I^D etc.
198 */
199 int
200nextwild(
201 expand_T *xp,
202 int type,
203 int options, // extra options for ExpandOne()
204 int escape) // if TRUE, escape the returned matches
205{
206 cmdline_info_T *ccline = get_cmdline_info();
207 int i, j;
208 char_u *p1;
209 char_u *p2;
210 int difflen;
211 int v;
212
213 if (xp->xp_numfiles == -1)
214 {
215 set_expand_context(xp);
216 cmd_showtail = expand_showtail(xp);
217 }
218
219 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
220 {
221 beep_flush();
222 return OK; // Something illegal on command line
223 }
224 if (xp->xp_context == EXPAND_NOTHING)
225 {
226 // Caller can use the character as a normal char instead
227 return FAIL;
228 }
229
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000230 // If cmd_silent is set then don't show the dots, because redrawcmd() below
231 // won't remove them.
232 if (!cmd_silent)
233 {
234 msg_puts("..."); // show that we are busy
235 out_flush();
236 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200237
238 i = (int)(xp->xp_pattern - ccline->cmdbuff);
239 xp->xp_pattern_len = ccline->cmdpos - i;
240
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000241 if (type == WILD_NEXT || type == WILD_PREV
242 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200243 {
244 // Get next/previous match for a previous expanded pattern.
245 p2 = ExpandOne(xp, NULL, NULL, 0, type);
246 }
247 else
248 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000249 if (cmdline_fuzzy_completion_supported(xp))
250 // If fuzzy matching, don't modify the search string
251 p1 = vim_strsave(xp->xp_pattern);
252 else
253 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
254
Bram Moolenaar66b51422019-08-18 21:44:12 +0200255 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000256 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200257 p2 = NULL;
258 else
259 {
260 int use_options = options |
261 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
262 if (escape)
263 use_options |= WILD_ESCAPE;
264
265 if (p_wic)
266 use_options += WILD_ICASE;
267 p2 = ExpandOne(xp, p1,
268 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
269 use_options, type);
270 vim_free(p1);
271 // longest match: make sure it is not shorter, happens with :help
272 if (p2 != NULL && type == WILD_LONGEST)
273 {
274 for (j = 0; j < xp->xp_pattern_len; ++j)
275 if (ccline->cmdbuff[i + j] == '*'
276 || ccline->cmdbuff[i + j] == '?')
277 break;
278 if ((int)STRLEN(p2) < j)
279 VIM_CLEAR(p2);
280 }
281 }
282 }
283
284 if (p2 != NULL && !got_int)
285 {
286 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
287 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
288 {
289 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
290 xp->xp_pattern = ccline->cmdbuff + i;
291 }
292 else
293 v = OK;
294 if (v == OK)
295 {
296 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
297 &ccline->cmdbuff[ccline->cmdpos],
298 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
299 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
300 ccline->cmdlen += difflen;
301 ccline->cmdpos += difflen;
302 }
303 }
304 vim_free(p2);
305
306 redrawcmd();
307 cursorcmd();
308
309 // When expanding a ":map" command and no matches are found, assume that
310 // the key is supposed to be inserted literally
311 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
312 return FAIL;
313
314 if (xp->xp_numfiles <= 0 && p2 == NULL)
315 beep_flush();
316 else if (xp->xp_numfiles == 1)
317 // free expanded pattern
318 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
319
320 return OK;
321}
322
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000323/*
324 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000325 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000326 */
327 static int
328cmdline_pum_create(
329 cmdline_info_T *ccline,
330 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000331 char_u **matches,
332 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000333 int showtail)
334{
335 int i;
336 int columns;
337
338 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000339 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000340 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000341 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000342 {
zeertzjqc51a3762022-12-10 10:22:29 +0000343 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000344 compl_match_array[i].pum_info = NULL;
345 compl_match_array[i].pum_extra = NULL;
346 compl_match_array[i].pum_kind = NULL;
347 }
348
349 // Compute the popup menu starting column
350 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
351 columns = vim_strsize(xp->xp_pattern);
352 if (showtail)
353 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000354 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 }
357 if (columns >= compl_startcol)
358 compl_startcol = 0;
359 else
360 compl_startcol -= columns;
361
362 // no default selection
363 compl_selected = -1;
364
365 cmdline_pum_display();
366
367 return EXPAND_OK;
368}
369
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000370/*
371 * Display the cmdline completion matches in a popup menu
372 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000373 void
374cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000375{
376 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
377}
378
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000379/*
380 * Returns TRUE if the cmdline completion popup menu is being displayed.
381 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000382 int
383cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000384{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100385 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000386}
387
388/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000389 * Remove the cmdline completion popup menu (if present), free the list of
390 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000391 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000392 void
393cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000394{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000395 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100396 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000397
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000398 pum_undisplay();
399 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000400 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000401 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000402 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000403 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100404
405 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
406 // as a side effect.
407 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000408}
409
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000410 void
411cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000412{
413 cmdline_pum_remove();
414 wildmenu_cleanup(cclp);
415}
416
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000417/*
418 * Returns the starting column number to use for the cmdline completion popup
419 * menu.
420 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000421 int
422cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423{
424 return compl_startcol;
425}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426
Bram Moolenaar66b51422019-08-18 21:44:12 +0200427/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000428 * Return the number of characters that should be skipped in a status match.
429 * These are backslashes used for escaping. Do show backslashes in help tags.
430 */
431 static int
432skip_status_match_char(expand_T *xp, char_u *s)
433{
434 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
435#ifdef FEAT_MENU
436 || ((xp->xp_context == EXPAND_MENUS
437 || xp->xp_context == EXPAND_MENUNAMES)
438 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
439#endif
440 )
441 {
442#ifndef BACKSLASH_IN_FILENAME
443 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
444 return 2;
445#endif
446 return 1;
447 }
448 return 0;
449}
450
451/*
452 * Get the length of an item as it will be shown in the status line.
453 */
454 static int
455status_match_len(expand_T *xp, char_u *s)
456{
457 int len = 0;
458
459#ifdef FEAT_MENU
460 int emenu = xp->xp_context == EXPAND_MENUS
461 || xp->xp_context == EXPAND_MENUNAMES;
462
463 // Check for menu separators - replace with '|'.
464 if (emenu && menu_is_separator(s))
465 return 1;
466#endif
467
468 while (*s != NUL)
469 {
470 s += skip_status_match_char(xp, s);
471 len += ptr2cells(s);
472 MB_PTR_ADV(s);
473 }
474
475 return len;
476}
477
478/*
479 * Show wildchar matches in the status line.
480 * Show at least the "match" item.
481 * We start at item 'first_match' in the list and show all matches that fit.
482 *
483 * If inversion is possible we use it. Else '=' characters are used.
484 */
485 static void
486win_redr_status_matches(
487 expand_T *xp,
488 int num_matches,
489 char_u **matches, // list of matches
490 int match,
491 int showtail)
492{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000493 int row;
494 char_u *buf;
495 int len;
496 int clen; // length in screen cells
497 int fillchar;
498 int attr;
499 int i;
500 int highlight = TRUE;
501 char_u *selstart = NULL;
502 int selstart_col = 0;
503 char_u *selend = NULL;
504 static int first_match = 0;
505 int add_left = FALSE;
506 char_u *s;
507#ifdef FEAT_MENU
508 int emenu;
509#endif
510 int l;
511
512 if (matches == NULL) // interrupted completion?
513 return;
514
515 if (has_mbyte)
516 buf = alloc(Columns * MB_MAXBYTES + 1);
517 else
518 buf = alloc(Columns + 1);
519 if (buf == NULL)
520 return;
521
522 if (match == -1) // don't show match but original text
523 {
524 match = 0;
525 highlight = FALSE;
526 }
527 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000528 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000529 if (match == 0)
530 first_match = 0;
531 else if (match < first_match)
532 {
533 // jumping left, as far as we can go
534 first_match = match;
535 add_left = TRUE;
536 }
537 else
538 {
539 // check if match fits on the screen
540 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000541 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000542 if (first_match > 0)
543 clen += 2;
544 // jumping right, put match at the left
545 if ((long)clen > Columns)
546 {
547 first_match = match;
548 // if showing the last match, we can add some on the left
549 clen = 2;
550 for (i = match; i < num_matches; ++i)
551 {
zeertzjqc51a3762022-12-10 10:22:29 +0000552 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000553 if ((long)clen >= Columns)
554 break;
555 }
556 if (i == num_matches)
557 add_left = TRUE;
558 }
559 }
560 if (add_left)
561 while (first_match > 0)
562 {
zeertzjqc51a3762022-12-10 10:22:29 +0000563 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000564 if ((long)clen >= Columns)
565 break;
566 --first_match;
567 }
568
569 fillchar = fillchar_status(&attr, curwin);
570
571 if (first_match == 0)
572 {
573 *buf = NUL;
574 len = 0;
575 }
576 else
577 {
578 STRCPY(buf, "< ");
579 len = 2;
580 }
581 clen = len;
582
583 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000584 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000585 {
586 if (i == match)
587 {
588 selstart = buf + len;
589 selstart_col = clen;
590 }
591
zeertzjqc51a3762022-12-10 10:22:29 +0000592 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000593 // Check for menu separators - replace with '|'
594#ifdef FEAT_MENU
595 emenu = (xp->xp_context == EXPAND_MENUS
596 || xp->xp_context == EXPAND_MENUNAMES);
597 if (emenu && menu_is_separator(s))
598 {
599 STRCPY(buf + len, transchar('|'));
600 l = (int)STRLEN(buf + len);
601 len += l;
602 clen += l;
603 }
604 else
605#endif
606 for ( ; *s != NUL; ++s)
607 {
608 s += skip_status_match_char(xp, s);
609 clen += ptr2cells(s);
610 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
611 {
612 STRNCPY(buf + len, s, l);
613 s += l - 1;
614 len += l;
615 }
616 else
617 {
618 STRCPY(buf + len, transchar_byte(*s));
619 len += (int)STRLEN(buf + len);
620 }
621 }
622 if (i == match)
623 selend = buf + len;
624
625 *(buf + len++) = ' ';
626 *(buf + len++) = ' ';
627 clen += 2;
628 if (++i == num_matches)
629 break;
630 }
631
632 if (i != num_matches)
633 {
634 *(buf + len++) = '>';
635 ++clen;
636 }
637
638 buf[len] = NUL;
639
640 row = cmdline_row - 1;
641 if (row >= 0)
642 {
643 if (wild_menu_showing == 0)
644 {
645 if (msg_scrolled > 0)
646 {
647 // Put the wildmenu just above the command line. If there is
648 // no room, scroll the screen one line up.
649 if (cmdline_row == Rows - 1)
650 {
651 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
652 ++msg_scrolled;
653 }
654 else
655 {
656 ++cmdline_row;
657 ++row;
658 }
659 wild_menu_showing = WM_SCROLLED;
660 }
661 else
662 {
663 // Create status line if needed by setting 'laststatus' to 2.
664 // Set 'winminheight' to zero to avoid that the window is
665 // resized.
666 if (lastwin->w_status_height == 0)
667 {
668 save_p_ls = p_ls;
669 save_p_wmh = p_wmh;
670 p_ls = 2;
671 p_wmh = 0;
672 last_status(FALSE);
673 }
674 wild_menu_showing = WM_SHOWN;
675 }
676 }
677
678 screen_puts(buf, row, 0, attr);
679 if (selstart != NULL && highlight)
680 {
681 *selend = NUL;
682 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
683 }
684
685 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
686 }
687
688 win_redraw_last_status(topframe);
689 vim_free(buf);
690}
691
692/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000693 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000694 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000695 */
696 static char_u *
697get_next_or_prev_match(
698 int mode,
699 expand_T *xp,
700 int *p_findex,
701 char_u *orig_save)
702{
703 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000704 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000705
706 if (xp->xp_numfiles <= 0)
707 return NULL;
708
709 if (mode == WILD_PREV)
710 {
711 if (findex == -1)
712 findex = xp->xp_numfiles;
713 --findex;
714 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000715 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000716 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000717 else if (mode == WILD_PAGEUP)
718 {
719 if (findex == 0)
720 // at the first entry, don't select any entries
721 findex = -1;
722 else if (findex == -1)
723 // no entry is selected. select the last entry
724 findex = xp->xp_numfiles - 1;
725 else
726 {
727 // go up by the pum height
728 ht = pum_get_height();
729 if (ht > 3)
730 ht -= 2;
731 findex -= ht;
732 if (findex < 0)
733 // few entries left, select the first entry
734 findex = 0;
735 }
736 }
737 else // mode == WILD_PAGEDOWN
738 {
739 if (findex == xp->xp_numfiles - 1)
740 // at the last entry, don't select any entries
741 findex = -1;
742 else if (findex == -1)
743 // no entry is selected. select the first entry
744 findex = 0;
745 else
746 {
747 // go down by the pum height
748 ht = pum_get_height();
749 if (ht > 3)
750 ht -= 2;
751 findex += ht;
752 if (findex >= xp->xp_numfiles)
753 // few entries left, select the last entry
754 findex = xp->xp_numfiles - 1;
755 }
756 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000757
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000758 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000759 if (findex < 0)
760 {
761 if (orig_save == NULL)
762 findex = xp->xp_numfiles - 1;
763 else
764 findex = -1;
765 }
766 if (findex >= xp->xp_numfiles)
767 {
768 if (orig_save == NULL)
769 findex = 0;
770 else
771 findex = -1;
772 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000773 if (compl_match_array)
774 {
775 compl_selected = findex;
776 cmdline_pum_display();
777 }
778 else if (p_wmnu)
779 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
780 findex, cmd_showtail);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000781 *p_findex = findex;
782
783 if (findex == -1)
784 return vim_strsave(orig_save);
785
786 return vim_strsave(xp->xp_files[findex]);
787}
788
789/*
790 * Start the command-line expansion and get the matches.
791 */
792 static char_u *
793ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
794{
795 int non_suf_match; // number without matching suffix
796 int i;
797 char_u *ss = NULL;
798
799 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000800 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000801 options) == FAIL)
802 {
803#ifdef FNAME_ILLEGAL
804 // Illegal file name has been silently skipped. But when there
805 // are wildcards, the real problem is that there was no match,
806 // causing the pattern to be added, which has illegal characters.
807 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
808 semsg(_(e_no_match_str_2), str);
809#endif
810 }
811 else if (xp->xp_numfiles == 0)
812 {
813 if (!(options & WILD_SILENT))
814 semsg(_(e_no_match_str_2), str);
815 }
816 else
817 {
818 // Escape the matches for use on the command line.
819 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
820
821 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000822 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000823 {
824 if (xp->xp_numfiles)
825 non_suf_match = xp->xp_numfiles;
826 else
827 non_suf_match = 1;
828 if ((xp->xp_context == EXPAND_FILES
829 || xp->xp_context == EXPAND_DIRECTORIES)
830 && xp->xp_numfiles > 1)
831 {
832 // More than one match; check suffix.
833 // The files will have been sorted on matching suffix in
834 // expand_wildcards, only need to check the first two.
835 non_suf_match = 0;
836 for (i = 0; i < 2; ++i)
837 if (match_suffix(xp->xp_files[i]))
838 ++non_suf_match;
839 }
840 if (non_suf_match != 1)
841 {
842 // Can we ever get here unless it's while expanding
843 // interactively? If not, we can get rid of this all
844 // together. Don't really want to wait for this message
845 // (and possibly have to hit return to continue!).
846 if (!(options & WILD_SILENT))
847 emsg(_(e_too_many_file_names));
848 else if (!(options & WILD_NO_BEEP))
849 beep_flush();
850 }
851 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
852 ss = vim_strsave(xp->xp_files[0]);
853 }
854 }
855
856 return ss;
857}
858
859/*
860 * Return the longest common part in the list of cmdline completion matches.
861 */
862 static char_u *
863find_longest_match(expand_T *xp, int options)
864{
865 long_u len;
866 int mb_len = 1;
867 int c0, ci;
868 int i;
869 char_u *ss;
870
871 for (len = 0; xp->xp_files[0][len]; len += mb_len)
872 {
873 if (has_mbyte)
874 {
875 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
876 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
877 }
878 else
879 c0 = xp->xp_files[0][len];
880 for (i = 1; i < xp->xp_numfiles; ++i)
881 {
882 if (has_mbyte)
883 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
884 else
885 ci = xp->xp_files[i][len];
886 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
887 || xp->xp_context == EXPAND_FILES
888 || xp->xp_context == EXPAND_SHELLCMD
889 || xp->xp_context == EXPAND_BUFFERS))
890 {
891 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
892 break;
893 }
894 else if (c0 != ci)
895 break;
896 }
897 if (i < xp->xp_numfiles)
898 {
899 if (!(options & WILD_NO_BEEP))
900 vim_beep(BO_WILD);
901 break;
902 }
903 }
904
905 ss = alloc(len + 1);
906 if (ss)
907 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
908
909 return ss;
910}
911
912/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000913 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200914 * Chars that should not be expanded must be preceded with a backslash.
915 * Return a pointer to allocated memory containing the new string.
916 * Return NULL for failure.
917 *
918 * "orig" is the originally expanded string, copied to allocated memory. It
919 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
920 * WILD_PREV "orig" should be NULL.
921 *
922 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
923 * is WILD_EXPAND_FREE or WILD_ALL.
924 *
925 * mode = WILD_FREE: just free previously expanded matches
926 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
927 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
928 * mode = WILD_NEXT: use next match in multiple match, wrap to first
929 * mode = WILD_PREV: use previous match in multiple match, wrap to first
930 * mode = WILD_ALL: return all matches concatenated
931 * mode = WILD_LONGEST: return longest matched part
932 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000933 * mode = WILD_APPLY: apply the item selected in the cmdline completion
934 * popup menu and close the menu.
935 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
936 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200937 *
938 * options = WILD_LIST_NOTFOUND: list entries without a match
939 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
940 * options = WILD_USE_NL: Use '\n' for WILD_ALL
941 * options = WILD_NO_BEEP: Don't beep for multiple matches
942 * options = WILD_ADD_SLASH: add a slash after directory names
943 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
944 * options = WILD_SILENT: don't print warning messages
945 * options = WILD_ESCAPE: put backslash before special chars
946 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200947 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200948 *
949 * The variables xp->xp_context and xp->xp_backslash must have been set!
950 */
951 char_u *
952ExpandOne(
953 expand_T *xp,
954 char_u *str,
955 char_u *orig, // allocated copy of original of expanded string
956 int options,
957 int mode)
958{
959 char_u *ss = NULL;
zeertzjq094dd152023-06-15 22:51:57 +0100960 static int findex; // TODO: Move into expand_T
Bram Moolenaar66b51422019-08-18 21:44:12 +0200961 static char_u *orig_save = NULL; // kept value of orig
962 int orig_saved = FALSE;
963 int i;
964 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200965
966 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000967 if (mode == WILD_NEXT || mode == WILD_PREV
968 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000969 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200970
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000971 if (mode == WILD_CANCEL)
972 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
973 else if (mode == WILD_APPLY)
zeertzjq094dd152023-06-15 22:51:57 +0100974 ss = vim_strsave(findex == -1
975 ? (orig_save ? orig_save : (char_u *)"")
976 : xp->xp_files[findex]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000977
Bram Moolenaar66b51422019-08-18 21:44:12 +0200978 // free old names
979 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
980 {
981 FreeWild(xp->xp_numfiles, xp->xp_files);
982 xp->xp_numfiles = -1;
983 VIM_CLEAR(orig_save);
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000984
985 // The entries from xp_files may be used in the PUM, remove it.
986 if (compl_match_array != NULL)
987 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200988 }
zeertzjq094dd152023-06-15 22:51:57 +0100989 // TODO: Remove condition if "findex" is part of expand_T ?
990 if (mode != WILD_EXPAND_FREE && mode != WILD_ALL && mode != WILD_ALL_KEEP)
991 findex = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200992
993 if (mode == WILD_FREE) // only release file name
994 return NULL;
995
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000996 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200997 {
998 vim_free(orig_save);
999 orig_save = orig;
1000 orig_saved = TRUE;
1001
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001002 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001003 }
1004
1005 // Find longest common part
1006 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1007 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001008 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001009 findex = -1; // next p_wc gets first one
1010 }
1011
Bram Moolenaar57e95172022-08-20 19:26:14 +01001012 // Concatenate all matching names. Unless interrupted, this can be slow
1013 // and the result probably won't be used.
1014 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001015 {
1016 len = 0;
1017 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001018 {
1019 if (i > 0)
1020 {
1021 if (xp->xp_prefix == XP_PREFIX_NO)
1022 len += 2; // prefix "no"
1023 else if (xp->xp_prefix == XP_PREFIX_INV)
1024 len += 3; // prefix "inv"
1025 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001026 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001027 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001028 ss = alloc(len);
1029 if (ss != NULL)
1030 {
1031 *ss = NUL;
1032 for (i = 0; i < xp->xp_numfiles; ++i)
1033 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001034 if (i > 0)
1035 {
1036 if (xp->xp_prefix == XP_PREFIX_NO)
1037 STRCAT(ss, "no");
1038 else if (xp->xp_prefix == XP_PREFIX_INV)
1039 STRCAT(ss, "inv");
1040 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001041 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001042
Bram Moolenaar66b51422019-08-18 21:44:12 +02001043 if (i != xp->xp_numfiles - 1)
1044 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1045 }
1046 }
1047 }
1048
1049 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1050 ExpandCleanup(xp);
1051
1052 // Free "orig" if it wasn't stored in "orig_save".
1053 if (!orig_saved)
1054 vim_free(orig);
1055
1056 return ss;
1057}
1058
1059/*
1060 * Prepare an expand structure for use.
1061 */
1062 void
1063ExpandInit(expand_T *xp)
1064{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001065 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001066 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001067 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001068 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001069}
1070
1071/*
1072 * Cleanup an expand structure after use.
1073 */
1074 void
1075ExpandCleanup(expand_T *xp)
1076{
1077 if (xp->xp_numfiles >= 0)
1078 {
1079 FreeWild(xp->xp_numfiles, xp->xp_files);
1080 xp->xp_numfiles = -1;
1081 }
1082}
1083
1084/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001085 * Display one line of completion matches. Multiple matches are displayed in
1086 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001087 * matches - list of completion match names
1088 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001089 * lines - number of output lines
1090 * linenr - line number of matches to display
1091 * maxlen - maximum number of characters in each line
1092 * showtail - display only the tail of the full path of a file name
1093 * dir_attr - highlight attribute to use for directory names
1094 */
1095 static void
1096showmatches_oneline(
1097 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001098 char_u **matches,
1099 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001100 int lines,
1101 int linenr,
1102 int maxlen,
1103 int showtail,
1104 int dir_attr)
1105{
1106 int i, j;
1107 int isdir;
1108 int lastlen;
1109 char_u *p;
1110
1111 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001112 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001113 {
1114 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1115 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001116 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1117 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001118 msg_advance(maxlen + 1);
1119 msg_puts((char *)p);
1120 msg_advance(maxlen + 3);
1121 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1122 break;
1123 }
1124 for (i = maxlen - lastlen; --i >= 0; )
1125 msg_putchar(' ');
1126 if (xp->xp_context == EXPAND_FILES
1127 || xp->xp_context == EXPAND_SHELLCMD
1128 || xp->xp_context == EXPAND_BUFFERS)
1129 {
1130 // highlight directories
1131 if (xp->xp_numfiles != -1)
1132 {
1133 char_u *halved_slash;
1134 char_u *exp_path;
1135 char_u *path;
1136
1137 // Expansion was done before and special characters
1138 // were escaped, need to halve backslashes. Also
1139 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001140 exp_path = expand_env_save_opt(matches[j], TRUE);
1141 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001142 halved_slash = backslash_halve_save(path);
1143 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001144 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001145 vim_free(exp_path);
1146 if (halved_slash != path)
1147 vim_free(halved_slash);
1148 }
1149 else
1150 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001151 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001152 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001153 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001154 else
1155 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001156 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001157 TRUE);
1158 p = NameBuff;
1159 }
1160 }
1161 else
1162 {
1163 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001164 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001165 }
1166 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1167 }
1168 if (msg_col > 0) // when not wrapped around
1169 {
1170 msg_clr_eos();
1171 msg_putchar('\n');
1172 }
1173 out_flush(); // show one line at a time
1174}
1175
1176/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001177 * Show all matches for completion on the command line.
1178 * Returns EXPAND_NOTHING when the character that triggered expansion should
1179 * be inserted like a normal character.
1180 */
1181 int
1182showmatches(expand_T *xp, int wildmenu UNUSED)
1183{
1184 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001185 int numMatches;
1186 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001187 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001188 int maxlen;
1189 int lines;
1190 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001191 int attr;
1192 int showtail;
1193
1194 if (xp->xp_numfiles == -1)
1195 {
1196 set_expand_context(xp);
1197 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001198 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001199 showtail = expand_showtail(xp);
1200 if (i != EXPAND_OK)
1201 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001202 }
1203 else
1204 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001205 numMatches = xp->xp_numfiles;
1206 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001207 showtail = cmd_showtail;
1208 }
1209
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001210 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001211 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001212 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001213
Bram Moolenaar66b51422019-08-18 21:44:12 +02001214 if (!wildmenu)
1215 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001216 msg_didany = FALSE; // lines_left will be set
1217 msg_start(); // prepare for paging
1218 msg_putchar('\n');
1219 out_flush();
1220 cmdline_row = msg_row;
1221 msg_didany = FALSE; // lines_left will be set again
1222 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001224
1225 if (got_int)
1226 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001227 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001228 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001229 else
1230 {
1231 // find the length of the longest file name
1232 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001233 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001234 {
1235 if (!showtail && (xp->xp_context == EXPAND_FILES
1236 || xp->xp_context == EXPAND_SHELLCMD
1237 || xp->xp_context == EXPAND_BUFFERS))
1238 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001239 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001240 j = vim_strsize(NameBuff);
1241 }
1242 else
zeertzjqc51a3762022-12-10 10:22:29 +00001243 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001244 if (j > maxlen)
1245 maxlen = j;
1246 }
1247
1248 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001249 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001250 else
1251 {
1252 // compute the number of columns and lines for the listing
1253 maxlen += 2; // two spaces between file names
1254 columns = ((int)Columns + 2) / maxlen;
1255 if (columns < 1)
1256 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001257 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001258 }
1259
1260 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1261
1262 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1263 {
1264 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1265 msg_clr_eos();
1266 msg_advance(maxlen - 3);
1267 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1268 }
1269
1270 // list the files line by line
1271 for (i = 0; i < lines; ++i)
1272 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001273 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001274 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001275 if (got_int)
1276 {
1277 got_int = FALSE;
1278 break;
1279 }
1280 }
1281
1282 // we redraw the command below the lines that we have just listed
1283 // This is a bit tricky, but it saves a lot of screen updating.
1284 cmdline_row = msg_row; // will put it back later
1285 }
1286
1287 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001288 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001289
1290 return EXPAND_OK;
1291}
1292
1293/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001294 * gettail() version for showmatches() and win_redr_status_matches():
1295 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001296 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001297 static char_u *
1298showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001299{
1300 char_u *p;
1301 char_u *t = s;
1302 int had_sep = FALSE;
1303
1304 for (p = s; *p != NUL; )
1305 {
1306 if (vim_ispathsep(*p)
1307#ifdef BACKSLASH_IN_FILENAME
1308 && !rem_backslash(p)
1309#endif
1310 )
1311 had_sep = TRUE;
1312 else if (had_sep)
1313 {
1314 t = p;
1315 had_sep = FALSE;
1316 }
1317 MB_PTR_ADV(p);
1318 }
1319 return t;
1320}
1321
1322/*
1323 * Return TRUE if we only need to show the tail of completion matches.
1324 * When not completing file names or there is a wildcard in the path FALSE is
1325 * returned.
1326 */
1327 static int
1328expand_showtail(expand_T *xp)
1329{
1330 char_u *s;
1331 char_u *end;
1332
1333 // When not completing file names a "/" may mean something different.
1334 if (xp->xp_context != EXPAND_FILES
1335 && xp->xp_context != EXPAND_SHELLCMD
1336 && xp->xp_context != EXPAND_DIRECTORIES)
1337 return FALSE;
1338
1339 end = gettail(xp->xp_pattern);
1340 if (end == xp->xp_pattern) // there is no path separator
1341 return FALSE;
1342
1343 for (s = xp->xp_pattern; s < end; s++)
1344 {
1345 // Skip escaped wildcards. Only when the backslash is not a path
1346 // separator, on DOS the '*' "path\*\file" must not be skipped.
1347 if (rem_backslash(s))
1348 ++s;
1349 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1350 return FALSE;
1351 }
1352 return TRUE;
1353}
1354
1355/*
1356 * Prepare a string for expansion.
1357 * When expanding file names: The string will be used with expand_wildcards().
1358 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1359 * When expanding other names: The string will be used with regcomp(). Copy
1360 * the name into allocated memory and prepend "^".
1361 */
1362 char_u *
1363addstar(
1364 char_u *fname,
1365 int len,
1366 int context) // EXPAND_FILES etc.
1367{
1368 char_u *retval;
1369 int i, j;
1370 int new_len;
1371 char_u *tail;
1372 int ends_in_star;
1373
1374 if (context != EXPAND_FILES
1375 && context != EXPAND_FILES_IN_PATH
1376 && context != EXPAND_SHELLCMD
1377 && context != EXPAND_DIRECTORIES)
1378 {
1379 // Matching will be done internally (on something other than files).
1380 // So we convert the file-matching-type wildcards into our kind for
1381 // use with vim_regcomp(). First work out how long it will be:
1382
1383 // For help tags the translation is done in find_help_tags().
1384 // For a tag pattern starting with "/" no translation is needed.
1385 if (context == EXPAND_HELP
1386 || context == EXPAND_COLORS
1387 || context == EXPAND_COMPILER
1388 || context == EXPAND_OWNSYNTAX
1389 || context == EXPAND_FILETYPE
1390 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001391 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001392 || ((context == EXPAND_TAGS_LISTFILES
1393 || context == EXPAND_TAGS)
1394 && fname[0] == '/'))
1395 retval = vim_strnsave(fname, len);
1396 else
1397 {
1398 new_len = len + 2; // +2 for '^' at start, NUL at end
1399 for (i = 0; i < len; i++)
1400 {
1401 if (fname[i] == '*' || fname[i] == '~')
1402 new_len++; // '*' needs to be replaced by ".*"
1403 // '~' needs to be replaced by "\~"
1404
1405 // Buffer names are like file names. "." should be literal
1406 if (context == EXPAND_BUFFERS && fname[i] == '.')
1407 new_len++; // "." becomes "\."
1408
1409 // Custom expansion takes care of special things, match
1410 // backslashes literally (perhaps also for other types?)
1411 if ((context == EXPAND_USER_DEFINED
1412 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1413 new_len++; // '\' becomes "\\"
1414 }
1415 retval = alloc(new_len);
1416 if (retval != NULL)
1417 {
1418 retval[0] = '^';
1419 j = 1;
1420 for (i = 0; i < len; i++, j++)
1421 {
1422 // Skip backslash. But why? At least keep it for custom
1423 // expansion.
1424 if (context != EXPAND_USER_DEFINED
1425 && context != EXPAND_USER_LIST
1426 && fname[i] == '\\'
1427 && ++i == len)
1428 break;
1429
1430 switch (fname[i])
1431 {
1432 case '*': retval[j++] = '.';
1433 break;
1434 case '~': retval[j++] = '\\';
1435 break;
1436 case '?': retval[j] = '.';
1437 continue;
1438 case '.': if (context == EXPAND_BUFFERS)
1439 retval[j++] = '\\';
1440 break;
1441 case '\\': if (context == EXPAND_USER_DEFINED
1442 || context == EXPAND_USER_LIST)
1443 retval[j++] = '\\';
1444 break;
1445 }
1446 retval[j] = fname[i];
1447 }
1448 retval[j] = NUL;
1449 }
1450 }
1451 }
1452 else
1453 {
1454 retval = alloc(len + 4);
1455 if (retval != NULL)
1456 {
1457 vim_strncpy(retval, fname, len);
1458
1459 // Don't add a star to *, ~, ~user, $var or `cmd`.
1460 // * would become **, which walks the whole tree.
1461 // ~ would be at the start of the file name, but not the tail.
1462 // $ could be anywhere in the tail.
1463 // ` could be anywhere in the file name.
1464 // When the name ends in '$' don't add a star, remove the '$'.
1465 tail = gettail(retval);
1466 ends_in_star = (len > 0 && retval[len - 1] == '*');
1467#ifndef BACKSLASH_IN_FILENAME
1468 for (i = len - 2; i >= 0; --i)
1469 {
1470 if (retval[i] != '\\')
1471 break;
1472 ends_in_star = !ends_in_star;
1473 }
1474#endif
1475 if ((*retval != '~' || tail != retval)
1476 && !ends_in_star
1477 && vim_strchr(tail, '$') == NULL
1478 && vim_strchr(retval, '`') == NULL)
1479 retval[len++] = '*';
1480 else if (len > 0 && retval[len - 1] == '$')
1481 --len;
1482 retval[len] = NUL;
1483 }
1484 }
1485 return retval;
1486}
1487
1488/*
1489 * Must parse the command line so far to work out what context we are in.
1490 * Completion can then be done based on that context.
1491 * This routine sets the variables:
1492 * xp->xp_pattern The start of the pattern to be expanded within
1493 * the command line (ends at the cursor).
1494 * xp->xp_context The type of thing to expand. Will be one of:
1495 *
1496 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1497 * the command line, like an unknown command. Caller
1498 * should beep.
1499 * EXPAND_NOTHING Unrecognised context for completion, use char like
1500 * a normal char, rather than for completion. eg
1501 * :s/^I/
1502 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1503 * it.
1504 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1505 * EXPAND_FILES After command with EX_XFILE set, or after setting
1506 * with P_EXPAND set. eg :e ^I, :w>>^I
1507 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001508 * when we know only directories are of interest.
1509 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001510 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1511 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1512 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1513 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1514 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1515 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1516 * EXPAND_EVENTS Complete event names
1517 * EXPAND_SYNTAX Complete :syntax command arguments
1518 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1519 * EXPAND_AUGROUP Complete autocommand group names
1520 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1521 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1522 * eg :unmap a^I , :cunab x^I
1523 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1524 * eg :call sub^I
1525 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1526 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1527 * names in expressions, eg :while s^I
1528 * EXPAND_ENV_VARS Complete environment variable names
1529 * EXPAND_USER Complete user names
1530 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001531 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001532set_expand_context(expand_T *xp)
1533{
1534 cmdline_info_T *ccline = get_cmdline_info();
1535
1536 // only expansion for ':', '>' and '=' command-lines
1537 if (ccline->cmdfirstc != ':'
1538#ifdef FEAT_EVAL
1539 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1540 && !ccline->input_fn
1541#endif
1542 )
1543 {
1544 xp->xp_context = EXPAND_NOTHING;
1545 return;
1546 }
1547 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1548}
1549
Bram Moolenaard0190392019-08-23 21:17:35 +02001550/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001551 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1552 * For user defined commands, the completion context is set in 'xp' and the
1553 * completion flags in 'complp'.
1554 *
1555 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001556 */
1557 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001558set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001559{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001560 char_u *p = NULL;
1561 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001562 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001563
1564 // Isolate the command and search for it in the command table.
1565 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001566 // - the 'k' command can directly be followed by any character, but do
1567 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1568 // find matches anywhere in the command name, do this only for command
1569 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001570 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001571 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001572 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001573 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001574 p = cmd + 1;
1575 }
1576 else
1577 {
1578 p = cmd;
1579 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1580 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001581 // A user command may contain digits.
1582 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1583 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001584 while (ASCII_ISALNUM(*p) || *p == '*')
1585 ++p;
1586 // for python 3.x: ":py3*" commands completion
1587 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1588 {
1589 ++p;
1590 while (ASCII_ISALPHA(*p) || *p == '*')
1591 ++p;
1592 }
1593 // check for non-alpha command
1594 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1595 ++p;
1596 len = (int)(p - cmd);
1597
1598 if (len == 0)
1599 {
1600 xp->xp_context = EXPAND_UNSUCCESSFUL;
1601 return NULL;
1602 }
1603
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001604 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001605
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001606 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001607 // Also when doing fuzzy expansion for non-shell commands, support
1608 // alphanumeric characters.
1609 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1610 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001611 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1612 ++p;
1613 }
1614
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001615 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001616 // character, complete the command name.
1617 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1618 return NULL;
1619
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001620 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001621 {
1622 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1623 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001624 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001625 p = cmd + 1;
1626 }
1627 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1628 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001629 eap->cmd = cmd;
1630 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001631 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001632 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001633 }
1634 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001635 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001636 {
1637 // Not still touching the command and it was an illegal one
1638 xp->xp_context = EXPAND_UNSUCCESSFUL;
1639 return NULL;
1640 }
1641
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001642 return p;
1643}
Bram Moolenaard0190392019-08-23 21:17:35 +02001644
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001645/*
1646 * Set the completion context for a command argument with wild card characters.
1647 */
1648 static void
1649set_context_for_wildcard_arg(
1650 exarg_T *eap,
1651 char_u *arg,
1652 int usefilter,
1653 expand_T *xp,
1654 int *complp)
1655{
1656 char_u *p;
1657 int c;
1658 int in_quote = FALSE;
1659 char_u *bow = NULL; // Beginning of word
1660 int len = 0;
1661
1662 // Allow spaces within back-quotes to count as part of the argument
1663 // being expanded.
1664 xp->xp_pattern = skipwhite(arg);
1665 p = xp->xp_pattern;
1666 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001667 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001668 if (has_mbyte)
1669 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001670 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001671 c = *p;
1672 if (c == '\\' && p[1] != NUL)
1673 ++p;
1674 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001675 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001676 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001678 xp->xp_pattern = p;
1679 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001680 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001681 in_quote = !in_quote;
1682 }
1683 // An argument can contain just about everything, except
1684 // characters that end the command and white space.
1685 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001686#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001687 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001688#endif
1689 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001690 {
1691 len = 0; // avoid getting stuck when space is in 'isfname'
1692 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001693 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001694 if (has_mbyte)
1695 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001696 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001697 c = *p;
1698 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001699 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001700 if (has_mbyte)
1701 len = (*mb_ptr2len)(p);
1702 else
1703 len = 1;
1704 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001705 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001706 if (in_quote)
1707 bow = p;
1708 else
1709 xp->xp_pattern = p;
1710 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001711 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001712 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001713 }
1714
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001715 // If we are still inside the quotes, and we passed a space, just
1716 // expand from there.
1717 if (bow != NULL && in_quote)
1718 xp->xp_pattern = bow;
1719 xp->xp_context = EXPAND_FILES;
1720
1721 // For a shell command more chars need to be escaped.
1722 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1723 {
1724#ifndef BACKSLASH_IN_FILENAME
1725 xp->xp_shell = TRUE;
1726#endif
1727 // When still after the command name expand executables.
1728 if (xp->xp_pattern == skipwhite(arg))
1729 xp->xp_context = EXPAND_SHELLCMD;
1730 }
1731
1732 // Check for environment variable.
1733 if (*xp->xp_pattern == '$')
1734 {
1735 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1736 if (!vim_isIDc(*p))
1737 break;
1738 if (*p == NUL)
1739 {
1740 xp->xp_context = EXPAND_ENV_VARS;
1741 ++xp->xp_pattern;
1742 // Avoid that the assignment uses EXPAND_FILES again.
1743 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1744 *complp = EXPAND_ENV_VARS;
1745 }
1746 }
1747 // Check for user names.
1748 if (*xp->xp_pattern == '~')
1749 {
1750 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1751 ;
1752 // Complete ~user only if it partially matches a user name.
1753 // A full match ~user<Tab> will be replaced by user's home
1754 // directory i.e. something like ~user<Tab> -> /home/user/
1755 if (*p == NUL && p > xp->xp_pattern + 1
1756 && match_user(xp->xp_pattern + 1) >= 1)
1757 {
1758 xp->xp_context = EXPAND_USER;
1759 ++xp->xp_pattern;
1760 }
1761 }
1762}
1763
1764/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001765 * Set the completion context for the :filter command. Returns a pointer to the
1766 * next command after the :filter command.
1767 */
1768 static char_u *
1769set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1770{
1771 if (*arg != NUL)
1772 arg = skip_vimgrep_pat(arg, NULL, NULL);
1773 if (arg == NULL || *arg == NUL)
1774 {
1775 xp->xp_context = EXPAND_NOTHING;
1776 return NULL;
1777 }
1778 return skipwhite(arg);
1779}
1780
1781#ifdef FEAT_SEARCH_EXTRA
1782/*
1783 * Set the completion context for the :match command. Returns a pointer to the
1784 * next command after the :match command.
1785 */
1786 static char_u *
1787set_context_in_match_cmd(expand_T *xp, char_u *arg)
1788{
1789 if (*arg == NUL || !ends_excmd(*arg))
1790 {
1791 // also complete "None"
1792 set_context_in_echohl_cmd(xp, arg);
1793 arg = skipwhite(skiptowhite(arg));
1794 if (*arg != NUL)
1795 {
1796 xp->xp_context = EXPAND_NOTHING;
1797 arg = skip_regexp(arg + 1, *arg, magic_isset());
1798 }
1799 }
1800 return find_nextcmd(arg);
1801}
1802#endif
1803
1804/*
1805 * Returns a pointer to the next command after a :global or a :v command.
1806 * Returns NULL if there is no next command.
1807 */
1808 static char_u *
1809find_cmd_after_global_cmd(char_u *arg)
1810{
1811 int delim;
1812
1813 delim = *arg; // get the delimiter
1814 if (delim)
1815 ++arg; // skip delimiter if there is one
1816
1817 while (arg[0] != NUL && arg[0] != delim)
1818 {
1819 if (arg[0] == '\\' && arg[1] != NUL)
1820 ++arg;
1821 ++arg;
1822 }
1823 if (arg[0] != NUL)
1824 return arg + 1;
1825
1826 return NULL;
1827}
1828
1829/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001830 * Returns a pointer to the next command after a :substitute or a :& command.
1831 * Returns NULL if there is no next command.
1832 */
1833 static char_u *
1834find_cmd_after_substitute_cmd(char_u *arg)
1835{
1836 int delim;
1837
1838 delim = *arg;
1839 if (delim)
1840 {
1841 // skip "from" part
1842 ++arg;
1843 arg = skip_regexp(arg, delim, magic_isset());
1844
1845 if (arg[0] != NUL && arg[0] == delim)
1846 {
1847 // skip "to" part
1848 ++arg;
1849 while (arg[0] != NUL && arg[0] != delim)
1850 {
1851 if (arg[0] == '\\' && arg[1] != NUL)
1852 ++arg;
1853 ++arg;
1854 }
1855 if (arg[0] != NUL) // skip delimiter
1856 ++arg;
1857 }
1858 }
1859 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1860 ++arg;
1861 if (arg[0] != NUL)
1862 return arg;
1863
1864 return NULL;
1865}
1866
1867/*
1868 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1869 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1870 * Returns NULL if there is no next command.
1871 */
1872 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001873find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001874{
1875 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001876 if (*arg != '/')
1877 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001878
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001879 // Match regexp, not just whole words
1880 for (++arg; *arg && *arg != '/'; arg++)
1881 if (*arg == '\\' && arg[1] != NUL)
1882 arg++;
1883 if (*arg)
1884 {
1885 arg = skipwhite(arg + 1);
1886
1887 // Check for trailing illegal characters
1888 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1889 xp->xp_context = EXPAND_NOTHING;
1890 else
1891 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001892 }
1893
1894 return NULL;
1895}
1896
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001897#ifdef FEAT_EVAL
1898/*
1899 * Set the completion context for the :unlet command. Always returns NULL.
1900 */
1901 static char_u *
1902set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1903{
1904 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1905 arg = xp->xp_pattern + 1;
1906
1907 xp->xp_context = EXPAND_USER_VARS;
1908 xp->xp_pattern = arg;
1909
1910 if (*xp->xp_pattern == '$')
1911 {
1912 xp->xp_context = EXPAND_ENV_VARS;
1913 ++xp->xp_pattern;
1914 }
1915
1916 return NULL;
1917}
1918#endif
1919
1920#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1921/*
1922 * Set the completion context for the :language command. Always returns NULL.
1923 */
1924 static char_u *
1925set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1926{
1927 char_u *p;
1928
1929 p = skiptowhite(arg);
1930 if (*p == NUL)
1931 {
1932 xp->xp_context = EXPAND_LANGUAGE;
1933 xp->xp_pattern = arg;
1934 }
1935 else
1936 {
1937 if ( STRNCMP(arg, "messages", p - arg) == 0
1938 || STRNCMP(arg, "ctype", p - arg) == 0
1939 || STRNCMP(arg, "time", p - arg) == 0
1940 || STRNCMP(arg, "collate", p - arg) == 0)
1941 {
1942 xp->xp_context = EXPAND_LOCALES;
1943 xp->xp_pattern = skipwhite(p);
1944 }
1945 else
1946 xp->xp_context = EXPAND_NOTHING;
1947 }
1948
1949 return NULL;
1950}
1951#endif
1952
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001953#ifdef FEAT_EVAL
1954static enum
1955{
1956 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001957 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1958 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001959} breakpt_expand_what;
1960
1961/*
1962 * Set the completion context for the :breakadd command. Always returns NULL.
1963 */
1964 static char_u *
1965set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1966{
1967 char_u *p;
1968 char_u *subcmd_start;
1969
1970 xp->xp_context = EXPAND_BREAKPOINT;
1971 xp->xp_pattern = arg;
1972
1973 if (cmdidx == CMD_breakadd)
1974 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001975 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001976 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001977 else
1978 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001979
1980 p = skipwhite(arg);
1981 if (*p == NUL)
1982 return NULL;
1983 subcmd_start = p;
1984
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001985 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001986 {
1987 // :breakadd file [lnum] <filename>
1988 // :breakadd func [lnum] <funcname>
1989 p += 4;
1990 p = skipwhite(p);
1991
1992 // skip line number (if specified)
1993 if (VIM_ISDIGIT(*p))
1994 {
1995 p = skipdigits(p);
1996 if (*p != ' ')
1997 {
1998 xp->xp_context = EXPAND_NOTHING;
1999 return NULL;
2000 }
2001 p = skipwhite(p);
2002 }
2003 if (STRNCMP("file", subcmd_start, 4) == 0)
2004 xp->xp_context = EXPAND_FILES;
2005 else
2006 xp->xp_context = EXPAND_USER_FUNC;
2007 xp->xp_pattern = p;
2008 }
2009 else if (STRNCMP("expr ", p, 5) == 0)
2010 {
2011 // :breakadd expr <expression>
2012 xp->xp_context = EXPAND_EXPRESSION;
2013 xp->xp_pattern = skipwhite(p + 5);
2014 }
2015
2016 return NULL;
2017}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002018
2019 static char_u *
2020set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2021{
2022 char_u *p;
2023
2024 xp->xp_context = EXPAND_NOTHING;
2025 xp->xp_pattern = NULL;
2026
2027 p = skipwhite(arg);
2028 if (VIM_ISDIGIT(*p))
2029 return NULL;
2030
2031 xp->xp_context = EXPAND_SCRIPTNAMES;
2032 xp->xp_pattern = p;
2033
2034 return NULL;
2035}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002036#endif
2037
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002038/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002039 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2040 * The argument to the command is 'arg' and the argument flags is 'argt'.
2041 * For user-defined commands and for environment variables, 'compl' has the
2042 * completion type.
2043 * Returns a pointer to the next command. Returns NULL if there is no next
2044 * command.
2045 */
2046 static char_u *
2047set_context_by_cmdname(
2048 char_u *cmd,
2049 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002050 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002051 char_u *arg,
2052 long argt,
2053 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002054 int forceit)
2055{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002056 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002057 {
2058 case CMD_find:
2059 case CMD_sfind:
2060 case CMD_tabfind:
2061 if (xp->xp_context == EXPAND_FILES)
2062 xp->xp_context = EXPAND_FILES_IN_PATH;
2063 break;
2064 case CMD_cd:
2065 case CMD_chdir:
2066 case CMD_tcd:
2067 case CMD_tchdir:
2068 case CMD_lcd:
2069 case CMD_lchdir:
2070 if (xp->xp_context == EXPAND_FILES)
2071 xp->xp_context = EXPAND_DIRECTORIES;
2072 break;
2073 case CMD_help:
2074 xp->xp_context = EXPAND_HELP;
2075 xp->xp_pattern = arg;
2076 break;
2077
2078 // Command modifiers: return the argument.
2079 // Also for commands with an argument that is a command.
2080 case CMD_aboveleft:
2081 case CMD_argdo:
2082 case CMD_belowright:
2083 case CMD_botright:
2084 case CMD_browse:
2085 case CMD_bufdo:
2086 case CMD_cdo:
2087 case CMD_cfdo:
2088 case CMD_confirm:
2089 case CMD_debug:
2090 case CMD_folddoclosed:
2091 case CMD_folddoopen:
2092 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002093 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002094 case CMD_keepalt:
2095 case CMD_keepjumps:
2096 case CMD_keepmarks:
2097 case CMD_keeppatterns:
2098 case CMD_ldo:
2099 case CMD_leftabove:
2100 case CMD_lfdo:
2101 case CMD_lockmarks:
2102 case CMD_noautocmd:
2103 case CMD_noswapfile:
2104 case CMD_rightbelow:
2105 case CMD_sandbox:
2106 case CMD_silent:
2107 case CMD_tab:
2108 case CMD_tabdo:
2109 case CMD_topleft:
2110 case CMD_verbose:
2111 case CMD_vertical:
2112 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002113 case CMD_vim9cmd:
2114 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002115 return arg;
2116
2117 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002118 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002119
2120#ifdef FEAT_SEARCH_EXTRA
2121 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002122 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002123#endif
2124
2125 // All completion for the +cmdline_compl feature goes here.
2126
2127 case CMD_command:
2128 return set_context_in_user_cmd(xp, arg);
2129
2130 case CMD_delcommand:
2131 xp->xp_context = EXPAND_USER_COMMANDS;
2132 xp->xp_pattern = arg;
2133 break;
2134
2135 case CMD_global:
2136 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002137 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002138 case CMD_and:
2139 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002140 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002141 case CMD_isearch:
2142 case CMD_dsearch:
2143 case CMD_ilist:
2144 case CMD_dlist:
2145 case CMD_ijump:
2146 case CMD_psearch:
2147 case CMD_djump:
2148 case CMD_isplit:
2149 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002150 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002151 case CMD_autocmd:
2152 return set_context_in_autocmd(xp, arg, FALSE);
2153 case CMD_doautocmd:
2154 case CMD_doautoall:
2155 return set_context_in_autocmd(xp, arg, TRUE);
2156 case CMD_set:
2157 set_context_in_set_cmd(xp, arg, 0);
2158 break;
2159 case CMD_setglobal:
2160 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2161 break;
2162 case CMD_setlocal:
2163 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2164 break;
2165 case CMD_tag:
2166 case CMD_stag:
2167 case CMD_ptag:
2168 case CMD_ltag:
2169 case CMD_tselect:
2170 case CMD_stselect:
2171 case CMD_ptselect:
2172 case CMD_tjump:
2173 case CMD_stjump:
2174 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002175 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002176 xp->xp_context = EXPAND_TAGS_LISTFILES;
2177 else
2178 xp->xp_context = EXPAND_TAGS;
2179 xp->xp_pattern = arg;
2180 break;
2181 case CMD_augroup:
2182 xp->xp_context = EXPAND_AUGROUP;
2183 xp->xp_pattern = arg;
2184 break;
2185#ifdef FEAT_SYN_HL
2186 case CMD_syntax:
2187 set_context_in_syntax_cmd(xp, arg);
2188 break;
2189#endif
2190#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002191 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002192 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002193 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002194 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002195 case CMD_if:
2196 case CMD_elseif:
2197 case CMD_while:
2198 case CMD_for:
2199 case CMD_echo:
2200 case CMD_echon:
2201 case CMD_execute:
2202 case CMD_echomsg:
2203 case CMD_echoerr:
2204 case CMD_call:
2205 case CMD_return:
2206 case CMD_cexpr:
2207 case CMD_caddexpr:
2208 case CMD_cgetexpr:
2209 case CMD_lexpr:
2210 case CMD_laddexpr:
2211 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002212 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002213 break;
2214
2215 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002216 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002217 case CMD_function:
2218 case CMD_delfunction:
2219 xp->xp_context = EXPAND_USER_FUNC;
2220 xp->xp_pattern = arg;
2221 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002222 case CMD_disassemble:
2223 set_context_in_disassemble_cmd(xp, arg);
2224 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002225
2226 case CMD_echohl:
2227 set_context_in_echohl_cmd(xp, arg);
2228 break;
2229#endif
2230 case CMD_highlight:
2231 set_context_in_highlight_cmd(xp, arg);
2232 break;
2233#ifdef FEAT_CSCOPE
2234 case CMD_cscope:
2235 case CMD_lcscope:
2236 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002237 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002238 break;
2239#endif
2240#ifdef FEAT_SIGNS
2241 case CMD_sign:
2242 set_context_in_sign_cmd(xp, arg);
2243 break;
2244#endif
2245 case CMD_bdelete:
2246 case CMD_bwipeout:
2247 case CMD_bunload:
2248 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2249 arg = xp->xp_pattern + 1;
2250 // FALLTHROUGH
2251 case CMD_buffer:
2252 case CMD_sbuffer:
2253 case CMD_checktime:
2254 xp->xp_context = EXPAND_BUFFERS;
2255 xp->xp_pattern = arg;
2256 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002257#ifdef FEAT_DIFF
2258 case CMD_diffget:
2259 case CMD_diffput:
2260 // If current buffer is in diff mode, complete buffer names
2261 // which are in diff mode, and different than current buffer.
2262 xp->xp_context = EXPAND_DIFF_BUFFERS;
2263 xp->xp_pattern = arg;
2264 break;
2265#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002266 case CMD_USER:
2267 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002268 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2269 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002270
2271 case CMD_map: case CMD_noremap:
2272 case CMD_nmap: case CMD_nnoremap:
2273 case CMD_vmap: case CMD_vnoremap:
2274 case CMD_omap: case CMD_onoremap:
2275 case CMD_imap: case CMD_inoremap:
2276 case CMD_cmap: case CMD_cnoremap:
2277 case CMD_lmap: case CMD_lnoremap:
2278 case CMD_smap: case CMD_snoremap:
2279 case CMD_tmap: case CMD_tnoremap:
2280 case CMD_xmap: case CMD_xnoremap:
2281 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002282 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002283 case CMD_unmap:
2284 case CMD_nunmap:
2285 case CMD_vunmap:
2286 case CMD_ounmap:
2287 case CMD_iunmap:
2288 case CMD_cunmap:
2289 case CMD_lunmap:
2290 case CMD_sunmap:
2291 case CMD_tunmap:
2292 case CMD_xunmap:
2293 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002294 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002295 case CMD_mapclear:
2296 case CMD_nmapclear:
2297 case CMD_vmapclear:
2298 case CMD_omapclear:
2299 case CMD_imapclear:
2300 case CMD_cmapclear:
2301 case CMD_lmapclear:
2302 case CMD_smapclear:
2303 case CMD_tmapclear:
2304 case CMD_xmapclear:
2305 xp->xp_context = EXPAND_MAPCLEAR;
2306 xp->xp_pattern = arg;
2307 break;
2308
2309 case CMD_abbreviate: case CMD_noreabbrev:
2310 case CMD_cabbrev: case CMD_cnoreabbrev:
2311 case CMD_iabbrev: case CMD_inoreabbrev:
2312 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002313 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002314 case CMD_unabbreviate:
2315 case CMD_cunabbrev:
2316 case CMD_iunabbrev:
2317 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002318 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002319#ifdef FEAT_MENU
2320 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2321 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2322 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2323 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2324 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2325 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2326 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2327 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2328 case CMD_tmenu: case CMD_tunmenu:
2329 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2330 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2331#endif
2332
2333 case CMD_colorscheme:
2334 xp->xp_context = EXPAND_COLORS;
2335 xp->xp_pattern = arg;
2336 break;
2337
2338 case CMD_compiler:
2339 xp->xp_context = EXPAND_COMPILER;
2340 xp->xp_pattern = arg;
2341 break;
2342
2343 case CMD_ownsyntax:
2344 xp->xp_context = EXPAND_OWNSYNTAX;
2345 xp->xp_pattern = arg;
2346 break;
2347
2348 case CMD_setfiletype:
2349 xp->xp_context = EXPAND_FILETYPE;
2350 xp->xp_pattern = arg;
2351 break;
2352
2353 case CMD_packadd:
2354 xp->xp_context = EXPAND_PACKADD;
2355 xp->xp_pattern = arg;
2356 break;
2357
zeertzjqb0d45ec2023-01-25 15:04:22 +00002358 case CMD_runtime:
2359 set_context_in_runtime_cmd(xp, arg);
2360 break;
2361
Bram Moolenaard0190392019-08-23 21:17:35 +02002362#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2363 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002364 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002365#endif
2366#if defined(FEAT_PROFILE)
2367 case CMD_profile:
2368 set_context_in_profile_cmd(xp, arg);
2369 break;
2370#endif
2371 case CMD_behave:
2372 xp->xp_context = EXPAND_BEHAVE;
2373 xp->xp_pattern = arg;
2374 break;
2375
2376 case CMD_messages:
2377 xp->xp_context = EXPAND_MESSAGES;
2378 xp->xp_pattern = arg;
2379 break;
2380
2381 case CMD_history:
2382 xp->xp_context = EXPAND_HISTORY;
2383 xp->xp_pattern = arg;
2384 break;
2385#if defined(FEAT_PROFILE)
2386 case CMD_syntime:
2387 xp->xp_context = EXPAND_SYNTIME;
2388 xp->xp_pattern = arg;
2389 break;
2390#endif
2391
2392 case CMD_argdelete:
2393 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2394 arg = xp->xp_pattern + 1;
2395 xp->xp_context = EXPAND_ARGLIST;
2396 xp->xp_pattern = arg;
2397 break;
2398
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002399#ifdef FEAT_EVAL
2400 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002401 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002402 case CMD_breakdel:
2403 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002404
2405 case CMD_scriptnames:
2406 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002407#endif
2408
Bram Moolenaard0190392019-08-23 21:17:35 +02002409 default:
2410 break;
2411 }
2412 return NULL;
2413}
2414
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002415/*
2416 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2417 * we don't need/want deleted. Maybe this could be done better if we didn't
2418 * repeat all this stuff. The only problem is that they may not stay
2419 * perfectly compatible with each other, but then the command line syntax
2420 * probably won't change that much -- webb.
2421 */
2422 static char_u *
2423set_one_cmd_context(
2424 expand_T *xp,
2425 char_u *buff) // buffer for command string
2426{
2427 char_u *p;
2428 char_u *cmd, *arg;
2429 int len = 0;
2430 exarg_T ea;
2431 int compl = EXPAND_NOTHING;
2432 int forceit = FALSE;
2433 int usefilter = FALSE; // filter instead of file name
2434
2435 ExpandInit(xp);
2436 xp->xp_pattern = buff;
2437 xp->xp_line = buff;
2438 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2439 ea.argt = 0;
2440
2441 // 1. skip comment lines and leading space, colons or bars
2442 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2443 ;
2444 xp->xp_pattern = cmd;
2445
2446 if (*cmd == NUL)
2447 return NULL;
2448 if (*cmd == '"') // ignore comment lines
2449 {
2450 xp->xp_context = EXPAND_NOTHING;
2451 return NULL;
2452 }
2453
2454 // 3. Skip over the range to find the command.
2455 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2456 xp->xp_pattern = cmd;
2457 if (*cmd == NUL)
2458 return NULL;
2459 if (*cmd == '"')
2460 {
2461 xp->xp_context = EXPAND_NOTHING;
2462 return NULL;
2463 }
2464
2465 if (*cmd == '|' || *cmd == '\n')
2466 return cmd + 1; // There's another command
2467
2468 // Get the command index.
2469 p = set_cmd_index(cmd, &ea, xp, &compl);
2470 if (p == NULL)
2471 return NULL;
2472
2473 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2474
2475 if (*p == '!') // forced commands
2476 {
2477 forceit = TRUE;
2478 ++p;
2479 }
2480
2481 // 6. parse arguments
2482 if (!IS_USER_CMDIDX(ea.cmdidx))
2483 ea.argt = excmd_get_argt(ea.cmdidx);
2484
2485 arg = skipwhite(p);
2486
2487 // Skip over ++argopt argument
2488 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2489 {
2490 p = arg;
2491 while (*p && !vim_isspace(*p))
2492 MB_PTR_ADV(p);
2493 arg = skipwhite(p);
2494 }
2495
2496 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2497 {
2498 if (*arg == '>') // append
2499 {
2500 if (*++arg == '>')
2501 ++arg;
2502 arg = skipwhite(arg);
2503 }
2504 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2505 {
2506 ++arg;
2507 usefilter = TRUE;
2508 }
2509 }
2510
2511 if (ea.cmdidx == CMD_read)
2512 {
2513 usefilter = forceit; // :r! filter if forced
2514 if (*arg == '!') // :r !filter
2515 {
2516 ++arg;
2517 usefilter = TRUE;
2518 }
2519 }
2520
2521 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2522 {
2523 while (*arg == *cmd) // allow any number of '>' or '<'
2524 ++arg;
2525 arg = skipwhite(arg);
2526 }
2527
2528 // Does command allow "+command"?
2529 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2530 {
2531 // Check if we're in the +command
2532 p = arg + 1;
2533 arg = skip_cmd_arg(arg, FALSE);
2534
2535 // Still touching the command after '+'?
2536 if (*arg == NUL)
2537 return p;
2538
2539 // Skip space(s) after +command to get to the real argument
2540 arg = skipwhite(arg);
2541 }
2542
2543
2544 // Check for '|' to separate commands and '"' to start comments.
2545 // Don't do this for ":read !cmd" and ":write !cmd".
2546 if ((ea.argt & EX_TRLBAR) && !usefilter)
2547 {
2548 p = arg;
2549 // ":redir @" is not the start of a comment
2550 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2551 p += 2;
2552 while (*p)
2553 {
2554 if (*p == Ctrl_V)
2555 {
2556 if (p[1] != NUL)
2557 ++p;
2558 }
2559 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2560 || *p == '|' || *p == '\n')
2561 {
2562 if (*(p - 1) != '\\')
2563 {
2564 if (*p == '|' || *p == '\n')
2565 return p + 1;
2566 return NULL; // It's a comment
2567 }
2568 }
2569 MB_PTR_ADV(p);
2570 }
2571 }
2572
2573 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2574 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2575 // no arguments allowed but there is something
2576 return NULL;
2577
2578 // Find start of last argument (argument just before cursor):
2579 p = buff;
2580 xp->xp_pattern = p;
2581 len = (int)STRLEN(buff);
2582 while (*p && p < buff + len)
2583 {
2584 if (*p == ' ' || *p == TAB)
2585 {
2586 // argument starts after a space
2587 xp->xp_pattern = ++p;
2588 }
2589 else
2590 {
2591 if (*p == '\\' && *(p + 1) != NUL)
2592 ++p; // skip over escaped character
2593 MB_PTR_ADV(p);
2594 }
2595 }
2596
2597 if (ea.argt & EX_XFILE)
2598 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2599
2600 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002601 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002602 forceit);
2603}
2604
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002605/*
2606 * Set the completion context in 'xp' for command 'str'
2607 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002608 void
2609set_cmd_context(
2610 expand_T *xp,
2611 char_u *str, // start of command line
2612 int len, // length of command line (excl. NUL)
2613 int col, // position of cursor
2614 int use_ccline UNUSED) // use ccline for info
2615{
2616#ifdef FEAT_EVAL
2617 cmdline_info_T *ccline = get_cmdline_info();
2618#endif
2619 int old_char = NUL;
2620 char_u *nextcomm;
2621
2622 // Avoid a UMR warning from Purify, only save the character if it has been
2623 // written before.
2624 if (col < len)
2625 old_char = str[col];
2626 str[col] = NUL;
2627 nextcomm = str;
2628
2629#ifdef FEAT_EVAL
2630 if (use_ccline && ccline->cmdfirstc == '=')
2631 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002632 // pass CMD_SIZE because there is no real command
2633 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002634 }
2635 else if (use_ccline && ccline->input_fn)
2636 {
2637 xp->xp_context = ccline->xp_context;
2638 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002639 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002640 }
2641 else
2642#endif
2643 while (nextcomm != NULL)
2644 nextcomm = set_one_cmd_context(xp, nextcomm);
2645
2646 // Store the string here so that call_user_expand_func() can get to them
2647 // easily.
2648 xp->xp_line = str;
2649 xp->xp_col = col;
2650
2651 str[col] = old_char;
2652}
2653
2654/*
2655 * Expand the command line "str" from context "xp".
2656 * "xp" must have been set by set_cmd_context().
2657 * xp->xp_pattern points into "str", to where the text that is to be expanded
2658 * starts.
2659 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2660 * cursor.
2661 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2662 * key that triggered expansion literally.
2663 * Returns EXPAND_OK otherwise.
2664 */
2665 int
2666expand_cmdline(
2667 expand_T *xp,
2668 char_u *str, // start of command line
2669 int col, // position of cursor
2670 int *matchcount, // return: nr of matches
2671 char_u ***matches) // return: array of pointers to matches
2672{
2673 char_u *file_str = NULL;
2674 int options = WILD_ADD_SLASH|WILD_SILENT;
2675
2676 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2677 {
2678 beep_flush();
2679 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2680 }
2681 if (xp->xp_context == EXPAND_NOTHING)
2682 {
2683 // Caller can use the character as a normal char instead
2684 return EXPAND_NOTHING;
2685 }
2686
2687 // add star to file name, or convert to regexp if not exp. files.
2688 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002689 if (cmdline_fuzzy_completion_supported(xp))
2690 // If fuzzy matching, don't modify the search string
2691 file_str = vim_strsave(xp->xp_pattern);
2692 else
2693 {
2694 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2695 if (file_str == NULL)
2696 return EXPAND_UNSUCCESSFUL;
2697 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002698
2699 if (p_wic)
2700 options += WILD_ICASE;
2701
2702 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002703 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002704 {
2705 *matchcount = 0;
2706 *matches = NULL;
2707 }
2708 vim_free(file_str);
2709
2710 return EXPAND_OK;
2711}
2712
Bram Moolenaar66b51422019-08-18 21:44:12 +02002713/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002714 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002715 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002716 */
2717 static int
2718expand_files_and_dirs(
2719 expand_T *xp,
2720 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002721 char_u ***matches,
2722 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002723 int flags,
2724 int options)
2725{
2726 int free_pat = FALSE;
2727 int i;
2728 int ret;
2729
2730 // for ":set path=" and ":set tags=" halve backslashes for escaped
2731 // space
2732 if (xp->xp_backslash != XP_BS_NONE)
2733 {
2734 free_pat = TRUE;
2735 pat = vim_strsave(pat);
2736 for (i = 0; pat[i]; ++i)
2737 if (pat[i] == '\\')
2738 {
2739 if (xp->xp_backslash == XP_BS_THREE
2740 && pat[i + 1] == '\\'
2741 && pat[i + 2] == '\\'
2742 && pat[i + 3] == ' ')
2743 STRMOVE(pat + i, pat + i + 3);
2744 if (xp->xp_backslash == XP_BS_ONE
2745 && pat[i + 1] == ' ')
2746 STRMOVE(pat + i, pat + i + 1);
2747 }
2748 }
2749
2750 if (xp->xp_context == EXPAND_FILES)
2751 flags |= EW_FILE;
2752 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2753 flags |= (EW_FILE | EW_PATH);
2754 else
2755 flags = (flags | EW_DIR) & ~EW_FILE;
2756 if (options & WILD_ICASE)
2757 flags |= EW_ICASE;
2758
2759 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002760 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002761 if (free_pat)
2762 vim_free(pat);
2763#ifdef BACKSLASH_IN_FILENAME
2764 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2765 {
2766 int j;
2767
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002768 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002769 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002770 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002771
2772 while (*ptr != NUL)
2773 {
2774 if (p_csl[0] == 's' && *ptr == '\\')
2775 *ptr = '/';
2776 else if (p_csl[0] == 'b' && *ptr == '/')
2777 *ptr = '\\';
2778 ptr += (*mb_ptr2len)(ptr);
2779 }
2780 }
2781 }
2782#endif
2783 return ret;
2784}
2785
2786/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002787 * Function given to ExpandGeneric() to obtain the possible arguments of the
2788 * ":behave {mswin,xterm}" command.
2789 */
2790 static char_u *
2791get_behave_arg(expand_T *xp UNUSED, int idx)
2792{
2793 if (idx == 0)
2794 return (char_u *)"mswin";
2795 if (idx == 1)
2796 return (char_u *)"xterm";
2797 return NULL;
2798}
2799
K.Takata161b6ac2022-11-14 15:31:07 +00002800#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002801/*
2802 * Function given to ExpandGeneric() to obtain the possible arguments of the
2803 * ":breakadd {expr, file, func, here}" command.
2804 * ":breakdel {func, file, here}" command.
2805 */
2806 static char_u *
2807get_breakadd_arg(expand_T *xp UNUSED, int idx)
2808{
2809 char *opts[] = {"expr", "file", "func", "here"};
2810
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002811 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002812 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002813 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002814 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2815 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002816 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2817 {
2818 // breakdel {func, file, here}
2819 if (idx <= 2)
2820 return (char_u *)opts[idx + 1];
2821 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002822 else
2823 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002824 // profdel {func, file}
2825 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002826 return (char_u *)opts[idx + 1];
2827 }
2828 }
2829 return NULL;
2830}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002831
2832/*
2833 * Function given to ExpandGeneric() to obtain the possible arguments for the
2834 * ":scriptnames" command.
2835 */
2836 static char_u *
2837get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2838{
2839 scriptitem_T *si;
2840
2841 if (!SCRIPT_ID_VALID(idx + 1))
2842 return NULL;
2843
2844 si = SCRIPT_ITEM(idx + 1);
2845 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2846 return NameBuff;
2847}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002848#endif
2849
Bram Moolenaard0190392019-08-23 21:17:35 +02002850/*
2851 * Function given to ExpandGeneric() to obtain the possible arguments of the
2852 * ":messages {clear}" command.
2853 */
2854 static char_u *
2855get_messages_arg(expand_T *xp UNUSED, int idx)
2856{
2857 if (idx == 0)
2858 return (char_u *)"clear";
2859 return NULL;
2860}
2861
2862 static char_u *
2863get_mapclear_arg(expand_T *xp UNUSED, int idx)
2864{
2865 if (idx == 0)
2866 return (char_u *)"<buffer>";
2867 return NULL;
2868}
2869
2870/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002871 * Do the expansion based on xp->xp_context and 'rmp'.
2872 */
2873 static int
2874ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002875 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002876 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002877 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002878 char_u ***matches,
2879 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002880{
2881 static struct expgen
2882 {
2883 int context;
2884 char_u *((*func)(expand_T *, int));
2885 int ic;
2886 int escaped;
2887 } tab[] =
2888 {
2889 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2890 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2891 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2892 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2893 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2894 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2895 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2896 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2897 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2898 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002899#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002900 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2901 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2902 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2903 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2904 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002905#endif
2906#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002907 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2908 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002909#endif
2910#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002911 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002912#endif
2913#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002914 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002915#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002916 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2917 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2918 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002919#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002920 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002921#endif
2922#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002923 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002924#endif
2925#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002926 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002927#endif
2928#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002929 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2930 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002931#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002932 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2933 {EXPAND_USER, get_users, TRUE, FALSE},
2934 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002935#ifdef FEAT_EVAL
2936 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002937 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002938#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002939 };
2940 int i;
2941 int ret = FAIL;
2942
2943 // Find a context in the table and call the ExpandGeneric() with the
2944 // right function to do the expansion.
2945 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2946 {
2947 if (xp->xp_context == tab[i].context)
2948 {
2949 if (tab[i].ic)
2950 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002951 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2952 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002953 break;
2954 }
2955 }
2956
2957 return ret;
2958}
2959
2960/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002961 * Map wild expand options to flags for expand_wildcards()
2962 */
2963 static int
2964map_wildopts_to_ewflags(int options)
2965{
2966 int flags;
2967
2968 flags = EW_DIR; // include directories
2969 if (options & WILD_LIST_NOTFOUND)
2970 flags |= EW_NOTFOUND;
2971 if (options & WILD_ADD_SLASH)
2972 flags |= EW_ADDSLASH;
2973 if (options & WILD_KEEP_ALL)
2974 flags |= EW_KEEPALL;
2975 if (options & WILD_SILENT)
2976 flags |= EW_SILENT;
2977 if (options & WILD_NOERROR)
2978 flags |= EW_NOERROR;
2979 if (options & WILD_ALLLINKS)
2980 flags |= EW_ALLLINKS;
2981
2982 return flags;
2983}
2984
2985/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002986 * Do the expansion based on xp->xp_context and "pat".
2987 */
2988 static int
2989ExpandFromContext(
2990 expand_T *xp,
2991 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002992 char_u ***matches,
2993 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002994 int options) // WILD_ flags
2995{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002996 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002997 int ret;
2998 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002999 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003000 int fuzzy = cmdline_fuzzy_complete(pat)
3001 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003002
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003003 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003004
3005 if (xp->xp_context == EXPAND_FILES
3006 || xp->xp_context == EXPAND_DIRECTORIES
3007 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003008 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3009 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003010
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003011 *matches = (char_u **)"";
3012 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003013 if (xp->xp_context == EXPAND_HELP)
3014 {
3015 // With an empty argument we would get all the help tags, which is
3016 // very slow. Get matches for "help" instead.
3017 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003018 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003019 {
3020#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003021 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003022#endif
3023 return OK;
3024 }
3025 return FAIL;
3026 }
3027
Bram Moolenaar66b51422019-08-18 21:44:12 +02003028 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003029 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003030 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003031 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003032 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003033 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003034#ifdef FEAT_DIFF
3035 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003036 return ExpandBufnames(pat, numMatches, matches,
3037 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003038#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003039 if (xp->xp_context == EXPAND_TAGS
3040 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003041 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3042 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003043 if (xp->xp_context == EXPAND_COLORS)
3044 {
3045 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003046 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003047 directories);
3048 }
3049 if (xp->xp_context == EXPAND_COMPILER)
3050 {
3051 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003052 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003053 }
3054 if (xp->xp_context == EXPAND_OWNSYNTAX)
3055 {
3056 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003057 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003058 }
3059 if (xp->xp_context == EXPAND_FILETYPE)
3060 {
3061 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003062 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003063 }
K.Takata161b6ac2022-11-14 15:31:07 +00003064#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003065 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003066 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003067#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003068 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003069 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003070 if (xp->xp_context == EXPAND_RUNTIME)
3071 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003072
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003073 // When expanding a function name starting with s:, match the <SNR>nr_
3074 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003075 if ((xp->xp_context == EXPAND_USER_FUNC
3076 || xp->xp_context == EXPAND_DISASSEMBLE)
3077 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003078 {
3079 int len = (int)STRLEN(pat) + 20;
3080
3081 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003082 if (tofree == NULL)
3083 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003084 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003085 pat = tofree;
3086 }
3087
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003088 if (!fuzzy)
3089 {
3090 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3091 if (regmatch.regprog == NULL)
3092 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003093
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003094 // set ignore-case according to p_ic, p_scs and pat
3095 regmatch.rm_ic = ignorecase(pat);
3096 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003097
3098 if (xp->xp_context == EXPAND_SETTINGS
3099 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003100 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003101 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003102 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003103#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003105 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003106#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003107 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003108 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003109
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003110 if (!fuzzy)
3111 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003112 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113
3114 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115}
3116
Bram Moolenaar66b51422019-08-18 21:44:12 +02003117/*
3118 * Expand a list of names.
3119 *
3120 * Generic function for command line completion. It calls a function to
3121 * obtain strings, one by one. The strings are matched against a regexp
3122 * program. Matching strings are copied into an array, which is returned.
3123 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003124 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3125 * is used.
3126 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003127 * Returns OK when no problems encountered, FAIL for error (out of memory).
3128 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003129 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003131 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003132 expand_T *xp,
3133 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003134 char_u ***matches,
3135 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003136 char_u *((*func)(expand_T *, int)),
3137 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003138 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139{
3140 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003141 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003142 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003143 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003144 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003145 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003146 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003147 int sort_matches = FALSE;
3148 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003149
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003150 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003151 *matches = NULL;
3152 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003153
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003154 if (!fuzzy)
3155 ga_init2(&ga, sizeof(char *), 30);
3156 else
3157 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3158
3159 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003160 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003161 str = (*func)(xp, i);
3162 if (str == NULL) // end of list
3163 break;
3164 if (*str == NUL) // skip empty strings
3165 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003166
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003167 if (xp->xp_pattern[0] != NUL)
3168 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003169 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003170 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003171 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003172 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003173 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003174 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003175 }
3176 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003177 else
3178 match = TRUE;
3179
3180 if (!match)
3181 continue;
3182
3183 if (escaped)
3184 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3185 else
3186 str = vim_strsave(str);
3187 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003188 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003189 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003190 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003191 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003192 return FAIL;
3193 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003194 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003195 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003196 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003197
3198 if (ga_grow(&ga, 1) == FAIL)
3199 {
3200 vim_free(str);
3201 break;
3202 }
3203
3204 if (fuzzy)
3205 {
3206 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3207 fuzmatch->idx = ga.ga_len;
3208 fuzmatch->str = str;
3209 fuzmatch->score = score;
3210 }
3211 else
3212 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3213
K.Takata161b6ac2022-11-14 15:31:07 +00003214#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003215 if (func == get_menu_names)
3216 {
3217 // test for separator added by get_menu_names()
3218 str += STRLEN(str) - 1;
3219 if (*str == '\001')
3220 *str = '.';
3221 }
K.Takata161b6ac2022-11-14 15:31:07 +00003222#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003223
3224 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003225 }
3226
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003227 if (ga.ga_len == 0)
3228 return OK;
3229
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003230 // sort the matches when using regular expression matching and sorting
3231 // applies to the completion context. Menus and scriptnames should be kept
3232 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003233 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003234 && xp->xp_context != EXPAND_MENUS
3235 && xp->xp_context != EXPAND_SCRIPTNAMES)
3236 sort_matches = TRUE;
3237
3238 // <SNR> functions should be sorted to the end.
3239 if (xp->xp_context == EXPAND_EXPRESSION
3240 || xp->xp_context == EXPAND_FUNCTIONS
3241 || xp->xp_context == EXPAND_USER_FUNC
3242 || xp->xp_context == EXPAND_DISASSEMBLE)
3243 funcsort = TRUE;
3244
3245 // Sort the matches.
3246 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003247 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003248 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003249 // <SNR> functions should be sorted to the end.
3250 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3251 sort_func_compare);
3252 else
3253 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3254 }
3255
3256 if (!fuzzy)
3257 {
3258 *matches = ga.ga_data;
3259 *numMatches = ga.ga_len;
3260 }
3261 else
3262 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003263 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3264 funcsort) == FAIL)
3265 return FAIL;
3266 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003267 }
3268
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003269#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003270 // Reset the variables used for special highlight names expansion, so that
3271 // they don't show up when getting normal highlight names by ID.
3272 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003273#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003274
Bram Moolenaar66b51422019-08-18 21:44:12 +02003275 return OK;
3276}
3277
3278/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003279 * Expand shell command matches in one directory of $PATH.
3280 */
3281 static void
3282expand_shellcmd_onedir(
3283 char_u *buf,
3284 char_u *s,
3285 size_t l,
3286 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003287 char_u ***matches,
3288 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003289 int flags,
3290 hashtab_T *ht,
3291 garray_T *gap)
3292{
3293 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003294 hash_T hash;
3295 hashitem_T *hi;
3296
3297 vim_strncpy(buf, s, l);
3298 add_pathsep(buf);
3299 l = STRLEN(buf);
3300 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3301
3302 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003303 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003304 if (ret != OK)
3305 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003306
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003307 if (ga_grow(gap, *numMatches) == FAIL)
3308 {
3309 FreeWild(*numMatches, *matches);
3310 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003311 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003312
3313 for (int i = 0; i < *numMatches; ++i)
3314 {
3315 char_u *name = (*matches)[i];
3316
3317 if (STRLEN(name) > l)
3318 {
3319 // Check if this name was already found.
3320 hash = hash_hash(name + l);
3321 hi = hash_lookup(ht, name + l, hash);
3322 if (HASHITEM_EMPTY(hi))
3323 {
3324 // Remove the path that was prepended.
3325 STRMOVE(name, name + l);
3326 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3327 hash_add_item(ht, hi, name, hash);
3328 name = NULL;
3329 }
3330 }
3331 vim_free(name);
3332 }
3333 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003334}
3335
3336/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003337 * Complete a shell command.
3338 * Returns FAIL or OK;
3339 */
3340 static int
3341expand_shellcmd(
3342 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003343 char_u ***matches, // return: array with matches
3344 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003345 int flagsarg) // EW_ flags
3346{
3347 char_u *pat;
3348 int i;
3349 char_u *path = NULL;
3350 int mustfree = FALSE;
3351 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003352 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353 size_t l;
3354 char_u *s, *e;
3355 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003356 int did_curdir = FALSE;
3357 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003358
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003359 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360 if (buf == NULL)
3361 return FAIL;
3362
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003363 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003364 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003365 if (pat == NULL)
3366 {
3367 vim_free(buf);
3368 return FAIL;
3369 }
3370
Bram Moolenaar66b51422019-08-18 21:44:12 +02003371 for (i = 0; pat[i]; ++i)
3372 if (pat[i] == '\\' && pat[i + 1] == ' ')
3373 STRMOVE(pat + i, pat + i + 1);
3374
3375 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3376
3377 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3378 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3379 path = (char_u *)".";
3380 else
3381 {
3382 // For an absolute name we don't use $PATH.
3383 if (!mch_isFullName(pat))
3384 path = vim_getenv((char_u *)"PATH", &mustfree);
3385 if (path == NULL)
3386 path = (char_u *)"";
3387 }
3388
3389 // Go over all directories in $PATH. Expand matches in that directory and
3390 // collect them in "ga". When "." is not in $PATH also expand for the
3391 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003392 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003393 hash_init(&found_ht);
3394 for (s = path; ; s = e)
3395 {
K.Takata161b6ac2022-11-14 15:31:07 +00003396#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003397 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003398#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003399 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003400#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003401 if (e == NULL)
3402 e = s + STRLEN(s);
3403
3404 if (*s == NUL)
3405 {
3406 if (did_curdir)
3407 break;
3408 // Find directories in the current directory, path is empty.
3409 did_curdir = TRUE;
3410 flags |= EW_DIR;
3411 }
3412 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3413 {
3414 did_curdir = TRUE;
3415 flags |= EW_DIR;
3416 }
3417 else
3418 // Do not match directories inside a $PATH item.
3419 flags &= ~EW_DIR;
3420
3421 l = e - s;
3422 if (l > MAXPATHL - 5)
3423 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003424
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003425 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003426 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003427
Bram Moolenaar66b51422019-08-18 21:44:12 +02003428 if (*e != NUL)
3429 ++e;
3430 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003431 *matches = ga.ga_data;
3432 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003433
3434 vim_free(buf);
3435 vim_free(pat);
3436 if (mustfree)
3437 vim_free(path);
3438 hash_clear(&found_ht);
3439 return OK;
3440}
3441
K.Takata161b6ac2022-11-14 15:31:07 +00003442#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003443/*
3444 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003445 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003446 */
3447 static void *
3448call_user_expand_func(
3449 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003450 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003451{
3452 cmdline_info_T *ccline = get_cmdline_info();
3453 int keep = 0;
3454 typval_T args[4];
3455 sctx_T save_current_sctx = current_sctx;
3456 char_u *pat = NULL;
3457 void *ret;
3458
3459 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3460 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003461
3462 if (ccline->cmdbuff != NULL)
3463 {
3464 keep = ccline->cmdbuff[ccline->cmdlen];
3465 ccline->cmdbuff[ccline->cmdlen] = 0;
3466 }
3467
3468 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3469
3470 args[0].v_type = VAR_STRING;
3471 args[0].vval.v_string = pat;
3472 args[1].v_type = VAR_STRING;
3473 args[1].vval.v_string = xp->xp_line;
3474 args[2].v_type = VAR_NUMBER;
3475 args[2].vval.v_number = xp->xp_col;
3476 args[3].v_type = VAR_UNKNOWN;
3477
3478 current_sctx = xp->xp_script_ctx;
3479
3480 ret = user_expand_func(xp->xp_arg, 3, args);
3481
3482 current_sctx = save_current_sctx;
3483 if (ccline->cmdbuff != NULL)
3484 ccline->cmdbuff[ccline->cmdlen] = keep;
3485
3486 vim_free(pat);
3487 return ret;
3488}
3489
3490/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003491 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3492 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003493 */
3494 static int
3495ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003496 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003497 expand_T *xp,
3498 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003499 char_u ***matches,
3500 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003501{
3502 char_u *retstr;
3503 char_u *s;
3504 char_u *e;
3505 int keep;
3506 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003507 int fuzzy;
3508 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003509 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003510
3511 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003512 *matches = NULL;
3513 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003514
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003515 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003516 if (retstr == NULL)
3517 return FAIL;
3518
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003519 if (!fuzzy)
3520 ga_init2(&ga, sizeof(char *), 3);
3521 else
3522 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3523
Bram Moolenaar66b51422019-08-18 21:44:12 +02003524 for (s = retstr; *s != NUL; s = e)
3525 {
3526 e = vim_strchr(s, '\n');
3527 if (e == NULL)
3528 e = s + STRLEN(s);
3529 keep = *e;
3530 *e = NUL;
3531
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003532 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003533 {
3534 if (!fuzzy)
3535 match = vim_regexec(regmatch, s, (colnr_T)0);
3536 else
3537 {
3538 score = fuzzy_match_str(s, pat);
3539 match = (score != 0);
3540 }
3541 }
3542 else
3543 match = TRUE; // match everything
3544
Bram Moolenaar66b51422019-08-18 21:44:12 +02003545 *e = keep;
3546
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003547 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003548 {
3549 if (ga_grow(&ga, 1) == FAIL)
3550 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003551 if (!fuzzy)
3552 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3553 else
3554 {
3555 fuzmatch_str_T *fuzmatch =
3556 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003557 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003558 fuzmatch->str = vim_strnsave(s, e - s);
3559 fuzmatch->score = score;
3560 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003561 ++ga.ga_len;
3562 }
3563
3564 if (*e != NUL)
3565 ++e;
3566 }
3567 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003568
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003569 if (ga.ga_len == 0)
3570 return OK;
3571
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003572 if (!fuzzy)
3573 {
3574 *matches = ga.ga_data;
3575 *numMatches = ga.ga_len;
3576 }
3577 else
3578 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003579 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3580 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003581 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003582 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003583 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003584 return OK;
3585}
3586
3587/*
3588 * Expand names with a list returned by a function defined by the user.
3589 */
3590 static int
3591ExpandUserList(
3592 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003593 char_u ***matches,
3594 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003595{
3596 list_T *retlist;
3597 listitem_T *li;
3598 garray_T ga;
3599
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003600 *matches = NULL;
3601 *numMatches = 0;
3602 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003603 if (retlist == NULL)
3604 return FAIL;
3605
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003606 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003607 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003608 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003609 {
3610 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3611 continue; // Skip non-string items and empty strings
3612
3613 if (ga_grow(&ga, 1) == FAIL)
3614 break;
3615
3616 ((char_u **)ga.ga_data)[ga.ga_len] =
3617 vim_strsave(li->li_tv.vval.v_string);
3618 ++ga.ga_len;
3619 }
3620 list_unref(retlist);
3621
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003622 *matches = ga.ga_data;
3623 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003624 return OK;
3625}
K.Takata161b6ac2022-11-14 15:31:07 +00003626#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003627
3628/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003629 * Expand "file" for all comma-separated directories in "path".
3630 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003631 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003632 */
3633 void
3634globpath(
3635 char_u *path,
3636 char_u *file,
3637 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003638 int expand_options,
3639 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003640{
3641 expand_T xpc;
3642 char_u *buf;
3643 int i;
3644 int num_p;
3645 char_u **p;
3646
3647 buf = alloc(MAXPATHL);
3648 if (buf == NULL)
3649 return;
3650
3651 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003652 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003653
3654 // Loop over all entries in {path}.
3655 while (*path != NUL)
3656 {
3657 // Copy one item of the path to buf[] and concatenate the file name.
3658 copy_option_part(&path, buf, MAXPATHL, ",");
3659 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3660 {
K.Takata161b6ac2022-11-14 15:31:07 +00003661#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003662 // Using the platform's path separator (\) makes vim incorrectly
3663 // treat it as an escape character, use '/' instead.
3664 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3665 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003666#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003667 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003668#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003669 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003670 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003671 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3672 {
3673 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3674
3675 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003676 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003677 for (i = 0; i < num_p; ++i)
3678 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003679 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003680 ++ga->ga_len;
3681 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003682 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003683 }
3684 }
3685 }
3686
3687 vim_free(buf);
3688}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003689
Bram Moolenaareadee482020-09-04 15:37:31 +02003690/*
3691 * Translate some keys pressed when 'wildmenu' is used.
3692 */
3693 int
3694wildmenu_translate_key(
3695 cmdline_info_T *cclp,
3696 int key,
3697 expand_T *xp,
3698 int did_wild_list)
3699{
3700 int c = key;
3701
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003702 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003703 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003704 // When the popup menu is used for cmdline completion:
3705 // Up : go to the previous item in the menu
3706 // Down : go to the next item in the menu
3707 // Left : go to the parent directory
3708 // Right: list the files in the selected directory
3709 switch (c)
3710 {
3711 case K_UP: c = K_LEFT; break;
3712 case K_DOWN: c = K_RIGHT; break;
3713 case K_LEFT: c = K_UP; break;
3714 case K_RIGHT: c = K_DOWN; break;
3715 default: break;
3716 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003717 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003718
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003719 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003720 {
3721 if (c == K_LEFT)
3722 c = Ctrl_P;
3723 else if (c == K_RIGHT)
3724 c = Ctrl_N;
3725 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003726
Bram Moolenaareadee482020-09-04 15:37:31 +02003727 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003728 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003729 && cclp->cmdpos > 1
3730 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3731 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3732 && (c == '\n' || c == '\r' || c == K_KENTER))
3733 c = K_DOWN;
3734
3735 return c;
3736}
3737
3738/*
3739 * Delete characters on the command line, from "from" to the current
3740 * position.
3741 */
3742 static void
3743cmdline_del(cmdline_info_T *cclp, int from)
3744{
3745 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3746 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3747 cclp->cmdlen -= cclp->cmdpos - from;
3748 cclp->cmdpos = from;
3749}
3750
3751/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003752 * Handle a key pressed when the wild menu for the menu names
3753 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003754 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003755 static int
3756wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003757{
Bram Moolenaareadee482020-09-04 15:37:31 +02003758 int i;
3759 int j;
3760
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003761 // Hitting <Down> after "emenu Name.": complete submenu
3762 if (key == K_DOWN && cclp->cmdpos > 0
3763 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003764 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003765 key = p_wc;
3766 KeyTyped = TRUE; // in case the key was mapped
3767 }
3768 else if (key == K_UP)
3769 {
3770 // Hitting <Up>: Remove one submenu name in front of the
3771 // cursor
3772 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003773
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003774 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3775 i = 0;
3776 while (--j > 0)
3777 {
3778 // check for start of menu name
3779 if (cclp->cmdbuff[j] == ' '
3780 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003781 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003782 i = j + 1;
3783 break;
3784 }
3785 // check for start of submenu name
3786 if (cclp->cmdbuff[j] == '.'
3787 && cclp->cmdbuff[j - 1] != '\\')
3788 {
3789 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003790 {
3791 i = j + 1;
3792 break;
3793 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003794 else
3795 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003796 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003797 }
3798 if (i > 0)
3799 cmdline_del(cclp, i);
3800 key = p_wc;
3801 KeyTyped = TRUE; // in case the key was mapped
3802 xp->xp_context = EXPAND_NOTHING;
3803 }
3804
3805 return key;
3806}
3807
3808/*
3809 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3810 * directory names (EXPAND_DIRECTORIES) or shell command names
3811 * (EXPAND_SHELLCMD) is displayed.
3812 */
3813 static int
3814wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3815{
3816 int i;
3817 int j;
3818 char_u upseg[5];
3819
3820 upseg[0] = PATHSEP;
3821 upseg[1] = '.';
3822 upseg[2] = '.';
3823 upseg[3] = PATHSEP;
3824 upseg[4] = NUL;
3825
3826 if (key == K_DOWN
3827 && cclp->cmdpos > 0
3828 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3829 && (cclp->cmdpos < 3
3830 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3831 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3832 {
3833 // go down a directory
3834 key = p_wc;
3835 KeyTyped = TRUE; // in case the key was mapped
3836 }
3837 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3838 {
3839 // If in a direct ancestor, strip off one ../ to go down
3840 int found = FALSE;
3841
3842 j = cclp->cmdpos;
3843 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3844 while (--j > i)
3845 {
3846 if (has_mbyte)
3847 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3848 if (vim_ispathsep(cclp->cmdbuff[j]))
3849 {
3850 found = TRUE;
3851 break;
3852 }
3853 }
3854 if (found
3855 && cclp->cmdbuff[j - 1] == '.'
3856 && cclp->cmdbuff[j - 2] == '.'
3857 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3858 {
3859 cmdline_del(cclp, j - 2);
3860 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003861 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003862 }
3863 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003864 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003865 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003866 // go up a directory
3867 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003868
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003869 j = cclp->cmdpos - 1;
3870 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3871 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003872 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003873 if (has_mbyte)
3874 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3875 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003876#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003877 && vim_strchr((char_u *)" *?[{`$%#",
3878 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003879#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003880 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003881 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003882 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003883 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003884 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003885 break;
3886 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003887 else
3888 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003889 }
3890 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003891
3892 if (!found)
3893 j = i;
3894 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3895 j += 4;
3896 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3897 && j == i)
3898 j += 3;
3899 else
3900 j = 0;
3901 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003902 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003903 // TODO this is only for DOS/UNIX systems - need to put in
3904 // machine-specific stuff here and in upseg init
3905 cmdline_del(cclp, j);
3906 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003907 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003908 else if (cclp->cmdpos > i)
3909 cmdline_del(cclp, i);
3910
3911 // Now complete in the new directory. Set KeyTyped in case the
3912 // Up key came from a mapping.
3913 key = p_wc;
3914 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003915 }
3916
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003917 return key;
3918}
3919
3920/*
3921 * Handle a key pressed when the wild menu is displayed
3922 */
3923 int
3924wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3925{
3926 if (xp->xp_context == EXPAND_MENUNAMES)
3927 return wildmenu_process_key_menunames(cclp, key, xp);
3928 else if ((xp->xp_context == EXPAND_FILES
3929 || xp->xp_context == EXPAND_DIRECTORIES
3930 || xp->xp_context == EXPAND_SHELLCMD))
3931 return wildmenu_process_key_filenames(cclp, key, xp);
3932
3933 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003934}
3935
3936/*
3937 * Free expanded names when finished walking through the matches
3938 */
3939 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003940wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003941{
3942 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02003943
3944 if (!p_wmnu || wild_menu_showing == 0)
3945 return;
3946
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003947#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003948 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02003949 if (cclp->input_fn)
3950 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003951#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003952
3953 if (wild_menu_showing == WM_SCROLLED)
3954 {
3955 // Entered command line, move it up
3956 cmdline_row--;
3957 redrawcmd();
3958 }
3959 else if (save_p_ls != -1)
3960 {
3961 // restore 'laststatus' and 'winminheight'
3962 p_ls = save_p_ls;
3963 p_wmh = save_p_wmh;
3964 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003965 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003966 redrawcmd();
3967 save_p_ls = -1;
3968 }
3969 else
3970 {
3971 win_redraw_last_status(topframe);
3972 redraw_statuslines();
3973 }
3974 KeyTyped = skt;
3975 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003976#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003977 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003978 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003979#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003980}
Bram Moolenaareadee482020-09-04 15:37:31 +02003981
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003982#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003983/*
3984 * "getcompletion()" function
3985 */
3986 void
3987f_getcompletion(typval_T *argvars, typval_T *rettv)
3988{
3989 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003990 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003991 expand_T xpc;
3992 int filtered = FALSE;
3993 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003994 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003995
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003996 if (in_vim9script()
3997 && (check_for_string_arg(argvars, 0) == FAIL
3998 || check_for_string_arg(argvars, 1) == FAIL
3999 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4000 return;
4001
ii144785fe02021-11-21 12:13:56 +00004002 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004003 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004004 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004005 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004006
4007 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004008 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004009
4010 if (p_wic)
4011 options |= WILD_ICASE;
4012
4013 // For filtered results, 'wildignore' is used
4014 if (!filtered)
4015 options |= WILD_KEEP_ALL;
4016
4017 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004018 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004019 {
ii144785fe02021-11-21 12:13:56 +00004020 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004021 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00004022 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004023 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004024 else
4025 {
ii144785fe02021-11-21 12:13:56 +00004026 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004027 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4028
4029 xpc.xp_context = cmdcomplete_str_to_type(type);
4030 if (xpc.xp_context == EXPAND_NOTHING)
4031 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004032 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004033 return;
4034 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004035
4036# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004037 if (xpc.xp_context == EXPAND_MENUS)
4038 {
4039 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4040 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4041 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004042# endif
4043# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004044 if (xpc.xp_context == EXPAND_CSCOPE)
4045 {
4046 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4047 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4048 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004049# endif
4050# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004051 if (xpc.xp_context == EXPAND_SIGN)
4052 {
4053 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4054 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4055 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004056# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004057 if (xpc.xp_context == EXPAND_RUNTIME)
4058 {
4059 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4060 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4061 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004062 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004063
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004064 if (cmdline_fuzzy_completion_supported(&xpc))
4065 // when fuzzy matching, don't modify the search string
4066 pat = vim_strsave(xpc.xp_pattern);
4067 else
4068 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4069
Bram Moolenaar93a10962022-06-16 11:42:09 +01004070 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004071 {
4072 int i;
4073
4074 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4075
4076 for (i = 0; i < xpc.xp_numfiles; i++)
4077 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4078 }
4079 vim_free(pat);
4080 ExpandCleanup(&xpc);
4081}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004082#endif // FEAT_EVAL