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