blob: e9db8cfc95e5599c76c6f02877270d0eeedaf23e [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{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000380 int save_p_lz = p_lz;
381
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000382 pum_undisplay();
383 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000384 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000385 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000386 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000387 redrawcmd();
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000388}
389
390void cmdline_pum_cleanup(cmdline_info_T *cclp)
391{
392 cmdline_pum_remove();
393 wildmenu_cleanup(cclp);
394}
395
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000396/*
397 * Returns the starting column number to use for the cmdline completion popup
398 * menu.
399 */
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400int cmdline_compl_startcol(void)
401{
402 return compl_startcol;
403}
404#endif
405
Bram Moolenaar66b51422019-08-18 21:44:12 +0200406/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000407 * Get the next or prev cmdline completion match. The index of the match is set
408 * in 'p_findex'
409 */
410 static char_u *
411get_next_or_prev_match(
412 int mode,
413 expand_T *xp,
414 int *p_findex,
415 char_u *orig_save)
416{
417 int findex = *p_findex;
418
419 if (xp->xp_numfiles <= 0)
420 return NULL;
421
422 if (mode == WILD_PREV)
423 {
424 if (findex == -1)
425 findex = xp->xp_numfiles;
426 --findex;
427 }
428 else // mode == WILD_NEXT
429 ++findex;
430
431 // When wrapping around, return the original string, set findex to
432 // -1.
433 if (findex < 0)
434 {
435 if (orig_save == NULL)
436 findex = xp->xp_numfiles - 1;
437 else
438 findex = -1;
439 }
440 if (findex >= xp->xp_numfiles)
441 {
442 if (orig_save == NULL)
443 findex = 0;
444 else
445 findex = -1;
446 }
447#ifdef FEAT_WILDMENU
448 if (compl_match_array)
449 {
450 compl_selected = findex;
451 cmdline_pum_display();
452 }
453 else if (p_wmnu)
454 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
455 findex, cmd_showtail);
456#endif
457 *p_findex = findex;
458
459 if (findex == -1)
460 return vim_strsave(orig_save);
461
462 return vim_strsave(xp->xp_files[findex]);
463}
464
465/*
466 * Start the command-line expansion and get the matches.
467 */
468 static char_u *
469ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
470{
471 int non_suf_match; // number without matching suffix
472 int i;
473 char_u *ss = NULL;
474
475 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000476 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000477 options) == FAIL)
478 {
479#ifdef FNAME_ILLEGAL
480 // Illegal file name has been silently skipped. But when there
481 // are wildcards, the real problem is that there was no match,
482 // causing the pattern to be added, which has illegal characters.
483 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
484 semsg(_(e_no_match_str_2), str);
485#endif
486 }
487 else if (xp->xp_numfiles == 0)
488 {
489 if (!(options & WILD_SILENT))
490 semsg(_(e_no_match_str_2), str);
491 }
492 else
493 {
494 // Escape the matches for use on the command line.
495 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
496
497 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000498 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000499 {
500 if (xp->xp_numfiles)
501 non_suf_match = xp->xp_numfiles;
502 else
503 non_suf_match = 1;
504 if ((xp->xp_context == EXPAND_FILES
505 || xp->xp_context == EXPAND_DIRECTORIES)
506 && xp->xp_numfiles > 1)
507 {
508 // More than one match; check suffix.
509 // The files will have been sorted on matching suffix in
510 // expand_wildcards, only need to check the first two.
511 non_suf_match = 0;
512 for (i = 0; i < 2; ++i)
513 if (match_suffix(xp->xp_files[i]))
514 ++non_suf_match;
515 }
516 if (non_suf_match != 1)
517 {
518 // Can we ever get here unless it's while expanding
519 // interactively? If not, we can get rid of this all
520 // together. Don't really want to wait for this message
521 // (and possibly have to hit return to continue!).
522 if (!(options & WILD_SILENT))
523 emsg(_(e_too_many_file_names));
524 else if (!(options & WILD_NO_BEEP))
525 beep_flush();
526 }
527 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
528 ss = vim_strsave(xp->xp_files[0]);
529 }
530 }
531
532 return ss;
533}
534
535/*
536 * Return the longest common part in the list of cmdline completion matches.
537 */
538 static char_u *
539find_longest_match(expand_T *xp, int options)
540{
541 long_u len;
542 int mb_len = 1;
543 int c0, ci;
544 int i;
545 char_u *ss;
546
547 for (len = 0; xp->xp_files[0][len]; len += mb_len)
548 {
549 if (has_mbyte)
550 {
551 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
552 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
553 }
554 else
555 c0 = xp->xp_files[0][len];
556 for (i = 1; i < xp->xp_numfiles; ++i)
557 {
558 if (has_mbyte)
559 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
560 else
561 ci = xp->xp_files[i][len];
562 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
563 || xp->xp_context == EXPAND_FILES
564 || xp->xp_context == EXPAND_SHELLCMD
565 || xp->xp_context == EXPAND_BUFFERS))
566 {
567 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
568 break;
569 }
570 else if (c0 != ci)
571 break;
572 }
573 if (i < xp->xp_numfiles)
574 {
575 if (!(options & WILD_NO_BEEP))
576 vim_beep(BO_WILD);
577 break;
578 }
579 }
580
581 ss = alloc(len + 1);
582 if (ss)
583 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
584
585 return ss;
586}
587
588/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200589 * Do wildcard expansion on the string 'str'.
590 * Chars that should not be expanded must be preceded with a backslash.
591 * Return a pointer to allocated memory containing the new string.
592 * Return NULL for failure.
593 *
594 * "orig" is the originally expanded string, copied to allocated memory. It
595 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
596 * WILD_PREV "orig" should be NULL.
597 *
598 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
599 * is WILD_EXPAND_FREE or WILD_ALL.
600 *
601 * mode = WILD_FREE: just free previously expanded matches
602 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
603 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
604 * mode = WILD_NEXT: use next match in multiple match, wrap to first
605 * mode = WILD_PREV: use previous match in multiple match, wrap to first
606 * mode = WILD_ALL: return all matches concatenated
607 * mode = WILD_LONGEST: return longest matched part
608 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000609 * mode = WILD_APPLY: apply the item selected in the cmdline completion
610 * popup menu and close the menu.
611 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
612 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200613 *
614 * options = WILD_LIST_NOTFOUND: list entries without a match
615 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
616 * options = WILD_USE_NL: Use '\n' for WILD_ALL
617 * options = WILD_NO_BEEP: Don't beep for multiple matches
618 * options = WILD_ADD_SLASH: add a slash after directory names
619 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
620 * options = WILD_SILENT: don't print warning messages
621 * options = WILD_ESCAPE: put backslash before special chars
622 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200623 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200624 *
625 * The variables xp->xp_context and xp->xp_backslash must have been set!
626 */
627 char_u *
628ExpandOne(
629 expand_T *xp,
630 char_u *str,
631 char_u *orig, // allocated copy of original of expanded string
632 int options,
633 int mode)
634{
635 char_u *ss = NULL;
636 static int findex;
637 static char_u *orig_save = NULL; // kept value of orig
638 int orig_saved = FALSE;
639 int i;
640 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200641
642 // first handle the case of using an old match
643 if (mode == WILD_NEXT || mode == WILD_PREV)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000644 return get_next_or_prev_match(mode, xp, &findex, orig_save);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200645
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000646 if (mode == WILD_CANCEL)
647 ss = vim_strsave(orig_save ? orig_save : (char_u *)"");
648 else if (mode == WILD_APPLY)
649 ss = vim_strsave(findex == -1 ? (orig_save ?
650 orig_save : (char_u *)"") : xp->xp_files[findex]);
651
Bram Moolenaar66b51422019-08-18 21:44:12 +0200652 // free old names
653 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
654 {
655 FreeWild(xp->xp_numfiles, xp->xp_files);
656 xp->xp_numfiles = -1;
657 VIM_CLEAR(orig_save);
658 }
659 findex = 0;
660
661 if (mode == WILD_FREE) // only release file name
662 return NULL;
663
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000664 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200665 {
666 vim_free(orig_save);
667 orig_save = orig;
668 orig_saved = TRUE;
669
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000670 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200671 }
672
673 // Find longest common part
674 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
675 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000676 ss = find_longest_match(xp, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200677 findex = -1; // next p_wc gets first one
678 }
679
680 // Concatenate all matching names
681 if (mode == WILD_ALL && xp->xp_numfiles > 0)
682 {
683 len = 0;
684 for (i = 0; i < xp->xp_numfiles; ++i)
685 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
686 ss = alloc(len);
687 if (ss != NULL)
688 {
689 *ss = NUL;
690 for (i = 0; i < xp->xp_numfiles; ++i)
691 {
692 STRCAT(ss, xp->xp_files[i]);
693 if (i != xp->xp_numfiles - 1)
694 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
695 }
696 }
697 }
698
699 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
700 ExpandCleanup(xp);
701
702 // Free "orig" if it wasn't stored in "orig_save".
703 if (!orig_saved)
704 vim_free(orig);
705
706 return ss;
707}
708
709/*
710 * Prepare an expand structure for use.
711 */
712 void
713ExpandInit(expand_T *xp)
714{
Bram Moolenaarc841aff2020-07-25 14:11:55 +0200715 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200716 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200717 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200718}
719
720/*
721 * Cleanup an expand structure after use.
722 */
723 void
724ExpandCleanup(expand_T *xp)
725{
726 if (xp->xp_numfiles >= 0)
727 {
728 FreeWild(xp->xp_numfiles, xp->xp_files);
729 xp->xp_numfiles = -1;
730 }
731}
732
733/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000734 * Display one line of completion matches. Multiple matches are displayed in
735 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000736 * matches - list of completion match names
737 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000738 * lines - number of output lines
739 * linenr - line number of matches to display
740 * maxlen - maximum number of characters in each line
741 * showtail - display only the tail of the full path of a file name
742 * dir_attr - highlight attribute to use for directory names
743 */
744 static void
745showmatches_oneline(
746 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000747 char_u **matches,
748 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000749 int lines,
750 int linenr,
751 int maxlen,
752 int showtail,
753 int dir_attr)
754{
755 int i, j;
756 int isdir;
757 int lastlen;
758 char_u *p;
759
760 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000761 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000762 {
763 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
764 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000765 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
766 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000767 msg_advance(maxlen + 1);
768 msg_puts((char *)p);
769 msg_advance(maxlen + 3);
770 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
771 break;
772 }
773 for (i = maxlen - lastlen; --i >= 0; )
774 msg_putchar(' ');
775 if (xp->xp_context == EXPAND_FILES
776 || xp->xp_context == EXPAND_SHELLCMD
777 || xp->xp_context == EXPAND_BUFFERS)
778 {
779 // highlight directories
780 if (xp->xp_numfiles != -1)
781 {
782 char_u *halved_slash;
783 char_u *exp_path;
784 char_u *path;
785
786 // Expansion was done before and special characters
787 // were escaped, need to halve backslashes. Also
788 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000789 exp_path = expand_env_save_opt(matches[j], TRUE);
790 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000791 halved_slash = backslash_halve_save(path);
792 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000793 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000794 vim_free(exp_path);
795 if (halved_slash != path)
796 vim_free(halved_slash);
797 }
798 else
799 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000800 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000801 if (showtail)
802 p = SHOW_FILE_TEXT(j);
803 else
804 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000805 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000806 TRUE);
807 p = NameBuff;
808 }
809 }
810 else
811 {
812 isdir = FALSE;
813 p = SHOW_FILE_TEXT(j);
814 }
815 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
816 }
817 if (msg_col > 0) // when not wrapped around
818 {
819 msg_clr_eos();
820 msg_putchar('\n');
821 }
822 out_flush(); // show one line at a time
823}
824
825/*
Bram Moolenaar66b51422019-08-18 21:44:12 +0200826 * Show all matches for completion on the command line.
827 * Returns EXPAND_NOTHING when the character that triggered expansion should
828 * be inserted like a normal character.
829 */
830 int
831showmatches(expand_T *xp, int wildmenu UNUSED)
832{
833 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000834 int numMatches;
835 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000836 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200837 int maxlen;
838 int lines;
839 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200840 int attr;
841 int showtail;
842
843 if (xp->xp_numfiles == -1)
844 {
845 set_expand_context(xp);
846 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000847 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200848 showtail = expand_showtail(xp);
849 if (i != EXPAND_OK)
850 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200851 }
852 else
853 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000854 numMatches = xp->xp_numfiles;
855 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200856 showtail = cmd_showtail;
857 }
858
859#ifdef FEAT_WILDMENU
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000860 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000861 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000862 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000863#endif
864
865#ifdef FEAT_WILDMENU
Bram Moolenaar66b51422019-08-18 21:44:12 +0200866 if (!wildmenu)
867 {
868#endif
869 msg_didany = FALSE; // lines_left will be set
870 msg_start(); // prepare for paging
871 msg_putchar('\n');
872 out_flush();
873 cmdline_row = msg_row;
874 msg_didany = FALSE; // lines_left will be set again
875 msg_start(); // prepare for paging
876#ifdef FEAT_WILDMENU
877 }
878#endif
879
880 if (got_int)
881 got_int = FALSE; // only int. the completion, not the cmd line
882#ifdef FEAT_WILDMENU
883 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000884 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200885#endif
886 else
887 {
888 // find the length of the longest file name
889 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000890 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200891 {
892 if (!showtail && (xp->xp_context == EXPAND_FILES
893 || xp->xp_context == EXPAND_SHELLCMD
894 || xp->xp_context == EXPAND_BUFFERS))
895 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000896 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200897 j = vim_strsize(NameBuff);
898 }
899 else
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000900 j = vim_strsize(SHOW_FILE_TEXT(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +0200901 if (j > maxlen)
902 maxlen = j;
903 }
904
905 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000906 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200907 else
908 {
909 // compute the number of columns and lines for the listing
910 maxlen += 2; // two spaces between file names
911 columns = ((int)Columns + 2) / maxlen;
912 if (columns < 1)
913 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000914 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200915 }
916
917 attr = HL_ATTR(HLF_D); // find out highlighting for directories
918
919 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
920 {
921 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
922 msg_clr_eos();
923 msg_advance(maxlen - 3);
924 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
925 }
926
927 // list the files line by line
928 for (i = 0; i < lines; ++i)
929 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000930 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000931 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200932 if (got_int)
933 {
934 got_int = FALSE;
935 break;
936 }
937 }
938
939 // we redraw the command below the lines that we have just listed
940 // This is a bit tricky, but it saves a lot of screen updating.
941 cmdline_row = msg_row; // will put it back later
942 }
943
944 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000945 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200946
947 return EXPAND_OK;
948}
949
950/*
951 * Private gettail for showmatches() (and win_redr_status_matches()):
952 * Find tail of file name path, but ignore trailing "/".
953 */
954 char_u *
955sm_gettail(char_u *s)
956{
957 char_u *p;
958 char_u *t = s;
959 int had_sep = FALSE;
960
961 for (p = s; *p != NUL; )
962 {
963 if (vim_ispathsep(*p)
964#ifdef BACKSLASH_IN_FILENAME
965 && !rem_backslash(p)
966#endif
967 )
968 had_sep = TRUE;
969 else if (had_sep)
970 {
971 t = p;
972 had_sep = FALSE;
973 }
974 MB_PTR_ADV(p);
975 }
976 return t;
977}
978
979/*
980 * Return TRUE if we only need to show the tail of completion matches.
981 * When not completing file names or there is a wildcard in the path FALSE is
982 * returned.
983 */
984 static int
985expand_showtail(expand_T *xp)
986{
987 char_u *s;
988 char_u *end;
989
990 // When not completing file names a "/" may mean something different.
991 if (xp->xp_context != EXPAND_FILES
992 && xp->xp_context != EXPAND_SHELLCMD
993 && xp->xp_context != EXPAND_DIRECTORIES)
994 return FALSE;
995
996 end = gettail(xp->xp_pattern);
997 if (end == xp->xp_pattern) // there is no path separator
998 return FALSE;
999
1000 for (s = xp->xp_pattern; s < end; s++)
1001 {
1002 // Skip escaped wildcards. Only when the backslash is not a path
1003 // separator, on DOS the '*' "path\*\file" must not be skipped.
1004 if (rem_backslash(s))
1005 ++s;
1006 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1007 return FALSE;
1008 }
1009 return TRUE;
1010}
1011
1012/*
1013 * Prepare a string for expansion.
1014 * When expanding file names: The string will be used with expand_wildcards().
1015 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1016 * When expanding other names: The string will be used with regcomp(). Copy
1017 * the name into allocated memory and prepend "^".
1018 */
1019 char_u *
1020addstar(
1021 char_u *fname,
1022 int len,
1023 int context) // EXPAND_FILES etc.
1024{
1025 char_u *retval;
1026 int i, j;
1027 int new_len;
1028 char_u *tail;
1029 int ends_in_star;
1030
1031 if (context != EXPAND_FILES
1032 && context != EXPAND_FILES_IN_PATH
1033 && context != EXPAND_SHELLCMD
1034 && context != EXPAND_DIRECTORIES)
1035 {
1036 // Matching will be done internally (on something other than files).
1037 // So we convert the file-matching-type wildcards into our kind for
1038 // use with vim_regcomp(). First work out how long it will be:
1039
1040 // For help tags the translation is done in find_help_tags().
1041 // For a tag pattern starting with "/" no translation is needed.
1042 if (context == EXPAND_HELP
1043 || context == EXPAND_COLORS
1044 || context == EXPAND_COMPILER
1045 || context == EXPAND_OWNSYNTAX
1046 || context == EXPAND_FILETYPE
1047 || context == EXPAND_PACKADD
1048 || ((context == EXPAND_TAGS_LISTFILES
1049 || context == EXPAND_TAGS)
1050 && fname[0] == '/'))
1051 retval = vim_strnsave(fname, len);
1052 else
1053 {
1054 new_len = len + 2; // +2 for '^' at start, NUL at end
1055 for (i = 0; i < len; i++)
1056 {
1057 if (fname[i] == '*' || fname[i] == '~')
1058 new_len++; // '*' needs to be replaced by ".*"
1059 // '~' needs to be replaced by "\~"
1060
1061 // Buffer names are like file names. "." should be literal
1062 if (context == EXPAND_BUFFERS && fname[i] == '.')
1063 new_len++; // "." becomes "\."
1064
1065 // Custom expansion takes care of special things, match
1066 // backslashes literally (perhaps also for other types?)
1067 if ((context == EXPAND_USER_DEFINED
1068 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1069 new_len++; // '\' becomes "\\"
1070 }
1071 retval = alloc(new_len);
1072 if (retval != NULL)
1073 {
1074 retval[0] = '^';
1075 j = 1;
1076 for (i = 0; i < len; i++, j++)
1077 {
1078 // Skip backslash. But why? At least keep it for custom
1079 // expansion.
1080 if (context != EXPAND_USER_DEFINED
1081 && context != EXPAND_USER_LIST
1082 && fname[i] == '\\'
1083 && ++i == len)
1084 break;
1085
1086 switch (fname[i])
1087 {
1088 case '*': retval[j++] = '.';
1089 break;
1090 case '~': retval[j++] = '\\';
1091 break;
1092 case '?': retval[j] = '.';
1093 continue;
1094 case '.': if (context == EXPAND_BUFFERS)
1095 retval[j++] = '\\';
1096 break;
1097 case '\\': if (context == EXPAND_USER_DEFINED
1098 || context == EXPAND_USER_LIST)
1099 retval[j++] = '\\';
1100 break;
1101 }
1102 retval[j] = fname[i];
1103 }
1104 retval[j] = NUL;
1105 }
1106 }
1107 }
1108 else
1109 {
1110 retval = alloc(len + 4);
1111 if (retval != NULL)
1112 {
1113 vim_strncpy(retval, fname, len);
1114
1115 // Don't add a star to *, ~, ~user, $var or `cmd`.
1116 // * would become **, which walks the whole tree.
1117 // ~ would be at the start of the file name, but not the tail.
1118 // $ could be anywhere in the tail.
1119 // ` could be anywhere in the file name.
1120 // When the name ends in '$' don't add a star, remove the '$'.
1121 tail = gettail(retval);
1122 ends_in_star = (len > 0 && retval[len - 1] == '*');
1123#ifndef BACKSLASH_IN_FILENAME
1124 for (i = len - 2; i >= 0; --i)
1125 {
1126 if (retval[i] != '\\')
1127 break;
1128 ends_in_star = !ends_in_star;
1129 }
1130#endif
1131 if ((*retval != '~' || tail != retval)
1132 && !ends_in_star
1133 && vim_strchr(tail, '$') == NULL
1134 && vim_strchr(retval, '`') == NULL)
1135 retval[len++] = '*';
1136 else if (len > 0 && retval[len - 1] == '$')
1137 --len;
1138 retval[len] = NUL;
1139 }
1140 }
1141 return retval;
1142}
1143
1144/*
1145 * Must parse the command line so far to work out what context we are in.
1146 * Completion can then be done based on that context.
1147 * This routine sets the variables:
1148 * xp->xp_pattern The start of the pattern to be expanded within
1149 * the command line (ends at the cursor).
1150 * xp->xp_context The type of thing to expand. Will be one of:
1151 *
1152 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1153 * the command line, like an unknown command. Caller
1154 * should beep.
1155 * EXPAND_NOTHING Unrecognised context for completion, use char like
1156 * a normal char, rather than for completion. eg
1157 * :s/^I/
1158 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1159 * it.
1160 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1161 * EXPAND_FILES After command with EX_XFILE set, or after setting
1162 * with P_EXPAND set. eg :e ^I, :w>>^I
1163 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
1164 * when we know only directories are of interest. eg
1165 * :set dir=^I
1166 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1167 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1168 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1169 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1170 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1171 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1172 * EXPAND_EVENTS Complete event names
1173 * EXPAND_SYNTAX Complete :syntax command arguments
1174 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1175 * EXPAND_AUGROUP Complete autocommand group names
1176 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1177 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1178 * eg :unmap a^I , :cunab x^I
1179 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1180 * eg :call sub^I
1181 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1182 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1183 * names in expressions, eg :while s^I
1184 * EXPAND_ENV_VARS Complete environment variable names
1185 * EXPAND_USER Complete user names
1186 */
1187 static void
1188set_expand_context(expand_T *xp)
1189{
1190 cmdline_info_T *ccline = get_cmdline_info();
1191
1192 // only expansion for ':', '>' and '=' command-lines
1193 if (ccline->cmdfirstc != ':'
1194#ifdef FEAT_EVAL
1195 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1196 && !ccline->input_fn
1197#endif
1198 )
1199 {
1200 xp->xp_context = EXPAND_NOTHING;
1201 return;
1202 }
1203 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1204}
1205
Bram Moolenaard0190392019-08-23 21:17:35 +02001206/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001207 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1208 * For user defined commands, the completion context is set in 'xp' and the
1209 * completion flags in 'complp'.
1210 *
1211 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001212 */
1213 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001214set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001215{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001216 char_u *p = NULL;
1217 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001218 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001219
1220 // Isolate the command and search for it in the command table.
1221 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001222 // - the 'k' command can directly be followed by any character, but do
1223 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1224 // find matches anywhere in the command name, do this only for command
1225 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001226 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001227 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001228 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001229 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001230 p = cmd + 1;
1231 }
1232 else
1233 {
1234 p = cmd;
1235 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1236 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001237 // A user command may contain digits.
1238 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1239 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001240 while (ASCII_ISALNUM(*p) || *p == '*')
1241 ++p;
1242 // for python 3.x: ":py3*" commands completion
1243 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1244 {
1245 ++p;
1246 while (ASCII_ISALPHA(*p) || *p == '*')
1247 ++p;
1248 }
1249 // check for non-alpha command
1250 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1251 ++p;
1252 len = (int)(p - cmd);
1253
1254 if (len == 0)
1255 {
1256 xp->xp_context = EXPAND_UNSUCCESSFUL;
1257 return NULL;
1258 }
1259
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001260 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001261
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001262 // User defined commands support alphanumeric characters.
1263 // Also when doing fuzzy expansion, support alphanumeric characters.
1264 if ((cmd[0] >= 'A' && cmd[0] <= 'Z') || (fuzzy && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001265 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1266 ++p;
1267 }
1268
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001269 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001270 // character, complete the command name.
1271 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1272 return NULL;
1273
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001274 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001275 {
1276 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1277 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001278 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001279 p = cmd + 1;
1280 }
1281 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1282 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001283 eap->cmd = cmd;
1284 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001285 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001286 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001287 }
1288 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001289 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001290 {
1291 // Not still touching the command and it was an illegal one
1292 xp->xp_context = EXPAND_UNSUCCESSFUL;
1293 return NULL;
1294 }
1295
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001296 return p;
1297}
Bram Moolenaard0190392019-08-23 21:17:35 +02001298
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001299/*
1300 * Set the completion context for a command argument with wild card characters.
1301 */
1302 static void
1303set_context_for_wildcard_arg(
1304 exarg_T *eap,
1305 char_u *arg,
1306 int usefilter,
1307 expand_T *xp,
1308 int *complp)
1309{
1310 char_u *p;
1311 int c;
1312 int in_quote = FALSE;
1313 char_u *bow = NULL; // Beginning of word
1314 int len = 0;
1315
1316 // Allow spaces within back-quotes to count as part of the argument
1317 // being expanded.
1318 xp->xp_pattern = skipwhite(arg);
1319 p = xp->xp_pattern;
1320 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001321 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001322 if (has_mbyte)
1323 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001324 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001325 c = *p;
1326 if (c == '\\' && p[1] != NUL)
1327 ++p;
1328 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001329 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001330 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001331 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001332 xp->xp_pattern = p;
1333 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001334 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001335 in_quote = !in_quote;
1336 }
1337 // An argument can contain just about everything, except
1338 // characters that end the command and white space.
1339 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001340#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001341 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001342#endif
1343 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001344 {
1345 len = 0; // avoid getting stuck when space is in 'isfname'
1346 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001347 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001348 if (has_mbyte)
1349 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001350 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001351 c = *p;
1352 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001353 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001354 if (has_mbyte)
1355 len = (*mb_ptr2len)(p);
1356 else
1357 len = 1;
1358 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001359 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001360 if (in_quote)
1361 bow = p;
1362 else
1363 xp->xp_pattern = p;
1364 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001365 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001366 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001367 }
1368
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001369 // If we are still inside the quotes, and we passed a space, just
1370 // expand from there.
1371 if (bow != NULL && in_quote)
1372 xp->xp_pattern = bow;
1373 xp->xp_context = EXPAND_FILES;
1374
1375 // For a shell command more chars need to be escaped.
1376 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1377 {
1378#ifndef BACKSLASH_IN_FILENAME
1379 xp->xp_shell = TRUE;
1380#endif
1381 // When still after the command name expand executables.
1382 if (xp->xp_pattern == skipwhite(arg))
1383 xp->xp_context = EXPAND_SHELLCMD;
1384 }
1385
1386 // Check for environment variable.
1387 if (*xp->xp_pattern == '$')
1388 {
1389 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1390 if (!vim_isIDc(*p))
1391 break;
1392 if (*p == NUL)
1393 {
1394 xp->xp_context = EXPAND_ENV_VARS;
1395 ++xp->xp_pattern;
1396 // Avoid that the assignment uses EXPAND_FILES again.
1397 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1398 *complp = EXPAND_ENV_VARS;
1399 }
1400 }
1401 // Check for user names.
1402 if (*xp->xp_pattern == '~')
1403 {
1404 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1405 ;
1406 // Complete ~user only if it partially matches a user name.
1407 // A full match ~user<Tab> will be replaced by user's home
1408 // directory i.e. something like ~user<Tab> -> /home/user/
1409 if (*p == NUL && p > xp->xp_pattern + 1
1410 && match_user(xp->xp_pattern + 1) >= 1)
1411 {
1412 xp->xp_context = EXPAND_USER;
1413 ++xp->xp_pattern;
1414 }
1415 }
1416}
1417
1418/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001419 * Set the completion context for the :filter command. Returns a pointer to the
1420 * next command after the :filter command.
1421 */
1422 static char_u *
1423set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1424{
1425 if (*arg != NUL)
1426 arg = skip_vimgrep_pat(arg, NULL, NULL);
1427 if (arg == NULL || *arg == NUL)
1428 {
1429 xp->xp_context = EXPAND_NOTHING;
1430 return NULL;
1431 }
1432 return skipwhite(arg);
1433}
1434
1435#ifdef FEAT_SEARCH_EXTRA
1436/*
1437 * Set the completion context for the :match command. Returns a pointer to the
1438 * next command after the :match command.
1439 */
1440 static char_u *
1441set_context_in_match_cmd(expand_T *xp, char_u *arg)
1442{
1443 if (*arg == NUL || !ends_excmd(*arg))
1444 {
1445 // also complete "None"
1446 set_context_in_echohl_cmd(xp, arg);
1447 arg = skipwhite(skiptowhite(arg));
1448 if (*arg != NUL)
1449 {
1450 xp->xp_context = EXPAND_NOTHING;
1451 arg = skip_regexp(arg + 1, *arg, magic_isset());
1452 }
1453 }
1454 return find_nextcmd(arg);
1455}
1456#endif
1457
1458/*
1459 * Returns a pointer to the next command after a :global or a :v command.
1460 * Returns NULL if there is no next command.
1461 */
1462 static char_u *
1463find_cmd_after_global_cmd(char_u *arg)
1464{
1465 int delim;
1466
1467 delim = *arg; // get the delimiter
1468 if (delim)
1469 ++arg; // skip delimiter if there is one
1470
1471 while (arg[0] != NUL && arg[0] != delim)
1472 {
1473 if (arg[0] == '\\' && arg[1] != NUL)
1474 ++arg;
1475 ++arg;
1476 }
1477 if (arg[0] != NUL)
1478 return arg + 1;
1479
1480 return NULL;
1481}
1482
1483/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001484 * Returns a pointer to the next command after a :substitute or a :& command.
1485 * Returns NULL if there is no next command.
1486 */
1487 static char_u *
1488find_cmd_after_substitute_cmd(char_u *arg)
1489{
1490 int delim;
1491
1492 delim = *arg;
1493 if (delim)
1494 {
1495 // skip "from" part
1496 ++arg;
1497 arg = skip_regexp(arg, delim, magic_isset());
1498
1499 if (arg[0] != NUL && arg[0] == delim)
1500 {
1501 // skip "to" part
1502 ++arg;
1503 while (arg[0] != NUL && arg[0] != delim)
1504 {
1505 if (arg[0] == '\\' && arg[1] != NUL)
1506 ++arg;
1507 ++arg;
1508 }
1509 if (arg[0] != NUL) // skip delimiter
1510 ++arg;
1511 }
1512 }
1513 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1514 ++arg;
1515 if (arg[0] != NUL)
1516 return arg;
1517
1518 return NULL;
1519}
1520
1521/*
1522 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1523 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1524 * Returns NULL if there is no next command.
1525 */
1526 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001527find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001528{
1529 arg = skipwhite(skipdigits(arg)); // skip count
1530 if (*arg == '/') // Match regexp, not just whole words
1531 {
1532 for (++arg; *arg && *arg != '/'; arg++)
1533 if (*arg == '\\' && arg[1] != NUL)
1534 arg++;
1535 if (*arg)
1536 {
1537 arg = skipwhite(arg + 1);
1538
1539 // Check for trailing illegal characters
1540 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1541 xp->xp_context = EXPAND_NOTHING;
1542 else
1543 return arg;
1544 }
1545 }
1546
1547 return NULL;
1548}
1549
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001550#ifdef FEAT_EVAL
1551/*
1552 * Set the completion context for the :unlet command. Always returns NULL.
1553 */
1554 static char_u *
1555set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1556{
1557 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1558 arg = xp->xp_pattern + 1;
1559
1560 xp->xp_context = EXPAND_USER_VARS;
1561 xp->xp_pattern = arg;
1562
1563 if (*xp->xp_pattern == '$')
1564 {
1565 xp->xp_context = EXPAND_ENV_VARS;
1566 ++xp->xp_pattern;
1567 }
1568
1569 return NULL;
1570}
1571#endif
1572
1573#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1574/*
1575 * Set the completion context for the :language command. Always returns NULL.
1576 */
1577 static char_u *
1578set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1579{
1580 char_u *p;
1581
1582 p = skiptowhite(arg);
1583 if (*p == NUL)
1584 {
1585 xp->xp_context = EXPAND_LANGUAGE;
1586 xp->xp_pattern = arg;
1587 }
1588 else
1589 {
1590 if ( STRNCMP(arg, "messages", p - arg) == 0
1591 || STRNCMP(arg, "ctype", p - arg) == 0
1592 || STRNCMP(arg, "time", p - arg) == 0
1593 || STRNCMP(arg, "collate", p - arg) == 0)
1594 {
1595 xp->xp_context = EXPAND_LOCALES;
1596 xp->xp_pattern = skipwhite(p);
1597 }
1598 else
1599 xp->xp_context = EXPAND_NOTHING;
1600 }
1601
1602 return NULL;
1603}
1604#endif
1605
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001606/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001607 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
1608 * The argument to the command is 'arg' and the argument flags is 'argt'.
1609 * For user-defined commands and for environment variables, 'compl' has the
1610 * completion type.
1611 * Returns a pointer to the next command. Returns NULL if there is no next
1612 * command.
1613 */
1614 static char_u *
1615set_context_by_cmdname(
1616 char_u *cmd,
1617 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001618 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001619 char_u *arg,
1620 long argt,
1621 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001622 int forceit)
1623{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001624 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02001625 {
1626 case CMD_find:
1627 case CMD_sfind:
1628 case CMD_tabfind:
1629 if (xp->xp_context == EXPAND_FILES)
1630 xp->xp_context = EXPAND_FILES_IN_PATH;
1631 break;
1632 case CMD_cd:
1633 case CMD_chdir:
1634 case CMD_tcd:
1635 case CMD_tchdir:
1636 case CMD_lcd:
1637 case CMD_lchdir:
1638 if (xp->xp_context == EXPAND_FILES)
1639 xp->xp_context = EXPAND_DIRECTORIES;
1640 break;
1641 case CMD_help:
1642 xp->xp_context = EXPAND_HELP;
1643 xp->xp_pattern = arg;
1644 break;
1645
1646 // Command modifiers: return the argument.
1647 // Also for commands with an argument that is a command.
1648 case CMD_aboveleft:
1649 case CMD_argdo:
1650 case CMD_belowright:
1651 case CMD_botright:
1652 case CMD_browse:
1653 case CMD_bufdo:
1654 case CMD_cdo:
1655 case CMD_cfdo:
1656 case CMD_confirm:
1657 case CMD_debug:
1658 case CMD_folddoclosed:
1659 case CMD_folddoopen:
1660 case CMD_hide:
1661 case CMD_keepalt:
1662 case CMD_keepjumps:
1663 case CMD_keepmarks:
1664 case CMD_keeppatterns:
1665 case CMD_ldo:
1666 case CMD_leftabove:
1667 case CMD_lfdo:
1668 case CMD_lockmarks:
1669 case CMD_noautocmd:
1670 case CMD_noswapfile:
1671 case CMD_rightbelow:
1672 case CMD_sandbox:
1673 case CMD_silent:
1674 case CMD_tab:
1675 case CMD_tabdo:
1676 case CMD_topleft:
1677 case CMD_verbose:
1678 case CMD_vertical:
1679 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02001680 case CMD_vim9cmd:
1681 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02001682 return arg;
1683
1684 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001685 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001686
1687#ifdef FEAT_SEARCH_EXTRA
1688 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001689 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001690#endif
1691
1692 // All completion for the +cmdline_compl feature goes here.
1693
1694 case CMD_command:
1695 return set_context_in_user_cmd(xp, arg);
1696
1697 case CMD_delcommand:
1698 xp->xp_context = EXPAND_USER_COMMANDS;
1699 xp->xp_pattern = arg;
1700 break;
1701
1702 case CMD_global:
1703 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001704 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001705 case CMD_and:
1706 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001707 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001708 case CMD_isearch:
1709 case CMD_dsearch:
1710 case CMD_ilist:
1711 case CMD_dlist:
1712 case CMD_ijump:
1713 case CMD_psearch:
1714 case CMD_djump:
1715 case CMD_isplit:
1716 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001717 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001718 case CMD_autocmd:
1719 return set_context_in_autocmd(xp, arg, FALSE);
1720 case CMD_doautocmd:
1721 case CMD_doautoall:
1722 return set_context_in_autocmd(xp, arg, TRUE);
1723 case CMD_set:
1724 set_context_in_set_cmd(xp, arg, 0);
1725 break;
1726 case CMD_setglobal:
1727 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1728 break;
1729 case CMD_setlocal:
1730 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1731 break;
1732 case CMD_tag:
1733 case CMD_stag:
1734 case CMD_ptag:
1735 case CMD_ltag:
1736 case CMD_tselect:
1737 case CMD_stselect:
1738 case CMD_ptselect:
1739 case CMD_tjump:
1740 case CMD_stjump:
1741 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001742 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001743 xp->xp_context = EXPAND_TAGS_LISTFILES;
1744 else
1745 xp->xp_context = EXPAND_TAGS;
1746 xp->xp_pattern = arg;
1747 break;
1748 case CMD_augroup:
1749 xp->xp_context = EXPAND_AUGROUP;
1750 xp->xp_pattern = arg;
1751 break;
1752#ifdef FEAT_SYN_HL
1753 case CMD_syntax:
1754 set_context_in_syntax_cmd(xp, arg);
1755 break;
1756#endif
1757#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001758 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001759 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02001760 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001761 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02001762 case CMD_if:
1763 case CMD_elseif:
1764 case CMD_while:
1765 case CMD_for:
1766 case CMD_echo:
1767 case CMD_echon:
1768 case CMD_execute:
1769 case CMD_echomsg:
1770 case CMD_echoerr:
1771 case CMD_call:
1772 case CMD_return:
1773 case CMD_cexpr:
1774 case CMD_caddexpr:
1775 case CMD_cgetexpr:
1776 case CMD_lexpr:
1777 case CMD_laddexpr:
1778 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001779 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001780 break;
1781
1782 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001783 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001784 case CMD_function:
1785 case CMD_delfunction:
1786 xp->xp_context = EXPAND_USER_FUNC;
1787 xp->xp_pattern = arg;
1788 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02001789 case CMD_disassemble:
1790 set_context_in_disassemble_cmd(xp, arg);
1791 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02001792
1793 case CMD_echohl:
1794 set_context_in_echohl_cmd(xp, arg);
1795 break;
1796#endif
1797 case CMD_highlight:
1798 set_context_in_highlight_cmd(xp, arg);
1799 break;
1800#ifdef FEAT_CSCOPE
1801 case CMD_cscope:
1802 case CMD_lcscope:
1803 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001804 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001805 break;
1806#endif
1807#ifdef FEAT_SIGNS
1808 case CMD_sign:
1809 set_context_in_sign_cmd(xp, arg);
1810 break;
1811#endif
1812 case CMD_bdelete:
1813 case CMD_bwipeout:
1814 case CMD_bunload:
1815 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1816 arg = xp->xp_pattern + 1;
1817 // FALLTHROUGH
1818 case CMD_buffer:
1819 case CMD_sbuffer:
1820 case CMD_checktime:
1821 xp->xp_context = EXPAND_BUFFERS;
1822 xp->xp_pattern = arg;
1823 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01001824#ifdef FEAT_DIFF
1825 case CMD_diffget:
1826 case CMD_diffput:
1827 // If current buffer is in diff mode, complete buffer names
1828 // which are in diff mode, and different than current buffer.
1829 xp->xp_context = EXPAND_DIFF_BUFFERS;
1830 xp->xp_pattern = arg;
1831 break;
1832#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02001833 case CMD_USER:
1834 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001835 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
1836 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02001837
1838 case CMD_map: case CMD_noremap:
1839 case CMD_nmap: case CMD_nnoremap:
1840 case CMD_vmap: case CMD_vnoremap:
1841 case CMD_omap: case CMD_onoremap:
1842 case CMD_imap: case CMD_inoremap:
1843 case CMD_cmap: case CMD_cnoremap:
1844 case CMD_lmap: case CMD_lnoremap:
1845 case CMD_smap: case CMD_snoremap:
1846 case CMD_tmap: case CMD_tnoremap:
1847 case CMD_xmap: case CMD_xnoremap:
1848 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001849 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001850 case CMD_unmap:
1851 case CMD_nunmap:
1852 case CMD_vunmap:
1853 case CMD_ounmap:
1854 case CMD_iunmap:
1855 case CMD_cunmap:
1856 case CMD_lunmap:
1857 case CMD_sunmap:
1858 case CMD_tunmap:
1859 case CMD_xunmap:
1860 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001861 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001862 case CMD_mapclear:
1863 case CMD_nmapclear:
1864 case CMD_vmapclear:
1865 case CMD_omapclear:
1866 case CMD_imapclear:
1867 case CMD_cmapclear:
1868 case CMD_lmapclear:
1869 case CMD_smapclear:
1870 case CMD_tmapclear:
1871 case CMD_xmapclear:
1872 xp->xp_context = EXPAND_MAPCLEAR;
1873 xp->xp_pattern = arg;
1874 break;
1875
1876 case CMD_abbreviate: case CMD_noreabbrev:
1877 case CMD_cabbrev: case CMD_cnoreabbrev:
1878 case CMD_iabbrev: case CMD_inoreabbrev:
1879 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001880 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001881 case CMD_unabbreviate:
1882 case CMD_cunabbrev:
1883 case CMD_iunabbrev:
1884 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001885 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02001886#ifdef FEAT_MENU
1887 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
1888 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
1889 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
1890 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
1891 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
1892 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
1893 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
1894 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
1895 case CMD_tmenu: case CMD_tunmenu:
1896 case CMD_popup: case CMD_tearoff: case CMD_emenu:
1897 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1898#endif
1899
1900 case CMD_colorscheme:
1901 xp->xp_context = EXPAND_COLORS;
1902 xp->xp_pattern = arg;
1903 break;
1904
1905 case CMD_compiler:
1906 xp->xp_context = EXPAND_COMPILER;
1907 xp->xp_pattern = arg;
1908 break;
1909
1910 case CMD_ownsyntax:
1911 xp->xp_context = EXPAND_OWNSYNTAX;
1912 xp->xp_pattern = arg;
1913 break;
1914
1915 case CMD_setfiletype:
1916 xp->xp_context = EXPAND_FILETYPE;
1917 xp->xp_pattern = arg;
1918 break;
1919
1920 case CMD_packadd:
1921 xp->xp_context = EXPAND_PACKADD;
1922 xp->xp_pattern = arg;
1923 break;
1924
1925#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1926 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001927 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02001928#endif
1929#if defined(FEAT_PROFILE)
1930 case CMD_profile:
1931 set_context_in_profile_cmd(xp, arg);
1932 break;
1933#endif
1934 case CMD_behave:
1935 xp->xp_context = EXPAND_BEHAVE;
1936 xp->xp_pattern = arg;
1937 break;
1938
1939 case CMD_messages:
1940 xp->xp_context = EXPAND_MESSAGES;
1941 xp->xp_pattern = arg;
1942 break;
1943
1944 case CMD_history:
1945 xp->xp_context = EXPAND_HISTORY;
1946 xp->xp_pattern = arg;
1947 break;
1948#if defined(FEAT_PROFILE)
1949 case CMD_syntime:
1950 xp->xp_context = EXPAND_SYNTIME;
1951 xp->xp_pattern = arg;
1952 break;
1953#endif
1954
1955 case CMD_argdelete:
1956 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1957 arg = xp->xp_pattern + 1;
1958 xp->xp_context = EXPAND_ARGLIST;
1959 xp->xp_pattern = arg;
1960 break;
1961
1962 default:
1963 break;
1964 }
1965 return NULL;
1966}
1967
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001968/*
1969 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
1970 * we don't need/want deleted. Maybe this could be done better if we didn't
1971 * repeat all this stuff. The only problem is that they may not stay
1972 * perfectly compatible with each other, but then the command line syntax
1973 * probably won't change that much -- webb.
1974 */
1975 static char_u *
1976set_one_cmd_context(
1977 expand_T *xp,
1978 char_u *buff) // buffer for command string
1979{
1980 char_u *p;
1981 char_u *cmd, *arg;
1982 int len = 0;
1983 exarg_T ea;
1984 int compl = EXPAND_NOTHING;
1985 int forceit = FALSE;
1986 int usefilter = FALSE; // filter instead of file name
1987
1988 ExpandInit(xp);
1989 xp->xp_pattern = buff;
1990 xp->xp_line = buff;
1991 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
1992 ea.argt = 0;
1993
1994 // 1. skip comment lines and leading space, colons or bars
1995 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
1996 ;
1997 xp->xp_pattern = cmd;
1998
1999 if (*cmd == NUL)
2000 return NULL;
2001 if (*cmd == '"') // ignore comment lines
2002 {
2003 xp->xp_context = EXPAND_NOTHING;
2004 return NULL;
2005 }
2006
2007 // 3. Skip over the range to find the command.
2008 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2009 xp->xp_pattern = cmd;
2010 if (*cmd == NUL)
2011 return NULL;
2012 if (*cmd == '"')
2013 {
2014 xp->xp_context = EXPAND_NOTHING;
2015 return NULL;
2016 }
2017
2018 if (*cmd == '|' || *cmd == '\n')
2019 return cmd + 1; // There's another command
2020
2021 // Get the command index.
2022 p = set_cmd_index(cmd, &ea, xp, &compl);
2023 if (p == NULL)
2024 return NULL;
2025
2026 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2027
2028 if (*p == '!') // forced commands
2029 {
2030 forceit = TRUE;
2031 ++p;
2032 }
2033
2034 // 6. parse arguments
2035 if (!IS_USER_CMDIDX(ea.cmdidx))
2036 ea.argt = excmd_get_argt(ea.cmdidx);
2037
2038 arg = skipwhite(p);
2039
2040 // Skip over ++argopt argument
2041 if ((ea.argt & EX_ARGOPT) && *arg != NUL && STRNCMP(arg, "++", 2) == 0)
2042 {
2043 p = arg;
2044 while (*p && !vim_isspace(*p))
2045 MB_PTR_ADV(p);
2046 arg = skipwhite(p);
2047 }
2048
2049 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2050 {
2051 if (*arg == '>') // append
2052 {
2053 if (*++arg == '>')
2054 ++arg;
2055 arg = skipwhite(arg);
2056 }
2057 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2058 {
2059 ++arg;
2060 usefilter = TRUE;
2061 }
2062 }
2063
2064 if (ea.cmdidx == CMD_read)
2065 {
2066 usefilter = forceit; // :r! filter if forced
2067 if (*arg == '!') // :r !filter
2068 {
2069 ++arg;
2070 usefilter = TRUE;
2071 }
2072 }
2073
2074 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2075 {
2076 while (*arg == *cmd) // allow any number of '>' or '<'
2077 ++arg;
2078 arg = skipwhite(arg);
2079 }
2080
2081 // Does command allow "+command"?
2082 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2083 {
2084 // Check if we're in the +command
2085 p = arg + 1;
2086 arg = skip_cmd_arg(arg, FALSE);
2087
2088 // Still touching the command after '+'?
2089 if (*arg == NUL)
2090 return p;
2091
2092 // Skip space(s) after +command to get to the real argument
2093 arg = skipwhite(arg);
2094 }
2095
2096
2097 // Check for '|' to separate commands and '"' to start comments.
2098 // Don't do this for ":read !cmd" and ":write !cmd".
2099 if ((ea.argt & EX_TRLBAR) && !usefilter)
2100 {
2101 p = arg;
2102 // ":redir @" is not the start of a comment
2103 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2104 p += 2;
2105 while (*p)
2106 {
2107 if (*p == Ctrl_V)
2108 {
2109 if (p[1] != NUL)
2110 ++p;
2111 }
2112 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2113 || *p == '|' || *p == '\n')
2114 {
2115 if (*(p - 1) != '\\')
2116 {
2117 if (*p == '|' || *p == '\n')
2118 return p + 1;
2119 return NULL; // It's a comment
2120 }
2121 }
2122 MB_PTR_ADV(p);
2123 }
2124 }
2125
2126 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2127 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2128 // no arguments allowed but there is something
2129 return NULL;
2130
2131 // Find start of last argument (argument just before cursor):
2132 p = buff;
2133 xp->xp_pattern = p;
2134 len = (int)STRLEN(buff);
2135 while (*p && p < buff + len)
2136 {
2137 if (*p == ' ' || *p == TAB)
2138 {
2139 // argument starts after a space
2140 xp->xp_pattern = ++p;
2141 }
2142 else
2143 {
2144 if (*p == '\\' && *(p + 1) != NUL)
2145 ++p; // skip over escaped character
2146 MB_PTR_ADV(p);
2147 }
2148 }
2149
2150 if (ea.argt & EX_XFILE)
2151 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2152
2153 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002154 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002155 forceit);
2156}
2157
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002158/*
2159 * Set the completion context in 'xp' for command 'str'
2160 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002161 void
2162set_cmd_context(
2163 expand_T *xp,
2164 char_u *str, // start of command line
2165 int len, // length of command line (excl. NUL)
2166 int col, // position of cursor
2167 int use_ccline UNUSED) // use ccline for info
2168{
2169#ifdef FEAT_EVAL
2170 cmdline_info_T *ccline = get_cmdline_info();
2171#endif
2172 int old_char = NUL;
2173 char_u *nextcomm;
2174
2175 // Avoid a UMR warning from Purify, only save the character if it has been
2176 // written before.
2177 if (col < len)
2178 old_char = str[col];
2179 str[col] = NUL;
2180 nextcomm = str;
2181
2182#ifdef FEAT_EVAL
2183 if (use_ccline && ccline->cmdfirstc == '=')
2184 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002185 // pass CMD_SIZE because there is no real command
2186 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002187 }
2188 else if (use_ccline && ccline->input_fn)
2189 {
2190 xp->xp_context = ccline->xp_context;
2191 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002192 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002193 }
2194 else
2195#endif
2196 while (nextcomm != NULL)
2197 nextcomm = set_one_cmd_context(xp, nextcomm);
2198
2199 // Store the string here so that call_user_expand_func() can get to them
2200 // easily.
2201 xp->xp_line = str;
2202 xp->xp_col = col;
2203
2204 str[col] = old_char;
2205}
2206
2207/*
2208 * Expand the command line "str" from context "xp".
2209 * "xp" must have been set by set_cmd_context().
2210 * xp->xp_pattern points into "str", to where the text that is to be expanded
2211 * starts.
2212 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2213 * cursor.
2214 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2215 * key that triggered expansion literally.
2216 * Returns EXPAND_OK otherwise.
2217 */
2218 int
2219expand_cmdline(
2220 expand_T *xp,
2221 char_u *str, // start of command line
2222 int col, // position of cursor
2223 int *matchcount, // return: nr of matches
2224 char_u ***matches) // return: array of pointers to matches
2225{
2226 char_u *file_str = NULL;
2227 int options = WILD_ADD_SLASH|WILD_SILENT;
2228
2229 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2230 {
2231 beep_flush();
2232 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2233 }
2234 if (xp->xp_context == EXPAND_NOTHING)
2235 {
2236 // Caller can use the character as a normal char instead
2237 return EXPAND_NOTHING;
2238 }
2239
2240 // add star to file name, or convert to regexp if not exp. files.
2241 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002242 if (cmdline_fuzzy_completion_supported(xp))
2243 // If fuzzy matching, don't modify the search string
2244 file_str = vim_strsave(xp->xp_pattern);
2245 else
2246 {
2247 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2248 if (file_str == NULL)
2249 return EXPAND_UNSUCCESSFUL;
2250 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002251
2252 if (p_wic)
2253 options += WILD_ICASE;
2254
2255 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002256 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002257 {
2258 *matchcount = 0;
2259 *matches = NULL;
2260 }
2261 vim_free(file_str);
2262
2263 return EXPAND_OK;
2264}
2265
Bram Moolenaar66b51422019-08-18 21:44:12 +02002266/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002267 * Expand file or directory names.
2268 */
2269 static int
2270expand_files_and_dirs(
2271 expand_T *xp,
2272 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002273 char_u ***matches,
2274 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002275 int flags,
2276 int options)
2277{
2278 int free_pat = FALSE;
2279 int i;
2280 int ret;
2281
2282 // for ":set path=" and ":set tags=" halve backslashes for escaped
2283 // space
2284 if (xp->xp_backslash != XP_BS_NONE)
2285 {
2286 free_pat = TRUE;
2287 pat = vim_strsave(pat);
2288 for (i = 0; pat[i]; ++i)
2289 if (pat[i] == '\\')
2290 {
2291 if (xp->xp_backslash == XP_BS_THREE
2292 && pat[i + 1] == '\\'
2293 && pat[i + 2] == '\\'
2294 && pat[i + 3] == ' ')
2295 STRMOVE(pat + i, pat + i + 3);
2296 if (xp->xp_backslash == XP_BS_ONE
2297 && pat[i + 1] == ' ')
2298 STRMOVE(pat + i, pat + i + 1);
2299 }
2300 }
2301
2302 if (xp->xp_context == EXPAND_FILES)
2303 flags |= EW_FILE;
2304 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2305 flags |= (EW_FILE | EW_PATH);
2306 else
2307 flags = (flags | EW_DIR) & ~EW_FILE;
2308 if (options & WILD_ICASE)
2309 flags |= EW_ICASE;
2310
2311 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002312 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002313 if (free_pat)
2314 vim_free(pat);
2315#ifdef BACKSLASH_IN_FILENAME
2316 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2317 {
2318 int j;
2319
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002320 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002321 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002322 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002323
2324 while (*ptr != NUL)
2325 {
2326 if (p_csl[0] == 's' && *ptr == '\\')
2327 *ptr = '/';
2328 else if (p_csl[0] == 'b' && *ptr == '/')
2329 *ptr = '\\';
2330 ptr += (*mb_ptr2len)(ptr);
2331 }
2332 }
2333 }
2334#endif
2335 return ret;
2336}
2337
2338/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002339 * Function given to ExpandGeneric() to obtain the possible arguments of the
2340 * ":behave {mswin,xterm}" command.
2341 */
2342 static char_u *
2343get_behave_arg(expand_T *xp UNUSED, int idx)
2344{
2345 if (idx == 0)
2346 return (char_u *)"mswin";
2347 if (idx == 1)
2348 return (char_u *)"xterm";
2349 return NULL;
2350}
2351
2352/*
2353 * Function given to ExpandGeneric() to obtain the possible arguments of the
2354 * ":messages {clear}" command.
2355 */
2356 static char_u *
2357get_messages_arg(expand_T *xp UNUSED, int idx)
2358{
2359 if (idx == 0)
2360 return (char_u *)"clear";
2361 return NULL;
2362}
2363
2364 static char_u *
2365get_mapclear_arg(expand_T *xp UNUSED, int idx)
2366{
2367 if (idx == 0)
2368 return (char_u *)"<buffer>";
2369 return NULL;
2370}
2371
2372/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002373 * Do the expansion based on xp->xp_context and 'rmp'.
2374 */
2375 static int
2376ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002377 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002378 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002379 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002380 char_u ***matches,
2381 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002382{
2383 static struct expgen
2384 {
2385 int context;
2386 char_u *((*func)(expand_T *, int));
2387 int ic;
2388 int escaped;
2389 } tab[] =
2390 {
2391 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2392 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2393 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2394 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2395 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2396 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2397 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2398 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2399 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2400 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2401# ifdef FEAT_EVAL
2402 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2403 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2404 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2405 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2406 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2407# endif
2408# ifdef FEAT_MENU
2409 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2410 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2411# endif
2412# ifdef FEAT_SYN_HL
2413 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2414# endif
2415# ifdef FEAT_PROFILE
2416 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2417# endif
2418 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2419 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2420 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
2421# ifdef FEAT_CSCOPE
2422 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2423# endif
2424# ifdef FEAT_SIGNS
2425 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2426# endif
2427# ifdef FEAT_PROFILE
2428 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2429# endif
2430# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2431 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2432 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2433# endif
2434 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2435 {EXPAND_USER, get_users, TRUE, FALSE},
2436 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2437 };
2438 int i;
2439 int ret = FAIL;
2440
2441 // Find a context in the table and call the ExpandGeneric() with the
2442 // right function to do the expansion.
2443 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
2444 {
2445 if (xp->xp_context == tab[i].context)
2446 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002447 // Use fuzzy matching if 'wildoptions' has 'fuzzy'.
2448 // If no search pattern is supplied, then don't use fuzzy
2449 // matching and return all the found items.
2450 int fuzzy = cmdline_fuzzy_complete(pat);
2451
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002452 if (tab[i].ic)
2453 rmp->rm_ic = TRUE;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002454 ret = ExpandGeneric(xp, rmp, matches, numMatches,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002455 tab[i].func, tab[i].escaped,
2456 fuzzy ? pat : NULL);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002457 break;
2458 }
2459 }
2460
2461 return ret;
2462}
2463
2464/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002465 * Map wild expand options to flags for expand_wildcards()
2466 */
2467 static int
2468map_wildopts_to_ewflags(int options)
2469{
2470 int flags;
2471
2472 flags = EW_DIR; // include directories
2473 if (options & WILD_LIST_NOTFOUND)
2474 flags |= EW_NOTFOUND;
2475 if (options & WILD_ADD_SLASH)
2476 flags |= EW_ADDSLASH;
2477 if (options & WILD_KEEP_ALL)
2478 flags |= EW_KEEPALL;
2479 if (options & WILD_SILENT)
2480 flags |= EW_SILENT;
2481 if (options & WILD_NOERROR)
2482 flags |= EW_NOERROR;
2483 if (options & WILD_ALLLINKS)
2484 flags |= EW_ALLLINKS;
2485
2486 return flags;
2487}
2488
2489/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002490 * Do the expansion based on xp->xp_context and "pat".
2491 */
2492 static int
2493ExpandFromContext(
2494 expand_T *xp,
2495 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002496 char_u ***matches,
2497 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002498 int options) // WILD_ flags
2499{
Bram Moolenaar66b51422019-08-18 21:44:12 +02002500 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002501 int ret;
2502 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002503 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002504 int fuzzy = cmdline_fuzzy_complete(pat)
2505 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002506
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002507 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002508
2509 if (xp->xp_context == EXPAND_FILES
2510 || xp->xp_context == EXPAND_DIRECTORIES
2511 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002512 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
2513 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002514
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002515 *matches = (char_u **)"";
2516 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002517 if (xp->xp_context == EXPAND_HELP)
2518 {
2519 // With an empty argument we would get all the help tags, which is
2520 // very slow. Get matches for "help" instead.
2521 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002522 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002523 {
2524#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002525 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002526#endif
2527 return OK;
2528 }
2529 return FAIL;
2530 }
2531
Bram Moolenaar66b51422019-08-18 21:44:12 +02002532 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002533 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002534 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002535 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002536 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002537 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002538#ifdef FEAT_DIFF
2539 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002540 return ExpandBufnames(pat, numMatches, matches,
2541 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002542#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02002543 if (xp->xp_context == EXPAND_TAGS
2544 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002545 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
2546 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002547 if (xp->xp_context == EXPAND_COLORS)
2548 {
2549 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002550 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002551 directories);
2552 }
2553 if (xp->xp_context == EXPAND_COMPILER)
2554 {
2555 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002556 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002557 }
2558 if (xp->xp_context == EXPAND_OWNSYNTAX)
2559 {
2560 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002561 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002562 }
2563 if (xp->xp_context == EXPAND_FILETYPE)
2564 {
2565 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002566 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002567 }
2568# if defined(FEAT_EVAL)
2569 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002570 return ExpandUserList(xp, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002571# endif
2572 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002573 return ExpandPackAddDir(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002574
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002575 // When expanding a function name starting with s:, match the <SNR>nr_
2576 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02002577 if ((xp->xp_context == EXPAND_USER_FUNC
2578 || xp->xp_context == EXPAND_DISASSEMBLE)
2579 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002580 {
2581 int len = (int)STRLEN(pat) + 20;
2582
2583 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00002584 if (tofree == NULL)
2585 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01002586 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002587 pat = tofree;
2588 }
2589
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002590 if (!fuzzy)
2591 {
2592 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
2593 if (regmatch.regprog == NULL)
2594 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002595
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002596 // set ignore-case according to p_ic, p_scs and pat
2597 regmatch.rm_ic = ignorecase(pat);
2598 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002599
2600 if (xp->xp_context == EXPAND_SETTINGS
2601 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002602 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002603 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002604 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002605# if defined(FEAT_EVAL)
2606 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002607 ret = ExpandUserDefined(xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002608# endif
2609 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002610 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002611
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00002612 if (!fuzzy)
2613 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01002614 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002615
2616 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002617}
2618
Bram Moolenaar66b51422019-08-18 21:44:12 +02002619/*
2620 * Expand a list of names.
2621 *
2622 * Generic function for command line completion. It calls a function to
2623 * obtain strings, one by one. The strings are matched against a regexp
2624 * program. Matching strings are copied into an array, which is returned.
2625 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002626 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
2627 * is used.
2628 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02002629 * Returns OK when no problems encountered, FAIL for error (out of memory).
2630 */
Bram Moolenaar61d7c0d2020-01-05 14:38:40 +01002631 static int
Bram Moolenaar66b51422019-08-18 21:44:12 +02002632ExpandGeneric(
2633 expand_T *xp,
2634 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002635 char_u ***matches,
2636 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02002637 char_u *((*func)(expand_T *, int)),
2638 // returns a string from the list
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002639 int escaped,
2640 char_u *fuzzystr)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002641{
2642 int i;
2643 int count = 0;
2644 int round;
2645 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002646 fuzmatch_str_T *fuzmatch = NULL;
2647 int score = 0;
2648 int fuzzy = (fuzzystr != NULL);
2649 int funcsort = FALSE;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002650 int match;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002651
2652 // do this loop twice:
2653 // round == 0: count the number of matching names
2654 // round == 1: copy the matching names into allocated memory
2655 for (round = 0; round <= 1; ++round)
2656 {
2657 for (i = 0; ; ++i)
2658 {
2659 str = (*func)(xp, i);
2660 if (str == NULL) // end of list
2661 break;
2662 if (*str == NUL) // skip empty strings
2663 continue;
2664
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002665 if (!fuzzy)
2666 match = vim_regexec(regmatch, str, (colnr_T)0);
2667 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02002668 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002669 score = fuzzy_match_str(str, fuzzystr);
2670 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002671 }
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00002672
2673 if (!match)
2674 continue;
2675
2676 if (round)
2677 {
2678 if (escaped)
2679 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2680 else
2681 str = vim_strsave(str);
2682 if (str == NULL)
2683 {
2684 if (fuzzy)
2685 fuzmatch_str_free(fuzmatch, count);
2686 else if (count > 0)
2687 FreeWild(count, *matches);
2688 *numMatches = 0;
2689 *matches = NULL;
2690 return FAIL;
2691 }
2692 if (fuzzy)
2693 {
2694 fuzmatch[count].idx = count;
2695 fuzmatch[count].str = str;
2696 fuzmatch[count].score = score;
2697 }
2698 else
2699 (*matches)[count] = str;
2700# ifdef FEAT_MENU
2701 if (func == get_menu_names && str != NULL)
2702 {
2703 // test for separator added by get_menu_names()
2704 str += STRLEN(str) - 1;
2705 if (*str == '\001')
2706 *str = '.';
2707 }
2708# endif
2709 }
2710 ++count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002711 }
2712 if (round == 0)
2713 {
2714 if (count == 0)
2715 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002716 if (fuzzy)
2717 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2718 else
2719 *matches = ALLOC_MULT(char_u *, count);
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00002720 if ((!fuzzy && (*matches == NULL))
2721 || (fuzzy && (fuzmatch == NULL)))
Bram Moolenaar66b51422019-08-18 21:44:12 +02002722 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002723 *numMatches = 0;
2724 *matches = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002725 return FAIL;
2726 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002727 *numMatches = count;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002728 count = 0;
2729 }
2730 }
2731
2732 // Sort the results. Keep menu's in the specified order.
2733 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2734 {
2735 if (xp->xp_context == EXPAND_EXPRESSION
2736 || xp->xp_context == EXPAND_FUNCTIONS
naohiro onodfe04db2021-09-12 15:45:10 +02002737 || xp->xp_context == EXPAND_USER_FUNC
2738 || xp->xp_context == EXPAND_DISASSEMBLE)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002739 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002740 // <SNR> functions should be sorted to the end.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002741 funcsort = TRUE;
2742 if (!fuzzy)
2743 qsort((void *)*matches, (size_t)*numMatches, sizeof(char_u *),
Bram Moolenaar66b51422019-08-18 21:44:12 +02002744 sort_func_compare);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002745 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002746 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002747 {
2748 if (!fuzzy)
2749 sort_strings(*matches, *numMatches);
2750 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002751 }
2752
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002753#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002754 // Reset the variables used for special highlight names expansion, so that
2755 // they don't show up when getting normal highlight names by ID.
2756 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02002757#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002758
2759 if (fuzzy && fuzzymatches_to_strmatches(fuzmatch, matches, count,
2760 funcsort) == FAIL)
2761 return FAIL;
2762
Bram Moolenaar66b51422019-08-18 21:44:12 +02002763 return OK;
2764}
2765
2766/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002767 * Expand shell command matches in one directory of $PATH.
2768 */
2769 static void
2770expand_shellcmd_onedir(
2771 char_u *buf,
2772 char_u *s,
2773 size_t l,
2774 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002775 char_u ***matches,
2776 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002777 int flags,
2778 hashtab_T *ht,
2779 garray_T *gap)
2780{
2781 int ret;
2782 int i;
2783 hash_T hash;
2784 hashitem_T *hi;
2785
2786 vim_strncpy(buf, s, l);
2787 add_pathsep(buf);
2788 l = STRLEN(buf);
2789 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2790
2791 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002792 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002793 if (ret == OK)
2794 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002795 if (ga_grow(gap, *numMatches) == FAIL)
2796 FreeWild(*numMatches, *matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002797 else
2798 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002799 for (i = 0; i < *numMatches; ++i)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002800 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002801 char_u *name = (*matches)[i];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002802
2803 if (STRLEN(name) > l)
2804 {
2805 // Check if this name was already found.
2806 hash = hash_hash(name + l);
2807 hi = hash_lookup(ht, name + l, hash);
2808 if (HASHITEM_EMPTY(hi))
2809 {
2810 // Remove the path that was prepended.
2811 STRMOVE(name, name + l);
2812 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
2813 hash_add_item(ht, hi, name, hash);
2814 name = NULL;
2815 }
2816 }
2817 vim_free(name);
2818 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002819 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002820 }
2821 }
2822}
2823
2824/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02002825 * Complete a shell command.
2826 * Returns FAIL or OK;
2827 */
2828 static int
2829expand_shellcmd(
2830 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002831 char_u ***matches, // return: array with matches
2832 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02002833 int flagsarg) // EW_ flags
2834{
2835 char_u *pat;
2836 int i;
2837 char_u *path = NULL;
2838 int mustfree = FALSE;
2839 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002840 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002841 size_t l;
2842 char_u *s, *e;
2843 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002844 int did_curdir = FALSE;
2845 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002846
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002847 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002848 if (buf == NULL)
2849 return FAIL;
2850
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002851 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02002852 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01002853 if (pat == NULL)
2854 {
2855 vim_free(buf);
2856 return FAIL;
2857 }
2858
Bram Moolenaar66b51422019-08-18 21:44:12 +02002859 for (i = 0; pat[i]; ++i)
2860 if (pat[i] == '\\' && pat[i + 1] == ' ')
2861 STRMOVE(pat + i, pat + i + 1);
2862
2863 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2864
2865 if (pat[0] == '.' && (vim_ispathsep(pat[1])
2866 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2867 path = (char_u *)".";
2868 else
2869 {
2870 // For an absolute name we don't use $PATH.
2871 if (!mch_isFullName(pat))
2872 path = vim_getenv((char_u *)"PATH", &mustfree);
2873 if (path == NULL)
2874 path = (char_u *)"";
2875 }
2876
2877 // Go over all directories in $PATH. Expand matches in that directory and
2878 // collect them in "ga". When "." is not in $PATH also expand for the
2879 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002880 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002881 hash_init(&found_ht);
2882 for (s = path; ; s = e)
2883 {
2884# if defined(MSWIN)
2885 e = vim_strchr(s, ';');
2886# else
2887 e = vim_strchr(s, ':');
2888# endif
2889 if (e == NULL)
2890 e = s + STRLEN(s);
2891
2892 if (*s == NUL)
2893 {
2894 if (did_curdir)
2895 break;
2896 // Find directories in the current directory, path is empty.
2897 did_curdir = TRUE;
2898 flags |= EW_DIR;
2899 }
2900 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2901 {
2902 did_curdir = TRUE;
2903 flags |= EW_DIR;
2904 }
2905 else
2906 // Do not match directories inside a $PATH item.
2907 flags &= ~EW_DIR;
2908
2909 l = e - s;
2910 if (l > MAXPATHL - 5)
2911 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002912
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002913 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002914 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002915
Bram Moolenaar66b51422019-08-18 21:44:12 +02002916 if (*e != NUL)
2917 ++e;
2918 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002919 *matches = ga.ga_data;
2920 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002921
2922 vim_free(buf);
2923 vim_free(pat);
2924 if (mustfree)
2925 vim_free(path);
2926 hash_clear(&found_ht);
2927 return OK;
2928}
2929
2930# if defined(FEAT_EVAL)
2931/*
2932 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02002933 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02002934 */
2935 static void *
2936call_user_expand_func(
2937 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002938 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002939{
2940 cmdline_info_T *ccline = get_cmdline_info();
2941 int keep = 0;
2942 typval_T args[4];
2943 sctx_T save_current_sctx = current_sctx;
2944 char_u *pat = NULL;
2945 void *ret;
2946
2947 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2948 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002949
2950 if (ccline->cmdbuff != NULL)
2951 {
2952 keep = ccline->cmdbuff[ccline->cmdlen];
2953 ccline->cmdbuff[ccline->cmdlen] = 0;
2954 }
2955
2956 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2957
2958 args[0].v_type = VAR_STRING;
2959 args[0].vval.v_string = pat;
2960 args[1].v_type = VAR_STRING;
2961 args[1].vval.v_string = xp->xp_line;
2962 args[2].v_type = VAR_NUMBER;
2963 args[2].vval.v_number = xp->xp_col;
2964 args[3].v_type = VAR_UNKNOWN;
2965
2966 current_sctx = xp->xp_script_ctx;
2967
2968 ret = user_expand_func(xp->xp_arg, 3, args);
2969
2970 current_sctx = save_current_sctx;
2971 if (ccline->cmdbuff != NULL)
2972 ccline->cmdbuff[ccline->cmdlen] = keep;
2973
2974 vim_free(pat);
2975 return ret;
2976}
2977
2978/*
2979 * Expand names with a function defined by the user.
2980 */
2981 static int
2982ExpandUserDefined(
2983 expand_T *xp,
2984 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002985 char_u ***matches,
2986 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002987{
2988 char_u *retstr;
2989 char_u *s;
2990 char_u *e;
2991 int keep;
2992 garray_T ga;
2993 int skip;
2994
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002995 *matches = NULL;
2996 *numMatches = 0;
2997 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002998 if (retstr == NULL)
2999 return FAIL;
3000
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003001 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003002 for (s = retstr; *s != NUL; s = e)
3003 {
3004 e = vim_strchr(s, '\n');
3005 if (e == NULL)
3006 e = s + STRLEN(s);
3007 keep = *e;
3008 *e = NUL;
3009
3010 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
3011 *e = keep;
3012
3013 if (!skip)
3014 {
3015 if (ga_grow(&ga, 1) == FAIL)
3016 break;
Bram Moolenaardf44a272020-06-07 20:49:05 +02003017 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003018 ++ga.ga_len;
3019 }
3020
3021 if (*e != NUL)
3022 ++e;
3023 }
3024 vim_free(retstr);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003025 *matches = ga.ga_data;
3026 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003027 return OK;
3028}
3029
3030/*
3031 * Expand names with a list returned by a function defined by the user.
3032 */
3033 static int
3034ExpandUserList(
3035 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003036 char_u ***matches,
3037 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003038{
3039 list_T *retlist;
3040 listitem_T *li;
3041 garray_T ga;
3042
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003043 *matches = NULL;
3044 *numMatches = 0;
3045 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003046 if (retlist == NULL)
3047 return FAIL;
3048
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003049 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003050 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003051 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003052 {
3053 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3054 continue; // Skip non-string items and empty strings
3055
3056 if (ga_grow(&ga, 1) == FAIL)
3057 break;
3058
3059 ((char_u **)ga.ga_data)[ga.ga_len] =
3060 vim_strsave(li->li_tv.vval.v_string);
3061 ++ga.ga_len;
3062 }
3063 list_unref(retlist);
3064
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003065 *matches = ga.ga_data;
3066 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003067 return OK;
3068}
3069# endif
3070
3071/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003072 * Expand "file" for all comma-separated directories in "path".
3073 * Adds the matches to "ga". Caller must init "ga".
3074 */
3075 void
3076globpath(
3077 char_u *path,
3078 char_u *file,
3079 garray_T *ga,
3080 int expand_options)
3081{
3082 expand_T xpc;
3083 char_u *buf;
3084 int i;
3085 int num_p;
3086 char_u **p;
3087
3088 buf = alloc(MAXPATHL);
3089 if (buf == NULL)
3090 return;
3091
3092 ExpandInit(&xpc);
3093 xpc.xp_context = EXPAND_FILES;
3094
3095 // Loop over all entries in {path}.
3096 while (*path != NUL)
3097 {
3098 // Copy one item of the path to buf[] and concatenate the file name.
3099 copy_option_part(&path, buf, MAXPATHL, ",");
3100 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3101 {
3102# if defined(MSWIN)
3103 // Using the platform's path separator (\) makes vim incorrectly
3104 // treat it as an escape character, use '/' instead.
3105 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3106 STRCAT(buf, "/");
3107# else
3108 add_pathsep(buf);
3109# endif
3110 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003111 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3113 {
3114 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3115
3116 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003117 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003118 for (i = 0; i < num_p; ++i)
3119 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003120 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003121 ++ga->ga_len;
3122 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003123 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003124 }
3125 }
3126 }
3127
3128 vim_free(buf);
3129}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130
Bram Moolenaareadee482020-09-04 15:37:31 +02003131#ifdef FEAT_WILDMENU
3132
3133/*
3134 * Translate some keys pressed when 'wildmenu' is used.
3135 */
3136 int
3137wildmenu_translate_key(
3138 cmdline_info_T *cclp,
3139 int key,
3140 expand_T *xp,
3141 int did_wild_list)
3142{
3143 int c = key;
3144
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003145#ifdef FEAT_WILDMENU
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003146 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003147 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003148 // When the popup menu is used for cmdline completion:
3149 // Up : go to the previous item in the menu
3150 // Down : go to the next item in the menu
3151 // Left : go to the parent directory
3152 // Right: list the files in the selected directory
3153 switch (c)
3154 {
3155 case K_UP: c = K_LEFT; break;
3156 case K_DOWN: c = K_RIGHT; break;
3157 case K_LEFT: c = K_UP; break;
3158 case K_RIGHT: c = K_DOWN; break;
3159 default: break;
3160 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003161 }
3162#endif
3163
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003164 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003165 {
3166 if (c == K_LEFT)
3167 c = Ctrl_P;
3168 else if (c == K_RIGHT)
3169 c = Ctrl_N;
3170 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003171
Bram Moolenaareadee482020-09-04 15:37:31 +02003172 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003173 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003174 && cclp->cmdpos > 1
3175 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3176 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3177 && (c == '\n' || c == '\r' || c == K_KENTER))
3178 c = K_DOWN;
3179
3180 return c;
3181}
3182
3183/*
3184 * Delete characters on the command line, from "from" to the current
3185 * position.
3186 */
3187 static void
3188cmdline_del(cmdline_info_T *cclp, int from)
3189{
3190 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3191 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3192 cclp->cmdlen -= cclp->cmdpos - from;
3193 cclp->cmdpos = from;
3194}
3195
3196/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003197 * Handle a key pressed when the wild menu for the menu names
3198 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003199 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003200 static int
3201wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003202{
Bram Moolenaareadee482020-09-04 15:37:31 +02003203 int i;
3204 int j;
3205
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003206 // Hitting <Down> after "emenu Name.": complete submenu
3207 if (key == K_DOWN && cclp->cmdpos > 0
3208 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003209 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003210 key = p_wc;
3211 KeyTyped = TRUE; // in case the key was mapped
3212 }
3213 else if (key == K_UP)
3214 {
3215 // Hitting <Up>: Remove one submenu name in front of the
3216 // cursor
3217 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003218
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003219 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3220 i = 0;
3221 while (--j > 0)
3222 {
3223 // check for start of menu name
3224 if (cclp->cmdbuff[j] == ' '
3225 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003226 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003227 i = j + 1;
3228 break;
3229 }
3230 // check for start of submenu name
3231 if (cclp->cmdbuff[j] == '.'
3232 && cclp->cmdbuff[j - 1] != '\\')
3233 {
3234 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003235 {
3236 i = j + 1;
3237 break;
3238 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003239 else
3240 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003241 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003242 }
3243 if (i > 0)
3244 cmdline_del(cclp, i);
3245 key = p_wc;
3246 KeyTyped = TRUE; // in case the key was mapped
3247 xp->xp_context = EXPAND_NOTHING;
3248 }
3249
3250 return key;
3251}
3252
3253/*
3254 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3255 * directory names (EXPAND_DIRECTORIES) or shell command names
3256 * (EXPAND_SHELLCMD) is displayed.
3257 */
3258 static int
3259wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3260{
3261 int i;
3262 int j;
3263 char_u upseg[5];
3264
3265 upseg[0] = PATHSEP;
3266 upseg[1] = '.';
3267 upseg[2] = '.';
3268 upseg[3] = PATHSEP;
3269 upseg[4] = NUL;
3270
3271 if (key == K_DOWN
3272 && cclp->cmdpos > 0
3273 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3274 && (cclp->cmdpos < 3
3275 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3276 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3277 {
3278 // go down a directory
3279 key = p_wc;
3280 KeyTyped = TRUE; // in case the key was mapped
3281 }
3282 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3283 {
3284 // If in a direct ancestor, strip off one ../ to go down
3285 int found = FALSE;
3286
3287 j = cclp->cmdpos;
3288 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3289 while (--j > i)
3290 {
3291 if (has_mbyte)
3292 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3293 if (vim_ispathsep(cclp->cmdbuff[j]))
3294 {
3295 found = TRUE;
3296 break;
3297 }
3298 }
3299 if (found
3300 && cclp->cmdbuff[j - 1] == '.'
3301 && cclp->cmdbuff[j - 2] == '.'
3302 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3303 {
3304 cmdline_del(cclp, j - 2);
3305 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003306 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003307 }
3308 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003309 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003310 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003311 // go up a directory
3312 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003313
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003314 j = cclp->cmdpos - 1;
3315 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3316 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003317 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003318 if (has_mbyte)
3319 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3320 if (vim_ispathsep(cclp->cmdbuff[j])
3321# ifdef BACKSLASH_IN_FILENAME
3322 && vim_strchr((char_u *)" *?[{`$%#",
3323 cclp->cmdbuff[j + 1]) == NULL
3324# endif
3325 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003326 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003327 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003328 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003329 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003330 break;
3331 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003332 else
3333 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003334 }
3335 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003336
3337 if (!found)
3338 j = i;
3339 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3340 j += 4;
3341 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3342 && j == i)
3343 j += 3;
3344 else
3345 j = 0;
3346 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003347 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003348 // TODO this is only for DOS/UNIX systems - need to put in
3349 // machine-specific stuff here and in upseg init
3350 cmdline_del(cclp, j);
3351 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003352 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003353 else if (cclp->cmdpos > i)
3354 cmdline_del(cclp, i);
3355
3356 // Now complete in the new directory. Set KeyTyped in case the
3357 // Up key came from a mapping.
3358 key = p_wc;
3359 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003360 }
3361
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003362 return key;
3363}
3364
3365/*
3366 * Handle a key pressed when the wild menu is displayed
3367 */
3368 int
3369wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
3370{
3371 if (xp->xp_context == EXPAND_MENUNAMES)
3372 return wildmenu_process_key_menunames(cclp, key, xp);
3373 else if ((xp->xp_context == EXPAND_FILES
3374 || xp->xp_context == EXPAND_DIRECTORIES
3375 || xp->xp_context == EXPAND_SHELLCMD))
3376 return wildmenu_process_key_filenames(cclp, key, xp);
3377
3378 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02003379}
3380
3381/*
3382 * Free expanded names when finished walking through the matches
3383 */
3384 void
3385wildmenu_cleanup(cmdline_info_T *cclp)
3386{
3387 int skt = KeyTyped;
3388 int old_RedrawingDisabled = RedrawingDisabled;
3389
3390 if (!p_wmnu || wild_menu_showing == 0)
3391 return;
3392
3393 if (cclp->input_fn)
3394 RedrawingDisabled = 0;
3395
3396 if (wild_menu_showing == WM_SCROLLED)
3397 {
3398 // Entered command line, move it up
3399 cmdline_row--;
3400 redrawcmd();
3401 }
3402 else if (save_p_ls != -1)
3403 {
3404 // restore 'laststatus' and 'winminheight'
3405 p_ls = save_p_ls;
3406 p_wmh = save_p_wmh;
3407 last_status(FALSE);
3408 update_screen(VALID); // redraw the screen NOW
3409 redrawcmd();
3410 save_p_ls = -1;
3411 }
3412 else
3413 {
3414 win_redraw_last_status(topframe);
3415 redraw_statuslines();
3416 }
3417 KeyTyped = skt;
3418 wild_menu_showing = 0;
3419 if (cclp->input_fn)
3420 RedrawingDisabled = old_RedrawingDisabled;
3421}
3422#endif
3423
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003424#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003425/*
3426 * "getcompletion()" function
3427 */
3428 void
3429f_getcompletion(typval_T *argvars, typval_T *rettv)
3430{
3431 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003432 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003433 expand_T xpc;
3434 int filtered = FALSE;
3435 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01003436 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003437
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02003438 if (in_vim9script()
3439 && (check_for_string_arg(argvars, 0) == FAIL
3440 || check_for_string_arg(argvars, 1) == FAIL
3441 || check_for_opt_bool_arg(argvars, 2) == FAIL))
3442 return;
3443
ii144785fe02021-11-21 12:13:56 +00003444 pat = tv_get_string(&argvars[0]);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003445 if (argvars[1].v_type != VAR_STRING)
3446 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003447 semsg(_(e_invalid_argument_str), "type must be a string");
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003448 return;
3449 }
3450 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003451
3452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02003453 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003454
3455 if (p_wic)
3456 options |= WILD_ICASE;
3457
3458 // For filtered results, 'wildignore' is used
3459 if (!filtered)
3460 options |= WILD_KEEP_ALL;
3461
3462 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003463 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003464 {
ii144785fe02021-11-21 12:13:56 +00003465 set_one_cmd_context(&xpc, pat);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003466 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
ii144785fe02021-11-21 12:13:56 +00003467 xpc.xp_col = (int)STRLEN(pat);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003468 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003469 else
3470 {
ii144785fe02021-11-21 12:13:56 +00003471 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003472 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3473
3474 xpc.xp_context = cmdcomplete_str_to_type(type);
3475 if (xpc.xp_context == EXPAND_NOTHING)
3476 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003477 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003478 return;
3479 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480
3481# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003482 if (xpc.xp_context == EXPAND_MENUS)
3483 {
3484 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
3485 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3486 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003487# endif
3488# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003489 if (xpc.xp_context == EXPAND_CSCOPE)
3490 {
3491 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
3492 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3493 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003494# endif
3495# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003496 if (xpc.xp_context == EXPAND_SIGN)
3497 {
3498 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
3499 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
3500 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003501# endif
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02003502 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003503
3504 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
3505 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
3506 {
3507 int i;
3508
3509 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
3510
3511 for (i = 0; i < xpc.xp_numfiles; i++)
3512 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3513 }
3514 vim_free(pat);
3515 ExpandCleanup(&xpc);
3516}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003517#endif // FEAT_EVAL