blob: 7ab1bba31bf9038d49351ec2c52051c750af4796 [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
1609 EXP_BREAKPT_DEL // expand ":breakdel" sub-commands
1610} breakpt_expand_what;
1611
1612/*
1613 * Set the completion context for the :breakadd command. Always returns NULL.
1614 */
1615 static char_u *
1616set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
1617{
1618 char_u *p;
1619 char_u *subcmd_start;
1620
1621 xp->xp_context = EXPAND_BREAKPOINT;
1622 xp->xp_pattern = arg;
1623
1624 if (cmdidx == CMD_breakadd)
1625 breakpt_expand_what = EXP_BREAKPT_ADD;
1626 else
1627 breakpt_expand_what = EXP_BREAKPT_DEL;
1628
1629 p = skipwhite(arg);
1630 if (*p == NUL)
1631 return NULL;
1632 subcmd_start = p;
1633
1634 if (STRNCMP("file ", p, 5) == 0 ||
1635 STRNCMP("func ", p, 5) == 0)
1636 {
1637 // :breakadd file [lnum] <filename>
1638 // :breakadd func [lnum] <funcname>
1639 p += 4;
1640 p = skipwhite(p);
1641
1642 // skip line number (if specified)
1643 if (VIM_ISDIGIT(*p))
1644 {
1645 p = skipdigits(p);
1646 if (*p != ' ')
1647 {
1648 xp->xp_context = EXPAND_NOTHING;
1649 return NULL;
1650 }
1651 p = skipwhite(p);
1652 }
1653 if (STRNCMP("file", subcmd_start, 4) == 0)
1654 xp->xp_context = EXPAND_FILES;
1655 else
1656 xp->xp_context = EXPAND_USER_FUNC;
1657 xp->xp_pattern = p;
1658 }
1659 else if (STRNCMP("expr ", p, 5) == 0)
1660 {
1661 // :breakadd expr <expression>
1662 xp->xp_context = EXPAND_EXPRESSION;
1663 xp->xp_pattern = skipwhite(p + 5);
1664 }
1665
1666 return NULL;
1667}
1668#endif
1669
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001670/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001671 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1672 * The argument to the command is 'arg' and the argument flags is 'argt'.
1673 * For user-defined commands and for environment variables, 'compl' has the
1674 * completion type.
1675 * Returns a pointer to the next command. Returns NULL if there is no next
1676 * command.
1677 */
1678 static char_u *
1679set_context_by_cmdname(
1680 char_u *cmd,
1681 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001682 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001683 char_u *arg,
1684 long argt,
1685 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001686 int forceit)
1687{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001688 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001689 {
1690 case CMD_find:
1691 case CMD_sfind:
1692 case CMD_tabfind:
1693 if (xp->xp_context == EXPAND_FILES)
1694 xp->xp_context = EXPAND_FILES_IN_PATH;
1695 break;
1696 case CMD_cd:
1697 case CMD_chdir:
1698 case CMD_tcd:
1699 case CMD_tchdir:
1700 case CMD_lcd:
1701 case CMD_lchdir:
1702 if (xp->xp_context == EXPAND_FILES)
1703 xp->xp_context = EXPAND_DIRECTORIES;
1704 break;
1705 case CMD_help:
1706 xp->xp_context = EXPAND_HELP;
1707 xp->xp_pattern = arg;
1708 break;
1709
1710 // Command modifiers: return the argument.
1711 // Also for commands with an argument that is a command.
1712 case CMD_aboveleft:
1713 case CMD_argdo:
1714 case CMD_belowright:
1715 case CMD_botright:
1716 case CMD_browse:
1717 case CMD_bufdo:
1718 case CMD_cdo:
1719 case CMD_cfdo:
1720 case CMD_confirm:
1721 case CMD_debug:
1722 case CMD_folddoclosed:
1723 case CMD_folddoopen:
1724 case CMD_hide:
1725 case CMD_keepalt:
1726 case CMD_keepjumps:
1727 case CMD_keepmarks:
1728 case CMD_keeppatterns:
1729 case CMD_ldo:
1730 case CMD_leftabove:
1731 case CMD_lfdo:
1732 case CMD_lockmarks:
1733 case CMD_noautocmd:
1734 case CMD_noswapfile:
1735 case CMD_rightbelow:
1736 case CMD_sandbox:
1737 case CMD_silent:
1738 case CMD_tab:
1739 case CMD_tabdo:
1740 case CMD_topleft:
1741 case CMD_verbose:
1742 case CMD_vertical:
1743 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001744 case CMD_vim9cmd:
1745 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001746 return arg;
1747
1748 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001749 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001750
1751#ifdef FEAT_SEARCH_EXTRA
1752 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001753 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001754#endif
1755
1756 // All completion for the +cmdline_compl feature goes here.
1757
1758 case CMD_command:
1759 return set_context_in_user_cmd(xp, arg);
1760
1761 case CMD_delcommand:
1762 xp->xp_context = EXPAND_USER_COMMANDS;
1763 xp->xp_pattern = arg;
1764 break;
1765
1766 case CMD_global:
1767 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001768 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001769 case CMD_and:
1770 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001771 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001772 case CMD_isearch:
1773 case CMD_dsearch:
1774 case CMD_ilist:
1775 case CMD_dlist:
1776 case CMD_ijump:
1777 case CMD_psearch:
1778 case CMD_djump:
1779 case CMD_isplit:
1780 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001781 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001782 case CMD_autocmd:
1783 return set_context_in_autocmd(xp, arg, FALSE);
1784 case CMD_doautocmd:
1785 case CMD_doautoall:
1786 return set_context_in_autocmd(xp, arg, TRUE);
1787 case CMD_set:
1788 set_context_in_set_cmd(xp, arg, 0);
1789 break;
1790 case CMD_setglobal:
1791 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1792 break;
1793 case CMD_setlocal:
1794 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1795 break;
1796 case CMD_tag:
1797 case CMD_stag:
1798 case CMD_ptag:
1799 case CMD_ltag:
1800 case CMD_tselect:
1801 case CMD_stselect:
1802 case CMD_ptselect:
1803 case CMD_tjump:
1804 case CMD_stjump:
1805 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001806 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001807 xp->xp_context = EXPAND_TAGS_LISTFILES;
1808 else
1809 xp->xp_context = EXPAND_TAGS;
1810 xp->xp_pattern = arg;
1811 break;
1812 case CMD_augroup:
1813 xp->xp_context = EXPAND_AUGROUP;
1814 xp->xp_pattern = arg;
1815 break;
1816#ifdef FEAT_SYN_HL
1817 case CMD_syntax:
1818 set_context_in_syntax_cmd(xp, arg);
1819 break;
1820#endif
1821#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001822 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001823 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001824 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001825 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001826 case CMD_if:
1827 case CMD_elseif:
1828 case CMD_while:
1829 case CMD_for:
1830 case CMD_echo:
1831 case CMD_echon:
1832 case CMD_execute:
1833 case CMD_echomsg:
1834 case CMD_echoerr:
1835 case CMD_call:
1836 case CMD_return:
1837 case CMD_cexpr:
1838 case CMD_caddexpr:
1839 case CMD_cgetexpr:
1840 case CMD_lexpr:
1841 case CMD_laddexpr:
1842 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001843 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001844 break;
1845
1846 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001847 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001848 case CMD_function:
1849 case CMD_delfunction:
1850 xp->xp_context = EXPAND_USER_FUNC;
1851 xp->xp_pattern = arg;
1852 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001853 case CMD_disassemble:
1854 set_context_in_disassemble_cmd(xp, arg);
1855 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001856
1857 case CMD_echohl:
1858 set_context_in_echohl_cmd(xp, arg);
1859 break;
1860#endif
1861 case CMD_highlight:
1862 set_context_in_highlight_cmd(xp, arg);
1863 break;
1864#ifdef FEAT_CSCOPE
1865 case CMD_cscope:
1866 case CMD_lcscope:
1867 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001868 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001869 break;
1870#endif
1871#ifdef FEAT_SIGNS
1872 case CMD_sign:
1873 set_context_in_sign_cmd(xp, arg);
1874 break;
1875#endif
1876 case CMD_bdelete:
1877 case CMD_bwipeout:
1878 case CMD_bunload:
1879 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1880 arg = xp->xp_pattern + 1;
1881 // FALLTHROUGH
1882 case CMD_buffer:
1883 case CMD_sbuffer:
1884 case CMD_checktime:
1885 xp->xp_context = EXPAND_BUFFERS;
1886 xp->xp_pattern = arg;
1887 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001888#ifdef FEAT_DIFF
1889 case CMD_diffget:
1890 case CMD_diffput:
1891 // If current buffer is in diff mode, complete buffer names
1892 // which are in diff mode, and different than current buffer.
1893 xp->xp_context = EXPAND_DIFF_BUFFERS;
1894 xp->xp_pattern = arg;
1895 break;
1896#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001897 case CMD_USER:
1898 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001899 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1900 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001901
1902 case CMD_map: case CMD_noremap:
1903 case CMD_nmap: case CMD_nnoremap:
1904 case CMD_vmap: case CMD_vnoremap:
1905 case CMD_omap: case CMD_onoremap:
1906 case CMD_imap: case CMD_inoremap:
1907 case CMD_cmap: case CMD_cnoremap:
1908 case CMD_lmap: case CMD_lnoremap:
1909 case CMD_smap: case CMD_snoremap:
1910 case CMD_tmap: case CMD_tnoremap:
1911 case CMD_xmap: case CMD_xnoremap:
1912 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001913 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001914 case CMD_unmap:
1915 case CMD_nunmap:
1916 case CMD_vunmap:
1917 case CMD_ounmap:
1918 case CMD_iunmap:
1919 case CMD_cunmap:
1920 case CMD_lunmap:
1921 case CMD_sunmap:
1922 case CMD_tunmap:
1923 case CMD_xunmap:
1924 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001925 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001926 case CMD_mapclear:
1927 case CMD_nmapclear:
1928 case CMD_vmapclear:
1929 case CMD_omapclear:
1930 case CMD_imapclear:
1931 case CMD_cmapclear:
1932 case CMD_lmapclear:
1933 case CMD_smapclear:
1934 case CMD_tmapclear:
1935 case CMD_xmapclear:
1936 xp->xp_context = EXPAND_MAPCLEAR;
1937 xp->xp_pattern = arg;
1938 break;
1939
1940 case CMD_abbreviate: case CMD_noreabbrev:
1941 case CMD_cabbrev: case CMD_cnoreabbrev:
1942 case CMD_iabbrev: case CMD_inoreabbrev:
1943 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001944 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001945 case CMD_unabbreviate:
1946 case CMD_cunabbrev:
1947 case CMD_iunabbrev:
1948 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001949 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001950#ifdef FEAT_MENU
1951 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1952 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1953 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1954 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1955 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1956 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1957 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1958 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1959 case CMD_tmenu: case CMD_tunmenu:
1960 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1961 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1962#endif
1963
1964 case CMD_colorscheme:
1965 xp->xp_context = EXPAND_COLORS;
1966 xp->xp_pattern = arg;
1967 break;
1968
1969 case CMD_compiler:
1970 xp->xp_context = EXPAND_COMPILER;
1971 xp->xp_pattern = arg;
1972 break;
1973
1974 case CMD_ownsyntax:
1975 xp->xp_context = EXPAND_OWNSYNTAX;
1976 xp->xp_pattern = arg;
1977 break;
1978
1979 case CMD_setfiletype:
1980 xp->xp_context = EXPAND_FILETYPE;
1981 xp->xp_pattern = arg;
1982 break;
1983
1984 case CMD_packadd:
1985 xp->xp_context = EXPAND_PACKADD;
1986 xp->xp_pattern = arg;
1987 break;
1988
1989#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1990 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001991 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001992#endif
1993#if defined(FEAT_PROFILE)
1994 case CMD_profile:
1995 set_context_in_profile_cmd(xp, arg);
1996 break;
1997#endif
1998 case CMD_behave:
1999 xp->xp_context = EXPAND_BEHAVE;
2000 xp->xp_pattern = arg;
2001 break;
2002
2003 case CMD_messages:
2004 xp->xp_context = EXPAND_MESSAGES;
2005 xp->xp_pattern = arg;
2006 break;
2007
2008 case CMD_history:
2009 xp->xp_context = EXPAND_HISTORY;
2010 xp->xp_pattern = arg;
2011 break;
2012#if defined(FEAT_PROFILE)
2013 case CMD_syntime:
2014 xp->xp_context = EXPAND_SYNTIME;
2015 xp->xp_pattern = arg;
2016 break;
2017#endif
2018
2019 case CMD_argdelete:
2020 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2021 arg = xp->xp_pattern + 1;
2022 xp->xp_context = EXPAND_ARGLIST;
2023 xp->xp_pattern = arg;
2024 break;
2025
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002026#ifdef FEAT_EVAL
2027 case CMD_breakadd:
2028 case CMD_breakdel:
2029 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
2030#endif
2031
Bram Moolenaard0190392019-08-23 21:17:35 +02002032 default:
2033 break;
2034 }
2035 return NULL;
2036}
2037
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002038/*
2039 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2040 * we don't need/want deleted. Maybe this could be done better if we didn't
2041 * repeat all this stuff. The only problem is that they may not stay
2042 * perfectly compatible with each other, but then the command line syntax
2043 * probably won't change that much -- webb.
2044 */
2045 static char_u *
2046set_one_cmd_context(
2047 expand_T *xp,
2048 char_u *buff) // buffer for command string
2049{
2050 char_u *p;
2051 char_u *cmd, *arg;
2052 int len = 0;
2053 exarg_T ea;
2054 int compl = EXPAND_NOTHING;
2055 int forceit = FALSE;
2056 int usefilter = FALSE; // filter instead of file name
2057
2058 ExpandInit(xp);
2059 xp->xp_pattern = buff;
2060 xp->xp_line = buff;
2061 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2062 ea.argt = 0;
2063
2064 // 1. skip comment lines and leading space, colons or bars
2065 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2066 ;
2067 xp->xp_pattern = cmd;
2068
2069 if (*cmd == NUL)
2070 return NULL;
2071 if (*cmd == '"') // ignore comment lines
2072 {
2073 xp->xp_context = EXPAND_NOTHING;
2074 return NULL;
2075 }
2076
2077 // 3. Skip over the range to find the command.
2078 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2079 xp->xp_pattern = cmd;
2080 if (*cmd == NUL)
2081 return NULL;
2082 if (*cmd == '"')
2083 {
2084 xp->xp_context = EXPAND_NOTHING;
2085 return NULL;
2086 }
2087
2088 if (*cmd == '|' || *cmd == '\n')
2089 return cmd + 1; // There's another command
2090
2091 // Get the command index.
2092 p = set_cmd_index(cmd, &ea, xp, &compl);
2093 if (p == NULL)
2094 return NULL;
2095
2096 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2097
2098 if (*p == '!') // forced commands
2099 {
2100 forceit = TRUE;
2101 ++p;
2102 }
2103
2104 // 6. parse arguments
2105 if (!IS_USER_CMDIDX(ea.cmdidx))
2106 ea.argt = excmd_get_argt(ea.cmdidx);
2107
2108 arg = skipwhite(p);
2109
2110 // Skip over ++argopt argument
2111 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2112 {
2113 p = arg;
2114 while (*p && !vim_isspace(*p))
2115 MB_PTR_ADV(p);
2116 arg = skipwhite(p);
2117 }
2118
2119 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2120 {
2121 if (*arg == '>') // append
2122 {
2123 if (*++arg == '>')
2124 ++arg;
2125 arg = skipwhite(arg);
2126 }
2127 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2128 {
2129 ++arg;
2130 usefilter = TRUE;
2131 }
2132 }
2133
2134 if (ea.cmdidx == CMD_read)
2135 {
2136 usefilter = forceit; // :r! filter if forced
2137 if (*arg == '!') // :r !filter
2138 {
2139 ++arg;
2140 usefilter = TRUE;
2141 }
2142 }
2143
2144 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2145 {
2146 while (*arg == *cmd) // allow any number of '>' or '<'
2147 ++arg;
2148 arg = skipwhite(arg);
2149 }
2150
2151 // Does command allow "+command"?
2152 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2153 {
2154 // Check if we're in the +command
2155 p = arg + 1;
2156 arg = skip_cmd_arg(arg, FALSE);
2157
2158 // Still touching the command after '+'?
2159 if (*arg == NUL)
2160 return p;
2161
2162 // Skip space(s) after +command to get to the real argument
2163 arg = skipwhite(arg);
2164 }
2165
2166
2167 // Check for '|' to separate commands and '"' to start comments.
2168 // Don't do this for ":read !cmd" and ":write !cmd".
2169 if ((ea.argt & EX_TRLBAR) && !usefilter)
2170 {
2171 p = arg;
2172 // ":redir @" is not the start of a comment
2173 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2174 p += 2;
2175 while (*p)
2176 {
2177 if (*p == Ctrl_V)
2178 {
2179 if (p[1] != NUL)
2180 ++p;
2181 }
2182 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2183 || *p == '|' || *p == '\n')
2184 {
2185 if (*(p - 1) != '\\')
2186 {
2187 if (*p == '|' || *p == '\n')
2188 return p + 1;
2189 return NULL; // It's a comment
2190 }
2191 }
2192 MB_PTR_ADV(p);
2193 }
2194 }
2195
2196 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2197 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2198 // no arguments allowed but there is something
2199 return NULL;
2200
2201 // Find start of last argument (argument just before cursor):
2202 p = buff;
2203 xp->xp_pattern = p;
2204 len = (int)STRLEN(buff);
2205 while (*p && p < buff + len)
2206 {
2207 if (*p == ' ' || *p == TAB)
2208 {
2209 // argument starts after a space
2210 xp->xp_pattern = ++p;
2211 }
2212 else
2213 {
2214 if (*p == '\\' && *(p + 1) != NUL)
2215 ++p; // skip over escaped character
2216 MB_PTR_ADV(p);
2217 }
2218 }
2219
2220 if (ea.argt & EX_XFILE)
2221 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2222
2223 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002224 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002225 forceit);
2226}
2227
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002228/*
2229 * Set the completion context in 'xp' for command 'str'
2230 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002231 void
2232set_cmd_context(
2233 expand_T *xp,
2234 char_u *str, // start of command line
2235 int len, // length of command line (excl. NUL)
2236 int col, // position of cursor
2237 int use_ccline UNUSED) // use ccline for info
2238{
2239#ifdef FEAT_EVAL
2240 cmdline_info_T *ccline = get_cmdline_info();
2241#endif
2242 int old_char = NUL;
2243 char_u *nextcomm;
2244
2245 // Avoid a UMR warning from Purify, only save the character if it has been
2246 // written before.
2247 if (col < len)
2248 old_char = str[col];
2249 str[col] = NUL;
2250 nextcomm = str;
2251
2252#ifdef FEAT_EVAL
2253 if (use_ccline && ccline->cmdfirstc == '=')
2254 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002255 // pass CMD_SIZE because there is no real command
2256 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002257 }
2258 else if (use_ccline && ccline->input_fn)
2259 {
2260 xp->xp_context = ccline->xp_context;
2261 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002262 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002263 }
2264 else
2265#endif
2266 while (nextcomm != NULL)
2267 nextcomm = set_one_cmd_context(xp, nextcomm);
2268
2269 // Store the string here so that call_user_expand_func() can get to them
2270 // easily.
2271 xp->xp_line = str;
2272 xp->xp_col = col;
2273
2274 str[col] = old_char;
2275}
2276
2277/*
2278 * Expand the command line "str" from context "xp".
2279 * "xp" must have been set by set_cmd_context().
2280 * xp->xp_pattern points into "str", to where the text that is to be expanded
2281 * starts.
2282 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2283 * cursor.
2284 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2285 * key that triggered expansion literally.
2286 * Returns EXPAND_OK otherwise.
2287 */
2288 int
2289expand_cmdline(
2290 expand_T *xp,
2291 char_u *str, // start of command line
2292 int col, // position of cursor
2293 int *matchcount, // return: nr of matches
2294 char_u ***matches) // return: array of pointers to matches
2295{
2296 char_u *file_str = NULL;
2297 int options = WILD_ADD_SLASH|WILD_SILENT;
2298
2299 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2300 {
2301 beep_flush();
2302 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2303 }
2304 if (xp->xp_context == EXPAND_NOTHING)
2305 {
2306 // Caller can use the character as a normal char instead
2307 return EXPAND_NOTHING;
2308 }
2309
2310 // add star to file name, or convert to regexp if not exp. files.
2311 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002312 if (cmdline_fuzzy_completion_supported(xp))
2313 // If fuzzy matching, don't modify the search string
2314 file_str = vim_strsave(xp->xp_pattern);
2315 else
2316 {
2317 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2318 if (file_str == NULL)
2319 return EXPAND_UNSUCCESSFUL;
2320 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002321
2322 if (p_wic)
2323 options += WILD_ICASE;
2324
2325 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002326 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002327 {
2328 *matchcount = 0;
2329 *matches = NULL;
2330 }
2331 vim_free(file_str);
2332
2333 return EXPAND_OK;
2334}
2335
Bram Moolenaar66b51422019-08-18 21:44:12 +02002336/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002337 * Expand file or directory names.
2338 */
2339 static int
2340expand_files_and_dirs(
2341 expand_T *xp,
2342 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002343 char_u ***matches,
2344 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002345 int flags,
2346 int options)
2347{
2348 int free_pat = FALSE;
2349 int i;
2350 int ret;
2351
2352 // for ":set path=" and ":set tags=" halve backslashes for escaped
2353 // space
2354 if (xp->xp_backslash != XP_BS_NONE)
2355 {
2356 free_pat = TRUE;
2357 pat = vim_strsave(pat);
2358 for (i = 0; pat[i]; ++i)
2359 if (pat[i] == '\\')
2360 {
2361 if (xp->xp_backslash == XP_BS_THREE
2362 && pat[i + 1] == '\\'
2363 && pat[i + 2] == '\\'
2364 && pat[i + 3] == ' ')
2365 STRMOVE(pat + i, pat + i + 3);
2366 if (xp->xp_backslash == XP_BS_ONE
2367 && pat[i + 1] == ' ')
2368 STRMOVE(pat + i, pat + i + 1);
2369 }
2370 }
2371
2372 if (xp->xp_context == EXPAND_FILES)
2373 flags |= EW_FILE;
2374 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2375 flags |= (EW_FILE | EW_PATH);
2376 else
2377 flags = (flags | EW_DIR) & ~EW_FILE;
2378 if (options & WILD_ICASE)
2379 flags |= EW_ICASE;
2380
2381 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002382 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002383 if (free_pat)
2384 vim_free(pat);
2385#ifdef BACKSLASH_IN_FILENAME
2386 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2387 {
2388 int j;
2389
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002390 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002391 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002392 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002393
2394 while (*ptr != NUL)
2395 {
2396 if (p_csl[0] == 's' && *ptr == '\\')
2397 *ptr = '/';
2398 else if (p_csl[0] == 'b' && *ptr == '/')
2399 *ptr = '\\';
2400 ptr += (*mb_ptr2len)(ptr);
2401 }
2402 }
2403 }
2404#endif
2405 return ret;
2406}
2407
2408/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002409 * Function given to ExpandGeneric() to obtain the possible arguments of the
2410 * ":behave {mswin,xterm}" command.
2411 */
2412 static char_u *
2413get_behave_arg(expand_T *xp UNUSED, int idx)
2414{
2415 if (idx == 0)
2416 return (char_u *)"mswin";
2417 if (idx == 1)
2418 return (char_u *)"xterm";
2419 return NULL;
2420}
2421
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002422# ifdef FEAT_EVAL
2423/*
2424 * Function given to ExpandGeneric() to obtain the possible arguments of the
2425 * ":breakadd {expr, file, func, here}" command.
2426 * ":breakdel {func, file, here}" command.
2427 */
2428 static char_u *
2429get_breakadd_arg(expand_T *xp UNUSED, int idx)
2430{
2431 char *opts[] = {"expr", "file", "func", "here"};
2432
2433 if (idx >=0 && idx <= 3)
2434 {
2435 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2436 return (char_u *)opts[idx];
2437 else
2438 {
2439 if (idx <= 2)
2440 return (char_u *)opts[idx + 1];
2441 }
2442 }
2443 return NULL;
2444}
2445#endif
2446
Bram Moolenaard0190392019-08-23 21:17:35 +02002447/*
2448 * Function given to ExpandGeneric() to obtain the possible arguments of the
2449 * ":messages {clear}" command.
2450 */
2451 static char_u *
2452get_messages_arg(expand_T *xp UNUSED, int idx)
2453{
2454 if (idx == 0)
2455 return (char_u *)"clear";
2456 return NULL;
2457}
2458
2459 static char_u *
2460get_mapclear_arg(expand_T *xp UNUSED, int idx)
2461{
2462 if (idx == 0)
2463 return (char_u *)"<buffer>";
2464 return NULL;
2465}
2466
2467/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002468 * Do the expansion based on xp->xp_context and 'rmp'.
2469 */
2470 static int
2471ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002472 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002473 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002474 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002475 char_u ***matches,
2476 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002477{
2478 static struct expgen
2479 {
2480 int context;
2481 char_u *((*func)(expand_T *, int));
2482 int ic;
2483 int escaped;
2484 } tab[] =
2485 {
2486 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2487 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2488 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2489 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2490 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2491 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2492 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2493 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2494 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2495 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002496#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002497 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2498 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2499 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2500 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2501 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002502#endif
2503#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002504 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2505 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002506#endif
2507#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002508 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002509#endif
2510#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002511 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002512#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002513 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2514 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2515 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002516#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002517 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002518#endif
2519#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002520 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002521#endif
2522#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002523 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002524#endif
2525#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002526 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2527 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002528#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002529 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2530 {EXPAND_USER, get_users, TRUE, FALSE},
2531 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002532#ifdef FEAT_EVAL
2533 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
2534#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002535 };
2536 int i;
2537 int ret = FAIL;
2538
2539 // Find a context in the table and call the ExpandGeneric() with the
2540 // right function to do the expansion.
2541 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2542 {
2543 if (xp->xp_context == tab[i].context)
2544 {
2545 if (tab[i].ic)
2546 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002547 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2548 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002549 break;
2550 }
2551 }
2552
2553 return ret;
2554}
2555
2556/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002557 * Map wild expand options to flags for expand_wildcards()
2558 */
2559 static int
2560map_wildopts_to_ewflags(int options)
2561{
2562 int flags;
2563
2564 flags = EW_DIR; // include directories
2565 if (options & WILD_LIST_NOTFOUND)
2566 flags |= EW_NOTFOUND;
2567 if (options & WILD_ADD_SLASH)
2568 flags |= EW_ADDSLASH;
2569 if (options & WILD_KEEP_ALL)
2570 flags |= EW_KEEPALL;
2571 if (options & WILD_SILENT)
2572 flags |= EW_SILENT;
2573 if (options & WILD_NOERROR)
2574 flags |= EW_NOERROR;
2575 if (options & WILD_ALLLINKS)
2576 flags |= EW_ALLLINKS;
2577
2578 return flags;
2579}
2580
2581/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002582 * Do the expansion based on xp->xp_context and "pat".
2583 */
2584 static int
2585ExpandFromContext(
2586 expand_T *xp,
2587 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002588 char_u ***matches,
2589 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002590 int options) // WILD_ flags
2591{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002592 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002593 int ret;
2594 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002595 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002596 int fuzzy = cmdline_fuzzy_complete(pat)
2597 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002598
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002599 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002600
2601 if (xp->xp_context == EXPAND_FILES
2602 || xp->xp_context == EXPAND_DIRECTORIES
2603 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002604 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2605 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002606
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002607 *matches = (char_u **)"";
2608 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002609 if (xp->xp_context == EXPAND_HELP)
2610 {
2611 // With an empty argument we would get all the help tags, which is
2612 // very slow. Get matches for "help" instead.
2613 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002614 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002615 {
2616#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002617 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002618#endif
2619 return OK;
2620 }
2621 return FAIL;
2622 }
2623
Bram Moolenaar66b51422019-08-18 21:44:12 +02002624 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002625 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002626 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002627 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002628 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002629 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002630#ifdef FEAT_DIFF
2631 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002632 return ExpandBufnames(pat, numMatches, matches,
2633 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002634#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002635 if (xp->xp_context == EXPAND_TAGS
2636 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002637 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2638 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002639 if (xp->xp_context == EXPAND_COLORS)
2640 {
2641 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002642 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002643 directories);
2644 }
2645 if (xp->xp_context == EXPAND_COMPILER)
2646 {
2647 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002648 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002649 }
2650 if (xp->xp_context == EXPAND_OWNSYNTAX)
2651 {
2652 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002653 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002654 }
2655 if (xp->xp_context == EXPAND_FILETYPE)
2656 {
2657 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002658 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002659 }
2660# if defined(FEAT_EVAL)
2661 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002662 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002663# endif
2664 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002665 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002666
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002667 // When expanding a function name starting with s:, match the <SNR>nr_
2668 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002669 if ((xp->xp_context == EXPAND_USER_FUNC
2670 || xp->xp_context == EXPAND_DISASSEMBLE)
2671 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002672 {
2673 int len = (int)STRLEN(pat) + 20;
2674
2675 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002676 if (tofree == NULL)
2677 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002678 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002679 pat = tofree;
2680 }
2681
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002682 if (!fuzzy)
2683 {
2684 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2685 if (regmatch.regprog == NULL)
2686 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002687
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002688 // set ignore-case according to p_ic, p_scs and pat
2689 regmatch.rm_ic = ignorecase(pat);
2690 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002691
2692 if (xp->xp_context == EXPAND_SETTINGS
2693 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002694 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002695 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002696 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002697# if defined(FEAT_EVAL)
2698 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002699 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002700# endif
2701 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002702 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002703
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002704 if (!fuzzy)
2705 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002706 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002707
2708 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002709}
2710
Bram Moolenaar66b51422019-08-18 21:44:12 +02002711/*
2712 * Expand a list of names.
2713 *
2714 * Generic function for command line completion. It calls a function to
2715 * obtain strings, one by one. The strings are matched against a regexp
2716 * program. Matching strings are copied into an array, which is returned.
2717 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002718 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2719 * is used.
2720 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002721 * Returns OK when no problems encountered, FAIL for error (out of memory).
2722 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002723 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002724ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002725 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002726 expand_T *xp,
2727 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002728 char_u ***matches,
2729 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002730 char_u *((*func)(expand_T *, int)),
2731 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002732 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002733{
2734 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002735 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002736 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002737 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002738 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002739 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002740 int match;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002741
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002742 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002743 *matches = NULL;
2744 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002745
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002746 if (!fuzzy)
2747 ga_init2(&ga, sizeof(char *), 30);
2748 else
2749 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
2750
2751 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002752 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002753 str = (*func)(xp, i);
2754 if (str == NULL) // end of list
2755 break;
2756 if (*str == NUL) // skip empty strings
2757 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002758
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002759 if (xp->xp_pattern[0] != NUL)
2760 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002761 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002762 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002763 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002764 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002765 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002766 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002767 }
2768 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002769 else
2770 match = TRUE;
2771
2772 if (!match)
2773 continue;
2774
2775 if (escaped)
2776 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2777 else
2778 str = vim_strsave(str);
2779 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002780 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002781 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002782 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002783 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002784 return FAIL;
2785 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002786
2787 for (i = 0; i < ga.ga_len; ++i)
2788 {
2789 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[i];
2790 vim_free(fuzmatch->str);
2791 }
2792 ga_clear(&ga);
2793 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002794 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002795
2796 if (ga_grow(&ga, 1) == FAIL)
2797 {
2798 vim_free(str);
2799 break;
2800 }
2801
2802 if (fuzzy)
2803 {
2804 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
2805 fuzmatch->idx = ga.ga_len;
2806 fuzmatch->str = str;
2807 fuzmatch->score = score;
2808 }
2809 else
2810 ((char_u **)ga.ga_data)[ga.ga_len] = str;
2811
2812# ifdef FEAT_MENU
2813 if (func == get_menu_names)
2814 {
2815 // test for separator added by get_menu_names()
2816 str += STRLEN(str) - 1;
2817 if (*str == '\001')
2818 *str = '.';
2819 }
2820# endif
2821
2822 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002823 }
2824
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002825 if (ga.ga_len == 0)
2826 return OK;
2827
Bram Moolenaar66b51422019-08-18 21:44:12 +02002828 // Sort the results. Keep menu's in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002829 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
2830 && xp->xp_context != EXPAND_MENUS)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002831 {
2832 if (xp->xp_context == EXPAND_EXPRESSION
2833 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002834 || xp->xp_context == EXPAND_USER_FUNC
2835 || xp->xp_context == EXPAND_DISASSEMBLE)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002836 // <SNR> functions should be sorted to the end.
2837 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
2838 sort_func_compare);
2839 else
2840 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2841 }
2842
2843 if (!fuzzy)
2844 {
2845 *matches = ga.ga_data;
2846 *numMatches = ga.ga_len;
2847 }
2848 else
2849 {
2850 int funcsort = FALSE;
2851
2852 if (xp->xp_context == EXPAND_EXPRESSION
2853 || xp->xp_context == EXPAND_FUNCTIONS
2854 || xp->xp_context == EXPAND_USER_FUNC
2855 || xp->xp_context == EXPAND_DISASSEMBLE)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002856 // <SNR> functions should be sorted to the end.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002857 funcsort = TRUE;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002858
2859 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
2860 funcsort) == FAIL)
2861 return FAIL;
2862 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002863 }
2864
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002865#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002866 // Reset the variables used for special highlight names expansion, so that
2867 // they don't show up when getting normal highlight names by ID.
2868 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002869#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002870
Bram Moolenaar66b51422019-08-18 21:44:12 +02002871 return OK;
2872}
2873
2874/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002875 * Expand shell command matches in one directory of $PATH.
2876 */
2877 static void
2878expand_shellcmd_onedir(
2879 char_u *buf,
2880 char_u *s,
2881 size_t l,
2882 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002883 char_u ***matches,
2884 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002885 int flags,
2886 hashtab_T *ht,
2887 garray_T *gap)
2888{
2889 int ret;
2890 int i;
2891 hash_T hash;
2892 hashitem_T *hi;
2893
2894 vim_strncpy(buf, s, l);
2895 add_pathsep(buf);
2896 l = STRLEN(buf);
2897 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2898
2899 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002900 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002901 if (ret == OK)
2902 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002903 if (ga_grow(gap, *numMatches) == FAIL)
2904 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002905 else
2906 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002907 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002908 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002909 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002910
2911 if (STRLEN(name) > l)
2912 {
2913 // Check if this name was already found.
2914 hash = hash_hash(name + l);
2915 hi = hash_lookup(ht, name + l, hash);
2916 if (HASHITEM_EMPTY(hi))
2917 {
2918 // Remove the path that was prepended.
2919 STRMOVE(name, name + l);
2920 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2921 hash_add_item(ht, hi, name, hash);
2922 name = NULL;
2923 }
2924 }
2925 vim_free(name);
2926 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002927 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002928 }
2929 }
2930}
2931
2932/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002933 * Complete a shell command.
2934 * Returns FAIL or OK;
2935 */
2936 static int
2937expand_shellcmd(
2938 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002939 char_u ***matches, // return: array with matches
2940 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02002941 int flagsarg) // EW_ flags
2942{
2943 char_u *pat;
2944 int i;
2945 char_u *path = NULL;
2946 int mustfree = FALSE;
2947 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002948 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002949 size_t l;
2950 char_u *s, *e;
2951 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002952 int did_curdir = FALSE;
2953 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002954
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002955 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002956 if (buf == NULL)
2957 return FAIL;
2958
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002959 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002960 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002961 if (pat == NULL)
2962 {
2963 vim_free(buf);
2964 return FAIL;
2965 }
2966
Bram Moolenaar66b51422019-08-18 21:44:12 +02002967 for (i = 0; pat[i]; ++i)
2968 if (pat[i] == '\\' && pat[i + 1] == ' ')
2969 STRMOVE(pat + i, pat + i + 1);
2970
2971 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2972
2973 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2974 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2975 path = (char_u *)".";
2976 else
2977 {
2978 // For an absolute name we don't use $PATH.
2979 if (!mch_isFullName(pat))
2980 path = vim_getenv((char_u *)"PATH", &mustfree);
2981 if (path == NULL)
2982 path = (char_u *)"";
2983 }
2984
2985 // Go over all directories in $PATH. Expand matches in that directory and
2986 // collect them in "ga". When "." is not in $PATH also expand for the
2987 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002988 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002989 hash_init(&found_ht);
2990 for (s = path; ; s = e)
2991 {
2992# if defined(MSWIN)
2993 e = vim_strchr(s, ';');
2994# else
2995 e = vim_strchr(s, ':');
2996# endif
2997 if (e == NULL)
2998 e = s + STRLEN(s);
2999
3000 if (*s == NUL)
3001 {
3002 if (did_curdir)
3003 break;
3004 // Find directories in the current directory, path is empty.
3005 did_curdir = TRUE;
3006 flags |= EW_DIR;
3007 }
3008 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3009 {
3010 did_curdir = TRUE;
3011 flags |= EW_DIR;
3012 }
3013 else
3014 // Do not match directories inside a $PATH item.
3015 flags &= ~EW_DIR;
3016
3017 l = e - s;
3018 if (l > MAXPATHL - 5)
3019 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003020
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003021 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003022 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003023
Bram Moolenaar66b51422019-08-18 21:44:12 +02003024 if (*e != NUL)
3025 ++e;
3026 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003027 *matches = ga.ga_data;
3028 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003029
3030 vim_free(buf);
3031 vim_free(pat);
3032 if (mustfree)
3033 vim_free(path);
3034 hash_clear(&found_ht);
3035 return OK;
3036}
3037
3038# if defined(FEAT_EVAL)
3039/*
3040 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003041 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003042 */
3043 static void *
3044call_user_expand_func(
3045 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003046 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003047{
3048 cmdline_info_T *ccline = get_cmdline_info();
3049 int keep = 0;
3050 typval_T args[4];
3051 sctx_T save_current_sctx = current_sctx;
3052 char_u *pat = NULL;
3053 void *ret;
3054
3055 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3056 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003057
3058 if (ccline->cmdbuff != NULL)
3059 {
3060 keep = ccline->cmdbuff[ccline->cmdlen];
3061 ccline->cmdbuff[ccline->cmdlen] = 0;
3062 }
3063
3064 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3065
3066 args[0].v_type = VAR_STRING;
3067 args[0].vval.v_string = pat;
3068 args[1].v_type = VAR_STRING;
3069 args[1].vval.v_string = xp->xp_line;
3070 args[2].v_type = VAR_NUMBER;
3071 args[2].vval.v_number = xp->xp_col;
3072 args[3].v_type = VAR_UNKNOWN;
3073
3074 current_sctx = xp->xp_script_ctx;
3075
3076 ret = user_expand_func(xp->xp_arg, 3, args);
3077
3078 current_sctx = save_current_sctx;
3079 if (ccline->cmdbuff != NULL)
3080 ccline->cmdbuff[ccline->cmdlen] = keep;
3081
3082 vim_free(pat);
3083 return ret;
3084}
3085
3086/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003087 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3088 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003089 */
3090 static int
3091ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003092 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003093 expand_T *xp,
3094 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003095 char_u ***matches,
3096 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003097{
3098 char_u *retstr;
3099 char_u *s;
3100 char_u *e;
3101 int keep;
3102 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003103 int fuzzy;
3104 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003105 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003106
3107 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003108 *matches = NULL;
3109 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003110
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003111 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112 if (retstr == NULL)
3113 return FAIL;
3114
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003115 if (!fuzzy)
3116 ga_init2(&ga, sizeof(char *), 3);
3117 else
3118 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3119
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120 for (s = retstr; *s != NUL; s = e)
3121 {
3122 e = vim_strchr(s, '\n');
3123 if (e == NULL)
3124 e = s + STRLEN(s);
3125 keep = *e;
3126 *e = NUL;
3127
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003128 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003129 {
3130 if (!fuzzy)
3131 match = vim_regexec(regmatch, s, (colnr_T)0);
3132 else
3133 {
3134 score = fuzzy_match_str(s, pat);
3135 match = (score != 0);
3136 }
3137 }
3138 else
3139 match = TRUE; // match everything
3140
Bram Moolenaar66b51422019-08-18 21:44:12 +02003141 *e = keep;
3142
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003143 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003144 {
3145 if (ga_grow(&ga, 1) == FAIL)
3146 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003147 if (!fuzzy)
3148 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3149 else
3150 {
3151 fuzmatch_str_T *fuzmatch =
3152 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003153 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003154 fuzmatch->str = vim_strnsave(s, e - s);
3155 fuzmatch->score = score;
3156 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003157 ++ga.ga_len;
3158 }
3159
3160 if (*e != NUL)
3161 ++e;
3162 }
3163 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003164
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003165 if (ga.ga_len == 0)
3166 return OK;
3167
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003168 if (!fuzzy)
3169 {
3170 *matches = ga.ga_data;
3171 *numMatches = ga.ga_len;
3172 }
3173 else
3174 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003175 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3176 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003177 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003178 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003179 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003180 return OK;
3181}
3182
3183/*
3184 * Expand names with a list returned by a function defined by the user.
3185 */
3186 static int
3187ExpandUserList(
3188 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003189 char_u ***matches,
3190 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003191{
3192 list_T *retlist;
3193 listitem_T *li;
3194 garray_T ga;
3195
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003196 *matches = NULL;
3197 *numMatches = 0;
3198 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003199 if (retlist == NULL)
3200 return FAIL;
3201
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003202 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003203 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003204 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003205 {
3206 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3207 continue; // Skip non-string items and empty strings
3208
3209 if (ga_grow(&ga, 1) == FAIL)
3210 break;
3211
3212 ((char_u **)ga.ga_data)[ga.ga_len] =
3213 vim_strsave(li->li_tv.vval.v_string);
3214 ++ga.ga_len;
3215 }
3216 list_unref(retlist);
3217
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003218 *matches = ga.ga_data;
3219 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003220 return OK;
3221}
3222# endif
3223
3224/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003225 * Expand "file" for all comma-separated directories in "path".
3226 * Adds the matches to "ga". Caller must init "ga".
3227 */
3228 void
3229globpath(
3230 char_u *path,
3231 char_u *file,
3232 garray_T *ga,
3233 int expand_options)
3234{
3235 expand_T xpc;
3236 char_u *buf;
3237 int i;
3238 int num_p;
3239 char_u **p;
3240
3241 buf = alloc(MAXPATHL);
3242 if (buf == NULL)
3243 return;
3244
3245 ExpandInit(&xpc);
3246 xpc.xp_context = EXPAND_FILES;
3247
3248 // Loop over all entries in {path}.
3249 while (*path != NUL)
3250 {
3251 // Copy one item of the path to buf[] and concatenate the file name.
3252 copy_option_part(&path, buf, MAXPATHL, ",");
3253 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3254 {
3255# if defined(MSWIN)
3256 // Using the platform's path separator (\) makes vim incorrectly
3257 // treat it as an escape character, use '/' instead.
3258 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3259 STRCAT(buf, "/");
3260# else
3261 add_pathsep(buf);
3262# endif
3263 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003264 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003265 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3266 {
3267 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3268
3269 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003270 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003271 for (i = 0; i < num_p; ++i)
3272 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003273 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003274 ++ga->ga_len;
3275 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003276 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003277 }
3278 }
3279 }
3280
3281 vim_free(buf);
3282}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003283
Bram Moolenaareadee482020-09-04 15:37:31 +02003284#ifdef FEAT_WILDMENU
3285
3286/*
3287 * Translate some keys pressed when 'wildmenu' is used.
3288 */
3289 int
3290wildmenu_translate_key(
3291 cmdline_info_T *cclp,
3292 int key,
3293 expand_T *xp,
3294 int did_wild_list)
3295{
3296 int c = key;
3297
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003298#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003299 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003300 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003301 // When the popup menu is used for cmdline completion:
3302 // Up : go to the previous item in the menu
3303 // Down : go to the next item in the menu
3304 // Left : go to the parent directory
3305 // Right: list the files in the selected directory
3306 switch (c)
3307 {
3308 case K_UP: c = K_LEFT; break;
3309 case K_DOWN: c = K_RIGHT; break;
3310 case K_LEFT: c = K_UP; break;
3311 case K_RIGHT: c = K_DOWN; break;
3312 default: break;
3313 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003314 }
3315#endif
3316
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003317 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003318 {
3319 if (c == K_LEFT)
3320 c = Ctrl_P;
3321 else if (c == K_RIGHT)
3322 c = Ctrl_N;
3323 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003324
Bram Moolenaareadee482020-09-04 15:37:31 +02003325 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003326 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003327 && cclp->cmdpos > 1
3328 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3329 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3330 && (c == '\n' || c == '\r' || c == K_KENTER))
3331 c = K_DOWN;
3332
3333 return c;
3334}
3335
3336/*
3337 * Delete characters on the command line, from "from" to the current
3338 * position.
3339 */
3340 static void
3341cmdline_del(cmdline_info_T *cclp, int from)
3342{
3343 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3344 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3345 cclp->cmdlen -= cclp->cmdpos - from;
3346 cclp->cmdpos = from;
3347}
3348
3349/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003350 * Handle a key pressed when the wild menu for the menu names
3351 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003352 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003353 static int
3354wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003355{
Bram Moolenaareadee482020-09-04 15:37:31 +02003356 int i;
3357 int j;
3358
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003359 // Hitting <Down> after "emenu Name.": complete submenu
3360 if (key == K_DOWN && cclp->cmdpos > 0
3361 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003362 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003363 key = p_wc;
3364 KeyTyped = TRUE; // in case the key was mapped
3365 }
3366 else if (key == K_UP)
3367 {
3368 // Hitting <Up>: Remove one submenu name in front of the
3369 // cursor
3370 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003371
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003372 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3373 i = 0;
3374 while (--j > 0)
3375 {
3376 // check for start of menu name
3377 if (cclp->cmdbuff[j] == ' '
3378 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003379 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003380 i = j + 1;
3381 break;
3382 }
3383 // check for start of submenu name
3384 if (cclp->cmdbuff[j] == '.'
3385 && cclp->cmdbuff[j - 1] != '\\')
3386 {
3387 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003388 {
3389 i = j + 1;
3390 break;
3391 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003392 else
3393 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003394 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003395 }
3396 if (i > 0)
3397 cmdline_del(cclp, i);
3398 key = p_wc;
3399 KeyTyped = TRUE; // in case the key was mapped
3400 xp->xp_context = EXPAND_NOTHING;
3401 }
3402
3403 return key;
3404}
3405
3406/*
3407 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3408 * directory names (EXPAND_DIRECTORIES) or shell command names
3409 * (EXPAND_SHELLCMD) is displayed.
3410 */
3411 static int
3412wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3413{
3414 int i;
3415 int j;
3416 char_u upseg[5];
3417
3418 upseg[0] = PATHSEP;
3419 upseg[1] = '.';
3420 upseg[2] = '.';
3421 upseg[3] = PATHSEP;
3422 upseg[4] = NUL;
3423
3424 if (key == K_DOWN
3425 && cclp->cmdpos > 0
3426 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3427 && (cclp->cmdpos < 3
3428 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3429 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3430 {
3431 // go down a directory
3432 key = p_wc;
3433 KeyTyped = TRUE; // in case the key was mapped
3434 }
3435 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3436 {
3437 // If in a direct ancestor, strip off one ../ to go down
3438 int found = FALSE;
3439
3440 j = cclp->cmdpos;
3441 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3442 while (--j > i)
3443 {
3444 if (has_mbyte)
3445 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3446 if (vim_ispathsep(cclp->cmdbuff[j]))
3447 {
3448 found = TRUE;
3449 break;
3450 }
3451 }
3452 if (found
3453 && cclp->cmdbuff[j - 1] == '.'
3454 && cclp->cmdbuff[j - 2] == '.'
3455 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3456 {
3457 cmdline_del(cclp, j - 2);
3458 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003459 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003460 }
3461 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003462 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003463 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003464 // go up a directory
3465 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003466
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003467 j = cclp->cmdpos - 1;
3468 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3469 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003470 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003471 if (has_mbyte)
3472 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3473 if (vim_ispathsep(cclp->cmdbuff[j])
3474# ifdef BACKSLASH_IN_FILENAME
3475 && vim_strchr((char_u *)" *?[{`$%#",
3476 cclp->cmdbuff[j + 1]) == NULL
3477# endif
3478 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003479 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003480 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003481 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003482 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003483 break;
3484 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003485 else
3486 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003487 }
3488 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003489
3490 if (!found)
3491 j = i;
3492 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3493 j += 4;
3494 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3495 && j == i)
3496 j += 3;
3497 else
3498 j = 0;
3499 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003500 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003501 // TODO this is only for DOS/UNIX systems - need to put in
3502 // machine-specific stuff here and in upseg init
3503 cmdline_del(cclp, j);
3504 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003505 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003506 else if (cclp->cmdpos > i)
3507 cmdline_del(cclp, i);
3508
3509 // Now complete in the new directory. Set KeyTyped in case the
3510 // Up key came from a mapping.
3511 key = p_wc;
3512 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003513 }
3514
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003515 return key;
3516}
3517
3518/*
3519 * Handle a key pressed when the wild menu is displayed
3520 */
3521 int
3522wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3523{
3524 if (xp->xp_context == EXPAND_MENUNAMES)
3525 return wildmenu_process_key_menunames(cclp, key, xp);
3526 else if ((xp->xp_context == EXPAND_FILES
3527 || xp->xp_context == EXPAND_DIRECTORIES
3528 || xp->xp_context == EXPAND_SHELLCMD))
3529 return wildmenu_process_key_filenames(cclp, key, xp);
3530
3531 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003532}
3533
3534/*
3535 * Free expanded names when finished walking through the matches
3536 */
3537 void
3538wildmenu_cleanup(cmdline_info_T *cclp)
3539{
3540 int skt = KeyTyped;
3541 int old_RedrawingDisabled = RedrawingDisabled;
3542
3543 if (!p_wmnu || wild_menu_showing == 0)
3544 return;
3545
3546 if (cclp->input_fn)
3547 RedrawingDisabled = 0;
3548
3549 if (wild_menu_showing == WM_SCROLLED)
3550 {
3551 // Entered command line, move it up
3552 cmdline_row--;
3553 redrawcmd();
3554 }
3555 else if (save_p_ls != -1)
3556 {
3557 // restore 'laststatus' and 'winminheight'
3558 p_ls = save_p_ls;
3559 p_wmh = save_p_wmh;
3560 last_status(FALSE);
3561 update_screen(VALID); // redraw the screen NOW
3562 redrawcmd();
3563 save_p_ls = -1;
3564 }
3565 else
3566 {
3567 win_redraw_last_status(topframe);
3568 redraw_statuslines();
3569 }
3570 KeyTyped = skt;
3571 wild_menu_showing = 0;
3572 if (cclp->input_fn)
3573 RedrawingDisabled = old_RedrawingDisabled;
3574}
3575#endif
3576
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003577#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003578/*
3579 * "getcompletion()" function
3580 */
3581 void
3582f_getcompletion(typval_T *argvars, typval_T *rettv)
3583{
3584 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003585 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003586 expand_T xpc;
3587 int filtered = FALSE;
3588 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003589 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003590
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003591 if (in_vim9script()
3592 && (check_for_string_arg(argvars, 0) == FAIL
3593 || check_for_string_arg(argvars, 1) == FAIL
3594 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3595 return;
3596
ii144785fe02021-11-21 12:13:56 +00003597 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003598 if (argvars[1].v_type != VAR_STRING)
3599 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003600 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003601 return;
3602 }
3603 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003604
3605 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003606 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003607
3608 if (p_wic)
3609 options |= WILD_ICASE;
3610
3611 // For filtered results, 'wildignore' is used
3612 if (!filtered)
3613 options |= WILD_KEEP_ALL;
3614
3615 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003616 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003617 {
ii144785fe02021-11-21 12:13:56 +00003618 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003619 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003620 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003621 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003622 else
3623 {
ii144785fe02021-11-21 12:13:56 +00003624 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003625 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3626
3627 xpc.xp_context = cmdcomplete_str_to_type(type);
3628 if (xpc.xp_context == EXPAND_NOTHING)
3629 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003630 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003631 return;
3632 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003633
3634# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003635 if (xpc.xp_context == EXPAND_MENUS)
3636 {
3637 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3638 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3639 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003640# endif
3641# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003642 if (xpc.xp_context == EXPAND_CSCOPE)
3643 {
3644 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3645 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3646 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003647# endif
3648# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003649 if (xpc.xp_context == EXPAND_SIGN)
3650 {
3651 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3652 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3653 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003654# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003655 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003656
3657 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3658 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3659 {
3660 int i;
3661
3662 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3663
3664 for (i = 0; i < xpc.xp_numfiles; i++)
3665 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3666 }
3667 vim_free(pat);
3668 ExpandCleanup(&xpc);
3669}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003670#endif // FEAT_EVAL