blob: 9b6d9aa81d3c256b0c365149861a3e5abeb5b69d [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{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200497 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200498 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200499 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200500}
501
502/*
503 * Cleanup an expand structure after use.
504 */
505 void
506ExpandCleanup(expand_T *xp)
507{
508 if (xp->xp_numfiles >= 0)
509 {
510 FreeWild(xp->xp_numfiles, xp->xp_files);
511 xp->xp_numfiles = -1;
512 }
513}
514
515/*
516 * Show all matches for completion on the command line.
517 * Returns EXPAND_NOTHING when the character that triggered expansion should
518 * be inserted like a normal character.
519 */
520 int
521showmatches(expand_T *xp, int wildmenu UNUSED)
522{
523 cmdline_info_T *ccline = get_cmdline_info();
524#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
525 int num_files;
526 char_u **files_found;
527 int i, j, k;
528 int maxlen;
529 int lines;
530 int columns;
531 char_u *p;
532 int lastlen;
533 int attr;
534 int showtail;
535
536 if (xp->xp_numfiles == -1)
537 {
538 set_expand_context(xp);
539 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
540 &num_files, &files_found);
541 showtail = expand_showtail(xp);
542 if (i != EXPAND_OK)
543 return i;
544
545 }
546 else
547 {
548 num_files = xp->xp_numfiles;
549 files_found = xp->xp_files;
550 showtail = cmd_showtail;
551 }
552
553#ifdef FEAT_WILDMENU
554 if (!wildmenu)
555 {
556#endif
557 msg_didany = FALSE; // lines_left will be set
558 msg_start(); // prepare for paging
559 msg_putchar('\n');
560 out_flush();
561 cmdline_row = msg_row;
562 msg_didany = FALSE; // lines_left will be set again
563 msg_start(); // prepare for paging
564#ifdef FEAT_WILDMENU
565 }
566#endif
567
568 if (got_int)
569 got_int = FALSE; // only int. the completion, not the cmd line
570#ifdef FEAT_WILDMENU
571 else if (wildmenu)
572 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
573#endif
574 else
575 {
576 // find the length of the longest file name
577 maxlen = 0;
578 for (i = 0; i < num_files; ++i)
579 {
580 if (!showtail && (xp->xp_context == EXPAND_FILES
581 || xp->xp_context == EXPAND_SHELLCMD
582 || xp->xp_context == EXPAND_BUFFERS))
583 {
584 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
585 j = vim_strsize(NameBuff);
586 }
587 else
588 j = vim_strsize(L_SHOWFILE(i));
589 if (j > maxlen)
590 maxlen = j;
591 }
592
593 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
594 lines = num_files;
595 else
596 {
597 // compute the number of columns and lines for the listing
598 maxlen += 2; // two spaces between file names
599 columns = ((int)Columns + 2) / maxlen;
600 if (columns < 1)
601 columns = 1;
602 lines = (num_files + columns - 1) / columns;
603 }
604
605 attr = HL_ATTR(HLF_D); // find out highlighting for directories
606
607 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
608 {
609 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
610 msg_clr_eos();
611 msg_advance(maxlen - 3);
612 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
613 }
614
615 // list the files line by line
616 for (i = 0; i < lines; ++i)
617 {
618 lastlen = 999;
619 for (k = i; k < num_files; k += lines)
620 {
621 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
622 {
623 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
624 p = files_found[k] + STRLEN(files_found[k]) + 1;
625 msg_advance(maxlen + 1);
626 msg_puts((char *)p);
627 msg_advance(maxlen + 3);
628 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
629 break;
630 }
631 for (j = maxlen - lastlen; --j >= 0; )
632 msg_putchar(' ');
633 if (xp->xp_context == EXPAND_FILES
634 || xp->xp_context == EXPAND_SHELLCMD
635 || xp->xp_context == EXPAND_BUFFERS)
636 {
637 // highlight directories
638 if (xp->xp_numfiles != -1)
639 {
640 char_u *halved_slash;
641 char_u *exp_path;
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200642 char_u *path;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200643
644 // Expansion was done before and special characters
645 // were escaped, need to halve backslashes. Also
646 // $HOME has been replaced with ~/.
647 exp_path = expand_env_save_opt(files_found[k], TRUE);
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200648 path = exp_path != NULL ? exp_path : files_found[k];
649 halved_slash = backslash_halve_save(path);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200650 j = mch_isdir(halved_slash != NULL ? halved_slash
651 : files_found[k]);
652 vim_free(exp_path);
Bram Moolenaarf1552d02019-08-21 12:54:18 +0200653 if (halved_slash != path)
654 vim_free(halved_slash);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200655 }
656 else
657 // Expansion was done here, file names are literal.
658 j = mch_isdir(files_found[k]);
659 if (showtail)
660 p = L_SHOWFILE(k);
661 else
662 {
663 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
664 TRUE);
665 p = NameBuff;
666 }
667 }
668 else
669 {
670 j = FALSE;
671 p = L_SHOWFILE(k);
672 }
673 lastlen = msg_outtrans_attr(p, j ? attr : 0);
674 }
675 if (msg_col > 0) // when not wrapped around
676 {
677 msg_clr_eos();
678 msg_putchar('\n');
679 }
680 out_flush(); // show one line at a time
681 if (got_int)
682 {
683 got_int = FALSE;
684 break;
685 }
686 }
687
688 // we redraw the command below the lines that we have just listed
689 // This is a bit tricky, but it saves a lot of screen updating.
690 cmdline_row = msg_row; // will put it back later
691 }
692
693 if (xp->xp_numfiles == -1)
694 FreeWild(num_files, files_found);
695
696 return EXPAND_OK;
697}
698
699/*
700 * Private gettail for showmatches() (and win_redr_status_matches()):
701 * Find tail of file name path, but ignore trailing "/".
702 */
703 char_u *
704sm_gettail(char_u *s)
705{
706 char_u *p;
707 char_u *t = s;
708 int had_sep = FALSE;
709
710 for (p = s; *p != NUL; )
711 {
712 if (vim_ispathsep(*p)
713#ifdef BACKSLASH_IN_FILENAME
714 && !rem_backslash(p)
715#endif
716 )
717 had_sep = TRUE;
718 else if (had_sep)
719 {
720 t = p;
721 had_sep = FALSE;
722 }
723 MB_PTR_ADV(p);
724 }
725 return t;
726}
727
728/*
729 * Return TRUE if we only need to show the tail of completion matches.
730 * When not completing file names or there is a wildcard in the path FALSE is
731 * returned.
732 */
733 static int
734expand_showtail(expand_T *xp)
735{
736 char_u *s;
737 char_u *end;
738
739 // When not completing file names a "/" may mean something different.
740 if (xp->xp_context != EXPAND_FILES
741 && xp->xp_context != EXPAND_SHELLCMD
742 && xp->xp_context != EXPAND_DIRECTORIES)
743 return FALSE;
744
745 end = gettail(xp->xp_pattern);
746 if (end == xp->xp_pattern) // there is no path separator
747 return FALSE;
748
749 for (s = xp->xp_pattern; s < end; s++)
750 {
751 // Skip escaped wildcards. Only when the backslash is not a path
752 // separator, on DOS the '*' "path\*\file" must not be skipped.
753 if (rem_backslash(s))
754 ++s;
755 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
756 return FALSE;
757 }
758 return TRUE;
759}
760
761/*
762 * Prepare a string for expansion.
763 * When expanding file names: The string will be used with expand_wildcards().
764 * Copy "fname[len]" into allocated memory and add a '*' at the end.
765 * When expanding other names: The string will be used with regcomp(). Copy
766 * the name into allocated memory and prepend "^".
767 */
768 char_u *
769addstar(
770 char_u *fname,
771 int len,
772 int context) // EXPAND_FILES etc.
773{
774 char_u *retval;
775 int i, j;
776 int new_len;
777 char_u *tail;
778 int ends_in_star;
779
780 if (context != EXPAND_FILES
781 && context != EXPAND_FILES_IN_PATH
782 && context != EXPAND_SHELLCMD
783 && context != EXPAND_DIRECTORIES)
784 {
785 // Matching will be done internally (on something other than files).
786 // So we convert the file-matching-type wildcards into our kind for
787 // use with vim_regcomp(). First work out how long it will be:
788
789 // For help tags the translation is done in find_help_tags().
790 // For a tag pattern starting with "/" no translation is needed.
791 if (context == EXPAND_HELP
792 || context == EXPAND_COLORS
793 || context == EXPAND_COMPILER
794 || context == EXPAND_OWNSYNTAX
795 || context == EXPAND_FILETYPE
796 || context == EXPAND_PACKADD
797 || ((context == EXPAND_TAGS_LISTFILES
798 || context == EXPAND_TAGS)
799 && fname[0] == '/'))
800 retval = vim_strnsave(fname, len);
801 else
802 {
803 new_len = len + 2; // +2 for '^' at start, NUL at end
804 for (i = 0; i < len; i++)
805 {
806 if (fname[i] == '*' || fname[i] == '~')
807 new_len++; // '*' needs to be replaced by ".*"
808 // '~' needs to be replaced by "\~"
809
810 // Buffer names are like file names. "." should be literal
811 if (context == EXPAND_BUFFERS && fname[i] == '.')
812 new_len++; // "." becomes "\."
813
814 // Custom expansion takes care of special things, match
815 // backslashes literally (perhaps also for other types?)
816 if ((context == EXPAND_USER_DEFINED
817 || context == EXPAND_USER_LIST) && fname[i] == '\\')
818 new_len++; // '\' becomes "\\"
819 }
820 retval = alloc(new_len);
821 if (retval != NULL)
822 {
823 retval[0] = '^';
824 j = 1;
825 for (i = 0; i < len; i++, j++)
826 {
827 // Skip backslash. But why? At least keep it for custom
828 // expansion.
829 if (context != EXPAND_USER_DEFINED
830 && context != EXPAND_USER_LIST
831 && fname[i] == '\\'
832 && ++i == len)
833 break;
834
835 switch (fname[i])
836 {
837 case '*': retval[j++] = '.';
838 break;
839 case '~': retval[j++] = '\\';
840 break;
841 case '?': retval[j] = '.';
842 continue;
843 case '.': if (context == EXPAND_BUFFERS)
844 retval[j++] = '\\';
845 break;
846 case '\\': if (context == EXPAND_USER_DEFINED
847 || context == EXPAND_USER_LIST)
848 retval[j++] = '\\';
849 break;
850 }
851 retval[j] = fname[i];
852 }
853 retval[j] = NUL;
854 }
855 }
856 }
857 else
858 {
859 retval = alloc(len + 4);
860 if (retval != NULL)
861 {
862 vim_strncpy(retval, fname, len);
863
864 // Don't add a star to *, ~, ~user, $var or `cmd`.
865 // * would become **, which walks the whole tree.
866 // ~ would be at the start of the file name, but not the tail.
867 // $ could be anywhere in the tail.
868 // ` could be anywhere in the file name.
869 // When the name ends in '$' don't add a star, remove the '$'.
870 tail = gettail(retval);
871 ends_in_star = (len > 0 && retval[len - 1] == '*');
872#ifndef BACKSLASH_IN_FILENAME
873 for (i = len - 2; i >= 0; --i)
874 {
875 if (retval[i] != '\\')
876 break;
877 ends_in_star = !ends_in_star;
878 }
879#endif
880 if ((*retval != '~' || tail != retval)
881 && !ends_in_star
882 && vim_strchr(tail, '$') == NULL
883 && vim_strchr(retval, '`') == NULL)
884 retval[len++] = '*';
885 else if (len > 0 && retval[len - 1] == '$')
886 --len;
887 retval[len] = NUL;
888 }
889 }
890 return retval;
891}
892
893/*
894 * Must parse the command line so far to work out what context we are in.
895 * Completion can then be done based on that context.
896 * This routine sets the variables:
897 * xp->xp_pattern The start of the pattern to be expanded within
898 * the command line (ends at the cursor).
899 * xp->xp_context The type of thing to expand. Will be one of:
900 *
901 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
902 * the command line, like an unknown command. Caller
903 * should beep.
904 * EXPAND_NOTHING Unrecognised context for completion, use char like
905 * a normal char, rather than for completion. eg
906 * :s/^I/
907 * EXPAND_COMMANDS Cursor is still touching the command, so complete
908 * it.
909 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
910 * EXPAND_FILES After command with EX_XFILE set, or after setting
911 * with P_EXPAND set. eg :e ^I, :w>>^I
912 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
913 * when we know only directories are of interest. eg
914 * :set dir=^I
915 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
916 * EXPAND_SETTINGS Complete variable names. eg :set d^I
917 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
918 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
919 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
920 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
921 * EXPAND_EVENTS Complete event names
922 * EXPAND_SYNTAX Complete :syntax command arguments
923 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
924 * EXPAND_AUGROUP Complete autocommand group names
925 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
926 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
927 * eg :unmap a^I , :cunab x^I
928 * EXPAND_FUNCTIONS Complete internal or user defined function names,
929 * eg :call sub^I
930 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
931 * EXPAND_EXPRESSION Complete internal or user defined function/variable
932 * names in expressions, eg :while s^I
933 * EXPAND_ENV_VARS Complete environment variable names
934 * EXPAND_USER Complete user names
935 */
936 static void
937set_expand_context(expand_T *xp)
938{
939 cmdline_info_T *ccline = get_cmdline_info();
940
941 // only expansion for ':', '>' and '=' command-lines
942 if (ccline->cmdfirstc != ':'
943#ifdef FEAT_EVAL
944 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
945 && !ccline->input_fn
946#endif
947 )
948 {
949 xp->xp_context = EXPAND_NOTHING;
950 return;
951 }
952 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
953}
954
Bram Moolenaard0190392019-08-23 21:17:35 +0200955/*
956 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
957 * we don't need/want deleted. Maybe this could be done better if we didn't
958 * repeat all this stuff. The only problem is that they may not stay
959 * perfectly compatible with each other, but then the command line syntax
960 * probably won't change that much -- webb.
961 */
962 static char_u *
963set_one_cmd_context(
964 expand_T *xp,
965 char_u *buff) // buffer for command string
966{
967 char_u *p;
968 char_u *cmd, *arg;
969 int len = 0;
970 exarg_T ea;
971 int compl = EXPAND_NOTHING;
972 int delim;
973 int forceit = FALSE;
974 int usefilter = FALSE; // filter instead of file name
975
976 ExpandInit(xp);
977 xp->xp_pattern = buff;
978 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
979 ea.argt = 0;
980
981 // 1. skip comment lines and leading space, colons or bars
982 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
983 ;
984 xp->xp_pattern = cmd;
985
986 if (*cmd == NUL)
987 return NULL;
988 if (*cmd == '"') // ignore comment lines
989 {
990 xp->xp_context = EXPAND_NOTHING;
991 return NULL;
992 }
993
994 // 3. Skip over the range to find the command.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200995 cmd = skip_range(cmd, TRUE, &xp->xp_context);
Bram Moolenaard0190392019-08-23 21:17:35 +0200996 xp->xp_pattern = cmd;
997 if (*cmd == NUL)
998 return NULL;
999 if (*cmd == '"')
1000 {
1001 xp->xp_context = EXPAND_NOTHING;
1002 return NULL;
1003 }
1004
1005 if (*cmd == '|' || *cmd == '\n')
1006 return cmd + 1; // There's another command
1007
1008 // Isolate the command and search for it in the command table.
1009 // Exceptions:
1010 // - the 'k' command can directly be followed by any character, but
1011 // do accept "keepmarks", "keepalt" and "keepjumps".
1012 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
1013 if (*cmd == 'k' && cmd[1] != 'e')
1014 {
1015 ea.cmdidx = CMD_k;
1016 p = cmd + 1;
1017 }
1018 else
1019 {
1020 p = cmd;
1021 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1022 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001023 // A user command may contain digits.
1024 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1025 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001026 while (ASCII_ISALNUM(*p) || *p == '*')
1027 ++p;
1028 // for python 3.x: ":py3*" commands completion
1029 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1030 {
1031 ++p;
1032 while (ASCII_ISALPHA(*p) || *p == '*')
1033 ++p;
1034 }
1035 // check for non-alpha command
1036 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1037 ++p;
1038 len = (int)(p - cmd);
1039
1040 if (len == 0)
1041 {
1042 xp->xp_context = EXPAND_UNSUCCESSFUL;
1043 return NULL;
1044 }
1045
1046 ea.cmdidx = excmd_get_cmdidx(cmd, len);
1047
1048 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1049 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1050 ++p;
1051 }
1052
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001053 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001054 // character, complete the command name.
1055 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1056 return NULL;
1057
1058 if (ea.cmdidx == CMD_SIZE)
1059 {
1060 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1061 {
1062 ea.cmdidx = CMD_substitute;
1063 p = cmd + 1;
1064 }
1065 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1066 {
1067 ea.cmd = cmd;
1068 p = find_ucmd(&ea, p, NULL, xp, &compl);
1069 if (p == NULL)
1070 ea.cmdidx = CMD_SIZE; // ambiguous user command
1071 }
1072 }
1073 if (ea.cmdidx == CMD_SIZE)
1074 {
1075 // Not still touching the command and it was an illegal one
1076 xp->xp_context = EXPAND_UNSUCCESSFUL;
1077 return NULL;
1078 }
1079
1080 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
1081
1082 if (*p == '!') // forced commands
1083 {
1084 forceit = TRUE;
1085 ++p;
1086 }
1087
1088 // 6. parse arguments
1089 if (!IS_USER_CMDIDX(ea.cmdidx))
1090 ea.argt = excmd_get_argt(ea.cmdidx);
1091
1092 arg = skipwhite(p);
1093
Bram Moolenaar743d0622020-07-03 18:15:06 +02001094 // Skip over ++argopt argument
1095 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
1096 {
1097 p = arg;
1098 while (*p && !vim_isspace(*p))
1099 MB_PTR_ADV(p);
1100 arg = skipwhite(p);
1101 }
1102
Bram Moolenaard0190392019-08-23 21:17:35 +02001103 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
1104 {
1105 if (*arg == '>') // append
1106 {
1107 if (*++arg == '>')
1108 ++arg;
1109 arg = skipwhite(arg);
1110 }
1111 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
1112 {
1113 ++arg;
1114 usefilter = TRUE;
1115 }
1116 }
1117
1118 if (ea.cmdidx == CMD_read)
1119 {
1120 usefilter = forceit; // :r! filter if forced
1121 if (*arg == '!') // :r !filter
1122 {
1123 ++arg;
1124 usefilter = TRUE;
1125 }
1126 }
1127
1128 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
1129 {
1130 while (*arg == *cmd) // allow any number of '>' or '<'
1131 ++arg;
1132 arg = skipwhite(arg);
1133 }
1134
1135 // Does command allow "+command"?
1136 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
1137 {
1138 // Check if we're in the +command
1139 p = arg + 1;
1140 arg = skip_cmd_arg(arg, FALSE);
1141
1142 // Still touching the command after '+'?
1143 if (*arg == NUL)
1144 return p;
1145
1146 // Skip space(s) after +command to get to the real argument
1147 arg = skipwhite(arg);
1148 }
1149
Bram Moolenaarc8cb8832020-06-18 21:14:30 +02001150
Bram Moolenaard0190392019-08-23 21:17:35 +02001151 // Check for '|' to separate commands and '"' to start comments.
1152 // Don't do this for ":read !cmd" and ":write !cmd".
1153 if ((ea.argt & EX_TRLBAR) && !usefilter)
1154 {
1155 p = arg;
1156 // ":redir @" is not the start of a comment
1157 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
1158 p += 2;
1159 while (*p)
1160 {
1161 if (*p == Ctrl_V)
1162 {
1163 if (p[1] != NUL)
1164 ++p;
1165 }
1166 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
1167 || *p == '|' || *p == '\n')
1168 {
1169 if (*(p - 1) != '\\')
1170 {
1171 if (*p == '|' || *p == '\n')
1172 return p + 1;
1173 return NULL; // It's a comment
1174 }
1175 }
1176 MB_PTR_ADV(p);
1177 }
1178 }
1179
1180 if (!(ea.argt & EX_EXTRA) && *arg != NUL
1181 && vim_strchr((char_u *)"|\"", *arg) == NULL)
1182 // no arguments allowed but there is something
1183 return NULL;
1184
1185 // Find start of last argument (argument just before cursor):
1186 p = buff;
1187 xp->xp_pattern = p;
1188 len = (int)STRLEN(buff);
1189 while (*p && p < buff + len)
1190 {
1191 if (*p == ' ' || *p == TAB)
1192 {
1193 // argument starts after a space
1194 xp->xp_pattern = ++p;
1195 }
1196 else
1197 {
1198 if (*p == '\\' && *(p + 1) != NUL)
1199 ++p; // skip over escaped character
1200 MB_PTR_ADV(p);
1201 }
1202 }
1203
1204 if (ea.argt & EX_XFILE)
1205 {
1206 int c;
1207 int in_quote = FALSE;
1208 char_u *bow = NULL; // Beginning of word
1209
1210 // Allow spaces within back-quotes to count as part of the argument
1211 // being expanded.
1212 xp->xp_pattern = skipwhite(arg);
1213 p = xp->xp_pattern;
1214 while (*p != NUL)
1215 {
1216 if (has_mbyte)
1217 c = mb_ptr2char(p);
1218 else
1219 c = *p;
1220 if (c == '\\' && p[1] != NUL)
1221 ++p;
1222 else if (c == '`')
1223 {
1224 if (!in_quote)
1225 {
1226 xp->xp_pattern = p;
1227 bow = p + 1;
1228 }
1229 in_quote = !in_quote;
1230 }
1231 // An argument can contain just about everything, except
1232 // characters that end the command and white space.
1233 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
1234#ifdef SPACE_IN_FILENAME
1235 && (!(ea.argt & EX_NOSPC) || usefilter)
1236#endif
1237 ))
1238 {
1239 len = 0; // avoid getting stuck when space is in 'isfname'
1240 while (*p != NUL)
1241 {
1242 if (has_mbyte)
1243 c = mb_ptr2char(p);
1244 else
1245 c = *p;
1246 if (c == '`' || vim_isfilec_or_wc(c))
1247 break;
1248 if (has_mbyte)
1249 len = (*mb_ptr2len)(p);
1250 else
1251 len = 1;
1252 MB_PTR_ADV(p);
1253 }
1254 if (in_quote)
1255 bow = p;
1256 else
1257 xp->xp_pattern = p;
1258 p -= len;
1259 }
1260 MB_PTR_ADV(p);
1261 }
1262
1263 // If we are still inside the quotes, and we passed a space, just
1264 // expand from there.
1265 if (bow != NULL && in_quote)
1266 xp->xp_pattern = bow;
1267 xp->xp_context = EXPAND_FILES;
1268
1269 // For a shell command more chars need to be escaped.
1270 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
1271 {
1272#ifndef BACKSLASH_IN_FILENAME
1273 xp->xp_shell = TRUE;
1274#endif
1275 // When still after the command name expand executables.
1276 if (xp->xp_pattern == skipwhite(arg))
1277 xp->xp_context = EXPAND_SHELLCMD;
1278 }
1279
1280 // Check for environment variable
1281 if (*xp->xp_pattern == '$'
1282#if defined(MSWIN)
1283 || *xp->xp_pattern == '%'
1284#endif
1285 )
1286 {
1287 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1288 if (!vim_isIDc(*p))
1289 break;
1290 if (*p == NUL)
1291 {
1292 xp->xp_context = EXPAND_ENV_VARS;
1293 ++xp->xp_pattern;
1294 // Avoid that the assignment uses EXPAND_FILES again.
1295 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
1296 compl = EXPAND_ENV_VARS;
1297 }
1298 }
1299 // Check for user names
1300 if (*xp->xp_pattern == '~')
1301 {
1302 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1303 ;
1304 // Complete ~user only if it partially matches a user name.
1305 // A full match ~user<Tab> will be replaced by user's home
1306 // directory i.e. something like ~user<Tab> -> /home/user/
1307 if (*p == NUL && p > xp->xp_pattern + 1
1308 && match_user(xp->xp_pattern + 1) >= 1)
1309 {
1310 xp->xp_context = EXPAND_USER;
1311 ++xp->xp_pattern;
1312 }
1313 }
1314 }
1315
1316 // 6. Switch on command name.
1317 switch (ea.cmdidx)
1318 {
1319 case CMD_find:
1320 case CMD_sfind:
1321 case CMD_tabfind:
1322 if (xp->xp_context == EXPAND_FILES)
1323 xp->xp_context = EXPAND_FILES_IN_PATH;
1324 break;
1325 case CMD_cd:
1326 case CMD_chdir:
1327 case CMD_tcd:
1328 case CMD_tchdir:
1329 case CMD_lcd:
1330 case CMD_lchdir:
1331 if (xp->xp_context == EXPAND_FILES)
1332 xp->xp_context = EXPAND_DIRECTORIES;
1333 break;
1334 case CMD_help:
1335 xp->xp_context = EXPAND_HELP;
1336 xp->xp_pattern = arg;
1337 break;
1338
1339 // Command modifiers: return the argument.
1340 // Also for commands with an argument that is a command.
1341 case CMD_aboveleft:
1342 case CMD_argdo:
1343 case CMD_belowright:
1344 case CMD_botright:
1345 case CMD_browse:
1346 case CMD_bufdo:
1347 case CMD_cdo:
1348 case CMD_cfdo:
1349 case CMD_confirm:
1350 case CMD_debug:
1351 case CMD_folddoclosed:
1352 case CMD_folddoopen:
1353 case CMD_hide:
1354 case CMD_keepalt:
1355 case CMD_keepjumps:
1356 case CMD_keepmarks:
1357 case CMD_keeppatterns:
1358 case CMD_ldo:
1359 case CMD_leftabove:
1360 case CMD_lfdo:
1361 case CMD_lockmarks:
1362 case CMD_noautocmd:
1363 case CMD_noswapfile:
1364 case CMD_rightbelow:
1365 case CMD_sandbox:
1366 case CMD_silent:
1367 case CMD_tab:
1368 case CMD_tabdo:
1369 case CMD_topleft:
1370 case CMD_verbose:
1371 case CMD_vertical:
1372 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001373 case CMD_vim9cmd:
1374 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001375 return arg;
1376
1377 case CMD_filter:
1378 if (*arg != NUL)
1379 arg = skip_vimgrep_pat(arg, NULL, NULL);
1380 if (arg == NULL || *arg == NUL)
1381 {
1382 xp->xp_context = EXPAND_NOTHING;
1383 return NULL;
1384 }
1385 return skipwhite(arg);
1386
1387#ifdef FEAT_SEARCH_EXTRA
1388 case CMD_match:
1389 if (*arg == NUL || !ends_excmd(*arg))
1390 {
1391 // also complete "None"
1392 set_context_in_echohl_cmd(xp, arg);
1393 arg = skipwhite(skiptowhite(arg));
1394 if (*arg != NUL)
1395 {
1396 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001397 arg = skip_regexp(arg + 1, *arg, magic_isset());
Bram Moolenaard0190392019-08-23 21:17:35 +02001398 }
1399 }
1400 return find_nextcmd(arg);
1401#endif
1402
1403 // All completion for the +cmdline_compl feature goes here.
1404
1405 case CMD_command:
1406 return set_context_in_user_cmd(xp, arg);
1407
1408 case CMD_delcommand:
1409 xp->xp_context = EXPAND_USER_COMMANDS;
1410 xp->xp_pattern = arg;
1411 break;
1412
1413 case CMD_global:
1414 case CMD_vglobal:
1415 delim = *arg; // get the delimiter
1416 if (delim)
1417 ++arg; // skip delimiter if there is one
1418
1419 while (arg[0] != NUL && arg[0] != delim)
1420 {
1421 if (arg[0] == '\\' && arg[1] != NUL)
1422 ++arg;
1423 ++arg;
1424 }
1425 if (arg[0] != NUL)
1426 return arg + 1;
1427 break;
1428 case CMD_and:
1429 case CMD_substitute:
1430 delim = *arg;
1431 if (delim)
1432 {
1433 // skip "from" part
1434 ++arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001435 arg = skip_regexp(arg, delim, magic_isset());
Bram Moolenaard0190392019-08-23 21:17:35 +02001436 }
1437 // skip "to" part
1438 while (arg[0] != NUL && arg[0] != delim)
1439 {
1440 if (arg[0] == '\\' && arg[1] != NUL)
1441 ++arg;
1442 ++arg;
1443 }
1444 if (arg[0] != NUL) // skip delimiter
1445 ++arg;
1446 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1447 ++arg;
1448 if (arg[0] != NUL)
1449 return arg;
1450 break;
1451 case CMD_isearch:
1452 case CMD_dsearch:
1453 case CMD_ilist:
1454 case CMD_dlist:
1455 case CMD_ijump:
1456 case CMD_psearch:
1457 case CMD_djump:
1458 case CMD_isplit:
1459 case CMD_dsplit:
1460 arg = skipwhite(skipdigits(arg)); // skip count
1461 if (*arg == '/') // Match regexp, not just whole words
1462 {
1463 for (++arg; *arg && *arg != '/'; arg++)
1464 if (*arg == '\\' && arg[1] != NUL)
1465 arg++;
1466 if (*arg)
1467 {
1468 arg = skipwhite(arg + 1);
1469
1470 // Check for trailing illegal characters
1471 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1472 xp->xp_context = EXPAND_NOTHING;
1473 else
1474 return arg;
1475 }
1476 }
1477 break;
1478
1479 case CMD_autocmd:
1480 return set_context_in_autocmd(xp, arg, FALSE);
1481 case CMD_doautocmd:
1482 case CMD_doautoall:
1483 return set_context_in_autocmd(xp, arg, TRUE);
1484 case CMD_set:
1485 set_context_in_set_cmd(xp, arg, 0);
1486 break;
1487 case CMD_setglobal:
1488 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1489 break;
1490 case CMD_setlocal:
1491 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1492 break;
1493 case CMD_tag:
1494 case CMD_stag:
1495 case CMD_ptag:
1496 case CMD_ltag:
1497 case CMD_tselect:
1498 case CMD_stselect:
1499 case CMD_ptselect:
1500 case CMD_tjump:
1501 case CMD_stjump:
1502 case CMD_ptjump:
1503 if (*p_wop != NUL)
1504 xp->xp_context = EXPAND_TAGS_LISTFILES;
1505 else
1506 xp->xp_context = EXPAND_TAGS;
1507 xp->xp_pattern = arg;
1508 break;
1509 case CMD_augroup:
1510 xp->xp_context = EXPAND_AUGROUP;
1511 xp->xp_pattern = arg;
1512 break;
1513#ifdef FEAT_SYN_HL
1514 case CMD_syntax:
1515 set_context_in_syntax_cmd(xp, arg);
1516 break;
1517#endif
1518#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001519 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001520 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001521 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001522 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001523 case CMD_if:
1524 case CMD_elseif:
1525 case CMD_while:
1526 case CMD_for:
1527 case CMD_echo:
1528 case CMD_echon:
1529 case CMD_execute:
1530 case CMD_echomsg:
1531 case CMD_echoerr:
1532 case CMD_call:
1533 case CMD_return:
1534 case CMD_cexpr:
1535 case CMD_caddexpr:
1536 case CMD_cgetexpr:
1537 case CMD_lexpr:
1538 case CMD_laddexpr:
1539 case CMD_lgetexpr:
1540 set_context_for_expression(xp, arg, ea.cmdidx);
1541 break;
1542
1543 case CMD_unlet:
1544 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1545 arg = xp->xp_pattern + 1;
1546
1547 xp->xp_context = EXPAND_USER_VARS;
1548 xp->xp_pattern = arg;
1549
1550 if (*xp->xp_pattern == '$')
1551 {
1552 xp->xp_context = EXPAND_ENV_VARS;
1553 ++xp->xp_pattern;
1554 }
1555
1556 break;
1557
1558 case CMD_function:
1559 case CMD_delfunction:
1560 xp->xp_context = EXPAND_USER_FUNC;
1561 xp->xp_pattern = arg;
1562 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001563 case CMD_disassemble:
1564 set_context_in_disassemble_cmd(xp, arg);
1565 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001566
1567 case CMD_echohl:
1568 set_context_in_echohl_cmd(xp, arg);
1569 break;
1570#endif
1571 case CMD_highlight:
1572 set_context_in_highlight_cmd(xp, arg);
1573 break;
1574#ifdef FEAT_CSCOPE
1575 case CMD_cscope:
1576 case CMD_lcscope:
1577 case CMD_scscope:
1578 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
1579 break;
1580#endif
1581#ifdef FEAT_SIGNS
1582 case CMD_sign:
1583 set_context_in_sign_cmd(xp, arg);
1584 break;
1585#endif
1586 case CMD_bdelete:
1587 case CMD_bwipeout:
1588 case CMD_bunload:
1589 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1590 arg = xp->xp_pattern + 1;
1591 // FALLTHROUGH
1592 case CMD_buffer:
1593 case CMD_sbuffer:
1594 case CMD_checktime:
1595 xp->xp_context = EXPAND_BUFFERS;
1596 xp->xp_pattern = arg;
1597 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001598#ifdef FEAT_DIFF
1599 case CMD_diffget:
1600 case CMD_diffput:
1601 // If current buffer is in diff mode, complete buffer names
1602 // which are in diff mode, and different than current buffer.
1603 xp->xp_context = EXPAND_DIFF_BUFFERS;
1604 xp->xp_pattern = arg;
1605 break;
1606#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001607 case CMD_USER:
1608 case CMD_USER_BUF:
1609 if (compl != EXPAND_NOTHING)
1610 {
1611 // EX_XFILE: file names are handled above
1612 if (!(ea.argt & EX_XFILE))
1613 {
1614#ifdef FEAT_MENU
1615 if (compl == EXPAND_MENUS)
1616 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1617#endif
1618 if (compl == EXPAND_COMMANDS)
1619 return arg;
1620 if (compl == EXPAND_MAPPINGS)
1621 return set_context_in_map_cmd(xp, (char_u *)"map",
1622 arg, forceit, FALSE, FALSE, CMD_map);
1623 // Find start of last argument.
1624 p = arg;
1625 while (*p)
1626 {
1627 if (*p == ' ')
1628 // argument starts after a space
1629 arg = p + 1;
1630 else if (*p == '\\' && *(p + 1) != NUL)
1631 ++p; // skip over escaped character
1632 MB_PTR_ADV(p);
1633 }
1634 xp->xp_pattern = arg;
1635 }
1636 xp->xp_context = compl;
1637 }
1638 break;
1639
1640 case CMD_map: case CMD_noremap:
1641 case CMD_nmap: case CMD_nnoremap:
1642 case CMD_vmap: case CMD_vnoremap:
1643 case CMD_omap: case CMD_onoremap:
1644 case CMD_imap: case CMD_inoremap:
1645 case CMD_cmap: case CMD_cnoremap:
1646 case CMD_lmap: case CMD_lnoremap:
1647 case CMD_smap: case CMD_snoremap:
1648 case CMD_tmap: case CMD_tnoremap:
1649 case CMD_xmap: case CMD_xnoremap:
1650 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1651 FALSE, FALSE, ea.cmdidx);
1652 case CMD_unmap:
1653 case CMD_nunmap:
1654 case CMD_vunmap:
1655 case CMD_ounmap:
1656 case CMD_iunmap:
1657 case CMD_cunmap:
1658 case CMD_lunmap:
1659 case CMD_sunmap:
1660 case CMD_tunmap:
1661 case CMD_xunmap:
1662 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1663 FALSE, TRUE, ea.cmdidx);
1664 case CMD_mapclear:
1665 case CMD_nmapclear:
1666 case CMD_vmapclear:
1667 case CMD_omapclear:
1668 case CMD_imapclear:
1669 case CMD_cmapclear:
1670 case CMD_lmapclear:
1671 case CMD_smapclear:
1672 case CMD_tmapclear:
1673 case CMD_xmapclear:
1674 xp->xp_context = EXPAND_MAPCLEAR;
1675 xp->xp_pattern = arg;
1676 break;
1677
1678 case CMD_abbreviate: case CMD_noreabbrev:
1679 case CMD_cabbrev: case CMD_cnoreabbrev:
1680 case CMD_iabbrev: case CMD_inoreabbrev:
1681 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1682 TRUE, FALSE, ea.cmdidx);
1683 case CMD_unabbreviate:
1684 case CMD_cunabbrev:
1685 case CMD_iunabbrev:
1686 return set_context_in_map_cmd(xp, cmd, arg, forceit,
1687 TRUE, TRUE, ea.cmdidx);
1688#ifdef FEAT_MENU
1689 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1690 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1691 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1692 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1693 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1694 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1695 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1696 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1697 case CMD_tmenu: case CMD_tunmenu:
1698 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1699 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1700#endif
1701
1702 case CMD_colorscheme:
1703 xp->xp_context = EXPAND_COLORS;
1704 xp->xp_pattern = arg;
1705 break;
1706
1707 case CMD_compiler:
1708 xp->xp_context = EXPAND_COMPILER;
1709 xp->xp_pattern = arg;
1710 break;
1711
1712 case CMD_ownsyntax:
1713 xp->xp_context = EXPAND_OWNSYNTAX;
1714 xp->xp_pattern = arg;
1715 break;
1716
1717 case CMD_setfiletype:
1718 xp->xp_context = EXPAND_FILETYPE;
1719 xp->xp_pattern = arg;
1720 break;
1721
1722 case CMD_packadd:
1723 xp->xp_context = EXPAND_PACKADD;
1724 xp->xp_pattern = arg;
1725 break;
1726
1727#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1728 case CMD_language:
1729 p = skiptowhite(arg);
1730 if (*p == NUL)
1731 {
1732 xp->xp_context = EXPAND_LANGUAGE;
1733 xp->xp_pattern = arg;
1734 }
1735 else
1736 {
1737 if ( STRNCMP(arg, "messages", p - arg) == 0
1738 || STRNCMP(arg, "ctype", p - arg) == 0
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001739 || STRNCMP(arg, "time", p - arg) == 0
1740 || STRNCMP(arg, "collate", p - arg) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001741 {
1742 xp->xp_context = EXPAND_LOCALES;
1743 xp->xp_pattern = skipwhite(p);
1744 }
1745 else
1746 xp->xp_context = EXPAND_NOTHING;
1747 }
1748 break;
1749#endif
1750#if defined(FEAT_PROFILE)
1751 case CMD_profile:
1752 set_context_in_profile_cmd(xp, arg);
1753 break;
1754#endif
1755 case CMD_behave:
1756 xp->xp_context = EXPAND_BEHAVE;
1757 xp->xp_pattern = arg;
1758 break;
1759
1760 case CMD_messages:
1761 xp->xp_context = EXPAND_MESSAGES;
1762 xp->xp_pattern = arg;
1763 break;
1764
1765 case CMD_history:
1766 xp->xp_context = EXPAND_HISTORY;
1767 xp->xp_pattern = arg;
1768 break;
1769#if defined(FEAT_PROFILE)
1770 case CMD_syntime:
1771 xp->xp_context = EXPAND_SYNTIME;
1772 xp->xp_pattern = arg;
1773 break;
1774#endif
1775
1776 case CMD_argdelete:
1777 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1778 arg = xp->xp_pattern + 1;
1779 xp->xp_context = EXPAND_ARGLIST;
1780 xp->xp_pattern = arg;
1781 break;
1782
1783 default:
1784 break;
1785 }
1786 return NULL;
1787}
1788
Bram Moolenaar66b51422019-08-18 21:44:12 +02001789 void
1790set_cmd_context(
1791 expand_T *xp,
1792 char_u *str, // start of command line
1793 int len, // length of command line (excl. NUL)
1794 int col, // position of cursor
1795 int use_ccline UNUSED) // use ccline for info
1796{
1797#ifdef FEAT_EVAL
1798 cmdline_info_T *ccline = get_cmdline_info();
1799#endif
1800 int old_char = NUL;
1801 char_u *nextcomm;
1802
1803 // Avoid a UMR warning from Purify, only save the character if it has been
1804 // written before.
1805 if (col < len)
1806 old_char = str[col];
1807 str[col] = NUL;
1808 nextcomm = str;
1809
1810#ifdef FEAT_EVAL
1811 if (use_ccline && ccline->cmdfirstc == '=')
1812 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001813 // pass CMD_SIZE because there is no real command
1814 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001815 }
1816 else if (use_ccline && ccline->input_fn)
1817 {
1818 xp->xp_context = ccline->xp_context;
1819 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001820 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001821 }
1822 else
1823#endif
1824 while (nextcomm != NULL)
1825 nextcomm = set_one_cmd_context(xp, nextcomm);
1826
1827 // Store the string here so that call_user_expand_func() can get to them
1828 // easily.
1829 xp->xp_line = str;
1830 xp->xp_col = col;
1831
1832 str[col] = old_char;
1833}
1834
1835/*
1836 * Expand the command line "str" from context "xp".
1837 * "xp" must have been set by set_cmd_context().
1838 * xp->xp_pattern points into "str", to where the text that is to be expanded
1839 * starts.
1840 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
1841 * cursor.
1842 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
1843 * key that triggered expansion literally.
1844 * Returns EXPAND_OK otherwise.
1845 */
1846 int
1847expand_cmdline(
1848 expand_T *xp,
1849 char_u *str, // start of command line
1850 int col, // position of cursor
1851 int *matchcount, // return: nr of matches
1852 char_u ***matches) // return: array of pointers to matches
1853{
1854 char_u *file_str = NULL;
1855 int options = WILD_ADD_SLASH|WILD_SILENT;
1856
1857 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
1858 {
1859 beep_flush();
1860 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
1861 }
1862 if (xp->xp_context == EXPAND_NOTHING)
1863 {
1864 // Caller can use the character as a normal char instead
1865 return EXPAND_NOTHING;
1866 }
1867
1868 // add star to file name, or convert to regexp if not exp. files.
1869 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
1870 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
1871 if (file_str == NULL)
1872 return EXPAND_UNSUCCESSFUL;
1873
1874 if (p_wic)
1875 options += WILD_ICASE;
1876
1877 // find all files that match the description
1878 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
1879 {
1880 *matchcount = 0;
1881 *matches = NULL;
1882 }
1883 vim_free(file_str);
1884
1885 return EXPAND_OK;
1886}
1887
Bram Moolenaar66b51422019-08-18 21:44:12 +02001888/*
Bram Moolenaard0190392019-08-23 21:17:35 +02001889 * Function given to ExpandGeneric() to obtain the possible arguments of the
1890 * ":behave {mswin,xterm}" command.
1891 */
1892 static char_u *
1893get_behave_arg(expand_T *xp UNUSED, int idx)
1894{
1895 if (idx == 0)
1896 return (char_u *)"mswin";
1897 if (idx == 1)
1898 return (char_u *)"xterm";
1899 return NULL;
1900}
1901
1902/*
1903 * Function given to ExpandGeneric() to obtain the possible arguments of the
1904 * ":messages {clear}" command.
1905 */
1906 static char_u *
1907get_messages_arg(expand_T *xp UNUSED, int idx)
1908{
1909 if (idx == 0)
1910 return (char_u *)"clear";
1911 return NULL;
1912}
1913
1914 static char_u *
1915get_mapclear_arg(expand_T *xp UNUSED, int idx)
1916{
1917 if (idx == 0)
1918 return (char_u *)"<buffer>";
1919 return NULL;
1920}
1921
1922/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001923 * Do the expansion based on xp->xp_context and "pat".
1924 */
1925 static int
1926ExpandFromContext(
1927 expand_T *xp,
1928 char_u *pat,
1929 int *num_file,
1930 char_u ***file,
1931 int options) // WILD_ flags
1932{
Bram Moolenaar66b51422019-08-18 21:44:12 +02001933 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001934 int ret;
1935 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01001936 char_u *tofree = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001937
1938 flags = EW_DIR; // include directories
1939 if (options & WILD_LIST_NOTFOUND)
1940 flags |= EW_NOTFOUND;
1941 if (options & WILD_ADD_SLASH)
1942 flags |= EW_ADDSLASH;
1943 if (options & WILD_KEEP_ALL)
1944 flags |= EW_KEEPALL;
1945 if (options & WILD_SILENT)
1946 flags |= EW_SILENT;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02001947 if (options & WILD_NOERROR)
1948 flags |= EW_NOERROR;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001949 if (options & WILD_ALLLINKS)
1950 flags |= EW_ALLLINKS;
1951
1952 if (xp->xp_context == EXPAND_FILES
1953 || xp->xp_context == EXPAND_DIRECTORIES
1954 || xp->xp_context == EXPAND_FILES_IN_PATH)
1955 {
1956 // Expand file or directory names.
1957 int free_pat = FALSE;
1958 int i;
1959
1960 // for ":set path=" and ":set tags=" halve backslashes for escaped
1961 // space
1962 if (xp->xp_backslash != XP_BS_NONE)
1963 {
1964 free_pat = TRUE;
1965 pat = vim_strsave(pat);
1966 for (i = 0; pat[i]; ++i)
1967 if (pat[i] == '\\')
1968 {
1969 if (xp->xp_backslash == XP_BS_THREE
1970 && pat[i + 1] == '\\'
1971 && pat[i + 2] == '\\'
1972 && pat[i + 3] == ' ')
1973 STRMOVE(pat + i, pat + i + 3);
1974 if (xp->xp_backslash == XP_BS_ONE
1975 && pat[i + 1] == ' ')
1976 STRMOVE(pat + i, pat + i + 1);
1977 }
1978 }
1979
1980 if (xp->xp_context == EXPAND_FILES)
1981 flags |= EW_FILE;
1982 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
1983 flags |= (EW_FILE | EW_PATH);
1984 else
1985 flags = (flags | EW_DIR) & ~EW_FILE;
1986 if (options & WILD_ICASE)
1987 flags |= EW_ICASE;
1988
1989 // Expand wildcards, supporting %:h and the like.
1990 ret = expand_wildcards_eval(&pat, num_file, file, flags);
1991 if (free_pat)
1992 vim_free(pat);
1993#ifdef BACKSLASH_IN_FILENAME
1994 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
1995 {
1996 int i;
1997
1998 for (i = 0; i < *num_file; ++i)
1999 {
2000 char_u *ptr = (*file)[i];
2001
2002 while (*ptr != NUL)
2003 {
2004 if (p_csl[0] == 's' && *ptr == '\\')
2005 *ptr = '/';
2006 else if (p_csl[0] == 'b' && *ptr == '/')
2007 *ptr = '\\';
2008 ptr += (*mb_ptr2len)(ptr);
2009 }
2010 }
2011 }
2012#endif
2013 return ret;
2014 }
2015
2016 *file = (char_u **)"";
2017 *num_file = 0;
2018 if (xp->xp_context == EXPAND_HELP)
2019 {
2020 // With an empty argument we would get all the help tags, which is
2021 // very slow. Get matches for "help" instead.
2022 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
2023 num_file, file, FALSE) == OK)
2024 {
2025#ifdef FEAT_MULTI_LANG
2026 cleanup_help_tags(*num_file, *file);
2027#endif
2028 return OK;
2029 }
2030 return FAIL;
2031 }
2032
Bram Moolenaar66b51422019-08-18 21:44:12 +02002033 if (xp->xp_context == EXPAND_SHELLCMD)
2034 return expand_shellcmd(pat, num_file, file, flags);
2035 if (xp->xp_context == EXPAND_OLD_SETTING)
2036 return ExpandOldSetting(num_file, file);
2037 if (xp->xp_context == EXPAND_BUFFERS)
2038 return ExpandBufnames(pat, num_file, file, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002039#ifdef FEAT_DIFF
2040 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
2041 return ExpandBufnames(pat, num_file, file, options | BUF_DIFF_FILTER);
2042#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002043 if (xp->xp_context == EXPAND_TAGS
2044 || xp->xp_context == EXPAND_TAGS_LISTFILES)
2045 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
2046 if (xp->xp_context == EXPAND_COLORS)
2047 {
2048 char *directories[] = {"colors", NULL};
2049 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
2050 directories);
2051 }
2052 if (xp->xp_context == EXPAND_COMPILER)
2053 {
2054 char *directories[] = {"compiler", NULL};
2055 return ExpandRTDir(pat, 0, num_file, file, directories);
2056 }
2057 if (xp->xp_context == EXPAND_OWNSYNTAX)
2058 {
2059 char *directories[] = {"syntax", NULL};
2060 return ExpandRTDir(pat, 0, num_file, file, directories);
2061 }
2062 if (xp->xp_context == EXPAND_FILETYPE)
2063 {
2064 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
2065 return ExpandRTDir(pat, 0, num_file, file, directories);
2066 }
2067# if defined(FEAT_EVAL)
2068 if (xp->xp_context == EXPAND_USER_LIST)
2069 return ExpandUserList(xp, num_file, file);
2070# endif
2071 if (xp->xp_context == EXPAND_PACKADD)
2072 return ExpandPackAddDir(pat, num_file, file);
2073
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002074 // When expanding a function name starting with s:, match the <SNR>nr_
2075 // prefix.
2076 if (xp->xp_context == EXPAND_USER_FUNC && STRNCMP(pat, "^s:", 3) == 0)
2077 {
2078 int len = (int)STRLEN(pat) + 20;
2079
2080 tofree = alloc(len);
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002081 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002082 pat = tofree;
2083 }
2084
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002085 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002086 if (regmatch.regprog == NULL)
2087 return FAIL;
2088
2089 // set ignore-case according to p_ic, p_scs and pat
2090 regmatch.rm_ic = ignorecase(pat);
2091
2092 if (xp->xp_context == EXPAND_SETTINGS
2093 || xp->xp_context == EXPAND_BOOL_SETTINGS)
2094 ret = ExpandSettings(xp, &regmatch, num_file, file);
2095 else if (xp->xp_context == EXPAND_MAPPINGS)
2096 ret = ExpandMappings(&regmatch, num_file, file);
2097# if defined(FEAT_EVAL)
2098 else if (xp->xp_context == EXPAND_USER_DEFINED)
2099 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
2100# endif
2101 else
2102 {
2103 static struct expgen
2104 {
2105 int context;
2106 char_u *((*func)(expand_T *, int));
2107 int ic;
2108 int escaped;
2109 } tab[] =
2110 {
2111 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2112 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2113 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2114 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2115 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2116 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2117 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2118 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2119 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2120 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2121# ifdef FEAT_EVAL
2122 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2123 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2124 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002125 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
Bram Moolenaar66b51422019-08-18 21:44:12 +02002126 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2127# endif
2128# ifdef FEAT_MENU
2129 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2130 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2131# endif
2132# ifdef FEAT_SYN_HL
2133 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2134# endif
2135# ifdef FEAT_PROFILE
2136 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2137# endif
2138 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2139 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
2140 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
2141# ifdef FEAT_CSCOPE
2142 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2143# endif
2144# ifdef FEAT_SIGNS
2145 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2146# endif
2147# ifdef FEAT_PROFILE
2148 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2149# endif
2150# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2151 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2152 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2153# endif
2154 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2155 {EXPAND_USER, get_users, TRUE, FALSE},
2156 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2157 };
2158 int i;
2159
2160 // Find a context in the table and call the ExpandGeneric() with the
2161 // right function to do the expansion.
2162 ret = FAIL;
K.Takataeeec2542021-06-02 13:28:16 +02002163 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002164 if (xp->xp_context == tab[i].context)
2165 {
2166 if (tab[i].ic)
2167 regmatch.rm_ic = TRUE;
2168 ret = ExpandGeneric(xp, &regmatch, num_file, file,
2169 tab[i].func, tab[i].escaped);
2170 break;
2171 }
2172 }
2173
2174 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002175 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002176
2177 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002178}
2179
Bram Moolenaar66b51422019-08-18 21:44:12 +02002180/*
2181 * Expand a list of names.
2182 *
2183 * Generic function for command line completion. It calls a function to
2184 * obtain strings, one by one. The strings are matched against a regexp
2185 * program. Matching strings are copied into an array, which is returned.
2186 *
2187 * Returns OK when no problems encountered, FAIL for error (out of memory).
2188 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002189 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002190ExpandGeneric(
2191 expand_T *xp,
2192 regmatch_T *regmatch,
2193 int *num_file,
2194 char_u ***file,
2195 char_u *((*func)(expand_T *, int)),
2196 // returns a string from the list
2197 int escaped)
2198{
2199 int i;
2200 int count = 0;
2201 int round;
2202 char_u *str;
2203
2204 // do this loop twice:
2205 // round == 0: count the number of matching names
2206 // round == 1: copy the matching names into allocated memory
2207 for (round = 0; round <= 1; ++round)
2208 {
2209 for (i = 0; ; ++i)
2210 {
2211 str = (*func)(xp, i);
2212 if (str == NULL) // end of list
2213 break;
2214 if (*str == NUL) // skip empty strings
2215 continue;
2216
2217 if (vim_regexec(regmatch, str, (colnr_T)0))
2218 {
2219 if (round)
2220 {
2221 if (escaped)
2222 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2223 else
2224 str = vim_strsave(str);
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002225 if (str == NULL)
2226 {
2227 FreeWild(count, *file);
2228 *num_file = 0;
2229 *file = NULL;
2230 return FAIL;
2231 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002232 (*file)[count] = str;
2233# ifdef FEAT_MENU
2234 if (func == get_menu_names && str != NULL)
2235 {
2236 // test for separator added by get_menu_names()
2237 str += STRLEN(str) - 1;
2238 if (*str == '\001')
2239 *str = '.';
2240 }
2241# endif
2242 }
2243 ++count;
2244 }
2245 }
2246 if (round == 0)
2247 {
2248 if (count == 0)
2249 return OK;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002250 *file = ALLOC_MULT(char_u *, count);
2251 if (*file == NULL)
2252 {
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002253 *num_file = 0;
2254 *file = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002255 return FAIL;
2256 }
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002257 *num_file = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002258 count = 0;
2259 }
2260 }
2261
2262 // Sort the results. Keep menu's in the specified order.
2263 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2264 {
2265 if (xp->xp_context == EXPAND_EXPRESSION
2266 || xp->xp_context == EXPAND_FUNCTIONS
2267 || xp->xp_context == EXPAND_USER_FUNC)
2268 // <SNR> functions should be sorted to the end.
2269 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
2270 sort_func_compare);
2271 else
2272 sort_strings(*file, *num_file);
2273 }
2274
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002275#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002276 // Reset the variables used for special highlight names expansion, so that
2277 // they don't show up when getting normal highlight names by ID.
2278 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002279#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002280 return OK;
2281}
2282
2283/*
2284 * Complete a shell command.
2285 * Returns FAIL or OK;
2286 */
2287 static int
2288expand_shellcmd(
2289 char_u *filepat, // pattern to match with command names
2290 int *num_file, // return: number of matches
2291 char_u ***file, // return: array with matches
2292 int flagsarg) // EW_ flags
2293{
2294 char_u *pat;
2295 int i;
2296 char_u *path = NULL;
2297 int mustfree = FALSE;
2298 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002299 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002300 size_t l;
2301 char_u *s, *e;
2302 int flags = flagsarg;
2303 int ret;
2304 int did_curdir = FALSE;
2305 hashtab_T found_ht;
2306 hashitem_T *hi;
2307 hash_T hash;
2308
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002309 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002310 if (buf == NULL)
2311 return FAIL;
2312
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002313 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002314 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002315 if (pat == NULL)
2316 {
2317 vim_free(buf);
2318 return FAIL;
2319 }
2320
Bram Moolenaar66b51422019-08-18 21:44:12 +02002321 for (i = 0; pat[i]; ++i)
2322 if (pat[i] == '\\' && pat[i + 1] == ' ')
2323 STRMOVE(pat + i, pat + i + 1);
2324
2325 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2326
2327 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2328 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2329 path = (char_u *)".";
2330 else
2331 {
2332 // For an absolute name we don't use $PATH.
2333 if (!mch_isFullName(pat))
2334 path = vim_getenv((char_u *)"PATH", &mustfree);
2335 if (path == NULL)
2336 path = (char_u *)"";
2337 }
2338
2339 // Go over all directories in $PATH. Expand matches in that directory and
2340 // collect them in "ga". When "." is not in $PATH also expand for the
2341 // current directory, to find "subdir/cmd".
2342 ga_init2(&ga, (int)sizeof(char *), 10);
2343 hash_init(&found_ht);
2344 for (s = path; ; s = e)
2345 {
2346# if defined(MSWIN)
2347 e = vim_strchr(s, ';');
2348# else
2349 e = vim_strchr(s, ':');
2350# endif
2351 if (e == NULL)
2352 e = s + STRLEN(s);
2353
2354 if (*s == NUL)
2355 {
2356 if (did_curdir)
2357 break;
2358 // Find directories in the current directory, path is empty.
2359 did_curdir = TRUE;
2360 flags |= EW_DIR;
2361 }
2362 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2363 {
2364 did_curdir = TRUE;
2365 flags |= EW_DIR;
2366 }
2367 else
2368 // Do not match directories inside a $PATH item.
2369 flags &= ~EW_DIR;
2370
2371 l = e - s;
2372 if (l > MAXPATHL - 5)
2373 break;
2374 vim_strncpy(buf, s, l);
2375 add_pathsep(buf);
2376 l = STRLEN(buf);
2377 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2378
2379 // Expand matches in one directory of $PATH.
2380 ret = expand_wildcards(1, &buf, num_file, file, flags);
2381 if (ret == OK)
2382 {
2383 if (ga_grow(&ga, *num_file) == FAIL)
2384 FreeWild(*num_file, *file);
2385 else
2386 {
2387 for (i = 0; i < *num_file; ++i)
2388 {
2389 char_u *name = (*file)[i];
2390
2391 if (STRLEN(name) > l)
2392 {
2393 // Check if this name was already found.
2394 hash = hash_hash(name + l);
2395 hi = hash_lookup(&found_ht, name + l, hash);
2396 if (HASHITEM_EMPTY(hi))
2397 {
2398 // Remove the path that was prepended.
2399 STRMOVE(name, name + l);
2400 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
2401 hash_add_item(&found_ht, hi, name, hash);
2402 name = NULL;
2403 }
2404 }
2405 vim_free(name);
2406 }
2407 vim_free(*file);
2408 }
2409 }
2410 if (*e != NUL)
2411 ++e;
2412 }
2413 *file = ga.ga_data;
2414 *num_file = ga.ga_len;
2415
2416 vim_free(buf);
2417 vim_free(pat);
2418 if (mustfree)
2419 vim_free(path);
2420 hash_clear(&found_ht);
2421 return OK;
2422}
2423
2424# if defined(FEAT_EVAL)
2425/*
2426 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002427 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002428 */
2429 static void *
2430call_user_expand_func(
2431 void *(*user_expand_func)(char_u *, int, typval_T *),
2432 expand_T *xp,
2433 int *num_file,
2434 char_u ***file)
2435{
2436 cmdline_info_T *ccline = get_cmdline_info();
2437 int keep = 0;
2438 typval_T args[4];
2439 sctx_T save_current_sctx = current_sctx;
2440 char_u *pat = NULL;
2441 void *ret;
2442
2443 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2444 return NULL;
2445 *num_file = 0;
2446 *file = NULL;
2447
2448 if (ccline->cmdbuff != NULL)
2449 {
2450 keep = ccline->cmdbuff[ccline->cmdlen];
2451 ccline->cmdbuff[ccline->cmdlen] = 0;
2452 }
2453
2454 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2455
2456 args[0].v_type = VAR_STRING;
2457 args[0].vval.v_string = pat;
2458 args[1].v_type = VAR_STRING;
2459 args[1].vval.v_string = xp->xp_line;
2460 args[2].v_type = VAR_NUMBER;
2461 args[2].vval.v_number = xp->xp_col;
2462 args[3].v_type = VAR_UNKNOWN;
2463
2464 current_sctx = xp->xp_script_ctx;
2465
2466 ret = user_expand_func(xp->xp_arg, 3, args);
2467
2468 current_sctx = save_current_sctx;
2469 if (ccline->cmdbuff != NULL)
2470 ccline->cmdbuff[ccline->cmdlen] = keep;
2471
2472 vim_free(pat);
2473 return ret;
2474}
2475
2476/*
2477 * Expand names with a function defined by the user.
2478 */
2479 static int
2480ExpandUserDefined(
2481 expand_T *xp,
2482 regmatch_T *regmatch,
2483 int *num_file,
2484 char_u ***file)
2485{
2486 char_u *retstr;
2487 char_u *s;
2488 char_u *e;
2489 int keep;
2490 garray_T ga;
2491 int skip;
2492
2493 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
2494 if (retstr == NULL)
2495 return FAIL;
2496
2497 ga_init2(&ga, (int)sizeof(char *), 3);
2498 for (s = retstr; *s != NUL; s = e)
2499 {
2500 e = vim_strchr(s, '\n');
2501 if (e == NULL)
2502 e = s + STRLEN(s);
2503 keep = *e;
2504 *e = NUL;
2505
2506 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2507 *e = keep;
2508
2509 if (!skip)
2510 {
2511 if (ga_grow(&ga, 1) == FAIL)
2512 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02002513 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002514 ++ga.ga_len;
2515 }
2516
2517 if (*e != NUL)
2518 ++e;
2519 }
2520 vim_free(retstr);
2521 *file = ga.ga_data;
2522 *num_file = ga.ga_len;
2523 return OK;
2524}
2525
2526/*
2527 * Expand names with a list returned by a function defined by the user.
2528 */
2529 static int
2530ExpandUserList(
2531 expand_T *xp,
2532 int *num_file,
2533 char_u ***file)
2534{
2535 list_T *retlist;
2536 listitem_T *li;
2537 garray_T ga;
2538
2539 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
2540 if (retlist == NULL)
2541 return FAIL;
2542
2543 ga_init2(&ga, (int)sizeof(char *), 3);
2544 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002545 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002546 {
2547 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
2548 continue; // Skip non-string items and empty strings
2549
2550 if (ga_grow(&ga, 1) == FAIL)
2551 break;
2552
2553 ((char_u **)ga.ga_data)[ga.ga_len] =
2554 vim_strsave(li->li_tv.vval.v_string);
2555 ++ga.ga_len;
2556 }
2557 list_unref(retlist);
2558
2559 *file = ga.ga_data;
2560 *num_file = ga.ga_len;
2561 return OK;
2562}
2563# endif
2564
2565/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002566 * Expand "file" for all comma-separated directories in "path".
2567 * Adds the matches to "ga". Caller must init "ga".
2568 */
2569 void
2570globpath(
2571 char_u *path,
2572 char_u *file,
2573 garray_T *ga,
2574 int expand_options)
2575{
2576 expand_T xpc;
2577 char_u *buf;
2578 int i;
2579 int num_p;
2580 char_u **p;
2581
2582 buf = alloc(MAXPATHL);
2583 if (buf == NULL)
2584 return;
2585
2586 ExpandInit(&xpc);
2587 xpc.xp_context = EXPAND_FILES;
2588
2589 // Loop over all entries in {path}.
2590 while (*path != NUL)
2591 {
2592 // Copy one item of the path to buf[] and concatenate the file name.
2593 copy_option_part(&path, buf, MAXPATHL, ",");
2594 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
2595 {
2596# if defined(MSWIN)
2597 // Using the platform's path separator (\) makes vim incorrectly
2598 // treat it as an escape character, use '/' instead.
2599 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
2600 STRCAT(buf, "/");
2601# else
2602 add_pathsep(buf);
2603# endif
2604 STRCAT(buf, file);
2605 if (ExpandFromContext(&xpc, buf, &num_p, &p,
2606 WILD_SILENT|expand_options) != FAIL && num_p > 0)
2607 {
2608 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
2609
2610 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002611 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02002612 for (i = 0; i < num_p; ++i)
2613 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002614 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02002615 ++ga->ga_len;
2616 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002617 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002618 }
2619 }
2620 }
2621
2622 vim_free(buf);
2623}
Bram Moolenaar66b51422019-08-18 21:44:12 +02002624
Bram Moolenaareadee482020-09-04 15:37:31 +02002625#ifdef FEAT_WILDMENU
2626
2627/*
2628 * Translate some keys pressed when 'wildmenu' is used.
2629 */
2630 int
2631wildmenu_translate_key(
2632 cmdline_info_T *cclp,
2633 int key,
2634 expand_T *xp,
2635 int did_wild_list)
2636{
2637 int c = key;
2638
2639 if (did_wild_list && p_wmnu)
2640 {
2641 if (c == K_LEFT)
2642 c = Ctrl_P;
2643 else if (c == K_RIGHT)
2644 c = Ctrl_N;
2645 }
2646 // Hitting CR after "emenu Name.": complete submenu
2647 if (xp->xp_context == EXPAND_MENUNAMES && p_wmnu
2648 && cclp->cmdpos > 1
2649 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
2650 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
2651 && (c == '\n' || c == '\r' || c == K_KENTER))
2652 c = K_DOWN;
2653
2654 return c;
2655}
2656
2657/*
2658 * Delete characters on the command line, from "from" to the current
2659 * position.
2660 */
2661 static void
2662cmdline_del(cmdline_info_T *cclp, int from)
2663{
2664 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
2665 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
2666 cclp->cmdlen -= cclp->cmdpos - from;
2667 cclp->cmdpos = from;
2668}
2669
2670/*
2671 * Handle a key pressed when wild menu is displayed
2672 */
2673 int
2674wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
2675{
2676 int c = key;
2677 int i;
2678 int j;
2679
2680 if (!p_wmnu)
2681 return c;
2682
2683 // Special translations for 'wildmenu'
2684 if (xp->xp_context == EXPAND_MENUNAMES)
2685 {
2686 // Hitting <Down> after "emenu Name.": complete submenu
2687 if (c == K_DOWN && cclp->cmdpos > 0
2688 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002689 {
Bram Moolenaareadee482020-09-04 15:37:31 +02002690 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002691 KeyTyped = TRUE; // in case the key was mapped
2692 }
Bram Moolenaareadee482020-09-04 15:37:31 +02002693 else if (c == K_UP)
2694 {
2695 // Hitting <Up>: Remove one submenu name in front of the
2696 // cursor
2697 int found = FALSE;
2698
2699 j = (int)(xp->xp_pattern - cclp->cmdbuff);
2700 i = 0;
2701 while (--j > 0)
2702 {
2703 // check for start of menu name
2704 if (cclp->cmdbuff[j] == ' '
2705 && cclp->cmdbuff[j - 1] != '\\')
2706 {
2707 i = j + 1;
2708 break;
2709 }
2710 // check for start of submenu name
2711 if (cclp->cmdbuff[j] == '.'
2712 && cclp->cmdbuff[j - 1] != '\\')
2713 {
2714 if (found)
2715 {
2716 i = j + 1;
2717 break;
2718 }
2719 else
2720 found = TRUE;
2721 }
2722 }
2723 if (i > 0)
2724 cmdline_del(cclp, i);
2725 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002726 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02002727 xp->xp_context = EXPAND_NOTHING;
2728 }
2729 }
2730 if ((xp->xp_context == EXPAND_FILES
2731 || xp->xp_context == EXPAND_DIRECTORIES
2732 || xp->xp_context == EXPAND_SHELLCMD))
2733 {
2734 char_u upseg[5];
2735
2736 upseg[0] = PATHSEP;
2737 upseg[1] = '.';
2738 upseg[2] = '.';
2739 upseg[3] = PATHSEP;
2740 upseg[4] = NUL;
2741
2742 if (c == K_DOWN
2743 && cclp->cmdpos > 0
2744 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
2745 && (cclp->cmdpos < 3
2746 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
2747 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
2748 {
2749 // go down a directory
2750 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002751 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02002752 }
2753 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
2754 {
2755 // If in a direct ancestor, strip off one ../ to go down
2756 int found = FALSE;
2757
2758 j = cclp->cmdpos;
2759 i = (int)(xp->xp_pattern - cclp->cmdbuff);
2760 while (--j > i)
2761 {
2762 if (has_mbyte)
2763 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
2764 if (vim_ispathsep(cclp->cmdbuff[j]))
2765 {
2766 found = TRUE;
2767 break;
2768 }
2769 }
2770 if (found
2771 && cclp->cmdbuff[j - 1] == '.'
2772 && cclp->cmdbuff[j - 2] == '.'
2773 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
2774 {
2775 cmdline_del(cclp, j - 2);
2776 c = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01002777 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02002778 }
2779 }
2780 else if (c == K_UP)
2781 {
2782 // go up a directory
2783 int found = FALSE;
2784
2785 j = cclp->cmdpos - 1;
2786 i = (int)(xp->xp_pattern - cclp->cmdbuff);
2787 while (--j > i)
2788 {
2789 if (has_mbyte)
2790 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
2791 if (vim_ispathsep(cclp->cmdbuff[j])
2792# ifdef BACKSLASH_IN_FILENAME
2793 && vim_strchr((char_u *)" *?[{`$%#",
2794 cclp->cmdbuff[j + 1]) == NULL
2795# endif
2796 )
2797 {
2798 if (found)
2799 {
2800 i = j + 1;
2801 break;
2802 }
2803 else
2804 found = TRUE;
2805 }
2806 }
2807
2808 if (!found)
2809 j = i;
2810 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
2811 j += 4;
2812 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
2813 && j == i)
2814 j += 3;
2815 else
2816 j = 0;
2817 if (j > 0)
2818 {
2819 // TODO this is only for DOS/UNIX systems - need to put in
2820 // machine-specific stuff here and in upseg init
2821 cmdline_del(cclp, j);
2822 put_on_cmdline(upseg + 1, 3, FALSE);
2823 }
2824 else if (cclp->cmdpos > i)
2825 cmdline_del(cclp, i);
2826
2827 // Now complete in the new directory. Set KeyTyped in case the
2828 // Up key came from a mapping.
2829 c = p_wc;
2830 KeyTyped = TRUE;
2831 }
2832 }
2833
2834 return c;
2835}
2836
2837/*
2838 * Free expanded names when finished walking through the matches
2839 */
2840 void
2841wildmenu_cleanup(cmdline_info_T *cclp)
2842{
2843 int skt = KeyTyped;
2844 int old_RedrawingDisabled = RedrawingDisabled;
2845
2846 if (!p_wmnu || wild_menu_showing == 0)
2847 return;
2848
2849 if (cclp->input_fn)
2850 RedrawingDisabled = 0;
2851
2852 if (wild_menu_showing == WM_SCROLLED)
2853 {
2854 // Entered command line, move it up
2855 cmdline_row--;
2856 redrawcmd();
2857 }
2858 else if (save_p_ls != -1)
2859 {
2860 // restore 'laststatus' and 'winminheight'
2861 p_ls = save_p_ls;
2862 p_wmh = save_p_wmh;
2863 last_status(FALSE);
2864 update_screen(VALID); // redraw the screen NOW
2865 redrawcmd();
2866 save_p_ls = -1;
2867 }
2868 else
2869 {
2870 win_redraw_last_status(topframe);
2871 redraw_statuslines();
2872 }
2873 KeyTyped = skt;
2874 wild_menu_showing = 0;
2875 if (cclp->input_fn)
2876 RedrawingDisabled = old_RedrawingDisabled;
2877}
2878#endif
2879
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002880#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002881/*
2882 * "getcompletion()" function
2883 */
2884 void
2885f_getcompletion(typval_T *argvars, typval_T *rettv)
2886{
2887 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002888 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002889 expand_T xpc;
2890 int filtered = FALSE;
2891 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002892 | WILD_NO_BEEP;
2893
2894 if (argvars[1].v_type != VAR_STRING)
2895 {
2896 semsg(_(e_invarg2), "type must be a string");
2897 return;
2898 }
2899 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002900
2901 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02002902 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002903
2904 if (p_wic)
2905 options |= WILD_ICASE;
2906
2907 // For filtered results, 'wildignore' is used
2908 if (!filtered)
2909 options |= WILD_KEEP_ALL;
2910
2911 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002912 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002913 {
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002914 set_one_cmd_context(&xpc, tv_get_string(&argvars[0]));
2915 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002916 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002917 else
2918 {
2919 xpc.xp_pattern = tv_get_string(&argvars[0]);
2920 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2921
2922 xpc.xp_context = cmdcomplete_str_to_type(type);
2923 if (xpc.xp_context == EXPAND_NOTHING)
2924 {
2925 semsg(_(e_invarg2), type);
2926 return;
2927 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002928
2929# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002930 if (xpc.xp_context == EXPAND_MENUS)
2931 {
2932 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
2933 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2934 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002935# endif
2936# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002937 if (xpc.xp_context == EXPAND_CSCOPE)
2938 {
2939 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
2940 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2941 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002942# endif
2943# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002944 if (xpc.xp_context == EXPAND_SIGN)
2945 {
2946 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
2947 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2948 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002949# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02002950 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002951
2952 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
2953 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
2954 {
2955 int i;
2956
2957 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
2958
2959 for (i = 0; i < xpc.xp_numfiles; i++)
2960 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2961 }
2962 vim_free(pat);
2963 ExpandCleanup(&xpc);
2964}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002965#endif // FEAT_EVAL