blob: fee9f660995d7ce453f7d331158fb4d05322f91c [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
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001605/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001606 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1607 * The argument to the command is 'arg' and the argument flags is 'argt'.
1608 * For user-defined commands and for environment variables, 'compl' has the
1609 * completion type.
1610 * Returns a pointer to the next command. Returns NULL if there is no next
1611 * command.
1612 */
1613 static char_u *
1614set_context_by_cmdname(
1615 char_u *cmd,
1616 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001617 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001618 char_u *arg,
1619 long argt,
1620 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001621 int forceit)
1622{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001623 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001624 {
1625 case CMD_find:
1626 case CMD_sfind:
1627 case CMD_tabfind:
1628 if (xp->xp_context == EXPAND_FILES)
1629 xp->xp_context = EXPAND_FILES_IN_PATH;
1630 break;
1631 case CMD_cd:
1632 case CMD_chdir:
1633 case CMD_tcd:
1634 case CMD_tchdir:
1635 case CMD_lcd:
1636 case CMD_lchdir:
1637 if (xp->xp_context == EXPAND_FILES)
1638 xp->xp_context = EXPAND_DIRECTORIES;
1639 break;
1640 case CMD_help:
1641 xp->xp_context = EXPAND_HELP;
1642 xp->xp_pattern = arg;
1643 break;
1644
1645 // Command modifiers: return the argument.
1646 // Also for commands with an argument that is a command.
1647 case CMD_aboveleft:
1648 case CMD_argdo:
1649 case CMD_belowright:
1650 case CMD_botright:
1651 case CMD_browse:
1652 case CMD_bufdo:
1653 case CMD_cdo:
1654 case CMD_cfdo:
1655 case CMD_confirm:
1656 case CMD_debug:
1657 case CMD_folddoclosed:
1658 case CMD_folddoopen:
1659 case CMD_hide:
1660 case CMD_keepalt:
1661 case CMD_keepjumps:
1662 case CMD_keepmarks:
1663 case CMD_keeppatterns:
1664 case CMD_ldo:
1665 case CMD_leftabove:
1666 case CMD_lfdo:
1667 case CMD_lockmarks:
1668 case CMD_noautocmd:
1669 case CMD_noswapfile:
1670 case CMD_rightbelow:
1671 case CMD_sandbox:
1672 case CMD_silent:
1673 case CMD_tab:
1674 case CMD_tabdo:
1675 case CMD_topleft:
1676 case CMD_verbose:
1677 case CMD_vertical:
1678 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001679 case CMD_vim9cmd:
1680 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001681 return arg;
1682
1683 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001684 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001685
1686#ifdef FEAT_SEARCH_EXTRA
1687 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001688 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001689#endif
1690
1691 // All completion for the +cmdline_compl feature goes here.
1692
1693 case CMD_command:
1694 return set_context_in_user_cmd(xp, arg);
1695
1696 case CMD_delcommand:
1697 xp->xp_context = EXPAND_USER_COMMANDS;
1698 xp->xp_pattern = arg;
1699 break;
1700
1701 case CMD_global:
1702 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001703 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 case CMD_and:
1705 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001706 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001707 case CMD_isearch:
1708 case CMD_dsearch:
1709 case CMD_ilist:
1710 case CMD_dlist:
1711 case CMD_ijump:
1712 case CMD_psearch:
1713 case CMD_djump:
1714 case CMD_isplit:
1715 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001716 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001717 case CMD_autocmd:
1718 return set_context_in_autocmd(xp, arg, FALSE);
1719 case CMD_doautocmd:
1720 case CMD_doautoall:
1721 return set_context_in_autocmd(xp, arg, TRUE);
1722 case CMD_set:
1723 set_context_in_set_cmd(xp, arg, 0);
1724 break;
1725 case CMD_setglobal:
1726 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1727 break;
1728 case CMD_setlocal:
1729 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1730 break;
1731 case CMD_tag:
1732 case CMD_stag:
1733 case CMD_ptag:
1734 case CMD_ltag:
1735 case CMD_tselect:
1736 case CMD_stselect:
1737 case CMD_ptselect:
1738 case CMD_tjump:
1739 case CMD_stjump:
1740 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001741 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001742 xp->xp_context = EXPAND_TAGS_LISTFILES;
1743 else
1744 xp->xp_context = EXPAND_TAGS;
1745 xp->xp_pattern = arg;
1746 break;
1747 case CMD_augroup:
1748 xp->xp_context = EXPAND_AUGROUP;
1749 xp->xp_pattern = arg;
1750 break;
1751#ifdef FEAT_SYN_HL
1752 case CMD_syntax:
1753 set_context_in_syntax_cmd(xp, arg);
1754 break;
1755#endif
1756#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001757 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001758 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001759 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001760 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001761 case CMD_if:
1762 case CMD_elseif:
1763 case CMD_while:
1764 case CMD_for:
1765 case CMD_echo:
1766 case CMD_echon:
1767 case CMD_execute:
1768 case CMD_echomsg:
1769 case CMD_echoerr:
1770 case CMD_call:
1771 case CMD_return:
1772 case CMD_cexpr:
1773 case CMD_caddexpr:
1774 case CMD_cgetexpr:
1775 case CMD_lexpr:
1776 case CMD_laddexpr:
1777 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001778 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001779 break;
1780
1781 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001782 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001783 case CMD_function:
1784 case CMD_delfunction:
1785 xp->xp_context = EXPAND_USER_FUNC;
1786 xp->xp_pattern = arg;
1787 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001788 case CMD_disassemble:
1789 set_context_in_disassemble_cmd(xp, arg);
1790 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001791
1792 case CMD_echohl:
1793 set_context_in_echohl_cmd(xp, arg);
1794 break;
1795#endif
1796 case CMD_highlight:
1797 set_context_in_highlight_cmd(xp, arg);
1798 break;
1799#ifdef FEAT_CSCOPE
1800 case CMD_cscope:
1801 case CMD_lcscope:
1802 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001803 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001804 break;
1805#endif
1806#ifdef FEAT_SIGNS
1807 case CMD_sign:
1808 set_context_in_sign_cmd(xp, arg);
1809 break;
1810#endif
1811 case CMD_bdelete:
1812 case CMD_bwipeout:
1813 case CMD_bunload:
1814 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1815 arg = xp->xp_pattern + 1;
1816 // FALLTHROUGH
1817 case CMD_buffer:
1818 case CMD_sbuffer:
1819 case CMD_checktime:
1820 xp->xp_context = EXPAND_BUFFERS;
1821 xp->xp_pattern = arg;
1822 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001823#ifdef FEAT_DIFF
1824 case CMD_diffget:
1825 case CMD_diffput:
1826 // If current buffer is in diff mode, complete buffer names
1827 // which are in diff mode, and different than current buffer.
1828 xp->xp_context = EXPAND_DIFF_BUFFERS;
1829 xp->xp_pattern = arg;
1830 break;
1831#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001832 case CMD_USER:
1833 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001834 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1835 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001836
1837 case CMD_map: case CMD_noremap:
1838 case CMD_nmap: case CMD_nnoremap:
1839 case CMD_vmap: case CMD_vnoremap:
1840 case CMD_omap: case CMD_onoremap:
1841 case CMD_imap: case CMD_inoremap:
1842 case CMD_cmap: case CMD_cnoremap:
1843 case CMD_lmap: case CMD_lnoremap:
1844 case CMD_smap: case CMD_snoremap:
1845 case CMD_tmap: case CMD_tnoremap:
1846 case CMD_xmap: case CMD_xnoremap:
1847 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001848 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001849 case CMD_unmap:
1850 case CMD_nunmap:
1851 case CMD_vunmap:
1852 case CMD_ounmap:
1853 case CMD_iunmap:
1854 case CMD_cunmap:
1855 case CMD_lunmap:
1856 case CMD_sunmap:
1857 case CMD_tunmap:
1858 case CMD_xunmap:
1859 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001860 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001861 case CMD_mapclear:
1862 case CMD_nmapclear:
1863 case CMD_vmapclear:
1864 case CMD_omapclear:
1865 case CMD_imapclear:
1866 case CMD_cmapclear:
1867 case CMD_lmapclear:
1868 case CMD_smapclear:
1869 case CMD_tmapclear:
1870 case CMD_xmapclear:
1871 xp->xp_context = EXPAND_MAPCLEAR;
1872 xp->xp_pattern = arg;
1873 break;
1874
1875 case CMD_abbreviate: case CMD_noreabbrev:
1876 case CMD_cabbrev: case CMD_cnoreabbrev:
1877 case CMD_iabbrev: case CMD_inoreabbrev:
1878 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001879 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001880 case CMD_unabbreviate:
1881 case CMD_cunabbrev:
1882 case CMD_iunabbrev:
1883 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001884 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001885#ifdef FEAT_MENU
1886 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1887 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1888 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1889 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1890 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1891 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1892 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1893 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1894 case CMD_tmenu: case CMD_tunmenu:
1895 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1896 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1897#endif
1898
1899 case CMD_colorscheme:
1900 xp->xp_context = EXPAND_COLORS;
1901 xp->xp_pattern = arg;
1902 break;
1903
1904 case CMD_compiler:
1905 xp->xp_context = EXPAND_COMPILER;
1906 xp->xp_pattern = arg;
1907 break;
1908
1909 case CMD_ownsyntax:
1910 xp->xp_context = EXPAND_OWNSYNTAX;
1911 xp->xp_pattern = arg;
1912 break;
1913
1914 case CMD_setfiletype:
1915 xp->xp_context = EXPAND_FILETYPE;
1916 xp->xp_pattern = arg;
1917 break;
1918
1919 case CMD_packadd:
1920 xp->xp_context = EXPAND_PACKADD;
1921 xp->xp_pattern = arg;
1922 break;
1923
1924#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1925 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001926 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001927#endif
1928#if defined(FEAT_PROFILE)
1929 case CMD_profile:
1930 set_context_in_profile_cmd(xp, arg);
1931 break;
1932#endif
1933 case CMD_behave:
1934 xp->xp_context = EXPAND_BEHAVE;
1935 xp->xp_pattern = arg;
1936 break;
1937
1938 case CMD_messages:
1939 xp->xp_context = EXPAND_MESSAGES;
1940 xp->xp_pattern = arg;
1941 break;
1942
1943 case CMD_history:
1944 xp->xp_context = EXPAND_HISTORY;
1945 xp->xp_pattern = arg;
1946 break;
1947#if defined(FEAT_PROFILE)
1948 case CMD_syntime:
1949 xp->xp_context = EXPAND_SYNTIME;
1950 xp->xp_pattern = arg;
1951 break;
1952#endif
1953
1954 case CMD_argdelete:
1955 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1956 arg = xp->xp_pattern + 1;
1957 xp->xp_context = EXPAND_ARGLIST;
1958 xp->xp_pattern = arg;
1959 break;
1960
1961 default:
1962 break;
1963 }
1964 return NULL;
1965}
1966
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001967/*
1968 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
1969 * we don't need/want deleted. Maybe this could be done better if we didn't
1970 * repeat all this stuff. The only problem is that they may not stay
1971 * perfectly compatible with each other, but then the command line syntax
1972 * probably won't change that much -- webb.
1973 */
1974 static char_u *
1975set_one_cmd_context(
1976 expand_T *xp,
1977 char_u *buff) // buffer for command string
1978{
1979 char_u *p;
1980 char_u *cmd, *arg;
1981 int len = 0;
1982 exarg_T ea;
1983 int compl = EXPAND_NOTHING;
1984 int forceit = FALSE;
1985 int usefilter = FALSE; // filter instead of file name
1986
1987 ExpandInit(xp);
1988 xp->xp_pattern = buff;
1989 xp->xp_line = buff;
1990 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
1991 ea.argt = 0;
1992
1993 // 1. skip comment lines and leading space, colons or bars
1994 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
1995 ;
1996 xp->xp_pattern = cmd;
1997
1998 if (*cmd == NUL)
1999 return NULL;
2000 if (*cmd == '"') // ignore comment lines
2001 {
2002 xp->xp_context = EXPAND_NOTHING;
2003 return NULL;
2004 }
2005
2006 // 3. Skip over the range to find the command.
2007 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2008 xp->xp_pattern = cmd;
2009 if (*cmd == NUL)
2010 return NULL;
2011 if (*cmd == '"')
2012 {
2013 xp->xp_context = EXPAND_NOTHING;
2014 return NULL;
2015 }
2016
2017 if (*cmd == '|' || *cmd == '\n')
2018 return cmd + 1; // There's another command
2019
2020 // Get the command index.
2021 p = set_cmd_index(cmd, &ea, xp, &compl);
2022 if (p == NULL)
2023 return NULL;
2024
2025 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2026
2027 if (*p == '!') // forced commands
2028 {
2029 forceit = TRUE;
2030 ++p;
2031 }
2032
2033 // 6. parse arguments
2034 if (!IS_USER_CMDIDX(ea.cmdidx))
2035 ea.argt = excmd_get_argt(ea.cmdidx);
2036
2037 arg = skipwhite(p);
2038
2039 // Skip over ++argopt argument
2040 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2041 {
2042 p = arg;
2043 while (*p && !vim_isspace(*p))
2044 MB_PTR_ADV(p);
2045 arg = skipwhite(p);
2046 }
2047
2048 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2049 {
2050 if (*arg == '>') // append
2051 {
2052 if (*++arg == '>')
2053 ++arg;
2054 arg = skipwhite(arg);
2055 }
2056 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2057 {
2058 ++arg;
2059 usefilter = TRUE;
2060 }
2061 }
2062
2063 if (ea.cmdidx == CMD_read)
2064 {
2065 usefilter = forceit; // :r! filter if forced
2066 if (*arg == '!') // :r !filter
2067 {
2068 ++arg;
2069 usefilter = TRUE;
2070 }
2071 }
2072
2073 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2074 {
2075 while (*arg == *cmd) // allow any number of '>' or '<'
2076 ++arg;
2077 arg = skipwhite(arg);
2078 }
2079
2080 // Does command allow "+command"?
2081 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2082 {
2083 // Check if we're in the +command
2084 p = arg + 1;
2085 arg = skip_cmd_arg(arg, FALSE);
2086
2087 // Still touching the command after '+'?
2088 if (*arg == NUL)
2089 return p;
2090
2091 // Skip space(s) after +command to get to the real argument
2092 arg = skipwhite(arg);
2093 }
2094
2095
2096 // Check for '|' to separate commands and '"' to start comments.
2097 // Don't do this for ":read !cmd" and ":write !cmd".
2098 if ((ea.argt & EX_TRLBAR) && !usefilter)
2099 {
2100 p = arg;
2101 // ":redir @" is not the start of a comment
2102 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2103 p += 2;
2104 while (*p)
2105 {
2106 if (*p == Ctrl_V)
2107 {
2108 if (p[1] != NUL)
2109 ++p;
2110 }
2111 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2112 || *p == '|' || *p == '\n')
2113 {
2114 if (*(p - 1) != '\\')
2115 {
2116 if (*p == '|' || *p == '\n')
2117 return p + 1;
2118 return NULL; // It's a comment
2119 }
2120 }
2121 MB_PTR_ADV(p);
2122 }
2123 }
2124
2125 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2126 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2127 // no arguments allowed but there is something
2128 return NULL;
2129
2130 // Find start of last argument (argument just before cursor):
2131 p = buff;
2132 xp->xp_pattern = p;
2133 len = (int)STRLEN(buff);
2134 while (*p && p < buff + len)
2135 {
2136 if (*p == ' ' || *p == TAB)
2137 {
2138 // argument starts after a space
2139 xp->xp_pattern = ++p;
2140 }
2141 else
2142 {
2143 if (*p == '\\' && *(p + 1) != NUL)
2144 ++p; // skip over escaped character
2145 MB_PTR_ADV(p);
2146 }
2147 }
2148
2149 if (ea.argt & EX_XFILE)
2150 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2151
2152 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002153 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002154 forceit);
2155}
2156
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002157/*
2158 * Set the completion context in 'xp' for command 'str'
2159 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002160 void
2161set_cmd_context(
2162 expand_T *xp,
2163 char_u *str, // start of command line
2164 int len, // length of command line (excl. NUL)
2165 int col, // position of cursor
2166 int use_ccline UNUSED) // use ccline for info
2167{
2168#ifdef FEAT_EVAL
2169 cmdline_info_T *ccline = get_cmdline_info();
2170#endif
2171 int old_char = NUL;
2172 char_u *nextcomm;
2173
2174 // Avoid a UMR warning from Purify, only save the character if it has been
2175 // written before.
2176 if (col < len)
2177 old_char = str[col];
2178 str[col] = NUL;
2179 nextcomm = str;
2180
2181#ifdef FEAT_EVAL
2182 if (use_ccline && ccline->cmdfirstc == '=')
2183 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002184 // pass CMD_SIZE because there is no real command
2185 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002186 }
2187 else if (use_ccline && ccline->input_fn)
2188 {
2189 xp->xp_context = ccline->xp_context;
2190 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002191 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002192 }
2193 else
2194#endif
2195 while (nextcomm != NULL)
2196 nextcomm = set_one_cmd_context(xp, nextcomm);
2197
2198 // Store the string here so that call_user_expand_func() can get to them
2199 // easily.
2200 xp->xp_line = str;
2201 xp->xp_col = col;
2202
2203 str[col] = old_char;
2204}
2205
2206/*
2207 * Expand the command line "str" from context "xp".
2208 * "xp" must have been set by set_cmd_context().
2209 * xp->xp_pattern points into "str", to where the text that is to be expanded
2210 * starts.
2211 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2212 * cursor.
2213 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2214 * key that triggered expansion literally.
2215 * Returns EXPAND_OK otherwise.
2216 */
2217 int
2218expand_cmdline(
2219 expand_T *xp,
2220 char_u *str, // start of command line
2221 int col, // position of cursor
2222 int *matchcount, // return: nr of matches
2223 char_u ***matches) // return: array of pointers to matches
2224{
2225 char_u *file_str = NULL;
2226 int options = WILD_ADD_SLASH|WILD_SILENT;
2227
2228 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2229 {
2230 beep_flush();
2231 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2232 }
2233 if (xp->xp_context == EXPAND_NOTHING)
2234 {
2235 // Caller can use the character as a normal char instead
2236 return EXPAND_NOTHING;
2237 }
2238
2239 // add star to file name, or convert to regexp if not exp. files.
2240 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002241 if (cmdline_fuzzy_completion_supported(xp))
2242 // If fuzzy matching, don't modify the search string
2243 file_str = vim_strsave(xp->xp_pattern);
2244 else
2245 {
2246 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2247 if (file_str == NULL)
2248 return EXPAND_UNSUCCESSFUL;
2249 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002250
2251 if (p_wic)
2252 options += WILD_ICASE;
2253
2254 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002255 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002256 {
2257 *matchcount = 0;
2258 *matches = NULL;
2259 }
2260 vim_free(file_str);
2261
2262 return EXPAND_OK;
2263}
2264
Bram Moolenaar66b51422019-08-18 21:44:12 +02002265/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002266 * Expand file or directory names.
2267 */
2268 static int
2269expand_files_and_dirs(
2270 expand_T *xp,
2271 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002272 char_u ***matches,
2273 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002274 int flags,
2275 int options)
2276{
2277 int free_pat = FALSE;
2278 int i;
2279 int ret;
2280
2281 // for ":set path=" and ":set tags=" halve backslashes for escaped
2282 // space
2283 if (xp->xp_backslash != XP_BS_NONE)
2284 {
2285 free_pat = TRUE;
2286 pat = vim_strsave(pat);
2287 for (i = 0; pat[i]; ++i)
2288 if (pat[i] == '\\')
2289 {
2290 if (xp->xp_backslash == XP_BS_THREE
2291 && pat[i + 1] == '\\'
2292 && pat[i + 2] == '\\'
2293 && pat[i + 3] == ' ')
2294 STRMOVE(pat + i, pat + i + 3);
2295 if (xp->xp_backslash == XP_BS_ONE
2296 && pat[i + 1] == ' ')
2297 STRMOVE(pat + i, pat + i + 1);
2298 }
2299 }
2300
2301 if (xp->xp_context == EXPAND_FILES)
2302 flags |= EW_FILE;
2303 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2304 flags |= (EW_FILE | EW_PATH);
2305 else
2306 flags = (flags | EW_DIR) & ~EW_FILE;
2307 if (options & WILD_ICASE)
2308 flags |= EW_ICASE;
2309
2310 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002311 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002312 if (free_pat)
2313 vim_free(pat);
2314#ifdef BACKSLASH_IN_FILENAME
2315 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2316 {
2317 int j;
2318
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002319 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002320 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002321 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002322
2323 while (*ptr != NUL)
2324 {
2325 if (p_csl[0] == 's' && *ptr == '\\')
2326 *ptr = '/';
2327 else if (p_csl[0] == 'b' && *ptr == '/')
2328 *ptr = '\\';
2329 ptr += (*mb_ptr2len)(ptr);
2330 }
2331 }
2332 }
2333#endif
2334 return ret;
2335}
2336
2337/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002338 * Function given to ExpandGeneric() to obtain the possible arguments of the
2339 * ":behave {mswin,xterm}" command.
2340 */
2341 static char_u *
2342get_behave_arg(expand_T *xp UNUSED, int idx)
2343{
2344 if (idx == 0)
2345 return (char_u *)"mswin";
2346 if (idx == 1)
2347 return (char_u *)"xterm";
2348 return NULL;
2349}
2350
2351/*
2352 * Function given to ExpandGeneric() to obtain the possible arguments of the
2353 * ":messages {clear}" command.
2354 */
2355 static char_u *
2356get_messages_arg(expand_T *xp UNUSED, int idx)
2357{
2358 if (idx == 0)
2359 return (char_u *)"clear";
2360 return NULL;
2361}
2362
2363 static char_u *
2364get_mapclear_arg(expand_T *xp UNUSED, int idx)
2365{
2366 if (idx == 0)
2367 return (char_u *)"<buffer>";
2368 return NULL;
2369}
2370
2371/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002372 * Do the expansion based on xp->xp_context and 'rmp'.
2373 */
2374 static int
2375ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002376 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002377 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002378 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002379 char_u ***matches,
2380 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002381{
2382 static struct expgen
2383 {
2384 int context;
2385 char_u *((*func)(expand_T *, int));
2386 int ic;
2387 int escaped;
2388 } tab[] =
2389 {
2390 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2391 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2392 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2393 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2394 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2395 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2396 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2397 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2398 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2399 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2400# ifdef FEAT_EVAL
2401 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2402 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2403 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2404 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2405 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2406# endif
2407# ifdef FEAT_MENU
2408 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2409 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2410# endif
2411# ifdef FEAT_SYN_HL
2412 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2413# endif
2414# ifdef FEAT_PROFILE
2415 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2416# endif
2417 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2418 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2419 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
2420# ifdef FEAT_CSCOPE
2421 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2422# endif
2423# ifdef FEAT_SIGNS
2424 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2425# endif
2426# ifdef FEAT_PROFILE
2427 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2428# endif
2429# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2430 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2431 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2432# endif
2433 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2434 {EXPAND_USER, get_users, TRUE, FALSE},
2435 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2436 };
2437 int i;
2438 int ret = FAIL;
2439
2440 // Find a context in the table and call the ExpandGeneric() with the
2441 // right function to do the expansion.
2442 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2443 {
2444 if (xp->xp_context == tab[i].context)
2445 {
2446 if (tab[i].ic)
2447 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002448 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
2449 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002450 break;
2451 }
2452 }
2453
2454 return ret;
2455}
2456
2457/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002458 * Map wild expand options to flags for expand_wildcards()
2459 */
2460 static int
2461map_wildopts_to_ewflags(int options)
2462{
2463 int flags;
2464
2465 flags = EW_DIR; // include directories
2466 if (options & WILD_LIST_NOTFOUND)
2467 flags |= EW_NOTFOUND;
2468 if (options & WILD_ADD_SLASH)
2469 flags |= EW_ADDSLASH;
2470 if (options & WILD_KEEP_ALL)
2471 flags |= EW_KEEPALL;
2472 if (options & WILD_SILENT)
2473 flags |= EW_SILENT;
2474 if (options & WILD_NOERROR)
2475 flags |= EW_NOERROR;
2476 if (options & WILD_ALLLINKS)
2477 flags |= EW_ALLLINKS;
2478
2479 return flags;
2480}
2481
2482/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002483 * Do the expansion based on xp->xp_context and "pat".
2484 */
2485 static int
2486ExpandFromContext(
2487 expand_T *xp,
2488 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002489 char_u ***matches,
2490 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002491 int options) // WILD_ flags
2492{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002493 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002494 int ret;
2495 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002496 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002497 int fuzzy = cmdline_fuzzy_complete(pat)
2498 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002499
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002500 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002501
2502 if (xp->xp_context == EXPAND_FILES
2503 || xp->xp_context == EXPAND_DIRECTORIES
2504 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002505 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2506 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002507
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002508 *matches = (char_u **)"";
2509 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002510 if (xp->xp_context == EXPAND_HELP)
2511 {
2512 // With an empty argument we would get all the help tags, which is
2513 // very slow. Get matches for "help" instead.
2514 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002515 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002516 {
2517#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002518 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002519#endif
2520 return OK;
2521 }
2522 return FAIL;
2523 }
2524
Bram Moolenaar66b51422019-08-18 21:44:12 +02002525 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002526 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002527 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002528 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002529 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002530 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002531#ifdef FEAT_DIFF
2532 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002533 return ExpandBufnames(pat, numMatches, matches,
2534 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002535#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002536 if (xp->xp_context == EXPAND_TAGS
2537 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002538 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2539 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002540 if (xp->xp_context == EXPAND_COLORS)
2541 {
2542 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002543 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002544 directories);
2545 }
2546 if (xp->xp_context == EXPAND_COMPILER)
2547 {
2548 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002549 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002550 }
2551 if (xp->xp_context == EXPAND_OWNSYNTAX)
2552 {
2553 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002554 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002555 }
2556 if (xp->xp_context == EXPAND_FILETYPE)
2557 {
2558 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002559 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002560 }
2561# if defined(FEAT_EVAL)
2562 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002563 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002564# endif
2565 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002566 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002567
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002568 // When expanding a function name starting with s:, match the <SNR>nr_
2569 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002570 if ((xp->xp_context == EXPAND_USER_FUNC
2571 || xp->xp_context == EXPAND_DISASSEMBLE)
2572 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002573 {
2574 int len = (int)STRLEN(pat) + 20;
2575
2576 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002577 if (tofree == NULL)
2578 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002579 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002580 pat = tofree;
2581 }
2582
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002583 if (!fuzzy)
2584 {
2585 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2586 if (regmatch.regprog == NULL)
2587 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002588
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002589 // set ignore-case according to p_ic, p_scs and pat
2590 regmatch.rm_ic = ignorecase(pat);
2591 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002592
2593 if (xp->xp_context == EXPAND_SETTINGS
2594 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002595 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002596 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002597 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002598# if defined(FEAT_EVAL)
2599 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002600 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002601# endif
2602 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002603 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002604
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002605 if (!fuzzy)
2606 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002607 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002608
2609 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002610}
2611
Bram Moolenaar66b51422019-08-18 21:44:12 +02002612/*
2613 * Expand a list of names.
2614 *
2615 * Generic function for command line completion. It calls a function to
2616 * obtain strings, one by one. The strings are matched against a regexp
2617 * program. Matching strings are copied into an array, which is returned.
2618 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002619 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2620 * is used.
2621 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002622 * Returns OK when no problems encountered, FAIL for error (out of memory).
2623 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002624 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002625ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002626 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002627 expand_T *xp,
2628 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002629 char_u ***matches,
2630 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002631 char_u *((*func)(expand_T *, int)),
2632 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002633 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002634{
2635 int i;
2636 int count = 0;
2637 int round;
2638 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002639 fuzmatch_str_T *fuzmatch = NULL;
2640 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002641 int fuzzy;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002642 int funcsort = FALSE;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002643 int match;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002644
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002645 fuzzy = cmdline_fuzzy_complete(pat);
2646
Bram Moolenaar66b51422019-08-18 21:44:12 +02002647 // do this loop twice:
2648 // round == 0: count the number of matching names
2649 // round == 1: copy the matching names into allocated memory
2650 for (round = 0; round <= 1; ++round)
2651 {
2652 for (i = 0; ; ++i)
2653 {
2654 str = (*func)(xp, i);
2655 if (str == NULL) // end of list
2656 break;
2657 if (*str == NUL) // skip empty strings
2658 continue;
2659
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002660 if (!fuzzy)
2661 match = vim_regexec(regmatch, str, (colnr_T)0);
2662 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002663 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002664 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002665 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002666 }
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002667
2668 if (!match)
2669 continue;
2670
2671 if (round)
2672 {
2673 if (escaped)
2674 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2675 else
2676 str = vim_strsave(str);
2677 if (str == NULL)
2678 {
2679 if (fuzzy)
2680 fuzmatch_str_free(fuzmatch, count);
2681 else if (count > 0)
2682 FreeWild(count, *matches);
2683 *numMatches = 0;
2684 *matches = NULL;
2685 return FAIL;
2686 }
2687 if (fuzzy)
2688 {
2689 fuzmatch[count].idx = count;
2690 fuzmatch[count].str = str;
2691 fuzmatch[count].score = score;
2692 }
2693 else
2694 (*matches)[count] = str;
2695# ifdef FEAT_MENU
2696 if (func == get_menu_names && str != NULL)
2697 {
2698 // test for separator added by get_menu_names()
2699 str += STRLEN(str) - 1;
2700 if (*str == '\001')
2701 *str = '.';
2702 }
2703# endif
2704 }
2705 ++count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002706 }
2707 if (round == 0)
2708 {
2709 if (count == 0)
2710 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002711 if (fuzzy)
2712 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2713 else
2714 *matches = ALLOC_MULT(char_u *, count);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002715 if ((!fuzzy && (*matches == NULL))
2716 || (fuzzy && (fuzmatch == NULL)))
Bram Moolenaar66b51422019-08-18 21:44:12 +02002717 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002718 *numMatches = 0;
2719 *matches = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002720 return FAIL;
2721 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002722 *numMatches = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002723 count = 0;
2724 }
2725 }
2726
2727 // Sort the results. Keep menu's in the specified order.
2728 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2729 {
2730 if (xp->xp_context == EXPAND_EXPRESSION
2731 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002732 || xp->xp_context == EXPAND_USER_FUNC
2733 || xp->xp_context == EXPAND_DISASSEMBLE)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002734 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002735 // <SNR> functions should be sorted to the end.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002736 funcsort = TRUE;
2737 if (!fuzzy)
2738 qsort((void *)*matches, (size_t)*numMatches, sizeof(char_u *),
Bram Moolenaar66b51422019-08-18 21:44:12 +02002739 sort_func_compare);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002740 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002741 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002742 {
2743 if (!fuzzy)
2744 sort_strings(*matches, *numMatches);
2745 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002746 }
2747
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002748#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002749 // Reset the variables used for special highlight names expansion, so that
2750 // they don't show up when getting normal highlight names by ID.
2751 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002752#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002753
2754 if (fuzzy && fuzzymatches_to_strmatches(fuzmatch, matches, count,
2755 funcsort) == FAIL)
2756 return FAIL;
2757
Bram Moolenaar66b51422019-08-18 21:44:12 +02002758 return OK;
2759}
2760
2761/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002762 * Expand shell command matches in one directory of $PATH.
2763 */
2764 static void
2765expand_shellcmd_onedir(
2766 char_u *buf,
2767 char_u *s,
2768 size_t l,
2769 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002770 char_u ***matches,
2771 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002772 int flags,
2773 hashtab_T *ht,
2774 garray_T *gap)
2775{
2776 int ret;
2777 int i;
2778 hash_T hash;
2779 hashitem_T *hi;
2780
2781 vim_strncpy(buf, s, l);
2782 add_pathsep(buf);
2783 l = STRLEN(buf);
2784 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2785
2786 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002787 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002788 if (ret == OK)
2789 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002790 if (ga_grow(gap, *numMatches) == FAIL)
2791 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002792 else
2793 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002794 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002795 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002796 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002797
2798 if (STRLEN(name) > l)
2799 {
2800 // Check if this name was already found.
2801 hash = hash_hash(name + l);
2802 hi = hash_lookup(ht, name + l, hash);
2803 if (HASHITEM_EMPTY(hi))
2804 {
2805 // Remove the path that was prepended.
2806 STRMOVE(name, name + l);
2807 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2808 hash_add_item(ht, hi, name, hash);
2809 name = NULL;
2810 }
2811 }
2812 vim_free(name);
2813 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002814 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002815 }
2816 }
2817}
2818
2819/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002820 * Complete a shell command.
2821 * Returns FAIL or OK;
2822 */
2823 static int
2824expand_shellcmd(
2825 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002826 char_u ***matches, // return: array with matches
2827 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02002828 int flagsarg) // EW_ flags
2829{
2830 char_u *pat;
2831 int i;
2832 char_u *path = NULL;
2833 int mustfree = FALSE;
2834 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002835 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002836 size_t l;
2837 char_u *s, *e;
2838 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002839 int did_curdir = FALSE;
2840 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002841
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002842 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002843 if (buf == NULL)
2844 return FAIL;
2845
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002846 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002847 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002848 if (pat == NULL)
2849 {
2850 vim_free(buf);
2851 return FAIL;
2852 }
2853
Bram Moolenaar66b51422019-08-18 21:44:12 +02002854 for (i = 0; pat[i]; ++i)
2855 if (pat[i] == '\\' && pat[i + 1] == ' ')
2856 STRMOVE(pat + i, pat + i + 1);
2857
2858 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2859
2860 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2861 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2862 path = (char_u *)".";
2863 else
2864 {
2865 // For an absolute name we don't use $PATH.
2866 if (!mch_isFullName(pat))
2867 path = vim_getenv((char_u *)"PATH", &mustfree);
2868 if (path == NULL)
2869 path = (char_u *)"";
2870 }
2871
2872 // Go over all directories in $PATH. Expand matches in that directory and
2873 // collect them in "ga". When "." is not in $PATH also expand for the
2874 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002875 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002876 hash_init(&found_ht);
2877 for (s = path; ; s = e)
2878 {
2879# if defined(MSWIN)
2880 e = vim_strchr(s, ';');
2881# else
2882 e = vim_strchr(s, ':');
2883# endif
2884 if (e == NULL)
2885 e = s + STRLEN(s);
2886
2887 if (*s == NUL)
2888 {
2889 if (did_curdir)
2890 break;
2891 // Find directories in the current directory, path is empty.
2892 did_curdir = TRUE;
2893 flags |= EW_DIR;
2894 }
2895 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2896 {
2897 did_curdir = TRUE;
2898 flags |= EW_DIR;
2899 }
2900 else
2901 // Do not match directories inside a $PATH item.
2902 flags &= ~EW_DIR;
2903
2904 l = e - s;
2905 if (l > MAXPATHL - 5)
2906 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002907
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002908 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002909 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002910
Bram Moolenaar66b51422019-08-18 21:44:12 +02002911 if (*e != NUL)
2912 ++e;
2913 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002914 *matches = ga.ga_data;
2915 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002916
2917 vim_free(buf);
2918 vim_free(pat);
2919 if (mustfree)
2920 vim_free(path);
2921 hash_clear(&found_ht);
2922 return OK;
2923}
2924
2925# if defined(FEAT_EVAL)
2926/*
2927 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002928 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002929 */
2930 static void *
2931call_user_expand_func(
2932 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002933 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002934{
2935 cmdline_info_T *ccline = get_cmdline_info();
2936 int keep = 0;
2937 typval_T args[4];
2938 sctx_T save_current_sctx = current_sctx;
2939 char_u *pat = NULL;
2940 void *ret;
2941
2942 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2943 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002944
2945 if (ccline->cmdbuff != NULL)
2946 {
2947 keep = ccline->cmdbuff[ccline->cmdlen];
2948 ccline->cmdbuff[ccline->cmdlen] = 0;
2949 }
2950
2951 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2952
2953 args[0].v_type = VAR_STRING;
2954 args[0].vval.v_string = pat;
2955 args[1].v_type = VAR_STRING;
2956 args[1].vval.v_string = xp->xp_line;
2957 args[2].v_type = VAR_NUMBER;
2958 args[2].vval.v_number = xp->xp_col;
2959 args[3].v_type = VAR_UNKNOWN;
2960
2961 current_sctx = xp->xp_script_ctx;
2962
2963 ret = user_expand_func(xp->xp_arg, 3, args);
2964
2965 current_sctx = save_current_sctx;
2966 if (ccline->cmdbuff != NULL)
2967 ccline->cmdbuff[ccline->cmdlen] = keep;
2968
2969 vim_free(pat);
2970 return ret;
2971}
2972
2973/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002974 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
2975 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002976 */
2977 static int
2978ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002979 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002980 expand_T *xp,
2981 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002982 char_u ***matches,
2983 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002984{
2985 char_u *retstr;
2986 char_u *s;
2987 char_u *e;
2988 int keep;
2989 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00002990 int fuzzy;
2991 int match;
2992 int score;
2993 int count = 0;
2994
2995 fuzzy = cmdline_fuzzy_complete(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002996
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002997 *matches = NULL;
2998 *numMatches = 0;
2999 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003000 if (retstr == NULL)
3001 return FAIL;
3002
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003003 if (!fuzzy)
3004 ga_init2(&ga, sizeof(char *), 3);
3005 else
3006 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3007
Bram Moolenaar66b51422019-08-18 21:44:12 +02003008 for (s = retstr; *s != NUL; s = e)
3009 {
3010 e = vim_strchr(s, '\n');
3011 if (e == NULL)
3012 e = s + STRLEN(s);
3013 keep = *e;
3014 *e = NUL;
3015
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003016 if (xp->xp_pattern[0] || fuzzy)
3017 {
3018 if (!fuzzy)
3019 match = vim_regexec(regmatch, s, (colnr_T)0);
3020 else
3021 {
3022 score = fuzzy_match_str(s, pat);
3023 match = (score != 0);
3024 }
3025 }
3026 else
3027 match = TRUE; // match everything
3028
Bram Moolenaar66b51422019-08-18 21:44:12 +02003029 *e = keep;
3030
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003031 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003032 {
3033 if (ga_grow(&ga, 1) == FAIL)
3034 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003035 if (!fuzzy)
3036 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3037 else
3038 {
3039 fuzmatch_str_T *fuzmatch =
3040 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3041 fuzmatch->idx = count;
3042 fuzmatch->str = vim_strnsave(s, e - s);
3043 fuzmatch->score = score;
3044 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003045 ++ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003046 count++;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003047 }
3048
3049 if (*e != NUL)
3050 ++e;
3051 }
3052 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003053
3054 if (!fuzzy)
3055 {
3056 *matches = ga.ga_data;
3057 *numMatches = ga.ga_len;
3058 }
3059 else
3060 {
3061 if (fuzzymatches_to_strmatches(ga.ga_data, matches, count,
3062 FALSE) == FAIL)
3063 return FAIL;
3064 *numMatches = count;
3065 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003066 return OK;
3067}
3068
3069/*
3070 * Expand names with a list returned by a function defined by the user.
3071 */
3072 static int
3073ExpandUserList(
3074 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003075 char_u ***matches,
3076 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003077{
3078 list_T *retlist;
3079 listitem_T *li;
3080 garray_T ga;
3081
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003082 *matches = NULL;
3083 *numMatches = 0;
3084 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003085 if (retlist == NULL)
3086 return FAIL;
3087
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003088 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003089 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003090 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003091 {
3092 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3093 continue; // Skip non-string items and empty strings
3094
3095 if (ga_grow(&ga, 1) == FAIL)
3096 break;
3097
3098 ((char_u **)ga.ga_data)[ga.ga_len] =
3099 vim_strsave(li->li_tv.vval.v_string);
3100 ++ga.ga_len;
3101 }
3102 list_unref(retlist);
3103
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003104 *matches = ga.ga_data;
3105 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003106 return OK;
3107}
3108# endif
3109
3110/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111 * Expand "file" for all comma-separated directories in "path".
3112 * Adds the matches to "ga". Caller must init "ga".
3113 */
3114 void
3115globpath(
3116 char_u *path,
3117 char_u *file,
3118 garray_T *ga,
3119 int expand_options)
3120{
3121 expand_T xpc;
3122 char_u *buf;
3123 int i;
3124 int num_p;
3125 char_u **p;
3126
3127 buf = alloc(MAXPATHL);
3128 if (buf == NULL)
3129 return;
3130
3131 ExpandInit(&xpc);
3132 xpc.xp_context = EXPAND_FILES;
3133
3134 // Loop over all entries in {path}.
3135 while (*path != NUL)
3136 {
3137 // Copy one item of the path to buf[] and concatenate the file name.
3138 copy_option_part(&path, buf, MAXPATHL, ",");
3139 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3140 {
3141# if defined(MSWIN)
3142 // Using the platform's path separator (\) makes vim incorrectly
3143 // treat it as an escape character, use '/' instead.
3144 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3145 STRCAT(buf, "/");
3146# else
3147 add_pathsep(buf);
3148# endif
3149 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003150 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003151 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3152 {
3153 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3154
3155 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003156 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003157 for (i = 0; i < num_p; ++i)
3158 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003159 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003160 ++ga->ga_len;
3161 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003162 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003163 }
3164 }
3165 }
3166
3167 vim_free(buf);
3168}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003169
Bram Moolenaareadee482020-09-04 15:37:31 +02003170#ifdef FEAT_WILDMENU
3171
3172/*
3173 * Translate some keys pressed when 'wildmenu' is used.
3174 */
3175 int
3176wildmenu_translate_key(
3177 cmdline_info_T *cclp,
3178 int key,
3179 expand_T *xp,
3180 int did_wild_list)
3181{
3182 int c = key;
3183
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003184#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003185 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003186 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003187 // When the popup menu is used for cmdline completion:
3188 // Up : go to the previous item in the menu
3189 // Down : go to the next item in the menu
3190 // Left : go to the parent directory
3191 // Right: list the files in the selected directory
3192 switch (c)
3193 {
3194 case K_UP: c = K_LEFT; break;
3195 case K_DOWN: c = K_RIGHT; break;
3196 case K_LEFT: c = K_UP; break;
3197 case K_RIGHT: c = K_DOWN; break;
3198 default: break;
3199 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003200 }
3201#endif
3202
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003203 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003204 {
3205 if (c == K_LEFT)
3206 c = Ctrl_P;
3207 else if (c == K_RIGHT)
3208 c = Ctrl_N;
3209 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003210
Bram Moolenaareadee482020-09-04 15:37:31 +02003211 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003212 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003213 && cclp->cmdpos > 1
3214 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3215 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3216 && (c == '\n' || c == '\r' || c == K_KENTER))
3217 c = K_DOWN;
3218
3219 return c;
3220}
3221
3222/*
3223 * Delete characters on the command line, from "from" to the current
3224 * position.
3225 */
3226 static void
3227cmdline_del(cmdline_info_T *cclp, int from)
3228{
3229 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3230 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3231 cclp->cmdlen -= cclp->cmdpos - from;
3232 cclp->cmdpos = from;
3233}
3234
3235/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003236 * Handle a key pressed when the wild menu for the menu names
3237 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003238 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003239 static int
3240wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003241{
Bram Moolenaareadee482020-09-04 15:37:31 +02003242 int i;
3243 int j;
3244
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003245 // Hitting <Down> after "emenu Name.": complete submenu
3246 if (key == K_DOWN && cclp->cmdpos > 0
3247 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003248 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003249 key = p_wc;
3250 KeyTyped = TRUE; // in case the key was mapped
3251 }
3252 else if (key == K_UP)
3253 {
3254 // Hitting <Up>: Remove one submenu name in front of the
3255 // cursor
3256 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003257
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003258 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3259 i = 0;
3260 while (--j > 0)
3261 {
3262 // check for start of menu name
3263 if (cclp->cmdbuff[j] == ' '
3264 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003265 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003266 i = j + 1;
3267 break;
3268 }
3269 // check for start of submenu name
3270 if (cclp->cmdbuff[j] == '.'
3271 && cclp->cmdbuff[j - 1] != '\\')
3272 {
3273 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003274 {
3275 i = j + 1;
3276 break;
3277 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003278 else
3279 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003280 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003281 }
3282 if (i > 0)
3283 cmdline_del(cclp, i);
3284 key = p_wc;
3285 KeyTyped = TRUE; // in case the key was mapped
3286 xp->xp_context = EXPAND_NOTHING;
3287 }
3288
3289 return key;
3290}
3291
3292/*
3293 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3294 * directory names (EXPAND_DIRECTORIES) or shell command names
3295 * (EXPAND_SHELLCMD) is displayed.
3296 */
3297 static int
3298wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3299{
3300 int i;
3301 int j;
3302 char_u upseg[5];
3303
3304 upseg[0] = PATHSEP;
3305 upseg[1] = '.';
3306 upseg[2] = '.';
3307 upseg[3] = PATHSEP;
3308 upseg[4] = NUL;
3309
3310 if (key == K_DOWN
3311 && cclp->cmdpos > 0
3312 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3313 && (cclp->cmdpos < 3
3314 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3315 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3316 {
3317 // go down a directory
3318 key = p_wc;
3319 KeyTyped = TRUE; // in case the key was mapped
3320 }
3321 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3322 {
3323 // If in a direct ancestor, strip off one ../ to go down
3324 int found = FALSE;
3325
3326 j = cclp->cmdpos;
3327 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3328 while (--j > i)
3329 {
3330 if (has_mbyte)
3331 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3332 if (vim_ispathsep(cclp->cmdbuff[j]))
3333 {
3334 found = TRUE;
3335 break;
3336 }
3337 }
3338 if (found
3339 && cclp->cmdbuff[j - 1] == '.'
3340 && cclp->cmdbuff[j - 2] == '.'
3341 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3342 {
3343 cmdline_del(cclp, j - 2);
3344 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003345 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003346 }
3347 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003348 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003349 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003350 // go up a directory
3351 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003352
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003353 j = cclp->cmdpos - 1;
3354 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3355 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003356 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003357 if (has_mbyte)
3358 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3359 if (vim_ispathsep(cclp->cmdbuff[j])
3360# ifdef BACKSLASH_IN_FILENAME
3361 && vim_strchr((char_u *)" *?[{`$%#",
3362 cclp->cmdbuff[j + 1]) == NULL
3363# endif
3364 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003365 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003366 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003367 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003368 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003369 break;
3370 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003371 else
3372 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003373 }
3374 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003375
3376 if (!found)
3377 j = i;
3378 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3379 j += 4;
3380 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3381 && j == i)
3382 j += 3;
3383 else
3384 j = 0;
3385 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003386 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003387 // TODO this is only for DOS/UNIX systems - need to put in
3388 // machine-specific stuff here and in upseg init
3389 cmdline_del(cclp, j);
3390 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003391 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003392 else if (cclp->cmdpos > i)
3393 cmdline_del(cclp, i);
3394
3395 // Now complete in the new directory. Set KeyTyped in case the
3396 // Up key came from a mapping.
3397 key = p_wc;
3398 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003399 }
3400
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003401 return key;
3402}
3403
3404/*
3405 * Handle a key pressed when the wild menu is displayed
3406 */
3407 int
3408wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3409{
3410 if (xp->xp_context == EXPAND_MENUNAMES)
3411 return wildmenu_process_key_menunames(cclp, key, xp);
3412 else if ((xp->xp_context == EXPAND_FILES
3413 || xp->xp_context == EXPAND_DIRECTORIES
3414 || xp->xp_context == EXPAND_SHELLCMD))
3415 return wildmenu_process_key_filenames(cclp, key, xp);
3416
3417 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003418}
3419
3420/*
3421 * Free expanded names when finished walking through the matches
3422 */
3423 void
3424wildmenu_cleanup(cmdline_info_T *cclp)
3425{
3426 int skt = KeyTyped;
3427 int old_RedrawingDisabled = RedrawingDisabled;
3428
3429 if (!p_wmnu || wild_menu_showing == 0)
3430 return;
3431
3432 if (cclp->input_fn)
3433 RedrawingDisabled = 0;
3434
3435 if (wild_menu_showing == WM_SCROLLED)
3436 {
3437 // Entered command line, move it up
3438 cmdline_row--;
3439 redrawcmd();
3440 }
3441 else if (save_p_ls != -1)
3442 {
3443 // restore 'laststatus' and 'winminheight'
3444 p_ls = save_p_ls;
3445 p_wmh = save_p_wmh;
3446 last_status(FALSE);
3447 update_screen(VALID); // redraw the screen NOW
3448 redrawcmd();
3449 save_p_ls = -1;
3450 }
3451 else
3452 {
3453 win_redraw_last_status(topframe);
3454 redraw_statuslines();
3455 }
3456 KeyTyped = skt;
3457 wild_menu_showing = 0;
3458 if (cclp->input_fn)
3459 RedrawingDisabled = old_RedrawingDisabled;
3460}
3461#endif
3462
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003463#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003464/*
3465 * "getcompletion()" function
3466 */
3467 void
3468f_getcompletion(typval_T *argvars, typval_T *rettv)
3469{
3470 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003471 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472 expand_T xpc;
3473 int filtered = FALSE;
3474 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003475 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003476
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003477 if (in_vim9script()
3478 && (check_for_string_arg(argvars, 0) == FAIL
3479 || check_for_string_arg(argvars, 1) == FAIL
3480 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3481 return;
3482
ii144785fe02021-11-21 12:13:56 +00003483 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003484 if (argvars[1].v_type != VAR_STRING)
3485 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003486 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003487 return;
3488 }
3489 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003490
3491 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003492 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003493
3494 if (p_wic)
3495 options |= WILD_ICASE;
3496
3497 // For filtered results, 'wildignore' is used
3498 if (!filtered)
3499 options |= WILD_KEEP_ALL;
3500
3501 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003502 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003503 {
ii144785fe02021-11-21 12:13:56 +00003504 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003505 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003506 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003507 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003508 else
3509 {
ii144785fe02021-11-21 12:13:56 +00003510 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003511 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3512
3513 xpc.xp_context = cmdcomplete_str_to_type(type);
3514 if (xpc.xp_context == EXPAND_NOTHING)
3515 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003516 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003517 return;
3518 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003519
3520# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003521 if (xpc.xp_context == EXPAND_MENUS)
3522 {
3523 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3524 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3525 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003526# endif
3527# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003528 if (xpc.xp_context == EXPAND_CSCOPE)
3529 {
3530 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3531 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3532 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003533# endif
3534# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003535 if (xpc.xp_context == EXPAND_SIGN)
3536 {
3537 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3538 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3539 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003541 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003542
3543 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3544 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3545 {
3546 int i;
3547
3548 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3549
3550 for (i = 0; i < xpc.xp_numfiles; i++)
3551 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3552 }
3553 vim_free(pat);
3554 ExpandCleanup(&xpc);
3555}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003556#endif // FEAT_EVAL