blob: 5cad1db2d70227ff56097d1b2beb9df373209668 [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);
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +010019static int ExpandGeneric(expand_T *xp, regmatch_T *regmatch,
20 int *num_file, char_u ***file,
21 char_u *((*func)(expand_T *, int)), int escaped);
Bram Moolenaar66b51422019-08-18 21:44:12 +020022static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
23static int expand_showtail(expand_T *xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +020024static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +020026static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
27static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
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 Lakshmanan560dff42022-02-10 19:52:10 +000040#define SHOW_FILE_TEXT(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
41
Bram Moolenaar66b51422019-08-18 21:44:12 +020042 static int
43sort_func_compare(const void *s1, const void *s2)
44{
45 char_u *p1 = *(char_u **)s1;
46 char_u *p2 = *(char_u **)s2;
47
48 if (*p1 != '<' && *p2 == '<') return -1;
49 if (*p1 == '<' && *p2 != '<') return 1;
50 return STRCMP(p1, p2);
51}
Bram Moolenaar66b51422019-08-18 21:44:12 +020052
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000053/*
54 * Escape special characters in the cmdline completion matches.
55 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020056 static void
57ExpandEscape(
58 expand_T *xp,
59 char_u *str,
60 int numfiles,
61 char_u **files,
62 int options)
63{
64 int i;
65 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +010066 int vse_what = xp->xp_context == EXPAND_BUFFERS
67 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +020068
69 // May change home directory back to "~"
70 if (options & WILD_HOME_REPLACE)
71 tilde_replace(str, numfiles, files);
72
73 if (options & WILD_ESCAPE)
74 {
75 if (xp->xp_context == EXPAND_FILES
76 || xp->xp_context == EXPAND_FILES_IN_PATH
77 || xp->xp_context == EXPAND_SHELLCMD
78 || xp->xp_context == EXPAND_BUFFERS
79 || xp->xp_context == EXPAND_DIRECTORIES)
80 {
81 // Insert a backslash into a file name before a space, \, %, #
82 // and wildmatch characters, except '~'.
83 for (i = 0; i < numfiles; ++i)
84 {
85 // for ":set path=" we need to escape spaces twice
86 if (xp->xp_backslash == XP_BS_THREE)
87 {
88 p = vim_strsave_escaped(files[i], (char_u *)" ");
89 if (p != NULL)
90 {
91 vim_free(files[i]);
92 files[i] = p;
93#if defined(BACKSLASH_IN_FILENAME)
94 p = vim_strsave_escaped(files[i], (char_u *)" ");
95 if (p != NULL)
96 {
97 vim_free(files[i]);
98 files[i] = p;
99 }
100#endif
101 }
102 }
103#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100104 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200105#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100106 p = vim_strsave_fnameescape(files[i],
107 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200108#endif
109 if (p != NULL)
110 {
111 vim_free(files[i]);
112 files[i] = p;
113 }
114
115 // If 'str' starts with "\~", replace "~" at start of
116 // files[i] with "\~".
117 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
118 escape_fname(&files[i]);
119 }
120 xp->xp_backslash = XP_BS_NONE;
121
122 // If the first file starts with a '+' escape it. Otherwise it
123 // could be seen as "+cmd".
124 if (*files[0] == '+')
125 escape_fname(&files[0]);
126 }
127 else if (xp->xp_context == EXPAND_TAGS)
128 {
129 // Insert a backslash before characters in a tag name that
130 // would terminate the ":tag" command.
131 for (i = 0; i < numfiles; ++i)
132 {
133 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
134 if (p != NULL)
135 {
136 vim_free(files[i]);
137 files[i] = p;
138 }
139 }
140 }
141 }
142}
143
144/*
145 * Return FAIL if this is not an appropriate context in which to do
146 * completion of anything, return OK if it is (even if there are no matches).
147 * For the caller, this means that the character is just passed through like a
148 * normal character (instead of being expanded). This allows :s/^I^D etc.
149 */
150 int
151nextwild(
152 expand_T *xp,
153 int type,
154 int options, // extra options for ExpandOne()
155 int escape) // if TRUE, escape the returned matches
156{
157 cmdline_info_T *ccline = get_cmdline_info();
158 int i, j;
159 char_u *p1;
160 char_u *p2;
161 int difflen;
162 int v;
163
164 if (xp->xp_numfiles == -1)
165 {
166 set_expand_context(xp);
167 cmd_showtail = expand_showtail(xp);
168 }
169
170 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
171 {
172 beep_flush();
173 return OK; // Something illegal on command line
174 }
175 if (xp->xp_context == EXPAND_NOTHING)
176 {
177 // Caller can use the character as a normal char instead
178 return FAIL;
179 }
180
181 msg_puts("..."); // show that we are busy
182 out_flush();
183
184 i = (int)(xp->xp_pattern - ccline->cmdbuff);
185 xp->xp_pattern_len = ccline->cmdpos - i;
186
187 if (type == WILD_NEXT || type == WILD_PREV)
188 {
189 // Get next/previous match for a previous expanded pattern.
190 p2 = ExpandOne(xp, NULL, NULL, 0, type);
191 }
192 else
193 {
194 // Translate string into pattern and expand it.
195 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
196 xp->xp_context)) == NULL)
197 p2 = NULL;
198 else
199 {
200 int use_options = options |
201 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
202 if (escape)
203 use_options |= WILD_ESCAPE;
204
205 if (p_wic)
206 use_options += WILD_ICASE;
207 p2 = ExpandOne(xp, p1,
208 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
209 use_options, type);
210 vim_free(p1);
211 // longest match: make sure it is not shorter, happens with :help
212 if (p2 != NULL && type == WILD_LONGEST)
213 {
214 for (j = 0; j < xp->xp_pattern_len; ++j)
215 if (ccline->cmdbuff[i + j] == '*'
216 || ccline->cmdbuff[i + j] == '?')
217 break;
218 if ((int)STRLEN(p2) < j)
219 VIM_CLEAR(p2);
220 }
221 }
222 }
223
224 if (p2 != NULL && !got_int)
225 {
226 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
227 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
228 {
229 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
230 xp->xp_pattern = ccline->cmdbuff + i;
231 }
232 else
233 v = OK;
234 if (v == OK)
235 {
236 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
237 &ccline->cmdbuff[ccline->cmdpos],
238 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
239 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
240 ccline->cmdlen += difflen;
241 ccline->cmdpos += difflen;
242 }
243 }
244 vim_free(p2);
245
246 redrawcmd();
247 cursorcmd();
248
249 // When expanding a ":map" command and no matches are found, assume that
250 // the key is supposed to be inserted literally
251 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
252 return FAIL;
253
254 if (xp->xp_numfiles <= 0 && p2 == NULL)
255 beep_flush();
256 else if (xp->xp_numfiles == 1)
257 // free expanded pattern
258 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
259
260 return OK;
261}
262
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000263#if defined(FEAT_WILDMENU) || defined(PROTO)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000264
265/*
266 * Create and display a cmdline completion popup menu with items from
267 * 'files_found'.
268 */
269 static int
270cmdline_pum_create(
271 cmdline_info_T *ccline,
272 expand_T *xp,
273 char_u **files_found,
274 int num_files,
275 int showtail)
276{
277 int i;
278 int columns;
279
280 // Add all the completion matches
281 compl_match_arraysize = num_files;
282 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
283 for (i = 0; i < num_files; i++)
284 {
285 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
286 compl_match_array[i].pum_info = NULL;
287 compl_match_array[i].pum_extra = NULL;
288 compl_match_array[i].pum_kind = NULL;
289 }
290
291 // Compute the popup menu starting column
292 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
293 columns = vim_strsize(xp->xp_pattern);
294 if (showtail)
295 {
296 columns += vim_strsize(sm_gettail(files_found[0]));
297 columns -= vim_strsize(files_found[0]);
298 }
299 if (columns >= compl_startcol)
300 compl_startcol = 0;
301 else
302 compl_startcol -= columns;
303
304 // no default selection
305 compl_selected = -1;
306
307 cmdline_pum_display();
308
309 return EXPAND_OK;
310}
311
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000312/*
313 * Display the cmdline completion matches in a popup menu
314 */
315void cmdline_pum_display(void)
316{
317 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
318}
319
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000320/*
321 * Returns TRUE if the cmdline completion popup menu is being displayed.
322 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000323int cmdline_pum_active(void)
324{
325 return p_wmnu && pum_visible() && compl_match_array != NULL;
326}
327
328/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000329 * Remove the cmdline completion popup menu (if present), free the list of
330 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000331 */
332void cmdline_pum_remove(void)
333{
334 pum_undisplay();
335 VIM_CLEAR(compl_match_array);
336 update_screen(0);
Bram Moolenaar414acd32022-02-10 21:09:45 +0000337 redrawcmd();
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000338}
339
340void cmdline_pum_cleanup(cmdline_info_T *cclp)
341{
342 cmdline_pum_remove();
343 wildmenu_cleanup(cclp);
344}
345
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000346/*
347 * Returns the starting column number to use for the cmdline completion popup
348 * menu.
349 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000350int cmdline_compl_startcol(void)
351{
352 return compl_startcol;
353}
354#endif
355
Bram Moolenaar66b51422019-08-18 21:44:12 +0200356/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000357 * Get the next or prev cmdline completion match. The index of the match is set
358 * in 'p_findex'
359 */
360 static char_u *
361get_next_or_prev_match(
362 int mode,
363 expand_T *xp,
364 int *p_findex,
365 char_u *orig_save)
366{
367 int findex = *p_findex;
368
369 if (xp->xp_numfiles <= 0)
370 return NULL;
371
372 if (mode == WILD_PREV)
373 {
374 if (findex == -1)
375 findex = xp->xp_numfiles;
376 --findex;
377 }
378 else // mode == WILD_NEXT
379 ++findex;
380
381 // When wrapping around, return the original string, set findex to
382 // -1.
383 if (findex < 0)
384 {
385 if (orig_save == NULL)
386 findex = xp->xp_numfiles - 1;
387 else
388 findex = -1;
389 }
390 if (findex >= xp->xp_numfiles)
391 {
392 if (orig_save == NULL)
393 findex = 0;
394 else
395 findex = -1;
396 }
397#ifdef FEAT_WILDMENU
398 if (compl_match_array)
399 {
400 compl_selected = findex;
401 cmdline_pum_display();
402 }
403 else if (p_wmnu)
404 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
405 findex, cmd_showtail);
406#endif
407 *p_findex = findex;
408
409 if (findex == -1)
410 return vim_strsave(orig_save);
411
412 return vim_strsave(xp->xp_files[findex]);
413}
414
415/*
416 * Start the command-line expansion and get the matches.
417 */
418 static char_u *
419ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
420{
421 int non_suf_match; // number without matching suffix
422 int i;
423 char_u *ss = NULL;
424
425 // Do the expansion.
426 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
427 options) == FAIL)
428 {
429#ifdef FNAME_ILLEGAL
430 // Illegal file name has been silently skipped. But when there
431 // are wildcards, the real problem is that there was no match,
432 // causing the pattern to be added, which has illegal characters.
433 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
434 semsg(_(e_no_match_str_2), str);
435#endif
436 }
437 else if (xp->xp_numfiles == 0)
438 {
439 if (!(options & WILD_SILENT))
440 semsg(_(e_no_match_str_2), str);
441 }
442 else
443 {
444 // Escape the matches for use on the command line.
445 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
446
447 // Check for matching suffixes in file names.
448 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
449 && mode != WILD_LONGEST)
450 {
451 if (xp->xp_numfiles)
452 non_suf_match = xp->xp_numfiles;
453 else
454 non_suf_match = 1;
455 if ((xp->xp_context == EXPAND_FILES
456 || xp->xp_context == EXPAND_DIRECTORIES)
457 && xp->xp_numfiles > 1)
458 {
459 // More than one match; check suffix.
460 // The files will have been sorted on matching suffix in
461 // expand_wildcards, only need to check the first two.
462 non_suf_match = 0;
463 for (i = 0; i < 2; ++i)
464 if (match_suffix(xp->xp_files[i]))
465 ++non_suf_match;
466 }
467 if (non_suf_match != 1)
468 {
469 // Can we ever get here unless it's while expanding
470 // interactively? If not, we can get rid of this all
471 // together. Don't really want to wait for this message
472 // (and possibly have to hit return to continue!).
473 if (!(options & WILD_SILENT))
474 emsg(_(e_too_many_file_names));
475 else if (!(options & WILD_NO_BEEP))
476 beep_flush();
477 }
478 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
479 ss = vim_strsave(xp->xp_files[0]);
480 }
481 }
482
483 return ss;
484}
485
486/*
487 * Return the longest common part in the list of cmdline completion matches.
488 */
489 static char_u *
490find_longest_match(expand_T *xp, int options)
491{
492 long_u len;
493 int mb_len = 1;
494 int c0, ci;
495 int i;
496 char_u *ss;
497
498 for (len = 0; xp->xp_files[0][len]; len += mb_len)
499 {
500 if (has_mbyte)
501 {
502 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
503 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
504 }
505 else
506 c0 = xp->xp_files[0][len];
507 for (i = 1; i < xp->xp_numfiles; ++i)
508 {
509 if (has_mbyte)
510 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
511 else
512 ci = xp->xp_files[i][len];
513 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
514 || xp->xp_context == EXPAND_FILES
515 || xp->xp_context == EXPAND_SHELLCMD
516 || xp->xp_context == EXPAND_BUFFERS))
517 {
518 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
519 break;
520 }
521 else if (c0 != ci)
522 break;
523 }
524 if (i < xp->xp_numfiles)
525 {
526 if (!(options & WILD_NO_BEEP))
527 vim_beep(BO_WILD);
528 break;
529 }
530 }
531
532 ss = alloc(len + 1);
533 if (ss)
534 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
535
536 return ss;
537}
538
539/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200540 * Do wildcard expansion on the string 'str'.
541 * Chars that should not be expanded must be preceded with a backslash.
542 * Return a pointer to allocated memory containing the new string.
543 * Return NULL for failure.
544 *
545 * "orig" is the originally expanded string, copied to allocated memory. It
546 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
547 * WILD_PREV "orig" should be NULL.
548 *
549 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
550 * is WILD_EXPAND_FREE or WILD_ALL.
551 *
552 * mode = WILD_FREE: just free previously expanded matches
553 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
554 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
555 * mode = WILD_NEXT: use next match in multiple match, wrap to first
556 * mode = WILD_PREV: use previous match in multiple match, wrap to first
557 * mode = WILD_ALL: return all matches concatenated
558 * mode = WILD_LONGEST: return longest matched part
559 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000560 * mode = WILD_APPLY: apply the item selected in the cmdline completion
561 * popup menu and close the menu.
562 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
563 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200564 *
565 * options = WILD_LIST_NOTFOUND: list entries without a match
566 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
567 * options = WILD_USE_NL: Use '\n' for WILD_ALL
568 * options = WILD_NO_BEEP: Don't beep for multiple matches
569 * options = WILD_ADD_SLASH: add a slash after directory names
570 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
571 * options = WILD_SILENT: don't print warning messages
572 * options = WILD_ESCAPE: put backslash before special chars
573 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200574 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200575 *
576 * The variables xp->xp_context and xp->xp_backslash must have been set!
577 */
578 char_u *
579ExpandOne(
580 expand_T *xp,
581 char_u *str,
582 char_u *orig, // allocated copy of original of expanded string
583 int options,
584 int mode)
585{
586 char_u *ss = NULL;
587 static int findex;
588 static char_u *orig_save = NULL; // kept value of orig
589 int orig_saved = FALSE;
590 int i;
591 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200592
593 // first handle the case of using an old match
594 if (mode == WILD_NEXT || mode == WILD_PREV)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000595 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200596
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000597 if (mode == WILD_CANCEL)
598 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
599 else if (mode == WILD_APPLY)
600 ss = vim_strsave(findex == -1 ? (orig_save ?
601 orig_save : (char_u *)"") : xp->xp_files[findex]);
602
Bram Moolenaar66b51422019-08-18 21:44:12 +0200603 // free old names
604 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
605 {
606 FreeWild(xp->xp_numfiles, xp->xp_files);
607 xp->xp_numfiles = -1;
608 VIM_CLEAR(orig_save);
609 }
610 findex = 0;
611
612 if (mode == WILD_FREE) // only release file name
613 return NULL;
614
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000615 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200616 {
617 vim_free(orig_save);
618 orig_save = orig;
619 orig_saved = TRUE;
620
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000621 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200622 }
623
624 // Find longest common part
625 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
626 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000627 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200628 findex = -1; // next p_wc gets first one
629 }
630
631 // Concatenate all matching names
632 if (mode == WILD_ALL && xp->xp_numfiles > 0)
633 {
634 len = 0;
635 for (i = 0; i < xp->xp_numfiles; ++i)
636 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
637 ss = alloc(len);
638 if (ss != NULL)
639 {
640 *ss = NUL;
641 for (i = 0; i < xp->xp_numfiles; ++i)
642 {
643 STRCAT(ss, xp->xp_files[i]);
644 if (i != xp->xp_numfiles - 1)
645 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
646 }
647 }
648 }
649
650 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
651 ExpandCleanup(xp);
652
653 // Free "orig" if it wasn't stored in "orig_save".
654 if (!orig_saved)
655 vim_free(orig);
656
657 return ss;
658}
659
660/*
661 * Prepare an expand structure for use.
662 */
663 void
664ExpandInit(expand_T *xp)
665{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200666 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200667 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200668 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200669}
670
671/*
672 * Cleanup an expand structure after use.
673 */
674 void
675ExpandCleanup(expand_T *xp)
676{
677 if (xp->xp_numfiles >= 0)
678 {
679 FreeWild(xp->xp_numfiles, xp->xp_files);
680 xp->xp_numfiles = -1;
681 }
682}
683
684/*
685 * Show all matches for completion on the command line.
686 * Returns EXPAND_NOTHING when the character that triggered expansion should
687 * be inserted like a normal character.
688 */
689 int
690showmatches(expand_T *xp, int wildmenu UNUSED)
691{
692 cmdline_info_T *ccline = get_cmdline_info();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200693 int num_files;
694 char_u **files_found;
695 int i, j, k;
696 int maxlen;
697 int lines;
698 int columns;
699 char_u *p;
700 int lastlen;
701 int attr;
702 int showtail;
703
704 if (xp->xp_numfiles == -1)
705 {
706 set_expand_context(xp);
707 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
708 &num_files, &files_found);
709 showtail = expand_showtail(xp);
710 if (i != EXPAND_OK)
711 return i;
712
713 }
714 else
715 {
716 num_files = xp->xp_numfiles;
717 files_found = xp->xp_files;
718 showtail = cmd_showtail;
719 }
720
721#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000722 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000723 // cmdline completion popup menu (with wildoptions=pum)
724 return cmdline_pum_create(ccline, xp, files_found, num_files, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000725#endif
726
727#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200728 if (!wildmenu)
729 {
730#endif
731 msg_didany = FALSE; // lines_left will be set
732 msg_start(); // prepare for paging
733 msg_putchar('\n');
734 out_flush();
735 cmdline_row = msg_row;
736 msg_didany = FALSE; // lines_left will be set again
737 msg_start(); // prepare for paging
738#ifdef FEAT_WILDMENU
739 }
740#endif
741
742 if (got_int)
743 got_int = FALSE; // only int. the completion, not the cmd line
744#ifdef FEAT_WILDMENU
745 else if (wildmenu)
746 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
747#endif
748 else
749 {
750 // find the length of the longest file name
751 maxlen = 0;
752 for (i = 0; i < num_files; ++i)
753 {
754 if (!showtail && (xp->xp_context == EXPAND_FILES
755 || xp->xp_context == EXPAND_SHELLCMD
756 || xp->xp_context == EXPAND_BUFFERS))
757 {
758 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
759 j = vim_strsize(NameBuff);
760 }
761 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000762 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200763 if (j > maxlen)
764 maxlen = j;
765 }
766
767 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
768 lines = num_files;
769 else
770 {
771 // compute the number of columns and lines for the listing
772 maxlen += 2; // two spaces between file names
773 columns = ((int)Columns + 2) / maxlen;
774 if (columns < 1)
775 columns = 1;
776 lines = (num_files + columns - 1) / columns;
777 }
778
779 attr = HL_ATTR(HLF_D); // find out highlighting for directories
780
781 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
782 {
783 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
784 msg_clr_eos();
785 msg_advance(maxlen - 3);
786 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
787 }
788
789 // list the files line by line
790 for (i = 0; i < lines; ++i)
791 {
792 lastlen = 999;
793 for (k = i; k < num_files; k += lines)
794 {
795 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
796 {
797 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
798 p = files_found[k] + STRLEN(files_found[k]) + 1;
799 msg_advance(maxlen + 1);
800 msg_puts((char *)p);
801 msg_advance(maxlen + 3);
802 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
803 break;
804 }
805 for (j = maxlen - lastlen; --j >= 0; )
806 msg_putchar(' ');
807 if (xp->xp_context == EXPAND_FILES
808 || xp->xp_context == EXPAND_SHELLCMD
809 || xp->xp_context == EXPAND_BUFFERS)
810 {
811 // highlight directories
812 if (xp->xp_numfiles != -1)
813 {
814 char_u *halved_slash;
815 char_u *exp_path;
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200816 char_u *path;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200817
818 // Expansion was done before and special characters
819 // were escaped, need to halve backslashes. Also
820 // $HOME has been replaced with ~/.
821 exp_path = expand_env_save_opt(files_found[k], TRUE);
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200822 path = exp_path != NULL ? exp_path : files_found[k];
823 halved_slash = backslash_halve_save(path);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200824 j = mch_isdir(halved_slash != NULL ? halved_slash
825 : files_found[k]);
826 vim_free(exp_path);
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200827 if (halved_slash != path)
828 vim_free(halved_slash);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200829 }
830 else
831 // Expansion was done here, file names are literal.
832 j = mch_isdir(files_found[k]);
833 if (showtail)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000834 p = SHOW_FILE_TEXT(k);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200835 else
836 {
837 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
838 TRUE);
839 p = NameBuff;
840 }
841 }
842 else
843 {
844 j = FALSE;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000845 p = SHOW_FILE_TEXT(k);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200846 }
847 lastlen = msg_outtrans_attr(p, j ? attr : 0);
848 }
849 if (msg_col > 0) // when not wrapped around
850 {
851 msg_clr_eos();
852 msg_putchar('\n');
853 }
854 out_flush(); // show one line at a time
855 if (got_int)
856 {
857 got_int = FALSE;
858 break;
859 }
860 }
861
862 // we redraw the command below the lines that we have just listed
863 // This is a bit tricky, but it saves a lot of screen updating.
864 cmdline_row = msg_row; // will put it back later
865 }
866
867 if (xp->xp_numfiles == -1)
868 FreeWild(num_files, files_found);
869
870 return EXPAND_OK;
871}
872
873/*
874 * Private gettail for showmatches() (and win_redr_status_matches()):
875 * Find tail of file name path, but ignore trailing "/".
876 */
877 char_u *
878sm_gettail(char_u *s)
879{
880 char_u *p;
881 char_u *t = s;
882 int had_sep = FALSE;
883
884 for (p = s; *p != NUL; )
885 {
886 if (vim_ispathsep(*p)
887#ifdef BACKSLASH_IN_FILENAME
888 && !rem_backslash(p)
889#endif
890 )
891 had_sep = TRUE;
892 else if (had_sep)
893 {
894 t = p;
895 had_sep = FALSE;
896 }
897 MB_PTR_ADV(p);
898 }
899 return t;
900}
901
902/*
903 * Return TRUE if we only need to show the tail of completion matches.
904 * When not completing file names or there is a wildcard in the path FALSE is
905 * returned.
906 */
907 static int
908expand_showtail(expand_T *xp)
909{
910 char_u *s;
911 char_u *end;
912
913 // When not completing file names a "/" may mean something different.
914 if (xp->xp_context != EXPAND_FILES
915 && xp->xp_context != EXPAND_SHELLCMD
916 && xp->xp_context != EXPAND_DIRECTORIES)
917 return FALSE;
918
919 end = gettail(xp->xp_pattern);
920 if (end == xp->xp_pattern) // there is no path separator
921 return FALSE;
922
923 for (s = xp->xp_pattern; s < end; s++)
924 {
925 // Skip escaped wildcards. Only when the backslash is not a path
926 // separator, on DOS the '*' "path\*\file" must not be skipped.
927 if (rem_backslash(s))
928 ++s;
929 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
930 return FALSE;
931 }
932 return TRUE;
933}
934
935/*
936 * Prepare a string for expansion.
937 * When expanding file names: The string will be used with expand_wildcards().
938 * Copy "fname[len]" into allocated memory and add a '*' at the end.
939 * When expanding other names: The string will be used with regcomp(). Copy
940 * the name into allocated memory and prepend "^".
941 */
942 char_u *
943addstar(
944 char_u *fname,
945 int len,
946 int context) // EXPAND_FILES etc.
947{
948 char_u *retval;
949 int i, j;
950 int new_len;
951 char_u *tail;
952 int ends_in_star;
953
954 if (context != EXPAND_FILES
955 && context != EXPAND_FILES_IN_PATH
956 && context != EXPAND_SHELLCMD
957 && context != EXPAND_DIRECTORIES)
958 {
959 // Matching will be done internally (on something other than files).
960 // So we convert the file-matching-type wildcards into our kind for
961 // use with vim_regcomp(). First work out how long it will be:
962
963 // For help tags the translation is done in find_help_tags().
964 // For a tag pattern starting with "/" no translation is needed.
965 if (context == EXPAND_HELP
966 || context == EXPAND_COLORS
967 || context == EXPAND_COMPILER
968 || context == EXPAND_OWNSYNTAX
969 || context == EXPAND_FILETYPE
970 || context == EXPAND_PACKADD
971 || ((context == EXPAND_TAGS_LISTFILES
972 || context == EXPAND_TAGS)
973 && fname[0] == '/'))
974 retval = vim_strnsave(fname, len);
975 else
976 {
977 new_len = len + 2; // +2 for '^' at start, NUL at end
978 for (i = 0; i < len; i++)
979 {
980 if (fname[i] == '*' || fname[i] == '~')
981 new_len++; // '*' needs to be replaced by ".*"
982 // '~' needs to be replaced by "\~"
983
984 // Buffer names are like file names. "." should be literal
985 if (context == EXPAND_BUFFERS && fname[i] == '.')
986 new_len++; // "." becomes "\."
987
988 // Custom expansion takes care of special things, match
989 // backslashes literally (perhaps also for other types?)
990 if ((context == EXPAND_USER_DEFINED
991 || context == EXPAND_USER_LIST) && fname[i] == '\\')
992 new_len++; // '\' becomes "\\"
993 }
994 retval = alloc(new_len);
995 if (retval != NULL)
996 {
997 retval[0] = '^';
998 j = 1;
999 for (i = 0; i < len; i++, j++)
1000 {
1001 // Skip backslash. But why? At least keep it for custom
1002 // expansion.
1003 if (context != EXPAND_USER_DEFINED
1004 && context != EXPAND_USER_LIST
1005 && fname[i] == '\\'
1006 && ++i == len)
1007 break;
1008
1009 switch (fname[i])
1010 {
1011 case '*': retval[j++] = '.';
1012 break;
1013 case '~': retval[j++] = '\\';
1014 break;
1015 case '?': retval[j] = '.';
1016 continue;
1017 case '.': if (context == EXPAND_BUFFERS)
1018 retval[j++] = '\\';
1019 break;
1020 case '\\': if (context == EXPAND_USER_DEFINED
1021 || context == EXPAND_USER_LIST)
1022 retval[j++] = '\\';
1023 break;
1024 }
1025 retval[j] = fname[i];
1026 }
1027 retval[j] = NUL;
1028 }
1029 }
1030 }
1031 else
1032 {
1033 retval = alloc(len + 4);
1034 if (retval != NULL)
1035 {
1036 vim_strncpy(retval, fname, len);
1037
1038 // Don't add a star to *, ~, ~user, $var or `cmd`.
1039 // * would become **, which walks the whole tree.
1040 // ~ would be at the start of the file name, but not the tail.
1041 // $ could be anywhere in the tail.
1042 // ` could be anywhere in the file name.
1043 // When the name ends in '$' don't add a star, remove the '$'.
1044 tail = gettail(retval);
1045 ends_in_star = (len > 0 && retval[len - 1] == '*');
1046#ifndef BACKSLASH_IN_FILENAME
1047 for (i = len - 2; i >= 0; --i)
1048 {
1049 if (retval[i] != '\\')
1050 break;
1051 ends_in_star = !ends_in_star;
1052 }
1053#endif
1054 if ((*retval != '~' || tail != retval)
1055 && !ends_in_star
1056 && vim_strchr(tail, '$') == NULL
1057 && vim_strchr(retval, '`') == NULL)
1058 retval[len++] = '*';
1059 else if (len > 0 && retval[len - 1] == '$')
1060 --len;
1061 retval[len] = NUL;
1062 }
1063 }
1064 return retval;
1065}
1066
1067/*
1068 * Must parse the command line so far to work out what context we are in.
1069 * Completion can then be done based on that context.
1070 * This routine sets the variables:
1071 * xp->xp_pattern The start of the pattern to be expanded within
1072 * the command line (ends at the cursor).
1073 * xp->xp_context The type of thing to expand. Will be one of:
1074 *
1075 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1076 * the command line, like an unknown command. Caller
1077 * should beep.
1078 * EXPAND_NOTHING Unrecognised context for completion, use char like
1079 * a normal char, rather than for completion. eg
1080 * :s/^I/
1081 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1082 * it.
1083 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1084 * EXPAND_FILES After command with EX_XFILE set, or after setting
1085 * with P_EXPAND set. eg :e ^I, :w>>^I
1086 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1087 * when we know only directories are of interest. eg
1088 * :set dir=^I
1089 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1090 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1091 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1092 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1093 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1094 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1095 * EXPAND_EVENTS Complete event names
1096 * EXPAND_SYNTAX Complete :syntax command arguments
1097 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1098 * EXPAND_AUGROUP Complete autocommand group names
1099 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1100 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1101 * eg :unmap a^I , :cunab x^I
1102 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1103 * eg :call sub^I
1104 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1105 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1106 * names in expressions, eg :while s^I
1107 * EXPAND_ENV_VARS Complete environment variable names
1108 * EXPAND_USER Complete user names
1109 */
1110 static void
1111set_expand_context(expand_T *xp)
1112{
1113 cmdline_info_T *ccline = get_cmdline_info();
1114
1115 // only expansion for ':', '>' and '=' command-lines
1116 if (ccline->cmdfirstc != ':'
1117#ifdef FEAT_EVAL
1118 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1119 && !ccline->input_fn
1120#endif
1121 )
1122 {
1123 xp->xp_context = EXPAND_NOTHING;
1124 return;
1125 }
1126 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1127}
1128
Bram Moolenaard0190392019-08-23 21:17:35 +02001129/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001130 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1131 * For user defined commands, the completion context is set in 'xp' and the
1132 * completion flags in 'complp'.
1133 *
1134 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001135 */
1136 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001137set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001138{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001139 char_u *p = NULL;
1140 int len = 0;
Bram Moolenaard0190392019-08-23 21:17:35 +02001141
1142 // Isolate the command and search for it in the command table.
1143 // Exceptions:
1144 // - the 'k' command can directly be followed by any character, but
1145 // do accept "keepmarks", "keepalt" and "keepjumps".
1146 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
1147 if (*cmd == 'k' && cmd[1] != 'e')
1148 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001149 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001150 p = cmd + 1;
1151 }
1152 else
1153 {
1154 p = cmd;
1155 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1156 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001157 // A user command may contain digits.
1158 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1159 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001160 while (ASCII_ISALNUM(*p) || *p == '*')
1161 ++p;
1162 // for python 3.x: ":py3*" commands completion
1163 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1164 {
1165 ++p;
1166 while (ASCII_ISALPHA(*p) || *p == '*')
1167 ++p;
1168 }
1169 // check for non-alpha command
1170 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1171 ++p;
1172 len = (int)(p - cmd);
1173
1174 if (len == 0)
1175 {
1176 xp->xp_context = EXPAND_UNSUCCESSFUL;
1177 return NULL;
1178 }
1179
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001180 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001181
1182 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1183 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1184 ++p;
1185 }
1186
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001187 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001188 // character, complete the command name.
1189 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1190 return NULL;
1191
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001192 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001193 {
1194 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1195 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001196 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001197 p = cmd + 1;
1198 }
1199 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1200 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001201 eap->cmd = cmd;
1202 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001203 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001204 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001205 }
1206 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001207 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001208 {
1209 // Not still touching the command and it was an illegal one
1210 xp->xp_context = EXPAND_UNSUCCESSFUL;
1211 return NULL;
1212 }
1213
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001214 return p;
1215}
Bram Moolenaard0190392019-08-23 21:17:35 +02001216
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001217/*
1218 * Set the completion context for a command argument with wild card characters.
1219 */
1220 static void
1221set_context_for_wildcard_arg(
1222 exarg_T *eap,
1223 char_u *arg,
1224 int usefilter,
1225 expand_T *xp,
1226 int *complp)
1227{
1228 char_u *p;
1229 int c;
1230 int in_quote = FALSE;
1231 char_u *bow = NULL; // Beginning of word
1232 int len = 0;
1233
1234 // Allow spaces within back-quotes to count as part of the argument
1235 // being expanded.
1236 xp->xp_pattern = skipwhite(arg);
1237 p = xp->xp_pattern;
1238 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001239 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001240 if (has_mbyte)
1241 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001242 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001243 c = *p;
1244 if (c == '\\' && p[1] != NUL)
1245 ++p;
1246 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001247 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001248 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001249 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001250 xp->xp_pattern = p;
1251 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001252 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001253 in_quote = !in_quote;
1254 }
1255 // An argument can contain just about everything, except
1256 // characters that end the command and white space.
1257 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001258#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001259 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001260#endif
1261 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001262 {
1263 len = 0; // avoid getting stuck when space is in 'isfname'
1264 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001265 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001266 if (has_mbyte)
1267 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001268 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001269 c = *p;
1270 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001271 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001272 if (has_mbyte)
1273 len = (*mb_ptr2len)(p);
1274 else
1275 len = 1;
1276 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001277 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001278 if (in_quote)
1279 bow = p;
1280 else
1281 xp->xp_pattern = p;
1282 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001283 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001284 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001285 }
1286
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001287 // If we are still inside the quotes, and we passed a space, just
1288 // expand from there.
1289 if (bow != NULL && in_quote)
1290 xp->xp_pattern = bow;
1291 xp->xp_context = EXPAND_FILES;
1292
1293 // For a shell command more chars need to be escaped.
1294 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1295 {
1296#ifndef BACKSLASH_IN_FILENAME
1297 xp->xp_shell = TRUE;
1298#endif
1299 // When still after the command name expand executables.
1300 if (xp->xp_pattern == skipwhite(arg))
1301 xp->xp_context = EXPAND_SHELLCMD;
1302 }
1303
1304 // Check for environment variable.
1305 if (*xp->xp_pattern == '$')
1306 {
1307 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1308 if (!vim_isIDc(*p))
1309 break;
1310 if (*p == NUL)
1311 {
1312 xp->xp_context = EXPAND_ENV_VARS;
1313 ++xp->xp_pattern;
1314 // Avoid that the assignment uses EXPAND_FILES again.
1315 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1316 *complp = EXPAND_ENV_VARS;
1317 }
1318 }
1319 // Check for user names.
1320 if (*xp->xp_pattern == '~')
1321 {
1322 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1323 ;
1324 // Complete ~user only if it partially matches a user name.
1325 // A full match ~user<Tab> will be replaced by user's home
1326 // directory i.e. something like ~user<Tab> -> /home/user/
1327 if (*p == NUL && p > xp->xp_pattern + 1
1328 && match_user(xp->xp_pattern + 1) >= 1)
1329 {
1330 xp->xp_context = EXPAND_USER;
1331 ++xp->xp_pattern;
1332 }
1333 }
1334}
1335
1336/*
1337 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1338 * The argument to the command is 'arg' and the argument flags is 'argt'.
1339 * For user-defined commands and for environment variables, 'compl' has the
1340 * completion type.
1341 * Returns a pointer to the next command. Returns NULL if there is no next
1342 * command.
1343 */
1344 static char_u *
1345set_context_by_cmdname(
1346 char_u *cmd,
1347 cmdidx_T cmdidx,
1348 char_u *arg,
1349 long argt,
1350 int compl,
1351 expand_T *xp,
1352 int forceit)
1353{
1354 char_u *p;
1355 int delim;
1356
1357 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001358 {
1359 case CMD_find:
1360 case CMD_sfind:
1361 case CMD_tabfind:
1362 if (xp->xp_context == EXPAND_FILES)
1363 xp->xp_context = EXPAND_FILES_IN_PATH;
1364 break;
1365 case CMD_cd:
1366 case CMD_chdir:
1367 case CMD_tcd:
1368 case CMD_tchdir:
1369 case CMD_lcd:
1370 case CMD_lchdir:
1371 if (xp->xp_context == EXPAND_FILES)
1372 xp->xp_context = EXPAND_DIRECTORIES;
1373 break;
1374 case CMD_help:
1375 xp->xp_context = EXPAND_HELP;
1376 xp->xp_pattern = arg;
1377 break;
1378
1379 // Command modifiers: return the argument.
1380 // Also for commands with an argument that is a command.
1381 case CMD_aboveleft:
1382 case CMD_argdo:
1383 case CMD_belowright:
1384 case CMD_botright:
1385 case CMD_browse:
1386 case CMD_bufdo:
1387 case CMD_cdo:
1388 case CMD_cfdo:
1389 case CMD_confirm:
1390 case CMD_debug:
1391 case CMD_folddoclosed:
1392 case CMD_folddoopen:
1393 case CMD_hide:
1394 case CMD_keepalt:
1395 case CMD_keepjumps:
1396 case CMD_keepmarks:
1397 case CMD_keeppatterns:
1398 case CMD_ldo:
1399 case CMD_leftabove:
1400 case CMD_lfdo:
1401 case CMD_lockmarks:
1402 case CMD_noautocmd:
1403 case CMD_noswapfile:
1404 case CMD_rightbelow:
1405 case CMD_sandbox:
1406 case CMD_silent:
1407 case CMD_tab:
1408 case CMD_tabdo:
1409 case CMD_topleft:
1410 case CMD_verbose:
1411 case CMD_vertical:
1412 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001413 case CMD_vim9cmd:
1414 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001415 return arg;
1416
1417 case CMD_filter:
1418 if (*arg != NUL)
1419 arg = skip_vimgrep_pat(arg, NULL, NULL);
1420 if (arg == NULL || *arg == NUL)
1421 {
1422 xp->xp_context = EXPAND_NOTHING;
1423 return NULL;
1424 }
1425 return skipwhite(arg);
1426
1427#ifdef FEAT_SEARCH_EXTRA
1428 case CMD_match:
1429 if (*arg == NUL || !ends_excmd(*arg))
1430 {
1431 // also complete "None"
1432 set_context_in_echohl_cmd(xp, arg);
1433 arg = skipwhite(skiptowhite(arg));
1434 if (*arg != NUL)
1435 {
1436 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001437 arg = skip_regexp(arg + 1, *arg, magic_isset());
Bram Moolenaard0190392019-08-23 21:17:35 +02001438 }
1439 }
1440 return find_nextcmd(arg);
1441#endif
1442
1443 // All completion for the +cmdline_compl feature goes here.
1444
1445 case CMD_command:
1446 return set_context_in_user_cmd(xp, arg);
1447
1448 case CMD_delcommand:
1449 xp->xp_context = EXPAND_USER_COMMANDS;
1450 xp->xp_pattern = arg;
1451 break;
1452
1453 case CMD_global:
1454 case CMD_vglobal:
1455 delim = *arg; // get the delimiter
1456 if (delim)
1457 ++arg; // skip delimiter if there is one
1458
1459 while (arg[0] != NUL && arg[0] != delim)
1460 {
1461 if (arg[0] == '\\' && arg[1] != NUL)
1462 ++arg;
1463 ++arg;
1464 }
1465 if (arg[0] != NUL)
1466 return arg + 1;
1467 break;
1468 case CMD_and:
1469 case CMD_substitute:
1470 delim = *arg;
1471 if (delim)
1472 {
1473 // skip "from" part
1474 ++arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001475 arg = skip_regexp(arg, delim, magic_isset());
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001476
1477 if (arg[0] != NUL && arg[0] == delim)
1478 {
1479 // skip "to" part
Bram Moolenaard0190392019-08-23 21:17:35 +02001480 ++arg;
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001481 while (arg[0] != NUL && arg[0] != delim)
1482 {
1483 if (arg[0] == '\\' && arg[1] != NUL)
1484 ++arg;
1485 ++arg;
1486 }
1487 if (arg[0] != NUL) // skip delimiter
1488 ++arg;
1489 }
Bram Moolenaard0190392019-08-23 21:17:35 +02001490 }
Bram Moolenaard0190392019-08-23 21:17:35 +02001491 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1492 ++arg;
1493 if (arg[0] != NUL)
1494 return arg;
1495 break;
1496 case CMD_isearch:
1497 case CMD_dsearch:
1498 case CMD_ilist:
1499 case CMD_dlist:
1500 case CMD_ijump:
1501 case CMD_psearch:
1502 case CMD_djump:
1503 case CMD_isplit:
1504 case CMD_dsplit:
1505 arg = skipwhite(skipdigits(arg)); // skip count
1506 if (*arg == '/') // Match regexp, not just whole words
1507 {
1508 for (++arg; *arg && *arg != '/'; arg++)
1509 if (*arg == '\\' && arg[1] != NUL)
1510 arg++;
1511 if (*arg)
1512 {
1513 arg = skipwhite(arg + 1);
1514
1515 // Check for trailing illegal characters
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00001516 if (*arg == NUL ||
1517 vim_strchr((char_u *)"|\"\n", *arg) == NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001518 xp->xp_context = EXPAND_NOTHING;
1519 else
1520 return arg;
1521 }
1522 }
1523 break;
1524
1525 case CMD_autocmd:
1526 return set_context_in_autocmd(xp, arg, FALSE);
1527 case CMD_doautocmd:
1528 case CMD_doautoall:
1529 return set_context_in_autocmd(xp, arg, TRUE);
1530 case CMD_set:
1531 set_context_in_set_cmd(xp, arg, 0);
1532 break;
1533 case CMD_setglobal:
1534 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1535 break;
1536 case CMD_setlocal:
1537 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1538 break;
1539 case CMD_tag:
1540 case CMD_stag:
1541 case CMD_ptag:
1542 case CMD_ltag:
1543 case CMD_tselect:
1544 case CMD_stselect:
1545 case CMD_ptselect:
1546 case CMD_tjump:
1547 case CMD_stjump:
1548 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001549 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001550 xp->xp_context = EXPAND_TAGS_LISTFILES;
1551 else
1552 xp->xp_context = EXPAND_TAGS;
1553 xp->xp_pattern = arg;
1554 break;
1555 case CMD_augroup:
1556 xp->xp_context = EXPAND_AUGROUP;
1557 xp->xp_pattern = arg;
1558 break;
1559#ifdef FEAT_SYN_HL
1560 case CMD_syntax:
1561 set_context_in_syntax_cmd(xp, arg);
1562 break;
1563#endif
1564#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001565 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001566 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001567 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001568 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001569 case CMD_if:
1570 case CMD_elseif:
1571 case CMD_while:
1572 case CMD_for:
1573 case CMD_echo:
1574 case CMD_echon:
1575 case CMD_execute:
1576 case CMD_echomsg:
1577 case CMD_echoerr:
1578 case CMD_call:
1579 case CMD_return:
1580 case CMD_cexpr:
1581 case CMD_caddexpr:
1582 case CMD_cgetexpr:
1583 case CMD_lexpr:
1584 case CMD_laddexpr:
1585 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001586 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001587 break;
1588
1589 case CMD_unlet:
1590 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1591 arg = xp->xp_pattern + 1;
1592
1593 xp->xp_context = EXPAND_USER_VARS;
1594 xp->xp_pattern = arg;
1595
1596 if (*xp->xp_pattern == '$')
1597 {
1598 xp->xp_context = EXPAND_ENV_VARS;
1599 ++xp->xp_pattern;
1600 }
1601
1602 break;
1603
1604 case CMD_function:
1605 case CMD_delfunction:
1606 xp->xp_context = EXPAND_USER_FUNC;
1607 xp->xp_pattern = arg;
1608 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001609 case CMD_disassemble:
1610 set_context_in_disassemble_cmd(xp, arg);
1611 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001612
1613 case CMD_echohl:
1614 set_context_in_echohl_cmd(xp, arg);
1615 break;
1616#endif
1617 case CMD_highlight:
1618 set_context_in_highlight_cmd(xp, arg);
1619 break;
1620#ifdef FEAT_CSCOPE
1621 case CMD_cscope:
1622 case CMD_lcscope:
1623 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001624 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001625 break;
1626#endif
1627#ifdef FEAT_SIGNS
1628 case CMD_sign:
1629 set_context_in_sign_cmd(xp, arg);
1630 break;
1631#endif
1632 case CMD_bdelete:
1633 case CMD_bwipeout:
1634 case CMD_bunload:
1635 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1636 arg = xp->xp_pattern + 1;
1637 // FALLTHROUGH
1638 case CMD_buffer:
1639 case CMD_sbuffer:
1640 case CMD_checktime:
1641 xp->xp_context = EXPAND_BUFFERS;
1642 xp->xp_pattern = arg;
1643 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001644#ifdef FEAT_DIFF
1645 case CMD_diffget:
1646 case CMD_diffput:
1647 // If current buffer is in diff mode, complete buffer names
1648 // which are in diff mode, and different than current buffer.
1649 xp->xp_context = EXPAND_DIFF_BUFFERS;
1650 xp->xp_pattern = arg;
1651 break;
1652#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001653 case CMD_USER:
1654 case CMD_USER_BUF:
1655 if (compl != EXPAND_NOTHING)
1656 {
1657 // EX_XFILE: file names are handled above
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001658 if (!(argt & EX_XFILE))
Bram Moolenaard0190392019-08-23 21:17:35 +02001659 {
1660#ifdef FEAT_MENU
1661 if (compl == EXPAND_MENUS)
1662 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1663#endif
1664 if (compl == EXPAND_COMMANDS)
1665 return arg;
1666 if (compl == EXPAND_MAPPINGS)
1667 return set_context_in_map_cmd(xp, (char_u *)"map",
1668 arg, forceit, FALSE, FALSE, CMD_map);
1669 // Find start of last argument.
1670 p = arg;
1671 while (*p)
1672 {
1673 if (*p == ' ')
1674 // argument starts after a space
1675 arg = p + 1;
1676 else if (*p == '\\' && *(p + 1) != NUL)
1677 ++p; // skip over escaped character
1678 MB_PTR_ADV(p);
1679 }
1680 xp->xp_pattern = arg;
1681 }
1682 xp->xp_context = compl;
1683 }
1684 break;
1685
1686 case CMD_map: case CMD_noremap:
1687 case CMD_nmap: case CMD_nnoremap:
1688 case CMD_vmap: case CMD_vnoremap:
1689 case CMD_omap: case CMD_onoremap:
1690 case CMD_imap: case CMD_inoremap:
1691 case CMD_cmap: case CMD_cnoremap:
1692 case CMD_lmap: case CMD_lnoremap:
1693 case CMD_smap: case CMD_snoremap:
1694 case CMD_tmap: case CMD_tnoremap:
1695 case CMD_xmap: case CMD_xnoremap:
1696 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001697 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001698 case CMD_unmap:
1699 case CMD_nunmap:
1700 case CMD_vunmap:
1701 case CMD_ounmap:
1702 case CMD_iunmap:
1703 case CMD_cunmap:
1704 case CMD_lunmap:
1705 case CMD_sunmap:
1706 case CMD_tunmap:
1707 case CMD_xunmap:
1708 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001709 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001710 case CMD_mapclear:
1711 case CMD_nmapclear:
1712 case CMD_vmapclear:
1713 case CMD_omapclear:
1714 case CMD_imapclear:
1715 case CMD_cmapclear:
1716 case CMD_lmapclear:
1717 case CMD_smapclear:
1718 case CMD_tmapclear:
1719 case CMD_xmapclear:
1720 xp->xp_context = EXPAND_MAPCLEAR;
1721 xp->xp_pattern = arg;
1722 break;
1723
1724 case CMD_abbreviate: case CMD_noreabbrev:
1725 case CMD_cabbrev: case CMD_cnoreabbrev:
1726 case CMD_iabbrev: case CMD_inoreabbrev:
1727 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001728 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001729 case CMD_unabbreviate:
1730 case CMD_cunabbrev:
1731 case CMD_iunabbrev:
1732 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001733 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001734#ifdef FEAT_MENU
1735 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1736 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1737 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1738 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1739 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1740 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1741 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1742 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1743 case CMD_tmenu: case CMD_tunmenu:
1744 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1745 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1746#endif
1747
1748 case CMD_colorscheme:
1749 xp->xp_context = EXPAND_COLORS;
1750 xp->xp_pattern = arg;
1751 break;
1752
1753 case CMD_compiler:
1754 xp->xp_context = EXPAND_COMPILER;
1755 xp->xp_pattern = arg;
1756 break;
1757
1758 case CMD_ownsyntax:
1759 xp->xp_context = EXPAND_OWNSYNTAX;
1760 xp->xp_pattern = arg;
1761 break;
1762
1763 case CMD_setfiletype:
1764 xp->xp_context = EXPAND_FILETYPE;
1765 xp->xp_pattern = arg;
1766 break;
1767
1768 case CMD_packadd:
1769 xp->xp_context = EXPAND_PACKADD;
1770 xp->xp_pattern = arg;
1771 break;
1772
1773#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1774 case CMD_language:
1775 p = skiptowhite(arg);
1776 if (*p == NUL)
1777 {
1778 xp->xp_context = EXPAND_LANGUAGE;
1779 xp->xp_pattern = arg;
1780 }
1781 else
1782 {
1783 if ( STRNCMP(arg, "messages", p - arg) == 0
1784 || STRNCMP(arg, "ctype", p - arg) == 0
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001785 || STRNCMP(arg, "time", p - arg) == 0
1786 || STRNCMP(arg, "collate", p - arg) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001787 {
1788 xp->xp_context = EXPAND_LOCALES;
1789 xp->xp_pattern = skipwhite(p);
1790 }
1791 else
1792 xp->xp_context = EXPAND_NOTHING;
1793 }
1794 break;
1795#endif
1796#if defined(FEAT_PROFILE)
1797 case CMD_profile:
1798 set_context_in_profile_cmd(xp, arg);
1799 break;
1800#endif
1801 case CMD_behave:
1802 xp->xp_context = EXPAND_BEHAVE;
1803 xp->xp_pattern = arg;
1804 break;
1805
1806 case CMD_messages:
1807 xp->xp_context = EXPAND_MESSAGES;
1808 xp->xp_pattern = arg;
1809 break;
1810
1811 case CMD_history:
1812 xp->xp_context = EXPAND_HISTORY;
1813 xp->xp_pattern = arg;
1814 break;
1815#if defined(FEAT_PROFILE)
1816 case CMD_syntime:
1817 xp->xp_context = EXPAND_SYNTIME;
1818 xp->xp_pattern = arg;
1819 break;
1820#endif
1821
1822 case CMD_argdelete:
1823 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1824 arg = xp->xp_pattern + 1;
1825 xp->xp_context = EXPAND_ARGLIST;
1826 xp->xp_pattern = arg;
1827 break;
1828
1829 default:
1830 break;
1831 }
1832 return NULL;
1833}
1834
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001835/*
1836 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
1837 * we don't need/want deleted. Maybe this could be done better if we didn't
1838 * repeat all this stuff. The only problem is that they may not stay
1839 * perfectly compatible with each other, but then the command line syntax
1840 * probably won't change that much -- webb.
1841 */
1842 static char_u *
1843set_one_cmd_context(
1844 expand_T *xp,
1845 char_u *buff) // buffer for command string
1846{
1847 char_u *p;
1848 char_u *cmd, *arg;
1849 int len = 0;
1850 exarg_T ea;
1851 int compl = EXPAND_NOTHING;
1852 int forceit = FALSE;
1853 int usefilter = FALSE; // filter instead of file name
1854
1855 ExpandInit(xp);
1856 xp->xp_pattern = buff;
1857 xp->xp_line = buff;
1858 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
1859 ea.argt = 0;
1860
1861 // 1. skip comment lines and leading space, colons or bars
1862 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
1863 ;
1864 xp->xp_pattern = cmd;
1865
1866 if (*cmd == NUL)
1867 return NULL;
1868 if (*cmd == '"') // ignore comment lines
1869 {
1870 xp->xp_context = EXPAND_NOTHING;
1871 return NULL;
1872 }
1873
1874 // 3. Skip over the range to find the command.
1875 cmd = skip_range(cmd, TRUE, &xp->xp_context);
1876 xp->xp_pattern = cmd;
1877 if (*cmd == NUL)
1878 return NULL;
1879 if (*cmd == '"')
1880 {
1881 xp->xp_context = EXPAND_NOTHING;
1882 return NULL;
1883 }
1884
1885 if (*cmd == '|' || *cmd == '\n')
1886 return cmd + 1; // There's another command
1887
1888 // Get the command index.
1889 p = set_cmd_index(cmd, &ea, xp, &compl);
1890 if (p == NULL)
1891 return NULL;
1892
1893 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
1894
1895 if (*p == '!') // forced commands
1896 {
1897 forceit = TRUE;
1898 ++p;
1899 }
1900
1901 // 6. parse arguments
1902 if (!IS_USER_CMDIDX(ea.cmdidx))
1903 ea.argt = excmd_get_argt(ea.cmdidx);
1904
1905 arg = skipwhite(p);
1906
1907 // Skip over ++argopt argument
1908 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
1909 {
1910 p = arg;
1911 while (*p && !vim_isspace(*p))
1912 MB_PTR_ADV(p);
1913 arg = skipwhite(p);
1914 }
1915
1916 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
1917 {
1918 if (*arg == '>') // append
1919 {
1920 if (*++arg == '>')
1921 ++arg;
1922 arg = skipwhite(arg);
1923 }
1924 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
1925 {
1926 ++arg;
1927 usefilter = TRUE;
1928 }
1929 }
1930
1931 if (ea.cmdidx == CMD_read)
1932 {
1933 usefilter = forceit; // :r! filter if forced
1934 if (*arg == '!') // :r !filter
1935 {
1936 ++arg;
1937 usefilter = TRUE;
1938 }
1939 }
1940
1941 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
1942 {
1943 while (*arg == *cmd) // allow any number of '>' or '<'
1944 ++arg;
1945 arg = skipwhite(arg);
1946 }
1947
1948 // Does command allow "+command"?
1949 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
1950 {
1951 // Check if we're in the +command
1952 p = arg + 1;
1953 arg = skip_cmd_arg(arg, FALSE);
1954
1955 // Still touching the command after '+'?
1956 if (*arg == NUL)
1957 return p;
1958
1959 // Skip space(s) after +command to get to the real argument
1960 arg = skipwhite(arg);
1961 }
1962
1963
1964 // Check for '|' to separate commands and '"' to start comments.
1965 // Don't do this for ":read !cmd" and ":write !cmd".
1966 if ((ea.argt & EX_TRLBAR) && !usefilter)
1967 {
1968 p = arg;
1969 // ":redir @" is not the start of a comment
1970 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
1971 p += 2;
1972 while (*p)
1973 {
1974 if (*p == Ctrl_V)
1975 {
1976 if (p[1] != NUL)
1977 ++p;
1978 }
1979 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
1980 || *p == '|' || *p == '\n')
1981 {
1982 if (*(p - 1) != '\\')
1983 {
1984 if (*p == '|' || *p == '\n')
1985 return p + 1;
1986 return NULL; // It's a comment
1987 }
1988 }
1989 MB_PTR_ADV(p);
1990 }
1991 }
1992
1993 if (!(ea.argt & EX_EXTRA) && *arg != NUL
1994 && vim_strchr((char_u *)"|\"", *arg) == NULL)
1995 // no arguments allowed but there is something
1996 return NULL;
1997
1998 // Find start of last argument (argument just before cursor):
1999 p = buff;
2000 xp->xp_pattern = p;
2001 len = (int)STRLEN(buff);
2002 while (*p && p < buff + len)
2003 {
2004 if (*p == ' ' || *p == TAB)
2005 {
2006 // argument starts after a space
2007 xp->xp_pattern = ++p;
2008 }
2009 else
2010 {
2011 if (*p == '\\' && *(p + 1) != NUL)
2012 ++p; // skip over escaped character
2013 MB_PTR_ADV(p);
2014 }
2015 }
2016
2017 if (ea.argt & EX_XFILE)
2018 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2019
2020 // 6. Switch on command name.
2021 return set_context_by_cmdname(cmd, ea.cmdidx, arg, ea.argt, compl, xp,
2022 forceit);
2023}
2024
Bram Moolenaar66b51422019-08-18 21:44:12 +02002025 void
2026set_cmd_context(
2027 expand_T *xp,
2028 char_u *str, // start of command line
2029 int len, // length of command line (excl. NUL)
2030 int col, // position of cursor
2031 int use_ccline UNUSED) // use ccline for info
2032{
2033#ifdef FEAT_EVAL
2034 cmdline_info_T *ccline = get_cmdline_info();
2035#endif
2036 int old_char = NUL;
2037 char_u *nextcomm;
2038
2039 // Avoid a UMR warning from Purify, only save the character if it has been
2040 // written before.
2041 if (col < len)
2042 old_char = str[col];
2043 str[col] = NUL;
2044 nextcomm = str;
2045
2046#ifdef FEAT_EVAL
2047 if (use_ccline && ccline->cmdfirstc == '=')
2048 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002049 // pass CMD_SIZE because there is no real command
2050 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002051 }
2052 else if (use_ccline && ccline->input_fn)
2053 {
2054 xp->xp_context = ccline->xp_context;
2055 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002056 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002057 }
2058 else
2059#endif
2060 while (nextcomm != NULL)
2061 nextcomm = set_one_cmd_context(xp, nextcomm);
2062
2063 // Store the string here so that call_user_expand_func() can get to them
2064 // easily.
2065 xp->xp_line = str;
2066 xp->xp_col = col;
2067
2068 str[col] = old_char;
2069}
2070
2071/*
2072 * Expand the command line "str" from context "xp".
2073 * "xp" must have been set by set_cmd_context().
2074 * xp->xp_pattern points into "str", to where the text that is to be expanded
2075 * starts.
2076 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2077 * cursor.
2078 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2079 * key that triggered expansion literally.
2080 * Returns EXPAND_OK otherwise.
2081 */
2082 int
2083expand_cmdline(
2084 expand_T *xp,
2085 char_u *str, // start of command line
2086 int col, // position of cursor
2087 int *matchcount, // return: nr of matches
2088 char_u ***matches) // return: array of pointers to matches
2089{
2090 char_u *file_str = NULL;
2091 int options = WILD_ADD_SLASH|WILD_SILENT;
2092
2093 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2094 {
2095 beep_flush();
2096 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2097 }
2098 if (xp->xp_context == EXPAND_NOTHING)
2099 {
2100 // Caller can use the character as a normal char instead
2101 return EXPAND_NOTHING;
2102 }
2103
2104 // add star to file name, or convert to regexp if not exp. files.
2105 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
2106 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2107 if (file_str == NULL)
2108 return EXPAND_UNSUCCESSFUL;
2109
2110 if (p_wic)
2111 options += WILD_ICASE;
2112
2113 // find all files that match the description
2114 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
2115 {
2116 *matchcount = 0;
2117 *matches = NULL;
2118 }
2119 vim_free(file_str);
2120
2121 return EXPAND_OK;
2122}
2123
Bram Moolenaar66b51422019-08-18 21:44:12 +02002124/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002125 * Expand file or directory names.
2126 */
2127 static int
2128expand_files_and_dirs(
2129 expand_T *xp,
2130 char_u *pat,
2131 char_u ***file,
2132 int *num_file,
2133 int flags,
2134 int options)
2135{
2136 int free_pat = FALSE;
2137 int i;
2138 int ret;
2139
2140 // for ":set path=" and ":set tags=" halve backslashes for escaped
2141 // space
2142 if (xp->xp_backslash != XP_BS_NONE)
2143 {
2144 free_pat = TRUE;
2145 pat = vim_strsave(pat);
2146 for (i = 0; pat[i]; ++i)
2147 if (pat[i] == '\\')
2148 {
2149 if (xp->xp_backslash == XP_BS_THREE
2150 && pat[i + 1] == '\\'
2151 && pat[i + 2] == '\\'
2152 && pat[i + 3] == ' ')
2153 STRMOVE(pat + i, pat + i + 3);
2154 if (xp->xp_backslash == XP_BS_ONE
2155 && pat[i + 1] == ' ')
2156 STRMOVE(pat + i, pat + i + 1);
2157 }
2158 }
2159
2160 if (xp->xp_context == EXPAND_FILES)
2161 flags |= EW_FILE;
2162 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2163 flags |= (EW_FILE | EW_PATH);
2164 else
2165 flags = (flags | EW_DIR) & ~EW_FILE;
2166 if (options & WILD_ICASE)
2167 flags |= EW_ICASE;
2168
2169 // Expand wildcards, supporting %:h and the like.
2170 ret = expand_wildcards_eval(&pat, num_file, file, flags);
2171 if (free_pat)
2172 vim_free(pat);
2173#ifdef BACKSLASH_IN_FILENAME
2174 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2175 {
2176 int j;
2177
2178 for (j = 0; j < *num_file; ++j)
2179 {
2180 char_u *ptr = (*file)[j];
2181
2182 while (*ptr != NUL)
2183 {
2184 if (p_csl[0] == 's' && *ptr == '\\')
2185 *ptr = '/';
2186 else if (p_csl[0] == 'b' && *ptr == '/')
2187 *ptr = '\\';
2188 ptr += (*mb_ptr2len)(ptr);
2189 }
2190 }
2191 }
2192#endif
2193 return ret;
2194}
2195
2196/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002197 * Function given to ExpandGeneric() to obtain the possible arguments of the
2198 * ":behave {mswin,xterm}" command.
2199 */
2200 static char_u *
2201get_behave_arg(expand_T *xp UNUSED, int idx)
2202{
2203 if (idx == 0)
2204 return (char_u *)"mswin";
2205 if (idx == 1)
2206 return (char_u *)"xterm";
2207 return NULL;
2208}
2209
2210/*
2211 * Function given to ExpandGeneric() to obtain the possible arguments of the
2212 * ":messages {clear}" command.
2213 */
2214 static char_u *
2215get_messages_arg(expand_T *xp UNUSED, int idx)
2216{
2217 if (idx == 0)
2218 return (char_u *)"clear";
2219 return NULL;
2220}
2221
2222 static char_u *
2223get_mapclear_arg(expand_T *xp UNUSED, int idx)
2224{
2225 if (idx == 0)
2226 return (char_u *)"<buffer>";
2227 return NULL;
2228}
2229
2230/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002231 * Do the expansion based on xp->xp_context and 'rmp'.
2232 */
2233 static int
2234ExpandOther(
2235 expand_T *xp,
2236 regmatch_T *rmp,
2237 int *num_file,
2238 char_u ***file)
2239{
2240 static struct expgen
2241 {
2242 int context;
2243 char_u *((*func)(expand_T *, int));
2244 int ic;
2245 int escaped;
2246 } tab[] =
2247 {
2248 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2249 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2250 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2251 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2252 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2253 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2254 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2255 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2256 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2257 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2258# ifdef FEAT_EVAL
2259 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2260 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2261 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2262 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2263 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2264# endif
2265# ifdef FEAT_MENU
2266 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2267 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2268# endif
2269# ifdef FEAT_SYN_HL
2270 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2271# endif
2272# ifdef FEAT_PROFILE
2273 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2274# endif
2275 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2276 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2277 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
2278# ifdef FEAT_CSCOPE
2279 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2280# endif
2281# ifdef FEAT_SIGNS
2282 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2283# endif
2284# ifdef FEAT_PROFILE
2285 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2286# endif
2287# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2288 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2289 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2290# endif
2291 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2292 {EXPAND_USER, get_users, TRUE, FALSE},
2293 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2294 };
2295 int i;
2296 int ret = FAIL;
2297
2298 // Find a context in the table and call the ExpandGeneric() with the
2299 // right function to do the expansion.
2300 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2301 {
2302 if (xp->xp_context == tab[i].context)
2303 {
2304 if (tab[i].ic)
2305 rmp->rm_ic = TRUE;
2306 ret = ExpandGeneric(xp, rmp, num_file, file,
2307 tab[i].func, tab[i].escaped);
2308 break;
2309 }
2310 }
2311
2312 return ret;
2313}
2314
2315/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002316 * Do the expansion based on xp->xp_context and "pat".
2317 */
2318 static int
2319ExpandFromContext(
2320 expand_T *xp,
2321 char_u *pat,
2322 int *num_file,
2323 char_u ***file,
2324 int options) // WILD_ flags
2325{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002326 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002327 int ret;
2328 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002329 char_u *tofree = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002330
2331 flags = EW_DIR; // include directories
2332 if (options & WILD_LIST_NOTFOUND)
2333 flags |= EW_NOTFOUND;
2334 if (options & WILD_ADD_SLASH)
2335 flags |= EW_ADDSLASH;
2336 if (options & WILD_KEEP_ALL)
2337 flags |= EW_KEEPALL;
2338 if (options & WILD_SILENT)
2339 flags |= EW_SILENT;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02002340 if (options & WILD_NOERROR)
2341 flags |= EW_NOERROR;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002342 if (options & WILD_ALLLINKS)
2343 flags |= EW_ALLLINKS;
2344
2345 if (xp->xp_context == EXPAND_FILES
2346 || xp->xp_context == EXPAND_DIRECTORIES
2347 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002348 return expand_files_and_dirs(xp, pat, file, num_file, flags, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002349
2350 *file = (char_u **)"";
2351 *num_file = 0;
2352 if (xp->xp_context == EXPAND_HELP)
2353 {
2354 // With an empty argument we would get all the help tags, which is
2355 // very slow. Get matches for "help" instead.
2356 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
2357 num_file, file, FALSE) == OK)
2358 {
2359#ifdef FEAT_MULTI_LANG
2360 cleanup_help_tags(*num_file, *file);
2361#endif
2362 return OK;
2363 }
2364 return FAIL;
2365 }
2366
Bram Moolenaar66b51422019-08-18 21:44:12 +02002367 if (xp->xp_context == EXPAND_SHELLCMD)
2368 return expand_shellcmd(pat, num_file, file, flags);
2369 if (xp->xp_context == EXPAND_OLD_SETTING)
2370 return ExpandOldSetting(num_file, file);
2371 if (xp->xp_context == EXPAND_BUFFERS)
2372 return ExpandBufnames(pat, num_file, file, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002373#ifdef FEAT_DIFF
2374 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
2375 return ExpandBufnames(pat, num_file, file, options | BUF_DIFF_FILTER);
2376#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002377 if (xp->xp_context == EXPAND_TAGS
2378 || xp->xp_context == EXPAND_TAGS_LISTFILES)
2379 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
2380 if (xp->xp_context == EXPAND_COLORS)
2381 {
2382 char *directories[] = {"colors", NULL};
2383 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
2384 directories);
2385 }
2386 if (xp->xp_context == EXPAND_COMPILER)
2387 {
2388 char *directories[] = {"compiler", NULL};
2389 return ExpandRTDir(pat, 0, num_file, file, directories);
2390 }
2391 if (xp->xp_context == EXPAND_OWNSYNTAX)
2392 {
2393 char *directories[] = {"syntax", NULL};
2394 return ExpandRTDir(pat, 0, num_file, file, directories);
2395 }
2396 if (xp->xp_context == EXPAND_FILETYPE)
2397 {
2398 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
2399 return ExpandRTDir(pat, 0, num_file, file, directories);
2400 }
2401# if defined(FEAT_EVAL)
2402 if (xp->xp_context == EXPAND_USER_LIST)
2403 return ExpandUserList(xp, num_file, file);
2404# endif
2405 if (xp->xp_context == EXPAND_PACKADD)
2406 return ExpandPackAddDir(pat, num_file, file);
2407
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002408 // When expanding a function name starting with s:, match the <SNR>nr_
2409 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002410 if ((xp->xp_context == EXPAND_USER_FUNC
2411 || xp->xp_context == EXPAND_DISASSEMBLE)
2412 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002413 {
2414 int len = (int)STRLEN(pat) + 20;
2415
2416 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002417 if (tofree == NULL)
2418 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002419 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002420 pat = tofree;
2421 }
2422
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002423 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002424 if (regmatch.regprog == NULL)
2425 return FAIL;
2426
2427 // set ignore-case according to p_ic, p_scs and pat
2428 regmatch.rm_ic = ignorecase(pat);
2429
2430 if (xp->xp_context == EXPAND_SETTINGS
2431 || xp->xp_context == EXPAND_BOOL_SETTINGS)
2432 ret = ExpandSettings(xp, &regmatch, num_file, file);
2433 else if (xp->xp_context == EXPAND_MAPPINGS)
2434 ret = ExpandMappings(&regmatch, num_file, file);
2435# if defined(FEAT_EVAL)
2436 else if (xp->xp_context == EXPAND_USER_DEFINED)
2437 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
2438# endif
2439 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002440 ret = ExpandOther(xp, &regmatch, num_file, file);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002441
2442 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002443 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002444
2445 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002446}
2447
Bram Moolenaar66b51422019-08-18 21:44:12 +02002448/*
2449 * Expand a list of names.
2450 *
2451 * Generic function for command line completion. It calls a function to
2452 * obtain strings, one by one. The strings are matched against a regexp
2453 * program. Matching strings are copied into an array, which is returned.
2454 *
2455 * Returns OK when no problems encountered, FAIL for error (out of memory).
2456 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002457 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002458ExpandGeneric(
2459 expand_T *xp,
2460 regmatch_T *regmatch,
2461 int *num_file,
2462 char_u ***file,
2463 char_u *((*func)(expand_T *, int)),
2464 // returns a string from the list
2465 int escaped)
2466{
2467 int i;
2468 int count = 0;
2469 int round;
2470 char_u *str;
2471
2472 // do this loop twice:
2473 // round == 0: count the number of matching names
2474 // round == 1: copy the matching names into allocated memory
2475 for (round = 0; round <= 1; ++round)
2476 {
2477 for (i = 0; ; ++i)
2478 {
2479 str = (*func)(xp, i);
2480 if (str == NULL) // end of list
2481 break;
2482 if (*str == NUL) // skip empty strings
2483 continue;
2484
2485 if (vim_regexec(regmatch, str, (colnr_T)0))
2486 {
2487 if (round)
2488 {
2489 if (escaped)
2490 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2491 else
2492 str = vim_strsave(str);
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002493 if (str == NULL)
2494 {
2495 FreeWild(count, *file);
2496 *num_file = 0;
2497 *file = NULL;
2498 return FAIL;
2499 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002500 (*file)[count] = str;
2501# ifdef FEAT_MENU
2502 if (func == get_menu_names && str != NULL)
2503 {
2504 // test for separator added by get_menu_names()
2505 str += STRLEN(str) - 1;
2506 if (*str == '\001')
2507 *str = '.';
2508 }
2509# endif
2510 }
2511 ++count;
2512 }
2513 }
2514 if (round == 0)
2515 {
2516 if (count == 0)
2517 return OK;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002518 *file = ALLOC_MULT(char_u *, count);
2519 if (*file == NULL)
2520 {
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002521 *num_file = 0;
2522 *file = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002523 return FAIL;
2524 }
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002525 *num_file = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002526 count = 0;
2527 }
2528 }
2529
2530 // Sort the results. Keep menu's in the specified order.
2531 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2532 {
2533 if (xp->xp_context == EXPAND_EXPRESSION
2534 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002535 || xp->xp_context == EXPAND_USER_FUNC
2536 || xp->xp_context == EXPAND_DISASSEMBLE)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002537 // <SNR> functions should be sorted to the end.
2538 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
2539 sort_func_compare);
2540 else
2541 sort_strings(*file, *num_file);
2542 }
2543
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002544#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002545 // Reset the variables used for special highlight names expansion, so that
2546 // they don't show up when getting normal highlight names by ID.
2547 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002548#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002549 return OK;
2550}
2551
2552/*
2553 * Complete a shell command.
2554 * Returns FAIL or OK;
2555 */
2556 static int
2557expand_shellcmd(
2558 char_u *filepat, // pattern to match with command names
2559 int *num_file, // return: number of matches
2560 char_u ***file, // return: array with matches
2561 int flagsarg) // EW_ flags
2562{
2563 char_u *pat;
2564 int i;
2565 char_u *path = NULL;
2566 int mustfree = FALSE;
2567 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002568 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002569 size_t l;
2570 char_u *s, *e;
2571 int flags = flagsarg;
2572 int ret;
2573 int did_curdir = FALSE;
2574 hashtab_T found_ht;
2575 hashitem_T *hi;
2576 hash_T hash;
2577
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002578 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002579 if (buf == NULL)
2580 return FAIL;
2581
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002582 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002583 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002584 if (pat == NULL)
2585 {
2586 vim_free(buf);
2587 return FAIL;
2588 }
2589
Bram Moolenaar66b51422019-08-18 21:44:12 +02002590 for (i = 0; pat[i]; ++i)
2591 if (pat[i] == '\\' && pat[i + 1] == ' ')
2592 STRMOVE(pat + i, pat + i + 1);
2593
2594 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2595
2596 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2597 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2598 path = (char_u *)".";
2599 else
2600 {
2601 // For an absolute name we don't use $PATH.
2602 if (!mch_isFullName(pat))
2603 path = vim_getenv((char_u *)"PATH", &mustfree);
2604 if (path == NULL)
2605 path = (char_u *)"";
2606 }
2607
2608 // Go over all directories in $PATH. Expand matches in that directory and
2609 // collect them in "ga". When "." is not in $PATH also expand for the
2610 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002611 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002612 hash_init(&found_ht);
2613 for (s = path; ; s = e)
2614 {
2615# if defined(MSWIN)
2616 e = vim_strchr(s, ';');
2617# else
2618 e = vim_strchr(s, ':');
2619# endif
2620 if (e == NULL)
2621 e = s + STRLEN(s);
2622
2623 if (*s == NUL)
2624 {
2625 if (did_curdir)
2626 break;
2627 // Find directories in the current directory, path is empty.
2628 did_curdir = TRUE;
2629 flags |= EW_DIR;
2630 }
2631 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2632 {
2633 did_curdir = TRUE;
2634 flags |= EW_DIR;
2635 }
2636 else
2637 // Do not match directories inside a $PATH item.
2638 flags &= ~EW_DIR;
2639
2640 l = e - s;
2641 if (l > MAXPATHL - 5)
2642 break;
2643 vim_strncpy(buf, s, l);
2644 add_pathsep(buf);
2645 l = STRLEN(buf);
2646 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2647
2648 // Expand matches in one directory of $PATH.
2649 ret = expand_wildcards(1, &buf, num_file, file, flags);
2650 if (ret == OK)
2651 {
2652 if (ga_grow(&ga, *num_file) == FAIL)
2653 FreeWild(*num_file, *file);
2654 else
2655 {
2656 for (i = 0; i < *num_file; ++i)
2657 {
2658 char_u *name = (*file)[i];
2659
2660 if (STRLEN(name) > l)
2661 {
2662 // Check if this name was already found.
2663 hash = hash_hash(name + l);
2664 hi = hash_lookup(&found_ht, name + l, hash);
2665 if (HASHITEM_EMPTY(hi))
2666 {
2667 // Remove the path that was prepended.
2668 STRMOVE(name, name + l);
2669 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
2670 hash_add_item(&found_ht, hi, name, hash);
2671 name = NULL;
2672 }
2673 }
2674 vim_free(name);
2675 }
2676 vim_free(*file);
2677 }
2678 }
2679 if (*e != NUL)
2680 ++e;
2681 }
2682 *file = ga.ga_data;
2683 *num_file = ga.ga_len;
2684
2685 vim_free(buf);
2686 vim_free(pat);
2687 if (mustfree)
2688 vim_free(path);
2689 hash_clear(&found_ht);
2690 return OK;
2691}
2692
2693# if defined(FEAT_EVAL)
2694/*
2695 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002696 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002697 */
2698 static void *
2699call_user_expand_func(
2700 void *(*user_expand_func)(char_u *, int, typval_T *),
2701 expand_T *xp,
2702 int *num_file,
2703 char_u ***file)
2704{
2705 cmdline_info_T *ccline = get_cmdline_info();
2706 int keep = 0;
2707 typval_T args[4];
2708 sctx_T save_current_sctx = current_sctx;
2709 char_u *pat = NULL;
2710 void *ret;
2711
2712 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2713 return NULL;
2714 *num_file = 0;
2715 *file = NULL;
2716
2717 if (ccline->cmdbuff != NULL)
2718 {
2719 keep = ccline->cmdbuff[ccline->cmdlen];
2720 ccline->cmdbuff[ccline->cmdlen] = 0;
2721 }
2722
2723 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2724
2725 args[0].v_type = VAR_STRING;
2726 args[0].vval.v_string = pat;
2727 args[1].v_type = VAR_STRING;
2728 args[1].vval.v_string = xp->xp_line;
2729 args[2].v_type = VAR_NUMBER;
2730 args[2].vval.v_number = xp->xp_col;
2731 args[3].v_type = VAR_UNKNOWN;
2732
2733 current_sctx = xp->xp_script_ctx;
2734
2735 ret = user_expand_func(xp->xp_arg, 3, args);
2736
2737 current_sctx = save_current_sctx;
2738 if (ccline->cmdbuff != NULL)
2739 ccline->cmdbuff[ccline->cmdlen] = keep;
2740
2741 vim_free(pat);
2742 return ret;
2743}
2744
2745/*
2746 * Expand names with a function defined by the user.
2747 */
2748 static int
2749ExpandUserDefined(
2750 expand_T *xp,
2751 regmatch_T *regmatch,
2752 int *num_file,
2753 char_u ***file)
2754{
2755 char_u *retstr;
2756 char_u *s;
2757 char_u *e;
2758 int keep;
2759 garray_T ga;
2760 int skip;
2761
2762 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
2763 if (retstr == NULL)
2764 return FAIL;
2765
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002766 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002767 for (s = retstr; *s != NUL; s = e)
2768 {
2769 e = vim_strchr(s, '\n');
2770 if (e == NULL)
2771 e = s + STRLEN(s);
2772 keep = *e;
2773 *e = NUL;
2774
2775 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2776 *e = keep;
2777
2778 if (!skip)
2779 {
2780 if (ga_grow(&ga, 1) == FAIL)
2781 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02002782 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002783 ++ga.ga_len;
2784 }
2785
2786 if (*e != NUL)
2787 ++e;
2788 }
2789 vim_free(retstr);
2790 *file = ga.ga_data;
2791 *num_file = ga.ga_len;
2792 return OK;
2793}
2794
2795/*
2796 * Expand names with a list returned by a function defined by the user.
2797 */
2798 static int
2799ExpandUserList(
2800 expand_T *xp,
2801 int *num_file,
2802 char_u ***file)
2803{
2804 list_T *retlist;
2805 listitem_T *li;
2806 garray_T ga;
2807
2808 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
2809 if (retlist == NULL)
2810 return FAIL;
2811
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002812 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002813 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002814 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002815 {
2816 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
2817 continue; // Skip non-string items and empty strings
2818
2819 if (ga_grow(&ga, 1) == FAIL)
2820 break;
2821
2822 ((char_u **)ga.ga_data)[ga.ga_len] =
2823 vim_strsave(li->li_tv.vval.v_string);
2824 ++ga.ga_len;
2825 }
2826 list_unref(retlist);
2827
2828 *file = ga.ga_data;
2829 *num_file = ga.ga_len;
2830 return OK;
2831}
2832# endif
2833
2834/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002835 * Expand "file" for all comma-separated directories in "path".
2836 * Adds the matches to "ga". Caller must init "ga".
2837 */
2838 void
2839globpath(
2840 char_u *path,
2841 char_u *file,
2842 garray_T *ga,
2843 int expand_options)
2844{
2845 expand_T xpc;
2846 char_u *buf;
2847 int i;
2848 int num_p;
2849 char_u **p;
2850
2851 buf = alloc(MAXPATHL);
2852 if (buf == NULL)
2853 return;
2854
2855 ExpandInit(&xpc);
2856 xpc.xp_context = EXPAND_FILES;
2857
2858 // Loop over all entries in {path}.
2859 while (*path != NUL)
2860 {
2861 // Copy one item of the path to buf[] and concatenate the file name.
2862 copy_option_part(&path, buf, MAXPATHL, ",");
2863 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
2864 {
2865# if defined(MSWIN)
2866 // Using the platform's path separator (\) makes vim incorrectly
2867 // treat it as an escape character, use '/' instead.
2868 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
2869 STRCAT(buf, "/");
2870# else
2871 add_pathsep(buf);
2872# endif
2873 STRCAT(buf, file);
2874 if (ExpandFromContext(&xpc, buf, &num_p, &p,
2875 WILD_SILENT|expand_options) != FAIL && num_p > 0)
2876 {
2877 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
2878
2879 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002880 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02002881 for (i = 0; i < num_p; ++i)
2882 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002883 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02002884 ++ga->ga_len;
2885 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002886 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002887 }
2888 }
2889 }
2890
2891 vim_free(buf);
2892}
Bram Moolenaar66b51422019-08-18 21:44:12 +02002893
Bram Moolenaareadee482020-09-04 15:37:31 +02002894#ifdef FEAT_WILDMENU
2895
2896/*
2897 * Translate some keys pressed when 'wildmenu' is used.
2898 */
2899 int
2900wildmenu_translate_key(
2901 cmdline_info_T *cclp,
2902 int key,
2903 expand_T *xp,
2904 int did_wild_list)
2905{
2906 int c = key;
2907
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002908#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002909 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002910 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002911 // When the popup menu is used for cmdline completion:
2912 // Up : go to the previous item in the menu
2913 // Down : go to the next item in the menu
2914 // Left : go to the parent directory
2915 // Right: list the files in the selected directory
2916 switch (c)
2917 {
2918 case K_UP: c = K_LEFT; break;
2919 case K_DOWN: c = K_RIGHT; break;
2920 case K_LEFT: c = K_UP; break;
2921 case K_RIGHT: c = K_DOWN; break;
2922 default: break;
2923 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002924 }
2925#endif
2926
Bram Moolenaareadee482020-09-04 15:37:31 +02002927 if (did_wild_list && p_wmnu)
2928 {
2929 if (c == K_LEFT)
2930 c = Ctrl_P;
2931 else if (c == K_RIGHT)
2932 c = Ctrl_N;
2933 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002934
Bram Moolenaareadee482020-09-04 15:37:31 +02002935 // Hitting CR after "emenu Name.": complete submenu
2936 if (xp->xp_context == EXPAND_MENUNAMES && p_wmnu
2937 && cclp->cmdpos > 1
2938 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
2939 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
2940 && (c == '\n' || c == '\r' || c == K_KENTER))
2941 c = K_DOWN;
2942
2943 return c;
2944}
2945
2946/*
2947 * Delete characters on the command line, from "from" to the current
2948 * position.
2949 */
2950 static void
2951cmdline_del(cmdline_info_T *cclp, int from)
2952{
2953 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
2954 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
2955 cclp->cmdlen -= cclp->cmdpos - from;
2956 cclp->cmdpos = from;
2957}
2958
2959/*
2960 * Handle a key pressed when wild menu is displayed
2961 */
2962 int
2963wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
2964{
2965 int c = key;
2966 int i;
2967 int j;
2968
2969 if (!p_wmnu)
2970 return c;
2971
2972 // Special translations for 'wildmenu'
2973 if (xp->xp_context == EXPAND_MENUNAMES)
2974 {
2975 // Hitting <Down> after "emenu Name.": complete submenu
2976 if (c == K_DOWN && cclp->cmdpos > 0
2977 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002978 {
Bram Moolenaareadee482020-09-04 15:37:31 +02002979 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002980 KeyTyped = TRUE; // in case the key was mapped
2981 }
Bram Moolenaareadee482020-09-04 15:37:31 +02002982 else if (c == K_UP)
2983 {
2984 // Hitting <Up>: Remove one submenu name in front of the
2985 // cursor
2986 int found = FALSE;
2987
2988 j = (int)(xp->xp_pattern - cclp->cmdbuff);
2989 i = 0;
2990 while (--j > 0)
2991 {
2992 // check for start of menu name
2993 if (cclp->cmdbuff[j] == ' '
2994 && cclp->cmdbuff[j - 1] != '\\')
2995 {
2996 i = j + 1;
2997 break;
2998 }
2999 // check for start of submenu name
3000 if (cclp->cmdbuff[j] == '.'
3001 && cclp->cmdbuff[j - 1] != '\\')
3002 {
3003 if (found)
3004 {
3005 i = j + 1;
3006 break;
3007 }
3008 else
3009 found = TRUE;
3010 }
3011 }
3012 if (i > 0)
3013 cmdline_del(cclp, i);
3014 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003015 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003016 xp->xp_context = EXPAND_NOTHING;
3017 }
3018 }
3019 if ((xp->xp_context == EXPAND_FILES
3020 || xp->xp_context == EXPAND_DIRECTORIES
3021 || xp->xp_context == EXPAND_SHELLCMD))
3022 {
3023 char_u upseg[5];
3024
3025 upseg[0] = PATHSEP;
3026 upseg[1] = '.';
3027 upseg[2] = '.';
3028 upseg[3] = PATHSEP;
3029 upseg[4] = NUL;
3030
3031 if (c == K_DOWN
3032 && cclp->cmdpos > 0
3033 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3034 && (cclp->cmdpos < 3
3035 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3036 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3037 {
3038 // go down a directory
3039 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003040 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003041 }
3042 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
3043 {
3044 // If in a direct ancestor, strip off one ../ to go down
3045 int found = FALSE;
3046
3047 j = cclp->cmdpos;
3048 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3049 while (--j > i)
3050 {
3051 if (has_mbyte)
3052 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3053 if (vim_ispathsep(cclp->cmdbuff[j]))
3054 {
3055 found = TRUE;
3056 break;
3057 }
3058 }
3059 if (found
3060 && cclp->cmdbuff[j - 1] == '.'
3061 && cclp->cmdbuff[j - 2] == '.'
3062 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3063 {
3064 cmdline_del(cclp, j - 2);
3065 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003066 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003067 }
3068 }
3069 else if (c == K_UP)
3070 {
3071 // go up a directory
3072 int found = FALSE;
3073
3074 j = cclp->cmdpos - 1;
3075 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3076 while (--j > i)
3077 {
3078 if (has_mbyte)
3079 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3080 if (vim_ispathsep(cclp->cmdbuff[j])
3081# ifdef BACKSLASH_IN_FILENAME
3082 && vim_strchr((char_u *)" *?[{`$%#",
3083 cclp->cmdbuff[j + 1]) == NULL
3084# endif
3085 )
3086 {
3087 if (found)
3088 {
3089 i = j + 1;
3090 break;
3091 }
3092 else
3093 found = TRUE;
3094 }
3095 }
3096
3097 if (!found)
3098 j = i;
3099 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3100 j += 4;
3101 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3102 && j == i)
3103 j += 3;
3104 else
3105 j = 0;
3106 if (j > 0)
3107 {
3108 // TODO this is only for DOS/UNIX systems - need to put in
3109 // machine-specific stuff here and in upseg init
3110 cmdline_del(cclp, j);
3111 put_on_cmdline(upseg + 1, 3, FALSE);
3112 }
3113 else if (cclp->cmdpos > i)
3114 cmdline_del(cclp, i);
3115
3116 // Now complete in the new directory. Set KeyTyped in case the
3117 // Up key came from a mapping.
3118 c = p_wc;
3119 KeyTyped = TRUE;
3120 }
3121 }
3122
3123 return c;
3124}
3125
3126/*
3127 * Free expanded names when finished walking through the matches
3128 */
3129 void
3130wildmenu_cleanup(cmdline_info_T *cclp)
3131{
3132 int skt = KeyTyped;
3133 int old_RedrawingDisabled = RedrawingDisabled;
3134
3135 if (!p_wmnu || wild_menu_showing == 0)
3136 return;
3137
3138 if (cclp->input_fn)
3139 RedrawingDisabled = 0;
3140
3141 if (wild_menu_showing == WM_SCROLLED)
3142 {
3143 // Entered command line, move it up
3144 cmdline_row--;
3145 redrawcmd();
3146 }
3147 else if (save_p_ls != -1)
3148 {
3149 // restore 'laststatus' and 'winminheight'
3150 p_ls = save_p_ls;
3151 p_wmh = save_p_wmh;
3152 last_status(FALSE);
3153 update_screen(VALID); // redraw the screen NOW
3154 redrawcmd();
3155 save_p_ls = -1;
3156 }
3157 else
3158 {
3159 win_redraw_last_status(topframe);
3160 redraw_statuslines();
3161 }
3162 KeyTyped = skt;
3163 wild_menu_showing = 0;
3164 if (cclp->input_fn)
3165 RedrawingDisabled = old_RedrawingDisabled;
3166}
3167#endif
3168
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003169#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003170/*
3171 * "getcompletion()" function
3172 */
3173 void
3174f_getcompletion(typval_T *argvars, typval_T *rettv)
3175{
3176 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003177 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003178 expand_T xpc;
3179 int filtered = FALSE;
3180 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003181 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003182
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003183 if (in_vim9script()
3184 && (check_for_string_arg(argvars, 0) == FAIL
3185 || check_for_string_arg(argvars, 1) == FAIL
3186 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3187 return;
3188
ii144785fe02021-11-21 12:13:56 +00003189 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003190 if (argvars[1].v_type != VAR_STRING)
3191 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003192 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003193 return;
3194 }
3195 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003196
3197 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003198 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003199
3200 if (p_wic)
3201 options |= WILD_ICASE;
3202
3203 // For filtered results, 'wildignore' is used
3204 if (!filtered)
3205 options |= WILD_KEEP_ALL;
3206
3207 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003208 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003209 {
ii144785fe02021-11-21 12:13:56 +00003210 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003211 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003212 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003213 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003214 else
3215 {
ii144785fe02021-11-21 12:13:56 +00003216 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003217 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3218
3219 xpc.xp_context = cmdcomplete_str_to_type(type);
3220 if (xpc.xp_context == EXPAND_NOTHING)
3221 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003222 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003223 return;
3224 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003225
3226# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003227 if (xpc.xp_context == EXPAND_MENUS)
3228 {
3229 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3230 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3231 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003232# endif
3233# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003234 if (xpc.xp_context == EXPAND_CSCOPE)
3235 {
3236 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3237 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3238 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003239# endif
3240# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003241 if (xpc.xp_context == EXPAND_SIGN)
3242 {
3243 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3244 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3245 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003247 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003248
3249 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3250 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3251 {
3252 int i;
3253
3254 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3255
3256 for (i = 0; i < xpc.xp_numfiles; i++)
3257 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3258 }
3259 vim_free(pat);
3260 ExpandCleanup(&xpc);
3261}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003262#endif // FEAT_EVAL