blob: 1008bf97e5dd731d86741d6d322033ecc0fe6a07 [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
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000018static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000019static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020020static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020022#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000023static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020025#endif
26
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000027// "compl_match_array" points the currently displayed list of entries in the
28// popup menu. It is NULL when there is no popup menu.
29static pumitem_T *compl_match_array = NULL;
30static int compl_match_arraysize;
31// First column in cmdline of the matched item for completion.
32static int compl_startcol;
33static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000034
zeertzjqc51a3762022-12-10 10:22:29 +000035#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000038 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
39 * context.
40 */
41 static int
42cmdline_fuzzy_completion_supported(expand_T *xp)
43{
44 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
45 && xp->xp_context != EXPAND_BOOL_SETTINGS
46 && xp->xp_context != EXPAND_COLORS
47 && xp->xp_context != EXPAND_COMPILER
48 && xp->xp_context != EXPAND_DIRECTORIES
49 && xp->xp_context != EXPAND_FILES
50 && xp->xp_context != EXPAND_FILES_IN_PATH
51 && xp->xp_context != EXPAND_FILETYPE
52 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010053 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000054 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020055 && xp->xp_context != EXPAND_STRING_SETTING
56 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000057 && xp->xp_context != EXPAND_OWNSYNTAX
58 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000059 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060 && xp->xp_context != EXPAND_SHELLCMD
61 && xp->xp_context != EXPAND_TAGS
62 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_USER_LIST);
64}
65
66/*
67 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000068 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
69 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000070 */
71 int
72cmdline_fuzzy_complete(char_u *fuzzystr)
73{
74 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
75}
76
77/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000078 * sort function for the completion matches.
79 * <SNR> functions should be sorted to the end.
80 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020081 static int
82sort_func_compare(const void *s1, const void *s2)
83{
84 char_u *p1 = *(char_u **)s1;
85 char_u *p2 = *(char_u **)s2;
86
87 if (*p1 != '<' && *p2 == '<') return -1;
88 if (*p1 == '<' && *p2 != '<') return 1;
89 return STRCMP(p1, p2);
90}
Bram Moolenaar66b51422019-08-18 21:44:12 +020091
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000092/*
93 * Escape special characters in the cmdline completion matches.
94 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020095 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000096wildescape(
97 expand_T *xp,
98 char_u *str,
99 int numfiles,
100 char_u **files)
101{
102 char_u *p;
103 int vse_what = xp->xp_context == EXPAND_BUFFERS
104 ? VSE_BUFFER : VSE_NONE;
105
106 if (xp->xp_context == EXPAND_FILES
107 || xp->xp_context == EXPAND_FILES_IN_PATH
108 || xp->xp_context == EXPAND_SHELLCMD
109 || xp->xp_context == EXPAND_BUFFERS
110 || xp->xp_context == EXPAND_DIRECTORIES)
111 {
112 // Insert a backslash into a file name before a space, \, %, #
113 // and wildmatch characters, except '~'.
114 for (int i = 0; i < numfiles; ++i)
115 {
116 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200117 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000118 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200119 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
120 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000121 if (p != NULL)
122 {
123 vim_free(files[i]);
124 files[i] = p;
125#if defined(BACKSLASH_IN_FILENAME)
126 p = vim_strsave_escaped(files[i], (char_u *)" ");
127 if (p != NULL)
128 {
129 vim_free(files[i]);
130 files[i] = p;
131 }
132#endif
133 }
134 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200135 else if (xp->xp_backslash & XP_BS_COMMA)
136 {
137 if (vim_strchr(files[i], ',') != NULL)
138 {
139 p = vim_strsave_escaped(files[i], (char_u *)",");
140 if (p != NULL)
141 {
142 vim_free(files[i]);
143 files[i] = p;
144 }
145 }
146 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000147#ifdef BACKSLASH_IN_FILENAME
148 p = vim_strsave_fnameescape(files[i], vse_what);
149#else
150 p = vim_strsave_fnameescape(files[i],
151 xp->xp_shell ? VSE_SHELL : vse_what);
152#endif
153 if (p != NULL)
154 {
155 vim_free(files[i]);
156 files[i] = p;
157 }
158
159 // If 'str' starts with "\~", replace "~" at start of
160 // files[i] with "\~".
161 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
162 escape_fname(&files[i]);
163 }
164 xp->xp_backslash = XP_BS_NONE;
165
166 // If the first file starts with a '+' escape it. Otherwise it
167 // could be seen as "+cmd".
168 if (*files[0] == '+')
169 escape_fname(&files[0]);
170 }
171 else if (xp->xp_context == EXPAND_TAGS)
172 {
173 // Insert a backslash before characters in a tag name that
174 // would terminate the ":tag" command.
175 for (int i = 0; i < numfiles; ++i)
176 {
177 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
178 if (p != NULL)
179 {
180 vim_free(files[i]);
181 files[i] = p;
182 }
183 }
184 }
185}
186
187/*
188 * Escape special characters in the cmdline completion matches.
189 */
190 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200191ExpandEscape(
192 expand_T *xp,
193 char_u *str,
194 int numfiles,
195 char_u **files,
196 int options)
197{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200198 // May change home directory back to "~"
199 if (options & WILD_HOME_REPLACE)
200 tilde_replace(str, numfiles, files);
201
202 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000203 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200204}
205
206/*
207 * Return FAIL if this is not an appropriate context in which to do
208 * completion of anything, return OK if it is (even if there are no matches).
209 * For the caller, this means that the character is just passed through like a
210 * normal character (instead of being expanded). This allows :s/^I^D etc.
211 */
212 int
213nextwild(
214 expand_T *xp,
215 int type,
216 int options, // extra options for ExpandOne()
217 int escape) // if TRUE, escape the returned matches
218{
219 cmdline_info_T *ccline = get_cmdline_info();
220 int i, j;
221 char_u *p1;
222 char_u *p2;
223 int difflen;
224 int v;
225
226 if (xp->xp_numfiles == -1)
227 {
228 set_expand_context(xp);
229 cmd_showtail = expand_showtail(xp);
230 }
231
232 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
233 {
234 beep_flush();
235 return OK; // Something illegal on command line
236 }
237 if (xp->xp_context == EXPAND_NOTHING)
238 {
239 // Caller can use the character as a normal char instead
240 return FAIL;
241 }
242
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000243 // If cmd_silent is set then don't show the dots, because redrawcmd() below
244 // won't remove them.
245 if (!cmd_silent)
246 {
247 msg_puts("..."); // show that we are busy
248 out_flush();
249 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200250
251 i = (int)(xp->xp_pattern - ccline->cmdbuff);
252 xp->xp_pattern_len = ccline->cmdpos - i;
253
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000254 if (type == WILD_NEXT || type == WILD_PREV
255 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200256 {
257 // Get next/previous match for a previous expanded pattern.
258 p2 = ExpandOne(xp, NULL, NULL, 0, type);
259 }
260 else
261 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000262 if (cmdline_fuzzy_completion_supported(xp))
263 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200264 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000265 else
266 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
267
Bram Moolenaar66b51422019-08-18 21:44:12 +0200268 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000269 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200270 p2 = NULL;
271 else
272 {
273 int use_options = options |
274 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
275 if (escape)
276 use_options |= WILD_ESCAPE;
277
278 if (p_wic)
279 use_options += WILD_ICASE;
280 p2 = ExpandOne(xp, p1,
281 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
282 use_options, type);
283 vim_free(p1);
284 // longest match: make sure it is not shorter, happens with :help
285 if (p2 != NULL && type == WILD_LONGEST)
286 {
287 for (j = 0; j < xp->xp_pattern_len; ++j)
288 if (ccline->cmdbuff[i + j] == '*'
289 || ccline->cmdbuff[i + j] == '?')
290 break;
291 if ((int)STRLEN(p2) < j)
292 VIM_CLEAR(p2);
293 }
294 }
295 }
296
297 if (p2 != NULL && !got_int)
298 {
299 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
300 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
301 {
302 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
303 xp->xp_pattern = ccline->cmdbuff + i;
304 }
305 else
306 v = OK;
307 if (v == OK)
308 {
309 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
310 &ccline->cmdbuff[ccline->cmdpos],
311 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
312 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
313 ccline->cmdlen += difflen;
314 ccline->cmdpos += difflen;
315 }
316 }
317 vim_free(p2);
318
319 redrawcmd();
320 cursorcmd();
321
322 // When expanding a ":map" command and no matches are found, assume that
323 // the key is supposed to be inserted literally
324 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
325 return FAIL;
326
327 if (xp->xp_numfiles <= 0 && p2 == NULL)
328 beep_flush();
329 else if (xp->xp_numfiles == 1)
330 // free expanded pattern
331 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
332
333 return OK;
334}
335
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000336/*
337 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000338 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000339 */
340 static int
341cmdline_pum_create(
342 cmdline_info_T *ccline,
343 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000344 char_u **matches,
345 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000346 int showtail)
347{
348 int i;
349 int columns;
350
351 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000352 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000353 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000354 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000355 {
zeertzjqc51a3762022-12-10 10:22:29 +0000356 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000357 compl_match_array[i].pum_info = NULL;
358 compl_match_array[i].pum_extra = NULL;
359 compl_match_array[i].pum_kind = NULL;
360 }
361
362 // Compute the popup menu starting column
363 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
364 columns = vim_strsize(xp->xp_pattern);
365 if (showtail)
366 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000367 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000368 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000369 }
370 if (columns >= compl_startcol)
371 compl_startcol = 0;
372 else
373 compl_startcol -= columns;
374
375 // no default selection
376 compl_selected = -1;
377
378 cmdline_pum_display();
379
380 return EXPAND_OK;
381}
382
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000383/*
384 * Display the cmdline completion matches in a popup menu
385 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000386 void
387cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000388{
389 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
390}
391
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000392/*
393 * Returns TRUE if the cmdline completion popup menu is being displayed.
394 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000395 int
396cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000397{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100398 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000399}
400
401/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000402 * Remove the cmdline completion popup menu (if present), free the list of
403 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000404 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000405 void
406cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000408 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100409 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000410
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000411 pum_undisplay();
412 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000413 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000414 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000415 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000416 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100417
418 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
419 // as a side effect.
420 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000421}
422
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000423 void
424cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000425{
426 cmdline_pum_remove();
427 wildmenu_cleanup(cclp);
428}
429
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000430/*
431 * Returns the starting column number to use for the cmdline completion popup
432 * menu.
433 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000434 int
435cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000436{
437 return compl_startcol;
438}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000439
Bram Moolenaar66b51422019-08-18 21:44:12 +0200440/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000441 * Return the number of characters that should be skipped in a status match.
442 * These are backslashes used for escaping. Do show backslashes in help tags.
443 */
444 static int
445skip_status_match_char(expand_T *xp, char_u *s)
446{
447 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
448#ifdef FEAT_MENU
449 || ((xp->xp_context == EXPAND_MENUS
450 || xp->xp_context == EXPAND_MENUNAMES)
451 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
452#endif
453 )
454 {
455#ifndef BACKSLASH_IN_FILENAME
456 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
457 return 2;
458#endif
459 return 1;
460 }
461 return 0;
462}
463
464/*
465 * Get the length of an item as it will be shown in the status line.
466 */
467 static int
468status_match_len(expand_T *xp, char_u *s)
469{
470 int len = 0;
471
472#ifdef FEAT_MENU
473 int emenu = xp->xp_context == EXPAND_MENUS
474 || xp->xp_context == EXPAND_MENUNAMES;
475
476 // Check for menu separators - replace with '|'.
477 if (emenu && menu_is_separator(s))
478 return 1;
479#endif
480
481 while (*s != NUL)
482 {
483 s += skip_status_match_char(xp, s);
484 len += ptr2cells(s);
485 MB_PTR_ADV(s);
486 }
487
488 return len;
489}
490
491/*
492 * Show wildchar matches in the status line.
493 * Show at least the "match" item.
494 * We start at item 'first_match' in the list and show all matches that fit.
495 *
496 * If inversion is possible we use it. Else '=' characters are used.
497 */
498 static void
499win_redr_status_matches(
500 expand_T *xp,
501 int num_matches,
502 char_u **matches, // list of matches
503 int match,
504 int showtail)
505{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000506 int row;
507 char_u *buf;
508 int len;
509 int clen; // length in screen cells
510 int fillchar;
511 int attr;
512 int i;
513 int highlight = TRUE;
514 char_u *selstart = NULL;
515 int selstart_col = 0;
516 char_u *selend = NULL;
517 static int first_match = 0;
518 int add_left = FALSE;
519 char_u *s;
520#ifdef FEAT_MENU
521 int emenu;
522#endif
523 int l;
524
525 if (matches == NULL) // interrupted completion?
526 return;
527
528 if (has_mbyte)
529 buf = alloc(Columns * MB_MAXBYTES + 1);
530 else
531 buf = alloc(Columns + 1);
532 if (buf == NULL)
533 return;
534
535 if (match == -1) // don't show match but original text
536 {
537 match = 0;
538 highlight = FALSE;
539 }
540 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000541 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000542 if (match == 0)
543 first_match = 0;
544 else if (match < first_match)
545 {
546 // jumping left, as far as we can go
547 first_match = match;
548 add_left = TRUE;
549 }
550 else
551 {
552 // check if match fits on the screen
553 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000554 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000555 if (first_match > 0)
556 clen += 2;
557 // jumping right, put match at the left
558 if ((long)clen > Columns)
559 {
560 first_match = match;
561 // if showing the last match, we can add some on the left
562 clen = 2;
563 for (i = match; i < num_matches; ++i)
564 {
zeertzjqc51a3762022-12-10 10:22:29 +0000565 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000566 if ((long)clen >= Columns)
567 break;
568 }
569 if (i == num_matches)
570 add_left = TRUE;
571 }
572 }
573 if (add_left)
574 while (first_match > 0)
575 {
zeertzjqc51a3762022-12-10 10:22:29 +0000576 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000577 if ((long)clen >= Columns)
578 break;
579 --first_match;
580 }
581
582 fillchar = fillchar_status(&attr, curwin);
583
584 if (first_match == 0)
585 {
586 *buf = NUL;
587 len = 0;
588 }
589 else
590 {
591 STRCPY(buf, "< ");
592 len = 2;
593 }
594 clen = len;
595
596 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000597 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000598 {
599 if (i == match)
600 {
601 selstart = buf + len;
602 selstart_col = clen;
603 }
604
zeertzjqc51a3762022-12-10 10:22:29 +0000605 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000606 // Check for menu separators - replace with '|'
607#ifdef FEAT_MENU
608 emenu = (xp->xp_context == EXPAND_MENUS
609 || xp->xp_context == EXPAND_MENUNAMES);
610 if (emenu && menu_is_separator(s))
611 {
612 STRCPY(buf + len, transchar('|'));
613 l = (int)STRLEN(buf + len);
614 len += l;
615 clen += l;
616 }
617 else
618#endif
619 for ( ; *s != NUL; ++s)
620 {
621 s += skip_status_match_char(xp, s);
622 clen += ptr2cells(s);
623 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
624 {
625 STRNCPY(buf + len, s, l);
626 s += l - 1;
627 len += l;
628 }
629 else
630 {
631 STRCPY(buf + len, transchar_byte(*s));
632 len += (int)STRLEN(buf + len);
633 }
634 }
635 if (i == match)
636 selend = buf + len;
637
638 *(buf + len++) = ' ';
639 *(buf + len++) = ' ';
640 clen += 2;
641 if (++i == num_matches)
642 break;
643 }
644
645 if (i != num_matches)
646 {
647 *(buf + len++) = '>';
648 ++clen;
649 }
650
651 buf[len] = NUL;
652
653 row = cmdline_row - 1;
654 if (row >= 0)
655 {
656 if (wild_menu_showing == 0)
657 {
658 if (msg_scrolled > 0)
659 {
660 // Put the wildmenu just above the command line. If there is
661 // no room, scroll the screen one line up.
662 if (cmdline_row == Rows - 1)
663 {
664 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
665 ++msg_scrolled;
666 }
667 else
668 {
669 ++cmdline_row;
670 ++row;
671 }
672 wild_menu_showing = WM_SCROLLED;
673 }
674 else
675 {
676 // Create status line if needed by setting 'laststatus' to 2.
677 // Set 'winminheight' to zero to avoid that the window is
678 // resized.
679 if (lastwin->w_status_height == 0)
680 {
681 save_p_ls = p_ls;
682 save_p_wmh = p_wmh;
683 p_ls = 2;
684 p_wmh = 0;
685 last_status(FALSE);
686 }
687 wild_menu_showing = WM_SHOWN;
688 }
689 }
690
691 screen_puts(buf, row, 0, attr);
692 if (selstart != NULL && highlight)
693 {
694 *selend = NUL;
695 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
696 }
697
698 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
699 }
700
701 win_redraw_last_status(topframe);
702 vim_free(buf);
703}
704
705/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000706 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200707 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000708 */
709 static char_u *
710get_next_or_prev_match(
711 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200712 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000713{
zeertzjqe9ef3472023-08-17 23:57:05 +0200714 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000715 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000716
717 if (xp->xp_numfiles <= 0)
718 return NULL;
719
720 if (mode == WILD_PREV)
721 {
722 if (findex == -1)
723 findex = xp->xp_numfiles;
724 --findex;
725 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000726 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000727 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000728 else if (mode == WILD_PAGEUP)
729 {
730 if (findex == 0)
731 // at the first entry, don't select any entries
732 findex = -1;
733 else if (findex == -1)
734 // no entry is selected. select the last entry
735 findex = xp->xp_numfiles - 1;
736 else
737 {
738 // go up by the pum height
739 ht = pum_get_height();
740 if (ht > 3)
741 ht -= 2;
742 findex -= ht;
743 if (findex < 0)
744 // few entries left, select the first entry
745 findex = 0;
746 }
747 }
748 else // mode == WILD_PAGEDOWN
749 {
750 if (findex == xp->xp_numfiles - 1)
751 // at the last entry, don't select any entries
752 findex = -1;
753 else if (findex == -1)
754 // no entry is selected. select the first entry
755 findex = 0;
756 else
757 {
758 // go down by the pum height
759 ht = pum_get_height();
760 if (ht > 3)
761 ht -= 2;
762 findex += ht;
763 if (findex >= xp->xp_numfiles)
764 // few entries left, select the last entry
765 findex = xp->xp_numfiles - 1;
766 }
767 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000768
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000769 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000770 if (findex < 0)
771 {
zeertzjq28a23602023-09-29 19:58:35 +0200772 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000773 findex = xp->xp_numfiles - 1;
774 else
775 findex = -1;
776 }
777 if (findex >= xp->xp_numfiles)
778 {
zeertzjq28a23602023-09-29 19:58:35 +0200779 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000780 findex = 0;
781 else
782 findex = -1;
783 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000784 if (compl_match_array)
785 {
786 compl_selected = findex;
787 cmdline_pum_display();
788 }
789 else if (p_wmnu)
790 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
791 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200792 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000793
794 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200795 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000796
797 return vim_strsave(xp->xp_files[findex]);
798}
799
800/*
801 * Start the command-line expansion and get the matches.
802 */
803 static char_u *
804ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
805{
806 int non_suf_match; // number without matching suffix
807 int i;
808 char_u *ss = NULL;
809
810 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000811 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000812 options) == FAIL)
813 {
814#ifdef FNAME_ILLEGAL
815 // Illegal file name has been silently skipped. But when there
816 // are wildcards, the real problem is that there was no match,
817 // causing the pattern to be added, which has illegal characters.
818 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
819 semsg(_(e_no_match_str_2), str);
820#endif
821 }
822 else if (xp->xp_numfiles == 0)
823 {
824 if (!(options & WILD_SILENT))
825 semsg(_(e_no_match_str_2), str);
826 }
827 else
828 {
829 // Escape the matches for use on the command line.
830 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
831
832 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000833 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000834 {
835 if (xp->xp_numfiles)
836 non_suf_match = xp->xp_numfiles;
837 else
838 non_suf_match = 1;
839 if ((xp->xp_context == EXPAND_FILES
840 || xp->xp_context == EXPAND_DIRECTORIES)
841 && xp->xp_numfiles > 1)
842 {
843 // More than one match; check suffix.
844 // The files will have been sorted on matching suffix in
845 // expand_wildcards, only need to check the first two.
846 non_suf_match = 0;
847 for (i = 0; i < 2; ++i)
848 if (match_suffix(xp->xp_files[i]))
849 ++non_suf_match;
850 }
851 if (non_suf_match != 1)
852 {
853 // Can we ever get here unless it's while expanding
854 // interactively? If not, we can get rid of this all
855 // together. Don't really want to wait for this message
856 // (and possibly have to hit return to continue!).
857 if (!(options & WILD_SILENT))
858 emsg(_(e_too_many_file_names));
859 else if (!(options & WILD_NO_BEEP))
860 beep_flush();
861 }
862 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
863 ss = vim_strsave(xp->xp_files[0]);
864 }
865 }
866
867 return ss;
868}
869
870/*
871 * Return the longest common part in the list of cmdline completion matches.
872 */
873 static char_u *
874find_longest_match(expand_T *xp, int options)
875{
876 long_u len;
877 int mb_len = 1;
878 int c0, ci;
879 int i;
880 char_u *ss;
881
882 for (len = 0; xp->xp_files[0][len]; len += mb_len)
883 {
884 if (has_mbyte)
885 {
886 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
887 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
888 }
889 else
890 c0 = xp->xp_files[0][len];
891 for (i = 1; i < xp->xp_numfiles; ++i)
892 {
893 if (has_mbyte)
894 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
895 else
896 ci = xp->xp_files[i][len];
897 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
898 || xp->xp_context == EXPAND_FILES
899 || xp->xp_context == EXPAND_SHELLCMD
900 || xp->xp_context == EXPAND_BUFFERS))
901 {
902 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
903 break;
904 }
905 else if (c0 != ci)
906 break;
907 }
908 if (i < xp->xp_numfiles)
909 {
910 if (!(options & WILD_NO_BEEP))
911 vim_beep(BO_WILD);
912 break;
913 }
914 }
915
916 ss = alloc(len + 1);
917 if (ss)
918 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
919
920 return ss;
921}
922
923/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000924 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200925 * Chars that should not be expanded must be preceded with a backslash.
926 * Return a pointer to allocated memory containing the new string.
927 * Return NULL for failure.
928 *
929 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200930 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
931 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200932 *
933 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
934 * is WILD_EXPAND_FREE or WILD_ALL.
935 *
936 * mode = WILD_FREE: just free previously expanded matches
937 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
938 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
939 * mode = WILD_NEXT: use next match in multiple match, wrap to first
940 * mode = WILD_PREV: use previous match in multiple match, wrap to first
941 * mode = WILD_ALL: return all matches concatenated
942 * mode = WILD_LONGEST: return longest matched part
943 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000944 * mode = WILD_APPLY: apply the item selected in the cmdline completion
945 * popup menu and close the menu.
946 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
947 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200948 *
949 * options = WILD_LIST_NOTFOUND: list entries without a match
950 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
951 * options = WILD_USE_NL: Use '\n' for WILD_ALL
952 * options = WILD_NO_BEEP: Don't beep for multiple matches
953 * options = WILD_ADD_SLASH: add a slash after directory names
954 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
955 * options = WILD_SILENT: don't print warning messages
956 * options = WILD_ESCAPE: put backslash before special chars
957 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200958 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200959 *
960 * The variables xp->xp_context and xp->xp_backslash must have been set!
961 */
962 char_u *
963ExpandOne(
964 expand_T *xp,
965 char_u *str,
966 char_u *orig, // allocated copy of original of expanded string
967 int options,
968 int mode)
969{
970 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200971 int orig_saved = FALSE;
972 int i;
973 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200974
975 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000976 if (mode == WILD_NEXT || mode == WILD_PREV
977 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +0200978 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200979
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000980 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +0200981 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000982 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +0200983 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +0200984 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +0200985 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000986
Bram Moolenaar66b51422019-08-18 21:44:12 +0200987 // free old names
988 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
989 {
990 FreeWild(xp->xp_numfiles, xp->xp_files);
991 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +0200992 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000993
994 // The entries from xp_files may be used in the PUM, remove it.
995 if (compl_match_array != NULL)
996 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +0200997 }
zeertzjqe9ef3472023-08-17 23:57:05 +0200998 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200999
1000 if (mode == WILD_FREE) // only release file name
1001 return NULL;
1002
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001003 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001004 {
zeertzjq28a23602023-09-29 19:58:35 +02001005 vim_free(xp->xp_orig);
1006 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001007 orig_saved = TRUE;
1008
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001009 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001010 }
1011
1012 // Find longest common part
1013 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1014 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001015 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001016 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001017 }
1018
Bram Moolenaar57e95172022-08-20 19:26:14 +01001019 // Concatenate all matching names. Unless interrupted, this can be slow
1020 // and the result probably won't be used.
1021 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001022 {
1023 len = 0;
1024 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001025 {
1026 if (i > 0)
1027 {
1028 if (xp->xp_prefix == XP_PREFIX_NO)
1029 len += 2; // prefix "no"
1030 else if (xp->xp_prefix == XP_PREFIX_INV)
1031 len += 3; // prefix "inv"
1032 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001034 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001035 ss = alloc(len);
1036 if (ss != NULL)
1037 {
1038 *ss = NUL;
1039 for (i = 0; i < xp->xp_numfiles; ++i)
1040 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001041 if (i > 0)
1042 {
1043 if (xp->xp_prefix == XP_PREFIX_NO)
1044 STRCAT(ss, "no");
1045 else if (xp->xp_prefix == XP_PREFIX_INV)
1046 STRCAT(ss, "inv");
1047 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001048 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001049
Bram Moolenaar66b51422019-08-18 21:44:12 +02001050 if (i != xp->xp_numfiles - 1)
1051 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1052 }
1053 }
1054 }
1055
1056 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1057 ExpandCleanup(xp);
1058
zeertzjq28a23602023-09-29 19:58:35 +02001059 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001060 if (!orig_saved)
1061 vim_free(orig);
1062
1063 return ss;
1064}
1065
1066/*
1067 * Prepare an expand structure for use.
1068 */
1069 void
1070ExpandInit(expand_T *xp)
1071{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001072 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001073 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001074 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001075 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076}
1077
1078/*
1079 * Cleanup an expand structure after use.
1080 */
1081 void
1082ExpandCleanup(expand_T *xp)
1083{
1084 if (xp->xp_numfiles >= 0)
1085 {
1086 FreeWild(xp->xp_numfiles, xp->xp_files);
1087 xp->xp_numfiles = -1;
1088 }
zeertzjq28a23602023-09-29 19:58:35 +02001089 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001090}
1091
1092/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001093 * Display one line of completion matches. Multiple matches are displayed in
1094 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001095 * matches - list of completion match names
1096 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001097 * lines - number of output lines
1098 * linenr - line number of matches to display
1099 * maxlen - maximum number of characters in each line
1100 * showtail - display only the tail of the full path of a file name
1101 * dir_attr - highlight attribute to use for directory names
1102 */
1103 static void
1104showmatches_oneline(
1105 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001106 char_u **matches,
1107 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001108 int lines,
1109 int linenr,
1110 int maxlen,
1111 int showtail,
1112 int dir_attr)
1113{
1114 int i, j;
1115 int isdir;
1116 int lastlen;
1117 char_u *p;
1118
1119 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001120 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001121 {
1122 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1123 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001124 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1125 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001126 msg_advance(maxlen + 1);
1127 msg_puts((char *)p);
1128 msg_advance(maxlen + 3);
1129 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1130 break;
1131 }
1132 for (i = maxlen - lastlen; --i >= 0; )
1133 msg_putchar(' ');
1134 if (xp->xp_context == EXPAND_FILES
1135 || xp->xp_context == EXPAND_SHELLCMD
1136 || xp->xp_context == EXPAND_BUFFERS)
1137 {
1138 // highlight directories
1139 if (xp->xp_numfiles != -1)
1140 {
1141 char_u *halved_slash;
1142 char_u *exp_path;
1143 char_u *path;
1144
1145 // Expansion was done before and special characters
1146 // were escaped, need to halve backslashes. Also
1147 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001148 exp_path = expand_env_save_opt(matches[j], TRUE);
1149 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001150 halved_slash = backslash_halve_save(path);
1151 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001152 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001153 vim_free(exp_path);
1154 if (halved_slash != path)
1155 vim_free(halved_slash);
1156 }
1157 else
1158 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001159 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001160 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001161 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001162 else
1163 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001164 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001165 TRUE);
1166 p = NameBuff;
1167 }
1168 }
1169 else
1170 {
1171 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001172 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001173 }
1174 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1175 }
1176 if (msg_col > 0) // when not wrapped around
1177 {
1178 msg_clr_eos();
1179 msg_putchar('\n');
1180 }
1181 out_flush(); // show one line at a time
1182}
1183
1184/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001185 * Show all matches for completion on the command line.
1186 * Returns EXPAND_NOTHING when the character that triggered expansion should
1187 * be inserted like a normal character.
1188 */
1189 int
1190showmatches(expand_T *xp, int wildmenu UNUSED)
1191{
1192 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001193 int numMatches;
1194 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001195 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001196 int maxlen;
1197 int lines;
1198 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001199 int attr;
1200 int showtail;
1201
1202 if (xp->xp_numfiles == -1)
1203 {
1204 set_expand_context(xp);
1205 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001206 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001207 showtail = expand_showtail(xp);
1208 if (i != EXPAND_OK)
1209 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001210 }
1211 else
1212 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001213 numMatches = xp->xp_numfiles;
1214 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001215 showtail = cmd_showtail;
1216 }
1217
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001218 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001219 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001220 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001221
Bram Moolenaar66b51422019-08-18 21:44:12 +02001222 if (!wildmenu)
1223 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001224 msg_didany = FALSE; // lines_left will be set
1225 msg_start(); // prepare for paging
1226 msg_putchar('\n');
1227 out_flush();
1228 cmdline_row = msg_row;
1229 msg_didany = FALSE; // lines_left will be set again
1230 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001231 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001232
1233 if (got_int)
1234 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001235 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001236 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001237 else
1238 {
1239 // find the length of the longest file name
1240 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001241 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001242 {
1243 if (!showtail && (xp->xp_context == EXPAND_FILES
1244 || xp->xp_context == EXPAND_SHELLCMD
1245 || xp->xp_context == EXPAND_BUFFERS))
1246 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001247 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001248 j = vim_strsize(NameBuff);
1249 }
1250 else
zeertzjqc51a3762022-12-10 10:22:29 +00001251 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001252 if (j > maxlen)
1253 maxlen = j;
1254 }
1255
1256 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001257 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001258 else
1259 {
1260 // compute the number of columns and lines for the listing
1261 maxlen += 2; // two spaces between file names
1262 columns = ((int)Columns + 2) / maxlen;
1263 if (columns < 1)
1264 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001265 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001266 }
1267
1268 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1269
1270 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1271 {
1272 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1273 msg_clr_eos();
1274 msg_advance(maxlen - 3);
1275 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1276 }
1277
1278 // list the files line by line
1279 for (i = 0; i < lines; ++i)
1280 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001281 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001282 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001283 if (got_int)
1284 {
1285 got_int = FALSE;
1286 break;
1287 }
1288 }
1289
1290 // we redraw the command below the lines that we have just listed
1291 // This is a bit tricky, but it saves a lot of screen updating.
1292 cmdline_row = msg_row; // will put it back later
1293 }
1294
1295 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001296 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001297
1298 return EXPAND_OK;
1299}
1300
1301/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001302 * gettail() version for showmatches() and win_redr_status_matches():
1303 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001304 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001305 static char_u *
1306showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001307{
1308 char_u *p;
1309 char_u *t = s;
1310 int had_sep = FALSE;
1311
1312 for (p = s; *p != NUL; )
1313 {
1314 if (vim_ispathsep(*p)
1315#ifdef BACKSLASH_IN_FILENAME
1316 && !rem_backslash(p)
1317#endif
1318 )
1319 had_sep = TRUE;
1320 else if (had_sep)
1321 {
1322 t = p;
1323 had_sep = FALSE;
1324 }
1325 MB_PTR_ADV(p);
1326 }
1327 return t;
1328}
1329
1330/*
1331 * Return TRUE if we only need to show the tail of completion matches.
1332 * When not completing file names or there is a wildcard in the path FALSE is
1333 * returned.
1334 */
1335 static int
1336expand_showtail(expand_T *xp)
1337{
1338 char_u *s;
1339 char_u *end;
1340
1341 // When not completing file names a "/" may mean something different.
1342 if (xp->xp_context != EXPAND_FILES
1343 && xp->xp_context != EXPAND_SHELLCMD
1344 && xp->xp_context != EXPAND_DIRECTORIES)
1345 return FALSE;
1346
1347 end = gettail(xp->xp_pattern);
1348 if (end == xp->xp_pattern) // there is no path separator
1349 return FALSE;
1350
1351 for (s = xp->xp_pattern; s < end; s++)
1352 {
1353 // Skip escaped wildcards. Only when the backslash is not a path
1354 // separator, on DOS the '*' "path\*\file" must not be skipped.
1355 if (rem_backslash(s))
1356 ++s;
1357 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1358 return FALSE;
1359 }
1360 return TRUE;
1361}
1362
1363/*
1364 * Prepare a string for expansion.
1365 * When expanding file names: The string will be used with expand_wildcards().
1366 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1367 * When expanding other names: The string will be used with regcomp(). Copy
1368 * the name into allocated memory and prepend "^".
1369 */
1370 char_u *
1371addstar(
1372 char_u *fname,
1373 int len,
1374 int context) // EXPAND_FILES etc.
1375{
1376 char_u *retval;
1377 int i, j;
1378 int new_len;
1379 char_u *tail;
1380 int ends_in_star;
1381
1382 if (context != EXPAND_FILES
1383 && context != EXPAND_FILES_IN_PATH
1384 && context != EXPAND_SHELLCMD
1385 && context != EXPAND_DIRECTORIES)
1386 {
1387 // Matching will be done internally (on something other than files).
1388 // So we convert the file-matching-type wildcards into our kind for
1389 // use with vim_regcomp(). First work out how long it will be:
1390
1391 // For help tags the translation is done in find_help_tags().
1392 // For a tag pattern starting with "/" no translation is needed.
1393 if (context == EXPAND_HELP
1394 || context == EXPAND_COLORS
1395 || context == EXPAND_COMPILER
1396 || context == EXPAND_OWNSYNTAX
1397 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001398 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001399 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001400 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001401 || ((context == EXPAND_TAGS_LISTFILES
1402 || context == EXPAND_TAGS)
1403 && fname[0] == '/'))
1404 retval = vim_strnsave(fname, len);
1405 else
1406 {
1407 new_len = len + 2; // +2 for '^' at start, NUL at end
1408 for (i = 0; i < len; i++)
1409 {
1410 if (fname[i] == '*' || fname[i] == '~')
1411 new_len++; // '*' needs to be replaced by ".*"
1412 // '~' needs to be replaced by "\~"
1413
1414 // Buffer names are like file names. "." should be literal
1415 if (context == EXPAND_BUFFERS && fname[i] == '.')
1416 new_len++; // "." becomes "\."
1417
1418 // Custom expansion takes care of special things, match
1419 // backslashes literally (perhaps also for other types?)
1420 if ((context == EXPAND_USER_DEFINED
1421 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1422 new_len++; // '\' becomes "\\"
1423 }
1424 retval = alloc(new_len);
1425 if (retval != NULL)
1426 {
1427 retval[0] = '^';
1428 j = 1;
1429 for (i = 0; i < len; i++, j++)
1430 {
1431 // Skip backslash. But why? At least keep it for custom
1432 // expansion.
1433 if (context != EXPAND_USER_DEFINED
1434 && context != EXPAND_USER_LIST
1435 && fname[i] == '\\'
1436 && ++i == len)
1437 break;
1438
1439 switch (fname[i])
1440 {
1441 case '*': retval[j++] = '.';
1442 break;
1443 case '~': retval[j++] = '\\';
1444 break;
1445 case '?': retval[j] = '.';
1446 continue;
1447 case '.': if (context == EXPAND_BUFFERS)
1448 retval[j++] = '\\';
1449 break;
1450 case '\\': if (context == EXPAND_USER_DEFINED
1451 || context == EXPAND_USER_LIST)
1452 retval[j++] = '\\';
1453 break;
1454 }
1455 retval[j] = fname[i];
1456 }
1457 retval[j] = NUL;
1458 }
1459 }
1460 }
1461 else
1462 {
1463 retval = alloc(len + 4);
1464 if (retval != NULL)
1465 {
1466 vim_strncpy(retval, fname, len);
1467
1468 // Don't add a star to *, ~, ~user, $var or `cmd`.
1469 // * would become **, which walks the whole tree.
1470 // ~ would be at the start of the file name, but not the tail.
1471 // $ could be anywhere in the tail.
1472 // ` could be anywhere in the file name.
1473 // When the name ends in '$' don't add a star, remove the '$'.
1474 tail = gettail(retval);
1475 ends_in_star = (len > 0 && retval[len - 1] == '*');
1476#ifndef BACKSLASH_IN_FILENAME
1477 for (i = len - 2; i >= 0; --i)
1478 {
1479 if (retval[i] != '\\')
1480 break;
1481 ends_in_star = !ends_in_star;
1482 }
1483#endif
1484 if ((*retval != '~' || tail != retval)
1485 && !ends_in_star
1486 && vim_strchr(tail, '$') == NULL
1487 && vim_strchr(retval, '`') == NULL)
1488 retval[len++] = '*';
1489 else if (len > 0 && retval[len - 1] == '$')
1490 --len;
1491 retval[len] = NUL;
1492 }
1493 }
1494 return retval;
1495}
1496
1497/*
1498 * Must parse the command line so far to work out what context we are in.
1499 * Completion can then be done based on that context.
1500 * This routine sets the variables:
1501 * xp->xp_pattern The start of the pattern to be expanded within
1502 * the command line (ends at the cursor).
1503 * xp->xp_context The type of thing to expand. Will be one of:
1504 *
1505 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1506 * the command line, like an unknown command. Caller
1507 * should beep.
1508 * EXPAND_NOTHING Unrecognised context for completion, use char like
1509 * a normal char, rather than for completion. eg
1510 * :s/^I/
1511 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1512 * it.
1513 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1514 * EXPAND_FILES After command with EX_XFILE set, or after setting
1515 * with P_EXPAND set. eg :e ^I, :w>>^I
1516 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001517 * when we know only directories are of interest.
1518 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001519 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1520 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1521 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1522 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1523 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1524 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1525 * EXPAND_EVENTS Complete event names
1526 * EXPAND_SYNTAX Complete :syntax command arguments
1527 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1528 * EXPAND_AUGROUP Complete autocommand group names
1529 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1530 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1531 * eg :unmap a^I , :cunab x^I
1532 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1533 * eg :call sub^I
1534 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1535 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1536 * names in expressions, eg :while s^I
1537 * EXPAND_ENV_VARS Complete environment variable names
1538 * EXPAND_USER Complete user names
1539 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001540 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001541set_expand_context(expand_T *xp)
1542{
1543 cmdline_info_T *ccline = get_cmdline_info();
1544
1545 // only expansion for ':', '>' and '=' command-lines
1546 if (ccline->cmdfirstc != ':'
1547#ifdef FEAT_EVAL
1548 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1549 && !ccline->input_fn
1550#endif
1551 )
1552 {
1553 xp->xp_context = EXPAND_NOTHING;
1554 return;
1555 }
1556 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1557}
1558
Bram Moolenaard0190392019-08-23 21:17:35 +02001559/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001560 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1561 * For user defined commands, the completion context is set in 'xp' and the
1562 * completion flags in 'complp'.
1563 *
1564 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001565 */
1566 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001567set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001568{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001569 char_u *p = NULL;
1570 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001571 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001572
1573 // Isolate the command and search for it in the command table.
1574 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001575 // - the 'k' command can directly be followed by any character, but do
1576 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1577 // find matches anywhere in the command name, do this only for command
1578 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001579 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001580 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001581 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001582 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001583 p = cmd + 1;
1584 }
1585 else
1586 {
1587 p = cmd;
1588 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1589 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001590 // A user command may contain digits.
1591 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1592 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001593 while (ASCII_ISALNUM(*p) || *p == '*')
1594 ++p;
1595 // for python 3.x: ":py3*" commands completion
1596 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1597 {
1598 ++p;
1599 while (ASCII_ISALPHA(*p) || *p == '*')
1600 ++p;
1601 }
1602 // check for non-alpha command
1603 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1604 ++p;
1605 len = (int)(p - cmd);
1606
1607 if (len == 0)
1608 {
1609 xp->xp_context = EXPAND_UNSUCCESSFUL;
1610 return NULL;
1611 }
1612
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001613 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001614
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001615 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001616 // Also when doing fuzzy expansion for non-shell commands, support
1617 // alphanumeric characters.
1618 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1619 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001620 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1621 ++p;
1622 }
1623
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001624 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001625 // character, complete the command name.
1626 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1627 return NULL;
1628
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001629 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001630 {
1631 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1632 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001633 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001634 p = cmd + 1;
1635 }
1636 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1637 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001638 eap->cmd = cmd;
1639 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001640 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001641 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001642 }
1643 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001644 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001645 {
1646 // Not still touching the command and it was an illegal one
1647 xp->xp_context = EXPAND_UNSUCCESSFUL;
1648 return NULL;
1649 }
1650
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001651 return p;
1652}
Bram Moolenaard0190392019-08-23 21:17:35 +02001653
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001654/*
1655 * Set the completion context for a command argument with wild card characters.
1656 */
1657 static void
1658set_context_for_wildcard_arg(
1659 exarg_T *eap,
1660 char_u *arg,
1661 int usefilter,
1662 expand_T *xp,
1663 int *complp)
1664{
1665 char_u *p;
1666 int c;
1667 int in_quote = FALSE;
1668 char_u *bow = NULL; // Beginning of word
1669 int len = 0;
1670
1671 // Allow spaces within back-quotes to count as part of the argument
1672 // being expanded.
1673 xp->xp_pattern = skipwhite(arg);
1674 p = xp->xp_pattern;
1675 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001676 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001677 if (has_mbyte)
1678 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001679 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001680 c = *p;
1681 if (c == '\\' && p[1] != NUL)
1682 ++p;
1683 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001684 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001686 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001687 xp->xp_pattern = p;
1688 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001689 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001690 in_quote = !in_quote;
1691 }
1692 // An argument can contain just about everything, except
1693 // characters that end the command and white space.
1694 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001695#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001696 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001697#endif
1698 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001699 {
1700 len = 0; // avoid getting stuck when space is in 'isfname'
1701 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001702 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001703 if (has_mbyte)
1704 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001705 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001706 c = *p;
1707 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001708 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001709 if (has_mbyte)
1710 len = (*mb_ptr2len)(p);
1711 else
1712 len = 1;
1713 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001714 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001715 if (in_quote)
1716 bow = p;
1717 else
1718 xp->xp_pattern = p;
1719 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001720 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001721 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001722 }
1723
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001724 // If we are still inside the quotes, and we passed a space, just
1725 // expand from there.
1726 if (bow != NULL && in_quote)
1727 xp->xp_pattern = bow;
1728 xp->xp_context = EXPAND_FILES;
1729
1730 // For a shell command more chars need to be escaped.
1731 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1732 {
1733#ifndef BACKSLASH_IN_FILENAME
1734 xp->xp_shell = TRUE;
1735#endif
1736 // When still after the command name expand executables.
1737 if (xp->xp_pattern == skipwhite(arg))
1738 xp->xp_context = EXPAND_SHELLCMD;
1739 }
1740
1741 // Check for environment variable.
1742 if (*xp->xp_pattern == '$')
1743 {
1744 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1745 if (!vim_isIDc(*p))
1746 break;
1747 if (*p == NUL)
1748 {
1749 xp->xp_context = EXPAND_ENV_VARS;
1750 ++xp->xp_pattern;
1751 // Avoid that the assignment uses EXPAND_FILES again.
1752 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1753 *complp = EXPAND_ENV_VARS;
1754 }
1755 }
1756 // Check for user names.
1757 if (*xp->xp_pattern == '~')
1758 {
1759 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1760 ;
1761 // Complete ~user only if it partially matches a user name.
1762 // A full match ~user<Tab> will be replaced by user's home
1763 // directory i.e. something like ~user<Tab> -> /home/user/
1764 if (*p == NUL && p > xp->xp_pattern + 1
1765 && match_user(xp->xp_pattern + 1) >= 1)
1766 {
1767 xp->xp_context = EXPAND_USER;
1768 ++xp->xp_pattern;
1769 }
1770 }
1771}
1772
1773/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001774 * Set the completion context for the "++opt=arg" argument. Always returns
1775 * NULL.
1776 */
1777 static char_u *
1778set_context_in_argopt(expand_T *xp, char_u *arg)
1779{
1780 char_u *p;
1781
1782 p = vim_strchr(arg, '=');
1783 if (p == NULL)
1784 xp->xp_pattern = arg;
1785 else
1786 xp->xp_pattern = p + 1;
1787
1788 xp->xp_context = EXPAND_ARGOPT;
1789 return NULL;
1790}
1791
1792#ifdef FEAT_TERMINAL
1793/*
1794 * Set the completion context for :terminal's [options]. Always returns NULL.
1795 */
1796 static char_u *
1797set_context_in_terminalopt(expand_T *xp, char_u *arg)
1798{
1799 char_u *p;
1800
1801 p = vim_strchr(arg, '=');
1802 if (p == NULL)
1803 xp->xp_pattern = arg;
1804 else
1805 xp->xp_pattern = p + 1;
1806
1807 xp->xp_context = EXPAND_TERMINALOPT;
1808 return NULL;
1809}
1810#endif
1811
1812/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001813 * Set the completion context for the :filter command. Returns a pointer to the
1814 * next command after the :filter command.
1815 */
1816 static char_u *
1817set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1818{
1819 if (*arg != NUL)
1820 arg = skip_vimgrep_pat(arg, NULL, NULL);
1821 if (arg == NULL || *arg == NUL)
1822 {
1823 xp->xp_context = EXPAND_NOTHING;
1824 return NULL;
1825 }
1826 return skipwhite(arg);
1827}
1828
1829#ifdef FEAT_SEARCH_EXTRA
1830/*
1831 * Set the completion context for the :match command. Returns a pointer to the
1832 * next command after the :match command.
1833 */
1834 static char_u *
1835set_context_in_match_cmd(expand_T *xp, char_u *arg)
1836{
1837 if (*arg == NUL || !ends_excmd(*arg))
1838 {
1839 // also complete "None"
1840 set_context_in_echohl_cmd(xp, arg);
1841 arg = skipwhite(skiptowhite(arg));
1842 if (*arg != NUL)
1843 {
1844 xp->xp_context = EXPAND_NOTHING;
1845 arg = skip_regexp(arg + 1, *arg, magic_isset());
1846 }
1847 }
1848 return find_nextcmd(arg);
1849}
1850#endif
1851
1852/*
1853 * Returns a pointer to the next command after a :global or a :v command.
1854 * Returns NULL if there is no next command.
1855 */
1856 static char_u *
1857find_cmd_after_global_cmd(char_u *arg)
1858{
1859 int delim;
1860
1861 delim = *arg; // get the delimiter
1862 if (delim)
1863 ++arg; // skip delimiter if there is one
1864
1865 while (arg[0] != NUL && arg[0] != delim)
1866 {
1867 if (arg[0] == '\\' && arg[1] != NUL)
1868 ++arg;
1869 ++arg;
1870 }
1871 if (arg[0] != NUL)
1872 return arg + 1;
1873
1874 return NULL;
1875}
1876
1877/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001878 * Returns a pointer to the next command after a :substitute or a :& command.
1879 * Returns NULL if there is no next command.
1880 */
1881 static char_u *
1882find_cmd_after_substitute_cmd(char_u *arg)
1883{
1884 int delim;
1885
1886 delim = *arg;
1887 if (delim)
1888 {
1889 // skip "from" part
1890 ++arg;
1891 arg = skip_regexp(arg, delim, magic_isset());
1892
1893 if (arg[0] != NUL && arg[0] == delim)
1894 {
1895 // skip "to" part
1896 ++arg;
1897 while (arg[0] != NUL && arg[0] != delim)
1898 {
1899 if (arg[0] == '\\' && arg[1] != NUL)
1900 ++arg;
1901 ++arg;
1902 }
1903 if (arg[0] != NUL) // skip delimiter
1904 ++arg;
1905 }
1906 }
1907 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1908 ++arg;
1909 if (arg[0] != NUL)
1910 return arg;
1911
1912 return NULL;
1913}
1914
1915/*
1916 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1917 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1918 * Returns NULL if there is no next command.
1919 */
1920 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001921find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001922{
1923 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001924 if (*arg != '/')
1925 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001926
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001927 // Match regexp, not just whole words
1928 for (++arg; *arg && *arg != '/'; arg++)
1929 if (*arg == '\\' && arg[1] != NUL)
1930 arg++;
1931 if (*arg)
1932 {
1933 arg = skipwhite(arg + 1);
1934
1935 // Check for trailing illegal characters
1936 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1937 xp->xp_context = EXPAND_NOTHING;
1938 else
1939 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001940 }
1941
1942 return NULL;
1943}
1944
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001945#ifdef FEAT_EVAL
1946/*
1947 * Set the completion context for the :unlet command. Always returns NULL.
1948 */
1949 static char_u *
1950set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1951{
1952 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1953 arg = xp->xp_pattern + 1;
1954
1955 xp->xp_context = EXPAND_USER_VARS;
1956 xp->xp_pattern = arg;
1957
1958 if (*xp->xp_pattern == '$')
1959 {
1960 xp->xp_context = EXPAND_ENV_VARS;
1961 ++xp->xp_pattern;
1962 }
1963
1964 return NULL;
1965}
1966#endif
1967
1968#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1969/*
1970 * Set the completion context for the :language command. Always returns NULL.
1971 */
1972 static char_u *
1973set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1974{
1975 char_u *p;
1976
1977 p = skiptowhite(arg);
1978 if (*p == NUL)
1979 {
1980 xp->xp_context = EXPAND_LANGUAGE;
1981 xp->xp_pattern = arg;
1982 }
1983 else
1984 {
1985 if ( STRNCMP(arg, "messages", p - arg) == 0
1986 || STRNCMP(arg, "ctype", p - arg) == 0
1987 || STRNCMP(arg, "time", p - arg) == 0
1988 || STRNCMP(arg, "collate", p - arg) == 0)
1989 {
1990 xp->xp_context = EXPAND_LOCALES;
1991 xp->xp_pattern = skipwhite(p);
1992 }
1993 else
1994 xp->xp_context = EXPAND_NOTHING;
1995 }
1996
1997 return NULL;
1998}
1999#endif
2000
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002001#ifdef FEAT_EVAL
2002static enum
2003{
2004 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002005 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2006 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002007} breakpt_expand_what;
2008
2009/*
2010 * Set the completion context for the :breakadd command. Always returns NULL.
2011 */
2012 static char_u *
2013set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2014{
2015 char_u *p;
2016 char_u *subcmd_start;
2017
2018 xp->xp_context = EXPAND_BREAKPOINT;
2019 xp->xp_pattern = arg;
2020
2021 if (cmdidx == CMD_breakadd)
2022 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002023 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002024 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002025 else
2026 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002027
2028 p = skipwhite(arg);
2029 if (*p == NUL)
2030 return NULL;
2031 subcmd_start = p;
2032
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002033 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002034 {
2035 // :breakadd file [lnum] <filename>
2036 // :breakadd func [lnum] <funcname>
2037 p += 4;
2038 p = skipwhite(p);
2039
2040 // skip line number (if specified)
2041 if (VIM_ISDIGIT(*p))
2042 {
2043 p = skipdigits(p);
2044 if (*p != ' ')
2045 {
2046 xp->xp_context = EXPAND_NOTHING;
2047 return NULL;
2048 }
2049 p = skipwhite(p);
2050 }
2051 if (STRNCMP("file", subcmd_start, 4) == 0)
2052 xp->xp_context = EXPAND_FILES;
2053 else
2054 xp->xp_context = EXPAND_USER_FUNC;
2055 xp->xp_pattern = p;
2056 }
2057 else if (STRNCMP("expr ", p, 5) == 0)
2058 {
2059 // :breakadd expr <expression>
2060 xp->xp_context = EXPAND_EXPRESSION;
2061 xp->xp_pattern = skipwhite(p + 5);
2062 }
2063
2064 return NULL;
2065}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002066
2067 static char_u *
2068set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2069{
2070 char_u *p;
2071
2072 xp->xp_context = EXPAND_NOTHING;
2073 xp->xp_pattern = NULL;
2074
2075 p = skipwhite(arg);
2076 if (VIM_ISDIGIT(*p))
2077 return NULL;
2078
2079 xp->xp_context = EXPAND_SCRIPTNAMES;
2080 xp->xp_pattern = p;
2081
2082 return NULL;
2083}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002084#endif
2085
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002086/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002087 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2088 * The argument to the command is 'arg' and the argument flags is 'argt'.
2089 * For user-defined commands and for environment variables, 'compl' has the
2090 * completion type.
2091 * Returns a pointer to the next command. Returns NULL if there is no next
2092 * command.
2093 */
2094 static char_u *
2095set_context_by_cmdname(
2096 char_u *cmd,
2097 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002098 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002099 char_u *arg,
2100 long argt,
2101 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002102 int forceit)
2103{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002104 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002105 {
2106 case CMD_find:
2107 case CMD_sfind:
2108 case CMD_tabfind:
2109 if (xp->xp_context == EXPAND_FILES)
2110 xp->xp_context = EXPAND_FILES_IN_PATH;
2111 break;
2112 case CMD_cd:
2113 case CMD_chdir:
2114 case CMD_tcd:
2115 case CMD_tchdir:
2116 case CMD_lcd:
2117 case CMD_lchdir:
2118 if (xp->xp_context == EXPAND_FILES)
2119 xp->xp_context = EXPAND_DIRECTORIES;
2120 break;
2121 case CMD_help:
2122 xp->xp_context = EXPAND_HELP;
2123 xp->xp_pattern = arg;
2124 break;
2125
2126 // Command modifiers: return the argument.
2127 // Also for commands with an argument that is a command.
2128 case CMD_aboveleft:
2129 case CMD_argdo:
2130 case CMD_belowright:
2131 case CMD_botright:
2132 case CMD_browse:
2133 case CMD_bufdo:
2134 case CMD_cdo:
2135 case CMD_cfdo:
2136 case CMD_confirm:
2137 case CMD_debug:
2138 case CMD_folddoclosed:
2139 case CMD_folddoopen:
2140 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002141 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002142 case CMD_keepalt:
2143 case CMD_keepjumps:
2144 case CMD_keepmarks:
2145 case CMD_keeppatterns:
2146 case CMD_ldo:
2147 case CMD_leftabove:
2148 case CMD_lfdo:
2149 case CMD_lockmarks:
2150 case CMD_noautocmd:
2151 case CMD_noswapfile:
2152 case CMD_rightbelow:
2153 case CMD_sandbox:
2154 case CMD_silent:
2155 case CMD_tab:
2156 case CMD_tabdo:
2157 case CMD_topleft:
2158 case CMD_verbose:
2159 case CMD_vertical:
2160 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002161 case CMD_vim9cmd:
2162 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002163 return arg;
2164
2165 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002166 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002167
2168#ifdef FEAT_SEARCH_EXTRA
2169 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002170 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002171#endif
2172
2173 // All completion for the +cmdline_compl feature goes here.
2174
2175 case CMD_command:
2176 return set_context_in_user_cmd(xp, arg);
2177
2178 case CMD_delcommand:
2179 xp->xp_context = EXPAND_USER_COMMANDS;
2180 xp->xp_pattern = arg;
2181 break;
2182
2183 case CMD_global:
2184 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002185 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002186 case CMD_and:
2187 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002188 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002189 case CMD_isearch:
2190 case CMD_dsearch:
2191 case CMD_ilist:
2192 case CMD_dlist:
2193 case CMD_ijump:
2194 case CMD_psearch:
2195 case CMD_djump:
2196 case CMD_isplit:
2197 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002198 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002199 case CMD_autocmd:
2200 return set_context_in_autocmd(xp, arg, FALSE);
2201 case CMD_doautocmd:
2202 case CMD_doautoall:
2203 return set_context_in_autocmd(xp, arg, TRUE);
2204 case CMD_set:
2205 set_context_in_set_cmd(xp, arg, 0);
2206 break;
2207 case CMD_setglobal:
2208 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2209 break;
2210 case CMD_setlocal:
2211 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2212 break;
2213 case CMD_tag:
2214 case CMD_stag:
2215 case CMD_ptag:
2216 case CMD_ltag:
2217 case CMD_tselect:
2218 case CMD_stselect:
2219 case CMD_ptselect:
2220 case CMD_tjump:
2221 case CMD_stjump:
2222 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002223 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002224 xp->xp_context = EXPAND_TAGS_LISTFILES;
2225 else
2226 xp->xp_context = EXPAND_TAGS;
2227 xp->xp_pattern = arg;
2228 break;
2229 case CMD_augroup:
2230 xp->xp_context = EXPAND_AUGROUP;
2231 xp->xp_pattern = arg;
2232 break;
2233#ifdef FEAT_SYN_HL
2234 case CMD_syntax:
2235 set_context_in_syntax_cmd(xp, arg);
2236 break;
2237#endif
2238#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002239 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002240 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002241 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002242 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002243 case CMD_if:
2244 case CMD_elseif:
2245 case CMD_while:
2246 case CMD_for:
2247 case CMD_echo:
2248 case CMD_echon:
2249 case CMD_execute:
2250 case CMD_echomsg:
2251 case CMD_echoerr:
2252 case CMD_call:
2253 case CMD_return:
2254 case CMD_cexpr:
2255 case CMD_caddexpr:
2256 case CMD_cgetexpr:
2257 case CMD_lexpr:
2258 case CMD_laddexpr:
2259 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002260 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002261 break;
2262
2263 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002264 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002265 case CMD_function:
2266 case CMD_delfunction:
2267 xp->xp_context = EXPAND_USER_FUNC;
2268 xp->xp_pattern = arg;
2269 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002270 case CMD_disassemble:
2271 set_context_in_disassemble_cmd(xp, arg);
2272 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002273
2274 case CMD_echohl:
2275 set_context_in_echohl_cmd(xp, arg);
2276 break;
2277#endif
2278 case CMD_highlight:
2279 set_context_in_highlight_cmd(xp, arg);
2280 break;
2281#ifdef FEAT_CSCOPE
2282 case CMD_cscope:
2283 case CMD_lcscope:
2284 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002285 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002286 break;
2287#endif
2288#ifdef FEAT_SIGNS
2289 case CMD_sign:
2290 set_context_in_sign_cmd(xp, arg);
2291 break;
2292#endif
2293 case CMD_bdelete:
2294 case CMD_bwipeout:
2295 case CMD_bunload:
2296 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2297 arg = xp->xp_pattern + 1;
2298 // FALLTHROUGH
2299 case CMD_buffer:
2300 case CMD_sbuffer:
2301 case CMD_checktime:
2302 xp->xp_context = EXPAND_BUFFERS;
2303 xp->xp_pattern = arg;
2304 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002305#ifdef FEAT_DIFF
2306 case CMD_diffget:
2307 case CMD_diffput:
2308 // If current buffer is in diff mode, complete buffer names
2309 // which are in diff mode, and different than current buffer.
2310 xp->xp_context = EXPAND_DIFF_BUFFERS;
2311 xp->xp_pattern = arg;
2312 break;
2313#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002314 case CMD_USER:
2315 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002316 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2317 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002318
2319 case CMD_map: case CMD_noremap:
2320 case CMD_nmap: case CMD_nnoremap:
2321 case CMD_vmap: case CMD_vnoremap:
2322 case CMD_omap: case CMD_onoremap:
2323 case CMD_imap: case CMD_inoremap:
2324 case CMD_cmap: case CMD_cnoremap:
2325 case CMD_lmap: case CMD_lnoremap:
2326 case CMD_smap: case CMD_snoremap:
2327 case CMD_tmap: case CMD_tnoremap:
2328 case CMD_xmap: case CMD_xnoremap:
2329 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002330 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002331 case CMD_unmap:
2332 case CMD_nunmap:
2333 case CMD_vunmap:
2334 case CMD_ounmap:
2335 case CMD_iunmap:
2336 case CMD_cunmap:
2337 case CMD_lunmap:
2338 case CMD_sunmap:
2339 case CMD_tunmap:
2340 case CMD_xunmap:
2341 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002342 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002343 case CMD_mapclear:
2344 case CMD_nmapclear:
2345 case CMD_vmapclear:
2346 case CMD_omapclear:
2347 case CMD_imapclear:
2348 case CMD_cmapclear:
2349 case CMD_lmapclear:
2350 case CMD_smapclear:
2351 case CMD_tmapclear:
2352 case CMD_xmapclear:
2353 xp->xp_context = EXPAND_MAPCLEAR;
2354 xp->xp_pattern = arg;
2355 break;
2356
2357 case CMD_abbreviate: case CMD_noreabbrev:
2358 case CMD_cabbrev: case CMD_cnoreabbrev:
2359 case CMD_iabbrev: case CMD_inoreabbrev:
2360 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002361 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002362 case CMD_unabbreviate:
2363 case CMD_cunabbrev:
2364 case CMD_iunabbrev:
2365 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002366 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002367#ifdef FEAT_MENU
2368 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2369 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2370 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2371 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2372 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2373 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2374 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2375 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2376 case CMD_tmenu: case CMD_tunmenu:
2377 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2378 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2379#endif
2380
2381 case CMD_colorscheme:
2382 xp->xp_context = EXPAND_COLORS;
2383 xp->xp_pattern = arg;
2384 break;
2385
2386 case CMD_compiler:
2387 xp->xp_context = EXPAND_COMPILER;
2388 xp->xp_pattern = arg;
2389 break;
2390
2391 case CMD_ownsyntax:
2392 xp->xp_context = EXPAND_OWNSYNTAX;
2393 xp->xp_pattern = arg;
2394 break;
2395
2396 case CMD_setfiletype:
2397 xp->xp_context = EXPAND_FILETYPE;
2398 xp->xp_pattern = arg;
2399 break;
2400
2401 case CMD_packadd:
2402 xp->xp_context = EXPAND_PACKADD;
2403 xp->xp_pattern = arg;
2404 break;
2405
zeertzjqb0d45ec2023-01-25 15:04:22 +00002406 case CMD_runtime:
2407 set_context_in_runtime_cmd(xp, arg);
2408 break;
2409
Bram Moolenaard0190392019-08-23 21:17:35 +02002410#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2411 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002412 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002413#endif
2414#if defined(FEAT_PROFILE)
2415 case CMD_profile:
2416 set_context_in_profile_cmd(xp, arg);
2417 break;
2418#endif
2419 case CMD_behave:
2420 xp->xp_context = EXPAND_BEHAVE;
2421 xp->xp_pattern = arg;
2422 break;
2423
2424 case CMD_messages:
2425 xp->xp_context = EXPAND_MESSAGES;
2426 xp->xp_pattern = arg;
2427 break;
2428
2429 case CMD_history:
2430 xp->xp_context = EXPAND_HISTORY;
2431 xp->xp_pattern = arg;
2432 break;
2433#if defined(FEAT_PROFILE)
2434 case CMD_syntime:
2435 xp->xp_context = EXPAND_SYNTIME;
2436 xp->xp_pattern = arg;
2437 break;
2438#endif
2439
2440 case CMD_argdelete:
2441 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2442 arg = xp->xp_pattern + 1;
2443 xp->xp_context = EXPAND_ARGLIST;
2444 xp->xp_pattern = arg;
2445 break;
2446
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002447#ifdef FEAT_EVAL
2448 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002449 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002450 case CMD_breakdel:
2451 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002452
2453 case CMD_scriptnames:
2454 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002455#endif
2456
Bram Moolenaard0190392019-08-23 21:17:35 +02002457 default:
2458 break;
2459 }
2460 return NULL;
2461}
2462
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002463/*
2464 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2465 * we don't need/want deleted. Maybe this could be done better if we didn't
2466 * repeat all this stuff. The only problem is that they may not stay
2467 * perfectly compatible with each other, but then the command line syntax
2468 * probably won't change that much -- webb.
2469 */
2470 static char_u *
2471set_one_cmd_context(
2472 expand_T *xp,
2473 char_u *buff) // buffer for command string
2474{
2475 char_u *p;
2476 char_u *cmd, *arg;
2477 int len = 0;
2478 exarg_T ea;
2479 int compl = EXPAND_NOTHING;
2480 int forceit = FALSE;
2481 int usefilter = FALSE; // filter instead of file name
2482
2483 ExpandInit(xp);
2484 xp->xp_pattern = buff;
2485 xp->xp_line = buff;
2486 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2487 ea.argt = 0;
2488
2489 // 1. skip comment lines and leading space, colons or bars
2490 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2491 ;
2492 xp->xp_pattern = cmd;
2493
2494 if (*cmd == NUL)
2495 return NULL;
2496 if (*cmd == '"') // ignore comment lines
2497 {
2498 xp->xp_context = EXPAND_NOTHING;
2499 return NULL;
2500 }
2501
2502 // 3. Skip over the range to find the command.
2503 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2504 xp->xp_pattern = cmd;
2505 if (*cmd == NUL)
2506 return NULL;
2507 if (*cmd == '"')
2508 {
2509 xp->xp_context = EXPAND_NOTHING;
2510 return NULL;
2511 }
2512
2513 if (*cmd == '|' || *cmd == '\n')
2514 return cmd + 1; // There's another command
2515
2516 // Get the command index.
2517 p = set_cmd_index(cmd, &ea, xp, &compl);
2518 if (p == NULL)
2519 return NULL;
2520
2521 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2522
2523 if (*p == '!') // forced commands
2524 {
2525 forceit = TRUE;
2526 ++p;
2527 }
2528
2529 // 6. parse arguments
2530 if (!IS_USER_CMDIDX(ea.cmdidx))
2531 ea.argt = excmd_get_argt(ea.cmdidx);
2532
2533 arg = skipwhite(p);
2534
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002535 // Does command allow "++argopt" argument?
2536 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002537 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002538 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2539 {
2540 p = arg + 2;
2541 while (*p && !vim_isspace(*p))
2542 MB_PTR_ADV(p);
2543
2544 // Still touching the command after "++"?
2545 if (*p == NUL)
2546 {
2547 if (ea.argt & EX_ARGOPT)
2548 return set_context_in_argopt(xp, arg + 2);
2549#ifdef FEAT_TERMINAL
2550 if (ea.cmdidx == CMD_terminal)
2551 return set_context_in_terminalopt(xp, arg + 2);
2552#endif
2553 }
2554
2555 arg = skipwhite(p);
2556 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002557 }
2558
2559 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2560 {
2561 if (*arg == '>') // append
2562 {
2563 if (*++arg == '>')
2564 ++arg;
2565 arg = skipwhite(arg);
2566 }
2567 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2568 {
2569 ++arg;
2570 usefilter = TRUE;
2571 }
2572 }
2573
2574 if (ea.cmdidx == CMD_read)
2575 {
2576 usefilter = forceit; // :r! filter if forced
2577 if (*arg == '!') // :r !filter
2578 {
2579 ++arg;
2580 usefilter = TRUE;
2581 }
2582 }
2583
2584 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2585 {
2586 while (*arg == *cmd) // allow any number of '>' or '<'
2587 ++arg;
2588 arg = skipwhite(arg);
2589 }
2590
2591 // Does command allow "+command"?
2592 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2593 {
2594 // Check if we're in the +command
2595 p = arg + 1;
2596 arg = skip_cmd_arg(arg, FALSE);
2597
2598 // Still touching the command after '+'?
2599 if (*arg == NUL)
2600 return p;
2601
2602 // Skip space(s) after +command to get to the real argument
2603 arg = skipwhite(arg);
2604 }
2605
2606
2607 // Check for '|' to separate commands and '"' to start comments.
2608 // Don't do this for ":read !cmd" and ":write !cmd".
2609 if ((ea.argt & EX_TRLBAR) && !usefilter)
2610 {
2611 p = arg;
2612 // ":redir @" is not the start of a comment
2613 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2614 p += 2;
2615 while (*p)
2616 {
2617 if (*p == Ctrl_V)
2618 {
2619 if (p[1] != NUL)
2620 ++p;
2621 }
2622 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2623 || *p == '|' || *p == '\n')
2624 {
2625 if (*(p - 1) != '\\')
2626 {
2627 if (*p == '|' || *p == '\n')
2628 return p + 1;
2629 return NULL; // It's a comment
2630 }
2631 }
2632 MB_PTR_ADV(p);
2633 }
2634 }
2635
2636 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2637 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2638 // no arguments allowed but there is something
2639 return NULL;
2640
2641 // Find start of last argument (argument just before cursor):
2642 p = buff;
2643 xp->xp_pattern = p;
2644 len = (int)STRLEN(buff);
2645 while (*p && p < buff + len)
2646 {
2647 if (*p == ' ' || *p == TAB)
2648 {
2649 // argument starts after a space
2650 xp->xp_pattern = ++p;
2651 }
2652 else
2653 {
2654 if (*p == '\\' && *(p + 1) != NUL)
2655 ++p; // skip over escaped character
2656 MB_PTR_ADV(p);
2657 }
2658 }
2659
2660 if (ea.argt & EX_XFILE)
2661 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2662
2663 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002664 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002665 forceit);
2666}
2667
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002668/*
2669 * Set the completion context in 'xp' for command 'str'
2670 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002671 void
2672set_cmd_context(
2673 expand_T *xp,
2674 char_u *str, // start of command line
2675 int len, // length of command line (excl. NUL)
2676 int col, // position of cursor
2677 int use_ccline UNUSED) // use ccline for info
2678{
2679#ifdef FEAT_EVAL
2680 cmdline_info_T *ccline = get_cmdline_info();
2681#endif
2682 int old_char = NUL;
2683 char_u *nextcomm;
2684
2685 // Avoid a UMR warning from Purify, only save the character if it has been
2686 // written before.
2687 if (col < len)
2688 old_char = str[col];
2689 str[col] = NUL;
2690 nextcomm = str;
2691
2692#ifdef FEAT_EVAL
2693 if (use_ccline && ccline->cmdfirstc == '=')
2694 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002695 // pass CMD_SIZE because there is no real command
2696 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002697 }
2698 else if (use_ccline && ccline->input_fn)
2699 {
2700 xp->xp_context = ccline->xp_context;
2701 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002702 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002703 }
2704 else
2705#endif
2706 while (nextcomm != NULL)
2707 nextcomm = set_one_cmd_context(xp, nextcomm);
2708
2709 // Store the string here so that call_user_expand_func() can get to them
2710 // easily.
2711 xp->xp_line = str;
2712 xp->xp_col = col;
2713
2714 str[col] = old_char;
2715}
2716
2717/*
2718 * Expand the command line "str" from context "xp".
2719 * "xp" must have been set by set_cmd_context().
2720 * xp->xp_pattern points into "str", to where the text that is to be expanded
2721 * starts.
2722 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2723 * cursor.
2724 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2725 * key that triggered expansion literally.
2726 * Returns EXPAND_OK otherwise.
2727 */
2728 int
2729expand_cmdline(
2730 expand_T *xp,
2731 char_u *str, // start of command line
2732 int col, // position of cursor
2733 int *matchcount, // return: nr of matches
2734 char_u ***matches) // return: array of pointers to matches
2735{
2736 char_u *file_str = NULL;
2737 int options = WILD_ADD_SLASH|WILD_SILENT;
2738
2739 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2740 {
2741 beep_flush();
2742 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2743 }
2744 if (xp->xp_context == EXPAND_NOTHING)
2745 {
2746 // Caller can use the character as a normal char instead
2747 return EXPAND_NOTHING;
2748 }
2749
2750 // add star to file name, or convert to regexp if not exp. files.
2751 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002752 if (cmdline_fuzzy_completion_supported(xp))
2753 // If fuzzy matching, don't modify the search string
2754 file_str = vim_strsave(xp->xp_pattern);
2755 else
2756 {
2757 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2758 if (file_str == NULL)
2759 return EXPAND_UNSUCCESSFUL;
2760 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002761
2762 if (p_wic)
2763 options += WILD_ICASE;
2764
2765 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002766 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002767 {
2768 *matchcount = 0;
2769 *matches = NULL;
2770 }
2771 vim_free(file_str);
2772
2773 return EXPAND_OK;
2774}
2775
Bram Moolenaar66b51422019-08-18 21:44:12 +02002776/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002777 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002778 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002779 */
2780 static int
2781expand_files_and_dirs(
2782 expand_T *xp,
2783 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002784 char_u ***matches,
2785 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002786 int flags,
2787 int options)
2788{
2789 int free_pat = FALSE;
2790 int i;
2791 int ret;
2792
2793 // for ":set path=" and ":set tags=" halve backslashes for escaped
2794 // space
2795 if (xp->xp_backslash != XP_BS_NONE)
2796 {
2797 free_pat = TRUE;
2798 pat = vim_strsave(pat);
2799 for (i = 0; pat[i]; ++i)
2800 if (pat[i] == '\\')
2801 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002802 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002803 && pat[i + 1] == '\\'
2804 && pat[i + 2] == '\\'
2805 && pat[i + 3] == ' ')
2806 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002807 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002808 && pat[i + 1] == ' ')
2809 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002810 else if ((xp->xp_backslash & XP_BS_COMMA)
2811 && pat[i + 1] == '\\'
2812 && pat[i + 2] == ',')
2813 STRMOVE(pat + i, pat + i + 2);
2814#ifdef BACKSLASH_IN_FILENAME
2815 else if ((xp->xp_backslash & XP_BS_COMMA)
2816 && pat[i + 1] == ',')
2817 STRMOVE(pat + i, pat + i + 1);
2818#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002819 }
2820 }
2821
2822 if (xp->xp_context == EXPAND_FILES)
2823 flags |= EW_FILE;
2824 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2825 flags |= (EW_FILE | EW_PATH);
2826 else
2827 flags = (flags | EW_DIR) & ~EW_FILE;
2828 if (options & WILD_ICASE)
2829 flags |= EW_ICASE;
2830
2831 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002832 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002833 if (free_pat)
2834 vim_free(pat);
2835#ifdef BACKSLASH_IN_FILENAME
2836 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2837 {
2838 int j;
2839
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002840 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002841 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002842 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002843
2844 while (*ptr != NUL)
2845 {
2846 if (p_csl[0] == 's' && *ptr == '\\')
2847 *ptr = '/';
2848 else if (p_csl[0] == 'b' && *ptr == '/')
2849 *ptr = '\\';
2850 ptr += (*mb_ptr2len)(ptr);
2851 }
2852 }
2853 }
2854#endif
2855 return ret;
2856}
2857
2858/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002859 * Function given to ExpandGeneric() to obtain the possible arguments of the
2860 * ":behave {mswin,xterm}" command.
2861 */
2862 static char_u *
2863get_behave_arg(expand_T *xp UNUSED, int idx)
2864{
2865 if (idx == 0)
2866 return (char_u *)"mswin";
2867 if (idx == 1)
2868 return (char_u *)"xterm";
2869 return NULL;
2870}
2871
K.Takata161b6ac2022-11-14 15:31:07 +00002872#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002873/*
2874 * Function given to ExpandGeneric() to obtain the possible arguments of the
2875 * ":breakadd {expr, file, func, here}" command.
2876 * ":breakdel {func, file, here}" command.
2877 */
2878 static char_u *
2879get_breakadd_arg(expand_T *xp UNUSED, int idx)
2880{
2881 char *opts[] = {"expr", "file", "func", "here"};
2882
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002883 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002884 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002885 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002886 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2887 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002888 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2889 {
2890 // breakdel {func, file, here}
2891 if (idx <= 2)
2892 return (char_u *)opts[idx + 1];
2893 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002894 else
2895 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002896 // profdel {func, file}
2897 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002898 return (char_u *)opts[idx + 1];
2899 }
2900 }
2901 return NULL;
2902}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002903
2904/*
2905 * Function given to ExpandGeneric() to obtain the possible arguments for the
2906 * ":scriptnames" command.
2907 */
2908 static char_u *
2909get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2910{
2911 scriptitem_T *si;
2912
2913 if (!SCRIPT_ID_VALID(idx + 1))
2914 return NULL;
2915
2916 si = SCRIPT_ITEM(idx + 1);
2917 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2918 return NameBuff;
2919}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002920#endif
2921
Bram Moolenaard0190392019-08-23 21:17:35 +02002922/*
2923 * Function given to ExpandGeneric() to obtain the possible arguments of the
2924 * ":messages {clear}" command.
2925 */
2926 static char_u *
2927get_messages_arg(expand_T *xp UNUSED, int idx)
2928{
2929 if (idx == 0)
2930 return (char_u *)"clear";
2931 return NULL;
2932}
2933
2934 static char_u *
2935get_mapclear_arg(expand_T *xp UNUSED, int idx)
2936{
2937 if (idx == 0)
2938 return (char_u *)"<buffer>";
2939 return NULL;
2940}
2941
2942/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002943 * Do the expansion based on xp->xp_context and 'rmp'.
2944 */
2945 static int
2946ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002947 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002948 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002949 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002950 char_u ***matches,
2951 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002952{
2953 static struct expgen
2954 {
2955 int context;
2956 char_u *((*func)(expand_T *, int));
2957 int ic;
2958 int escaped;
2959 } tab[] =
2960 {
2961 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2962 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2963 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2964 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2965 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2966 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2967 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2968 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2969 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2970 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002971#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002972 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2973 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2974 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2975 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2976 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002977#endif
2978#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002979 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2980 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002981#endif
2982#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002983 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002984#endif
2985#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002986 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002987#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002988 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2989 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
2990 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002991#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002992 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002993#endif
2994#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002995 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002996#endif
2997#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002998 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002999#endif
3000#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003001 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3002 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003003#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003004 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3005 {EXPAND_USER, get_users, TRUE, FALSE},
3006 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003007#ifdef FEAT_EVAL
3008 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003009 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003010#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003011 };
3012 int i;
3013 int ret = FAIL;
3014
3015 // Find a context in the table and call the ExpandGeneric() with the
3016 // right function to do the expansion.
3017 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3018 {
3019 if (xp->xp_context == tab[i].context)
3020 {
3021 if (tab[i].ic)
3022 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003023 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3024 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003025 break;
3026 }
3027 }
3028
3029 return ret;
3030}
3031
3032/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003033 * Map wild expand options to flags for expand_wildcards()
3034 */
3035 static int
3036map_wildopts_to_ewflags(int options)
3037{
3038 int flags;
3039
3040 flags = EW_DIR; // include directories
3041 if (options & WILD_LIST_NOTFOUND)
3042 flags |= EW_NOTFOUND;
3043 if (options & WILD_ADD_SLASH)
3044 flags |= EW_ADDSLASH;
3045 if (options & WILD_KEEP_ALL)
3046 flags |= EW_KEEPALL;
3047 if (options & WILD_SILENT)
3048 flags |= EW_SILENT;
3049 if (options & WILD_NOERROR)
3050 flags |= EW_NOERROR;
3051 if (options & WILD_ALLLINKS)
3052 flags |= EW_ALLLINKS;
3053
3054 return flags;
3055}
3056
3057/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003058 * Do the expansion based on xp->xp_context and "pat".
3059 */
3060 static int
3061ExpandFromContext(
3062 expand_T *xp,
3063 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003064 char_u ***matches,
3065 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003066 int options) // WILD_ flags
3067{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003068 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003069 int ret;
3070 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003071 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003072 int fuzzy = cmdline_fuzzy_complete(pat)
3073 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003074
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003075 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003076
3077 if (xp->xp_context == EXPAND_FILES
3078 || xp->xp_context == EXPAND_DIRECTORIES
3079 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003080 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3081 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003082
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003083 *matches = (char_u **)"";
3084 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003085 if (xp->xp_context == EXPAND_HELP)
3086 {
3087 // With an empty argument we would get all the help tags, which is
3088 // very slow. Get matches for "help" instead.
3089 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003090 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003091 {
3092#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003093 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003094#endif
3095 return OK;
3096 }
3097 return FAIL;
3098 }
3099
Bram Moolenaar66b51422019-08-18 21:44:12 +02003100 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003101 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003103 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003105 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003106#ifdef FEAT_DIFF
3107 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003108 return ExpandBufnames(pat, numMatches, matches,
3109 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003110#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111 if (xp->xp_context == EXPAND_TAGS
3112 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003113 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3114 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115 if (xp->xp_context == EXPAND_COLORS)
3116 {
3117 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003118 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003119 directories);
3120 }
3121 if (xp->xp_context == EXPAND_COMPILER)
3122 {
3123 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003124 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125 }
3126 if (xp->xp_context == EXPAND_OWNSYNTAX)
3127 {
3128 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003129 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130 }
3131 if (xp->xp_context == EXPAND_FILETYPE)
3132 {
3133 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003134 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003135 }
Doug Kearns81642d92024-01-04 22:37:44 +01003136#ifdef FEAT_KEYMAP
3137 if (xp->xp_context == EXPAND_KEYMAP)
3138 {
3139 char *directories[] = {"keymap", NULL};
3140 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3141 }
3142#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003143#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003144 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003145 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003146#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003148 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003149 if (xp->xp_context == EXPAND_RUNTIME)
3150 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003151
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003152 // When expanding a function name starting with s:, match the <SNR>nr_
3153 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003154 if ((xp->xp_context == EXPAND_USER_FUNC
3155 || xp->xp_context == EXPAND_DISASSEMBLE)
3156 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003157 {
3158 int len = (int)STRLEN(pat) + 20;
3159
3160 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003161 if (tofree == NULL)
3162 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003163 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003164 pat = tofree;
3165 }
3166
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003167 if (!fuzzy)
3168 {
3169 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3170 if (regmatch.regprog == NULL)
3171 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003172
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003173 // set ignore-case according to p_ic, p_scs and pat
3174 regmatch.rm_ic = ignorecase(pat);
3175 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003176
3177 if (xp->xp_context == EXPAND_SETTINGS
3178 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003179 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003180 else if (xp->xp_context == EXPAND_STRING_SETTING)
3181 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3182 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3183 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003184 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003185 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003186 else if (xp->xp_context == EXPAND_ARGOPT)
3187 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3188#if defined(FEAT_TERMINAL)
3189 else if (xp->xp_context == EXPAND_TERMINALOPT)
3190 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3191#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003192#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003194 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003195#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003196 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003197 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003198
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003199 if (!fuzzy)
3200 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003201 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003202
3203 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003204}
3205
Bram Moolenaar66b51422019-08-18 21:44:12 +02003206/*
3207 * Expand a list of names.
3208 *
3209 * Generic function for command line completion. It calls a function to
3210 * obtain strings, one by one. The strings are matched against a regexp
3211 * program. Matching strings are copied into an array, which is returned.
3212 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003213 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3214 * is used.
3215 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003216 * Returns OK when no problems encountered, FAIL for error (out of memory).
3217 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003218 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003219ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003220 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003221 expand_T *xp,
3222 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003223 char_u ***matches,
3224 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003225 char_u *((*func)(expand_T *, int)),
3226 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003227 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003228{
3229 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003230 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003231 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003232 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003233 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003234 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003235 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003236 int sort_matches = FALSE;
3237 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003238
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003239 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003240 *matches = NULL;
3241 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003242
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003243 if (!fuzzy)
3244 ga_init2(&ga, sizeof(char *), 30);
3245 else
3246 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3247
3248 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003249 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003250 str = (*func)(xp, i);
3251 if (str == NULL) // end of list
3252 break;
3253 if (*str == NUL) // skip empty strings
3254 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003255
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003256 if (xp->xp_pattern[0] != NUL)
3257 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003258 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003259 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003260 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003261 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003262 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003263 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264 }
3265 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003266 else
3267 match = TRUE;
3268
3269 if (!match)
3270 continue;
3271
3272 if (escaped)
3273 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3274 else
3275 str = vim_strsave(str);
3276 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003277 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003278 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003279 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003280 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003281 return FAIL;
3282 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003283 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003284 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003285 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003286
3287 if (ga_grow(&ga, 1) == FAIL)
3288 {
3289 vim_free(str);
3290 break;
3291 }
3292
3293 if (fuzzy)
3294 {
3295 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3296 fuzmatch->idx = ga.ga_len;
3297 fuzmatch->str = str;
3298 fuzmatch->score = score;
3299 }
3300 else
3301 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3302
K.Takata161b6ac2022-11-14 15:31:07 +00003303#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003304 if (func == get_menu_names)
3305 {
3306 // test for separator added by get_menu_names()
3307 str += STRLEN(str) - 1;
3308 if (*str == '\001')
3309 *str = '.';
3310 }
K.Takata161b6ac2022-11-14 15:31:07 +00003311#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003312
3313 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003314 }
3315
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003316 if (ga.ga_len == 0)
3317 return OK;
3318
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003319 // sort the matches when using regular expression matching and sorting
3320 // applies to the completion context. Menus and scriptnames should be kept
3321 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003322 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003323 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003324 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003325 && xp->xp_context != EXPAND_SCRIPTNAMES
3326 && xp->xp_context != EXPAND_ARGOPT
3327 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003328 sort_matches = TRUE;
3329
3330 // <SNR> functions should be sorted to the end.
3331 if (xp->xp_context == EXPAND_EXPRESSION
3332 || xp->xp_context == EXPAND_FUNCTIONS
3333 || xp->xp_context == EXPAND_USER_FUNC
3334 || xp->xp_context == EXPAND_DISASSEMBLE)
3335 funcsort = TRUE;
3336
3337 // Sort the matches.
3338 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003339 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003340 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003341 // <SNR> functions should be sorted to the end.
3342 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3343 sort_func_compare);
3344 else
3345 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3346 }
3347
3348 if (!fuzzy)
3349 {
3350 *matches = ga.ga_data;
3351 *numMatches = ga.ga_len;
3352 }
3353 else
3354 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003355 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3356 funcsort) == FAIL)
3357 return FAIL;
3358 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003359 }
3360
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003361#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003362 // Reset the variables used for special highlight names expansion, so that
3363 // they don't show up when getting normal highlight names by ID.
3364 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003365#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003366
Bram Moolenaar66b51422019-08-18 21:44:12 +02003367 return OK;
3368}
3369
3370/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003371 * Expand shell command matches in one directory of $PATH.
3372 */
3373 static void
3374expand_shellcmd_onedir(
3375 char_u *buf,
3376 char_u *s,
3377 size_t l,
3378 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003379 char_u ***matches,
3380 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003381 int flags,
3382 hashtab_T *ht,
3383 garray_T *gap)
3384{
3385 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003386 hash_T hash;
3387 hashitem_T *hi;
3388
3389 vim_strncpy(buf, s, l);
3390 add_pathsep(buf);
3391 l = STRLEN(buf);
3392 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3393
3394 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003395 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003396 if (ret != OK)
3397 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003398
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003399 if (ga_grow(gap, *numMatches) == FAIL)
3400 {
3401 FreeWild(*numMatches, *matches);
3402 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003403 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003404
3405 for (int i = 0; i < *numMatches; ++i)
3406 {
3407 char_u *name = (*matches)[i];
3408
3409 if (STRLEN(name) > l)
3410 {
3411 // Check if this name was already found.
3412 hash = hash_hash(name + l);
3413 hi = hash_lookup(ht, name + l, hash);
3414 if (HASHITEM_EMPTY(hi))
3415 {
3416 // Remove the path that was prepended.
3417 STRMOVE(name, name + l);
3418 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3419 hash_add_item(ht, hi, name, hash);
3420 name = NULL;
3421 }
3422 }
3423 vim_free(name);
3424 }
3425 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003426}
3427
3428/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003429 * Complete a shell command.
3430 * Returns FAIL or OK;
3431 */
3432 static int
3433expand_shellcmd(
3434 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003435 char_u ***matches, // return: array with matches
3436 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003437 int flagsarg) // EW_ flags
3438{
3439 char_u *pat;
3440 int i;
3441 char_u *path = NULL;
3442 int mustfree = FALSE;
3443 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003444 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003445 size_t l;
3446 char_u *s, *e;
3447 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003448 int did_curdir = FALSE;
3449 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003450
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003451 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003452 if (buf == NULL)
3453 return FAIL;
3454
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003455 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003456 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003457 if (pat == NULL)
3458 {
3459 vim_free(buf);
3460 return FAIL;
3461 }
3462
Bram Moolenaar66b51422019-08-18 21:44:12 +02003463 for (i = 0; pat[i]; ++i)
3464 if (pat[i] == '\\' && pat[i + 1] == ' ')
3465 STRMOVE(pat + i, pat + i + 1);
3466
3467 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3468
3469 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3470 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3471 path = (char_u *)".";
3472 else
3473 {
3474 // For an absolute name we don't use $PATH.
3475 if (!mch_isFullName(pat))
3476 path = vim_getenv((char_u *)"PATH", &mustfree);
3477 if (path == NULL)
3478 path = (char_u *)"";
3479 }
3480
3481 // Go over all directories in $PATH. Expand matches in that directory and
3482 // collect them in "ga". When "." is not in $PATH also expand for the
3483 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003484 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003485 hash_init(&found_ht);
3486 for (s = path; ; s = e)
3487 {
K.Takata161b6ac2022-11-14 15:31:07 +00003488#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003489 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003490#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003491 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003492#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003493 if (e == NULL)
3494 e = s + STRLEN(s);
3495
3496 if (*s == NUL)
3497 {
3498 if (did_curdir)
3499 break;
3500 // Find directories in the current directory, path is empty.
3501 did_curdir = TRUE;
3502 flags |= EW_DIR;
3503 }
3504 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3505 {
3506 did_curdir = TRUE;
3507 flags |= EW_DIR;
3508 }
3509 else
3510 // Do not match directories inside a $PATH item.
3511 flags &= ~EW_DIR;
3512
3513 l = e - s;
3514 if (l > MAXPATHL - 5)
3515 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003516
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003517 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003518 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003519
Bram Moolenaar66b51422019-08-18 21:44:12 +02003520 if (*e != NUL)
3521 ++e;
3522 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003523 *matches = ga.ga_data;
3524 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003525
3526 vim_free(buf);
3527 vim_free(pat);
3528 if (mustfree)
3529 vim_free(path);
3530 hash_clear(&found_ht);
3531 return OK;
3532}
3533
K.Takata161b6ac2022-11-14 15:31:07 +00003534#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003535/*
3536 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003537 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003538 */
3539 static void *
3540call_user_expand_func(
3541 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003542 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003543{
3544 cmdline_info_T *ccline = get_cmdline_info();
3545 int keep = 0;
3546 typval_T args[4];
3547 sctx_T save_current_sctx = current_sctx;
3548 char_u *pat = NULL;
3549 void *ret;
3550
3551 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3552 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003553
3554 if (ccline->cmdbuff != NULL)
3555 {
3556 keep = ccline->cmdbuff[ccline->cmdlen];
3557 ccline->cmdbuff[ccline->cmdlen] = 0;
3558 }
3559
3560 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3561
3562 args[0].v_type = VAR_STRING;
3563 args[0].vval.v_string = pat;
3564 args[1].v_type = VAR_STRING;
3565 args[1].vval.v_string = xp->xp_line;
3566 args[2].v_type = VAR_NUMBER;
3567 args[2].vval.v_number = xp->xp_col;
3568 args[3].v_type = VAR_UNKNOWN;
3569
3570 current_sctx = xp->xp_script_ctx;
3571
3572 ret = user_expand_func(xp->xp_arg, 3, args);
3573
3574 current_sctx = save_current_sctx;
3575 if (ccline->cmdbuff != NULL)
3576 ccline->cmdbuff[ccline->cmdlen] = keep;
3577
3578 vim_free(pat);
3579 return ret;
3580}
3581
3582/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003583 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3584 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003585 */
3586 static int
3587ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003588 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003589 expand_T *xp,
3590 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003591 char_u ***matches,
3592 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003593{
3594 char_u *retstr;
3595 char_u *s;
3596 char_u *e;
3597 int keep;
3598 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003599 int fuzzy;
3600 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003601 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003602
3603 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003604 *matches = NULL;
3605 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003606
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003607 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003608 if (retstr == NULL)
3609 return FAIL;
3610
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003611 if (!fuzzy)
3612 ga_init2(&ga, sizeof(char *), 3);
3613 else
3614 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3615
Bram Moolenaar66b51422019-08-18 21:44:12 +02003616 for (s = retstr; *s != NUL; s = e)
3617 {
3618 e = vim_strchr(s, '\n');
3619 if (e == NULL)
3620 e = s + STRLEN(s);
3621 keep = *e;
3622 *e = NUL;
3623
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003624 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003625 {
3626 if (!fuzzy)
3627 match = vim_regexec(regmatch, s, (colnr_T)0);
3628 else
3629 {
3630 score = fuzzy_match_str(s, pat);
3631 match = (score != 0);
3632 }
3633 }
3634 else
3635 match = TRUE; // match everything
3636
Bram Moolenaar66b51422019-08-18 21:44:12 +02003637 *e = keep;
3638
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003639 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003640 {
3641 if (ga_grow(&ga, 1) == FAIL)
3642 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003643 if (!fuzzy)
3644 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3645 else
3646 {
3647 fuzmatch_str_T *fuzmatch =
3648 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003649 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003650 fuzmatch->str = vim_strnsave(s, e - s);
3651 fuzmatch->score = score;
3652 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003653 ++ga.ga_len;
3654 }
3655
3656 if (*e != NUL)
3657 ++e;
3658 }
3659 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003660
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003661 if (ga.ga_len == 0)
3662 return OK;
3663
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003664 if (!fuzzy)
3665 {
3666 *matches = ga.ga_data;
3667 *numMatches = ga.ga_len;
3668 }
3669 else
3670 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003671 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3672 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003673 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003674 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003675 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003676 return OK;
3677}
3678
3679/*
3680 * Expand names with a list returned by a function defined by the user.
3681 */
3682 static int
3683ExpandUserList(
3684 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003685 char_u ***matches,
3686 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003687{
3688 list_T *retlist;
3689 listitem_T *li;
3690 garray_T ga;
3691
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003692 *matches = NULL;
3693 *numMatches = 0;
3694 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003695 if (retlist == NULL)
3696 return FAIL;
3697
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003698 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003699 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003700 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003701 {
3702 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3703 continue; // Skip non-string items and empty strings
3704
3705 if (ga_grow(&ga, 1) == FAIL)
3706 break;
3707
3708 ((char_u **)ga.ga_data)[ga.ga_len] =
3709 vim_strsave(li->li_tv.vval.v_string);
3710 ++ga.ga_len;
3711 }
3712 list_unref(retlist);
3713
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003714 *matches = ga.ga_data;
3715 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003716 return OK;
3717}
K.Takata161b6ac2022-11-14 15:31:07 +00003718#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003719
3720/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003721 * Expand "file" for all comma-separated directories in "path".
3722 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003723 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003724 */
3725 void
3726globpath(
3727 char_u *path,
3728 char_u *file,
3729 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003730 int expand_options,
3731 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003732{
3733 expand_T xpc;
3734 char_u *buf;
3735 int i;
3736 int num_p;
3737 char_u **p;
3738
3739 buf = alloc(MAXPATHL);
3740 if (buf == NULL)
3741 return;
3742
3743 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003744 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003745
3746 // Loop over all entries in {path}.
3747 while (*path != NUL)
3748 {
3749 // Copy one item of the path to buf[] and concatenate the file name.
3750 copy_option_part(&path, buf, MAXPATHL, ",");
3751 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3752 {
K.Takata161b6ac2022-11-14 15:31:07 +00003753#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003754 // Using the platform's path separator (\) makes vim incorrectly
3755 // treat it as an escape character, use '/' instead.
3756 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3757 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003758#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003759 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003760#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003761 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003762 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003763 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3764 {
3765 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3766
3767 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003768 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003769 for (i = 0; i < num_p; ++i)
3770 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003771 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003772 ++ga->ga_len;
3773 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003774 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003775 }
3776 }
3777 }
3778
3779 vim_free(buf);
3780}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003781
Bram Moolenaareadee482020-09-04 15:37:31 +02003782/*
3783 * Translate some keys pressed when 'wildmenu' is used.
3784 */
3785 int
3786wildmenu_translate_key(
3787 cmdline_info_T *cclp,
3788 int key,
3789 expand_T *xp,
3790 int did_wild_list)
3791{
3792 int c = key;
3793
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003794 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003795 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003796 // When the popup menu is used for cmdline completion:
3797 // Up : go to the previous item in the menu
3798 // Down : go to the next item in the menu
3799 // Left : go to the parent directory
3800 // Right: list the files in the selected directory
3801 switch (c)
3802 {
3803 case K_UP: c = K_LEFT; break;
3804 case K_DOWN: c = K_RIGHT; break;
3805 case K_LEFT: c = K_UP; break;
3806 case K_RIGHT: c = K_DOWN; break;
3807 default: break;
3808 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003809 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003810
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003811 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003812 {
3813 if (c == K_LEFT)
3814 c = Ctrl_P;
3815 else if (c == K_RIGHT)
3816 c = Ctrl_N;
3817 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003818
Bram Moolenaareadee482020-09-04 15:37:31 +02003819 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003820 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003821 && cclp->cmdpos > 1
3822 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3823 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3824 && (c == '\n' || c == '\r' || c == K_KENTER))
3825 c = K_DOWN;
3826
3827 return c;
3828}
3829
3830/*
3831 * Delete characters on the command line, from "from" to the current
3832 * position.
3833 */
3834 static void
3835cmdline_del(cmdline_info_T *cclp, int from)
3836{
3837 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3838 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3839 cclp->cmdlen -= cclp->cmdpos - from;
3840 cclp->cmdpos = from;
3841}
3842
3843/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003844 * Handle a key pressed when the wild menu for the menu names
3845 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003846 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003847 static int
3848wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003849{
Bram Moolenaareadee482020-09-04 15:37:31 +02003850 int i;
3851 int j;
3852
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003853 // Hitting <Down> after "emenu Name.": complete submenu
3854 if (key == K_DOWN && cclp->cmdpos > 0
3855 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003856 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003857 key = p_wc;
3858 KeyTyped = TRUE; // in case the key was mapped
3859 }
3860 else if (key == K_UP)
3861 {
3862 // Hitting <Up>: Remove one submenu name in front of the
3863 // cursor
3864 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003865
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003866 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3867 i = 0;
3868 while (--j > 0)
3869 {
3870 // check for start of menu name
3871 if (cclp->cmdbuff[j] == ' '
3872 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003873 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003874 i = j + 1;
3875 break;
3876 }
3877 // check for start of submenu name
3878 if (cclp->cmdbuff[j] == '.'
3879 && cclp->cmdbuff[j - 1] != '\\')
3880 {
3881 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003882 {
3883 i = j + 1;
3884 break;
3885 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003886 else
3887 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003888 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003889 }
3890 if (i > 0)
3891 cmdline_del(cclp, i);
3892 key = p_wc;
3893 KeyTyped = TRUE; // in case the key was mapped
3894 xp->xp_context = EXPAND_NOTHING;
3895 }
3896
3897 return key;
3898}
3899
3900/*
3901 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3902 * directory names (EXPAND_DIRECTORIES) or shell command names
3903 * (EXPAND_SHELLCMD) is displayed.
3904 */
3905 static int
3906wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3907{
3908 int i;
3909 int j;
3910 char_u upseg[5];
3911
3912 upseg[0] = PATHSEP;
3913 upseg[1] = '.';
3914 upseg[2] = '.';
3915 upseg[3] = PATHSEP;
3916 upseg[4] = NUL;
3917
3918 if (key == K_DOWN
3919 && cclp->cmdpos > 0
3920 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3921 && (cclp->cmdpos < 3
3922 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3923 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3924 {
3925 // go down a directory
3926 key = p_wc;
3927 KeyTyped = TRUE; // in case the key was mapped
3928 }
3929 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3930 {
3931 // If in a direct ancestor, strip off one ../ to go down
3932 int found = FALSE;
3933
3934 j = cclp->cmdpos;
3935 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3936 while (--j > i)
3937 {
3938 if (has_mbyte)
3939 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3940 if (vim_ispathsep(cclp->cmdbuff[j]))
3941 {
3942 found = TRUE;
3943 break;
3944 }
3945 }
3946 if (found
3947 && cclp->cmdbuff[j - 1] == '.'
3948 && cclp->cmdbuff[j - 2] == '.'
3949 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3950 {
3951 cmdline_del(cclp, j - 2);
3952 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003953 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003954 }
3955 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003956 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003957 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003958 // go up a directory
3959 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003960
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003961 j = cclp->cmdpos - 1;
3962 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3963 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003964 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003965 if (has_mbyte)
3966 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3967 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003968#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003969 && vim_strchr((char_u *)" *?[{`$%#",
3970 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003971#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003972 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003973 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003974 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003975 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003976 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003977 break;
3978 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003979 else
3980 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003981 }
3982 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003983
3984 if (!found)
3985 j = i;
3986 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
3987 j += 4;
3988 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
3989 && j == i)
3990 j += 3;
3991 else
3992 j = 0;
3993 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02003994 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003995 // TODO this is only for DOS/UNIX systems - need to put in
3996 // machine-specific stuff here and in upseg init
3997 cmdline_del(cclp, j);
3998 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02003999 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004000 else if (cclp->cmdpos > i)
4001 cmdline_del(cclp, i);
4002
4003 // Now complete in the new directory. Set KeyTyped in case the
4004 // Up key came from a mapping.
4005 key = p_wc;
4006 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004007 }
4008
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004009 return key;
4010}
4011
4012/*
4013 * Handle a key pressed when the wild menu is displayed
4014 */
4015 int
4016wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4017{
4018 if (xp->xp_context == EXPAND_MENUNAMES)
4019 return wildmenu_process_key_menunames(cclp, key, xp);
4020 else if ((xp->xp_context == EXPAND_FILES
4021 || xp->xp_context == EXPAND_DIRECTORIES
4022 || xp->xp_context == EXPAND_SHELLCMD))
4023 return wildmenu_process_key_filenames(cclp, key, xp);
4024
4025 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004026}
4027
4028/*
4029 * Free expanded names when finished walking through the matches
4030 */
4031 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004032wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004033{
4034 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004035
4036 if (!p_wmnu || wild_menu_showing == 0)
4037 return;
4038
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004039#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004040 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004041 if (cclp->input_fn)
4042 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004043#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004044
4045 if (wild_menu_showing == WM_SCROLLED)
4046 {
4047 // Entered command line, move it up
4048 cmdline_row--;
4049 redrawcmd();
4050 }
4051 else if (save_p_ls != -1)
4052 {
4053 // restore 'laststatus' and 'winminheight'
4054 p_ls = save_p_ls;
4055 p_wmh = save_p_wmh;
4056 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004057 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004058 redrawcmd();
4059 save_p_ls = -1;
4060 }
4061 else
4062 {
4063 win_redraw_last_status(topframe);
4064 redraw_statuslines();
4065 }
4066 KeyTyped = skt;
4067 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004068#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004069 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004070 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004071#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004072}
Bram Moolenaareadee482020-09-04 15:37:31 +02004073
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004074#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004075/*
4076 * "getcompletion()" function
4077 */
4078 void
4079f_getcompletion(typval_T *argvars, typval_T *rettv)
4080{
4081 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004082 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004083 expand_T xpc;
4084 int filtered = FALSE;
4085 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004086 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004087
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004088 if (in_vim9script()
4089 && (check_for_string_arg(argvars, 0) == FAIL
4090 || check_for_string_arg(argvars, 1) == FAIL
4091 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4092 return;
4093
ii144785fe02021-11-21 12:13:56 +00004094 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004095 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004096 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004097 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004098
4099 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004100 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004101
4102 if (p_wic)
4103 options |= WILD_ICASE;
4104
4105 // For filtered results, 'wildignore' is used
4106 if (!filtered)
4107 options |= WILD_KEEP_ALL;
4108
4109 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004110 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004111 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004112 int cmdline_len = (int)STRLEN(pat);
4113 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004114 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004115 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004116 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004117 else
4118 {
ii144785fe02021-11-21 12:13:56 +00004119 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004120 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004121 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004122
4123 xpc.xp_context = cmdcomplete_str_to_type(type);
4124 if (xpc.xp_context == EXPAND_NOTHING)
4125 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004126 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004127 return;
4128 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004129
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004130 if (xpc.xp_context == EXPAND_USER_DEFINED)
4131 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004132 // Must be "custom,funcname" pattern
4133 if (STRNCMP(type, "custom,", 7) != 0)
4134 {
4135 semsg(_(e_invalid_argument_str), type);
4136 return;
4137 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004138
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004139 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004140 }
4141
4142 if (xpc.xp_context == EXPAND_USER_LIST)
4143 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004144 // Must be "customlist,funcname" pattern
4145 if (STRNCMP(type, "customlist,", 11) != 0)
4146 {
4147 semsg(_(e_invalid_argument_str), type);
4148 return;
4149 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004150
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004151 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004152 }
4153
Bram Moolenaar66b51422019-08-18 21:44:12 +02004154# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004155 if (xpc.xp_context == EXPAND_MENUS)
4156 {
4157 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4158 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4159 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004160# endif
4161# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004162 if (xpc.xp_context == EXPAND_CSCOPE)
4163 {
4164 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4165 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4166 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004167# endif
4168# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004169 if (xpc.xp_context == EXPAND_SIGN)
4170 {
4171 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4172 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4173 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004174# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004175 if (xpc.xp_context == EXPAND_RUNTIME)
4176 {
4177 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4178 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4179 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004180 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004181
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004182 if (cmdline_fuzzy_completion_supported(&xpc))
4183 // when fuzzy matching, don't modify the search string
4184 pat = vim_strsave(xpc.xp_pattern);
4185 else
4186 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4187
Bram Moolenaar93a10962022-06-16 11:42:09 +01004188 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004189 {
4190 int i;
4191
4192 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4193
4194 for (i = 0; i < xpc.xp_numfiles; i++)
4195 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4196 }
4197 vim_free(pat);
4198 ExpandCleanup(&xpc);
4199}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004200#endif // FEAT_EVAL