blob: 9d012ad4b55bb5ae4d8d6facae4e53b6d80059cd [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
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000040#define SHOW_FILE_TEXT(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
41
Bram Moolenaar66b51422019-08-18 21:44:12 +020042 static int
43sort_func_compare(const void *s1, const void *s2)
44{
45 char_u *p1 = *(char_u **)s1;
46 char_u *p2 = *(char_u **)s2;
47
48 if (*p1 != '<' && *p2 == '<') return -1;
49 if (*p1 == '<' && *p2 != '<') return 1;
50 return STRCMP(p1, p2);
51}
Bram Moolenaar66b51422019-08-18 21:44:12 +020052
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000053/*
54 * Escape special characters in the cmdline completion matches.
55 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020056 static void
57ExpandEscape(
58 expand_T *xp,
59 char_u *str,
60 int numfiles,
61 char_u **files,
62 int options)
63{
64 int i;
65 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +010066 int vse_what = xp->xp_context == EXPAND_BUFFERS
67 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +020068
69 // May change home directory back to "~"
70 if (options & WILD_HOME_REPLACE)
71 tilde_replace(str, numfiles, files);
72
73 if (options & WILD_ESCAPE)
74 {
75 if (xp->xp_context == EXPAND_FILES
76 || xp->xp_context == EXPAND_FILES_IN_PATH
77 || xp->xp_context == EXPAND_SHELLCMD
78 || xp->xp_context == EXPAND_BUFFERS
79 || xp->xp_context == EXPAND_DIRECTORIES)
80 {
81 // Insert a backslash into a file name before a space, \, %, #
82 // and wildmatch characters, except '~'.
83 for (i = 0; i < numfiles; ++i)
84 {
85 // for ":set path=" we need to escape spaces twice
86 if (xp->xp_backslash == XP_BS_THREE)
87 {
88 p = vim_strsave_escaped(files[i], (char_u *)" ");
89 if (p != NULL)
90 {
91 vim_free(files[i]);
92 files[i] = p;
93#if defined(BACKSLASH_IN_FILENAME)
94 p = vim_strsave_escaped(files[i], (char_u *)" ");
95 if (p != NULL)
96 {
97 vim_free(files[i]);
98 files[i] = p;
99 }
100#endif
101 }
102 }
103#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100104 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200105#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100106 p = vim_strsave_fnameescape(files[i],
107 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200108#endif
109 if (p != NULL)
110 {
111 vim_free(files[i]);
112 files[i] = p;
113 }
114
115 // If 'str' starts with "\~", replace "~" at start of
116 // files[i] with "\~".
117 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
118 escape_fname(&files[i]);
119 }
120 xp->xp_backslash = XP_BS_NONE;
121
122 // If the first file starts with a '+' escape it. Otherwise it
123 // could be seen as "+cmd".
124 if (*files[0] == '+')
125 escape_fname(&files[0]);
126 }
127 else if (xp->xp_context == EXPAND_TAGS)
128 {
129 // Insert a backslash before characters in a tag name that
130 // would terminate the ":tag" command.
131 for (i = 0; i < numfiles; ++i)
132 {
133 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
134 if (p != NULL)
135 {
136 vim_free(files[i]);
137 files[i] = p;
138 }
139 }
140 }
141 }
142}
143
144/*
145 * Return FAIL if this is not an appropriate context in which to do
146 * completion of anything, return OK if it is (even if there are no matches).
147 * For the caller, this means that the character is just passed through like a
148 * normal character (instead of being expanded). This allows :s/^I^D etc.
149 */
150 int
151nextwild(
152 expand_T *xp,
153 int type,
154 int options, // extra options for ExpandOne()
155 int escape) // if TRUE, escape the returned matches
156{
157 cmdline_info_T *ccline = get_cmdline_info();
158 int i, j;
159 char_u *p1;
160 char_u *p2;
161 int difflen;
162 int v;
163
164 if (xp->xp_numfiles == -1)
165 {
166 set_expand_context(xp);
167 cmd_showtail = expand_showtail(xp);
168 }
169
170 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
171 {
172 beep_flush();
173 return OK; // Something illegal on command line
174 }
175 if (xp->xp_context == EXPAND_NOTHING)
176 {
177 // Caller can use the character as a normal char instead
178 return FAIL;
179 }
180
181 msg_puts("..."); // show that we are busy
182 out_flush();
183
184 i = (int)(xp->xp_pattern - ccline->cmdbuff);
185 xp->xp_pattern_len = ccline->cmdpos - i;
186
187 if (type == WILD_NEXT || type == WILD_PREV)
188 {
189 // Get next/previous match for a previous expanded pattern.
190 p2 = ExpandOne(xp, NULL, NULL, 0, type);
191 }
192 else
193 {
194 // Translate string into pattern and expand it.
195 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
196 xp->xp_context)) == NULL)
197 p2 = NULL;
198 else
199 {
200 int use_options = options |
201 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
202 if (escape)
203 use_options |= WILD_ESCAPE;
204
205 if (p_wic)
206 use_options += WILD_ICASE;
207 p2 = ExpandOne(xp, p1,
208 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
209 use_options, type);
210 vim_free(p1);
211 // longest match: make sure it is not shorter, happens with :help
212 if (p2 != NULL && type == WILD_LONGEST)
213 {
214 for (j = 0; j < xp->xp_pattern_len; ++j)
215 if (ccline->cmdbuff[i + j] == '*'
216 || ccline->cmdbuff[i + j] == '?')
217 break;
218 if ((int)STRLEN(p2) < j)
219 VIM_CLEAR(p2);
220 }
221 }
222 }
223
224 if (p2 != NULL && !got_int)
225 {
226 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
227 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
228 {
229 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
230 xp->xp_pattern = ccline->cmdbuff + i;
231 }
232 else
233 v = OK;
234 if (v == OK)
235 {
236 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
237 &ccline->cmdbuff[ccline->cmdpos],
238 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
239 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
240 ccline->cmdlen += difflen;
241 ccline->cmdpos += difflen;
242 }
243 }
244 vim_free(p2);
245
246 redrawcmd();
247 cursorcmd();
248
249 // When expanding a ":map" command and no matches are found, assume that
250 // the key is supposed to be inserted literally
251 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
252 return FAIL;
253
254 if (xp->xp_numfiles <= 0 && p2 == NULL)
255 beep_flush();
256 else if (xp->xp_numfiles == 1)
257 // free expanded pattern
258 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
259
260 return OK;
261}
262
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000263#if defined(FEAT_WILDMENU) || defined(PROTO)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000264
265/*
266 * Create and display a cmdline completion popup menu with items from
267 * 'files_found'.
268 */
269 static int
270cmdline_pum_create(
271 cmdline_info_T *ccline,
272 expand_T *xp,
273 char_u **files_found,
274 int num_files,
275 int showtail)
276{
277 int i;
278 int columns;
279
280 // Add all the completion matches
281 compl_match_arraysize = num_files;
282 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
283 for (i = 0; i < num_files; i++)
284 {
285 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
286 compl_match_array[i].pum_info = NULL;
287 compl_match_array[i].pum_extra = NULL;
288 compl_match_array[i].pum_kind = NULL;
289 }
290
291 // Compute the popup menu starting column
292 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
293 columns = vim_strsize(xp->xp_pattern);
294 if (showtail)
295 {
296 columns += vim_strsize(sm_gettail(files_found[0]));
297 columns -= vim_strsize(files_found[0]);
298 }
299 if (columns >= compl_startcol)
300 compl_startcol = 0;
301 else
302 compl_startcol -= columns;
303
304 // no default selection
305 compl_selected = -1;
306
307 cmdline_pum_display();
308
309 return EXPAND_OK;
310}
311
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000312/*
313 * Display the cmdline completion matches in a popup menu
314 */
315void cmdline_pum_display(void)
316{
317 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
318}
319
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000320/*
321 * Returns TRUE if the cmdline completion popup menu is being displayed.
322 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000323int cmdline_pum_active(void)
324{
325 return p_wmnu && pum_visible() && compl_match_array != NULL;
326}
327
328/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000329 * Remove the cmdline completion popup menu (if present), free the list of
330 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000331 */
332void cmdline_pum_remove(void)
333{
334 pum_undisplay();
335 VIM_CLEAR(compl_match_array);
336 update_screen(0);
Bram Moolenaar414acd32022-02-10 21:09:45 +0000337 redrawcmd();
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000338}
339
340void cmdline_pum_cleanup(cmdline_info_T *cclp)
341{
342 cmdline_pum_remove();
343 wildmenu_cleanup(cclp);
344}
345
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000346/*
347 * Returns the starting column number to use for the cmdline completion popup
348 * menu.
349 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000350int cmdline_compl_startcol(void)
351{
352 return compl_startcol;
353}
354#endif
355
Bram Moolenaar66b51422019-08-18 21:44:12 +0200356/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000357 * Get the next or prev cmdline completion match. The index of the match is set
358 * in 'p_findex'
359 */
360 static char_u *
361get_next_or_prev_match(
362 int mode,
363 expand_T *xp,
364 int *p_findex,
365 char_u *orig_save)
366{
367 int findex = *p_findex;
368
369 if (xp->xp_numfiles <= 0)
370 return NULL;
371
372 if (mode == WILD_PREV)
373 {
374 if (findex == -1)
375 findex = xp->xp_numfiles;
376 --findex;
377 }
378 else // mode == WILD_NEXT
379 ++findex;
380
381 // When wrapping around, return the original string, set findex to
382 // -1.
383 if (findex < 0)
384 {
385 if (orig_save == NULL)
386 findex = xp->xp_numfiles - 1;
387 else
388 findex = -1;
389 }
390 if (findex >= xp->xp_numfiles)
391 {
392 if (orig_save == NULL)
393 findex = 0;
394 else
395 findex = -1;
396 }
397#ifdef FEAT_WILDMENU
398 if (compl_match_array)
399 {
400 compl_selected = findex;
401 cmdline_pum_display();
402 }
403 else if (p_wmnu)
404 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
405 findex, cmd_showtail);
406#endif
407 *p_findex = findex;
408
409 if (findex == -1)
410 return vim_strsave(orig_save);
411
412 return vim_strsave(xp->xp_files[findex]);
413}
414
415/*
416 * Start the command-line expansion and get the matches.
417 */
418 static char_u *
419ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
420{
421 int non_suf_match; // number without matching suffix
422 int i;
423 char_u *ss = NULL;
424
425 // Do the expansion.
426 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
427 options) == FAIL)
428 {
429#ifdef FNAME_ILLEGAL
430 // Illegal file name has been silently skipped. But when there
431 // are wildcards, the real problem is that there was no match,
432 // causing the pattern to be added, which has illegal characters.
433 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
434 semsg(_(e_no_match_str_2), str);
435#endif
436 }
437 else if (xp->xp_numfiles == 0)
438 {
439 if (!(options & WILD_SILENT))
440 semsg(_(e_no_match_str_2), str);
441 }
442 else
443 {
444 // Escape the matches for use on the command line.
445 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
446
447 // Check for matching suffixes in file names.
448 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
449 && mode != WILD_LONGEST)
450 {
451 if (xp->xp_numfiles)
452 non_suf_match = xp->xp_numfiles;
453 else
454 non_suf_match = 1;
455 if ((xp->xp_context == EXPAND_FILES
456 || xp->xp_context == EXPAND_DIRECTORIES)
457 && xp->xp_numfiles > 1)
458 {
459 // More than one match; check suffix.
460 // The files will have been sorted on matching suffix in
461 // expand_wildcards, only need to check the first two.
462 non_suf_match = 0;
463 for (i = 0; i < 2; ++i)
464 if (match_suffix(xp->xp_files[i]))
465 ++non_suf_match;
466 }
467 if (non_suf_match != 1)
468 {
469 // Can we ever get here unless it's while expanding
470 // interactively? If not, we can get rid of this all
471 // together. Don't really want to wait for this message
472 // (and possibly have to hit return to continue!).
473 if (!(options & WILD_SILENT))
474 emsg(_(e_too_many_file_names));
475 else if (!(options & WILD_NO_BEEP))
476 beep_flush();
477 }
478 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
479 ss = vim_strsave(xp->xp_files[0]);
480 }
481 }
482
483 return ss;
484}
485
486/*
487 * Return the longest common part in the list of cmdline completion matches.
488 */
489 static char_u *
490find_longest_match(expand_T *xp, int options)
491{
492 long_u len;
493 int mb_len = 1;
494 int c0, ci;
495 int i;
496 char_u *ss;
497
498 for (len = 0; xp->xp_files[0][len]; len += mb_len)
499 {
500 if (has_mbyte)
501 {
502 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
503 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
504 }
505 else
506 c0 = xp->xp_files[0][len];
507 for (i = 1; i < xp->xp_numfiles; ++i)
508 {
509 if (has_mbyte)
510 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
511 else
512 ci = xp->xp_files[i][len];
513 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
514 || xp->xp_context == EXPAND_FILES
515 || xp->xp_context == EXPAND_SHELLCMD
516 || xp->xp_context == EXPAND_BUFFERS))
517 {
518 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
519 break;
520 }
521 else if (c0 != ci)
522 break;
523 }
524 if (i < xp->xp_numfiles)
525 {
526 if (!(options & WILD_NO_BEEP))
527 vim_beep(BO_WILD);
528 break;
529 }
530 }
531
532 ss = alloc(len + 1);
533 if (ss)
534 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
535
536 return ss;
537}
538
539/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200540 * Do wildcard expansion on the string 'str'.
541 * Chars that should not be expanded must be preceded with a backslash.
542 * Return a pointer to allocated memory containing the new string.
543 * Return NULL for failure.
544 *
545 * "orig" is the originally expanded string, copied to allocated memory. It
546 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
547 * WILD_PREV "orig" should be NULL.
548 *
549 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
550 * is WILD_EXPAND_FREE or WILD_ALL.
551 *
552 * mode = WILD_FREE: just free previously expanded matches
553 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
554 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
555 * mode = WILD_NEXT: use next match in multiple match, wrap to first
556 * mode = WILD_PREV: use previous match in multiple match, wrap to first
557 * mode = WILD_ALL: return all matches concatenated
558 * mode = WILD_LONGEST: return longest matched part
559 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000560 * mode = WILD_APPLY: apply the item selected in the cmdline completion
561 * popup menu and close the menu.
562 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
563 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200564 *
565 * options = WILD_LIST_NOTFOUND: list entries without a match
566 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
567 * options = WILD_USE_NL: Use '\n' for WILD_ALL
568 * options = WILD_NO_BEEP: Don't beep for multiple matches
569 * options = WILD_ADD_SLASH: add a slash after directory names
570 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
571 * options = WILD_SILENT: don't print warning messages
572 * options = WILD_ESCAPE: put backslash before special chars
573 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200574 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200575 *
576 * The variables xp->xp_context and xp->xp_backslash must have been set!
577 */
578 char_u *
579ExpandOne(
580 expand_T *xp,
581 char_u *str,
582 char_u *orig, // allocated copy of original of expanded string
583 int options,
584 int mode)
585{
586 char_u *ss = NULL;
587 static int findex;
588 static char_u *orig_save = NULL; // kept value of orig
589 int orig_saved = FALSE;
590 int i;
591 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200592
593 // first handle the case of using an old match
594 if (mode == WILD_NEXT || mode == WILD_PREV)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000595 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200596
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000597 if (mode == WILD_CANCEL)
598 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
599 else if (mode == WILD_APPLY)
600 ss = vim_strsave(findex == -1 ? (orig_save ?
601 orig_save : (char_u *)"") : xp->xp_files[findex]);
602
Bram Moolenaar66b51422019-08-18 21:44:12 +0200603 // free old names
604 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
605 {
606 FreeWild(xp->xp_numfiles, xp->xp_files);
607 xp->xp_numfiles = -1;
608 VIM_CLEAR(orig_save);
609 }
610 findex = 0;
611
612 if (mode == WILD_FREE) // only release file name
613 return NULL;
614
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000615 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200616 {
617 vim_free(orig_save);
618 orig_save = orig;
619 orig_saved = TRUE;
620
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000621 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200622 }
623
624 // Find longest common part
625 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
626 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000627 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200628 findex = -1; // next p_wc gets first one
629 }
630
631 // Concatenate all matching names
632 if (mode == WILD_ALL && xp->xp_numfiles > 0)
633 {
634 len = 0;
635 for (i = 0; i < xp->xp_numfiles; ++i)
636 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
637 ss = alloc(len);
638 if (ss != NULL)
639 {
640 *ss = NUL;
641 for (i = 0; i < xp->xp_numfiles; ++i)
642 {
643 STRCAT(ss, xp->xp_files[i]);
644 if (i != xp->xp_numfiles - 1)
645 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
646 }
647 }
648 }
649
650 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
651 ExpandCleanup(xp);
652
653 // Free "orig" if it wasn't stored in "orig_save".
654 if (!orig_saved)
655 vim_free(orig);
656
657 return ss;
658}
659
660/*
661 * Prepare an expand structure for use.
662 */
663 void
664ExpandInit(expand_T *xp)
665{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200666 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200667 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200668 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200669}
670
671/*
672 * Cleanup an expand structure after use.
673 */
674 void
675ExpandCleanup(expand_T *xp)
676{
677 if (xp->xp_numfiles >= 0)
678 {
679 FreeWild(xp->xp_numfiles, xp->xp_files);
680 xp->xp_numfiles = -1;
681 }
682}
683
684/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000685 * Display one line of completion matches. Multiple matches are displayed in
686 * each line (used by wildmode=list and CTRL-D)
687 * files_found - list of completion match names
688 * num_files - number of completion matches in "files_found"
689 * lines - number of output lines
690 * linenr - line number of matches to display
691 * maxlen - maximum number of characters in each line
692 * showtail - display only the tail of the full path of a file name
693 * dir_attr - highlight attribute to use for directory names
694 */
695 static void
696showmatches_oneline(
697 expand_T *xp,
698 char_u **files_found,
699 int num_files,
700 int lines,
701 int linenr,
702 int maxlen,
703 int showtail,
704 int dir_attr)
705{
706 int i, j;
707 int isdir;
708 int lastlen;
709 char_u *p;
710
711 lastlen = 999;
712 for (j = linenr; j < num_files; j += lines)
713 {
714 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
715 {
716 msg_outtrans_attr(files_found[j], HL_ATTR(HLF_D));
717 p = files_found[j] + STRLEN(files_found[j]) + 1;
718 msg_advance(maxlen + 1);
719 msg_puts((char *)p);
720 msg_advance(maxlen + 3);
721 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
722 break;
723 }
724 for (i = maxlen - lastlen; --i >= 0; )
725 msg_putchar(' ');
726 if (xp->xp_context == EXPAND_FILES
727 || xp->xp_context == EXPAND_SHELLCMD
728 || xp->xp_context == EXPAND_BUFFERS)
729 {
730 // highlight directories
731 if (xp->xp_numfiles != -1)
732 {
733 char_u *halved_slash;
734 char_u *exp_path;
735 char_u *path;
736
737 // Expansion was done before and special characters
738 // were escaped, need to halve backslashes. Also
739 // $HOME has been replaced with ~/.
740 exp_path = expand_env_save_opt(files_found[j], TRUE);
741 path = exp_path != NULL ? exp_path : files_found[j];
742 halved_slash = backslash_halve_save(path);
743 isdir = mch_isdir(halved_slash != NULL ? halved_slash
744 : files_found[j]);
745 vim_free(exp_path);
746 if (halved_slash != path)
747 vim_free(halved_slash);
748 }
749 else
750 // Expansion was done here, file names are literal.
751 isdir = mch_isdir(files_found[j]);
752 if (showtail)
753 p = SHOW_FILE_TEXT(j);
754 else
755 {
756 home_replace(NULL, files_found[j], NameBuff, MAXPATHL,
757 TRUE);
758 p = NameBuff;
759 }
760 }
761 else
762 {
763 isdir = FALSE;
764 p = SHOW_FILE_TEXT(j);
765 }
766 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
767 }
768 if (msg_col > 0) // when not wrapped around
769 {
770 msg_clr_eos();
771 msg_putchar('\n');
772 }
773 out_flush(); // show one line at a time
774}
775
776/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200777 * Show all matches for completion on the command line.
778 * Returns EXPAND_NOTHING when the character that triggered expansion should
779 * be inserted like a normal character.
780 */
781 int
782showmatches(expand_T *xp, int wildmenu UNUSED)
783{
784 cmdline_info_T *ccline = get_cmdline_info();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200785 int num_files;
786 char_u **files_found;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000787 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200788 int maxlen;
789 int lines;
790 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200791 int attr;
792 int showtail;
793
794 if (xp->xp_numfiles == -1)
795 {
796 set_expand_context(xp);
797 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
798 &num_files, &files_found);
799 showtail = expand_showtail(xp);
800 if (i != EXPAND_OK)
801 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200802 }
803 else
804 {
805 num_files = xp->xp_numfiles;
806 files_found = xp->xp_files;
807 showtail = cmd_showtail;
808 }
809
810#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000811 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000812 // cmdline completion popup menu (with wildoptions=pum)
813 return cmdline_pum_create(ccline, xp, files_found, num_files, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000814#endif
815
816#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200817 if (!wildmenu)
818 {
819#endif
820 msg_didany = FALSE; // lines_left will be set
821 msg_start(); // prepare for paging
822 msg_putchar('\n');
823 out_flush();
824 cmdline_row = msg_row;
825 msg_didany = FALSE; // lines_left will be set again
826 msg_start(); // prepare for paging
827#ifdef FEAT_WILDMENU
828 }
829#endif
830
831 if (got_int)
832 got_int = FALSE; // only int. the completion, not the cmd line
833#ifdef FEAT_WILDMENU
834 else if (wildmenu)
835 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
836#endif
837 else
838 {
839 // find the length of the longest file name
840 maxlen = 0;
841 for (i = 0; i < num_files; ++i)
842 {
843 if (!showtail && (xp->xp_context == EXPAND_FILES
844 || xp->xp_context == EXPAND_SHELLCMD
845 || xp->xp_context == EXPAND_BUFFERS))
846 {
847 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
848 j = vim_strsize(NameBuff);
849 }
850 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000851 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200852 if (j > maxlen)
853 maxlen = j;
854 }
855
856 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
857 lines = num_files;
858 else
859 {
860 // compute the number of columns and lines for the listing
861 maxlen += 2; // two spaces between file names
862 columns = ((int)Columns + 2) / maxlen;
863 if (columns < 1)
864 columns = 1;
865 lines = (num_files + columns - 1) / columns;
866 }
867
868 attr = HL_ATTR(HLF_D); // find out highlighting for directories
869
870 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
871 {
872 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
873 msg_clr_eos();
874 msg_advance(maxlen - 3);
875 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
876 }
877
878 // list the files line by line
879 for (i = 0; i < lines; ++i)
880 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000881 showmatches_oneline(xp, files_found, num_files, lines, i,
882 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200883 if (got_int)
884 {
885 got_int = FALSE;
886 break;
887 }
888 }
889
890 // we redraw the command below the lines that we have just listed
891 // This is a bit tricky, but it saves a lot of screen updating.
892 cmdline_row = msg_row; // will put it back later
893 }
894
895 if (xp->xp_numfiles == -1)
896 FreeWild(num_files, files_found);
897
898 return EXPAND_OK;
899}
900
901/*
902 * Private gettail for showmatches() (and win_redr_status_matches()):
903 * Find tail of file name path, but ignore trailing "/".
904 */
905 char_u *
906sm_gettail(char_u *s)
907{
908 char_u *p;
909 char_u *t = s;
910 int had_sep = FALSE;
911
912 for (p = s; *p != NUL; )
913 {
914 if (vim_ispathsep(*p)
915#ifdef BACKSLASH_IN_FILENAME
916 && !rem_backslash(p)
917#endif
918 )
919 had_sep = TRUE;
920 else if (had_sep)
921 {
922 t = p;
923 had_sep = FALSE;
924 }
925 MB_PTR_ADV(p);
926 }
927 return t;
928}
929
930/*
931 * Return TRUE if we only need to show the tail of completion matches.
932 * When not completing file names or there is a wildcard in the path FALSE is
933 * returned.
934 */
935 static int
936expand_showtail(expand_T *xp)
937{
938 char_u *s;
939 char_u *end;
940
941 // When not completing file names a "/" may mean something different.
942 if (xp->xp_context != EXPAND_FILES
943 && xp->xp_context != EXPAND_SHELLCMD
944 && xp->xp_context != EXPAND_DIRECTORIES)
945 return FALSE;
946
947 end = gettail(xp->xp_pattern);
948 if (end == xp->xp_pattern) // there is no path separator
949 return FALSE;
950
951 for (s = xp->xp_pattern; s < end; s++)
952 {
953 // Skip escaped wildcards. Only when the backslash is not a path
954 // separator, on DOS the '*' "path\*\file" must not be skipped.
955 if (rem_backslash(s))
956 ++s;
957 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
958 return FALSE;
959 }
960 return TRUE;
961}
962
963/*
964 * Prepare a string for expansion.
965 * When expanding file names: The string will be used with expand_wildcards().
966 * Copy "fname[len]" into allocated memory and add a '*' at the end.
967 * When expanding other names: The string will be used with regcomp(). Copy
968 * the name into allocated memory and prepend "^".
969 */
970 char_u *
971addstar(
972 char_u *fname,
973 int len,
974 int context) // EXPAND_FILES etc.
975{
976 char_u *retval;
977 int i, j;
978 int new_len;
979 char_u *tail;
980 int ends_in_star;
981
982 if (context != EXPAND_FILES
983 && context != EXPAND_FILES_IN_PATH
984 && context != EXPAND_SHELLCMD
985 && context != EXPAND_DIRECTORIES)
986 {
987 // Matching will be done internally (on something other than files).
988 // So we convert the file-matching-type wildcards into our kind for
989 // use with vim_regcomp(). First work out how long it will be:
990
991 // For help tags the translation is done in find_help_tags().
992 // For a tag pattern starting with "/" no translation is needed.
993 if (context == EXPAND_HELP
994 || context == EXPAND_COLORS
995 || context == EXPAND_COMPILER
996 || context == EXPAND_OWNSYNTAX
997 || context == EXPAND_FILETYPE
998 || context == EXPAND_PACKADD
999 || ((context == EXPAND_TAGS_LISTFILES
1000 || context == EXPAND_TAGS)
1001 && fname[0] == '/'))
1002 retval = vim_strnsave(fname, len);
1003 else
1004 {
1005 new_len = len + 2; // +2 for '^' at start, NUL at end
1006 for (i = 0; i < len; i++)
1007 {
1008 if (fname[i] == '*' || fname[i] == '~')
1009 new_len++; // '*' needs to be replaced by ".*"
1010 // '~' needs to be replaced by "\~"
1011
1012 // Buffer names are like file names. "." should be literal
1013 if (context == EXPAND_BUFFERS && fname[i] == '.')
1014 new_len++; // "." becomes "\."
1015
1016 // Custom expansion takes care of special things, match
1017 // backslashes literally (perhaps also for other types?)
1018 if ((context == EXPAND_USER_DEFINED
1019 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1020 new_len++; // '\' becomes "\\"
1021 }
1022 retval = alloc(new_len);
1023 if (retval != NULL)
1024 {
1025 retval[0] = '^';
1026 j = 1;
1027 for (i = 0; i < len; i++, j++)
1028 {
1029 // Skip backslash. But why? At least keep it for custom
1030 // expansion.
1031 if (context != EXPAND_USER_DEFINED
1032 && context != EXPAND_USER_LIST
1033 && fname[i] == '\\'
1034 && ++i == len)
1035 break;
1036
1037 switch (fname[i])
1038 {
1039 case '*': retval[j++] = '.';
1040 break;
1041 case '~': retval[j++] = '\\';
1042 break;
1043 case '?': retval[j] = '.';
1044 continue;
1045 case '.': if (context == EXPAND_BUFFERS)
1046 retval[j++] = '\\';
1047 break;
1048 case '\\': if (context == EXPAND_USER_DEFINED
1049 || context == EXPAND_USER_LIST)
1050 retval[j++] = '\\';
1051 break;
1052 }
1053 retval[j] = fname[i];
1054 }
1055 retval[j] = NUL;
1056 }
1057 }
1058 }
1059 else
1060 {
1061 retval = alloc(len + 4);
1062 if (retval != NULL)
1063 {
1064 vim_strncpy(retval, fname, len);
1065
1066 // Don't add a star to *, ~, ~user, $var or `cmd`.
1067 // * would become **, which walks the whole tree.
1068 // ~ would be at the start of the file name, but not the tail.
1069 // $ could be anywhere in the tail.
1070 // ` could be anywhere in the file name.
1071 // When the name ends in '$' don't add a star, remove the '$'.
1072 tail = gettail(retval);
1073 ends_in_star = (len > 0 && retval[len - 1] == '*');
1074#ifndef BACKSLASH_IN_FILENAME
1075 for (i = len - 2; i >= 0; --i)
1076 {
1077 if (retval[i] != '\\')
1078 break;
1079 ends_in_star = !ends_in_star;
1080 }
1081#endif
1082 if ((*retval != '~' || tail != retval)
1083 && !ends_in_star
1084 && vim_strchr(tail, '$') == NULL
1085 && vim_strchr(retval, '`') == NULL)
1086 retval[len++] = '*';
1087 else if (len > 0 && retval[len - 1] == '$')
1088 --len;
1089 retval[len] = NUL;
1090 }
1091 }
1092 return retval;
1093}
1094
1095/*
1096 * Must parse the command line so far to work out what context we are in.
1097 * Completion can then be done based on that context.
1098 * This routine sets the variables:
1099 * xp->xp_pattern The start of the pattern to be expanded within
1100 * the command line (ends at the cursor).
1101 * xp->xp_context The type of thing to expand. Will be one of:
1102 *
1103 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1104 * the command line, like an unknown command. Caller
1105 * should beep.
1106 * EXPAND_NOTHING Unrecognised context for completion, use char like
1107 * a normal char, rather than for completion. eg
1108 * :s/^I/
1109 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1110 * it.
1111 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1112 * EXPAND_FILES After command with EX_XFILE set, or after setting
1113 * with P_EXPAND set. eg :e ^I, :w>>^I
1114 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1115 * when we know only directories are of interest. eg
1116 * :set dir=^I
1117 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1118 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1119 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1120 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1121 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1122 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1123 * EXPAND_EVENTS Complete event names
1124 * EXPAND_SYNTAX Complete :syntax command arguments
1125 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1126 * EXPAND_AUGROUP Complete autocommand group names
1127 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1128 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1129 * eg :unmap a^I , :cunab x^I
1130 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1131 * eg :call sub^I
1132 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1133 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1134 * names in expressions, eg :while s^I
1135 * EXPAND_ENV_VARS Complete environment variable names
1136 * EXPAND_USER Complete user names
1137 */
1138 static void
1139set_expand_context(expand_T *xp)
1140{
1141 cmdline_info_T *ccline = get_cmdline_info();
1142
1143 // only expansion for ':', '>' and '=' command-lines
1144 if (ccline->cmdfirstc != ':'
1145#ifdef FEAT_EVAL
1146 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1147 && !ccline->input_fn
1148#endif
1149 )
1150 {
1151 xp->xp_context = EXPAND_NOTHING;
1152 return;
1153 }
1154 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1155}
1156
Bram Moolenaard0190392019-08-23 21:17:35 +02001157/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001158 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1159 * For user defined commands, the completion context is set in 'xp' and the
1160 * completion flags in 'complp'.
1161 *
1162 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001163 */
1164 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001165set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001166{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001167 char_u *p = NULL;
1168 int len = 0;
Bram Moolenaard0190392019-08-23 21:17:35 +02001169
1170 // Isolate the command and search for it in the command table.
1171 // Exceptions:
1172 // - the 'k' command can directly be followed by any character, but
1173 // do accept "keepmarks", "keepalt" and "keepjumps".
1174 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
1175 if (*cmd == 'k' && cmd[1] != 'e')
1176 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001177 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001178 p = cmd + 1;
1179 }
1180 else
1181 {
1182 p = cmd;
1183 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1184 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001185 // A user command may contain digits.
1186 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1187 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001188 while (ASCII_ISALNUM(*p) || *p == '*')
1189 ++p;
1190 // for python 3.x: ":py3*" commands completion
1191 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1192 {
1193 ++p;
1194 while (ASCII_ISALPHA(*p) || *p == '*')
1195 ++p;
1196 }
1197 // check for non-alpha command
1198 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1199 ++p;
1200 len = (int)(p - cmd);
1201
1202 if (len == 0)
1203 {
1204 xp->xp_context = EXPAND_UNSUCCESSFUL;
1205 return NULL;
1206 }
1207
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001208 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001209
1210 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1211 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1212 ++p;
1213 }
1214
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001215 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001216 // character, complete the command name.
1217 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1218 return NULL;
1219
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001220 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001221 {
1222 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1223 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001224 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001225 p = cmd + 1;
1226 }
1227 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1228 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001229 eap->cmd = cmd;
1230 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001231 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001232 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001233 }
1234 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001235 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001236 {
1237 // Not still touching the command and it was an illegal one
1238 xp->xp_context = EXPAND_UNSUCCESSFUL;
1239 return NULL;
1240 }
1241
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001242 return p;
1243}
Bram Moolenaard0190392019-08-23 21:17:35 +02001244
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001245/*
1246 * Set the completion context for a command argument with wild card characters.
1247 */
1248 static void
1249set_context_for_wildcard_arg(
1250 exarg_T *eap,
1251 char_u *arg,
1252 int usefilter,
1253 expand_T *xp,
1254 int *complp)
1255{
1256 char_u *p;
1257 int c;
1258 int in_quote = FALSE;
1259 char_u *bow = NULL; // Beginning of word
1260 int len = 0;
1261
1262 // Allow spaces within back-quotes to count as part of the argument
1263 // being expanded.
1264 xp->xp_pattern = skipwhite(arg);
1265 p = xp->xp_pattern;
1266 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001267 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001268 if (has_mbyte)
1269 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001270 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001271 c = *p;
1272 if (c == '\\' && p[1] != NUL)
1273 ++p;
1274 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001275 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001276 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001277 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001278 xp->xp_pattern = p;
1279 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001280 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001281 in_quote = !in_quote;
1282 }
1283 // An argument can contain just about everything, except
1284 // characters that end the command and white space.
1285 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001286#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001287 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001288#endif
1289 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001290 {
1291 len = 0; // avoid getting stuck when space is in 'isfname'
1292 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001293 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001294 if (has_mbyte)
1295 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001296 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001297 c = *p;
1298 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001299 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001300 if (has_mbyte)
1301 len = (*mb_ptr2len)(p);
1302 else
1303 len = 1;
1304 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001305 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001306 if (in_quote)
1307 bow = p;
1308 else
1309 xp->xp_pattern = p;
1310 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001311 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001312 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001313 }
1314
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001315 // If we are still inside the quotes, and we passed a space, just
1316 // expand from there.
1317 if (bow != NULL && in_quote)
1318 xp->xp_pattern = bow;
1319 xp->xp_context = EXPAND_FILES;
1320
1321 // For a shell command more chars need to be escaped.
1322 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1323 {
1324#ifndef BACKSLASH_IN_FILENAME
1325 xp->xp_shell = TRUE;
1326#endif
1327 // When still after the command name expand executables.
1328 if (xp->xp_pattern == skipwhite(arg))
1329 xp->xp_context = EXPAND_SHELLCMD;
1330 }
1331
1332 // Check for environment variable.
1333 if (*xp->xp_pattern == '$')
1334 {
1335 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1336 if (!vim_isIDc(*p))
1337 break;
1338 if (*p == NUL)
1339 {
1340 xp->xp_context = EXPAND_ENV_VARS;
1341 ++xp->xp_pattern;
1342 // Avoid that the assignment uses EXPAND_FILES again.
1343 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1344 *complp = EXPAND_ENV_VARS;
1345 }
1346 }
1347 // Check for user names.
1348 if (*xp->xp_pattern == '~')
1349 {
1350 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1351 ;
1352 // Complete ~user only if it partially matches a user name.
1353 // A full match ~user<Tab> will be replaced by user's home
1354 // directory i.e. something like ~user<Tab> -> /home/user/
1355 if (*p == NUL && p > xp->xp_pattern + 1
1356 && match_user(xp->xp_pattern + 1) >= 1)
1357 {
1358 xp->xp_context = EXPAND_USER;
1359 ++xp->xp_pattern;
1360 }
1361 }
1362}
1363
1364/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001365 * Returns a pointer to the next command after a :substitute or a :& command.
1366 * Returns NULL if there is no next command.
1367 */
1368 static char_u *
1369find_cmd_after_substitute_cmd(char_u *arg)
1370{
1371 int delim;
1372
1373 delim = *arg;
1374 if (delim)
1375 {
1376 // skip "from" part
1377 ++arg;
1378 arg = skip_regexp(arg, delim, magic_isset());
1379
1380 if (arg[0] != NUL && arg[0] == delim)
1381 {
1382 // skip "to" part
1383 ++arg;
1384 while (arg[0] != NUL && arg[0] != delim)
1385 {
1386 if (arg[0] == '\\' && arg[1] != NUL)
1387 ++arg;
1388 ++arg;
1389 }
1390 if (arg[0] != NUL) // skip delimiter
1391 ++arg;
1392 }
1393 }
1394 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1395 ++arg;
1396 if (arg[0] != NUL)
1397 return arg;
1398
1399 return NULL;
1400}
1401
1402/*
1403 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1404 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1405 * Returns NULL if there is no next command.
1406 */
1407 static char_u *
1408find_cmd_after_isearch_cmd(char_u *arg, expand_T *xp)
1409{
1410 arg = skipwhite(skipdigits(arg)); // skip count
1411 if (*arg == '/') // Match regexp, not just whole words
1412 {
1413 for (++arg; *arg && *arg != '/'; arg++)
1414 if (*arg == '\\' && arg[1] != NUL)
1415 arg++;
1416 if (*arg)
1417 {
1418 arg = skipwhite(arg + 1);
1419
1420 // Check for trailing illegal characters
1421 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1422 xp->xp_context = EXPAND_NOTHING;
1423 else
1424 return arg;
1425 }
1426 }
1427
1428 return NULL;
1429}
1430
1431/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001432 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1433 * The argument to the command is 'arg' and the argument flags is 'argt'.
1434 * For user-defined commands and for environment variables, 'compl' has the
1435 * completion type.
1436 * Returns a pointer to the next command. Returns NULL if there is no next
1437 * command.
1438 */
1439 static char_u *
1440set_context_by_cmdname(
1441 char_u *cmd,
1442 cmdidx_T cmdidx,
1443 char_u *arg,
1444 long argt,
1445 int compl,
1446 expand_T *xp,
1447 int forceit)
1448{
1449 char_u *p;
1450 int delim;
1451
1452 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001453 {
1454 case CMD_find:
1455 case CMD_sfind:
1456 case CMD_tabfind:
1457 if (xp->xp_context == EXPAND_FILES)
1458 xp->xp_context = EXPAND_FILES_IN_PATH;
1459 break;
1460 case CMD_cd:
1461 case CMD_chdir:
1462 case CMD_tcd:
1463 case CMD_tchdir:
1464 case CMD_lcd:
1465 case CMD_lchdir:
1466 if (xp->xp_context == EXPAND_FILES)
1467 xp->xp_context = EXPAND_DIRECTORIES;
1468 break;
1469 case CMD_help:
1470 xp->xp_context = EXPAND_HELP;
1471 xp->xp_pattern = arg;
1472 break;
1473
1474 // Command modifiers: return the argument.
1475 // Also for commands with an argument that is a command.
1476 case CMD_aboveleft:
1477 case CMD_argdo:
1478 case CMD_belowright:
1479 case CMD_botright:
1480 case CMD_browse:
1481 case CMD_bufdo:
1482 case CMD_cdo:
1483 case CMD_cfdo:
1484 case CMD_confirm:
1485 case CMD_debug:
1486 case CMD_folddoclosed:
1487 case CMD_folddoopen:
1488 case CMD_hide:
1489 case CMD_keepalt:
1490 case CMD_keepjumps:
1491 case CMD_keepmarks:
1492 case CMD_keeppatterns:
1493 case CMD_ldo:
1494 case CMD_leftabove:
1495 case CMD_lfdo:
1496 case CMD_lockmarks:
1497 case CMD_noautocmd:
1498 case CMD_noswapfile:
1499 case CMD_rightbelow:
1500 case CMD_sandbox:
1501 case CMD_silent:
1502 case CMD_tab:
1503 case CMD_tabdo:
1504 case CMD_topleft:
1505 case CMD_verbose:
1506 case CMD_vertical:
1507 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001508 case CMD_vim9cmd:
1509 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001510 return arg;
1511
1512 case CMD_filter:
1513 if (*arg != NUL)
1514 arg = skip_vimgrep_pat(arg, NULL, NULL);
1515 if (arg == NULL || *arg == NUL)
1516 {
1517 xp->xp_context = EXPAND_NOTHING;
1518 return NULL;
1519 }
1520 return skipwhite(arg);
1521
1522#ifdef FEAT_SEARCH_EXTRA
1523 case CMD_match:
1524 if (*arg == NUL || !ends_excmd(*arg))
1525 {
1526 // also complete "None"
1527 set_context_in_echohl_cmd(xp, arg);
1528 arg = skipwhite(skiptowhite(arg));
1529 if (*arg != NUL)
1530 {
1531 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001532 arg = skip_regexp(arg + 1, *arg, magic_isset());
Bram Moolenaard0190392019-08-23 21:17:35 +02001533 }
1534 }
1535 return find_nextcmd(arg);
1536#endif
1537
1538 // All completion for the +cmdline_compl feature goes here.
1539
1540 case CMD_command:
1541 return set_context_in_user_cmd(xp, arg);
1542
1543 case CMD_delcommand:
1544 xp->xp_context = EXPAND_USER_COMMANDS;
1545 xp->xp_pattern = arg;
1546 break;
1547
1548 case CMD_global:
1549 case CMD_vglobal:
1550 delim = *arg; // get the delimiter
1551 if (delim)
1552 ++arg; // skip delimiter if there is one
1553
1554 while (arg[0] != NUL && arg[0] != delim)
1555 {
1556 if (arg[0] == '\\' && arg[1] != NUL)
1557 ++arg;
1558 ++arg;
1559 }
1560 if (arg[0] != NUL)
1561 return arg + 1;
1562 break;
1563 case CMD_and:
1564 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001565 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001566 case CMD_isearch:
1567 case CMD_dsearch:
1568 case CMD_ilist:
1569 case CMD_dlist:
1570 case CMD_ijump:
1571 case CMD_psearch:
1572 case CMD_djump:
1573 case CMD_isplit:
1574 case CMD_dsplit:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001575 return find_cmd_after_isearch_cmd(arg, xp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001576 case CMD_autocmd:
1577 return set_context_in_autocmd(xp, arg, FALSE);
1578 case CMD_doautocmd:
1579 case CMD_doautoall:
1580 return set_context_in_autocmd(xp, arg, TRUE);
1581 case CMD_set:
1582 set_context_in_set_cmd(xp, arg, 0);
1583 break;
1584 case CMD_setglobal:
1585 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1586 break;
1587 case CMD_setlocal:
1588 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1589 break;
1590 case CMD_tag:
1591 case CMD_stag:
1592 case CMD_ptag:
1593 case CMD_ltag:
1594 case CMD_tselect:
1595 case CMD_stselect:
1596 case CMD_ptselect:
1597 case CMD_tjump:
1598 case CMD_stjump:
1599 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001600 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001601 xp->xp_context = EXPAND_TAGS_LISTFILES;
1602 else
1603 xp->xp_context = EXPAND_TAGS;
1604 xp->xp_pattern = arg;
1605 break;
1606 case CMD_augroup:
1607 xp->xp_context = EXPAND_AUGROUP;
1608 xp->xp_pattern = arg;
1609 break;
1610#ifdef FEAT_SYN_HL
1611 case CMD_syntax:
1612 set_context_in_syntax_cmd(xp, arg);
1613 break;
1614#endif
1615#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001616 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001617 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001618 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001619 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001620 case CMD_if:
1621 case CMD_elseif:
1622 case CMD_while:
1623 case CMD_for:
1624 case CMD_echo:
1625 case CMD_echon:
1626 case CMD_execute:
1627 case CMD_echomsg:
1628 case CMD_echoerr:
1629 case CMD_call:
1630 case CMD_return:
1631 case CMD_cexpr:
1632 case CMD_caddexpr:
1633 case CMD_cgetexpr:
1634 case CMD_lexpr:
1635 case CMD_laddexpr:
1636 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001637 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001638 break;
1639
1640 case CMD_unlet:
1641 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1642 arg = xp->xp_pattern + 1;
1643
1644 xp->xp_context = EXPAND_USER_VARS;
1645 xp->xp_pattern = arg;
1646
1647 if (*xp->xp_pattern == '$')
1648 {
1649 xp->xp_context = EXPAND_ENV_VARS;
1650 ++xp->xp_pattern;
1651 }
1652
1653 break;
1654
1655 case CMD_function:
1656 case CMD_delfunction:
1657 xp->xp_context = EXPAND_USER_FUNC;
1658 xp->xp_pattern = arg;
1659 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001660 case CMD_disassemble:
1661 set_context_in_disassemble_cmd(xp, arg);
1662 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001663
1664 case CMD_echohl:
1665 set_context_in_echohl_cmd(xp, arg);
1666 break;
1667#endif
1668 case CMD_highlight:
1669 set_context_in_highlight_cmd(xp, arg);
1670 break;
1671#ifdef FEAT_CSCOPE
1672 case CMD_cscope:
1673 case CMD_lcscope:
1674 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001675 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001676 break;
1677#endif
1678#ifdef FEAT_SIGNS
1679 case CMD_sign:
1680 set_context_in_sign_cmd(xp, arg);
1681 break;
1682#endif
1683 case CMD_bdelete:
1684 case CMD_bwipeout:
1685 case CMD_bunload:
1686 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1687 arg = xp->xp_pattern + 1;
1688 // FALLTHROUGH
1689 case CMD_buffer:
1690 case CMD_sbuffer:
1691 case CMD_checktime:
1692 xp->xp_context = EXPAND_BUFFERS;
1693 xp->xp_pattern = arg;
1694 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001695#ifdef FEAT_DIFF
1696 case CMD_diffget:
1697 case CMD_diffput:
1698 // If current buffer is in diff mode, complete buffer names
1699 // which are in diff mode, and different than current buffer.
1700 xp->xp_context = EXPAND_DIFF_BUFFERS;
1701 xp->xp_pattern = arg;
1702 break;
1703#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 case CMD_USER:
1705 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001706 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1707 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001708
1709 case CMD_map: case CMD_noremap:
1710 case CMD_nmap: case CMD_nnoremap:
1711 case CMD_vmap: case CMD_vnoremap:
1712 case CMD_omap: case CMD_onoremap:
1713 case CMD_imap: case CMD_inoremap:
1714 case CMD_cmap: case CMD_cnoremap:
1715 case CMD_lmap: case CMD_lnoremap:
1716 case CMD_smap: case CMD_snoremap:
1717 case CMD_tmap: case CMD_tnoremap:
1718 case CMD_xmap: case CMD_xnoremap:
1719 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001720 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001721 case CMD_unmap:
1722 case CMD_nunmap:
1723 case CMD_vunmap:
1724 case CMD_ounmap:
1725 case CMD_iunmap:
1726 case CMD_cunmap:
1727 case CMD_lunmap:
1728 case CMD_sunmap:
1729 case CMD_tunmap:
1730 case CMD_xunmap:
1731 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001732 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001733 case CMD_mapclear:
1734 case CMD_nmapclear:
1735 case CMD_vmapclear:
1736 case CMD_omapclear:
1737 case CMD_imapclear:
1738 case CMD_cmapclear:
1739 case CMD_lmapclear:
1740 case CMD_smapclear:
1741 case CMD_tmapclear:
1742 case CMD_xmapclear:
1743 xp->xp_context = EXPAND_MAPCLEAR;
1744 xp->xp_pattern = arg;
1745 break;
1746
1747 case CMD_abbreviate: case CMD_noreabbrev:
1748 case CMD_cabbrev: case CMD_cnoreabbrev:
1749 case CMD_iabbrev: case CMD_inoreabbrev:
1750 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001751 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001752 case CMD_unabbreviate:
1753 case CMD_cunabbrev:
1754 case CMD_iunabbrev:
1755 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001756 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001757#ifdef FEAT_MENU
1758 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1759 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1760 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1761 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1762 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1763 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1764 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1765 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1766 case CMD_tmenu: case CMD_tunmenu:
1767 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1768 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1769#endif
1770
1771 case CMD_colorscheme:
1772 xp->xp_context = EXPAND_COLORS;
1773 xp->xp_pattern = arg;
1774 break;
1775
1776 case CMD_compiler:
1777 xp->xp_context = EXPAND_COMPILER;
1778 xp->xp_pattern = arg;
1779 break;
1780
1781 case CMD_ownsyntax:
1782 xp->xp_context = EXPAND_OWNSYNTAX;
1783 xp->xp_pattern = arg;
1784 break;
1785
1786 case CMD_setfiletype:
1787 xp->xp_context = EXPAND_FILETYPE;
1788 xp->xp_pattern = arg;
1789 break;
1790
1791 case CMD_packadd:
1792 xp->xp_context = EXPAND_PACKADD;
1793 xp->xp_pattern = arg;
1794 break;
1795
1796#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1797 case CMD_language:
1798 p = skiptowhite(arg);
1799 if (*p == NUL)
1800 {
1801 xp->xp_context = EXPAND_LANGUAGE;
1802 xp->xp_pattern = arg;
1803 }
1804 else
1805 {
1806 if ( STRNCMP(arg, "messages", p - arg) == 0
1807 || STRNCMP(arg, "ctype", p - arg) == 0
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02001808 || STRNCMP(arg, "time", p - arg) == 0
1809 || STRNCMP(arg, "collate", p - arg) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001810 {
1811 xp->xp_context = EXPAND_LOCALES;
1812 xp->xp_pattern = skipwhite(p);
1813 }
1814 else
1815 xp->xp_context = EXPAND_NOTHING;
1816 }
1817 break;
1818#endif
1819#if defined(FEAT_PROFILE)
1820 case CMD_profile:
1821 set_context_in_profile_cmd(xp, arg);
1822 break;
1823#endif
1824 case CMD_behave:
1825 xp->xp_context = EXPAND_BEHAVE;
1826 xp->xp_pattern = arg;
1827 break;
1828
1829 case CMD_messages:
1830 xp->xp_context = EXPAND_MESSAGES;
1831 xp->xp_pattern = arg;
1832 break;
1833
1834 case CMD_history:
1835 xp->xp_context = EXPAND_HISTORY;
1836 xp->xp_pattern = arg;
1837 break;
1838#if defined(FEAT_PROFILE)
1839 case CMD_syntime:
1840 xp->xp_context = EXPAND_SYNTIME;
1841 xp->xp_pattern = arg;
1842 break;
1843#endif
1844
1845 case CMD_argdelete:
1846 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1847 arg = xp->xp_pattern + 1;
1848 xp->xp_context = EXPAND_ARGLIST;
1849 xp->xp_pattern = arg;
1850 break;
1851
1852 default:
1853 break;
1854 }
1855 return NULL;
1856}
1857
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001858/*
1859 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
1860 * we don't need/want deleted. Maybe this could be done better if we didn't
1861 * repeat all this stuff. The only problem is that they may not stay
1862 * perfectly compatible with each other, but then the command line syntax
1863 * probably won't change that much -- webb.
1864 */
1865 static char_u *
1866set_one_cmd_context(
1867 expand_T *xp,
1868 char_u *buff) // buffer for command string
1869{
1870 char_u *p;
1871 char_u *cmd, *arg;
1872 int len = 0;
1873 exarg_T ea;
1874 int compl = EXPAND_NOTHING;
1875 int forceit = FALSE;
1876 int usefilter = FALSE; // filter instead of file name
1877
1878 ExpandInit(xp);
1879 xp->xp_pattern = buff;
1880 xp->xp_line = buff;
1881 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
1882 ea.argt = 0;
1883
1884 // 1. skip comment lines and leading space, colons or bars
1885 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
1886 ;
1887 xp->xp_pattern = cmd;
1888
1889 if (*cmd == NUL)
1890 return NULL;
1891 if (*cmd == '"') // ignore comment lines
1892 {
1893 xp->xp_context = EXPAND_NOTHING;
1894 return NULL;
1895 }
1896
1897 // 3. Skip over the range to find the command.
1898 cmd = skip_range(cmd, TRUE, &xp->xp_context);
1899 xp->xp_pattern = cmd;
1900 if (*cmd == NUL)
1901 return NULL;
1902 if (*cmd == '"')
1903 {
1904 xp->xp_context = EXPAND_NOTHING;
1905 return NULL;
1906 }
1907
1908 if (*cmd == '|' || *cmd == '\n')
1909 return cmd + 1; // There's another command
1910
1911 // Get the command index.
1912 p = set_cmd_index(cmd, &ea, xp, &compl);
1913 if (p == NULL)
1914 return NULL;
1915
1916 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
1917
1918 if (*p == '!') // forced commands
1919 {
1920 forceit = TRUE;
1921 ++p;
1922 }
1923
1924 // 6. parse arguments
1925 if (!IS_USER_CMDIDX(ea.cmdidx))
1926 ea.argt = excmd_get_argt(ea.cmdidx);
1927
1928 arg = skipwhite(p);
1929
1930 // Skip over ++argopt argument
1931 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
1932 {
1933 p = arg;
1934 while (*p && !vim_isspace(*p))
1935 MB_PTR_ADV(p);
1936 arg = skipwhite(p);
1937 }
1938
1939 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
1940 {
1941 if (*arg == '>') // append
1942 {
1943 if (*++arg == '>')
1944 ++arg;
1945 arg = skipwhite(arg);
1946 }
1947 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
1948 {
1949 ++arg;
1950 usefilter = TRUE;
1951 }
1952 }
1953
1954 if (ea.cmdidx == CMD_read)
1955 {
1956 usefilter = forceit; // :r! filter if forced
1957 if (*arg == '!') // :r !filter
1958 {
1959 ++arg;
1960 usefilter = TRUE;
1961 }
1962 }
1963
1964 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
1965 {
1966 while (*arg == *cmd) // allow any number of '>' or '<'
1967 ++arg;
1968 arg = skipwhite(arg);
1969 }
1970
1971 // Does command allow "+command"?
1972 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
1973 {
1974 // Check if we're in the +command
1975 p = arg + 1;
1976 arg = skip_cmd_arg(arg, FALSE);
1977
1978 // Still touching the command after '+'?
1979 if (*arg == NUL)
1980 return p;
1981
1982 // Skip space(s) after +command to get to the real argument
1983 arg = skipwhite(arg);
1984 }
1985
1986
1987 // Check for '|' to separate commands and '"' to start comments.
1988 // Don't do this for ":read !cmd" and ":write !cmd".
1989 if ((ea.argt & EX_TRLBAR) && !usefilter)
1990 {
1991 p = arg;
1992 // ":redir @" is not the start of a comment
1993 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
1994 p += 2;
1995 while (*p)
1996 {
1997 if (*p == Ctrl_V)
1998 {
1999 if (p[1] != NUL)
2000 ++p;
2001 }
2002 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2003 || *p == '|' || *p == '\n')
2004 {
2005 if (*(p - 1) != '\\')
2006 {
2007 if (*p == '|' || *p == '\n')
2008 return p + 1;
2009 return NULL; // It's a comment
2010 }
2011 }
2012 MB_PTR_ADV(p);
2013 }
2014 }
2015
2016 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2017 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2018 // no arguments allowed but there is something
2019 return NULL;
2020
2021 // Find start of last argument (argument just before cursor):
2022 p = buff;
2023 xp->xp_pattern = p;
2024 len = (int)STRLEN(buff);
2025 while (*p && p < buff + len)
2026 {
2027 if (*p == ' ' || *p == TAB)
2028 {
2029 // argument starts after a space
2030 xp->xp_pattern = ++p;
2031 }
2032 else
2033 {
2034 if (*p == '\\' && *(p + 1) != NUL)
2035 ++p; // skip over escaped character
2036 MB_PTR_ADV(p);
2037 }
2038 }
2039
2040 if (ea.argt & EX_XFILE)
2041 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2042
2043 // 6. Switch on command name.
2044 return set_context_by_cmdname(cmd, ea.cmdidx, arg, ea.argt, compl, xp,
2045 forceit);
2046}
2047
Bram Moolenaar66b51422019-08-18 21:44:12 +02002048 void
2049set_cmd_context(
2050 expand_T *xp,
2051 char_u *str, // start of command line
2052 int len, // length of command line (excl. NUL)
2053 int col, // position of cursor
2054 int use_ccline UNUSED) // use ccline for info
2055{
2056#ifdef FEAT_EVAL
2057 cmdline_info_T *ccline = get_cmdline_info();
2058#endif
2059 int old_char = NUL;
2060 char_u *nextcomm;
2061
2062 // Avoid a UMR warning from Purify, only save the character if it has been
2063 // written before.
2064 if (col < len)
2065 old_char = str[col];
2066 str[col] = NUL;
2067 nextcomm = str;
2068
2069#ifdef FEAT_EVAL
2070 if (use_ccline && ccline->cmdfirstc == '=')
2071 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002072 // pass CMD_SIZE because there is no real command
2073 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002074 }
2075 else if (use_ccline && ccline->input_fn)
2076 {
2077 xp->xp_context = ccline->xp_context;
2078 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002079 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002080 }
2081 else
2082#endif
2083 while (nextcomm != NULL)
2084 nextcomm = set_one_cmd_context(xp, nextcomm);
2085
2086 // Store the string here so that call_user_expand_func() can get to them
2087 // easily.
2088 xp->xp_line = str;
2089 xp->xp_col = col;
2090
2091 str[col] = old_char;
2092}
2093
2094/*
2095 * Expand the command line "str" from context "xp".
2096 * "xp" must have been set by set_cmd_context().
2097 * xp->xp_pattern points into "str", to where the text that is to be expanded
2098 * starts.
2099 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2100 * cursor.
2101 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2102 * key that triggered expansion literally.
2103 * Returns EXPAND_OK otherwise.
2104 */
2105 int
2106expand_cmdline(
2107 expand_T *xp,
2108 char_u *str, // start of command line
2109 int col, // position of cursor
2110 int *matchcount, // return: nr of matches
2111 char_u ***matches) // return: array of pointers to matches
2112{
2113 char_u *file_str = NULL;
2114 int options = WILD_ADD_SLASH|WILD_SILENT;
2115
2116 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2117 {
2118 beep_flush();
2119 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2120 }
2121 if (xp->xp_context == EXPAND_NOTHING)
2122 {
2123 // Caller can use the character as a normal char instead
2124 return EXPAND_NOTHING;
2125 }
2126
2127 // add star to file name, or convert to regexp if not exp. files.
2128 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
2129 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2130 if (file_str == NULL)
2131 return EXPAND_UNSUCCESSFUL;
2132
2133 if (p_wic)
2134 options += WILD_ICASE;
2135
2136 // find all files that match the description
2137 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
2138 {
2139 *matchcount = 0;
2140 *matches = NULL;
2141 }
2142 vim_free(file_str);
2143
2144 return EXPAND_OK;
2145}
2146
Bram Moolenaar66b51422019-08-18 21:44:12 +02002147/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002148 * Expand file or directory names.
2149 */
2150 static int
2151expand_files_and_dirs(
2152 expand_T *xp,
2153 char_u *pat,
2154 char_u ***file,
2155 int *num_file,
2156 int flags,
2157 int options)
2158{
2159 int free_pat = FALSE;
2160 int i;
2161 int ret;
2162
2163 // for ":set path=" and ":set tags=" halve backslashes for escaped
2164 // space
2165 if (xp->xp_backslash != XP_BS_NONE)
2166 {
2167 free_pat = TRUE;
2168 pat = vim_strsave(pat);
2169 for (i = 0; pat[i]; ++i)
2170 if (pat[i] == '\\')
2171 {
2172 if (xp->xp_backslash == XP_BS_THREE
2173 && pat[i + 1] == '\\'
2174 && pat[i + 2] == '\\'
2175 && pat[i + 3] == ' ')
2176 STRMOVE(pat + i, pat + i + 3);
2177 if (xp->xp_backslash == XP_BS_ONE
2178 && pat[i + 1] == ' ')
2179 STRMOVE(pat + i, pat + i + 1);
2180 }
2181 }
2182
2183 if (xp->xp_context == EXPAND_FILES)
2184 flags |= EW_FILE;
2185 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2186 flags |= (EW_FILE | EW_PATH);
2187 else
2188 flags = (flags | EW_DIR) & ~EW_FILE;
2189 if (options & WILD_ICASE)
2190 flags |= EW_ICASE;
2191
2192 // Expand wildcards, supporting %:h and the like.
2193 ret = expand_wildcards_eval(&pat, num_file, file, flags);
2194 if (free_pat)
2195 vim_free(pat);
2196#ifdef BACKSLASH_IN_FILENAME
2197 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2198 {
2199 int j;
2200
2201 for (j = 0; j < *num_file; ++j)
2202 {
2203 char_u *ptr = (*file)[j];
2204
2205 while (*ptr != NUL)
2206 {
2207 if (p_csl[0] == 's' && *ptr == '\\')
2208 *ptr = '/';
2209 else if (p_csl[0] == 'b' && *ptr == '/')
2210 *ptr = '\\';
2211 ptr += (*mb_ptr2len)(ptr);
2212 }
2213 }
2214 }
2215#endif
2216 return ret;
2217}
2218
2219/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002220 * Function given to ExpandGeneric() to obtain the possible arguments of the
2221 * ":behave {mswin,xterm}" command.
2222 */
2223 static char_u *
2224get_behave_arg(expand_T *xp UNUSED, int idx)
2225{
2226 if (idx == 0)
2227 return (char_u *)"mswin";
2228 if (idx == 1)
2229 return (char_u *)"xterm";
2230 return NULL;
2231}
2232
2233/*
2234 * Function given to ExpandGeneric() to obtain the possible arguments of the
2235 * ":messages {clear}" command.
2236 */
2237 static char_u *
2238get_messages_arg(expand_T *xp UNUSED, int idx)
2239{
2240 if (idx == 0)
2241 return (char_u *)"clear";
2242 return NULL;
2243}
2244
2245 static char_u *
2246get_mapclear_arg(expand_T *xp UNUSED, int idx)
2247{
2248 if (idx == 0)
2249 return (char_u *)"<buffer>";
2250 return NULL;
2251}
2252
2253/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002254 * Do the expansion based on xp->xp_context and 'rmp'.
2255 */
2256 static int
2257ExpandOther(
2258 expand_T *xp,
2259 regmatch_T *rmp,
2260 int *num_file,
2261 char_u ***file)
2262{
2263 static struct expgen
2264 {
2265 int context;
2266 char_u *((*func)(expand_T *, int));
2267 int ic;
2268 int escaped;
2269 } tab[] =
2270 {
2271 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2272 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2273 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2274 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2275 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2276 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2277 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2278 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2279 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2280 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2281# ifdef FEAT_EVAL
2282 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2283 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2284 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2285 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2286 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2287# endif
2288# ifdef FEAT_MENU
2289 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2290 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2291# endif
2292# ifdef FEAT_SYN_HL
2293 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2294# endif
2295# ifdef FEAT_PROFILE
2296 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2297# endif
2298 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2299 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2300 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
2301# ifdef FEAT_CSCOPE
2302 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2303# endif
2304# ifdef FEAT_SIGNS
2305 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2306# endif
2307# ifdef FEAT_PROFILE
2308 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2309# endif
2310# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2311 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2312 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2313# endif
2314 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2315 {EXPAND_USER, get_users, TRUE, FALSE},
2316 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2317 };
2318 int i;
2319 int ret = FAIL;
2320
2321 // Find a context in the table and call the ExpandGeneric() with the
2322 // right function to do the expansion.
2323 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2324 {
2325 if (xp->xp_context == tab[i].context)
2326 {
2327 if (tab[i].ic)
2328 rmp->rm_ic = TRUE;
2329 ret = ExpandGeneric(xp, rmp, num_file, file,
2330 tab[i].func, tab[i].escaped);
2331 break;
2332 }
2333 }
2334
2335 return ret;
2336}
2337
2338/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002339 * Map wild expand options to flags for expand_wildcards()
2340 */
2341 static int
2342map_wildopts_to_ewflags(int options)
2343{
2344 int flags;
2345
2346 flags = EW_DIR; // include directories
2347 if (options & WILD_LIST_NOTFOUND)
2348 flags |= EW_NOTFOUND;
2349 if (options & WILD_ADD_SLASH)
2350 flags |= EW_ADDSLASH;
2351 if (options & WILD_KEEP_ALL)
2352 flags |= EW_KEEPALL;
2353 if (options & WILD_SILENT)
2354 flags |= EW_SILENT;
2355 if (options & WILD_NOERROR)
2356 flags |= EW_NOERROR;
2357 if (options & WILD_ALLLINKS)
2358 flags |= EW_ALLLINKS;
2359
2360 return flags;
2361}
2362
2363/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002364 * Do the expansion based on xp->xp_context and "pat".
2365 */
2366 static int
2367ExpandFromContext(
2368 expand_T *xp,
2369 char_u *pat,
2370 int *num_file,
2371 char_u ***file,
2372 int options) // WILD_ flags
2373{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002374 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002375 int ret;
2376 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002377 char_u *tofree = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002378
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002379 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002380
2381 if (xp->xp_context == EXPAND_FILES
2382 || xp->xp_context == EXPAND_DIRECTORIES
2383 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002384 return expand_files_and_dirs(xp, pat, file, num_file, flags, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002385
2386 *file = (char_u **)"";
2387 *num_file = 0;
2388 if (xp->xp_context == EXPAND_HELP)
2389 {
2390 // With an empty argument we would get all the help tags, which is
2391 // very slow. Get matches for "help" instead.
2392 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
2393 num_file, file, FALSE) == OK)
2394 {
2395#ifdef FEAT_MULTI_LANG
2396 cleanup_help_tags(*num_file, *file);
2397#endif
2398 return OK;
2399 }
2400 return FAIL;
2401 }
2402
Bram Moolenaar66b51422019-08-18 21:44:12 +02002403 if (xp->xp_context == EXPAND_SHELLCMD)
2404 return expand_shellcmd(pat, num_file, file, flags);
2405 if (xp->xp_context == EXPAND_OLD_SETTING)
2406 return ExpandOldSetting(num_file, file);
2407 if (xp->xp_context == EXPAND_BUFFERS)
2408 return ExpandBufnames(pat, num_file, file, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002409#ifdef FEAT_DIFF
2410 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
2411 return ExpandBufnames(pat, num_file, file, options | BUF_DIFF_FILTER);
2412#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002413 if (xp->xp_context == EXPAND_TAGS
2414 || xp->xp_context == EXPAND_TAGS_LISTFILES)
2415 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
2416 if (xp->xp_context == EXPAND_COLORS)
2417 {
2418 char *directories[] = {"colors", NULL};
2419 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
2420 directories);
2421 }
2422 if (xp->xp_context == EXPAND_COMPILER)
2423 {
2424 char *directories[] = {"compiler", NULL};
2425 return ExpandRTDir(pat, 0, num_file, file, directories);
2426 }
2427 if (xp->xp_context == EXPAND_OWNSYNTAX)
2428 {
2429 char *directories[] = {"syntax", NULL};
2430 return ExpandRTDir(pat, 0, num_file, file, directories);
2431 }
2432 if (xp->xp_context == EXPAND_FILETYPE)
2433 {
2434 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
2435 return ExpandRTDir(pat, 0, num_file, file, directories);
2436 }
2437# if defined(FEAT_EVAL)
2438 if (xp->xp_context == EXPAND_USER_LIST)
2439 return ExpandUserList(xp, num_file, file);
2440# endif
2441 if (xp->xp_context == EXPAND_PACKADD)
2442 return ExpandPackAddDir(pat, num_file, file);
2443
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002444 // When expanding a function name starting with s:, match the <SNR>nr_
2445 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002446 if ((xp->xp_context == EXPAND_USER_FUNC
2447 || xp->xp_context == EXPAND_DISASSEMBLE)
2448 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002449 {
2450 int len = (int)STRLEN(pat) + 20;
2451
2452 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002453 if (tofree == NULL)
2454 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002455 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002456 pat = tofree;
2457 }
2458
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002459 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002460 if (regmatch.regprog == NULL)
2461 return FAIL;
2462
2463 // set ignore-case according to p_ic, p_scs and pat
2464 regmatch.rm_ic = ignorecase(pat);
2465
2466 if (xp->xp_context == EXPAND_SETTINGS
2467 || xp->xp_context == EXPAND_BOOL_SETTINGS)
2468 ret = ExpandSettings(xp, &regmatch, num_file, file);
2469 else if (xp->xp_context == EXPAND_MAPPINGS)
2470 ret = ExpandMappings(&regmatch, num_file, file);
2471# if defined(FEAT_EVAL)
2472 else if (xp->xp_context == EXPAND_USER_DEFINED)
2473 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
2474# endif
2475 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002476 ret = ExpandOther(xp, &regmatch, num_file, file);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002477
2478 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002479 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002480
2481 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002482}
2483
Bram Moolenaar66b51422019-08-18 21:44:12 +02002484/*
2485 * Expand a list of names.
2486 *
2487 * Generic function for command line completion. It calls a function to
2488 * obtain strings, one by one. The strings are matched against a regexp
2489 * program. Matching strings are copied into an array, which is returned.
2490 *
2491 * Returns OK when no problems encountered, FAIL for error (out of memory).
2492 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002493 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002494ExpandGeneric(
2495 expand_T *xp,
2496 regmatch_T *regmatch,
2497 int *num_file,
2498 char_u ***file,
2499 char_u *((*func)(expand_T *, int)),
2500 // returns a string from the list
2501 int escaped)
2502{
2503 int i;
2504 int count = 0;
2505 int round;
2506 char_u *str;
2507
2508 // do this loop twice:
2509 // round == 0: count the number of matching names
2510 // round == 1: copy the matching names into allocated memory
2511 for (round = 0; round <= 1; ++round)
2512 {
2513 for (i = 0; ; ++i)
2514 {
2515 str = (*func)(xp, i);
2516 if (str == NULL) // end of list
2517 break;
2518 if (*str == NUL) // skip empty strings
2519 continue;
2520
2521 if (vim_regexec(regmatch, str, (colnr_T)0))
2522 {
2523 if (round)
2524 {
2525 if (escaped)
2526 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2527 else
2528 str = vim_strsave(str);
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002529 if (str == NULL)
2530 {
2531 FreeWild(count, *file);
2532 *num_file = 0;
2533 *file = NULL;
2534 return FAIL;
2535 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002536 (*file)[count] = str;
2537# ifdef FEAT_MENU
2538 if (func == get_menu_names && str != NULL)
2539 {
2540 // test for separator added by get_menu_names()
2541 str += STRLEN(str) - 1;
2542 if (*str == '\001')
2543 *str = '.';
2544 }
2545# endif
2546 }
2547 ++count;
2548 }
2549 }
2550 if (round == 0)
2551 {
2552 if (count == 0)
2553 return OK;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002554 *file = ALLOC_MULT(char_u *, count);
2555 if (*file == NULL)
2556 {
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002557 *num_file = 0;
2558 *file = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002559 return FAIL;
2560 }
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002561 *num_file = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002562 count = 0;
2563 }
2564 }
2565
2566 // Sort the results. Keep menu's in the specified order.
2567 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2568 {
2569 if (xp->xp_context == EXPAND_EXPRESSION
2570 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002571 || xp->xp_context == EXPAND_USER_FUNC
2572 || xp->xp_context == EXPAND_DISASSEMBLE)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002573 // <SNR> functions should be sorted to the end.
2574 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
2575 sort_func_compare);
2576 else
2577 sort_strings(*file, *num_file);
2578 }
2579
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002580#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002581 // Reset the variables used for special highlight names expansion, so that
2582 // they don't show up when getting normal highlight names by ID.
2583 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002584#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002585 return OK;
2586}
2587
2588/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002589 * Expand shell command matches in one directory of $PATH.
2590 */
2591 static void
2592expand_shellcmd_onedir(
2593 char_u *buf,
2594 char_u *s,
2595 size_t l,
2596 char_u *pat,
2597 char_u ***files,
2598 int *num_files,
2599 int flags,
2600 hashtab_T *ht,
2601 garray_T *gap)
2602{
2603 int ret;
2604 int i;
2605 hash_T hash;
2606 hashitem_T *hi;
2607
2608 vim_strncpy(buf, s, l);
2609 add_pathsep(buf);
2610 l = STRLEN(buf);
2611 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2612
2613 // Expand matches in one directory of $PATH.
2614 ret = expand_wildcards(1, &buf, num_files, files, flags);
2615 if (ret == OK)
2616 {
2617 if (ga_grow(gap, *num_files) == FAIL)
2618 FreeWild(*num_files, *files);
2619 else
2620 {
2621 for (i = 0; i < *num_files; ++i)
2622 {
2623 char_u *name = (*files)[i];
2624
2625 if (STRLEN(name) > l)
2626 {
2627 // Check if this name was already found.
2628 hash = hash_hash(name + l);
2629 hi = hash_lookup(ht, name + l, hash);
2630 if (HASHITEM_EMPTY(hi))
2631 {
2632 // Remove the path that was prepended.
2633 STRMOVE(name, name + l);
2634 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2635 hash_add_item(ht, hi, name, hash);
2636 name = NULL;
2637 }
2638 }
2639 vim_free(name);
2640 }
2641 vim_free(*files);
2642 }
2643 }
2644}
2645
2646/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002647 * Complete a shell command.
2648 * Returns FAIL or OK;
2649 */
2650 static int
2651expand_shellcmd(
2652 char_u *filepat, // pattern to match with command names
2653 int *num_file, // return: number of matches
2654 char_u ***file, // return: array with matches
2655 int flagsarg) // EW_ flags
2656{
2657 char_u *pat;
2658 int i;
2659 char_u *path = NULL;
2660 int mustfree = FALSE;
2661 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002662 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002663 size_t l;
2664 char_u *s, *e;
2665 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002666 int did_curdir = FALSE;
2667 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002668
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002669 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002670 if (buf == NULL)
2671 return FAIL;
2672
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002673 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002674 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002675 if (pat == NULL)
2676 {
2677 vim_free(buf);
2678 return FAIL;
2679 }
2680
Bram Moolenaar66b51422019-08-18 21:44:12 +02002681 for (i = 0; pat[i]; ++i)
2682 if (pat[i] == '\\' && pat[i + 1] == ' ')
2683 STRMOVE(pat + i, pat + i + 1);
2684
2685 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2686
2687 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2688 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2689 path = (char_u *)".";
2690 else
2691 {
2692 // For an absolute name we don't use $PATH.
2693 if (!mch_isFullName(pat))
2694 path = vim_getenv((char_u *)"PATH", &mustfree);
2695 if (path == NULL)
2696 path = (char_u *)"";
2697 }
2698
2699 // Go over all directories in $PATH. Expand matches in that directory and
2700 // collect them in "ga". When "." is not in $PATH also expand for the
2701 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002702 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002703 hash_init(&found_ht);
2704 for (s = path; ; s = e)
2705 {
2706# if defined(MSWIN)
2707 e = vim_strchr(s, ';');
2708# else
2709 e = vim_strchr(s, ':');
2710# endif
2711 if (e == NULL)
2712 e = s + STRLEN(s);
2713
2714 if (*s == NUL)
2715 {
2716 if (did_curdir)
2717 break;
2718 // Find directories in the current directory, path is empty.
2719 did_curdir = TRUE;
2720 flags |= EW_DIR;
2721 }
2722 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2723 {
2724 did_curdir = TRUE;
2725 flags |= EW_DIR;
2726 }
2727 else
2728 // Do not match directories inside a $PATH item.
2729 flags &= ~EW_DIR;
2730
2731 l = e - s;
2732 if (l > MAXPATHL - 5)
2733 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002734
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002735 expand_shellcmd_onedir(buf, s, l, pat, file, num_file, flags,
2736 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002737
Bram Moolenaar66b51422019-08-18 21:44:12 +02002738 if (*e != NUL)
2739 ++e;
2740 }
2741 *file = ga.ga_data;
2742 *num_file = ga.ga_len;
2743
2744 vim_free(buf);
2745 vim_free(pat);
2746 if (mustfree)
2747 vim_free(path);
2748 hash_clear(&found_ht);
2749 return OK;
2750}
2751
2752# if defined(FEAT_EVAL)
2753/*
2754 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002755 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002756 */
2757 static void *
2758call_user_expand_func(
2759 void *(*user_expand_func)(char_u *, int, typval_T *),
2760 expand_T *xp,
2761 int *num_file,
2762 char_u ***file)
2763{
2764 cmdline_info_T *ccline = get_cmdline_info();
2765 int keep = 0;
2766 typval_T args[4];
2767 sctx_T save_current_sctx = current_sctx;
2768 char_u *pat = NULL;
2769 void *ret;
2770
2771 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2772 return NULL;
2773 *num_file = 0;
2774 *file = NULL;
2775
2776 if (ccline->cmdbuff != NULL)
2777 {
2778 keep = ccline->cmdbuff[ccline->cmdlen];
2779 ccline->cmdbuff[ccline->cmdlen] = 0;
2780 }
2781
2782 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2783
2784 args[0].v_type = VAR_STRING;
2785 args[0].vval.v_string = pat;
2786 args[1].v_type = VAR_STRING;
2787 args[1].vval.v_string = xp->xp_line;
2788 args[2].v_type = VAR_NUMBER;
2789 args[2].vval.v_number = xp->xp_col;
2790 args[3].v_type = VAR_UNKNOWN;
2791
2792 current_sctx = xp->xp_script_ctx;
2793
2794 ret = user_expand_func(xp->xp_arg, 3, args);
2795
2796 current_sctx = save_current_sctx;
2797 if (ccline->cmdbuff != NULL)
2798 ccline->cmdbuff[ccline->cmdlen] = keep;
2799
2800 vim_free(pat);
2801 return ret;
2802}
2803
2804/*
2805 * Expand names with a function defined by the user.
2806 */
2807 static int
2808ExpandUserDefined(
2809 expand_T *xp,
2810 regmatch_T *regmatch,
2811 int *num_file,
2812 char_u ***file)
2813{
2814 char_u *retstr;
2815 char_u *s;
2816 char_u *e;
2817 int keep;
2818 garray_T ga;
2819 int skip;
2820
2821 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
2822 if (retstr == NULL)
2823 return FAIL;
2824
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002825 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002826 for (s = retstr; *s != NUL; s = e)
2827 {
2828 e = vim_strchr(s, '\n');
2829 if (e == NULL)
2830 e = s + STRLEN(s);
2831 keep = *e;
2832 *e = NUL;
2833
2834 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2835 *e = keep;
2836
2837 if (!skip)
2838 {
2839 if (ga_grow(&ga, 1) == FAIL)
2840 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02002841 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002842 ++ga.ga_len;
2843 }
2844
2845 if (*e != NUL)
2846 ++e;
2847 }
2848 vim_free(retstr);
2849 *file = ga.ga_data;
2850 *num_file = ga.ga_len;
2851 return OK;
2852}
2853
2854/*
2855 * Expand names with a list returned by a function defined by the user.
2856 */
2857 static int
2858ExpandUserList(
2859 expand_T *xp,
2860 int *num_file,
2861 char_u ***file)
2862{
2863 list_T *retlist;
2864 listitem_T *li;
2865 garray_T ga;
2866
2867 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
2868 if (retlist == NULL)
2869 return FAIL;
2870
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002871 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002872 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002873 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002874 {
2875 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
2876 continue; // Skip non-string items and empty strings
2877
2878 if (ga_grow(&ga, 1) == FAIL)
2879 break;
2880
2881 ((char_u **)ga.ga_data)[ga.ga_len] =
2882 vim_strsave(li->li_tv.vval.v_string);
2883 ++ga.ga_len;
2884 }
2885 list_unref(retlist);
2886
2887 *file = ga.ga_data;
2888 *num_file = ga.ga_len;
2889 return OK;
2890}
2891# endif
2892
2893/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002894 * Expand "file" for all comma-separated directories in "path".
2895 * Adds the matches to "ga". Caller must init "ga".
2896 */
2897 void
2898globpath(
2899 char_u *path,
2900 char_u *file,
2901 garray_T *ga,
2902 int expand_options)
2903{
2904 expand_T xpc;
2905 char_u *buf;
2906 int i;
2907 int num_p;
2908 char_u **p;
2909
2910 buf = alloc(MAXPATHL);
2911 if (buf == NULL)
2912 return;
2913
2914 ExpandInit(&xpc);
2915 xpc.xp_context = EXPAND_FILES;
2916
2917 // Loop over all entries in {path}.
2918 while (*path != NUL)
2919 {
2920 // Copy one item of the path to buf[] and concatenate the file name.
2921 copy_option_part(&path, buf, MAXPATHL, ",");
2922 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
2923 {
2924# if defined(MSWIN)
2925 // Using the platform's path separator (\) makes vim incorrectly
2926 // treat it as an escape character, use '/' instead.
2927 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
2928 STRCAT(buf, "/");
2929# else
2930 add_pathsep(buf);
2931# endif
2932 STRCAT(buf, file);
2933 if (ExpandFromContext(&xpc, buf, &num_p, &p,
2934 WILD_SILENT|expand_options) != FAIL && num_p > 0)
2935 {
2936 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
2937
2938 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002939 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02002940 for (i = 0; i < num_p; ++i)
2941 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002942 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02002943 ++ga->ga_len;
2944 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01002945 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002946 }
2947 }
2948 }
2949
2950 vim_free(buf);
2951}
Bram Moolenaar66b51422019-08-18 21:44:12 +02002952
Bram Moolenaareadee482020-09-04 15:37:31 +02002953#ifdef FEAT_WILDMENU
2954
2955/*
2956 * Translate some keys pressed when 'wildmenu' is used.
2957 */
2958 int
2959wildmenu_translate_key(
2960 cmdline_info_T *cclp,
2961 int key,
2962 expand_T *xp,
2963 int did_wild_list)
2964{
2965 int c = key;
2966
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002967#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002968 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002969 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002970 // When the popup menu is used for cmdline completion:
2971 // Up : go to the previous item in the menu
2972 // Down : go to the next item in the menu
2973 // Left : go to the parent directory
2974 // Right: list the files in the selected directory
2975 switch (c)
2976 {
2977 case K_UP: c = K_LEFT; break;
2978 case K_DOWN: c = K_RIGHT; break;
2979 case K_LEFT: c = K_UP; break;
2980 case K_RIGHT: c = K_DOWN; break;
2981 default: break;
2982 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002983 }
2984#endif
2985
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002986 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02002987 {
2988 if (c == K_LEFT)
2989 c = Ctrl_P;
2990 else if (c == K_RIGHT)
2991 c = Ctrl_N;
2992 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002993
Bram Moolenaareadee482020-09-04 15:37:31 +02002994 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002995 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02002996 && cclp->cmdpos > 1
2997 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
2998 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
2999 && (c == '\n' || c == '\r' || c == K_KENTER))
3000 c = K_DOWN;
3001
3002 return c;
3003}
3004
3005/*
3006 * Delete characters on the command line, from "from" to the current
3007 * position.
3008 */
3009 static void
3010cmdline_del(cmdline_info_T *cclp, int from)
3011{
3012 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3013 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3014 cclp->cmdlen -= cclp->cmdpos - from;
3015 cclp->cmdpos = from;
3016}
3017
3018/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003019 * Handle a key pressed when the wild menu for the menu names
3020 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003021 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003022 static int
3023wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003024{
Bram Moolenaareadee482020-09-04 15:37:31 +02003025 int i;
3026 int j;
3027
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003028 // Hitting <Down> after "emenu Name.": complete submenu
3029 if (key == K_DOWN && cclp->cmdpos > 0
3030 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003031 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003032 key = p_wc;
3033 KeyTyped = TRUE; // in case the key was mapped
3034 }
3035 else if (key == K_UP)
3036 {
3037 // Hitting <Up>: Remove one submenu name in front of the
3038 // cursor
3039 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003040
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003041 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3042 i = 0;
3043 while (--j > 0)
3044 {
3045 // check for start of menu name
3046 if (cclp->cmdbuff[j] == ' '
3047 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003048 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003049 i = j + 1;
3050 break;
3051 }
3052 // check for start of submenu name
3053 if (cclp->cmdbuff[j] == '.'
3054 && cclp->cmdbuff[j - 1] != '\\')
3055 {
3056 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003057 {
3058 i = j + 1;
3059 break;
3060 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003061 else
3062 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003063 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003064 }
3065 if (i > 0)
3066 cmdline_del(cclp, i);
3067 key = p_wc;
3068 KeyTyped = TRUE; // in case the key was mapped
3069 xp->xp_context = EXPAND_NOTHING;
3070 }
3071
3072 return key;
3073}
3074
3075/*
3076 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3077 * directory names (EXPAND_DIRECTORIES) or shell command names
3078 * (EXPAND_SHELLCMD) is displayed.
3079 */
3080 static int
3081wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3082{
3083 int i;
3084 int j;
3085 char_u upseg[5];
3086
3087 upseg[0] = PATHSEP;
3088 upseg[1] = '.';
3089 upseg[2] = '.';
3090 upseg[3] = PATHSEP;
3091 upseg[4] = NUL;
3092
3093 if (key == K_DOWN
3094 && cclp->cmdpos > 0
3095 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3096 && (cclp->cmdpos < 3
3097 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3098 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3099 {
3100 // go down a directory
3101 key = p_wc;
3102 KeyTyped = TRUE; // in case the key was mapped
3103 }
3104 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3105 {
3106 // If in a direct ancestor, strip off one ../ to go down
3107 int found = FALSE;
3108
3109 j = cclp->cmdpos;
3110 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3111 while (--j > i)
3112 {
3113 if (has_mbyte)
3114 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3115 if (vim_ispathsep(cclp->cmdbuff[j]))
3116 {
3117 found = TRUE;
3118 break;
3119 }
3120 }
3121 if (found
3122 && cclp->cmdbuff[j - 1] == '.'
3123 && cclp->cmdbuff[j - 2] == '.'
3124 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3125 {
3126 cmdline_del(cclp, j - 2);
3127 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003128 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003129 }
3130 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003131 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003132 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003133 // go up a directory
3134 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003135
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003136 j = cclp->cmdpos - 1;
3137 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3138 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003139 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003140 if (has_mbyte)
3141 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3142 if (vim_ispathsep(cclp->cmdbuff[j])
3143# ifdef BACKSLASH_IN_FILENAME
3144 && vim_strchr((char_u *)" *?[{`$%#",
3145 cclp->cmdbuff[j + 1]) == NULL
3146# endif
3147 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003148 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003149 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003150 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003151 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003152 break;
3153 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003154 else
3155 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003156 }
3157 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003158
3159 if (!found)
3160 j = i;
3161 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3162 j += 4;
3163 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3164 && j == i)
3165 j += 3;
3166 else
3167 j = 0;
3168 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003169 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003170 // TODO this is only for DOS/UNIX systems - need to put in
3171 // machine-specific stuff here and in upseg init
3172 cmdline_del(cclp, j);
3173 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003174 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003175 else if (cclp->cmdpos > i)
3176 cmdline_del(cclp, i);
3177
3178 // Now complete in the new directory. Set KeyTyped in case the
3179 // Up key came from a mapping.
3180 key = p_wc;
3181 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003182 }
3183
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003184 return key;
3185}
3186
3187/*
3188 * Handle a key pressed when the wild menu is displayed
3189 */
3190 int
3191wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3192{
3193 if (xp->xp_context == EXPAND_MENUNAMES)
3194 return wildmenu_process_key_menunames(cclp, key, xp);
3195 else if ((xp->xp_context == EXPAND_FILES
3196 || xp->xp_context == EXPAND_DIRECTORIES
3197 || xp->xp_context == EXPAND_SHELLCMD))
3198 return wildmenu_process_key_filenames(cclp, key, xp);
3199
3200 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003201}
3202
3203/*
3204 * Free expanded names when finished walking through the matches
3205 */
3206 void
3207wildmenu_cleanup(cmdline_info_T *cclp)
3208{
3209 int skt = KeyTyped;
3210 int old_RedrawingDisabled = RedrawingDisabled;
3211
3212 if (!p_wmnu || wild_menu_showing == 0)
3213 return;
3214
3215 if (cclp->input_fn)
3216 RedrawingDisabled = 0;
3217
3218 if (wild_menu_showing == WM_SCROLLED)
3219 {
3220 // Entered command line, move it up
3221 cmdline_row--;
3222 redrawcmd();
3223 }
3224 else if (save_p_ls != -1)
3225 {
3226 // restore 'laststatus' and 'winminheight'
3227 p_ls = save_p_ls;
3228 p_wmh = save_p_wmh;
3229 last_status(FALSE);
3230 update_screen(VALID); // redraw the screen NOW
3231 redrawcmd();
3232 save_p_ls = -1;
3233 }
3234 else
3235 {
3236 win_redraw_last_status(topframe);
3237 redraw_statuslines();
3238 }
3239 KeyTyped = skt;
3240 wild_menu_showing = 0;
3241 if (cclp->input_fn)
3242 RedrawingDisabled = old_RedrawingDisabled;
3243}
3244#endif
3245
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003246#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003247/*
3248 * "getcompletion()" function
3249 */
3250 void
3251f_getcompletion(typval_T *argvars, typval_T *rettv)
3252{
3253 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003254 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003255 expand_T xpc;
3256 int filtered = FALSE;
3257 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003258 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003259
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003260 if (in_vim9script()
3261 && (check_for_string_arg(argvars, 0) == FAIL
3262 || check_for_string_arg(argvars, 1) == FAIL
3263 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3264 return;
3265
ii144785fe02021-11-21 12:13:56 +00003266 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003267 if (argvars[1].v_type != VAR_STRING)
3268 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003269 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003270 return;
3271 }
3272 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003273
3274 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003275 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003276
3277 if (p_wic)
3278 options |= WILD_ICASE;
3279
3280 // For filtered results, 'wildignore' is used
3281 if (!filtered)
3282 options |= WILD_KEEP_ALL;
3283
3284 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003285 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003286 {
ii144785fe02021-11-21 12:13:56 +00003287 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003288 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003289 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003290 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003291 else
3292 {
ii144785fe02021-11-21 12:13:56 +00003293 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003294 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3295
3296 xpc.xp_context = cmdcomplete_str_to_type(type);
3297 if (xpc.xp_context == EXPAND_NOTHING)
3298 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003299 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003300 return;
3301 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003302
3303# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003304 if (xpc.xp_context == EXPAND_MENUS)
3305 {
3306 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3307 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3308 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003309# endif
3310# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003311 if (xpc.xp_context == EXPAND_CSCOPE)
3312 {
3313 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3314 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3315 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003316# endif
3317# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003318 if (xpc.xp_context == EXPAND_SIGN)
3319 {
3320 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3321 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3322 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003323# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003324 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003325
3326 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3327 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3328 {
3329 int i;
3330
3331 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3332
3333 for (i = 0; i < xpc.xp_numfiles; i++)
3334 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3335 }
3336 vim_free(pat);
3337 ExpandCleanup(&xpc);
3338}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003339#endif // FEAT_EVAL