blob: c98e126a5c2b0fbe389ea6d000c978812b6dd2d2 [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
Bram Moolenaar66b51422019-08-18 21:44:12 +020030 static int
31sort_func_compare(const void *s1, const void *s2)
32{
33 char_u *p1 = *(char_u **)s1;
34 char_u *p2 = *(char_u **)s2;
35
36 if (*p1 != '<' && *p2 == '<') return -1;
37 if (*p1 == '<' && *p2 != '<') return 1;
38 return STRCMP(p1, p2);
39}
Bram Moolenaar66b51422019-08-18 21:44:12 +020040
41 static void
42ExpandEscape(
43 expand_T *xp,
44 char_u *str,
45 int numfiles,
46 char_u **files,
47 int options)
48{
49 int i;
50 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +010051 int vse_what = xp->xp_context == EXPAND_BUFFERS
52 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +020053
54 // May change home directory back to "~"
55 if (options & WILD_HOME_REPLACE)
56 tilde_replace(str, numfiles, files);
57
58 if (options & WILD_ESCAPE)
59 {
60 if (xp->xp_context == EXPAND_FILES
61 || xp->xp_context == EXPAND_FILES_IN_PATH
62 || xp->xp_context == EXPAND_SHELLCMD
63 || xp->xp_context == EXPAND_BUFFERS
64 || xp->xp_context == EXPAND_DIRECTORIES)
65 {
66 // Insert a backslash into a file name before a space, \, %, #
67 // and wildmatch characters, except '~'.
68 for (i = 0; i < numfiles; ++i)
69 {
70 // for ":set path=" we need to escape spaces twice
71 if (xp->xp_backslash == XP_BS_THREE)
72 {
73 p = vim_strsave_escaped(files[i], (char_u *)" ");
74 if (p != NULL)
75 {
76 vim_free(files[i]);
77 files[i] = p;
78#if defined(BACKSLASH_IN_FILENAME)
79 p = vim_strsave_escaped(files[i], (char_u *)" ");
80 if (p != NULL)
81 {
82 vim_free(files[i]);
83 files[i] = p;
84 }
85#endif
86 }
87 }
88#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +010089 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +020090#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +010091 p = vim_strsave_fnameescape(files[i],
92 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +020093#endif
94 if (p != NULL)
95 {
96 vim_free(files[i]);
97 files[i] = p;
98 }
99
100 // If 'str' starts with "\~", replace "~" at start of
101 // files[i] with "\~".
102 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
103 escape_fname(&files[i]);
104 }
105 xp->xp_backslash = XP_BS_NONE;
106
107 // If the first file starts with a '+' escape it. Otherwise it
108 // could be seen as "+cmd".
109 if (*files[0] == '+')
110 escape_fname(&files[0]);
111 }
112 else if (xp->xp_context == EXPAND_TAGS)
113 {
114 // Insert a backslash before characters in a tag name that
115 // would terminate the ":tag" command.
116 for (i = 0; i < numfiles; ++i)
117 {
118 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
119 if (p != NULL)
120 {
121 vim_free(files[i]);
122 files[i] = p;
123 }
124 }
125 }
126 }
127}
128
129/*
130 * Return FAIL if this is not an appropriate context in which to do
131 * completion of anything, return OK if it is (even if there are no matches).
132 * For the caller, this means that the character is just passed through like a
133 * normal character (instead of being expanded). This allows :s/^I^D etc.
134 */
135 int
136nextwild(
137 expand_T *xp,
138 int type,
139 int options, // extra options for ExpandOne()
140 int escape) // if TRUE, escape the returned matches
141{
142 cmdline_info_T *ccline = get_cmdline_info();
143 int i, j;
144 char_u *p1;
145 char_u *p2;
146 int difflen;
147 int v;
148
149 if (xp->xp_numfiles == -1)
150 {
151 set_expand_context(xp);
152 cmd_showtail = expand_showtail(xp);
153 }
154
155 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
156 {
157 beep_flush();
158 return OK; // Something illegal on command line
159 }
160 if (xp->xp_context == EXPAND_NOTHING)
161 {
162 // Caller can use the character as a normal char instead
163 return FAIL;
164 }
165
166 msg_puts("..."); // show that we are busy
167 out_flush();
168
169 i = (int)(xp->xp_pattern - ccline->cmdbuff);
170 xp->xp_pattern_len = ccline->cmdpos - i;
171
172 if (type == WILD_NEXT || type == WILD_PREV)
173 {
174 // Get next/previous match for a previous expanded pattern.
175 p2 = ExpandOne(xp, NULL, NULL, 0, type);
176 }
177 else
178 {
179 // Translate string into pattern and expand it.
180 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
181 xp->xp_context)) == NULL)
182 p2 = NULL;
183 else
184 {
185 int use_options = options |
186 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
187 if (escape)
188 use_options |= WILD_ESCAPE;
189
190 if (p_wic)
191 use_options += WILD_ICASE;
192 p2 = ExpandOne(xp, p1,
193 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
194 use_options, type);
195 vim_free(p1);
196 // longest match: make sure it is not shorter, happens with :help
197 if (p2 != NULL && type == WILD_LONGEST)
198 {
199 for (j = 0; j < xp->xp_pattern_len; ++j)
200 if (ccline->cmdbuff[i + j] == '*'
201 || ccline->cmdbuff[i + j] == '?')
202 break;
203 if ((int)STRLEN(p2) < j)
204 VIM_CLEAR(p2);
205 }
206 }
207 }
208
209 if (p2 != NULL && !got_int)
210 {
211 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
212 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
213 {
214 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
215 xp->xp_pattern = ccline->cmdbuff + i;
216 }
217 else
218 v = OK;
219 if (v == OK)
220 {
221 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
222 &ccline->cmdbuff[ccline->cmdpos],
223 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
224 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
225 ccline->cmdlen += difflen;
226 ccline->cmdpos += difflen;
227 }
228 }
229 vim_free(p2);
230
231 redrawcmd();
232 cursorcmd();
233
234 // When expanding a ":map" command and no matches are found, assume that
235 // the key is supposed to be inserted literally
236 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
237 return FAIL;
238
239 if (xp->xp_numfiles <= 0 && p2 == NULL)
240 beep_flush();
241 else if (xp->xp_numfiles == 1)
242 // free expanded pattern
243 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
244
245 return OK;
246}
247
248/*
249 * Do wildcard expansion on the string 'str'.
250 * Chars that should not be expanded must be preceded with a backslash.
251 * Return a pointer to allocated memory containing the new string.
252 * Return NULL for failure.
253 *
254 * "orig" is the originally expanded string, copied to allocated memory. It
255 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
256 * WILD_PREV "orig" should be NULL.
257 *
258 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
259 * is WILD_EXPAND_FREE or WILD_ALL.
260 *
261 * mode = WILD_FREE: just free previously expanded matches
262 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
263 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
264 * mode = WILD_NEXT: use next match in multiple match, wrap to first
265 * mode = WILD_PREV: use previous match in multiple match, wrap to first
266 * mode = WILD_ALL: return all matches concatenated
267 * mode = WILD_LONGEST: return longest matched part
268 * mode = WILD_ALL_KEEP: get all matches, keep matches
269 *
270 * options = WILD_LIST_NOTFOUND: list entries without a match
271 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
272 * options = WILD_USE_NL: Use '\n' for WILD_ALL
273 * options = WILD_NO_BEEP: Don't beep for multiple matches
274 * options = WILD_ADD_SLASH: add a slash after directory names
275 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
276 * options = WILD_SILENT: don't print warning messages
277 * options = WILD_ESCAPE: put backslash before special chars
278 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200279 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200280 *
281 * The variables xp->xp_context and xp->xp_backslash must have been set!
282 */
283 char_u *
284ExpandOne(
285 expand_T *xp,
286 char_u *str,
287 char_u *orig, // allocated copy of original of expanded string
288 int options,
289 int mode)
290{
291 char_u *ss = NULL;
292 static int findex;
293 static char_u *orig_save = NULL; // kept value of orig
294 int orig_saved = FALSE;
295 int i;
296 long_u len;
297 int non_suf_match; // number without matching suffix
298
299 // first handle the case of using an old match
300 if (mode == WILD_NEXT || mode == WILD_PREV)
301 {
302 if (xp->xp_numfiles > 0)
303 {
304 if (mode == WILD_PREV)
305 {
306 if (findex == -1)
307 findex = xp->xp_numfiles;
308 --findex;
309 }
310 else // mode == WILD_NEXT
311 ++findex;
312
313 // When wrapping around, return the original string, set findex to
314 // -1.
315 if (findex < 0)
316 {
317 if (orig_save == NULL)
318 findex = xp->xp_numfiles - 1;
319 else
320 findex = -1;
321 }
322 if (findex >= xp->xp_numfiles)
323 {
324 if (orig_save == NULL)
325 findex = 0;
326 else
327 findex = -1;
328 }
329#ifdef FEAT_WILDMENU
330 if (p_wmnu)
331 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
332 findex, cmd_showtail);
333#endif
334 if (findex == -1)
335 return vim_strsave(orig_save);
336 return vim_strsave(xp->xp_files[findex]);
337 }
338 else
339 return NULL;
340 }
341
342 // free old names
343 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
344 {
345 FreeWild(xp->xp_numfiles, xp->xp_files);
346 xp->xp_numfiles = -1;
347 VIM_CLEAR(orig_save);
348 }
349 findex = 0;
350
351 if (mode == WILD_FREE) // only release file name
352 return NULL;
353
354 if (xp->xp_numfiles == -1)
355 {
356 vim_free(orig_save);
357 orig_save = orig;
358 orig_saved = TRUE;
359
360 // Do the expansion.
361 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
362 options) == FAIL)
363 {
364#ifdef FNAME_ILLEGAL
365 // Illegal file name has been silently skipped. But when there
366 // are wildcards, the real problem is that there was no match,
367 // causing the pattern to be added, which has illegal characters.
368 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
369 semsg(_(e_nomatch2), str);
370#endif
371 }
372 else if (xp->xp_numfiles == 0)
373 {
374 if (!(options & WILD_SILENT))
375 semsg(_(e_nomatch2), str);
376 }
377 else
378 {
379 // Escape the matches for use on the command line.
380 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
381
382 // Check for matching suffixes in file names.
383 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
384 && mode != WILD_LONGEST)
385 {
386 if (xp->xp_numfiles)
387 non_suf_match = xp->xp_numfiles;
388 else
389 non_suf_match = 1;
390 if ((xp->xp_context == EXPAND_FILES
391 || xp->xp_context == EXPAND_DIRECTORIES)
392 && xp->xp_numfiles > 1)
393 {
394 // More than one match; check suffix.
395 // The files will have been sorted on matching suffix in
396 // expand_wildcards, only need to check the first two.
397 non_suf_match = 0;
398 for (i = 0; i < 2; ++i)
399 if (match_suffix(xp->xp_files[i]))
400 ++non_suf_match;
401 }
402 if (non_suf_match != 1)
403 {
404 // Can we ever get here unless it's while expanding
405 // interactively? If not, we can get rid of this all
406 // together. Don't really want to wait for this message
407 // (and possibly have to hit return to continue!).
408 if (!(options & WILD_SILENT))
409 emsg(_(e_toomany));
410 else if (!(options & WILD_NO_BEEP))
411 beep_flush();
412 }
413 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
414 ss = vim_strsave(xp->xp_files[0]);
415 }
416 }
417 }
418
419 // Find longest common part
420 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
421 {
422 int mb_len = 1;
423 int c0, ci;
424
425 for (len = 0; xp->xp_files[0][len]; len += mb_len)
426 {
427 if (has_mbyte)
428 {
429 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
430 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
431 }
432 else
433 c0 = xp->xp_files[0][len];
434 for (i = 1; i < xp->xp_numfiles; ++i)
435 {
436 if (has_mbyte)
437 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
438 else
439 ci = xp->xp_files[i][len];
440 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
441 || xp->xp_context == EXPAND_FILES
442 || xp->xp_context == EXPAND_SHELLCMD
443 || xp->xp_context == EXPAND_BUFFERS))
444 {
445 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
446 break;
447 }
448 else if (c0 != ci)
449 break;
450 }
451 if (i < xp->xp_numfiles)
452 {
453 if (!(options & WILD_NO_BEEP))
454 vim_beep(BO_WILD);
455 break;
456 }
457 }
458
459 ss = alloc(len + 1);
460 if (ss)
461 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
462 findex = -1; // next p_wc gets first one
463 }
464
465 // Concatenate all matching names
466 if (mode == WILD_ALL && xp->xp_numfiles > 0)
467 {
468 len = 0;
469 for (i = 0; i < xp->xp_numfiles; ++i)
470 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
471 ss = alloc(len);
472 if (ss != NULL)
473 {
474 *ss = NUL;
475 for (i = 0; i < xp->xp_numfiles; ++i)
476 {
477 STRCAT(ss, xp->xp_files[i]);
478 if (i != xp->xp_numfiles - 1)
479 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
480 }
481 }
482 }
483
484 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
485 ExpandCleanup(xp);
486
487 // Free "orig" if it wasn't stored in "orig_save".
488 if (!orig_saved)
489 vim_free(orig);
490
491 return ss;
492}
493
494/*
495 * Prepare an expand structure for use.
496 */
497 void
498ExpandInit(expand_T *xp)
499{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200500 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200501 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200502 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200503}
504
505/*
506 * Cleanup an expand structure after use.
507 */
508 void
509ExpandCleanup(expand_T *xp)
510{
511 if (xp->xp_numfiles >= 0)
512 {
513 FreeWild(xp->xp_numfiles, xp->xp_files);
514 xp->xp_numfiles = -1;
515 }
516}
517
518/*
519 * Show all matches for completion on the command line.
520 * Returns EXPAND_NOTHING when the character that triggered expansion should
521 * be inserted like a normal character.
522 */
523 int
524showmatches(expand_T *xp, int wildmenu UNUSED)
525{
526 cmdline_info_T *ccline = get_cmdline_info();
527#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
528 int num_files;
529 char_u **files_found;
530 int i, j, k;
531 int maxlen;
532 int lines;
533 int columns;
534 char_u *p;
535 int lastlen;
536 int attr;
537 int showtail;
538
539 if (xp->xp_numfiles == -1)
540 {
541 set_expand_context(xp);
542 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
543 &num_files, &files_found);
544 showtail = expand_showtail(xp);
545 if (i != EXPAND_OK)
546 return i;
547
548 }
549 else
550 {
551 num_files = xp->xp_numfiles;
552 files_found = xp->xp_files;
553 showtail = cmd_showtail;
554 }
555
556#ifdef FEAT_WILDMENU
557 if (!wildmenu)
558 {
559#endif
560 msg_didany = FALSE; // lines_left will be set
561 msg_start(); // prepare for paging
562 msg_putchar('\n');
563 out_flush();
564 cmdline_row = msg_row;
565 msg_didany = FALSE; // lines_left will be set again
566 msg_start(); // prepare for paging
567#ifdef FEAT_WILDMENU
568 }
569#endif
570
571 if (got_int)
572 got_int = FALSE; // only int. the completion, not the cmd line
573#ifdef FEAT_WILDMENU
574 else if (wildmenu)
575 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
576#endif
577 else
578 {
579 // find the length of the longest file name
580 maxlen = 0;
581 for (i = 0; i < num_files; ++i)
582 {
583 if (!showtail && (xp->xp_context == EXPAND_FILES
584 || xp->xp_context == EXPAND_SHELLCMD
585 || xp->xp_context == EXPAND_BUFFERS))
586 {
587 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
588 j = vim_strsize(NameBuff);
589 }
590 else
591 j = vim_strsize(L_SHOWFILE(i));
592 if (j > maxlen)
593 maxlen = j;
594 }
595
596 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
597 lines = num_files;
598 else
599 {
600 // compute the number of columns and lines for the listing
601 maxlen += 2; // two spaces between file names
602 columns = ((int)Columns + 2) / maxlen;
603 if (columns < 1)
604 columns = 1;
605 lines = (num_files + columns - 1) / columns;
606 }
607
608 attr = HL_ATTR(HLF_D); // find out highlighting for directories
609
610 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
611 {
612 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
613 msg_clr_eos();
614 msg_advance(maxlen - 3);
615 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
616 }
617
618 // list the files line by line
619 for (i = 0; i < lines; ++i)
620 {
621 lastlen = 999;
622 for (k = i; k < num_files; k += lines)
623 {
624 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
625 {
626 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
627 p = files_found[k] + STRLEN(files_found[k]) + 1;
628 msg_advance(maxlen + 1);
629 msg_puts((char *)p);
630 msg_advance(maxlen + 3);
631 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
632 break;
633 }
634 for (j = maxlen - lastlen; --j >= 0; )
635 msg_putchar(' ');
636 if (xp->xp_context == EXPAND_FILES
637 || xp->xp_context == EXPAND_SHELLCMD
638 || xp->xp_context == EXPAND_BUFFERS)
639 {
640 // highlight directories
641 if (xp->xp_numfiles != -1)
642 {
643 char_u *halved_slash;
644 char_u *exp_path;
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200645 char_u *path;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200646
647 // Expansion was done before and special characters
648 // were escaped, need to halve backslashes. Also
649 // $HOME has been replaced with ~/.
650 exp_path = expand_env_save_opt(files_found[k], TRUE);
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200651 path = exp_path != NULL ? exp_path : files_found[k];
652 halved_slash = backslash_halve_save(path);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200653 j = mch_isdir(halved_slash != NULL ? halved_slash
654 : files_found[k]);
655 vim_free(exp_path);
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200656 if (halved_slash != path)
657 vim_free(halved_slash);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200658 }
659 else
660 // Expansion was done here, file names are literal.
661 j = mch_isdir(files_found[k]);
662 if (showtail)
663 p = L_SHOWFILE(k);
664 else
665 {
666 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
667 TRUE);
668 p = NameBuff;
669 }
670 }
671 else
672 {
673 j = FALSE;
674 p = L_SHOWFILE(k);
675 }
676 lastlen = msg_outtrans_attr(p, j ? attr : 0);
677 }
678 if (msg_col > 0) // when not wrapped around
679 {
680 msg_clr_eos();
681 msg_putchar('\n');
682 }
683 out_flush(); // show one line at a time
684 if (got_int)
685 {
686 got_int = FALSE;
687 break;
688 }
689 }
690
691 // we redraw the command below the lines that we have just listed
692 // This is a bit tricky, but it saves a lot of screen updating.
693 cmdline_row = msg_row; // will put it back later
694 }
695
696 if (xp->xp_numfiles == -1)
697 FreeWild(num_files, files_found);
698
699 return EXPAND_OK;
700}
701
702/*
703 * Private gettail for showmatches() (and win_redr_status_matches()):
704 * Find tail of file name path, but ignore trailing "/".
705 */
706 char_u *
707sm_gettail(char_u *s)
708{
709 char_u *p;
710 char_u *t = s;
711 int had_sep = FALSE;
712
713 for (p = s; *p != NUL; )
714 {
715 if (vim_ispathsep(*p)
716#ifdef BACKSLASH_IN_FILENAME
717 && !rem_backslash(p)
718#endif
719 )
720 had_sep = TRUE;
721 else if (had_sep)
722 {
723 t = p;
724 had_sep = FALSE;
725 }
726 MB_PTR_ADV(p);
727 }
728 return t;
729}
730
731/*
732 * Return TRUE if we only need to show the tail of completion matches.
733 * When not completing file names or there is a wildcard in the path FALSE is
734 * returned.
735 */
736 static int
737expand_showtail(expand_T *xp)
738{
739 char_u *s;
740 char_u *end;
741
742 // When not completing file names a "/" may mean something different.
743 if (xp->xp_context != EXPAND_FILES
744 && xp->xp_context != EXPAND_SHELLCMD
745 && xp->xp_context != EXPAND_DIRECTORIES)
746 return FALSE;
747
748 end = gettail(xp->xp_pattern);
749 if (end == xp->xp_pattern) // there is no path separator
750 return FALSE;
751
752 for (s = xp->xp_pattern; s < end; s++)
753 {
754 // Skip escaped wildcards. Only when the backslash is not a path
755 // separator, on DOS the '*' "path\*\file" must not be skipped.
756 if (rem_backslash(s))
757 ++s;
758 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
759 return FALSE;
760 }
761 return TRUE;
762}
763
764/*
765 * Prepare a string for expansion.
766 * When expanding file names: The string will be used with expand_wildcards().
767 * Copy "fname[len]" into allocated memory and add a '*' at the end.
768 * When expanding other names: The string will be used with regcomp(). Copy
769 * the name into allocated memory and prepend "^".
770 */
771 char_u *
772addstar(
773 char_u *fname,
774 int len,
775 int context) // EXPAND_FILES etc.
776{
777 char_u *retval;
778 int i, j;
779 int new_len;
780 char_u *tail;
781 int ends_in_star;
782
783 if (context != EXPAND_FILES
784 && context != EXPAND_FILES_IN_PATH
785 && context != EXPAND_SHELLCMD
786 && context != EXPAND_DIRECTORIES)
787 {
788 // Matching will be done internally (on something other than files).
789 // So we convert the file-matching-type wildcards into our kind for
790 // use with vim_regcomp(). First work out how long it will be:
791
792 // For help tags the translation is done in find_help_tags().
793 // For a tag pattern starting with "/" no translation is needed.
794 if (context == EXPAND_HELP
795 || context == EXPAND_COLORS
796 || context == EXPAND_COMPILER
797 || context == EXPAND_OWNSYNTAX
798 || context == EXPAND_FILETYPE
799 || context == EXPAND_PACKADD
800 || ((context == EXPAND_TAGS_LISTFILES
801 || context == EXPAND_TAGS)
802 && fname[0] == '/'))
803 retval = vim_strnsave(fname, len);
804 else
805 {
806 new_len = len + 2; // +2 for '^' at start, NUL at end
807 for (i = 0; i < len; i++)
808 {
809 if (fname[i] == '*' || fname[i] == '~')
810 new_len++; // '*' needs to be replaced by ".*"
811 // '~' needs to be replaced by "\~"
812
813 // Buffer names are like file names. "." should be literal
814 if (context == EXPAND_BUFFERS && fname[i] == '.')
815 new_len++; // "." becomes "\."
816
817 // Custom expansion takes care of special things, match
818 // backslashes literally (perhaps also for other types?)
819 if ((context == EXPAND_USER_DEFINED
820 || context == EXPAND_USER_LIST) && fname[i] == '\\')
821 new_len++; // '\' becomes "\\"
822 }
823 retval = alloc(new_len);
824 if (retval != NULL)
825 {
826 retval[0] = '^';
827 j = 1;
828 for (i = 0; i < len; i++, j++)
829 {
830 // Skip backslash. But why? At least keep it for custom
831 // expansion.
832 if (context != EXPAND_USER_DEFINED
833 && context != EXPAND_USER_LIST
834 && fname[i] == '\\'
835 && ++i == len)
836 break;
837
838 switch (fname[i])
839 {
840 case '*': retval[j++] = '.';
841 break;
842 case '~': retval[j++] = '\\';
843 break;
844 case '?': retval[j] = '.';
845 continue;
846 case '.': if (context == EXPAND_BUFFERS)
847 retval[j++] = '\\';
848 break;
849 case '\\': if (context == EXPAND_USER_DEFINED
850 || context == EXPAND_USER_LIST)
851 retval[j++] = '\\';
852 break;
853 }
854 retval[j] = fname[i];
855 }
856 retval[j] = NUL;
857 }
858 }
859 }
860 else
861 {
862 retval = alloc(len + 4);
863 if (retval != NULL)
864 {
865 vim_strncpy(retval, fname, len);
866
867 // Don't add a star to *, ~, ~user, $var or `cmd`.
868 // * would become **, which walks the whole tree.
869 // ~ would be at the start of the file name, but not the tail.
870 // $ could be anywhere in the tail.
871 // ` could be anywhere in the file name.
872 // When the name ends in '$' don't add a star, remove the '$'.
873 tail = gettail(retval);
874 ends_in_star = (len > 0 && retval[len - 1] == '*');
875#ifndef BACKSLASH_IN_FILENAME
876 for (i = len - 2; i >= 0; --i)
877 {
878 if (retval[i] != '\\')
879 break;
880 ends_in_star = !ends_in_star;
881 }
882#endif
883 if ((*retval != '~' || tail != retval)
884 && !ends_in_star
885 && vim_strchr(tail, '$') == NULL
886 && vim_strchr(retval, '`') == NULL)
887 retval[len++] = '*';
888 else if (len > 0 && retval[len - 1] == '$')
889 --len;
890 retval[len] = NUL;
891 }
892 }
893 return retval;
894}
895
896/*
897 * Must parse the command line so far to work out what context we are in.
898 * Completion can then be done based on that context.
899 * This routine sets the variables:
900 * xp->xp_pattern The start of the pattern to be expanded within
901 * the command line (ends at the cursor).
902 * xp->xp_context The type of thing to expand. Will be one of:
903 *
904 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
905 * the command line, like an unknown command. Caller
906 * should beep.
907 * EXPAND_NOTHING Unrecognised context for completion, use char like
908 * a normal char, rather than for completion. eg
909 * :s/^I/
910 * EXPAND_COMMANDS Cursor is still touching the command, so complete
911 * it.
912 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
913 * EXPAND_FILES After command with EX_XFILE set, or after setting
914 * with P_EXPAND set. eg :e ^I, :w>>^I
915 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
916 * when we know only directories are of interest. eg
917 * :set dir=^I
918 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
919 * EXPAND_SETTINGS Complete variable names. eg :set d^I
920 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
921 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
922 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
923 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
924 * EXPAND_EVENTS Complete event names
925 * EXPAND_SYNTAX Complete :syntax command arguments
926 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
927 * EXPAND_AUGROUP Complete autocommand group names
928 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
929 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
930 * eg :unmap a^I , :cunab x^I
931 * EXPAND_FUNCTIONS Complete internal or user defined function names,
932 * eg :call sub^I
933 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
934 * EXPAND_EXPRESSION Complete internal or user defined function/variable
935 * names in expressions, eg :while s^I
936 * EXPAND_ENV_VARS Complete environment variable names
937 * EXPAND_USER Complete user names
938 */
939 static void
940set_expand_context(expand_T *xp)
941{
942 cmdline_info_T *ccline = get_cmdline_info();
943
944 // only expansion for ':', '>' and '=' command-lines
945 if (ccline->cmdfirstc != ':'
946#ifdef FEAT_EVAL
947 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
948 && !ccline->input_fn
949#endif
950 )
951 {
952 xp->xp_context = EXPAND_NOTHING;
953 return;
954 }
955 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
956}
957
Bram Moolenaard0190392019-08-23 21:17:35 +0200958/*
959 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
960 * we don't need/want deleted. Maybe this could be done better if we didn't
961 * repeat all this stuff. The only problem is that they may not stay
962 * perfectly compatible with each other, but then the command line syntax
963 * probably won't change that much -- webb.
964 */
965 static char_u *
966set_one_cmd_context(
967 expand_T *xp,
968 char_u *buff) // buffer for command string
969{
970 char_u *p;
971 char_u *cmd, *arg;
972 int len = 0;
973 exarg_T ea;
974 int compl = EXPAND_NOTHING;
975 int delim;
976 int forceit = FALSE;
977 int usefilter = FALSE; // filter instead of file name
978
979 ExpandInit(xp);
980 xp->xp_pattern = buff;
981 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
982 ea.argt = 0;
983
984 // 1. skip comment lines and leading space, colons or bars
985 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
986 ;
987 xp->xp_pattern = cmd;
988
989 if (*cmd == NUL)
990 return NULL;
991 if (*cmd == '"') // ignore comment lines
992 {
993 xp->xp_context = EXPAND_NOTHING;
994 return NULL;
995 }
996
997 // 3. Skip over the range to find the command.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200998 cmd = skip_range(cmd, TRUE, &xp->xp_context);
Bram Moolenaard0190392019-08-23 21:17:35 +0200999 xp->xp_pattern = cmd;
1000 if (*cmd == NUL)
1001 return NULL;
1002 if (*cmd == '"')
1003 {
1004 xp->xp_context = EXPAND_NOTHING;
1005 return NULL;
1006 }
1007
1008 if (*cmd == '|' || *cmd == '\n')
1009 return cmd + 1; // There's another command
1010
1011 // Isolate the command and search for it in the command table.
1012 // Exceptions:
1013 // - the 'k' command can directly be followed by any character, but
1014 // do accept "keepmarks", "keepalt" and "keepjumps".
1015 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
1016 if (*cmd == 'k' && cmd[1] != 'e')
1017 {
1018 ea.cmdidx = CMD_k;
1019 p = cmd + 1;
1020 }
1021 else
1022 {
1023 p = cmd;
1024 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1025 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001026 // A user command may contain digits.
1027 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1028 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001029 while (ASCII_ISALNUM(*p) || *p == '*')
1030 ++p;
1031 // for python 3.x: ":py3*" commands completion
1032 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1033 {
1034 ++p;
1035 while (ASCII_ISALPHA(*p) || *p == '*')
1036 ++p;
1037 }
1038 // check for non-alpha command
1039 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1040 ++p;
1041 len = (int)(p - cmd);
1042
1043 if (len == 0)
1044 {
1045 xp->xp_context = EXPAND_UNSUCCESSFUL;
1046 return NULL;
1047 }
1048
1049 ea.cmdidx = excmd_get_cmdidx(cmd, len);
1050
1051 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1052 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1053 ++p;
1054 }
1055
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001056 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001057 // character, complete the command name.
1058 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1059 return NULL;
1060
1061 if (ea.cmdidx == CMD_SIZE)
1062 {
1063 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1064 {
1065 ea.cmdidx = CMD_substitute;
1066 p = cmd + 1;
1067 }
1068 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1069 {
1070 ea.cmd = cmd;
1071 p = find_ucmd(&ea, p, NULL, xp, &compl);
1072 if (p == NULL)
1073 ea.cmdidx = CMD_SIZE; // ambiguous user command
1074 }
1075 }
1076 if (ea.cmdidx == CMD_SIZE)
1077 {
1078 // Not still touching the command and it was an illegal one
1079 xp->xp_context = EXPAND_UNSUCCESSFUL;
1080 return NULL;
1081 }
1082
1083 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
1084
1085 if (*p == '!') // forced commands
1086 {
1087 forceit = TRUE;
1088 ++p;
1089 }
1090
1091 // 6. parse arguments
1092 if (!IS_USER_CMDIDX(ea.cmdidx))
1093 ea.argt = excmd_get_argt(ea.cmdidx);
1094
1095 arg = skipwhite(p);
1096
Bram Moolenaar743d0622020-07-03 18:15:06 +02001097 // Skip over ++argopt argument
1098 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
1099 {
1100 p = arg;
1101 while (*p && !vim_isspace(*p))
1102 MB_PTR_ADV(p);
1103 arg = skipwhite(p);
1104 }
1105
Bram Moolenaard0190392019-08-23 21:17:35 +02001106 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
1107 {
1108 if (*arg == '>') // append
1109 {
1110 if (*++arg == '>')
1111 ++arg;
1112 arg = skipwhite(arg);
1113 }
1114 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
1115 {
1116 ++arg;
1117 usefilter = TRUE;
1118 }
1119 }
1120
1121 if (ea.cmdidx == CMD_read)
1122 {
1123 usefilter = forceit; // :r! filter if forced
1124 if (*arg == '!') // :r !filter
1125 {
1126 ++arg;
1127 usefilter = TRUE;
1128 }
1129 }
1130
1131 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
1132 {
1133 while (*arg == *cmd) // allow any number of '>' or '<'
1134 ++arg;
1135 arg = skipwhite(arg);
1136 }
1137
1138 // Does command allow "+command"?
1139 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
1140 {
1141 // Check if we're in the +command
1142 p = arg + 1;
1143 arg = skip_cmd_arg(arg, FALSE);
1144
1145 // Still touching the command after '+'?
1146 if (*arg == NUL)
1147 return p;
1148
1149 // Skip space(s) after +command to get to the real argument
1150 arg = skipwhite(arg);
1151 }
1152
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001153
Bram Moolenaard0190392019-08-23 21:17:35 +02001154 // Check for '|' to separate commands and '"' to start comments.
1155 // Don't do this for ":read !cmd" and ":write !cmd".
1156 if ((ea.argt & EX_TRLBAR) && !usefilter)
1157 {
1158 p = arg;
1159 // ":redir @" is not the start of a comment
1160 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
1161 p += 2;
1162 while (*p)
1163 {
1164 if (*p == Ctrl_V)
1165 {
1166 if (p[1] != NUL)
1167 ++p;
1168 }
1169 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
1170 || *p == '|' || *p == '\n')
1171 {
1172 if (*(p - 1) != '\\')
1173 {
1174 if (*p == '|' || *p == '\n')
1175 return p + 1;
1176 return NULL; // It's a comment
1177 }
1178 }
1179 MB_PTR_ADV(p);
1180 }
1181 }
1182
1183 if (!(ea.argt & EX_EXTRA) && *arg != NUL
1184 && vim_strchr((char_u *)"|\"", *arg) == NULL)
1185 // no arguments allowed but there is something
1186 return NULL;
1187
1188 // Find start of last argument (argument just before cursor):
1189 p = buff;
1190 xp->xp_pattern = p;
1191 len = (int)STRLEN(buff);
1192 while (*p && p < buff + len)
1193 {
1194 if (*p == ' ' || *p == TAB)
1195 {
1196 // argument starts after a space
1197 xp->xp_pattern = ++p;
1198 }
1199 else
1200 {
1201 if (*p == '\\' && *(p + 1) != NUL)
1202 ++p; // skip over escaped character
1203 MB_PTR_ADV(p);
1204 }
1205 }
1206
1207 if (ea.argt & EX_XFILE)
1208 {
1209 int c;
1210 int in_quote = FALSE;
1211 char_u *bow = NULL; // Beginning of word
1212
1213 // Allow spaces within back-quotes to count as part of the argument
1214 // being expanded.
1215 xp->xp_pattern = skipwhite(arg);
1216 p = xp->xp_pattern;
1217 while (*p != NUL)
1218 {
1219 if (has_mbyte)
1220 c = mb_ptr2char(p);
1221 else
1222 c = *p;
1223 if (c == '\\' && p[1] != NUL)
1224 ++p;
1225 else if (c == '`')
1226 {
1227 if (!in_quote)
1228 {
1229 xp->xp_pattern = p;
1230 bow = p + 1;
1231 }
1232 in_quote = !in_quote;
1233 }
1234 // An argument can contain just about everything, except
1235 // characters that end the command and white space.
1236 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
1237#ifdef SPACE_IN_FILENAME
1238 && (!(ea.argt & EX_NOSPC) || usefilter)
1239#endif
1240 ))
1241 {
1242 len = 0; // avoid getting stuck when space is in 'isfname'
1243 while (*p != NUL)
1244 {
1245 if (has_mbyte)
1246 c = mb_ptr2char(p);
1247 else
1248 c = *p;
1249 if (c == '`' || vim_isfilec_or_wc(c))
1250 break;
1251 if (has_mbyte)
1252 len = (*mb_ptr2len)(p);
1253 else
1254 len = 1;
1255 MB_PTR_ADV(p);
1256 }
1257 if (in_quote)
1258 bow = p;
1259 else
1260 xp->xp_pattern = p;
1261 p -= len;
1262 }
1263 MB_PTR_ADV(p);
1264 }
1265
1266 // If we are still inside the quotes, and we passed a space, just
1267 // expand from there.
1268 if (bow != NULL && in_quote)
1269 xp->xp_pattern = bow;
1270 xp->xp_context = EXPAND_FILES;
1271
1272 // For a shell command more chars need to be escaped.
1273 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
1274 {
1275#ifndef BACKSLASH_IN_FILENAME
1276 xp->xp_shell = TRUE;
1277#endif
1278 // When still after the command name expand executables.
1279 if (xp->xp_pattern == skipwhite(arg))
1280 xp->xp_context = EXPAND_SHELLCMD;
1281 }
1282
Albert Liu6024c042021-08-27 20:59:35 +02001283 // Check for environment variable.
1284 if (*xp->xp_pattern == '$')
Bram Moolenaard0190392019-08-23 21:17:35 +02001285 {
1286 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1287 if (!vim_isIDc(*p))
1288 break;
1289 if (*p == NUL)
1290 {
1291 xp->xp_context = EXPAND_ENV_VARS;
1292 ++xp->xp_pattern;
1293 // Avoid that the assignment uses EXPAND_FILES again.
1294 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
1295 compl = EXPAND_ENV_VARS;
1296 }
1297 }
Albert Liu6024c042021-08-27 20:59:35 +02001298 // Check for user names.
Bram Moolenaard0190392019-08-23 21:17:35 +02001299 if (*xp->xp_pattern == '~')
1300 {
1301 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1302 ;
1303 // Complete ~user only if it partially matches a user name.
1304 // A full match ~user<Tab> will be replaced by user's home
1305 // directory i.e. something like ~user<Tab> -> /home/user/
1306 if (*p == NUL && p > xp->xp_pattern + 1
1307 && match_user(xp->xp_pattern + 1) >= 1)
1308 {
1309 xp->xp_context = EXPAND_USER;
1310 ++xp->xp_pattern;
1311 }
1312 }
1313 }
1314
1315 // 6. Switch on command name.
1316 switch (ea.cmdidx)
1317 {
1318 case CMD_find:
1319 case CMD_sfind:
1320 case CMD_tabfind:
1321 if (xp->xp_context == EXPAND_FILES)
1322 xp->xp_context = EXPAND_FILES_IN_PATH;
1323 break;
1324 case CMD_cd:
1325 case CMD_chdir:
1326 case CMD_tcd:
1327 case CMD_tchdir:
1328 case CMD_lcd:
1329 case CMD_lchdir:
1330 if (xp->xp_context == EXPAND_FILES)
1331 xp->xp_context = EXPAND_DIRECTORIES;
1332 break;
1333 case CMD_help:
1334 xp->xp_context = EXPAND_HELP;
1335 xp->xp_pattern = arg;
1336 break;
1337
1338 // Command modifiers: return the argument.
1339 // Also for commands with an argument that is a command.
1340 case CMD_aboveleft:
1341 case CMD_argdo:
1342 case CMD_belowright:
1343 case CMD_botright:
1344 case CMD_browse:
1345 case CMD_bufdo:
1346 case CMD_cdo:
1347 case CMD_cfdo:
1348 case CMD_confirm:
1349 case CMD_debug:
1350 case CMD_folddoclosed:
1351 case CMD_folddoopen:
1352 case CMD_hide:
1353 case CMD_keepalt:
1354 case CMD_keepjumps:
1355 case CMD_keepmarks:
1356 case CMD_keeppatterns:
1357 case CMD_ldo:
1358 case CMD_leftabove:
1359 case CMD_lfdo:
1360 case CMD_lockmarks:
1361 case CMD_noautocmd:
1362 case CMD_noswapfile:
1363 case CMD_rightbelow:
1364 case CMD_sandbox:
1365 case CMD_silent:
1366 case CMD_tab:
1367 case CMD_tabdo:
1368 case CMD_topleft:
1369 case CMD_verbose:
1370 case CMD_vertical:
1371 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001372 case CMD_vim9cmd:
1373 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001374 return arg;
1375
1376 case CMD_filter:
1377 if (*arg != NUL)
1378 arg = skip_vimgrep_pat(arg, NULL, NULL);
1379 if (arg == NULL || *arg == NUL)
1380 {
1381 xp->xp_context = EXPAND_NOTHING;
1382 return NULL;
1383 }
1384 return skipwhite(arg);
1385
1386#ifdef FEAT_SEARCH_EXTRA
1387 case CMD_match:
1388 if (*arg == NUL || !ends_excmd(*arg))
1389 {
1390 // also complete "None"
1391 set_context_in_echohl_cmd(xp, arg);
1392 arg = skipwhite(skiptowhite(arg));
1393 if (*arg != NUL)
1394 {
1395 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001396 arg = skip_regexp(arg + 1, *arg, magic_isset());
Bram Moolenaard0190392019-08-23 21:17:35 +02001397 }
1398 }
1399 return find_nextcmd(arg);
1400#endif
1401
1402 // All completion for the +cmdline_compl feature goes here.
1403
1404 case CMD_command:
1405 return set_context_in_user_cmd(xp, arg);
1406
1407 case CMD_delcommand:
1408 xp->xp_context = EXPAND_USER_COMMANDS;
1409 xp->xp_pattern = arg;
1410 break;
1411
1412 case CMD_global:
1413 case CMD_vglobal:
1414 delim = *arg; // get the delimiter
1415 if (delim)
1416 ++arg; // skip delimiter if there is one
1417
1418 while (arg[0] != NUL && arg[0] != delim)
1419 {
1420 if (arg[0] == '\\' && arg[1] != NUL)
1421 ++arg;
1422 ++arg;
1423 }
1424 if (arg[0] != NUL)
1425 return arg + 1;
1426 break;
1427 case CMD_and:
1428 case CMD_substitute:
1429 delim = *arg;
1430 if (delim)
1431 {
1432 // skip "from" part
1433 ++arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001434 arg = skip_regexp(arg, delim, magic_isset());
Bram Moolenaard0190392019-08-23 21:17:35 +02001435 }
1436 // skip "to" part
1437 while (arg[0] != NUL && arg[0] != delim)
1438 {
1439 if (arg[0] == '\\' && arg[1] != NUL)
1440 ++arg;
1441 ++arg;
1442 }
1443 if (arg[0] != NUL) // skip delimiter
1444 ++arg;
1445 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1446 ++arg;
1447 if (arg[0] != NUL)
1448 return arg;
1449 break;
1450 case CMD_isearch:
1451 case CMD_dsearch:
1452 case CMD_ilist:
1453 case CMD_dlist:
1454 case CMD_ijump:
1455 case CMD_psearch:
1456 case CMD_djump:
1457 case CMD_isplit:
1458 case CMD_dsplit:
1459 arg = skipwhite(skipdigits(arg)); // skip count
1460 if (*arg == '/') // Match regexp, not just whole words
1461 {
1462 for (++arg; *arg && *arg != '/'; arg++)
1463 if (*arg == '\\' && arg[1] != NUL)
1464 arg++;
1465 if (*arg)
1466 {
1467 arg = skipwhite(arg + 1);
1468
1469 // Check for trailing illegal characters
1470 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1471 xp->xp_context = EXPAND_NOTHING;
1472 else
1473 return arg;
1474 }
1475 }
1476 break;
1477
1478 case CMD_autocmd:
1479 return set_context_in_autocmd(xp, arg, FALSE);
1480 case CMD_doautocmd:
1481 case CMD_doautoall:
1482 return set_context_in_autocmd(xp, arg, TRUE);
1483 case CMD_set:
1484 set_context_in_set_cmd(xp, arg, 0);
1485 break;
1486 case CMD_setglobal:
1487 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1488 break;
1489 case CMD_setlocal:
1490 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1491 break;
1492 case CMD_tag:
1493 case CMD_stag:
1494 case CMD_ptag:
1495 case CMD_ltag:
1496 case CMD_tselect:
1497 case CMD_stselect:
1498 case CMD_ptselect:
1499 case CMD_tjump:
1500 case CMD_stjump:
1501 case CMD_ptjump:
1502 if (*p_wop != NUL)
1503 xp->xp_context = EXPAND_TAGS_LISTFILES;
1504 else
1505 xp->xp_context = EXPAND_TAGS;
1506 xp->xp_pattern = arg;
1507 break;
1508 case CMD_augroup:
1509 xp->xp_context = EXPAND_AUGROUP;
1510 xp->xp_pattern = arg;
1511 break;
1512#ifdef FEAT_SYN_HL
1513 case CMD_syntax:
1514 set_context_in_syntax_cmd(xp, arg);
1515 break;
1516#endif
1517#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001518 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001519 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001520 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001521 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001522 case CMD_if:
1523 case CMD_elseif:
1524 case CMD_while:
1525 case CMD_for:
1526 case CMD_echo:
1527 case CMD_echon:
1528 case CMD_execute:
1529 case CMD_echomsg:
1530 case CMD_echoerr:
1531 case CMD_call:
1532 case CMD_return:
1533 case CMD_cexpr:
1534 case CMD_caddexpr:
1535 case CMD_cgetexpr:
1536 case CMD_lexpr:
1537 case CMD_laddexpr:
1538 case CMD_lgetexpr:
1539 set_context_for_expression(xp, arg, ea.cmdidx);
1540 break;
1541
1542 case CMD_unlet:
1543 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1544 arg = xp->xp_pattern + 1;
1545
1546 xp->xp_context = EXPAND_USER_VARS;
1547 xp->xp_pattern = arg;
1548
1549 if (*xp->xp_pattern == '$')
1550 {
1551 xp->xp_context = EXPAND_ENV_VARS;
1552 ++xp->xp_pattern;
1553 }
1554
1555 break;
1556
1557 case CMD_function:
1558 case CMD_delfunction:
1559 xp->xp_context = EXPAND_USER_FUNC;
1560 xp->xp_pattern = arg;
1561 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001562 case CMD_disassemble:
1563 set_context_in_disassemble_cmd(xp, arg);
1564 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001565
1566 case CMD_echohl:
1567 set_context_in_echohl_cmd(xp, arg);
1568 break;
1569#endif
1570 case CMD_highlight:
1571 set_context_in_highlight_cmd(xp, arg);
1572 break;
1573#ifdef FEAT_CSCOPE
1574 case CMD_cscope:
1575 case CMD_lcscope:
1576 case CMD_scscope:
1577 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
1578 break;
1579#endif
1580#ifdef FEAT_SIGNS
1581 case CMD_sign:
1582 set_context_in_sign_cmd(xp, arg);
1583 break;
1584#endif
1585 case CMD_bdelete:
1586 case CMD_bwipeout:
1587 case CMD_bunload:
1588 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1589 arg = xp->xp_pattern + 1;
1590 // FALLTHROUGH
1591 case CMD_buffer:
1592 case CMD_sbuffer:
1593 case CMD_checktime:
1594 xp->xp_context = EXPAND_BUFFERS;
1595 xp->xp_pattern = arg;
1596 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001597#ifdef FEAT_DIFF
1598 case CMD_diffget:
1599 case CMD_diffput:
1600 // If current buffer is in diff mode, complete buffer names
1601 // which are in diff mode, and different than current buffer.
1602 xp->xp_context = EXPAND_DIFF_BUFFERS;
1603 xp->xp_pattern = arg;
1604 break;
1605#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001606 case CMD_USER:
1607 case CMD_USER_BUF:
1608 if (compl != EXPAND_NOTHING)
1609 {
1610 // EX_XFILE: file names are handled above
1611 if (!(ea.argt & EX_XFILE))
1612 {
1613#ifdef FEAT_MENU
1614 if (compl == EXPAND_MENUS)
1615 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1616#endif
1617 if (compl == EXPAND_COMMANDS)
1618 return arg;
1619 if (compl == EXPAND_MAPPINGS)
1620 return set_context_in_map_cmd(xp, (char_u *)"map",
1621 arg, forceit, FALSE, FALSE, CMD_map);
1622 // Find start of last argument.
1623 p = arg;
1624 while (*p)
1625 {
1626 if (*p == ' ')
1627 // argument starts after a space
1628 arg = p + 1;
1629 else if (*p == '\\' && *(p + 1) != NUL)
1630 ++p; // skip over escaped character
1631 MB_PTR_ADV(p);
1632 }
1633 xp->xp_pattern = arg;
1634 }
1635 xp->xp_context = compl;
1636 }
1637 break;
1638
1639 case CMD_map: case CMD_noremap:
1640 case CMD_nmap: case CMD_nnoremap:
1641 case CMD_vmap: case CMD_vnoremap:
1642 case CMD_omap: case CMD_onoremap:
1643 case CMD_imap: case CMD_inoremap:
1644 case CMD_cmap: case CMD_cnoremap:
1645 case CMD_lmap: case CMD_lnoremap:
1646 case CMD_smap: case CMD_snoremap:
1647 case CMD_tmap: case CMD_tnoremap:
1648 case CMD_xmap: case CMD_xnoremap:
1649 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1650 FALSE, FALSE, ea.cmdidx);
1651 case CMD_unmap:
1652 case CMD_nunmap:
1653 case CMD_vunmap:
1654 case CMD_ounmap:
1655 case CMD_iunmap:
1656 case CMD_cunmap:
1657 case CMD_lunmap:
1658 case CMD_sunmap:
1659 case CMD_tunmap:
1660 case CMD_xunmap:
1661 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1662 FALSE, TRUE, ea.cmdidx);
1663 case CMD_mapclear:
1664 case CMD_nmapclear:
1665 case CMD_vmapclear:
1666 case CMD_omapclear:
1667 case CMD_imapclear:
1668 case CMD_cmapclear:
1669 case CMD_lmapclear:
1670 case CMD_smapclear:
1671 case CMD_tmapclear:
1672 case CMD_xmapclear:
1673 xp->xp_context = EXPAND_MAPCLEAR;
1674 xp->xp_pattern = arg;
1675 break;
1676
1677 case CMD_abbreviate: case CMD_noreabbrev:
1678 case CMD_cabbrev: case CMD_cnoreabbrev:
1679 case CMD_iabbrev: case CMD_inoreabbrev:
1680 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1681 TRUE, FALSE, ea.cmdidx);
1682 case CMD_unabbreviate:
1683 case CMD_cunabbrev:
1684 case CMD_iunabbrev:
1685 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1686 TRUE, TRUE, ea.cmdidx);
1687#ifdef FEAT_MENU
1688 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1689 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1690 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1691 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1692 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1693 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1694 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1695 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1696 case CMD_tmenu: case CMD_tunmenu:
1697 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1698 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1699#endif
1700
1701 case CMD_colorscheme:
1702 xp->xp_context = EXPAND_COLORS;
1703 xp->xp_pattern = arg;
1704 break;
1705
1706 case CMD_compiler:
1707 xp->xp_context = EXPAND_COMPILER;
1708 xp->xp_pattern = arg;
1709 break;
1710
1711 case CMD_ownsyntax:
1712 xp->xp_context = EXPAND_OWNSYNTAX;
1713 xp->xp_pattern = arg;
1714 break;
1715
1716 case CMD_setfiletype:
1717 xp->xp_context = EXPAND_FILETYPE;
1718 xp->xp_pattern = arg;
1719 break;
1720
1721 case CMD_packadd:
1722 xp->xp_context = EXPAND_PACKADD;
1723 xp->xp_pattern = arg;
1724 break;
1725
1726#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1727 case CMD_language:
1728 p = skiptowhite(arg);
1729 if (*p == NUL)
1730 {
1731 xp->xp_context = EXPAND_LANGUAGE;
1732 xp->xp_pattern = arg;
1733 }
1734 else
1735 {
1736 if ( STRNCMP(arg, "messages", p - arg) == 0
1737 || STRNCMP(arg, "ctype", p - arg) == 0
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001738 || STRNCMP(arg, "time", p - arg) == 0
1739 || STRNCMP(arg, "collate", p - arg) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001740 {
1741 xp->xp_context = EXPAND_LOCALES;
1742 xp->xp_pattern = skipwhite(p);
1743 }
1744 else
1745 xp->xp_context = EXPAND_NOTHING;
1746 }
1747 break;
1748#endif
1749#if defined(FEAT_PROFILE)
1750 case CMD_profile:
1751 set_context_in_profile_cmd(xp, arg);
1752 break;
1753#endif
1754 case CMD_behave:
1755 xp->xp_context = EXPAND_BEHAVE;
1756 xp->xp_pattern = arg;
1757 break;
1758
1759 case CMD_messages:
1760 xp->xp_context = EXPAND_MESSAGES;
1761 xp->xp_pattern = arg;
1762 break;
1763
1764 case CMD_history:
1765 xp->xp_context = EXPAND_HISTORY;
1766 xp->xp_pattern = arg;
1767 break;
1768#if defined(FEAT_PROFILE)
1769 case CMD_syntime:
1770 xp->xp_context = EXPAND_SYNTIME;
1771 xp->xp_pattern = arg;
1772 break;
1773#endif
1774
1775 case CMD_argdelete:
1776 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1777 arg = xp->xp_pattern + 1;
1778 xp->xp_context = EXPAND_ARGLIST;
1779 xp->xp_pattern = arg;
1780 break;
1781
1782 default:
1783 break;
1784 }
1785 return NULL;
1786}
1787
Bram Moolenaar66b51422019-08-18 21:44:12 +02001788 void
1789set_cmd_context(
1790 expand_T *xp,
1791 char_u *str, // start of command line
1792 int len, // length of command line (excl. NUL)
1793 int col, // position of cursor
1794 int use_ccline UNUSED) // use ccline for info
1795{
1796#ifdef FEAT_EVAL
1797 cmdline_info_T *ccline = get_cmdline_info();
1798#endif
1799 int old_char = NUL;
1800 char_u *nextcomm;
1801
1802 // Avoid a UMR warning from Purify, only save the character if it has been
1803 // written before.
1804 if (col < len)
1805 old_char = str[col];
1806 str[col] = NUL;
1807 nextcomm = str;
1808
1809#ifdef FEAT_EVAL
1810 if (use_ccline && ccline->cmdfirstc == '=')
1811 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001812 // pass CMD_SIZE because there is no real command
1813 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001814 }
1815 else if (use_ccline && ccline->input_fn)
1816 {
1817 xp->xp_context = ccline->xp_context;
1818 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001819 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001820 }
1821 else
1822#endif
1823 while (nextcomm != NULL)
1824 nextcomm = set_one_cmd_context(xp, nextcomm);
1825
1826 // Store the string here so that call_user_expand_func() can get to them
1827 // easily.
1828 xp->xp_line = str;
1829 xp->xp_col = col;
1830
1831 str[col] = old_char;
1832}
1833
1834/*
1835 * Expand the command line "str" from context "xp".
1836 * "xp" must have been set by set_cmd_context().
1837 * xp->xp_pattern points into "str", to where the text that is to be expanded
1838 * starts.
1839 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
1840 * cursor.
1841 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
1842 * key that triggered expansion literally.
1843 * Returns EXPAND_OK otherwise.
1844 */
1845 int
1846expand_cmdline(
1847 expand_T *xp,
1848 char_u *str, // start of command line
1849 int col, // position of cursor
1850 int *matchcount, // return: nr of matches
1851 char_u ***matches) // return: array of pointers to matches
1852{
1853 char_u *file_str = NULL;
1854 int options = WILD_ADD_SLASH|WILD_SILENT;
1855
1856 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
1857 {
1858 beep_flush();
1859 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
1860 }
1861 if (xp->xp_context == EXPAND_NOTHING)
1862 {
1863 // Caller can use the character as a normal char instead
1864 return EXPAND_NOTHING;
1865 }
1866
1867 // add star to file name, or convert to regexp if not exp. files.
1868 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
1869 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
1870 if (file_str == NULL)
1871 return EXPAND_UNSUCCESSFUL;
1872
1873 if (p_wic)
1874 options += WILD_ICASE;
1875
1876 // find all files that match the description
1877 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
1878 {
1879 *matchcount = 0;
1880 *matches = NULL;
1881 }
1882 vim_free(file_str);
1883
1884 return EXPAND_OK;
1885}
1886
Bram Moolenaar66b51422019-08-18 21:44:12 +02001887/*
Bram Moolenaard0190392019-08-23 21:17:35 +02001888 * Function given to ExpandGeneric() to obtain the possible arguments of the
1889 * ":behave {mswin,xterm}" command.
1890 */
1891 static char_u *
1892get_behave_arg(expand_T *xp UNUSED, int idx)
1893{
1894 if (idx == 0)
1895 return (char_u *)"mswin";
1896 if (idx == 1)
1897 return (char_u *)"xterm";
1898 return NULL;
1899}
1900
1901/*
1902 * Function given to ExpandGeneric() to obtain the possible arguments of the
1903 * ":messages {clear}" command.
1904 */
1905 static char_u *
1906get_messages_arg(expand_T *xp UNUSED, int idx)
1907{
1908 if (idx == 0)
1909 return (char_u *)"clear";
1910 return NULL;
1911}
1912
1913 static char_u *
1914get_mapclear_arg(expand_T *xp UNUSED, int idx)
1915{
1916 if (idx == 0)
1917 return (char_u *)"<buffer>";
1918 return NULL;
1919}
1920
1921/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001922 * Do the expansion based on xp->xp_context and "pat".
1923 */
1924 static int
1925ExpandFromContext(
1926 expand_T *xp,
1927 char_u *pat,
1928 int *num_file,
1929 char_u ***file,
1930 int options) // WILD_ flags
1931{
Bram Moolenaar66b51422019-08-18 21:44:12 +02001932 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001933 int ret;
1934 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001935 char_u *tofree = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001936
1937 flags = EW_DIR; // include directories
1938 if (options & WILD_LIST_NOTFOUND)
1939 flags |= EW_NOTFOUND;
1940 if (options & WILD_ADD_SLASH)
1941 flags |= EW_ADDSLASH;
1942 if (options & WILD_KEEP_ALL)
1943 flags |= EW_KEEPALL;
1944 if (options & WILD_SILENT)
1945 flags |= EW_SILENT;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02001946 if (options & WILD_NOERROR)
1947 flags |= EW_NOERROR;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001948 if (options & WILD_ALLLINKS)
1949 flags |= EW_ALLLINKS;
1950
1951 if (xp->xp_context == EXPAND_FILES
1952 || xp->xp_context == EXPAND_DIRECTORIES
1953 || xp->xp_context == EXPAND_FILES_IN_PATH)
1954 {
1955 // Expand file or directory names.
1956 int free_pat = FALSE;
1957 int i;
1958
1959 // for ":set path=" and ":set tags=" halve backslashes for escaped
1960 // space
1961 if (xp->xp_backslash != XP_BS_NONE)
1962 {
1963 free_pat = TRUE;
1964 pat = vim_strsave(pat);
1965 for (i = 0; pat[i]; ++i)
1966 if (pat[i] == '\\')
1967 {
1968 if (xp->xp_backslash == XP_BS_THREE
1969 && pat[i + 1] == '\\'
1970 && pat[i + 2] == '\\'
1971 && pat[i + 3] == ' ')
1972 STRMOVE(pat + i, pat + i + 3);
1973 if (xp->xp_backslash == XP_BS_ONE
1974 && pat[i + 1] == ' ')
1975 STRMOVE(pat + i, pat + i + 1);
1976 }
1977 }
1978
1979 if (xp->xp_context == EXPAND_FILES)
1980 flags |= EW_FILE;
1981 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
1982 flags |= (EW_FILE | EW_PATH);
1983 else
1984 flags = (flags | EW_DIR) & ~EW_FILE;
1985 if (options & WILD_ICASE)
1986 flags |= EW_ICASE;
1987
1988 // Expand wildcards, supporting %:h and the like.
1989 ret = expand_wildcards_eval(&pat, num_file, file, flags);
1990 if (free_pat)
1991 vim_free(pat);
1992#ifdef BACKSLASH_IN_FILENAME
1993 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
1994 {
1995 int i;
1996
1997 for (i = 0; i < *num_file; ++i)
1998 {
1999 char_u *ptr = (*file)[i];
2000
2001 while (*ptr != NUL)
2002 {
2003 if (p_csl[0] == 's' && *ptr == '\\')
2004 *ptr = '/';
2005 else if (p_csl[0] == 'b' && *ptr == '/')
2006 *ptr = '\\';
2007 ptr += (*mb_ptr2len)(ptr);
2008 }
2009 }
2010 }
2011#endif
2012 return ret;
2013 }
2014
2015 *file = (char_u **)"";
2016 *num_file = 0;
2017 if (xp->xp_context == EXPAND_HELP)
2018 {
2019 // With an empty argument we would get all the help tags, which is
2020 // very slow. Get matches for "help" instead.
2021 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
2022 num_file, file, FALSE) == OK)
2023 {
2024#ifdef FEAT_MULTI_LANG
2025 cleanup_help_tags(*num_file, *file);
2026#endif
2027 return OK;
2028 }
2029 return FAIL;
2030 }
2031
Bram Moolenaar66b51422019-08-18 21:44:12 +02002032 if (xp->xp_context == EXPAND_SHELLCMD)
2033 return expand_shellcmd(pat, num_file, file, flags);
2034 if (xp->xp_context == EXPAND_OLD_SETTING)
2035 return ExpandOldSetting(num_file, file);
2036 if (xp->xp_context == EXPAND_BUFFERS)
2037 return ExpandBufnames(pat, num_file, file, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002038#ifdef FEAT_DIFF
2039 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
2040 return ExpandBufnames(pat, num_file, file, options | BUF_DIFF_FILTER);
2041#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002042 if (xp->xp_context == EXPAND_TAGS
2043 || xp->xp_context == EXPAND_TAGS_LISTFILES)
2044 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
2045 if (xp->xp_context == EXPAND_COLORS)
2046 {
2047 char *directories[] = {"colors", NULL};
2048 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
2049 directories);
2050 }
2051 if (xp->xp_context == EXPAND_COMPILER)
2052 {
2053 char *directories[] = {"compiler", NULL};
2054 return ExpandRTDir(pat, 0, num_file, file, directories);
2055 }
2056 if (xp->xp_context == EXPAND_OWNSYNTAX)
2057 {
2058 char *directories[] = {"syntax", NULL};
2059 return ExpandRTDir(pat, 0, num_file, file, directories);
2060 }
2061 if (xp->xp_context == EXPAND_FILETYPE)
2062 {
2063 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
2064 return ExpandRTDir(pat, 0, num_file, file, directories);
2065 }
2066# if defined(FEAT_EVAL)
2067 if (xp->xp_context == EXPAND_USER_LIST)
2068 return ExpandUserList(xp, num_file, file);
2069# endif
2070 if (xp->xp_context == EXPAND_PACKADD)
2071 return ExpandPackAddDir(pat, num_file, file);
2072
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002073 // When expanding a function name starting with s:, match the <SNR>nr_
2074 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002075 if ((xp->xp_context == EXPAND_USER_FUNC
2076 || xp->xp_context == EXPAND_DISASSEMBLE)
2077 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002078 {
2079 int len = (int)STRLEN(pat) + 20;
2080
2081 tofree = alloc(len);
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002082 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002083 pat = tofree;
2084 }
2085
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002086 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002087 if (regmatch.regprog == NULL)
2088 return FAIL;
2089
2090 // set ignore-case according to p_ic, p_scs and pat
2091 regmatch.rm_ic = ignorecase(pat);
2092
2093 if (xp->xp_context == EXPAND_SETTINGS
2094 || xp->xp_context == EXPAND_BOOL_SETTINGS)
2095 ret = ExpandSettings(xp, &regmatch, num_file, file);
2096 else if (xp->xp_context == EXPAND_MAPPINGS)
2097 ret = ExpandMappings(&regmatch, num_file, file);
2098# if defined(FEAT_EVAL)
2099 else if (xp->xp_context == EXPAND_USER_DEFINED)
2100 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
2101# endif
2102 else
2103 {
2104 static struct expgen
2105 {
2106 int context;
2107 char_u *((*func)(expand_T *, int));
2108 int ic;
2109 int escaped;
2110 } tab[] =
2111 {
2112 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2113 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2114 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2115 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2116 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2117 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2118 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2119 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2120 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2121 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2122# ifdef FEAT_EVAL
2123 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2124 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2125 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002126 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
Bram Moolenaar66b51422019-08-18 21:44:12 +02002127 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2128# endif
2129# ifdef FEAT_MENU
2130 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2131 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2132# endif
2133# ifdef FEAT_SYN_HL
2134 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2135# endif
2136# ifdef FEAT_PROFILE
2137 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2138# endif
2139 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaarb4d82e22021-09-01 13:03:39 +02002140 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2141 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar66b51422019-08-18 21:44:12 +02002142# ifdef FEAT_CSCOPE
2143 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2144# endif
2145# ifdef FEAT_SIGNS
2146 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2147# endif
2148# ifdef FEAT_PROFILE
2149 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2150# endif
2151# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2152 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2153 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2154# endif
2155 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2156 {EXPAND_USER, get_users, TRUE, FALSE},
2157 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2158 };
2159 int i;
2160
2161 // Find a context in the table and call the ExpandGeneric() with the
2162 // right function to do the expansion.
2163 ret = FAIL;
K.Takataeeec2542021-06-02 13:28:16 +02002164 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002165 if (xp->xp_context == tab[i].context)
2166 {
2167 if (tab[i].ic)
2168 regmatch.rm_ic = TRUE;
2169 ret = ExpandGeneric(xp, &regmatch, num_file, file,
2170 tab[i].func, tab[i].escaped);
2171 break;
2172 }
2173 }
2174
2175 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002176 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002177
2178 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002179}
2180
Bram Moolenaar66b51422019-08-18 21:44:12 +02002181/*
2182 * Expand a list of names.
2183 *
2184 * Generic function for command line completion. It calls a function to
2185 * obtain strings, one by one. The strings are matched against a regexp
2186 * program. Matching strings are copied into an array, which is returned.
2187 *
2188 * Returns OK when no problems encountered, FAIL for error (out of memory).
2189 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002190 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002191ExpandGeneric(
2192 expand_T *xp,
2193 regmatch_T *regmatch,
2194 int *num_file,
2195 char_u ***file,
2196 char_u *((*func)(expand_T *, int)),
2197 // returns a string from the list
2198 int escaped)
2199{
2200 int i;
2201 int count = 0;
2202 int round;
2203 char_u *str;
2204
2205 // do this loop twice:
2206 // round == 0: count the number of matching names
2207 // round == 1: copy the matching names into allocated memory
2208 for (round = 0; round <= 1; ++round)
2209 {
2210 for (i = 0; ; ++i)
2211 {
2212 str = (*func)(xp, i);
2213 if (str == NULL) // end of list
2214 break;
2215 if (*str == NUL) // skip empty strings
2216 continue;
2217
2218 if (vim_regexec(regmatch, str, (colnr_T)0))
2219 {
2220 if (round)
2221 {
2222 if (escaped)
2223 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2224 else
2225 str = vim_strsave(str);
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002226 if (str == NULL)
2227 {
2228 FreeWild(count, *file);
2229 *num_file = 0;
2230 *file = NULL;
2231 return FAIL;
2232 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002233 (*file)[count] = str;
2234# ifdef FEAT_MENU
2235 if (func == get_menu_names && str != NULL)
2236 {
2237 // test for separator added by get_menu_names()
2238 str += STRLEN(str) - 1;
2239 if (*str == '\001')
2240 *str = '.';
2241 }
2242# endif
2243 }
2244 ++count;
2245 }
2246 }
2247 if (round == 0)
2248 {
2249 if (count == 0)
2250 return OK;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002251 *file = ALLOC_MULT(char_u *, count);
2252 if (*file == NULL)
2253 {
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002254 *num_file = 0;
2255 *file = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002256 return FAIL;
2257 }
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002258 *num_file = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002259 count = 0;
2260 }
2261 }
2262
2263 // Sort the results. Keep menu's in the specified order.
2264 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2265 {
2266 if (xp->xp_context == EXPAND_EXPRESSION
2267 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002268 || xp->xp_context == EXPAND_USER_FUNC
2269 || xp->xp_context == EXPAND_DISASSEMBLE)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002270 // <SNR> functions should be sorted to the end.
2271 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
2272 sort_func_compare);
2273 else
2274 sort_strings(*file, *num_file);
2275 }
2276
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002277#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002278 // Reset the variables used for special highlight names expansion, so that
2279 // they don't show up when getting normal highlight names by ID.
2280 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002281#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002282 return OK;
2283}
2284
2285/*
2286 * Complete a shell command.
2287 * Returns FAIL or OK;
2288 */
2289 static int
2290expand_shellcmd(
2291 char_u *filepat, // pattern to match with command names
2292 int *num_file, // return: number of matches
2293 char_u ***file, // return: array with matches
2294 int flagsarg) // EW_ flags
2295{
2296 char_u *pat;
2297 int i;
2298 char_u *path = NULL;
2299 int mustfree = FALSE;
2300 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002301 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002302 size_t l;
2303 char_u *s, *e;
2304 int flags = flagsarg;
2305 int ret;
2306 int did_curdir = FALSE;
2307 hashtab_T found_ht;
2308 hashitem_T *hi;
2309 hash_T hash;
2310
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002311 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002312 if (buf == NULL)
2313 return FAIL;
2314
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002315 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002316 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002317 if (pat == NULL)
2318 {
2319 vim_free(buf);
2320 return FAIL;
2321 }
2322
Bram Moolenaar66b51422019-08-18 21:44:12 +02002323 for (i = 0; pat[i]; ++i)
2324 if (pat[i] == '\\' && pat[i + 1] == ' ')
2325 STRMOVE(pat + i, pat + i + 1);
2326
2327 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2328
2329 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2330 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2331 path = (char_u *)".";
2332 else
2333 {
2334 // For an absolute name we don't use $PATH.
2335 if (!mch_isFullName(pat))
2336 path = vim_getenv((char_u *)"PATH", &mustfree);
2337 if (path == NULL)
2338 path = (char_u *)"";
2339 }
2340
2341 // Go over all directories in $PATH. Expand matches in that directory and
2342 // collect them in "ga". When "." is not in $PATH also expand for the
2343 // current directory, to find "subdir/cmd".
2344 ga_init2(&ga, (int)sizeof(char *), 10);
2345 hash_init(&found_ht);
2346 for (s = path; ; s = e)
2347 {
2348# if defined(MSWIN)
2349 e = vim_strchr(s, ';');
2350# else
2351 e = vim_strchr(s, ':');
2352# endif
2353 if (e == NULL)
2354 e = s + STRLEN(s);
2355
2356 if (*s == NUL)
2357 {
2358 if (did_curdir)
2359 break;
2360 // Find directories in the current directory, path is empty.
2361 did_curdir = TRUE;
2362 flags |= EW_DIR;
2363 }
2364 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2365 {
2366 did_curdir = TRUE;
2367 flags |= EW_DIR;
2368 }
2369 else
2370 // Do not match directories inside a $PATH item.
2371 flags &= ~EW_DIR;
2372
2373 l = e - s;
2374 if (l > MAXPATHL - 5)
2375 break;
2376 vim_strncpy(buf, s, l);
2377 add_pathsep(buf);
2378 l = STRLEN(buf);
2379 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2380
2381 // Expand matches in one directory of $PATH.
2382 ret = expand_wildcards(1, &buf, num_file, file, flags);
2383 if (ret == OK)
2384 {
2385 if (ga_grow(&ga, *num_file) == FAIL)
2386 FreeWild(*num_file, *file);
2387 else
2388 {
2389 for (i = 0; i < *num_file; ++i)
2390 {
2391 char_u *name = (*file)[i];
2392
2393 if (STRLEN(name) > l)
2394 {
2395 // Check if this name was already found.
2396 hash = hash_hash(name + l);
2397 hi = hash_lookup(&found_ht, name + l, hash);
2398 if (HASHITEM_EMPTY(hi))
2399 {
2400 // Remove the path that was prepended.
2401 STRMOVE(name, name + l);
2402 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
2403 hash_add_item(&found_ht, hi, name, hash);
2404 name = NULL;
2405 }
2406 }
2407 vim_free(name);
2408 }
2409 vim_free(*file);
2410 }
2411 }
2412 if (*e != NUL)
2413 ++e;
2414 }
2415 *file = ga.ga_data;
2416 *num_file = ga.ga_len;
2417
2418 vim_free(buf);
2419 vim_free(pat);
2420 if (mustfree)
2421 vim_free(path);
2422 hash_clear(&found_ht);
2423 return OK;
2424}
2425
2426# if defined(FEAT_EVAL)
2427/*
2428 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002429 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002430 */
2431 static void *
2432call_user_expand_func(
2433 void *(*user_expand_func)(char_u *, int, typval_T *),
2434 expand_T *xp,
2435 int *num_file,
2436 char_u ***file)
2437{
2438 cmdline_info_T *ccline = get_cmdline_info();
2439 int keep = 0;
2440 typval_T args[4];
2441 sctx_T save_current_sctx = current_sctx;
2442 char_u *pat = NULL;
2443 void *ret;
2444
2445 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2446 return NULL;
2447 *num_file = 0;
2448 *file = NULL;
2449
2450 if (ccline->cmdbuff != NULL)
2451 {
2452 keep = ccline->cmdbuff[ccline->cmdlen];
2453 ccline->cmdbuff[ccline->cmdlen] = 0;
2454 }
2455
2456 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2457
2458 args[0].v_type = VAR_STRING;
2459 args[0].vval.v_string = pat;
2460 args[1].v_type = VAR_STRING;
2461 args[1].vval.v_string = xp->xp_line;
2462 args[2].v_type = VAR_NUMBER;
2463 args[2].vval.v_number = xp->xp_col;
2464 args[3].v_type = VAR_UNKNOWN;
2465
2466 current_sctx = xp->xp_script_ctx;
2467
2468 ret = user_expand_func(xp->xp_arg, 3, args);
2469
2470 current_sctx = save_current_sctx;
2471 if (ccline->cmdbuff != NULL)
2472 ccline->cmdbuff[ccline->cmdlen] = keep;
2473
2474 vim_free(pat);
2475 return ret;
2476}
2477
2478/*
2479 * Expand names with a function defined by the user.
2480 */
2481 static int
2482ExpandUserDefined(
2483 expand_T *xp,
2484 regmatch_T *regmatch,
2485 int *num_file,
2486 char_u ***file)
2487{
2488 char_u *retstr;
2489 char_u *s;
2490 char_u *e;
2491 int keep;
2492 garray_T ga;
2493 int skip;
2494
2495 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
2496 if (retstr == NULL)
2497 return FAIL;
2498
2499 ga_init2(&ga, (int)sizeof(char *), 3);
2500 for (s = retstr; *s != NUL; s = e)
2501 {
2502 e = vim_strchr(s, '\n');
2503 if (e == NULL)
2504 e = s + STRLEN(s);
2505 keep = *e;
2506 *e = NUL;
2507
2508 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2509 *e = keep;
2510
2511 if (!skip)
2512 {
2513 if (ga_grow(&ga, 1) == FAIL)
2514 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02002515 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002516 ++ga.ga_len;
2517 }
2518
2519 if (*e != NUL)
2520 ++e;
2521 }
2522 vim_free(retstr);
2523 *file = ga.ga_data;
2524 *num_file = ga.ga_len;
2525 return OK;
2526}
2527
2528/*
2529 * Expand names with a list returned by a function defined by the user.
2530 */
2531 static int
2532ExpandUserList(
2533 expand_T *xp,
2534 int *num_file,
2535 char_u ***file)
2536{
2537 list_T *retlist;
2538 listitem_T *li;
2539 garray_T ga;
2540
2541 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
2542 if (retlist == NULL)
2543 return FAIL;
2544
2545 ga_init2(&ga, (int)sizeof(char *), 3);
2546 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002547 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002548 {
2549 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
2550 continue; // Skip non-string items and empty strings
2551
2552 if (ga_grow(&ga, 1) == FAIL)
2553 break;
2554
2555 ((char_u **)ga.ga_data)[ga.ga_len] =
2556 vim_strsave(li->li_tv.vval.v_string);
2557 ++ga.ga_len;
2558 }
2559 list_unref(retlist);
2560
2561 *file = ga.ga_data;
2562 *num_file = ga.ga_len;
2563 return OK;
2564}
2565# endif
2566
2567/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002568 * Expand "file" for all comma-separated directories in "path".
2569 * Adds the matches to "ga". Caller must init "ga".
2570 */
2571 void
2572globpath(
2573 char_u *path,
2574 char_u *file,
2575 garray_T *ga,
2576 int expand_options)
2577{
2578 expand_T xpc;
2579 char_u *buf;
2580 int i;
2581 int num_p;
2582 char_u **p;
2583
2584 buf = alloc(MAXPATHL);
2585 if (buf == NULL)
2586 return;
2587
2588 ExpandInit(&xpc);
2589 xpc.xp_context = EXPAND_FILES;
2590
2591 // Loop over all entries in {path}.
2592 while (*path != NUL)
2593 {
2594 // Copy one item of the path to buf[] and concatenate the file name.
2595 copy_option_part(&path, buf, MAXPATHL, ",");
2596 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
2597 {
2598# if defined(MSWIN)
2599 // Using the platform's path separator (\) makes vim incorrectly
2600 // treat it as an escape character, use '/' instead.
2601 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
2602 STRCAT(buf, "/");
2603# else
2604 add_pathsep(buf);
2605# endif
2606 STRCAT(buf, file);
2607 if (ExpandFromContext(&xpc, buf, &num_p, &p,
2608 WILD_SILENT|expand_options) != FAIL && num_p > 0)
2609 {
2610 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
2611
2612 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002613 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02002614 for (i = 0; i < num_p; ++i)
2615 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002616 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02002617 ++ga->ga_len;
2618 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002619 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002620 }
2621 }
2622 }
2623
2624 vim_free(buf);
2625}
Bram Moolenaar66b51422019-08-18 21:44:12 +02002626
Bram Moolenaareadee482020-09-04 15:37:31 +02002627#ifdef FEAT_WILDMENU
2628
2629/*
2630 * Translate some keys pressed when 'wildmenu' is used.
2631 */
2632 int
2633wildmenu_translate_key(
2634 cmdline_info_T *cclp,
2635 int key,
2636 expand_T *xp,
2637 int did_wild_list)
2638{
2639 int c = key;
2640
2641 if (did_wild_list && p_wmnu)
2642 {
2643 if (c == K_LEFT)
2644 c = Ctrl_P;
2645 else if (c == K_RIGHT)
2646 c = Ctrl_N;
2647 }
2648 // Hitting CR after "emenu Name.": complete submenu
2649 if (xp->xp_context == EXPAND_MENUNAMES && p_wmnu
2650 && cclp->cmdpos > 1
2651 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
2652 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
2653 && (c == '\n' || c == '\r' || c == K_KENTER))
2654 c = K_DOWN;
2655
2656 return c;
2657}
2658
2659/*
2660 * Delete characters on the command line, from "from" to the current
2661 * position.
2662 */
2663 static void
2664cmdline_del(cmdline_info_T *cclp, int from)
2665{
2666 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
2667 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
2668 cclp->cmdlen -= cclp->cmdpos - from;
2669 cclp->cmdpos = from;
2670}
2671
2672/*
2673 * Handle a key pressed when wild menu is displayed
2674 */
2675 int
2676wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
2677{
2678 int c = key;
2679 int i;
2680 int j;
2681
2682 if (!p_wmnu)
2683 return c;
2684
2685 // Special translations for 'wildmenu'
2686 if (xp->xp_context == EXPAND_MENUNAMES)
2687 {
2688 // Hitting <Down> after "emenu Name.": complete submenu
2689 if (c == K_DOWN && cclp->cmdpos > 0
2690 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002691 {
Bram Moolenaareadee482020-09-04 15:37:31 +02002692 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002693 KeyTyped = TRUE; // in case the key was mapped
2694 }
Bram Moolenaareadee482020-09-04 15:37:31 +02002695 else if (c == K_UP)
2696 {
2697 // Hitting <Up>: Remove one submenu name in front of the
2698 // cursor
2699 int found = FALSE;
2700
2701 j = (int)(xp->xp_pattern - cclp->cmdbuff);
2702 i = 0;
2703 while (--j > 0)
2704 {
2705 // check for start of menu name
2706 if (cclp->cmdbuff[j] == ' '
2707 && cclp->cmdbuff[j - 1] != '\\')
2708 {
2709 i = j + 1;
2710 break;
2711 }
2712 // check for start of submenu name
2713 if (cclp->cmdbuff[j] == '.'
2714 && cclp->cmdbuff[j - 1] != '\\')
2715 {
2716 if (found)
2717 {
2718 i = j + 1;
2719 break;
2720 }
2721 else
2722 found = TRUE;
2723 }
2724 }
2725 if (i > 0)
2726 cmdline_del(cclp, i);
2727 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002728 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02002729 xp->xp_context = EXPAND_NOTHING;
2730 }
2731 }
2732 if ((xp->xp_context == EXPAND_FILES
2733 || xp->xp_context == EXPAND_DIRECTORIES
2734 || xp->xp_context == EXPAND_SHELLCMD))
2735 {
2736 char_u upseg[5];
2737
2738 upseg[0] = PATHSEP;
2739 upseg[1] = '.';
2740 upseg[2] = '.';
2741 upseg[3] = PATHSEP;
2742 upseg[4] = NUL;
2743
2744 if (c == K_DOWN
2745 && cclp->cmdpos > 0
2746 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
2747 && (cclp->cmdpos < 3
2748 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
2749 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
2750 {
2751 // go down a directory
2752 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002753 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02002754 }
2755 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
2756 {
2757 // If in a direct ancestor, strip off one ../ to go down
2758 int found = FALSE;
2759
2760 j = cclp->cmdpos;
2761 i = (int)(xp->xp_pattern - cclp->cmdbuff);
2762 while (--j > i)
2763 {
2764 if (has_mbyte)
2765 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
2766 if (vim_ispathsep(cclp->cmdbuff[j]))
2767 {
2768 found = TRUE;
2769 break;
2770 }
2771 }
2772 if (found
2773 && cclp->cmdbuff[j - 1] == '.'
2774 && cclp->cmdbuff[j - 2] == '.'
2775 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
2776 {
2777 cmdline_del(cclp, j - 2);
2778 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002779 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02002780 }
2781 }
2782 else if (c == K_UP)
2783 {
2784 // go up a directory
2785 int found = FALSE;
2786
2787 j = cclp->cmdpos - 1;
2788 i = (int)(xp->xp_pattern - cclp->cmdbuff);
2789 while (--j > i)
2790 {
2791 if (has_mbyte)
2792 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
2793 if (vim_ispathsep(cclp->cmdbuff[j])
2794# ifdef BACKSLASH_IN_FILENAME
2795 && vim_strchr((char_u *)" *?[{`$%#",
2796 cclp->cmdbuff[j + 1]) == NULL
2797# endif
2798 )
2799 {
2800 if (found)
2801 {
2802 i = j + 1;
2803 break;
2804 }
2805 else
2806 found = TRUE;
2807 }
2808 }
2809
2810 if (!found)
2811 j = i;
2812 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
2813 j += 4;
2814 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
2815 && j == i)
2816 j += 3;
2817 else
2818 j = 0;
2819 if (j > 0)
2820 {
2821 // TODO this is only for DOS/UNIX systems - need to put in
2822 // machine-specific stuff here and in upseg init
2823 cmdline_del(cclp, j);
2824 put_on_cmdline(upseg + 1, 3, FALSE);
2825 }
2826 else if (cclp->cmdpos > i)
2827 cmdline_del(cclp, i);
2828
2829 // Now complete in the new directory. Set KeyTyped in case the
2830 // Up key came from a mapping.
2831 c = p_wc;
2832 KeyTyped = TRUE;
2833 }
2834 }
2835
2836 return c;
2837}
2838
2839/*
2840 * Free expanded names when finished walking through the matches
2841 */
2842 void
2843wildmenu_cleanup(cmdline_info_T *cclp)
2844{
2845 int skt = KeyTyped;
2846 int old_RedrawingDisabled = RedrawingDisabled;
2847
2848 if (!p_wmnu || wild_menu_showing == 0)
2849 return;
2850
2851 if (cclp->input_fn)
2852 RedrawingDisabled = 0;
2853
2854 if (wild_menu_showing == WM_SCROLLED)
2855 {
2856 // Entered command line, move it up
2857 cmdline_row--;
2858 redrawcmd();
2859 }
2860 else if (save_p_ls != -1)
2861 {
2862 // restore 'laststatus' and 'winminheight'
2863 p_ls = save_p_ls;
2864 p_wmh = save_p_wmh;
2865 last_status(FALSE);
2866 update_screen(VALID); // redraw the screen NOW
2867 redrawcmd();
2868 save_p_ls = -1;
2869 }
2870 else
2871 {
2872 win_redraw_last_status(topframe);
2873 redraw_statuslines();
2874 }
2875 KeyTyped = skt;
2876 wild_menu_showing = 0;
2877 if (cclp->input_fn)
2878 RedrawingDisabled = old_RedrawingDisabled;
2879}
2880#endif
2881
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002882#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002883/*
2884 * "getcompletion()" function
2885 */
2886 void
2887f_getcompletion(typval_T *argvars, typval_T *rettv)
2888{
2889 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002890 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002891 expand_T xpc;
2892 int filtered = FALSE;
2893 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002894 | WILD_NO_BEEP;
2895
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002896 if (in_vim9script()
2897 && (check_for_string_arg(argvars, 0) == FAIL
2898 || check_for_string_arg(argvars, 1) == FAIL
2899 || check_for_opt_bool_arg(argvars, 2) == FAIL))
2900 return;
2901
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002902 if (argvars[1].v_type != VAR_STRING)
2903 {
2904 semsg(_(e_invarg2), "type must be a string");
2905 return;
2906 }
2907 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002908
2909 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02002910 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002911
2912 if (p_wic)
2913 options |= WILD_ICASE;
2914
2915 // For filtered results, 'wildignore' is used
2916 if (!filtered)
2917 options |= WILD_KEEP_ALL;
2918
2919 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002920 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002921 {
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002922 set_one_cmd_context(&xpc, tv_get_string(&argvars[0]));
2923 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002924 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002925 else
2926 {
2927 xpc.xp_pattern = tv_get_string(&argvars[0]);
2928 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2929
2930 xpc.xp_context = cmdcomplete_str_to_type(type);
2931 if (xpc.xp_context == EXPAND_NOTHING)
2932 {
2933 semsg(_(e_invarg2), type);
2934 return;
2935 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002936
2937# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002938 if (xpc.xp_context == EXPAND_MENUS)
2939 {
2940 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
2941 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2942 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002943# endif
2944# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002945 if (xpc.xp_context == EXPAND_CSCOPE)
2946 {
2947 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
2948 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2949 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002950# endif
2951# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002952 if (xpc.xp_context == EXPAND_SIGN)
2953 {
2954 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
2955 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2956 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002957# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002958 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002959
2960 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
2961 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
2962 {
2963 int i;
2964
2965 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
2966
2967 for (i = 0; i < xpc.xp_numfiles; i++)
2968 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2969 }
2970 vim_free(pat);
2971 ExpandCleanup(&xpc);
2972}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002973#endif // FEAT_EVAL