blob: 4fe9bd35bcceaecb4370e49aec3d7dc7a3f7e5e6 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000018static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000020 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000022static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000026static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020028#endif
29
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000030// "compl_match_array" points the currently displayed list of entries in the
31// popup menu. It is NULL when there is no popup menu.
32static pumitem_T *compl_match_array = NULL;
33static int compl_match_arraysize;
34// First column in cmdline of the matched item for completion.
35static int compl_startcol;
36static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
zeertzjqc51a3762022-12-10 10:22:29 +000038#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
52 && xp->xp_context != EXPAND_FILES
53 && xp->xp_context != EXPAND_FILES_IN_PATH
54 && xp->xp_context != EXPAND_FILETYPE
55 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
57 && xp->xp_context != EXPAND_OWNSYNTAX
58 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000059 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060 && xp->xp_context != EXPAND_SHELLCMD
61 && xp->xp_context != EXPAND_TAGS
62 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_USER_LIST);
64}
65
66/*
67 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000068 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
69 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000070 */
71 int
72cmdline_fuzzy_complete(char_u *fuzzystr)
73{
74 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
75}
76
77/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000078 * sort function for the completion matches.
79 * <SNR> functions should be sorted to the end.
80 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020081 static int
82sort_func_compare(const void *s1, const void *s2)
83{
84 char_u *p1 = *(char_u **)s1;
85 char_u *p2 = *(char_u **)s2;
86
87 if (*p1 != '<' && *p2 == '<') return -1;
88 if (*p1 == '<' && *p2 != '<') return 1;
89 return STRCMP(p1, p2);
90}
Bram Moolenaar66b51422019-08-18 21:44:12 +020091
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000092/*
93 * Escape special characters in the cmdline completion matches.
94 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020095 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000096wildescape(
97 expand_T *xp,
98 char_u *str,
99 int numfiles,
100 char_u **files)
101{
102 char_u *p;
103 int vse_what = xp->xp_context == EXPAND_BUFFERS
104 ? VSE_BUFFER : VSE_NONE;
105
106 if (xp->xp_context == EXPAND_FILES
107 || xp->xp_context == EXPAND_FILES_IN_PATH
108 || xp->xp_context == EXPAND_SHELLCMD
109 || xp->xp_context == EXPAND_BUFFERS
110 || xp->xp_context == EXPAND_DIRECTORIES)
111 {
112 // Insert a backslash into a file name before a space, \, %, #
113 // and wildmatch characters, except '~'.
114 for (int i = 0; i < numfiles; ++i)
115 {
116 // for ":set path=" we need to escape spaces twice
117 if (xp->xp_backslash == XP_BS_THREE)
118 {
119 p = vim_strsave_escaped(files[i], (char_u *)" ");
120 if (p != NULL)
121 {
122 vim_free(files[i]);
123 files[i] = p;
124#if defined(BACKSLASH_IN_FILENAME)
125 p = vim_strsave_escaped(files[i], (char_u *)" ");
126 if (p != NULL)
127 {
128 vim_free(files[i]);
129 files[i] = p;
130 }
131#endif
132 }
133 }
134#ifdef BACKSLASH_IN_FILENAME
135 p = vim_strsave_fnameescape(files[i], vse_what);
136#else
137 p = vim_strsave_fnameescape(files[i],
138 xp->xp_shell ? VSE_SHELL : vse_what);
139#endif
140 if (p != NULL)
141 {
142 vim_free(files[i]);
143 files[i] = p;
144 }
145
146 // If 'str' starts with "\~", replace "~" at start of
147 // files[i] with "\~".
148 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
149 escape_fname(&files[i]);
150 }
151 xp->xp_backslash = XP_BS_NONE;
152
153 // If the first file starts with a '+' escape it. Otherwise it
154 // could be seen as "+cmd".
155 if (*files[0] == '+')
156 escape_fname(&files[0]);
157 }
158 else if (xp->xp_context == EXPAND_TAGS)
159 {
160 // Insert a backslash before characters in a tag name that
161 // would terminate the ":tag" command.
162 for (int i = 0; i < numfiles; ++i)
163 {
164 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
165 if (p != NULL)
166 {
167 vim_free(files[i]);
168 files[i] = p;
169 }
170 }
171 }
172}
173
174/*
175 * Escape special characters in the cmdline completion matches.
176 */
177 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200178ExpandEscape(
179 expand_T *xp,
180 char_u *str,
181 int numfiles,
182 char_u **files,
183 int options)
184{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200185 // May change home directory back to "~"
186 if (options & WILD_HOME_REPLACE)
187 tilde_replace(str, numfiles, files);
188
189 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000190 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200191}
192
193/*
194 * Return FAIL if this is not an appropriate context in which to do
195 * completion of anything, return OK if it is (even if there are no matches).
196 * For the caller, this means that the character is just passed through like a
197 * normal character (instead of being expanded). This allows :s/^I^D etc.
198 */
199 int
200nextwild(
201 expand_T *xp,
202 int type,
203 int options, // extra options for ExpandOne()
204 int escape) // if TRUE, escape the returned matches
205{
206 cmdline_info_T *ccline = get_cmdline_info();
207 int i, j;
208 char_u *p1;
209 char_u *p2;
210 int difflen;
211 int v;
212
213 if (xp->xp_numfiles == -1)
214 {
215 set_expand_context(xp);
216 cmd_showtail = expand_showtail(xp);
217 }
218
219 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
220 {
221 beep_flush();
222 return OK; // Something illegal on command line
223 }
224 if (xp->xp_context == EXPAND_NOTHING)
225 {
226 // Caller can use the character as a normal char instead
227 return FAIL;
228 }
229
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000230 // If cmd_silent is set then don't show the dots, because redrawcmd() below
231 // won't remove them.
232 if (!cmd_silent)
233 {
234 msg_puts("..."); // show that we are busy
235 out_flush();
236 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200237
238 i = (int)(xp->xp_pattern - ccline->cmdbuff);
239 xp->xp_pattern_len = ccline->cmdpos - i;
240
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000241 if (type == WILD_NEXT || type == WILD_PREV
242 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200243 {
244 // Get next/previous match for a previous expanded pattern.
245 p2 = ExpandOne(xp, NULL, NULL, 0, type);
246 }
247 else
248 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000249 if (cmdline_fuzzy_completion_supported(xp))
250 // If fuzzy matching, don't modify the search string
251 p1 = vim_strsave(xp->xp_pattern);
252 else
253 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
254
Bram Moolenaar66b51422019-08-18 21:44:12 +0200255 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000256 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200257 p2 = NULL;
258 else
259 {
260 int use_options = options |
261 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
262 if (escape)
263 use_options |= WILD_ESCAPE;
264
265 if (p_wic)
266 use_options += WILD_ICASE;
267 p2 = ExpandOne(xp, p1,
268 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
269 use_options, type);
270 vim_free(p1);
271 // longest match: make sure it is not shorter, happens with :help
272 if (p2 != NULL && type == WILD_LONGEST)
273 {
274 for (j = 0; j < xp->xp_pattern_len; ++j)
275 if (ccline->cmdbuff[i + j] == '*'
276 || ccline->cmdbuff[i + j] == '?')
277 break;
278 if ((int)STRLEN(p2) < j)
279 VIM_CLEAR(p2);
280 }
281 }
282 }
283
284 if (p2 != NULL && !got_int)
285 {
286 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
287 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
288 {
289 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
290 xp->xp_pattern = ccline->cmdbuff + i;
291 }
292 else
293 v = OK;
294 if (v == OK)
295 {
296 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
297 &ccline->cmdbuff[ccline->cmdpos],
298 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
299 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
300 ccline->cmdlen += difflen;
301 ccline->cmdpos += difflen;
302 }
303 }
304 vim_free(p2);
305
306 redrawcmd();
307 cursorcmd();
308
309 // When expanding a ":map" command and no matches are found, assume that
310 // the key is supposed to be inserted literally
311 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
312 return FAIL;
313
314 if (xp->xp_numfiles <= 0 && p2 == NULL)
315 beep_flush();
316 else if (xp->xp_numfiles == 1)
317 // free expanded pattern
318 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
319
320 return OK;
321}
322
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000323/*
324 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000325 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000326 */
327 static int
328cmdline_pum_create(
329 cmdline_info_T *ccline,
330 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000331 char_u **matches,
332 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000333 int showtail)
334{
335 int i;
336 int columns;
337
338 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000339 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000340 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000341 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000342 {
zeertzjqc51a3762022-12-10 10:22:29 +0000343 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000344 compl_match_array[i].pum_info = NULL;
345 compl_match_array[i].pum_extra = NULL;
346 compl_match_array[i].pum_kind = NULL;
347 }
348
349 // Compute the popup menu starting column
350 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
351 columns = vim_strsize(xp->xp_pattern);
352 if (showtail)
353 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000354 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 }
357 if (columns >= compl_startcol)
358 compl_startcol = 0;
359 else
360 compl_startcol -= columns;
361
362 // no default selection
363 compl_selected = -1;
364
365 cmdline_pum_display();
366
367 return EXPAND_OK;
368}
369
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000370/*
371 * Display the cmdline completion matches in a popup menu
372 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000373 void
374cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000375{
376 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
377}
378
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000379/*
380 * Returns TRUE if the cmdline completion popup menu is being displayed.
381 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000382 int
383cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000384{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100385 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000386}
387
388/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000389 * Remove the cmdline completion popup menu (if present), free the list of
390 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000391 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000392 void
393cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000394{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000395 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100396 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000397
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000398 pum_undisplay();
399 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000400 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000401 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000402 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000403 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100404
405 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
406 // as a side effect.
407 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000408}
409
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000410 void
411cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000412{
413 cmdline_pum_remove();
414 wildmenu_cleanup(cclp);
415}
416
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000417/*
418 * Returns the starting column number to use for the cmdline completion popup
419 * menu.
420 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000421 int
422cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423{
424 return compl_startcol;
425}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426
Bram Moolenaar66b51422019-08-18 21:44:12 +0200427/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000428 * Return the number of characters that should be skipped in a status match.
429 * These are backslashes used for escaping. Do show backslashes in help tags.
430 */
431 static int
432skip_status_match_char(expand_T *xp, char_u *s)
433{
434 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
435#ifdef FEAT_MENU
436 || ((xp->xp_context == EXPAND_MENUS
437 || xp->xp_context == EXPAND_MENUNAMES)
438 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
439#endif
440 )
441 {
442#ifndef BACKSLASH_IN_FILENAME
443 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
444 return 2;
445#endif
446 return 1;
447 }
448 return 0;
449}
450
451/*
452 * Get the length of an item as it will be shown in the status line.
453 */
454 static int
455status_match_len(expand_T *xp, char_u *s)
456{
457 int len = 0;
458
459#ifdef FEAT_MENU
460 int emenu = xp->xp_context == EXPAND_MENUS
461 || xp->xp_context == EXPAND_MENUNAMES;
462
463 // Check for menu separators - replace with '|'.
464 if (emenu && menu_is_separator(s))
465 return 1;
466#endif
467
468 while (*s != NUL)
469 {
470 s += skip_status_match_char(xp, s);
471 len += ptr2cells(s);
472 MB_PTR_ADV(s);
473 }
474
475 return len;
476}
477
478/*
479 * Show wildchar matches in the status line.
480 * Show at least the "match" item.
481 * We start at item 'first_match' in the list and show all matches that fit.
482 *
483 * If inversion is possible we use it. Else '=' characters are used.
484 */
485 static void
486win_redr_status_matches(
487 expand_T *xp,
488 int num_matches,
489 char_u **matches, // list of matches
490 int match,
491 int showtail)
492{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000493 int row;
494 char_u *buf;
495 int len;
496 int clen; // length in screen cells
497 int fillchar;
498 int attr;
499 int i;
500 int highlight = TRUE;
501 char_u *selstart = NULL;
502 int selstart_col = 0;
503 char_u *selend = NULL;
504 static int first_match = 0;
505 int add_left = FALSE;
506 char_u *s;
507#ifdef FEAT_MENU
508 int emenu;
509#endif
510 int l;
511
512 if (matches == NULL) // interrupted completion?
513 return;
514
515 if (has_mbyte)
516 buf = alloc(Columns * MB_MAXBYTES + 1);
517 else
518 buf = alloc(Columns + 1);
519 if (buf == NULL)
520 return;
521
522 if (match == -1) // don't show match but original text
523 {
524 match = 0;
525 highlight = FALSE;
526 }
527 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000528 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000529 if (match == 0)
530 first_match = 0;
531 else if (match < first_match)
532 {
533 // jumping left, as far as we can go
534 first_match = match;
535 add_left = TRUE;
536 }
537 else
538 {
539 // check if match fits on the screen
540 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000541 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000542 if (first_match > 0)
543 clen += 2;
544 // jumping right, put match at the left
545 if ((long)clen > Columns)
546 {
547 first_match = match;
548 // if showing the last match, we can add some on the left
549 clen = 2;
550 for (i = match; i < num_matches; ++i)
551 {
zeertzjqc51a3762022-12-10 10:22:29 +0000552 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000553 if ((long)clen >= Columns)
554 break;
555 }
556 if (i == num_matches)
557 add_left = TRUE;
558 }
559 }
560 if (add_left)
561 while (first_match > 0)
562 {
zeertzjqc51a3762022-12-10 10:22:29 +0000563 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000564 if ((long)clen >= Columns)
565 break;
566 --first_match;
567 }
568
569 fillchar = fillchar_status(&attr, curwin);
570
571 if (first_match == 0)
572 {
573 *buf = NUL;
574 len = 0;
575 }
576 else
577 {
578 STRCPY(buf, "< ");
579 len = 2;
580 }
581 clen = len;
582
583 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000584 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000585 {
586 if (i == match)
587 {
588 selstart = buf + len;
589 selstart_col = clen;
590 }
591
zeertzjqc51a3762022-12-10 10:22:29 +0000592 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000593 // Check for menu separators - replace with '|'
594#ifdef FEAT_MENU
595 emenu = (xp->xp_context == EXPAND_MENUS
596 || xp->xp_context == EXPAND_MENUNAMES);
597 if (emenu && menu_is_separator(s))
598 {
599 STRCPY(buf + len, transchar('|'));
600 l = (int)STRLEN(buf + len);
601 len += l;
602 clen += l;
603 }
604 else
605#endif
606 for ( ; *s != NUL; ++s)
607 {
608 s += skip_status_match_char(xp, s);
609 clen += ptr2cells(s);
610 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
611 {
612 STRNCPY(buf + len, s, l);
613 s += l - 1;
614 len += l;
615 }
616 else
617 {
618 STRCPY(buf + len, transchar_byte(*s));
619 len += (int)STRLEN(buf + len);
620 }
621 }
622 if (i == match)
623 selend = buf + len;
624
625 *(buf + len++) = ' ';
626 *(buf + len++) = ' ';
627 clen += 2;
628 if (++i == num_matches)
629 break;
630 }
631
632 if (i != num_matches)
633 {
634 *(buf + len++) = '>';
635 ++clen;
636 }
637
638 buf[len] = NUL;
639
640 row = cmdline_row - 1;
641 if (row >= 0)
642 {
643 if (wild_menu_showing == 0)
644 {
645 if (msg_scrolled > 0)
646 {
647 // Put the wildmenu just above the command line. If there is
648 // no room, scroll the screen one line up.
649 if (cmdline_row == Rows - 1)
650 {
651 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
652 ++msg_scrolled;
653 }
654 else
655 {
656 ++cmdline_row;
657 ++row;
658 }
659 wild_menu_showing = WM_SCROLLED;
660 }
661 else
662 {
663 // Create status line if needed by setting 'laststatus' to 2.
664 // Set 'winminheight' to zero to avoid that the window is
665 // resized.
666 if (lastwin->w_status_height == 0)
667 {
668 save_p_ls = p_ls;
669 save_p_wmh = p_wmh;
670 p_ls = 2;
671 p_wmh = 0;
672 last_status(FALSE);
673 }
674 wild_menu_showing = WM_SHOWN;
675 }
676 }
677
678 screen_puts(buf, row, 0, attr);
679 if (selstart != NULL && highlight)
680 {
681 *selend = NUL;
682 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
683 }
684
685 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
686 }
687
688 win_redraw_last_status(topframe);
689 vim_free(buf);
690}
691
692/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000693 * Get the next or prev cmdline completion match. The index of the match is set
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000694 * in "p_findex"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000695 */
696 static char_u *
697get_next_or_prev_match(
698 int mode,
699 expand_T *xp,
700 int *p_findex,
701 char_u *orig_save)
702{
703 int findex = *p_findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000704 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000705
706 if (xp->xp_numfiles <= 0)
707 return NULL;
708
709 if (mode == WILD_PREV)
710 {
711 if (findex == -1)
712 findex = xp->xp_numfiles;
713 --findex;
714 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000715 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000716 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000717 else if (mode == WILD_PAGEUP)
718 {
719 if (findex == 0)
720 // at the first entry, don't select any entries
721 findex = -1;
722 else if (findex == -1)
723 // no entry is selected. select the last entry
724 findex = xp->xp_numfiles - 1;
725 else
726 {
727 // go up by the pum height
728 ht = pum_get_height();
729 if (ht > 3)
730 ht -= 2;
731 findex -= ht;
732 if (findex < 0)
733 // few entries left, select the first entry
734 findex = 0;
735 }
736 }
737 else // mode == WILD_PAGEDOWN
738 {
739 if (findex == xp->xp_numfiles - 1)
740 // at the last entry, don't select any entries
741 findex = -1;
742 else if (findex == -1)
743 // no entry is selected. select the first entry
744 findex = 0;
745 else
746 {
747 // go down by the pum height
748 ht = pum_get_height();
749 if (ht > 3)
750 ht -= 2;
751 findex += ht;
752 if (findex >= xp->xp_numfiles)
753 // few entries left, select the last entry
754 findex = xp->xp_numfiles - 1;
755 }
756 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000757
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000758 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000759 if (findex < 0)
760 {
761 if (orig_save == NULL)
762 findex = xp->xp_numfiles - 1;
763 else
764 findex = -1;
765 }
766 if (findex >= xp->xp_numfiles)
767 {
768 if (orig_save == NULL)
769 findex = 0;
770 else
771 findex = -1;
772 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000773 if (compl_match_array)
774 {
775 compl_selected = findex;
776 cmdline_pum_display();
777 }
778 else if (p_wmnu)
779 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
780 findex, cmd_showtail);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000781 *p_findex = findex;
782
783 if (findex == -1)
784 return vim_strsave(orig_save);
785
786 return vim_strsave(xp->xp_files[findex]);
787}
788
789/*
790 * Start the command-line expansion and get the matches.
791 */
792 static char_u *
793ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
794{
795 int non_suf_match; // number without matching suffix
796 int i;
797 char_u *ss = NULL;
798
799 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000800 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000801 options) == FAIL)
802 {
803#ifdef FNAME_ILLEGAL
804 // Illegal file name has been silently skipped. But when there
805 // are wildcards, the real problem is that there was no match,
806 // causing the pattern to be added, which has illegal characters.
807 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
808 semsg(_(e_no_match_str_2), str);
809#endif
810 }
811 else if (xp->xp_numfiles == 0)
812 {
813 if (!(options & WILD_SILENT))
814 semsg(_(e_no_match_str_2), str);
815 }
816 else
817 {
818 // Escape the matches for use on the command line.
819 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
820
821 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000822 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000823 {
824 if (xp->xp_numfiles)
825 non_suf_match = xp->xp_numfiles;
826 else
827 non_suf_match = 1;
828 if ((xp->xp_context == EXPAND_FILES
829 || xp->xp_context == EXPAND_DIRECTORIES)
830 && xp->xp_numfiles > 1)
831 {
832 // More than one match; check suffix.
833 // The files will have been sorted on matching suffix in
834 // expand_wildcards, only need to check the first two.
835 non_suf_match = 0;
836 for (i = 0; i < 2; ++i)
837 if (match_suffix(xp->xp_files[i]))
838 ++non_suf_match;
839 }
840 if (non_suf_match != 1)
841 {
842 // Can we ever get here unless it's while expanding
843 // interactively? If not, we can get rid of this all
844 // together. Don't really want to wait for this message
845 // (and possibly have to hit return to continue!).
846 if (!(options & WILD_SILENT))
847 emsg(_(e_too_many_file_names));
848 else if (!(options & WILD_NO_BEEP))
849 beep_flush();
850 }
851 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
852 ss = vim_strsave(xp->xp_files[0]);
853 }
854 }
855
856 return ss;
857}
858
859/*
860 * Return the longest common part in the list of cmdline completion matches.
861 */
862 static char_u *
863find_longest_match(expand_T *xp, int options)
864{
865 long_u len;
866 int mb_len = 1;
867 int c0, ci;
868 int i;
869 char_u *ss;
870
871 for (len = 0; xp->xp_files[0][len]; len += mb_len)
872 {
873 if (has_mbyte)
874 {
875 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
876 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
877 }
878 else
879 c0 = xp->xp_files[0][len];
880 for (i = 1; i < xp->xp_numfiles; ++i)
881 {
882 if (has_mbyte)
883 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
884 else
885 ci = xp->xp_files[i][len];
886 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
887 || xp->xp_context == EXPAND_FILES
888 || xp->xp_context == EXPAND_SHELLCMD
889 || xp->xp_context == EXPAND_BUFFERS))
890 {
891 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
892 break;
893 }
894 else if (c0 != ci)
895 break;
896 }
897 if (i < xp->xp_numfiles)
898 {
899 if (!(options & WILD_NO_BEEP))
900 vim_beep(BO_WILD);
901 break;
902 }
903 }
904
905 ss = alloc(len + 1);
906 if (ss)
907 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
908
909 return ss;
910}
911
912/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000913 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200914 * Chars that should not be expanded must be preceded with a backslash.
915 * Return a pointer to allocated memory containing the new string.
916 * Return NULL for failure.
917 *
918 * "orig" is the originally expanded string, copied to allocated memory. It
919 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
920 * WILD_PREV "orig" should be NULL.
921 *
922 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
923 * is WILD_EXPAND_FREE or WILD_ALL.
924 *
925 * mode = WILD_FREE: just free previously expanded matches
926 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
927 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
928 * mode = WILD_NEXT: use next match in multiple match, wrap to first
929 * mode = WILD_PREV: use previous match in multiple match, wrap to first
930 * mode = WILD_ALL: return all matches concatenated
931 * mode = WILD_LONGEST: return longest matched part
932 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000933 * mode = WILD_APPLY: apply the item selected in the cmdline completion
934 * popup menu and close the menu.
935 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
936 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200937 *
938 * options = WILD_LIST_NOTFOUND: list entries without a match
939 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
940 * options = WILD_USE_NL: Use '\n' for WILD_ALL
941 * options = WILD_NO_BEEP: Don't beep for multiple matches
942 * options = WILD_ADD_SLASH: add a slash after directory names
943 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
944 * options = WILD_SILENT: don't print warning messages
945 * options = WILD_ESCAPE: put backslash before special chars
946 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200947 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200948 *
949 * The variables xp->xp_context and xp->xp_backslash must have been set!
950 */
951 char_u *
952ExpandOne(
953 expand_T *xp,
954 char_u *str,
955 char_u *orig, // allocated copy of original of expanded string
956 int options,
957 int mode)
958{
959 char_u *ss = NULL;
960 static int findex;
961 static char_u *orig_save = NULL; // kept value of orig
962 int orig_saved = FALSE;
963 int i;
964 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200965
966 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000967 if (mode == WILD_NEXT || mode == WILD_PREV
968 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000969 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200970
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000971 if (mode == WILD_CANCEL)
972 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
973 else if (mode == WILD_APPLY)
974 ss = vim_strsave(findex == -1 ? (orig_save ?
975 orig_save : (char_u *)"") : xp->xp_files[findex]);
976
Bram Moolenaar66b51422019-08-18 21:44:12 +0200977 // free old names
978 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
979 {
980 FreeWild(xp->xp_numfiles, xp->xp_files);
981 xp->xp_numfiles = -1;
982 VIM_CLEAR(orig_save);
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000983
984 // The entries from xp_files may be used in the PUM, remove it.
985 if (compl_match_array != NULL)
986 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200987 }
988 findex = 0;
989
990 if (mode == WILD_FREE) // only release file name
991 return NULL;
992
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000993 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200994 {
995 vim_free(orig_save);
996 orig_save = orig;
997 orig_saved = TRUE;
998
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000999 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001000 }
1001
1002 // Find longest common part
1003 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1004 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001005 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001006 findex = -1; // next p_wc gets first one
1007 }
1008
Bram Moolenaar57e95172022-08-20 19:26:14 +01001009 // Concatenate all matching names. Unless interrupted, this can be slow
1010 // and the result probably won't be used.
1011 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001012 {
1013 len = 0;
1014 for (i = 0; i < xp->xp_numfiles; ++i)
1015 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
1016 ss = alloc(len);
1017 if (ss != NULL)
1018 {
1019 *ss = NUL;
1020 for (i = 0; i < xp->xp_numfiles; ++i)
1021 {
1022 STRCAT(ss, xp->xp_files[i]);
1023 if (i != xp->xp_numfiles - 1)
1024 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1025 }
1026 }
1027 }
1028
1029 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1030 ExpandCleanup(xp);
1031
1032 // Free "orig" if it wasn't stored in "orig_save".
1033 if (!orig_saved)
1034 vim_free(orig);
1035
1036 return ss;
1037}
1038
1039/*
1040 * Prepare an expand structure for use.
1041 */
1042 void
1043ExpandInit(expand_T *xp)
1044{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001045 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001047 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001048}
1049
1050/*
1051 * Cleanup an expand structure after use.
1052 */
1053 void
1054ExpandCleanup(expand_T *xp)
1055{
1056 if (xp->xp_numfiles >= 0)
1057 {
1058 FreeWild(xp->xp_numfiles, xp->xp_files);
1059 xp->xp_numfiles = -1;
1060 }
1061}
1062
1063/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001064 * Display one line of completion matches. Multiple matches are displayed in
1065 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001066 * matches - list of completion match names
1067 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001068 * lines - number of output lines
1069 * linenr - line number of matches to display
1070 * maxlen - maximum number of characters in each line
1071 * showtail - display only the tail of the full path of a file name
1072 * dir_attr - highlight attribute to use for directory names
1073 */
1074 static void
1075showmatches_oneline(
1076 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001077 char_u **matches,
1078 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001079 int lines,
1080 int linenr,
1081 int maxlen,
1082 int showtail,
1083 int dir_attr)
1084{
1085 int i, j;
1086 int isdir;
1087 int lastlen;
1088 char_u *p;
1089
1090 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001091 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001092 {
1093 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1094 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001095 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1096 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001097 msg_advance(maxlen + 1);
1098 msg_puts((char *)p);
1099 msg_advance(maxlen + 3);
1100 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1101 break;
1102 }
1103 for (i = maxlen - lastlen; --i >= 0; )
1104 msg_putchar(' ');
1105 if (xp->xp_context == EXPAND_FILES
1106 || xp->xp_context == EXPAND_SHELLCMD
1107 || xp->xp_context == EXPAND_BUFFERS)
1108 {
1109 // highlight directories
1110 if (xp->xp_numfiles != -1)
1111 {
1112 char_u *halved_slash;
1113 char_u *exp_path;
1114 char_u *path;
1115
1116 // Expansion was done before and special characters
1117 // were escaped, need to halve backslashes. Also
1118 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001119 exp_path = expand_env_save_opt(matches[j], TRUE);
1120 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001121 halved_slash = backslash_halve_save(path);
1122 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001123 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001124 vim_free(exp_path);
1125 if (halved_slash != path)
1126 vim_free(halved_slash);
1127 }
1128 else
1129 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001130 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001131 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001132 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001133 else
1134 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001135 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001136 TRUE);
1137 p = NameBuff;
1138 }
1139 }
1140 else
1141 {
1142 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001143 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001144 }
1145 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1146 }
1147 if (msg_col > 0) // when not wrapped around
1148 {
1149 msg_clr_eos();
1150 msg_putchar('\n');
1151 }
1152 out_flush(); // show one line at a time
1153}
1154
1155/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001156 * Show all matches for completion on the command line.
1157 * Returns EXPAND_NOTHING when the character that triggered expansion should
1158 * be inserted like a normal character.
1159 */
1160 int
1161showmatches(expand_T *xp, int wildmenu UNUSED)
1162{
1163 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001164 int numMatches;
1165 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001166 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001167 int maxlen;
1168 int lines;
1169 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001170 int attr;
1171 int showtail;
1172
1173 if (xp->xp_numfiles == -1)
1174 {
1175 set_expand_context(xp);
1176 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001177 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001178 showtail = expand_showtail(xp);
1179 if (i != EXPAND_OK)
1180 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001181 }
1182 else
1183 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001184 numMatches = xp->xp_numfiles;
1185 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001186 showtail = cmd_showtail;
1187 }
1188
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001189 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001190 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001191 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001192
Bram Moolenaar66b51422019-08-18 21:44:12 +02001193 if (!wildmenu)
1194 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001195 msg_didany = FALSE; // lines_left will be set
1196 msg_start(); // prepare for paging
1197 msg_putchar('\n');
1198 out_flush();
1199 cmdline_row = msg_row;
1200 msg_didany = FALSE; // lines_left will be set again
1201 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001202 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001203
1204 if (got_int)
1205 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001206 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001207 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001208 else
1209 {
1210 // find the length of the longest file name
1211 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001212 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001213 {
1214 if (!showtail && (xp->xp_context == EXPAND_FILES
1215 || xp->xp_context == EXPAND_SHELLCMD
1216 || xp->xp_context == EXPAND_BUFFERS))
1217 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001218 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001219 j = vim_strsize(NameBuff);
1220 }
1221 else
zeertzjqc51a3762022-12-10 10:22:29 +00001222 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 if (j > maxlen)
1224 maxlen = j;
1225 }
1226
1227 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001228 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001229 else
1230 {
1231 // compute the number of columns and lines for the listing
1232 maxlen += 2; // two spaces between file names
1233 columns = ((int)Columns + 2) / maxlen;
1234 if (columns < 1)
1235 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001236 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001237 }
1238
1239 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1240
1241 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1242 {
1243 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1244 msg_clr_eos();
1245 msg_advance(maxlen - 3);
1246 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1247 }
1248
1249 // list the files line by line
1250 for (i = 0; i < lines; ++i)
1251 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001252 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001253 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001254 if (got_int)
1255 {
1256 got_int = FALSE;
1257 break;
1258 }
1259 }
1260
1261 // we redraw the command below the lines that we have just listed
1262 // This is a bit tricky, but it saves a lot of screen updating.
1263 cmdline_row = msg_row; // will put it back later
1264 }
1265
1266 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001267 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001268
1269 return EXPAND_OK;
1270}
1271
1272/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001273 * gettail() version for showmatches() and win_redr_status_matches():
1274 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001275 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001276 static char_u *
1277showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001278{
1279 char_u *p;
1280 char_u *t = s;
1281 int had_sep = FALSE;
1282
1283 for (p = s; *p != NUL; )
1284 {
1285 if (vim_ispathsep(*p)
1286#ifdef BACKSLASH_IN_FILENAME
1287 && !rem_backslash(p)
1288#endif
1289 )
1290 had_sep = TRUE;
1291 else if (had_sep)
1292 {
1293 t = p;
1294 had_sep = FALSE;
1295 }
1296 MB_PTR_ADV(p);
1297 }
1298 return t;
1299}
1300
1301/*
1302 * Return TRUE if we only need to show the tail of completion matches.
1303 * When not completing file names or there is a wildcard in the path FALSE is
1304 * returned.
1305 */
1306 static int
1307expand_showtail(expand_T *xp)
1308{
1309 char_u *s;
1310 char_u *end;
1311
1312 // When not completing file names a "/" may mean something different.
1313 if (xp->xp_context != EXPAND_FILES
1314 && xp->xp_context != EXPAND_SHELLCMD
1315 && xp->xp_context != EXPAND_DIRECTORIES)
1316 return FALSE;
1317
1318 end = gettail(xp->xp_pattern);
1319 if (end == xp->xp_pattern) // there is no path separator
1320 return FALSE;
1321
1322 for (s = xp->xp_pattern; s < end; s++)
1323 {
1324 // Skip escaped wildcards. Only when the backslash is not a path
1325 // separator, on DOS the '*' "path\*\file" must not be skipped.
1326 if (rem_backslash(s))
1327 ++s;
1328 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1329 return FALSE;
1330 }
1331 return TRUE;
1332}
1333
1334/*
1335 * Prepare a string for expansion.
1336 * When expanding file names: The string will be used with expand_wildcards().
1337 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1338 * When expanding other names: The string will be used with regcomp(). Copy
1339 * the name into allocated memory and prepend "^".
1340 */
1341 char_u *
1342addstar(
1343 char_u *fname,
1344 int len,
1345 int context) // EXPAND_FILES etc.
1346{
1347 char_u *retval;
1348 int i, j;
1349 int new_len;
1350 char_u *tail;
1351 int ends_in_star;
1352
1353 if (context != EXPAND_FILES
1354 && context != EXPAND_FILES_IN_PATH
1355 && context != EXPAND_SHELLCMD
1356 && context != EXPAND_DIRECTORIES)
1357 {
1358 // Matching will be done internally (on something other than files).
1359 // So we convert the file-matching-type wildcards into our kind for
1360 // use with vim_regcomp(). First work out how long it will be:
1361
1362 // For help tags the translation is done in find_help_tags().
1363 // For a tag pattern starting with "/" no translation is needed.
1364 if (context == EXPAND_HELP
1365 || context == EXPAND_COLORS
roota6759382023-01-21 21:56:06 +00001366 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001367 || context == EXPAND_COMPILER
1368 || context == EXPAND_OWNSYNTAX
1369 || context == EXPAND_FILETYPE
1370 || context == EXPAND_PACKADD
1371 || ((context == EXPAND_TAGS_LISTFILES
1372 || context == EXPAND_TAGS)
1373 && fname[0] == '/'))
1374 retval = vim_strnsave(fname, len);
1375 else
1376 {
1377 new_len = len + 2; // +2 for '^' at start, NUL at end
1378 for (i = 0; i < len; i++)
1379 {
1380 if (fname[i] == '*' || fname[i] == '~')
1381 new_len++; // '*' needs to be replaced by ".*"
1382 // '~' needs to be replaced by "\~"
1383
1384 // Buffer names are like file names. "." should be literal
1385 if (context == EXPAND_BUFFERS && fname[i] == '.')
1386 new_len++; // "." becomes "\."
1387
1388 // Custom expansion takes care of special things, match
1389 // backslashes literally (perhaps also for other types?)
1390 if ((context == EXPAND_USER_DEFINED
1391 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1392 new_len++; // '\' becomes "\\"
1393 }
1394 retval = alloc(new_len);
1395 if (retval != NULL)
1396 {
1397 retval[0] = '^';
1398 j = 1;
1399 for (i = 0; i < len; i++, j++)
1400 {
1401 // Skip backslash. But why? At least keep it for custom
1402 // expansion.
1403 if (context != EXPAND_USER_DEFINED
1404 && context != EXPAND_USER_LIST
1405 && fname[i] == '\\'
1406 && ++i == len)
1407 break;
1408
1409 switch (fname[i])
1410 {
1411 case '*': retval[j++] = '.';
1412 break;
1413 case '~': retval[j++] = '\\';
1414 break;
1415 case '?': retval[j] = '.';
1416 continue;
1417 case '.': if (context == EXPAND_BUFFERS)
1418 retval[j++] = '\\';
1419 break;
1420 case '\\': if (context == EXPAND_USER_DEFINED
1421 || context == EXPAND_USER_LIST)
1422 retval[j++] = '\\';
1423 break;
1424 }
1425 retval[j] = fname[i];
1426 }
1427 retval[j] = NUL;
1428 }
1429 }
1430 }
1431 else
1432 {
1433 retval = alloc(len + 4);
1434 if (retval != NULL)
1435 {
1436 vim_strncpy(retval, fname, len);
1437
1438 // Don't add a star to *, ~, ~user, $var or `cmd`.
1439 // * would become **, which walks the whole tree.
1440 // ~ would be at the start of the file name, but not the tail.
1441 // $ could be anywhere in the tail.
1442 // ` could be anywhere in the file name.
1443 // When the name ends in '$' don't add a star, remove the '$'.
1444 tail = gettail(retval);
1445 ends_in_star = (len > 0 && retval[len - 1] == '*');
1446#ifndef BACKSLASH_IN_FILENAME
1447 for (i = len - 2; i >= 0; --i)
1448 {
1449 if (retval[i] != '\\')
1450 break;
1451 ends_in_star = !ends_in_star;
1452 }
1453#endif
1454 if ((*retval != '~' || tail != retval)
1455 && !ends_in_star
1456 && vim_strchr(tail, '$') == NULL
1457 && vim_strchr(retval, '`') == NULL)
1458 retval[len++] = '*';
1459 else if (len > 0 && retval[len - 1] == '$')
1460 --len;
1461 retval[len] = NUL;
1462 }
1463 }
1464 return retval;
1465}
1466
1467/*
1468 * Must parse the command line so far to work out what context we are in.
1469 * Completion can then be done based on that context.
1470 * This routine sets the variables:
1471 * xp->xp_pattern The start of the pattern to be expanded within
1472 * the command line (ends at the cursor).
1473 * xp->xp_context The type of thing to expand. Will be one of:
1474 *
1475 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1476 * the command line, like an unknown command. Caller
1477 * should beep.
1478 * EXPAND_NOTHING Unrecognised context for completion, use char like
1479 * a normal char, rather than for completion. eg
1480 * :s/^I/
1481 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1482 * it.
1483 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1484 * EXPAND_FILES After command with EX_XFILE set, or after setting
1485 * with P_EXPAND set. eg :e ^I, :w>>^I
1486 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1487 * when we know only directories are of interest. eg
1488 * :set dir=^I
1489 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1490 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1491 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1492 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1493 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1494 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1495 * EXPAND_EVENTS Complete event names
1496 * EXPAND_SYNTAX Complete :syntax command arguments
1497 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1498 * EXPAND_AUGROUP Complete autocommand group names
1499 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1500 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1501 * eg :unmap a^I , :cunab x^I
1502 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1503 * eg :call sub^I
1504 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1505 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1506 * names in expressions, eg :while s^I
1507 * EXPAND_ENV_VARS Complete environment variable names
1508 * EXPAND_USER Complete user names
1509 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001510 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001511set_expand_context(expand_T *xp)
1512{
1513 cmdline_info_T *ccline = get_cmdline_info();
1514
1515 // only expansion for ':', '>' and '=' command-lines
1516 if (ccline->cmdfirstc != ':'
1517#ifdef FEAT_EVAL
1518 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1519 && !ccline->input_fn
1520#endif
1521 )
1522 {
1523 xp->xp_context = EXPAND_NOTHING;
1524 return;
1525 }
1526 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1527}
1528
Bram Moolenaard0190392019-08-23 21:17:35 +02001529/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001530 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1531 * For user defined commands, the completion context is set in 'xp' and the
1532 * completion flags in 'complp'.
1533 *
1534 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001535 */
1536 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001537set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001538{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001539 char_u *p = NULL;
1540 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001541 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001542
1543 // Isolate the command and search for it in the command table.
1544 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001545 // - the 'k' command can directly be followed by any character, but do
1546 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1547 // find matches anywhere in the command name, do this only for command
1548 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001549 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001550 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001551 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001552 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001553 p = cmd + 1;
1554 }
1555 else
1556 {
1557 p = cmd;
1558 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1559 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001560 // A user command may contain digits.
1561 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1562 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001563 while (ASCII_ISALNUM(*p) || *p == '*')
1564 ++p;
1565 // for python 3.x: ":py3*" commands completion
1566 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1567 {
1568 ++p;
1569 while (ASCII_ISALPHA(*p) || *p == '*')
1570 ++p;
1571 }
1572 // check for non-alpha command
1573 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1574 ++p;
1575 len = (int)(p - cmd);
1576
1577 if (len == 0)
1578 {
1579 xp->xp_context = EXPAND_UNSUCCESSFUL;
1580 return NULL;
1581 }
1582
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001583 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001584
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001585 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001586 // Also when doing fuzzy expansion for non-shell commands, support
1587 // alphanumeric characters.
1588 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1589 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001590 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1591 ++p;
1592 }
1593
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001594 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001595 // character, complete the command name.
1596 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1597 return NULL;
1598
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001599 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001600 {
1601 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1602 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001603 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001604 p = cmd + 1;
1605 }
1606 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1607 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001608 eap->cmd = cmd;
1609 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001610 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001611 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001612 }
1613 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001614 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001615 {
1616 // Not still touching the command and it was an illegal one
1617 xp->xp_context = EXPAND_UNSUCCESSFUL;
1618 return NULL;
1619 }
1620
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001621 return p;
1622}
Bram Moolenaard0190392019-08-23 21:17:35 +02001623
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001624/*
1625 * Set the completion context for a command argument with wild card characters.
1626 */
1627 static void
1628set_context_for_wildcard_arg(
1629 exarg_T *eap,
1630 char_u *arg,
1631 int usefilter,
1632 expand_T *xp,
1633 int *complp)
1634{
1635 char_u *p;
1636 int c;
1637 int in_quote = FALSE;
1638 char_u *bow = NULL; // Beginning of word
1639 int len = 0;
1640
1641 // Allow spaces within back-quotes to count as part of the argument
1642 // being expanded.
1643 xp->xp_pattern = skipwhite(arg);
1644 p = xp->xp_pattern;
1645 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001646 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001647 if (has_mbyte)
1648 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001649 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001650 c = *p;
1651 if (c == '\\' && p[1] != NUL)
1652 ++p;
1653 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001654 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001655 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001656 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001657 xp->xp_pattern = p;
1658 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001659 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001660 in_quote = !in_quote;
1661 }
1662 // An argument can contain just about everything, except
1663 // characters that end the command and white space.
1664 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001665#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001667#endif
1668 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 {
1670 len = 0; // avoid getting stuck when space is in 'isfname'
1671 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001672 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001673 if (has_mbyte)
1674 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001675 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001676 c = *p;
1677 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001678 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001679 if (has_mbyte)
1680 len = (*mb_ptr2len)(p);
1681 else
1682 len = 1;
1683 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001684 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685 if (in_quote)
1686 bow = p;
1687 else
1688 xp->xp_pattern = p;
1689 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001690 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001691 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001692 }
1693
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001694 // If we are still inside the quotes, and we passed a space, just
1695 // expand from there.
1696 if (bow != NULL && in_quote)
1697 xp->xp_pattern = bow;
1698 xp->xp_context = EXPAND_FILES;
1699
1700 // For a shell command more chars need to be escaped.
1701 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1702 {
1703#ifndef BACKSLASH_IN_FILENAME
1704 xp->xp_shell = TRUE;
1705#endif
1706 // When still after the command name expand executables.
1707 if (xp->xp_pattern == skipwhite(arg))
1708 xp->xp_context = EXPAND_SHELLCMD;
1709 }
1710
1711 // Check for environment variable.
1712 if (*xp->xp_pattern == '$')
1713 {
1714 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1715 if (!vim_isIDc(*p))
1716 break;
1717 if (*p == NUL)
1718 {
1719 xp->xp_context = EXPAND_ENV_VARS;
1720 ++xp->xp_pattern;
1721 // Avoid that the assignment uses EXPAND_FILES again.
1722 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1723 *complp = EXPAND_ENV_VARS;
1724 }
1725 }
1726 // Check for user names.
1727 if (*xp->xp_pattern == '~')
1728 {
1729 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1730 ;
1731 // Complete ~user only if it partially matches a user name.
1732 // A full match ~user<Tab> will be replaced by user's home
1733 // directory i.e. something like ~user<Tab> -> /home/user/
1734 if (*p == NUL && p > xp->xp_pattern + 1
1735 && match_user(xp->xp_pattern + 1) >= 1)
1736 {
1737 xp->xp_context = EXPAND_USER;
1738 ++xp->xp_pattern;
1739 }
1740 }
1741}
1742
1743/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001744 * Set the completion context for the :filter command. Returns a pointer to the
1745 * next command after the :filter command.
1746 */
1747 static char_u *
1748set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1749{
1750 if (*arg != NUL)
1751 arg = skip_vimgrep_pat(arg, NULL, NULL);
1752 if (arg == NULL || *arg == NUL)
1753 {
1754 xp->xp_context = EXPAND_NOTHING;
1755 return NULL;
1756 }
1757 return skipwhite(arg);
1758}
1759
1760#ifdef FEAT_SEARCH_EXTRA
1761/*
1762 * Set the completion context for the :match command. Returns a pointer to the
1763 * next command after the :match command.
1764 */
1765 static char_u *
1766set_context_in_match_cmd(expand_T *xp, char_u *arg)
1767{
1768 if (*arg == NUL || !ends_excmd(*arg))
1769 {
1770 // also complete "None"
1771 set_context_in_echohl_cmd(xp, arg);
1772 arg = skipwhite(skiptowhite(arg));
1773 if (*arg != NUL)
1774 {
1775 xp->xp_context = EXPAND_NOTHING;
1776 arg = skip_regexp(arg + 1, *arg, magic_isset());
1777 }
1778 }
1779 return find_nextcmd(arg);
1780}
1781#endif
1782
1783/*
1784 * Returns a pointer to the next command after a :global or a :v command.
1785 * Returns NULL if there is no next command.
1786 */
1787 static char_u *
1788find_cmd_after_global_cmd(char_u *arg)
1789{
1790 int delim;
1791
1792 delim = *arg; // get the delimiter
1793 if (delim)
1794 ++arg; // skip delimiter if there is one
1795
1796 while (arg[0] != NUL && arg[0] != delim)
1797 {
1798 if (arg[0] == '\\' && arg[1] != NUL)
1799 ++arg;
1800 ++arg;
1801 }
1802 if (arg[0] != NUL)
1803 return arg + 1;
1804
1805 return NULL;
1806}
1807
1808/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001809 * Returns a pointer to the next command after a :substitute or a :& command.
1810 * Returns NULL if there is no next command.
1811 */
1812 static char_u *
1813find_cmd_after_substitute_cmd(char_u *arg)
1814{
1815 int delim;
1816
1817 delim = *arg;
1818 if (delim)
1819 {
1820 // skip "from" part
1821 ++arg;
1822 arg = skip_regexp(arg, delim, magic_isset());
1823
1824 if (arg[0] != NUL && arg[0] == delim)
1825 {
1826 // skip "to" part
1827 ++arg;
1828 while (arg[0] != NUL && arg[0] != delim)
1829 {
1830 if (arg[0] == '\\' && arg[1] != NUL)
1831 ++arg;
1832 ++arg;
1833 }
1834 if (arg[0] != NUL) // skip delimiter
1835 ++arg;
1836 }
1837 }
1838 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1839 ++arg;
1840 if (arg[0] != NUL)
1841 return arg;
1842
1843 return NULL;
1844}
1845
1846/*
1847 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1848 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1849 * Returns NULL if there is no next command.
1850 */
1851 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001852find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001853{
1854 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001855 if (*arg != '/')
1856 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001857
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001858 // Match regexp, not just whole words
1859 for (++arg; *arg && *arg != '/'; arg++)
1860 if (*arg == '\\' && arg[1] != NUL)
1861 arg++;
1862 if (*arg)
1863 {
1864 arg = skipwhite(arg + 1);
1865
1866 // Check for trailing illegal characters
1867 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1868 xp->xp_context = EXPAND_NOTHING;
1869 else
1870 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001871 }
1872
1873 return NULL;
1874}
1875
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001876#ifdef FEAT_EVAL
1877/*
1878 * Set the completion context for the :unlet command. Always returns NULL.
1879 */
1880 static char_u *
1881set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1882{
1883 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1884 arg = xp->xp_pattern + 1;
1885
1886 xp->xp_context = EXPAND_USER_VARS;
1887 xp->xp_pattern = arg;
1888
1889 if (*xp->xp_pattern == '$')
1890 {
1891 xp->xp_context = EXPAND_ENV_VARS;
1892 ++xp->xp_pattern;
1893 }
1894
1895 return NULL;
1896}
1897#endif
1898
1899#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1900/*
1901 * Set the completion context for the :language command. Always returns NULL.
1902 */
1903 static char_u *
1904set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1905{
1906 char_u *p;
1907
1908 p = skiptowhite(arg);
1909 if (*p == NUL)
1910 {
1911 xp->xp_context = EXPAND_LANGUAGE;
1912 xp->xp_pattern = arg;
1913 }
1914 else
1915 {
1916 if ( STRNCMP(arg, "messages", p - arg) == 0
1917 || STRNCMP(arg, "ctype", p - arg) == 0
1918 || STRNCMP(arg, "time", p - arg) == 0
1919 || STRNCMP(arg, "collate", p - arg) == 0)
1920 {
1921 xp->xp_context = EXPAND_LOCALES;
1922 xp->xp_pattern = skipwhite(p);
1923 }
1924 else
1925 xp->xp_context = EXPAND_NOTHING;
1926 }
1927
1928 return NULL;
1929}
1930#endif
1931
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001932#ifdef FEAT_EVAL
1933static enum
1934{
1935 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001936 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1937 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001938} breakpt_expand_what;
1939
1940/*
1941 * Set the completion context for the :breakadd command. Always returns NULL.
1942 */
1943 static char_u *
1944set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1945{
1946 char_u *p;
1947 char_u *subcmd_start;
1948
1949 xp->xp_context = EXPAND_BREAKPOINT;
1950 xp->xp_pattern = arg;
1951
1952 if (cmdidx == CMD_breakadd)
1953 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001954 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001955 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001956 else
1957 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001958
1959 p = skipwhite(arg);
1960 if (*p == NUL)
1961 return NULL;
1962 subcmd_start = p;
1963
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001964 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001965 {
1966 // :breakadd file [lnum] <filename>
1967 // :breakadd func [lnum] <funcname>
1968 p += 4;
1969 p = skipwhite(p);
1970
1971 // skip line number (if specified)
1972 if (VIM_ISDIGIT(*p))
1973 {
1974 p = skipdigits(p);
1975 if (*p != ' ')
1976 {
1977 xp->xp_context = EXPAND_NOTHING;
1978 return NULL;
1979 }
1980 p = skipwhite(p);
1981 }
1982 if (STRNCMP("file", subcmd_start, 4) == 0)
1983 xp->xp_context = EXPAND_FILES;
1984 else
1985 xp->xp_context = EXPAND_USER_FUNC;
1986 xp->xp_pattern = p;
1987 }
1988 else if (STRNCMP("expr ", p, 5) == 0)
1989 {
1990 // :breakadd expr <expression>
1991 xp->xp_context = EXPAND_EXPRESSION;
1992 xp->xp_pattern = skipwhite(p + 5);
1993 }
1994
1995 return NULL;
1996}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00001997
1998 static char_u *
1999set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2000{
2001 char_u *p;
2002
2003 xp->xp_context = EXPAND_NOTHING;
2004 xp->xp_pattern = NULL;
2005
2006 p = skipwhite(arg);
2007 if (VIM_ISDIGIT(*p))
2008 return NULL;
2009
2010 xp->xp_context = EXPAND_SCRIPTNAMES;
2011 xp->xp_pattern = p;
2012
2013 return NULL;
2014}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002015#endif
2016
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002017/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002018 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2019 * The argument to the command is 'arg' and the argument flags is 'argt'.
2020 * For user-defined commands and for environment variables, 'compl' has the
2021 * completion type.
2022 * Returns a pointer to the next command. Returns NULL if there is no next
2023 * command.
2024 */
2025 static char_u *
2026set_context_by_cmdname(
2027 char_u *cmd,
2028 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002029 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002030 char_u *arg,
2031 long argt,
2032 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002033 int forceit)
2034{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002035 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002036 {
2037 case CMD_find:
2038 case CMD_sfind:
2039 case CMD_tabfind:
2040 if (xp->xp_context == EXPAND_FILES)
2041 xp->xp_context = EXPAND_FILES_IN_PATH;
2042 break;
2043 case CMD_cd:
2044 case CMD_chdir:
2045 case CMD_tcd:
2046 case CMD_tchdir:
2047 case CMD_lcd:
2048 case CMD_lchdir:
2049 if (xp->xp_context == EXPAND_FILES)
2050 xp->xp_context = EXPAND_DIRECTORIES;
2051 break;
2052 case CMD_help:
2053 xp->xp_context = EXPAND_HELP;
2054 xp->xp_pattern = arg;
2055 break;
2056
2057 // Command modifiers: return the argument.
2058 // Also for commands with an argument that is a command.
2059 case CMD_aboveleft:
2060 case CMD_argdo:
2061 case CMD_belowright:
2062 case CMD_botright:
2063 case CMD_browse:
2064 case CMD_bufdo:
2065 case CMD_cdo:
2066 case CMD_cfdo:
2067 case CMD_confirm:
2068 case CMD_debug:
2069 case CMD_folddoclosed:
2070 case CMD_folddoopen:
2071 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002072 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002073 case CMD_keepalt:
2074 case CMD_keepjumps:
2075 case CMD_keepmarks:
2076 case CMD_keeppatterns:
2077 case CMD_ldo:
2078 case CMD_leftabove:
2079 case CMD_lfdo:
2080 case CMD_lockmarks:
2081 case CMD_noautocmd:
2082 case CMD_noswapfile:
2083 case CMD_rightbelow:
2084 case CMD_sandbox:
2085 case CMD_silent:
2086 case CMD_tab:
2087 case CMD_tabdo:
2088 case CMD_topleft:
2089 case CMD_verbose:
2090 case CMD_vertical:
2091 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002092 case CMD_vim9cmd:
2093 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002094 return arg;
2095
2096 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002097 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002098
2099#ifdef FEAT_SEARCH_EXTRA
2100 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002101 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002102#endif
2103
2104 // All completion for the +cmdline_compl feature goes here.
2105
2106 case CMD_command:
2107 return set_context_in_user_cmd(xp, arg);
2108
2109 case CMD_delcommand:
2110 xp->xp_context = EXPAND_USER_COMMANDS;
2111 xp->xp_pattern = arg;
2112 break;
2113
2114 case CMD_global:
2115 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002116 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002117 case CMD_and:
2118 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002119 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002120 case CMD_isearch:
2121 case CMD_dsearch:
2122 case CMD_ilist:
2123 case CMD_dlist:
2124 case CMD_ijump:
2125 case CMD_psearch:
2126 case CMD_djump:
2127 case CMD_isplit:
2128 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002129 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002130 case CMD_autocmd:
2131 return set_context_in_autocmd(xp, arg, FALSE);
2132 case CMD_doautocmd:
2133 case CMD_doautoall:
2134 return set_context_in_autocmd(xp, arg, TRUE);
2135 case CMD_set:
2136 set_context_in_set_cmd(xp, arg, 0);
2137 break;
2138 case CMD_setglobal:
2139 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2140 break;
2141 case CMD_setlocal:
2142 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2143 break;
2144 case CMD_tag:
2145 case CMD_stag:
2146 case CMD_ptag:
2147 case CMD_ltag:
2148 case CMD_tselect:
2149 case CMD_stselect:
2150 case CMD_ptselect:
2151 case CMD_tjump:
2152 case CMD_stjump:
2153 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002154 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002155 xp->xp_context = EXPAND_TAGS_LISTFILES;
2156 else
2157 xp->xp_context = EXPAND_TAGS;
2158 xp->xp_pattern = arg;
2159 break;
2160 case CMD_augroup:
2161 xp->xp_context = EXPAND_AUGROUP;
2162 xp->xp_pattern = arg;
2163 break;
2164#ifdef FEAT_SYN_HL
2165 case CMD_syntax:
2166 set_context_in_syntax_cmd(xp, arg);
2167 break;
2168#endif
2169#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002170 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002171 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002172 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002173 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002174 case CMD_if:
2175 case CMD_elseif:
2176 case CMD_while:
2177 case CMD_for:
2178 case CMD_echo:
2179 case CMD_echon:
2180 case CMD_execute:
2181 case CMD_echomsg:
2182 case CMD_echoerr:
2183 case CMD_call:
2184 case CMD_return:
2185 case CMD_cexpr:
2186 case CMD_caddexpr:
2187 case CMD_cgetexpr:
2188 case CMD_lexpr:
2189 case CMD_laddexpr:
2190 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002191 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002192 break;
2193
2194 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002195 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002196 case CMD_function:
2197 case CMD_delfunction:
2198 xp->xp_context = EXPAND_USER_FUNC;
2199 xp->xp_pattern = arg;
2200 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002201 case CMD_disassemble:
2202 set_context_in_disassemble_cmd(xp, arg);
2203 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002204
2205 case CMD_echohl:
2206 set_context_in_echohl_cmd(xp, arg);
2207 break;
2208#endif
2209 case CMD_highlight:
2210 set_context_in_highlight_cmd(xp, arg);
2211 break;
2212#ifdef FEAT_CSCOPE
2213 case CMD_cscope:
2214 case CMD_lcscope:
2215 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002216 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002217 break;
2218#endif
2219#ifdef FEAT_SIGNS
2220 case CMD_sign:
2221 set_context_in_sign_cmd(xp, arg);
2222 break;
2223#endif
2224 case CMD_bdelete:
2225 case CMD_bwipeout:
2226 case CMD_bunload:
2227 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2228 arg = xp->xp_pattern + 1;
2229 // FALLTHROUGH
2230 case CMD_buffer:
2231 case CMD_sbuffer:
2232 case CMD_checktime:
2233 xp->xp_context = EXPAND_BUFFERS;
2234 xp->xp_pattern = arg;
2235 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002236#ifdef FEAT_DIFF
2237 case CMD_diffget:
2238 case CMD_diffput:
2239 // If current buffer is in diff mode, complete buffer names
2240 // which are in diff mode, and different than current buffer.
2241 xp->xp_context = EXPAND_DIFF_BUFFERS;
2242 xp->xp_pattern = arg;
2243 break;
2244#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002245 case CMD_USER:
2246 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002247 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2248 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002249
2250 case CMD_map: case CMD_noremap:
2251 case CMD_nmap: case CMD_nnoremap:
2252 case CMD_vmap: case CMD_vnoremap:
2253 case CMD_omap: case CMD_onoremap:
2254 case CMD_imap: case CMD_inoremap:
2255 case CMD_cmap: case CMD_cnoremap:
2256 case CMD_lmap: case CMD_lnoremap:
2257 case CMD_smap: case CMD_snoremap:
2258 case CMD_tmap: case CMD_tnoremap:
2259 case CMD_xmap: case CMD_xnoremap:
2260 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002261 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002262 case CMD_unmap:
2263 case CMD_nunmap:
2264 case CMD_vunmap:
2265 case CMD_ounmap:
2266 case CMD_iunmap:
2267 case CMD_cunmap:
2268 case CMD_lunmap:
2269 case CMD_sunmap:
2270 case CMD_tunmap:
2271 case CMD_xunmap:
2272 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002273 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002274 case CMD_mapclear:
2275 case CMD_nmapclear:
2276 case CMD_vmapclear:
2277 case CMD_omapclear:
2278 case CMD_imapclear:
2279 case CMD_cmapclear:
2280 case CMD_lmapclear:
2281 case CMD_smapclear:
2282 case CMD_tmapclear:
2283 case CMD_xmapclear:
2284 xp->xp_context = EXPAND_MAPCLEAR;
2285 xp->xp_pattern = arg;
2286 break;
2287
2288 case CMD_abbreviate: case CMD_noreabbrev:
2289 case CMD_cabbrev: case CMD_cnoreabbrev:
2290 case CMD_iabbrev: case CMD_inoreabbrev:
2291 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002292 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002293 case CMD_unabbreviate:
2294 case CMD_cunabbrev:
2295 case CMD_iunabbrev:
2296 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002297 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002298#ifdef FEAT_MENU
2299 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2300 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2301 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2302 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2303 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2304 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2305 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2306 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2307 case CMD_tmenu: case CMD_tunmenu:
2308 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2309 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2310#endif
2311
2312 case CMD_colorscheme:
2313 xp->xp_context = EXPAND_COLORS;
2314 xp->xp_pattern = arg;
2315 break;
2316
roota6759382023-01-21 21:56:06 +00002317 case CMD_runtime:
2318 xp->xp_context = EXPAND_RUNTIME;
2319 xp->xp_pattern = arg;
2320 break;
2321
Bram Moolenaard0190392019-08-23 21:17:35 +02002322 case CMD_compiler:
2323 xp->xp_context = EXPAND_COMPILER;
2324 xp->xp_pattern = arg;
2325 break;
2326
2327 case CMD_ownsyntax:
2328 xp->xp_context = EXPAND_OWNSYNTAX;
2329 xp->xp_pattern = arg;
2330 break;
2331
2332 case CMD_setfiletype:
2333 xp->xp_context = EXPAND_FILETYPE;
2334 xp->xp_pattern = arg;
2335 break;
2336
2337 case CMD_packadd:
2338 xp->xp_context = EXPAND_PACKADD;
2339 xp->xp_pattern = arg;
2340 break;
2341
2342#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2343 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002344 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002345#endif
2346#if defined(FEAT_PROFILE)
2347 case CMD_profile:
2348 set_context_in_profile_cmd(xp, arg);
2349 break;
2350#endif
2351 case CMD_behave:
2352 xp->xp_context = EXPAND_BEHAVE;
2353 xp->xp_pattern = arg;
2354 break;
2355
2356 case CMD_messages:
2357 xp->xp_context = EXPAND_MESSAGES;
2358 xp->xp_pattern = arg;
2359 break;
2360
2361 case CMD_history:
2362 xp->xp_context = EXPAND_HISTORY;
2363 xp->xp_pattern = arg;
2364 break;
2365#if defined(FEAT_PROFILE)
2366 case CMD_syntime:
2367 xp->xp_context = EXPAND_SYNTIME;
2368 xp->xp_pattern = arg;
2369 break;
2370#endif
2371
2372 case CMD_argdelete:
2373 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2374 arg = xp->xp_pattern + 1;
2375 xp->xp_context = EXPAND_ARGLIST;
2376 xp->xp_pattern = arg;
2377 break;
2378
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002379#ifdef FEAT_EVAL
2380 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002381 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002382 case CMD_breakdel:
2383 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002384
2385 case CMD_scriptnames:
2386 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002387#endif
2388
Bram Moolenaard0190392019-08-23 21:17:35 +02002389 default:
2390 break;
2391 }
2392 return NULL;
2393}
2394
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002395/*
2396 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2397 * we don't need/want deleted. Maybe this could be done better if we didn't
2398 * repeat all this stuff. The only problem is that they may not stay
2399 * perfectly compatible with each other, but then the command line syntax
2400 * probably won't change that much -- webb.
2401 */
2402 static char_u *
2403set_one_cmd_context(
2404 expand_T *xp,
2405 char_u *buff) // buffer for command string
2406{
2407 char_u *p;
2408 char_u *cmd, *arg;
2409 int len = 0;
2410 exarg_T ea;
2411 int compl = EXPAND_NOTHING;
2412 int forceit = FALSE;
2413 int usefilter = FALSE; // filter instead of file name
2414
2415 ExpandInit(xp);
2416 xp->xp_pattern = buff;
2417 xp->xp_line = buff;
2418 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2419 ea.argt = 0;
2420
2421 // 1. skip comment lines and leading space, colons or bars
2422 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2423 ;
2424 xp->xp_pattern = cmd;
2425
2426 if (*cmd == NUL)
2427 return NULL;
2428 if (*cmd == '"') // ignore comment lines
2429 {
2430 xp->xp_context = EXPAND_NOTHING;
2431 return NULL;
2432 }
2433
2434 // 3. Skip over the range to find the command.
2435 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2436 xp->xp_pattern = cmd;
2437 if (*cmd == NUL)
2438 return NULL;
2439 if (*cmd == '"')
2440 {
2441 xp->xp_context = EXPAND_NOTHING;
2442 return NULL;
2443 }
2444
2445 if (*cmd == '|' || *cmd == '\n')
2446 return cmd + 1; // There's another command
2447
2448 // Get the command index.
2449 p = set_cmd_index(cmd, &ea, xp, &compl);
2450 if (p == NULL)
2451 return NULL;
2452
2453 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2454
2455 if (*p == '!') // forced commands
2456 {
2457 forceit = TRUE;
2458 ++p;
2459 }
2460
2461 // 6. parse arguments
2462 if (!IS_USER_CMDIDX(ea.cmdidx))
2463 ea.argt = excmd_get_argt(ea.cmdidx);
2464
2465 arg = skipwhite(p);
2466
2467 // Skip over ++argopt argument
2468 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2469 {
2470 p = arg;
2471 while (*p && !vim_isspace(*p))
2472 MB_PTR_ADV(p);
2473 arg = skipwhite(p);
2474 }
2475
2476 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2477 {
2478 if (*arg == '>') // append
2479 {
2480 if (*++arg == '>')
2481 ++arg;
2482 arg = skipwhite(arg);
2483 }
2484 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2485 {
2486 ++arg;
2487 usefilter = TRUE;
2488 }
2489 }
2490
2491 if (ea.cmdidx == CMD_read)
2492 {
2493 usefilter = forceit; // :r! filter if forced
2494 if (*arg == '!') // :r !filter
2495 {
2496 ++arg;
2497 usefilter = TRUE;
2498 }
2499 }
2500
2501 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2502 {
2503 while (*arg == *cmd) // allow any number of '>' or '<'
2504 ++arg;
2505 arg = skipwhite(arg);
2506 }
2507
2508 // Does command allow "+command"?
2509 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2510 {
2511 // Check if we're in the +command
2512 p = arg + 1;
2513 arg = skip_cmd_arg(arg, FALSE);
2514
2515 // Still touching the command after '+'?
2516 if (*arg == NUL)
2517 return p;
2518
2519 // Skip space(s) after +command to get to the real argument
2520 arg = skipwhite(arg);
2521 }
2522
2523
2524 // Check for '|' to separate commands and '"' to start comments.
2525 // Don't do this for ":read !cmd" and ":write !cmd".
2526 if ((ea.argt & EX_TRLBAR) && !usefilter)
2527 {
2528 p = arg;
2529 // ":redir @" is not the start of a comment
2530 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2531 p += 2;
2532 while (*p)
2533 {
2534 if (*p == Ctrl_V)
2535 {
2536 if (p[1] != NUL)
2537 ++p;
2538 }
2539 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2540 || *p == '|' || *p == '\n')
2541 {
2542 if (*(p - 1) != '\\')
2543 {
2544 if (*p == '|' || *p == '\n')
2545 return p + 1;
2546 return NULL; // It's a comment
2547 }
2548 }
2549 MB_PTR_ADV(p);
2550 }
2551 }
2552
2553 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2554 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2555 // no arguments allowed but there is something
2556 return NULL;
2557
2558 // Find start of last argument (argument just before cursor):
2559 p = buff;
2560 xp->xp_pattern = p;
2561 len = (int)STRLEN(buff);
2562 while (*p && p < buff + len)
2563 {
2564 if (*p == ' ' || *p == TAB)
2565 {
2566 // argument starts after a space
2567 xp->xp_pattern = ++p;
2568 }
2569 else
2570 {
2571 if (*p == '\\' && *(p + 1) != NUL)
2572 ++p; // skip over escaped character
2573 MB_PTR_ADV(p);
2574 }
2575 }
2576
2577 if (ea.argt & EX_XFILE)
2578 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2579
2580 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002581 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002582 forceit);
2583}
2584
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002585/*
2586 * Set the completion context in 'xp' for command 'str'
2587 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002588 void
2589set_cmd_context(
2590 expand_T *xp,
2591 char_u *str, // start of command line
2592 int len, // length of command line (excl. NUL)
2593 int col, // position of cursor
2594 int use_ccline UNUSED) // use ccline for info
2595{
2596#ifdef FEAT_EVAL
2597 cmdline_info_T *ccline = get_cmdline_info();
2598#endif
2599 int old_char = NUL;
2600 char_u *nextcomm;
2601
2602 // Avoid a UMR warning from Purify, only save the character if it has been
2603 // written before.
2604 if (col < len)
2605 old_char = str[col];
2606 str[col] = NUL;
2607 nextcomm = str;
2608
2609#ifdef FEAT_EVAL
2610 if (use_ccline && ccline->cmdfirstc == '=')
2611 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002612 // pass CMD_SIZE because there is no real command
2613 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002614 }
2615 else if (use_ccline && ccline->input_fn)
2616 {
2617 xp->xp_context = ccline->xp_context;
2618 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002619 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002620 }
2621 else
2622#endif
2623 while (nextcomm != NULL)
2624 nextcomm = set_one_cmd_context(xp, nextcomm);
2625
2626 // Store the string here so that call_user_expand_func() can get to them
2627 // easily.
2628 xp->xp_line = str;
2629 xp->xp_col = col;
2630
2631 str[col] = old_char;
2632}
2633
2634/*
2635 * Expand the command line "str" from context "xp".
2636 * "xp" must have been set by set_cmd_context().
2637 * xp->xp_pattern points into "str", to where the text that is to be expanded
2638 * starts.
2639 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2640 * cursor.
2641 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2642 * key that triggered expansion literally.
2643 * Returns EXPAND_OK otherwise.
2644 */
2645 int
2646expand_cmdline(
2647 expand_T *xp,
2648 char_u *str, // start of command line
2649 int col, // position of cursor
2650 int *matchcount, // return: nr of matches
2651 char_u ***matches) // return: array of pointers to matches
2652{
2653 char_u *file_str = NULL;
2654 int options = WILD_ADD_SLASH|WILD_SILENT;
2655
2656 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2657 {
2658 beep_flush();
2659 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2660 }
2661 if (xp->xp_context == EXPAND_NOTHING)
2662 {
2663 // Caller can use the character as a normal char instead
2664 return EXPAND_NOTHING;
2665 }
2666
2667 // add star to file name, or convert to regexp if not exp. files.
2668 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002669 if (cmdline_fuzzy_completion_supported(xp))
2670 // If fuzzy matching, don't modify the search string
2671 file_str = vim_strsave(xp->xp_pattern);
2672 else
2673 {
2674 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2675 if (file_str == NULL)
2676 return EXPAND_UNSUCCESSFUL;
2677 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002678
2679 if (p_wic)
2680 options += WILD_ICASE;
2681
2682 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002683 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002684 {
2685 *matchcount = 0;
2686 *matches = NULL;
2687 }
2688 vim_free(file_str);
2689
2690 return EXPAND_OK;
2691}
2692
Bram Moolenaar66b51422019-08-18 21:44:12 +02002693/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002694 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002695 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002696 */
2697 static int
2698expand_files_and_dirs(
2699 expand_T *xp,
2700 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002701 char_u ***matches,
2702 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002703 int flags,
2704 int options)
2705{
2706 int free_pat = FALSE;
2707 int i;
2708 int ret;
2709
2710 // for ":set path=" and ":set tags=" halve backslashes for escaped
2711 // space
2712 if (xp->xp_backslash != XP_BS_NONE)
2713 {
2714 free_pat = TRUE;
2715 pat = vim_strsave(pat);
2716 for (i = 0; pat[i]; ++i)
2717 if (pat[i] == '\\')
2718 {
2719 if (xp->xp_backslash == XP_BS_THREE
2720 && pat[i + 1] == '\\'
2721 && pat[i + 2] == '\\'
2722 && pat[i + 3] == ' ')
2723 STRMOVE(pat + i, pat + i + 3);
2724 if (xp->xp_backslash == XP_BS_ONE
2725 && pat[i + 1] == ' ')
2726 STRMOVE(pat + i, pat + i + 1);
2727 }
2728 }
2729
2730 if (xp->xp_context == EXPAND_FILES)
2731 flags |= EW_FILE;
2732 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2733 flags |= (EW_FILE | EW_PATH);
2734 else
2735 flags = (flags | EW_DIR) & ~EW_FILE;
2736 if (options & WILD_ICASE)
2737 flags |= EW_ICASE;
2738
2739 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002740 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002741 if (free_pat)
2742 vim_free(pat);
2743#ifdef BACKSLASH_IN_FILENAME
2744 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2745 {
2746 int j;
2747
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002748 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002749 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002750 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002751
2752 while (*ptr != NUL)
2753 {
2754 if (p_csl[0] == 's' && *ptr == '\\')
2755 *ptr = '/';
2756 else if (p_csl[0] == 'b' && *ptr == '/')
2757 *ptr = '\\';
2758 ptr += (*mb_ptr2len)(ptr);
2759 }
2760 }
2761 }
2762#endif
2763 return ret;
2764}
2765
2766/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002767 * Function given to ExpandGeneric() to obtain the possible arguments of the
2768 * ":behave {mswin,xterm}" command.
2769 */
2770 static char_u *
2771get_behave_arg(expand_T *xp UNUSED, int idx)
2772{
2773 if (idx == 0)
2774 return (char_u *)"mswin";
2775 if (idx == 1)
2776 return (char_u *)"xterm";
2777 return NULL;
2778}
2779
K.Takata161b6ac2022-11-14 15:31:07 +00002780#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002781/*
2782 * Function given to ExpandGeneric() to obtain the possible arguments of the
2783 * ":breakadd {expr, file, func, here}" command.
2784 * ":breakdel {func, file, here}" command.
2785 */
2786 static char_u *
2787get_breakadd_arg(expand_T *xp UNUSED, int idx)
2788{
2789 char *opts[] = {"expr", "file", "func", "here"};
2790
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002791 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002792 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002793 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002794 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2795 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002796 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2797 {
2798 // breakdel {func, file, here}
2799 if (idx <= 2)
2800 return (char_u *)opts[idx + 1];
2801 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002802 else
2803 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002804 // profdel {func, file}
2805 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002806 return (char_u *)opts[idx + 1];
2807 }
2808 }
2809 return NULL;
2810}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002811
2812/*
2813 * Function given to ExpandGeneric() to obtain the possible arguments for the
2814 * ":scriptnames" command.
2815 */
2816 static char_u *
2817get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2818{
2819 scriptitem_T *si;
2820
2821 if (!SCRIPT_ID_VALID(idx + 1))
2822 return NULL;
2823
2824 si = SCRIPT_ITEM(idx + 1);
2825 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2826 return NameBuff;
2827}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002828#endif
2829
Bram Moolenaard0190392019-08-23 21:17:35 +02002830/*
2831 * Function given to ExpandGeneric() to obtain the possible arguments of the
2832 * ":messages {clear}" command.
2833 */
2834 static char_u *
2835get_messages_arg(expand_T *xp UNUSED, int idx)
2836{
2837 if (idx == 0)
2838 return (char_u *)"clear";
2839 return NULL;
2840}
2841
2842 static char_u *
2843get_mapclear_arg(expand_T *xp UNUSED, int idx)
2844{
2845 if (idx == 0)
2846 return (char_u *)"<buffer>";
2847 return NULL;
2848}
2849
2850/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002851 * Do the expansion based on xp->xp_context and 'rmp'.
2852 */
2853 static int
2854ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002855 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002856 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002857 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002858 char_u ***matches,
2859 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002860{
2861 static struct expgen
2862 {
2863 int context;
2864 char_u *((*func)(expand_T *, int));
2865 int ic;
2866 int escaped;
2867 } tab[] =
2868 {
2869 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2870 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2871 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2872 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2873 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2874 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2875 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2876 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2877 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2878 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002879#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002880 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2881 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2882 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2883 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2884 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002885#endif
2886#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002887 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2888 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002889#endif
2890#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002891 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002892#endif
2893#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002894 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002895#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002896 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2897 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2898 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002899#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002900 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002901#endif
2902#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002903 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002904#endif
2905#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002906 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002907#endif
2908#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002909 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2910 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002911#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002912 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2913 {EXPAND_USER, get_users, TRUE, FALSE},
2914 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002915#ifdef FEAT_EVAL
2916 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002917 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002918#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002919 };
2920 int i;
2921 int ret = FAIL;
2922
2923 // Find a context in the table and call the ExpandGeneric() with the
2924 // right function to do the expansion.
2925 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2926 {
2927 if (xp->xp_context == tab[i].context)
2928 {
2929 if (tab[i].ic)
2930 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002931 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2932 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002933 break;
2934 }
2935 }
2936
2937 return ret;
2938}
2939
2940/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002941 * Map wild expand options to flags for expand_wildcards()
2942 */
2943 static int
2944map_wildopts_to_ewflags(int options)
2945{
2946 int flags;
2947
2948 flags = EW_DIR; // include directories
2949 if (options & WILD_LIST_NOTFOUND)
2950 flags |= EW_NOTFOUND;
2951 if (options & WILD_ADD_SLASH)
2952 flags |= EW_ADDSLASH;
2953 if (options & WILD_KEEP_ALL)
2954 flags |= EW_KEEPALL;
2955 if (options & WILD_SILENT)
2956 flags |= EW_SILENT;
2957 if (options & WILD_NOERROR)
2958 flags |= EW_NOERROR;
2959 if (options & WILD_ALLLINKS)
2960 flags |= EW_ALLLINKS;
2961
2962 return flags;
2963}
2964
2965/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002966 * Do the expansion based on xp->xp_context and "pat".
2967 */
2968 static int
2969ExpandFromContext(
2970 expand_T *xp,
2971 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002972 char_u ***matches,
2973 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002974 int options) // WILD_ flags
2975{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002976 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002977 int ret;
2978 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002979 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002980 int fuzzy = cmdline_fuzzy_complete(pat)
2981 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002982
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002983 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002984
2985 if (xp->xp_context == EXPAND_FILES
2986 || xp->xp_context == EXPAND_DIRECTORIES
2987 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002988 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2989 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002990
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002991 *matches = (char_u **)"";
2992 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002993 if (xp->xp_context == EXPAND_HELP)
2994 {
2995 // With an empty argument we would get all the help tags, which is
2996 // very slow. Get matches for "help" instead.
2997 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002998 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002999 {
3000#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003001 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003002#endif
3003 return OK;
3004 }
3005 return FAIL;
3006 }
3007
Bram Moolenaar66b51422019-08-18 21:44:12 +02003008 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003009 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003010 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003011 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003012 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003013 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003014#ifdef FEAT_DIFF
3015 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003016 return ExpandBufnames(pat, numMatches, matches,
3017 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003018#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003019 if (xp->xp_context == EXPAND_TAGS
3020 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003021 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3022 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003023 if (xp->xp_context == EXPAND_COLORS)
3024 {
3025 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003026 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003027 directories);
3028 }
roota6759382023-01-21 21:56:06 +00003029 if (xp->xp_context == EXPAND_RUNTIME)
3030 {
3031 char *directories[] = {"", NULL};
3032 return ExpandRTDir(pat, DIP_START + DIP_OPT + DIP_PRNEXT, numMatches,
3033 matches, directories);
3034 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003035 if (xp->xp_context == EXPAND_COMPILER)
3036 {
3037 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003038 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003039 }
3040 if (xp->xp_context == EXPAND_OWNSYNTAX)
3041 {
3042 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003043 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003044 }
3045 if (xp->xp_context == EXPAND_FILETYPE)
3046 {
3047 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003048 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003049 }
K.Takata161b6ac2022-11-14 15:31:07 +00003050#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003051 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003052 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003053#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003054 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003055 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003056
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003057 // When expanding a function name starting with s:, match the <SNR>nr_
3058 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003059 if ((xp->xp_context == EXPAND_USER_FUNC
3060 || xp->xp_context == EXPAND_DISASSEMBLE)
3061 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003062 {
3063 int len = (int)STRLEN(pat) + 20;
3064
3065 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003066 if (tofree == NULL)
3067 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003068 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003069 pat = tofree;
3070 }
3071
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003072 if (!fuzzy)
3073 {
3074 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3075 if (regmatch.regprog == NULL)
3076 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003077
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003078 // set ignore-case according to p_ic, p_scs and pat
3079 regmatch.rm_ic = ignorecase(pat);
3080 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003081
3082 if (xp->xp_context == EXPAND_SETTINGS
3083 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003084 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003085 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003086 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003087#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003088 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003089 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003090#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003091 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003092 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003093
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003094 if (!fuzzy)
3095 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003096 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003097
3098 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003099}
3100
Bram Moolenaar66b51422019-08-18 21:44:12 +02003101/*
3102 * Expand a list of names.
3103 *
3104 * Generic function for command line completion. It calls a function to
3105 * obtain strings, one by one. The strings are matched against a regexp
3106 * program. Matching strings are copied into an array, which is returned.
3107 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003108 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3109 * is used.
3110 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111 * Returns OK when no problems encountered, FAIL for error (out of memory).
3112 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003113 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003114ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003115 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003116 expand_T *xp,
3117 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003118 char_u ***matches,
3119 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120 char_u *((*func)(expand_T *, int)),
3121 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003122 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003123{
3124 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003125 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003126 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003127 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003128 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003129 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003130 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003131 int sort_matches = FALSE;
3132 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003134 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003135 *matches = NULL;
3136 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003137
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003138 if (!fuzzy)
3139 ga_init2(&ga, sizeof(char *), 30);
3140 else
3141 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3142
3143 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003144 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003145 str = (*func)(xp, i);
3146 if (str == NULL) // end of list
3147 break;
3148 if (*str == NUL) // skip empty strings
3149 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003150
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003151 if (xp->xp_pattern[0] != NUL)
3152 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003153 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003154 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003155 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003156 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003157 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003158 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003159 }
3160 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003161 else
3162 match = TRUE;
3163
3164 if (!match)
3165 continue;
3166
3167 if (escaped)
3168 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3169 else
3170 str = vim_strsave(str);
3171 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003172 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003173 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003174 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003175 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003176 return FAIL;
3177 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003178 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003179 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003180 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003181
3182 if (ga_grow(&ga, 1) == FAIL)
3183 {
3184 vim_free(str);
3185 break;
3186 }
3187
3188 if (fuzzy)
3189 {
3190 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3191 fuzmatch->idx = ga.ga_len;
3192 fuzmatch->str = str;
3193 fuzmatch->score = score;
3194 }
3195 else
3196 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3197
K.Takata161b6ac2022-11-14 15:31:07 +00003198#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003199 if (func == get_menu_names)
3200 {
3201 // test for separator added by get_menu_names()
3202 str += STRLEN(str) - 1;
3203 if (*str == '\001')
3204 *str = '.';
3205 }
K.Takata161b6ac2022-11-14 15:31:07 +00003206#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003207
3208 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003209 }
3210
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003211 if (ga.ga_len == 0)
3212 return OK;
3213
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003214 // sort the matches when using regular expression matching and sorting
3215 // applies to the completion context. Menus and scriptnames should be kept
3216 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003217 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003218 && xp->xp_context != EXPAND_MENUS
3219 && xp->xp_context != EXPAND_SCRIPTNAMES)
3220 sort_matches = TRUE;
3221
3222 // <SNR> functions should be sorted to the end.
3223 if (xp->xp_context == EXPAND_EXPRESSION
3224 || xp->xp_context == EXPAND_FUNCTIONS
3225 || xp->xp_context == EXPAND_USER_FUNC
3226 || xp->xp_context == EXPAND_DISASSEMBLE)
3227 funcsort = TRUE;
3228
3229 // Sort the matches.
3230 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003231 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003232 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003233 // <SNR> functions should be sorted to the end.
3234 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3235 sort_func_compare);
3236 else
3237 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3238 }
3239
3240 if (!fuzzy)
3241 {
3242 *matches = ga.ga_data;
3243 *numMatches = ga.ga_len;
3244 }
3245 else
3246 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003247 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3248 funcsort) == FAIL)
3249 return FAIL;
3250 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003251 }
3252
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003253#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003254 // Reset the variables used for special highlight names expansion, so that
3255 // they don't show up when getting normal highlight names by ID.
3256 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003257#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003258
Bram Moolenaar66b51422019-08-18 21:44:12 +02003259 return OK;
3260}
3261
3262/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003263 * Expand shell command matches in one directory of $PATH.
3264 */
3265 static void
3266expand_shellcmd_onedir(
3267 char_u *buf,
3268 char_u *s,
3269 size_t l,
3270 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003271 char_u ***matches,
3272 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003273 int flags,
3274 hashtab_T *ht,
3275 garray_T *gap)
3276{
3277 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003278 hash_T hash;
3279 hashitem_T *hi;
3280
3281 vim_strncpy(buf, s, l);
3282 add_pathsep(buf);
3283 l = STRLEN(buf);
3284 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3285
3286 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003287 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003288 if (ret != OK)
3289 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003290
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003291 if (ga_grow(gap, *numMatches) == FAIL)
3292 {
3293 FreeWild(*numMatches, *matches);
3294 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003295 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003296
3297 for (int i = 0; i < *numMatches; ++i)
3298 {
3299 char_u *name = (*matches)[i];
3300
3301 if (STRLEN(name) > l)
3302 {
3303 // Check if this name was already found.
3304 hash = hash_hash(name + l);
3305 hi = hash_lookup(ht, name + l, hash);
3306 if (HASHITEM_EMPTY(hi))
3307 {
3308 // Remove the path that was prepended.
3309 STRMOVE(name, name + l);
3310 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3311 hash_add_item(ht, hi, name, hash);
3312 name = NULL;
3313 }
3314 }
3315 vim_free(name);
3316 }
3317 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003318}
3319
3320/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003321 * Complete a shell command.
3322 * Returns FAIL or OK;
3323 */
3324 static int
3325expand_shellcmd(
3326 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003327 char_u ***matches, // return: array with matches
3328 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003329 int flagsarg) // EW_ flags
3330{
3331 char_u *pat;
3332 int i;
3333 char_u *path = NULL;
3334 int mustfree = FALSE;
3335 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003336 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003337 size_t l;
3338 char_u *s, *e;
3339 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003340 int did_curdir = FALSE;
3341 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003342
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003343 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003344 if (buf == NULL)
3345 return FAIL;
3346
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003347 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003348 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003349 if (pat == NULL)
3350 {
3351 vim_free(buf);
3352 return FAIL;
3353 }
3354
Bram Moolenaar66b51422019-08-18 21:44:12 +02003355 for (i = 0; pat[i]; ++i)
3356 if (pat[i] == '\\' && pat[i + 1] == ' ')
3357 STRMOVE(pat + i, pat + i + 1);
3358
3359 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3360
3361 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3362 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3363 path = (char_u *)".";
3364 else
3365 {
3366 // For an absolute name we don't use $PATH.
3367 if (!mch_isFullName(pat))
3368 path = vim_getenv((char_u *)"PATH", &mustfree);
3369 if (path == NULL)
3370 path = (char_u *)"";
3371 }
3372
3373 // Go over all directories in $PATH. Expand matches in that directory and
3374 // collect them in "ga". When "." is not in $PATH also expand for the
3375 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003376 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003377 hash_init(&found_ht);
3378 for (s = path; ; s = e)
3379 {
K.Takata161b6ac2022-11-14 15:31:07 +00003380#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003381 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003382#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003383 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003384#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003385 if (e == NULL)
3386 e = s + STRLEN(s);
3387
3388 if (*s == NUL)
3389 {
3390 if (did_curdir)
3391 break;
3392 // Find directories in the current directory, path is empty.
3393 did_curdir = TRUE;
3394 flags |= EW_DIR;
3395 }
3396 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3397 {
3398 did_curdir = TRUE;
3399 flags |= EW_DIR;
3400 }
3401 else
3402 // Do not match directories inside a $PATH item.
3403 flags &= ~EW_DIR;
3404
3405 l = e - s;
3406 if (l > MAXPATHL - 5)
3407 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003408
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003409 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003410 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003411
Bram Moolenaar66b51422019-08-18 21:44:12 +02003412 if (*e != NUL)
3413 ++e;
3414 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003415 *matches = ga.ga_data;
3416 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003417
3418 vim_free(buf);
3419 vim_free(pat);
3420 if (mustfree)
3421 vim_free(path);
3422 hash_clear(&found_ht);
3423 return OK;
3424}
3425
K.Takata161b6ac2022-11-14 15:31:07 +00003426#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003427/*
3428 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003429 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003430 */
3431 static void *
3432call_user_expand_func(
3433 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003434 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003435{
3436 cmdline_info_T *ccline = get_cmdline_info();
3437 int keep = 0;
3438 typval_T args[4];
3439 sctx_T save_current_sctx = current_sctx;
3440 char_u *pat = NULL;
3441 void *ret;
3442
3443 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3444 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003445
3446 if (ccline->cmdbuff != NULL)
3447 {
3448 keep = ccline->cmdbuff[ccline->cmdlen];
3449 ccline->cmdbuff[ccline->cmdlen] = 0;
3450 }
3451
3452 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3453
3454 args[0].v_type = VAR_STRING;
3455 args[0].vval.v_string = pat;
3456 args[1].v_type = VAR_STRING;
3457 args[1].vval.v_string = xp->xp_line;
3458 args[2].v_type = VAR_NUMBER;
3459 args[2].vval.v_number = xp->xp_col;
3460 args[3].v_type = VAR_UNKNOWN;
3461
3462 current_sctx = xp->xp_script_ctx;
3463
3464 ret = user_expand_func(xp->xp_arg, 3, args);
3465
3466 current_sctx = save_current_sctx;
3467 if (ccline->cmdbuff != NULL)
3468 ccline->cmdbuff[ccline->cmdlen] = keep;
3469
3470 vim_free(pat);
3471 return ret;
3472}
3473
3474/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003475 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3476 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003477 */
3478 static int
3479ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003480 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003481 expand_T *xp,
3482 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003483 char_u ***matches,
3484 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003485{
3486 char_u *retstr;
3487 char_u *s;
3488 char_u *e;
3489 int keep;
3490 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003491 int fuzzy;
3492 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003493 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003494
3495 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003496 *matches = NULL;
3497 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003498
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003499 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003500 if (retstr == NULL)
3501 return FAIL;
3502
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003503 if (!fuzzy)
3504 ga_init2(&ga, sizeof(char *), 3);
3505 else
3506 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3507
Bram Moolenaar66b51422019-08-18 21:44:12 +02003508 for (s = retstr; *s != NUL; s = e)
3509 {
3510 e = vim_strchr(s, '\n');
3511 if (e == NULL)
3512 e = s + STRLEN(s);
3513 keep = *e;
3514 *e = NUL;
3515
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003516 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003517 {
3518 if (!fuzzy)
3519 match = vim_regexec(regmatch, s, (colnr_T)0);
3520 else
3521 {
3522 score = fuzzy_match_str(s, pat);
3523 match = (score != 0);
3524 }
3525 }
3526 else
3527 match = TRUE; // match everything
3528
Bram Moolenaar66b51422019-08-18 21:44:12 +02003529 *e = keep;
3530
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003531 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003532 {
3533 if (ga_grow(&ga, 1) == FAIL)
3534 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003535 if (!fuzzy)
3536 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3537 else
3538 {
3539 fuzmatch_str_T *fuzmatch =
3540 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003541 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003542 fuzmatch->str = vim_strnsave(s, e - s);
3543 fuzmatch->score = score;
3544 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003545 ++ga.ga_len;
3546 }
3547
3548 if (*e != NUL)
3549 ++e;
3550 }
3551 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003552
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003553 if (ga.ga_len == 0)
3554 return OK;
3555
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003556 if (!fuzzy)
3557 {
3558 *matches = ga.ga_data;
3559 *numMatches = ga.ga_len;
3560 }
3561 else
3562 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003563 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3564 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003565 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003566 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003567 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003568 return OK;
3569}
3570
3571/*
3572 * Expand names with a list returned by a function defined by the user.
3573 */
3574 static int
3575ExpandUserList(
3576 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003577 char_u ***matches,
3578 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003579{
3580 list_T *retlist;
3581 listitem_T *li;
3582 garray_T ga;
3583
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003584 *matches = NULL;
3585 *numMatches = 0;
3586 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003587 if (retlist == NULL)
3588 return FAIL;
3589
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003590 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003591 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003592 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003593 {
3594 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3595 continue; // Skip non-string items and empty strings
3596
3597 if (ga_grow(&ga, 1) == FAIL)
3598 break;
3599
3600 ((char_u **)ga.ga_data)[ga.ga_len] =
3601 vim_strsave(li->li_tv.vval.v_string);
3602 ++ga.ga_len;
3603 }
3604 list_unref(retlist);
3605
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003606 *matches = ga.ga_data;
3607 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003608 return OK;
3609}
K.Takata161b6ac2022-11-14 15:31:07 +00003610#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003611
3612/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003613 * Expand "file" for all comma-separated directories in "path".
3614 * Adds the matches to "ga". Caller must init "ga".
3615 */
3616 void
3617globpath(
3618 char_u *path,
3619 char_u *file,
3620 garray_T *ga,
3621 int expand_options)
3622{
3623 expand_T xpc;
3624 char_u *buf;
3625 int i;
3626 int num_p;
3627 char_u **p;
3628
3629 buf = alloc(MAXPATHL);
3630 if (buf == NULL)
3631 return;
3632
3633 ExpandInit(&xpc);
3634 xpc.xp_context = EXPAND_FILES;
3635
3636 // Loop over all entries in {path}.
3637 while (*path != NUL)
3638 {
3639 // Copy one item of the path to buf[] and concatenate the file name.
3640 copy_option_part(&path, buf, MAXPATHL, ",");
3641 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3642 {
K.Takata161b6ac2022-11-14 15:31:07 +00003643#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003644 // Using the platform's path separator (\) makes vim incorrectly
3645 // treat it as an escape character, use '/' instead.
3646 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3647 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003648#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003649 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003650#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003651 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003652 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003653 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3654 {
3655 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3656
3657 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003658 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003659 for (i = 0; i < num_p; ++i)
3660 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003661 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003662 ++ga->ga_len;
3663 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003664 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003665 }
3666 }
3667 }
3668
3669 vim_free(buf);
3670}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003671
Bram Moolenaareadee482020-09-04 15:37:31 +02003672/*
3673 * Translate some keys pressed when 'wildmenu' is used.
3674 */
3675 int
3676wildmenu_translate_key(
3677 cmdline_info_T *cclp,
3678 int key,
3679 expand_T *xp,
3680 int did_wild_list)
3681{
3682 int c = key;
3683
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003684 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003685 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003686 // When the popup menu is used for cmdline completion:
3687 // Up : go to the previous item in the menu
3688 // Down : go to the next item in the menu
3689 // Left : go to the parent directory
3690 // Right: list the files in the selected directory
3691 switch (c)
3692 {
3693 case K_UP: c = K_LEFT; break;
3694 case K_DOWN: c = K_RIGHT; break;
3695 case K_LEFT: c = K_UP; break;
3696 case K_RIGHT: c = K_DOWN; break;
3697 default: break;
3698 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003699 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003700
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003701 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003702 {
3703 if (c == K_LEFT)
3704 c = Ctrl_P;
3705 else if (c == K_RIGHT)
3706 c = Ctrl_N;
3707 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003708
Bram Moolenaareadee482020-09-04 15:37:31 +02003709 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003710 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003711 && cclp->cmdpos > 1
3712 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3713 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3714 && (c == '\n' || c == '\r' || c == K_KENTER))
3715 c = K_DOWN;
3716
3717 return c;
3718}
3719
3720/*
3721 * Delete characters on the command line, from "from" to the current
3722 * position.
3723 */
3724 static void
3725cmdline_del(cmdline_info_T *cclp, int from)
3726{
3727 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3728 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3729 cclp->cmdlen -= cclp->cmdpos - from;
3730 cclp->cmdpos = from;
3731}
3732
3733/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003734 * Handle a key pressed when the wild menu for the menu names
3735 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003736 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003737 static int
3738wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003739{
Bram Moolenaareadee482020-09-04 15:37:31 +02003740 int i;
3741 int j;
3742
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003743 // Hitting <Down> after "emenu Name.": complete submenu
3744 if (key == K_DOWN && cclp->cmdpos > 0
3745 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003746 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003747 key = p_wc;
3748 KeyTyped = TRUE; // in case the key was mapped
3749 }
3750 else if (key == K_UP)
3751 {
3752 // Hitting <Up>: Remove one submenu name in front of the
3753 // cursor
3754 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003755
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003756 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3757 i = 0;
3758 while (--j > 0)
3759 {
3760 // check for start of menu name
3761 if (cclp->cmdbuff[j] == ' '
3762 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003763 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003764 i = j + 1;
3765 break;
3766 }
3767 // check for start of submenu name
3768 if (cclp->cmdbuff[j] == '.'
3769 && cclp->cmdbuff[j - 1] != '\\')
3770 {
3771 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003772 {
3773 i = j + 1;
3774 break;
3775 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003776 else
3777 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003778 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003779 }
3780 if (i > 0)
3781 cmdline_del(cclp, i);
3782 key = p_wc;
3783 KeyTyped = TRUE; // in case the key was mapped
3784 xp->xp_context = EXPAND_NOTHING;
3785 }
3786
3787 return key;
3788}
3789
3790/*
3791 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3792 * directory names (EXPAND_DIRECTORIES) or shell command names
3793 * (EXPAND_SHELLCMD) is displayed.
3794 */
3795 static int
3796wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3797{
3798 int i;
3799 int j;
3800 char_u upseg[5];
3801
3802 upseg[0] = PATHSEP;
3803 upseg[1] = '.';
3804 upseg[2] = '.';
3805 upseg[3] = PATHSEP;
3806 upseg[4] = NUL;
3807
3808 if (key == K_DOWN
3809 && cclp->cmdpos > 0
3810 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3811 && (cclp->cmdpos < 3
3812 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3813 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3814 {
3815 // go down a directory
3816 key = p_wc;
3817 KeyTyped = TRUE; // in case the key was mapped
3818 }
3819 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3820 {
3821 // If in a direct ancestor, strip off one ../ to go down
3822 int found = FALSE;
3823
3824 j = cclp->cmdpos;
3825 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3826 while (--j > i)
3827 {
3828 if (has_mbyte)
3829 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3830 if (vim_ispathsep(cclp->cmdbuff[j]))
3831 {
3832 found = TRUE;
3833 break;
3834 }
3835 }
3836 if (found
3837 && cclp->cmdbuff[j - 1] == '.'
3838 && cclp->cmdbuff[j - 2] == '.'
3839 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3840 {
3841 cmdline_del(cclp, j - 2);
3842 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003843 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003844 }
3845 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003846 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003847 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003848 // go up a directory
3849 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003850
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003851 j = cclp->cmdpos - 1;
3852 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3853 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003854 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003855 if (has_mbyte)
3856 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3857 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003858#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003859 && vim_strchr((char_u *)" *?[{`$%#",
3860 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003861#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003862 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003863 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003864 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003865 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003866 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003867 break;
3868 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003869 else
3870 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003871 }
3872 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003873
3874 if (!found)
3875 j = i;
3876 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3877 j += 4;
3878 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3879 && j == i)
3880 j += 3;
3881 else
3882 j = 0;
3883 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003884 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003885 // TODO this is only for DOS/UNIX systems - need to put in
3886 // machine-specific stuff here and in upseg init
3887 cmdline_del(cclp, j);
3888 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003889 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003890 else if (cclp->cmdpos > i)
3891 cmdline_del(cclp, i);
3892
3893 // Now complete in the new directory. Set KeyTyped in case the
3894 // Up key came from a mapping.
3895 key = p_wc;
3896 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003897 }
3898
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003899 return key;
3900}
3901
3902/*
3903 * Handle a key pressed when the wild menu is displayed
3904 */
3905 int
3906wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3907{
3908 if (xp->xp_context == EXPAND_MENUNAMES)
3909 return wildmenu_process_key_menunames(cclp, key, xp);
3910 else if ((xp->xp_context == EXPAND_FILES
3911 || xp->xp_context == EXPAND_DIRECTORIES
3912 || xp->xp_context == EXPAND_SHELLCMD))
3913 return wildmenu_process_key_filenames(cclp, key, xp);
3914
3915 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003916}
3917
3918/*
3919 * Free expanded names when finished walking through the matches
3920 */
3921 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003922wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003923{
3924 int skt = KeyTyped;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003925#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003926 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003927#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003928
3929 if (!p_wmnu || wild_menu_showing == 0)
3930 return;
3931
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003932#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003933 if (cclp->input_fn)
3934 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003935#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003936
3937 if (wild_menu_showing == WM_SCROLLED)
3938 {
3939 // Entered command line, move it up
3940 cmdline_row--;
3941 redrawcmd();
3942 }
3943 else if (save_p_ls != -1)
3944 {
3945 // restore 'laststatus' and 'winminheight'
3946 p_ls = save_p_ls;
3947 p_wmh = save_p_wmh;
3948 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003949 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003950 redrawcmd();
3951 save_p_ls = -1;
3952 }
3953 else
3954 {
3955 win_redraw_last_status(topframe);
3956 redraw_statuslines();
3957 }
3958 KeyTyped = skt;
3959 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003960#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003961 if (cclp->input_fn)
3962 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003963#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003964}
Bram Moolenaareadee482020-09-04 15:37:31 +02003965
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003966#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003967/*
3968 * "getcompletion()" function
3969 */
3970 void
3971f_getcompletion(typval_T *argvars, typval_T *rettv)
3972{
3973 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003974 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003975 expand_T xpc;
3976 int filtered = FALSE;
3977 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003978 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003979
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003980 if (in_vim9script()
3981 && (check_for_string_arg(argvars, 0) == FAIL
3982 || check_for_string_arg(argvars, 1) == FAIL
3983 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3984 return;
3985
ii144785fe02021-11-21 12:13:56 +00003986 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003987 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003988 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003989 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003990
3991 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003992 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003993
3994 if (p_wic)
3995 options |= WILD_ICASE;
3996
3997 // For filtered results, 'wildignore' is used
3998 if (!filtered)
3999 options |= WILD_KEEP_ALL;
4000
4001 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004002 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004003 {
ii144785fe02021-11-21 12:13:56 +00004004 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004005 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00004006 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004007 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004008 else
4009 {
ii144785fe02021-11-21 12:13:56 +00004010 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004011 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4012
4013 xpc.xp_context = cmdcomplete_str_to_type(type);
4014 if (xpc.xp_context == EXPAND_NOTHING)
4015 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004016 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004017 return;
4018 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004019
4020# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004021 if (xpc.xp_context == EXPAND_MENUS)
4022 {
4023 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4024 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4025 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004026# endif
4027# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004028 if (xpc.xp_context == EXPAND_CSCOPE)
4029 {
4030 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4031 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4032 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004033# endif
4034# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004035 if (xpc.xp_context == EXPAND_SIGN)
4036 {
4037 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4038 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4039 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004040# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004041 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004042
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004043 if (cmdline_fuzzy_completion_supported(&xpc))
4044 // when fuzzy matching, don't modify the search string
4045 pat = vim_strsave(xpc.xp_pattern);
4046 else
4047 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4048
Bram Moolenaar93a10962022-06-16 11:42:09 +01004049 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004050 {
4051 int i;
4052
4053 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4054
4055 for (i = 0; i < xpc.xp_numfiles; i++)
4056 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4057 }
4058 vim_free(pat);
4059 ExpandCleanup(&xpc);
4060}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004061#endif // FEAT_EVAL