blob: 8d8bf06b5fb9f836ef3b3c0190c0fafaa8370cd9 [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/*
zeertzjqd8c93402024-06-17 18:25:32 +0200441 * Returns the current cmdline completion pattern.
442 */
443 char_u *
444cmdline_compl_pattern(void)
445{
446 expand_T *xp = get_cmdline_info()->xpc;
447
448 return xp == NULL ? NULL : xp->xp_orig;
449}
450
451/*
452 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
453 */
454 int
455cmdline_compl_is_fuzzy(void)
456{
457 expand_T *xp = get_cmdline_info()->xpc;
458
459 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
460}
461
462/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000463 * Return the number of characters that should be skipped in a status match.
464 * These are backslashes used for escaping. Do show backslashes in help tags.
465 */
466 static int
467skip_status_match_char(expand_T *xp, char_u *s)
468{
469 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
470#ifdef FEAT_MENU
471 || ((xp->xp_context == EXPAND_MENUS
472 || xp->xp_context == EXPAND_MENUNAMES)
473 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
474#endif
475 )
476 {
477#ifndef BACKSLASH_IN_FILENAME
478 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
479 return 2;
480#endif
481 return 1;
482 }
483 return 0;
484}
485
486/*
487 * Get the length of an item as it will be shown in the status line.
488 */
489 static int
490status_match_len(expand_T *xp, char_u *s)
491{
492 int len = 0;
493
494#ifdef FEAT_MENU
495 int emenu = xp->xp_context == EXPAND_MENUS
496 || xp->xp_context == EXPAND_MENUNAMES;
497
498 // Check for menu separators - replace with '|'.
499 if (emenu && menu_is_separator(s))
500 return 1;
501#endif
502
503 while (*s != NUL)
504 {
505 s += skip_status_match_char(xp, s);
506 len += ptr2cells(s);
507 MB_PTR_ADV(s);
508 }
509
510 return len;
511}
512
513/*
514 * Show wildchar matches in the status line.
515 * Show at least the "match" item.
516 * We start at item 'first_match' in the list and show all matches that fit.
517 *
518 * If inversion is possible we use it. Else '=' characters are used.
519 */
520 static void
521win_redr_status_matches(
522 expand_T *xp,
523 int num_matches,
524 char_u **matches, // list of matches
525 int match,
526 int showtail)
527{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000528 int row;
529 char_u *buf;
530 int len;
531 int clen; // length in screen cells
532 int fillchar;
533 int attr;
534 int i;
535 int highlight = TRUE;
536 char_u *selstart = NULL;
537 int selstart_col = 0;
538 char_u *selend = NULL;
539 static int first_match = 0;
540 int add_left = FALSE;
541 char_u *s;
542#ifdef FEAT_MENU
543 int emenu;
544#endif
545 int l;
546
547 if (matches == NULL) // interrupted completion?
548 return;
549
550 if (has_mbyte)
551 buf = alloc(Columns * MB_MAXBYTES + 1);
552 else
553 buf = alloc(Columns + 1);
554 if (buf == NULL)
555 return;
556
557 if (match == -1) // don't show match but original text
558 {
559 match = 0;
560 highlight = FALSE;
561 }
562 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000563 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000564 if (match == 0)
565 first_match = 0;
566 else if (match < first_match)
567 {
568 // jumping left, as far as we can go
569 first_match = match;
570 add_left = TRUE;
571 }
572 else
573 {
574 // check if match fits on the screen
575 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000576 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000577 if (first_match > 0)
578 clen += 2;
579 // jumping right, put match at the left
580 if ((long)clen > Columns)
581 {
582 first_match = match;
583 // if showing the last match, we can add some on the left
584 clen = 2;
585 for (i = match; i < num_matches; ++i)
586 {
zeertzjqc51a3762022-12-10 10:22:29 +0000587 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000588 if ((long)clen >= Columns)
589 break;
590 }
591 if (i == num_matches)
592 add_left = TRUE;
593 }
594 }
595 if (add_left)
596 while (first_match > 0)
597 {
zeertzjqc51a3762022-12-10 10:22:29 +0000598 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000599 if ((long)clen >= Columns)
600 break;
601 --first_match;
602 }
603
604 fillchar = fillchar_status(&attr, curwin);
605
606 if (first_match == 0)
607 {
608 *buf = NUL;
609 len = 0;
610 }
611 else
612 {
613 STRCPY(buf, "< ");
614 len = 2;
615 }
616 clen = len;
617
618 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000619 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000620 {
621 if (i == match)
622 {
623 selstart = buf + len;
624 selstart_col = clen;
625 }
626
zeertzjqc51a3762022-12-10 10:22:29 +0000627 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000628 // Check for menu separators - replace with '|'
629#ifdef FEAT_MENU
630 emenu = (xp->xp_context == EXPAND_MENUS
631 || xp->xp_context == EXPAND_MENUNAMES);
632 if (emenu && menu_is_separator(s))
633 {
634 STRCPY(buf + len, transchar('|'));
635 l = (int)STRLEN(buf + len);
636 len += l;
637 clen += l;
638 }
639 else
640#endif
641 for ( ; *s != NUL; ++s)
642 {
643 s += skip_status_match_char(xp, s);
644 clen += ptr2cells(s);
645 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
646 {
647 STRNCPY(buf + len, s, l);
648 s += l - 1;
649 len += l;
650 }
651 else
652 {
653 STRCPY(buf + len, transchar_byte(*s));
654 len += (int)STRLEN(buf + len);
655 }
656 }
657 if (i == match)
658 selend = buf + len;
659
660 *(buf + len++) = ' ';
661 *(buf + len++) = ' ';
662 clen += 2;
663 if (++i == num_matches)
664 break;
665 }
666
667 if (i != num_matches)
668 {
669 *(buf + len++) = '>';
670 ++clen;
671 }
672
673 buf[len] = NUL;
674
675 row = cmdline_row - 1;
676 if (row >= 0)
677 {
678 if (wild_menu_showing == 0)
679 {
680 if (msg_scrolled > 0)
681 {
682 // Put the wildmenu just above the command line. If there is
683 // no room, scroll the screen one line up.
684 if (cmdline_row == Rows - 1)
685 {
686 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
687 ++msg_scrolled;
688 }
689 else
690 {
691 ++cmdline_row;
692 ++row;
693 }
694 wild_menu_showing = WM_SCROLLED;
695 }
696 else
697 {
698 // Create status line if needed by setting 'laststatus' to 2.
699 // Set 'winminheight' to zero to avoid that the window is
700 // resized.
701 if (lastwin->w_status_height == 0)
702 {
703 save_p_ls = p_ls;
704 save_p_wmh = p_wmh;
705 p_ls = 2;
706 p_wmh = 0;
707 last_status(FALSE);
708 }
709 wild_menu_showing = WM_SHOWN;
710 }
711 }
712
713 screen_puts(buf, row, 0, attr);
714 if (selstart != NULL && highlight)
715 {
716 *selend = NUL;
717 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
718 }
719
720 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
721 }
722
723 win_redraw_last_status(topframe);
724 vim_free(buf);
725}
726
727/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000728 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200729 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000730 */
731 static char_u *
732get_next_or_prev_match(
733 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200734 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000735{
zeertzjqe9ef3472023-08-17 23:57:05 +0200736 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000737 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000738
739 if (xp->xp_numfiles <= 0)
740 return NULL;
741
742 if (mode == WILD_PREV)
743 {
744 if (findex == -1)
745 findex = xp->xp_numfiles;
746 --findex;
747 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000748 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000749 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000750 else if (mode == WILD_PAGEUP)
751 {
752 if (findex == 0)
753 // at the first entry, don't select any entries
754 findex = -1;
755 else if (findex == -1)
756 // no entry is selected. select the last entry
757 findex = xp->xp_numfiles - 1;
758 else
759 {
760 // go up by the pum height
761 ht = pum_get_height();
762 if (ht > 3)
763 ht -= 2;
764 findex -= ht;
765 if (findex < 0)
766 // few entries left, select the first entry
767 findex = 0;
768 }
769 }
770 else // mode == WILD_PAGEDOWN
771 {
772 if (findex == xp->xp_numfiles - 1)
773 // at the last entry, don't select any entries
774 findex = -1;
775 else if (findex == -1)
776 // no entry is selected. select the first entry
777 findex = 0;
778 else
779 {
780 // go down by the pum height
781 ht = pum_get_height();
782 if (ht > 3)
783 ht -= 2;
784 findex += ht;
785 if (findex >= xp->xp_numfiles)
786 // few entries left, select the last entry
787 findex = xp->xp_numfiles - 1;
788 }
789 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000790
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000791 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000792 if (findex < 0)
793 {
zeertzjq28a23602023-09-29 19:58:35 +0200794 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000795 findex = xp->xp_numfiles - 1;
796 else
797 findex = -1;
798 }
799 if (findex >= xp->xp_numfiles)
800 {
zeertzjq28a23602023-09-29 19:58:35 +0200801 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000802 findex = 0;
803 else
804 findex = -1;
805 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000806 if (compl_match_array)
807 {
808 compl_selected = findex;
809 cmdline_pum_display();
810 }
811 else if (p_wmnu)
812 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
813 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200814 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000815
816 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200817 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000818
819 return vim_strsave(xp->xp_files[findex]);
820}
821
822/*
823 * Start the command-line expansion and get the matches.
824 */
825 static char_u *
826ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
827{
828 int non_suf_match; // number without matching suffix
829 int i;
830 char_u *ss = NULL;
831
832 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000833 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000834 options) == FAIL)
835 {
836#ifdef FNAME_ILLEGAL
837 // Illegal file name has been silently skipped. But when there
838 // are wildcards, the real problem is that there was no match,
839 // causing the pattern to be added, which has illegal characters.
840 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
841 semsg(_(e_no_match_str_2), str);
842#endif
843 }
844 else if (xp->xp_numfiles == 0)
845 {
846 if (!(options & WILD_SILENT))
847 semsg(_(e_no_match_str_2), str);
848 }
849 else
850 {
851 // Escape the matches for use on the command line.
852 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
853
854 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000855 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000856 {
857 if (xp->xp_numfiles)
858 non_suf_match = xp->xp_numfiles;
859 else
860 non_suf_match = 1;
861 if ((xp->xp_context == EXPAND_FILES
862 || xp->xp_context == EXPAND_DIRECTORIES)
863 && xp->xp_numfiles > 1)
864 {
865 // More than one match; check suffix.
866 // The files will have been sorted on matching suffix in
867 // expand_wildcards, only need to check the first two.
868 non_suf_match = 0;
869 for (i = 0; i < 2; ++i)
870 if (match_suffix(xp->xp_files[i]))
871 ++non_suf_match;
872 }
873 if (non_suf_match != 1)
874 {
875 // Can we ever get here unless it's while expanding
876 // interactively? If not, we can get rid of this all
877 // together. Don't really want to wait for this message
878 // (and possibly have to hit return to continue!).
879 if (!(options & WILD_SILENT))
880 emsg(_(e_too_many_file_names));
881 else if (!(options & WILD_NO_BEEP))
882 beep_flush();
883 }
884 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
885 ss = vim_strsave(xp->xp_files[0]);
886 }
887 }
888
889 return ss;
890}
891
892/*
893 * Return the longest common part in the list of cmdline completion matches.
894 */
895 static char_u *
896find_longest_match(expand_T *xp, int options)
897{
898 long_u len;
899 int mb_len = 1;
900 int c0, ci;
901 int i;
902 char_u *ss;
903
904 for (len = 0; xp->xp_files[0][len]; len += mb_len)
905 {
906 if (has_mbyte)
907 {
908 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
909 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
910 }
911 else
912 c0 = xp->xp_files[0][len];
913 for (i = 1; i < xp->xp_numfiles; ++i)
914 {
915 if (has_mbyte)
916 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
917 else
918 ci = xp->xp_files[i][len];
919 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
920 || xp->xp_context == EXPAND_FILES
921 || xp->xp_context == EXPAND_SHELLCMD
922 || xp->xp_context == EXPAND_BUFFERS))
923 {
924 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
925 break;
926 }
927 else if (c0 != ci)
928 break;
929 }
930 if (i < xp->xp_numfiles)
931 {
932 if (!(options & WILD_NO_BEEP))
933 vim_beep(BO_WILD);
934 break;
935 }
936 }
937
938 ss = alloc(len + 1);
939 if (ss)
940 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
941
942 return ss;
943}
944
945/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000946 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200947 * Chars that should not be expanded must be preceded with a backslash.
948 * Return a pointer to allocated memory containing the new string.
949 * Return NULL for failure.
950 *
951 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200952 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
953 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200954 *
955 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
956 * is WILD_EXPAND_FREE or WILD_ALL.
957 *
958 * mode = WILD_FREE: just free previously expanded matches
959 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
960 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
961 * mode = WILD_NEXT: use next match in multiple match, wrap to first
962 * mode = WILD_PREV: use previous match in multiple match, wrap to first
963 * mode = WILD_ALL: return all matches concatenated
964 * mode = WILD_LONGEST: return longest matched part
965 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000966 * mode = WILD_APPLY: apply the item selected in the cmdline completion
967 * popup menu and close the menu.
968 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
969 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200970 *
971 * options = WILD_LIST_NOTFOUND: list entries without a match
972 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
973 * options = WILD_USE_NL: Use '\n' for WILD_ALL
974 * options = WILD_NO_BEEP: Don't beep for multiple matches
975 * options = WILD_ADD_SLASH: add a slash after directory names
976 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
977 * options = WILD_SILENT: don't print warning messages
978 * options = WILD_ESCAPE: put backslash before special chars
979 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200980 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200981 *
982 * The variables xp->xp_context and xp->xp_backslash must have been set!
983 */
984 char_u *
985ExpandOne(
986 expand_T *xp,
987 char_u *str,
988 char_u *orig, // allocated copy of original of expanded string
989 int options,
990 int mode)
991{
992 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200993 int orig_saved = FALSE;
994 int i;
995 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200996
997 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000998 if (mode == WILD_NEXT || mode == WILD_PREV
999 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001000 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001001
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001002 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001003 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001004 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001005 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001006 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001007 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001008
Bram Moolenaar66b51422019-08-18 21:44:12 +02001009 // free old names
1010 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1011 {
1012 FreeWild(xp->xp_numfiles, xp->xp_files);
1013 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001014 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001015
1016 // The entries from xp_files may be used in the PUM, remove it.
1017 if (compl_match_array != NULL)
1018 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001019 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001020 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001021
1022 if (mode == WILD_FREE) // only release file name
1023 return NULL;
1024
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001025 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001026 {
zeertzjq28a23602023-09-29 19:58:35 +02001027 vim_free(xp->xp_orig);
1028 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001029 orig_saved = TRUE;
1030
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001031 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001032 }
1033
1034 // Find longest common part
1035 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1036 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001037 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001038 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001039 }
1040
Bram Moolenaar57e95172022-08-20 19:26:14 +01001041 // Concatenate all matching names. Unless interrupted, this can be slow
1042 // and the result probably won't be used.
1043 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001044 {
1045 len = 0;
1046 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001047 {
1048 if (i > 0)
1049 {
1050 if (xp->xp_prefix == XP_PREFIX_NO)
1051 len += 2; // prefix "no"
1052 else if (xp->xp_prefix == XP_PREFIX_INV)
1053 len += 3; // prefix "inv"
1054 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001055 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001056 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001057 ss = alloc(len);
1058 if (ss != NULL)
1059 {
1060 *ss = NUL;
1061 for (i = 0; i < xp->xp_numfiles; ++i)
1062 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001063 if (i > 0)
1064 {
1065 if (xp->xp_prefix == XP_PREFIX_NO)
1066 STRCAT(ss, "no");
1067 else if (xp->xp_prefix == XP_PREFIX_INV)
1068 STRCAT(ss, "inv");
1069 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001070 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001071
Bram Moolenaar66b51422019-08-18 21:44:12 +02001072 if (i != xp->xp_numfiles - 1)
1073 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1074 }
1075 }
1076 }
1077
1078 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1079 ExpandCleanup(xp);
1080
zeertzjq28a23602023-09-29 19:58:35 +02001081 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001082 if (!orig_saved)
1083 vim_free(orig);
1084
1085 return ss;
1086}
1087
1088/*
1089 * Prepare an expand structure for use.
1090 */
1091 void
1092ExpandInit(expand_T *xp)
1093{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001094 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001095 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001096 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001097 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001098}
1099
1100/*
1101 * Cleanup an expand structure after use.
1102 */
1103 void
1104ExpandCleanup(expand_T *xp)
1105{
1106 if (xp->xp_numfiles >= 0)
1107 {
1108 FreeWild(xp->xp_numfiles, xp->xp_files);
1109 xp->xp_numfiles = -1;
1110 }
zeertzjq28a23602023-09-29 19:58:35 +02001111 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001112}
1113
1114/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001115 * Display one line of completion matches. Multiple matches are displayed in
1116 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001117 * matches - list of completion match names
1118 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001119 * lines - number of output lines
1120 * linenr - line number of matches to display
1121 * maxlen - maximum number of characters in each line
1122 * showtail - display only the tail of the full path of a file name
1123 * dir_attr - highlight attribute to use for directory names
1124 */
1125 static void
1126showmatches_oneline(
1127 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001128 char_u **matches,
1129 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001130 int lines,
1131 int linenr,
1132 int maxlen,
1133 int showtail,
1134 int dir_attr)
1135{
1136 int i, j;
1137 int isdir;
1138 int lastlen;
1139 char_u *p;
1140
1141 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001142 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001143 {
1144 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1145 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001146 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1147 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001148 msg_advance(maxlen + 1);
1149 msg_puts((char *)p);
1150 msg_advance(maxlen + 3);
1151 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1152 break;
1153 }
1154 for (i = maxlen - lastlen; --i >= 0; )
1155 msg_putchar(' ');
1156 if (xp->xp_context == EXPAND_FILES
1157 || xp->xp_context == EXPAND_SHELLCMD
1158 || xp->xp_context == EXPAND_BUFFERS)
1159 {
1160 // highlight directories
1161 if (xp->xp_numfiles != -1)
1162 {
1163 char_u *halved_slash;
1164 char_u *exp_path;
1165 char_u *path;
1166
1167 // Expansion was done before and special characters
1168 // were escaped, need to halve backslashes. Also
1169 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001170 exp_path = expand_env_save_opt(matches[j], TRUE);
1171 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001172 halved_slash = backslash_halve_save(path);
1173 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001174 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001175 vim_free(exp_path);
1176 if (halved_slash != path)
1177 vim_free(halved_slash);
1178 }
1179 else
1180 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001181 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001182 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001183 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001184 else
1185 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001186 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001187 TRUE);
1188 p = NameBuff;
1189 }
1190 }
1191 else
1192 {
1193 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001194 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001195 }
1196 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1197 }
1198 if (msg_col > 0) // when not wrapped around
1199 {
1200 msg_clr_eos();
1201 msg_putchar('\n');
1202 }
1203 out_flush(); // show one line at a time
1204}
1205
1206/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001207 * Show all matches for completion on the command line.
1208 * Returns EXPAND_NOTHING when the character that triggered expansion should
1209 * be inserted like a normal character.
1210 */
1211 int
1212showmatches(expand_T *xp, int wildmenu UNUSED)
1213{
1214 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001215 int numMatches;
1216 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001217 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001218 int maxlen;
1219 int lines;
1220 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001221 int attr;
1222 int showtail;
1223
1224 if (xp->xp_numfiles == -1)
1225 {
1226 set_expand_context(xp);
1227 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001228 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001229 showtail = expand_showtail(xp);
1230 if (i != EXPAND_OK)
1231 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001232 }
1233 else
1234 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001235 numMatches = xp->xp_numfiles;
1236 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001237 showtail = cmd_showtail;
1238 }
1239
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001240 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001241 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001242 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001243
Bram Moolenaar66b51422019-08-18 21:44:12 +02001244 if (!wildmenu)
1245 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001246 msg_didany = FALSE; // lines_left will be set
1247 msg_start(); // prepare for paging
1248 msg_putchar('\n');
1249 out_flush();
1250 cmdline_row = msg_row;
1251 msg_didany = FALSE; // lines_left will be set again
1252 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001253 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001254
1255 if (got_int)
1256 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001258 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001259 else
1260 {
1261 // find the length of the longest file name
1262 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001263 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001264 {
1265 if (!showtail && (xp->xp_context == EXPAND_FILES
1266 || xp->xp_context == EXPAND_SHELLCMD
1267 || xp->xp_context == EXPAND_BUFFERS))
1268 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001269 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001270 j = vim_strsize(NameBuff);
1271 }
1272 else
zeertzjqc51a3762022-12-10 10:22:29 +00001273 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001274 if (j > maxlen)
1275 maxlen = j;
1276 }
1277
1278 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001279 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001280 else
1281 {
1282 // compute the number of columns and lines for the listing
1283 maxlen += 2; // two spaces between file names
1284 columns = ((int)Columns + 2) / maxlen;
1285 if (columns < 1)
1286 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001287 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001288 }
1289
1290 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1291
1292 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1293 {
1294 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1295 msg_clr_eos();
1296 msg_advance(maxlen - 3);
1297 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1298 }
1299
1300 // list the files line by line
1301 for (i = 0; i < lines; ++i)
1302 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001303 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001304 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001305 if (got_int)
1306 {
1307 got_int = FALSE;
1308 break;
1309 }
1310 }
1311
1312 // we redraw the command below the lines that we have just listed
1313 // This is a bit tricky, but it saves a lot of screen updating.
1314 cmdline_row = msg_row; // will put it back later
1315 }
1316
1317 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001318 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001319
1320 return EXPAND_OK;
1321}
1322
1323/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001324 * gettail() version for showmatches() and win_redr_status_matches():
1325 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001326 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001327 static char_u *
1328showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001329{
1330 char_u *p;
1331 char_u *t = s;
1332 int had_sep = FALSE;
1333
1334 for (p = s; *p != NUL; )
1335 {
1336 if (vim_ispathsep(*p)
1337#ifdef BACKSLASH_IN_FILENAME
1338 && !rem_backslash(p)
1339#endif
1340 )
1341 had_sep = TRUE;
1342 else if (had_sep)
1343 {
1344 t = p;
1345 had_sep = FALSE;
1346 }
1347 MB_PTR_ADV(p);
1348 }
1349 return t;
1350}
1351
1352/*
1353 * Return TRUE if we only need to show the tail of completion matches.
1354 * When not completing file names or there is a wildcard in the path FALSE is
1355 * returned.
1356 */
1357 static int
1358expand_showtail(expand_T *xp)
1359{
1360 char_u *s;
1361 char_u *end;
1362
1363 // When not completing file names a "/" may mean something different.
1364 if (xp->xp_context != EXPAND_FILES
1365 && xp->xp_context != EXPAND_SHELLCMD
1366 && xp->xp_context != EXPAND_DIRECTORIES)
1367 return FALSE;
1368
1369 end = gettail(xp->xp_pattern);
1370 if (end == xp->xp_pattern) // there is no path separator
1371 return FALSE;
1372
1373 for (s = xp->xp_pattern; s < end; s++)
1374 {
1375 // Skip escaped wildcards. Only when the backslash is not a path
1376 // separator, on DOS the '*' "path\*\file" must not be skipped.
1377 if (rem_backslash(s))
1378 ++s;
1379 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1380 return FALSE;
1381 }
1382 return TRUE;
1383}
1384
1385/*
1386 * Prepare a string for expansion.
1387 * When expanding file names: The string will be used with expand_wildcards().
1388 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1389 * When expanding other names: The string will be used with regcomp(). Copy
1390 * the name into allocated memory and prepend "^".
1391 */
1392 char_u *
1393addstar(
1394 char_u *fname,
1395 int len,
1396 int context) // EXPAND_FILES etc.
1397{
1398 char_u *retval;
1399 int i, j;
1400 int new_len;
1401 char_u *tail;
1402 int ends_in_star;
1403
1404 if (context != EXPAND_FILES
1405 && context != EXPAND_FILES_IN_PATH
1406 && context != EXPAND_SHELLCMD
1407 && context != EXPAND_DIRECTORIES)
1408 {
1409 // Matching will be done internally (on something other than files).
1410 // So we convert the file-matching-type wildcards into our kind for
1411 // use with vim_regcomp(). First work out how long it will be:
1412
1413 // For help tags the translation is done in find_help_tags().
1414 // For a tag pattern starting with "/" no translation is needed.
1415 if (context == EXPAND_HELP
1416 || context == EXPAND_COLORS
1417 || context == EXPAND_COMPILER
1418 || context == EXPAND_OWNSYNTAX
1419 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001420 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001421 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001422 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001423 || ((context == EXPAND_TAGS_LISTFILES
1424 || context == EXPAND_TAGS)
1425 && fname[0] == '/'))
1426 retval = vim_strnsave(fname, len);
1427 else
1428 {
1429 new_len = len + 2; // +2 for '^' at start, NUL at end
1430 for (i = 0; i < len; i++)
1431 {
1432 if (fname[i] == '*' || fname[i] == '~')
1433 new_len++; // '*' needs to be replaced by ".*"
1434 // '~' needs to be replaced by "\~"
1435
1436 // Buffer names are like file names. "." should be literal
1437 if (context == EXPAND_BUFFERS && fname[i] == '.')
1438 new_len++; // "." becomes "\."
1439
1440 // Custom expansion takes care of special things, match
1441 // backslashes literally (perhaps also for other types?)
1442 if ((context == EXPAND_USER_DEFINED
1443 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1444 new_len++; // '\' becomes "\\"
1445 }
1446 retval = alloc(new_len);
1447 if (retval != NULL)
1448 {
1449 retval[0] = '^';
1450 j = 1;
1451 for (i = 0; i < len; i++, j++)
1452 {
1453 // Skip backslash. But why? At least keep it for custom
1454 // expansion.
1455 if (context != EXPAND_USER_DEFINED
1456 && context != EXPAND_USER_LIST
1457 && fname[i] == '\\'
1458 && ++i == len)
1459 break;
1460
1461 switch (fname[i])
1462 {
1463 case '*': retval[j++] = '.';
1464 break;
1465 case '~': retval[j++] = '\\';
1466 break;
1467 case '?': retval[j] = '.';
1468 continue;
1469 case '.': if (context == EXPAND_BUFFERS)
1470 retval[j++] = '\\';
1471 break;
1472 case '\\': if (context == EXPAND_USER_DEFINED
1473 || context == EXPAND_USER_LIST)
1474 retval[j++] = '\\';
1475 break;
1476 }
1477 retval[j] = fname[i];
1478 }
1479 retval[j] = NUL;
1480 }
1481 }
1482 }
1483 else
1484 {
1485 retval = alloc(len + 4);
1486 if (retval != NULL)
1487 {
1488 vim_strncpy(retval, fname, len);
1489
1490 // Don't add a star to *, ~, ~user, $var or `cmd`.
1491 // * would become **, which walks the whole tree.
1492 // ~ would be at the start of the file name, but not the tail.
1493 // $ could be anywhere in the tail.
1494 // ` could be anywhere in the file name.
1495 // When the name ends in '$' don't add a star, remove the '$'.
1496 tail = gettail(retval);
1497 ends_in_star = (len > 0 && retval[len - 1] == '*');
1498#ifndef BACKSLASH_IN_FILENAME
1499 for (i = len - 2; i >= 0; --i)
1500 {
1501 if (retval[i] != '\\')
1502 break;
1503 ends_in_star = !ends_in_star;
1504 }
1505#endif
1506 if ((*retval != '~' || tail != retval)
1507 && !ends_in_star
1508 && vim_strchr(tail, '$') == NULL
1509 && vim_strchr(retval, '`') == NULL)
1510 retval[len++] = '*';
1511 else if (len > 0 && retval[len - 1] == '$')
1512 --len;
1513 retval[len] = NUL;
1514 }
1515 }
1516 return retval;
1517}
1518
1519/*
1520 * Must parse the command line so far to work out what context we are in.
1521 * Completion can then be done based on that context.
1522 * This routine sets the variables:
1523 * xp->xp_pattern The start of the pattern to be expanded within
1524 * the command line (ends at the cursor).
1525 * xp->xp_context The type of thing to expand. Will be one of:
1526 *
1527 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1528 * the command line, like an unknown command. Caller
1529 * should beep.
1530 * EXPAND_NOTHING Unrecognised context for completion, use char like
1531 * a normal char, rather than for completion. eg
1532 * :s/^I/
1533 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1534 * it.
1535 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1536 * EXPAND_FILES After command with EX_XFILE set, or after setting
1537 * with P_EXPAND set. eg :e ^I, :w>>^I
1538 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001539 * when we know only directories are of interest.
1540 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001541 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1542 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1543 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1544 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1545 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1546 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1547 * EXPAND_EVENTS Complete event names
1548 * EXPAND_SYNTAX Complete :syntax command arguments
1549 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1550 * EXPAND_AUGROUP Complete autocommand group names
1551 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1552 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1553 * eg :unmap a^I , :cunab x^I
1554 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1555 * eg :call sub^I
1556 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1557 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1558 * names in expressions, eg :while s^I
1559 * EXPAND_ENV_VARS Complete environment variable names
1560 * EXPAND_USER Complete user names
1561 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001562 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001563set_expand_context(expand_T *xp)
1564{
1565 cmdline_info_T *ccline = get_cmdline_info();
1566
1567 // only expansion for ':', '>' and '=' command-lines
1568 if (ccline->cmdfirstc != ':'
1569#ifdef FEAT_EVAL
1570 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1571 && !ccline->input_fn
1572#endif
1573 )
1574 {
1575 xp->xp_context = EXPAND_NOTHING;
1576 return;
1577 }
1578 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1579}
1580
Bram Moolenaard0190392019-08-23 21:17:35 +02001581/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001582 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1583 * For user defined commands, the completion context is set in 'xp' and the
1584 * completion flags in 'complp'.
1585 *
1586 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001587 */
1588 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001589set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001590{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001591 char_u *p = NULL;
1592 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001593 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001594
1595 // Isolate the command and search for it in the command table.
1596 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001597 // - the 'k' command can directly be followed by any character, but do
1598 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1599 // find matches anywhere in the command name, do this only for command
1600 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001601 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001602 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001603 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001604 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001605 p = cmd + 1;
1606 }
1607 else
1608 {
1609 p = cmd;
1610 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1611 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001612 // A user command may contain digits.
1613 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1614 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001615 while (ASCII_ISALNUM(*p) || *p == '*')
1616 ++p;
1617 // for python 3.x: ":py3*" commands completion
1618 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1619 {
1620 ++p;
1621 while (ASCII_ISALPHA(*p) || *p == '*')
1622 ++p;
1623 }
1624 // check for non-alpha command
1625 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1626 ++p;
1627 len = (int)(p - cmd);
1628
1629 if (len == 0)
1630 {
1631 xp->xp_context = EXPAND_UNSUCCESSFUL;
1632 return NULL;
1633 }
1634
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001635 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001636
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001637 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001638 // Also when doing fuzzy expansion for non-shell commands, support
1639 // alphanumeric characters.
1640 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1641 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001642 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1643 ++p;
1644 }
1645
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001646 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001647 // character, complete the command name.
1648 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1649 return NULL;
1650
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001651 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001652 {
1653 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1654 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001655 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001656 p = cmd + 1;
1657 }
1658 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1659 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001660 eap->cmd = cmd;
1661 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001662 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001663 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001664 }
1665 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001667 {
1668 // Not still touching the command and it was an illegal one
1669 xp->xp_context = EXPAND_UNSUCCESSFUL;
1670 return NULL;
1671 }
1672
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001673 return p;
1674}
Bram Moolenaard0190392019-08-23 21:17:35 +02001675
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001676/*
1677 * Set the completion context for a command argument with wild card characters.
1678 */
1679 static void
1680set_context_for_wildcard_arg(
1681 exarg_T *eap,
1682 char_u *arg,
1683 int usefilter,
1684 expand_T *xp,
1685 int *complp)
1686{
1687 char_u *p;
1688 int c;
1689 int in_quote = FALSE;
1690 char_u *bow = NULL; // Beginning of word
1691 int len = 0;
1692
1693 // Allow spaces within back-quotes to count as part of the argument
1694 // being expanded.
1695 xp->xp_pattern = skipwhite(arg);
1696 p = xp->xp_pattern;
1697 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001698 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001699 if (has_mbyte)
1700 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001701 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001702 c = *p;
1703 if (c == '\\' && p[1] != NUL)
1704 ++p;
1705 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001706 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001707 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001708 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001709 xp->xp_pattern = p;
1710 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001711 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001712 in_quote = !in_quote;
1713 }
1714 // An argument can contain just about everything, except
1715 // characters that end the command and white space.
1716 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001717#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001718 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001719#endif
1720 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001721 {
1722 len = 0; // avoid getting stuck when space is in 'isfname'
1723 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001724 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001725 if (has_mbyte)
1726 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001727 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001728 c = *p;
1729 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001730 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001731 if (has_mbyte)
1732 len = (*mb_ptr2len)(p);
1733 else
1734 len = 1;
1735 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001736 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001737 if (in_quote)
1738 bow = p;
1739 else
1740 xp->xp_pattern = p;
1741 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001742 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001743 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001744 }
1745
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001746 // If we are still inside the quotes, and we passed a space, just
1747 // expand from there.
1748 if (bow != NULL && in_quote)
1749 xp->xp_pattern = bow;
1750 xp->xp_context = EXPAND_FILES;
1751
1752 // For a shell command more chars need to be escaped.
1753 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1754 {
1755#ifndef BACKSLASH_IN_FILENAME
1756 xp->xp_shell = TRUE;
1757#endif
1758 // When still after the command name expand executables.
1759 if (xp->xp_pattern == skipwhite(arg))
1760 xp->xp_context = EXPAND_SHELLCMD;
1761 }
1762
1763 // Check for environment variable.
1764 if (*xp->xp_pattern == '$')
1765 {
1766 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1767 if (!vim_isIDc(*p))
1768 break;
1769 if (*p == NUL)
1770 {
1771 xp->xp_context = EXPAND_ENV_VARS;
1772 ++xp->xp_pattern;
1773 // Avoid that the assignment uses EXPAND_FILES again.
1774 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1775 *complp = EXPAND_ENV_VARS;
1776 }
1777 }
1778 // Check for user names.
1779 if (*xp->xp_pattern == '~')
1780 {
1781 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1782 ;
1783 // Complete ~user only if it partially matches a user name.
1784 // A full match ~user<Tab> will be replaced by user's home
1785 // directory i.e. something like ~user<Tab> -> /home/user/
1786 if (*p == NUL && p > xp->xp_pattern + 1
1787 && match_user(xp->xp_pattern + 1) >= 1)
1788 {
1789 xp->xp_context = EXPAND_USER;
1790 ++xp->xp_pattern;
1791 }
1792 }
1793}
1794
1795/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001796 * Set the completion context for the "++opt=arg" argument. Always returns
1797 * NULL.
1798 */
1799 static char_u *
1800set_context_in_argopt(expand_T *xp, char_u *arg)
1801{
1802 char_u *p;
1803
1804 p = vim_strchr(arg, '=');
1805 if (p == NULL)
1806 xp->xp_pattern = arg;
1807 else
1808 xp->xp_pattern = p + 1;
1809
1810 xp->xp_context = EXPAND_ARGOPT;
1811 return NULL;
1812}
1813
1814#ifdef FEAT_TERMINAL
1815/*
1816 * Set the completion context for :terminal's [options]. Always returns NULL.
1817 */
1818 static char_u *
1819set_context_in_terminalopt(expand_T *xp, char_u *arg)
1820{
1821 char_u *p;
1822
1823 p = vim_strchr(arg, '=');
1824 if (p == NULL)
1825 xp->xp_pattern = arg;
1826 else
1827 xp->xp_pattern = p + 1;
1828
1829 xp->xp_context = EXPAND_TERMINALOPT;
1830 return NULL;
1831}
1832#endif
1833
1834/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001835 * Set the completion context for the :filter command. Returns a pointer to the
1836 * next command after the :filter command.
1837 */
1838 static char_u *
1839set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1840{
1841 if (*arg != NUL)
1842 arg = skip_vimgrep_pat(arg, NULL, NULL);
1843 if (arg == NULL || *arg == NUL)
1844 {
1845 xp->xp_context = EXPAND_NOTHING;
1846 return NULL;
1847 }
1848 return skipwhite(arg);
1849}
1850
1851#ifdef FEAT_SEARCH_EXTRA
1852/*
1853 * Set the completion context for the :match command. Returns a pointer to the
1854 * next command after the :match command.
1855 */
1856 static char_u *
1857set_context_in_match_cmd(expand_T *xp, char_u *arg)
1858{
1859 if (*arg == NUL || !ends_excmd(*arg))
1860 {
1861 // also complete "None"
1862 set_context_in_echohl_cmd(xp, arg);
1863 arg = skipwhite(skiptowhite(arg));
1864 if (*arg != NUL)
1865 {
1866 xp->xp_context = EXPAND_NOTHING;
1867 arg = skip_regexp(arg + 1, *arg, magic_isset());
1868 }
1869 }
1870 return find_nextcmd(arg);
1871}
1872#endif
1873
1874/*
1875 * Returns a pointer to the next command after a :global or a :v command.
1876 * Returns NULL if there is no next command.
1877 */
1878 static char_u *
1879find_cmd_after_global_cmd(char_u *arg)
1880{
1881 int delim;
1882
1883 delim = *arg; // get the delimiter
1884 if (delim)
1885 ++arg; // skip delimiter if there is one
1886
1887 while (arg[0] != NUL && arg[0] != delim)
1888 {
1889 if (arg[0] == '\\' && arg[1] != NUL)
1890 ++arg;
1891 ++arg;
1892 }
1893 if (arg[0] != NUL)
1894 return arg + 1;
1895
1896 return NULL;
1897}
1898
1899/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001900 * Returns a pointer to the next command after a :substitute or a :& command.
1901 * Returns NULL if there is no next command.
1902 */
1903 static char_u *
1904find_cmd_after_substitute_cmd(char_u *arg)
1905{
1906 int delim;
1907
1908 delim = *arg;
1909 if (delim)
1910 {
1911 // skip "from" part
1912 ++arg;
1913 arg = skip_regexp(arg, delim, magic_isset());
1914
1915 if (arg[0] != NUL && arg[0] == delim)
1916 {
1917 // skip "to" part
1918 ++arg;
1919 while (arg[0] != NUL && arg[0] != delim)
1920 {
1921 if (arg[0] == '\\' && arg[1] != NUL)
1922 ++arg;
1923 ++arg;
1924 }
1925 if (arg[0] != NUL) // skip delimiter
1926 ++arg;
1927 }
1928 }
1929 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1930 ++arg;
1931 if (arg[0] != NUL)
1932 return arg;
1933
1934 return NULL;
1935}
1936
1937/*
1938 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1939 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1940 * Returns NULL if there is no next command.
1941 */
1942 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001943find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001944{
1945 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001946 if (*arg != '/')
1947 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001948
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001949 // Match regexp, not just whole words
1950 for (++arg; *arg && *arg != '/'; arg++)
1951 if (*arg == '\\' && arg[1] != NUL)
1952 arg++;
1953 if (*arg)
1954 {
1955 arg = skipwhite(arg + 1);
1956
1957 // Check for trailing illegal characters
1958 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1959 xp->xp_context = EXPAND_NOTHING;
1960 else
1961 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001962 }
1963
1964 return NULL;
1965}
1966
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001967#ifdef FEAT_EVAL
1968/*
1969 * Set the completion context for the :unlet command. Always returns NULL.
1970 */
1971 static char_u *
1972set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1973{
1974 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1975 arg = xp->xp_pattern + 1;
1976
1977 xp->xp_context = EXPAND_USER_VARS;
1978 xp->xp_pattern = arg;
1979
1980 if (*xp->xp_pattern == '$')
1981 {
1982 xp->xp_context = EXPAND_ENV_VARS;
1983 ++xp->xp_pattern;
1984 }
1985
1986 return NULL;
1987}
1988#endif
1989
1990#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1991/*
1992 * Set the completion context for the :language command. Always returns NULL.
1993 */
1994 static char_u *
1995set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1996{
1997 char_u *p;
1998
1999 p = skiptowhite(arg);
2000 if (*p == NUL)
2001 {
2002 xp->xp_context = EXPAND_LANGUAGE;
2003 xp->xp_pattern = arg;
2004 }
2005 else
2006 {
2007 if ( STRNCMP(arg, "messages", p - arg) == 0
2008 || STRNCMP(arg, "ctype", p - arg) == 0
2009 || STRNCMP(arg, "time", p - arg) == 0
2010 || STRNCMP(arg, "collate", p - arg) == 0)
2011 {
2012 xp->xp_context = EXPAND_LOCALES;
2013 xp->xp_pattern = skipwhite(p);
2014 }
2015 else
2016 xp->xp_context = EXPAND_NOTHING;
2017 }
2018
2019 return NULL;
2020}
2021#endif
2022
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002023#ifdef FEAT_EVAL
2024static enum
2025{
2026 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002027 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2028 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002029} breakpt_expand_what;
2030
2031/*
2032 * Set the completion context for the :breakadd command. Always returns NULL.
2033 */
2034 static char_u *
2035set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2036{
2037 char_u *p;
2038 char_u *subcmd_start;
2039
2040 xp->xp_context = EXPAND_BREAKPOINT;
2041 xp->xp_pattern = arg;
2042
2043 if (cmdidx == CMD_breakadd)
2044 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002045 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002046 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002047 else
2048 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002049
2050 p = skipwhite(arg);
2051 if (*p == NUL)
2052 return NULL;
2053 subcmd_start = p;
2054
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002055 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002056 {
2057 // :breakadd file [lnum] <filename>
2058 // :breakadd func [lnum] <funcname>
2059 p += 4;
2060 p = skipwhite(p);
2061
2062 // skip line number (if specified)
2063 if (VIM_ISDIGIT(*p))
2064 {
2065 p = skipdigits(p);
2066 if (*p != ' ')
2067 {
2068 xp->xp_context = EXPAND_NOTHING;
2069 return NULL;
2070 }
2071 p = skipwhite(p);
2072 }
2073 if (STRNCMP("file", subcmd_start, 4) == 0)
2074 xp->xp_context = EXPAND_FILES;
2075 else
2076 xp->xp_context = EXPAND_USER_FUNC;
2077 xp->xp_pattern = p;
2078 }
2079 else if (STRNCMP("expr ", p, 5) == 0)
2080 {
2081 // :breakadd expr <expression>
2082 xp->xp_context = EXPAND_EXPRESSION;
2083 xp->xp_pattern = skipwhite(p + 5);
2084 }
2085
2086 return NULL;
2087}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002088
2089 static char_u *
2090set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2091{
2092 char_u *p;
2093
2094 xp->xp_context = EXPAND_NOTHING;
2095 xp->xp_pattern = NULL;
2096
2097 p = skipwhite(arg);
2098 if (VIM_ISDIGIT(*p))
2099 return NULL;
2100
2101 xp->xp_context = EXPAND_SCRIPTNAMES;
2102 xp->xp_pattern = p;
2103
2104 return NULL;
2105}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002106#endif
2107
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002108/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002109 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2110 * The argument to the command is 'arg' and the argument flags is 'argt'.
2111 * For user-defined commands and for environment variables, 'compl' has the
2112 * completion type.
2113 * Returns a pointer to the next command. Returns NULL if there is no next
2114 * command.
2115 */
2116 static char_u *
2117set_context_by_cmdname(
2118 char_u *cmd,
2119 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002120 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002121 char_u *arg,
2122 long argt,
2123 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002124 int forceit)
2125{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002126 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002127 {
2128 case CMD_find:
2129 case CMD_sfind:
2130 case CMD_tabfind:
2131 if (xp->xp_context == EXPAND_FILES)
2132 xp->xp_context = EXPAND_FILES_IN_PATH;
2133 break;
2134 case CMD_cd:
2135 case CMD_chdir:
2136 case CMD_tcd:
2137 case CMD_tchdir:
2138 case CMD_lcd:
2139 case CMD_lchdir:
2140 if (xp->xp_context == EXPAND_FILES)
2141 xp->xp_context = EXPAND_DIRECTORIES;
2142 break;
2143 case CMD_help:
2144 xp->xp_context = EXPAND_HELP;
2145 xp->xp_pattern = arg;
2146 break;
2147
2148 // Command modifiers: return the argument.
2149 // Also for commands with an argument that is a command.
2150 case CMD_aboveleft:
2151 case CMD_argdo:
2152 case CMD_belowright:
2153 case CMD_botright:
2154 case CMD_browse:
2155 case CMD_bufdo:
2156 case CMD_cdo:
2157 case CMD_cfdo:
2158 case CMD_confirm:
2159 case CMD_debug:
2160 case CMD_folddoclosed:
2161 case CMD_folddoopen:
2162 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002163 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002164 case CMD_keepalt:
2165 case CMD_keepjumps:
2166 case CMD_keepmarks:
2167 case CMD_keeppatterns:
2168 case CMD_ldo:
2169 case CMD_leftabove:
2170 case CMD_lfdo:
2171 case CMD_lockmarks:
2172 case CMD_noautocmd:
2173 case CMD_noswapfile:
2174 case CMD_rightbelow:
2175 case CMD_sandbox:
2176 case CMD_silent:
2177 case CMD_tab:
2178 case CMD_tabdo:
2179 case CMD_topleft:
2180 case CMD_verbose:
2181 case CMD_vertical:
2182 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002183 case CMD_vim9cmd:
2184 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002185 return arg;
2186
2187 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002188 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002189
2190#ifdef FEAT_SEARCH_EXTRA
2191 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002192 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002193#endif
2194
2195 // All completion for the +cmdline_compl feature goes here.
2196
2197 case CMD_command:
2198 return set_context_in_user_cmd(xp, arg);
2199
2200 case CMD_delcommand:
2201 xp->xp_context = EXPAND_USER_COMMANDS;
2202 xp->xp_pattern = arg;
2203 break;
2204
2205 case CMD_global:
2206 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002207 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002208 case CMD_and:
2209 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002210 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002211 case CMD_isearch:
2212 case CMD_dsearch:
2213 case CMD_ilist:
2214 case CMD_dlist:
2215 case CMD_ijump:
2216 case CMD_psearch:
2217 case CMD_djump:
2218 case CMD_isplit:
2219 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002220 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002221 case CMD_autocmd:
2222 return set_context_in_autocmd(xp, arg, FALSE);
2223 case CMD_doautocmd:
2224 case CMD_doautoall:
2225 return set_context_in_autocmd(xp, arg, TRUE);
2226 case CMD_set:
2227 set_context_in_set_cmd(xp, arg, 0);
2228 break;
2229 case CMD_setglobal:
2230 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2231 break;
2232 case CMD_setlocal:
2233 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2234 break;
2235 case CMD_tag:
2236 case CMD_stag:
2237 case CMD_ptag:
2238 case CMD_ltag:
2239 case CMD_tselect:
2240 case CMD_stselect:
2241 case CMD_ptselect:
2242 case CMD_tjump:
2243 case CMD_stjump:
2244 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002245 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002246 xp->xp_context = EXPAND_TAGS_LISTFILES;
2247 else
2248 xp->xp_context = EXPAND_TAGS;
2249 xp->xp_pattern = arg;
2250 break;
2251 case CMD_augroup:
2252 xp->xp_context = EXPAND_AUGROUP;
2253 xp->xp_pattern = arg;
2254 break;
2255#ifdef FEAT_SYN_HL
2256 case CMD_syntax:
2257 set_context_in_syntax_cmd(xp, arg);
2258 break;
2259#endif
2260#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002261 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002262 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002263 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002264 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002265 case CMD_if:
2266 case CMD_elseif:
2267 case CMD_while:
2268 case CMD_for:
2269 case CMD_echo:
2270 case CMD_echon:
2271 case CMD_execute:
2272 case CMD_echomsg:
2273 case CMD_echoerr:
2274 case CMD_call:
2275 case CMD_return:
2276 case CMD_cexpr:
2277 case CMD_caddexpr:
2278 case CMD_cgetexpr:
2279 case CMD_lexpr:
2280 case CMD_laddexpr:
2281 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002282 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002283 break;
2284
2285 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002286 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002287 case CMD_function:
2288 case CMD_delfunction:
2289 xp->xp_context = EXPAND_USER_FUNC;
2290 xp->xp_pattern = arg;
2291 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002292 case CMD_disassemble:
2293 set_context_in_disassemble_cmd(xp, arg);
2294 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002295
2296 case CMD_echohl:
2297 set_context_in_echohl_cmd(xp, arg);
2298 break;
2299#endif
2300 case CMD_highlight:
2301 set_context_in_highlight_cmd(xp, arg);
2302 break;
2303#ifdef FEAT_CSCOPE
2304 case CMD_cscope:
2305 case CMD_lcscope:
2306 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002307 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002308 break;
2309#endif
2310#ifdef FEAT_SIGNS
2311 case CMD_sign:
2312 set_context_in_sign_cmd(xp, arg);
2313 break;
2314#endif
2315 case CMD_bdelete:
2316 case CMD_bwipeout:
2317 case CMD_bunload:
2318 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2319 arg = xp->xp_pattern + 1;
2320 // FALLTHROUGH
2321 case CMD_buffer:
2322 case CMD_sbuffer:
2323 case CMD_checktime:
2324 xp->xp_context = EXPAND_BUFFERS;
2325 xp->xp_pattern = arg;
2326 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002327#ifdef FEAT_DIFF
2328 case CMD_diffget:
2329 case CMD_diffput:
2330 // If current buffer is in diff mode, complete buffer names
2331 // which are in diff mode, and different than current buffer.
2332 xp->xp_context = EXPAND_DIFF_BUFFERS;
2333 xp->xp_pattern = arg;
2334 break;
2335#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002336 case CMD_USER:
2337 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002338 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2339 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002340
2341 case CMD_map: case CMD_noremap:
2342 case CMD_nmap: case CMD_nnoremap:
2343 case CMD_vmap: case CMD_vnoremap:
2344 case CMD_omap: case CMD_onoremap:
2345 case CMD_imap: case CMD_inoremap:
2346 case CMD_cmap: case CMD_cnoremap:
2347 case CMD_lmap: case CMD_lnoremap:
2348 case CMD_smap: case CMD_snoremap:
2349 case CMD_tmap: case CMD_tnoremap:
2350 case CMD_xmap: case CMD_xnoremap:
2351 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002352 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002353 case CMD_unmap:
2354 case CMD_nunmap:
2355 case CMD_vunmap:
2356 case CMD_ounmap:
2357 case CMD_iunmap:
2358 case CMD_cunmap:
2359 case CMD_lunmap:
2360 case CMD_sunmap:
2361 case CMD_tunmap:
2362 case CMD_xunmap:
2363 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002364 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002365 case CMD_mapclear:
2366 case CMD_nmapclear:
2367 case CMD_vmapclear:
2368 case CMD_omapclear:
2369 case CMD_imapclear:
2370 case CMD_cmapclear:
2371 case CMD_lmapclear:
2372 case CMD_smapclear:
2373 case CMD_tmapclear:
2374 case CMD_xmapclear:
2375 xp->xp_context = EXPAND_MAPCLEAR;
2376 xp->xp_pattern = arg;
2377 break;
2378
2379 case CMD_abbreviate: case CMD_noreabbrev:
2380 case CMD_cabbrev: case CMD_cnoreabbrev:
2381 case CMD_iabbrev: case CMD_inoreabbrev:
2382 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002383 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002384 case CMD_unabbreviate:
2385 case CMD_cunabbrev:
2386 case CMD_iunabbrev:
2387 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002388 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002389#ifdef FEAT_MENU
2390 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2391 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2392 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2393 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2394 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2395 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2396 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2397 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2398 case CMD_tmenu: case CMD_tunmenu:
2399 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2400 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2401#endif
2402
2403 case CMD_colorscheme:
2404 xp->xp_context = EXPAND_COLORS;
2405 xp->xp_pattern = arg;
2406 break;
2407
2408 case CMD_compiler:
2409 xp->xp_context = EXPAND_COMPILER;
2410 xp->xp_pattern = arg;
2411 break;
2412
2413 case CMD_ownsyntax:
2414 xp->xp_context = EXPAND_OWNSYNTAX;
2415 xp->xp_pattern = arg;
2416 break;
2417
2418 case CMD_setfiletype:
2419 xp->xp_context = EXPAND_FILETYPE;
2420 xp->xp_pattern = arg;
2421 break;
2422
2423 case CMD_packadd:
2424 xp->xp_context = EXPAND_PACKADD;
2425 xp->xp_pattern = arg;
2426 break;
2427
zeertzjqb0d45ec2023-01-25 15:04:22 +00002428 case CMD_runtime:
2429 set_context_in_runtime_cmd(xp, arg);
2430 break;
2431
Bram Moolenaard0190392019-08-23 21:17:35 +02002432#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2433 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002434 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002435#endif
2436#if defined(FEAT_PROFILE)
2437 case CMD_profile:
2438 set_context_in_profile_cmd(xp, arg);
2439 break;
2440#endif
2441 case CMD_behave:
2442 xp->xp_context = EXPAND_BEHAVE;
2443 xp->xp_pattern = arg;
2444 break;
2445
2446 case CMD_messages:
2447 xp->xp_context = EXPAND_MESSAGES;
2448 xp->xp_pattern = arg;
2449 break;
2450
2451 case CMD_history:
2452 xp->xp_context = EXPAND_HISTORY;
2453 xp->xp_pattern = arg;
2454 break;
2455#if defined(FEAT_PROFILE)
2456 case CMD_syntime:
2457 xp->xp_context = EXPAND_SYNTIME;
2458 xp->xp_pattern = arg;
2459 break;
2460#endif
2461
2462 case CMD_argdelete:
2463 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2464 arg = xp->xp_pattern + 1;
2465 xp->xp_context = EXPAND_ARGLIST;
2466 xp->xp_pattern = arg;
2467 break;
2468
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002469#ifdef FEAT_EVAL
2470 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002471 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002472 case CMD_breakdel:
2473 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002474
2475 case CMD_scriptnames:
2476 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002477#endif
2478
Bram Moolenaard0190392019-08-23 21:17:35 +02002479 default:
2480 break;
2481 }
2482 return NULL;
2483}
2484
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002485/*
2486 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2487 * we don't need/want deleted. Maybe this could be done better if we didn't
2488 * repeat all this stuff. The only problem is that they may not stay
2489 * perfectly compatible with each other, but then the command line syntax
2490 * probably won't change that much -- webb.
2491 */
2492 static char_u *
2493set_one_cmd_context(
2494 expand_T *xp,
2495 char_u *buff) // buffer for command string
2496{
2497 char_u *p;
2498 char_u *cmd, *arg;
2499 int len = 0;
2500 exarg_T ea;
2501 int compl = EXPAND_NOTHING;
2502 int forceit = FALSE;
2503 int usefilter = FALSE; // filter instead of file name
2504
2505 ExpandInit(xp);
2506 xp->xp_pattern = buff;
2507 xp->xp_line = buff;
2508 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2509 ea.argt = 0;
2510
2511 // 1. skip comment lines and leading space, colons or bars
2512 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2513 ;
2514 xp->xp_pattern = cmd;
2515
2516 if (*cmd == NUL)
2517 return NULL;
2518 if (*cmd == '"') // ignore comment lines
2519 {
2520 xp->xp_context = EXPAND_NOTHING;
2521 return NULL;
2522 }
2523
2524 // 3. Skip over the range to find the command.
2525 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2526 xp->xp_pattern = cmd;
2527 if (*cmd == NUL)
2528 return NULL;
2529 if (*cmd == '"')
2530 {
2531 xp->xp_context = EXPAND_NOTHING;
2532 return NULL;
2533 }
2534
2535 if (*cmd == '|' || *cmd == '\n')
2536 return cmd + 1; // There's another command
2537
2538 // Get the command index.
2539 p = set_cmd_index(cmd, &ea, xp, &compl);
2540 if (p == NULL)
2541 return NULL;
2542
2543 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2544
2545 if (*p == '!') // forced commands
2546 {
2547 forceit = TRUE;
2548 ++p;
2549 }
2550
2551 // 6. parse arguments
2552 if (!IS_USER_CMDIDX(ea.cmdidx))
2553 ea.argt = excmd_get_argt(ea.cmdidx);
2554
2555 arg = skipwhite(p);
2556
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002557 // Does command allow "++argopt" argument?
2558 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002559 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002560 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2561 {
2562 p = arg + 2;
2563 while (*p && !vim_isspace(*p))
2564 MB_PTR_ADV(p);
2565
2566 // Still touching the command after "++"?
2567 if (*p == NUL)
2568 {
2569 if (ea.argt & EX_ARGOPT)
2570 return set_context_in_argopt(xp, arg + 2);
2571#ifdef FEAT_TERMINAL
2572 if (ea.cmdidx == CMD_terminal)
2573 return set_context_in_terminalopt(xp, arg + 2);
2574#endif
2575 }
2576
2577 arg = skipwhite(p);
2578 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002579 }
2580
2581 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2582 {
2583 if (*arg == '>') // append
2584 {
2585 if (*++arg == '>')
2586 ++arg;
2587 arg = skipwhite(arg);
2588 }
2589 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2590 {
2591 ++arg;
2592 usefilter = TRUE;
2593 }
2594 }
2595
2596 if (ea.cmdidx == CMD_read)
2597 {
2598 usefilter = forceit; // :r! filter if forced
2599 if (*arg == '!') // :r !filter
2600 {
2601 ++arg;
2602 usefilter = TRUE;
2603 }
2604 }
2605
2606 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2607 {
2608 while (*arg == *cmd) // allow any number of '>' or '<'
2609 ++arg;
2610 arg = skipwhite(arg);
2611 }
2612
2613 // Does command allow "+command"?
2614 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2615 {
2616 // Check if we're in the +command
2617 p = arg + 1;
2618 arg = skip_cmd_arg(arg, FALSE);
2619
2620 // Still touching the command after '+'?
2621 if (*arg == NUL)
2622 return p;
2623
2624 // Skip space(s) after +command to get to the real argument
2625 arg = skipwhite(arg);
2626 }
2627
2628
2629 // Check for '|' to separate commands and '"' to start comments.
2630 // Don't do this for ":read !cmd" and ":write !cmd".
2631 if ((ea.argt & EX_TRLBAR) && !usefilter)
2632 {
2633 p = arg;
2634 // ":redir @" is not the start of a comment
2635 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2636 p += 2;
2637 while (*p)
2638 {
2639 if (*p == Ctrl_V)
2640 {
2641 if (p[1] != NUL)
2642 ++p;
2643 }
2644 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2645 || *p == '|' || *p == '\n')
2646 {
2647 if (*(p - 1) != '\\')
2648 {
2649 if (*p == '|' || *p == '\n')
2650 return p + 1;
2651 return NULL; // It's a comment
2652 }
2653 }
2654 MB_PTR_ADV(p);
2655 }
2656 }
2657
2658 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2659 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2660 // no arguments allowed but there is something
2661 return NULL;
2662
2663 // Find start of last argument (argument just before cursor):
2664 p = buff;
2665 xp->xp_pattern = p;
2666 len = (int)STRLEN(buff);
2667 while (*p && p < buff + len)
2668 {
2669 if (*p == ' ' || *p == TAB)
2670 {
2671 // argument starts after a space
2672 xp->xp_pattern = ++p;
2673 }
2674 else
2675 {
2676 if (*p == '\\' && *(p + 1) != NUL)
2677 ++p; // skip over escaped character
2678 MB_PTR_ADV(p);
2679 }
2680 }
2681
2682 if (ea.argt & EX_XFILE)
2683 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2684
2685 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002686 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002687 forceit);
2688}
2689
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002690/*
2691 * Set the completion context in 'xp' for command 'str'
2692 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002693 void
2694set_cmd_context(
2695 expand_T *xp,
2696 char_u *str, // start of command line
2697 int len, // length of command line (excl. NUL)
2698 int col, // position of cursor
2699 int use_ccline UNUSED) // use ccline for info
2700{
2701#ifdef FEAT_EVAL
2702 cmdline_info_T *ccline = get_cmdline_info();
2703#endif
2704 int old_char = NUL;
2705 char_u *nextcomm;
2706
2707 // Avoid a UMR warning from Purify, only save the character if it has been
2708 // written before.
2709 if (col < len)
2710 old_char = str[col];
2711 str[col] = NUL;
2712 nextcomm = str;
2713
2714#ifdef FEAT_EVAL
2715 if (use_ccline && ccline->cmdfirstc == '=')
2716 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002717 // pass CMD_SIZE because there is no real command
2718 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002719 }
2720 else if (use_ccline && ccline->input_fn)
2721 {
2722 xp->xp_context = ccline->xp_context;
2723 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002724 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002725 }
2726 else
2727#endif
2728 while (nextcomm != NULL)
2729 nextcomm = set_one_cmd_context(xp, nextcomm);
2730
2731 // Store the string here so that call_user_expand_func() can get to them
2732 // easily.
2733 xp->xp_line = str;
2734 xp->xp_col = col;
2735
2736 str[col] = old_char;
2737}
2738
2739/*
2740 * Expand the command line "str" from context "xp".
2741 * "xp" must have been set by set_cmd_context().
2742 * xp->xp_pattern points into "str", to where the text that is to be expanded
2743 * starts.
2744 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2745 * cursor.
2746 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2747 * key that triggered expansion literally.
2748 * Returns EXPAND_OK otherwise.
2749 */
2750 int
2751expand_cmdline(
2752 expand_T *xp,
2753 char_u *str, // start of command line
2754 int col, // position of cursor
2755 int *matchcount, // return: nr of matches
2756 char_u ***matches) // return: array of pointers to matches
2757{
2758 char_u *file_str = NULL;
2759 int options = WILD_ADD_SLASH|WILD_SILENT;
2760
2761 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2762 {
2763 beep_flush();
2764 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2765 }
2766 if (xp->xp_context == EXPAND_NOTHING)
2767 {
2768 // Caller can use the character as a normal char instead
2769 return EXPAND_NOTHING;
2770 }
2771
2772 // add star to file name, or convert to regexp if not exp. files.
2773 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002774 if (cmdline_fuzzy_completion_supported(xp))
2775 // If fuzzy matching, don't modify the search string
2776 file_str = vim_strsave(xp->xp_pattern);
2777 else
2778 {
2779 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2780 if (file_str == NULL)
2781 return EXPAND_UNSUCCESSFUL;
2782 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002783
2784 if (p_wic)
2785 options += WILD_ICASE;
2786
2787 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002788 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002789 {
2790 *matchcount = 0;
2791 *matches = NULL;
2792 }
2793 vim_free(file_str);
2794
2795 return EXPAND_OK;
2796}
2797
Bram Moolenaar66b51422019-08-18 21:44:12 +02002798/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002799 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002800 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002801 */
2802 static int
2803expand_files_and_dirs(
2804 expand_T *xp,
2805 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002806 char_u ***matches,
2807 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002808 int flags,
2809 int options)
2810{
2811 int free_pat = FALSE;
2812 int i;
2813 int ret;
2814
2815 // for ":set path=" and ":set tags=" halve backslashes for escaped
2816 // space
2817 if (xp->xp_backslash != XP_BS_NONE)
2818 {
2819 free_pat = TRUE;
2820 pat = vim_strsave(pat);
2821 for (i = 0; pat[i]; ++i)
2822 if (pat[i] == '\\')
2823 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002824 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002825 && pat[i + 1] == '\\'
2826 && pat[i + 2] == '\\'
2827 && pat[i + 3] == ' ')
2828 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002829 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002830 && pat[i + 1] == ' ')
2831 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002832 else if ((xp->xp_backslash & XP_BS_COMMA)
2833 && pat[i + 1] == '\\'
2834 && pat[i + 2] == ',')
2835 STRMOVE(pat + i, pat + i + 2);
2836#ifdef BACKSLASH_IN_FILENAME
2837 else if ((xp->xp_backslash & XP_BS_COMMA)
2838 && pat[i + 1] == ',')
2839 STRMOVE(pat + i, pat + i + 1);
2840#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002841 }
2842 }
2843
2844 if (xp->xp_context == EXPAND_FILES)
2845 flags |= EW_FILE;
2846 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2847 flags |= (EW_FILE | EW_PATH);
2848 else
2849 flags = (flags | EW_DIR) & ~EW_FILE;
2850 if (options & WILD_ICASE)
2851 flags |= EW_ICASE;
2852
2853 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002854 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002855 if (free_pat)
2856 vim_free(pat);
2857#ifdef BACKSLASH_IN_FILENAME
2858 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2859 {
2860 int j;
2861
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002862 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002863 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002864 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002865
2866 while (*ptr != NUL)
2867 {
2868 if (p_csl[0] == 's' && *ptr == '\\')
2869 *ptr = '/';
2870 else if (p_csl[0] == 'b' && *ptr == '/')
2871 *ptr = '\\';
2872 ptr += (*mb_ptr2len)(ptr);
2873 }
2874 }
2875 }
2876#endif
2877 return ret;
2878}
2879
2880/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002881 * Function given to ExpandGeneric() to obtain the possible arguments of the
2882 * ":behave {mswin,xterm}" command.
2883 */
2884 static char_u *
2885get_behave_arg(expand_T *xp UNUSED, int idx)
2886{
2887 if (idx == 0)
2888 return (char_u *)"mswin";
2889 if (idx == 1)
2890 return (char_u *)"xterm";
2891 return NULL;
2892}
2893
K.Takata161b6ac2022-11-14 15:31:07 +00002894#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002895/*
2896 * Function given to ExpandGeneric() to obtain the possible arguments of the
2897 * ":breakadd {expr, file, func, here}" command.
2898 * ":breakdel {func, file, here}" command.
2899 */
2900 static char_u *
2901get_breakadd_arg(expand_T *xp UNUSED, int idx)
2902{
2903 char *opts[] = {"expr", "file", "func", "here"};
2904
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002905 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002906 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002907 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002908 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2909 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002910 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2911 {
2912 // breakdel {func, file, here}
2913 if (idx <= 2)
2914 return (char_u *)opts[idx + 1];
2915 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002916 else
2917 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002918 // profdel {func, file}
2919 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002920 return (char_u *)opts[idx + 1];
2921 }
2922 }
2923 return NULL;
2924}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002925
2926/*
2927 * Function given to ExpandGeneric() to obtain the possible arguments for the
2928 * ":scriptnames" command.
2929 */
2930 static char_u *
2931get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2932{
2933 scriptitem_T *si;
2934
2935 if (!SCRIPT_ID_VALID(idx + 1))
2936 return NULL;
2937
2938 si = SCRIPT_ITEM(idx + 1);
2939 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2940 return NameBuff;
2941}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002942#endif
2943
Bram Moolenaard0190392019-08-23 21:17:35 +02002944/*
2945 * Function given to ExpandGeneric() to obtain the possible arguments of the
2946 * ":messages {clear}" command.
2947 */
2948 static char_u *
2949get_messages_arg(expand_T *xp UNUSED, int idx)
2950{
2951 if (idx == 0)
2952 return (char_u *)"clear";
2953 return NULL;
2954}
2955
2956 static char_u *
2957get_mapclear_arg(expand_T *xp UNUSED, int idx)
2958{
2959 if (idx == 0)
2960 return (char_u *)"<buffer>";
2961 return NULL;
2962}
2963
2964/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002965 * Do the expansion based on xp->xp_context and 'rmp'.
2966 */
2967 static int
2968ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002969 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002970 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002971 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002972 char_u ***matches,
2973 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002974{
2975 static struct expgen
2976 {
2977 int context;
2978 char_u *((*func)(expand_T *, int));
2979 int ic;
2980 int escaped;
2981 } tab[] =
2982 {
2983 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2984 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2985 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2986 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2987 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2988 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2989 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2990 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2991 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2992 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002993#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002994 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2995 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2996 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2997 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
2998 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002999#endif
3000#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003001 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3002 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003003#endif
3004#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003005 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003006#endif
3007#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003008 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003009#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003010 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3011 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3012 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003013#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003014 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003015#endif
3016#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003017 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003018#endif
3019#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003020 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003021#endif
3022#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003023 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3024 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003025#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003026 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3027 {EXPAND_USER, get_users, TRUE, FALSE},
3028 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003029#ifdef FEAT_EVAL
3030 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003031 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003032#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003033 };
3034 int i;
3035 int ret = FAIL;
3036
3037 // Find a context in the table and call the ExpandGeneric() with the
3038 // right function to do the expansion.
3039 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3040 {
3041 if (xp->xp_context == tab[i].context)
3042 {
3043 if (tab[i].ic)
3044 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003045 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3046 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003047 break;
3048 }
3049 }
3050
3051 return ret;
3052}
3053
3054/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003055 * Map wild expand options to flags for expand_wildcards()
3056 */
3057 static int
3058map_wildopts_to_ewflags(int options)
3059{
3060 int flags;
3061
3062 flags = EW_DIR; // include directories
3063 if (options & WILD_LIST_NOTFOUND)
3064 flags |= EW_NOTFOUND;
3065 if (options & WILD_ADD_SLASH)
3066 flags |= EW_ADDSLASH;
3067 if (options & WILD_KEEP_ALL)
3068 flags |= EW_KEEPALL;
3069 if (options & WILD_SILENT)
3070 flags |= EW_SILENT;
3071 if (options & WILD_NOERROR)
3072 flags |= EW_NOERROR;
3073 if (options & WILD_ALLLINKS)
3074 flags |= EW_ALLLINKS;
3075
3076 return flags;
3077}
3078
3079/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003080 * Do the expansion based on xp->xp_context and "pat".
3081 */
3082 static int
3083ExpandFromContext(
3084 expand_T *xp,
3085 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003086 char_u ***matches,
3087 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003088 int options) // WILD_ flags
3089{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003090 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003091 int ret;
3092 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003093 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003094 int fuzzy = cmdline_fuzzy_complete(pat)
3095 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003096
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003097 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003098
3099 if (xp->xp_context == EXPAND_FILES
3100 || xp->xp_context == EXPAND_DIRECTORIES
3101 || xp->xp_context == EXPAND_FILES_IN_PATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003102 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3103 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003105 *matches = (char_u **)"";
3106 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003107 if (xp->xp_context == EXPAND_HELP)
3108 {
3109 // With an empty argument we would get all the help tags, which is
3110 // very slow. Get matches for "help" instead.
3111 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003112 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113 {
3114#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003115 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003116#endif
3117 return OK;
3118 }
3119 return FAIL;
3120 }
3121
Bram Moolenaar66b51422019-08-18 21:44:12 +02003122 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003123 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003124 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003125 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003126 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003127 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003128#ifdef FEAT_DIFF
3129 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003130 return ExpandBufnames(pat, numMatches, matches,
3131 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003132#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133 if (xp->xp_context == EXPAND_TAGS
3134 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003135 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3136 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137 if (xp->xp_context == EXPAND_COLORS)
3138 {
3139 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003140 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003141 directories);
3142 }
3143 if (xp->xp_context == EXPAND_COMPILER)
3144 {
3145 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003146 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147 }
3148 if (xp->xp_context == EXPAND_OWNSYNTAX)
3149 {
3150 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003151 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003152 }
3153 if (xp->xp_context == EXPAND_FILETYPE)
3154 {
3155 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003156 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003157 }
Doug Kearns81642d92024-01-04 22:37:44 +01003158#ifdef FEAT_KEYMAP
3159 if (xp->xp_context == EXPAND_KEYMAP)
3160 {
3161 char *directories[] = {"keymap", NULL};
3162 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3163 }
3164#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003165#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003166 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003167 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003168#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003169 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003170 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003171 if (xp->xp_context == EXPAND_RUNTIME)
3172 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003173
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003174 // When expanding a function name starting with s:, match the <SNR>nr_
3175 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003176 if ((xp->xp_context == EXPAND_USER_FUNC
3177 || xp->xp_context == EXPAND_DISASSEMBLE)
3178 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003179 {
3180 int len = (int)STRLEN(pat) + 20;
3181
3182 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003183 if (tofree == NULL)
3184 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003185 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003186 pat = tofree;
3187 }
3188
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003189 if (!fuzzy)
3190 {
3191 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3192 if (regmatch.regprog == NULL)
3193 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003194
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003195 // set ignore-case according to p_ic, p_scs and pat
3196 regmatch.rm_ic = ignorecase(pat);
3197 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003198
3199 if (xp->xp_context == EXPAND_SETTINGS
3200 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003201 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003202 else if (xp->xp_context == EXPAND_STRING_SETTING)
3203 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3204 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3205 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003206 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003207 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003208 else if (xp->xp_context == EXPAND_ARGOPT)
3209 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3210#if defined(FEAT_TERMINAL)
3211 else if (xp->xp_context == EXPAND_TERMINALOPT)
3212 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3213#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003214#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003215 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003216 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003217#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003218 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003219 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003220
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003221 if (!fuzzy)
3222 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003223 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003224
3225 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003226}
3227
Bram Moolenaar66b51422019-08-18 21:44:12 +02003228/*
3229 * Expand a list of names.
3230 *
3231 * Generic function for command line completion. It calls a function to
3232 * obtain strings, one by one. The strings are matched against a regexp
3233 * program. Matching strings are copied into an array, which is returned.
3234 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003235 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3236 * is used.
3237 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003238 * Returns OK when no problems encountered, FAIL for error (out of memory).
3239 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003240 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003241ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003242 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003243 expand_T *xp,
3244 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003245 char_u ***matches,
3246 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003247 char_u *((*func)(expand_T *, int)),
3248 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003249 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003250{
3251 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003252 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003254 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003255 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003256 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003257 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003258 int sort_matches = FALSE;
3259 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003260
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003261 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003262 *matches = NULL;
3263 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003264
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003265 if (!fuzzy)
3266 ga_init2(&ga, sizeof(char *), 30);
3267 else
3268 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3269
3270 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003271 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003272 str = (*func)(xp, i);
3273 if (str == NULL) // end of list
3274 break;
3275 if (*str == NUL) // skip empty strings
3276 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003277
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003278 if (xp->xp_pattern[0] != NUL)
3279 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003280 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003281 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003282 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003283 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003284 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003285 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003286 }
3287 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003288 else
3289 match = TRUE;
3290
3291 if (!match)
3292 continue;
3293
3294 if (escaped)
3295 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3296 else
3297 str = vim_strsave(str);
3298 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003299 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003300 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003301 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003302 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003303 return FAIL;
3304 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003305 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003306 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003308
3309 if (ga_grow(&ga, 1) == FAIL)
3310 {
3311 vim_free(str);
3312 break;
3313 }
3314
3315 if (fuzzy)
3316 {
3317 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3318 fuzmatch->idx = ga.ga_len;
3319 fuzmatch->str = str;
3320 fuzmatch->score = score;
3321 }
3322 else
3323 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3324
K.Takata161b6ac2022-11-14 15:31:07 +00003325#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003326 if (func == get_menu_names)
3327 {
3328 // test for separator added by get_menu_names()
3329 str += STRLEN(str) - 1;
3330 if (*str == '\001')
3331 *str = '.';
3332 }
K.Takata161b6ac2022-11-14 15:31:07 +00003333#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003334
3335 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003336 }
3337
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003338 if (ga.ga_len == 0)
3339 return OK;
3340
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003341 // sort the matches when using regular expression matching and sorting
3342 // applies to the completion context. Menus and scriptnames should be kept
3343 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003344 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003345 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003346 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003347 && xp->xp_context != EXPAND_SCRIPTNAMES
3348 && xp->xp_context != EXPAND_ARGOPT
3349 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003350 sort_matches = TRUE;
3351
3352 // <SNR> functions should be sorted to the end.
3353 if (xp->xp_context == EXPAND_EXPRESSION
3354 || xp->xp_context == EXPAND_FUNCTIONS
3355 || xp->xp_context == EXPAND_USER_FUNC
3356 || xp->xp_context == EXPAND_DISASSEMBLE)
3357 funcsort = TRUE;
3358
3359 // Sort the matches.
3360 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003362 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003363 // <SNR> functions should be sorted to the end.
3364 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3365 sort_func_compare);
3366 else
3367 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3368 }
3369
3370 if (!fuzzy)
3371 {
3372 *matches = ga.ga_data;
3373 *numMatches = ga.ga_len;
3374 }
3375 else
3376 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003377 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3378 funcsort) == FAIL)
3379 return FAIL;
3380 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003381 }
3382
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003383#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003384 // Reset the variables used for special highlight names expansion, so that
3385 // they don't show up when getting normal highlight names by ID.
3386 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003387#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003388
Bram Moolenaar66b51422019-08-18 21:44:12 +02003389 return OK;
3390}
3391
3392/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003393 * Expand shell command matches in one directory of $PATH.
3394 */
3395 static void
3396expand_shellcmd_onedir(
3397 char_u *buf,
3398 char_u *s,
3399 size_t l,
3400 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003401 char_u ***matches,
3402 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003403 int flags,
3404 hashtab_T *ht,
3405 garray_T *gap)
3406{
3407 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003408 hash_T hash;
3409 hashitem_T *hi;
3410
3411 vim_strncpy(buf, s, l);
3412 add_pathsep(buf);
3413 l = STRLEN(buf);
3414 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3415
3416 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003417 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003418 if (ret != OK)
3419 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003420
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003421 if (ga_grow(gap, *numMatches) == FAIL)
3422 {
3423 FreeWild(*numMatches, *matches);
3424 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003425 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003426
3427 for (int i = 0; i < *numMatches; ++i)
3428 {
3429 char_u *name = (*matches)[i];
3430
3431 if (STRLEN(name) > l)
3432 {
3433 // Check if this name was already found.
3434 hash = hash_hash(name + l);
3435 hi = hash_lookup(ht, name + l, hash);
3436 if (HASHITEM_EMPTY(hi))
3437 {
3438 // Remove the path that was prepended.
3439 STRMOVE(name, name + l);
3440 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3441 hash_add_item(ht, hi, name, hash);
3442 name = NULL;
3443 }
3444 }
3445 vim_free(name);
3446 }
3447 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003448}
3449
3450/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003451 * Complete a shell command.
3452 * Returns FAIL or OK;
3453 */
3454 static int
3455expand_shellcmd(
3456 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003457 char_u ***matches, // return: array with matches
3458 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003459 int flagsarg) // EW_ flags
3460{
3461 char_u *pat;
3462 int i;
3463 char_u *path = NULL;
3464 int mustfree = FALSE;
3465 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003466 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003467 size_t l;
3468 char_u *s, *e;
3469 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003470 int did_curdir = FALSE;
3471 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003473 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003474 if (buf == NULL)
3475 return FAIL;
3476
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003477 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003478 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003479 if (pat == NULL)
3480 {
3481 vim_free(buf);
3482 return FAIL;
3483 }
3484
Bram Moolenaar66b51422019-08-18 21:44:12 +02003485 for (i = 0; pat[i]; ++i)
3486 if (pat[i] == '\\' && pat[i + 1] == ' ')
3487 STRMOVE(pat + i, pat + i + 1);
3488
3489 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3490
3491 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3492 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3493 path = (char_u *)".";
3494 else
3495 {
3496 // For an absolute name we don't use $PATH.
3497 if (!mch_isFullName(pat))
3498 path = vim_getenv((char_u *)"PATH", &mustfree);
3499 if (path == NULL)
3500 path = (char_u *)"";
3501 }
3502
3503 // Go over all directories in $PATH. Expand matches in that directory and
3504 // collect them in "ga". When "." is not in $PATH also expand for the
3505 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003506 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003507 hash_init(&found_ht);
3508 for (s = path; ; s = e)
3509 {
K.Takata161b6ac2022-11-14 15:31:07 +00003510#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003511 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003512#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003513 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003514#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003515 if (e == NULL)
3516 e = s + STRLEN(s);
3517
3518 if (*s == NUL)
3519 {
3520 if (did_curdir)
3521 break;
3522 // Find directories in the current directory, path is empty.
3523 did_curdir = TRUE;
3524 flags |= EW_DIR;
3525 }
3526 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3527 {
3528 did_curdir = TRUE;
3529 flags |= EW_DIR;
3530 }
3531 else
3532 // Do not match directories inside a $PATH item.
3533 flags &= ~EW_DIR;
3534
3535 l = e - s;
3536 if (l > MAXPATHL - 5)
3537 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003538
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003539 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003540 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003541
Bram Moolenaar66b51422019-08-18 21:44:12 +02003542 if (*e != NUL)
3543 ++e;
3544 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003545 *matches = ga.ga_data;
3546 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003547
3548 vim_free(buf);
3549 vim_free(pat);
3550 if (mustfree)
3551 vim_free(path);
3552 hash_clear(&found_ht);
3553 return OK;
3554}
3555
K.Takata161b6ac2022-11-14 15:31:07 +00003556#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003557/*
3558 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003559 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003560 */
3561 static void *
3562call_user_expand_func(
3563 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003564 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003565{
3566 cmdline_info_T *ccline = get_cmdline_info();
3567 int keep = 0;
3568 typval_T args[4];
3569 sctx_T save_current_sctx = current_sctx;
3570 char_u *pat = NULL;
3571 void *ret;
3572
3573 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3574 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003575
3576 if (ccline->cmdbuff != NULL)
3577 {
3578 keep = ccline->cmdbuff[ccline->cmdlen];
3579 ccline->cmdbuff[ccline->cmdlen] = 0;
3580 }
3581
3582 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3583
3584 args[0].v_type = VAR_STRING;
3585 args[0].vval.v_string = pat;
3586 args[1].v_type = VAR_STRING;
3587 args[1].vval.v_string = xp->xp_line;
3588 args[2].v_type = VAR_NUMBER;
3589 args[2].vval.v_number = xp->xp_col;
3590 args[3].v_type = VAR_UNKNOWN;
3591
3592 current_sctx = xp->xp_script_ctx;
3593
3594 ret = user_expand_func(xp->xp_arg, 3, args);
3595
3596 current_sctx = save_current_sctx;
3597 if (ccline->cmdbuff != NULL)
3598 ccline->cmdbuff[ccline->cmdlen] = keep;
3599
3600 vim_free(pat);
3601 return ret;
3602}
3603
3604/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003605 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3606 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003607 */
3608 static int
3609ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003610 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003611 expand_T *xp,
3612 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003613 char_u ***matches,
3614 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003615{
3616 char_u *retstr;
3617 char_u *s;
3618 char_u *e;
3619 int keep;
3620 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003621 int fuzzy;
3622 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003623 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003624
3625 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003626 *matches = NULL;
3627 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003628
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003629 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003630 if (retstr == NULL)
3631 return FAIL;
3632
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003633 if (!fuzzy)
3634 ga_init2(&ga, sizeof(char *), 3);
3635 else
3636 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3637
Bram Moolenaar66b51422019-08-18 21:44:12 +02003638 for (s = retstr; *s != NUL; s = e)
3639 {
3640 e = vim_strchr(s, '\n');
3641 if (e == NULL)
3642 e = s + STRLEN(s);
3643 keep = *e;
3644 *e = NUL;
3645
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003646 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003647 {
3648 if (!fuzzy)
3649 match = vim_regexec(regmatch, s, (colnr_T)0);
3650 else
3651 {
3652 score = fuzzy_match_str(s, pat);
3653 match = (score != 0);
3654 }
3655 }
3656 else
3657 match = TRUE; // match everything
3658
Bram Moolenaar66b51422019-08-18 21:44:12 +02003659 *e = keep;
3660
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003661 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003662 {
3663 if (ga_grow(&ga, 1) == FAIL)
3664 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003665 if (!fuzzy)
3666 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3667 else
3668 {
3669 fuzmatch_str_T *fuzmatch =
3670 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003671 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003672 fuzmatch->str = vim_strnsave(s, e - s);
3673 fuzmatch->score = score;
3674 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003675 ++ga.ga_len;
3676 }
3677
3678 if (*e != NUL)
3679 ++e;
3680 }
3681 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003682
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003683 if (ga.ga_len == 0)
3684 return OK;
3685
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003686 if (!fuzzy)
3687 {
3688 *matches = ga.ga_data;
3689 *numMatches = ga.ga_len;
3690 }
3691 else
3692 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003693 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3694 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003695 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003696 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003697 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003698 return OK;
3699}
3700
3701/*
3702 * Expand names with a list returned by a function defined by the user.
3703 */
3704 static int
3705ExpandUserList(
3706 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003707 char_u ***matches,
3708 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003709{
3710 list_T *retlist;
3711 listitem_T *li;
3712 garray_T ga;
3713
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003714 *matches = NULL;
3715 *numMatches = 0;
3716 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003717 if (retlist == NULL)
3718 return FAIL;
3719
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003720 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003721 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003722 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003723 {
3724 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3725 continue; // Skip non-string items and empty strings
3726
3727 if (ga_grow(&ga, 1) == FAIL)
3728 break;
3729
3730 ((char_u **)ga.ga_data)[ga.ga_len] =
3731 vim_strsave(li->li_tv.vval.v_string);
3732 ++ga.ga_len;
3733 }
3734 list_unref(retlist);
3735
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003736 *matches = ga.ga_data;
3737 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003738 return OK;
3739}
K.Takata161b6ac2022-11-14 15:31:07 +00003740#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003741
3742/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003743 * Expand "file" for all comma-separated directories in "path".
3744 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003745 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003746 */
3747 void
3748globpath(
3749 char_u *path,
3750 char_u *file,
3751 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003752 int expand_options,
3753 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003754{
3755 expand_T xpc;
3756 char_u *buf;
3757 int i;
3758 int num_p;
3759 char_u **p;
3760
3761 buf = alloc(MAXPATHL);
3762 if (buf == NULL)
3763 return;
3764
3765 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003766 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003767
3768 // Loop over all entries in {path}.
3769 while (*path != NUL)
3770 {
3771 // Copy one item of the path to buf[] and concatenate the file name.
3772 copy_option_part(&path, buf, MAXPATHL, ",");
3773 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3774 {
K.Takata161b6ac2022-11-14 15:31:07 +00003775#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003776 // Using the platform's path separator (\) makes vim incorrectly
3777 // treat it as an escape character, use '/' instead.
3778 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3779 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003780#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003781 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003782#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003783 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003784 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003785 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3786 {
3787 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3788
3789 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003790 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003791 for (i = 0; i < num_p; ++i)
3792 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003793 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003794 ++ga->ga_len;
3795 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003796 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003797 }
3798 }
3799 }
3800
3801 vim_free(buf);
3802}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003803
Bram Moolenaareadee482020-09-04 15:37:31 +02003804/*
3805 * Translate some keys pressed when 'wildmenu' is used.
3806 */
3807 int
3808wildmenu_translate_key(
3809 cmdline_info_T *cclp,
3810 int key,
3811 expand_T *xp,
3812 int did_wild_list)
3813{
3814 int c = key;
3815
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003816 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003817 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003818 // When the popup menu is used for cmdline completion:
3819 // Up : go to the previous item in the menu
3820 // Down : go to the next item in the menu
3821 // Left : go to the parent directory
3822 // Right: list the files in the selected directory
3823 switch (c)
3824 {
3825 case K_UP: c = K_LEFT; break;
3826 case K_DOWN: c = K_RIGHT; break;
3827 case K_LEFT: c = K_UP; break;
3828 case K_RIGHT: c = K_DOWN; break;
3829 default: break;
3830 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003831 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003832
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003833 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003834 {
3835 if (c == K_LEFT)
3836 c = Ctrl_P;
3837 else if (c == K_RIGHT)
3838 c = Ctrl_N;
3839 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003840
Bram Moolenaareadee482020-09-04 15:37:31 +02003841 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003842 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003843 && cclp->cmdpos > 1
3844 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3845 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3846 && (c == '\n' || c == '\r' || c == K_KENTER))
3847 c = K_DOWN;
3848
3849 return c;
3850}
3851
3852/*
3853 * Delete characters on the command line, from "from" to the current
3854 * position.
3855 */
3856 static void
3857cmdline_del(cmdline_info_T *cclp, int from)
3858{
3859 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3860 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3861 cclp->cmdlen -= cclp->cmdpos - from;
3862 cclp->cmdpos = from;
3863}
3864
3865/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003866 * Handle a key pressed when the wild menu for the menu names
3867 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003868 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003869 static int
3870wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003871{
Bram Moolenaareadee482020-09-04 15:37:31 +02003872 int i;
3873 int j;
3874
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003875 // Hitting <Down> after "emenu Name.": complete submenu
3876 if (key == K_DOWN && cclp->cmdpos > 0
3877 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003878 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003879 key = p_wc;
3880 KeyTyped = TRUE; // in case the key was mapped
3881 }
3882 else if (key == K_UP)
3883 {
3884 // Hitting <Up>: Remove one submenu name in front of the
3885 // cursor
3886 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003887
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003888 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3889 i = 0;
3890 while (--j > 0)
3891 {
3892 // check for start of menu name
3893 if (cclp->cmdbuff[j] == ' '
3894 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003895 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003896 i = j + 1;
3897 break;
3898 }
3899 // check for start of submenu name
3900 if (cclp->cmdbuff[j] == '.'
3901 && cclp->cmdbuff[j - 1] != '\\')
3902 {
3903 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003904 {
3905 i = j + 1;
3906 break;
3907 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003908 else
3909 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003910 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003911 }
3912 if (i > 0)
3913 cmdline_del(cclp, i);
3914 key = p_wc;
3915 KeyTyped = TRUE; // in case the key was mapped
3916 xp->xp_context = EXPAND_NOTHING;
3917 }
3918
3919 return key;
3920}
3921
3922/*
3923 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3924 * directory names (EXPAND_DIRECTORIES) or shell command names
3925 * (EXPAND_SHELLCMD) is displayed.
3926 */
3927 static int
3928wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3929{
3930 int i;
3931 int j;
3932 char_u upseg[5];
3933
3934 upseg[0] = PATHSEP;
3935 upseg[1] = '.';
3936 upseg[2] = '.';
3937 upseg[3] = PATHSEP;
3938 upseg[4] = NUL;
3939
3940 if (key == K_DOWN
3941 && cclp->cmdpos > 0
3942 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3943 && (cclp->cmdpos < 3
3944 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3945 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3946 {
3947 // go down a directory
3948 key = p_wc;
3949 KeyTyped = TRUE; // in case the key was mapped
3950 }
3951 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3952 {
3953 // If in a direct ancestor, strip off one ../ to go down
3954 int found = FALSE;
3955
3956 j = cclp->cmdpos;
3957 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3958 while (--j > i)
3959 {
3960 if (has_mbyte)
3961 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3962 if (vim_ispathsep(cclp->cmdbuff[j]))
3963 {
3964 found = TRUE;
3965 break;
3966 }
3967 }
3968 if (found
3969 && cclp->cmdbuff[j - 1] == '.'
3970 && cclp->cmdbuff[j - 2] == '.'
3971 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3972 {
3973 cmdline_del(cclp, j - 2);
3974 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003975 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003976 }
3977 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003978 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003979 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003980 // go up a directory
3981 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003982
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003983 j = cclp->cmdpos - 1;
3984 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3985 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003986 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003987 if (has_mbyte)
3988 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3989 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003990#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003991 && vim_strchr((char_u *)" *?[{`$%#",
3992 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00003993#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003994 )
Bram Moolenaareadee482020-09-04 15:37:31 +02003995 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003996 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003997 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003998 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02003999 break;
4000 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004001 else
4002 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004003 }
4004 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004005
4006 if (!found)
4007 j = i;
4008 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4009 j += 4;
4010 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4011 && j == i)
4012 j += 3;
4013 else
4014 j = 0;
4015 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004016 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004017 // TODO this is only for DOS/UNIX systems - need to put in
4018 // machine-specific stuff here and in upseg init
4019 cmdline_del(cclp, j);
4020 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004021 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004022 else if (cclp->cmdpos > i)
4023 cmdline_del(cclp, i);
4024
4025 // Now complete in the new directory. Set KeyTyped in case the
4026 // Up key came from a mapping.
4027 key = p_wc;
4028 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004029 }
4030
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004031 return key;
4032}
4033
4034/*
4035 * Handle a key pressed when the wild menu is displayed
4036 */
4037 int
4038wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4039{
4040 if (xp->xp_context == EXPAND_MENUNAMES)
4041 return wildmenu_process_key_menunames(cclp, key, xp);
4042 else if ((xp->xp_context == EXPAND_FILES
4043 || xp->xp_context == EXPAND_DIRECTORIES
4044 || xp->xp_context == EXPAND_SHELLCMD))
4045 return wildmenu_process_key_filenames(cclp, key, xp);
4046
4047 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004048}
4049
4050/*
4051 * Free expanded names when finished walking through the matches
4052 */
4053 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004054wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004055{
4056 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004057
4058 if (!p_wmnu || wild_menu_showing == 0)
4059 return;
4060
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004061#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004062 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004063 if (cclp->input_fn)
4064 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004065#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004066
4067 if (wild_menu_showing == WM_SCROLLED)
4068 {
4069 // Entered command line, move it up
4070 cmdline_row--;
4071 redrawcmd();
4072 }
4073 else if (save_p_ls != -1)
4074 {
4075 // restore 'laststatus' and 'winminheight'
4076 p_ls = save_p_ls;
4077 p_wmh = save_p_wmh;
4078 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004079 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004080 redrawcmd();
4081 save_p_ls = -1;
4082 }
4083 else
4084 {
4085 win_redraw_last_status(topframe);
4086 redraw_statuslines();
4087 }
4088 KeyTyped = skt;
4089 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004090#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004091 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004092 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004093#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004094}
Bram Moolenaareadee482020-09-04 15:37:31 +02004095
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004096#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004097/*
4098 * "getcompletion()" function
4099 */
4100 void
4101f_getcompletion(typval_T *argvars, typval_T *rettv)
4102{
4103 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004104 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004105 expand_T xpc;
4106 int filtered = FALSE;
4107 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004108 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004109
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004110 if (in_vim9script()
4111 && (check_for_string_arg(argvars, 0) == FAIL
4112 || check_for_string_arg(argvars, 1) == FAIL
4113 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4114 return;
4115
ii144785fe02021-11-21 12:13:56 +00004116 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004117 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004118 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004119 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004120
4121 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004122 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004123
4124 if (p_wic)
4125 options |= WILD_ICASE;
4126
4127 // For filtered results, 'wildignore' is used
4128 if (!filtered)
4129 options |= WILD_KEEP_ALL;
4130
4131 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004132 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004133 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004134 int cmdline_len = (int)STRLEN(pat);
4135 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004136 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004137 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004138 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004139 else
4140 {
ii144785fe02021-11-21 12:13:56 +00004141 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004142 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004143 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004144
4145 xpc.xp_context = cmdcomplete_str_to_type(type);
4146 if (xpc.xp_context == EXPAND_NOTHING)
4147 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004148 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004149 return;
4150 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004151
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004152 if (xpc.xp_context == EXPAND_USER_DEFINED)
4153 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004154 // Must be "custom,funcname" pattern
4155 if (STRNCMP(type, "custom,", 7) != 0)
4156 {
4157 semsg(_(e_invalid_argument_str), type);
4158 return;
4159 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004160
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004161 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004162 }
4163
4164 if (xpc.xp_context == EXPAND_USER_LIST)
4165 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004166 // Must be "customlist,funcname" pattern
4167 if (STRNCMP(type, "customlist,", 11) != 0)
4168 {
4169 semsg(_(e_invalid_argument_str), type);
4170 return;
4171 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004172
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004173 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004174 }
4175
Bram Moolenaar66b51422019-08-18 21:44:12 +02004176# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004177 if (xpc.xp_context == EXPAND_MENUS)
4178 {
4179 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4180 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4181 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004182# endif
4183# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004184 if (xpc.xp_context == EXPAND_CSCOPE)
4185 {
4186 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4187 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4188 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004189# endif
4190# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004191 if (xpc.xp_context == EXPAND_SIGN)
4192 {
4193 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4194 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4195 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004196# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004197 if (xpc.xp_context == EXPAND_RUNTIME)
4198 {
4199 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4200 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4201 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004202 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004203
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004204 if (cmdline_fuzzy_completion_supported(&xpc))
4205 // when fuzzy matching, don't modify the search string
4206 pat = vim_strsave(xpc.xp_pattern);
4207 else
4208 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4209
Bram Moolenaar93a10962022-06-16 11:42:09 +01004210 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004211 {
4212 int i;
4213
4214 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4215
4216 for (i = 0; i < xpc.xp_numfiles; i++)
4217 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4218 }
4219 vim_free(pat);
4220 ExpandCleanup(&xpc);
4221}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004222#endif // FEAT_EVAL