blob: d92366c61e75b5d07e6132cf83cd2195a05e760b [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
1890#ifdef FEAT_MULTI_LANG
1891/*
1892 * Cleanup matches for help tags:
1893 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
1894 * tag matches it. Otherwise remove "@en" if "en" is the only language.
1895 */
1896 static void
1897cleanup_help_tags(int num_file, char_u **file)
1898{
1899 int i, j;
1900 int len;
1901 char_u buf[4];
1902 char_u *p = buf;
1903
1904 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
1905 {
1906 *p++ = '@';
1907 *p++ = p_hlg[0];
1908 *p++ = p_hlg[1];
1909 }
1910 *p = NUL;
1911
1912 for (i = 0; i < num_file; ++i)
1913 {
1914 len = (int)STRLEN(file[i]) - 3;
1915 if (len <= 0)
1916 continue;
1917 if (STRCMP(file[i] + len, "@en") == 0)
1918 {
1919 // Sorting on priority means the same item in another language may
1920 // be anywhere. Search all items for a match up to the "@en".
1921 for (j = 0; j < num_file; ++j)
1922 if (j != i && (int)STRLEN(file[j]) == len + 3
1923 && STRNCMP(file[i], file[j], len + 1) == 0)
1924 break;
1925 if (j == num_file)
1926 // item only exists with @en, remove it
1927 file[i][len] = NUL;
1928 }
1929 }
1930
1931 if (*buf != NUL)
1932 for (i = 0; i < num_file; ++i)
1933 {
1934 len = (int)STRLEN(file[i]) - 3;
1935 if (len <= 0)
1936 continue;
1937 if (STRCMP(file[i] + len, buf) == 0)
1938 {
1939 // remove the default language
1940 file[i][len] = NUL;
1941 }
1942 }
1943}
1944#endif
1945
1946/*
Bram Moolenaard0190392019-08-23 21:17:35 +02001947 * Function given to ExpandGeneric() to obtain the possible arguments of the
1948 * ":behave {mswin,xterm}" command.
1949 */
1950 static char_u *
1951get_behave_arg(expand_T *xp UNUSED, int idx)
1952{
1953 if (idx == 0)
1954 return (char_u *)"mswin";
1955 if (idx == 1)
1956 return (char_u *)"xterm";
1957 return NULL;
1958}
1959
1960/*
1961 * Function given to ExpandGeneric() to obtain the possible arguments of the
1962 * ":messages {clear}" command.
1963 */
1964 static char_u *
1965get_messages_arg(expand_T *xp UNUSED, int idx)
1966{
1967 if (idx == 0)
1968 return (char_u *)"clear";
1969 return NULL;
1970}
1971
1972 static char_u *
1973get_mapclear_arg(expand_T *xp UNUSED, int idx)
1974{
1975 if (idx == 0)
1976 return (char_u *)"<buffer>";
1977 return NULL;
1978}
1979
1980/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001981 * Do the expansion based on xp->xp_context and "pat".
1982 */
1983 static int
1984ExpandFromContext(
1985 expand_T *xp,
1986 char_u *pat,
1987 int *num_file,
1988 char_u ***file,
1989 int options) // WILD_ flags
1990{
Bram Moolenaar66b51422019-08-18 21:44:12 +02001991 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001992 int ret;
1993 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001994 char_u *tofree = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001995
1996 flags = EW_DIR; // include directories
1997 if (options & WILD_LIST_NOTFOUND)
1998 flags |= EW_NOTFOUND;
1999 if (options & WILD_ADD_SLASH)
2000 flags |= EW_ADDSLASH;
2001 if (options & WILD_KEEP_ALL)
2002 flags |= EW_KEEPALL;
2003 if (options & WILD_SILENT)
2004 flags |= EW_SILENT;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02002005 if (options & WILD_NOERROR)
2006 flags |= EW_NOERROR;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002007 if (options & WILD_ALLLINKS)
2008 flags |= EW_ALLLINKS;
2009
2010 if (xp->xp_context == EXPAND_FILES
2011 || xp->xp_context == EXPAND_DIRECTORIES
2012 || xp->xp_context == EXPAND_FILES_IN_PATH)
2013 {
2014 // Expand file or directory names.
2015 int free_pat = FALSE;
2016 int i;
2017
2018 // for ":set path=" and ":set tags=" halve backslashes for escaped
2019 // space
2020 if (xp->xp_backslash != XP_BS_NONE)
2021 {
2022 free_pat = TRUE;
2023 pat = vim_strsave(pat);
2024 for (i = 0; pat[i]; ++i)
2025 if (pat[i] == '\\')
2026 {
2027 if (xp->xp_backslash == XP_BS_THREE
2028 && pat[i + 1] == '\\'
2029 && pat[i + 2] == '\\'
2030 && pat[i + 3] == ' ')
2031 STRMOVE(pat + i, pat + i + 3);
2032 if (xp->xp_backslash == XP_BS_ONE
2033 && pat[i + 1] == ' ')
2034 STRMOVE(pat + i, pat + i + 1);
2035 }
2036 }
2037
2038 if (xp->xp_context == EXPAND_FILES)
2039 flags |= EW_FILE;
2040 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2041 flags |= (EW_FILE | EW_PATH);
2042 else
2043 flags = (flags | EW_DIR) & ~EW_FILE;
2044 if (options & WILD_ICASE)
2045 flags |= EW_ICASE;
2046
2047 // Expand wildcards, supporting %:h and the like.
2048 ret = expand_wildcards_eval(&pat, num_file, file, flags);
2049 if (free_pat)
2050 vim_free(pat);
2051#ifdef BACKSLASH_IN_FILENAME
2052 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2053 {
2054 int i;
2055
2056 for (i = 0; i < *num_file; ++i)
2057 {
2058 char_u *ptr = (*file)[i];
2059
2060 while (*ptr != NUL)
2061 {
2062 if (p_csl[0] == 's' && *ptr == '\\')
2063 *ptr = '/';
2064 else if (p_csl[0] == 'b' && *ptr == '/')
2065 *ptr = '\\';
2066 ptr += (*mb_ptr2len)(ptr);
2067 }
2068 }
2069 }
2070#endif
2071 return ret;
2072 }
2073
2074 *file = (char_u **)"";
2075 *num_file = 0;
2076 if (xp->xp_context == EXPAND_HELP)
2077 {
2078 // With an empty argument we would get all the help tags, which is
2079 // very slow. Get matches for "help" instead.
2080 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
2081 num_file, file, FALSE) == OK)
2082 {
2083#ifdef FEAT_MULTI_LANG
2084 cleanup_help_tags(*num_file, *file);
2085#endif
2086 return OK;
2087 }
2088 return FAIL;
2089 }
2090
Bram Moolenaar66b51422019-08-18 21:44:12 +02002091 if (xp->xp_context == EXPAND_SHELLCMD)
2092 return expand_shellcmd(pat, num_file, file, flags);
2093 if (xp->xp_context == EXPAND_OLD_SETTING)
2094 return ExpandOldSetting(num_file, file);
2095 if (xp->xp_context == EXPAND_BUFFERS)
2096 return ExpandBufnames(pat, num_file, file, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002097#ifdef FEAT_DIFF
2098 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
2099 return ExpandBufnames(pat, num_file, file, options | BUF_DIFF_FILTER);
2100#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002101 if (xp->xp_context == EXPAND_TAGS
2102 || xp->xp_context == EXPAND_TAGS_LISTFILES)
2103 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
2104 if (xp->xp_context == EXPAND_COLORS)
2105 {
2106 char *directories[] = {"colors", NULL};
2107 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
2108 directories);
2109 }
2110 if (xp->xp_context == EXPAND_COMPILER)
2111 {
2112 char *directories[] = {"compiler", NULL};
2113 return ExpandRTDir(pat, 0, num_file, file, directories);
2114 }
2115 if (xp->xp_context == EXPAND_OWNSYNTAX)
2116 {
2117 char *directories[] = {"syntax", NULL};
2118 return ExpandRTDir(pat, 0, num_file, file, directories);
2119 }
2120 if (xp->xp_context == EXPAND_FILETYPE)
2121 {
2122 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
2123 return ExpandRTDir(pat, 0, num_file, file, directories);
2124 }
2125# if defined(FEAT_EVAL)
2126 if (xp->xp_context == EXPAND_USER_LIST)
2127 return ExpandUserList(xp, num_file, file);
2128# endif
2129 if (xp->xp_context == EXPAND_PACKADD)
2130 return ExpandPackAddDir(pat, num_file, file);
2131
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002132 // When expanding a function name starting with s:, match the <SNR>nr_
2133 // prefix.
2134 if (xp->xp_context == EXPAND_USER_FUNC && STRNCMP(pat, "^s:", 3) == 0)
2135 {
2136 int len = (int)STRLEN(pat) + 20;
2137
2138 tofree = alloc(len);
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002139 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002140 pat = tofree;
2141 }
2142
Bram Moolenaar66b51422019-08-18 21:44:12 +02002143 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2144 if (regmatch.regprog == NULL)
2145 return FAIL;
2146
2147 // set ignore-case according to p_ic, p_scs and pat
2148 regmatch.rm_ic = ignorecase(pat);
2149
2150 if (xp->xp_context == EXPAND_SETTINGS
2151 || xp->xp_context == EXPAND_BOOL_SETTINGS)
2152 ret = ExpandSettings(xp, &regmatch, num_file, file);
2153 else if (xp->xp_context == EXPAND_MAPPINGS)
2154 ret = ExpandMappings(&regmatch, num_file, file);
2155# if defined(FEAT_EVAL)
2156 else if (xp->xp_context == EXPAND_USER_DEFINED)
2157 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
2158# endif
2159 else
2160 {
2161 static struct expgen
2162 {
2163 int context;
2164 char_u *((*func)(expand_T *, int));
2165 int ic;
2166 int escaped;
2167 } tab[] =
2168 {
2169 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2170 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2171 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2172 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2173 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2174 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2175 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2176 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2177 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2178 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2179# ifdef FEAT_EVAL
2180 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2181 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2182 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2183 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2184# endif
2185# ifdef FEAT_MENU
2186 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2187 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2188# endif
2189# ifdef FEAT_SYN_HL
2190 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2191# endif
2192# ifdef FEAT_PROFILE
2193 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2194# endif
2195 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2196 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
2197 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
2198# ifdef FEAT_CSCOPE
2199 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2200# endif
2201# ifdef FEAT_SIGNS
2202 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2203# endif
2204# ifdef FEAT_PROFILE
2205 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2206# endif
2207# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2208 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2209 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2210# endif
2211 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2212 {EXPAND_USER, get_users, TRUE, FALSE},
2213 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2214 };
2215 int i;
2216
2217 // Find a context in the table and call the ExpandGeneric() with the
2218 // right function to do the expansion.
2219 ret = FAIL;
2220 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
2221 if (xp->xp_context == tab[i].context)
2222 {
2223 if (tab[i].ic)
2224 regmatch.rm_ic = TRUE;
2225 ret = ExpandGeneric(xp, &regmatch, num_file, file,
2226 tab[i].func, tab[i].escaped);
2227 break;
2228 }
2229 }
2230
2231 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002232 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002233
2234 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002235}
2236
Bram Moolenaar66b51422019-08-18 21:44:12 +02002237/*
2238 * Expand a list of names.
2239 *
2240 * Generic function for command line completion. It calls a function to
2241 * obtain strings, one by one. The strings are matched against a regexp
2242 * program. Matching strings are copied into an array, which is returned.
2243 *
2244 * Returns OK when no problems encountered, FAIL for error (out of memory).
2245 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002246 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002247ExpandGeneric(
2248 expand_T *xp,
2249 regmatch_T *regmatch,
2250 int *num_file,
2251 char_u ***file,
2252 char_u *((*func)(expand_T *, int)),
2253 // returns a string from the list
2254 int escaped)
2255{
2256 int i;
2257 int count = 0;
2258 int round;
2259 char_u *str;
2260
2261 // do this loop twice:
2262 // round == 0: count the number of matching names
2263 // round == 1: copy the matching names into allocated memory
2264 for (round = 0; round <= 1; ++round)
2265 {
2266 for (i = 0; ; ++i)
2267 {
2268 str = (*func)(xp, i);
2269 if (str == NULL) // end of list
2270 break;
2271 if (*str == NUL) // skip empty strings
2272 continue;
2273
2274 if (vim_regexec(regmatch, str, (colnr_T)0))
2275 {
2276 if (round)
2277 {
2278 if (escaped)
2279 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2280 else
2281 str = vim_strsave(str);
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002282 if (str == NULL)
2283 {
2284 FreeWild(count, *file);
2285 *num_file = 0;
2286 *file = NULL;
2287 return FAIL;
2288 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002289 (*file)[count] = str;
2290# ifdef FEAT_MENU
2291 if (func == get_menu_names && str != NULL)
2292 {
2293 // test for separator added by get_menu_names()
2294 str += STRLEN(str) - 1;
2295 if (*str == '\001')
2296 *str = '.';
2297 }
2298# endif
2299 }
2300 ++count;
2301 }
2302 }
2303 if (round == 0)
2304 {
2305 if (count == 0)
2306 return OK;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002307 *file = ALLOC_MULT(char_u *, count);
2308 if (*file == NULL)
2309 {
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002310 *num_file = 0;
2311 *file = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002312 return FAIL;
2313 }
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002314 *num_file = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002315 count = 0;
2316 }
2317 }
2318
2319 // Sort the results. Keep menu's in the specified order.
2320 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2321 {
2322 if (xp->xp_context == EXPAND_EXPRESSION
2323 || xp->xp_context == EXPAND_FUNCTIONS
2324 || xp->xp_context == EXPAND_USER_FUNC)
2325 // <SNR> functions should be sorted to the end.
2326 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
2327 sort_func_compare);
2328 else
2329 sort_strings(*file, *num_file);
2330 }
2331
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002332#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002333 // Reset the variables used for special highlight names expansion, so that
2334 // they don't show up when getting normal highlight names by ID.
2335 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002336#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002337 return OK;
2338}
2339
2340/*
2341 * Complete a shell command.
2342 * Returns FAIL or OK;
2343 */
2344 static int
2345expand_shellcmd(
2346 char_u *filepat, // pattern to match with command names
2347 int *num_file, // return: number of matches
2348 char_u ***file, // return: array with matches
2349 int flagsarg) // EW_ flags
2350{
2351 char_u *pat;
2352 int i;
2353 char_u *path = NULL;
2354 int mustfree = FALSE;
2355 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002356 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002357 size_t l;
2358 char_u *s, *e;
2359 int flags = flagsarg;
2360 int ret;
2361 int did_curdir = FALSE;
2362 hashtab_T found_ht;
2363 hashitem_T *hi;
2364 hash_T hash;
2365
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002366 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002367 if (buf == NULL)
2368 return FAIL;
2369
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002370 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002371 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002372 if (pat == NULL)
2373 {
2374 vim_free(buf);
2375 return FAIL;
2376 }
2377
Bram Moolenaar66b51422019-08-18 21:44:12 +02002378 for (i = 0; pat[i]; ++i)
2379 if (pat[i] == '\\' && pat[i + 1] == ' ')
2380 STRMOVE(pat + i, pat + i + 1);
2381
2382 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2383
2384 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2385 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2386 path = (char_u *)".";
2387 else
2388 {
2389 // For an absolute name we don't use $PATH.
2390 if (!mch_isFullName(pat))
2391 path = vim_getenv((char_u *)"PATH", &mustfree);
2392 if (path == NULL)
2393 path = (char_u *)"";
2394 }
2395
2396 // Go over all directories in $PATH. Expand matches in that directory and
2397 // collect them in "ga". When "." is not in $PATH also expand for the
2398 // current directory, to find "subdir/cmd".
2399 ga_init2(&ga, (int)sizeof(char *), 10);
2400 hash_init(&found_ht);
2401 for (s = path; ; s = e)
2402 {
2403# if defined(MSWIN)
2404 e = vim_strchr(s, ';');
2405# else
2406 e = vim_strchr(s, ':');
2407# endif
2408 if (e == NULL)
2409 e = s + STRLEN(s);
2410
2411 if (*s == NUL)
2412 {
2413 if (did_curdir)
2414 break;
2415 // Find directories in the current directory, path is empty.
2416 did_curdir = TRUE;
2417 flags |= EW_DIR;
2418 }
2419 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2420 {
2421 did_curdir = TRUE;
2422 flags |= EW_DIR;
2423 }
2424 else
2425 // Do not match directories inside a $PATH item.
2426 flags &= ~EW_DIR;
2427
2428 l = e - s;
2429 if (l > MAXPATHL - 5)
2430 break;
2431 vim_strncpy(buf, s, l);
2432 add_pathsep(buf);
2433 l = STRLEN(buf);
2434 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2435
2436 // Expand matches in one directory of $PATH.
2437 ret = expand_wildcards(1, &buf, num_file, file, flags);
2438 if (ret == OK)
2439 {
2440 if (ga_grow(&ga, *num_file) == FAIL)
2441 FreeWild(*num_file, *file);
2442 else
2443 {
2444 for (i = 0; i < *num_file; ++i)
2445 {
2446 char_u *name = (*file)[i];
2447
2448 if (STRLEN(name) > l)
2449 {
2450 // Check if this name was already found.
2451 hash = hash_hash(name + l);
2452 hi = hash_lookup(&found_ht, name + l, hash);
2453 if (HASHITEM_EMPTY(hi))
2454 {
2455 // Remove the path that was prepended.
2456 STRMOVE(name, name + l);
2457 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
2458 hash_add_item(&found_ht, hi, name, hash);
2459 name = NULL;
2460 }
2461 }
2462 vim_free(name);
2463 }
2464 vim_free(*file);
2465 }
2466 }
2467 if (*e != NUL)
2468 ++e;
2469 }
2470 *file = ga.ga_data;
2471 *num_file = ga.ga_len;
2472
2473 vim_free(buf);
2474 vim_free(pat);
2475 if (mustfree)
2476 vim_free(path);
2477 hash_clear(&found_ht);
2478 return OK;
2479}
2480
2481# if defined(FEAT_EVAL)
2482/*
2483 * Call "user_expand_func()" to invoke a user defined Vim script function and
2484 * return the result (either a string or a List).
2485 */
2486 static void *
2487call_user_expand_func(
2488 void *(*user_expand_func)(char_u *, int, typval_T *),
2489 expand_T *xp,
2490 int *num_file,
2491 char_u ***file)
2492{
2493 cmdline_info_T *ccline = get_cmdline_info();
2494 int keep = 0;
2495 typval_T args[4];
2496 sctx_T save_current_sctx = current_sctx;
2497 char_u *pat = NULL;
2498 void *ret;
2499
2500 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2501 return NULL;
2502 *num_file = 0;
2503 *file = NULL;
2504
2505 if (ccline->cmdbuff != NULL)
2506 {
2507 keep = ccline->cmdbuff[ccline->cmdlen];
2508 ccline->cmdbuff[ccline->cmdlen] = 0;
2509 }
2510
2511 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2512
2513 args[0].v_type = VAR_STRING;
2514 args[0].vval.v_string = pat;
2515 args[1].v_type = VAR_STRING;
2516 args[1].vval.v_string = xp->xp_line;
2517 args[2].v_type = VAR_NUMBER;
2518 args[2].vval.v_number = xp->xp_col;
2519 args[3].v_type = VAR_UNKNOWN;
2520
2521 current_sctx = xp->xp_script_ctx;
2522
2523 ret = user_expand_func(xp->xp_arg, 3, args);
2524
2525 current_sctx = save_current_sctx;
2526 if (ccline->cmdbuff != NULL)
2527 ccline->cmdbuff[ccline->cmdlen] = keep;
2528
2529 vim_free(pat);
2530 return ret;
2531}
2532
2533/*
2534 * Expand names with a function defined by the user.
2535 */
2536 static int
2537ExpandUserDefined(
2538 expand_T *xp,
2539 regmatch_T *regmatch,
2540 int *num_file,
2541 char_u ***file)
2542{
2543 char_u *retstr;
2544 char_u *s;
2545 char_u *e;
2546 int keep;
2547 garray_T ga;
2548 int skip;
2549
2550 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
2551 if (retstr == NULL)
2552 return FAIL;
2553
2554 ga_init2(&ga, (int)sizeof(char *), 3);
2555 for (s = retstr; *s != NUL; s = e)
2556 {
2557 e = vim_strchr(s, '\n');
2558 if (e == NULL)
2559 e = s + STRLEN(s);
2560 keep = *e;
2561 *e = NUL;
2562
2563 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2564 *e = keep;
2565
2566 if (!skip)
2567 {
2568 if (ga_grow(&ga, 1) == FAIL)
2569 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02002570 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002571 ++ga.ga_len;
2572 }
2573
2574 if (*e != NUL)
2575 ++e;
2576 }
2577 vim_free(retstr);
2578 *file = ga.ga_data;
2579 *num_file = ga.ga_len;
2580 return OK;
2581}
2582
2583/*
2584 * Expand names with a list returned by a function defined by the user.
2585 */
2586 static int
2587ExpandUserList(
2588 expand_T *xp,
2589 int *num_file,
2590 char_u ***file)
2591{
2592 list_T *retlist;
2593 listitem_T *li;
2594 garray_T ga;
2595
2596 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
2597 if (retlist == NULL)
2598 return FAIL;
2599
2600 ga_init2(&ga, (int)sizeof(char *), 3);
2601 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002602 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002603 {
2604 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
2605 continue; // Skip non-string items and empty strings
2606
2607 if (ga_grow(&ga, 1) == FAIL)
2608 break;
2609
2610 ((char_u **)ga.ga_data)[ga.ga_len] =
2611 vim_strsave(li->li_tv.vval.v_string);
2612 ++ga.ga_len;
2613 }
2614 list_unref(retlist);
2615
2616 *file = ga.ga_data;
2617 *num_file = ga.ga_len;
2618 return OK;
2619}
2620# endif
2621
2622/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002623 * Expand "file" for all comma-separated directories in "path".
2624 * Adds the matches to "ga". Caller must init "ga".
2625 */
2626 void
2627globpath(
2628 char_u *path,
2629 char_u *file,
2630 garray_T *ga,
2631 int expand_options)
2632{
2633 expand_T xpc;
2634 char_u *buf;
2635 int i;
2636 int num_p;
2637 char_u **p;
2638
2639 buf = alloc(MAXPATHL);
2640 if (buf == NULL)
2641 return;
2642
2643 ExpandInit(&xpc);
2644 xpc.xp_context = EXPAND_FILES;
2645
2646 // Loop over all entries in {path}.
2647 while (*path != NUL)
2648 {
2649 // Copy one item of the path to buf[] and concatenate the file name.
2650 copy_option_part(&path, buf, MAXPATHL, ",");
2651 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
2652 {
2653# if defined(MSWIN)
2654 // Using the platform's path separator (\) makes vim incorrectly
2655 // treat it as an escape character, use '/' instead.
2656 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
2657 STRCAT(buf, "/");
2658# else
2659 add_pathsep(buf);
2660# endif
2661 STRCAT(buf, file);
2662 if (ExpandFromContext(&xpc, buf, &num_p, &p,
2663 WILD_SILENT|expand_options) != FAIL && num_p > 0)
2664 {
2665 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
2666
2667 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002668 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02002669 for (i = 0; i < num_p; ++i)
2670 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002671 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02002672 ++ga->ga_len;
2673 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002674 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002675 }
2676 }
2677 }
2678
2679 vim_free(buf);
2680}
Bram Moolenaar66b51422019-08-18 21:44:12 +02002681
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002682#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002683/*
2684 * "getcompletion()" function
2685 */
2686 void
2687f_getcompletion(typval_T *argvars, typval_T *rettv)
2688{
2689 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002690 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002691 expand_T xpc;
2692 int filtered = FALSE;
2693 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002694 | WILD_NO_BEEP;
2695
2696 if (argvars[1].v_type != VAR_STRING)
2697 {
2698 semsg(_(e_invarg2), "type must be a string");
2699 return;
2700 }
2701 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002702
2703 if (argvars[2].v_type != VAR_UNKNOWN)
2704 filtered = tv_get_number_chk(&argvars[2], NULL);
2705
2706 if (p_wic)
2707 options |= WILD_ICASE;
2708
2709 // For filtered results, 'wildignore' is used
2710 if (!filtered)
2711 options |= WILD_KEEP_ALL;
2712
2713 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002714 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002715 {
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002716 set_one_cmd_context(&xpc, tv_get_string(&argvars[0]));
2717 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002718 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002719 else
2720 {
2721 xpc.xp_pattern = tv_get_string(&argvars[0]);
2722 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2723
2724 xpc.xp_context = cmdcomplete_str_to_type(type);
2725 if (xpc.xp_context == EXPAND_NOTHING)
2726 {
2727 semsg(_(e_invarg2), type);
2728 return;
2729 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002730
2731# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002732 if (xpc.xp_context == EXPAND_MENUS)
2733 {
2734 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
2735 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2736 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002737# endif
2738# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002739 if (xpc.xp_context == EXPAND_CSCOPE)
2740 {
2741 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
2742 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2743 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002744# endif
2745# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002746 if (xpc.xp_context == EXPAND_SIGN)
2747 {
2748 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
2749 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2750 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002751# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002752 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002753
2754 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
2755 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
2756 {
2757 int i;
2758
2759 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
2760
2761 for (i = 0; i < xpc.xp_numfiles; i++)
2762 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2763 }
2764 vim_free(pat);
2765 ExpandCleanup(&xpc);
2766}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002767#endif // FEAT_EVAL