blob: c408bd099cef9e9e73fde373350678e35ce7a4e3 [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
18static void set_expand_context(expand_T *xp);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000019static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000020 char_u ***matches, int *numMatches,
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000021 char_u *((*func)(expand_T *, int)), int escaped);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000022static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
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#ifdef FEAT_WILDMENU
31// "compl_match_array" points the currently displayed list of entries in the
32// popup menu. It is NULL when there is no popup menu.
33static pumitem_T *compl_match_array = NULL;
34static int compl_match_arraysize;
35// First column in cmdline of the matched item for completion.
36static int compl_startcol;
37static int compl_selected;
38#endif
39
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040#define SHOW_FILE_TEXT(m) (showtail ? sm_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000041
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000042/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000043 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
44 * context.
45 */
46 static int
47cmdline_fuzzy_completion_supported(expand_T *xp)
48{
49 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
50 && xp->xp_context != EXPAND_BOOL_SETTINGS
51 && xp->xp_context != EXPAND_COLORS
52 && xp->xp_context != EXPAND_COMPILER
53 && xp->xp_context != EXPAND_DIRECTORIES
54 && xp->xp_context != EXPAND_FILES
55 && xp->xp_context != EXPAND_FILES_IN_PATH
56 && xp->xp_context != EXPAND_FILETYPE
57 && xp->xp_context != EXPAND_HELP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000058 && xp->xp_context != EXPAND_OLD_SETTING
59 && xp->xp_context != EXPAND_OWNSYNTAX
60 && xp->xp_context != EXPAND_PACKADD
61 && xp->xp_context != EXPAND_SHELLCMD
62 && xp->xp_context != EXPAND_TAGS
63 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064 && xp->xp_context != EXPAND_USER_LIST);
65}
66
67/*
68 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000069 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
70 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000071 */
72 int
73cmdline_fuzzy_complete(char_u *fuzzystr)
74{
75 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
76}
77
78/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000079 * sort function for the completion matches.
80 * <SNR> functions should be sorted to the end.
81 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020082 static int
83sort_func_compare(const void *s1, const void *s2)
84{
85 char_u *p1 = *(char_u **)s1;
86 char_u *p2 = *(char_u **)s2;
87
88 if (*p1 != '<' && *p2 == '<') return -1;
89 if (*p1 == '<' && *p2 != '<') return 1;
90 return STRCMP(p1, p2);
91}
Bram Moolenaar66b51422019-08-18 21:44:12 +020092
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000093/*
94 * Escape special characters in the cmdline completion matches.
95 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020096 static void
97ExpandEscape(
98 expand_T *xp,
99 char_u *str,
100 int numfiles,
101 char_u **files,
102 int options)
103{
104 int i;
105 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100106 int vse_what = xp->xp_context == EXPAND_BUFFERS
107 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200108
109 // May change home directory back to "~"
110 if (options & WILD_HOME_REPLACE)
111 tilde_replace(str, numfiles, files);
112
113 if (options & WILD_ESCAPE)
114 {
115 if (xp->xp_context == EXPAND_FILES
116 || xp->xp_context == EXPAND_FILES_IN_PATH
117 || xp->xp_context == EXPAND_SHELLCMD
118 || xp->xp_context == EXPAND_BUFFERS
119 || xp->xp_context == EXPAND_DIRECTORIES)
120 {
121 // Insert a backslash into a file name before a space, \, %, #
122 // and wildmatch characters, except '~'.
123 for (i = 0; i < numfiles; ++i)
124 {
125 // for ":set path=" we need to escape spaces twice
126 if (xp->xp_backslash == XP_BS_THREE)
127 {
128 p = vim_strsave_escaped(files[i], (char_u *)" ");
129 if (p != NULL)
130 {
131 vim_free(files[i]);
132 files[i] = p;
133#if defined(BACKSLASH_IN_FILENAME)
134 p = vim_strsave_escaped(files[i], (char_u *)" ");
135 if (p != NULL)
136 {
137 vim_free(files[i]);
138 files[i] = p;
139 }
140#endif
141 }
142 }
143#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100144 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200145#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100146 p = vim_strsave_fnameescape(files[i],
147 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200148#endif
149 if (p != NULL)
150 {
151 vim_free(files[i]);
152 files[i] = p;
153 }
154
155 // If 'str' starts with "\~", replace "~" at start of
156 // files[i] with "\~".
157 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
158 escape_fname(&files[i]);
159 }
160 xp->xp_backslash = XP_BS_NONE;
161
162 // If the first file starts with a '+' escape it. Otherwise it
163 // could be seen as "+cmd".
164 if (*files[0] == '+')
165 escape_fname(&files[0]);
166 }
167 else if (xp->xp_context == EXPAND_TAGS)
168 {
169 // Insert a backslash before characters in a tag name that
170 // would terminate the ":tag" command.
171 for (i = 0; i < numfiles; ++i)
172 {
173 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
174 if (p != NULL)
175 {
176 vim_free(files[i]);
177 files[i] = p;
178 }
179 }
180 }
181 }
182}
183
184/*
185 * Return FAIL if this is not an appropriate context in which to do
186 * completion of anything, return OK if it is (even if there are no matches).
187 * For the caller, this means that the character is just passed through like a
188 * normal character (instead of being expanded). This allows :s/^I^D etc.
189 */
190 int
191nextwild(
192 expand_T *xp,
193 int type,
194 int options, // extra options for ExpandOne()
195 int escape) // if TRUE, escape the returned matches
196{
197 cmdline_info_T *ccline = get_cmdline_info();
198 int i, j;
199 char_u *p1;
200 char_u *p2;
201 int difflen;
202 int v;
203
204 if (xp->xp_numfiles == -1)
205 {
206 set_expand_context(xp);
207 cmd_showtail = expand_showtail(xp);
208 }
209
210 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
211 {
212 beep_flush();
213 return OK; // Something illegal on command line
214 }
215 if (xp->xp_context == EXPAND_NOTHING)
216 {
217 // Caller can use the character as a normal char instead
218 return FAIL;
219 }
220
221 msg_puts("..."); // show that we are busy
222 out_flush();
223
224 i = (int)(xp->xp_pattern - ccline->cmdbuff);
225 xp->xp_pattern_len = ccline->cmdpos - i;
226
227 if (type == WILD_NEXT || type == WILD_PREV)
228 {
229 // Get next/previous match for a previous expanded pattern.
230 p2 = ExpandOne(xp, NULL, NULL, 0, type);
231 }
232 else
233 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000234 if (cmdline_fuzzy_completion_supported(xp))
235 // If fuzzy matching, don't modify the search string
236 p1 = vim_strsave(xp->xp_pattern);
237 else
238 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
239
Bram Moolenaar66b51422019-08-18 21:44:12 +0200240 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000241 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200242 p2 = NULL;
243 else
244 {
245 int use_options = options |
246 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
247 if (escape)
248 use_options |= WILD_ESCAPE;
249
250 if (p_wic)
251 use_options += WILD_ICASE;
252 p2 = ExpandOne(xp, p1,
253 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
254 use_options, type);
255 vim_free(p1);
256 // longest match: make sure it is not shorter, happens with :help
257 if (p2 != NULL && type == WILD_LONGEST)
258 {
259 for (j = 0; j < xp->xp_pattern_len; ++j)
260 if (ccline->cmdbuff[i + j] == '*'
261 || ccline->cmdbuff[i + j] == '?')
262 break;
263 if ((int)STRLEN(p2) < j)
264 VIM_CLEAR(p2);
265 }
266 }
267 }
268
269 if (p2 != NULL && !got_int)
270 {
271 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
272 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
273 {
274 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
275 xp->xp_pattern = ccline->cmdbuff + i;
276 }
277 else
278 v = OK;
279 if (v == OK)
280 {
281 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
282 &ccline->cmdbuff[ccline->cmdpos],
283 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
284 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
285 ccline->cmdlen += difflen;
286 ccline->cmdpos += difflen;
287 }
288 }
289 vim_free(p2);
290
291 redrawcmd();
292 cursorcmd();
293
294 // When expanding a ":map" command and no matches are found, assume that
295 // the key is supposed to be inserted literally
296 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
297 return FAIL;
298
299 if (xp->xp_numfiles <= 0 && p2 == NULL)
300 beep_flush();
301 else if (xp->xp_numfiles == 1)
302 // free expanded pattern
303 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
304
305 return OK;
306}
307
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000308#if defined(FEAT_WILDMENU) || defined(PROTO)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000309
310/*
311 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000312 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000313 */
314 static int
315cmdline_pum_create(
316 cmdline_info_T *ccline,
317 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000318 char_u **matches,
319 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000320 int showtail)
321{
322 int i;
323 int columns;
324
325 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000326 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000327 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000328 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000329 {
330 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
331 compl_match_array[i].pum_info = NULL;
332 compl_match_array[i].pum_extra = NULL;
333 compl_match_array[i].pum_kind = NULL;
334 }
335
336 // Compute the popup menu starting column
337 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
338 columns = vim_strsize(xp->xp_pattern);
339 if (showtail)
340 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000341 columns += vim_strsize(sm_gettail(matches[0]));
342 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000343 }
344 if (columns >= compl_startcol)
345 compl_startcol = 0;
346 else
347 compl_startcol -= columns;
348
349 // no default selection
350 compl_selected = -1;
351
352 cmdline_pum_display();
353
354 return EXPAND_OK;
355}
356
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000357/*
358 * Display the cmdline completion matches in a popup menu
359 */
360void cmdline_pum_display(void)
361{
362 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
363}
364
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000365/*
366 * Returns TRUE if the cmdline completion popup menu is being displayed.
367 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000368int cmdline_pum_active(void)
369{
370 return p_wmnu && pum_visible() && compl_match_array != NULL;
371}
372
373/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000374 * Remove the cmdline completion popup menu (if present), free the list of
375 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000376 */
377void cmdline_pum_remove(void)
378{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000379 int save_p_lz = p_lz;
380
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000381 pum_undisplay();
382 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000383 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000384 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000385 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000386 redrawcmd();
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000387}
388
389void cmdline_pum_cleanup(cmdline_info_T *cclp)
390{
391 cmdline_pum_remove();
392 wildmenu_cleanup(cclp);
393}
394
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000395/*
396 * Returns the starting column number to use for the cmdline completion popup
397 * menu.
398 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000399int cmdline_compl_startcol(void)
400{
401 return compl_startcol;
402}
403#endif
404
Bram Moolenaar66b51422019-08-18 21:44:12 +0200405/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000406 * Get the next or prev cmdline completion match. The index of the match is set
407 * in 'p_findex'
408 */
409 static char_u *
410get_next_or_prev_match(
411 int mode,
412 expand_T *xp,
413 int *p_findex,
414 char_u *orig_save)
415{
416 int findex = *p_findex;
417
418 if (xp->xp_numfiles <= 0)
419 return NULL;
420
421 if (mode == WILD_PREV)
422 {
423 if (findex == -1)
424 findex = xp->xp_numfiles;
425 --findex;
426 }
427 else // mode == WILD_NEXT
428 ++findex;
429
430 // When wrapping around, return the original string, set findex to
431 // -1.
432 if (findex < 0)
433 {
434 if (orig_save == NULL)
435 findex = xp->xp_numfiles - 1;
436 else
437 findex = -1;
438 }
439 if (findex >= xp->xp_numfiles)
440 {
441 if (orig_save == NULL)
442 findex = 0;
443 else
444 findex = -1;
445 }
446#ifdef FEAT_WILDMENU
447 if (compl_match_array)
448 {
449 compl_selected = findex;
450 cmdline_pum_display();
451 }
452 else if (p_wmnu)
453 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
454 findex, cmd_showtail);
455#endif
456 *p_findex = findex;
457
458 if (findex == -1)
459 return vim_strsave(orig_save);
460
461 return vim_strsave(xp->xp_files[findex]);
462}
463
464/*
465 * Start the command-line expansion and get the matches.
466 */
467 static char_u *
468ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
469{
470 int non_suf_match; // number without matching suffix
471 int i;
472 char_u *ss = NULL;
473
474 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000475 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000476 options) == FAIL)
477 {
478#ifdef FNAME_ILLEGAL
479 // Illegal file name has been silently skipped. But when there
480 // are wildcards, the real problem is that there was no match,
481 // causing the pattern to be added, which has illegal characters.
482 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
483 semsg(_(e_no_match_str_2), str);
484#endif
485 }
486 else if (xp->xp_numfiles == 0)
487 {
488 if (!(options & WILD_SILENT))
489 semsg(_(e_no_match_str_2), str);
490 }
491 else
492 {
493 // Escape the matches for use on the command line.
494 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
495
496 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000497 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000498 {
499 if (xp->xp_numfiles)
500 non_suf_match = xp->xp_numfiles;
501 else
502 non_suf_match = 1;
503 if ((xp->xp_context == EXPAND_FILES
504 || xp->xp_context == EXPAND_DIRECTORIES)
505 && xp->xp_numfiles > 1)
506 {
507 // More than one match; check suffix.
508 // The files will have been sorted on matching suffix in
509 // expand_wildcards, only need to check the first two.
510 non_suf_match = 0;
511 for (i = 0; i < 2; ++i)
512 if (match_suffix(xp->xp_files[i]))
513 ++non_suf_match;
514 }
515 if (non_suf_match != 1)
516 {
517 // Can we ever get here unless it's while expanding
518 // interactively? If not, we can get rid of this all
519 // together. Don't really want to wait for this message
520 // (and possibly have to hit return to continue!).
521 if (!(options & WILD_SILENT))
522 emsg(_(e_too_many_file_names));
523 else if (!(options & WILD_NO_BEEP))
524 beep_flush();
525 }
526 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
527 ss = vim_strsave(xp->xp_files[0]);
528 }
529 }
530
531 return ss;
532}
533
534/*
535 * Return the longest common part in the list of cmdline completion matches.
536 */
537 static char_u *
538find_longest_match(expand_T *xp, int options)
539{
540 long_u len;
541 int mb_len = 1;
542 int c0, ci;
543 int i;
544 char_u *ss;
545
546 for (len = 0; xp->xp_files[0][len]; len += mb_len)
547 {
548 if (has_mbyte)
549 {
550 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
551 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
552 }
553 else
554 c0 = xp->xp_files[0][len];
555 for (i = 1; i < xp->xp_numfiles; ++i)
556 {
557 if (has_mbyte)
558 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
559 else
560 ci = xp->xp_files[i][len];
561 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
562 || xp->xp_context == EXPAND_FILES
563 || xp->xp_context == EXPAND_SHELLCMD
564 || xp->xp_context == EXPAND_BUFFERS))
565 {
566 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
567 break;
568 }
569 else if (c0 != ci)
570 break;
571 }
572 if (i < xp->xp_numfiles)
573 {
574 if (!(options & WILD_NO_BEEP))
575 vim_beep(BO_WILD);
576 break;
577 }
578 }
579
580 ss = alloc(len + 1);
581 if (ss)
582 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
583
584 return ss;
585}
586
587/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200588 * Do wildcard expansion on the string 'str'.
589 * Chars that should not be expanded must be preceded with a backslash.
590 * Return a pointer to allocated memory containing the new string.
591 * Return NULL for failure.
592 *
593 * "orig" is the originally expanded string, copied to allocated memory. It
594 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
595 * WILD_PREV "orig" should be NULL.
596 *
597 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
598 * is WILD_EXPAND_FREE or WILD_ALL.
599 *
600 * mode = WILD_FREE: just free previously expanded matches
601 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
602 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
603 * mode = WILD_NEXT: use next match in multiple match, wrap to first
604 * mode = WILD_PREV: use previous match in multiple match, wrap to first
605 * mode = WILD_ALL: return all matches concatenated
606 * mode = WILD_LONGEST: return longest matched part
607 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000608 * mode = WILD_APPLY: apply the item selected in the cmdline completion
609 * popup menu and close the menu.
610 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
611 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200612 *
613 * options = WILD_LIST_NOTFOUND: list entries without a match
614 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
615 * options = WILD_USE_NL: Use '\n' for WILD_ALL
616 * options = WILD_NO_BEEP: Don't beep for multiple matches
617 * options = WILD_ADD_SLASH: add a slash after directory names
618 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
619 * options = WILD_SILENT: don't print warning messages
620 * options = WILD_ESCAPE: put backslash before special chars
621 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200622 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200623 *
624 * The variables xp->xp_context and xp->xp_backslash must have been set!
625 */
626 char_u *
627ExpandOne(
628 expand_T *xp,
629 char_u *str,
630 char_u *orig, // allocated copy of original of expanded string
631 int options,
632 int mode)
633{
634 char_u *ss = NULL;
635 static int findex;
636 static char_u *orig_save = NULL; // kept value of orig
637 int orig_saved = FALSE;
638 int i;
639 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200640
641 // first handle the case of using an old match
642 if (mode == WILD_NEXT || mode == WILD_PREV)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000643 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200644
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000645 if (mode == WILD_CANCEL)
646 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
647 else if (mode == WILD_APPLY)
648 ss = vim_strsave(findex == -1 ? (orig_save ?
649 orig_save : (char_u *)"") : xp->xp_files[findex]);
650
Bram Moolenaar66b51422019-08-18 21:44:12 +0200651 // free old names
652 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
653 {
654 FreeWild(xp->xp_numfiles, xp->xp_files);
655 xp->xp_numfiles = -1;
656 VIM_CLEAR(orig_save);
657 }
658 findex = 0;
659
660 if (mode == WILD_FREE) // only release file name
661 return NULL;
662
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000663 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200664 {
665 vim_free(orig_save);
666 orig_save = orig;
667 orig_saved = TRUE;
668
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000669 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200670 }
671
672 // Find longest common part
673 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
674 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000675 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200676 findex = -1; // next p_wc gets first one
677 }
678
679 // Concatenate all matching names
680 if (mode == WILD_ALL && xp->xp_numfiles > 0)
681 {
682 len = 0;
683 for (i = 0; i < xp->xp_numfiles; ++i)
684 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
685 ss = alloc(len);
686 if (ss != NULL)
687 {
688 *ss = NUL;
689 for (i = 0; i < xp->xp_numfiles; ++i)
690 {
691 STRCAT(ss, xp->xp_files[i]);
692 if (i != xp->xp_numfiles - 1)
693 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
694 }
695 }
696 }
697
698 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
699 ExpandCleanup(xp);
700
701 // Free "orig" if it wasn't stored in "orig_save".
702 if (!orig_saved)
703 vim_free(orig);
704
705 return ss;
706}
707
708/*
709 * Prepare an expand structure for use.
710 */
711 void
712ExpandInit(expand_T *xp)
713{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200714 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200715 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200716 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200717}
718
719/*
720 * Cleanup an expand structure after use.
721 */
722 void
723ExpandCleanup(expand_T *xp)
724{
725 if (xp->xp_numfiles >= 0)
726 {
727 FreeWild(xp->xp_numfiles, xp->xp_files);
728 xp->xp_numfiles = -1;
729 }
730}
731
732/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000733 * Display one line of completion matches. Multiple matches are displayed in
734 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000735 * matches - list of completion match names
736 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000737 * lines - number of output lines
738 * linenr - line number of matches to display
739 * maxlen - maximum number of characters in each line
740 * showtail - display only the tail of the full path of a file name
741 * dir_attr - highlight attribute to use for directory names
742 */
743 static void
744showmatches_oneline(
745 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000746 char_u **matches,
747 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000748 int lines,
749 int linenr,
750 int maxlen,
751 int showtail,
752 int dir_attr)
753{
754 int i, j;
755 int isdir;
756 int lastlen;
757 char_u *p;
758
759 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000760 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000761 {
762 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
763 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000764 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
765 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000766 msg_advance(maxlen + 1);
767 msg_puts((char *)p);
768 msg_advance(maxlen + 3);
769 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
770 break;
771 }
772 for (i = maxlen - lastlen; --i >= 0; )
773 msg_putchar(' ');
774 if (xp->xp_context == EXPAND_FILES
775 || xp->xp_context == EXPAND_SHELLCMD
776 || xp->xp_context == EXPAND_BUFFERS)
777 {
778 // highlight directories
779 if (xp->xp_numfiles != -1)
780 {
781 char_u *halved_slash;
782 char_u *exp_path;
783 char_u *path;
784
785 // Expansion was done before and special characters
786 // were escaped, need to halve backslashes. Also
787 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000788 exp_path = expand_env_save_opt(matches[j], TRUE);
789 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000790 halved_slash = backslash_halve_save(path);
791 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000792 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000793 vim_free(exp_path);
794 if (halved_slash != path)
795 vim_free(halved_slash);
796 }
797 else
798 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000799 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000800 if (showtail)
801 p = SHOW_FILE_TEXT(j);
802 else
803 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000804 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000805 TRUE);
806 p = NameBuff;
807 }
808 }
809 else
810 {
811 isdir = FALSE;
812 p = SHOW_FILE_TEXT(j);
813 }
814 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
815 }
816 if (msg_col > 0) // when not wrapped around
817 {
818 msg_clr_eos();
819 msg_putchar('\n');
820 }
821 out_flush(); // show one line at a time
822}
823
824/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200825 * Show all matches for completion on the command line.
826 * Returns EXPAND_NOTHING when the character that triggered expansion should
827 * be inserted like a normal character.
828 */
829 int
830showmatches(expand_T *xp, int wildmenu UNUSED)
831{
832 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000833 int numMatches;
834 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000835 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200836 int maxlen;
837 int lines;
838 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200839 int attr;
840 int showtail;
841
842 if (xp->xp_numfiles == -1)
843 {
844 set_expand_context(xp);
845 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000846 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200847 showtail = expand_showtail(xp);
848 if (i != EXPAND_OK)
849 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200850 }
851 else
852 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000853 numMatches = xp->xp_numfiles;
854 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200855 showtail = cmd_showtail;
856 }
857
858#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000859 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000860 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000861 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000862#endif
863
864#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200865 if (!wildmenu)
866 {
867#endif
868 msg_didany = FALSE; // lines_left will be set
869 msg_start(); // prepare for paging
870 msg_putchar('\n');
871 out_flush();
872 cmdline_row = msg_row;
873 msg_didany = FALSE; // lines_left will be set again
874 msg_start(); // prepare for paging
875#ifdef FEAT_WILDMENU
876 }
877#endif
878
879 if (got_int)
880 got_int = FALSE; // only int. the completion, not the cmd line
881#ifdef FEAT_WILDMENU
882 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000883 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200884#endif
885 else
886 {
887 // find the length of the longest file name
888 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000889 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200890 {
891 if (!showtail && (xp->xp_context == EXPAND_FILES
892 || xp->xp_context == EXPAND_SHELLCMD
893 || xp->xp_context == EXPAND_BUFFERS))
894 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000895 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200896 j = vim_strsize(NameBuff);
897 }
898 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000899 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200900 if (j > maxlen)
901 maxlen = j;
902 }
903
904 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000905 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200906 else
907 {
908 // compute the number of columns and lines for the listing
909 maxlen += 2; // two spaces between file names
910 columns = ((int)Columns + 2) / maxlen;
911 if (columns < 1)
912 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000913 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200914 }
915
916 attr = HL_ATTR(HLF_D); // find out highlighting for directories
917
918 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
919 {
920 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
921 msg_clr_eos();
922 msg_advance(maxlen - 3);
923 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
924 }
925
926 // list the files line by line
927 for (i = 0; i < lines; ++i)
928 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000929 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000930 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200931 if (got_int)
932 {
933 got_int = FALSE;
934 break;
935 }
936 }
937
938 // we redraw the command below the lines that we have just listed
939 // This is a bit tricky, but it saves a lot of screen updating.
940 cmdline_row = msg_row; // will put it back later
941 }
942
943 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000944 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200945
946 return EXPAND_OK;
947}
948
949/*
950 * Private gettail for showmatches() (and win_redr_status_matches()):
951 * Find tail of file name path, but ignore trailing "/".
952 */
953 char_u *
954sm_gettail(char_u *s)
955{
956 char_u *p;
957 char_u *t = s;
958 int had_sep = FALSE;
959
960 for (p = s; *p != NUL; )
961 {
962 if (vim_ispathsep(*p)
963#ifdef BACKSLASH_IN_FILENAME
964 && !rem_backslash(p)
965#endif
966 )
967 had_sep = TRUE;
968 else if (had_sep)
969 {
970 t = p;
971 had_sep = FALSE;
972 }
973 MB_PTR_ADV(p);
974 }
975 return t;
976}
977
978/*
979 * Return TRUE if we only need to show the tail of completion matches.
980 * When not completing file names or there is a wildcard in the path FALSE is
981 * returned.
982 */
983 static int
984expand_showtail(expand_T *xp)
985{
986 char_u *s;
987 char_u *end;
988
989 // When not completing file names a "/" may mean something different.
990 if (xp->xp_context != EXPAND_FILES
991 && xp->xp_context != EXPAND_SHELLCMD
992 && xp->xp_context != EXPAND_DIRECTORIES)
993 return FALSE;
994
995 end = gettail(xp->xp_pattern);
996 if (end == xp->xp_pattern) // there is no path separator
997 return FALSE;
998
999 for (s = xp->xp_pattern; s < end; s++)
1000 {
1001 // Skip escaped wildcards. Only when the backslash is not a path
1002 // separator, on DOS the '*' "path\*\file" must not be skipped.
1003 if (rem_backslash(s))
1004 ++s;
1005 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1006 return FALSE;
1007 }
1008 return TRUE;
1009}
1010
1011/*
1012 * Prepare a string for expansion.
1013 * When expanding file names: The string will be used with expand_wildcards().
1014 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1015 * When expanding other names: The string will be used with regcomp(). Copy
1016 * the name into allocated memory and prepend "^".
1017 */
1018 char_u *
1019addstar(
1020 char_u *fname,
1021 int len,
1022 int context) // EXPAND_FILES etc.
1023{
1024 char_u *retval;
1025 int i, j;
1026 int new_len;
1027 char_u *tail;
1028 int ends_in_star;
1029
1030 if (context != EXPAND_FILES
1031 && context != EXPAND_FILES_IN_PATH
1032 && context != EXPAND_SHELLCMD
1033 && context != EXPAND_DIRECTORIES)
1034 {
1035 // Matching will be done internally (on something other than files).
1036 // So we convert the file-matching-type wildcards into our kind for
1037 // use with vim_regcomp(). First work out how long it will be:
1038
1039 // For help tags the translation is done in find_help_tags().
1040 // For a tag pattern starting with "/" no translation is needed.
1041 if (context == EXPAND_HELP
1042 || context == EXPAND_COLORS
1043 || context == EXPAND_COMPILER
1044 || context == EXPAND_OWNSYNTAX
1045 || context == EXPAND_FILETYPE
1046 || context == EXPAND_PACKADD
1047 || ((context == EXPAND_TAGS_LISTFILES
1048 || context == EXPAND_TAGS)
1049 && fname[0] == '/'))
1050 retval = vim_strnsave(fname, len);
1051 else
1052 {
1053 new_len = len + 2; // +2 for '^' at start, NUL at end
1054 for (i = 0; i < len; i++)
1055 {
1056 if (fname[i] == '*' || fname[i] == '~')
1057 new_len++; // '*' needs to be replaced by ".*"
1058 // '~' needs to be replaced by "\~"
1059
1060 // Buffer names are like file names. "." should be literal
1061 if (context == EXPAND_BUFFERS && fname[i] == '.')
1062 new_len++; // "." becomes "\."
1063
1064 // Custom expansion takes care of special things, match
1065 // backslashes literally (perhaps also for other types?)
1066 if ((context == EXPAND_USER_DEFINED
1067 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1068 new_len++; // '\' becomes "\\"
1069 }
1070 retval = alloc(new_len);
1071 if (retval != NULL)
1072 {
1073 retval[0] = '^';
1074 j = 1;
1075 for (i = 0; i < len; i++, j++)
1076 {
1077 // Skip backslash. But why? At least keep it for custom
1078 // expansion.
1079 if (context != EXPAND_USER_DEFINED
1080 && context != EXPAND_USER_LIST
1081 && fname[i] == '\\'
1082 && ++i == len)
1083 break;
1084
1085 switch (fname[i])
1086 {
1087 case '*': retval[j++] = '.';
1088 break;
1089 case '~': retval[j++] = '\\';
1090 break;
1091 case '?': retval[j] = '.';
1092 continue;
1093 case '.': if (context == EXPAND_BUFFERS)
1094 retval[j++] = '\\';
1095 break;
1096 case '\\': if (context == EXPAND_USER_DEFINED
1097 || context == EXPAND_USER_LIST)
1098 retval[j++] = '\\';
1099 break;
1100 }
1101 retval[j] = fname[i];
1102 }
1103 retval[j] = NUL;
1104 }
1105 }
1106 }
1107 else
1108 {
1109 retval = alloc(len + 4);
1110 if (retval != NULL)
1111 {
1112 vim_strncpy(retval, fname, len);
1113
1114 // Don't add a star to *, ~, ~user, $var or `cmd`.
1115 // * would become **, which walks the whole tree.
1116 // ~ would be at the start of the file name, but not the tail.
1117 // $ could be anywhere in the tail.
1118 // ` could be anywhere in the file name.
1119 // When the name ends in '$' don't add a star, remove the '$'.
1120 tail = gettail(retval);
1121 ends_in_star = (len > 0 && retval[len - 1] == '*');
1122#ifndef BACKSLASH_IN_FILENAME
1123 for (i = len - 2; i >= 0; --i)
1124 {
1125 if (retval[i] != '\\')
1126 break;
1127 ends_in_star = !ends_in_star;
1128 }
1129#endif
1130 if ((*retval != '~' || tail != retval)
1131 && !ends_in_star
1132 && vim_strchr(tail, '$') == NULL
1133 && vim_strchr(retval, '`') == NULL)
1134 retval[len++] = '*';
1135 else if (len > 0 && retval[len - 1] == '$')
1136 --len;
1137 retval[len] = NUL;
1138 }
1139 }
1140 return retval;
1141}
1142
1143/*
1144 * Must parse the command line so far to work out what context we are in.
1145 * Completion can then be done based on that context.
1146 * This routine sets the variables:
1147 * xp->xp_pattern The start of the pattern to be expanded within
1148 * the command line (ends at the cursor).
1149 * xp->xp_context The type of thing to expand. Will be one of:
1150 *
1151 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1152 * the command line, like an unknown command. Caller
1153 * should beep.
1154 * EXPAND_NOTHING Unrecognised context for completion, use char like
1155 * a normal char, rather than for completion. eg
1156 * :s/^I/
1157 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1158 * it.
1159 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1160 * EXPAND_FILES After command with EX_XFILE set, or after setting
1161 * with P_EXPAND set. eg :e ^I, :w>>^I
1162 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1163 * when we know only directories are of interest. eg
1164 * :set dir=^I
1165 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1166 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1167 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1168 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1169 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1170 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1171 * EXPAND_EVENTS Complete event names
1172 * EXPAND_SYNTAX Complete :syntax command arguments
1173 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1174 * EXPAND_AUGROUP Complete autocommand group names
1175 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1176 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1177 * eg :unmap a^I , :cunab x^I
1178 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1179 * eg :call sub^I
1180 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1181 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1182 * names in expressions, eg :while s^I
1183 * EXPAND_ENV_VARS Complete environment variable names
1184 * EXPAND_USER Complete user names
1185 */
1186 static void
1187set_expand_context(expand_T *xp)
1188{
1189 cmdline_info_T *ccline = get_cmdline_info();
1190
1191 // only expansion for ':', '>' and '=' command-lines
1192 if (ccline->cmdfirstc != ':'
1193#ifdef FEAT_EVAL
1194 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1195 && !ccline->input_fn
1196#endif
1197 )
1198 {
1199 xp->xp_context = EXPAND_NOTHING;
1200 return;
1201 }
1202 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1203}
1204
Bram Moolenaard0190392019-08-23 21:17:35 +02001205/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001206 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1207 * For user defined commands, the completion context is set in 'xp' and the
1208 * completion flags in 'complp'.
1209 *
1210 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001211 */
1212 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001213set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001214{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001215 char_u *p = NULL;
1216 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001217 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001218
1219 // Isolate the command and search for it in the command table.
1220 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001221 // - the 'k' command can directly be followed by any character, but do
1222 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1223 // find matches anywhere in the command name, do this only for command
1224 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001225 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001226 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001227 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001228 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001229 p = cmd + 1;
1230 }
1231 else
1232 {
1233 p = cmd;
1234 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1235 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001236 // A user command may contain digits.
1237 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1238 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001239 while (ASCII_ISALNUM(*p) || *p == '*')
1240 ++p;
1241 // for python 3.x: ":py3*" commands completion
1242 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1243 {
1244 ++p;
1245 while (ASCII_ISALPHA(*p) || *p == '*')
1246 ++p;
1247 }
1248 // check for non-alpha command
1249 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1250 ++p;
1251 len = (int)(p - cmd);
1252
1253 if (len == 0)
1254 {
1255 xp->xp_context = EXPAND_UNSUCCESSFUL;
1256 return NULL;
1257 }
1258
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001259 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001260
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001261 // User defined commands support alphanumeric characters.
1262 // Also when doing fuzzy expansion, support alphanumeric characters.
1263 if ((cmd[0] >= 'A' && cmd[0] <= 'Z') || (fuzzy && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001264 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1265 ++p;
1266 }
1267
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001268 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001269 // character, complete the command name.
1270 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1271 return NULL;
1272
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001273 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001274 {
1275 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1276 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001277 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001278 p = cmd + 1;
1279 }
1280 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1281 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001282 eap->cmd = cmd;
1283 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001284 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001285 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001286 }
1287 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001288 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001289 {
1290 // Not still touching the command and it was an illegal one
1291 xp->xp_context = EXPAND_UNSUCCESSFUL;
1292 return NULL;
1293 }
1294
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001295 return p;
1296}
Bram Moolenaard0190392019-08-23 21:17:35 +02001297
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001298/*
1299 * Set the completion context for a command argument with wild card characters.
1300 */
1301 static void
1302set_context_for_wildcard_arg(
1303 exarg_T *eap,
1304 char_u *arg,
1305 int usefilter,
1306 expand_T *xp,
1307 int *complp)
1308{
1309 char_u *p;
1310 int c;
1311 int in_quote = FALSE;
1312 char_u *bow = NULL; // Beginning of word
1313 int len = 0;
1314
1315 // Allow spaces within back-quotes to count as part of the argument
1316 // being expanded.
1317 xp->xp_pattern = skipwhite(arg);
1318 p = xp->xp_pattern;
1319 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001320 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001321 if (has_mbyte)
1322 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001323 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001324 c = *p;
1325 if (c == '\\' && p[1] != NUL)
1326 ++p;
1327 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001328 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001329 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001330 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001331 xp->xp_pattern = p;
1332 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001333 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001334 in_quote = !in_quote;
1335 }
1336 // An argument can contain just about everything, except
1337 // characters that end the command and white space.
1338 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001339#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001340 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001341#endif
1342 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001343 {
1344 len = 0; // avoid getting stuck when space is in 'isfname'
1345 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001346 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001347 if (has_mbyte)
1348 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001349 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001350 c = *p;
1351 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001352 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001353 if (has_mbyte)
1354 len = (*mb_ptr2len)(p);
1355 else
1356 len = 1;
1357 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001358 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001359 if (in_quote)
1360 bow = p;
1361 else
1362 xp->xp_pattern = p;
1363 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001364 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001365 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001366 }
1367
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001368 // If we are still inside the quotes, and we passed a space, just
1369 // expand from there.
1370 if (bow != NULL && in_quote)
1371 xp->xp_pattern = bow;
1372 xp->xp_context = EXPAND_FILES;
1373
1374 // For a shell command more chars need to be escaped.
1375 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1376 {
1377#ifndef BACKSLASH_IN_FILENAME
1378 xp->xp_shell = TRUE;
1379#endif
1380 // When still after the command name expand executables.
1381 if (xp->xp_pattern == skipwhite(arg))
1382 xp->xp_context = EXPAND_SHELLCMD;
1383 }
1384
1385 // Check for environment variable.
1386 if (*xp->xp_pattern == '$')
1387 {
1388 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1389 if (!vim_isIDc(*p))
1390 break;
1391 if (*p == NUL)
1392 {
1393 xp->xp_context = EXPAND_ENV_VARS;
1394 ++xp->xp_pattern;
1395 // Avoid that the assignment uses EXPAND_FILES again.
1396 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1397 *complp = EXPAND_ENV_VARS;
1398 }
1399 }
1400 // Check for user names.
1401 if (*xp->xp_pattern == '~')
1402 {
1403 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1404 ;
1405 // Complete ~user only if it partially matches a user name.
1406 // A full match ~user<Tab> will be replaced by user's home
1407 // directory i.e. something like ~user<Tab> -> /home/user/
1408 if (*p == NUL && p > xp->xp_pattern + 1
1409 && match_user(xp->xp_pattern + 1) >= 1)
1410 {
1411 xp->xp_context = EXPAND_USER;
1412 ++xp->xp_pattern;
1413 }
1414 }
1415}
1416
1417/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001418 * Set the completion context for the :filter command. Returns a pointer to the
1419 * next command after the :filter command.
1420 */
1421 static char_u *
1422set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1423{
1424 if (*arg != NUL)
1425 arg = skip_vimgrep_pat(arg, NULL, NULL);
1426 if (arg == NULL || *arg == NUL)
1427 {
1428 xp->xp_context = EXPAND_NOTHING;
1429 return NULL;
1430 }
1431 return skipwhite(arg);
1432}
1433
1434#ifdef FEAT_SEARCH_EXTRA
1435/*
1436 * Set the completion context for the :match command. Returns a pointer to the
1437 * next command after the :match command.
1438 */
1439 static char_u *
1440set_context_in_match_cmd(expand_T *xp, char_u *arg)
1441{
1442 if (*arg == NUL || !ends_excmd(*arg))
1443 {
1444 // also complete "None"
1445 set_context_in_echohl_cmd(xp, arg);
1446 arg = skipwhite(skiptowhite(arg));
1447 if (*arg != NUL)
1448 {
1449 xp->xp_context = EXPAND_NOTHING;
1450 arg = skip_regexp(arg + 1, *arg, magic_isset());
1451 }
1452 }
1453 return find_nextcmd(arg);
1454}
1455#endif
1456
1457/*
1458 * Returns a pointer to the next command after a :global or a :v command.
1459 * Returns NULL if there is no next command.
1460 */
1461 static char_u *
1462find_cmd_after_global_cmd(char_u *arg)
1463{
1464 int delim;
1465
1466 delim = *arg; // get the delimiter
1467 if (delim)
1468 ++arg; // skip delimiter if there is one
1469
1470 while (arg[0] != NUL && arg[0] != delim)
1471 {
1472 if (arg[0] == '\\' && arg[1] != NUL)
1473 ++arg;
1474 ++arg;
1475 }
1476 if (arg[0] != NUL)
1477 return arg + 1;
1478
1479 return NULL;
1480}
1481
1482/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001483 * Returns a pointer to the next command after a :substitute or a :& command.
1484 * Returns NULL if there is no next command.
1485 */
1486 static char_u *
1487find_cmd_after_substitute_cmd(char_u *arg)
1488{
1489 int delim;
1490
1491 delim = *arg;
1492 if (delim)
1493 {
1494 // skip "from" part
1495 ++arg;
1496 arg = skip_regexp(arg, delim, magic_isset());
1497
1498 if (arg[0] != NUL && arg[0] == delim)
1499 {
1500 // skip "to" part
1501 ++arg;
1502 while (arg[0] != NUL && arg[0] != delim)
1503 {
1504 if (arg[0] == '\\' && arg[1] != NUL)
1505 ++arg;
1506 ++arg;
1507 }
1508 if (arg[0] != NUL) // skip delimiter
1509 ++arg;
1510 }
1511 }
1512 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1513 ++arg;
1514 if (arg[0] != NUL)
1515 return arg;
1516
1517 return NULL;
1518}
1519
1520/*
1521 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1522 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1523 * Returns NULL if there is no next command.
1524 */
1525 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001526find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001527{
1528 arg = skipwhite(skipdigits(arg)); // skip count
1529 if (*arg == '/') // Match regexp, not just whole words
1530 {
1531 for (++arg; *arg && *arg != '/'; arg++)
1532 if (*arg == '\\' && arg[1] != NUL)
1533 arg++;
1534 if (*arg)
1535 {
1536 arg = skipwhite(arg + 1);
1537
1538 // Check for trailing illegal characters
1539 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1540 xp->xp_context = EXPAND_NOTHING;
1541 else
1542 return arg;
1543 }
1544 }
1545
1546 return NULL;
1547}
1548
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001549#ifdef FEAT_EVAL
1550/*
1551 * Set the completion context for the :unlet command. Always returns NULL.
1552 */
1553 static char_u *
1554set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1555{
1556 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1557 arg = xp->xp_pattern + 1;
1558
1559 xp->xp_context = EXPAND_USER_VARS;
1560 xp->xp_pattern = arg;
1561
1562 if (*xp->xp_pattern == '$')
1563 {
1564 xp->xp_context = EXPAND_ENV_VARS;
1565 ++xp->xp_pattern;
1566 }
1567
1568 return NULL;
1569}
1570#endif
1571
1572#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1573/*
1574 * Set the completion context for the :language command. Always returns NULL.
1575 */
1576 static char_u *
1577set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1578{
1579 char_u *p;
1580
1581 p = skiptowhite(arg);
1582 if (*p == NUL)
1583 {
1584 xp->xp_context = EXPAND_LANGUAGE;
1585 xp->xp_pattern = arg;
1586 }
1587 else
1588 {
1589 if ( STRNCMP(arg, "messages", p - arg) == 0
1590 || STRNCMP(arg, "ctype", p - arg) == 0
1591 || STRNCMP(arg, "time", p - arg) == 0
1592 || STRNCMP(arg, "collate", p - arg) == 0)
1593 {
1594 xp->xp_context = EXPAND_LOCALES;
1595 xp->xp_pattern = skipwhite(p);
1596 }
1597 else
1598 xp->xp_context = EXPAND_NOTHING;
1599 }
1600
1601 return NULL;
1602}
1603#endif
1604
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001605#ifdef FEAT_EVAL
1606static enum
1607{
1608 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001609 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
1610 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001611} breakpt_expand_what;
1612
1613/*
1614 * Set the completion context for the :breakadd command. Always returns NULL.
1615 */
1616 static char_u *
1617set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1618{
1619 char_u *p;
1620 char_u *subcmd_start;
1621
1622 xp->xp_context = EXPAND_BREAKPOINT;
1623 xp->xp_pattern = arg;
1624
1625 if (cmdidx == CMD_breakadd)
1626 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001627 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001628 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001629 else
1630 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001631
1632 p = skipwhite(arg);
1633 if (*p == NUL)
1634 return NULL;
1635 subcmd_start = p;
1636
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00001637 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00001638 {
1639 // :breakadd file [lnum] <filename>
1640 // :breakadd func [lnum] <funcname>
1641 p += 4;
1642 p = skipwhite(p);
1643
1644 // skip line number (if specified)
1645 if (VIM_ISDIGIT(*p))
1646 {
1647 p = skipdigits(p);
1648 if (*p != ' ')
1649 {
1650 xp->xp_context = EXPAND_NOTHING;
1651 return NULL;
1652 }
1653 p = skipwhite(p);
1654 }
1655 if (STRNCMP("file", subcmd_start, 4) == 0)
1656 xp->xp_context = EXPAND_FILES;
1657 else
1658 xp->xp_context = EXPAND_USER_FUNC;
1659 xp->xp_pattern = p;
1660 }
1661 else if (STRNCMP("expr ", p, 5) == 0)
1662 {
1663 // :breakadd expr <expression>
1664 xp->xp_context = EXPAND_EXPRESSION;
1665 xp->xp_pattern = skipwhite(p + 5);
1666 }
1667
1668 return NULL;
1669}
1670#endif
1671
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001672/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001673 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1674 * The argument to the command is 'arg' and the argument flags is 'argt'.
1675 * For user-defined commands and for environment variables, 'compl' has the
1676 * completion type.
1677 * Returns a pointer to the next command. Returns NULL if there is no next
1678 * command.
1679 */
1680 static char_u *
1681set_context_by_cmdname(
1682 char_u *cmd,
1683 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001684 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685 char_u *arg,
1686 long argt,
1687 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001688 int forceit)
1689{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001690 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001691 {
1692 case CMD_find:
1693 case CMD_sfind:
1694 case CMD_tabfind:
1695 if (xp->xp_context == EXPAND_FILES)
1696 xp->xp_context = EXPAND_FILES_IN_PATH;
1697 break;
1698 case CMD_cd:
1699 case CMD_chdir:
1700 case CMD_tcd:
1701 case CMD_tchdir:
1702 case CMD_lcd:
1703 case CMD_lchdir:
1704 if (xp->xp_context == EXPAND_FILES)
1705 xp->xp_context = EXPAND_DIRECTORIES;
1706 break;
1707 case CMD_help:
1708 xp->xp_context = EXPAND_HELP;
1709 xp->xp_pattern = arg;
1710 break;
1711
1712 // Command modifiers: return the argument.
1713 // Also for commands with an argument that is a command.
1714 case CMD_aboveleft:
1715 case CMD_argdo:
1716 case CMD_belowright:
1717 case CMD_botright:
1718 case CMD_browse:
1719 case CMD_bufdo:
1720 case CMD_cdo:
1721 case CMD_cfdo:
1722 case CMD_confirm:
1723 case CMD_debug:
1724 case CMD_folddoclosed:
1725 case CMD_folddoopen:
1726 case CMD_hide:
1727 case CMD_keepalt:
1728 case CMD_keepjumps:
1729 case CMD_keepmarks:
1730 case CMD_keeppatterns:
1731 case CMD_ldo:
1732 case CMD_leftabove:
1733 case CMD_lfdo:
1734 case CMD_lockmarks:
1735 case CMD_noautocmd:
1736 case CMD_noswapfile:
1737 case CMD_rightbelow:
1738 case CMD_sandbox:
1739 case CMD_silent:
1740 case CMD_tab:
1741 case CMD_tabdo:
1742 case CMD_topleft:
1743 case CMD_verbose:
1744 case CMD_vertical:
1745 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001746 case CMD_vim9cmd:
1747 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001748 return arg;
1749
1750 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001751 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001752
1753#ifdef FEAT_SEARCH_EXTRA
1754 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001755 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001756#endif
1757
1758 // All completion for the +cmdline_compl feature goes here.
1759
1760 case CMD_command:
1761 return set_context_in_user_cmd(xp, arg);
1762
1763 case CMD_delcommand:
1764 xp->xp_context = EXPAND_USER_COMMANDS;
1765 xp->xp_pattern = arg;
1766 break;
1767
1768 case CMD_global:
1769 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001770 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001771 case CMD_and:
1772 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001773 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001774 case CMD_isearch:
1775 case CMD_dsearch:
1776 case CMD_ilist:
1777 case CMD_dlist:
1778 case CMD_ijump:
1779 case CMD_psearch:
1780 case CMD_djump:
1781 case CMD_isplit:
1782 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001783 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001784 case CMD_autocmd:
1785 return set_context_in_autocmd(xp, arg, FALSE);
1786 case CMD_doautocmd:
1787 case CMD_doautoall:
1788 return set_context_in_autocmd(xp, arg, TRUE);
1789 case CMD_set:
1790 set_context_in_set_cmd(xp, arg, 0);
1791 break;
1792 case CMD_setglobal:
1793 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1794 break;
1795 case CMD_setlocal:
1796 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1797 break;
1798 case CMD_tag:
1799 case CMD_stag:
1800 case CMD_ptag:
1801 case CMD_ltag:
1802 case CMD_tselect:
1803 case CMD_stselect:
1804 case CMD_ptselect:
1805 case CMD_tjump:
1806 case CMD_stjump:
1807 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001808 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001809 xp->xp_context = EXPAND_TAGS_LISTFILES;
1810 else
1811 xp->xp_context = EXPAND_TAGS;
1812 xp->xp_pattern = arg;
1813 break;
1814 case CMD_augroup:
1815 xp->xp_context = EXPAND_AUGROUP;
1816 xp->xp_pattern = arg;
1817 break;
1818#ifdef FEAT_SYN_HL
1819 case CMD_syntax:
1820 set_context_in_syntax_cmd(xp, arg);
1821 break;
1822#endif
1823#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001824 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001825 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001826 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001827 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001828 case CMD_if:
1829 case CMD_elseif:
1830 case CMD_while:
1831 case CMD_for:
1832 case CMD_echo:
1833 case CMD_echon:
1834 case CMD_execute:
1835 case CMD_echomsg:
1836 case CMD_echoerr:
1837 case CMD_call:
1838 case CMD_return:
1839 case CMD_cexpr:
1840 case CMD_caddexpr:
1841 case CMD_cgetexpr:
1842 case CMD_lexpr:
1843 case CMD_laddexpr:
1844 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001845 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001846 break;
1847
1848 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001849 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001850 case CMD_function:
1851 case CMD_delfunction:
1852 xp->xp_context = EXPAND_USER_FUNC;
1853 xp->xp_pattern = arg;
1854 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001855 case CMD_disassemble:
1856 set_context_in_disassemble_cmd(xp, arg);
1857 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001858
1859 case CMD_echohl:
1860 set_context_in_echohl_cmd(xp, arg);
1861 break;
1862#endif
1863 case CMD_highlight:
1864 set_context_in_highlight_cmd(xp, arg);
1865 break;
1866#ifdef FEAT_CSCOPE
1867 case CMD_cscope:
1868 case CMD_lcscope:
1869 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001870 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001871 break;
1872#endif
1873#ifdef FEAT_SIGNS
1874 case CMD_sign:
1875 set_context_in_sign_cmd(xp, arg);
1876 break;
1877#endif
1878 case CMD_bdelete:
1879 case CMD_bwipeout:
1880 case CMD_bunload:
1881 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1882 arg = xp->xp_pattern + 1;
1883 // FALLTHROUGH
1884 case CMD_buffer:
1885 case CMD_sbuffer:
1886 case CMD_checktime:
1887 xp->xp_context = EXPAND_BUFFERS;
1888 xp->xp_pattern = arg;
1889 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001890#ifdef FEAT_DIFF
1891 case CMD_diffget:
1892 case CMD_diffput:
1893 // If current buffer is in diff mode, complete buffer names
1894 // which are in diff mode, and different than current buffer.
1895 xp->xp_context = EXPAND_DIFF_BUFFERS;
1896 xp->xp_pattern = arg;
1897 break;
1898#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001899 case CMD_USER:
1900 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001901 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1902 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001903
1904 case CMD_map: case CMD_noremap:
1905 case CMD_nmap: case CMD_nnoremap:
1906 case CMD_vmap: case CMD_vnoremap:
1907 case CMD_omap: case CMD_onoremap:
1908 case CMD_imap: case CMD_inoremap:
1909 case CMD_cmap: case CMD_cnoremap:
1910 case CMD_lmap: case CMD_lnoremap:
1911 case CMD_smap: case CMD_snoremap:
1912 case CMD_tmap: case CMD_tnoremap:
1913 case CMD_xmap: case CMD_xnoremap:
1914 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001915 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001916 case CMD_unmap:
1917 case CMD_nunmap:
1918 case CMD_vunmap:
1919 case CMD_ounmap:
1920 case CMD_iunmap:
1921 case CMD_cunmap:
1922 case CMD_lunmap:
1923 case CMD_sunmap:
1924 case CMD_tunmap:
1925 case CMD_xunmap:
1926 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001927 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001928 case CMD_mapclear:
1929 case CMD_nmapclear:
1930 case CMD_vmapclear:
1931 case CMD_omapclear:
1932 case CMD_imapclear:
1933 case CMD_cmapclear:
1934 case CMD_lmapclear:
1935 case CMD_smapclear:
1936 case CMD_tmapclear:
1937 case CMD_xmapclear:
1938 xp->xp_context = EXPAND_MAPCLEAR;
1939 xp->xp_pattern = arg;
1940 break;
1941
1942 case CMD_abbreviate: case CMD_noreabbrev:
1943 case CMD_cabbrev: case CMD_cnoreabbrev:
1944 case CMD_iabbrev: case CMD_inoreabbrev:
1945 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001946 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001947 case CMD_unabbreviate:
1948 case CMD_cunabbrev:
1949 case CMD_iunabbrev:
1950 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001951 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001952#ifdef FEAT_MENU
1953 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1954 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1955 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1956 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1957 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1958 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1959 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1960 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1961 case CMD_tmenu: case CMD_tunmenu:
1962 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1963 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1964#endif
1965
1966 case CMD_colorscheme:
1967 xp->xp_context = EXPAND_COLORS;
1968 xp->xp_pattern = arg;
1969 break;
1970
1971 case CMD_compiler:
1972 xp->xp_context = EXPAND_COMPILER;
1973 xp->xp_pattern = arg;
1974 break;
1975
1976 case CMD_ownsyntax:
1977 xp->xp_context = EXPAND_OWNSYNTAX;
1978 xp->xp_pattern = arg;
1979 break;
1980
1981 case CMD_setfiletype:
1982 xp->xp_context = EXPAND_FILETYPE;
1983 xp->xp_pattern = arg;
1984 break;
1985
1986 case CMD_packadd:
1987 xp->xp_context = EXPAND_PACKADD;
1988 xp->xp_pattern = arg;
1989 break;
1990
1991#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1992 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001993 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001994#endif
1995#if defined(FEAT_PROFILE)
1996 case CMD_profile:
1997 set_context_in_profile_cmd(xp, arg);
1998 break;
1999#endif
2000 case CMD_behave:
2001 xp->xp_context = EXPAND_BEHAVE;
2002 xp->xp_pattern = arg;
2003 break;
2004
2005 case CMD_messages:
2006 xp->xp_context = EXPAND_MESSAGES;
2007 xp->xp_pattern = arg;
2008 break;
2009
2010 case CMD_history:
2011 xp->xp_context = EXPAND_HISTORY;
2012 xp->xp_pattern = arg;
2013 break;
2014#if defined(FEAT_PROFILE)
2015 case CMD_syntime:
2016 xp->xp_context = EXPAND_SYNTIME;
2017 xp->xp_pattern = arg;
2018 break;
2019#endif
2020
2021 case CMD_argdelete:
2022 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2023 arg = xp->xp_pattern + 1;
2024 xp->xp_context = EXPAND_ARGLIST;
2025 xp->xp_pattern = arg;
2026 break;
2027
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002028#ifdef FEAT_EVAL
2029 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002030 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002031 case CMD_breakdel:
2032 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
2033#endif
2034
Bram Moolenaard0190392019-08-23 21:17:35 +02002035 default:
2036 break;
2037 }
2038 return NULL;
2039}
2040
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002041/*
2042 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2043 * we don't need/want deleted. Maybe this could be done better if we didn't
2044 * repeat all this stuff. The only problem is that they may not stay
2045 * perfectly compatible with each other, but then the command line syntax
2046 * probably won't change that much -- webb.
2047 */
2048 static char_u *
2049set_one_cmd_context(
2050 expand_T *xp,
2051 char_u *buff) // buffer for command string
2052{
2053 char_u *p;
2054 char_u *cmd, *arg;
2055 int len = 0;
2056 exarg_T ea;
2057 int compl = EXPAND_NOTHING;
2058 int forceit = FALSE;
2059 int usefilter = FALSE; // filter instead of file name
2060
2061 ExpandInit(xp);
2062 xp->xp_pattern = buff;
2063 xp->xp_line = buff;
2064 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2065 ea.argt = 0;
2066
2067 // 1. skip comment lines and leading space, colons or bars
2068 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2069 ;
2070 xp->xp_pattern = cmd;
2071
2072 if (*cmd == NUL)
2073 return NULL;
2074 if (*cmd == '"') // ignore comment lines
2075 {
2076 xp->xp_context = EXPAND_NOTHING;
2077 return NULL;
2078 }
2079
2080 // 3. Skip over the range to find the command.
2081 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2082 xp->xp_pattern = cmd;
2083 if (*cmd == NUL)
2084 return NULL;
2085 if (*cmd == '"')
2086 {
2087 xp->xp_context = EXPAND_NOTHING;
2088 return NULL;
2089 }
2090
2091 if (*cmd == '|' || *cmd == '\n')
2092 return cmd + 1; // There's another command
2093
2094 // Get the command index.
2095 p = set_cmd_index(cmd, &ea, xp, &compl);
2096 if (p == NULL)
2097 return NULL;
2098
2099 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2100
2101 if (*p == '!') // forced commands
2102 {
2103 forceit = TRUE;
2104 ++p;
2105 }
2106
2107 // 6. parse arguments
2108 if (!IS_USER_CMDIDX(ea.cmdidx))
2109 ea.argt = excmd_get_argt(ea.cmdidx);
2110
2111 arg = skipwhite(p);
2112
2113 // Skip over ++argopt argument
2114 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2115 {
2116 p = arg;
2117 while (*p && !vim_isspace(*p))
2118 MB_PTR_ADV(p);
2119 arg = skipwhite(p);
2120 }
2121
2122 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2123 {
2124 if (*arg == '>') // append
2125 {
2126 if (*++arg == '>')
2127 ++arg;
2128 arg = skipwhite(arg);
2129 }
2130 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2131 {
2132 ++arg;
2133 usefilter = TRUE;
2134 }
2135 }
2136
2137 if (ea.cmdidx == CMD_read)
2138 {
2139 usefilter = forceit; // :r! filter if forced
2140 if (*arg == '!') // :r !filter
2141 {
2142 ++arg;
2143 usefilter = TRUE;
2144 }
2145 }
2146
2147 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2148 {
2149 while (*arg == *cmd) // allow any number of '>' or '<'
2150 ++arg;
2151 arg = skipwhite(arg);
2152 }
2153
2154 // Does command allow "+command"?
2155 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2156 {
2157 // Check if we're in the +command
2158 p = arg + 1;
2159 arg = skip_cmd_arg(arg, FALSE);
2160
2161 // Still touching the command after '+'?
2162 if (*arg == NUL)
2163 return p;
2164
2165 // Skip space(s) after +command to get to the real argument
2166 arg = skipwhite(arg);
2167 }
2168
2169
2170 // Check for '|' to separate commands and '"' to start comments.
2171 // Don't do this for ":read !cmd" and ":write !cmd".
2172 if ((ea.argt & EX_TRLBAR) && !usefilter)
2173 {
2174 p = arg;
2175 // ":redir @" is not the start of a comment
2176 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2177 p += 2;
2178 while (*p)
2179 {
2180 if (*p == Ctrl_V)
2181 {
2182 if (p[1] != NUL)
2183 ++p;
2184 }
2185 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2186 || *p == '|' || *p == '\n')
2187 {
2188 if (*(p - 1) != '\\')
2189 {
2190 if (*p == '|' || *p == '\n')
2191 return p + 1;
2192 return NULL; // It's a comment
2193 }
2194 }
2195 MB_PTR_ADV(p);
2196 }
2197 }
2198
2199 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2200 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2201 // no arguments allowed but there is something
2202 return NULL;
2203
2204 // Find start of last argument (argument just before cursor):
2205 p = buff;
2206 xp->xp_pattern = p;
2207 len = (int)STRLEN(buff);
2208 while (*p && p < buff + len)
2209 {
2210 if (*p == ' ' || *p == TAB)
2211 {
2212 // argument starts after a space
2213 xp->xp_pattern = ++p;
2214 }
2215 else
2216 {
2217 if (*p == '\\' && *(p + 1) != NUL)
2218 ++p; // skip over escaped character
2219 MB_PTR_ADV(p);
2220 }
2221 }
2222
2223 if (ea.argt & EX_XFILE)
2224 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2225
2226 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002227 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002228 forceit);
2229}
2230
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002231/*
2232 * Set the completion context in 'xp' for command 'str'
2233 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002234 void
2235set_cmd_context(
2236 expand_T *xp,
2237 char_u *str, // start of command line
2238 int len, // length of command line (excl. NUL)
2239 int col, // position of cursor
2240 int use_ccline UNUSED) // use ccline for info
2241{
2242#ifdef FEAT_EVAL
2243 cmdline_info_T *ccline = get_cmdline_info();
2244#endif
2245 int old_char = NUL;
2246 char_u *nextcomm;
2247
2248 // Avoid a UMR warning from Purify, only save the character if it has been
2249 // written before.
2250 if (col < len)
2251 old_char = str[col];
2252 str[col] = NUL;
2253 nextcomm = str;
2254
2255#ifdef FEAT_EVAL
2256 if (use_ccline && ccline->cmdfirstc == '=')
2257 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002258 // pass CMD_SIZE because there is no real command
2259 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002260 }
2261 else if (use_ccline && ccline->input_fn)
2262 {
2263 xp->xp_context = ccline->xp_context;
2264 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002265 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002266 }
2267 else
2268#endif
2269 while (nextcomm != NULL)
2270 nextcomm = set_one_cmd_context(xp, nextcomm);
2271
2272 // Store the string here so that call_user_expand_func() can get to them
2273 // easily.
2274 xp->xp_line = str;
2275 xp->xp_col = col;
2276
2277 str[col] = old_char;
2278}
2279
2280/*
2281 * Expand the command line "str" from context "xp".
2282 * "xp" must have been set by set_cmd_context().
2283 * xp->xp_pattern points into "str", to where the text that is to be expanded
2284 * starts.
2285 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2286 * cursor.
2287 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2288 * key that triggered expansion literally.
2289 * Returns EXPAND_OK otherwise.
2290 */
2291 int
2292expand_cmdline(
2293 expand_T *xp,
2294 char_u *str, // start of command line
2295 int col, // position of cursor
2296 int *matchcount, // return: nr of matches
2297 char_u ***matches) // return: array of pointers to matches
2298{
2299 char_u *file_str = NULL;
2300 int options = WILD_ADD_SLASH|WILD_SILENT;
2301
2302 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2303 {
2304 beep_flush();
2305 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2306 }
2307 if (xp->xp_context == EXPAND_NOTHING)
2308 {
2309 // Caller can use the character as a normal char instead
2310 return EXPAND_NOTHING;
2311 }
2312
2313 // add star to file name, or convert to regexp if not exp. files.
2314 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002315 if (cmdline_fuzzy_completion_supported(xp))
2316 // If fuzzy matching, don't modify the search string
2317 file_str = vim_strsave(xp->xp_pattern);
2318 else
2319 {
2320 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2321 if (file_str == NULL)
2322 return EXPAND_UNSUCCESSFUL;
2323 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002324
2325 if (p_wic)
2326 options += WILD_ICASE;
2327
2328 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002329 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002330 {
2331 *matchcount = 0;
2332 *matches = NULL;
2333 }
2334 vim_free(file_str);
2335
2336 return EXPAND_OK;
2337}
2338
Bram Moolenaar66b51422019-08-18 21:44:12 +02002339/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002340 * Expand file or directory names.
2341 */
2342 static int
2343expand_files_and_dirs(
2344 expand_T *xp,
2345 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002346 char_u ***matches,
2347 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002348 int flags,
2349 int options)
2350{
2351 int free_pat = FALSE;
2352 int i;
2353 int ret;
2354
2355 // for ":set path=" and ":set tags=" halve backslashes for escaped
2356 // space
2357 if (xp->xp_backslash != XP_BS_NONE)
2358 {
2359 free_pat = TRUE;
2360 pat = vim_strsave(pat);
2361 for (i = 0; pat[i]; ++i)
2362 if (pat[i] == '\\')
2363 {
2364 if (xp->xp_backslash == XP_BS_THREE
2365 && pat[i + 1] == '\\'
2366 && pat[i + 2] == '\\'
2367 && pat[i + 3] == ' ')
2368 STRMOVE(pat + i, pat + i + 3);
2369 if (xp->xp_backslash == XP_BS_ONE
2370 && pat[i + 1] == ' ')
2371 STRMOVE(pat + i, pat + i + 1);
2372 }
2373 }
2374
2375 if (xp->xp_context == EXPAND_FILES)
2376 flags |= EW_FILE;
2377 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2378 flags |= (EW_FILE | EW_PATH);
2379 else
2380 flags = (flags | EW_DIR) & ~EW_FILE;
2381 if (options & WILD_ICASE)
2382 flags |= EW_ICASE;
2383
2384 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002385 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002386 if (free_pat)
2387 vim_free(pat);
2388#ifdef BACKSLASH_IN_FILENAME
2389 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2390 {
2391 int j;
2392
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002393 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002394 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002395 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002396
2397 while (*ptr != NUL)
2398 {
2399 if (p_csl[0] == 's' && *ptr == '\\')
2400 *ptr = '/';
2401 else if (p_csl[0] == 'b' && *ptr == '/')
2402 *ptr = '\\';
2403 ptr += (*mb_ptr2len)(ptr);
2404 }
2405 }
2406 }
2407#endif
2408 return ret;
2409}
2410
2411/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002412 * Function given to ExpandGeneric() to obtain the possible arguments of the
2413 * ":behave {mswin,xterm}" command.
2414 */
2415 static char_u *
2416get_behave_arg(expand_T *xp UNUSED, int idx)
2417{
2418 if (idx == 0)
2419 return (char_u *)"mswin";
2420 if (idx == 1)
2421 return (char_u *)"xterm";
2422 return NULL;
2423}
2424
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002425# ifdef FEAT_EVAL
2426/*
2427 * Function given to ExpandGeneric() to obtain the possible arguments of the
2428 * ":breakadd {expr, file, func, here}" command.
2429 * ":breakdel {func, file, here}" command.
2430 */
2431 static char_u *
2432get_breakadd_arg(expand_T *xp UNUSED, int idx)
2433{
2434 char *opts[] = {"expr", "file", "func", "here"};
2435
2436 if (idx >=0 && idx <= 3)
2437 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002438 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002439 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2440 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002441 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2442 {
2443 // breakdel {func, file, here}
2444 if (idx <= 2)
2445 return (char_u *)opts[idx + 1];
2446 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002447 else
2448 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002449 // profdel {func, file}
2450 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002451 return (char_u *)opts[idx + 1];
2452 }
2453 }
2454 return NULL;
2455}
2456#endif
2457
Bram Moolenaard0190392019-08-23 21:17:35 +02002458/*
2459 * Function given to ExpandGeneric() to obtain the possible arguments of the
2460 * ":messages {clear}" command.
2461 */
2462 static char_u *
2463get_messages_arg(expand_T *xp UNUSED, int idx)
2464{
2465 if (idx == 0)
2466 return (char_u *)"clear";
2467 return NULL;
2468}
2469
2470 static char_u *
2471get_mapclear_arg(expand_T *xp UNUSED, int idx)
2472{
2473 if (idx == 0)
2474 return (char_u *)"<buffer>";
2475 return NULL;
2476}
2477
2478/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002479 * Do the expansion based on xp->xp_context and 'rmp'.
2480 */
2481 static int
2482ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002483 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002484 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002485 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002486 char_u ***matches,
2487 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002488{
2489 static struct expgen
2490 {
2491 int context;
2492 char_u *((*func)(expand_T *, int));
2493 int ic;
2494 int escaped;
2495 } tab[] =
2496 {
2497 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2498 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2499 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2500 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2501 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2502 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2503 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2504 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2505 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2506 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002507#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002508 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2509 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2510 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2511 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2512 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002513#endif
2514#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002515 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2516 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002517#endif
2518#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002519 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002520#endif
2521#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002522 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002523#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002524 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2525 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2526 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002527#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002528 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002529#endif
2530#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002531 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002532#endif
2533#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002534 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002535#endif
2536#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002537 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2538 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002539#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002540 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2541 {EXPAND_USER, get_users, TRUE, FALSE},
2542 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002543#ifdef FEAT_EVAL
2544 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
2545#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002546 };
2547 int i;
2548 int ret = FAIL;
2549
2550 // Find a context in the table and call the ExpandGeneric() with the
2551 // right function to do the expansion.
2552 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2553 {
2554 if (xp->xp_context == tab[i].context)
2555 {
2556 if (tab[i].ic)
2557 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002558 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2559 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002560 break;
2561 }
2562 }
2563
2564 return ret;
2565}
2566
2567/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002568 * Map wild expand options to flags for expand_wildcards()
2569 */
2570 static int
2571map_wildopts_to_ewflags(int options)
2572{
2573 int flags;
2574
2575 flags = EW_DIR; // include directories
2576 if (options & WILD_LIST_NOTFOUND)
2577 flags |= EW_NOTFOUND;
2578 if (options & WILD_ADD_SLASH)
2579 flags |= EW_ADDSLASH;
2580 if (options & WILD_KEEP_ALL)
2581 flags |= EW_KEEPALL;
2582 if (options & WILD_SILENT)
2583 flags |= EW_SILENT;
2584 if (options & WILD_NOERROR)
2585 flags |= EW_NOERROR;
2586 if (options & WILD_ALLLINKS)
2587 flags |= EW_ALLLINKS;
2588
2589 return flags;
2590}
2591
2592/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002593 * Do the expansion based on xp->xp_context and "pat".
2594 */
2595 static int
2596ExpandFromContext(
2597 expand_T *xp,
2598 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002599 char_u ***matches,
2600 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002601 int options) // WILD_ flags
2602{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002603 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002604 int ret;
2605 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002606 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002607 int fuzzy = cmdline_fuzzy_complete(pat)
2608 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002609
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002610 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002611
2612 if (xp->xp_context == EXPAND_FILES
2613 || xp->xp_context == EXPAND_DIRECTORIES
2614 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002615 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2616 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002617
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002618 *matches = (char_u **)"";
2619 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002620 if (xp->xp_context == EXPAND_HELP)
2621 {
2622 // With an empty argument we would get all the help tags, which is
2623 // very slow. Get matches for "help" instead.
2624 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002625 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002626 {
2627#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002628 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002629#endif
2630 return OK;
2631 }
2632 return FAIL;
2633 }
2634
Bram Moolenaar66b51422019-08-18 21:44:12 +02002635 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002636 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002637 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002638 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002639 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002640 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002641#ifdef FEAT_DIFF
2642 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002643 return ExpandBufnames(pat, numMatches, matches,
2644 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002645#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002646 if (xp->xp_context == EXPAND_TAGS
2647 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002648 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2649 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002650 if (xp->xp_context == EXPAND_COLORS)
2651 {
2652 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002653 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002654 directories);
2655 }
2656 if (xp->xp_context == EXPAND_COMPILER)
2657 {
2658 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002659 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002660 }
2661 if (xp->xp_context == EXPAND_OWNSYNTAX)
2662 {
2663 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002664 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002665 }
2666 if (xp->xp_context == EXPAND_FILETYPE)
2667 {
2668 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002669 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002670 }
2671# if defined(FEAT_EVAL)
2672 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002673 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002674# endif
2675 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002676 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002677
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002678 // When expanding a function name starting with s:, match the <SNR>nr_
2679 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002680 if ((xp->xp_context == EXPAND_USER_FUNC
2681 || xp->xp_context == EXPAND_DISASSEMBLE)
2682 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002683 {
2684 int len = (int)STRLEN(pat) + 20;
2685
2686 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002687 if (tofree == NULL)
2688 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002689 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002690 pat = tofree;
2691 }
2692
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002693 if (!fuzzy)
2694 {
2695 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2696 if (regmatch.regprog == NULL)
2697 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002698
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002699 // set ignore-case according to p_ic, p_scs and pat
2700 regmatch.rm_ic = ignorecase(pat);
2701 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002702
2703 if (xp->xp_context == EXPAND_SETTINGS
2704 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002705 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002706 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002707 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002708# if defined(FEAT_EVAL)
2709 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002710 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002711# endif
2712 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002713 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002714
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002715 if (!fuzzy)
2716 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002717 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002718
2719 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002720}
2721
Bram Moolenaar66b51422019-08-18 21:44:12 +02002722/*
2723 * Expand a list of names.
2724 *
2725 * Generic function for command line completion. It calls a function to
2726 * obtain strings, one by one. The strings are matched against a regexp
2727 * program. Matching strings are copied into an array, which is returned.
2728 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002729 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2730 * is used.
2731 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002732 * Returns OK when no problems encountered, FAIL for error (out of memory).
2733 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002734 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002735ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002736 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002737 expand_T *xp,
2738 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002739 char_u ***matches,
2740 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002741 char_u *((*func)(expand_T *, int)),
2742 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002743 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002744{
2745 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002746 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002747 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002748 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002749 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002750 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002751 int match;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002752
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002753 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002754 *matches = NULL;
2755 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002756
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002757 if (!fuzzy)
2758 ga_init2(&ga, sizeof(char *), 30);
2759 else
2760 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2761
2762 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002763 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002764 str = (*func)(xp, i);
2765 if (str == NULL) // end of list
2766 break;
2767 if (*str == NUL) // skip empty strings
2768 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002769
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002770 if (xp->xp_pattern[0] != NUL)
2771 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002772 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002773 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002774 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002775 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002776 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002777 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002778 }
2779 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002780 else
2781 match = TRUE;
2782
2783 if (!match)
2784 continue;
2785
2786 if (escaped)
2787 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2788 else
2789 str = vim_strsave(str);
2790 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002791 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002792 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002793 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002794 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002795 return FAIL;
2796 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002797
2798 for (i = 0; i < ga.ga_len; ++i)
2799 {
2800 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[i];
2801 vim_free(fuzmatch->str);
2802 }
2803 ga_clear(&ga);
2804 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002805 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002806
2807 if (ga_grow(&ga, 1) == FAIL)
2808 {
2809 vim_free(str);
2810 break;
2811 }
2812
2813 if (fuzzy)
2814 {
2815 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2816 fuzmatch->idx = ga.ga_len;
2817 fuzmatch->str = str;
2818 fuzmatch->score = score;
2819 }
2820 else
2821 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2822
2823# ifdef FEAT_MENU
2824 if (func == get_menu_names)
2825 {
2826 // test for separator added by get_menu_names()
2827 str += STRLEN(str) - 1;
2828 if (*str == '\001')
2829 *str = '.';
2830 }
2831# endif
2832
2833 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002834 }
2835
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002836 if (ga.ga_len == 0)
2837 return OK;
2838
Bram Moolenaar66b51422019-08-18 21:44:12 +02002839 // Sort the results. Keep menu's in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002840 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
2841 && xp->xp_context != EXPAND_MENUS)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002842 {
2843 if (xp->xp_context == EXPAND_EXPRESSION
2844 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002845 || xp->xp_context == EXPAND_USER_FUNC
2846 || xp->xp_context == EXPAND_DISASSEMBLE)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002847 // <SNR> functions should be sorted to the end.
2848 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2849 sort_func_compare);
2850 else
2851 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2852 }
2853
2854 if (!fuzzy)
2855 {
2856 *matches = ga.ga_data;
2857 *numMatches = ga.ga_len;
2858 }
2859 else
2860 {
2861 int funcsort = FALSE;
2862
2863 if (xp->xp_context == EXPAND_EXPRESSION
2864 || xp->xp_context == EXPAND_FUNCTIONS
2865 || xp->xp_context == EXPAND_USER_FUNC
2866 || xp->xp_context == EXPAND_DISASSEMBLE)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002867 // <SNR> functions should be sorted to the end.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002868 funcsort = TRUE;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002869
2870 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2871 funcsort) == FAIL)
2872 return FAIL;
2873 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002874 }
2875
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002876#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002877 // Reset the variables used for special highlight names expansion, so that
2878 // they don't show up when getting normal highlight names by ID.
2879 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002880#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002881
Bram Moolenaar66b51422019-08-18 21:44:12 +02002882 return OK;
2883}
2884
2885/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002886 * Expand shell command matches in one directory of $PATH.
2887 */
2888 static void
2889expand_shellcmd_onedir(
2890 char_u *buf,
2891 char_u *s,
2892 size_t l,
2893 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002894 char_u ***matches,
2895 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002896 int flags,
2897 hashtab_T *ht,
2898 garray_T *gap)
2899{
2900 int ret;
2901 int i;
2902 hash_T hash;
2903 hashitem_T *hi;
2904
2905 vim_strncpy(buf, s, l);
2906 add_pathsep(buf);
2907 l = STRLEN(buf);
2908 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2909
2910 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002911 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002912 if (ret == OK)
2913 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002914 if (ga_grow(gap, *numMatches) == FAIL)
2915 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002916 else
2917 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002918 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002919 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002920 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002921
2922 if (STRLEN(name) > l)
2923 {
2924 // Check if this name was already found.
2925 hash = hash_hash(name + l);
2926 hi = hash_lookup(ht, name + l, hash);
2927 if (HASHITEM_EMPTY(hi))
2928 {
2929 // Remove the path that was prepended.
2930 STRMOVE(name, name + l);
2931 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2932 hash_add_item(ht, hi, name, hash);
2933 name = NULL;
2934 }
2935 }
2936 vim_free(name);
2937 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002938 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002939 }
2940 }
2941}
2942
2943/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002944 * Complete a shell command.
2945 * Returns FAIL or OK;
2946 */
2947 static int
2948expand_shellcmd(
2949 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002950 char_u ***matches, // return: array with matches
2951 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02002952 int flagsarg) // EW_ flags
2953{
2954 char_u *pat;
2955 int i;
2956 char_u *path = NULL;
2957 int mustfree = FALSE;
2958 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002959 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002960 size_t l;
2961 char_u *s, *e;
2962 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002963 int did_curdir = FALSE;
2964 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002965
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002966 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002967 if (buf == NULL)
2968 return FAIL;
2969
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002970 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002971 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002972 if (pat == NULL)
2973 {
2974 vim_free(buf);
2975 return FAIL;
2976 }
2977
Bram Moolenaar66b51422019-08-18 21:44:12 +02002978 for (i = 0; pat[i]; ++i)
2979 if (pat[i] == '\\' && pat[i + 1] == ' ')
2980 STRMOVE(pat + i, pat + i + 1);
2981
2982 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2983
2984 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2985 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2986 path = (char_u *)".";
2987 else
2988 {
2989 // For an absolute name we don't use $PATH.
2990 if (!mch_isFullName(pat))
2991 path = vim_getenv((char_u *)"PATH", &mustfree);
2992 if (path == NULL)
2993 path = (char_u *)"";
2994 }
2995
2996 // Go over all directories in $PATH. Expand matches in that directory and
2997 // collect them in "ga". When "." is not in $PATH also expand for the
2998 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002999 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003000 hash_init(&found_ht);
3001 for (s = path; ; s = e)
3002 {
3003# if defined(MSWIN)
3004 e = vim_strchr(s, ';');
3005# else
3006 e = vim_strchr(s, ':');
3007# endif
3008 if (e == NULL)
3009 e = s + STRLEN(s);
3010
3011 if (*s == NUL)
3012 {
3013 if (did_curdir)
3014 break;
3015 // Find directories in the current directory, path is empty.
3016 did_curdir = TRUE;
3017 flags |= EW_DIR;
3018 }
3019 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3020 {
3021 did_curdir = TRUE;
3022 flags |= EW_DIR;
3023 }
3024 else
3025 // Do not match directories inside a $PATH item.
3026 flags &= ~EW_DIR;
3027
3028 l = e - s;
3029 if (l > MAXPATHL - 5)
3030 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003031
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003032 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003033 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003034
Bram Moolenaar66b51422019-08-18 21:44:12 +02003035 if (*e != NUL)
3036 ++e;
3037 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003038 *matches = ga.ga_data;
3039 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003040
3041 vim_free(buf);
3042 vim_free(pat);
3043 if (mustfree)
3044 vim_free(path);
3045 hash_clear(&found_ht);
3046 return OK;
3047}
3048
3049# if defined(FEAT_EVAL)
3050/*
3051 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003052 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003053 */
3054 static void *
3055call_user_expand_func(
3056 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003057 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003058{
3059 cmdline_info_T *ccline = get_cmdline_info();
3060 int keep = 0;
3061 typval_T args[4];
3062 sctx_T save_current_sctx = current_sctx;
3063 char_u *pat = NULL;
3064 void *ret;
3065
3066 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3067 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003068
3069 if (ccline->cmdbuff != NULL)
3070 {
3071 keep = ccline->cmdbuff[ccline->cmdlen];
3072 ccline->cmdbuff[ccline->cmdlen] = 0;
3073 }
3074
3075 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3076
3077 args[0].v_type = VAR_STRING;
3078 args[0].vval.v_string = pat;
3079 args[1].v_type = VAR_STRING;
3080 args[1].vval.v_string = xp->xp_line;
3081 args[2].v_type = VAR_NUMBER;
3082 args[2].vval.v_number = xp->xp_col;
3083 args[3].v_type = VAR_UNKNOWN;
3084
3085 current_sctx = xp->xp_script_ctx;
3086
3087 ret = user_expand_func(xp->xp_arg, 3, args);
3088
3089 current_sctx = save_current_sctx;
3090 if (ccline->cmdbuff != NULL)
3091 ccline->cmdbuff[ccline->cmdlen] = keep;
3092
3093 vim_free(pat);
3094 return ret;
3095}
3096
3097/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003098 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3099 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003100 */
3101 static int
3102ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003103 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104 expand_T *xp,
3105 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003106 char_u ***matches,
3107 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003108{
3109 char_u *retstr;
3110 char_u *s;
3111 char_u *e;
3112 int keep;
3113 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003114 int fuzzy;
3115 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003116 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003117
3118 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003119 *matches = NULL;
3120 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003121
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003122 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003123 if (retstr == NULL)
3124 return FAIL;
3125
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003126 if (!fuzzy)
3127 ga_init2(&ga, sizeof(char *), 3);
3128 else
3129 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3130
Bram Moolenaar66b51422019-08-18 21:44:12 +02003131 for (s = retstr; *s != NUL; s = e)
3132 {
3133 e = vim_strchr(s, '\n');
3134 if (e == NULL)
3135 e = s + STRLEN(s);
3136 keep = *e;
3137 *e = NUL;
3138
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003139 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003140 {
3141 if (!fuzzy)
3142 match = vim_regexec(regmatch, s, (colnr_T)0);
3143 else
3144 {
3145 score = fuzzy_match_str(s, pat);
3146 match = (score != 0);
3147 }
3148 }
3149 else
3150 match = TRUE; // match everything
3151
Bram Moolenaar66b51422019-08-18 21:44:12 +02003152 *e = keep;
3153
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003154 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003155 {
3156 if (ga_grow(&ga, 1) == FAIL)
3157 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003158 if (!fuzzy)
3159 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3160 else
3161 {
3162 fuzmatch_str_T *fuzmatch =
3163 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003164 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003165 fuzmatch->str = vim_strnsave(s, e - s);
3166 fuzmatch->score = score;
3167 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003168 ++ga.ga_len;
3169 }
3170
3171 if (*e != NUL)
3172 ++e;
3173 }
3174 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003175
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003176 if (ga.ga_len == 0)
3177 return OK;
3178
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003179 if (!fuzzy)
3180 {
3181 *matches = ga.ga_data;
3182 *numMatches = ga.ga_len;
3183 }
3184 else
3185 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003186 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3187 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003188 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003189 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003190 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003191 return OK;
3192}
3193
3194/*
3195 * Expand names with a list returned by a function defined by the user.
3196 */
3197 static int
3198ExpandUserList(
3199 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003200 char_u ***matches,
3201 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003202{
3203 list_T *retlist;
3204 listitem_T *li;
3205 garray_T ga;
3206
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003207 *matches = NULL;
3208 *numMatches = 0;
3209 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003210 if (retlist == NULL)
3211 return FAIL;
3212
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003213 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003214 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003215 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003216 {
3217 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3218 continue; // Skip non-string items and empty strings
3219
3220 if (ga_grow(&ga, 1) == FAIL)
3221 break;
3222
3223 ((char_u **)ga.ga_data)[ga.ga_len] =
3224 vim_strsave(li->li_tv.vval.v_string);
3225 ++ga.ga_len;
3226 }
3227 list_unref(retlist);
3228
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003229 *matches = ga.ga_data;
3230 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003231 return OK;
3232}
3233# endif
3234
3235/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003236 * Expand "file" for all comma-separated directories in "path".
3237 * Adds the matches to "ga". Caller must init "ga".
3238 */
3239 void
3240globpath(
3241 char_u *path,
3242 char_u *file,
3243 garray_T *ga,
3244 int expand_options)
3245{
3246 expand_T xpc;
3247 char_u *buf;
3248 int i;
3249 int num_p;
3250 char_u **p;
3251
3252 buf = alloc(MAXPATHL);
3253 if (buf == NULL)
3254 return;
3255
3256 ExpandInit(&xpc);
3257 xpc.xp_context = EXPAND_FILES;
3258
3259 // Loop over all entries in {path}.
3260 while (*path != NUL)
3261 {
3262 // Copy one item of the path to buf[] and concatenate the file name.
3263 copy_option_part(&path, buf, MAXPATHL, ",");
3264 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3265 {
3266# if defined(MSWIN)
3267 // Using the platform's path separator (\) makes vim incorrectly
3268 // treat it as an escape character, use '/' instead.
3269 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3270 STRCAT(buf, "/");
3271# else
3272 add_pathsep(buf);
3273# endif
3274 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003275 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003276 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3277 {
3278 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3279
3280 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003281 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003282 for (i = 0; i < num_p; ++i)
3283 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003284 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003285 ++ga->ga_len;
3286 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003287 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003288 }
3289 }
3290 }
3291
3292 vim_free(buf);
3293}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003294
Bram Moolenaareadee482020-09-04 15:37:31 +02003295#ifdef FEAT_WILDMENU
3296
3297/*
3298 * Translate some keys pressed when 'wildmenu' is used.
3299 */
3300 int
3301wildmenu_translate_key(
3302 cmdline_info_T *cclp,
3303 int key,
3304 expand_T *xp,
3305 int did_wild_list)
3306{
3307 int c = key;
3308
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003309#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003310 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003311 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003312 // When the popup menu is used for cmdline completion:
3313 // Up : go to the previous item in the menu
3314 // Down : go to the next item in the menu
3315 // Left : go to the parent directory
3316 // Right: list the files in the selected directory
3317 switch (c)
3318 {
3319 case K_UP: c = K_LEFT; break;
3320 case K_DOWN: c = K_RIGHT; break;
3321 case K_LEFT: c = K_UP; break;
3322 case K_RIGHT: c = K_DOWN; break;
3323 default: break;
3324 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003325 }
3326#endif
3327
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003328 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003329 {
3330 if (c == K_LEFT)
3331 c = Ctrl_P;
3332 else if (c == K_RIGHT)
3333 c = Ctrl_N;
3334 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003335
Bram Moolenaareadee482020-09-04 15:37:31 +02003336 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003337 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003338 && cclp->cmdpos > 1
3339 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3340 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3341 && (c == '\n' || c == '\r' || c == K_KENTER))
3342 c = K_DOWN;
3343
3344 return c;
3345}
3346
3347/*
3348 * Delete characters on the command line, from "from" to the current
3349 * position.
3350 */
3351 static void
3352cmdline_del(cmdline_info_T *cclp, int from)
3353{
3354 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3355 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3356 cclp->cmdlen -= cclp->cmdpos - from;
3357 cclp->cmdpos = from;
3358}
3359
3360/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003361 * Handle a key pressed when the wild menu for the menu names
3362 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003363 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003364 static int
3365wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003366{
Bram Moolenaareadee482020-09-04 15:37:31 +02003367 int i;
3368 int j;
3369
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003370 // Hitting <Down> after "emenu Name.": complete submenu
3371 if (key == K_DOWN && cclp->cmdpos > 0
3372 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003373 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003374 key = p_wc;
3375 KeyTyped = TRUE; // in case the key was mapped
3376 }
3377 else if (key == K_UP)
3378 {
3379 // Hitting <Up>: Remove one submenu name in front of the
3380 // cursor
3381 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003382
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003383 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3384 i = 0;
3385 while (--j > 0)
3386 {
3387 // check for start of menu name
3388 if (cclp->cmdbuff[j] == ' '
3389 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003390 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003391 i = j + 1;
3392 break;
3393 }
3394 // check for start of submenu name
3395 if (cclp->cmdbuff[j] == '.'
3396 && cclp->cmdbuff[j - 1] != '\\')
3397 {
3398 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003399 {
3400 i = j + 1;
3401 break;
3402 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003403 else
3404 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003405 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003406 }
3407 if (i > 0)
3408 cmdline_del(cclp, i);
3409 key = p_wc;
3410 KeyTyped = TRUE; // in case the key was mapped
3411 xp->xp_context = EXPAND_NOTHING;
3412 }
3413
3414 return key;
3415}
3416
3417/*
3418 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3419 * directory names (EXPAND_DIRECTORIES) or shell command names
3420 * (EXPAND_SHELLCMD) is displayed.
3421 */
3422 static int
3423wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3424{
3425 int i;
3426 int j;
3427 char_u upseg[5];
3428
3429 upseg[0] = PATHSEP;
3430 upseg[1] = '.';
3431 upseg[2] = '.';
3432 upseg[3] = PATHSEP;
3433 upseg[4] = NUL;
3434
3435 if (key == K_DOWN
3436 && cclp->cmdpos > 0
3437 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3438 && (cclp->cmdpos < 3
3439 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3440 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3441 {
3442 // go down a directory
3443 key = p_wc;
3444 KeyTyped = TRUE; // in case the key was mapped
3445 }
3446 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3447 {
3448 // If in a direct ancestor, strip off one ../ to go down
3449 int found = FALSE;
3450
3451 j = cclp->cmdpos;
3452 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3453 while (--j > i)
3454 {
3455 if (has_mbyte)
3456 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3457 if (vim_ispathsep(cclp->cmdbuff[j]))
3458 {
3459 found = TRUE;
3460 break;
3461 }
3462 }
3463 if (found
3464 && cclp->cmdbuff[j - 1] == '.'
3465 && cclp->cmdbuff[j - 2] == '.'
3466 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3467 {
3468 cmdline_del(cclp, j - 2);
3469 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003470 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003471 }
3472 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003473 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003474 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003475 // go up a directory
3476 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003477
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003478 j = cclp->cmdpos - 1;
3479 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3480 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003481 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003482 if (has_mbyte)
3483 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3484 if (vim_ispathsep(cclp->cmdbuff[j])
3485# ifdef BACKSLASH_IN_FILENAME
3486 && vim_strchr((char_u *)" *?[{`$%#",
3487 cclp->cmdbuff[j + 1]) == NULL
3488# endif
3489 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003490 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003491 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003492 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003493 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003494 break;
3495 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003496 else
3497 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003498 }
3499 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003500
3501 if (!found)
3502 j = i;
3503 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3504 j += 4;
3505 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3506 && j == i)
3507 j += 3;
3508 else
3509 j = 0;
3510 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003511 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003512 // TODO this is only for DOS/UNIX systems - need to put in
3513 // machine-specific stuff here and in upseg init
3514 cmdline_del(cclp, j);
3515 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003516 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003517 else if (cclp->cmdpos > i)
3518 cmdline_del(cclp, i);
3519
3520 // Now complete in the new directory. Set KeyTyped in case the
3521 // Up key came from a mapping.
3522 key = p_wc;
3523 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003524 }
3525
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003526 return key;
3527}
3528
3529/*
3530 * Handle a key pressed when the wild menu is displayed
3531 */
3532 int
3533wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3534{
3535 if (xp->xp_context == EXPAND_MENUNAMES)
3536 return wildmenu_process_key_menunames(cclp, key, xp);
3537 else if ((xp->xp_context == EXPAND_FILES
3538 || xp->xp_context == EXPAND_DIRECTORIES
3539 || xp->xp_context == EXPAND_SHELLCMD))
3540 return wildmenu_process_key_filenames(cclp, key, xp);
3541
3542 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003543}
3544
3545/*
3546 * Free expanded names when finished walking through the matches
3547 */
3548 void
3549wildmenu_cleanup(cmdline_info_T *cclp)
3550{
3551 int skt = KeyTyped;
3552 int old_RedrawingDisabled = RedrawingDisabled;
3553
3554 if (!p_wmnu || wild_menu_showing == 0)
3555 return;
3556
3557 if (cclp->input_fn)
3558 RedrawingDisabled = 0;
3559
3560 if (wild_menu_showing == WM_SCROLLED)
3561 {
3562 // Entered command line, move it up
3563 cmdline_row--;
3564 redrawcmd();
3565 }
3566 else if (save_p_ls != -1)
3567 {
3568 // restore 'laststatus' and 'winminheight'
3569 p_ls = save_p_ls;
3570 p_wmh = save_p_wmh;
3571 last_status(FALSE);
3572 update_screen(VALID); // redraw the screen NOW
3573 redrawcmd();
3574 save_p_ls = -1;
3575 }
3576 else
3577 {
3578 win_redraw_last_status(topframe);
3579 redraw_statuslines();
3580 }
3581 KeyTyped = skt;
3582 wild_menu_showing = 0;
3583 if (cclp->input_fn)
3584 RedrawingDisabled = old_RedrawingDisabled;
3585}
3586#endif
3587
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003588#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003589/*
3590 * "getcompletion()" function
3591 */
3592 void
3593f_getcompletion(typval_T *argvars, typval_T *rettv)
3594{
3595 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003596 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003597 expand_T xpc;
3598 int filtered = FALSE;
3599 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003600 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003601
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003602 if (in_vim9script()
3603 && (check_for_string_arg(argvars, 0) == FAIL
3604 || check_for_string_arg(argvars, 1) == FAIL
3605 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3606 return;
3607
ii144785fe02021-11-21 12:13:56 +00003608 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003609 if (argvars[1].v_type != VAR_STRING)
3610 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003611 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003612 return;
3613 }
3614 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003615
3616 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003617 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003618
3619 if (p_wic)
3620 options |= WILD_ICASE;
3621
3622 // For filtered results, 'wildignore' is used
3623 if (!filtered)
3624 options |= WILD_KEEP_ALL;
3625
3626 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003627 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628 {
ii144785fe02021-11-21 12:13:56 +00003629 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003630 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003631 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003632 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003633 else
3634 {
ii144785fe02021-11-21 12:13:56 +00003635 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003636 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3637
3638 xpc.xp_context = cmdcomplete_str_to_type(type);
3639 if (xpc.xp_context == EXPAND_NOTHING)
3640 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003641 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003642 return;
3643 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003644
3645# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003646 if (xpc.xp_context == EXPAND_MENUS)
3647 {
3648 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3649 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3650 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003651# endif
3652# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003653 if (xpc.xp_context == EXPAND_CSCOPE)
3654 {
3655 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3656 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3657 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003658# endif
3659# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003660 if (xpc.xp_context == EXPAND_SIGN)
3661 {
3662 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3663 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3664 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003665# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003666 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003667
3668 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3669 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3670 {
3671 int i;
3672
3673 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3674
3675 for (i = 0; i < xpc.xp_numfiles; i++)
3676 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3677 }
3678 vim_free(pat);
3679 ExpandCleanup(&xpc);
3680}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003681#endif // FEAT_EVAL