blob: 2a65a0691e588eaf3939bc06bbac83062b15607f [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());
Bram Moolenaard0190392019-08-23 21:17:35 +02001476 }
1477 // skip "to" part
1478 while (arg[0] != NUL && arg[0] != delim)
1479 {
1480 if (arg[0] == '\\' && arg[1] != NUL)
1481 ++arg;
1482 ++arg;
1483 }
1484 if (arg[0] != NUL) // skip delimiter
1485 ++arg;
1486 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1487 ++arg;
1488 if (arg[0] != NUL)
1489 return arg;
1490 break;
1491 case CMD_isearch:
1492 case CMD_dsearch:
1493 case CMD_ilist:
1494 case CMD_dlist:
1495 case CMD_ijump:
1496 case CMD_psearch:
1497 case CMD_djump:
1498 case CMD_isplit:
1499 case CMD_dsplit:
1500 arg = skipwhite(skipdigits(arg)); // skip count
1501 if (*arg == '/') // Match regexp, not just whole words
1502 {
1503 for (++arg; *arg && *arg != '/'; arg++)
1504 if (*arg == '\\' && arg[1] != NUL)
1505 arg++;
1506 if (*arg)
1507 {
1508 arg = skipwhite(arg + 1);
1509
1510 // Check for trailing illegal characters
1511 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1512 xp->xp_context = EXPAND_NOTHING;
1513 else
1514 return arg;
1515 }
1516 }
1517 break;
1518
1519 case CMD_autocmd:
1520 return set_context_in_autocmd(xp, arg, FALSE);
1521 case CMD_doautocmd:
1522 case CMD_doautoall:
1523 return set_context_in_autocmd(xp, arg, TRUE);
1524 case CMD_set:
1525 set_context_in_set_cmd(xp, arg, 0);
1526 break;
1527 case CMD_setglobal:
1528 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1529 break;
1530 case CMD_setlocal:
1531 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1532 break;
1533 case CMD_tag:
1534 case CMD_stag:
1535 case CMD_ptag:
1536 case CMD_ltag:
1537 case CMD_tselect:
1538 case CMD_stselect:
1539 case CMD_ptselect:
1540 case CMD_tjump:
1541 case CMD_stjump:
1542 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001543 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001544 xp->xp_context = EXPAND_TAGS_LISTFILES;
1545 else
1546 xp->xp_context = EXPAND_TAGS;
1547 xp->xp_pattern = arg;
1548 break;
1549 case CMD_augroup:
1550 xp->xp_context = EXPAND_AUGROUP;
1551 xp->xp_pattern = arg;
1552 break;
1553#ifdef FEAT_SYN_HL
1554 case CMD_syntax:
1555 set_context_in_syntax_cmd(xp, arg);
1556 break;
1557#endif
1558#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001559 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001560 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001561 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001562 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001563 case CMD_if:
1564 case CMD_elseif:
1565 case CMD_while:
1566 case CMD_for:
1567 case CMD_echo:
1568 case CMD_echon:
1569 case CMD_execute:
1570 case CMD_echomsg:
1571 case CMD_echoerr:
1572 case CMD_call:
1573 case CMD_return:
1574 case CMD_cexpr:
1575 case CMD_caddexpr:
1576 case CMD_cgetexpr:
1577 case CMD_lexpr:
1578 case CMD_laddexpr:
1579 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001580 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001581 break;
1582
1583 case CMD_unlet:
1584 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1585 arg = xp->xp_pattern + 1;
1586
1587 xp->xp_context = EXPAND_USER_VARS;
1588 xp->xp_pattern = arg;
1589
1590 if (*xp->xp_pattern == '$')
1591 {
1592 xp->xp_context = EXPAND_ENV_VARS;
1593 ++xp->xp_pattern;
1594 }
1595
1596 break;
1597
1598 case CMD_function:
1599 case CMD_delfunction:
1600 xp->xp_context = EXPAND_USER_FUNC;
1601 xp->xp_pattern = arg;
1602 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001603 case CMD_disassemble:
1604 set_context_in_disassemble_cmd(xp, arg);
1605 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001606
1607 case CMD_echohl:
1608 set_context_in_echohl_cmd(xp, arg);
1609 break;
1610#endif
1611 case CMD_highlight:
1612 set_context_in_highlight_cmd(xp, arg);
1613 break;
1614#ifdef FEAT_CSCOPE
1615 case CMD_cscope:
1616 case CMD_lcscope:
1617 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001618 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001619 break;
1620#endif
1621#ifdef FEAT_SIGNS
1622 case CMD_sign:
1623 set_context_in_sign_cmd(xp, arg);
1624 break;
1625#endif
1626 case CMD_bdelete:
1627 case CMD_bwipeout:
1628 case CMD_bunload:
1629 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1630 arg = xp->xp_pattern + 1;
1631 // FALLTHROUGH
1632 case CMD_buffer:
1633 case CMD_sbuffer:
1634 case CMD_checktime:
1635 xp->xp_context = EXPAND_BUFFERS;
1636 xp->xp_pattern = arg;
1637 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001638#ifdef FEAT_DIFF
1639 case CMD_diffget:
1640 case CMD_diffput:
1641 // If current buffer is in diff mode, complete buffer names
1642 // which are in diff mode, and different than current buffer.
1643 xp->xp_context = EXPAND_DIFF_BUFFERS;
1644 xp->xp_pattern = arg;
1645 break;
1646#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001647 case CMD_USER:
1648 case CMD_USER_BUF:
1649 if (compl != EXPAND_NOTHING)
1650 {
1651 // EX_XFILE: file names are handled above
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001652 if (!(argt & EX_XFILE))
Bram Moolenaard0190392019-08-23 21:17:35 +02001653 {
1654#ifdef FEAT_MENU
1655 if (compl == EXPAND_MENUS)
1656 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1657#endif
1658 if (compl == EXPAND_COMMANDS)
1659 return arg;
1660 if (compl == EXPAND_MAPPINGS)
1661 return set_context_in_map_cmd(xp, (char_u *)"map",
1662 arg, forceit, FALSE, FALSE, CMD_map);
1663 // Find start of last argument.
1664 p = arg;
1665 while (*p)
1666 {
1667 if (*p == ' ')
1668 // argument starts after a space
1669 arg = p + 1;
1670 else if (*p == '\\' && *(p + 1) != NUL)
1671 ++p; // skip over escaped character
1672 MB_PTR_ADV(p);
1673 }
1674 xp->xp_pattern = arg;
1675 }
1676 xp->xp_context = compl;
1677 }
1678 break;
1679
1680 case CMD_map: case CMD_noremap:
1681 case CMD_nmap: case CMD_nnoremap:
1682 case CMD_vmap: case CMD_vnoremap:
1683 case CMD_omap: case CMD_onoremap:
1684 case CMD_imap: case CMD_inoremap:
1685 case CMD_cmap: case CMD_cnoremap:
1686 case CMD_lmap: case CMD_lnoremap:
1687 case CMD_smap: case CMD_snoremap:
1688 case CMD_tmap: case CMD_tnoremap:
1689 case CMD_xmap: case CMD_xnoremap:
1690 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001691 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001692 case CMD_unmap:
1693 case CMD_nunmap:
1694 case CMD_vunmap:
1695 case CMD_ounmap:
1696 case CMD_iunmap:
1697 case CMD_cunmap:
1698 case CMD_lunmap:
1699 case CMD_sunmap:
1700 case CMD_tunmap:
1701 case CMD_xunmap:
1702 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001703 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 case CMD_mapclear:
1705 case CMD_nmapclear:
1706 case CMD_vmapclear:
1707 case CMD_omapclear:
1708 case CMD_imapclear:
1709 case CMD_cmapclear:
1710 case CMD_lmapclear:
1711 case CMD_smapclear:
1712 case CMD_tmapclear:
1713 case CMD_xmapclear:
1714 xp->xp_context = EXPAND_MAPCLEAR;
1715 xp->xp_pattern = arg;
1716 break;
1717
1718 case CMD_abbreviate: case CMD_noreabbrev:
1719 case CMD_cabbrev: case CMD_cnoreabbrev:
1720 case CMD_iabbrev: case CMD_inoreabbrev:
1721 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001722 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001723 case CMD_unabbreviate:
1724 case CMD_cunabbrev:
1725 case CMD_iunabbrev:
1726 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001727 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001728#ifdef FEAT_MENU
1729 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1730 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1731 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1732 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1733 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1734 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1735 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1736 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1737 case CMD_tmenu: case CMD_tunmenu:
1738 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1739 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1740#endif
1741
1742 case CMD_colorscheme:
1743 xp->xp_context = EXPAND_COLORS;
1744 xp->xp_pattern = arg;
1745 break;
1746
1747 case CMD_compiler:
1748 xp->xp_context = EXPAND_COMPILER;
1749 xp->xp_pattern = arg;
1750 break;
1751
1752 case CMD_ownsyntax:
1753 xp->xp_context = EXPAND_OWNSYNTAX;
1754 xp->xp_pattern = arg;
1755 break;
1756
1757 case CMD_setfiletype:
1758 xp->xp_context = EXPAND_FILETYPE;
1759 xp->xp_pattern = arg;
1760 break;
1761
1762 case CMD_packadd:
1763 xp->xp_context = EXPAND_PACKADD;
1764 xp->xp_pattern = arg;
1765 break;
1766
1767#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1768 case CMD_language:
1769 p = skiptowhite(arg);
1770 if (*p == NUL)
1771 {
1772 xp->xp_context = EXPAND_LANGUAGE;
1773 xp->xp_pattern = arg;
1774 }
1775 else
1776 {
1777 if ( STRNCMP(arg, "messages", p - arg) == 0
1778 || STRNCMP(arg, "ctype", p - arg) == 0
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001779 || STRNCMP(arg, "time", p - arg) == 0
1780 || STRNCMP(arg, "collate", p - arg) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001781 {
1782 xp->xp_context = EXPAND_LOCALES;
1783 xp->xp_pattern = skipwhite(p);
1784 }
1785 else
1786 xp->xp_context = EXPAND_NOTHING;
1787 }
1788 break;
1789#endif
1790#if defined(FEAT_PROFILE)
1791 case CMD_profile:
1792 set_context_in_profile_cmd(xp, arg);
1793 break;
1794#endif
1795 case CMD_behave:
1796 xp->xp_context = EXPAND_BEHAVE;
1797 xp->xp_pattern = arg;
1798 break;
1799
1800 case CMD_messages:
1801 xp->xp_context = EXPAND_MESSAGES;
1802 xp->xp_pattern = arg;
1803 break;
1804
1805 case CMD_history:
1806 xp->xp_context = EXPAND_HISTORY;
1807 xp->xp_pattern = arg;
1808 break;
1809#if defined(FEAT_PROFILE)
1810 case CMD_syntime:
1811 xp->xp_context = EXPAND_SYNTIME;
1812 xp->xp_pattern = arg;
1813 break;
1814#endif
1815
1816 case CMD_argdelete:
1817 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1818 arg = xp->xp_pattern + 1;
1819 xp->xp_context = EXPAND_ARGLIST;
1820 xp->xp_pattern = arg;
1821 break;
1822
1823 default:
1824 break;
1825 }
1826 return NULL;
1827}
1828
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001829/*
1830 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
1831 * we don't need/want deleted. Maybe this could be done better if we didn't
1832 * repeat all this stuff. The only problem is that they may not stay
1833 * perfectly compatible with each other, but then the command line syntax
1834 * probably won't change that much -- webb.
1835 */
1836 static char_u *
1837set_one_cmd_context(
1838 expand_T *xp,
1839 char_u *buff) // buffer for command string
1840{
1841 char_u *p;
1842 char_u *cmd, *arg;
1843 int len = 0;
1844 exarg_T ea;
1845 int compl = EXPAND_NOTHING;
1846 int forceit = FALSE;
1847 int usefilter = FALSE; // filter instead of file name
1848
1849 ExpandInit(xp);
1850 xp->xp_pattern = buff;
1851 xp->xp_line = buff;
1852 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
1853 ea.argt = 0;
1854
1855 // 1. skip comment lines and leading space, colons or bars
1856 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
1857 ;
1858 xp->xp_pattern = cmd;
1859
1860 if (*cmd == NUL)
1861 return NULL;
1862 if (*cmd == '"') // ignore comment lines
1863 {
1864 xp->xp_context = EXPAND_NOTHING;
1865 return NULL;
1866 }
1867
1868 // 3. Skip over the range to find the command.
1869 cmd = skip_range(cmd, TRUE, &xp->xp_context);
1870 xp->xp_pattern = cmd;
1871 if (*cmd == NUL)
1872 return NULL;
1873 if (*cmd == '"')
1874 {
1875 xp->xp_context = EXPAND_NOTHING;
1876 return NULL;
1877 }
1878
1879 if (*cmd == '|' || *cmd == '\n')
1880 return cmd + 1; // There's another command
1881
1882 // Get the command index.
1883 p = set_cmd_index(cmd, &ea, xp, &compl);
1884 if (p == NULL)
1885 return NULL;
1886
1887 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
1888
1889 if (*p == '!') // forced commands
1890 {
1891 forceit = TRUE;
1892 ++p;
1893 }
1894
1895 // 6. parse arguments
1896 if (!IS_USER_CMDIDX(ea.cmdidx))
1897 ea.argt = excmd_get_argt(ea.cmdidx);
1898
1899 arg = skipwhite(p);
1900
1901 // Skip over ++argopt argument
1902 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
1903 {
1904 p = arg;
1905 while (*p && !vim_isspace(*p))
1906 MB_PTR_ADV(p);
1907 arg = skipwhite(p);
1908 }
1909
1910 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
1911 {
1912 if (*arg == '>') // append
1913 {
1914 if (*++arg == '>')
1915 ++arg;
1916 arg = skipwhite(arg);
1917 }
1918 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
1919 {
1920 ++arg;
1921 usefilter = TRUE;
1922 }
1923 }
1924
1925 if (ea.cmdidx == CMD_read)
1926 {
1927 usefilter = forceit; // :r! filter if forced
1928 if (*arg == '!') // :r !filter
1929 {
1930 ++arg;
1931 usefilter = TRUE;
1932 }
1933 }
1934
1935 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
1936 {
1937 while (*arg == *cmd) // allow any number of '>' or '<'
1938 ++arg;
1939 arg = skipwhite(arg);
1940 }
1941
1942 // Does command allow "+command"?
1943 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
1944 {
1945 // Check if we're in the +command
1946 p = arg + 1;
1947 arg = skip_cmd_arg(arg, FALSE);
1948
1949 // Still touching the command after '+'?
1950 if (*arg == NUL)
1951 return p;
1952
1953 // Skip space(s) after +command to get to the real argument
1954 arg = skipwhite(arg);
1955 }
1956
1957
1958 // Check for '|' to separate commands and '"' to start comments.
1959 // Don't do this for ":read !cmd" and ":write !cmd".
1960 if ((ea.argt & EX_TRLBAR) && !usefilter)
1961 {
1962 p = arg;
1963 // ":redir @" is not the start of a comment
1964 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
1965 p += 2;
1966 while (*p)
1967 {
1968 if (*p == Ctrl_V)
1969 {
1970 if (p[1] != NUL)
1971 ++p;
1972 }
1973 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
1974 || *p == '|' || *p == '\n')
1975 {
1976 if (*(p - 1) != '\\')
1977 {
1978 if (*p == '|' || *p == '\n')
1979 return p + 1;
1980 return NULL; // It's a comment
1981 }
1982 }
1983 MB_PTR_ADV(p);
1984 }
1985 }
1986
1987 if (!(ea.argt & EX_EXTRA) && *arg != NUL
1988 && vim_strchr((char_u *)"|\"", *arg) == NULL)
1989 // no arguments allowed but there is something
1990 return NULL;
1991
1992 // Find start of last argument (argument just before cursor):
1993 p = buff;
1994 xp->xp_pattern = p;
1995 len = (int)STRLEN(buff);
1996 while (*p && p < buff + len)
1997 {
1998 if (*p == ' ' || *p == TAB)
1999 {
2000 // argument starts after a space
2001 xp->xp_pattern = ++p;
2002 }
2003 else
2004 {
2005 if (*p == '\\' && *(p + 1) != NUL)
2006 ++p; // skip over escaped character
2007 MB_PTR_ADV(p);
2008 }
2009 }
2010
2011 if (ea.argt & EX_XFILE)
2012 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2013
2014 // 6. Switch on command name.
2015 return set_context_by_cmdname(cmd, ea.cmdidx, arg, ea.argt, compl, xp,
2016 forceit);
2017}
2018
Bram Moolenaar66b51422019-08-18 21:44:12 +02002019 void
2020set_cmd_context(
2021 expand_T *xp,
2022 char_u *str, // start of command line
2023 int len, // length of command line (excl. NUL)
2024 int col, // position of cursor
2025 int use_ccline UNUSED) // use ccline for info
2026{
2027#ifdef FEAT_EVAL
2028 cmdline_info_T *ccline = get_cmdline_info();
2029#endif
2030 int old_char = NUL;
2031 char_u *nextcomm;
2032
2033 // Avoid a UMR warning from Purify, only save the character if it has been
2034 // written before.
2035 if (col < len)
2036 old_char = str[col];
2037 str[col] = NUL;
2038 nextcomm = str;
2039
2040#ifdef FEAT_EVAL
2041 if (use_ccline && ccline->cmdfirstc == '=')
2042 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002043 // pass CMD_SIZE because there is no real command
2044 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002045 }
2046 else if (use_ccline && ccline->input_fn)
2047 {
2048 xp->xp_context = ccline->xp_context;
2049 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002050 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002051 }
2052 else
2053#endif
2054 while (nextcomm != NULL)
2055 nextcomm = set_one_cmd_context(xp, nextcomm);
2056
2057 // Store the string here so that call_user_expand_func() can get to them
2058 // easily.
2059 xp->xp_line = str;
2060 xp->xp_col = col;
2061
2062 str[col] = old_char;
2063}
2064
2065/*
2066 * Expand the command line "str" from context "xp".
2067 * "xp" must have been set by set_cmd_context().
2068 * xp->xp_pattern points into "str", to where the text that is to be expanded
2069 * starts.
2070 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2071 * cursor.
2072 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2073 * key that triggered expansion literally.
2074 * Returns EXPAND_OK otherwise.
2075 */
2076 int
2077expand_cmdline(
2078 expand_T *xp,
2079 char_u *str, // start of command line
2080 int col, // position of cursor
2081 int *matchcount, // return: nr of matches
2082 char_u ***matches) // return: array of pointers to matches
2083{
2084 char_u *file_str = NULL;
2085 int options = WILD_ADD_SLASH|WILD_SILENT;
2086
2087 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2088 {
2089 beep_flush();
2090 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2091 }
2092 if (xp->xp_context == EXPAND_NOTHING)
2093 {
2094 // Caller can use the character as a normal char instead
2095 return EXPAND_NOTHING;
2096 }
2097
2098 // add star to file name, or convert to regexp if not exp. files.
2099 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
2100 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2101 if (file_str == NULL)
2102 return EXPAND_UNSUCCESSFUL;
2103
2104 if (p_wic)
2105 options += WILD_ICASE;
2106
2107 // find all files that match the description
2108 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
2109 {
2110 *matchcount = 0;
2111 *matches = NULL;
2112 }
2113 vim_free(file_str);
2114
2115 return EXPAND_OK;
2116}
2117
Bram Moolenaar66b51422019-08-18 21:44:12 +02002118/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002119 * Expand file or directory names.
2120 */
2121 static int
2122expand_files_and_dirs(
2123 expand_T *xp,
2124 char_u *pat,
2125 char_u ***file,
2126 int *num_file,
2127 int flags,
2128 int options)
2129{
2130 int free_pat = FALSE;
2131 int i;
2132 int ret;
2133
2134 // for ":set path=" and ":set tags=" halve backslashes for escaped
2135 // space
2136 if (xp->xp_backslash != XP_BS_NONE)
2137 {
2138 free_pat = TRUE;
2139 pat = vim_strsave(pat);
2140 for (i = 0; pat[i]; ++i)
2141 if (pat[i] == '\\')
2142 {
2143 if (xp->xp_backslash == XP_BS_THREE
2144 && pat[i + 1] == '\\'
2145 && pat[i + 2] == '\\'
2146 && pat[i + 3] == ' ')
2147 STRMOVE(pat + i, pat + i + 3);
2148 if (xp->xp_backslash == XP_BS_ONE
2149 && pat[i + 1] == ' ')
2150 STRMOVE(pat + i, pat + i + 1);
2151 }
2152 }
2153
2154 if (xp->xp_context == EXPAND_FILES)
2155 flags |= EW_FILE;
2156 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2157 flags |= (EW_FILE | EW_PATH);
2158 else
2159 flags = (flags | EW_DIR) & ~EW_FILE;
2160 if (options & WILD_ICASE)
2161 flags |= EW_ICASE;
2162
2163 // Expand wildcards, supporting %:h and the like.
2164 ret = expand_wildcards_eval(&pat, num_file, file, flags);
2165 if (free_pat)
2166 vim_free(pat);
2167#ifdef BACKSLASH_IN_FILENAME
2168 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2169 {
2170 int j;
2171
2172 for (j = 0; j < *num_file; ++j)
2173 {
2174 char_u *ptr = (*file)[j];
2175
2176 while (*ptr != NUL)
2177 {
2178 if (p_csl[0] == 's' && *ptr == '\\')
2179 *ptr = '/';
2180 else if (p_csl[0] == 'b' && *ptr == '/')
2181 *ptr = '\\';
2182 ptr += (*mb_ptr2len)(ptr);
2183 }
2184 }
2185 }
2186#endif
2187 return ret;
2188}
2189
2190/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002191 * Function given to ExpandGeneric() to obtain the possible arguments of the
2192 * ":behave {mswin,xterm}" command.
2193 */
2194 static char_u *
2195get_behave_arg(expand_T *xp UNUSED, int idx)
2196{
2197 if (idx == 0)
2198 return (char_u *)"mswin";
2199 if (idx == 1)
2200 return (char_u *)"xterm";
2201 return NULL;
2202}
2203
2204/*
2205 * Function given to ExpandGeneric() to obtain the possible arguments of the
2206 * ":messages {clear}" command.
2207 */
2208 static char_u *
2209get_messages_arg(expand_T *xp UNUSED, int idx)
2210{
2211 if (idx == 0)
2212 return (char_u *)"clear";
2213 return NULL;
2214}
2215
2216 static char_u *
2217get_mapclear_arg(expand_T *xp UNUSED, int idx)
2218{
2219 if (idx == 0)
2220 return (char_u *)"<buffer>";
2221 return NULL;
2222}
2223
2224/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002225 * Do the expansion based on xp->xp_context and 'rmp'.
2226 */
2227 static int
2228ExpandOther(
2229 expand_T *xp,
2230 regmatch_T *rmp,
2231 int *num_file,
2232 char_u ***file)
2233{
2234 static struct expgen
2235 {
2236 int context;
2237 char_u *((*func)(expand_T *, int));
2238 int ic;
2239 int escaped;
2240 } tab[] =
2241 {
2242 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2243 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2244 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2245 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2246 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2247 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2248 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2249 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2250 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2251 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2252# ifdef FEAT_EVAL
2253 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2254 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2255 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2256 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2257 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2258# endif
2259# ifdef FEAT_MENU
2260 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2261 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2262# endif
2263# ifdef FEAT_SYN_HL
2264 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2265# endif
2266# ifdef FEAT_PROFILE
2267 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2268# endif
2269 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2270 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2271 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
2272# ifdef FEAT_CSCOPE
2273 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2274# endif
2275# ifdef FEAT_SIGNS
2276 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2277# endif
2278# ifdef FEAT_PROFILE
2279 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2280# endif
2281# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2282 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2283 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2284# endif
2285 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2286 {EXPAND_USER, get_users, TRUE, FALSE},
2287 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2288 };
2289 int i;
2290 int ret = FAIL;
2291
2292 // Find a context in the table and call the ExpandGeneric() with the
2293 // right function to do the expansion.
2294 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2295 {
2296 if (xp->xp_context == tab[i].context)
2297 {
2298 if (tab[i].ic)
2299 rmp->rm_ic = TRUE;
2300 ret = ExpandGeneric(xp, rmp, num_file, file,
2301 tab[i].func, tab[i].escaped);
2302 break;
2303 }
2304 }
2305
2306 return ret;
2307}
2308
2309/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002310 * Do the expansion based on xp->xp_context and "pat".
2311 */
2312 static int
2313ExpandFromContext(
2314 expand_T *xp,
2315 char_u *pat,
2316 int *num_file,
2317 char_u ***file,
2318 int options) // WILD_ flags
2319{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002320 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002321 int ret;
2322 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002323 char_u *tofree = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002324
2325 flags = EW_DIR; // include directories
2326 if (options & WILD_LIST_NOTFOUND)
2327 flags |= EW_NOTFOUND;
2328 if (options & WILD_ADD_SLASH)
2329 flags |= EW_ADDSLASH;
2330 if (options & WILD_KEEP_ALL)
2331 flags |= EW_KEEPALL;
2332 if (options & WILD_SILENT)
2333 flags |= EW_SILENT;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02002334 if (options & WILD_NOERROR)
2335 flags |= EW_NOERROR;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002336 if (options & WILD_ALLLINKS)
2337 flags |= EW_ALLLINKS;
2338
2339 if (xp->xp_context == EXPAND_FILES
2340 || xp->xp_context == EXPAND_DIRECTORIES
2341 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002342 return expand_files_and_dirs(xp, pat, file, num_file, flags, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002343
2344 *file = (char_u **)"";
2345 *num_file = 0;
2346 if (xp->xp_context == EXPAND_HELP)
2347 {
2348 // With an empty argument we would get all the help tags, which is
2349 // very slow. Get matches for "help" instead.
2350 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
2351 num_file, file, FALSE) == OK)
2352 {
2353#ifdef FEAT_MULTI_LANG
2354 cleanup_help_tags(*num_file, *file);
2355#endif
2356 return OK;
2357 }
2358 return FAIL;
2359 }
2360
Bram Moolenaar66b51422019-08-18 21:44:12 +02002361 if (xp->xp_context == EXPAND_SHELLCMD)
2362 return expand_shellcmd(pat, num_file, file, flags);
2363 if (xp->xp_context == EXPAND_OLD_SETTING)
2364 return ExpandOldSetting(num_file, file);
2365 if (xp->xp_context == EXPAND_BUFFERS)
2366 return ExpandBufnames(pat, num_file, file, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002367#ifdef FEAT_DIFF
2368 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
2369 return ExpandBufnames(pat, num_file, file, options | BUF_DIFF_FILTER);
2370#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002371 if (xp->xp_context == EXPAND_TAGS
2372 || xp->xp_context == EXPAND_TAGS_LISTFILES)
2373 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
2374 if (xp->xp_context == EXPAND_COLORS)
2375 {
2376 char *directories[] = {"colors", NULL};
2377 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
2378 directories);
2379 }
2380 if (xp->xp_context == EXPAND_COMPILER)
2381 {
2382 char *directories[] = {"compiler", NULL};
2383 return ExpandRTDir(pat, 0, num_file, file, directories);
2384 }
2385 if (xp->xp_context == EXPAND_OWNSYNTAX)
2386 {
2387 char *directories[] = {"syntax", NULL};
2388 return ExpandRTDir(pat, 0, num_file, file, directories);
2389 }
2390 if (xp->xp_context == EXPAND_FILETYPE)
2391 {
2392 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
2393 return ExpandRTDir(pat, 0, num_file, file, directories);
2394 }
2395# if defined(FEAT_EVAL)
2396 if (xp->xp_context == EXPAND_USER_LIST)
2397 return ExpandUserList(xp, num_file, file);
2398# endif
2399 if (xp->xp_context == EXPAND_PACKADD)
2400 return ExpandPackAddDir(pat, num_file, file);
2401
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002402 // When expanding a function name starting with s:, match the <SNR>nr_
2403 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002404 if ((xp->xp_context == EXPAND_USER_FUNC
2405 || xp->xp_context == EXPAND_DISASSEMBLE)
2406 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002407 {
2408 int len = (int)STRLEN(pat) + 20;
2409
2410 tofree = alloc(len);
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002411 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002412 pat = tofree;
2413 }
2414
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002415 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002416 if (regmatch.regprog == NULL)
2417 return FAIL;
2418
2419 // set ignore-case according to p_ic, p_scs and pat
2420 regmatch.rm_ic = ignorecase(pat);
2421
2422 if (xp->xp_context == EXPAND_SETTINGS
2423 || xp->xp_context == EXPAND_BOOL_SETTINGS)
2424 ret = ExpandSettings(xp, &regmatch, num_file, file);
2425 else if (xp->xp_context == EXPAND_MAPPINGS)
2426 ret = ExpandMappings(&regmatch, num_file, file);
2427# if defined(FEAT_EVAL)
2428 else if (xp->xp_context == EXPAND_USER_DEFINED)
2429 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
2430# endif
2431 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002432 ret = ExpandOther(xp, &regmatch, num_file, file);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002433
2434 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002435 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002436
2437 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002438}
2439
Bram Moolenaar66b51422019-08-18 21:44:12 +02002440/*
2441 * Expand a list of names.
2442 *
2443 * Generic function for command line completion. It calls a function to
2444 * obtain strings, one by one. The strings are matched against a regexp
2445 * program. Matching strings are copied into an array, which is returned.
2446 *
2447 * Returns OK when no problems encountered, FAIL for error (out of memory).
2448 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002449 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002450ExpandGeneric(
2451 expand_T *xp,
2452 regmatch_T *regmatch,
2453 int *num_file,
2454 char_u ***file,
2455 char_u *((*func)(expand_T *, int)),
2456 // returns a string from the list
2457 int escaped)
2458{
2459 int i;
2460 int count = 0;
2461 int round;
2462 char_u *str;
2463
2464 // do this loop twice:
2465 // round == 0: count the number of matching names
2466 // round == 1: copy the matching names into allocated memory
2467 for (round = 0; round <= 1; ++round)
2468 {
2469 for (i = 0; ; ++i)
2470 {
2471 str = (*func)(xp, i);
2472 if (str == NULL) // end of list
2473 break;
2474 if (*str == NUL) // skip empty strings
2475 continue;
2476
2477 if (vim_regexec(regmatch, str, (colnr_T)0))
2478 {
2479 if (round)
2480 {
2481 if (escaped)
2482 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2483 else
2484 str = vim_strsave(str);
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002485 if (str == NULL)
2486 {
2487 FreeWild(count, *file);
2488 *num_file = 0;
2489 *file = NULL;
2490 return FAIL;
2491 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002492 (*file)[count] = str;
2493# ifdef FEAT_MENU
2494 if (func == get_menu_names && str != NULL)
2495 {
2496 // test for separator added by get_menu_names()
2497 str += STRLEN(str) - 1;
2498 if (*str == '\001')
2499 *str = '.';
2500 }
2501# endif
2502 }
2503 ++count;
2504 }
2505 }
2506 if (round == 0)
2507 {
2508 if (count == 0)
2509 return OK;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002510 *file = ALLOC_MULT(char_u *, count);
2511 if (*file == NULL)
2512 {
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002513 *num_file = 0;
2514 *file = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002515 return FAIL;
2516 }
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002517 *num_file = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002518 count = 0;
2519 }
2520 }
2521
2522 // Sort the results. Keep menu's in the specified order.
2523 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2524 {
2525 if (xp->xp_context == EXPAND_EXPRESSION
2526 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002527 || xp->xp_context == EXPAND_USER_FUNC
2528 || xp->xp_context == EXPAND_DISASSEMBLE)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002529 // <SNR> functions should be sorted to the end.
2530 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
2531 sort_func_compare);
2532 else
2533 sort_strings(*file, *num_file);
2534 }
2535
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002536#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002537 // Reset the variables used for special highlight names expansion, so that
2538 // they don't show up when getting normal highlight names by ID.
2539 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002540#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002541 return OK;
2542}
2543
2544/*
2545 * Complete a shell command.
2546 * Returns FAIL or OK;
2547 */
2548 static int
2549expand_shellcmd(
2550 char_u *filepat, // pattern to match with command names
2551 int *num_file, // return: number of matches
2552 char_u ***file, // return: array with matches
2553 int flagsarg) // EW_ flags
2554{
2555 char_u *pat;
2556 int i;
2557 char_u *path = NULL;
2558 int mustfree = FALSE;
2559 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002560 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002561 size_t l;
2562 char_u *s, *e;
2563 int flags = flagsarg;
2564 int ret;
2565 int did_curdir = FALSE;
2566 hashtab_T found_ht;
2567 hashitem_T *hi;
2568 hash_T hash;
2569
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002570 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002571 if (buf == NULL)
2572 return FAIL;
2573
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002574 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002575 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002576 if (pat == NULL)
2577 {
2578 vim_free(buf);
2579 return FAIL;
2580 }
2581
Bram Moolenaar66b51422019-08-18 21:44:12 +02002582 for (i = 0; pat[i]; ++i)
2583 if (pat[i] == '\\' && pat[i + 1] == ' ')
2584 STRMOVE(pat + i, pat + i + 1);
2585
2586 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2587
2588 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2589 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2590 path = (char_u *)".";
2591 else
2592 {
2593 // For an absolute name we don't use $PATH.
2594 if (!mch_isFullName(pat))
2595 path = vim_getenv((char_u *)"PATH", &mustfree);
2596 if (path == NULL)
2597 path = (char_u *)"";
2598 }
2599
2600 // Go over all directories in $PATH. Expand matches in that directory and
2601 // collect them in "ga". When "." is not in $PATH also expand for the
2602 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002603 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002604 hash_init(&found_ht);
2605 for (s = path; ; s = e)
2606 {
2607# if defined(MSWIN)
2608 e = vim_strchr(s, ';');
2609# else
2610 e = vim_strchr(s, ':');
2611# endif
2612 if (e == NULL)
2613 e = s + STRLEN(s);
2614
2615 if (*s == NUL)
2616 {
2617 if (did_curdir)
2618 break;
2619 // Find directories in the current directory, path is empty.
2620 did_curdir = TRUE;
2621 flags |= EW_DIR;
2622 }
2623 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2624 {
2625 did_curdir = TRUE;
2626 flags |= EW_DIR;
2627 }
2628 else
2629 // Do not match directories inside a $PATH item.
2630 flags &= ~EW_DIR;
2631
2632 l = e - s;
2633 if (l > MAXPATHL - 5)
2634 break;
2635 vim_strncpy(buf, s, l);
2636 add_pathsep(buf);
2637 l = STRLEN(buf);
2638 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2639
2640 // Expand matches in one directory of $PATH.
2641 ret = expand_wildcards(1, &buf, num_file, file, flags);
2642 if (ret == OK)
2643 {
2644 if (ga_grow(&ga, *num_file) == FAIL)
2645 FreeWild(*num_file, *file);
2646 else
2647 {
2648 for (i = 0; i < *num_file; ++i)
2649 {
2650 char_u *name = (*file)[i];
2651
2652 if (STRLEN(name) > l)
2653 {
2654 // Check if this name was already found.
2655 hash = hash_hash(name + l);
2656 hi = hash_lookup(&found_ht, name + l, hash);
2657 if (HASHITEM_EMPTY(hi))
2658 {
2659 // Remove the path that was prepended.
2660 STRMOVE(name, name + l);
2661 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
2662 hash_add_item(&found_ht, hi, name, hash);
2663 name = NULL;
2664 }
2665 }
2666 vim_free(name);
2667 }
2668 vim_free(*file);
2669 }
2670 }
2671 if (*e != NUL)
2672 ++e;
2673 }
2674 *file = ga.ga_data;
2675 *num_file = ga.ga_len;
2676
2677 vim_free(buf);
2678 vim_free(pat);
2679 if (mustfree)
2680 vim_free(path);
2681 hash_clear(&found_ht);
2682 return OK;
2683}
2684
2685# if defined(FEAT_EVAL)
2686/*
2687 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002688 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002689 */
2690 static void *
2691call_user_expand_func(
2692 void *(*user_expand_func)(char_u *, int, typval_T *),
2693 expand_T *xp,
2694 int *num_file,
2695 char_u ***file)
2696{
2697 cmdline_info_T *ccline = get_cmdline_info();
2698 int keep = 0;
2699 typval_T args[4];
2700 sctx_T save_current_sctx = current_sctx;
2701 char_u *pat = NULL;
2702 void *ret;
2703
2704 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2705 return NULL;
2706 *num_file = 0;
2707 *file = NULL;
2708
2709 if (ccline->cmdbuff != NULL)
2710 {
2711 keep = ccline->cmdbuff[ccline->cmdlen];
2712 ccline->cmdbuff[ccline->cmdlen] = 0;
2713 }
2714
2715 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2716
2717 args[0].v_type = VAR_STRING;
2718 args[0].vval.v_string = pat;
2719 args[1].v_type = VAR_STRING;
2720 args[1].vval.v_string = xp->xp_line;
2721 args[2].v_type = VAR_NUMBER;
2722 args[2].vval.v_number = xp->xp_col;
2723 args[3].v_type = VAR_UNKNOWN;
2724
2725 current_sctx = xp->xp_script_ctx;
2726
2727 ret = user_expand_func(xp->xp_arg, 3, args);
2728
2729 current_sctx = save_current_sctx;
2730 if (ccline->cmdbuff != NULL)
2731 ccline->cmdbuff[ccline->cmdlen] = keep;
2732
2733 vim_free(pat);
2734 return ret;
2735}
2736
2737/*
2738 * Expand names with a function defined by the user.
2739 */
2740 static int
2741ExpandUserDefined(
2742 expand_T *xp,
2743 regmatch_T *regmatch,
2744 int *num_file,
2745 char_u ***file)
2746{
2747 char_u *retstr;
2748 char_u *s;
2749 char_u *e;
2750 int keep;
2751 garray_T ga;
2752 int skip;
2753
2754 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
2755 if (retstr == NULL)
2756 return FAIL;
2757
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002758 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002759 for (s = retstr; *s != NUL; s = e)
2760 {
2761 e = vim_strchr(s, '\n');
2762 if (e == NULL)
2763 e = s + STRLEN(s);
2764 keep = *e;
2765 *e = NUL;
2766
2767 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2768 *e = keep;
2769
2770 if (!skip)
2771 {
2772 if (ga_grow(&ga, 1) == FAIL)
2773 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02002774 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002775 ++ga.ga_len;
2776 }
2777
2778 if (*e != NUL)
2779 ++e;
2780 }
2781 vim_free(retstr);
2782 *file = ga.ga_data;
2783 *num_file = ga.ga_len;
2784 return OK;
2785}
2786
2787/*
2788 * Expand names with a list returned by a function defined by the user.
2789 */
2790 static int
2791ExpandUserList(
2792 expand_T *xp,
2793 int *num_file,
2794 char_u ***file)
2795{
2796 list_T *retlist;
2797 listitem_T *li;
2798 garray_T ga;
2799
2800 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
2801 if (retlist == NULL)
2802 return FAIL;
2803
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002804 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002805 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002806 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002807 {
2808 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
2809 continue; // Skip non-string items and empty strings
2810
2811 if (ga_grow(&ga, 1) == FAIL)
2812 break;
2813
2814 ((char_u **)ga.ga_data)[ga.ga_len] =
2815 vim_strsave(li->li_tv.vval.v_string);
2816 ++ga.ga_len;
2817 }
2818 list_unref(retlist);
2819
2820 *file = ga.ga_data;
2821 *num_file = ga.ga_len;
2822 return OK;
2823}
2824# endif
2825
2826/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002827 * Expand "file" for all comma-separated directories in "path".
2828 * Adds the matches to "ga". Caller must init "ga".
2829 */
2830 void
2831globpath(
2832 char_u *path,
2833 char_u *file,
2834 garray_T *ga,
2835 int expand_options)
2836{
2837 expand_T xpc;
2838 char_u *buf;
2839 int i;
2840 int num_p;
2841 char_u **p;
2842
2843 buf = alloc(MAXPATHL);
2844 if (buf == NULL)
2845 return;
2846
2847 ExpandInit(&xpc);
2848 xpc.xp_context = EXPAND_FILES;
2849
2850 // Loop over all entries in {path}.
2851 while (*path != NUL)
2852 {
2853 // Copy one item of the path to buf[] and concatenate the file name.
2854 copy_option_part(&path, buf, MAXPATHL, ",");
2855 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
2856 {
2857# if defined(MSWIN)
2858 // Using the platform's path separator (\) makes vim incorrectly
2859 // treat it as an escape character, use '/' instead.
2860 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
2861 STRCAT(buf, "/");
2862# else
2863 add_pathsep(buf);
2864# endif
2865 STRCAT(buf, file);
2866 if (ExpandFromContext(&xpc, buf, &num_p, &p,
2867 WILD_SILENT|expand_options) != FAIL && num_p > 0)
2868 {
2869 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
2870
2871 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002872 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02002873 for (i = 0; i < num_p; ++i)
2874 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002875 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02002876 ++ga->ga_len;
2877 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002878 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002879 }
2880 }
2881 }
2882
2883 vim_free(buf);
2884}
Bram Moolenaar66b51422019-08-18 21:44:12 +02002885
Bram Moolenaareadee482020-09-04 15:37:31 +02002886#ifdef FEAT_WILDMENU
2887
2888/*
2889 * Translate some keys pressed when 'wildmenu' is used.
2890 */
2891 int
2892wildmenu_translate_key(
2893 cmdline_info_T *cclp,
2894 int key,
2895 expand_T *xp,
2896 int did_wild_list)
2897{
2898 int c = key;
2899
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002900#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002901 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002902 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002903 // When the popup menu is used for cmdline completion:
2904 // Up : go to the previous item in the menu
2905 // Down : go to the next item in the menu
2906 // Left : go to the parent directory
2907 // Right: list the files in the selected directory
2908 switch (c)
2909 {
2910 case K_UP: c = K_LEFT; break;
2911 case K_DOWN: c = K_RIGHT; break;
2912 case K_LEFT: c = K_UP; break;
2913 case K_RIGHT: c = K_DOWN; break;
2914 default: break;
2915 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002916 }
2917#endif
2918
Bram Moolenaareadee482020-09-04 15:37:31 +02002919 if (did_wild_list && p_wmnu)
2920 {
2921 if (c == K_LEFT)
2922 c = Ctrl_P;
2923 else if (c == K_RIGHT)
2924 c = Ctrl_N;
2925 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002926
Bram Moolenaareadee482020-09-04 15:37:31 +02002927 // Hitting CR after "emenu Name.": complete submenu
2928 if (xp->xp_context == EXPAND_MENUNAMES && p_wmnu
2929 && cclp->cmdpos > 1
2930 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
2931 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
2932 && (c == '\n' || c == '\r' || c == K_KENTER))
2933 c = K_DOWN;
2934
2935 return c;
2936}
2937
2938/*
2939 * Delete characters on the command line, from "from" to the current
2940 * position.
2941 */
2942 static void
2943cmdline_del(cmdline_info_T *cclp, int from)
2944{
2945 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
2946 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
2947 cclp->cmdlen -= cclp->cmdpos - from;
2948 cclp->cmdpos = from;
2949}
2950
2951/*
2952 * Handle a key pressed when wild menu is displayed
2953 */
2954 int
2955wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
2956{
2957 int c = key;
2958 int i;
2959 int j;
2960
2961 if (!p_wmnu)
2962 return c;
2963
2964 // Special translations for 'wildmenu'
2965 if (xp->xp_context == EXPAND_MENUNAMES)
2966 {
2967 // Hitting <Down> after "emenu Name.": complete submenu
2968 if (c == K_DOWN && cclp->cmdpos > 0
2969 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002970 {
Bram Moolenaareadee482020-09-04 15:37:31 +02002971 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002972 KeyTyped = TRUE; // in case the key was mapped
2973 }
Bram Moolenaareadee482020-09-04 15:37:31 +02002974 else if (c == K_UP)
2975 {
2976 // Hitting <Up>: Remove one submenu name in front of the
2977 // cursor
2978 int found = FALSE;
2979
2980 j = (int)(xp->xp_pattern - cclp->cmdbuff);
2981 i = 0;
2982 while (--j > 0)
2983 {
2984 // check for start of menu name
2985 if (cclp->cmdbuff[j] == ' '
2986 && cclp->cmdbuff[j - 1] != '\\')
2987 {
2988 i = j + 1;
2989 break;
2990 }
2991 // check for start of submenu name
2992 if (cclp->cmdbuff[j] == '.'
2993 && cclp->cmdbuff[j - 1] != '\\')
2994 {
2995 if (found)
2996 {
2997 i = j + 1;
2998 break;
2999 }
3000 else
3001 found = TRUE;
3002 }
3003 }
3004 if (i > 0)
3005 cmdline_del(cclp, i);
3006 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003007 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003008 xp->xp_context = EXPAND_NOTHING;
3009 }
3010 }
3011 if ((xp->xp_context == EXPAND_FILES
3012 || xp->xp_context == EXPAND_DIRECTORIES
3013 || xp->xp_context == EXPAND_SHELLCMD))
3014 {
3015 char_u upseg[5];
3016
3017 upseg[0] = PATHSEP;
3018 upseg[1] = '.';
3019 upseg[2] = '.';
3020 upseg[3] = PATHSEP;
3021 upseg[4] = NUL;
3022
3023 if (c == K_DOWN
3024 && cclp->cmdpos > 0
3025 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3026 && (cclp->cmdpos < 3
3027 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3028 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3029 {
3030 // go down a directory
3031 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003032 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003033 }
3034 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
3035 {
3036 // If in a direct ancestor, strip off one ../ to go down
3037 int found = FALSE;
3038
3039 j = cclp->cmdpos;
3040 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3041 while (--j > i)
3042 {
3043 if (has_mbyte)
3044 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3045 if (vim_ispathsep(cclp->cmdbuff[j]))
3046 {
3047 found = TRUE;
3048 break;
3049 }
3050 }
3051 if (found
3052 && cclp->cmdbuff[j - 1] == '.'
3053 && cclp->cmdbuff[j - 2] == '.'
3054 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3055 {
3056 cmdline_del(cclp, j - 2);
3057 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003058 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003059 }
3060 }
3061 else if (c == K_UP)
3062 {
3063 // go up a directory
3064 int found = FALSE;
3065
3066 j = cclp->cmdpos - 1;
3067 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3068 while (--j > i)
3069 {
3070 if (has_mbyte)
3071 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3072 if (vim_ispathsep(cclp->cmdbuff[j])
3073# ifdef BACKSLASH_IN_FILENAME
3074 && vim_strchr((char_u *)" *?[{`$%#",
3075 cclp->cmdbuff[j + 1]) == NULL
3076# endif
3077 )
3078 {
3079 if (found)
3080 {
3081 i = j + 1;
3082 break;
3083 }
3084 else
3085 found = TRUE;
3086 }
3087 }
3088
3089 if (!found)
3090 j = i;
3091 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3092 j += 4;
3093 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3094 && j == i)
3095 j += 3;
3096 else
3097 j = 0;
3098 if (j > 0)
3099 {
3100 // TODO this is only for DOS/UNIX systems - need to put in
3101 // machine-specific stuff here and in upseg init
3102 cmdline_del(cclp, j);
3103 put_on_cmdline(upseg + 1, 3, FALSE);
3104 }
3105 else if (cclp->cmdpos > i)
3106 cmdline_del(cclp, i);
3107
3108 // Now complete in the new directory. Set KeyTyped in case the
3109 // Up key came from a mapping.
3110 c = p_wc;
3111 KeyTyped = TRUE;
3112 }
3113 }
3114
3115 return c;
3116}
3117
3118/*
3119 * Free expanded names when finished walking through the matches
3120 */
3121 void
3122wildmenu_cleanup(cmdline_info_T *cclp)
3123{
3124 int skt = KeyTyped;
3125 int old_RedrawingDisabled = RedrawingDisabled;
3126
3127 if (!p_wmnu || wild_menu_showing == 0)
3128 return;
3129
3130 if (cclp->input_fn)
3131 RedrawingDisabled = 0;
3132
3133 if (wild_menu_showing == WM_SCROLLED)
3134 {
3135 // Entered command line, move it up
3136 cmdline_row--;
3137 redrawcmd();
3138 }
3139 else if (save_p_ls != -1)
3140 {
3141 // restore 'laststatus' and 'winminheight'
3142 p_ls = save_p_ls;
3143 p_wmh = save_p_wmh;
3144 last_status(FALSE);
3145 update_screen(VALID); // redraw the screen NOW
3146 redrawcmd();
3147 save_p_ls = -1;
3148 }
3149 else
3150 {
3151 win_redraw_last_status(topframe);
3152 redraw_statuslines();
3153 }
3154 KeyTyped = skt;
3155 wild_menu_showing = 0;
3156 if (cclp->input_fn)
3157 RedrawingDisabled = old_RedrawingDisabled;
3158}
3159#endif
3160
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003161#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003162/*
3163 * "getcompletion()" function
3164 */
3165 void
3166f_getcompletion(typval_T *argvars, typval_T *rettv)
3167{
3168 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003169 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003170 expand_T xpc;
3171 int filtered = FALSE;
3172 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003173 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003174
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003175 if (in_vim9script()
3176 && (check_for_string_arg(argvars, 0) == FAIL
3177 || check_for_string_arg(argvars, 1) == FAIL
3178 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3179 return;
3180
ii144785fe02021-11-21 12:13:56 +00003181 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003182 if (argvars[1].v_type != VAR_STRING)
3183 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003184 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003185 return;
3186 }
3187 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003188
3189 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003190 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003191
3192 if (p_wic)
3193 options |= WILD_ICASE;
3194
3195 // For filtered results, 'wildignore' is used
3196 if (!filtered)
3197 options |= WILD_KEEP_ALL;
3198
3199 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003200 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003201 {
ii144785fe02021-11-21 12:13:56 +00003202 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003203 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003204 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003205 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003206 else
3207 {
ii144785fe02021-11-21 12:13:56 +00003208 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003209 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3210
3211 xpc.xp_context = cmdcomplete_str_to_type(type);
3212 if (xpc.xp_context == EXPAND_NOTHING)
3213 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003214 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003215 return;
3216 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003217
3218# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003219 if (xpc.xp_context == EXPAND_MENUS)
3220 {
3221 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3222 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3223 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003224# endif
3225# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003226 if (xpc.xp_context == EXPAND_CSCOPE)
3227 {
3228 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3229 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3230 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003231# endif
3232# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003233 if (xpc.xp_context == EXPAND_SIGN)
3234 {
3235 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3236 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3237 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003238# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003239 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003240
3241 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3242 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3243 {
3244 int i;
3245
3246 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3247
3248 for (i = 0; i < xpc.xp_numfiles; i++)
3249 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3250 }
3251 vim_free(pat);
3252 ExpandCleanup(&xpc);
3253}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003254#endif // FEAT_EVAL