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