blob: 138ff9ef814bd5ff5b736d5ff0e4bb3e67853888 [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,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000020 char_u ***matches, int *numMatches,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000021 char_u *((*func)(expand_T *, int)), int escaped,
22 char_u *fuzzystr);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000023static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaar66b51422019-08-18 21:44:12 +020024static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000025static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020026#if defined(FEAT_EVAL)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
28static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020029#endif
30
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000031#ifdef FEAT_WILDMENU
32// "compl_match_array" points the currently displayed list of entries in the
33// popup menu. It is NULL when there is no popup menu.
34static pumitem_T *compl_match_array = NULL;
35static int compl_match_arraysize;
36// First column in cmdline of the matched item for completion.
37static int compl_startcol;
38static int compl_selected;
39#endif
40
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000041#define SHOW_FILE_TEXT(m) (showtail ? sm_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000042
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000043/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000044 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
45 * context.
46 */
47 static int
48cmdline_fuzzy_completion_supported(expand_T *xp)
49{
50 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
51 && xp->xp_context != EXPAND_BOOL_SETTINGS
52 && xp->xp_context != EXPAND_COLORS
53 && xp->xp_context != EXPAND_COMPILER
54 && xp->xp_context != EXPAND_DIRECTORIES
55 && xp->xp_context != EXPAND_FILES
56 && xp->xp_context != EXPAND_FILES_IN_PATH
57 && xp->xp_context != EXPAND_FILETYPE
58 && xp->xp_context != EXPAND_HELP
59 && xp->xp_context != EXPAND_MAPPINGS
60 && xp->xp_context != EXPAND_OLD_SETTING
61 && xp->xp_context != EXPAND_OWNSYNTAX
62 && xp->xp_context != EXPAND_PACKADD
63 && xp->xp_context != EXPAND_SHELLCMD
64 && xp->xp_context != EXPAND_TAGS
65 && xp->xp_context != EXPAND_TAGS_LISTFILES
66 && xp->xp_context != EXPAND_USER_DEFINED
67 && xp->xp_context != EXPAND_USER_LIST);
68}
69
70/*
71 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
72 * 'fuzzystr' is not empty.
73 */
74 int
75cmdline_fuzzy_complete(char_u *fuzzystr)
76{
77 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
78}
79
80/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000081 * sort function for the completion matches.
82 * <SNR> functions should be sorted to the end.
83 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020084 static int
85sort_func_compare(const void *s1, const void *s2)
86{
87 char_u *p1 = *(char_u **)s1;
88 char_u *p2 = *(char_u **)s2;
89
90 if (*p1 != '<' && *p2 == '<') return -1;
91 if (*p1 == '<' && *p2 != '<') return 1;
92 return STRCMP(p1, p2);
93}
Bram Moolenaar66b51422019-08-18 21:44:12 +020094
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000095/*
96 * Escape special characters in the cmdline completion matches.
97 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020098 static void
99ExpandEscape(
100 expand_T *xp,
101 char_u *str,
102 int numfiles,
103 char_u **files,
104 int options)
105{
106 int i;
107 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100108 int vse_what = xp->xp_context == EXPAND_BUFFERS
109 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200110
111 // May change home directory back to "~"
112 if (options & WILD_HOME_REPLACE)
113 tilde_replace(str, numfiles, files);
114
115 if (options & WILD_ESCAPE)
116 {
117 if (xp->xp_context == EXPAND_FILES
118 || xp->xp_context == EXPAND_FILES_IN_PATH
119 || xp->xp_context == EXPAND_SHELLCMD
120 || xp->xp_context == EXPAND_BUFFERS
121 || xp->xp_context == EXPAND_DIRECTORIES)
122 {
123 // Insert a backslash into a file name before a space, \, %, #
124 // and wildmatch characters, except '~'.
125 for (i = 0; i < numfiles; ++i)
126 {
127 // for ":set path=" we need to escape spaces twice
128 if (xp->xp_backslash == XP_BS_THREE)
129 {
130 p = vim_strsave_escaped(files[i], (char_u *)" ");
131 if (p != NULL)
132 {
133 vim_free(files[i]);
134 files[i] = p;
135#if defined(BACKSLASH_IN_FILENAME)
136 p = vim_strsave_escaped(files[i], (char_u *)" ");
137 if (p != NULL)
138 {
139 vim_free(files[i]);
140 files[i] = p;
141 }
142#endif
143 }
144 }
145#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100146 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200147#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100148 p = vim_strsave_fnameescape(files[i],
149 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200150#endif
151 if (p != NULL)
152 {
153 vim_free(files[i]);
154 files[i] = p;
155 }
156
157 // If 'str' starts with "\~", replace "~" at start of
158 // files[i] with "\~".
159 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
160 escape_fname(&files[i]);
161 }
162 xp->xp_backslash = XP_BS_NONE;
163
164 // If the first file starts with a '+' escape it. Otherwise it
165 // could be seen as "+cmd".
166 if (*files[0] == '+')
167 escape_fname(&files[0]);
168 }
169 else if (xp->xp_context == EXPAND_TAGS)
170 {
171 // Insert a backslash before characters in a tag name that
172 // would terminate the ":tag" command.
173 for (i = 0; i < numfiles; ++i)
174 {
175 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
176 if (p != NULL)
177 {
178 vim_free(files[i]);
179 files[i] = p;
180 }
181 }
182 }
183 }
184}
185
186/*
187 * Return FAIL if this is not an appropriate context in which to do
188 * completion of anything, return OK if it is (even if there are no matches).
189 * For the caller, this means that the character is just passed through like a
190 * normal character (instead of being expanded). This allows :s/^I^D etc.
191 */
192 int
193nextwild(
194 expand_T *xp,
195 int type,
196 int options, // extra options for ExpandOne()
197 int escape) // if TRUE, escape the returned matches
198{
199 cmdline_info_T *ccline = get_cmdline_info();
200 int i, j;
201 char_u *p1;
202 char_u *p2;
203 int difflen;
204 int v;
205
206 if (xp->xp_numfiles == -1)
207 {
208 set_expand_context(xp);
209 cmd_showtail = expand_showtail(xp);
210 }
211
212 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
213 {
214 beep_flush();
215 return OK; // Something illegal on command line
216 }
217 if (xp->xp_context == EXPAND_NOTHING)
218 {
219 // Caller can use the character as a normal char instead
220 return FAIL;
221 }
222
223 msg_puts("..."); // show that we are busy
224 out_flush();
225
226 i = (int)(xp->xp_pattern - ccline->cmdbuff);
227 xp->xp_pattern_len = ccline->cmdpos - i;
228
229 if (type == WILD_NEXT || type == WILD_PREV)
230 {
231 // Get next/previous match for a previous expanded pattern.
232 p2 = ExpandOne(xp, NULL, NULL, 0, type);
233 }
234 else
235 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000236 if (cmdline_fuzzy_completion_supported(xp))
237 // If fuzzy matching, don't modify the search string
238 p1 = vim_strsave(xp->xp_pattern);
239 else
240 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
241
Bram Moolenaar66b51422019-08-18 21:44:12 +0200242 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000243 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200244 p2 = NULL;
245 else
246 {
247 int use_options = options |
248 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
249 if (escape)
250 use_options |= WILD_ESCAPE;
251
252 if (p_wic)
253 use_options += WILD_ICASE;
254 p2 = ExpandOne(xp, p1,
255 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
256 use_options, type);
257 vim_free(p1);
258 // longest match: make sure it is not shorter, happens with :help
259 if (p2 != NULL && type == WILD_LONGEST)
260 {
261 for (j = 0; j < xp->xp_pattern_len; ++j)
262 if (ccline->cmdbuff[i + j] == '*'
263 || ccline->cmdbuff[i + j] == '?')
264 break;
265 if ((int)STRLEN(p2) < j)
266 VIM_CLEAR(p2);
267 }
268 }
269 }
270
271 if (p2 != NULL && !got_int)
272 {
273 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
274 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
275 {
276 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
277 xp->xp_pattern = ccline->cmdbuff + i;
278 }
279 else
280 v = OK;
281 if (v == OK)
282 {
283 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
284 &ccline->cmdbuff[ccline->cmdpos],
285 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
286 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
287 ccline->cmdlen += difflen;
288 ccline->cmdpos += difflen;
289 }
290 }
291 vim_free(p2);
292
293 redrawcmd();
294 cursorcmd();
295
296 // When expanding a ":map" command and no matches are found, assume that
297 // the key is supposed to be inserted literally
298 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
299 return FAIL;
300
301 if (xp->xp_numfiles <= 0 && p2 == NULL)
302 beep_flush();
303 else if (xp->xp_numfiles == 1)
304 // free expanded pattern
305 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
306
307 return OK;
308}
309
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000310#if defined(FEAT_WILDMENU) || defined(PROTO)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000311
312/*
313 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000314 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000315 */
316 static int
317cmdline_pum_create(
318 cmdline_info_T *ccline,
319 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000320 char_u **matches,
321 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000322 int showtail)
323{
324 int i;
325 int columns;
326
327 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000328 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000329 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000330 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000331 {
332 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
333 compl_match_array[i].pum_info = NULL;
334 compl_match_array[i].pum_extra = NULL;
335 compl_match_array[i].pum_kind = NULL;
336 }
337
338 // Compute the popup menu starting column
339 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
340 columns = vim_strsize(xp->xp_pattern);
341 if (showtail)
342 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000343 columns += vim_strsize(sm_gettail(matches[0]));
344 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000345 }
346 if (columns >= compl_startcol)
347 compl_startcol = 0;
348 else
349 compl_startcol -= columns;
350
351 // no default selection
352 compl_selected = -1;
353
354 cmdline_pum_display();
355
356 return EXPAND_OK;
357}
358
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000359/*
360 * Display the cmdline completion matches in a popup menu
361 */
362void cmdline_pum_display(void)
363{
364 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
365}
366
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000367/*
368 * Returns TRUE if the cmdline completion popup menu is being displayed.
369 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000370int cmdline_pum_active(void)
371{
372 return p_wmnu && pum_visible() && compl_match_array != NULL;
373}
374
375/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000376 * Remove the cmdline completion popup menu (if present), free the list of
377 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000378 */
379void cmdline_pum_remove(void)
380{
381 pum_undisplay();
382 VIM_CLEAR(compl_match_array);
383 update_screen(0);
Bram Moolenaar414acd32022-02-10 21:09:45 +0000384 redrawcmd();
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000385}
386
387void cmdline_pum_cleanup(cmdline_info_T *cclp)
388{
389 cmdline_pum_remove();
390 wildmenu_cleanup(cclp);
391}
392
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000393/*
394 * Returns the starting column number to use for the cmdline completion popup
395 * menu.
396 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000397int cmdline_compl_startcol(void)
398{
399 return compl_startcol;
400}
401#endif
402
Bram Moolenaar66b51422019-08-18 21:44:12 +0200403/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000404 * Get the next or prev cmdline completion match. The index of the match is set
405 * in 'p_findex'
406 */
407 static char_u *
408get_next_or_prev_match(
409 int mode,
410 expand_T *xp,
411 int *p_findex,
412 char_u *orig_save)
413{
414 int findex = *p_findex;
415
416 if (xp->xp_numfiles <= 0)
417 return NULL;
418
419 if (mode == WILD_PREV)
420 {
421 if (findex == -1)
422 findex = xp->xp_numfiles;
423 --findex;
424 }
425 else // mode == WILD_NEXT
426 ++findex;
427
428 // When wrapping around, return the original string, set findex to
429 // -1.
430 if (findex < 0)
431 {
432 if (orig_save == NULL)
433 findex = xp->xp_numfiles - 1;
434 else
435 findex = -1;
436 }
437 if (findex >= xp->xp_numfiles)
438 {
439 if (orig_save == NULL)
440 findex = 0;
441 else
442 findex = -1;
443 }
444#ifdef FEAT_WILDMENU
445 if (compl_match_array)
446 {
447 compl_selected = findex;
448 cmdline_pum_display();
449 }
450 else if (p_wmnu)
451 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
452 findex, cmd_showtail);
453#endif
454 *p_findex = findex;
455
456 if (findex == -1)
457 return vim_strsave(orig_save);
458
459 return vim_strsave(xp->xp_files[findex]);
460}
461
462/*
463 * Start the command-line expansion and get the matches.
464 */
465 static char_u *
466ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
467{
468 int non_suf_match; // number without matching suffix
469 int i;
470 char_u *ss = NULL;
471
472 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000473 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000474 options) == FAIL)
475 {
476#ifdef FNAME_ILLEGAL
477 // Illegal file name has been silently skipped. But when there
478 // are wildcards, the real problem is that there was no match,
479 // causing the pattern to be added, which has illegal characters.
480 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
481 semsg(_(e_no_match_str_2), str);
482#endif
483 }
484 else if (xp->xp_numfiles == 0)
485 {
486 if (!(options & WILD_SILENT))
487 semsg(_(e_no_match_str_2), str);
488 }
489 else
490 {
491 // Escape the matches for use on the command line.
492 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
493
494 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000495 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000496 {
497 if (xp->xp_numfiles)
498 non_suf_match = xp->xp_numfiles;
499 else
500 non_suf_match = 1;
501 if ((xp->xp_context == EXPAND_FILES
502 || xp->xp_context == EXPAND_DIRECTORIES)
503 && xp->xp_numfiles > 1)
504 {
505 // More than one match; check suffix.
506 // The files will have been sorted on matching suffix in
507 // expand_wildcards, only need to check the first two.
508 non_suf_match = 0;
509 for (i = 0; i < 2; ++i)
510 if (match_suffix(xp->xp_files[i]))
511 ++non_suf_match;
512 }
513 if (non_suf_match != 1)
514 {
515 // Can we ever get here unless it's while expanding
516 // interactively? If not, we can get rid of this all
517 // together. Don't really want to wait for this message
518 // (and possibly have to hit return to continue!).
519 if (!(options & WILD_SILENT))
520 emsg(_(e_too_many_file_names));
521 else if (!(options & WILD_NO_BEEP))
522 beep_flush();
523 }
524 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
525 ss = vim_strsave(xp->xp_files[0]);
526 }
527 }
528
529 return ss;
530}
531
532/*
533 * Return the longest common part in the list of cmdline completion matches.
534 */
535 static char_u *
536find_longest_match(expand_T *xp, int options)
537{
538 long_u len;
539 int mb_len = 1;
540 int c0, ci;
541 int i;
542 char_u *ss;
543
544 for (len = 0; xp->xp_files[0][len]; len += mb_len)
545 {
546 if (has_mbyte)
547 {
548 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
549 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
550 }
551 else
552 c0 = xp->xp_files[0][len];
553 for (i = 1; i < xp->xp_numfiles; ++i)
554 {
555 if (has_mbyte)
556 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
557 else
558 ci = xp->xp_files[i][len];
559 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
560 || xp->xp_context == EXPAND_FILES
561 || xp->xp_context == EXPAND_SHELLCMD
562 || xp->xp_context == EXPAND_BUFFERS))
563 {
564 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
565 break;
566 }
567 else if (c0 != ci)
568 break;
569 }
570 if (i < xp->xp_numfiles)
571 {
572 if (!(options & WILD_NO_BEEP))
573 vim_beep(BO_WILD);
574 break;
575 }
576 }
577
578 ss = alloc(len + 1);
579 if (ss)
580 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
581
582 return ss;
583}
584
585/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200586 * Do wildcard expansion on the string 'str'.
587 * Chars that should not be expanded must be preceded with a backslash.
588 * Return a pointer to allocated memory containing the new string.
589 * Return NULL for failure.
590 *
591 * "orig" is the originally expanded string, copied to allocated memory. It
592 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
593 * WILD_PREV "orig" should be NULL.
594 *
595 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
596 * is WILD_EXPAND_FREE or WILD_ALL.
597 *
598 * mode = WILD_FREE: just free previously expanded matches
599 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
600 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
601 * mode = WILD_NEXT: use next match in multiple match, wrap to first
602 * mode = WILD_PREV: use previous match in multiple match, wrap to first
603 * mode = WILD_ALL: return all matches concatenated
604 * mode = WILD_LONGEST: return longest matched part
605 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000606 * mode = WILD_APPLY: apply the item selected in the cmdline completion
607 * popup menu and close the menu.
608 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
609 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200610 *
611 * options = WILD_LIST_NOTFOUND: list entries without a match
612 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
613 * options = WILD_USE_NL: Use '\n' for WILD_ALL
614 * options = WILD_NO_BEEP: Don't beep for multiple matches
615 * options = WILD_ADD_SLASH: add a slash after directory names
616 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
617 * options = WILD_SILENT: don't print warning messages
618 * options = WILD_ESCAPE: put backslash before special chars
619 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200620 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200621 *
622 * The variables xp->xp_context and xp->xp_backslash must have been set!
623 */
624 char_u *
625ExpandOne(
626 expand_T *xp,
627 char_u *str,
628 char_u *orig, // allocated copy of original of expanded string
629 int options,
630 int mode)
631{
632 char_u *ss = NULL;
633 static int findex;
634 static char_u *orig_save = NULL; // kept value of orig
635 int orig_saved = FALSE;
636 int i;
637 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200638
639 // first handle the case of using an old match
640 if (mode == WILD_NEXT || mode == WILD_PREV)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000641 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200642
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000643 if (mode == WILD_CANCEL)
644 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
645 else if (mode == WILD_APPLY)
646 ss = vim_strsave(findex == -1 ? (orig_save ?
647 orig_save : (char_u *)"") : xp->xp_files[findex]);
648
Bram Moolenaar66b51422019-08-18 21:44:12 +0200649 // free old names
650 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
651 {
652 FreeWild(xp->xp_numfiles, xp->xp_files);
653 xp->xp_numfiles = -1;
654 VIM_CLEAR(orig_save);
655 }
656 findex = 0;
657
658 if (mode == WILD_FREE) // only release file name
659 return NULL;
660
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000661 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200662 {
663 vim_free(orig_save);
664 orig_save = orig;
665 orig_saved = TRUE;
666
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000667 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200668 }
669
670 // Find longest common part
671 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
672 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000673 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200674 findex = -1; // next p_wc gets first one
675 }
676
677 // Concatenate all matching names
678 if (mode == WILD_ALL && xp->xp_numfiles > 0)
679 {
680 len = 0;
681 for (i = 0; i < xp->xp_numfiles; ++i)
682 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
683 ss = alloc(len);
684 if (ss != NULL)
685 {
686 *ss = NUL;
687 for (i = 0; i < xp->xp_numfiles; ++i)
688 {
689 STRCAT(ss, xp->xp_files[i]);
690 if (i != xp->xp_numfiles - 1)
691 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
692 }
693 }
694 }
695
696 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
697 ExpandCleanup(xp);
698
699 // Free "orig" if it wasn't stored in "orig_save".
700 if (!orig_saved)
701 vim_free(orig);
702
703 return ss;
704}
705
706/*
707 * Prepare an expand structure for use.
708 */
709 void
710ExpandInit(expand_T *xp)
711{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200712 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200713 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200714 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200715}
716
717/*
718 * Cleanup an expand structure after use.
719 */
720 void
721ExpandCleanup(expand_T *xp)
722{
723 if (xp->xp_numfiles >= 0)
724 {
725 FreeWild(xp->xp_numfiles, xp->xp_files);
726 xp->xp_numfiles = -1;
727 }
728}
729
730/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000731 * Display one line of completion matches. Multiple matches are displayed in
732 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000733 * matches - list of completion match names
734 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000735 * lines - number of output lines
736 * linenr - line number of matches to display
737 * maxlen - maximum number of characters in each line
738 * showtail - display only the tail of the full path of a file name
739 * dir_attr - highlight attribute to use for directory names
740 */
741 static void
742showmatches_oneline(
743 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000744 char_u **matches,
745 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000746 int lines,
747 int linenr,
748 int maxlen,
749 int showtail,
750 int dir_attr)
751{
752 int i, j;
753 int isdir;
754 int lastlen;
755 char_u *p;
756
757 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000758 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000759 {
760 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
761 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000762 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
763 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000764 msg_advance(maxlen + 1);
765 msg_puts((char *)p);
766 msg_advance(maxlen + 3);
767 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
768 break;
769 }
770 for (i = maxlen - lastlen; --i >= 0; )
771 msg_putchar(' ');
772 if (xp->xp_context == EXPAND_FILES
773 || xp->xp_context == EXPAND_SHELLCMD
774 || xp->xp_context == EXPAND_BUFFERS)
775 {
776 // highlight directories
777 if (xp->xp_numfiles != -1)
778 {
779 char_u *halved_slash;
780 char_u *exp_path;
781 char_u *path;
782
783 // Expansion was done before and special characters
784 // were escaped, need to halve backslashes. Also
785 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000786 exp_path = expand_env_save_opt(matches[j], TRUE);
787 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000788 halved_slash = backslash_halve_save(path);
789 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000790 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000791 vim_free(exp_path);
792 if (halved_slash != path)
793 vim_free(halved_slash);
794 }
795 else
796 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000797 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000798 if (showtail)
799 p = SHOW_FILE_TEXT(j);
800 else
801 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000802 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000803 TRUE);
804 p = NameBuff;
805 }
806 }
807 else
808 {
809 isdir = FALSE;
810 p = SHOW_FILE_TEXT(j);
811 }
812 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
813 }
814 if (msg_col > 0) // when not wrapped around
815 {
816 msg_clr_eos();
817 msg_putchar('\n');
818 }
819 out_flush(); // show one line at a time
820}
821
822/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200823 * Show all matches for completion on the command line.
824 * Returns EXPAND_NOTHING when the character that triggered expansion should
825 * be inserted like a normal character.
826 */
827 int
828showmatches(expand_T *xp, int wildmenu UNUSED)
829{
830 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000831 int numMatches;
832 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000833 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200834 int maxlen;
835 int lines;
836 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200837 int attr;
838 int showtail;
839
840 if (xp->xp_numfiles == -1)
841 {
842 set_expand_context(xp);
843 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000844 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200845 showtail = expand_showtail(xp);
846 if (i != EXPAND_OK)
847 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200848 }
849 else
850 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000851 numMatches = xp->xp_numfiles;
852 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200853 showtail = cmd_showtail;
854 }
855
856#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000857 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000858 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000859 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000860#endif
861
862#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200863 if (!wildmenu)
864 {
865#endif
866 msg_didany = FALSE; // lines_left will be set
867 msg_start(); // prepare for paging
868 msg_putchar('\n');
869 out_flush();
870 cmdline_row = msg_row;
871 msg_didany = FALSE; // lines_left will be set again
872 msg_start(); // prepare for paging
873#ifdef FEAT_WILDMENU
874 }
875#endif
876
877 if (got_int)
878 got_int = FALSE; // only int. the completion, not the cmd line
879#ifdef FEAT_WILDMENU
880 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000881 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200882#endif
883 else
884 {
885 // find the length of the longest file name
886 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000887 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200888 {
889 if (!showtail && (xp->xp_context == EXPAND_FILES
890 || xp->xp_context == EXPAND_SHELLCMD
891 || xp->xp_context == EXPAND_BUFFERS))
892 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000893 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200894 j = vim_strsize(NameBuff);
895 }
896 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000897 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200898 if (j > maxlen)
899 maxlen = j;
900 }
901
902 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000903 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200904 else
905 {
906 // compute the number of columns and lines for the listing
907 maxlen += 2; // two spaces between file names
908 columns = ((int)Columns + 2) / maxlen;
909 if (columns < 1)
910 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000911 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200912 }
913
914 attr = HL_ATTR(HLF_D); // find out highlighting for directories
915
916 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
917 {
918 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
919 msg_clr_eos();
920 msg_advance(maxlen - 3);
921 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
922 }
923
924 // list the files line by line
925 for (i = 0; i < lines; ++i)
926 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000927 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000928 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200929 if (got_int)
930 {
931 got_int = FALSE;
932 break;
933 }
934 }
935
936 // we redraw the command below the lines that we have just listed
937 // This is a bit tricky, but it saves a lot of screen updating.
938 cmdline_row = msg_row; // will put it back later
939 }
940
941 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000942 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200943
944 return EXPAND_OK;
945}
946
947/*
948 * Private gettail for showmatches() (and win_redr_status_matches()):
949 * Find tail of file name path, but ignore trailing "/".
950 */
951 char_u *
952sm_gettail(char_u *s)
953{
954 char_u *p;
955 char_u *t = s;
956 int had_sep = FALSE;
957
958 for (p = s; *p != NUL; )
959 {
960 if (vim_ispathsep(*p)
961#ifdef BACKSLASH_IN_FILENAME
962 && !rem_backslash(p)
963#endif
964 )
965 had_sep = TRUE;
966 else if (had_sep)
967 {
968 t = p;
969 had_sep = FALSE;
970 }
971 MB_PTR_ADV(p);
972 }
973 return t;
974}
975
976/*
977 * Return TRUE if we only need to show the tail of completion matches.
978 * When not completing file names or there is a wildcard in the path FALSE is
979 * returned.
980 */
981 static int
982expand_showtail(expand_T *xp)
983{
984 char_u *s;
985 char_u *end;
986
987 // When not completing file names a "/" may mean something different.
988 if (xp->xp_context != EXPAND_FILES
989 && xp->xp_context != EXPAND_SHELLCMD
990 && xp->xp_context != EXPAND_DIRECTORIES)
991 return FALSE;
992
993 end = gettail(xp->xp_pattern);
994 if (end == xp->xp_pattern) // there is no path separator
995 return FALSE;
996
997 for (s = xp->xp_pattern; s < end; s++)
998 {
999 // Skip escaped wildcards. Only when the backslash is not a path
1000 // separator, on DOS the '*' "path\*\file" must not be skipped.
1001 if (rem_backslash(s))
1002 ++s;
1003 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1004 return FALSE;
1005 }
1006 return TRUE;
1007}
1008
1009/*
1010 * Prepare a string for expansion.
1011 * When expanding file names: The string will be used with expand_wildcards().
1012 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1013 * When expanding other names: The string will be used with regcomp(). Copy
1014 * the name into allocated memory and prepend "^".
1015 */
1016 char_u *
1017addstar(
1018 char_u *fname,
1019 int len,
1020 int context) // EXPAND_FILES etc.
1021{
1022 char_u *retval;
1023 int i, j;
1024 int new_len;
1025 char_u *tail;
1026 int ends_in_star;
1027
1028 if (context != EXPAND_FILES
1029 && context != EXPAND_FILES_IN_PATH
1030 && context != EXPAND_SHELLCMD
1031 && context != EXPAND_DIRECTORIES)
1032 {
1033 // Matching will be done internally (on something other than files).
1034 // So we convert the file-matching-type wildcards into our kind for
1035 // use with vim_regcomp(). First work out how long it will be:
1036
1037 // For help tags the translation is done in find_help_tags().
1038 // For a tag pattern starting with "/" no translation is needed.
1039 if (context == EXPAND_HELP
1040 || context == EXPAND_COLORS
1041 || context == EXPAND_COMPILER
1042 || context == EXPAND_OWNSYNTAX
1043 || context == EXPAND_FILETYPE
1044 || context == EXPAND_PACKADD
1045 || ((context == EXPAND_TAGS_LISTFILES
1046 || context == EXPAND_TAGS)
1047 && fname[0] == '/'))
1048 retval = vim_strnsave(fname, len);
1049 else
1050 {
1051 new_len = len + 2; // +2 for '^' at start, NUL at end
1052 for (i = 0; i < len; i++)
1053 {
1054 if (fname[i] == '*' || fname[i] == '~')
1055 new_len++; // '*' needs to be replaced by ".*"
1056 // '~' needs to be replaced by "\~"
1057
1058 // Buffer names are like file names. "." should be literal
1059 if (context == EXPAND_BUFFERS && fname[i] == '.')
1060 new_len++; // "." becomes "\."
1061
1062 // Custom expansion takes care of special things, match
1063 // backslashes literally (perhaps also for other types?)
1064 if ((context == EXPAND_USER_DEFINED
1065 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1066 new_len++; // '\' becomes "\\"
1067 }
1068 retval = alloc(new_len);
1069 if (retval != NULL)
1070 {
1071 retval[0] = '^';
1072 j = 1;
1073 for (i = 0; i < len; i++, j++)
1074 {
1075 // Skip backslash. But why? At least keep it for custom
1076 // expansion.
1077 if (context != EXPAND_USER_DEFINED
1078 && context != EXPAND_USER_LIST
1079 && fname[i] == '\\'
1080 && ++i == len)
1081 break;
1082
1083 switch (fname[i])
1084 {
1085 case '*': retval[j++] = '.';
1086 break;
1087 case '~': retval[j++] = '\\';
1088 break;
1089 case '?': retval[j] = '.';
1090 continue;
1091 case '.': if (context == EXPAND_BUFFERS)
1092 retval[j++] = '\\';
1093 break;
1094 case '\\': if (context == EXPAND_USER_DEFINED
1095 || context == EXPAND_USER_LIST)
1096 retval[j++] = '\\';
1097 break;
1098 }
1099 retval[j] = fname[i];
1100 }
1101 retval[j] = NUL;
1102 }
1103 }
1104 }
1105 else
1106 {
1107 retval = alloc(len + 4);
1108 if (retval != NULL)
1109 {
1110 vim_strncpy(retval, fname, len);
1111
1112 // Don't add a star to *, ~, ~user, $var or `cmd`.
1113 // * would become **, which walks the whole tree.
1114 // ~ would be at the start of the file name, but not the tail.
1115 // $ could be anywhere in the tail.
1116 // ` could be anywhere in the file name.
1117 // When the name ends in '$' don't add a star, remove the '$'.
1118 tail = gettail(retval);
1119 ends_in_star = (len > 0 && retval[len - 1] == '*');
1120#ifndef BACKSLASH_IN_FILENAME
1121 for (i = len - 2; i >= 0; --i)
1122 {
1123 if (retval[i] != '\\')
1124 break;
1125 ends_in_star = !ends_in_star;
1126 }
1127#endif
1128 if ((*retval != '~' || tail != retval)
1129 && !ends_in_star
1130 && vim_strchr(tail, '$') == NULL
1131 && vim_strchr(retval, '`') == NULL)
1132 retval[len++] = '*';
1133 else if (len > 0 && retval[len - 1] == '$')
1134 --len;
1135 retval[len] = NUL;
1136 }
1137 }
1138 return retval;
1139}
1140
1141/*
1142 * Must parse the command line so far to work out what context we are in.
1143 * Completion can then be done based on that context.
1144 * This routine sets the variables:
1145 * xp->xp_pattern The start of the pattern to be expanded within
1146 * the command line (ends at the cursor).
1147 * xp->xp_context The type of thing to expand. Will be one of:
1148 *
1149 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1150 * the command line, like an unknown command. Caller
1151 * should beep.
1152 * EXPAND_NOTHING Unrecognised context for completion, use char like
1153 * a normal char, rather than for completion. eg
1154 * :s/^I/
1155 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1156 * it.
1157 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1158 * EXPAND_FILES After command with EX_XFILE set, or after setting
1159 * with P_EXPAND set. eg :e ^I, :w>>^I
1160 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1161 * when we know only directories are of interest. eg
1162 * :set dir=^I
1163 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1164 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1165 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1166 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1167 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1168 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1169 * EXPAND_EVENTS Complete event names
1170 * EXPAND_SYNTAX Complete :syntax command arguments
1171 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1172 * EXPAND_AUGROUP Complete autocommand group names
1173 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1174 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1175 * eg :unmap a^I , :cunab x^I
1176 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1177 * eg :call sub^I
1178 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1179 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1180 * names in expressions, eg :while s^I
1181 * EXPAND_ENV_VARS Complete environment variable names
1182 * EXPAND_USER Complete user names
1183 */
1184 static void
1185set_expand_context(expand_T *xp)
1186{
1187 cmdline_info_T *ccline = get_cmdline_info();
1188
1189 // only expansion for ':', '>' and '=' command-lines
1190 if (ccline->cmdfirstc != ':'
1191#ifdef FEAT_EVAL
1192 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1193 && !ccline->input_fn
1194#endif
1195 )
1196 {
1197 xp->xp_context = EXPAND_NOTHING;
1198 return;
1199 }
1200 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1201}
1202
Bram Moolenaard0190392019-08-23 21:17:35 +02001203/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001204 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1205 * For user defined commands, the completion context is set in 'xp' and the
1206 * completion flags in 'complp'.
1207 *
1208 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001209 */
1210 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001211set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001212{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001213 char_u *p = NULL;
1214 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001215 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001216
1217 // Isolate the command and search for it in the command table.
1218 // Exceptions:
1219 // - the 'k' command can directly be followed by any character, but
1220 // do accept "keepmarks", "keepalt" and "keepjumps".
1221 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
1222 if (*cmd == 'k' && cmd[1] != 'e')
1223 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001224 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001225 p = cmd + 1;
1226 }
1227 else
1228 {
1229 p = cmd;
1230 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1231 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001232 // A user command may contain digits.
1233 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1234 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001235 while (ASCII_ISALNUM(*p) || *p == '*')
1236 ++p;
1237 // for python 3.x: ":py3*" commands completion
1238 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1239 {
1240 ++p;
1241 while (ASCII_ISALPHA(*p) || *p == '*')
1242 ++p;
1243 }
1244 // check for non-alpha command
1245 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1246 ++p;
1247 len = (int)(p - cmd);
1248
1249 if (len == 0)
1250 {
1251 xp->xp_context = EXPAND_UNSUCCESSFUL;
1252 return NULL;
1253 }
1254
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001255 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001256
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001257 // User defined commands support alphanumeric characters.
1258 // Also when doing fuzzy expansion, support alphanumeric characters.
1259 if ((cmd[0] >= 'A' && cmd[0] <= 'Z') || (fuzzy && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001260 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1261 ++p;
1262 }
1263
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001264 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001265 // character, complete the command name.
1266 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1267 return NULL;
1268
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001269 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001270 {
1271 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1272 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001273 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001274 p = cmd + 1;
1275 }
1276 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1277 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001278 eap->cmd = cmd;
1279 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001280 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001281 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001282 }
1283 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001284 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001285 {
1286 // Not still touching the command and it was an illegal one
1287 xp->xp_context = EXPAND_UNSUCCESSFUL;
1288 return NULL;
1289 }
1290
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001291 return p;
1292}
Bram Moolenaard0190392019-08-23 21:17:35 +02001293
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001294/*
1295 * Set the completion context for a command argument with wild card characters.
1296 */
1297 static void
1298set_context_for_wildcard_arg(
1299 exarg_T *eap,
1300 char_u *arg,
1301 int usefilter,
1302 expand_T *xp,
1303 int *complp)
1304{
1305 char_u *p;
1306 int c;
1307 int in_quote = FALSE;
1308 char_u *bow = NULL; // Beginning of word
1309 int len = 0;
1310
1311 // Allow spaces within back-quotes to count as part of the argument
1312 // being expanded.
1313 xp->xp_pattern = skipwhite(arg);
1314 p = xp->xp_pattern;
1315 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001316 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001317 if (has_mbyte)
1318 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001319 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001320 c = *p;
1321 if (c == '\\' && p[1] != NUL)
1322 ++p;
1323 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001324 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001325 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001326 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001327 xp->xp_pattern = p;
1328 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001329 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001330 in_quote = !in_quote;
1331 }
1332 // An argument can contain just about everything, except
1333 // characters that end the command and white space.
1334 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001335#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001336 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001337#endif
1338 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001339 {
1340 len = 0; // avoid getting stuck when space is in 'isfname'
1341 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001342 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001343 if (has_mbyte)
1344 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001345 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001346 c = *p;
1347 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001348 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001349 if (has_mbyte)
1350 len = (*mb_ptr2len)(p);
1351 else
1352 len = 1;
1353 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001354 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001355 if (in_quote)
1356 bow = p;
1357 else
1358 xp->xp_pattern = p;
1359 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001360 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001361 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001362 }
1363
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001364 // If we are still inside the quotes, and we passed a space, just
1365 // expand from there.
1366 if (bow != NULL && in_quote)
1367 xp->xp_pattern = bow;
1368 xp->xp_context = EXPAND_FILES;
1369
1370 // For a shell command more chars need to be escaped.
1371 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1372 {
1373#ifndef BACKSLASH_IN_FILENAME
1374 xp->xp_shell = TRUE;
1375#endif
1376 // When still after the command name expand executables.
1377 if (xp->xp_pattern == skipwhite(arg))
1378 xp->xp_context = EXPAND_SHELLCMD;
1379 }
1380
1381 // Check for environment variable.
1382 if (*xp->xp_pattern == '$')
1383 {
1384 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1385 if (!vim_isIDc(*p))
1386 break;
1387 if (*p == NUL)
1388 {
1389 xp->xp_context = EXPAND_ENV_VARS;
1390 ++xp->xp_pattern;
1391 // Avoid that the assignment uses EXPAND_FILES again.
1392 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1393 *complp = EXPAND_ENV_VARS;
1394 }
1395 }
1396 // Check for user names.
1397 if (*xp->xp_pattern == '~')
1398 {
1399 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1400 ;
1401 // Complete ~user only if it partially matches a user name.
1402 // A full match ~user<Tab> will be replaced by user's home
1403 // directory i.e. something like ~user<Tab> -> /home/user/
1404 if (*p == NUL && p > xp->xp_pattern + 1
1405 && match_user(xp->xp_pattern + 1) >= 1)
1406 {
1407 xp->xp_context = EXPAND_USER;
1408 ++xp->xp_pattern;
1409 }
1410 }
1411}
1412
1413/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001414 * Set the completion context for the :filter command. Returns a pointer to the
1415 * next command after the :filter command.
1416 */
1417 static char_u *
1418set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1419{
1420 if (*arg != NUL)
1421 arg = skip_vimgrep_pat(arg, NULL, NULL);
1422 if (arg == NULL || *arg == NUL)
1423 {
1424 xp->xp_context = EXPAND_NOTHING;
1425 return NULL;
1426 }
1427 return skipwhite(arg);
1428}
1429
1430#ifdef FEAT_SEARCH_EXTRA
1431/*
1432 * Set the completion context for the :match command. Returns a pointer to the
1433 * next command after the :match command.
1434 */
1435 static char_u *
1436set_context_in_match_cmd(expand_T *xp, char_u *arg)
1437{
1438 if (*arg == NUL || !ends_excmd(*arg))
1439 {
1440 // also complete "None"
1441 set_context_in_echohl_cmd(xp, arg);
1442 arg = skipwhite(skiptowhite(arg));
1443 if (*arg != NUL)
1444 {
1445 xp->xp_context = EXPAND_NOTHING;
1446 arg = skip_regexp(arg + 1, *arg, magic_isset());
1447 }
1448 }
1449 return find_nextcmd(arg);
1450}
1451#endif
1452
1453/*
1454 * Returns a pointer to the next command after a :global or a :v command.
1455 * Returns NULL if there is no next command.
1456 */
1457 static char_u *
1458find_cmd_after_global_cmd(char_u *arg)
1459{
1460 int delim;
1461
1462 delim = *arg; // get the delimiter
1463 if (delim)
1464 ++arg; // skip delimiter if there is one
1465
1466 while (arg[0] != NUL && arg[0] != delim)
1467 {
1468 if (arg[0] == '\\' && arg[1] != NUL)
1469 ++arg;
1470 ++arg;
1471 }
1472 if (arg[0] != NUL)
1473 return arg + 1;
1474
1475 return NULL;
1476}
1477
1478/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001479 * Returns a pointer to the next command after a :substitute or a :& command.
1480 * Returns NULL if there is no next command.
1481 */
1482 static char_u *
1483find_cmd_after_substitute_cmd(char_u *arg)
1484{
1485 int delim;
1486
1487 delim = *arg;
1488 if (delim)
1489 {
1490 // skip "from" part
1491 ++arg;
1492 arg = skip_regexp(arg, delim, magic_isset());
1493
1494 if (arg[0] != NUL && arg[0] == delim)
1495 {
1496 // skip "to" part
1497 ++arg;
1498 while (arg[0] != NUL && arg[0] != delim)
1499 {
1500 if (arg[0] == '\\' && arg[1] != NUL)
1501 ++arg;
1502 ++arg;
1503 }
1504 if (arg[0] != NUL) // skip delimiter
1505 ++arg;
1506 }
1507 }
1508 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1509 ++arg;
1510 if (arg[0] != NUL)
1511 return arg;
1512
1513 return NULL;
1514}
1515
1516/*
1517 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1518 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1519 * Returns NULL if there is no next command.
1520 */
1521 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001522find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001523{
1524 arg = skipwhite(skipdigits(arg)); // skip count
1525 if (*arg == '/') // Match regexp, not just whole words
1526 {
1527 for (++arg; *arg && *arg != '/'; arg++)
1528 if (*arg == '\\' && arg[1] != NUL)
1529 arg++;
1530 if (*arg)
1531 {
1532 arg = skipwhite(arg + 1);
1533
1534 // Check for trailing illegal characters
1535 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1536 xp->xp_context = EXPAND_NOTHING;
1537 else
1538 return arg;
1539 }
1540 }
1541
1542 return NULL;
1543}
1544
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001545#ifdef FEAT_EVAL
1546/*
1547 * Set the completion context for the :unlet command. Always returns NULL.
1548 */
1549 static char_u *
1550set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1551{
1552 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1553 arg = xp->xp_pattern + 1;
1554
1555 xp->xp_context = EXPAND_USER_VARS;
1556 xp->xp_pattern = arg;
1557
1558 if (*xp->xp_pattern == '$')
1559 {
1560 xp->xp_context = EXPAND_ENV_VARS;
1561 ++xp->xp_pattern;
1562 }
1563
1564 return NULL;
1565}
1566#endif
1567
1568#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1569/*
1570 * Set the completion context for the :language command. Always returns NULL.
1571 */
1572 static char_u *
1573set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1574{
1575 char_u *p;
1576
1577 p = skiptowhite(arg);
1578 if (*p == NUL)
1579 {
1580 xp->xp_context = EXPAND_LANGUAGE;
1581 xp->xp_pattern = arg;
1582 }
1583 else
1584 {
1585 if ( STRNCMP(arg, "messages", p - arg) == 0
1586 || STRNCMP(arg, "ctype", p - arg) == 0
1587 || STRNCMP(arg, "time", p - arg) == 0
1588 || STRNCMP(arg, "collate", p - arg) == 0)
1589 {
1590 xp->xp_context = EXPAND_LOCALES;
1591 xp->xp_pattern = skipwhite(p);
1592 }
1593 else
1594 xp->xp_context = EXPAND_NOTHING;
1595 }
1596
1597 return NULL;
1598}
1599#endif
1600
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001601/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001602 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1603 * The argument to the command is 'arg' and the argument flags is 'argt'.
1604 * For user-defined commands and for environment variables, 'compl' has the
1605 * completion type.
1606 * Returns a pointer to the next command. Returns NULL if there is no next
1607 * command.
1608 */
1609 static char_u *
1610set_context_by_cmdname(
1611 char_u *cmd,
1612 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001613 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001614 char_u *arg,
1615 long argt,
1616 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001617 int forceit)
1618{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001619 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001620 {
1621 case CMD_find:
1622 case CMD_sfind:
1623 case CMD_tabfind:
1624 if (xp->xp_context == EXPAND_FILES)
1625 xp->xp_context = EXPAND_FILES_IN_PATH;
1626 break;
1627 case CMD_cd:
1628 case CMD_chdir:
1629 case CMD_tcd:
1630 case CMD_tchdir:
1631 case CMD_lcd:
1632 case CMD_lchdir:
1633 if (xp->xp_context == EXPAND_FILES)
1634 xp->xp_context = EXPAND_DIRECTORIES;
1635 break;
1636 case CMD_help:
1637 xp->xp_context = EXPAND_HELP;
1638 xp->xp_pattern = arg;
1639 break;
1640
1641 // Command modifiers: return the argument.
1642 // Also for commands with an argument that is a command.
1643 case CMD_aboveleft:
1644 case CMD_argdo:
1645 case CMD_belowright:
1646 case CMD_botright:
1647 case CMD_browse:
1648 case CMD_bufdo:
1649 case CMD_cdo:
1650 case CMD_cfdo:
1651 case CMD_confirm:
1652 case CMD_debug:
1653 case CMD_folddoclosed:
1654 case CMD_folddoopen:
1655 case CMD_hide:
1656 case CMD_keepalt:
1657 case CMD_keepjumps:
1658 case CMD_keepmarks:
1659 case CMD_keeppatterns:
1660 case CMD_ldo:
1661 case CMD_leftabove:
1662 case CMD_lfdo:
1663 case CMD_lockmarks:
1664 case CMD_noautocmd:
1665 case CMD_noswapfile:
1666 case CMD_rightbelow:
1667 case CMD_sandbox:
1668 case CMD_silent:
1669 case CMD_tab:
1670 case CMD_tabdo:
1671 case CMD_topleft:
1672 case CMD_verbose:
1673 case CMD_vertical:
1674 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001675 case CMD_vim9cmd:
1676 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 return arg;
1678
1679 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001680 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001681
1682#ifdef FEAT_SEARCH_EXTRA
1683 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001684 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001685#endif
1686
1687 // All completion for the +cmdline_compl feature goes here.
1688
1689 case CMD_command:
1690 return set_context_in_user_cmd(xp, arg);
1691
1692 case CMD_delcommand:
1693 xp->xp_context = EXPAND_USER_COMMANDS;
1694 xp->xp_pattern = arg;
1695 break;
1696
1697 case CMD_global:
1698 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001699 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001700 case CMD_and:
1701 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001702 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001703 case CMD_isearch:
1704 case CMD_dsearch:
1705 case CMD_ilist:
1706 case CMD_dlist:
1707 case CMD_ijump:
1708 case CMD_psearch:
1709 case CMD_djump:
1710 case CMD_isplit:
1711 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001712 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001713 case CMD_autocmd:
1714 return set_context_in_autocmd(xp, arg, FALSE);
1715 case CMD_doautocmd:
1716 case CMD_doautoall:
1717 return set_context_in_autocmd(xp, arg, TRUE);
1718 case CMD_set:
1719 set_context_in_set_cmd(xp, arg, 0);
1720 break;
1721 case CMD_setglobal:
1722 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1723 break;
1724 case CMD_setlocal:
1725 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1726 break;
1727 case CMD_tag:
1728 case CMD_stag:
1729 case CMD_ptag:
1730 case CMD_ltag:
1731 case CMD_tselect:
1732 case CMD_stselect:
1733 case CMD_ptselect:
1734 case CMD_tjump:
1735 case CMD_stjump:
1736 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001737 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001738 xp->xp_context = EXPAND_TAGS_LISTFILES;
1739 else
1740 xp->xp_context = EXPAND_TAGS;
1741 xp->xp_pattern = arg;
1742 break;
1743 case CMD_augroup:
1744 xp->xp_context = EXPAND_AUGROUP;
1745 xp->xp_pattern = arg;
1746 break;
1747#ifdef FEAT_SYN_HL
1748 case CMD_syntax:
1749 set_context_in_syntax_cmd(xp, arg);
1750 break;
1751#endif
1752#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001753 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001754 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001755 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001756 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001757 case CMD_if:
1758 case CMD_elseif:
1759 case CMD_while:
1760 case CMD_for:
1761 case CMD_echo:
1762 case CMD_echon:
1763 case CMD_execute:
1764 case CMD_echomsg:
1765 case CMD_echoerr:
1766 case CMD_call:
1767 case CMD_return:
1768 case CMD_cexpr:
1769 case CMD_caddexpr:
1770 case CMD_cgetexpr:
1771 case CMD_lexpr:
1772 case CMD_laddexpr:
1773 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001774 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001775 break;
1776
1777 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001778 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001779 case CMD_function:
1780 case CMD_delfunction:
1781 xp->xp_context = EXPAND_USER_FUNC;
1782 xp->xp_pattern = arg;
1783 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001784 case CMD_disassemble:
1785 set_context_in_disassemble_cmd(xp, arg);
1786 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001787
1788 case CMD_echohl:
1789 set_context_in_echohl_cmd(xp, arg);
1790 break;
1791#endif
1792 case CMD_highlight:
1793 set_context_in_highlight_cmd(xp, arg);
1794 break;
1795#ifdef FEAT_CSCOPE
1796 case CMD_cscope:
1797 case CMD_lcscope:
1798 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001799 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001800 break;
1801#endif
1802#ifdef FEAT_SIGNS
1803 case CMD_sign:
1804 set_context_in_sign_cmd(xp, arg);
1805 break;
1806#endif
1807 case CMD_bdelete:
1808 case CMD_bwipeout:
1809 case CMD_bunload:
1810 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1811 arg = xp->xp_pattern + 1;
1812 // FALLTHROUGH
1813 case CMD_buffer:
1814 case CMD_sbuffer:
1815 case CMD_checktime:
1816 xp->xp_context = EXPAND_BUFFERS;
1817 xp->xp_pattern = arg;
1818 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001819#ifdef FEAT_DIFF
1820 case CMD_diffget:
1821 case CMD_diffput:
1822 // If current buffer is in diff mode, complete buffer names
1823 // which are in diff mode, and different than current buffer.
1824 xp->xp_context = EXPAND_DIFF_BUFFERS;
1825 xp->xp_pattern = arg;
1826 break;
1827#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001828 case CMD_USER:
1829 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001830 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1831 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001832
1833 case CMD_map: case CMD_noremap:
1834 case CMD_nmap: case CMD_nnoremap:
1835 case CMD_vmap: case CMD_vnoremap:
1836 case CMD_omap: case CMD_onoremap:
1837 case CMD_imap: case CMD_inoremap:
1838 case CMD_cmap: case CMD_cnoremap:
1839 case CMD_lmap: case CMD_lnoremap:
1840 case CMD_smap: case CMD_snoremap:
1841 case CMD_tmap: case CMD_tnoremap:
1842 case CMD_xmap: case CMD_xnoremap:
1843 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001844 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001845 case CMD_unmap:
1846 case CMD_nunmap:
1847 case CMD_vunmap:
1848 case CMD_ounmap:
1849 case CMD_iunmap:
1850 case CMD_cunmap:
1851 case CMD_lunmap:
1852 case CMD_sunmap:
1853 case CMD_tunmap:
1854 case CMD_xunmap:
1855 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001856 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001857 case CMD_mapclear:
1858 case CMD_nmapclear:
1859 case CMD_vmapclear:
1860 case CMD_omapclear:
1861 case CMD_imapclear:
1862 case CMD_cmapclear:
1863 case CMD_lmapclear:
1864 case CMD_smapclear:
1865 case CMD_tmapclear:
1866 case CMD_xmapclear:
1867 xp->xp_context = EXPAND_MAPCLEAR;
1868 xp->xp_pattern = arg;
1869 break;
1870
1871 case CMD_abbreviate: case CMD_noreabbrev:
1872 case CMD_cabbrev: case CMD_cnoreabbrev:
1873 case CMD_iabbrev: case CMD_inoreabbrev:
1874 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001875 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001876 case CMD_unabbreviate:
1877 case CMD_cunabbrev:
1878 case CMD_iunabbrev:
1879 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001880 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001881#ifdef FEAT_MENU
1882 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1883 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1884 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1885 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1886 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1887 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1888 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1889 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1890 case CMD_tmenu: case CMD_tunmenu:
1891 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1892 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1893#endif
1894
1895 case CMD_colorscheme:
1896 xp->xp_context = EXPAND_COLORS;
1897 xp->xp_pattern = arg;
1898 break;
1899
1900 case CMD_compiler:
1901 xp->xp_context = EXPAND_COMPILER;
1902 xp->xp_pattern = arg;
1903 break;
1904
1905 case CMD_ownsyntax:
1906 xp->xp_context = EXPAND_OWNSYNTAX;
1907 xp->xp_pattern = arg;
1908 break;
1909
1910 case CMD_setfiletype:
1911 xp->xp_context = EXPAND_FILETYPE;
1912 xp->xp_pattern = arg;
1913 break;
1914
1915 case CMD_packadd:
1916 xp->xp_context = EXPAND_PACKADD;
1917 xp->xp_pattern = arg;
1918 break;
1919
1920#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1921 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001922 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001923#endif
1924#if defined(FEAT_PROFILE)
1925 case CMD_profile:
1926 set_context_in_profile_cmd(xp, arg);
1927 break;
1928#endif
1929 case CMD_behave:
1930 xp->xp_context = EXPAND_BEHAVE;
1931 xp->xp_pattern = arg;
1932 break;
1933
1934 case CMD_messages:
1935 xp->xp_context = EXPAND_MESSAGES;
1936 xp->xp_pattern = arg;
1937 break;
1938
1939 case CMD_history:
1940 xp->xp_context = EXPAND_HISTORY;
1941 xp->xp_pattern = arg;
1942 break;
1943#if defined(FEAT_PROFILE)
1944 case CMD_syntime:
1945 xp->xp_context = EXPAND_SYNTIME;
1946 xp->xp_pattern = arg;
1947 break;
1948#endif
1949
1950 case CMD_argdelete:
1951 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1952 arg = xp->xp_pattern + 1;
1953 xp->xp_context = EXPAND_ARGLIST;
1954 xp->xp_pattern = arg;
1955 break;
1956
1957 default:
1958 break;
1959 }
1960 return NULL;
1961}
1962
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001963/*
1964 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
1965 * we don't need/want deleted. Maybe this could be done better if we didn't
1966 * repeat all this stuff. The only problem is that they may not stay
1967 * perfectly compatible with each other, but then the command line syntax
1968 * probably won't change that much -- webb.
1969 */
1970 static char_u *
1971set_one_cmd_context(
1972 expand_T *xp,
1973 char_u *buff) // buffer for command string
1974{
1975 char_u *p;
1976 char_u *cmd, *arg;
1977 int len = 0;
1978 exarg_T ea;
1979 int compl = EXPAND_NOTHING;
1980 int forceit = FALSE;
1981 int usefilter = FALSE; // filter instead of file name
1982
1983 ExpandInit(xp);
1984 xp->xp_pattern = buff;
1985 xp->xp_line = buff;
1986 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
1987 ea.argt = 0;
1988
1989 // 1. skip comment lines and leading space, colons or bars
1990 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
1991 ;
1992 xp->xp_pattern = cmd;
1993
1994 if (*cmd == NUL)
1995 return NULL;
1996 if (*cmd == '"') // ignore comment lines
1997 {
1998 xp->xp_context = EXPAND_NOTHING;
1999 return NULL;
2000 }
2001
2002 // 3. Skip over the range to find the command.
2003 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2004 xp->xp_pattern = cmd;
2005 if (*cmd == NUL)
2006 return NULL;
2007 if (*cmd == '"')
2008 {
2009 xp->xp_context = EXPAND_NOTHING;
2010 return NULL;
2011 }
2012
2013 if (*cmd == '|' || *cmd == '\n')
2014 return cmd + 1; // There's another command
2015
2016 // Get the command index.
2017 p = set_cmd_index(cmd, &ea, xp, &compl);
2018 if (p == NULL)
2019 return NULL;
2020
2021 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2022
2023 if (*p == '!') // forced commands
2024 {
2025 forceit = TRUE;
2026 ++p;
2027 }
2028
2029 // 6. parse arguments
2030 if (!IS_USER_CMDIDX(ea.cmdidx))
2031 ea.argt = excmd_get_argt(ea.cmdidx);
2032
2033 arg = skipwhite(p);
2034
2035 // Skip over ++argopt argument
2036 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2037 {
2038 p = arg;
2039 while (*p && !vim_isspace(*p))
2040 MB_PTR_ADV(p);
2041 arg = skipwhite(p);
2042 }
2043
2044 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2045 {
2046 if (*arg == '>') // append
2047 {
2048 if (*++arg == '>')
2049 ++arg;
2050 arg = skipwhite(arg);
2051 }
2052 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2053 {
2054 ++arg;
2055 usefilter = TRUE;
2056 }
2057 }
2058
2059 if (ea.cmdidx == CMD_read)
2060 {
2061 usefilter = forceit; // :r! filter if forced
2062 if (*arg == '!') // :r !filter
2063 {
2064 ++arg;
2065 usefilter = TRUE;
2066 }
2067 }
2068
2069 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2070 {
2071 while (*arg == *cmd) // allow any number of '>' or '<'
2072 ++arg;
2073 arg = skipwhite(arg);
2074 }
2075
2076 // Does command allow "+command"?
2077 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2078 {
2079 // Check if we're in the +command
2080 p = arg + 1;
2081 arg = skip_cmd_arg(arg, FALSE);
2082
2083 // Still touching the command after '+'?
2084 if (*arg == NUL)
2085 return p;
2086
2087 // Skip space(s) after +command to get to the real argument
2088 arg = skipwhite(arg);
2089 }
2090
2091
2092 // Check for '|' to separate commands and '"' to start comments.
2093 // Don't do this for ":read !cmd" and ":write !cmd".
2094 if ((ea.argt & EX_TRLBAR) && !usefilter)
2095 {
2096 p = arg;
2097 // ":redir @" is not the start of a comment
2098 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2099 p += 2;
2100 while (*p)
2101 {
2102 if (*p == Ctrl_V)
2103 {
2104 if (p[1] != NUL)
2105 ++p;
2106 }
2107 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2108 || *p == '|' || *p == '\n')
2109 {
2110 if (*(p - 1) != '\\')
2111 {
2112 if (*p == '|' || *p == '\n')
2113 return p + 1;
2114 return NULL; // It's a comment
2115 }
2116 }
2117 MB_PTR_ADV(p);
2118 }
2119 }
2120
2121 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2122 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2123 // no arguments allowed but there is something
2124 return NULL;
2125
2126 // Find start of last argument (argument just before cursor):
2127 p = buff;
2128 xp->xp_pattern = p;
2129 len = (int)STRLEN(buff);
2130 while (*p && p < buff + len)
2131 {
2132 if (*p == ' ' || *p == TAB)
2133 {
2134 // argument starts after a space
2135 xp->xp_pattern = ++p;
2136 }
2137 else
2138 {
2139 if (*p == '\\' && *(p + 1) != NUL)
2140 ++p; // skip over escaped character
2141 MB_PTR_ADV(p);
2142 }
2143 }
2144
2145 if (ea.argt & EX_XFILE)
2146 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2147
2148 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002149 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002150 forceit);
2151}
2152
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002153/*
2154 * Set the completion context in 'xp' for command 'str'
2155 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002156 void
2157set_cmd_context(
2158 expand_T *xp,
2159 char_u *str, // start of command line
2160 int len, // length of command line (excl. NUL)
2161 int col, // position of cursor
2162 int use_ccline UNUSED) // use ccline for info
2163{
2164#ifdef FEAT_EVAL
2165 cmdline_info_T *ccline = get_cmdline_info();
2166#endif
2167 int old_char = NUL;
2168 char_u *nextcomm;
2169
2170 // Avoid a UMR warning from Purify, only save the character if it has been
2171 // written before.
2172 if (col < len)
2173 old_char = str[col];
2174 str[col] = NUL;
2175 nextcomm = str;
2176
2177#ifdef FEAT_EVAL
2178 if (use_ccline && ccline->cmdfirstc == '=')
2179 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002180 // pass CMD_SIZE because there is no real command
2181 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002182 }
2183 else if (use_ccline && ccline->input_fn)
2184 {
2185 xp->xp_context = ccline->xp_context;
2186 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002187 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002188 }
2189 else
2190#endif
2191 while (nextcomm != NULL)
2192 nextcomm = set_one_cmd_context(xp, nextcomm);
2193
2194 // Store the string here so that call_user_expand_func() can get to them
2195 // easily.
2196 xp->xp_line = str;
2197 xp->xp_col = col;
2198
2199 str[col] = old_char;
2200}
2201
2202/*
2203 * Expand the command line "str" from context "xp".
2204 * "xp" must have been set by set_cmd_context().
2205 * xp->xp_pattern points into "str", to where the text that is to be expanded
2206 * starts.
2207 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2208 * cursor.
2209 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2210 * key that triggered expansion literally.
2211 * Returns EXPAND_OK otherwise.
2212 */
2213 int
2214expand_cmdline(
2215 expand_T *xp,
2216 char_u *str, // start of command line
2217 int col, // position of cursor
2218 int *matchcount, // return: nr of matches
2219 char_u ***matches) // return: array of pointers to matches
2220{
2221 char_u *file_str = NULL;
2222 int options = WILD_ADD_SLASH|WILD_SILENT;
2223
2224 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2225 {
2226 beep_flush();
2227 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2228 }
2229 if (xp->xp_context == EXPAND_NOTHING)
2230 {
2231 // Caller can use the character as a normal char instead
2232 return EXPAND_NOTHING;
2233 }
2234
2235 // add star to file name, or convert to regexp if not exp. files.
2236 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002237 if (cmdline_fuzzy_completion_supported(xp))
2238 // If fuzzy matching, don't modify the search string
2239 file_str = vim_strsave(xp->xp_pattern);
2240 else
2241 {
2242 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2243 if (file_str == NULL)
2244 return EXPAND_UNSUCCESSFUL;
2245 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002246
2247 if (p_wic)
2248 options += WILD_ICASE;
2249
2250 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002251 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002252 {
2253 *matchcount = 0;
2254 *matches = NULL;
2255 }
2256 vim_free(file_str);
2257
2258 return EXPAND_OK;
2259}
2260
Bram Moolenaar66b51422019-08-18 21:44:12 +02002261/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002262 * Expand file or directory names.
2263 */
2264 static int
2265expand_files_and_dirs(
2266 expand_T *xp,
2267 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002268 char_u ***matches,
2269 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002270 int flags,
2271 int options)
2272{
2273 int free_pat = FALSE;
2274 int i;
2275 int ret;
2276
2277 // for ":set path=" and ":set tags=" halve backslashes for escaped
2278 // space
2279 if (xp->xp_backslash != XP_BS_NONE)
2280 {
2281 free_pat = TRUE;
2282 pat = vim_strsave(pat);
2283 for (i = 0; pat[i]; ++i)
2284 if (pat[i] == '\\')
2285 {
2286 if (xp->xp_backslash == XP_BS_THREE
2287 && pat[i + 1] == '\\'
2288 && pat[i + 2] == '\\'
2289 && pat[i + 3] == ' ')
2290 STRMOVE(pat + i, pat + i + 3);
2291 if (xp->xp_backslash == XP_BS_ONE
2292 && pat[i + 1] == ' ')
2293 STRMOVE(pat + i, pat + i + 1);
2294 }
2295 }
2296
2297 if (xp->xp_context == EXPAND_FILES)
2298 flags |= EW_FILE;
2299 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2300 flags |= (EW_FILE | EW_PATH);
2301 else
2302 flags = (flags | EW_DIR) & ~EW_FILE;
2303 if (options & WILD_ICASE)
2304 flags |= EW_ICASE;
2305
2306 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002307 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002308 if (free_pat)
2309 vim_free(pat);
2310#ifdef BACKSLASH_IN_FILENAME
2311 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2312 {
2313 int j;
2314
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002315 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002316 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002317 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002318
2319 while (*ptr != NUL)
2320 {
2321 if (p_csl[0] == 's' && *ptr == '\\')
2322 *ptr = '/';
2323 else if (p_csl[0] == 'b' && *ptr == '/')
2324 *ptr = '\\';
2325 ptr += (*mb_ptr2len)(ptr);
2326 }
2327 }
2328 }
2329#endif
2330 return ret;
2331}
2332
2333/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002334 * Function given to ExpandGeneric() to obtain the possible arguments of the
2335 * ":behave {mswin,xterm}" command.
2336 */
2337 static char_u *
2338get_behave_arg(expand_T *xp UNUSED, int idx)
2339{
2340 if (idx == 0)
2341 return (char_u *)"mswin";
2342 if (idx == 1)
2343 return (char_u *)"xterm";
2344 return NULL;
2345}
2346
2347/*
2348 * Function given to ExpandGeneric() to obtain the possible arguments of the
2349 * ":messages {clear}" command.
2350 */
2351 static char_u *
2352get_messages_arg(expand_T *xp UNUSED, int idx)
2353{
2354 if (idx == 0)
2355 return (char_u *)"clear";
2356 return NULL;
2357}
2358
2359 static char_u *
2360get_mapclear_arg(expand_T *xp UNUSED, int idx)
2361{
2362 if (idx == 0)
2363 return (char_u *)"<buffer>";
2364 return NULL;
2365}
2366
2367/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002368 * Do the expansion based on xp->xp_context and 'rmp'.
2369 */
2370 static int
2371ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002372 char_u *pat,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002373 expand_T *xp,
2374 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002375 char_u ***matches,
2376 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002377{
2378 static struct expgen
2379 {
2380 int context;
2381 char_u *((*func)(expand_T *, int));
2382 int ic;
2383 int escaped;
2384 } tab[] =
2385 {
2386 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2387 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2388 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2389 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2390 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2391 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2392 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2393 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2394 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2395 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2396# ifdef FEAT_EVAL
2397 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2398 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2399 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2400 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2401 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2402# endif
2403# ifdef FEAT_MENU
2404 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2405 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2406# endif
2407# ifdef FEAT_SYN_HL
2408 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2409# endif
2410# ifdef FEAT_PROFILE
2411 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2412# endif
2413 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2414 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2415 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
2416# ifdef FEAT_CSCOPE
2417 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2418# endif
2419# ifdef FEAT_SIGNS
2420 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2421# endif
2422# ifdef FEAT_PROFILE
2423 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2424# endif
2425# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2426 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2427 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2428# endif
2429 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2430 {EXPAND_USER, get_users, TRUE, FALSE},
2431 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2432 };
2433 int i;
2434 int ret = FAIL;
2435
2436 // Find a context in the table and call the ExpandGeneric() with the
2437 // right function to do the expansion.
2438 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2439 {
2440 if (xp->xp_context == tab[i].context)
2441 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002442 // Use fuzzy matching if 'wildoptions' has 'fuzzy'.
2443 // If no search pattern is supplied, then don't use fuzzy
2444 // matching and return all the found items.
2445 int fuzzy = cmdline_fuzzy_complete(pat);
2446
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002447 if (tab[i].ic)
2448 rmp->rm_ic = TRUE;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002449 ret = ExpandGeneric(xp, rmp, matches, numMatches,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002450 tab[i].func, tab[i].escaped,
2451 fuzzy ? pat : NULL);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002452 break;
2453 }
2454 }
2455
2456 return ret;
2457}
2458
2459/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002460 * Map wild expand options to flags for expand_wildcards()
2461 */
2462 static int
2463map_wildopts_to_ewflags(int options)
2464{
2465 int flags;
2466
2467 flags = EW_DIR; // include directories
2468 if (options & WILD_LIST_NOTFOUND)
2469 flags |= EW_NOTFOUND;
2470 if (options & WILD_ADD_SLASH)
2471 flags |= EW_ADDSLASH;
2472 if (options & WILD_KEEP_ALL)
2473 flags |= EW_KEEPALL;
2474 if (options & WILD_SILENT)
2475 flags |= EW_SILENT;
2476 if (options & WILD_NOERROR)
2477 flags |= EW_NOERROR;
2478 if (options & WILD_ALLLINKS)
2479 flags |= EW_ALLLINKS;
2480
2481 return flags;
2482}
2483
2484/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002485 * Do the expansion based on xp->xp_context and "pat".
2486 */
2487 static int
2488ExpandFromContext(
2489 expand_T *xp,
2490 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002491 char_u ***matches,
2492 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002493 int options) // WILD_ flags
2494{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002495 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002496 int ret;
2497 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002498 char_u *tofree = NULL;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00002499 int fuzzy = cmdline_fuzzy_complete(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002500
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002501 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002502
2503 if (xp->xp_context == EXPAND_FILES
2504 || xp->xp_context == EXPAND_DIRECTORIES
2505 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002506 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2507 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002508
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002509 *matches = (char_u **)"";
2510 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002511 if (xp->xp_context == EXPAND_HELP)
2512 {
2513 // With an empty argument we would get all the help tags, which is
2514 // very slow. Get matches for "help" instead.
2515 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002516 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002517 {
2518#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002519 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002520#endif
2521 return OK;
2522 }
2523 return FAIL;
2524 }
2525
Bram Moolenaar66b51422019-08-18 21:44:12 +02002526 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002527 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002528 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002529 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002530 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002531 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002532#ifdef FEAT_DIFF
2533 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002534 return ExpandBufnames(pat, numMatches, matches,
2535 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002536#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002537 if (xp->xp_context == EXPAND_TAGS
2538 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002539 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2540 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002541 if (xp->xp_context == EXPAND_COLORS)
2542 {
2543 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002544 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002545 directories);
2546 }
2547 if (xp->xp_context == EXPAND_COMPILER)
2548 {
2549 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002550 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002551 }
2552 if (xp->xp_context == EXPAND_OWNSYNTAX)
2553 {
2554 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002555 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002556 }
2557 if (xp->xp_context == EXPAND_FILETYPE)
2558 {
2559 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002560 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002561 }
2562# if defined(FEAT_EVAL)
2563 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002564 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002565# endif
2566 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002567 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002568
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002569 // When expanding a function name starting with s:, match the <SNR>nr_
2570 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002571 if ((xp->xp_context == EXPAND_USER_FUNC
2572 || xp->xp_context == EXPAND_DISASSEMBLE)
2573 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002574 {
2575 int len = (int)STRLEN(pat) + 20;
2576
2577 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002578 if (tofree == NULL)
2579 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002580 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002581 pat = tofree;
2582 }
2583
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00002584 if (!fuzzy)
2585 {
2586 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2587 if (regmatch.regprog == NULL)
2588 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002589
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00002590 // set ignore-case according to p_ic, p_scs and pat
2591 regmatch.rm_ic = ignorecase(pat);
2592 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002593
2594 if (xp->xp_context == EXPAND_SETTINGS
2595 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002596 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002597 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002598 ret = ExpandMappings(&regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002599# if defined(FEAT_EVAL)
2600 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002601 ret = ExpandUserDefined(xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002602# endif
2603 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002604 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002605
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00002606 if (!fuzzy)
2607 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002608 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002609
2610 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002611}
2612
Bram Moolenaar66b51422019-08-18 21:44:12 +02002613/*
2614 * Expand a list of names.
2615 *
2616 * Generic function for command line completion. It calls a function to
2617 * obtain strings, one by one. The strings are matched against a regexp
2618 * program. Matching strings are copied into an array, which is returned.
2619 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002620 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2621 * is used.
2622 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002623 * Returns OK when no problems encountered, FAIL for error (out of memory).
2624 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002625 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002626ExpandGeneric(
2627 expand_T *xp,
2628 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002629 char_u ***matches,
2630 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002631 char_u *((*func)(expand_T *, int)),
2632 // returns a string from the list
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002633 int escaped,
2634 char_u *fuzzystr)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002635{
2636 int i;
2637 int count = 0;
2638 int round;
2639 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002640 fuzmatch_str_T *fuzmatch = NULL;
2641 int score = 0;
2642 int fuzzy = (fuzzystr != NULL);
2643 int funcsort = FALSE;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002644 int match;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002645
2646 // do this loop twice:
2647 // round == 0: count the number of matching names
2648 // round == 1: copy the matching names into allocated memory
2649 for (round = 0; round <= 1; ++round)
2650 {
2651 for (i = 0; ; ++i)
2652 {
2653 str = (*func)(xp, i);
2654 if (str == NULL) // end of list
2655 break;
2656 if (*str == NUL) // skip empty strings
2657 continue;
2658
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002659 if (!fuzzy)
2660 match = vim_regexec(regmatch, str, (colnr_T)0);
2661 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002662 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002663 score = fuzzy_match_str(str, fuzzystr);
2664 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002665 }
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002666
2667 if (!match)
2668 continue;
2669
2670 if (round)
2671 {
2672 if (escaped)
2673 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2674 else
2675 str = vim_strsave(str);
2676 if (str == NULL)
2677 {
2678 if (fuzzy)
2679 fuzmatch_str_free(fuzmatch, count);
2680 else if (count > 0)
2681 FreeWild(count, *matches);
2682 *numMatches = 0;
2683 *matches = NULL;
2684 return FAIL;
2685 }
2686 if (fuzzy)
2687 {
2688 fuzmatch[count].idx = count;
2689 fuzmatch[count].str = str;
2690 fuzmatch[count].score = score;
2691 }
2692 else
2693 (*matches)[count] = str;
2694# ifdef FEAT_MENU
2695 if (func == get_menu_names && str != NULL)
2696 {
2697 // test for separator added by get_menu_names()
2698 str += STRLEN(str) - 1;
2699 if (*str == '\001')
2700 *str = '.';
2701 }
2702# endif
2703 }
2704 ++count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002705 }
2706 if (round == 0)
2707 {
2708 if (count == 0)
2709 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002710 if (fuzzy)
2711 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2712 else
2713 *matches = ALLOC_MULT(char_u *, count);
2714 if ((fuzzy && (fuzmatch == NULL)) || (*matches == NULL))
Bram Moolenaar66b51422019-08-18 21:44:12 +02002715 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002716 *numMatches = 0;
2717 *matches = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002718 return FAIL;
2719 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002720 *numMatches = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002721 count = 0;
2722 }
2723 }
2724
2725 // Sort the results. Keep menu's in the specified order.
2726 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2727 {
2728 if (xp->xp_context == EXPAND_EXPRESSION
2729 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002730 || xp->xp_context == EXPAND_USER_FUNC
2731 || xp->xp_context == EXPAND_DISASSEMBLE)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002732 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002733 // <SNR> functions should be sorted to the end.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002734 funcsort = TRUE;
2735 if (!fuzzy)
2736 qsort((void *)*matches, (size_t)*numMatches, sizeof(char_u *),
Bram Moolenaar66b51422019-08-18 21:44:12 +02002737 sort_func_compare);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002738 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002739 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002740 {
2741 if (!fuzzy)
2742 sort_strings(*matches, *numMatches);
2743 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002744 }
2745
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002746#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002747 // Reset the variables used for special highlight names expansion, so that
2748 // they don't show up when getting normal highlight names by ID.
2749 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002750#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002751
2752 if (fuzzy && fuzzymatches_to_strmatches(fuzmatch, matches, count,
2753 funcsort) == FAIL)
2754 return FAIL;
2755
Bram Moolenaar66b51422019-08-18 21:44:12 +02002756 return OK;
2757}
2758
2759/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002760 * Expand shell command matches in one directory of $PATH.
2761 */
2762 static void
2763expand_shellcmd_onedir(
2764 char_u *buf,
2765 char_u *s,
2766 size_t l,
2767 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002768 char_u ***matches,
2769 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002770 int flags,
2771 hashtab_T *ht,
2772 garray_T *gap)
2773{
2774 int ret;
2775 int i;
2776 hash_T hash;
2777 hashitem_T *hi;
2778
2779 vim_strncpy(buf, s, l);
2780 add_pathsep(buf);
2781 l = STRLEN(buf);
2782 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2783
2784 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002785 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002786 if (ret == OK)
2787 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002788 if (ga_grow(gap, *numMatches) == FAIL)
2789 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002790 else
2791 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002792 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002793 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002794 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002795
2796 if (STRLEN(name) > l)
2797 {
2798 // Check if this name was already found.
2799 hash = hash_hash(name + l);
2800 hi = hash_lookup(ht, name + l, hash);
2801 if (HASHITEM_EMPTY(hi))
2802 {
2803 // Remove the path that was prepended.
2804 STRMOVE(name, name + l);
2805 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2806 hash_add_item(ht, hi, name, hash);
2807 name = NULL;
2808 }
2809 }
2810 vim_free(name);
2811 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002812 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002813 }
2814 }
2815}
2816
2817/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002818 * Complete a shell command.
2819 * Returns FAIL or OK;
2820 */
2821 static int
2822expand_shellcmd(
2823 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002824 char_u ***matches, // return: array with matches
2825 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02002826 int flagsarg) // EW_ flags
2827{
2828 char_u *pat;
2829 int i;
2830 char_u *path = NULL;
2831 int mustfree = FALSE;
2832 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002833 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002834 size_t l;
2835 char_u *s, *e;
2836 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002837 int did_curdir = FALSE;
2838 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002839
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002840 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002841 if (buf == NULL)
2842 return FAIL;
2843
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002844 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002845 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002846 if (pat == NULL)
2847 {
2848 vim_free(buf);
2849 return FAIL;
2850 }
2851
Bram Moolenaar66b51422019-08-18 21:44:12 +02002852 for (i = 0; pat[i]; ++i)
2853 if (pat[i] == '\\' && pat[i + 1] == ' ')
2854 STRMOVE(pat + i, pat + i + 1);
2855
2856 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2857
2858 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2859 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2860 path = (char_u *)".";
2861 else
2862 {
2863 // For an absolute name we don't use $PATH.
2864 if (!mch_isFullName(pat))
2865 path = vim_getenv((char_u *)"PATH", &mustfree);
2866 if (path == NULL)
2867 path = (char_u *)"";
2868 }
2869
2870 // Go over all directories in $PATH. Expand matches in that directory and
2871 // collect them in "ga". When "." is not in $PATH also expand for the
2872 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002873 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002874 hash_init(&found_ht);
2875 for (s = path; ; s = e)
2876 {
2877# if defined(MSWIN)
2878 e = vim_strchr(s, ';');
2879# else
2880 e = vim_strchr(s, ':');
2881# endif
2882 if (e == NULL)
2883 e = s + STRLEN(s);
2884
2885 if (*s == NUL)
2886 {
2887 if (did_curdir)
2888 break;
2889 // Find directories in the current directory, path is empty.
2890 did_curdir = TRUE;
2891 flags |= EW_DIR;
2892 }
2893 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2894 {
2895 did_curdir = TRUE;
2896 flags |= EW_DIR;
2897 }
2898 else
2899 // Do not match directories inside a $PATH item.
2900 flags &= ~EW_DIR;
2901
2902 l = e - s;
2903 if (l > MAXPATHL - 5)
2904 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002905
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002906 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002907 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002908
Bram Moolenaar66b51422019-08-18 21:44:12 +02002909 if (*e != NUL)
2910 ++e;
2911 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002912 *matches = ga.ga_data;
2913 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002914
2915 vim_free(buf);
2916 vim_free(pat);
2917 if (mustfree)
2918 vim_free(path);
2919 hash_clear(&found_ht);
2920 return OK;
2921}
2922
2923# if defined(FEAT_EVAL)
2924/*
2925 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002926 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002927 */
2928 static void *
2929call_user_expand_func(
2930 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002931 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002932{
2933 cmdline_info_T *ccline = get_cmdline_info();
2934 int keep = 0;
2935 typval_T args[4];
2936 sctx_T save_current_sctx = current_sctx;
2937 char_u *pat = NULL;
2938 void *ret;
2939
2940 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2941 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002942
2943 if (ccline->cmdbuff != NULL)
2944 {
2945 keep = ccline->cmdbuff[ccline->cmdlen];
2946 ccline->cmdbuff[ccline->cmdlen] = 0;
2947 }
2948
2949 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2950
2951 args[0].v_type = VAR_STRING;
2952 args[0].vval.v_string = pat;
2953 args[1].v_type = VAR_STRING;
2954 args[1].vval.v_string = xp->xp_line;
2955 args[2].v_type = VAR_NUMBER;
2956 args[2].vval.v_number = xp->xp_col;
2957 args[3].v_type = VAR_UNKNOWN;
2958
2959 current_sctx = xp->xp_script_ctx;
2960
2961 ret = user_expand_func(xp->xp_arg, 3, args);
2962
2963 current_sctx = save_current_sctx;
2964 if (ccline->cmdbuff != NULL)
2965 ccline->cmdbuff[ccline->cmdlen] = keep;
2966
2967 vim_free(pat);
2968 return ret;
2969}
2970
2971/*
2972 * Expand names with a function defined by the user.
2973 */
2974 static int
2975ExpandUserDefined(
2976 expand_T *xp,
2977 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002978 char_u ***matches,
2979 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002980{
2981 char_u *retstr;
2982 char_u *s;
2983 char_u *e;
2984 int keep;
2985 garray_T ga;
2986 int skip;
2987
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002988 *matches = NULL;
2989 *numMatches = 0;
2990 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002991 if (retstr == NULL)
2992 return FAIL;
2993
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002994 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002995 for (s = retstr; *s != NUL; s = e)
2996 {
2997 e = vim_strchr(s, '\n');
2998 if (e == NULL)
2999 e = s + STRLEN(s);
3000 keep = *e;
3001 *e = NUL;
3002
3003 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
3004 *e = keep;
3005
3006 if (!skip)
3007 {
3008 if (ga_grow(&ga, 1) == FAIL)
3009 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02003010 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003011 ++ga.ga_len;
3012 }
3013
3014 if (*e != NUL)
3015 ++e;
3016 }
3017 vim_free(retstr);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003018 *matches = ga.ga_data;
3019 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003020 return OK;
3021}
3022
3023/*
3024 * Expand names with a list returned by a function defined by the user.
3025 */
3026 static int
3027ExpandUserList(
3028 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003029 char_u ***matches,
3030 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003031{
3032 list_T *retlist;
3033 listitem_T *li;
3034 garray_T ga;
3035
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003036 *matches = NULL;
3037 *numMatches = 0;
3038 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003039 if (retlist == NULL)
3040 return FAIL;
3041
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003042 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003043 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003044 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003045 {
3046 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3047 continue; // Skip non-string items and empty strings
3048
3049 if (ga_grow(&ga, 1) == FAIL)
3050 break;
3051
3052 ((char_u **)ga.ga_data)[ga.ga_len] =
3053 vim_strsave(li->li_tv.vval.v_string);
3054 ++ga.ga_len;
3055 }
3056 list_unref(retlist);
3057
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003058 *matches = ga.ga_data;
3059 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003060 return OK;
3061}
3062# endif
3063
3064/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003065 * Expand "file" for all comma-separated directories in "path".
3066 * Adds the matches to "ga". Caller must init "ga".
3067 */
3068 void
3069globpath(
3070 char_u *path,
3071 char_u *file,
3072 garray_T *ga,
3073 int expand_options)
3074{
3075 expand_T xpc;
3076 char_u *buf;
3077 int i;
3078 int num_p;
3079 char_u **p;
3080
3081 buf = alloc(MAXPATHL);
3082 if (buf == NULL)
3083 return;
3084
3085 ExpandInit(&xpc);
3086 xpc.xp_context = EXPAND_FILES;
3087
3088 // Loop over all entries in {path}.
3089 while (*path != NUL)
3090 {
3091 // Copy one item of the path to buf[] and concatenate the file name.
3092 copy_option_part(&path, buf, MAXPATHL, ",");
3093 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3094 {
3095# if defined(MSWIN)
3096 // Using the platform's path separator (\) makes vim incorrectly
3097 // treat it as an escape character, use '/' instead.
3098 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3099 STRCAT(buf, "/");
3100# else
3101 add_pathsep(buf);
3102# endif
3103 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003104 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003105 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3106 {
3107 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3108
3109 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003110 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111 for (i = 0; i < num_p; ++i)
3112 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003113 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003114 ++ga->ga_len;
3115 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003116 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003117 }
3118 }
3119 }
3120
3121 vim_free(buf);
3122}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003123
Bram Moolenaareadee482020-09-04 15:37:31 +02003124#ifdef FEAT_WILDMENU
3125
3126/*
3127 * Translate some keys pressed when 'wildmenu' is used.
3128 */
3129 int
3130wildmenu_translate_key(
3131 cmdline_info_T *cclp,
3132 int key,
3133 expand_T *xp,
3134 int did_wild_list)
3135{
3136 int c = key;
3137
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003138#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003139 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003140 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003141 // When the popup menu is used for cmdline completion:
3142 // Up : go to the previous item in the menu
3143 // Down : go to the next item in the menu
3144 // Left : go to the parent directory
3145 // Right: list the files in the selected directory
3146 switch (c)
3147 {
3148 case K_UP: c = K_LEFT; break;
3149 case K_DOWN: c = K_RIGHT; break;
3150 case K_LEFT: c = K_UP; break;
3151 case K_RIGHT: c = K_DOWN; break;
3152 default: break;
3153 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003154 }
3155#endif
3156
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003157 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003158 {
3159 if (c == K_LEFT)
3160 c = Ctrl_P;
3161 else if (c == K_RIGHT)
3162 c = Ctrl_N;
3163 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003164
Bram Moolenaareadee482020-09-04 15:37:31 +02003165 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003166 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003167 && cclp->cmdpos > 1
3168 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3169 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3170 && (c == '\n' || c == '\r' || c == K_KENTER))
3171 c = K_DOWN;
3172
3173 return c;
3174}
3175
3176/*
3177 * Delete characters on the command line, from "from" to the current
3178 * position.
3179 */
3180 static void
3181cmdline_del(cmdline_info_T *cclp, int from)
3182{
3183 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3184 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3185 cclp->cmdlen -= cclp->cmdpos - from;
3186 cclp->cmdpos = from;
3187}
3188
3189/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003190 * Handle a key pressed when the wild menu for the menu names
3191 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003192 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003193 static int
3194wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003195{
Bram Moolenaareadee482020-09-04 15:37:31 +02003196 int i;
3197 int j;
3198
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003199 // Hitting <Down> after "emenu Name.": complete submenu
3200 if (key == K_DOWN && cclp->cmdpos > 0
3201 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003202 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003203 key = p_wc;
3204 KeyTyped = TRUE; // in case the key was mapped
3205 }
3206 else if (key == K_UP)
3207 {
3208 // Hitting <Up>: Remove one submenu name in front of the
3209 // cursor
3210 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003211
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003212 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3213 i = 0;
3214 while (--j > 0)
3215 {
3216 // check for start of menu name
3217 if (cclp->cmdbuff[j] == ' '
3218 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003219 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003220 i = j + 1;
3221 break;
3222 }
3223 // check for start of submenu name
3224 if (cclp->cmdbuff[j] == '.'
3225 && cclp->cmdbuff[j - 1] != '\\')
3226 {
3227 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003228 {
3229 i = j + 1;
3230 break;
3231 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003232 else
3233 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003234 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003235 }
3236 if (i > 0)
3237 cmdline_del(cclp, i);
3238 key = p_wc;
3239 KeyTyped = TRUE; // in case the key was mapped
3240 xp->xp_context = EXPAND_NOTHING;
3241 }
3242
3243 return key;
3244}
3245
3246/*
3247 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3248 * directory names (EXPAND_DIRECTORIES) or shell command names
3249 * (EXPAND_SHELLCMD) is displayed.
3250 */
3251 static int
3252wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3253{
3254 int i;
3255 int j;
3256 char_u upseg[5];
3257
3258 upseg[0] = PATHSEP;
3259 upseg[1] = '.';
3260 upseg[2] = '.';
3261 upseg[3] = PATHSEP;
3262 upseg[4] = NUL;
3263
3264 if (key == K_DOWN
3265 && cclp->cmdpos > 0
3266 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3267 && (cclp->cmdpos < 3
3268 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3269 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3270 {
3271 // go down a directory
3272 key = p_wc;
3273 KeyTyped = TRUE; // in case the key was mapped
3274 }
3275 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3276 {
3277 // If in a direct ancestor, strip off one ../ to go down
3278 int found = FALSE;
3279
3280 j = cclp->cmdpos;
3281 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3282 while (--j > i)
3283 {
3284 if (has_mbyte)
3285 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3286 if (vim_ispathsep(cclp->cmdbuff[j]))
3287 {
3288 found = TRUE;
3289 break;
3290 }
3291 }
3292 if (found
3293 && cclp->cmdbuff[j - 1] == '.'
3294 && cclp->cmdbuff[j - 2] == '.'
3295 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3296 {
3297 cmdline_del(cclp, j - 2);
3298 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003299 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003300 }
3301 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003302 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003303 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003304 // go up a directory
3305 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003306
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003307 j = cclp->cmdpos - 1;
3308 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3309 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003310 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003311 if (has_mbyte)
3312 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3313 if (vim_ispathsep(cclp->cmdbuff[j])
3314# ifdef BACKSLASH_IN_FILENAME
3315 && vim_strchr((char_u *)" *?[{`$%#",
3316 cclp->cmdbuff[j + 1]) == NULL
3317# endif
3318 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003319 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003320 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003321 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003322 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003323 break;
3324 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003325 else
3326 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003327 }
3328 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003329
3330 if (!found)
3331 j = i;
3332 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3333 j += 4;
3334 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3335 && j == i)
3336 j += 3;
3337 else
3338 j = 0;
3339 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003340 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003341 // TODO this is only for DOS/UNIX systems - need to put in
3342 // machine-specific stuff here and in upseg init
3343 cmdline_del(cclp, j);
3344 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003345 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003346 else if (cclp->cmdpos > i)
3347 cmdline_del(cclp, i);
3348
3349 // Now complete in the new directory. Set KeyTyped in case the
3350 // Up key came from a mapping.
3351 key = p_wc;
3352 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003353 }
3354
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003355 return key;
3356}
3357
3358/*
3359 * Handle a key pressed when the wild menu is displayed
3360 */
3361 int
3362wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3363{
3364 if (xp->xp_context == EXPAND_MENUNAMES)
3365 return wildmenu_process_key_menunames(cclp, key, xp);
3366 else if ((xp->xp_context == EXPAND_FILES
3367 || xp->xp_context == EXPAND_DIRECTORIES
3368 || xp->xp_context == EXPAND_SHELLCMD))
3369 return wildmenu_process_key_filenames(cclp, key, xp);
3370
3371 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003372}
3373
3374/*
3375 * Free expanded names when finished walking through the matches
3376 */
3377 void
3378wildmenu_cleanup(cmdline_info_T *cclp)
3379{
3380 int skt = KeyTyped;
3381 int old_RedrawingDisabled = RedrawingDisabled;
3382
3383 if (!p_wmnu || wild_menu_showing == 0)
3384 return;
3385
3386 if (cclp->input_fn)
3387 RedrawingDisabled = 0;
3388
3389 if (wild_menu_showing == WM_SCROLLED)
3390 {
3391 // Entered command line, move it up
3392 cmdline_row--;
3393 redrawcmd();
3394 }
3395 else if (save_p_ls != -1)
3396 {
3397 // restore 'laststatus' and 'winminheight'
3398 p_ls = save_p_ls;
3399 p_wmh = save_p_wmh;
3400 last_status(FALSE);
3401 update_screen(VALID); // redraw the screen NOW
3402 redrawcmd();
3403 save_p_ls = -1;
3404 }
3405 else
3406 {
3407 win_redraw_last_status(topframe);
3408 redraw_statuslines();
3409 }
3410 KeyTyped = skt;
3411 wild_menu_showing = 0;
3412 if (cclp->input_fn)
3413 RedrawingDisabled = old_RedrawingDisabled;
3414}
3415#endif
3416
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003417#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003418/*
3419 * "getcompletion()" function
3420 */
3421 void
3422f_getcompletion(typval_T *argvars, typval_T *rettv)
3423{
3424 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003425 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003426 expand_T xpc;
3427 int filtered = FALSE;
3428 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003429 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003430
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003431 if (in_vim9script()
3432 && (check_for_string_arg(argvars, 0) == FAIL
3433 || check_for_string_arg(argvars, 1) == FAIL
3434 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3435 return;
3436
ii144785fe02021-11-21 12:13:56 +00003437 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003438 if (argvars[1].v_type != VAR_STRING)
3439 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003440 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003441 return;
3442 }
3443 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003444
3445 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003446 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003447
3448 if (p_wic)
3449 options |= WILD_ICASE;
3450
3451 // For filtered results, 'wildignore' is used
3452 if (!filtered)
3453 options |= WILD_KEEP_ALL;
3454
3455 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003456 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003457 {
ii144785fe02021-11-21 12:13:56 +00003458 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003459 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003460 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003461 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003462 else
3463 {
ii144785fe02021-11-21 12:13:56 +00003464 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003465 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3466
3467 xpc.xp_context = cmdcomplete_str_to_type(type);
3468 if (xpc.xp_context == EXPAND_NOTHING)
3469 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003470 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003471 return;
3472 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003473
3474# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003475 if (xpc.xp_context == EXPAND_MENUS)
3476 {
3477 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3478 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3479 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480# endif
3481# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003482 if (xpc.xp_context == EXPAND_CSCOPE)
3483 {
3484 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3485 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3486 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003487# endif
3488# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003489 if (xpc.xp_context == EXPAND_SIGN)
3490 {
3491 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3492 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3493 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003494# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003495 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003496
3497 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3498 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3499 {
3500 int i;
3501
3502 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3503
3504 for (i = 0; i < xpc.xp_numfiles; i++)
3505 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3506 }
3507 vim_free(pat);
3508 ExpandCleanup(&xpc);
3509}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003510#endif // FEAT_EVAL