blob: 4ef9c31121fe4f1f9e62f0a02dc99a07d24a6d18 [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)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001015 {
1016 if (i > 0)
1017 {
1018 if (xp->xp_prefix == XP_PREFIX_NO)
1019 len += 2; // prefix "no"
1020 else if (xp->xp_prefix == XP_PREFIX_INV)
1021 len += 3; // prefix "inv"
1022 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001023 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001024 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001025 ss = alloc(len);
1026 if (ss != NULL)
1027 {
1028 *ss = NUL;
1029 for (i = 0; i < xp->xp_numfiles; ++i)
1030 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001031 if (i > 0)
1032 {
1033 if (xp->xp_prefix == XP_PREFIX_NO)
1034 STRCAT(ss, "no");
1035 else if (xp->xp_prefix == XP_PREFIX_INV)
1036 STRCAT(ss, "inv");
1037 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001038 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001039
Bram Moolenaar66b51422019-08-18 21:44:12 +02001040 if (i != xp->xp_numfiles - 1)
1041 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1042 }
1043 }
1044 }
1045
1046 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1047 ExpandCleanup(xp);
1048
1049 // Free "orig" if it wasn't stored in "orig_save".
1050 if (!orig_saved)
1051 vim_free(orig);
1052
1053 return ss;
1054}
1055
1056/*
1057 * Prepare an expand structure for use.
1058 */
1059 void
1060ExpandInit(expand_T *xp)
1061{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001062 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001063 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001064 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001065 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001066}
1067
1068/*
1069 * Cleanup an expand structure after use.
1070 */
1071 void
1072ExpandCleanup(expand_T *xp)
1073{
1074 if (xp->xp_numfiles >= 0)
1075 {
1076 FreeWild(xp->xp_numfiles, xp->xp_files);
1077 xp->xp_numfiles = -1;
1078 }
1079}
1080
1081/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001082 * Display one line of completion matches. Multiple matches are displayed in
1083 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001084 * matches - list of completion match names
1085 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001086 * lines - number of output lines
1087 * linenr - line number of matches to display
1088 * maxlen - maximum number of characters in each line
1089 * showtail - display only the tail of the full path of a file name
1090 * dir_attr - highlight attribute to use for directory names
1091 */
1092 static void
1093showmatches_oneline(
1094 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001095 char_u **matches,
1096 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001097 int lines,
1098 int linenr,
1099 int maxlen,
1100 int showtail,
1101 int dir_attr)
1102{
1103 int i, j;
1104 int isdir;
1105 int lastlen;
1106 char_u *p;
1107
1108 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001109 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001110 {
1111 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1112 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001113 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1114 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001115 msg_advance(maxlen + 1);
1116 msg_puts((char *)p);
1117 msg_advance(maxlen + 3);
1118 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1119 break;
1120 }
1121 for (i = maxlen - lastlen; --i >= 0; )
1122 msg_putchar(' ');
1123 if (xp->xp_context == EXPAND_FILES
1124 || xp->xp_context == EXPAND_SHELLCMD
1125 || xp->xp_context == EXPAND_BUFFERS)
1126 {
1127 // highlight directories
1128 if (xp->xp_numfiles != -1)
1129 {
1130 char_u *halved_slash;
1131 char_u *exp_path;
1132 char_u *path;
1133
1134 // Expansion was done before and special characters
1135 // were escaped, need to halve backslashes. Also
1136 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001137 exp_path = expand_env_save_opt(matches[j], TRUE);
1138 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001139 halved_slash = backslash_halve_save(path);
1140 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001141 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001142 vim_free(exp_path);
1143 if (halved_slash != path)
1144 vim_free(halved_slash);
1145 }
1146 else
1147 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001148 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001149 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001150 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001151 else
1152 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001153 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001154 TRUE);
1155 p = NameBuff;
1156 }
1157 }
1158 else
1159 {
1160 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001161 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001162 }
1163 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1164 }
1165 if (msg_col > 0) // when not wrapped around
1166 {
1167 msg_clr_eos();
1168 msg_putchar('\n');
1169 }
1170 out_flush(); // show one line at a time
1171}
1172
1173/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001174 * Show all matches for completion on the command line.
1175 * Returns EXPAND_NOTHING when the character that triggered expansion should
1176 * be inserted like a normal character.
1177 */
1178 int
1179showmatches(expand_T *xp, int wildmenu UNUSED)
1180{
1181 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001182 int numMatches;
1183 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001184 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001185 int maxlen;
1186 int lines;
1187 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001188 int attr;
1189 int showtail;
1190
1191 if (xp->xp_numfiles == -1)
1192 {
1193 set_expand_context(xp);
1194 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001195 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001196 showtail = expand_showtail(xp);
1197 if (i != EXPAND_OK)
1198 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001199 }
1200 else
1201 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001202 numMatches = xp->xp_numfiles;
1203 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001204 showtail = cmd_showtail;
1205 }
1206
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001207 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001208 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001209 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001210
Bram Moolenaar66b51422019-08-18 21:44:12 +02001211 if (!wildmenu)
1212 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001213 msg_didany = FALSE; // lines_left will be set
1214 msg_start(); // prepare for paging
1215 msg_putchar('\n');
1216 out_flush();
1217 cmdline_row = msg_row;
1218 msg_didany = FALSE; // lines_left will be set again
1219 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001220 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001221
1222 if (got_int)
1223 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001224 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001225 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001226 else
1227 {
1228 // find the length of the longest file name
1229 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001230 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001231 {
1232 if (!showtail && (xp->xp_context == EXPAND_FILES
1233 || xp->xp_context == EXPAND_SHELLCMD
1234 || xp->xp_context == EXPAND_BUFFERS))
1235 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001236 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001237 j = vim_strsize(NameBuff);
1238 }
1239 else
zeertzjqc51a3762022-12-10 10:22:29 +00001240 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001241 if (j > maxlen)
1242 maxlen = j;
1243 }
1244
1245 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001246 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001247 else
1248 {
1249 // compute the number of columns and lines for the listing
1250 maxlen += 2; // two spaces between file names
1251 columns = ((int)Columns + 2) / maxlen;
1252 if (columns < 1)
1253 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001254 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001255 }
1256
1257 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1258
1259 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1260 {
1261 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1262 msg_clr_eos();
1263 msg_advance(maxlen - 3);
1264 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1265 }
1266
1267 // list the files line by line
1268 for (i = 0; i < lines; ++i)
1269 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001270 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001271 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001272 if (got_int)
1273 {
1274 got_int = FALSE;
1275 break;
1276 }
1277 }
1278
1279 // we redraw the command below the lines that we have just listed
1280 // This is a bit tricky, but it saves a lot of screen updating.
1281 cmdline_row = msg_row; // will put it back later
1282 }
1283
1284 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001285 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001286
1287 return EXPAND_OK;
1288}
1289
1290/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001291 * gettail() version for showmatches() and win_redr_status_matches():
1292 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001293 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001294 static char_u *
1295showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001296{
1297 char_u *p;
1298 char_u *t = s;
1299 int had_sep = FALSE;
1300
1301 for (p = s; *p != NUL; )
1302 {
1303 if (vim_ispathsep(*p)
1304#ifdef BACKSLASH_IN_FILENAME
1305 && !rem_backslash(p)
1306#endif
1307 )
1308 had_sep = TRUE;
1309 else if (had_sep)
1310 {
1311 t = p;
1312 had_sep = FALSE;
1313 }
1314 MB_PTR_ADV(p);
1315 }
1316 return t;
1317}
1318
1319/*
1320 * Return TRUE if we only need to show the tail of completion matches.
1321 * When not completing file names or there is a wildcard in the path FALSE is
1322 * returned.
1323 */
1324 static int
1325expand_showtail(expand_T *xp)
1326{
1327 char_u *s;
1328 char_u *end;
1329
1330 // When not completing file names a "/" may mean something different.
1331 if (xp->xp_context != EXPAND_FILES
1332 && xp->xp_context != EXPAND_SHELLCMD
1333 && xp->xp_context != EXPAND_DIRECTORIES)
1334 return FALSE;
1335
1336 end = gettail(xp->xp_pattern);
1337 if (end == xp->xp_pattern) // there is no path separator
1338 return FALSE;
1339
1340 for (s = xp->xp_pattern; s < end; s++)
1341 {
1342 // Skip escaped wildcards. Only when the backslash is not a path
1343 // separator, on DOS the '*' "path\*\file" must not be skipped.
1344 if (rem_backslash(s))
1345 ++s;
1346 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1347 return FALSE;
1348 }
1349 return TRUE;
1350}
1351
1352/*
1353 * Prepare a string for expansion.
1354 * When expanding file names: The string will be used with expand_wildcards().
1355 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1356 * When expanding other names: The string will be used with regcomp(). Copy
1357 * the name into allocated memory and prepend "^".
1358 */
1359 char_u *
1360addstar(
1361 char_u *fname,
1362 int len,
1363 int context) // EXPAND_FILES etc.
1364{
1365 char_u *retval;
1366 int i, j;
1367 int new_len;
1368 char_u *tail;
1369 int ends_in_star;
1370
1371 if (context != EXPAND_FILES
1372 && context != EXPAND_FILES_IN_PATH
1373 && context != EXPAND_SHELLCMD
1374 && context != EXPAND_DIRECTORIES)
1375 {
1376 // Matching will be done internally (on something other than files).
1377 // So we convert the file-matching-type wildcards into our kind for
1378 // use with vim_regcomp(). First work out how long it will be:
1379
1380 // For help tags the translation is done in find_help_tags().
1381 // For a tag pattern starting with "/" no translation is needed.
1382 if (context == EXPAND_HELP
1383 || context == EXPAND_COLORS
1384 || context == EXPAND_COMPILER
1385 || context == EXPAND_OWNSYNTAX
1386 || context == EXPAND_FILETYPE
1387 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001388 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001389 || ((context == EXPAND_TAGS_LISTFILES
1390 || context == EXPAND_TAGS)
1391 && fname[0] == '/'))
1392 retval = vim_strnsave(fname, len);
1393 else
1394 {
1395 new_len = len + 2; // +2 for '^' at start, NUL at end
1396 for (i = 0; i < len; i++)
1397 {
1398 if (fname[i] == '*' || fname[i] == '~')
1399 new_len++; // '*' needs to be replaced by ".*"
1400 // '~' needs to be replaced by "\~"
1401
1402 // Buffer names are like file names. "." should be literal
1403 if (context == EXPAND_BUFFERS && fname[i] == '.')
1404 new_len++; // "." becomes "\."
1405
1406 // Custom expansion takes care of special things, match
1407 // backslashes literally (perhaps also for other types?)
1408 if ((context == EXPAND_USER_DEFINED
1409 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1410 new_len++; // '\' becomes "\\"
1411 }
1412 retval = alloc(new_len);
1413 if (retval != NULL)
1414 {
1415 retval[0] = '^';
1416 j = 1;
1417 for (i = 0; i < len; i++, j++)
1418 {
1419 // Skip backslash. But why? At least keep it for custom
1420 // expansion.
1421 if (context != EXPAND_USER_DEFINED
1422 && context != EXPAND_USER_LIST
1423 && fname[i] == '\\'
1424 && ++i == len)
1425 break;
1426
1427 switch (fname[i])
1428 {
1429 case '*': retval[j++] = '.';
1430 break;
1431 case '~': retval[j++] = '\\';
1432 break;
1433 case '?': retval[j] = '.';
1434 continue;
1435 case '.': if (context == EXPAND_BUFFERS)
1436 retval[j++] = '\\';
1437 break;
1438 case '\\': if (context == EXPAND_USER_DEFINED
1439 || context == EXPAND_USER_LIST)
1440 retval[j++] = '\\';
1441 break;
1442 }
1443 retval[j] = fname[i];
1444 }
1445 retval[j] = NUL;
1446 }
1447 }
1448 }
1449 else
1450 {
1451 retval = alloc(len + 4);
1452 if (retval != NULL)
1453 {
1454 vim_strncpy(retval, fname, len);
1455
1456 // Don't add a star to *, ~, ~user, $var or `cmd`.
1457 // * would become **, which walks the whole tree.
1458 // ~ would be at the start of the file name, but not the tail.
1459 // $ could be anywhere in the tail.
1460 // ` could be anywhere in the file name.
1461 // When the name ends in '$' don't add a star, remove the '$'.
1462 tail = gettail(retval);
1463 ends_in_star = (len > 0 && retval[len - 1] == '*');
1464#ifndef BACKSLASH_IN_FILENAME
1465 for (i = len - 2; i >= 0; --i)
1466 {
1467 if (retval[i] != '\\')
1468 break;
1469 ends_in_star = !ends_in_star;
1470 }
1471#endif
1472 if ((*retval != '~' || tail != retval)
1473 && !ends_in_star
1474 && vim_strchr(tail, '$') == NULL
1475 && vim_strchr(retval, '`') == NULL)
1476 retval[len++] = '*';
1477 else if (len > 0 && retval[len - 1] == '$')
1478 --len;
1479 retval[len] = NUL;
1480 }
1481 }
1482 return retval;
1483}
1484
1485/*
1486 * Must parse the command line so far to work out what context we are in.
1487 * Completion can then be done based on that context.
1488 * This routine sets the variables:
1489 * xp->xp_pattern The start of the pattern to be expanded within
1490 * the command line (ends at the cursor).
1491 * xp->xp_context The type of thing to expand. Will be one of:
1492 *
1493 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1494 * the command line, like an unknown command. Caller
1495 * should beep.
1496 * EXPAND_NOTHING Unrecognised context for completion, use char like
1497 * a normal char, rather than for completion. eg
1498 * :s/^I/
1499 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1500 * it.
1501 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1502 * EXPAND_FILES After command with EX_XFILE set, or after setting
1503 * with P_EXPAND set. eg :e ^I, :w>>^I
1504 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001505 * when we know only directories are of interest.
1506 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001507 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1508 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1509 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1510 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1511 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1512 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1513 * EXPAND_EVENTS Complete event names
1514 * EXPAND_SYNTAX Complete :syntax command arguments
1515 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1516 * EXPAND_AUGROUP Complete autocommand group names
1517 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1518 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1519 * eg :unmap a^I , :cunab x^I
1520 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1521 * eg :call sub^I
1522 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1523 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1524 * names in expressions, eg :while s^I
1525 * EXPAND_ENV_VARS Complete environment variable names
1526 * EXPAND_USER Complete user names
1527 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001528 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001529set_expand_context(expand_T *xp)
1530{
1531 cmdline_info_T *ccline = get_cmdline_info();
1532
1533 // only expansion for ':', '>' and '=' command-lines
1534 if (ccline->cmdfirstc != ':'
1535#ifdef FEAT_EVAL
1536 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1537 && !ccline->input_fn
1538#endif
1539 )
1540 {
1541 xp->xp_context = EXPAND_NOTHING;
1542 return;
1543 }
1544 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1545}
1546
Bram Moolenaard0190392019-08-23 21:17:35 +02001547/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001548 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1549 * For user defined commands, the completion context is set in 'xp' and the
1550 * completion flags in 'complp'.
1551 *
1552 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001553 */
1554 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001555set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001556{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001557 char_u *p = NULL;
1558 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001559 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001560
1561 // Isolate the command and search for it in the command table.
1562 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001563 // - the 'k' command can directly be followed by any character, but do
1564 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1565 // find matches anywhere in the command name, do this only for command
1566 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001567 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001568 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001569 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001570 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001571 p = cmd + 1;
1572 }
1573 else
1574 {
1575 p = cmd;
1576 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1577 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001578 // A user command may contain digits.
1579 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1580 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001581 while (ASCII_ISALNUM(*p) || *p == '*')
1582 ++p;
1583 // for python 3.x: ":py3*" commands completion
1584 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1585 {
1586 ++p;
1587 while (ASCII_ISALPHA(*p) || *p == '*')
1588 ++p;
1589 }
1590 // check for non-alpha command
1591 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1592 ++p;
1593 len = (int)(p - cmd);
1594
1595 if (len == 0)
1596 {
1597 xp->xp_context = EXPAND_UNSUCCESSFUL;
1598 return NULL;
1599 }
1600
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001601 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001602
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001603 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001604 // Also when doing fuzzy expansion for non-shell commands, support
1605 // alphanumeric characters.
1606 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1607 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001608 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1609 ++p;
1610 }
1611
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001612 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001613 // character, complete the command name.
1614 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1615 return NULL;
1616
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001617 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001618 {
1619 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1620 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001621 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001622 p = cmd + 1;
1623 }
1624 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1625 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001626 eap->cmd = cmd;
1627 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001628 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001629 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001630 }
1631 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001632 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001633 {
1634 // Not still touching the command and it was an illegal one
1635 xp->xp_context = EXPAND_UNSUCCESSFUL;
1636 return NULL;
1637 }
1638
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001639 return p;
1640}
Bram Moolenaard0190392019-08-23 21:17:35 +02001641
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001642/*
1643 * Set the completion context for a command argument with wild card characters.
1644 */
1645 static void
1646set_context_for_wildcard_arg(
1647 exarg_T *eap,
1648 char_u *arg,
1649 int usefilter,
1650 expand_T *xp,
1651 int *complp)
1652{
1653 char_u *p;
1654 int c;
1655 int in_quote = FALSE;
1656 char_u *bow = NULL; // Beginning of word
1657 int len = 0;
1658
1659 // Allow spaces within back-quotes to count as part of the argument
1660 // being expanded.
1661 xp->xp_pattern = skipwhite(arg);
1662 p = xp->xp_pattern;
1663 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001664 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001665 if (has_mbyte)
1666 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001667 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001668 c = *p;
1669 if (c == '\\' && p[1] != NUL)
1670 ++p;
1671 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001672 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001673 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001674 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001675 xp->xp_pattern = p;
1676 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001678 in_quote = !in_quote;
1679 }
1680 // An argument can contain just about everything, except
1681 // characters that end the command and white space.
1682 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001683#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001684 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001685#endif
1686 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001687 {
1688 len = 0; // avoid getting stuck when space is in 'isfname'
1689 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001690 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001691 if (has_mbyte)
1692 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001693 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001694 c = *p;
1695 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001696 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001697 if (has_mbyte)
1698 len = (*mb_ptr2len)(p);
1699 else
1700 len = 1;
1701 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001702 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001703 if (in_quote)
1704 bow = p;
1705 else
1706 xp->xp_pattern = p;
1707 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001708 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001709 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001710 }
1711
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001712 // If we are still inside the quotes, and we passed a space, just
1713 // expand from there.
1714 if (bow != NULL && in_quote)
1715 xp->xp_pattern = bow;
1716 xp->xp_context = EXPAND_FILES;
1717
1718 // For a shell command more chars need to be escaped.
1719 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1720 {
1721#ifndef BACKSLASH_IN_FILENAME
1722 xp->xp_shell = TRUE;
1723#endif
1724 // When still after the command name expand executables.
1725 if (xp->xp_pattern == skipwhite(arg))
1726 xp->xp_context = EXPAND_SHELLCMD;
1727 }
1728
1729 // Check for environment variable.
1730 if (*xp->xp_pattern == '$')
1731 {
1732 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1733 if (!vim_isIDc(*p))
1734 break;
1735 if (*p == NUL)
1736 {
1737 xp->xp_context = EXPAND_ENV_VARS;
1738 ++xp->xp_pattern;
1739 // Avoid that the assignment uses EXPAND_FILES again.
1740 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1741 *complp = EXPAND_ENV_VARS;
1742 }
1743 }
1744 // Check for user names.
1745 if (*xp->xp_pattern == '~')
1746 {
1747 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1748 ;
1749 // Complete ~user only if it partially matches a user name.
1750 // A full match ~user<Tab> will be replaced by user's home
1751 // directory i.e. something like ~user<Tab> -> /home/user/
1752 if (*p == NUL && p > xp->xp_pattern + 1
1753 && match_user(xp->xp_pattern + 1) >= 1)
1754 {
1755 xp->xp_context = EXPAND_USER;
1756 ++xp->xp_pattern;
1757 }
1758 }
1759}
1760
1761/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001762 * Set the completion context for the :filter command. Returns a pointer to the
1763 * next command after the :filter command.
1764 */
1765 static char_u *
1766set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1767{
1768 if (*arg != NUL)
1769 arg = skip_vimgrep_pat(arg, NULL, NULL);
1770 if (arg == NULL || *arg == NUL)
1771 {
1772 xp->xp_context = EXPAND_NOTHING;
1773 return NULL;
1774 }
1775 return skipwhite(arg);
1776}
1777
1778#ifdef FEAT_SEARCH_EXTRA
1779/*
1780 * Set the completion context for the :match command. Returns a pointer to the
1781 * next command after the :match command.
1782 */
1783 static char_u *
1784set_context_in_match_cmd(expand_T *xp, char_u *arg)
1785{
1786 if (*arg == NUL || !ends_excmd(*arg))
1787 {
1788 // also complete "None"
1789 set_context_in_echohl_cmd(xp, arg);
1790 arg = skipwhite(skiptowhite(arg));
1791 if (*arg != NUL)
1792 {
1793 xp->xp_context = EXPAND_NOTHING;
1794 arg = skip_regexp(arg + 1, *arg, magic_isset());
1795 }
1796 }
1797 return find_nextcmd(arg);
1798}
1799#endif
1800
1801/*
1802 * Returns a pointer to the next command after a :global or a :v command.
1803 * Returns NULL if there is no next command.
1804 */
1805 static char_u *
1806find_cmd_after_global_cmd(char_u *arg)
1807{
1808 int delim;
1809
1810 delim = *arg; // get the delimiter
1811 if (delim)
1812 ++arg; // skip delimiter if there is one
1813
1814 while (arg[0] != NUL && arg[0] != delim)
1815 {
1816 if (arg[0] == '\\' && arg[1] != NUL)
1817 ++arg;
1818 ++arg;
1819 }
1820 if (arg[0] != NUL)
1821 return arg + 1;
1822
1823 return NULL;
1824}
1825
1826/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001827 * Returns a pointer to the next command after a :substitute or a :& command.
1828 * Returns NULL if there is no next command.
1829 */
1830 static char_u *
1831find_cmd_after_substitute_cmd(char_u *arg)
1832{
1833 int delim;
1834
1835 delim = *arg;
1836 if (delim)
1837 {
1838 // skip "from" part
1839 ++arg;
1840 arg = skip_regexp(arg, delim, magic_isset());
1841
1842 if (arg[0] != NUL && arg[0] == delim)
1843 {
1844 // skip "to" part
1845 ++arg;
1846 while (arg[0] != NUL && arg[0] != delim)
1847 {
1848 if (arg[0] == '\\' && arg[1] != NUL)
1849 ++arg;
1850 ++arg;
1851 }
1852 if (arg[0] != NUL) // skip delimiter
1853 ++arg;
1854 }
1855 }
1856 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1857 ++arg;
1858 if (arg[0] != NUL)
1859 return arg;
1860
1861 return NULL;
1862}
1863
1864/*
1865 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1866 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1867 * Returns NULL if there is no next command.
1868 */
1869 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001870find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001871{
1872 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001873 if (*arg != '/')
1874 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001875
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001876 // Match regexp, not just whole words
1877 for (++arg; *arg && *arg != '/'; arg++)
1878 if (*arg == '\\' && arg[1] != NUL)
1879 arg++;
1880 if (*arg)
1881 {
1882 arg = skipwhite(arg + 1);
1883
1884 // Check for trailing illegal characters
1885 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1886 xp->xp_context = EXPAND_NOTHING;
1887 else
1888 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001889 }
1890
1891 return NULL;
1892}
1893
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001894#ifdef FEAT_EVAL
1895/*
1896 * Set the completion context for the :unlet command. Always returns NULL.
1897 */
1898 static char_u *
1899set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1900{
1901 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1902 arg = xp->xp_pattern + 1;
1903
1904 xp->xp_context = EXPAND_USER_VARS;
1905 xp->xp_pattern = arg;
1906
1907 if (*xp->xp_pattern == '$')
1908 {
1909 xp->xp_context = EXPAND_ENV_VARS;
1910 ++xp->xp_pattern;
1911 }
1912
1913 return NULL;
1914}
1915#endif
1916
1917#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1918/*
1919 * Set the completion context for the :language command. Always returns NULL.
1920 */
1921 static char_u *
1922set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1923{
1924 char_u *p;
1925
1926 p = skiptowhite(arg);
1927 if (*p == NUL)
1928 {
1929 xp->xp_context = EXPAND_LANGUAGE;
1930 xp->xp_pattern = arg;
1931 }
1932 else
1933 {
1934 if ( STRNCMP(arg, "messages", p - arg) == 0
1935 || STRNCMP(arg, "ctype", p - arg) == 0
1936 || STRNCMP(arg, "time", p - arg) == 0
1937 || STRNCMP(arg, "collate", p - arg) == 0)
1938 {
1939 xp->xp_context = EXPAND_LOCALES;
1940 xp->xp_pattern = skipwhite(p);
1941 }
1942 else
1943 xp->xp_context = EXPAND_NOTHING;
1944 }
1945
1946 return NULL;
1947}
1948#endif
1949
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001950#ifdef FEAT_EVAL
1951static enum
1952{
1953 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001954 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1955 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001956} breakpt_expand_what;
1957
1958/*
1959 * Set the completion context for the :breakadd command. Always returns NULL.
1960 */
1961 static char_u *
1962set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1963{
1964 char_u *p;
1965 char_u *subcmd_start;
1966
1967 xp->xp_context = EXPAND_BREAKPOINT;
1968 xp->xp_pattern = arg;
1969
1970 if (cmdidx == CMD_breakadd)
1971 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001972 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001973 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001974 else
1975 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001976
1977 p = skipwhite(arg);
1978 if (*p == NUL)
1979 return NULL;
1980 subcmd_start = p;
1981
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001982 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001983 {
1984 // :breakadd file [lnum] <filename>
1985 // :breakadd func [lnum] <funcname>
1986 p += 4;
1987 p = skipwhite(p);
1988
1989 // skip line number (if specified)
1990 if (VIM_ISDIGIT(*p))
1991 {
1992 p = skipdigits(p);
1993 if (*p != ' ')
1994 {
1995 xp->xp_context = EXPAND_NOTHING;
1996 return NULL;
1997 }
1998 p = skipwhite(p);
1999 }
2000 if (STRNCMP("file", subcmd_start, 4) == 0)
2001 xp->xp_context = EXPAND_FILES;
2002 else
2003 xp->xp_context = EXPAND_USER_FUNC;
2004 xp->xp_pattern = p;
2005 }
2006 else if (STRNCMP("expr ", p, 5) == 0)
2007 {
2008 // :breakadd expr <expression>
2009 xp->xp_context = EXPAND_EXPRESSION;
2010 xp->xp_pattern = skipwhite(p + 5);
2011 }
2012
2013 return NULL;
2014}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002015
2016 static char_u *
2017set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2018{
2019 char_u *p;
2020
2021 xp->xp_context = EXPAND_NOTHING;
2022 xp->xp_pattern = NULL;
2023
2024 p = skipwhite(arg);
2025 if (VIM_ISDIGIT(*p))
2026 return NULL;
2027
2028 xp->xp_context = EXPAND_SCRIPTNAMES;
2029 xp->xp_pattern = p;
2030
2031 return NULL;
2032}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002033#endif
2034
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002035/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002036 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2037 * The argument to the command is 'arg' and the argument flags is 'argt'.
2038 * For user-defined commands and for environment variables, 'compl' has the
2039 * completion type.
2040 * Returns a pointer to the next command. Returns NULL if there is no next
2041 * command.
2042 */
2043 static char_u *
2044set_context_by_cmdname(
2045 char_u *cmd,
2046 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002047 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002048 char_u *arg,
2049 long argt,
2050 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002051 int forceit)
2052{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002053 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002054 {
2055 case CMD_find:
2056 case CMD_sfind:
2057 case CMD_tabfind:
2058 if (xp->xp_context == EXPAND_FILES)
2059 xp->xp_context = EXPAND_FILES_IN_PATH;
2060 break;
2061 case CMD_cd:
2062 case CMD_chdir:
2063 case CMD_tcd:
2064 case CMD_tchdir:
2065 case CMD_lcd:
2066 case CMD_lchdir:
2067 if (xp->xp_context == EXPAND_FILES)
2068 xp->xp_context = EXPAND_DIRECTORIES;
2069 break;
2070 case CMD_help:
2071 xp->xp_context = EXPAND_HELP;
2072 xp->xp_pattern = arg;
2073 break;
2074
2075 // Command modifiers: return the argument.
2076 // Also for commands with an argument that is a command.
2077 case CMD_aboveleft:
2078 case CMD_argdo:
2079 case CMD_belowright:
2080 case CMD_botright:
2081 case CMD_browse:
2082 case CMD_bufdo:
2083 case CMD_cdo:
2084 case CMD_cfdo:
2085 case CMD_confirm:
2086 case CMD_debug:
2087 case CMD_folddoclosed:
2088 case CMD_folddoopen:
2089 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002090 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002091 case CMD_keepalt:
2092 case CMD_keepjumps:
2093 case CMD_keepmarks:
2094 case CMD_keeppatterns:
2095 case CMD_ldo:
2096 case CMD_leftabove:
2097 case CMD_lfdo:
2098 case CMD_lockmarks:
2099 case CMD_noautocmd:
2100 case CMD_noswapfile:
2101 case CMD_rightbelow:
2102 case CMD_sandbox:
2103 case CMD_silent:
2104 case CMD_tab:
2105 case CMD_tabdo:
2106 case CMD_topleft:
2107 case CMD_verbose:
2108 case CMD_vertical:
2109 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002110 case CMD_vim9cmd:
2111 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002112 return arg;
2113
2114 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002115 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002116
2117#ifdef FEAT_SEARCH_EXTRA
2118 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002119 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002120#endif
2121
2122 // All completion for the +cmdline_compl feature goes here.
2123
2124 case CMD_command:
2125 return set_context_in_user_cmd(xp, arg);
2126
2127 case CMD_delcommand:
2128 xp->xp_context = EXPAND_USER_COMMANDS;
2129 xp->xp_pattern = arg;
2130 break;
2131
2132 case CMD_global:
2133 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002134 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002135 case CMD_and:
2136 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002137 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002138 case CMD_isearch:
2139 case CMD_dsearch:
2140 case CMD_ilist:
2141 case CMD_dlist:
2142 case CMD_ijump:
2143 case CMD_psearch:
2144 case CMD_djump:
2145 case CMD_isplit:
2146 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002147 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002148 case CMD_autocmd:
2149 return set_context_in_autocmd(xp, arg, FALSE);
2150 case CMD_doautocmd:
2151 case CMD_doautoall:
2152 return set_context_in_autocmd(xp, arg, TRUE);
2153 case CMD_set:
2154 set_context_in_set_cmd(xp, arg, 0);
2155 break;
2156 case CMD_setglobal:
2157 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2158 break;
2159 case CMD_setlocal:
2160 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2161 break;
2162 case CMD_tag:
2163 case CMD_stag:
2164 case CMD_ptag:
2165 case CMD_ltag:
2166 case CMD_tselect:
2167 case CMD_stselect:
2168 case CMD_ptselect:
2169 case CMD_tjump:
2170 case CMD_stjump:
2171 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002172 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002173 xp->xp_context = EXPAND_TAGS_LISTFILES;
2174 else
2175 xp->xp_context = EXPAND_TAGS;
2176 xp->xp_pattern = arg;
2177 break;
2178 case CMD_augroup:
2179 xp->xp_context = EXPAND_AUGROUP;
2180 xp->xp_pattern = arg;
2181 break;
2182#ifdef FEAT_SYN_HL
2183 case CMD_syntax:
2184 set_context_in_syntax_cmd(xp, arg);
2185 break;
2186#endif
2187#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002188 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002189 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002190 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002191 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002192 case CMD_if:
2193 case CMD_elseif:
2194 case CMD_while:
2195 case CMD_for:
2196 case CMD_echo:
2197 case CMD_echon:
2198 case CMD_execute:
2199 case CMD_echomsg:
2200 case CMD_echoerr:
2201 case CMD_call:
2202 case CMD_return:
2203 case CMD_cexpr:
2204 case CMD_caddexpr:
2205 case CMD_cgetexpr:
2206 case CMD_lexpr:
2207 case CMD_laddexpr:
2208 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002209 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002210 break;
2211
2212 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002213 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002214 case CMD_function:
2215 case CMD_delfunction:
2216 xp->xp_context = EXPAND_USER_FUNC;
2217 xp->xp_pattern = arg;
2218 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002219 case CMD_disassemble:
2220 set_context_in_disassemble_cmd(xp, arg);
2221 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002222
2223 case CMD_echohl:
2224 set_context_in_echohl_cmd(xp, arg);
2225 break;
2226#endif
2227 case CMD_highlight:
2228 set_context_in_highlight_cmd(xp, arg);
2229 break;
2230#ifdef FEAT_CSCOPE
2231 case CMD_cscope:
2232 case CMD_lcscope:
2233 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002234 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002235 break;
2236#endif
2237#ifdef FEAT_SIGNS
2238 case CMD_sign:
2239 set_context_in_sign_cmd(xp, arg);
2240 break;
2241#endif
2242 case CMD_bdelete:
2243 case CMD_bwipeout:
2244 case CMD_bunload:
2245 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2246 arg = xp->xp_pattern + 1;
2247 // FALLTHROUGH
2248 case CMD_buffer:
2249 case CMD_sbuffer:
2250 case CMD_checktime:
2251 xp->xp_context = EXPAND_BUFFERS;
2252 xp->xp_pattern = arg;
2253 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002254#ifdef FEAT_DIFF
2255 case CMD_diffget:
2256 case CMD_diffput:
2257 // If current buffer is in diff mode, complete buffer names
2258 // which are in diff mode, and different than current buffer.
2259 xp->xp_context = EXPAND_DIFF_BUFFERS;
2260 xp->xp_pattern = arg;
2261 break;
2262#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002263 case CMD_USER:
2264 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002265 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2266 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002267
2268 case CMD_map: case CMD_noremap:
2269 case CMD_nmap: case CMD_nnoremap:
2270 case CMD_vmap: case CMD_vnoremap:
2271 case CMD_omap: case CMD_onoremap:
2272 case CMD_imap: case CMD_inoremap:
2273 case CMD_cmap: case CMD_cnoremap:
2274 case CMD_lmap: case CMD_lnoremap:
2275 case CMD_smap: case CMD_snoremap:
2276 case CMD_tmap: case CMD_tnoremap:
2277 case CMD_xmap: case CMD_xnoremap:
2278 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002279 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002280 case CMD_unmap:
2281 case CMD_nunmap:
2282 case CMD_vunmap:
2283 case CMD_ounmap:
2284 case CMD_iunmap:
2285 case CMD_cunmap:
2286 case CMD_lunmap:
2287 case CMD_sunmap:
2288 case CMD_tunmap:
2289 case CMD_xunmap:
2290 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002291 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002292 case CMD_mapclear:
2293 case CMD_nmapclear:
2294 case CMD_vmapclear:
2295 case CMD_omapclear:
2296 case CMD_imapclear:
2297 case CMD_cmapclear:
2298 case CMD_lmapclear:
2299 case CMD_smapclear:
2300 case CMD_tmapclear:
2301 case CMD_xmapclear:
2302 xp->xp_context = EXPAND_MAPCLEAR;
2303 xp->xp_pattern = arg;
2304 break;
2305
2306 case CMD_abbreviate: case CMD_noreabbrev:
2307 case CMD_cabbrev: case CMD_cnoreabbrev:
2308 case CMD_iabbrev: case CMD_inoreabbrev:
2309 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002310 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002311 case CMD_unabbreviate:
2312 case CMD_cunabbrev:
2313 case CMD_iunabbrev:
2314 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002315 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002316#ifdef FEAT_MENU
2317 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2318 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2319 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2320 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2321 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2322 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2323 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2324 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2325 case CMD_tmenu: case CMD_tunmenu:
2326 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2327 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2328#endif
2329
2330 case CMD_colorscheme:
2331 xp->xp_context = EXPAND_COLORS;
2332 xp->xp_pattern = arg;
2333 break;
2334
2335 case CMD_compiler:
2336 xp->xp_context = EXPAND_COMPILER;
2337 xp->xp_pattern = arg;
2338 break;
2339
2340 case CMD_ownsyntax:
2341 xp->xp_context = EXPAND_OWNSYNTAX;
2342 xp->xp_pattern = arg;
2343 break;
2344
2345 case CMD_setfiletype:
2346 xp->xp_context = EXPAND_FILETYPE;
2347 xp->xp_pattern = arg;
2348 break;
2349
2350 case CMD_packadd:
2351 xp->xp_context = EXPAND_PACKADD;
2352 xp->xp_pattern = arg;
2353 break;
2354
zeertzjqb0d45ec2023-01-25 15:04:22 +00002355 case CMD_runtime:
2356 set_context_in_runtime_cmd(xp, arg);
2357 break;
2358
Bram Moolenaard0190392019-08-23 21:17:35 +02002359#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2360 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002361 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002362#endif
2363#if defined(FEAT_PROFILE)
2364 case CMD_profile:
2365 set_context_in_profile_cmd(xp, arg);
2366 break;
2367#endif
2368 case CMD_behave:
2369 xp->xp_context = EXPAND_BEHAVE;
2370 xp->xp_pattern = arg;
2371 break;
2372
2373 case CMD_messages:
2374 xp->xp_context = EXPAND_MESSAGES;
2375 xp->xp_pattern = arg;
2376 break;
2377
2378 case CMD_history:
2379 xp->xp_context = EXPAND_HISTORY;
2380 xp->xp_pattern = arg;
2381 break;
2382#if defined(FEAT_PROFILE)
2383 case CMD_syntime:
2384 xp->xp_context = EXPAND_SYNTIME;
2385 xp->xp_pattern = arg;
2386 break;
2387#endif
2388
2389 case CMD_argdelete:
2390 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2391 arg = xp->xp_pattern + 1;
2392 xp->xp_context = EXPAND_ARGLIST;
2393 xp->xp_pattern = arg;
2394 break;
2395
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002396#ifdef FEAT_EVAL
2397 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002398 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002399 case CMD_breakdel:
2400 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002401
2402 case CMD_scriptnames:
2403 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002404#endif
2405
Bram Moolenaard0190392019-08-23 21:17:35 +02002406 default:
2407 break;
2408 }
2409 return NULL;
2410}
2411
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002412/*
2413 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2414 * we don't need/want deleted. Maybe this could be done better if we didn't
2415 * repeat all this stuff. The only problem is that they may not stay
2416 * perfectly compatible with each other, but then the command line syntax
2417 * probably won't change that much -- webb.
2418 */
2419 static char_u *
2420set_one_cmd_context(
2421 expand_T *xp,
2422 char_u *buff) // buffer for command string
2423{
2424 char_u *p;
2425 char_u *cmd, *arg;
2426 int len = 0;
2427 exarg_T ea;
2428 int compl = EXPAND_NOTHING;
2429 int forceit = FALSE;
2430 int usefilter = FALSE; // filter instead of file name
2431
2432 ExpandInit(xp);
2433 xp->xp_pattern = buff;
2434 xp->xp_line = buff;
2435 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2436 ea.argt = 0;
2437
2438 // 1. skip comment lines and leading space, colons or bars
2439 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2440 ;
2441 xp->xp_pattern = cmd;
2442
2443 if (*cmd == NUL)
2444 return NULL;
2445 if (*cmd == '"') // ignore comment lines
2446 {
2447 xp->xp_context = EXPAND_NOTHING;
2448 return NULL;
2449 }
2450
2451 // 3. Skip over the range to find the command.
2452 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2453 xp->xp_pattern = cmd;
2454 if (*cmd == NUL)
2455 return NULL;
2456 if (*cmd == '"')
2457 {
2458 xp->xp_context = EXPAND_NOTHING;
2459 return NULL;
2460 }
2461
2462 if (*cmd == '|' || *cmd == '\n')
2463 return cmd + 1; // There's another command
2464
2465 // Get the command index.
2466 p = set_cmd_index(cmd, &ea, xp, &compl);
2467 if (p == NULL)
2468 return NULL;
2469
2470 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2471
2472 if (*p == '!') // forced commands
2473 {
2474 forceit = TRUE;
2475 ++p;
2476 }
2477
2478 // 6. parse arguments
2479 if (!IS_USER_CMDIDX(ea.cmdidx))
2480 ea.argt = excmd_get_argt(ea.cmdidx);
2481
2482 arg = skipwhite(p);
2483
2484 // Skip over ++argopt argument
2485 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2486 {
2487 p = arg;
2488 while (*p && !vim_isspace(*p))
2489 MB_PTR_ADV(p);
2490 arg = skipwhite(p);
2491 }
2492
2493 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2494 {
2495 if (*arg == '>') // append
2496 {
2497 if (*++arg == '>')
2498 ++arg;
2499 arg = skipwhite(arg);
2500 }
2501 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2502 {
2503 ++arg;
2504 usefilter = TRUE;
2505 }
2506 }
2507
2508 if (ea.cmdidx == CMD_read)
2509 {
2510 usefilter = forceit; // :r! filter if forced
2511 if (*arg == '!') // :r !filter
2512 {
2513 ++arg;
2514 usefilter = TRUE;
2515 }
2516 }
2517
2518 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2519 {
2520 while (*arg == *cmd) // allow any number of '>' or '<'
2521 ++arg;
2522 arg = skipwhite(arg);
2523 }
2524
2525 // Does command allow "+command"?
2526 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2527 {
2528 // Check if we're in the +command
2529 p = arg + 1;
2530 arg = skip_cmd_arg(arg, FALSE);
2531
2532 // Still touching the command after '+'?
2533 if (*arg == NUL)
2534 return p;
2535
2536 // Skip space(s) after +command to get to the real argument
2537 arg = skipwhite(arg);
2538 }
2539
2540
2541 // Check for '|' to separate commands and '"' to start comments.
2542 // Don't do this for ":read !cmd" and ":write !cmd".
2543 if ((ea.argt & EX_TRLBAR) && !usefilter)
2544 {
2545 p = arg;
2546 // ":redir @" is not the start of a comment
2547 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2548 p += 2;
2549 while (*p)
2550 {
2551 if (*p == Ctrl_V)
2552 {
2553 if (p[1] != NUL)
2554 ++p;
2555 }
2556 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2557 || *p == '|' || *p == '\n')
2558 {
2559 if (*(p - 1) != '\\')
2560 {
2561 if (*p == '|' || *p == '\n')
2562 return p + 1;
2563 return NULL; // It's a comment
2564 }
2565 }
2566 MB_PTR_ADV(p);
2567 }
2568 }
2569
2570 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2571 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2572 // no arguments allowed but there is something
2573 return NULL;
2574
2575 // Find start of last argument (argument just before cursor):
2576 p = buff;
2577 xp->xp_pattern = p;
2578 len = (int)STRLEN(buff);
2579 while (*p && p < buff + len)
2580 {
2581 if (*p == ' ' || *p == TAB)
2582 {
2583 // argument starts after a space
2584 xp->xp_pattern = ++p;
2585 }
2586 else
2587 {
2588 if (*p == '\\' && *(p + 1) != NUL)
2589 ++p; // skip over escaped character
2590 MB_PTR_ADV(p);
2591 }
2592 }
2593
2594 if (ea.argt & EX_XFILE)
2595 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2596
2597 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002598 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002599 forceit);
2600}
2601
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002602/*
2603 * Set the completion context in 'xp' for command 'str'
2604 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002605 void
2606set_cmd_context(
2607 expand_T *xp,
2608 char_u *str, // start of command line
2609 int len, // length of command line (excl. NUL)
2610 int col, // position of cursor
2611 int use_ccline UNUSED) // use ccline for info
2612{
2613#ifdef FEAT_EVAL
2614 cmdline_info_T *ccline = get_cmdline_info();
2615#endif
2616 int old_char = NUL;
2617 char_u *nextcomm;
2618
2619 // Avoid a UMR warning from Purify, only save the character if it has been
2620 // written before.
2621 if (col < len)
2622 old_char = str[col];
2623 str[col] = NUL;
2624 nextcomm = str;
2625
2626#ifdef FEAT_EVAL
2627 if (use_ccline && ccline->cmdfirstc == '=')
2628 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002629 // pass CMD_SIZE because there is no real command
2630 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002631 }
2632 else if (use_ccline && ccline->input_fn)
2633 {
2634 xp->xp_context = ccline->xp_context;
2635 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002636 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002637 }
2638 else
2639#endif
2640 while (nextcomm != NULL)
2641 nextcomm = set_one_cmd_context(xp, nextcomm);
2642
2643 // Store the string here so that call_user_expand_func() can get to them
2644 // easily.
2645 xp->xp_line = str;
2646 xp->xp_col = col;
2647
2648 str[col] = old_char;
2649}
2650
2651/*
2652 * Expand the command line "str" from context "xp".
2653 * "xp" must have been set by set_cmd_context().
2654 * xp->xp_pattern points into "str", to where the text that is to be expanded
2655 * starts.
2656 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2657 * cursor.
2658 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2659 * key that triggered expansion literally.
2660 * Returns EXPAND_OK otherwise.
2661 */
2662 int
2663expand_cmdline(
2664 expand_T *xp,
2665 char_u *str, // start of command line
2666 int col, // position of cursor
2667 int *matchcount, // return: nr of matches
2668 char_u ***matches) // return: array of pointers to matches
2669{
2670 char_u *file_str = NULL;
2671 int options = WILD_ADD_SLASH|WILD_SILENT;
2672
2673 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2674 {
2675 beep_flush();
2676 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2677 }
2678 if (xp->xp_context == EXPAND_NOTHING)
2679 {
2680 // Caller can use the character as a normal char instead
2681 return EXPAND_NOTHING;
2682 }
2683
2684 // add star to file name, or convert to regexp if not exp. files.
2685 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002686 if (cmdline_fuzzy_completion_supported(xp))
2687 // If fuzzy matching, don't modify the search string
2688 file_str = vim_strsave(xp->xp_pattern);
2689 else
2690 {
2691 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2692 if (file_str == NULL)
2693 return EXPAND_UNSUCCESSFUL;
2694 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002695
2696 if (p_wic)
2697 options += WILD_ICASE;
2698
2699 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002700 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002701 {
2702 *matchcount = 0;
2703 *matches = NULL;
2704 }
2705 vim_free(file_str);
2706
2707 return EXPAND_OK;
2708}
2709
Bram Moolenaar66b51422019-08-18 21:44:12 +02002710/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002711 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002712 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002713 */
2714 static int
2715expand_files_and_dirs(
2716 expand_T *xp,
2717 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002718 char_u ***matches,
2719 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002720 int flags,
2721 int options)
2722{
2723 int free_pat = FALSE;
2724 int i;
2725 int ret;
2726
2727 // for ":set path=" and ":set tags=" halve backslashes for escaped
2728 // space
2729 if (xp->xp_backslash != XP_BS_NONE)
2730 {
2731 free_pat = TRUE;
2732 pat = vim_strsave(pat);
2733 for (i = 0; pat[i]; ++i)
2734 if (pat[i] == '\\')
2735 {
2736 if (xp->xp_backslash == XP_BS_THREE
2737 && pat[i + 1] == '\\'
2738 && pat[i + 2] == '\\'
2739 && pat[i + 3] == ' ')
2740 STRMOVE(pat + i, pat + i + 3);
2741 if (xp->xp_backslash == XP_BS_ONE
2742 && pat[i + 1] == ' ')
2743 STRMOVE(pat + i, pat + i + 1);
2744 }
2745 }
2746
2747 if (xp->xp_context == EXPAND_FILES)
2748 flags |= EW_FILE;
2749 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2750 flags |= (EW_FILE | EW_PATH);
2751 else
2752 flags = (flags | EW_DIR) & ~EW_FILE;
2753 if (options & WILD_ICASE)
2754 flags |= EW_ICASE;
2755
2756 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002757 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002758 if (free_pat)
2759 vim_free(pat);
2760#ifdef BACKSLASH_IN_FILENAME
2761 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2762 {
2763 int j;
2764
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002765 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002766 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002767 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002768
2769 while (*ptr != NUL)
2770 {
2771 if (p_csl[0] == 's' && *ptr == '\\')
2772 *ptr = '/';
2773 else if (p_csl[0] == 'b' && *ptr == '/')
2774 *ptr = '\\';
2775 ptr += (*mb_ptr2len)(ptr);
2776 }
2777 }
2778 }
2779#endif
2780 return ret;
2781}
2782
2783/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002784 * Function given to ExpandGeneric() to obtain the possible arguments of the
2785 * ":behave {mswin,xterm}" command.
2786 */
2787 static char_u *
2788get_behave_arg(expand_T *xp UNUSED, int idx)
2789{
2790 if (idx == 0)
2791 return (char_u *)"mswin";
2792 if (idx == 1)
2793 return (char_u *)"xterm";
2794 return NULL;
2795}
2796
K.Takata161b6ac2022-11-14 15:31:07 +00002797#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002798/*
2799 * Function given to ExpandGeneric() to obtain the possible arguments of the
2800 * ":breakadd {expr, file, func, here}" command.
2801 * ":breakdel {func, file, here}" command.
2802 */
2803 static char_u *
2804get_breakadd_arg(expand_T *xp UNUSED, int idx)
2805{
2806 char *opts[] = {"expr", "file", "func", "here"};
2807
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002808 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002809 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002810 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002811 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2812 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002813 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2814 {
2815 // breakdel {func, file, here}
2816 if (idx <= 2)
2817 return (char_u *)opts[idx + 1];
2818 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002819 else
2820 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002821 // profdel {func, file}
2822 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002823 return (char_u *)opts[idx + 1];
2824 }
2825 }
2826 return NULL;
2827}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002828
2829/*
2830 * Function given to ExpandGeneric() to obtain the possible arguments for the
2831 * ":scriptnames" command.
2832 */
2833 static char_u *
2834get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2835{
2836 scriptitem_T *si;
2837
2838 if (!SCRIPT_ID_VALID(idx + 1))
2839 return NULL;
2840
2841 si = SCRIPT_ITEM(idx + 1);
2842 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2843 return NameBuff;
2844}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002845#endif
2846
Bram Moolenaard0190392019-08-23 21:17:35 +02002847/*
2848 * Function given to ExpandGeneric() to obtain the possible arguments of the
2849 * ":messages {clear}" command.
2850 */
2851 static char_u *
2852get_messages_arg(expand_T *xp UNUSED, int idx)
2853{
2854 if (idx == 0)
2855 return (char_u *)"clear";
2856 return NULL;
2857}
2858
2859 static char_u *
2860get_mapclear_arg(expand_T *xp UNUSED, int idx)
2861{
2862 if (idx == 0)
2863 return (char_u *)"<buffer>";
2864 return NULL;
2865}
2866
2867/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002868 * Do the expansion based on xp->xp_context and 'rmp'.
2869 */
2870 static int
2871ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002872 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002873 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002874 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002875 char_u ***matches,
2876 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002877{
2878 static struct expgen
2879 {
2880 int context;
2881 char_u *((*func)(expand_T *, int));
2882 int ic;
2883 int escaped;
2884 } tab[] =
2885 {
2886 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2887 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2888 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2889 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2890 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2891 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2892 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2893 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2894 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2895 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002896#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002897 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2898 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2899 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2900 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2901 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002902#endif
2903#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002904 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2905 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002906#endif
2907#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002908 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002909#endif
2910#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002911 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002912#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002913 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2914 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2915 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002916#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002917 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002918#endif
2919#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002920 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002921#endif
2922#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002923 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002924#endif
2925#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002926 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2927 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002928#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002929 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2930 {EXPAND_USER, get_users, TRUE, FALSE},
2931 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002932#ifdef FEAT_EVAL
2933 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002934 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002935#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002936 };
2937 int i;
2938 int ret = FAIL;
2939
2940 // Find a context in the table and call the ExpandGeneric() with the
2941 // right function to do the expansion.
2942 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2943 {
2944 if (xp->xp_context == tab[i].context)
2945 {
2946 if (tab[i].ic)
2947 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002948 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2949 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002950 break;
2951 }
2952 }
2953
2954 return ret;
2955}
2956
2957/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002958 * Map wild expand options to flags for expand_wildcards()
2959 */
2960 static int
2961map_wildopts_to_ewflags(int options)
2962{
2963 int flags;
2964
2965 flags = EW_DIR; // include directories
2966 if (options & WILD_LIST_NOTFOUND)
2967 flags |= EW_NOTFOUND;
2968 if (options & WILD_ADD_SLASH)
2969 flags |= EW_ADDSLASH;
2970 if (options & WILD_KEEP_ALL)
2971 flags |= EW_KEEPALL;
2972 if (options & WILD_SILENT)
2973 flags |= EW_SILENT;
2974 if (options & WILD_NOERROR)
2975 flags |= EW_NOERROR;
2976 if (options & WILD_ALLLINKS)
2977 flags |= EW_ALLLINKS;
2978
2979 return flags;
2980}
2981
2982/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002983 * Do the expansion based on xp->xp_context and "pat".
2984 */
2985 static int
2986ExpandFromContext(
2987 expand_T *xp,
2988 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002989 char_u ***matches,
2990 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002991 int options) // WILD_ flags
2992{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002993 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002994 int ret;
2995 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002996 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002997 int fuzzy = cmdline_fuzzy_complete(pat)
2998 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002999
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003000 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003001
3002 if (xp->xp_context == EXPAND_FILES
3003 || xp->xp_context == EXPAND_DIRECTORIES
3004 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003005 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3006 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003007
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003008 *matches = (char_u **)"";
3009 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003010 if (xp->xp_context == EXPAND_HELP)
3011 {
3012 // With an empty argument we would get all the help tags, which is
3013 // very slow. Get matches for "help" instead.
3014 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003015 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003016 {
3017#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003018 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003019#endif
3020 return OK;
3021 }
3022 return FAIL;
3023 }
3024
Bram Moolenaar66b51422019-08-18 21:44:12 +02003025 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003026 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003027 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003028 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003029 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003030 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003031#ifdef FEAT_DIFF
3032 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003033 return ExpandBufnames(pat, numMatches, matches,
3034 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003035#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003036 if (xp->xp_context == EXPAND_TAGS
3037 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003038 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3039 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003040 if (xp->xp_context == EXPAND_COLORS)
3041 {
3042 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003043 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003044 directories);
3045 }
3046 if (xp->xp_context == EXPAND_COMPILER)
3047 {
3048 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003049 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003050 }
3051 if (xp->xp_context == EXPAND_OWNSYNTAX)
3052 {
3053 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003054 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003055 }
3056 if (xp->xp_context == EXPAND_FILETYPE)
3057 {
3058 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003059 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003060 }
K.Takata161b6ac2022-11-14 15:31:07 +00003061#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003062 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003063 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003064#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003065 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003066 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003067 if (xp->xp_context == EXPAND_RUNTIME)
3068 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003069
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003070 // When expanding a function name starting with s:, match the <SNR>nr_
3071 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003072 if ((xp->xp_context == EXPAND_USER_FUNC
3073 || xp->xp_context == EXPAND_DISASSEMBLE)
3074 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003075 {
3076 int len = (int)STRLEN(pat) + 20;
3077
3078 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003079 if (tofree == NULL)
3080 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003081 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003082 pat = tofree;
3083 }
3084
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003085 if (!fuzzy)
3086 {
3087 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3088 if (regmatch.regprog == NULL)
3089 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003090
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003091 // set ignore-case according to p_ic, p_scs and pat
3092 regmatch.rm_ic = ignorecase(pat);
3093 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003094
3095 if (xp->xp_context == EXPAND_SETTINGS
3096 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003097 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003098 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003099 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
K.Takata161b6ac2022-11-14 15:31:07 +00003100#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003101 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003102 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003103#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003105 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003106
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003107 if (!fuzzy)
3108 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003109 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110
3111 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112}
3113
Bram Moolenaar66b51422019-08-18 21:44:12 +02003114/*
3115 * Expand a list of names.
3116 *
3117 * Generic function for command line completion. It calls a function to
3118 * obtain strings, one by one. The strings are matched against a regexp
3119 * program. Matching strings are copied into an array, which is returned.
3120 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003121 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3122 * is used.
3123 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003124 * Returns OK when no problems encountered, FAIL for error (out of memory).
3125 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01003126 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003127ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003128 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003129 expand_T *xp,
3130 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003131 char_u ***matches,
3132 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133 char_u *((*func)(expand_T *, int)),
3134 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003135 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003136{
3137 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003138 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003140 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003141 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003142 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003143 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003144 int sort_matches = FALSE;
3145 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003146
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003147 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003148 *matches = NULL;
3149 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003150
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003151 if (!fuzzy)
3152 ga_init2(&ga, sizeof(char *), 30);
3153 else
3154 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3155
3156 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003157 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003158 str = (*func)(xp, i);
3159 if (str == NULL) // end of list
3160 break;
3161 if (*str == NUL) // skip empty strings
3162 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003163
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003164 if (xp->xp_pattern[0] != NUL)
3165 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003166 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003167 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003168 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003169 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003170 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003171 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003172 }
3173 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003174 else
3175 match = TRUE;
3176
3177 if (!match)
3178 continue;
3179
3180 if (escaped)
3181 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3182 else
3183 str = vim_strsave(str);
3184 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003185 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003186 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003187 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003188 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003189 return FAIL;
3190 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003191 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003192 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003194
3195 if (ga_grow(&ga, 1) == FAIL)
3196 {
3197 vim_free(str);
3198 break;
3199 }
3200
3201 if (fuzzy)
3202 {
3203 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3204 fuzmatch->idx = ga.ga_len;
3205 fuzmatch->str = str;
3206 fuzmatch->score = score;
3207 }
3208 else
3209 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3210
K.Takata161b6ac2022-11-14 15:31:07 +00003211#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003212 if (func == get_menu_names)
3213 {
3214 // test for separator added by get_menu_names()
3215 str += STRLEN(str) - 1;
3216 if (*str == '\001')
3217 *str = '.';
3218 }
K.Takata161b6ac2022-11-14 15:31:07 +00003219#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003220
3221 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003222 }
3223
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003224 if (ga.ga_len == 0)
3225 return OK;
3226
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003227 // sort the matches when using regular expression matching and sorting
3228 // applies to the completion context. Menus and scriptnames should be kept
3229 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003230 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003231 && xp->xp_context != EXPAND_MENUS
3232 && xp->xp_context != EXPAND_SCRIPTNAMES)
3233 sort_matches = TRUE;
3234
3235 // <SNR> functions should be sorted to the end.
3236 if (xp->xp_context == EXPAND_EXPRESSION
3237 || xp->xp_context == EXPAND_FUNCTIONS
3238 || xp->xp_context == EXPAND_USER_FUNC
3239 || xp->xp_context == EXPAND_DISASSEMBLE)
3240 funcsort = TRUE;
3241
3242 // Sort the matches.
3243 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003244 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003245 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003246 // <SNR> functions should be sorted to the end.
3247 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3248 sort_func_compare);
3249 else
3250 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3251 }
3252
3253 if (!fuzzy)
3254 {
3255 *matches = ga.ga_data;
3256 *numMatches = ga.ga_len;
3257 }
3258 else
3259 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003260 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3261 funcsort) == FAIL)
3262 return FAIL;
3263 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264 }
3265
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003266#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003267 // Reset the variables used for special highlight names expansion, so that
3268 // they don't show up when getting normal highlight names by ID.
3269 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003270#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003271
Bram Moolenaar66b51422019-08-18 21:44:12 +02003272 return OK;
3273}
3274
3275/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003276 * Expand shell command matches in one directory of $PATH.
3277 */
3278 static void
3279expand_shellcmd_onedir(
3280 char_u *buf,
3281 char_u *s,
3282 size_t l,
3283 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003284 char_u ***matches,
3285 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003286 int flags,
3287 hashtab_T *ht,
3288 garray_T *gap)
3289{
3290 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003291 hash_T hash;
3292 hashitem_T *hi;
3293
3294 vim_strncpy(buf, s, l);
3295 add_pathsep(buf);
3296 l = STRLEN(buf);
3297 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3298
3299 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003300 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003301 if (ret != OK)
3302 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003303
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003304 if (ga_grow(gap, *numMatches) == FAIL)
3305 {
3306 FreeWild(*numMatches, *matches);
3307 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003308 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003309
3310 for (int i = 0; i < *numMatches; ++i)
3311 {
3312 char_u *name = (*matches)[i];
3313
3314 if (STRLEN(name) > l)
3315 {
3316 // Check if this name was already found.
3317 hash = hash_hash(name + l);
3318 hi = hash_lookup(ht, name + l, hash);
3319 if (HASHITEM_EMPTY(hi))
3320 {
3321 // Remove the path that was prepended.
3322 STRMOVE(name, name + l);
3323 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3324 hash_add_item(ht, hi, name, hash);
3325 name = NULL;
3326 }
3327 }
3328 vim_free(name);
3329 }
3330 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003331}
3332
3333/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003334 * Complete a shell command.
3335 * Returns FAIL or OK;
3336 */
3337 static int
3338expand_shellcmd(
3339 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003340 char_u ***matches, // return: array with matches
3341 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003342 int flagsarg) // EW_ flags
3343{
3344 char_u *pat;
3345 int i;
3346 char_u *path = NULL;
3347 int mustfree = FALSE;
3348 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003349 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 size_t l;
3351 char_u *s, *e;
3352 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353 int did_curdir = FALSE;
3354 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003355
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003356 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003357 if (buf == NULL)
3358 return FAIL;
3359
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003360 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003362 if (pat == NULL)
3363 {
3364 vim_free(buf);
3365 return FAIL;
3366 }
3367
Bram Moolenaar66b51422019-08-18 21:44:12 +02003368 for (i = 0; pat[i]; ++i)
3369 if (pat[i] == '\\' && pat[i + 1] == ' ')
3370 STRMOVE(pat + i, pat + i + 1);
3371
3372 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3373
3374 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3375 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3376 path = (char_u *)".";
3377 else
3378 {
3379 // For an absolute name we don't use $PATH.
3380 if (!mch_isFullName(pat))
3381 path = vim_getenv((char_u *)"PATH", &mustfree);
3382 if (path == NULL)
3383 path = (char_u *)"";
3384 }
3385
3386 // Go over all directories in $PATH. Expand matches in that directory and
3387 // collect them in "ga". When "." is not in $PATH also expand for the
3388 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003389 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003390 hash_init(&found_ht);
3391 for (s = path; ; s = e)
3392 {
K.Takata161b6ac2022-11-14 15:31:07 +00003393#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003394 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003395#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003396 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003397#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003398 if (e == NULL)
3399 e = s + STRLEN(s);
3400
3401 if (*s == NUL)
3402 {
3403 if (did_curdir)
3404 break;
3405 // Find directories in the current directory, path is empty.
3406 did_curdir = TRUE;
3407 flags |= EW_DIR;
3408 }
3409 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3410 {
3411 did_curdir = TRUE;
3412 flags |= EW_DIR;
3413 }
3414 else
3415 // Do not match directories inside a $PATH item.
3416 flags &= ~EW_DIR;
3417
3418 l = e - s;
3419 if (l > MAXPATHL - 5)
3420 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003421
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003422 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003423 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003424
Bram Moolenaar66b51422019-08-18 21:44:12 +02003425 if (*e != NUL)
3426 ++e;
3427 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003428 *matches = ga.ga_data;
3429 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003430
3431 vim_free(buf);
3432 vim_free(pat);
3433 if (mustfree)
3434 vim_free(path);
3435 hash_clear(&found_ht);
3436 return OK;
3437}
3438
K.Takata161b6ac2022-11-14 15:31:07 +00003439#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003440/*
3441 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003442 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003443 */
3444 static void *
3445call_user_expand_func(
3446 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003447 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003448{
3449 cmdline_info_T *ccline = get_cmdline_info();
3450 int keep = 0;
3451 typval_T args[4];
3452 sctx_T save_current_sctx = current_sctx;
3453 char_u *pat = NULL;
3454 void *ret;
3455
3456 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3457 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003458
3459 if (ccline->cmdbuff != NULL)
3460 {
3461 keep = ccline->cmdbuff[ccline->cmdlen];
3462 ccline->cmdbuff[ccline->cmdlen] = 0;
3463 }
3464
3465 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3466
3467 args[0].v_type = VAR_STRING;
3468 args[0].vval.v_string = pat;
3469 args[1].v_type = VAR_STRING;
3470 args[1].vval.v_string = xp->xp_line;
3471 args[2].v_type = VAR_NUMBER;
3472 args[2].vval.v_number = xp->xp_col;
3473 args[3].v_type = VAR_UNKNOWN;
3474
3475 current_sctx = xp->xp_script_ctx;
3476
3477 ret = user_expand_func(xp->xp_arg, 3, args);
3478
3479 current_sctx = save_current_sctx;
3480 if (ccline->cmdbuff != NULL)
3481 ccline->cmdbuff[ccline->cmdlen] = keep;
3482
3483 vim_free(pat);
3484 return ret;
3485}
3486
3487/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003488 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3489 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003490 */
3491 static int
3492ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003493 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003494 expand_T *xp,
3495 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003496 char_u ***matches,
3497 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003498{
3499 char_u *retstr;
3500 char_u *s;
3501 char_u *e;
3502 int keep;
3503 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003504 int fuzzy;
3505 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003506 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003507
3508 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003509 *matches = NULL;
3510 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003511
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003512 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003513 if (retstr == NULL)
3514 return FAIL;
3515
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003516 if (!fuzzy)
3517 ga_init2(&ga, sizeof(char *), 3);
3518 else
3519 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3520
Bram Moolenaar66b51422019-08-18 21:44:12 +02003521 for (s = retstr; *s != NUL; s = e)
3522 {
3523 e = vim_strchr(s, '\n');
3524 if (e == NULL)
3525 e = s + STRLEN(s);
3526 keep = *e;
3527 *e = NUL;
3528
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003529 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003530 {
3531 if (!fuzzy)
3532 match = vim_regexec(regmatch, s, (colnr_T)0);
3533 else
3534 {
3535 score = fuzzy_match_str(s, pat);
3536 match = (score != 0);
3537 }
3538 }
3539 else
3540 match = TRUE; // match everything
3541
Bram Moolenaar66b51422019-08-18 21:44:12 +02003542 *e = keep;
3543
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003544 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003545 {
3546 if (ga_grow(&ga, 1) == FAIL)
3547 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003548 if (!fuzzy)
3549 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3550 else
3551 {
3552 fuzmatch_str_T *fuzmatch =
3553 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003554 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003555 fuzmatch->str = vim_strnsave(s, e - s);
3556 fuzmatch->score = score;
3557 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003558 ++ga.ga_len;
3559 }
3560
3561 if (*e != NUL)
3562 ++e;
3563 }
3564 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003565
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003566 if (ga.ga_len == 0)
3567 return OK;
3568
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003569 if (!fuzzy)
3570 {
3571 *matches = ga.ga_data;
3572 *numMatches = ga.ga_len;
3573 }
3574 else
3575 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003576 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3577 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003578 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003579 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003580 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003581 return OK;
3582}
3583
3584/*
3585 * Expand names with a list returned by a function defined by the user.
3586 */
3587 static int
3588ExpandUserList(
3589 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003590 char_u ***matches,
3591 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003592{
3593 list_T *retlist;
3594 listitem_T *li;
3595 garray_T ga;
3596
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003597 *matches = NULL;
3598 *numMatches = 0;
3599 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003600 if (retlist == NULL)
3601 return FAIL;
3602
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003603 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003604 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003605 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003606 {
3607 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3608 continue; // Skip non-string items and empty strings
3609
3610 if (ga_grow(&ga, 1) == FAIL)
3611 break;
3612
3613 ((char_u **)ga.ga_data)[ga.ga_len] =
3614 vim_strsave(li->li_tv.vval.v_string);
3615 ++ga.ga_len;
3616 }
3617 list_unref(retlist);
3618
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003619 *matches = ga.ga_data;
3620 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003621 return OK;
3622}
K.Takata161b6ac2022-11-14 15:31:07 +00003623#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003624
3625/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003626 * Expand "file" for all comma-separated directories in "path".
3627 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003628 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003629 */
3630 void
3631globpath(
3632 char_u *path,
3633 char_u *file,
3634 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003635 int expand_options,
3636 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003637{
3638 expand_T xpc;
3639 char_u *buf;
3640 int i;
3641 int num_p;
3642 char_u **p;
3643
3644 buf = alloc(MAXPATHL);
3645 if (buf == NULL)
3646 return;
3647
3648 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003649 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003650
3651 // Loop over all entries in {path}.
3652 while (*path != NUL)
3653 {
3654 // Copy one item of the path to buf[] and concatenate the file name.
3655 copy_option_part(&path, buf, MAXPATHL, ",");
3656 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3657 {
K.Takata161b6ac2022-11-14 15:31:07 +00003658#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003659 // Using the platform's path separator (\) makes vim incorrectly
3660 // treat it as an escape character, use '/' instead.
3661 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3662 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003663#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003664 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003665#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003666 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003667 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003668 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3669 {
3670 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3671
3672 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003673 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003674 for (i = 0; i < num_p; ++i)
3675 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003676 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003677 ++ga->ga_len;
3678 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003679 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003680 }
3681 }
3682 }
3683
3684 vim_free(buf);
3685}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003686
Bram Moolenaareadee482020-09-04 15:37:31 +02003687/*
3688 * Translate some keys pressed when 'wildmenu' is used.
3689 */
3690 int
3691wildmenu_translate_key(
3692 cmdline_info_T *cclp,
3693 int key,
3694 expand_T *xp,
3695 int did_wild_list)
3696{
3697 int c = key;
3698
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003699 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003700 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003701 // When the popup menu is used for cmdline completion:
3702 // Up : go to the previous item in the menu
3703 // Down : go to the next item in the menu
3704 // Left : go to the parent directory
3705 // Right: list the files in the selected directory
3706 switch (c)
3707 {
3708 case K_UP: c = K_LEFT; break;
3709 case K_DOWN: c = K_RIGHT; break;
3710 case K_LEFT: c = K_UP; break;
3711 case K_RIGHT: c = K_DOWN; break;
3712 default: break;
3713 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003714 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003715
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003716 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003717 {
3718 if (c == K_LEFT)
3719 c = Ctrl_P;
3720 else if (c == K_RIGHT)
3721 c = Ctrl_N;
3722 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003723
Bram Moolenaareadee482020-09-04 15:37:31 +02003724 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003725 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003726 && cclp->cmdpos > 1
3727 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3728 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3729 && (c == '\n' || c == '\r' || c == K_KENTER))
3730 c = K_DOWN;
3731
3732 return c;
3733}
3734
3735/*
3736 * Delete characters on the command line, from "from" to the current
3737 * position.
3738 */
3739 static void
3740cmdline_del(cmdline_info_T *cclp, int from)
3741{
3742 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3743 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3744 cclp->cmdlen -= cclp->cmdpos - from;
3745 cclp->cmdpos = from;
3746}
3747
3748/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003749 * Handle a key pressed when the wild menu for the menu names
3750 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003751 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003752 static int
3753wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003754{
Bram Moolenaareadee482020-09-04 15:37:31 +02003755 int i;
3756 int j;
3757
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003758 // Hitting <Down> after "emenu Name.": complete submenu
3759 if (key == K_DOWN && cclp->cmdpos > 0
3760 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003761 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003762 key = p_wc;
3763 KeyTyped = TRUE; // in case the key was mapped
3764 }
3765 else if (key == K_UP)
3766 {
3767 // Hitting <Up>: Remove one submenu name in front of the
3768 // cursor
3769 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003770
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003771 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3772 i = 0;
3773 while (--j > 0)
3774 {
3775 // check for start of menu name
3776 if (cclp->cmdbuff[j] == ' '
3777 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003778 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003779 i = j + 1;
3780 break;
3781 }
3782 // check for start of submenu name
3783 if (cclp->cmdbuff[j] == '.'
3784 && cclp->cmdbuff[j - 1] != '\\')
3785 {
3786 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003787 {
3788 i = j + 1;
3789 break;
3790 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003791 else
3792 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003793 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003794 }
3795 if (i > 0)
3796 cmdline_del(cclp, i);
3797 key = p_wc;
3798 KeyTyped = TRUE; // in case the key was mapped
3799 xp->xp_context = EXPAND_NOTHING;
3800 }
3801
3802 return key;
3803}
3804
3805/*
3806 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3807 * directory names (EXPAND_DIRECTORIES) or shell command names
3808 * (EXPAND_SHELLCMD) is displayed.
3809 */
3810 static int
3811wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3812{
3813 int i;
3814 int j;
3815 char_u upseg[5];
3816
3817 upseg[0] = PATHSEP;
3818 upseg[1] = '.';
3819 upseg[2] = '.';
3820 upseg[3] = PATHSEP;
3821 upseg[4] = NUL;
3822
3823 if (key == K_DOWN
3824 && cclp->cmdpos > 0
3825 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3826 && (cclp->cmdpos < 3
3827 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3828 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3829 {
3830 // go down a directory
3831 key = p_wc;
3832 KeyTyped = TRUE; // in case the key was mapped
3833 }
3834 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3835 {
3836 // If in a direct ancestor, strip off one ../ to go down
3837 int found = FALSE;
3838
3839 j = cclp->cmdpos;
3840 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3841 while (--j > i)
3842 {
3843 if (has_mbyte)
3844 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3845 if (vim_ispathsep(cclp->cmdbuff[j]))
3846 {
3847 found = TRUE;
3848 break;
3849 }
3850 }
3851 if (found
3852 && cclp->cmdbuff[j - 1] == '.'
3853 && cclp->cmdbuff[j - 2] == '.'
3854 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3855 {
3856 cmdline_del(cclp, j - 2);
3857 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003858 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003859 }
3860 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003861 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003862 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003863 // go up a directory
3864 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003865
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003866 j = cclp->cmdpos - 1;
3867 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3868 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003869 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003870 if (has_mbyte)
3871 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3872 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003873#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003874 && vim_strchr((char_u *)" *?[{`$%#",
3875 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003876#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003877 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003878 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003879 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003880 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003881 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003882 break;
3883 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003884 else
3885 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003886 }
3887 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003888
3889 if (!found)
3890 j = i;
3891 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3892 j += 4;
3893 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3894 && j == i)
3895 j += 3;
3896 else
3897 j = 0;
3898 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003899 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003900 // TODO this is only for DOS/UNIX systems - need to put in
3901 // machine-specific stuff here and in upseg init
3902 cmdline_del(cclp, j);
3903 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003904 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003905 else if (cclp->cmdpos > i)
3906 cmdline_del(cclp, i);
3907
3908 // Now complete in the new directory. Set KeyTyped in case the
3909 // Up key came from a mapping.
3910 key = p_wc;
3911 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003912 }
3913
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003914 return key;
3915}
3916
3917/*
3918 * Handle a key pressed when the wild menu is displayed
3919 */
3920 int
3921wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3922{
3923 if (xp->xp_context == EXPAND_MENUNAMES)
3924 return wildmenu_process_key_menunames(cclp, key, xp);
3925 else if ((xp->xp_context == EXPAND_FILES
3926 || xp->xp_context == EXPAND_DIRECTORIES
3927 || xp->xp_context == EXPAND_SHELLCMD))
3928 return wildmenu_process_key_filenames(cclp, key, xp);
3929
3930 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003931}
3932
3933/*
3934 * Free expanded names when finished walking through the matches
3935 */
3936 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003937wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02003938{
3939 int skt = KeyTyped;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003940#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003941 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003942#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003943
3944 if (!p_wmnu || wild_menu_showing == 0)
3945 return;
3946
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003947#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003948 if (cclp->input_fn)
3949 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003950#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003951
3952 if (wild_menu_showing == WM_SCROLLED)
3953 {
3954 // Entered command line, move it up
3955 cmdline_row--;
3956 redrawcmd();
3957 }
3958 else if (save_p_ls != -1)
3959 {
3960 // restore 'laststatus' and 'winminheight'
3961 p_ls = save_p_ls;
3962 p_wmh = save_p_wmh;
3963 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003964 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02003965 redrawcmd();
3966 save_p_ls = -1;
3967 }
3968 else
3969 {
3970 win_redraw_last_status(topframe);
3971 redraw_statuslines();
3972 }
3973 KeyTyped = skt;
3974 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003975#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02003976 if (cclp->input_fn)
3977 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01003978#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02003979}
Bram Moolenaareadee482020-09-04 15:37:31 +02003980
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003981#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003982/*
3983 * "getcompletion()" function
3984 */
3985 void
3986f_getcompletion(typval_T *argvars, typval_T *rettv)
3987{
3988 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003989 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003990 expand_T xpc;
3991 int filtered = FALSE;
3992 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003993 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003994
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003995 if (in_vim9script()
3996 && (check_for_string_arg(argvars, 0) == FAIL
3997 || check_for_string_arg(argvars, 1) == FAIL
3998 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3999 return;
4000
ii144785fe02021-11-21 12:13:56 +00004001 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004002 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004003 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004004 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004005
4006 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004007 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004008
4009 if (p_wic)
4010 options |= WILD_ICASE;
4011
4012 // For filtered results, 'wildignore' is used
4013 if (!filtered)
4014 options |= WILD_KEEP_ALL;
4015
4016 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004017 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004018 {
ii144785fe02021-11-21 12:13:56 +00004019 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004020 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00004021 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004022 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004023 else
4024 {
ii144785fe02021-11-21 12:13:56 +00004025 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004026 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4027
4028 xpc.xp_context = cmdcomplete_str_to_type(type);
4029 if (xpc.xp_context == EXPAND_NOTHING)
4030 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004031 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004032 return;
4033 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004034
4035# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004036 if (xpc.xp_context == EXPAND_MENUS)
4037 {
4038 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4039 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4040 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004041# endif
4042# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004043 if (xpc.xp_context == EXPAND_CSCOPE)
4044 {
4045 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4046 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4047 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004048# endif
4049# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004050 if (xpc.xp_context == EXPAND_SIGN)
4051 {
4052 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4053 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4054 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004055# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004056 if (xpc.xp_context == EXPAND_RUNTIME)
4057 {
4058 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4059 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4060 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004061 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004062
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004063 if (cmdline_fuzzy_completion_supported(&xpc))
4064 // when fuzzy matching, don't modify the search string
4065 pat = vim_strsave(xpc.xp_pattern);
4066 else
4067 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4068
Bram Moolenaar93a10962022-06-16 11:42:09 +01004069 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004070 {
4071 int i;
4072
4073 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4074
4075 for (i = 0; i < xpc.xp_numfiles; i++)
4076 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4077 }
4078 vim_free(pat);
4079 ExpandCleanup(&xpc);
4080}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004081#endif // FEAT_EVAL