blob: 94781f82f0643b4f3beda0a42bb9068d15c76ebc [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;
Bram Moolenaard0190392019-08-23 21:17:35 +02001215
1216 // Isolate the command and search for it in the command table.
1217 // Exceptions:
1218 // - the 'k' command can directly be followed by any character, but
1219 // do accept "keepmarks", "keepalt" and "keepjumps".
1220 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
1221 if (*cmd == 'k' && cmd[1] != 'e')
1222 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001223 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001224 p = cmd + 1;
1225 }
1226 else
1227 {
1228 p = cmd;
1229 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1230 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001231 // A user command may contain digits.
1232 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1233 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001234 while (ASCII_ISALNUM(*p) || *p == '*')
1235 ++p;
1236 // for python 3.x: ":py3*" commands completion
1237 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1238 {
1239 ++p;
1240 while (ASCII_ISALPHA(*p) || *p == '*')
1241 ++p;
1242 }
1243 // check for non-alpha command
1244 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1245 ++p;
1246 len = (int)(p - cmd);
1247
1248 if (len == 0)
1249 {
1250 xp->xp_context = EXPAND_UNSUCCESSFUL;
1251 return NULL;
1252 }
1253
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001254 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001255
1256 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1257 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1258 ++p;
1259 }
1260
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001261 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001262 // character, complete the command name.
1263 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1264 return NULL;
1265
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001266 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001267 {
1268 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1269 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001270 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001271 p = cmd + 1;
1272 }
1273 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1274 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001275 eap->cmd = cmd;
1276 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001277 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001278 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001279 }
1280 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001281 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001282 {
1283 // Not still touching the command and it was an illegal one
1284 xp->xp_context = EXPAND_UNSUCCESSFUL;
1285 return NULL;
1286 }
1287
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001288 return p;
1289}
Bram Moolenaard0190392019-08-23 21:17:35 +02001290
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001291/*
1292 * Set the completion context for a command argument with wild card characters.
1293 */
1294 static void
1295set_context_for_wildcard_arg(
1296 exarg_T *eap,
1297 char_u *arg,
1298 int usefilter,
1299 expand_T *xp,
1300 int *complp)
1301{
1302 char_u *p;
1303 int c;
1304 int in_quote = FALSE;
1305 char_u *bow = NULL; // Beginning of word
1306 int len = 0;
1307
1308 // Allow spaces within back-quotes to count as part of the argument
1309 // being expanded.
1310 xp->xp_pattern = skipwhite(arg);
1311 p = xp->xp_pattern;
1312 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001313 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001314 if (has_mbyte)
1315 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001316 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001317 c = *p;
1318 if (c == '\\' && p[1] != NUL)
1319 ++p;
1320 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001321 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001322 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001323 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001324 xp->xp_pattern = p;
1325 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001326 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001327 in_quote = !in_quote;
1328 }
1329 // An argument can contain just about everything, except
1330 // characters that end the command and white space.
1331 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001332#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001333 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001334#endif
1335 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001336 {
1337 len = 0; // avoid getting stuck when space is in 'isfname'
1338 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001339 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001340 if (has_mbyte)
1341 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001342 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001343 c = *p;
1344 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001345 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001346 if (has_mbyte)
1347 len = (*mb_ptr2len)(p);
1348 else
1349 len = 1;
1350 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001351 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001352 if (in_quote)
1353 bow = p;
1354 else
1355 xp->xp_pattern = p;
1356 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001357 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001358 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001359 }
1360
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001361 // If we are still inside the quotes, and we passed a space, just
1362 // expand from there.
1363 if (bow != NULL && in_quote)
1364 xp->xp_pattern = bow;
1365 xp->xp_context = EXPAND_FILES;
1366
1367 // For a shell command more chars need to be escaped.
1368 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1369 {
1370#ifndef BACKSLASH_IN_FILENAME
1371 xp->xp_shell = TRUE;
1372#endif
1373 // When still after the command name expand executables.
1374 if (xp->xp_pattern == skipwhite(arg))
1375 xp->xp_context = EXPAND_SHELLCMD;
1376 }
1377
1378 // Check for environment variable.
1379 if (*xp->xp_pattern == '$')
1380 {
1381 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1382 if (!vim_isIDc(*p))
1383 break;
1384 if (*p == NUL)
1385 {
1386 xp->xp_context = EXPAND_ENV_VARS;
1387 ++xp->xp_pattern;
1388 // Avoid that the assignment uses EXPAND_FILES again.
1389 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1390 *complp = EXPAND_ENV_VARS;
1391 }
1392 }
1393 // Check for user names.
1394 if (*xp->xp_pattern == '~')
1395 {
1396 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1397 ;
1398 // Complete ~user only if it partially matches a user name.
1399 // A full match ~user<Tab> will be replaced by user's home
1400 // directory i.e. something like ~user<Tab> -> /home/user/
1401 if (*p == NUL && p > xp->xp_pattern + 1
1402 && match_user(xp->xp_pattern + 1) >= 1)
1403 {
1404 xp->xp_context = EXPAND_USER;
1405 ++xp->xp_pattern;
1406 }
1407 }
1408}
1409
1410/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001411 * Set the completion context for the :filter command. Returns a pointer to the
1412 * next command after the :filter command.
1413 */
1414 static char_u *
1415set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1416{
1417 if (*arg != NUL)
1418 arg = skip_vimgrep_pat(arg, NULL, NULL);
1419 if (arg == NULL || *arg == NUL)
1420 {
1421 xp->xp_context = EXPAND_NOTHING;
1422 return NULL;
1423 }
1424 return skipwhite(arg);
1425}
1426
1427#ifdef FEAT_SEARCH_EXTRA
1428/*
1429 * Set the completion context for the :match command. Returns a pointer to the
1430 * next command after the :match command.
1431 */
1432 static char_u *
1433set_context_in_match_cmd(expand_T *xp, char_u *arg)
1434{
1435 if (*arg == NUL || !ends_excmd(*arg))
1436 {
1437 // also complete "None"
1438 set_context_in_echohl_cmd(xp, arg);
1439 arg = skipwhite(skiptowhite(arg));
1440 if (*arg != NUL)
1441 {
1442 xp->xp_context = EXPAND_NOTHING;
1443 arg = skip_regexp(arg + 1, *arg, magic_isset());
1444 }
1445 }
1446 return find_nextcmd(arg);
1447}
1448#endif
1449
1450/*
1451 * Returns a pointer to the next command after a :global or a :v command.
1452 * Returns NULL if there is no next command.
1453 */
1454 static char_u *
1455find_cmd_after_global_cmd(char_u *arg)
1456{
1457 int delim;
1458
1459 delim = *arg; // get the delimiter
1460 if (delim)
1461 ++arg; // skip delimiter if there is one
1462
1463 while (arg[0] != NUL && arg[0] != delim)
1464 {
1465 if (arg[0] == '\\' && arg[1] != NUL)
1466 ++arg;
1467 ++arg;
1468 }
1469 if (arg[0] != NUL)
1470 return arg + 1;
1471
1472 return NULL;
1473}
1474
1475/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001476 * Returns a pointer to the next command after a :substitute or a :& command.
1477 * Returns NULL if there is no next command.
1478 */
1479 static char_u *
1480find_cmd_after_substitute_cmd(char_u *arg)
1481{
1482 int delim;
1483
1484 delim = *arg;
1485 if (delim)
1486 {
1487 // skip "from" part
1488 ++arg;
1489 arg = skip_regexp(arg, delim, magic_isset());
1490
1491 if (arg[0] != NUL && arg[0] == delim)
1492 {
1493 // skip "to" part
1494 ++arg;
1495 while (arg[0] != NUL && arg[0] != delim)
1496 {
1497 if (arg[0] == '\\' && arg[1] != NUL)
1498 ++arg;
1499 ++arg;
1500 }
1501 if (arg[0] != NUL) // skip delimiter
1502 ++arg;
1503 }
1504 }
1505 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1506 ++arg;
1507 if (arg[0] != NUL)
1508 return arg;
1509
1510 return NULL;
1511}
1512
1513/*
1514 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1515 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1516 * Returns NULL if there is no next command.
1517 */
1518 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001519find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001520{
1521 arg = skipwhite(skipdigits(arg)); // skip count
1522 if (*arg == '/') // Match regexp, not just whole words
1523 {
1524 for (++arg; *arg && *arg != '/'; arg++)
1525 if (*arg == '\\' && arg[1] != NUL)
1526 arg++;
1527 if (*arg)
1528 {
1529 arg = skipwhite(arg + 1);
1530
1531 // Check for trailing illegal characters
1532 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1533 xp->xp_context = EXPAND_NOTHING;
1534 else
1535 return arg;
1536 }
1537 }
1538
1539 return NULL;
1540}
1541
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001542#ifdef FEAT_EVAL
1543/*
1544 * Set the completion context for the :unlet command. Always returns NULL.
1545 */
1546 static char_u *
1547set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1548{
1549 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1550 arg = xp->xp_pattern + 1;
1551
1552 xp->xp_context = EXPAND_USER_VARS;
1553 xp->xp_pattern = arg;
1554
1555 if (*xp->xp_pattern == '$')
1556 {
1557 xp->xp_context = EXPAND_ENV_VARS;
1558 ++xp->xp_pattern;
1559 }
1560
1561 return NULL;
1562}
1563#endif
1564
1565#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1566/*
1567 * Set the completion context for the :language command. Always returns NULL.
1568 */
1569 static char_u *
1570set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1571{
1572 char_u *p;
1573
1574 p = skiptowhite(arg);
1575 if (*p == NUL)
1576 {
1577 xp->xp_context = EXPAND_LANGUAGE;
1578 xp->xp_pattern = arg;
1579 }
1580 else
1581 {
1582 if ( STRNCMP(arg, "messages", p - arg) == 0
1583 || STRNCMP(arg, "ctype", p - arg) == 0
1584 || STRNCMP(arg, "time", p - arg) == 0
1585 || STRNCMP(arg, "collate", p - arg) == 0)
1586 {
1587 xp->xp_context = EXPAND_LOCALES;
1588 xp->xp_pattern = skipwhite(p);
1589 }
1590 else
1591 xp->xp_context = EXPAND_NOTHING;
1592 }
1593
1594 return NULL;
1595}
1596#endif
1597
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001598/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001599 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1600 * The argument to the command is 'arg' and the argument flags is 'argt'.
1601 * For user-defined commands and for environment variables, 'compl' has the
1602 * completion type.
1603 * Returns a pointer to the next command. Returns NULL if there is no next
1604 * command.
1605 */
1606 static char_u *
1607set_context_by_cmdname(
1608 char_u *cmd,
1609 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001610 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001611 char_u *arg,
1612 long argt,
1613 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001614 int forceit)
1615{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001616 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001617 {
1618 case CMD_find:
1619 case CMD_sfind:
1620 case CMD_tabfind:
1621 if (xp->xp_context == EXPAND_FILES)
1622 xp->xp_context = EXPAND_FILES_IN_PATH;
1623 break;
1624 case CMD_cd:
1625 case CMD_chdir:
1626 case CMD_tcd:
1627 case CMD_tchdir:
1628 case CMD_lcd:
1629 case CMD_lchdir:
1630 if (xp->xp_context == EXPAND_FILES)
1631 xp->xp_context = EXPAND_DIRECTORIES;
1632 break;
1633 case CMD_help:
1634 xp->xp_context = EXPAND_HELP;
1635 xp->xp_pattern = arg;
1636 break;
1637
1638 // Command modifiers: return the argument.
1639 // Also for commands with an argument that is a command.
1640 case CMD_aboveleft:
1641 case CMD_argdo:
1642 case CMD_belowright:
1643 case CMD_botright:
1644 case CMD_browse:
1645 case CMD_bufdo:
1646 case CMD_cdo:
1647 case CMD_cfdo:
1648 case CMD_confirm:
1649 case CMD_debug:
1650 case CMD_folddoclosed:
1651 case CMD_folddoopen:
1652 case CMD_hide:
1653 case CMD_keepalt:
1654 case CMD_keepjumps:
1655 case CMD_keepmarks:
1656 case CMD_keeppatterns:
1657 case CMD_ldo:
1658 case CMD_leftabove:
1659 case CMD_lfdo:
1660 case CMD_lockmarks:
1661 case CMD_noautocmd:
1662 case CMD_noswapfile:
1663 case CMD_rightbelow:
1664 case CMD_sandbox:
1665 case CMD_silent:
1666 case CMD_tab:
1667 case CMD_tabdo:
1668 case CMD_topleft:
1669 case CMD_verbose:
1670 case CMD_vertical:
1671 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001672 case CMD_vim9cmd:
1673 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001674 return arg;
1675
1676 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001677 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001678
1679#ifdef FEAT_SEARCH_EXTRA
1680 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001681 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001682#endif
1683
1684 // All completion for the +cmdline_compl feature goes here.
1685
1686 case CMD_command:
1687 return set_context_in_user_cmd(xp, arg);
1688
1689 case CMD_delcommand:
1690 xp->xp_context = EXPAND_USER_COMMANDS;
1691 xp->xp_pattern = arg;
1692 break;
1693
1694 case CMD_global:
1695 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001696 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001697 case CMD_and:
1698 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001699 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001700 case CMD_isearch:
1701 case CMD_dsearch:
1702 case CMD_ilist:
1703 case CMD_dlist:
1704 case CMD_ijump:
1705 case CMD_psearch:
1706 case CMD_djump:
1707 case CMD_isplit:
1708 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001709 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001710 case CMD_autocmd:
1711 return set_context_in_autocmd(xp, arg, FALSE);
1712 case CMD_doautocmd:
1713 case CMD_doautoall:
1714 return set_context_in_autocmd(xp, arg, TRUE);
1715 case CMD_set:
1716 set_context_in_set_cmd(xp, arg, 0);
1717 break;
1718 case CMD_setglobal:
1719 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1720 break;
1721 case CMD_setlocal:
1722 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1723 break;
1724 case CMD_tag:
1725 case CMD_stag:
1726 case CMD_ptag:
1727 case CMD_ltag:
1728 case CMD_tselect:
1729 case CMD_stselect:
1730 case CMD_ptselect:
1731 case CMD_tjump:
1732 case CMD_stjump:
1733 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001734 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001735 xp->xp_context = EXPAND_TAGS_LISTFILES;
1736 else
1737 xp->xp_context = EXPAND_TAGS;
1738 xp->xp_pattern = arg;
1739 break;
1740 case CMD_augroup:
1741 xp->xp_context = EXPAND_AUGROUP;
1742 xp->xp_pattern = arg;
1743 break;
1744#ifdef FEAT_SYN_HL
1745 case CMD_syntax:
1746 set_context_in_syntax_cmd(xp, arg);
1747 break;
1748#endif
1749#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001750 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001751 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001752 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001753 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001754 case CMD_if:
1755 case CMD_elseif:
1756 case CMD_while:
1757 case CMD_for:
1758 case CMD_echo:
1759 case CMD_echon:
1760 case CMD_execute:
1761 case CMD_echomsg:
1762 case CMD_echoerr:
1763 case CMD_call:
1764 case CMD_return:
1765 case CMD_cexpr:
1766 case CMD_caddexpr:
1767 case CMD_cgetexpr:
1768 case CMD_lexpr:
1769 case CMD_laddexpr:
1770 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001771 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001772 break;
1773
1774 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001775 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001776 case CMD_function:
1777 case CMD_delfunction:
1778 xp->xp_context = EXPAND_USER_FUNC;
1779 xp->xp_pattern = arg;
1780 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001781 case CMD_disassemble:
1782 set_context_in_disassemble_cmd(xp, arg);
1783 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001784
1785 case CMD_echohl:
1786 set_context_in_echohl_cmd(xp, arg);
1787 break;
1788#endif
1789 case CMD_highlight:
1790 set_context_in_highlight_cmd(xp, arg);
1791 break;
1792#ifdef FEAT_CSCOPE
1793 case CMD_cscope:
1794 case CMD_lcscope:
1795 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001796 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001797 break;
1798#endif
1799#ifdef FEAT_SIGNS
1800 case CMD_sign:
1801 set_context_in_sign_cmd(xp, arg);
1802 break;
1803#endif
1804 case CMD_bdelete:
1805 case CMD_bwipeout:
1806 case CMD_bunload:
1807 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1808 arg = xp->xp_pattern + 1;
1809 // FALLTHROUGH
1810 case CMD_buffer:
1811 case CMD_sbuffer:
1812 case CMD_checktime:
1813 xp->xp_context = EXPAND_BUFFERS;
1814 xp->xp_pattern = arg;
1815 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001816#ifdef FEAT_DIFF
1817 case CMD_diffget:
1818 case CMD_diffput:
1819 // If current buffer is in diff mode, complete buffer names
1820 // which are in diff mode, and different than current buffer.
1821 xp->xp_context = EXPAND_DIFF_BUFFERS;
1822 xp->xp_pattern = arg;
1823 break;
1824#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001825 case CMD_USER:
1826 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001827 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1828 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001829
1830 case CMD_map: case CMD_noremap:
1831 case CMD_nmap: case CMD_nnoremap:
1832 case CMD_vmap: case CMD_vnoremap:
1833 case CMD_omap: case CMD_onoremap:
1834 case CMD_imap: case CMD_inoremap:
1835 case CMD_cmap: case CMD_cnoremap:
1836 case CMD_lmap: case CMD_lnoremap:
1837 case CMD_smap: case CMD_snoremap:
1838 case CMD_tmap: case CMD_tnoremap:
1839 case CMD_xmap: case CMD_xnoremap:
1840 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001841 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001842 case CMD_unmap:
1843 case CMD_nunmap:
1844 case CMD_vunmap:
1845 case CMD_ounmap:
1846 case CMD_iunmap:
1847 case CMD_cunmap:
1848 case CMD_lunmap:
1849 case CMD_sunmap:
1850 case CMD_tunmap:
1851 case CMD_xunmap:
1852 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001853 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001854 case CMD_mapclear:
1855 case CMD_nmapclear:
1856 case CMD_vmapclear:
1857 case CMD_omapclear:
1858 case CMD_imapclear:
1859 case CMD_cmapclear:
1860 case CMD_lmapclear:
1861 case CMD_smapclear:
1862 case CMD_tmapclear:
1863 case CMD_xmapclear:
1864 xp->xp_context = EXPAND_MAPCLEAR;
1865 xp->xp_pattern = arg;
1866 break;
1867
1868 case CMD_abbreviate: case CMD_noreabbrev:
1869 case CMD_cabbrev: case CMD_cnoreabbrev:
1870 case CMD_iabbrev: case CMD_inoreabbrev:
1871 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001872 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001873 case CMD_unabbreviate:
1874 case CMD_cunabbrev:
1875 case CMD_iunabbrev:
1876 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001877 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001878#ifdef FEAT_MENU
1879 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1880 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1881 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1882 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1883 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1884 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1885 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1886 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1887 case CMD_tmenu: case CMD_tunmenu:
1888 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1889 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1890#endif
1891
1892 case CMD_colorscheme:
1893 xp->xp_context = EXPAND_COLORS;
1894 xp->xp_pattern = arg;
1895 break;
1896
1897 case CMD_compiler:
1898 xp->xp_context = EXPAND_COMPILER;
1899 xp->xp_pattern = arg;
1900 break;
1901
1902 case CMD_ownsyntax:
1903 xp->xp_context = EXPAND_OWNSYNTAX;
1904 xp->xp_pattern = arg;
1905 break;
1906
1907 case CMD_setfiletype:
1908 xp->xp_context = EXPAND_FILETYPE;
1909 xp->xp_pattern = arg;
1910 break;
1911
1912 case CMD_packadd:
1913 xp->xp_context = EXPAND_PACKADD;
1914 xp->xp_pattern = arg;
1915 break;
1916
1917#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1918 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001919 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001920#endif
1921#if defined(FEAT_PROFILE)
1922 case CMD_profile:
1923 set_context_in_profile_cmd(xp, arg);
1924 break;
1925#endif
1926 case CMD_behave:
1927 xp->xp_context = EXPAND_BEHAVE;
1928 xp->xp_pattern = arg;
1929 break;
1930
1931 case CMD_messages:
1932 xp->xp_context = EXPAND_MESSAGES;
1933 xp->xp_pattern = arg;
1934 break;
1935
1936 case CMD_history:
1937 xp->xp_context = EXPAND_HISTORY;
1938 xp->xp_pattern = arg;
1939 break;
1940#if defined(FEAT_PROFILE)
1941 case CMD_syntime:
1942 xp->xp_context = EXPAND_SYNTIME;
1943 xp->xp_pattern = arg;
1944 break;
1945#endif
1946
1947 case CMD_argdelete:
1948 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1949 arg = xp->xp_pattern + 1;
1950 xp->xp_context = EXPAND_ARGLIST;
1951 xp->xp_pattern = arg;
1952 break;
1953
1954 default:
1955 break;
1956 }
1957 return NULL;
1958}
1959
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001960/*
1961 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
1962 * we don't need/want deleted. Maybe this could be done better if we didn't
1963 * repeat all this stuff. The only problem is that they may not stay
1964 * perfectly compatible with each other, but then the command line syntax
1965 * probably won't change that much -- webb.
1966 */
1967 static char_u *
1968set_one_cmd_context(
1969 expand_T *xp,
1970 char_u *buff) // buffer for command string
1971{
1972 char_u *p;
1973 char_u *cmd, *arg;
1974 int len = 0;
1975 exarg_T ea;
1976 int compl = EXPAND_NOTHING;
1977 int forceit = FALSE;
1978 int usefilter = FALSE; // filter instead of file name
1979
1980 ExpandInit(xp);
1981 xp->xp_pattern = buff;
1982 xp->xp_line = buff;
1983 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
1984 ea.argt = 0;
1985
1986 // 1. skip comment lines and leading space, colons or bars
1987 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
1988 ;
1989 xp->xp_pattern = cmd;
1990
1991 if (*cmd == NUL)
1992 return NULL;
1993 if (*cmd == '"') // ignore comment lines
1994 {
1995 xp->xp_context = EXPAND_NOTHING;
1996 return NULL;
1997 }
1998
1999 // 3. Skip over the range to find the command.
2000 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2001 xp->xp_pattern = cmd;
2002 if (*cmd == NUL)
2003 return NULL;
2004 if (*cmd == '"')
2005 {
2006 xp->xp_context = EXPAND_NOTHING;
2007 return NULL;
2008 }
2009
2010 if (*cmd == '|' || *cmd == '\n')
2011 return cmd + 1; // There's another command
2012
2013 // Get the command index.
2014 p = set_cmd_index(cmd, &ea, xp, &compl);
2015 if (p == NULL)
2016 return NULL;
2017
2018 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2019
2020 if (*p == '!') // forced commands
2021 {
2022 forceit = TRUE;
2023 ++p;
2024 }
2025
2026 // 6. parse arguments
2027 if (!IS_USER_CMDIDX(ea.cmdidx))
2028 ea.argt = excmd_get_argt(ea.cmdidx);
2029
2030 arg = skipwhite(p);
2031
2032 // Skip over ++argopt argument
2033 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2034 {
2035 p = arg;
2036 while (*p && !vim_isspace(*p))
2037 MB_PTR_ADV(p);
2038 arg = skipwhite(p);
2039 }
2040
2041 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2042 {
2043 if (*arg == '>') // append
2044 {
2045 if (*++arg == '>')
2046 ++arg;
2047 arg = skipwhite(arg);
2048 }
2049 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2050 {
2051 ++arg;
2052 usefilter = TRUE;
2053 }
2054 }
2055
2056 if (ea.cmdidx == CMD_read)
2057 {
2058 usefilter = forceit; // :r! filter if forced
2059 if (*arg == '!') // :r !filter
2060 {
2061 ++arg;
2062 usefilter = TRUE;
2063 }
2064 }
2065
2066 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2067 {
2068 while (*arg == *cmd) // allow any number of '>' or '<'
2069 ++arg;
2070 arg = skipwhite(arg);
2071 }
2072
2073 // Does command allow "+command"?
2074 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2075 {
2076 // Check if we're in the +command
2077 p = arg + 1;
2078 arg = skip_cmd_arg(arg, FALSE);
2079
2080 // Still touching the command after '+'?
2081 if (*arg == NUL)
2082 return p;
2083
2084 // Skip space(s) after +command to get to the real argument
2085 arg = skipwhite(arg);
2086 }
2087
2088
2089 // Check for '|' to separate commands and '"' to start comments.
2090 // Don't do this for ":read !cmd" and ":write !cmd".
2091 if ((ea.argt & EX_TRLBAR) && !usefilter)
2092 {
2093 p = arg;
2094 // ":redir @" is not the start of a comment
2095 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2096 p += 2;
2097 while (*p)
2098 {
2099 if (*p == Ctrl_V)
2100 {
2101 if (p[1] != NUL)
2102 ++p;
2103 }
2104 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2105 || *p == '|' || *p == '\n')
2106 {
2107 if (*(p - 1) != '\\')
2108 {
2109 if (*p == '|' || *p == '\n')
2110 return p + 1;
2111 return NULL; // It's a comment
2112 }
2113 }
2114 MB_PTR_ADV(p);
2115 }
2116 }
2117
2118 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2119 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2120 // no arguments allowed but there is something
2121 return NULL;
2122
2123 // Find start of last argument (argument just before cursor):
2124 p = buff;
2125 xp->xp_pattern = p;
2126 len = (int)STRLEN(buff);
2127 while (*p && p < buff + len)
2128 {
2129 if (*p == ' ' || *p == TAB)
2130 {
2131 // argument starts after a space
2132 xp->xp_pattern = ++p;
2133 }
2134 else
2135 {
2136 if (*p == '\\' && *(p + 1) != NUL)
2137 ++p; // skip over escaped character
2138 MB_PTR_ADV(p);
2139 }
2140 }
2141
2142 if (ea.argt & EX_XFILE)
2143 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2144
2145 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002146 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002147 forceit);
2148}
2149
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002150/*
2151 * Set the completion context in 'xp' for command 'str'
2152 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002153 void
2154set_cmd_context(
2155 expand_T *xp,
2156 char_u *str, // start of command line
2157 int len, // length of command line (excl. NUL)
2158 int col, // position of cursor
2159 int use_ccline UNUSED) // use ccline for info
2160{
2161#ifdef FEAT_EVAL
2162 cmdline_info_T *ccline = get_cmdline_info();
2163#endif
2164 int old_char = NUL;
2165 char_u *nextcomm;
2166
2167 // Avoid a UMR warning from Purify, only save the character if it has been
2168 // written before.
2169 if (col < len)
2170 old_char = str[col];
2171 str[col] = NUL;
2172 nextcomm = str;
2173
2174#ifdef FEAT_EVAL
2175 if (use_ccline && ccline->cmdfirstc == '=')
2176 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002177 // pass CMD_SIZE because there is no real command
2178 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002179 }
2180 else if (use_ccline && ccline->input_fn)
2181 {
2182 xp->xp_context = ccline->xp_context;
2183 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002184 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002185 }
2186 else
2187#endif
2188 while (nextcomm != NULL)
2189 nextcomm = set_one_cmd_context(xp, nextcomm);
2190
2191 // Store the string here so that call_user_expand_func() can get to them
2192 // easily.
2193 xp->xp_line = str;
2194 xp->xp_col = col;
2195
2196 str[col] = old_char;
2197}
2198
2199/*
2200 * Expand the command line "str" from context "xp".
2201 * "xp" must have been set by set_cmd_context().
2202 * xp->xp_pattern points into "str", to where the text that is to be expanded
2203 * starts.
2204 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2205 * cursor.
2206 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2207 * key that triggered expansion literally.
2208 * Returns EXPAND_OK otherwise.
2209 */
2210 int
2211expand_cmdline(
2212 expand_T *xp,
2213 char_u *str, // start of command line
2214 int col, // position of cursor
2215 int *matchcount, // return: nr of matches
2216 char_u ***matches) // return: array of pointers to matches
2217{
2218 char_u *file_str = NULL;
2219 int options = WILD_ADD_SLASH|WILD_SILENT;
2220
2221 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2222 {
2223 beep_flush();
2224 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2225 }
2226 if (xp->xp_context == EXPAND_NOTHING)
2227 {
2228 // Caller can use the character as a normal char instead
2229 return EXPAND_NOTHING;
2230 }
2231
2232 // add star to file name, or convert to regexp if not exp. files.
2233 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002234 if (cmdline_fuzzy_completion_supported(xp))
2235 // If fuzzy matching, don't modify the search string
2236 file_str = vim_strsave(xp->xp_pattern);
2237 else
2238 {
2239 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2240 if (file_str == NULL)
2241 return EXPAND_UNSUCCESSFUL;
2242 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002243
2244 if (p_wic)
2245 options += WILD_ICASE;
2246
2247 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002248 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002249 {
2250 *matchcount = 0;
2251 *matches = NULL;
2252 }
2253 vim_free(file_str);
2254
2255 return EXPAND_OK;
2256}
2257
Bram Moolenaar66b51422019-08-18 21:44:12 +02002258/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002259 * Expand file or directory names.
2260 */
2261 static int
2262expand_files_and_dirs(
2263 expand_T *xp,
2264 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002265 char_u ***matches,
2266 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002267 int flags,
2268 int options)
2269{
2270 int free_pat = FALSE;
2271 int i;
2272 int ret;
2273
2274 // for ":set path=" and ":set tags=" halve backslashes for escaped
2275 // space
2276 if (xp->xp_backslash != XP_BS_NONE)
2277 {
2278 free_pat = TRUE;
2279 pat = vim_strsave(pat);
2280 for (i = 0; pat[i]; ++i)
2281 if (pat[i] == '\\')
2282 {
2283 if (xp->xp_backslash == XP_BS_THREE
2284 && pat[i + 1] == '\\'
2285 && pat[i + 2] == '\\'
2286 && pat[i + 3] == ' ')
2287 STRMOVE(pat + i, pat + i + 3);
2288 if (xp->xp_backslash == XP_BS_ONE
2289 && pat[i + 1] == ' ')
2290 STRMOVE(pat + i, pat + i + 1);
2291 }
2292 }
2293
2294 if (xp->xp_context == EXPAND_FILES)
2295 flags |= EW_FILE;
2296 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2297 flags |= (EW_FILE | EW_PATH);
2298 else
2299 flags = (flags | EW_DIR) & ~EW_FILE;
2300 if (options & WILD_ICASE)
2301 flags |= EW_ICASE;
2302
2303 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002304 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002305 if (free_pat)
2306 vim_free(pat);
2307#ifdef BACKSLASH_IN_FILENAME
2308 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2309 {
2310 int j;
2311
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002312 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002313 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002314 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002315
2316 while (*ptr != NUL)
2317 {
2318 if (p_csl[0] == 's' && *ptr == '\\')
2319 *ptr = '/';
2320 else if (p_csl[0] == 'b' && *ptr == '/')
2321 *ptr = '\\';
2322 ptr += (*mb_ptr2len)(ptr);
2323 }
2324 }
2325 }
2326#endif
2327 return ret;
2328}
2329
2330/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002331 * Function given to ExpandGeneric() to obtain the possible arguments of the
2332 * ":behave {mswin,xterm}" command.
2333 */
2334 static char_u *
2335get_behave_arg(expand_T *xp UNUSED, int idx)
2336{
2337 if (idx == 0)
2338 return (char_u *)"mswin";
2339 if (idx == 1)
2340 return (char_u *)"xterm";
2341 return NULL;
2342}
2343
2344/*
2345 * Function given to ExpandGeneric() to obtain the possible arguments of the
2346 * ":messages {clear}" command.
2347 */
2348 static char_u *
2349get_messages_arg(expand_T *xp UNUSED, int idx)
2350{
2351 if (idx == 0)
2352 return (char_u *)"clear";
2353 return NULL;
2354}
2355
2356 static char_u *
2357get_mapclear_arg(expand_T *xp UNUSED, int idx)
2358{
2359 if (idx == 0)
2360 return (char_u *)"<buffer>";
2361 return NULL;
2362}
2363
2364/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002365 * Do the expansion based on xp->xp_context and 'rmp'.
2366 */
2367 static int
2368ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002369 char_u *pat,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002370 expand_T *xp,
2371 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002372 char_u ***matches,
2373 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002374{
2375 static struct expgen
2376 {
2377 int context;
2378 char_u *((*func)(expand_T *, int));
2379 int ic;
2380 int escaped;
2381 } tab[] =
2382 {
2383 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2384 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2385 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2386 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2387 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2388 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2389 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2390 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2391 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2392 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2393# ifdef FEAT_EVAL
2394 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2395 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2396 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2397 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2398 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2399# endif
2400# ifdef FEAT_MENU
2401 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2402 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2403# endif
2404# ifdef FEAT_SYN_HL
2405 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2406# endif
2407# ifdef FEAT_PROFILE
2408 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2409# endif
2410 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2411 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2412 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
2413# ifdef FEAT_CSCOPE
2414 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2415# endif
2416# ifdef FEAT_SIGNS
2417 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2418# endif
2419# ifdef FEAT_PROFILE
2420 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2421# endif
2422# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2423 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2424 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2425# endif
2426 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2427 {EXPAND_USER, get_users, TRUE, FALSE},
2428 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2429 };
2430 int i;
2431 int ret = FAIL;
2432
2433 // Find a context in the table and call the ExpandGeneric() with the
2434 // right function to do the expansion.
2435 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2436 {
2437 if (xp->xp_context == tab[i].context)
2438 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002439 // Use fuzzy matching if 'wildoptions' has 'fuzzy'.
2440 // If no search pattern is supplied, then don't use fuzzy
2441 // matching and return all the found items.
2442 int fuzzy = cmdline_fuzzy_complete(pat);
2443
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002444 if (tab[i].ic)
2445 rmp->rm_ic = TRUE;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002446 ret = ExpandGeneric(xp, rmp, matches, numMatches,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002447 tab[i].func, tab[i].escaped,
2448 fuzzy ? pat : NULL);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002449 break;
2450 }
2451 }
2452
2453 return ret;
2454}
2455
2456/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002457 * Map wild expand options to flags for expand_wildcards()
2458 */
2459 static int
2460map_wildopts_to_ewflags(int options)
2461{
2462 int flags;
2463
2464 flags = EW_DIR; // include directories
2465 if (options & WILD_LIST_NOTFOUND)
2466 flags |= EW_NOTFOUND;
2467 if (options & WILD_ADD_SLASH)
2468 flags |= EW_ADDSLASH;
2469 if (options & WILD_KEEP_ALL)
2470 flags |= EW_KEEPALL;
2471 if (options & WILD_SILENT)
2472 flags |= EW_SILENT;
2473 if (options & WILD_NOERROR)
2474 flags |= EW_NOERROR;
2475 if (options & WILD_ALLLINKS)
2476 flags |= EW_ALLLINKS;
2477
2478 return flags;
2479}
2480
2481/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002482 * Do the expansion based on xp->xp_context and "pat".
2483 */
2484 static int
2485ExpandFromContext(
2486 expand_T *xp,
2487 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002488 char_u ***matches,
2489 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002490 int options) // WILD_ flags
2491{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002492 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002493 int ret;
2494 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002495 char_u *tofree = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002496
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002497 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002498
2499 if (xp->xp_context == EXPAND_FILES
2500 || xp->xp_context == EXPAND_DIRECTORIES
2501 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002502 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2503 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002504
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002505 *matches = (char_u **)"";
2506 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002507 if (xp->xp_context == EXPAND_HELP)
2508 {
2509 // With an empty argument we would get all the help tags, which is
2510 // very slow. Get matches for "help" instead.
2511 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002512 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002513 {
2514#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002515 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002516#endif
2517 return OK;
2518 }
2519 return FAIL;
2520 }
2521
Bram Moolenaar66b51422019-08-18 21:44:12 +02002522 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002523 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002524 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002525 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002526 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002527 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002528#ifdef FEAT_DIFF
2529 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002530 return ExpandBufnames(pat, numMatches, matches,
2531 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002532#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002533 if (xp->xp_context == EXPAND_TAGS
2534 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002535 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2536 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002537 if (xp->xp_context == EXPAND_COLORS)
2538 {
2539 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002540 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002541 directories);
2542 }
2543 if (xp->xp_context == EXPAND_COMPILER)
2544 {
2545 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002546 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002547 }
2548 if (xp->xp_context == EXPAND_OWNSYNTAX)
2549 {
2550 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002551 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002552 }
2553 if (xp->xp_context == EXPAND_FILETYPE)
2554 {
2555 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002556 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002557 }
2558# if defined(FEAT_EVAL)
2559 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002560 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002561# endif
2562 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002563 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002564
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002565 // When expanding a function name starting with s:, match the <SNR>nr_
2566 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002567 if ((xp->xp_context == EXPAND_USER_FUNC
2568 || xp->xp_context == EXPAND_DISASSEMBLE)
2569 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002570 {
2571 int len = (int)STRLEN(pat) + 20;
2572
2573 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002574 if (tofree == NULL)
2575 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002576 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002577 pat = tofree;
2578 }
2579
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002580 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002581 if (regmatch.regprog == NULL)
2582 return FAIL;
2583
2584 // set ignore-case according to p_ic, p_scs and pat
2585 regmatch.rm_ic = ignorecase(pat);
2586
2587 if (xp->xp_context == EXPAND_SETTINGS
2588 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002589 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002590 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002591 ret = ExpandMappings(&regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002592# if defined(FEAT_EVAL)
2593 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002594 ret = ExpandUserDefined(xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002595# endif
2596 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002597 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002598
2599 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002600 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002601
2602 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002603}
2604
Bram Moolenaar66b51422019-08-18 21:44:12 +02002605/*
2606 * Expand a list of names.
2607 *
2608 * Generic function for command line completion. It calls a function to
2609 * obtain strings, one by one. The strings are matched against a regexp
2610 * program. Matching strings are copied into an array, which is returned.
2611 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002612 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2613 * is used.
2614 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002615 * Returns OK when no problems encountered, FAIL for error (out of memory).
2616 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002617 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002618ExpandGeneric(
2619 expand_T *xp,
2620 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002621 char_u ***matches,
2622 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002623 char_u *((*func)(expand_T *, int)),
2624 // returns a string from the list
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002625 int escaped,
2626 char_u *fuzzystr)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002627{
2628 int i;
2629 int count = 0;
2630 int round;
2631 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002632 fuzmatch_str_T *fuzmatch = NULL;
2633 int score = 0;
2634 int fuzzy = (fuzzystr != NULL);
2635 int funcsort = FALSE;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002636 int match;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002637
2638 // do this loop twice:
2639 // round == 0: count the number of matching names
2640 // round == 1: copy the matching names into allocated memory
2641 for (round = 0; round <= 1; ++round)
2642 {
2643 for (i = 0; ; ++i)
2644 {
2645 str = (*func)(xp, i);
2646 if (str == NULL) // end of list
2647 break;
2648 if (*str == NUL) // skip empty strings
2649 continue;
2650
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002651 if (!fuzzy)
2652 match = vim_regexec(regmatch, str, (colnr_T)0);
2653 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002654 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002655 score = fuzzy_match_str(str, fuzzystr);
2656 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002657 }
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002658
2659 if (!match)
2660 continue;
2661
2662 if (round)
2663 {
2664 if (escaped)
2665 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2666 else
2667 str = vim_strsave(str);
2668 if (str == NULL)
2669 {
2670 if (fuzzy)
2671 fuzmatch_str_free(fuzmatch, count);
2672 else if (count > 0)
2673 FreeWild(count, *matches);
2674 *numMatches = 0;
2675 *matches = NULL;
2676 return FAIL;
2677 }
2678 if (fuzzy)
2679 {
2680 fuzmatch[count].idx = count;
2681 fuzmatch[count].str = str;
2682 fuzmatch[count].score = score;
2683 }
2684 else
2685 (*matches)[count] = str;
2686# ifdef FEAT_MENU
2687 if (func == get_menu_names && str != NULL)
2688 {
2689 // test for separator added by get_menu_names()
2690 str += STRLEN(str) - 1;
2691 if (*str == '\001')
2692 *str = '.';
2693 }
2694# endif
2695 }
2696 ++count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002697 }
2698 if (round == 0)
2699 {
2700 if (count == 0)
2701 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002702 if (fuzzy)
2703 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2704 else
2705 *matches = ALLOC_MULT(char_u *, count);
2706 if ((fuzzy && (fuzmatch == NULL)) || (*matches == NULL))
Bram Moolenaar66b51422019-08-18 21:44:12 +02002707 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002708 *numMatches = 0;
2709 *matches = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002710 return FAIL;
2711 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002712 *numMatches = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002713 count = 0;
2714 }
2715 }
2716
2717 // Sort the results. Keep menu's in the specified order.
2718 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2719 {
2720 if (xp->xp_context == EXPAND_EXPRESSION
2721 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002722 || xp->xp_context == EXPAND_USER_FUNC
2723 || xp->xp_context == EXPAND_DISASSEMBLE)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002724 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002725 // <SNR> functions should be sorted to the end.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002726 funcsort = TRUE;
2727 if (!fuzzy)
2728 qsort((void *)*matches, (size_t)*numMatches, sizeof(char_u *),
Bram Moolenaar66b51422019-08-18 21:44:12 +02002729 sort_func_compare);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002730 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002731 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002732 {
2733 if (!fuzzy)
2734 sort_strings(*matches, *numMatches);
2735 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002736 }
2737
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002738#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002739 // Reset the variables used for special highlight names expansion, so that
2740 // they don't show up when getting normal highlight names by ID.
2741 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002742#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002743
2744 if (fuzzy && fuzzymatches_to_strmatches(fuzmatch, matches, count,
2745 funcsort) == FAIL)
2746 return FAIL;
2747
Bram Moolenaar66b51422019-08-18 21:44:12 +02002748 return OK;
2749}
2750
2751/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002752 * Expand shell command matches in one directory of $PATH.
2753 */
2754 static void
2755expand_shellcmd_onedir(
2756 char_u *buf,
2757 char_u *s,
2758 size_t l,
2759 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002760 char_u ***matches,
2761 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002762 int flags,
2763 hashtab_T *ht,
2764 garray_T *gap)
2765{
2766 int ret;
2767 int i;
2768 hash_T hash;
2769 hashitem_T *hi;
2770
2771 vim_strncpy(buf, s, l);
2772 add_pathsep(buf);
2773 l = STRLEN(buf);
2774 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2775
2776 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002777 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002778 if (ret == OK)
2779 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002780 if (ga_grow(gap, *numMatches) == FAIL)
2781 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002782 else
2783 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002784 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002785 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002786 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002787
2788 if (STRLEN(name) > l)
2789 {
2790 // Check if this name was already found.
2791 hash = hash_hash(name + l);
2792 hi = hash_lookup(ht, name + l, hash);
2793 if (HASHITEM_EMPTY(hi))
2794 {
2795 // Remove the path that was prepended.
2796 STRMOVE(name, name + l);
2797 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2798 hash_add_item(ht, hi, name, hash);
2799 name = NULL;
2800 }
2801 }
2802 vim_free(name);
2803 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002804 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002805 }
2806 }
2807}
2808
2809/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002810 * Complete a shell command.
2811 * Returns FAIL or OK;
2812 */
2813 static int
2814expand_shellcmd(
2815 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002816 char_u ***matches, // return: array with matches
2817 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02002818 int flagsarg) // EW_ flags
2819{
2820 char_u *pat;
2821 int i;
2822 char_u *path = NULL;
2823 int mustfree = FALSE;
2824 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002825 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002826 size_t l;
2827 char_u *s, *e;
2828 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002829 int did_curdir = FALSE;
2830 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002831
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002832 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002833 if (buf == NULL)
2834 return FAIL;
2835
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002836 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002837 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002838 if (pat == NULL)
2839 {
2840 vim_free(buf);
2841 return FAIL;
2842 }
2843
Bram Moolenaar66b51422019-08-18 21:44:12 +02002844 for (i = 0; pat[i]; ++i)
2845 if (pat[i] == '\\' && pat[i + 1] == ' ')
2846 STRMOVE(pat + i, pat + i + 1);
2847
2848 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2849
2850 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2851 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2852 path = (char_u *)".";
2853 else
2854 {
2855 // For an absolute name we don't use $PATH.
2856 if (!mch_isFullName(pat))
2857 path = vim_getenv((char_u *)"PATH", &mustfree);
2858 if (path == NULL)
2859 path = (char_u *)"";
2860 }
2861
2862 // Go over all directories in $PATH. Expand matches in that directory and
2863 // collect them in "ga". When "." is not in $PATH also expand for the
2864 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002865 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002866 hash_init(&found_ht);
2867 for (s = path; ; s = e)
2868 {
2869# if defined(MSWIN)
2870 e = vim_strchr(s, ';');
2871# else
2872 e = vim_strchr(s, ':');
2873# endif
2874 if (e == NULL)
2875 e = s + STRLEN(s);
2876
2877 if (*s == NUL)
2878 {
2879 if (did_curdir)
2880 break;
2881 // Find directories in the current directory, path is empty.
2882 did_curdir = TRUE;
2883 flags |= EW_DIR;
2884 }
2885 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2886 {
2887 did_curdir = TRUE;
2888 flags |= EW_DIR;
2889 }
2890 else
2891 // Do not match directories inside a $PATH item.
2892 flags &= ~EW_DIR;
2893
2894 l = e - s;
2895 if (l > MAXPATHL - 5)
2896 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002897
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002898 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002899 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002900
Bram Moolenaar66b51422019-08-18 21:44:12 +02002901 if (*e != NUL)
2902 ++e;
2903 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002904 *matches = ga.ga_data;
2905 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002906
2907 vim_free(buf);
2908 vim_free(pat);
2909 if (mustfree)
2910 vim_free(path);
2911 hash_clear(&found_ht);
2912 return OK;
2913}
2914
2915# if defined(FEAT_EVAL)
2916/*
2917 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002918 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002919 */
2920 static void *
2921call_user_expand_func(
2922 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002923 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002924{
2925 cmdline_info_T *ccline = get_cmdline_info();
2926 int keep = 0;
2927 typval_T args[4];
2928 sctx_T save_current_sctx = current_sctx;
2929 char_u *pat = NULL;
2930 void *ret;
2931
2932 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2933 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002934
2935 if (ccline->cmdbuff != NULL)
2936 {
2937 keep = ccline->cmdbuff[ccline->cmdlen];
2938 ccline->cmdbuff[ccline->cmdlen] = 0;
2939 }
2940
2941 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2942
2943 args[0].v_type = VAR_STRING;
2944 args[0].vval.v_string = pat;
2945 args[1].v_type = VAR_STRING;
2946 args[1].vval.v_string = xp->xp_line;
2947 args[2].v_type = VAR_NUMBER;
2948 args[2].vval.v_number = xp->xp_col;
2949 args[3].v_type = VAR_UNKNOWN;
2950
2951 current_sctx = xp->xp_script_ctx;
2952
2953 ret = user_expand_func(xp->xp_arg, 3, args);
2954
2955 current_sctx = save_current_sctx;
2956 if (ccline->cmdbuff != NULL)
2957 ccline->cmdbuff[ccline->cmdlen] = keep;
2958
2959 vim_free(pat);
2960 return ret;
2961}
2962
2963/*
2964 * Expand names with a function defined by the user.
2965 */
2966 static int
2967ExpandUserDefined(
2968 expand_T *xp,
2969 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002970 char_u ***matches,
2971 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002972{
2973 char_u *retstr;
2974 char_u *s;
2975 char_u *e;
2976 int keep;
2977 garray_T ga;
2978 int skip;
2979
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002980 *matches = NULL;
2981 *numMatches = 0;
2982 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002983 if (retstr == NULL)
2984 return FAIL;
2985
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002986 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002987 for (s = retstr; *s != NUL; s = e)
2988 {
2989 e = vim_strchr(s, '\n');
2990 if (e == NULL)
2991 e = s + STRLEN(s);
2992 keep = *e;
2993 *e = NUL;
2994
2995 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2996 *e = keep;
2997
2998 if (!skip)
2999 {
3000 if (ga_grow(&ga, 1) == FAIL)
3001 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02003002 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003003 ++ga.ga_len;
3004 }
3005
3006 if (*e != NUL)
3007 ++e;
3008 }
3009 vim_free(retstr);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003010 *matches = ga.ga_data;
3011 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003012 return OK;
3013}
3014
3015/*
3016 * Expand names with a list returned by a function defined by the user.
3017 */
3018 static int
3019ExpandUserList(
3020 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003021 char_u ***matches,
3022 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003023{
3024 list_T *retlist;
3025 listitem_T *li;
3026 garray_T ga;
3027
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003028 *matches = NULL;
3029 *numMatches = 0;
3030 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003031 if (retlist == NULL)
3032 return FAIL;
3033
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003034 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003035 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003036 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003037 {
3038 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3039 continue; // Skip non-string items and empty strings
3040
3041 if (ga_grow(&ga, 1) == FAIL)
3042 break;
3043
3044 ((char_u **)ga.ga_data)[ga.ga_len] =
3045 vim_strsave(li->li_tv.vval.v_string);
3046 ++ga.ga_len;
3047 }
3048 list_unref(retlist);
3049
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003050 *matches = ga.ga_data;
3051 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003052 return OK;
3053}
3054# endif
3055
3056/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003057 * Expand "file" for all comma-separated directories in "path".
3058 * Adds the matches to "ga". Caller must init "ga".
3059 */
3060 void
3061globpath(
3062 char_u *path,
3063 char_u *file,
3064 garray_T *ga,
3065 int expand_options)
3066{
3067 expand_T xpc;
3068 char_u *buf;
3069 int i;
3070 int num_p;
3071 char_u **p;
3072
3073 buf = alloc(MAXPATHL);
3074 if (buf == NULL)
3075 return;
3076
3077 ExpandInit(&xpc);
3078 xpc.xp_context = EXPAND_FILES;
3079
3080 // Loop over all entries in {path}.
3081 while (*path != NUL)
3082 {
3083 // Copy one item of the path to buf[] and concatenate the file name.
3084 copy_option_part(&path, buf, MAXPATHL, ",");
3085 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3086 {
3087# if defined(MSWIN)
3088 // Using the platform's path separator (\) makes vim incorrectly
3089 // treat it as an escape character, use '/' instead.
3090 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3091 STRCAT(buf, "/");
3092# else
3093 add_pathsep(buf);
3094# endif
3095 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003096 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003097 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3098 {
3099 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3100
3101 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003102 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003103 for (i = 0; i < num_p; ++i)
3104 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003105 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003106 ++ga->ga_len;
3107 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003108 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003109 }
3110 }
3111 }
3112
3113 vim_free(buf);
3114}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115
Bram Moolenaareadee482020-09-04 15:37:31 +02003116#ifdef FEAT_WILDMENU
3117
3118/*
3119 * Translate some keys pressed when 'wildmenu' is used.
3120 */
3121 int
3122wildmenu_translate_key(
3123 cmdline_info_T *cclp,
3124 int key,
3125 expand_T *xp,
3126 int did_wild_list)
3127{
3128 int c = key;
3129
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003130#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003131 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003132 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003133 // When the popup menu is used for cmdline completion:
3134 // Up : go to the previous item in the menu
3135 // Down : go to the next item in the menu
3136 // Left : go to the parent directory
3137 // Right: list the files in the selected directory
3138 switch (c)
3139 {
3140 case K_UP: c = K_LEFT; break;
3141 case K_DOWN: c = K_RIGHT; break;
3142 case K_LEFT: c = K_UP; break;
3143 case K_RIGHT: c = K_DOWN; break;
3144 default: break;
3145 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003146 }
3147#endif
3148
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003149 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003150 {
3151 if (c == K_LEFT)
3152 c = Ctrl_P;
3153 else if (c == K_RIGHT)
3154 c = Ctrl_N;
3155 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003156
Bram Moolenaareadee482020-09-04 15:37:31 +02003157 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003158 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003159 && cclp->cmdpos > 1
3160 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3161 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3162 && (c == '\n' || c == '\r' || c == K_KENTER))
3163 c = K_DOWN;
3164
3165 return c;
3166}
3167
3168/*
3169 * Delete characters on the command line, from "from" to the current
3170 * position.
3171 */
3172 static void
3173cmdline_del(cmdline_info_T *cclp, int from)
3174{
3175 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3176 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3177 cclp->cmdlen -= cclp->cmdpos - from;
3178 cclp->cmdpos = from;
3179}
3180
3181/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003182 * Handle a key pressed when the wild menu for the menu names
3183 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003184 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003185 static int
3186wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003187{
Bram Moolenaareadee482020-09-04 15:37:31 +02003188 int i;
3189 int j;
3190
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003191 // Hitting <Down> after "emenu Name.": complete submenu
3192 if (key == K_DOWN && cclp->cmdpos > 0
3193 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003194 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003195 key = p_wc;
3196 KeyTyped = TRUE; // in case the key was mapped
3197 }
3198 else if (key == K_UP)
3199 {
3200 // Hitting <Up>: Remove one submenu name in front of the
3201 // cursor
3202 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003203
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003204 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3205 i = 0;
3206 while (--j > 0)
3207 {
3208 // check for start of menu name
3209 if (cclp->cmdbuff[j] == ' '
3210 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003211 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003212 i = j + 1;
3213 break;
3214 }
3215 // check for start of submenu name
3216 if (cclp->cmdbuff[j] == '.'
3217 && cclp->cmdbuff[j - 1] != '\\')
3218 {
3219 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003220 {
3221 i = j + 1;
3222 break;
3223 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003224 else
3225 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003226 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003227 }
3228 if (i > 0)
3229 cmdline_del(cclp, i);
3230 key = p_wc;
3231 KeyTyped = TRUE; // in case the key was mapped
3232 xp->xp_context = EXPAND_NOTHING;
3233 }
3234
3235 return key;
3236}
3237
3238/*
3239 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3240 * directory names (EXPAND_DIRECTORIES) or shell command names
3241 * (EXPAND_SHELLCMD) is displayed.
3242 */
3243 static int
3244wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3245{
3246 int i;
3247 int j;
3248 char_u upseg[5];
3249
3250 upseg[0] = PATHSEP;
3251 upseg[1] = '.';
3252 upseg[2] = '.';
3253 upseg[3] = PATHSEP;
3254 upseg[4] = NUL;
3255
3256 if (key == K_DOWN
3257 && cclp->cmdpos > 0
3258 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3259 && (cclp->cmdpos < 3
3260 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3261 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3262 {
3263 // go down a directory
3264 key = p_wc;
3265 KeyTyped = TRUE; // in case the key was mapped
3266 }
3267 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3268 {
3269 // If in a direct ancestor, strip off one ../ to go down
3270 int found = FALSE;
3271
3272 j = cclp->cmdpos;
3273 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3274 while (--j > i)
3275 {
3276 if (has_mbyte)
3277 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3278 if (vim_ispathsep(cclp->cmdbuff[j]))
3279 {
3280 found = TRUE;
3281 break;
3282 }
3283 }
3284 if (found
3285 && cclp->cmdbuff[j - 1] == '.'
3286 && cclp->cmdbuff[j - 2] == '.'
3287 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3288 {
3289 cmdline_del(cclp, j - 2);
3290 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003291 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003292 }
3293 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003294 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003295 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003296 // go up a directory
3297 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003298
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003299 j = cclp->cmdpos - 1;
3300 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3301 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003302 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003303 if (has_mbyte)
3304 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3305 if (vim_ispathsep(cclp->cmdbuff[j])
3306# ifdef BACKSLASH_IN_FILENAME
3307 && vim_strchr((char_u *)" *?[{`$%#",
3308 cclp->cmdbuff[j + 1]) == NULL
3309# endif
3310 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003311 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003312 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003313 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003314 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003315 break;
3316 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003317 else
3318 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003319 }
3320 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003321
3322 if (!found)
3323 j = i;
3324 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3325 j += 4;
3326 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3327 && j == i)
3328 j += 3;
3329 else
3330 j = 0;
3331 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003332 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003333 // TODO this is only for DOS/UNIX systems - need to put in
3334 // machine-specific stuff here and in upseg init
3335 cmdline_del(cclp, j);
3336 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003337 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003338 else if (cclp->cmdpos > i)
3339 cmdline_del(cclp, i);
3340
3341 // Now complete in the new directory. Set KeyTyped in case the
3342 // Up key came from a mapping.
3343 key = p_wc;
3344 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003345 }
3346
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003347 return key;
3348}
3349
3350/*
3351 * Handle a key pressed when the wild menu is displayed
3352 */
3353 int
3354wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3355{
3356 if (xp->xp_context == EXPAND_MENUNAMES)
3357 return wildmenu_process_key_menunames(cclp, key, xp);
3358 else if ((xp->xp_context == EXPAND_FILES
3359 || xp->xp_context == EXPAND_DIRECTORIES
3360 || xp->xp_context == EXPAND_SHELLCMD))
3361 return wildmenu_process_key_filenames(cclp, key, xp);
3362
3363 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003364}
3365
3366/*
3367 * Free expanded names when finished walking through the matches
3368 */
3369 void
3370wildmenu_cleanup(cmdline_info_T *cclp)
3371{
3372 int skt = KeyTyped;
3373 int old_RedrawingDisabled = RedrawingDisabled;
3374
3375 if (!p_wmnu || wild_menu_showing == 0)
3376 return;
3377
3378 if (cclp->input_fn)
3379 RedrawingDisabled = 0;
3380
3381 if (wild_menu_showing == WM_SCROLLED)
3382 {
3383 // Entered command line, move it up
3384 cmdline_row--;
3385 redrawcmd();
3386 }
3387 else if (save_p_ls != -1)
3388 {
3389 // restore 'laststatus' and 'winminheight'
3390 p_ls = save_p_ls;
3391 p_wmh = save_p_wmh;
3392 last_status(FALSE);
3393 update_screen(VALID); // redraw the screen NOW
3394 redrawcmd();
3395 save_p_ls = -1;
3396 }
3397 else
3398 {
3399 win_redraw_last_status(topframe);
3400 redraw_statuslines();
3401 }
3402 KeyTyped = skt;
3403 wild_menu_showing = 0;
3404 if (cclp->input_fn)
3405 RedrawingDisabled = old_RedrawingDisabled;
3406}
3407#endif
3408
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003409#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003410/*
3411 * "getcompletion()" function
3412 */
3413 void
3414f_getcompletion(typval_T *argvars, typval_T *rettv)
3415{
3416 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003417 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003418 expand_T xpc;
3419 int filtered = FALSE;
3420 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003421 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003422
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003423 if (in_vim9script()
3424 && (check_for_string_arg(argvars, 0) == FAIL
3425 || check_for_string_arg(argvars, 1) == FAIL
3426 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3427 return;
3428
ii144785fe02021-11-21 12:13:56 +00003429 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003430 if (argvars[1].v_type != VAR_STRING)
3431 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003432 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003433 return;
3434 }
3435 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003436
3437 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003438 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003439
3440 if (p_wic)
3441 options |= WILD_ICASE;
3442
3443 // For filtered results, 'wildignore' is used
3444 if (!filtered)
3445 options |= WILD_KEEP_ALL;
3446
3447 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003448 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003449 {
ii144785fe02021-11-21 12:13:56 +00003450 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003451 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003452 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003453 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003454 else
3455 {
ii144785fe02021-11-21 12:13:56 +00003456 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003457 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3458
3459 xpc.xp_context = cmdcomplete_str_to_type(type);
3460 if (xpc.xp_context == EXPAND_NOTHING)
3461 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003462 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003463 return;
3464 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003465
3466# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003467 if (xpc.xp_context == EXPAND_MENUS)
3468 {
3469 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3470 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3471 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472# endif
3473# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003474 if (xpc.xp_context == EXPAND_CSCOPE)
3475 {
3476 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3477 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3478 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003479# endif
3480# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003481 if (xpc.xp_context == EXPAND_SIGN)
3482 {
3483 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3484 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3485 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003486# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003487 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003488
3489 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3490 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3491 {
3492 int i;
3493
3494 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3495
3496 for (i = 0; i < xpc.xp_numfiles; i++)
3497 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3498 }
3499 vim_free(pat);
3500 ExpandCleanup(&xpc);
3501}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003502#endif // FEAT_EVAL