blob: 63f0cc7b152e82483e6e5a8770505971c0e005a1 [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
1102 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
1103 {
1104 if (*arg == '>') // append
1105 {
1106 if (*++arg == '>')
1107 ++arg;
1108 arg = skipwhite(arg);
1109 }
1110 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
1111 {
1112 ++arg;
1113 usefilter = TRUE;
1114 }
1115 }
1116
1117 if (ea.cmdidx == CMD_read)
1118 {
1119 usefilter = forceit; // :r! filter if forced
1120 if (*arg == '!') // :r !filter
1121 {
1122 ++arg;
1123 usefilter = TRUE;
1124 }
1125 }
1126
1127 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
1128 {
1129 while (*arg == *cmd) // allow any number of '>' or '<'
1130 ++arg;
1131 arg = skipwhite(arg);
1132 }
1133
1134 // Does command allow "+command"?
1135 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
1136 {
1137 // Check if we're in the +command
1138 p = arg + 1;
1139 arg = skip_cmd_arg(arg, FALSE);
1140
1141 // Still touching the command after '+'?
1142 if (*arg == NUL)
1143 return p;
1144
1145 // Skip space(s) after +command to get to the real argument
1146 arg = skipwhite(arg);
1147 }
1148
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001149 // Skip over ++argopt argument
1150 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
1151 {
1152 p = arg;
1153 while (*p && !vim_isspace(*p))
1154 MB_PTR_ADV(p);
1155 arg = skipwhite(p);
1156 }
1157
Bram Moolenaard0190392019-08-23 21:17:35 +02001158 // Check for '|' to separate commands and '"' to start comments.
1159 // Don't do this for ":read !cmd" and ":write !cmd".
1160 if ((ea.argt & EX_TRLBAR) && !usefilter)
1161 {
1162 p = arg;
1163 // ":redir @" is not the start of a comment
1164 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
1165 p += 2;
1166 while (*p)
1167 {
1168 if (*p == Ctrl_V)
1169 {
1170 if (p[1] != NUL)
1171 ++p;
1172 }
1173 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
1174 || *p == '|' || *p == '\n')
1175 {
1176 if (*(p - 1) != '\\')
1177 {
1178 if (*p == '|' || *p == '\n')
1179 return p + 1;
1180 return NULL; // It's a comment
1181 }
1182 }
1183 MB_PTR_ADV(p);
1184 }
1185 }
1186
1187 if (!(ea.argt & EX_EXTRA) && *arg != NUL
1188 && vim_strchr((char_u *)"|\"", *arg) == NULL)
1189 // no arguments allowed but there is something
1190 return NULL;
1191
1192 // Find start of last argument (argument just before cursor):
1193 p = buff;
1194 xp->xp_pattern = p;
1195 len = (int)STRLEN(buff);
1196 while (*p && p < buff + len)
1197 {
1198 if (*p == ' ' || *p == TAB)
1199 {
1200 // argument starts after a space
1201 xp->xp_pattern = ++p;
1202 }
1203 else
1204 {
1205 if (*p == '\\' && *(p + 1) != NUL)
1206 ++p; // skip over escaped character
1207 MB_PTR_ADV(p);
1208 }
1209 }
1210
1211 if (ea.argt & EX_XFILE)
1212 {
1213 int c;
1214 int in_quote = FALSE;
1215 char_u *bow = NULL; // Beginning of word
1216
1217 // Allow spaces within back-quotes to count as part of the argument
1218 // being expanded.
1219 xp->xp_pattern = skipwhite(arg);
1220 p = xp->xp_pattern;
1221 while (*p != NUL)
1222 {
1223 if (has_mbyte)
1224 c = mb_ptr2char(p);
1225 else
1226 c = *p;
1227 if (c == '\\' && p[1] != NUL)
1228 ++p;
1229 else if (c == '`')
1230 {
1231 if (!in_quote)
1232 {
1233 xp->xp_pattern = p;
1234 bow = p + 1;
1235 }
1236 in_quote = !in_quote;
1237 }
1238 // An argument can contain just about everything, except
1239 // characters that end the command and white space.
1240 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
1241#ifdef SPACE_IN_FILENAME
1242 && (!(ea.argt & EX_NOSPC) || usefilter)
1243#endif
1244 ))
1245 {
1246 len = 0; // avoid getting stuck when space is in 'isfname'
1247 while (*p != NUL)
1248 {
1249 if (has_mbyte)
1250 c = mb_ptr2char(p);
1251 else
1252 c = *p;
1253 if (c == '`' || vim_isfilec_or_wc(c))
1254 break;
1255 if (has_mbyte)
1256 len = (*mb_ptr2len)(p);
1257 else
1258 len = 1;
1259 MB_PTR_ADV(p);
1260 }
1261 if (in_quote)
1262 bow = p;
1263 else
1264 xp->xp_pattern = p;
1265 p -= len;
1266 }
1267 MB_PTR_ADV(p);
1268 }
1269
1270 // If we are still inside the quotes, and we passed a space, just
1271 // expand from there.
1272 if (bow != NULL && in_quote)
1273 xp->xp_pattern = bow;
1274 xp->xp_context = EXPAND_FILES;
1275
1276 // For a shell command more chars need to be escaped.
1277 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
1278 {
1279#ifndef BACKSLASH_IN_FILENAME
1280 xp->xp_shell = TRUE;
1281#endif
1282 // When still after the command name expand executables.
1283 if (xp->xp_pattern == skipwhite(arg))
1284 xp->xp_context = EXPAND_SHELLCMD;
1285 }
1286
1287 // Check for environment variable
1288 if (*xp->xp_pattern == '$'
1289#if defined(MSWIN)
1290 || *xp->xp_pattern == '%'
1291#endif
1292 )
1293 {
1294 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1295 if (!vim_isIDc(*p))
1296 break;
1297 if (*p == NUL)
1298 {
1299 xp->xp_context = EXPAND_ENV_VARS;
1300 ++xp->xp_pattern;
1301 // Avoid that the assignment uses EXPAND_FILES again.
1302 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
1303 compl = EXPAND_ENV_VARS;
1304 }
1305 }
1306 // Check for user names
1307 if (*xp->xp_pattern == '~')
1308 {
1309 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1310 ;
1311 // Complete ~user only if it partially matches a user name.
1312 // A full match ~user<Tab> will be replaced by user's home
1313 // directory i.e. something like ~user<Tab> -> /home/user/
1314 if (*p == NUL && p > xp->xp_pattern + 1
1315 && match_user(xp->xp_pattern + 1) >= 1)
1316 {
1317 xp->xp_context = EXPAND_USER;
1318 ++xp->xp_pattern;
1319 }
1320 }
1321 }
1322
1323 // 6. Switch on command name.
1324 switch (ea.cmdidx)
1325 {
1326 case CMD_find:
1327 case CMD_sfind:
1328 case CMD_tabfind:
1329 if (xp->xp_context == EXPAND_FILES)
1330 xp->xp_context = EXPAND_FILES_IN_PATH;
1331 break;
1332 case CMD_cd:
1333 case CMD_chdir:
1334 case CMD_tcd:
1335 case CMD_tchdir:
1336 case CMD_lcd:
1337 case CMD_lchdir:
1338 if (xp->xp_context == EXPAND_FILES)
1339 xp->xp_context = EXPAND_DIRECTORIES;
1340 break;
1341 case CMD_help:
1342 xp->xp_context = EXPAND_HELP;
1343 xp->xp_pattern = arg;
1344 break;
1345
1346 // Command modifiers: return the argument.
1347 // Also for commands with an argument that is a command.
1348 case CMD_aboveleft:
1349 case CMD_argdo:
1350 case CMD_belowright:
1351 case CMD_botright:
1352 case CMD_browse:
1353 case CMD_bufdo:
1354 case CMD_cdo:
1355 case CMD_cfdo:
1356 case CMD_confirm:
1357 case CMD_debug:
1358 case CMD_folddoclosed:
1359 case CMD_folddoopen:
1360 case CMD_hide:
1361 case CMD_keepalt:
1362 case CMD_keepjumps:
1363 case CMD_keepmarks:
1364 case CMD_keeppatterns:
1365 case CMD_ldo:
1366 case CMD_leftabove:
1367 case CMD_lfdo:
1368 case CMD_lockmarks:
1369 case CMD_noautocmd:
1370 case CMD_noswapfile:
1371 case CMD_rightbelow:
1372 case CMD_sandbox:
1373 case CMD_silent:
1374 case CMD_tab:
1375 case CMD_tabdo:
1376 case CMD_topleft:
1377 case CMD_verbose:
1378 case CMD_vertical:
1379 case CMD_windo:
1380 return arg;
1381
1382 case CMD_filter:
1383 if (*arg != NUL)
1384 arg = skip_vimgrep_pat(arg, NULL, NULL);
1385 if (arg == NULL || *arg == NUL)
1386 {
1387 xp->xp_context = EXPAND_NOTHING;
1388 return NULL;
1389 }
1390 return skipwhite(arg);
1391
1392#ifdef FEAT_SEARCH_EXTRA
1393 case CMD_match:
1394 if (*arg == NUL || !ends_excmd(*arg))
1395 {
1396 // also complete "None"
1397 set_context_in_echohl_cmd(xp, arg);
1398 arg = skipwhite(skiptowhite(arg));
1399 if (*arg != NUL)
1400 {
1401 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001402 arg = skip_regexp(arg + 1, *arg, p_magic);
Bram Moolenaard0190392019-08-23 21:17:35 +02001403 }
1404 }
1405 return find_nextcmd(arg);
1406#endif
1407
1408 // All completion for the +cmdline_compl feature goes here.
1409
1410 case CMD_command:
1411 return set_context_in_user_cmd(xp, arg);
1412
1413 case CMD_delcommand:
1414 xp->xp_context = EXPAND_USER_COMMANDS;
1415 xp->xp_pattern = arg;
1416 break;
1417
1418 case CMD_global:
1419 case CMD_vglobal:
1420 delim = *arg; // get the delimiter
1421 if (delim)
1422 ++arg; // skip delimiter if there is one
1423
1424 while (arg[0] != NUL && arg[0] != delim)
1425 {
1426 if (arg[0] == '\\' && arg[1] != NUL)
1427 ++arg;
1428 ++arg;
1429 }
1430 if (arg[0] != NUL)
1431 return arg + 1;
1432 break;
1433 case CMD_and:
1434 case CMD_substitute:
1435 delim = *arg;
1436 if (delim)
1437 {
1438 // skip "from" part
1439 ++arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001440 arg = skip_regexp(arg, delim, p_magic);
Bram Moolenaard0190392019-08-23 21:17:35 +02001441 }
1442 // skip "to" part
1443 while (arg[0] != NUL && arg[0] != delim)
1444 {
1445 if (arg[0] == '\\' && arg[1] != NUL)
1446 ++arg;
1447 ++arg;
1448 }
1449 if (arg[0] != NUL) // skip delimiter
1450 ++arg;
1451 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1452 ++arg;
1453 if (arg[0] != NUL)
1454 return arg;
1455 break;
1456 case CMD_isearch:
1457 case CMD_dsearch:
1458 case CMD_ilist:
1459 case CMD_dlist:
1460 case CMD_ijump:
1461 case CMD_psearch:
1462 case CMD_djump:
1463 case CMD_isplit:
1464 case CMD_dsplit:
1465 arg = skipwhite(skipdigits(arg)); // skip count
1466 if (*arg == '/') // Match regexp, not just whole words
1467 {
1468 for (++arg; *arg && *arg != '/'; arg++)
1469 if (*arg == '\\' && arg[1] != NUL)
1470 arg++;
1471 if (*arg)
1472 {
1473 arg = skipwhite(arg + 1);
1474
1475 // Check for trailing illegal characters
1476 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1477 xp->xp_context = EXPAND_NOTHING;
1478 else
1479 return arg;
1480 }
1481 }
1482 break;
1483
1484 case CMD_autocmd:
1485 return set_context_in_autocmd(xp, arg, FALSE);
1486 case CMD_doautocmd:
1487 case CMD_doautoall:
1488 return set_context_in_autocmd(xp, arg, TRUE);
1489 case CMD_set:
1490 set_context_in_set_cmd(xp, arg, 0);
1491 break;
1492 case CMD_setglobal:
1493 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1494 break;
1495 case CMD_setlocal:
1496 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1497 break;
1498 case CMD_tag:
1499 case CMD_stag:
1500 case CMD_ptag:
1501 case CMD_ltag:
1502 case CMD_tselect:
1503 case CMD_stselect:
1504 case CMD_ptselect:
1505 case CMD_tjump:
1506 case CMD_stjump:
1507 case CMD_ptjump:
1508 if (*p_wop != NUL)
1509 xp->xp_context = EXPAND_TAGS_LISTFILES;
1510 else
1511 xp->xp_context = EXPAND_TAGS;
1512 xp->xp_pattern = arg;
1513 break;
1514 case CMD_augroup:
1515 xp->xp_context = EXPAND_AUGROUP;
1516 xp->xp_pattern = arg;
1517 break;
1518#ifdef FEAT_SYN_HL
1519 case CMD_syntax:
1520 set_context_in_syntax_cmd(xp, arg);
1521 break;
1522#endif
1523#ifdef FEAT_EVAL
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001524 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001525 case CMD_let:
1526 case CMD_if:
1527 case CMD_elseif:
1528 case CMD_while:
1529 case CMD_for:
1530 case CMD_echo:
1531 case CMD_echon:
1532 case CMD_execute:
1533 case CMD_echomsg:
1534 case CMD_echoerr:
1535 case CMD_call:
1536 case CMD_return:
1537 case CMD_cexpr:
1538 case CMD_caddexpr:
1539 case CMD_cgetexpr:
1540 case CMD_lexpr:
1541 case CMD_laddexpr:
1542 case CMD_lgetexpr:
1543 set_context_for_expression(xp, arg, ea.cmdidx);
1544 break;
1545
1546 case CMD_unlet:
1547 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1548 arg = xp->xp_pattern + 1;
1549
1550 xp->xp_context = EXPAND_USER_VARS;
1551 xp->xp_pattern = arg;
1552
1553 if (*xp->xp_pattern == '$')
1554 {
1555 xp->xp_context = EXPAND_ENV_VARS;
1556 ++xp->xp_pattern;
1557 }
1558
1559 break;
1560
1561 case CMD_function:
1562 case CMD_delfunction:
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001563 case CMD_disassemble:
Bram Moolenaard0190392019-08-23 21:17:35 +02001564 xp->xp_context = EXPAND_USER_FUNC;
1565 xp->xp_pattern = arg;
1566 break;
1567
1568 case CMD_echohl:
1569 set_context_in_echohl_cmd(xp, arg);
1570 break;
1571#endif
1572 case CMD_highlight:
1573 set_context_in_highlight_cmd(xp, arg);
1574 break;
1575#ifdef FEAT_CSCOPE
1576 case CMD_cscope:
1577 case CMD_lcscope:
1578 case CMD_scscope:
1579 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
1580 break;
1581#endif
1582#ifdef FEAT_SIGNS
1583 case CMD_sign:
1584 set_context_in_sign_cmd(xp, arg);
1585 break;
1586#endif
1587 case CMD_bdelete:
1588 case CMD_bwipeout:
1589 case CMD_bunload:
1590 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1591 arg = xp->xp_pattern + 1;
1592 // FALLTHROUGH
1593 case CMD_buffer:
1594 case CMD_sbuffer:
1595 case CMD_checktime:
1596 xp->xp_context = EXPAND_BUFFERS;
1597 xp->xp_pattern = arg;
1598 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001599#ifdef FEAT_DIFF
1600 case CMD_diffget:
1601 case CMD_diffput:
1602 // If current buffer is in diff mode, complete buffer names
1603 // which are in diff mode, and different than current buffer.
1604 xp->xp_context = EXPAND_DIFF_BUFFERS;
1605 xp->xp_pattern = arg;
1606 break;
1607#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001608 case CMD_USER:
1609 case CMD_USER_BUF:
1610 if (compl != EXPAND_NOTHING)
1611 {
1612 // EX_XFILE: file names are handled above
1613 if (!(ea.argt & EX_XFILE))
1614 {
1615#ifdef FEAT_MENU
1616 if (compl == EXPAND_MENUS)
1617 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1618#endif
1619 if (compl == EXPAND_COMMANDS)
1620 return arg;
1621 if (compl == EXPAND_MAPPINGS)
1622 return set_context_in_map_cmd(xp, (char_u *)"map",
1623 arg, forceit, FALSE, FALSE, CMD_map);
1624 // Find start of last argument.
1625 p = arg;
1626 while (*p)
1627 {
1628 if (*p == ' ')
1629 // argument starts after a space
1630 arg = p + 1;
1631 else if (*p == '\\' && *(p + 1) != NUL)
1632 ++p; // skip over escaped character
1633 MB_PTR_ADV(p);
1634 }
1635 xp->xp_pattern = arg;
1636 }
1637 xp->xp_context = compl;
1638 }
1639 break;
1640
1641 case CMD_map: case CMD_noremap:
1642 case CMD_nmap: case CMD_nnoremap:
1643 case CMD_vmap: case CMD_vnoremap:
1644 case CMD_omap: case CMD_onoremap:
1645 case CMD_imap: case CMD_inoremap:
1646 case CMD_cmap: case CMD_cnoremap:
1647 case CMD_lmap: case CMD_lnoremap:
1648 case CMD_smap: case CMD_snoremap:
1649 case CMD_tmap: case CMD_tnoremap:
1650 case CMD_xmap: case CMD_xnoremap:
1651 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1652 FALSE, FALSE, ea.cmdidx);
1653 case CMD_unmap:
1654 case CMD_nunmap:
1655 case CMD_vunmap:
1656 case CMD_ounmap:
1657 case CMD_iunmap:
1658 case CMD_cunmap:
1659 case CMD_lunmap:
1660 case CMD_sunmap:
1661 case CMD_tunmap:
1662 case CMD_xunmap:
1663 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1664 FALSE, TRUE, ea.cmdidx);
1665 case CMD_mapclear:
1666 case CMD_nmapclear:
1667 case CMD_vmapclear:
1668 case CMD_omapclear:
1669 case CMD_imapclear:
1670 case CMD_cmapclear:
1671 case CMD_lmapclear:
1672 case CMD_smapclear:
1673 case CMD_tmapclear:
1674 case CMD_xmapclear:
1675 xp->xp_context = EXPAND_MAPCLEAR;
1676 xp->xp_pattern = arg;
1677 break;
1678
1679 case CMD_abbreviate: case CMD_noreabbrev:
1680 case CMD_cabbrev: case CMD_cnoreabbrev:
1681 case CMD_iabbrev: case CMD_inoreabbrev:
1682 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1683 TRUE, FALSE, ea.cmdidx);
1684 case CMD_unabbreviate:
1685 case CMD_cunabbrev:
1686 case CMD_iunabbrev:
1687 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1688 TRUE, TRUE, ea.cmdidx);
1689#ifdef FEAT_MENU
1690 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1691 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1692 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1693 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1694 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1695 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1696 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1697 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1698 case CMD_tmenu: case CMD_tunmenu:
1699 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1700 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1701#endif
1702
1703 case CMD_colorscheme:
1704 xp->xp_context = EXPAND_COLORS;
1705 xp->xp_pattern = arg;
1706 break;
1707
1708 case CMD_compiler:
1709 xp->xp_context = EXPAND_COMPILER;
1710 xp->xp_pattern = arg;
1711 break;
1712
1713 case CMD_ownsyntax:
1714 xp->xp_context = EXPAND_OWNSYNTAX;
1715 xp->xp_pattern = arg;
1716 break;
1717
1718 case CMD_setfiletype:
1719 xp->xp_context = EXPAND_FILETYPE;
1720 xp->xp_pattern = arg;
1721 break;
1722
1723 case CMD_packadd:
1724 xp->xp_context = EXPAND_PACKADD;
1725 xp->xp_pattern = arg;
1726 break;
1727
1728#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1729 case CMD_language:
1730 p = skiptowhite(arg);
1731 if (*p == NUL)
1732 {
1733 xp->xp_context = EXPAND_LANGUAGE;
1734 xp->xp_pattern = arg;
1735 }
1736 else
1737 {
1738 if ( STRNCMP(arg, "messages", p - arg) == 0
1739 || STRNCMP(arg, "ctype", p - arg) == 0
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001740 || STRNCMP(arg, "time", p - arg) == 0
1741 || STRNCMP(arg, "collate", p - arg) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001742 {
1743 xp->xp_context = EXPAND_LOCALES;
1744 xp->xp_pattern = skipwhite(p);
1745 }
1746 else
1747 xp->xp_context = EXPAND_NOTHING;
1748 }
1749 break;
1750#endif
1751#if defined(FEAT_PROFILE)
1752 case CMD_profile:
1753 set_context_in_profile_cmd(xp, arg);
1754 break;
1755#endif
1756 case CMD_behave:
1757 xp->xp_context = EXPAND_BEHAVE;
1758 xp->xp_pattern = arg;
1759 break;
1760
1761 case CMD_messages:
1762 xp->xp_context = EXPAND_MESSAGES;
1763 xp->xp_pattern = arg;
1764 break;
1765
1766 case CMD_history:
1767 xp->xp_context = EXPAND_HISTORY;
1768 xp->xp_pattern = arg;
1769 break;
1770#if defined(FEAT_PROFILE)
1771 case CMD_syntime:
1772 xp->xp_context = EXPAND_SYNTIME;
1773 xp->xp_pattern = arg;
1774 break;
1775#endif
1776
1777 case CMD_argdelete:
1778 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1779 arg = xp->xp_pattern + 1;
1780 xp->xp_context = EXPAND_ARGLIST;
1781 xp->xp_pattern = arg;
1782 break;
1783
1784 default:
1785 break;
1786 }
1787 return NULL;
1788}
1789
Bram Moolenaar66b51422019-08-18 21:44:12 +02001790 void
1791set_cmd_context(
1792 expand_T *xp,
1793 char_u *str, // start of command line
1794 int len, // length of command line (excl. NUL)
1795 int col, // position of cursor
1796 int use_ccline UNUSED) // use ccline for info
1797{
1798#ifdef FEAT_EVAL
1799 cmdline_info_T *ccline = get_cmdline_info();
1800#endif
1801 int old_char = NUL;
1802 char_u *nextcomm;
1803
1804 // Avoid a UMR warning from Purify, only save the character if it has been
1805 // written before.
1806 if (col < len)
1807 old_char = str[col];
1808 str[col] = NUL;
1809 nextcomm = str;
1810
1811#ifdef FEAT_EVAL
1812 if (use_ccline && ccline->cmdfirstc == '=')
1813 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001814 // pass CMD_SIZE because there is no real command
1815 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001816 }
1817 else if (use_ccline && ccline->input_fn)
1818 {
1819 xp->xp_context = ccline->xp_context;
1820 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001821 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001822 }
1823 else
1824#endif
1825 while (nextcomm != NULL)
1826 nextcomm = set_one_cmd_context(xp, nextcomm);
1827
1828 // Store the string here so that call_user_expand_func() can get to them
1829 // easily.
1830 xp->xp_line = str;
1831 xp->xp_col = col;
1832
1833 str[col] = old_char;
1834}
1835
1836/*
1837 * Expand the command line "str" from context "xp".
1838 * "xp" must have been set by set_cmd_context().
1839 * xp->xp_pattern points into "str", to where the text that is to be expanded
1840 * starts.
1841 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
1842 * cursor.
1843 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
1844 * key that triggered expansion literally.
1845 * Returns EXPAND_OK otherwise.
1846 */
1847 int
1848expand_cmdline(
1849 expand_T *xp,
1850 char_u *str, // start of command line
1851 int col, // position of cursor
1852 int *matchcount, // return: nr of matches
1853 char_u ***matches) // return: array of pointers to matches
1854{
1855 char_u *file_str = NULL;
1856 int options = WILD_ADD_SLASH|WILD_SILENT;
1857
1858 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
1859 {
1860 beep_flush();
1861 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
1862 }
1863 if (xp->xp_context == EXPAND_NOTHING)
1864 {
1865 // Caller can use the character as a normal char instead
1866 return EXPAND_NOTHING;
1867 }
1868
1869 // add star to file name, or convert to regexp if not exp. files.
1870 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
1871 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
1872 if (file_str == NULL)
1873 return EXPAND_UNSUCCESSFUL;
1874
1875 if (p_wic)
1876 options += WILD_ICASE;
1877
1878 // find all files that match the description
1879 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
1880 {
1881 *matchcount = 0;
1882 *matches = NULL;
1883 }
1884 vim_free(file_str);
1885
1886 return EXPAND_OK;
1887}
1888
1889#ifdef FEAT_MULTI_LANG
1890/*
1891 * Cleanup matches for help tags:
1892 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
1893 * tag matches it. Otherwise remove "@en" if "en" is the only language.
1894 */
1895 static void
1896cleanup_help_tags(int num_file, char_u **file)
1897{
1898 int i, j;
1899 int len;
1900 char_u buf[4];
1901 char_u *p = buf;
1902
1903 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
1904 {
1905 *p++ = '@';
1906 *p++ = p_hlg[0];
1907 *p++ = p_hlg[1];
1908 }
1909 *p = NUL;
1910
1911 for (i = 0; i < num_file; ++i)
1912 {
1913 len = (int)STRLEN(file[i]) - 3;
1914 if (len <= 0)
1915 continue;
1916 if (STRCMP(file[i] + len, "@en") == 0)
1917 {
1918 // Sorting on priority means the same item in another language may
1919 // be anywhere. Search all items for a match up to the "@en".
1920 for (j = 0; j < num_file; ++j)
1921 if (j != i && (int)STRLEN(file[j]) == len + 3
1922 && STRNCMP(file[i], file[j], len + 1) == 0)
1923 break;
1924 if (j == num_file)
1925 // item only exists with @en, remove it
1926 file[i][len] = NUL;
1927 }
1928 }
1929
1930 if (*buf != NUL)
1931 for (i = 0; i < num_file; ++i)
1932 {
1933 len = (int)STRLEN(file[i]) - 3;
1934 if (len <= 0)
1935 continue;
1936 if (STRCMP(file[i] + len, buf) == 0)
1937 {
1938 // remove the default language
1939 file[i][len] = NUL;
1940 }
1941 }
1942}
1943#endif
1944
1945/*
Bram Moolenaard0190392019-08-23 21:17:35 +02001946 * Function given to ExpandGeneric() to obtain the possible arguments of the
1947 * ":behave {mswin,xterm}" command.
1948 */
1949 static char_u *
1950get_behave_arg(expand_T *xp UNUSED, int idx)
1951{
1952 if (idx == 0)
1953 return (char_u *)"mswin";
1954 if (idx == 1)
1955 return (char_u *)"xterm";
1956 return NULL;
1957}
1958
1959/*
1960 * Function given to ExpandGeneric() to obtain the possible arguments of the
1961 * ":messages {clear}" command.
1962 */
1963 static char_u *
1964get_messages_arg(expand_T *xp UNUSED, int idx)
1965{
1966 if (idx == 0)
1967 return (char_u *)"clear";
1968 return NULL;
1969}
1970
1971 static char_u *
1972get_mapclear_arg(expand_T *xp UNUSED, int idx)
1973{
1974 if (idx == 0)
1975 return (char_u *)"<buffer>";
1976 return NULL;
1977}
1978
1979/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001980 * Do the expansion based on xp->xp_context and "pat".
1981 */
1982 static int
1983ExpandFromContext(
1984 expand_T *xp,
1985 char_u *pat,
1986 int *num_file,
1987 char_u ***file,
1988 int options) // WILD_ flags
1989{
Bram Moolenaar66b51422019-08-18 21:44:12 +02001990 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001991 int ret;
1992 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001993 char_u *tofree = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001994
1995 flags = EW_DIR; // include directories
1996 if (options & WILD_LIST_NOTFOUND)
1997 flags |= EW_NOTFOUND;
1998 if (options & WILD_ADD_SLASH)
1999 flags |= EW_ADDSLASH;
2000 if (options & WILD_KEEP_ALL)
2001 flags |= EW_KEEPALL;
2002 if (options & WILD_SILENT)
2003 flags |= EW_SILENT;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02002004 if (options & WILD_NOERROR)
2005 flags |= EW_NOERROR;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002006 if (options & WILD_ALLLINKS)
2007 flags |= EW_ALLLINKS;
2008
2009 if (xp->xp_context == EXPAND_FILES
2010 || xp->xp_context == EXPAND_DIRECTORIES
2011 || xp->xp_context == EXPAND_FILES_IN_PATH)
2012 {
2013 // Expand file or directory names.
2014 int free_pat = FALSE;
2015 int i;
2016
2017 // for ":set path=" and ":set tags=" halve backslashes for escaped
2018 // space
2019 if (xp->xp_backslash != XP_BS_NONE)
2020 {
2021 free_pat = TRUE;
2022 pat = vim_strsave(pat);
2023 for (i = 0; pat[i]; ++i)
2024 if (pat[i] == '\\')
2025 {
2026 if (xp->xp_backslash == XP_BS_THREE
2027 && pat[i + 1] == '\\'
2028 && pat[i + 2] == '\\'
2029 && pat[i + 3] == ' ')
2030 STRMOVE(pat + i, pat + i + 3);
2031 if (xp->xp_backslash == XP_BS_ONE
2032 && pat[i + 1] == ' ')
2033 STRMOVE(pat + i, pat + i + 1);
2034 }
2035 }
2036
2037 if (xp->xp_context == EXPAND_FILES)
2038 flags |= EW_FILE;
2039 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2040 flags |= (EW_FILE | EW_PATH);
2041 else
2042 flags = (flags | EW_DIR) & ~EW_FILE;
2043 if (options & WILD_ICASE)
2044 flags |= EW_ICASE;
2045
2046 // Expand wildcards, supporting %:h and the like.
2047 ret = expand_wildcards_eval(&pat, num_file, file, flags);
2048 if (free_pat)
2049 vim_free(pat);
2050#ifdef BACKSLASH_IN_FILENAME
2051 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2052 {
2053 int i;
2054
2055 for (i = 0; i < *num_file; ++i)
2056 {
2057 char_u *ptr = (*file)[i];
2058
2059 while (*ptr != NUL)
2060 {
2061 if (p_csl[0] == 's' && *ptr == '\\')
2062 *ptr = '/';
2063 else if (p_csl[0] == 'b' && *ptr == '/')
2064 *ptr = '\\';
2065 ptr += (*mb_ptr2len)(ptr);
2066 }
2067 }
2068 }
2069#endif
2070 return ret;
2071 }
2072
2073 *file = (char_u **)"";
2074 *num_file = 0;
2075 if (xp->xp_context == EXPAND_HELP)
2076 {
2077 // With an empty argument we would get all the help tags, which is
2078 // very slow. Get matches for "help" instead.
2079 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
2080 num_file, file, FALSE) == OK)
2081 {
2082#ifdef FEAT_MULTI_LANG
2083 cleanup_help_tags(*num_file, *file);
2084#endif
2085 return OK;
2086 }
2087 return FAIL;
2088 }
2089
Bram Moolenaar66b51422019-08-18 21:44:12 +02002090 if (xp->xp_context == EXPAND_SHELLCMD)
2091 return expand_shellcmd(pat, num_file, file, flags);
2092 if (xp->xp_context == EXPAND_OLD_SETTING)
2093 return ExpandOldSetting(num_file, file);
2094 if (xp->xp_context == EXPAND_BUFFERS)
2095 return ExpandBufnames(pat, num_file, file, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002096#ifdef FEAT_DIFF
2097 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
2098 return ExpandBufnames(pat, num_file, file, options | BUF_DIFF_FILTER);
2099#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002100 if (xp->xp_context == EXPAND_TAGS
2101 || xp->xp_context == EXPAND_TAGS_LISTFILES)
2102 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
2103 if (xp->xp_context == EXPAND_COLORS)
2104 {
2105 char *directories[] = {"colors", NULL};
2106 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
2107 directories);
2108 }
2109 if (xp->xp_context == EXPAND_COMPILER)
2110 {
2111 char *directories[] = {"compiler", NULL};
2112 return ExpandRTDir(pat, 0, num_file, file, directories);
2113 }
2114 if (xp->xp_context == EXPAND_OWNSYNTAX)
2115 {
2116 char *directories[] = {"syntax", NULL};
2117 return ExpandRTDir(pat, 0, num_file, file, directories);
2118 }
2119 if (xp->xp_context == EXPAND_FILETYPE)
2120 {
2121 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
2122 return ExpandRTDir(pat, 0, num_file, file, directories);
2123 }
2124# if defined(FEAT_EVAL)
2125 if (xp->xp_context == EXPAND_USER_LIST)
2126 return ExpandUserList(xp, num_file, file);
2127# endif
2128 if (xp->xp_context == EXPAND_PACKADD)
2129 return ExpandPackAddDir(pat, num_file, file);
2130
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002131 // When expanding a function name starting with s:, match the <SNR>nr_
2132 // prefix.
2133 if (xp->xp_context == EXPAND_USER_FUNC && STRNCMP(pat, "^s:", 3) == 0)
2134 {
2135 int len = (int)STRLEN(pat) + 20;
2136
2137 tofree = alloc(len);
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002138 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002139 pat = tofree;
2140 }
2141
Bram Moolenaar66b51422019-08-18 21:44:12 +02002142 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2143 if (regmatch.regprog == NULL)
2144 return FAIL;
2145
2146 // set ignore-case according to p_ic, p_scs and pat
2147 regmatch.rm_ic = ignorecase(pat);
2148
2149 if (xp->xp_context == EXPAND_SETTINGS
2150 || xp->xp_context == EXPAND_BOOL_SETTINGS)
2151 ret = ExpandSettings(xp, &regmatch, num_file, file);
2152 else if (xp->xp_context == EXPAND_MAPPINGS)
2153 ret = ExpandMappings(&regmatch, num_file, file);
2154# if defined(FEAT_EVAL)
2155 else if (xp->xp_context == EXPAND_USER_DEFINED)
2156 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
2157# endif
2158 else
2159 {
2160 static struct expgen
2161 {
2162 int context;
2163 char_u *((*func)(expand_T *, int));
2164 int ic;
2165 int escaped;
2166 } tab[] =
2167 {
2168 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2169 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2170 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2171 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2172 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2173 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2174 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2175 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2176 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2177 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2178# ifdef FEAT_EVAL
2179 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2180 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2181 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2182 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2183# endif
2184# ifdef FEAT_MENU
2185 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2186 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2187# endif
2188# ifdef FEAT_SYN_HL
2189 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2190# endif
2191# ifdef FEAT_PROFILE
2192 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2193# endif
2194 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2195 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
2196 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
2197# ifdef FEAT_CSCOPE
2198 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2199# endif
2200# ifdef FEAT_SIGNS
2201 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2202# endif
2203# ifdef FEAT_PROFILE
2204 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2205# endif
2206# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2207 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2208 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2209# endif
2210 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2211 {EXPAND_USER, get_users, TRUE, FALSE},
2212 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2213 };
2214 int i;
2215
2216 // Find a context in the table and call the ExpandGeneric() with the
2217 // right function to do the expansion.
2218 ret = FAIL;
2219 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
2220 if (xp->xp_context == tab[i].context)
2221 {
2222 if (tab[i].ic)
2223 regmatch.rm_ic = TRUE;
2224 ret = ExpandGeneric(xp, &regmatch, num_file, file,
2225 tab[i].func, tab[i].escaped);
2226 break;
2227 }
2228 }
2229
2230 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002231 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002232
2233 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002234}
2235
Bram Moolenaar66b51422019-08-18 21:44:12 +02002236/*
2237 * Expand a list of names.
2238 *
2239 * Generic function for command line completion. It calls a function to
2240 * obtain strings, one by one. The strings are matched against a regexp
2241 * program. Matching strings are copied into an array, which is returned.
2242 *
2243 * Returns OK when no problems encountered, FAIL for error (out of memory).
2244 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002245 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002246ExpandGeneric(
2247 expand_T *xp,
2248 regmatch_T *regmatch,
2249 int *num_file,
2250 char_u ***file,
2251 char_u *((*func)(expand_T *, int)),
2252 // returns a string from the list
2253 int escaped)
2254{
2255 int i;
2256 int count = 0;
2257 int round;
2258 char_u *str;
2259
2260 // do this loop twice:
2261 // round == 0: count the number of matching names
2262 // round == 1: copy the matching names into allocated memory
2263 for (round = 0; round <= 1; ++round)
2264 {
2265 for (i = 0; ; ++i)
2266 {
2267 str = (*func)(xp, i);
2268 if (str == NULL) // end of list
2269 break;
2270 if (*str == NUL) // skip empty strings
2271 continue;
2272
2273 if (vim_regexec(regmatch, str, (colnr_T)0))
2274 {
2275 if (round)
2276 {
2277 if (escaped)
2278 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2279 else
2280 str = vim_strsave(str);
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002281 if (str == NULL)
2282 {
2283 FreeWild(count, *file);
2284 *num_file = 0;
2285 *file = NULL;
2286 return FAIL;
2287 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002288 (*file)[count] = str;
2289# ifdef FEAT_MENU
2290 if (func == get_menu_names && str != NULL)
2291 {
2292 // test for separator added by get_menu_names()
2293 str += STRLEN(str) - 1;
2294 if (*str == '\001')
2295 *str = '.';
2296 }
2297# endif
2298 }
2299 ++count;
2300 }
2301 }
2302 if (round == 0)
2303 {
2304 if (count == 0)
2305 return OK;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002306 *file = ALLOC_MULT(char_u *, count);
2307 if (*file == NULL)
2308 {
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002309 *num_file = 0;
2310 *file = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002311 return FAIL;
2312 }
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002313 *num_file = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002314 count = 0;
2315 }
2316 }
2317
2318 // Sort the results. Keep menu's in the specified order.
2319 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2320 {
2321 if (xp->xp_context == EXPAND_EXPRESSION
2322 || xp->xp_context == EXPAND_FUNCTIONS
2323 || xp->xp_context == EXPAND_USER_FUNC)
2324 // <SNR> functions should be sorted to the end.
2325 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
2326 sort_func_compare);
2327 else
2328 sort_strings(*file, *num_file);
2329 }
2330
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002331#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002332 // Reset the variables used for special highlight names expansion, so that
2333 // they don't show up when getting normal highlight names by ID.
2334 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002335#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002336 return OK;
2337}
2338
2339/*
2340 * Complete a shell command.
2341 * Returns FAIL or OK;
2342 */
2343 static int
2344expand_shellcmd(
2345 char_u *filepat, // pattern to match with command names
2346 int *num_file, // return: number of matches
2347 char_u ***file, // return: array with matches
2348 int flagsarg) // EW_ flags
2349{
2350 char_u *pat;
2351 int i;
2352 char_u *path = NULL;
2353 int mustfree = FALSE;
2354 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002355 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002356 size_t l;
2357 char_u *s, *e;
2358 int flags = flagsarg;
2359 int ret;
2360 int did_curdir = FALSE;
2361 hashtab_T found_ht;
2362 hashitem_T *hi;
2363 hash_T hash;
2364
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002365 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002366 if (buf == NULL)
2367 return FAIL;
2368
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002369 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002370 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002371 if (pat == NULL)
2372 {
2373 vim_free(buf);
2374 return FAIL;
2375 }
2376
Bram Moolenaar66b51422019-08-18 21:44:12 +02002377 for (i = 0; pat[i]; ++i)
2378 if (pat[i] == '\\' && pat[i + 1] == ' ')
2379 STRMOVE(pat + i, pat + i + 1);
2380
2381 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2382
2383 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2384 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2385 path = (char_u *)".";
2386 else
2387 {
2388 // For an absolute name we don't use $PATH.
2389 if (!mch_isFullName(pat))
2390 path = vim_getenv((char_u *)"PATH", &mustfree);
2391 if (path == NULL)
2392 path = (char_u *)"";
2393 }
2394
2395 // Go over all directories in $PATH. Expand matches in that directory and
2396 // collect them in "ga". When "." is not in $PATH also expand for the
2397 // current directory, to find "subdir/cmd".
2398 ga_init2(&ga, (int)sizeof(char *), 10);
2399 hash_init(&found_ht);
2400 for (s = path; ; s = e)
2401 {
2402# if defined(MSWIN)
2403 e = vim_strchr(s, ';');
2404# else
2405 e = vim_strchr(s, ':');
2406# endif
2407 if (e == NULL)
2408 e = s + STRLEN(s);
2409
2410 if (*s == NUL)
2411 {
2412 if (did_curdir)
2413 break;
2414 // Find directories in the current directory, path is empty.
2415 did_curdir = TRUE;
2416 flags |= EW_DIR;
2417 }
2418 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2419 {
2420 did_curdir = TRUE;
2421 flags |= EW_DIR;
2422 }
2423 else
2424 // Do not match directories inside a $PATH item.
2425 flags &= ~EW_DIR;
2426
2427 l = e - s;
2428 if (l > MAXPATHL - 5)
2429 break;
2430 vim_strncpy(buf, s, l);
2431 add_pathsep(buf);
2432 l = STRLEN(buf);
2433 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2434
2435 // Expand matches in one directory of $PATH.
2436 ret = expand_wildcards(1, &buf, num_file, file, flags);
2437 if (ret == OK)
2438 {
2439 if (ga_grow(&ga, *num_file) == FAIL)
2440 FreeWild(*num_file, *file);
2441 else
2442 {
2443 for (i = 0; i < *num_file; ++i)
2444 {
2445 char_u *name = (*file)[i];
2446
2447 if (STRLEN(name) > l)
2448 {
2449 // Check if this name was already found.
2450 hash = hash_hash(name + l);
2451 hi = hash_lookup(&found_ht, name + l, hash);
2452 if (HASHITEM_EMPTY(hi))
2453 {
2454 // Remove the path that was prepended.
2455 STRMOVE(name, name + l);
2456 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
2457 hash_add_item(&found_ht, hi, name, hash);
2458 name = NULL;
2459 }
2460 }
2461 vim_free(name);
2462 }
2463 vim_free(*file);
2464 }
2465 }
2466 if (*e != NUL)
2467 ++e;
2468 }
2469 *file = ga.ga_data;
2470 *num_file = ga.ga_len;
2471
2472 vim_free(buf);
2473 vim_free(pat);
2474 if (mustfree)
2475 vim_free(path);
2476 hash_clear(&found_ht);
2477 return OK;
2478}
2479
2480# if defined(FEAT_EVAL)
2481/*
2482 * Call "user_expand_func()" to invoke a user defined Vim script function and
2483 * return the result (either a string or a List).
2484 */
2485 static void *
2486call_user_expand_func(
2487 void *(*user_expand_func)(char_u *, int, typval_T *),
2488 expand_T *xp,
2489 int *num_file,
2490 char_u ***file)
2491{
2492 cmdline_info_T *ccline = get_cmdline_info();
2493 int keep = 0;
2494 typval_T args[4];
2495 sctx_T save_current_sctx = current_sctx;
2496 char_u *pat = NULL;
2497 void *ret;
2498
2499 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2500 return NULL;
2501 *num_file = 0;
2502 *file = NULL;
2503
2504 if (ccline->cmdbuff != NULL)
2505 {
2506 keep = ccline->cmdbuff[ccline->cmdlen];
2507 ccline->cmdbuff[ccline->cmdlen] = 0;
2508 }
2509
2510 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2511
2512 args[0].v_type = VAR_STRING;
2513 args[0].vval.v_string = pat;
2514 args[1].v_type = VAR_STRING;
2515 args[1].vval.v_string = xp->xp_line;
2516 args[2].v_type = VAR_NUMBER;
2517 args[2].vval.v_number = xp->xp_col;
2518 args[3].v_type = VAR_UNKNOWN;
2519
2520 current_sctx = xp->xp_script_ctx;
2521
2522 ret = user_expand_func(xp->xp_arg, 3, args);
2523
2524 current_sctx = save_current_sctx;
2525 if (ccline->cmdbuff != NULL)
2526 ccline->cmdbuff[ccline->cmdlen] = keep;
2527
2528 vim_free(pat);
2529 return ret;
2530}
2531
2532/*
2533 * Expand names with a function defined by the user.
2534 */
2535 static int
2536ExpandUserDefined(
2537 expand_T *xp,
2538 regmatch_T *regmatch,
2539 int *num_file,
2540 char_u ***file)
2541{
2542 char_u *retstr;
2543 char_u *s;
2544 char_u *e;
2545 int keep;
2546 garray_T ga;
2547 int skip;
2548
2549 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
2550 if (retstr == NULL)
2551 return FAIL;
2552
2553 ga_init2(&ga, (int)sizeof(char *), 3);
2554 for (s = retstr; *s != NUL; s = e)
2555 {
2556 e = vim_strchr(s, '\n');
2557 if (e == NULL)
2558 e = s + STRLEN(s);
2559 keep = *e;
2560 *e = NUL;
2561
2562 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2563 *e = keep;
2564
2565 if (!skip)
2566 {
2567 if (ga_grow(&ga, 1) == FAIL)
2568 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02002569 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002570 ++ga.ga_len;
2571 }
2572
2573 if (*e != NUL)
2574 ++e;
2575 }
2576 vim_free(retstr);
2577 *file = ga.ga_data;
2578 *num_file = ga.ga_len;
2579 return OK;
2580}
2581
2582/*
2583 * Expand names with a list returned by a function defined by the user.
2584 */
2585 static int
2586ExpandUserList(
2587 expand_T *xp,
2588 int *num_file,
2589 char_u ***file)
2590{
2591 list_T *retlist;
2592 listitem_T *li;
2593 garray_T ga;
2594
2595 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
2596 if (retlist == NULL)
2597 return FAIL;
2598
2599 ga_init2(&ga, (int)sizeof(char *), 3);
2600 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002601 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002602 {
2603 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
2604 continue; // Skip non-string items and empty strings
2605
2606 if (ga_grow(&ga, 1) == FAIL)
2607 break;
2608
2609 ((char_u **)ga.ga_data)[ga.ga_len] =
2610 vim_strsave(li->li_tv.vval.v_string);
2611 ++ga.ga_len;
2612 }
2613 list_unref(retlist);
2614
2615 *file = ga.ga_data;
2616 *num_file = ga.ga_len;
2617 return OK;
2618}
2619# endif
2620
2621/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002622 * Expand "file" for all comma-separated directories in "path".
2623 * Adds the matches to "ga". Caller must init "ga".
2624 */
2625 void
2626globpath(
2627 char_u *path,
2628 char_u *file,
2629 garray_T *ga,
2630 int expand_options)
2631{
2632 expand_T xpc;
2633 char_u *buf;
2634 int i;
2635 int num_p;
2636 char_u **p;
2637
2638 buf = alloc(MAXPATHL);
2639 if (buf == NULL)
2640 return;
2641
2642 ExpandInit(&xpc);
2643 xpc.xp_context = EXPAND_FILES;
2644
2645 // Loop over all entries in {path}.
2646 while (*path != NUL)
2647 {
2648 // Copy one item of the path to buf[] and concatenate the file name.
2649 copy_option_part(&path, buf, MAXPATHL, ",");
2650 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
2651 {
2652# if defined(MSWIN)
2653 // Using the platform's path separator (\) makes vim incorrectly
2654 // treat it as an escape character, use '/' instead.
2655 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
2656 STRCAT(buf, "/");
2657# else
2658 add_pathsep(buf);
2659# endif
2660 STRCAT(buf, file);
2661 if (ExpandFromContext(&xpc, buf, &num_p, &p,
2662 WILD_SILENT|expand_options) != FAIL && num_p > 0)
2663 {
2664 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
2665
2666 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002667 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02002668 for (i = 0; i < num_p; ++i)
2669 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002670 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02002671 ++ga->ga_len;
2672 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002673 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002674 }
2675 }
2676 }
2677
2678 vim_free(buf);
2679}
Bram Moolenaar66b51422019-08-18 21:44:12 +02002680
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002681#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002682/*
2683 * "getcompletion()" function
2684 */
2685 void
2686f_getcompletion(typval_T *argvars, typval_T *rettv)
2687{
2688 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002689 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002690 expand_T xpc;
2691 int filtered = FALSE;
2692 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002693 | WILD_NO_BEEP;
2694
2695 if (argvars[1].v_type != VAR_STRING)
2696 {
2697 semsg(_(e_invarg2), "type must be a string");
2698 return;
2699 }
2700 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002701
2702 if (argvars[2].v_type != VAR_UNKNOWN)
2703 filtered = tv_get_number_chk(&argvars[2], NULL);
2704
2705 if (p_wic)
2706 options |= WILD_ICASE;
2707
2708 // For filtered results, 'wildignore' is used
2709 if (!filtered)
2710 options |= WILD_KEEP_ALL;
2711
2712 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002713 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002714 {
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002715 set_one_cmd_context(&xpc, tv_get_string(&argvars[0]));
2716 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002717 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002718 else
2719 {
2720 xpc.xp_pattern = tv_get_string(&argvars[0]);
2721 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2722
2723 xpc.xp_context = cmdcomplete_str_to_type(type);
2724 if (xpc.xp_context == EXPAND_NOTHING)
2725 {
2726 semsg(_(e_invarg2), type);
2727 return;
2728 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002729
2730# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002731 if (xpc.xp_context == EXPAND_MENUS)
2732 {
2733 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
2734 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2735 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002736# endif
2737# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002738 if (xpc.xp_context == EXPAND_CSCOPE)
2739 {
2740 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
2741 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2742 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002743# endif
2744# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002745 if (xpc.xp_context == EXPAND_SIGN)
2746 {
2747 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
2748 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2749 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002750# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002751 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002752
2753 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
2754 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
2755 {
2756 int i;
2757
2758 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
2759
2760 for (i = 0; i < xpc.xp_numfiles; i++)
2761 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2762 }
2763 vim_free(pat);
2764 ExpandCleanup(&xpc);
2765}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002766#endif // FEAT_EVAL