blob: 84dc64342430c5b727608ce5a596922dab2255c8 [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
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_OLD_SETTING
60 && xp->xp_context != EXPAND_OWNSYNTAX
61 && xp->xp_context != EXPAND_PACKADD
62 && xp->xp_context != EXPAND_SHELLCMD
63 && xp->xp_context != EXPAND_TAGS
64 && xp->xp_context != EXPAND_TAGS_LISTFILES
65 && xp->xp_context != EXPAND_USER_DEFINED
66 && xp->xp_context != EXPAND_USER_LIST);
67}
68
69/*
70 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
71 * 'fuzzystr' is not empty.
72 */
73 int
74cmdline_fuzzy_complete(char_u *fuzzystr)
75{
76 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
77}
78
79/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000080 * sort function for the completion matches.
81 * <SNR> functions should be sorted to the end.
82 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020083 static int
84sort_func_compare(const void *s1, const void *s2)
85{
86 char_u *p1 = *(char_u **)s1;
87 char_u *p2 = *(char_u **)s2;
88
89 if (*p1 != '<' && *p2 == '<') return -1;
90 if (*p1 == '<' && *p2 != '<') return 1;
91 return STRCMP(p1, p2);
92}
Bram Moolenaar66b51422019-08-18 21:44:12 +020093
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000094/*
95 * Escape special characters in the cmdline completion matches.
96 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020097 static void
98ExpandEscape(
99 expand_T *xp,
100 char_u *str,
101 int numfiles,
102 char_u **files,
103 int options)
104{
105 int i;
106 char_u *p;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100107 int vse_what = xp->xp_context == EXPAND_BUFFERS
108 ? VSE_BUFFER : VSE_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200109
110 // May change home directory back to "~"
111 if (options & WILD_HOME_REPLACE)
112 tilde_replace(str, numfiles, files);
113
114 if (options & WILD_ESCAPE)
115 {
116 if (xp->xp_context == EXPAND_FILES
117 || xp->xp_context == EXPAND_FILES_IN_PATH
118 || xp->xp_context == EXPAND_SHELLCMD
119 || xp->xp_context == EXPAND_BUFFERS
120 || xp->xp_context == EXPAND_DIRECTORIES)
121 {
122 // Insert a backslash into a file name before a space, \, %, #
123 // and wildmatch characters, except '~'.
124 for (i = 0; i < numfiles; ++i)
125 {
126 // for ":set path=" we need to escape spaces twice
127 if (xp->xp_backslash == XP_BS_THREE)
128 {
129 p = vim_strsave_escaped(files[i], (char_u *)" ");
130 if (p != NULL)
131 {
132 vim_free(files[i]);
133 files[i] = p;
134#if defined(BACKSLASH_IN_FILENAME)
135 p = vim_strsave_escaped(files[i], (char_u *)" ");
136 if (p != NULL)
137 {
138 vim_free(files[i]);
139 files[i] = p;
140 }
141#endif
142 }
143 }
144#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100145 p = vim_strsave_fnameescape(files[i], vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200146#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100147 p = vim_strsave_fnameescape(files[i],
148 xp->xp_shell ? VSE_SHELL : vse_what);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200149#endif
150 if (p != NULL)
151 {
152 vim_free(files[i]);
153 files[i] = p;
154 }
155
156 // If 'str' starts with "\~", replace "~" at start of
157 // files[i] with "\~".
158 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
159 escape_fname(&files[i]);
160 }
161 xp->xp_backslash = XP_BS_NONE;
162
163 // If the first file starts with a '+' escape it. Otherwise it
164 // could be seen as "+cmd".
165 if (*files[0] == '+')
166 escape_fname(&files[0]);
167 }
168 else if (xp->xp_context == EXPAND_TAGS)
169 {
170 // Insert a backslash before characters in a tag name that
171 // would terminate the ":tag" command.
172 for (i = 0; i < numfiles; ++i)
173 {
174 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
175 if (p != NULL)
176 {
177 vim_free(files[i]);
178 files[i] = p;
179 }
180 }
181 }
182 }
183}
184
185/*
186 * Return FAIL if this is not an appropriate context in which to do
187 * completion of anything, return OK if it is (even if there are no matches).
188 * For the caller, this means that the character is just passed through like a
189 * normal character (instead of being expanded). This allows :s/^I^D etc.
190 */
191 int
192nextwild(
193 expand_T *xp,
194 int type,
195 int options, // extra options for ExpandOne()
196 int escape) // if TRUE, escape the returned matches
197{
198 cmdline_info_T *ccline = get_cmdline_info();
199 int i, j;
200 char_u *p1;
201 char_u *p2;
202 int difflen;
203 int v;
204
205 if (xp->xp_numfiles == -1)
206 {
207 set_expand_context(xp);
208 cmd_showtail = expand_showtail(xp);
209 }
210
211 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
212 {
213 beep_flush();
214 return OK; // Something illegal on command line
215 }
216 if (xp->xp_context == EXPAND_NOTHING)
217 {
218 // Caller can use the character as a normal char instead
219 return FAIL;
220 }
221
222 msg_puts("..."); // show that we are busy
223 out_flush();
224
225 i = (int)(xp->xp_pattern - ccline->cmdbuff);
226 xp->xp_pattern_len = ccline->cmdpos - i;
227
228 if (type == WILD_NEXT || type == WILD_PREV)
229 {
230 // Get next/previous match for a previous expanded pattern.
231 p2 = ExpandOne(xp, NULL, NULL, 0, type);
232 }
233 else
234 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000235 if (cmdline_fuzzy_completion_supported(xp))
236 // If fuzzy matching, don't modify the search string
237 p1 = vim_strsave(xp->xp_pattern);
238 else
239 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
240
Bram Moolenaar66b51422019-08-18 21:44:12 +0200241 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000242 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200243 p2 = NULL;
244 else
245 {
246 int use_options = options |
247 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
248 if (escape)
249 use_options |= WILD_ESCAPE;
250
251 if (p_wic)
252 use_options += WILD_ICASE;
253 p2 = ExpandOne(xp, p1,
254 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
255 use_options, type);
256 vim_free(p1);
257 // longest match: make sure it is not shorter, happens with :help
258 if (p2 != NULL && type == WILD_LONGEST)
259 {
260 for (j = 0; j < xp->xp_pattern_len; ++j)
261 if (ccline->cmdbuff[i + j] == '*'
262 || ccline->cmdbuff[i + j] == '?')
263 break;
264 if ((int)STRLEN(p2) < j)
265 VIM_CLEAR(p2);
266 }
267 }
268 }
269
270 if (p2 != NULL && !got_int)
271 {
272 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
273 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
274 {
275 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
276 xp->xp_pattern = ccline->cmdbuff + i;
277 }
278 else
279 v = OK;
280 if (v == OK)
281 {
282 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
283 &ccline->cmdbuff[ccline->cmdpos],
284 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
285 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
286 ccline->cmdlen += difflen;
287 ccline->cmdpos += difflen;
288 }
289 }
290 vim_free(p2);
291
292 redrawcmd();
293 cursorcmd();
294
295 // When expanding a ":map" command and no matches are found, assume that
296 // the key is supposed to be inserted literally
297 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
298 return FAIL;
299
300 if (xp->xp_numfiles <= 0 && p2 == NULL)
301 beep_flush();
302 else if (xp->xp_numfiles == 1)
303 // free expanded pattern
304 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
305
306 return OK;
307}
308
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000309#if defined(FEAT_WILDMENU) || defined(PROTO)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000310
311/*
312 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000313 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000314 */
315 static int
316cmdline_pum_create(
317 cmdline_info_T *ccline,
318 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000319 char_u **matches,
320 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000321 int showtail)
322{
323 int i;
324 int columns;
325
326 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000327 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000328 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000329 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000330 {
331 compl_match_array[i].pum_text = SHOW_FILE_TEXT(i);
332 compl_match_array[i].pum_info = NULL;
333 compl_match_array[i].pum_extra = NULL;
334 compl_match_array[i].pum_kind = NULL;
335 }
336
337 // Compute the popup menu starting column
338 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
339 columns = vim_strsize(xp->xp_pattern);
340 if (showtail)
341 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000342 columns += vim_strsize(sm_gettail(matches[0]));
343 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000344 }
345 if (columns >= compl_startcol)
346 compl_startcol = 0;
347 else
348 compl_startcol -= columns;
349
350 // no default selection
351 compl_selected = -1;
352
353 cmdline_pum_display();
354
355 return EXPAND_OK;
356}
357
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000358/*
359 * Display the cmdline completion matches in a popup menu
360 */
361void cmdline_pum_display(void)
362{
363 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
364}
365
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000366/*
367 * Returns TRUE if the cmdline completion popup menu is being displayed.
368 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000369int cmdline_pum_active(void)
370{
371 return p_wmnu && pum_visible() && compl_match_array != NULL;
372}
373
374/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000375 * Remove the cmdline completion popup menu (if present), free the list of
376 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000377 */
378void cmdline_pum_remove(void)
379{
380 pum_undisplay();
381 VIM_CLEAR(compl_match_array);
382 update_screen(0);
Bram Moolenaar414acd32022-02-10 21:09:45 +0000383 redrawcmd();
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000384}
385
386void cmdline_pum_cleanup(cmdline_info_T *cclp)
387{
388 cmdline_pum_remove();
389 wildmenu_cleanup(cclp);
390}
391
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000392/*
393 * Returns the starting column number to use for the cmdline completion popup
394 * menu.
395 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000396int cmdline_compl_startcol(void)
397{
398 return compl_startcol;
399}
400#endif
401
Bram Moolenaar66b51422019-08-18 21:44:12 +0200402/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000403 * Get the next or prev cmdline completion match. The index of the match is set
404 * in 'p_findex'
405 */
406 static char_u *
407get_next_or_prev_match(
408 int mode,
409 expand_T *xp,
410 int *p_findex,
411 char_u *orig_save)
412{
413 int findex = *p_findex;
414
415 if (xp->xp_numfiles <= 0)
416 return NULL;
417
418 if (mode == WILD_PREV)
419 {
420 if (findex == -1)
421 findex = xp->xp_numfiles;
422 --findex;
423 }
424 else // mode == WILD_NEXT
425 ++findex;
426
427 // When wrapping around, return the original string, set findex to
428 // -1.
429 if (findex < 0)
430 {
431 if (orig_save == NULL)
432 findex = xp->xp_numfiles - 1;
433 else
434 findex = -1;
435 }
436 if (findex >= xp->xp_numfiles)
437 {
438 if (orig_save == NULL)
439 findex = 0;
440 else
441 findex = -1;
442 }
443#ifdef FEAT_WILDMENU
444 if (compl_match_array)
445 {
446 compl_selected = findex;
447 cmdline_pum_display();
448 }
449 else if (p_wmnu)
450 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
451 findex, cmd_showtail);
452#endif
453 *p_findex = findex;
454
455 if (findex == -1)
456 return vim_strsave(orig_save);
457
458 return vim_strsave(xp->xp_files[findex]);
459}
460
461/*
462 * Start the command-line expansion and get the matches.
463 */
464 static char_u *
465ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
466{
467 int non_suf_match; // number without matching suffix
468 int i;
469 char_u *ss = NULL;
470
471 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000472 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000473 options) == FAIL)
474 {
475#ifdef FNAME_ILLEGAL
476 // Illegal file name has been silently skipped. But when there
477 // are wildcards, the real problem is that there was no match,
478 // causing the pattern to be added, which has illegal characters.
479 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
480 semsg(_(e_no_match_str_2), str);
481#endif
482 }
483 else if (xp->xp_numfiles == 0)
484 {
485 if (!(options & WILD_SILENT))
486 semsg(_(e_no_match_str_2), str);
487 }
488 else
489 {
490 // Escape the matches for use on the command line.
491 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
492
493 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000494 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000495 {
496 if (xp->xp_numfiles)
497 non_suf_match = xp->xp_numfiles;
498 else
499 non_suf_match = 1;
500 if ((xp->xp_context == EXPAND_FILES
501 || xp->xp_context == EXPAND_DIRECTORIES)
502 && xp->xp_numfiles > 1)
503 {
504 // More than one match; check suffix.
505 // The files will have been sorted on matching suffix in
506 // expand_wildcards, only need to check the first two.
507 non_suf_match = 0;
508 for (i = 0; i < 2; ++i)
509 if (match_suffix(xp->xp_files[i]))
510 ++non_suf_match;
511 }
512 if (non_suf_match != 1)
513 {
514 // Can we ever get here unless it's while expanding
515 // interactively? If not, we can get rid of this all
516 // together. Don't really want to wait for this message
517 // (and possibly have to hit return to continue!).
518 if (!(options & WILD_SILENT))
519 emsg(_(e_too_many_file_names));
520 else if (!(options & WILD_NO_BEEP))
521 beep_flush();
522 }
523 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
524 ss = vim_strsave(xp->xp_files[0]);
525 }
526 }
527
528 return ss;
529}
530
531/*
532 * Return the longest common part in the list of cmdline completion matches.
533 */
534 static char_u *
535find_longest_match(expand_T *xp, int options)
536{
537 long_u len;
538 int mb_len = 1;
539 int c0, ci;
540 int i;
541 char_u *ss;
542
543 for (len = 0; xp->xp_files[0][len]; len += mb_len)
544 {
545 if (has_mbyte)
546 {
547 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
548 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
549 }
550 else
551 c0 = xp->xp_files[0][len];
552 for (i = 1; i < xp->xp_numfiles; ++i)
553 {
554 if (has_mbyte)
555 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
556 else
557 ci = xp->xp_files[i][len];
558 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
559 || xp->xp_context == EXPAND_FILES
560 || xp->xp_context == EXPAND_SHELLCMD
561 || xp->xp_context == EXPAND_BUFFERS))
562 {
563 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
564 break;
565 }
566 else if (c0 != ci)
567 break;
568 }
569 if (i < xp->xp_numfiles)
570 {
571 if (!(options & WILD_NO_BEEP))
572 vim_beep(BO_WILD);
573 break;
574 }
575 }
576
577 ss = alloc(len + 1);
578 if (ss)
579 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
580
581 return ss;
582}
583
584/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200585 * Do wildcard expansion on the string 'str'.
586 * Chars that should not be expanded must be preceded with a backslash.
587 * Return a pointer to allocated memory containing the new string.
588 * Return NULL for failure.
589 *
590 * "orig" is the originally expanded string, copied to allocated memory. It
591 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
592 * WILD_PREV "orig" should be NULL.
593 *
594 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
595 * is WILD_EXPAND_FREE or WILD_ALL.
596 *
597 * mode = WILD_FREE: just free previously expanded matches
598 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
599 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
600 * mode = WILD_NEXT: use next match in multiple match, wrap to first
601 * mode = WILD_PREV: use previous match in multiple match, wrap to first
602 * mode = WILD_ALL: return all matches concatenated
603 * mode = WILD_LONGEST: return longest matched part
604 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000605 * mode = WILD_APPLY: apply the item selected in the cmdline completion
606 * popup menu and close the menu.
607 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
608 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200609 *
610 * options = WILD_LIST_NOTFOUND: list entries without a match
611 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
612 * options = WILD_USE_NL: Use '\n' for WILD_ALL
613 * options = WILD_NO_BEEP: Don't beep for multiple matches
614 * options = WILD_ADD_SLASH: add a slash after directory names
615 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
616 * options = WILD_SILENT: don't print warning messages
617 * options = WILD_ESCAPE: put backslash before special chars
618 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200619 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200620 *
621 * The variables xp->xp_context and xp->xp_backslash must have been set!
622 */
623 char_u *
624ExpandOne(
625 expand_T *xp,
626 char_u *str,
627 char_u *orig, // allocated copy of original of expanded string
628 int options,
629 int mode)
630{
631 char_u *ss = NULL;
632 static int findex;
633 static char_u *orig_save = NULL; // kept value of orig
634 int orig_saved = FALSE;
635 int i;
636 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200637
638 // first handle the case of using an old match
639 if (mode == WILD_NEXT || mode == WILD_PREV)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000640 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200641
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000642 if (mode == WILD_CANCEL)
643 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
644 else if (mode == WILD_APPLY)
645 ss = vim_strsave(findex == -1 ? (orig_save ?
646 orig_save : (char_u *)"") : xp->xp_files[findex]);
647
Bram Moolenaar66b51422019-08-18 21:44:12 +0200648 // free old names
649 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
650 {
651 FreeWild(xp->xp_numfiles, xp->xp_files);
652 xp->xp_numfiles = -1;
653 VIM_CLEAR(orig_save);
654 }
655 findex = 0;
656
657 if (mode == WILD_FREE) // only release file name
658 return NULL;
659
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000660 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200661 {
662 vim_free(orig_save);
663 orig_save = orig;
664 orig_saved = TRUE;
665
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000666 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200667 }
668
669 // Find longest common part
670 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
671 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000672 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200673 findex = -1; // next p_wc gets first one
674 }
675
676 // Concatenate all matching names
677 if (mode == WILD_ALL && xp->xp_numfiles > 0)
678 {
679 len = 0;
680 for (i = 0; i < xp->xp_numfiles; ++i)
681 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
682 ss = alloc(len);
683 if (ss != NULL)
684 {
685 *ss = NUL;
686 for (i = 0; i < xp->xp_numfiles; ++i)
687 {
688 STRCAT(ss, xp->xp_files[i]);
689 if (i != xp->xp_numfiles - 1)
690 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
691 }
692 }
693 }
694
695 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
696 ExpandCleanup(xp);
697
698 // Free "orig" if it wasn't stored in "orig_save".
699 if (!orig_saved)
700 vim_free(orig);
701
702 return ss;
703}
704
705/*
706 * Prepare an expand structure for use.
707 */
708 void
709ExpandInit(expand_T *xp)
710{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200711 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200712 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200713 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200714}
715
716/*
717 * Cleanup an expand structure after use.
718 */
719 void
720ExpandCleanup(expand_T *xp)
721{
722 if (xp->xp_numfiles >= 0)
723 {
724 FreeWild(xp->xp_numfiles, xp->xp_files);
725 xp->xp_numfiles = -1;
726 }
727}
728
729/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000730 * Display one line of completion matches. Multiple matches are displayed in
731 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000732 * matches - list of completion match names
733 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000734 * lines - number of output lines
735 * linenr - line number of matches to display
736 * maxlen - maximum number of characters in each line
737 * showtail - display only the tail of the full path of a file name
738 * dir_attr - highlight attribute to use for directory names
739 */
740 static void
741showmatches_oneline(
742 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000743 char_u **matches,
744 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000745 int lines,
746 int linenr,
747 int maxlen,
748 int showtail,
749 int dir_attr)
750{
751 int i, j;
752 int isdir;
753 int lastlen;
754 char_u *p;
755
756 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000757 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000758 {
759 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
760 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000761 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
762 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000763 msg_advance(maxlen + 1);
764 msg_puts((char *)p);
765 msg_advance(maxlen + 3);
766 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
767 break;
768 }
769 for (i = maxlen - lastlen; --i >= 0; )
770 msg_putchar(' ');
771 if (xp->xp_context == EXPAND_FILES
772 || xp->xp_context == EXPAND_SHELLCMD
773 || xp->xp_context == EXPAND_BUFFERS)
774 {
775 // highlight directories
776 if (xp->xp_numfiles != -1)
777 {
778 char_u *halved_slash;
779 char_u *exp_path;
780 char_u *path;
781
782 // Expansion was done before and special characters
783 // were escaped, need to halve backslashes. Also
784 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000785 exp_path = expand_env_save_opt(matches[j], TRUE);
786 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000787 halved_slash = backslash_halve_save(path);
788 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000789 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000790 vim_free(exp_path);
791 if (halved_slash != path)
792 vim_free(halved_slash);
793 }
794 else
795 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000796 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000797 if (showtail)
798 p = SHOW_FILE_TEXT(j);
799 else
800 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000801 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000802 TRUE);
803 p = NameBuff;
804 }
805 }
806 else
807 {
808 isdir = FALSE;
809 p = SHOW_FILE_TEXT(j);
810 }
811 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
812 }
813 if (msg_col > 0) // when not wrapped around
814 {
815 msg_clr_eos();
816 msg_putchar('\n');
817 }
818 out_flush(); // show one line at a time
819}
820
821/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200822 * Show all matches for completion on the command line.
823 * Returns EXPAND_NOTHING when the character that triggered expansion should
824 * be inserted like a normal character.
825 */
826 int
827showmatches(expand_T *xp, int wildmenu UNUSED)
828{
829 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000830 int numMatches;
831 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000832 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200833 int maxlen;
834 int lines;
835 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200836 int attr;
837 int showtail;
838
839 if (xp->xp_numfiles == -1)
840 {
841 set_expand_context(xp);
842 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000843 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200844 showtail = expand_showtail(xp);
845 if (i != EXPAND_OK)
846 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200847 }
848 else
849 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000850 numMatches = xp->xp_numfiles;
851 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200852 showtail = cmd_showtail;
853 }
854
855#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000856 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000857 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000858 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000859#endif
860
861#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200862 if (!wildmenu)
863 {
864#endif
865 msg_didany = FALSE; // lines_left will be set
866 msg_start(); // prepare for paging
867 msg_putchar('\n');
868 out_flush();
869 cmdline_row = msg_row;
870 msg_didany = FALSE; // lines_left will be set again
871 msg_start(); // prepare for paging
872#ifdef FEAT_WILDMENU
873 }
874#endif
875
876 if (got_int)
877 got_int = FALSE; // only int. the completion, not the cmd line
878#ifdef FEAT_WILDMENU
879 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000880 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200881#endif
882 else
883 {
884 // find the length of the longest file name
885 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000886 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200887 {
888 if (!showtail && (xp->xp_context == EXPAND_FILES
889 || xp->xp_context == EXPAND_SHELLCMD
890 || xp->xp_context == EXPAND_BUFFERS))
891 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000892 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200893 j = vim_strsize(NameBuff);
894 }
895 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000896 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200897 if (j > maxlen)
898 maxlen = j;
899 }
900
901 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000902 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200903 else
904 {
905 // compute the number of columns and lines for the listing
906 maxlen += 2; // two spaces between file names
907 columns = ((int)Columns + 2) / maxlen;
908 if (columns < 1)
909 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000910 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200911 }
912
913 attr = HL_ATTR(HLF_D); // find out highlighting for directories
914
915 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
916 {
917 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
918 msg_clr_eos();
919 msg_advance(maxlen - 3);
920 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
921 }
922
923 // list the files line by line
924 for (i = 0; i < lines; ++i)
925 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000926 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000927 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200928 if (got_int)
929 {
930 got_int = FALSE;
931 break;
932 }
933 }
934
935 // we redraw the command below the lines that we have just listed
936 // This is a bit tricky, but it saves a lot of screen updating.
937 cmdline_row = msg_row; // will put it back later
938 }
939
940 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000941 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200942
943 return EXPAND_OK;
944}
945
946/*
947 * Private gettail for showmatches() (and win_redr_status_matches()):
948 * Find tail of file name path, but ignore trailing "/".
949 */
950 char_u *
951sm_gettail(char_u *s)
952{
953 char_u *p;
954 char_u *t = s;
955 int had_sep = FALSE;
956
957 for (p = s; *p != NUL; )
958 {
959 if (vim_ispathsep(*p)
960#ifdef BACKSLASH_IN_FILENAME
961 && !rem_backslash(p)
962#endif
963 )
964 had_sep = TRUE;
965 else if (had_sep)
966 {
967 t = p;
968 had_sep = FALSE;
969 }
970 MB_PTR_ADV(p);
971 }
972 return t;
973}
974
975/*
976 * Return TRUE if we only need to show the tail of completion matches.
977 * When not completing file names or there is a wildcard in the path FALSE is
978 * returned.
979 */
980 static int
981expand_showtail(expand_T *xp)
982{
983 char_u *s;
984 char_u *end;
985
986 // When not completing file names a "/" may mean something different.
987 if (xp->xp_context != EXPAND_FILES
988 && xp->xp_context != EXPAND_SHELLCMD
989 && xp->xp_context != EXPAND_DIRECTORIES)
990 return FALSE;
991
992 end = gettail(xp->xp_pattern);
993 if (end == xp->xp_pattern) // there is no path separator
994 return FALSE;
995
996 for (s = xp->xp_pattern; s < end; s++)
997 {
998 // Skip escaped wildcards. Only when the backslash is not a path
999 // separator, on DOS the '*' "path\*\file" must not be skipped.
1000 if (rem_backslash(s))
1001 ++s;
1002 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1003 return FALSE;
1004 }
1005 return TRUE;
1006}
1007
1008/*
1009 * Prepare a string for expansion.
1010 * When expanding file names: The string will be used with expand_wildcards().
1011 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1012 * When expanding other names: The string will be used with regcomp(). Copy
1013 * the name into allocated memory and prepend "^".
1014 */
1015 char_u *
1016addstar(
1017 char_u *fname,
1018 int len,
1019 int context) // EXPAND_FILES etc.
1020{
1021 char_u *retval;
1022 int i, j;
1023 int new_len;
1024 char_u *tail;
1025 int ends_in_star;
1026
1027 if (context != EXPAND_FILES
1028 && context != EXPAND_FILES_IN_PATH
1029 && context != EXPAND_SHELLCMD
1030 && context != EXPAND_DIRECTORIES)
1031 {
1032 // Matching will be done internally (on something other than files).
1033 // So we convert the file-matching-type wildcards into our kind for
1034 // use with vim_regcomp(). First work out how long it will be:
1035
1036 // For help tags the translation is done in find_help_tags().
1037 // For a tag pattern starting with "/" no translation is needed.
1038 if (context == EXPAND_HELP
1039 || context == EXPAND_COLORS
1040 || context == EXPAND_COMPILER
1041 || context == EXPAND_OWNSYNTAX
1042 || context == EXPAND_FILETYPE
1043 || context == EXPAND_PACKADD
1044 || ((context == EXPAND_TAGS_LISTFILES
1045 || context == EXPAND_TAGS)
1046 && fname[0] == '/'))
1047 retval = vim_strnsave(fname, len);
1048 else
1049 {
1050 new_len = len + 2; // +2 for '^' at start, NUL at end
1051 for (i = 0; i < len; i++)
1052 {
1053 if (fname[i] == '*' || fname[i] == '~')
1054 new_len++; // '*' needs to be replaced by ".*"
1055 // '~' needs to be replaced by "\~"
1056
1057 // Buffer names are like file names. "." should be literal
1058 if (context == EXPAND_BUFFERS && fname[i] == '.')
1059 new_len++; // "." becomes "\."
1060
1061 // Custom expansion takes care of special things, match
1062 // backslashes literally (perhaps also for other types?)
1063 if ((context == EXPAND_USER_DEFINED
1064 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1065 new_len++; // '\' becomes "\\"
1066 }
1067 retval = alloc(new_len);
1068 if (retval != NULL)
1069 {
1070 retval[0] = '^';
1071 j = 1;
1072 for (i = 0; i < len; i++, j++)
1073 {
1074 // Skip backslash. But why? At least keep it for custom
1075 // expansion.
1076 if (context != EXPAND_USER_DEFINED
1077 && context != EXPAND_USER_LIST
1078 && fname[i] == '\\'
1079 && ++i == len)
1080 break;
1081
1082 switch (fname[i])
1083 {
1084 case '*': retval[j++] = '.';
1085 break;
1086 case '~': retval[j++] = '\\';
1087 break;
1088 case '?': retval[j] = '.';
1089 continue;
1090 case '.': if (context == EXPAND_BUFFERS)
1091 retval[j++] = '\\';
1092 break;
1093 case '\\': if (context == EXPAND_USER_DEFINED
1094 || context == EXPAND_USER_LIST)
1095 retval[j++] = '\\';
1096 break;
1097 }
1098 retval[j] = fname[i];
1099 }
1100 retval[j] = NUL;
1101 }
1102 }
1103 }
1104 else
1105 {
1106 retval = alloc(len + 4);
1107 if (retval != NULL)
1108 {
1109 vim_strncpy(retval, fname, len);
1110
1111 // Don't add a star to *, ~, ~user, $var or `cmd`.
1112 // * would become **, which walks the whole tree.
1113 // ~ would be at the start of the file name, but not the tail.
1114 // $ could be anywhere in the tail.
1115 // ` could be anywhere in the file name.
1116 // When the name ends in '$' don't add a star, remove the '$'.
1117 tail = gettail(retval);
1118 ends_in_star = (len > 0 && retval[len - 1] == '*');
1119#ifndef BACKSLASH_IN_FILENAME
1120 for (i = len - 2; i >= 0; --i)
1121 {
1122 if (retval[i] != '\\')
1123 break;
1124 ends_in_star = !ends_in_star;
1125 }
1126#endif
1127 if ((*retval != '~' || tail != retval)
1128 && !ends_in_star
1129 && vim_strchr(tail, '$') == NULL
1130 && vim_strchr(retval, '`') == NULL)
1131 retval[len++] = '*';
1132 else if (len > 0 && retval[len - 1] == '$')
1133 --len;
1134 retval[len] = NUL;
1135 }
1136 }
1137 return retval;
1138}
1139
1140/*
1141 * Must parse the command line so far to work out what context we are in.
1142 * Completion can then be done based on that context.
1143 * This routine sets the variables:
1144 * xp->xp_pattern The start of the pattern to be expanded within
1145 * the command line (ends at the cursor).
1146 * xp->xp_context The type of thing to expand. Will be one of:
1147 *
1148 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1149 * the command line, like an unknown command. Caller
1150 * should beep.
1151 * EXPAND_NOTHING Unrecognised context for completion, use char like
1152 * a normal char, rather than for completion. eg
1153 * :s/^I/
1154 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1155 * it.
1156 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1157 * EXPAND_FILES After command with EX_XFILE set, or after setting
1158 * with P_EXPAND set. eg :e ^I, :w>>^I
1159 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1160 * when we know only directories are of interest. eg
1161 * :set dir=^I
1162 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1163 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1164 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1165 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1166 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1167 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1168 * EXPAND_EVENTS Complete event names
1169 * EXPAND_SYNTAX Complete :syntax command arguments
1170 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1171 * EXPAND_AUGROUP Complete autocommand group names
1172 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1173 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1174 * eg :unmap a^I , :cunab x^I
1175 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1176 * eg :call sub^I
1177 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1178 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1179 * names in expressions, eg :while s^I
1180 * EXPAND_ENV_VARS Complete environment variable names
1181 * EXPAND_USER Complete user names
1182 */
1183 static void
1184set_expand_context(expand_T *xp)
1185{
1186 cmdline_info_T *ccline = get_cmdline_info();
1187
1188 // only expansion for ':', '>' and '=' command-lines
1189 if (ccline->cmdfirstc != ':'
1190#ifdef FEAT_EVAL
1191 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1192 && !ccline->input_fn
1193#endif
1194 )
1195 {
1196 xp->xp_context = EXPAND_NOTHING;
1197 return;
1198 }
1199 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1200}
1201
Bram Moolenaard0190392019-08-23 21:17:35 +02001202/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001203 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1204 * For user defined commands, the completion context is set in 'xp' and the
1205 * completion flags in 'complp'.
1206 *
1207 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001208 */
1209 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001210set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001211{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001212 char_u *p = NULL;
1213 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001214 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001215
1216 // Isolate the command and search for it in the command table.
1217 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001218 // - the 'k' command can directly be followed by any character, but do
1219 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1220 // find matches anywhere in the command name, do this only for command
1221 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001222 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001223 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001224 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001225 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001226 p = cmd + 1;
1227 }
1228 else
1229 {
1230 p = cmd;
1231 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1232 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001233 // A user command may contain digits.
1234 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1235 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001236 while (ASCII_ISALNUM(*p) || *p == '*')
1237 ++p;
1238 // for python 3.x: ":py3*" commands completion
1239 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1240 {
1241 ++p;
1242 while (ASCII_ISALPHA(*p) || *p == '*')
1243 ++p;
1244 }
1245 // check for non-alpha command
1246 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1247 ++p;
1248 len = (int)(p - cmd);
1249
1250 if (len == 0)
1251 {
1252 xp->xp_context = EXPAND_UNSUCCESSFUL;
1253 return NULL;
1254 }
1255
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001256 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001257
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001258 // User defined commands support alphanumeric characters.
1259 // Also when doing fuzzy expansion, support alphanumeric characters.
1260 if ((cmd[0] >= 'A' && cmd[0] <= 'Z') || (fuzzy && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001261 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1262 ++p;
1263 }
1264
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001265 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001266 // character, complete the command name.
1267 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1268 return NULL;
1269
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001270 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001271 {
1272 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1273 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001274 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001275 p = cmd + 1;
1276 }
1277 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1278 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001279 eap->cmd = cmd;
1280 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001281 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001282 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001283 }
1284 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001285 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001286 {
1287 // Not still touching the command and it was an illegal one
1288 xp->xp_context = EXPAND_UNSUCCESSFUL;
1289 return NULL;
1290 }
1291
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001292 return p;
1293}
Bram Moolenaard0190392019-08-23 21:17:35 +02001294
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001295/*
1296 * Set the completion context for a command argument with wild card characters.
1297 */
1298 static void
1299set_context_for_wildcard_arg(
1300 exarg_T *eap,
1301 char_u *arg,
1302 int usefilter,
1303 expand_T *xp,
1304 int *complp)
1305{
1306 char_u *p;
1307 int c;
1308 int in_quote = FALSE;
1309 char_u *bow = NULL; // Beginning of word
1310 int len = 0;
1311
1312 // Allow spaces within back-quotes to count as part of the argument
1313 // being expanded.
1314 xp->xp_pattern = skipwhite(arg);
1315 p = xp->xp_pattern;
1316 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001317 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001318 if (has_mbyte)
1319 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001320 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001321 c = *p;
1322 if (c == '\\' && p[1] != NUL)
1323 ++p;
1324 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001325 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001326 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001327 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001328 xp->xp_pattern = p;
1329 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001330 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001331 in_quote = !in_quote;
1332 }
1333 // An argument can contain just about everything, except
1334 // characters that end the command and white space.
1335 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001336#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001337 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001338#endif
1339 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001340 {
1341 len = 0; // avoid getting stuck when space is in 'isfname'
1342 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001343 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001344 if (has_mbyte)
1345 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001346 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001347 c = *p;
1348 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001349 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001350 if (has_mbyte)
1351 len = (*mb_ptr2len)(p);
1352 else
1353 len = 1;
1354 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001355 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001356 if (in_quote)
1357 bow = p;
1358 else
1359 xp->xp_pattern = p;
1360 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001361 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001362 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001363 }
1364
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001365 // If we are still inside the quotes, and we passed a space, just
1366 // expand from there.
1367 if (bow != NULL && in_quote)
1368 xp->xp_pattern = bow;
1369 xp->xp_context = EXPAND_FILES;
1370
1371 // For a shell command more chars need to be escaped.
1372 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1373 {
1374#ifndef BACKSLASH_IN_FILENAME
1375 xp->xp_shell = TRUE;
1376#endif
1377 // When still after the command name expand executables.
1378 if (xp->xp_pattern == skipwhite(arg))
1379 xp->xp_context = EXPAND_SHELLCMD;
1380 }
1381
1382 // Check for environment variable.
1383 if (*xp->xp_pattern == '$')
1384 {
1385 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1386 if (!vim_isIDc(*p))
1387 break;
1388 if (*p == NUL)
1389 {
1390 xp->xp_context = EXPAND_ENV_VARS;
1391 ++xp->xp_pattern;
1392 // Avoid that the assignment uses EXPAND_FILES again.
1393 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1394 *complp = EXPAND_ENV_VARS;
1395 }
1396 }
1397 // Check for user names.
1398 if (*xp->xp_pattern == '~')
1399 {
1400 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1401 ;
1402 // Complete ~user only if it partially matches a user name.
1403 // A full match ~user<Tab> will be replaced by user's home
1404 // directory i.e. something like ~user<Tab> -> /home/user/
1405 if (*p == NUL && p > xp->xp_pattern + 1
1406 && match_user(xp->xp_pattern + 1) >= 1)
1407 {
1408 xp->xp_context = EXPAND_USER;
1409 ++xp->xp_pattern;
1410 }
1411 }
1412}
1413
1414/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001415 * Set the completion context for the :filter command. Returns a pointer to the
1416 * next command after the :filter command.
1417 */
1418 static char_u *
1419set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1420{
1421 if (*arg != NUL)
1422 arg = skip_vimgrep_pat(arg, NULL, NULL);
1423 if (arg == NULL || *arg == NUL)
1424 {
1425 xp->xp_context = EXPAND_NOTHING;
1426 return NULL;
1427 }
1428 return skipwhite(arg);
1429}
1430
1431#ifdef FEAT_SEARCH_EXTRA
1432/*
1433 * Set the completion context for the :match command. Returns a pointer to the
1434 * next command after the :match command.
1435 */
1436 static char_u *
1437set_context_in_match_cmd(expand_T *xp, char_u *arg)
1438{
1439 if (*arg == NUL || !ends_excmd(*arg))
1440 {
1441 // also complete "None"
1442 set_context_in_echohl_cmd(xp, arg);
1443 arg = skipwhite(skiptowhite(arg));
1444 if (*arg != NUL)
1445 {
1446 xp->xp_context = EXPAND_NOTHING;
1447 arg = skip_regexp(arg + 1, *arg, magic_isset());
1448 }
1449 }
1450 return find_nextcmd(arg);
1451}
1452#endif
1453
1454/*
1455 * Returns a pointer to the next command after a :global or a :v command.
1456 * Returns NULL if there is no next command.
1457 */
1458 static char_u *
1459find_cmd_after_global_cmd(char_u *arg)
1460{
1461 int delim;
1462
1463 delim = *arg; // get the delimiter
1464 if (delim)
1465 ++arg; // skip delimiter if there is one
1466
1467 while (arg[0] != NUL && arg[0] != delim)
1468 {
1469 if (arg[0] == '\\' && arg[1] != NUL)
1470 ++arg;
1471 ++arg;
1472 }
1473 if (arg[0] != NUL)
1474 return arg + 1;
1475
1476 return NULL;
1477}
1478
1479/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001480 * Returns a pointer to the next command after a :substitute or a :& command.
1481 * Returns NULL if there is no next command.
1482 */
1483 static char_u *
1484find_cmd_after_substitute_cmd(char_u *arg)
1485{
1486 int delim;
1487
1488 delim = *arg;
1489 if (delim)
1490 {
1491 // skip "from" part
1492 ++arg;
1493 arg = skip_regexp(arg, delim, magic_isset());
1494
1495 if (arg[0] != NUL && arg[0] == delim)
1496 {
1497 // skip "to" part
1498 ++arg;
1499 while (arg[0] != NUL && arg[0] != delim)
1500 {
1501 if (arg[0] == '\\' && arg[1] != NUL)
1502 ++arg;
1503 ++arg;
1504 }
1505 if (arg[0] != NUL) // skip delimiter
1506 ++arg;
1507 }
1508 }
1509 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1510 ++arg;
1511 if (arg[0] != NUL)
1512 return arg;
1513
1514 return NULL;
1515}
1516
1517/*
1518 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1519 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1520 * Returns NULL if there is no next command.
1521 */
1522 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001523find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001524{
1525 arg = skipwhite(skipdigits(arg)); // skip count
1526 if (*arg == '/') // Match regexp, not just whole words
1527 {
1528 for (++arg; *arg && *arg != '/'; arg++)
1529 if (*arg == '\\' && arg[1] != NUL)
1530 arg++;
1531 if (*arg)
1532 {
1533 arg = skipwhite(arg + 1);
1534
1535 // Check for trailing illegal characters
1536 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1537 xp->xp_context = EXPAND_NOTHING;
1538 else
1539 return arg;
1540 }
1541 }
1542
1543 return NULL;
1544}
1545
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001546#ifdef FEAT_EVAL
1547/*
1548 * Set the completion context for the :unlet command. Always returns NULL.
1549 */
1550 static char_u *
1551set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1552{
1553 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1554 arg = xp->xp_pattern + 1;
1555
1556 xp->xp_context = EXPAND_USER_VARS;
1557 xp->xp_pattern = arg;
1558
1559 if (*xp->xp_pattern == '$')
1560 {
1561 xp->xp_context = EXPAND_ENV_VARS;
1562 ++xp->xp_pattern;
1563 }
1564
1565 return NULL;
1566}
1567#endif
1568
1569#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1570/*
1571 * Set the completion context for the :language command. Always returns NULL.
1572 */
1573 static char_u *
1574set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1575{
1576 char_u *p;
1577
1578 p = skiptowhite(arg);
1579 if (*p == NUL)
1580 {
1581 xp->xp_context = EXPAND_LANGUAGE;
1582 xp->xp_pattern = arg;
1583 }
1584 else
1585 {
1586 if ( STRNCMP(arg, "messages", p - arg) == 0
1587 || STRNCMP(arg, "ctype", p - arg) == 0
1588 || STRNCMP(arg, "time", p - arg) == 0
1589 || STRNCMP(arg, "collate", p - arg) == 0)
1590 {
1591 xp->xp_context = EXPAND_LOCALES;
1592 xp->xp_pattern = skipwhite(p);
1593 }
1594 else
1595 xp->xp_context = EXPAND_NOTHING;
1596 }
1597
1598 return NULL;
1599}
1600#endif
1601
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001602/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001603 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1604 * The argument to the command is 'arg' and the argument flags is 'argt'.
1605 * For user-defined commands and for environment variables, 'compl' has the
1606 * completion type.
1607 * Returns a pointer to the next command. Returns NULL if there is no next
1608 * command.
1609 */
1610 static char_u *
1611set_context_by_cmdname(
1612 char_u *cmd,
1613 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001614 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001615 char_u *arg,
1616 long argt,
1617 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001618 int forceit)
1619{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001620 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001621 {
1622 case CMD_find:
1623 case CMD_sfind:
1624 case CMD_tabfind:
1625 if (xp->xp_context == EXPAND_FILES)
1626 xp->xp_context = EXPAND_FILES_IN_PATH;
1627 break;
1628 case CMD_cd:
1629 case CMD_chdir:
1630 case CMD_tcd:
1631 case CMD_tchdir:
1632 case CMD_lcd:
1633 case CMD_lchdir:
1634 if (xp->xp_context == EXPAND_FILES)
1635 xp->xp_context = EXPAND_DIRECTORIES;
1636 break;
1637 case CMD_help:
1638 xp->xp_context = EXPAND_HELP;
1639 xp->xp_pattern = arg;
1640 break;
1641
1642 // Command modifiers: return the argument.
1643 // Also for commands with an argument that is a command.
1644 case CMD_aboveleft:
1645 case CMD_argdo:
1646 case CMD_belowright:
1647 case CMD_botright:
1648 case CMD_browse:
1649 case CMD_bufdo:
1650 case CMD_cdo:
1651 case CMD_cfdo:
1652 case CMD_confirm:
1653 case CMD_debug:
1654 case CMD_folddoclosed:
1655 case CMD_folddoopen:
1656 case CMD_hide:
1657 case CMD_keepalt:
1658 case CMD_keepjumps:
1659 case CMD_keepmarks:
1660 case CMD_keeppatterns:
1661 case CMD_ldo:
1662 case CMD_leftabove:
1663 case CMD_lfdo:
1664 case CMD_lockmarks:
1665 case CMD_noautocmd:
1666 case CMD_noswapfile:
1667 case CMD_rightbelow:
1668 case CMD_sandbox:
1669 case CMD_silent:
1670 case CMD_tab:
1671 case CMD_tabdo:
1672 case CMD_topleft:
1673 case CMD_verbose:
1674 case CMD_vertical:
1675 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001676 case CMD_vim9cmd:
1677 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001678 return arg;
1679
1680 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001681 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001682
1683#ifdef FEAT_SEARCH_EXTRA
1684 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001685 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001686#endif
1687
1688 // All completion for the +cmdline_compl feature goes here.
1689
1690 case CMD_command:
1691 return set_context_in_user_cmd(xp, arg);
1692
1693 case CMD_delcommand:
1694 xp->xp_context = EXPAND_USER_COMMANDS;
1695 xp->xp_pattern = arg;
1696 break;
1697
1698 case CMD_global:
1699 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001700 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001701 case CMD_and:
1702 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001703 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 case CMD_isearch:
1705 case CMD_dsearch:
1706 case CMD_ilist:
1707 case CMD_dlist:
1708 case CMD_ijump:
1709 case CMD_psearch:
1710 case CMD_djump:
1711 case CMD_isplit:
1712 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001713 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001714 case CMD_autocmd:
1715 return set_context_in_autocmd(xp, arg, FALSE);
1716 case CMD_doautocmd:
1717 case CMD_doautoall:
1718 return set_context_in_autocmd(xp, arg, TRUE);
1719 case CMD_set:
1720 set_context_in_set_cmd(xp, arg, 0);
1721 break;
1722 case CMD_setglobal:
1723 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1724 break;
1725 case CMD_setlocal:
1726 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1727 break;
1728 case CMD_tag:
1729 case CMD_stag:
1730 case CMD_ptag:
1731 case CMD_ltag:
1732 case CMD_tselect:
1733 case CMD_stselect:
1734 case CMD_ptselect:
1735 case CMD_tjump:
1736 case CMD_stjump:
1737 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001738 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001739 xp->xp_context = EXPAND_TAGS_LISTFILES;
1740 else
1741 xp->xp_context = EXPAND_TAGS;
1742 xp->xp_pattern = arg;
1743 break;
1744 case CMD_augroup:
1745 xp->xp_context = EXPAND_AUGROUP;
1746 xp->xp_pattern = arg;
1747 break;
1748#ifdef FEAT_SYN_HL
1749 case CMD_syntax:
1750 set_context_in_syntax_cmd(xp, arg);
1751 break;
1752#endif
1753#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001754 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001755 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001756 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001757 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001758 case CMD_if:
1759 case CMD_elseif:
1760 case CMD_while:
1761 case CMD_for:
1762 case CMD_echo:
1763 case CMD_echon:
1764 case CMD_execute:
1765 case CMD_echomsg:
1766 case CMD_echoerr:
1767 case CMD_call:
1768 case CMD_return:
1769 case CMD_cexpr:
1770 case CMD_caddexpr:
1771 case CMD_cgetexpr:
1772 case CMD_lexpr:
1773 case CMD_laddexpr:
1774 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001775 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001776 break;
1777
1778 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001779 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001780 case CMD_function:
1781 case CMD_delfunction:
1782 xp->xp_context = EXPAND_USER_FUNC;
1783 xp->xp_pattern = arg;
1784 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001785 case CMD_disassemble:
1786 set_context_in_disassemble_cmd(xp, arg);
1787 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001788
1789 case CMD_echohl:
1790 set_context_in_echohl_cmd(xp, arg);
1791 break;
1792#endif
1793 case CMD_highlight:
1794 set_context_in_highlight_cmd(xp, arg);
1795 break;
1796#ifdef FEAT_CSCOPE
1797 case CMD_cscope:
1798 case CMD_lcscope:
1799 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001800 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001801 break;
1802#endif
1803#ifdef FEAT_SIGNS
1804 case CMD_sign:
1805 set_context_in_sign_cmd(xp, arg);
1806 break;
1807#endif
1808 case CMD_bdelete:
1809 case CMD_bwipeout:
1810 case CMD_bunload:
1811 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1812 arg = xp->xp_pattern + 1;
1813 // FALLTHROUGH
1814 case CMD_buffer:
1815 case CMD_sbuffer:
1816 case CMD_checktime:
1817 xp->xp_context = EXPAND_BUFFERS;
1818 xp->xp_pattern = arg;
1819 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001820#ifdef FEAT_DIFF
1821 case CMD_diffget:
1822 case CMD_diffput:
1823 // If current buffer is in diff mode, complete buffer names
1824 // which are in diff mode, and different than current buffer.
1825 xp->xp_context = EXPAND_DIFF_BUFFERS;
1826 xp->xp_pattern = arg;
1827 break;
1828#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001829 case CMD_USER:
1830 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001831 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1832 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001833
1834 case CMD_map: case CMD_noremap:
1835 case CMD_nmap: case CMD_nnoremap:
1836 case CMD_vmap: case CMD_vnoremap:
1837 case CMD_omap: case CMD_onoremap:
1838 case CMD_imap: case CMD_inoremap:
1839 case CMD_cmap: case CMD_cnoremap:
1840 case CMD_lmap: case CMD_lnoremap:
1841 case CMD_smap: case CMD_snoremap:
1842 case CMD_tmap: case CMD_tnoremap:
1843 case CMD_xmap: case CMD_xnoremap:
1844 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001845 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001846 case CMD_unmap:
1847 case CMD_nunmap:
1848 case CMD_vunmap:
1849 case CMD_ounmap:
1850 case CMD_iunmap:
1851 case CMD_cunmap:
1852 case CMD_lunmap:
1853 case CMD_sunmap:
1854 case CMD_tunmap:
1855 case CMD_xunmap:
1856 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001857 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001858 case CMD_mapclear:
1859 case CMD_nmapclear:
1860 case CMD_vmapclear:
1861 case CMD_omapclear:
1862 case CMD_imapclear:
1863 case CMD_cmapclear:
1864 case CMD_lmapclear:
1865 case CMD_smapclear:
1866 case CMD_tmapclear:
1867 case CMD_xmapclear:
1868 xp->xp_context = EXPAND_MAPCLEAR;
1869 xp->xp_pattern = arg;
1870 break;
1871
1872 case CMD_abbreviate: case CMD_noreabbrev:
1873 case CMD_cabbrev: case CMD_cnoreabbrev:
1874 case CMD_iabbrev: case CMD_inoreabbrev:
1875 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001876 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001877 case CMD_unabbreviate:
1878 case CMD_cunabbrev:
1879 case CMD_iunabbrev:
1880 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001881 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001882#ifdef FEAT_MENU
1883 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1884 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1885 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1886 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1887 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1888 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1889 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1890 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1891 case CMD_tmenu: case CMD_tunmenu:
1892 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1893 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1894#endif
1895
1896 case CMD_colorscheme:
1897 xp->xp_context = EXPAND_COLORS;
1898 xp->xp_pattern = arg;
1899 break;
1900
1901 case CMD_compiler:
1902 xp->xp_context = EXPAND_COMPILER;
1903 xp->xp_pattern = arg;
1904 break;
1905
1906 case CMD_ownsyntax:
1907 xp->xp_context = EXPAND_OWNSYNTAX;
1908 xp->xp_pattern = arg;
1909 break;
1910
1911 case CMD_setfiletype:
1912 xp->xp_context = EXPAND_FILETYPE;
1913 xp->xp_pattern = arg;
1914 break;
1915
1916 case CMD_packadd:
1917 xp->xp_context = EXPAND_PACKADD;
1918 xp->xp_pattern = arg;
1919 break;
1920
1921#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1922 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001923 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001924#endif
1925#if defined(FEAT_PROFILE)
1926 case CMD_profile:
1927 set_context_in_profile_cmd(xp, arg);
1928 break;
1929#endif
1930 case CMD_behave:
1931 xp->xp_context = EXPAND_BEHAVE;
1932 xp->xp_pattern = arg;
1933 break;
1934
1935 case CMD_messages:
1936 xp->xp_context = EXPAND_MESSAGES;
1937 xp->xp_pattern = arg;
1938 break;
1939
1940 case CMD_history:
1941 xp->xp_context = EXPAND_HISTORY;
1942 xp->xp_pattern = arg;
1943 break;
1944#if defined(FEAT_PROFILE)
1945 case CMD_syntime:
1946 xp->xp_context = EXPAND_SYNTIME;
1947 xp->xp_pattern = arg;
1948 break;
1949#endif
1950
1951 case CMD_argdelete:
1952 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1953 arg = xp->xp_pattern + 1;
1954 xp->xp_context = EXPAND_ARGLIST;
1955 xp->xp_pattern = arg;
1956 break;
1957
1958 default:
1959 break;
1960 }
1961 return NULL;
1962}
1963
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001964/*
1965 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
1966 * we don't need/want deleted. Maybe this could be done better if we didn't
1967 * repeat all this stuff. The only problem is that they may not stay
1968 * perfectly compatible with each other, but then the command line syntax
1969 * probably won't change that much -- webb.
1970 */
1971 static char_u *
1972set_one_cmd_context(
1973 expand_T *xp,
1974 char_u *buff) // buffer for command string
1975{
1976 char_u *p;
1977 char_u *cmd, *arg;
1978 int len = 0;
1979 exarg_T ea;
1980 int compl = EXPAND_NOTHING;
1981 int forceit = FALSE;
1982 int usefilter = FALSE; // filter instead of file name
1983
1984 ExpandInit(xp);
1985 xp->xp_pattern = buff;
1986 xp->xp_line = buff;
1987 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
1988 ea.argt = 0;
1989
1990 // 1. skip comment lines and leading space, colons or bars
1991 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
1992 ;
1993 xp->xp_pattern = cmd;
1994
1995 if (*cmd == NUL)
1996 return NULL;
1997 if (*cmd == '"') // ignore comment lines
1998 {
1999 xp->xp_context = EXPAND_NOTHING;
2000 return NULL;
2001 }
2002
2003 // 3. Skip over the range to find the command.
2004 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2005 xp->xp_pattern = cmd;
2006 if (*cmd == NUL)
2007 return NULL;
2008 if (*cmd == '"')
2009 {
2010 xp->xp_context = EXPAND_NOTHING;
2011 return NULL;
2012 }
2013
2014 if (*cmd == '|' || *cmd == '\n')
2015 return cmd + 1; // There's another command
2016
2017 // Get the command index.
2018 p = set_cmd_index(cmd, &ea, xp, &compl);
2019 if (p == NULL)
2020 return NULL;
2021
2022 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2023
2024 if (*p == '!') // forced commands
2025 {
2026 forceit = TRUE;
2027 ++p;
2028 }
2029
2030 // 6. parse arguments
2031 if (!IS_USER_CMDIDX(ea.cmdidx))
2032 ea.argt = excmd_get_argt(ea.cmdidx);
2033
2034 arg = skipwhite(p);
2035
2036 // Skip over ++argopt argument
2037 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2038 {
2039 p = arg;
2040 while (*p && !vim_isspace(*p))
2041 MB_PTR_ADV(p);
2042 arg = skipwhite(p);
2043 }
2044
2045 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2046 {
2047 if (*arg == '>') // append
2048 {
2049 if (*++arg == '>')
2050 ++arg;
2051 arg = skipwhite(arg);
2052 }
2053 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2054 {
2055 ++arg;
2056 usefilter = TRUE;
2057 }
2058 }
2059
2060 if (ea.cmdidx == CMD_read)
2061 {
2062 usefilter = forceit; // :r! filter if forced
2063 if (*arg == '!') // :r !filter
2064 {
2065 ++arg;
2066 usefilter = TRUE;
2067 }
2068 }
2069
2070 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2071 {
2072 while (*arg == *cmd) // allow any number of '>' or '<'
2073 ++arg;
2074 arg = skipwhite(arg);
2075 }
2076
2077 // Does command allow "+command"?
2078 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2079 {
2080 // Check if we're in the +command
2081 p = arg + 1;
2082 arg = skip_cmd_arg(arg, FALSE);
2083
2084 // Still touching the command after '+'?
2085 if (*arg == NUL)
2086 return p;
2087
2088 // Skip space(s) after +command to get to the real argument
2089 arg = skipwhite(arg);
2090 }
2091
2092
2093 // Check for '|' to separate commands and '"' to start comments.
2094 // Don't do this for ":read !cmd" and ":write !cmd".
2095 if ((ea.argt & EX_TRLBAR) && !usefilter)
2096 {
2097 p = arg;
2098 // ":redir @" is not the start of a comment
2099 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2100 p += 2;
2101 while (*p)
2102 {
2103 if (*p == Ctrl_V)
2104 {
2105 if (p[1] != NUL)
2106 ++p;
2107 }
2108 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2109 || *p == '|' || *p == '\n')
2110 {
2111 if (*(p - 1) != '\\')
2112 {
2113 if (*p == '|' || *p == '\n')
2114 return p + 1;
2115 return NULL; // It's a comment
2116 }
2117 }
2118 MB_PTR_ADV(p);
2119 }
2120 }
2121
2122 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2123 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2124 // no arguments allowed but there is something
2125 return NULL;
2126
2127 // Find start of last argument (argument just before cursor):
2128 p = buff;
2129 xp->xp_pattern = p;
2130 len = (int)STRLEN(buff);
2131 while (*p && p < buff + len)
2132 {
2133 if (*p == ' ' || *p == TAB)
2134 {
2135 // argument starts after a space
2136 xp->xp_pattern = ++p;
2137 }
2138 else
2139 {
2140 if (*p == '\\' && *(p + 1) != NUL)
2141 ++p; // skip over escaped character
2142 MB_PTR_ADV(p);
2143 }
2144 }
2145
2146 if (ea.argt & EX_XFILE)
2147 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2148
2149 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002150 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002151 forceit);
2152}
2153
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002154/*
2155 * Set the completion context in 'xp' for command 'str'
2156 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002157 void
2158set_cmd_context(
2159 expand_T *xp,
2160 char_u *str, // start of command line
2161 int len, // length of command line (excl. NUL)
2162 int col, // position of cursor
2163 int use_ccline UNUSED) // use ccline for info
2164{
2165#ifdef FEAT_EVAL
2166 cmdline_info_T *ccline = get_cmdline_info();
2167#endif
2168 int old_char = NUL;
2169 char_u *nextcomm;
2170
2171 // Avoid a UMR warning from Purify, only save the character if it has been
2172 // written before.
2173 if (col < len)
2174 old_char = str[col];
2175 str[col] = NUL;
2176 nextcomm = str;
2177
2178#ifdef FEAT_EVAL
2179 if (use_ccline && ccline->cmdfirstc == '=')
2180 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002181 // pass CMD_SIZE because there is no real command
2182 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002183 }
2184 else if (use_ccline && ccline->input_fn)
2185 {
2186 xp->xp_context = ccline->xp_context;
2187 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002188 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002189 }
2190 else
2191#endif
2192 while (nextcomm != NULL)
2193 nextcomm = set_one_cmd_context(xp, nextcomm);
2194
2195 // Store the string here so that call_user_expand_func() can get to them
2196 // easily.
2197 xp->xp_line = str;
2198 xp->xp_col = col;
2199
2200 str[col] = old_char;
2201}
2202
2203/*
2204 * Expand the command line "str" from context "xp".
2205 * "xp" must have been set by set_cmd_context().
2206 * xp->xp_pattern points into "str", to where the text that is to be expanded
2207 * starts.
2208 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2209 * cursor.
2210 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2211 * key that triggered expansion literally.
2212 * Returns EXPAND_OK otherwise.
2213 */
2214 int
2215expand_cmdline(
2216 expand_T *xp,
2217 char_u *str, // start of command line
2218 int col, // position of cursor
2219 int *matchcount, // return: nr of matches
2220 char_u ***matches) // return: array of pointers to matches
2221{
2222 char_u *file_str = NULL;
2223 int options = WILD_ADD_SLASH|WILD_SILENT;
2224
2225 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2226 {
2227 beep_flush();
2228 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2229 }
2230 if (xp->xp_context == EXPAND_NOTHING)
2231 {
2232 // Caller can use the character as a normal char instead
2233 return EXPAND_NOTHING;
2234 }
2235
2236 // add star to file name, or convert to regexp if not exp. files.
2237 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002238 if (cmdline_fuzzy_completion_supported(xp))
2239 // If fuzzy matching, don't modify the search string
2240 file_str = vim_strsave(xp->xp_pattern);
2241 else
2242 {
2243 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2244 if (file_str == NULL)
2245 return EXPAND_UNSUCCESSFUL;
2246 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002247
2248 if (p_wic)
2249 options += WILD_ICASE;
2250
2251 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002252 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002253 {
2254 *matchcount = 0;
2255 *matches = NULL;
2256 }
2257 vim_free(file_str);
2258
2259 return EXPAND_OK;
2260}
2261
Bram Moolenaar66b51422019-08-18 21:44:12 +02002262/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002263 * Expand file or directory names.
2264 */
2265 static int
2266expand_files_and_dirs(
2267 expand_T *xp,
2268 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002269 char_u ***matches,
2270 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002271 int flags,
2272 int options)
2273{
2274 int free_pat = FALSE;
2275 int i;
2276 int ret;
2277
2278 // for ":set path=" and ":set tags=" halve backslashes for escaped
2279 // space
2280 if (xp->xp_backslash != XP_BS_NONE)
2281 {
2282 free_pat = TRUE;
2283 pat = vim_strsave(pat);
2284 for (i = 0; pat[i]; ++i)
2285 if (pat[i] == '\\')
2286 {
2287 if (xp->xp_backslash == XP_BS_THREE
2288 && pat[i + 1] == '\\'
2289 && pat[i + 2] == '\\'
2290 && pat[i + 3] == ' ')
2291 STRMOVE(pat + i, pat + i + 3);
2292 if (xp->xp_backslash == XP_BS_ONE
2293 && pat[i + 1] == ' ')
2294 STRMOVE(pat + i, pat + i + 1);
2295 }
2296 }
2297
2298 if (xp->xp_context == EXPAND_FILES)
2299 flags |= EW_FILE;
2300 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2301 flags |= (EW_FILE | EW_PATH);
2302 else
2303 flags = (flags | EW_DIR) & ~EW_FILE;
2304 if (options & WILD_ICASE)
2305 flags |= EW_ICASE;
2306
2307 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002308 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002309 if (free_pat)
2310 vim_free(pat);
2311#ifdef BACKSLASH_IN_FILENAME
2312 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2313 {
2314 int j;
2315
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002316 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002317 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002318 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002319
2320 while (*ptr != NUL)
2321 {
2322 if (p_csl[0] == 's' && *ptr == '\\')
2323 *ptr = '/';
2324 else if (p_csl[0] == 'b' && *ptr == '/')
2325 *ptr = '\\';
2326 ptr += (*mb_ptr2len)(ptr);
2327 }
2328 }
2329 }
2330#endif
2331 return ret;
2332}
2333
2334/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002335 * Function given to ExpandGeneric() to obtain the possible arguments of the
2336 * ":behave {mswin,xterm}" command.
2337 */
2338 static char_u *
2339get_behave_arg(expand_T *xp UNUSED, int idx)
2340{
2341 if (idx == 0)
2342 return (char_u *)"mswin";
2343 if (idx == 1)
2344 return (char_u *)"xterm";
2345 return NULL;
2346}
2347
2348/*
2349 * Function given to ExpandGeneric() to obtain the possible arguments of the
2350 * ":messages {clear}" command.
2351 */
2352 static char_u *
2353get_messages_arg(expand_T *xp UNUSED, int idx)
2354{
2355 if (idx == 0)
2356 return (char_u *)"clear";
2357 return NULL;
2358}
2359
2360 static char_u *
2361get_mapclear_arg(expand_T *xp UNUSED, int idx)
2362{
2363 if (idx == 0)
2364 return (char_u *)"<buffer>";
2365 return NULL;
2366}
2367
2368/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002369 * Do the expansion based on xp->xp_context and 'rmp'.
2370 */
2371 static int
2372ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002373 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002374 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002375 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002376 char_u ***matches,
2377 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002378{
2379 static struct expgen
2380 {
2381 int context;
2382 char_u *((*func)(expand_T *, int));
2383 int ic;
2384 int escaped;
2385 } tab[] =
2386 {
2387 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2388 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2389 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2390 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2391 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2392 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2393 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2394 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2395 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2396 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2397# ifdef FEAT_EVAL
2398 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2399 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2400 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2401 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2402 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2403# endif
2404# ifdef FEAT_MENU
2405 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2406 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2407# endif
2408# ifdef FEAT_SYN_HL
2409 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2410# endif
2411# ifdef FEAT_PROFILE
2412 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2413# endif
2414 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2415 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2416 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
2417# ifdef FEAT_CSCOPE
2418 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2419# endif
2420# ifdef FEAT_SIGNS
2421 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2422# endif
2423# ifdef FEAT_PROFILE
2424 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2425# endif
2426# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2427 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2428 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2429# endif
2430 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2431 {EXPAND_USER, get_users, TRUE, FALSE},
2432 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2433 };
2434 int i;
2435 int ret = FAIL;
2436
2437 // Find a context in the table and call the ExpandGeneric() with the
2438 // right function to do the expansion.
2439 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2440 {
2441 if (xp->xp_context == tab[i].context)
2442 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002443 // Use fuzzy matching if 'wildoptions' has 'fuzzy'.
2444 // If no search pattern is supplied, then don't use fuzzy
2445 // matching and return all the found items.
2446 int fuzzy = cmdline_fuzzy_complete(pat);
2447
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002448 if (tab[i].ic)
2449 rmp->rm_ic = TRUE;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002450 ret = ExpandGeneric(xp, rmp, matches, numMatches,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002451 tab[i].func, tab[i].escaped,
2452 fuzzy ? pat : NULL);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002453 break;
2454 }
2455 }
2456
2457 return ret;
2458}
2459
2460/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002461 * Map wild expand options to flags for expand_wildcards()
2462 */
2463 static int
2464map_wildopts_to_ewflags(int options)
2465{
2466 int flags;
2467
2468 flags = EW_DIR; // include directories
2469 if (options & WILD_LIST_NOTFOUND)
2470 flags |= EW_NOTFOUND;
2471 if (options & WILD_ADD_SLASH)
2472 flags |= EW_ADDSLASH;
2473 if (options & WILD_KEEP_ALL)
2474 flags |= EW_KEEPALL;
2475 if (options & WILD_SILENT)
2476 flags |= EW_SILENT;
2477 if (options & WILD_NOERROR)
2478 flags |= EW_NOERROR;
2479 if (options & WILD_ALLLINKS)
2480 flags |= EW_ALLLINKS;
2481
2482 return flags;
2483}
2484
2485/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002486 * Do the expansion based on xp->xp_context and "pat".
2487 */
2488 static int
2489ExpandFromContext(
2490 expand_T *xp,
2491 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002492 char_u ***matches,
2493 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002494 int options) // WILD_ flags
2495{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002496 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002497 int ret;
2498 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002499 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002500 int fuzzy = cmdline_fuzzy_complete(pat)
2501 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002502
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002503 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002504
2505 if (xp->xp_context == EXPAND_FILES
2506 || xp->xp_context == EXPAND_DIRECTORIES
2507 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002508 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2509 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002510
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002511 *matches = (char_u **)"";
2512 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002513 if (xp->xp_context == EXPAND_HELP)
2514 {
2515 // With an empty argument we would get all the help tags, which is
2516 // very slow. Get matches for "help" instead.
2517 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002518 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002519 {
2520#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002521 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002522#endif
2523 return OK;
2524 }
2525 return FAIL;
2526 }
2527
Bram Moolenaar66b51422019-08-18 21:44:12 +02002528 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002529 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002530 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002531 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002532 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002533 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002534#ifdef FEAT_DIFF
2535 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002536 return ExpandBufnames(pat, numMatches, matches,
2537 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002538#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002539 if (xp->xp_context == EXPAND_TAGS
2540 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002541 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2542 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002543 if (xp->xp_context == EXPAND_COLORS)
2544 {
2545 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002546 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002547 directories);
2548 }
2549 if (xp->xp_context == EXPAND_COMPILER)
2550 {
2551 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002552 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002553 }
2554 if (xp->xp_context == EXPAND_OWNSYNTAX)
2555 {
2556 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002557 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002558 }
2559 if (xp->xp_context == EXPAND_FILETYPE)
2560 {
2561 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002562 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002563 }
2564# if defined(FEAT_EVAL)
2565 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002566 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002567# endif
2568 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002569 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002570
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002571 // When expanding a function name starting with s:, match the <SNR>nr_
2572 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002573 if ((xp->xp_context == EXPAND_USER_FUNC
2574 || xp->xp_context == EXPAND_DISASSEMBLE)
2575 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002576 {
2577 int len = (int)STRLEN(pat) + 20;
2578
2579 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002580 if (tofree == NULL)
2581 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002582 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002583 pat = tofree;
2584 }
2585
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002586 if (!fuzzy)
2587 {
2588 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2589 if (regmatch.regprog == NULL)
2590 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002591
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002592 // set ignore-case according to p_ic, p_scs and pat
2593 regmatch.rm_ic = ignorecase(pat);
2594 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002595
2596 if (xp->xp_context == EXPAND_SETTINGS
2597 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002598 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002599 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002600 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002601# if defined(FEAT_EVAL)
2602 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002603 ret = ExpandUserDefined(xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002604# endif
2605 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002606 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002607
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002608 if (!fuzzy)
2609 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002610 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002611
2612 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002613}
2614
Bram Moolenaar66b51422019-08-18 21:44:12 +02002615/*
2616 * Expand a list of names.
2617 *
2618 * Generic function for command line completion. It calls a function to
2619 * obtain strings, one by one. The strings are matched against a regexp
2620 * program. Matching strings are copied into an array, which is returned.
2621 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002622 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2623 * is used.
2624 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002625 * Returns OK when no problems encountered, FAIL for error (out of memory).
2626 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002627 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002628ExpandGeneric(
2629 expand_T *xp,
2630 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002631 char_u ***matches,
2632 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002633 char_u *((*func)(expand_T *, int)),
2634 // returns a string from the list
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002635 int escaped,
2636 char_u *fuzzystr)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002637{
2638 int i;
2639 int count = 0;
2640 int round;
2641 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002642 fuzmatch_str_T *fuzmatch = NULL;
2643 int score = 0;
2644 int fuzzy = (fuzzystr != NULL);
2645 int funcsort = FALSE;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002646 int match;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002647
2648 // do this loop twice:
2649 // round == 0: count the number of matching names
2650 // round == 1: copy the matching names into allocated memory
2651 for (round = 0; round <= 1; ++round)
2652 {
2653 for (i = 0; ; ++i)
2654 {
2655 str = (*func)(xp, i);
2656 if (str == NULL) // end of list
2657 break;
2658 if (*str == NUL) // skip empty strings
2659 continue;
2660
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002661 if (!fuzzy)
2662 match = vim_regexec(regmatch, str, (colnr_T)0);
2663 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002664 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002665 score = fuzzy_match_str(str, fuzzystr);
2666 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002667 }
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002668
2669 if (!match)
2670 continue;
2671
2672 if (round)
2673 {
2674 if (escaped)
2675 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2676 else
2677 str = vim_strsave(str);
2678 if (str == NULL)
2679 {
2680 if (fuzzy)
2681 fuzmatch_str_free(fuzmatch, count);
2682 else if (count > 0)
2683 FreeWild(count, *matches);
2684 *numMatches = 0;
2685 *matches = NULL;
2686 return FAIL;
2687 }
2688 if (fuzzy)
2689 {
2690 fuzmatch[count].idx = count;
2691 fuzmatch[count].str = str;
2692 fuzmatch[count].score = score;
2693 }
2694 else
2695 (*matches)[count] = str;
2696# ifdef FEAT_MENU
2697 if (func == get_menu_names && str != NULL)
2698 {
2699 // test for separator added by get_menu_names()
2700 str += STRLEN(str) - 1;
2701 if (*str == '\001')
2702 *str = '.';
2703 }
2704# endif
2705 }
2706 ++count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002707 }
2708 if (round == 0)
2709 {
2710 if (count == 0)
2711 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002712 if (fuzzy)
2713 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2714 else
2715 *matches = ALLOC_MULT(char_u *, count);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002716 if ((!fuzzy && (*matches == NULL))
2717 || (fuzzy && (fuzmatch == NULL)))
Bram Moolenaar66b51422019-08-18 21:44:12 +02002718 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002719 *numMatches = 0;
2720 *matches = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002721 return FAIL;
2722 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002723 *numMatches = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002724 count = 0;
2725 }
2726 }
2727
2728 // Sort the results. Keep menu's in the specified order.
2729 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2730 {
2731 if (xp->xp_context == EXPAND_EXPRESSION
2732 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002733 || xp->xp_context == EXPAND_USER_FUNC
2734 || xp->xp_context == EXPAND_DISASSEMBLE)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002735 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002736 // <SNR> functions should be sorted to the end.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002737 funcsort = TRUE;
2738 if (!fuzzy)
2739 qsort((void *)*matches, (size_t)*numMatches, sizeof(char_u *),
Bram Moolenaar66b51422019-08-18 21:44:12 +02002740 sort_func_compare);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002741 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002742 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002743 {
2744 if (!fuzzy)
2745 sort_strings(*matches, *numMatches);
2746 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002747 }
2748
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002749#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002750 // Reset the variables used for special highlight names expansion, so that
2751 // they don't show up when getting normal highlight names by ID.
2752 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002753#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002754
2755 if (fuzzy && fuzzymatches_to_strmatches(fuzmatch, matches, count,
2756 funcsort) == FAIL)
2757 return FAIL;
2758
Bram Moolenaar66b51422019-08-18 21:44:12 +02002759 return OK;
2760}
2761
2762/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002763 * Expand shell command matches in one directory of $PATH.
2764 */
2765 static void
2766expand_shellcmd_onedir(
2767 char_u *buf,
2768 char_u *s,
2769 size_t l,
2770 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002771 char_u ***matches,
2772 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002773 int flags,
2774 hashtab_T *ht,
2775 garray_T *gap)
2776{
2777 int ret;
2778 int i;
2779 hash_T hash;
2780 hashitem_T *hi;
2781
2782 vim_strncpy(buf, s, l);
2783 add_pathsep(buf);
2784 l = STRLEN(buf);
2785 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2786
2787 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002788 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002789 if (ret == OK)
2790 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002791 if (ga_grow(gap, *numMatches) == FAIL)
2792 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002793 else
2794 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002795 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002796 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002797 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002798
2799 if (STRLEN(name) > l)
2800 {
2801 // Check if this name was already found.
2802 hash = hash_hash(name + l);
2803 hi = hash_lookup(ht, name + l, hash);
2804 if (HASHITEM_EMPTY(hi))
2805 {
2806 // Remove the path that was prepended.
2807 STRMOVE(name, name + l);
2808 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2809 hash_add_item(ht, hi, name, hash);
2810 name = NULL;
2811 }
2812 }
2813 vim_free(name);
2814 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002815 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002816 }
2817 }
2818}
2819
2820/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002821 * Complete a shell command.
2822 * Returns FAIL or OK;
2823 */
2824 static int
2825expand_shellcmd(
2826 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002827 char_u ***matches, // return: array with matches
2828 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02002829 int flagsarg) // EW_ flags
2830{
2831 char_u *pat;
2832 int i;
2833 char_u *path = NULL;
2834 int mustfree = FALSE;
2835 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002836 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002837 size_t l;
2838 char_u *s, *e;
2839 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002840 int did_curdir = FALSE;
2841 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002842
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002843 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002844 if (buf == NULL)
2845 return FAIL;
2846
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002847 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002848 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002849 if (pat == NULL)
2850 {
2851 vim_free(buf);
2852 return FAIL;
2853 }
2854
Bram Moolenaar66b51422019-08-18 21:44:12 +02002855 for (i = 0; pat[i]; ++i)
2856 if (pat[i] == '\\' && pat[i + 1] == ' ')
2857 STRMOVE(pat + i, pat + i + 1);
2858
2859 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2860
2861 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2862 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2863 path = (char_u *)".";
2864 else
2865 {
2866 // For an absolute name we don't use $PATH.
2867 if (!mch_isFullName(pat))
2868 path = vim_getenv((char_u *)"PATH", &mustfree);
2869 if (path == NULL)
2870 path = (char_u *)"";
2871 }
2872
2873 // Go over all directories in $PATH. Expand matches in that directory and
2874 // collect them in "ga". When "." is not in $PATH also expand for the
2875 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002876 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002877 hash_init(&found_ht);
2878 for (s = path; ; s = e)
2879 {
2880# if defined(MSWIN)
2881 e = vim_strchr(s, ';');
2882# else
2883 e = vim_strchr(s, ':');
2884# endif
2885 if (e == NULL)
2886 e = s + STRLEN(s);
2887
2888 if (*s == NUL)
2889 {
2890 if (did_curdir)
2891 break;
2892 // Find directories in the current directory, path is empty.
2893 did_curdir = TRUE;
2894 flags |= EW_DIR;
2895 }
2896 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2897 {
2898 did_curdir = TRUE;
2899 flags |= EW_DIR;
2900 }
2901 else
2902 // Do not match directories inside a $PATH item.
2903 flags &= ~EW_DIR;
2904
2905 l = e - s;
2906 if (l > MAXPATHL - 5)
2907 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002908
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002909 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002910 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002911
Bram Moolenaar66b51422019-08-18 21:44:12 +02002912 if (*e != NUL)
2913 ++e;
2914 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002915 *matches = ga.ga_data;
2916 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002917
2918 vim_free(buf);
2919 vim_free(pat);
2920 if (mustfree)
2921 vim_free(path);
2922 hash_clear(&found_ht);
2923 return OK;
2924}
2925
2926# if defined(FEAT_EVAL)
2927/*
2928 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002929 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002930 */
2931 static void *
2932call_user_expand_func(
2933 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002934 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002935{
2936 cmdline_info_T *ccline = get_cmdline_info();
2937 int keep = 0;
2938 typval_T args[4];
2939 sctx_T save_current_sctx = current_sctx;
2940 char_u *pat = NULL;
2941 void *ret;
2942
2943 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2944 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002945
2946 if (ccline->cmdbuff != NULL)
2947 {
2948 keep = ccline->cmdbuff[ccline->cmdlen];
2949 ccline->cmdbuff[ccline->cmdlen] = 0;
2950 }
2951
2952 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2953
2954 args[0].v_type = VAR_STRING;
2955 args[0].vval.v_string = pat;
2956 args[1].v_type = VAR_STRING;
2957 args[1].vval.v_string = xp->xp_line;
2958 args[2].v_type = VAR_NUMBER;
2959 args[2].vval.v_number = xp->xp_col;
2960 args[3].v_type = VAR_UNKNOWN;
2961
2962 current_sctx = xp->xp_script_ctx;
2963
2964 ret = user_expand_func(xp->xp_arg, 3, args);
2965
2966 current_sctx = save_current_sctx;
2967 if (ccline->cmdbuff != NULL)
2968 ccline->cmdbuff[ccline->cmdlen] = keep;
2969
2970 vim_free(pat);
2971 return ret;
2972}
2973
2974/*
2975 * Expand names with a function defined by the user.
2976 */
2977 static int
2978ExpandUserDefined(
2979 expand_T *xp,
2980 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002981 char_u ***matches,
2982 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002983{
2984 char_u *retstr;
2985 char_u *s;
2986 char_u *e;
2987 int keep;
2988 garray_T ga;
2989 int skip;
2990
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002991 *matches = NULL;
2992 *numMatches = 0;
2993 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002994 if (retstr == NULL)
2995 return FAIL;
2996
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002997 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002998 for (s = retstr; *s != NUL; s = e)
2999 {
3000 e = vim_strchr(s, '\n');
3001 if (e == NULL)
3002 e = s + STRLEN(s);
3003 keep = *e;
3004 *e = NUL;
3005
3006 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
3007 *e = keep;
3008
3009 if (!skip)
3010 {
3011 if (ga_grow(&ga, 1) == FAIL)
3012 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02003013 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003014 ++ga.ga_len;
3015 }
3016
3017 if (*e != NUL)
3018 ++e;
3019 }
3020 vim_free(retstr);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003021 *matches = ga.ga_data;
3022 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003023 return OK;
3024}
3025
3026/*
3027 * Expand names with a list returned by a function defined by the user.
3028 */
3029 static int
3030ExpandUserList(
3031 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003032 char_u ***matches,
3033 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003034{
3035 list_T *retlist;
3036 listitem_T *li;
3037 garray_T ga;
3038
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003039 *matches = NULL;
3040 *numMatches = 0;
3041 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003042 if (retlist == NULL)
3043 return FAIL;
3044
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003045 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003046 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003047 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003048 {
3049 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3050 continue; // Skip non-string items and empty strings
3051
3052 if (ga_grow(&ga, 1) == FAIL)
3053 break;
3054
3055 ((char_u **)ga.ga_data)[ga.ga_len] =
3056 vim_strsave(li->li_tv.vval.v_string);
3057 ++ga.ga_len;
3058 }
3059 list_unref(retlist);
3060
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003061 *matches = ga.ga_data;
3062 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003063 return OK;
3064}
3065# endif
3066
3067/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003068 * Expand "file" for all comma-separated directories in "path".
3069 * Adds the matches to "ga". Caller must init "ga".
3070 */
3071 void
3072globpath(
3073 char_u *path,
3074 char_u *file,
3075 garray_T *ga,
3076 int expand_options)
3077{
3078 expand_T xpc;
3079 char_u *buf;
3080 int i;
3081 int num_p;
3082 char_u **p;
3083
3084 buf = alloc(MAXPATHL);
3085 if (buf == NULL)
3086 return;
3087
3088 ExpandInit(&xpc);
3089 xpc.xp_context = EXPAND_FILES;
3090
3091 // Loop over all entries in {path}.
3092 while (*path != NUL)
3093 {
3094 // Copy one item of the path to buf[] and concatenate the file name.
3095 copy_option_part(&path, buf, MAXPATHL, ",");
3096 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3097 {
3098# if defined(MSWIN)
3099 // Using the platform's path separator (\) makes vim incorrectly
3100 // treat it as an escape character, use '/' instead.
3101 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3102 STRCAT(buf, "/");
3103# else
3104 add_pathsep(buf);
3105# endif
3106 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003107 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003108 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3109 {
3110 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3111
3112 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003113 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003114 for (i = 0; i < num_p; ++i)
3115 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003116 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003117 ++ga->ga_len;
3118 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003119 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120 }
3121 }
3122 }
3123
3124 vim_free(buf);
3125}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003126
Bram Moolenaareadee482020-09-04 15:37:31 +02003127#ifdef FEAT_WILDMENU
3128
3129/*
3130 * Translate some keys pressed when 'wildmenu' is used.
3131 */
3132 int
3133wildmenu_translate_key(
3134 cmdline_info_T *cclp,
3135 int key,
3136 expand_T *xp,
3137 int did_wild_list)
3138{
3139 int c = key;
3140
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003141#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003142 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003143 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003144 // When the popup menu is used for cmdline completion:
3145 // Up : go to the previous item in the menu
3146 // Down : go to the next item in the menu
3147 // Left : go to the parent directory
3148 // Right: list the files in the selected directory
3149 switch (c)
3150 {
3151 case K_UP: c = K_LEFT; break;
3152 case K_DOWN: c = K_RIGHT; break;
3153 case K_LEFT: c = K_UP; break;
3154 case K_RIGHT: c = K_DOWN; break;
3155 default: break;
3156 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003157 }
3158#endif
3159
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003160 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003161 {
3162 if (c == K_LEFT)
3163 c = Ctrl_P;
3164 else if (c == K_RIGHT)
3165 c = Ctrl_N;
3166 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003167
Bram Moolenaareadee482020-09-04 15:37:31 +02003168 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003169 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003170 && cclp->cmdpos > 1
3171 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3172 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3173 && (c == '\n' || c == '\r' || c == K_KENTER))
3174 c = K_DOWN;
3175
3176 return c;
3177}
3178
3179/*
3180 * Delete characters on the command line, from "from" to the current
3181 * position.
3182 */
3183 static void
3184cmdline_del(cmdline_info_T *cclp, int from)
3185{
3186 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3187 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3188 cclp->cmdlen -= cclp->cmdpos - from;
3189 cclp->cmdpos = from;
3190}
3191
3192/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003193 * Handle a key pressed when the wild menu for the menu names
3194 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003195 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003196 static int
3197wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003198{
Bram Moolenaareadee482020-09-04 15:37:31 +02003199 int i;
3200 int j;
3201
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003202 // Hitting <Down> after "emenu Name.": complete submenu
3203 if (key == K_DOWN && cclp->cmdpos > 0
3204 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003205 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003206 key = p_wc;
3207 KeyTyped = TRUE; // in case the key was mapped
3208 }
3209 else if (key == K_UP)
3210 {
3211 // Hitting <Up>: Remove one submenu name in front of the
3212 // cursor
3213 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003214
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003215 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3216 i = 0;
3217 while (--j > 0)
3218 {
3219 // check for start of menu name
3220 if (cclp->cmdbuff[j] == ' '
3221 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003222 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003223 i = j + 1;
3224 break;
3225 }
3226 // check for start of submenu name
3227 if (cclp->cmdbuff[j] == '.'
3228 && cclp->cmdbuff[j - 1] != '\\')
3229 {
3230 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003231 {
3232 i = j + 1;
3233 break;
3234 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003235 else
3236 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003237 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003238 }
3239 if (i > 0)
3240 cmdline_del(cclp, i);
3241 key = p_wc;
3242 KeyTyped = TRUE; // in case the key was mapped
3243 xp->xp_context = EXPAND_NOTHING;
3244 }
3245
3246 return key;
3247}
3248
3249/*
3250 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3251 * directory names (EXPAND_DIRECTORIES) or shell command names
3252 * (EXPAND_SHELLCMD) is displayed.
3253 */
3254 static int
3255wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3256{
3257 int i;
3258 int j;
3259 char_u upseg[5];
3260
3261 upseg[0] = PATHSEP;
3262 upseg[1] = '.';
3263 upseg[2] = '.';
3264 upseg[3] = PATHSEP;
3265 upseg[4] = NUL;
3266
3267 if (key == K_DOWN
3268 && cclp->cmdpos > 0
3269 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3270 && (cclp->cmdpos < 3
3271 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3272 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3273 {
3274 // go down a directory
3275 key = p_wc;
3276 KeyTyped = TRUE; // in case the key was mapped
3277 }
3278 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3279 {
3280 // If in a direct ancestor, strip off one ../ to go down
3281 int found = FALSE;
3282
3283 j = cclp->cmdpos;
3284 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3285 while (--j > i)
3286 {
3287 if (has_mbyte)
3288 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3289 if (vim_ispathsep(cclp->cmdbuff[j]))
3290 {
3291 found = TRUE;
3292 break;
3293 }
3294 }
3295 if (found
3296 && cclp->cmdbuff[j - 1] == '.'
3297 && cclp->cmdbuff[j - 2] == '.'
3298 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3299 {
3300 cmdline_del(cclp, j - 2);
3301 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003302 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003303 }
3304 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003305 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003306 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003307 // go up a directory
3308 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003309
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003310 j = cclp->cmdpos - 1;
3311 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3312 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003313 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003314 if (has_mbyte)
3315 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3316 if (vim_ispathsep(cclp->cmdbuff[j])
3317# ifdef BACKSLASH_IN_FILENAME
3318 && vim_strchr((char_u *)" *?[{`$%#",
3319 cclp->cmdbuff[j + 1]) == NULL
3320# endif
3321 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003322 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003323 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003324 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003325 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003326 break;
3327 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003328 else
3329 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003330 }
3331 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003332
3333 if (!found)
3334 j = i;
3335 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3336 j += 4;
3337 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3338 && j == i)
3339 j += 3;
3340 else
3341 j = 0;
3342 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003343 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003344 // TODO this is only for DOS/UNIX systems - need to put in
3345 // machine-specific stuff here and in upseg init
3346 cmdline_del(cclp, j);
3347 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003348 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003349 else if (cclp->cmdpos > i)
3350 cmdline_del(cclp, i);
3351
3352 // Now complete in the new directory. Set KeyTyped in case the
3353 // Up key came from a mapping.
3354 key = p_wc;
3355 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003356 }
3357
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003358 return key;
3359}
3360
3361/*
3362 * Handle a key pressed when the wild menu is displayed
3363 */
3364 int
3365wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3366{
3367 if (xp->xp_context == EXPAND_MENUNAMES)
3368 return wildmenu_process_key_menunames(cclp, key, xp);
3369 else if ((xp->xp_context == EXPAND_FILES
3370 || xp->xp_context == EXPAND_DIRECTORIES
3371 || xp->xp_context == EXPAND_SHELLCMD))
3372 return wildmenu_process_key_filenames(cclp, key, xp);
3373
3374 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003375}
3376
3377/*
3378 * Free expanded names when finished walking through the matches
3379 */
3380 void
3381wildmenu_cleanup(cmdline_info_T *cclp)
3382{
3383 int skt = KeyTyped;
3384 int old_RedrawingDisabled = RedrawingDisabled;
3385
3386 if (!p_wmnu || wild_menu_showing == 0)
3387 return;
3388
3389 if (cclp->input_fn)
3390 RedrawingDisabled = 0;
3391
3392 if (wild_menu_showing == WM_SCROLLED)
3393 {
3394 // Entered command line, move it up
3395 cmdline_row--;
3396 redrawcmd();
3397 }
3398 else if (save_p_ls != -1)
3399 {
3400 // restore 'laststatus' and 'winminheight'
3401 p_ls = save_p_ls;
3402 p_wmh = save_p_wmh;
3403 last_status(FALSE);
3404 update_screen(VALID); // redraw the screen NOW
3405 redrawcmd();
3406 save_p_ls = -1;
3407 }
3408 else
3409 {
3410 win_redraw_last_status(topframe);
3411 redraw_statuslines();
3412 }
3413 KeyTyped = skt;
3414 wild_menu_showing = 0;
3415 if (cclp->input_fn)
3416 RedrawingDisabled = old_RedrawingDisabled;
3417}
3418#endif
3419
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003420#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003421/*
3422 * "getcompletion()" function
3423 */
3424 void
3425f_getcompletion(typval_T *argvars, typval_T *rettv)
3426{
3427 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003428 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003429 expand_T xpc;
3430 int filtered = FALSE;
3431 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003432 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003433
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003434 if (in_vim9script()
3435 && (check_for_string_arg(argvars, 0) == FAIL
3436 || check_for_string_arg(argvars, 1) == FAIL
3437 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3438 return;
3439
ii144785fe02021-11-21 12:13:56 +00003440 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003441 if (argvars[1].v_type != VAR_STRING)
3442 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003443 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003444 return;
3445 }
3446 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003447
3448 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003449 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003450
3451 if (p_wic)
3452 options |= WILD_ICASE;
3453
3454 // For filtered results, 'wildignore' is used
3455 if (!filtered)
3456 options |= WILD_KEEP_ALL;
3457
3458 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003459 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003460 {
ii144785fe02021-11-21 12:13:56 +00003461 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003462 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003463 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003464 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003465 else
3466 {
ii144785fe02021-11-21 12:13:56 +00003467 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003468 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3469
3470 xpc.xp_context = cmdcomplete_str_to_type(type);
3471 if (xpc.xp_context == EXPAND_NOTHING)
3472 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003473 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003474 return;
3475 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003476
3477# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003478 if (xpc.xp_context == EXPAND_MENUS)
3479 {
3480 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3481 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3482 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003483# endif
3484# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003485 if (xpc.xp_context == EXPAND_CSCOPE)
3486 {
3487 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3488 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3489 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003490# endif
3491# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003492 if (xpc.xp_context == EXPAND_SIGN)
3493 {
3494 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3495 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3496 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003497# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003498 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003499
3500 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3501 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3502 {
3503 int i;
3504
3505 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3506
3507 for (i = 0; i < xpc.xp_numfiles; i++)
3508 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3509 }
3510 vim_free(pat);
3511 ExpandCleanup(&xpc);
3512}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003513#endif // FEAT_EVAL