blob: a33d6bede138e4c31e384fc3ec1883642bdfde01 [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
LemonBoya20bf692024-07-11 22:35:53 +020049 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000050 && xp->xp_context != EXPAND_FILES
51 && xp->xp_context != EXPAND_FILES_IN_PATH
52 && xp->xp_context != EXPAND_FILETYPE
53 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010054 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000055 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020056 && xp->xp_context != EXPAND_STRING_SETTING
57 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000058 && xp->xp_context != EXPAND_OWNSYNTAX
59 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000060 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000061 && xp->xp_context != EXPAND_SHELLCMD
62 && xp->xp_context != EXPAND_TAGS
63 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064 && xp->xp_context != EXPAND_USER_LIST);
65}
66
67/*
68 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000069 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
70 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000071 */
72 int
73cmdline_fuzzy_complete(char_u *fuzzystr)
74{
75 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
76}
77
78/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000079 * sort function for the completion matches.
80 * <SNR> functions should be sorted to the end.
81 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020082 static int
83sort_func_compare(const void *s1, const void *s2)
84{
85 char_u *p1 = *(char_u **)s1;
86 char_u *p2 = *(char_u **)s2;
87
88 if (*p1 != '<' && *p2 == '<') return -1;
89 if (*p1 == '<' && *p2 != '<') return 1;
90 return STRCMP(p1, p2);
91}
Bram Moolenaar66b51422019-08-18 21:44:12 +020092
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000093/*
94 * Escape special characters in the cmdline completion matches.
95 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020096 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000097wildescape(
98 expand_T *xp,
99 char_u *str,
100 int numfiles,
101 char_u **files)
102{
103 char_u *p;
104 int vse_what = xp->xp_context == EXPAND_BUFFERS
105 ? VSE_BUFFER : VSE_NONE;
106
107 if (xp->xp_context == EXPAND_FILES
108 || xp->xp_context == EXPAND_FILES_IN_PATH
109 || xp->xp_context == EXPAND_SHELLCMD
110 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200111 || xp->xp_context == EXPAND_DIRECTORIES
112 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000113 {
114 // Insert a backslash into a file name before a space, \, %, #
115 // and wildmatch characters, except '~'.
116 for (int i = 0; i < numfiles; ++i)
117 {
118 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200119 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000120 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200121 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
122 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000123 if (p != NULL)
124 {
125 vim_free(files[i]);
126 files[i] = p;
127#if defined(BACKSLASH_IN_FILENAME)
128 p = vim_strsave_escaped(files[i], (char_u *)" ");
129 if (p != NULL)
130 {
131 vim_free(files[i]);
132 files[i] = p;
133 }
134#endif
135 }
136 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200137 else if (xp->xp_backslash & XP_BS_COMMA)
138 {
139 if (vim_strchr(files[i], ',') != NULL)
140 {
141 p = vim_strsave_escaped(files[i], (char_u *)",");
142 if (p != NULL)
143 {
144 vim_free(files[i]);
145 files[i] = p;
146 }
147 }
148 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000149#ifdef BACKSLASH_IN_FILENAME
150 p = vim_strsave_fnameescape(files[i], vse_what);
151#else
152 p = vim_strsave_fnameescape(files[i],
153 xp->xp_shell ? VSE_SHELL : vse_what);
154#endif
155 if (p != NULL)
156 {
157 vim_free(files[i]);
158 files[i] = p;
159 }
160
161 // If 'str' starts with "\~", replace "~" at start of
162 // files[i] with "\~".
163 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
164 escape_fname(&files[i]);
165 }
166 xp->xp_backslash = XP_BS_NONE;
167
168 // If the first file starts with a '+' escape it. Otherwise it
169 // could be seen as "+cmd".
170 if (*files[0] == '+')
171 escape_fname(&files[0]);
172 }
173 else if (xp->xp_context == EXPAND_TAGS)
174 {
175 // Insert a backslash before characters in a tag name that
176 // would terminate the ":tag" command.
177 for (int i = 0; i < numfiles; ++i)
178 {
179 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
180 if (p != NULL)
181 {
182 vim_free(files[i]);
183 files[i] = p;
184 }
185 }
186 }
187}
188
189/*
190 * Escape special characters in the cmdline completion matches.
191 */
192 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200193ExpandEscape(
194 expand_T *xp,
195 char_u *str,
196 int numfiles,
197 char_u **files,
198 int options)
199{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200200 // May change home directory back to "~"
201 if (options & WILD_HOME_REPLACE)
202 tilde_replace(str, numfiles, files);
203
204 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000205 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200206}
207
208/*
209 * Return FAIL if this is not an appropriate context in which to do
210 * completion of anything, return OK if it is (even if there are no matches).
211 * For the caller, this means that the character is just passed through like a
212 * normal character (instead of being expanded). This allows :s/^I^D etc.
213 */
214 int
215nextwild(
216 expand_T *xp,
217 int type,
218 int options, // extra options for ExpandOne()
219 int escape) // if TRUE, escape the returned matches
220{
221 cmdline_info_T *ccline = get_cmdline_info();
222 int i, j;
223 char_u *p1;
224 char_u *p2;
225 int difflen;
226 int v;
227
228 if (xp->xp_numfiles == -1)
229 {
230 set_expand_context(xp);
231 cmd_showtail = expand_showtail(xp);
232 }
233
234 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
235 {
236 beep_flush();
237 return OK; // Something illegal on command line
238 }
239 if (xp->xp_context == EXPAND_NOTHING)
240 {
241 // Caller can use the character as a normal char instead
242 return FAIL;
243 }
244
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000245 // If cmd_silent is set then don't show the dots, because redrawcmd() below
246 // won't remove them.
247 if (!cmd_silent)
248 {
249 msg_puts("..."); // show that we are busy
250 out_flush();
251 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200252
253 i = (int)(xp->xp_pattern - ccline->cmdbuff);
254 xp->xp_pattern_len = ccline->cmdpos - i;
255
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000256 if (type == WILD_NEXT || type == WILD_PREV
257 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200258 {
259 // Get next/previous match for a previous expanded pattern.
260 p2 = ExpandOne(xp, NULL, NULL, 0, type);
261 }
262 else
263 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000264 if (cmdline_fuzzy_completion_supported(xp))
265 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200266 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000267 else
268 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
269
Bram Moolenaar66b51422019-08-18 21:44:12 +0200270 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000271 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200272 p2 = NULL;
273 else
274 {
275 int use_options = options |
276 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
277 if (escape)
278 use_options |= WILD_ESCAPE;
279
280 if (p_wic)
281 use_options += WILD_ICASE;
282 p2 = ExpandOne(xp, p1,
283 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
284 use_options, type);
285 vim_free(p1);
286 // longest match: make sure it is not shorter, happens with :help
287 if (p2 != NULL && type == WILD_LONGEST)
288 {
289 for (j = 0; j < xp->xp_pattern_len; ++j)
290 if (ccline->cmdbuff[i + j] == '*'
291 || ccline->cmdbuff[i + j] == '?')
292 break;
293 if ((int)STRLEN(p2) < j)
294 VIM_CLEAR(p2);
295 }
296 }
297 }
298
299 if (p2 != NULL && !got_int)
300 {
301 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
302 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
303 {
304 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
305 xp->xp_pattern = ccline->cmdbuff + i;
306 }
307 else
308 v = OK;
309 if (v == OK)
310 {
311 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
312 &ccline->cmdbuff[ccline->cmdpos],
313 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
314 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
315 ccline->cmdlen += difflen;
316 ccline->cmdpos += difflen;
317 }
318 }
319 vim_free(p2);
320
321 redrawcmd();
322 cursorcmd();
323
324 // When expanding a ":map" command and no matches are found, assume that
325 // the key is supposed to be inserted literally
326 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
327 return FAIL;
328
329 if (xp->xp_numfiles <= 0 && p2 == NULL)
330 beep_flush();
331 else if (xp->xp_numfiles == 1)
332 // free expanded pattern
333 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
334
335 return OK;
336}
337
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000338/*
339 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000340 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000341 */
342 static int
343cmdline_pum_create(
344 cmdline_info_T *ccline,
345 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000346 char_u **matches,
347 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000348 int showtail)
349{
350 int i;
351 int columns;
352
353 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000354 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000355 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000356 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000357 {
zeertzjqc51a3762022-12-10 10:22:29 +0000358 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000359 compl_match_array[i].pum_info = NULL;
360 compl_match_array[i].pum_extra = NULL;
361 compl_match_array[i].pum_kind = NULL;
glepnir508e7852024-07-25 21:39:08 +0200362 compl_match_array[i].pum_extrahlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000363 }
364
365 // Compute the popup menu starting column
366 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
367 columns = vim_strsize(xp->xp_pattern);
368 if (showtail)
369 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000370 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000371 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000372 }
373 if (columns >= compl_startcol)
374 compl_startcol = 0;
375 else
376 compl_startcol -= columns;
377
378 // no default selection
379 compl_selected = -1;
380
381 cmdline_pum_display();
382
383 return EXPAND_OK;
384}
385
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000386/*
387 * Display the cmdline completion matches in a popup menu
388 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000389 void
390cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000391{
392 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
393}
394
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000395/*
396 * Returns TRUE if the cmdline completion popup menu is being displayed.
397 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000398 int
399cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100401 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000402}
403
404/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000405 * Remove the cmdline completion popup menu (if present), free the list of
406 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000408 void
409cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000410{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000411 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100412 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000413
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000414 pum_undisplay();
415 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000416 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000417 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000418 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000419 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100420
421 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
422 // as a side effect.
423 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000424}
425
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000426 void
427cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000428{
429 cmdline_pum_remove();
430 wildmenu_cleanup(cclp);
431}
432
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000433/*
434 * Returns the starting column number to use for the cmdline completion popup
435 * menu.
436 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000437 int
438cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000439{
440 return compl_startcol;
441}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000442
Bram Moolenaar66b51422019-08-18 21:44:12 +0200443/*
zeertzjqd8c93402024-06-17 18:25:32 +0200444 * Returns the current cmdline completion pattern.
445 */
446 char_u *
447cmdline_compl_pattern(void)
448{
449 expand_T *xp = get_cmdline_info()->xpc;
450
451 return xp == NULL ? NULL : xp->xp_orig;
452}
453
454/*
455 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
456 */
457 int
458cmdline_compl_is_fuzzy(void)
459{
460 expand_T *xp = get_cmdline_info()->xpc;
461
462 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
463}
464
465/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000466 * Return the number of characters that should be skipped in a status match.
467 * These are backslashes used for escaping. Do show backslashes in help tags.
468 */
469 static int
470skip_status_match_char(expand_T *xp, char_u *s)
471{
472 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
473#ifdef FEAT_MENU
474 || ((xp->xp_context == EXPAND_MENUS
475 || xp->xp_context == EXPAND_MENUNAMES)
476 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
477#endif
478 )
479 {
480#ifndef BACKSLASH_IN_FILENAME
481 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
482 return 2;
483#endif
484 return 1;
485 }
486 return 0;
487}
488
489/*
490 * Get the length of an item as it will be shown in the status line.
491 */
492 static int
493status_match_len(expand_T *xp, char_u *s)
494{
495 int len = 0;
496
497#ifdef FEAT_MENU
498 int emenu = xp->xp_context == EXPAND_MENUS
499 || xp->xp_context == EXPAND_MENUNAMES;
500
501 // Check for menu separators - replace with '|'.
502 if (emenu && menu_is_separator(s))
503 return 1;
504#endif
505
506 while (*s != NUL)
507 {
508 s += skip_status_match_char(xp, s);
509 len += ptr2cells(s);
510 MB_PTR_ADV(s);
511 }
512
513 return len;
514}
515
516/*
517 * Show wildchar matches in the status line.
518 * Show at least the "match" item.
519 * We start at item 'first_match' in the list and show all matches that fit.
520 *
521 * If inversion is possible we use it. Else '=' characters are used.
522 */
523 static void
524win_redr_status_matches(
525 expand_T *xp,
526 int num_matches,
527 char_u **matches, // list of matches
528 int match,
529 int showtail)
530{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000531 int row;
532 char_u *buf;
533 int len;
534 int clen; // length in screen cells
535 int fillchar;
536 int attr;
537 int i;
538 int highlight = TRUE;
539 char_u *selstart = NULL;
540 int selstart_col = 0;
541 char_u *selend = NULL;
542 static int first_match = 0;
543 int add_left = FALSE;
544 char_u *s;
545#ifdef FEAT_MENU
546 int emenu;
547#endif
548 int l;
549
550 if (matches == NULL) // interrupted completion?
551 return;
552
553 if (has_mbyte)
554 buf = alloc(Columns * MB_MAXBYTES + 1);
555 else
556 buf = alloc(Columns + 1);
557 if (buf == NULL)
558 return;
559
560 if (match == -1) // don't show match but original text
561 {
562 match = 0;
563 highlight = FALSE;
564 }
565 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000566 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000567 if (match == 0)
568 first_match = 0;
569 else if (match < first_match)
570 {
571 // jumping left, as far as we can go
572 first_match = match;
573 add_left = TRUE;
574 }
575 else
576 {
577 // check if match fits on the screen
578 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000579 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000580 if (first_match > 0)
581 clen += 2;
582 // jumping right, put match at the left
583 if ((long)clen > Columns)
584 {
585 first_match = match;
586 // if showing the last match, we can add some on the left
587 clen = 2;
588 for (i = match; i < num_matches; ++i)
589 {
zeertzjqc51a3762022-12-10 10:22:29 +0000590 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000591 if ((long)clen >= Columns)
592 break;
593 }
594 if (i == num_matches)
595 add_left = TRUE;
596 }
597 }
598 if (add_left)
599 while (first_match > 0)
600 {
zeertzjqc51a3762022-12-10 10:22:29 +0000601 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000602 if ((long)clen >= Columns)
603 break;
604 --first_match;
605 }
606
607 fillchar = fillchar_status(&attr, curwin);
608
609 if (first_match == 0)
610 {
611 *buf = NUL;
612 len = 0;
613 }
614 else
615 {
616 STRCPY(buf, "< ");
617 len = 2;
618 }
619 clen = len;
620
621 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000622 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000623 {
624 if (i == match)
625 {
626 selstart = buf + len;
627 selstart_col = clen;
628 }
629
zeertzjqc51a3762022-12-10 10:22:29 +0000630 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000631 // Check for menu separators - replace with '|'
632#ifdef FEAT_MENU
633 emenu = (xp->xp_context == EXPAND_MENUS
634 || xp->xp_context == EXPAND_MENUNAMES);
635 if (emenu && menu_is_separator(s))
636 {
637 STRCPY(buf + len, transchar('|'));
638 l = (int)STRLEN(buf + len);
639 len += l;
640 clen += l;
641 }
642 else
643#endif
644 for ( ; *s != NUL; ++s)
645 {
646 s += skip_status_match_char(xp, s);
647 clen += ptr2cells(s);
648 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
649 {
650 STRNCPY(buf + len, s, l);
651 s += l - 1;
652 len += l;
653 }
654 else
655 {
656 STRCPY(buf + len, transchar_byte(*s));
657 len += (int)STRLEN(buf + len);
658 }
659 }
660 if (i == match)
661 selend = buf + len;
662
663 *(buf + len++) = ' ';
664 *(buf + len++) = ' ';
665 clen += 2;
666 if (++i == num_matches)
667 break;
668 }
669
670 if (i != num_matches)
671 {
672 *(buf + len++) = '>';
673 ++clen;
674 }
675
676 buf[len] = NUL;
677
678 row = cmdline_row - 1;
679 if (row >= 0)
680 {
681 if (wild_menu_showing == 0)
682 {
683 if (msg_scrolled > 0)
684 {
685 // Put the wildmenu just above the command line. If there is
686 // no room, scroll the screen one line up.
687 if (cmdline_row == Rows - 1)
688 {
689 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
690 ++msg_scrolled;
691 }
692 else
693 {
694 ++cmdline_row;
695 ++row;
696 }
697 wild_menu_showing = WM_SCROLLED;
698 }
699 else
700 {
701 // Create status line if needed by setting 'laststatus' to 2.
702 // Set 'winminheight' to zero to avoid that the window is
703 // resized.
704 if (lastwin->w_status_height == 0)
705 {
706 save_p_ls = p_ls;
707 save_p_wmh = p_wmh;
708 p_ls = 2;
709 p_wmh = 0;
710 last_status(FALSE);
711 }
712 wild_menu_showing = WM_SHOWN;
713 }
714 }
715
716 screen_puts(buf, row, 0, attr);
717 if (selstart != NULL && highlight)
718 {
719 *selend = NUL;
720 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
721 }
722
723 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
724 }
725
726 win_redraw_last_status(topframe);
727 vim_free(buf);
728}
729
730/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000731 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200732 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000733 */
734 static char_u *
735get_next_or_prev_match(
736 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200737 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000738{
zeertzjqe9ef3472023-08-17 23:57:05 +0200739 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000740 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000741
742 if (xp->xp_numfiles <= 0)
743 return NULL;
744
745 if (mode == WILD_PREV)
746 {
747 if (findex == -1)
748 findex = xp->xp_numfiles;
749 --findex;
750 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000751 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000752 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000753 else if (mode == WILD_PAGEUP)
754 {
755 if (findex == 0)
756 // at the first entry, don't select any entries
757 findex = -1;
758 else if (findex == -1)
759 // no entry is selected. select the last entry
760 findex = xp->xp_numfiles - 1;
761 else
762 {
763 // go up by the pum height
764 ht = pum_get_height();
765 if (ht > 3)
766 ht -= 2;
767 findex -= ht;
768 if (findex < 0)
769 // few entries left, select the first entry
770 findex = 0;
771 }
772 }
773 else // mode == WILD_PAGEDOWN
774 {
775 if (findex == xp->xp_numfiles - 1)
776 // at the last entry, don't select any entries
777 findex = -1;
778 else if (findex == -1)
779 // no entry is selected. select the first entry
780 findex = 0;
781 else
782 {
783 // go down by the pum height
784 ht = pum_get_height();
785 if (ht > 3)
786 ht -= 2;
787 findex += ht;
788 if (findex >= xp->xp_numfiles)
789 // few entries left, select the last entry
790 findex = xp->xp_numfiles - 1;
791 }
792 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000793
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000794 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000795 if (findex < 0)
796 {
zeertzjq28a23602023-09-29 19:58:35 +0200797 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000798 findex = xp->xp_numfiles - 1;
799 else
800 findex = -1;
801 }
802 if (findex >= xp->xp_numfiles)
803 {
zeertzjq28a23602023-09-29 19:58:35 +0200804 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000805 findex = 0;
806 else
807 findex = -1;
808 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000809 if (compl_match_array)
810 {
811 compl_selected = findex;
812 cmdline_pum_display();
813 }
814 else if (p_wmnu)
815 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
816 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200817 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000818
819 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200820 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000821
822 return vim_strsave(xp->xp_files[findex]);
823}
824
825/*
826 * Start the command-line expansion and get the matches.
827 */
828 static char_u *
829ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
830{
831 int non_suf_match; // number without matching suffix
832 int i;
833 char_u *ss = NULL;
834
835 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000836 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000837 options) == FAIL)
838 {
839#ifdef FNAME_ILLEGAL
840 // Illegal file name has been silently skipped. But when there
841 // are wildcards, the real problem is that there was no match,
842 // causing the pattern to be added, which has illegal characters.
843 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
844 semsg(_(e_no_match_str_2), str);
845#endif
846 }
847 else if (xp->xp_numfiles == 0)
848 {
849 if (!(options & WILD_SILENT))
850 semsg(_(e_no_match_str_2), str);
851 }
852 else
853 {
854 // Escape the matches for use on the command line.
855 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
856
857 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000858 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000859 {
860 if (xp->xp_numfiles)
861 non_suf_match = xp->xp_numfiles;
862 else
863 non_suf_match = 1;
864 if ((xp->xp_context == EXPAND_FILES
865 || xp->xp_context == EXPAND_DIRECTORIES)
866 && xp->xp_numfiles > 1)
867 {
868 // More than one match; check suffix.
869 // The files will have been sorted on matching suffix in
870 // expand_wildcards, only need to check the first two.
871 non_suf_match = 0;
872 for (i = 0; i < 2; ++i)
873 if (match_suffix(xp->xp_files[i]))
874 ++non_suf_match;
875 }
876 if (non_suf_match != 1)
877 {
878 // Can we ever get here unless it's while expanding
879 // interactively? If not, we can get rid of this all
880 // together. Don't really want to wait for this message
881 // (and possibly have to hit return to continue!).
882 if (!(options & WILD_SILENT))
883 emsg(_(e_too_many_file_names));
884 else if (!(options & WILD_NO_BEEP))
885 beep_flush();
886 }
887 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
888 ss = vim_strsave(xp->xp_files[0]);
889 }
890 }
891
892 return ss;
893}
894
895/*
896 * Return the longest common part in the list of cmdline completion matches.
897 */
898 static char_u *
899find_longest_match(expand_T *xp, int options)
900{
901 long_u len;
902 int mb_len = 1;
903 int c0, ci;
904 int i;
905 char_u *ss;
906
907 for (len = 0; xp->xp_files[0][len]; len += mb_len)
908 {
909 if (has_mbyte)
910 {
911 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
912 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
913 }
914 else
915 c0 = xp->xp_files[0][len];
916 for (i = 1; i < xp->xp_numfiles; ++i)
917 {
918 if (has_mbyte)
919 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
920 else
921 ci = xp->xp_files[i][len];
922 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
923 || xp->xp_context == EXPAND_FILES
924 || xp->xp_context == EXPAND_SHELLCMD
925 || xp->xp_context == EXPAND_BUFFERS))
926 {
927 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
928 break;
929 }
930 else if (c0 != ci)
931 break;
932 }
933 if (i < xp->xp_numfiles)
934 {
935 if (!(options & WILD_NO_BEEP))
936 vim_beep(BO_WILD);
937 break;
938 }
939 }
940
941 ss = alloc(len + 1);
942 if (ss)
943 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
944
945 return ss;
946}
947
948/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000949 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200950 * Chars that should not be expanded must be preceded with a backslash.
951 * Return a pointer to allocated memory containing the new string.
952 * Return NULL for failure.
953 *
954 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200955 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
956 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200957 *
958 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
959 * is WILD_EXPAND_FREE or WILD_ALL.
960 *
961 * mode = WILD_FREE: just free previously expanded matches
962 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
963 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
964 * mode = WILD_NEXT: use next match in multiple match, wrap to first
965 * mode = WILD_PREV: use previous match in multiple match, wrap to first
966 * mode = WILD_ALL: return all matches concatenated
967 * mode = WILD_LONGEST: return longest matched part
968 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000969 * mode = WILD_APPLY: apply the item selected in the cmdline completion
970 * popup menu and close the menu.
971 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
972 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200973 *
974 * options = WILD_LIST_NOTFOUND: list entries without a match
975 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
976 * options = WILD_USE_NL: Use '\n' for WILD_ALL
977 * options = WILD_NO_BEEP: Don't beep for multiple matches
978 * options = WILD_ADD_SLASH: add a slash after directory names
979 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
980 * options = WILD_SILENT: don't print warning messages
981 * options = WILD_ESCAPE: put backslash before special chars
982 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200983 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200984 *
985 * The variables xp->xp_context and xp->xp_backslash must have been set!
986 */
987 char_u *
988ExpandOne(
989 expand_T *xp,
990 char_u *str,
991 char_u *orig, // allocated copy of original of expanded string
992 int options,
993 int mode)
994{
995 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200996 int orig_saved = FALSE;
997 int i;
998 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200999
1000 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001001 if (mode == WILD_NEXT || mode == WILD_PREV
1002 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001003 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001004
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001005 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001006 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001007 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001008 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001009 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001010 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001011
Bram Moolenaar66b51422019-08-18 21:44:12 +02001012 // free old names
1013 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1014 {
1015 FreeWild(xp->xp_numfiles, xp->xp_files);
1016 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001017 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001018
1019 // The entries from xp_files may be used in the PUM, remove it.
1020 if (compl_match_array != NULL)
1021 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001022 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001023 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001024
1025 if (mode == WILD_FREE) // only release file name
1026 return NULL;
1027
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001028 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001029 {
zeertzjq28a23602023-09-29 19:58:35 +02001030 vim_free(xp->xp_orig);
1031 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001032 orig_saved = TRUE;
1033
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001034 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001035 }
1036
1037 // Find longest common part
1038 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1039 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001040 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001041 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001042 }
1043
Bram Moolenaar57e95172022-08-20 19:26:14 +01001044 // Concatenate all matching names. Unless interrupted, this can be slow
1045 // and the result probably won't be used.
1046 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001047 {
1048 len = 0;
1049 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001050 {
1051 if (i > 0)
1052 {
1053 if (xp->xp_prefix == XP_PREFIX_NO)
1054 len += 2; // prefix "no"
1055 else if (xp->xp_prefix == XP_PREFIX_INV)
1056 len += 3; // prefix "inv"
1057 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001058 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001059 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001060 ss = alloc(len);
1061 if (ss != NULL)
1062 {
1063 *ss = NUL;
1064 for (i = 0; i < xp->xp_numfiles; ++i)
1065 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001066 if (i > 0)
1067 {
1068 if (xp->xp_prefix == XP_PREFIX_NO)
1069 STRCAT(ss, "no");
1070 else if (xp->xp_prefix == XP_PREFIX_INV)
1071 STRCAT(ss, "inv");
1072 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001073 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001074
Bram Moolenaar66b51422019-08-18 21:44:12 +02001075 if (i != xp->xp_numfiles - 1)
1076 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1077 }
1078 }
1079 }
1080
1081 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1082 ExpandCleanup(xp);
1083
zeertzjq28a23602023-09-29 19:58:35 +02001084 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001085 if (!orig_saved)
1086 vim_free(orig);
1087
1088 return ss;
1089}
1090
1091/*
1092 * Prepare an expand structure for use.
1093 */
1094 void
1095ExpandInit(expand_T *xp)
1096{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001097 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001098 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001099 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001100 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001101}
1102
1103/*
1104 * Cleanup an expand structure after use.
1105 */
1106 void
1107ExpandCleanup(expand_T *xp)
1108{
1109 if (xp->xp_numfiles >= 0)
1110 {
1111 FreeWild(xp->xp_numfiles, xp->xp_files);
1112 xp->xp_numfiles = -1;
1113 }
zeertzjq28a23602023-09-29 19:58:35 +02001114 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001115}
1116
1117/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001118 * Display one line of completion matches. Multiple matches are displayed in
1119 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001120 * matches - list of completion match names
1121 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001122 * lines - number of output lines
1123 * linenr - line number of matches to display
1124 * maxlen - maximum number of characters in each line
1125 * showtail - display only the tail of the full path of a file name
1126 * dir_attr - highlight attribute to use for directory names
1127 */
1128 static void
1129showmatches_oneline(
1130 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001131 char_u **matches,
1132 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001133 int lines,
1134 int linenr,
1135 int maxlen,
1136 int showtail,
1137 int dir_attr)
1138{
1139 int i, j;
1140 int isdir;
1141 int lastlen;
1142 char_u *p;
1143
1144 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001145 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001146 {
1147 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1148 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001149 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1150 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001151 msg_advance(maxlen + 1);
1152 msg_puts((char *)p);
1153 msg_advance(maxlen + 3);
1154 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1155 break;
1156 }
1157 for (i = maxlen - lastlen; --i >= 0; )
1158 msg_putchar(' ');
1159 if (xp->xp_context == EXPAND_FILES
1160 || xp->xp_context == EXPAND_SHELLCMD
1161 || xp->xp_context == EXPAND_BUFFERS)
1162 {
1163 // highlight directories
1164 if (xp->xp_numfiles != -1)
1165 {
1166 char_u *halved_slash;
1167 char_u *exp_path;
1168 char_u *path;
1169
1170 // Expansion was done before and special characters
1171 // were escaped, need to halve backslashes. Also
1172 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001173 exp_path = expand_env_save_opt(matches[j], TRUE);
1174 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001175 halved_slash = backslash_halve_save(path);
1176 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001177 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001178 vim_free(exp_path);
1179 if (halved_slash != path)
1180 vim_free(halved_slash);
1181 }
1182 else
1183 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001184 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001185 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001186 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001187 else
1188 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001189 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001190 TRUE);
1191 p = NameBuff;
1192 }
1193 }
1194 else
1195 {
1196 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001197 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001198 }
1199 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1200 }
1201 if (msg_col > 0) // when not wrapped around
1202 {
1203 msg_clr_eos();
1204 msg_putchar('\n');
1205 }
1206 out_flush(); // show one line at a time
1207}
1208
1209/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001210 * Show all matches for completion on the command line.
1211 * Returns EXPAND_NOTHING when the character that triggered expansion should
1212 * be inserted like a normal character.
1213 */
1214 int
1215showmatches(expand_T *xp, int wildmenu UNUSED)
1216{
1217 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001218 int numMatches;
1219 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001220 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001221 int maxlen;
1222 int lines;
1223 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001224 int attr;
1225 int showtail;
1226
1227 if (xp->xp_numfiles == -1)
1228 {
1229 set_expand_context(xp);
1230 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001231 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001232 showtail = expand_showtail(xp);
1233 if (i != EXPAND_OK)
1234 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001235 }
1236 else
1237 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001238 numMatches = xp->xp_numfiles;
1239 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001240 showtail = cmd_showtail;
1241 }
1242
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001243 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001244 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001245 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001246
Bram Moolenaar66b51422019-08-18 21:44:12 +02001247 if (!wildmenu)
1248 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001249 msg_didany = FALSE; // lines_left will be set
1250 msg_start(); // prepare for paging
1251 msg_putchar('\n');
1252 out_flush();
1253 cmdline_row = msg_row;
1254 msg_didany = FALSE; // lines_left will be set again
1255 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001256 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257
1258 if (got_int)
1259 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001260 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001261 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001262 else
1263 {
1264 // find the length of the longest file name
1265 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001266 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001267 {
1268 if (!showtail && (xp->xp_context == EXPAND_FILES
1269 || xp->xp_context == EXPAND_SHELLCMD
1270 || xp->xp_context == EXPAND_BUFFERS))
1271 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001272 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001273 j = vim_strsize(NameBuff);
1274 }
1275 else
zeertzjqc51a3762022-12-10 10:22:29 +00001276 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001277 if (j > maxlen)
1278 maxlen = j;
1279 }
1280
1281 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001282 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001283 else
1284 {
1285 // compute the number of columns and lines for the listing
1286 maxlen += 2; // two spaces between file names
1287 columns = ((int)Columns + 2) / maxlen;
1288 if (columns < 1)
1289 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001290 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001291 }
1292
1293 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1294
1295 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1296 {
1297 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1298 msg_clr_eos();
1299 msg_advance(maxlen - 3);
1300 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1301 }
1302
1303 // list the files line by line
1304 for (i = 0; i < lines; ++i)
1305 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001306 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001307 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001308 if (got_int)
1309 {
1310 got_int = FALSE;
1311 break;
1312 }
1313 }
1314
1315 // we redraw the command below the lines that we have just listed
1316 // This is a bit tricky, but it saves a lot of screen updating.
1317 cmdline_row = msg_row; // will put it back later
1318 }
1319
1320 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001321 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001322
1323 return EXPAND_OK;
1324}
1325
1326/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001327 * gettail() version for showmatches() and win_redr_status_matches():
1328 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001329 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001330 static char_u *
1331showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001332{
1333 char_u *p;
1334 char_u *t = s;
1335 int had_sep = FALSE;
1336
1337 for (p = s; *p != NUL; )
1338 {
1339 if (vim_ispathsep(*p)
1340#ifdef BACKSLASH_IN_FILENAME
1341 && !rem_backslash(p)
1342#endif
1343 )
1344 had_sep = TRUE;
1345 else if (had_sep)
1346 {
1347 t = p;
1348 had_sep = FALSE;
1349 }
1350 MB_PTR_ADV(p);
1351 }
1352 return t;
1353}
1354
1355/*
1356 * Return TRUE if we only need to show the tail of completion matches.
1357 * When not completing file names or there is a wildcard in the path FALSE is
1358 * returned.
1359 */
1360 static int
1361expand_showtail(expand_T *xp)
1362{
1363 char_u *s;
1364 char_u *end;
1365
1366 // When not completing file names a "/" may mean something different.
1367 if (xp->xp_context != EXPAND_FILES
1368 && xp->xp_context != EXPAND_SHELLCMD
1369 && xp->xp_context != EXPAND_DIRECTORIES)
1370 return FALSE;
1371
1372 end = gettail(xp->xp_pattern);
1373 if (end == xp->xp_pattern) // there is no path separator
1374 return FALSE;
1375
1376 for (s = xp->xp_pattern; s < end; s++)
1377 {
1378 // Skip escaped wildcards. Only when the backslash is not a path
1379 // separator, on DOS the '*' "path\*\file" must not be skipped.
1380 if (rem_backslash(s))
1381 ++s;
1382 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1383 return FALSE;
1384 }
1385 return TRUE;
1386}
1387
1388/*
1389 * Prepare a string for expansion.
1390 * When expanding file names: The string will be used with expand_wildcards().
1391 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1392 * When expanding other names: The string will be used with regcomp(). Copy
1393 * the name into allocated memory and prepend "^".
1394 */
1395 char_u *
1396addstar(
1397 char_u *fname,
1398 int len,
1399 int context) // EXPAND_FILES etc.
1400{
1401 char_u *retval;
1402 int i, j;
1403 int new_len;
1404 char_u *tail;
1405 int ends_in_star;
1406
1407 if (context != EXPAND_FILES
1408 && context != EXPAND_FILES_IN_PATH
1409 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001410 && context != EXPAND_DIRECTORIES
1411 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001412 {
1413 // Matching will be done internally (on something other than files).
1414 // So we convert the file-matching-type wildcards into our kind for
1415 // use with vim_regcomp(). First work out how long it will be:
1416
1417 // For help tags the translation is done in find_help_tags().
1418 // For a tag pattern starting with "/" no translation is needed.
1419 if (context == EXPAND_HELP
1420 || context == EXPAND_COLORS
1421 || context == EXPAND_COMPILER
1422 || context == EXPAND_OWNSYNTAX
1423 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001424 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001425 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001426 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001427 || ((context == EXPAND_TAGS_LISTFILES
1428 || context == EXPAND_TAGS)
1429 && fname[0] == '/'))
1430 retval = vim_strnsave(fname, len);
1431 else
1432 {
1433 new_len = len + 2; // +2 for '^' at start, NUL at end
1434 for (i = 0; i < len; i++)
1435 {
1436 if (fname[i] == '*' || fname[i] == '~')
1437 new_len++; // '*' needs to be replaced by ".*"
1438 // '~' needs to be replaced by "\~"
1439
1440 // Buffer names are like file names. "." should be literal
1441 if (context == EXPAND_BUFFERS && fname[i] == '.')
1442 new_len++; // "." becomes "\."
1443
1444 // Custom expansion takes care of special things, match
1445 // backslashes literally (perhaps also for other types?)
1446 if ((context == EXPAND_USER_DEFINED
1447 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1448 new_len++; // '\' becomes "\\"
1449 }
1450 retval = alloc(new_len);
1451 if (retval != NULL)
1452 {
1453 retval[0] = '^';
1454 j = 1;
1455 for (i = 0; i < len; i++, j++)
1456 {
1457 // Skip backslash. But why? At least keep it for custom
1458 // expansion.
1459 if (context != EXPAND_USER_DEFINED
1460 && context != EXPAND_USER_LIST
1461 && fname[i] == '\\'
1462 && ++i == len)
1463 break;
1464
1465 switch (fname[i])
1466 {
1467 case '*': retval[j++] = '.';
1468 break;
1469 case '~': retval[j++] = '\\';
1470 break;
1471 case '?': retval[j] = '.';
1472 continue;
1473 case '.': if (context == EXPAND_BUFFERS)
1474 retval[j++] = '\\';
1475 break;
1476 case '\\': if (context == EXPAND_USER_DEFINED
1477 || context == EXPAND_USER_LIST)
1478 retval[j++] = '\\';
1479 break;
1480 }
1481 retval[j] = fname[i];
1482 }
1483 retval[j] = NUL;
1484 }
1485 }
1486 }
1487 else
1488 {
1489 retval = alloc(len + 4);
1490 if (retval != NULL)
1491 {
1492 vim_strncpy(retval, fname, len);
1493
1494 // Don't add a star to *, ~, ~user, $var or `cmd`.
1495 // * would become **, which walks the whole tree.
1496 // ~ would be at the start of the file name, but not the tail.
1497 // $ could be anywhere in the tail.
1498 // ` could be anywhere in the file name.
1499 // When the name ends in '$' don't add a star, remove the '$'.
1500 tail = gettail(retval);
1501 ends_in_star = (len > 0 && retval[len - 1] == '*');
1502#ifndef BACKSLASH_IN_FILENAME
1503 for (i = len - 2; i >= 0; --i)
1504 {
1505 if (retval[i] != '\\')
1506 break;
1507 ends_in_star = !ends_in_star;
1508 }
1509#endif
1510 if ((*retval != '~' || tail != retval)
1511 && !ends_in_star
1512 && vim_strchr(tail, '$') == NULL
1513 && vim_strchr(retval, '`') == NULL)
1514 retval[len++] = '*';
1515 else if (len > 0 && retval[len - 1] == '$')
1516 --len;
1517 retval[len] = NUL;
1518 }
1519 }
1520 return retval;
1521}
1522
1523/*
1524 * Must parse the command line so far to work out what context we are in.
1525 * Completion can then be done based on that context.
1526 * This routine sets the variables:
1527 * xp->xp_pattern The start of the pattern to be expanded within
1528 * the command line (ends at the cursor).
1529 * xp->xp_context The type of thing to expand. Will be one of:
1530 *
1531 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1532 * the command line, like an unknown command. Caller
1533 * should beep.
1534 * EXPAND_NOTHING Unrecognised context for completion, use char like
1535 * a normal char, rather than for completion. eg
1536 * :s/^I/
1537 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1538 * it.
1539 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1540 * EXPAND_FILES After command with EX_XFILE set, or after setting
1541 * with P_EXPAND set. eg :e ^I, :w>>^I
1542 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001543 * when we know only directories are of interest.
1544 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001545 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1546 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1547 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1548 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1549 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1550 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1551 * EXPAND_EVENTS Complete event names
1552 * EXPAND_SYNTAX Complete :syntax command arguments
1553 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1554 * EXPAND_AUGROUP Complete autocommand group names
1555 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1556 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1557 * eg :unmap a^I , :cunab x^I
1558 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1559 * eg :call sub^I
1560 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1561 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1562 * names in expressions, eg :while s^I
1563 * EXPAND_ENV_VARS Complete environment variable names
1564 * EXPAND_USER Complete user names
1565 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001566 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001567set_expand_context(expand_T *xp)
1568{
1569 cmdline_info_T *ccline = get_cmdline_info();
1570
1571 // only expansion for ':', '>' and '=' command-lines
1572 if (ccline->cmdfirstc != ':'
1573#ifdef FEAT_EVAL
1574 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1575 && !ccline->input_fn
1576#endif
1577 )
1578 {
1579 xp->xp_context = EXPAND_NOTHING;
1580 return;
1581 }
1582 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1583}
1584
Bram Moolenaard0190392019-08-23 21:17:35 +02001585/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001586 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1587 * For user defined commands, the completion context is set in 'xp' and the
1588 * completion flags in 'complp'.
1589 *
1590 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001591 */
1592 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001593set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001594{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001595 char_u *p = NULL;
1596 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001597 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001598
1599 // Isolate the command and search for it in the command table.
1600 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001601 // - the 'k' command can directly be followed by any character, but do
1602 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1603 // find matches anywhere in the command name, do this only for command
1604 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001605 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001606 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001607 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001608 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001609 p = cmd + 1;
1610 }
1611 else
1612 {
1613 p = cmd;
1614 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1615 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001616 // A user command may contain digits.
1617 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1618 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001619 while (ASCII_ISALNUM(*p) || *p == '*')
1620 ++p;
1621 // for python 3.x: ":py3*" commands completion
1622 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1623 {
1624 ++p;
1625 while (ASCII_ISALPHA(*p) || *p == '*')
1626 ++p;
1627 }
1628 // check for non-alpha command
1629 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1630 ++p;
1631 len = (int)(p - cmd);
1632
1633 if (len == 0)
1634 {
1635 xp->xp_context = EXPAND_UNSUCCESSFUL;
1636 return NULL;
1637 }
1638
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001639 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001640
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001641 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001642 // Also when doing fuzzy expansion for non-shell commands, support
1643 // alphanumeric characters.
1644 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1645 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001646 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1647 ++p;
1648 }
1649
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001650 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001651 // character, complete the command name.
1652 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1653 return NULL;
1654
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001655 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001656 {
1657 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1658 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001659 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001660 p = cmd + 1;
1661 }
1662 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1663 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001664 eap->cmd = cmd;
1665 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001666 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001667 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001668 }
1669 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001670 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001671 {
1672 // Not still touching the command and it was an illegal one
1673 xp->xp_context = EXPAND_UNSUCCESSFUL;
1674 return NULL;
1675 }
1676
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001677 return p;
1678}
Bram Moolenaard0190392019-08-23 21:17:35 +02001679
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001680/*
1681 * Set the completion context for a command argument with wild card characters.
1682 */
1683 static void
1684set_context_for_wildcard_arg(
1685 exarg_T *eap,
1686 char_u *arg,
1687 int usefilter,
1688 expand_T *xp,
1689 int *complp)
1690{
1691 char_u *p;
1692 int c;
1693 int in_quote = FALSE;
1694 char_u *bow = NULL; // Beginning of word
1695 int len = 0;
1696
1697 // Allow spaces within back-quotes to count as part of the argument
1698 // being expanded.
1699 xp->xp_pattern = skipwhite(arg);
1700 p = xp->xp_pattern;
1701 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001702 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001703 if (has_mbyte)
1704 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001705 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001706 c = *p;
1707 if (c == '\\' && p[1] != NUL)
1708 ++p;
1709 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001710 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001711 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001712 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001713 xp->xp_pattern = p;
1714 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001715 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001716 in_quote = !in_quote;
1717 }
1718 // An argument can contain just about everything, except
1719 // characters that end the command and white space.
1720 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001721#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001722 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001723#endif
1724 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001725 {
1726 len = 0; // avoid getting stuck when space is in 'isfname'
1727 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001728 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001729 if (has_mbyte)
1730 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001731 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001732 c = *p;
1733 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001734 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001735 if (has_mbyte)
1736 len = (*mb_ptr2len)(p);
1737 else
1738 len = 1;
1739 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001740 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001741 if (in_quote)
1742 bow = p;
1743 else
1744 xp->xp_pattern = p;
1745 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001746 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001747 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001748 }
1749
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001750 // If we are still inside the quotes, and we passed a space, just
1751 // expand from there.
1752 if (bow != NULL && in_quote)
1753 xp->xp_pattern = bow;
1754 xp->xp_context = EXPAND_FILES;
1755
1756 // For a shell command more chars need to be escaped.
1757 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal)
1758 {
1759#ifndef BACKSLASH_IN_FILENAME
1760 xp->xp_shell = TRUE;
1761#endif
1762 // When still after the command name expand executables.
1763 if (xp->xp_pattern == skipwhite(arg))
1764 xp->xp_context = EXPAND_SHELLCMD;
1765 }
1766
1767 // Check for environment variable.
1768 if (*xp->xp_pattern == '$')
1769 {
1770 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1771 if (!vim_isIDc(*p))
1772 break;
1773 if (*p == NUL)
1774 {
1775 xp->xp_context = EXPAND_ENV_VARS;
1776 ++xp->xp_pattern;
1777 // Avoid that the assignment uses EXPAND_FILES again.
1778 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1779 *complp = EXPAND_ENV_VARS;
1780 }
1781 }
1782 // Check for user names.
1783 if (*xp->xp_pattern == '~')
1784 {
1785 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1786 ;
1787 // Complete ~user only if it partially matches a user name.
1788 // A full match ~user<Tab> will be replaced by user's home
1789 // directory i.e. something like ~user<Tab> -> /home/user/
1790 if (*p == NUL && p > xp->xp_pattern + 1
1791 && match_user(xp->xp_pattern + 1) >= 1)
1792 {
1793 xp->xp_context = EXPAND_USER;
1794 ++xp->xp_pattern;
1795 }
1796 }
1797}
1798
1799/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001800 * Set the completion context for the "++opt=arg" argument. Always returns
1801 * NULL.
1802 */
1803 static char_u *
1804set_context_in_argopt(expand_T *xp, char_u *arg)
1805{
1806 char_u *p;
1807
1808 p = vim_strchr(arg, '=');
1809 if (p == NULL)
1810 xp->xp_pattern = arg;
1811 else
1812 xp->xp_pattern = p + 1;
1813
1814 xp->xp_context = EXPAND_ARGOPT;
1815 return NULL;
1816}
1817
1818#ifdef FEAT_TERMINAL
1819/*
1820 * Set the completion context for :terminal's [options]. Always returns NULL.
1821 */
1822 static char_u *
1823set_context_in_terminalopt(expand_T *xp, char_u *arg)
1824{
1825 char_u *p;
1826
1827 p = vim_strchr(arg, '=');
1828 if (p == NULL)
1829 xp->xp_pattern = arg;
1830 else
1831 xp->xp_pattern = p + 1;
1832
1833 xp->xp_context = EXPAND_TERMINALOPT;
1834 return NULL;
1835}
1836#endif
1837
1838/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001839 * Set the completion context for the :filter command. Returns a pointer to the
1840 * next command after the :filter command.
1841 */
1842 static char_u *
1843set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1844{
1845 if (*arg != NUL)
1846 arg = skip_vimgrep_pat(arg, NULL, NULL);
1847 if (arg == NULL || *arg == NUL)
1848 {
1849 xp->xp_context = EXPAND_NOTHING;
1850 return NULL;
1851 }
1852 return skipwhite(arg);
1853}
1854
1855#ifdef FEAT_SEARCH_EXTRA
1856/*
1857 * Set the completion context for the :match command. Returns a pointer to the
1858 * next command after the :match command.
1859 */
1860 static char_u *
1861set_context_in_match_cmd(expand_T *xp, char_u *arg)
1862{
1863 if (*arg == NUL || !ends_excmd(*arg))
1864 {
1865 // also complete "None"
1866 set_context_in_echohl_cmd(xp, arg);
1867 arg = skipwhite(skiptowhite(arg));
1868 if (*arg != NUL)
1869 {
1870 xp->xp_context = EXPAND_NOTHING;
1871 arg = skip_regexp(arg + 1, *arg, magic_isset());
1872 }
1873 }
1874 return find_nextcmd(arg);
1875}
1876#endif
1877
1878/*
1879 * Returns a pointer to the next command after a :global or a :v command.
1880 * Returns NULL if there is no next command.
1881 */
1882 static char_u *
1883find_cmd_after_global_cmd(char_u *arg)
1884{
1885 int delim;
1886
1887 delim = *arg; // get the delimiter
1888 if (delim)
1889 ++arg; // skip delimiter if there is one
1890
1891 while (arg[0] != NUL && arg[0] != delim)
1892 {
1893 if (arg[0] == '\\' && arg[1] != NUL)
1894 ++arg;
1895 ++arg;
1896 }
1897 if (arg[0] != NUL)
1898 return arg + 1;
1899
1900 return NULL;
1901}
1902
1903/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001904 * Returns a pointer to the next command after a :substitute or a :& command.
1905 * Returns NULL if there is no next command.
1906 */
1907 static char_u *
1908find_cmd_after_substitute_cmd(char_u *arg)
1909{
1910 int delim;
1911
1912 delim = *arg;
1913 if (delim)
1914 {
1915 // skip "from" part
1916 ++arg;
1917 arg = skip_regexp(arg, delim, magic_isset());
1918
1919 if (arg[0] != NUL && arg[0] == delim)
1920 {
1921 // skip "to" part
1922 ++arg;
1923 while (arg[0] != NUL && arg[0] != delim)
1924 {
1925 if (arg[0] == '\\' && arg[1] != NUL)
1926 ++arg;
1927 ++arg;
1928 }
1929 if (arg[0] != NUL) // skip delimiter
1930 ++arg;
1931 }
1932 }
1933 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1934 ++arg;
1935 if (arg[0] != NUL)
1936 return arg;
1937
1938 return NULL;
1939}
1940
1941/*
1942 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1943 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1944 * Returns NULL if there is no next command.
1945 */
1946 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001947find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001948{
1949 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001950 if (*arg != '/')
1951 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001952
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001953 // Match regexp, not just whole words
1954 for (++arg; *arg && *arg != '/'; arg++)
1955 if (*arg == '\\' && arg[1] != NUL)
1956 arg++;
1957 if (*arg)
1958 {
1959 arg = skipwhite(arg + 1);
1960
1961 // Check for trailing illegal characters
1962 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1963 xp->xp_context = EXPAND_NOTHING;
1964 else
1965 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001966 }
1967
1968 return NULL;
1969}
1970
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001971#ifdef FEAT_EVAL
1972/*
1973 * Set the completion context for the :unlet command. Always returns NULL.
1974 */
1975 static char_u *
1976set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1977{
1978 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1979 arg = xp->xp_pattern + 1;
1980
1981 xp->xp_context = EXPAND_USER_VARS;
1982 xp->xp_pattern = arg;
1983
1984 if (*xp->xp_pattern == '$')
1985 {
1986 xp->xp_context = EXPAND_ENV_VARS;
1987 ++xp->xp_pattern;
1988 }
1989
1990 return NULL;
1991}
1992#endif
1993
1994#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1995/*
1996 * Set the completion context for the :language command. Always returns NULL.
1997 */
1998 static char_u *
1999set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2000{
2001 char_u *p;
2002
2003 p = skiptowhite(arg);
2004 if (*p == NUL)
2005 {
2006 xp->xp_context = EXPAND_LANGUAGE;
2007 xp->xp_pattern = arg;
2008 }
2009 else
2010 {
2011 if ( STRNCMP(arg, "messages", p - arg) == 0
2012 || STRNCMP(arg, "ctype", p - arg) == 0
2013 || STRNCMP(arg, "time", p - arg) == 0
2014 || STRNCMP(arg, "collate", p - arg) == 0)
2015 {
2016 xp->xp_context = EXPAND_LOCALES;
2017 xp->xp_pattern = skipwhite(p);
2018 }
2019 else
2020 xp->xp_context = EXPAND_NOTHING;
2021 }
2022
2023 return NULL;
2024}
2025#endif
2026
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002027#ifdef FEAT_EVAL
2028static enum
2029{
2030 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002031 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2032 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002033} breakpt_expand_what;
2034
2035/*
2036 * Set the completion context for the :breakadd command. Always returns NULL.
2037 */
2038 static char_u *
2039set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2040{
2041 char_u *p;
2042 char_u *subcmd_start;
2043
2044 xp->xp_context = EXPAND_BREAKPOINT;
2045 xp->xp_pattern = arg;
2046
2047 if (cmdidx == CMD_breakadd)
2048 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002049 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002050 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002051 else
2052 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002053
2054 p = skipwhite(arg);
2055 if (*p == NUL)
2056 return NULL;
2057 subcmd_start = p;
2058
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002059 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002060 {
2061 // :breakadd file [lnum] <filename>
2062 // :breakadd func [lnum] <funcname>
2063 p += 4;
2064 p = skipwhite(p);
2065
2066 // skip line number (if specified)
2067 if (VIM_ISDIGIT(*p))
2068 {
2069 p = skipdigits(p);
2070 if (*p != ' ')
2071 {
2072 xp->xp_context = EXPAND_NOTHING;
2073 return NULL;
2074 }
2075 p = skipwhite(p);
2076 }
2077 if (STRNCMP("file", subcmd_start, 4) == 0)
2078 xp->xp_context = EXPAND_FILES;
2079 else
2080 xp->xp_context = EXPAND_USER_FUNC;
2081 xp->xp_pattern = p;
2082 }
2083 else if (STRNCMP("expr ", p, 5) == 0)
2084 {
2085 // :breakadd expr <expression>
2086 xp->xp_context = EXPAND_EXPRESSION;
2087 xp->xp_pattern = skipwhite(p + 5);
2088 }
2089
2090 return NULL;
2091}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002092
2093 static char_u *
2094set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2095{
2096 char_u *p;
2097
2098 xp->xp_context = EXPAND_NOTHING;
2099 xp->xp_pattern = NULL;
2100
2101 p = skipwhite(arg);
2102 if (VIM_ISDIGIT(*p))
2103 return NULL;
2104
2105 xp->xp_context = EXPAND_SCRIPTNAMES;
2106 xp->xp_pattern = p;
2107
2108 return NULL;
2109}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002110#endif
2111
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002112/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002113 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2114 * The argument to the command is 'arg' and the argument flags is 'argt'.
2115 * For user-defined commands and for environment variables, 'compl' has the
2116 * completion type.
2117 * Returns a pointer to the next command. Returns NULL if there is no next
2118 * command.
2119 */
2120 static char_u *
2121set_context_by_cmdname(
2122 char_u *cmd,
2123 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002124 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002125 char_u *arg,
2126 long argt,
2127 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002128 int forceit)
2129{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002130 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002131 {
2132 case CMD_find:
2133 case CMD_sfind:
2134 case CMD_tabfind:
2135 if (xp->xp_context == EXPAND_FILES)
2136 xp->xp_context = EXPAND_FILES_IN_PATH;
2137 break;
2138 case CMD_cd:
2139 case CMD_chdir:
2140 case CMD_tcd:
2141 case CMD_tchdir:
2142 case CMD_lcd:
2143 case CMD_lchdir:
2144 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002145 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002146 break;
2147 case CMD_help:
2148 xp->xp_context = EXPAND_HELP;
2149 xp->xp_pattern = arg;
2150 break;
2151
2152 // Command modifiers: return the argument.
2153 // Also for commands with an argument that is a command.
2154 case CMD_aboveleft:
2155 case CMD_argdo:
2156 case CMD_belowright:
2157 case CMD_botright:
2158 case CMD_browse:
2159 case CMD_bufdo:
2160 case CMD_cdo:
2161 case CMD_cfdo:
2162 case CMD_confirm:
2163 case CMD_debug:
2164 case CMD_folddoclosed:
2165 case CMD_folddoopen:
2166 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002167 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002168 case CMD_keepalt:
2169 case CMD_keepjumps:
2170 case CMD_keepmarks:
2171 case CMD_keeppatterns:
2172 case CMD_ldo:
2173 case CMD_leftabove:
2174 case CMD_lfdo:
2175 case CMD_lockmarks:
2176 case CMD_noautocmd:
2177 case CMD_noswapfile:
2178 case CMD_rightbelow:
2179 case CMD_sandbox:
2180 case CMD_silent:
2181 case CMD_tab:
2182 case CMD_tabdo:
2183 case CMD_topleft:
2184 case CMD_verbose:
2185 case CMD_vertical:
2186 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002187 case CMD_vim9cmd:
2188 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002189 return arg;
2190
2191 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002192 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002193
2194#ifdef FEAT_SEARCH_EXTRA
2195 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002196 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002197#endif
2198
2199 // All completion for the +cmdline_compl feature goes here.
2200
2201 case CMD_command:
2202 return set_context_in_user_cmd(xp, arg);
2203
2204 case CMD_delcommand:
2205 xp->xp_context = EXPAND_USER_COMMANDS;
2206 xp->xp_pattern = arg;
2207 break;
2208
2209 case CMD_global:
2210 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002211 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002212 case CMD_and:
2213 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002214 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002215 case CMD_isearch:
2216 case CMD_dsearch:
2217 case CMD_ilist:
2218 case CMD_dlist:
2219 case CMD_ijump:
2220 case CMD_psearch:
2221 case CMD_djump:
2222 case CMD_isplit:
2223 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002224 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002225 case CMD_autocmd:
2226 return set_context_in_autocmd(xp, arg, FALSE);
2227 case CMD_doautocmd:
2228 case CMD_doautoall:
2229 return set_context_in_autocmd(xp, arg, TRUE);
2230 case CMD_set:
2231 set_context_in_set_cmd(xp, arg, 0);
2232 break;
2233 case CMD_setglobal:
2234 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2235 break;
2236 case CMD_setlocal:
2237 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2238 break;
2239 case CMD_tag:
2240 case CMD_stag:
2241 case CMD_ptag:
2242 case CMD_ltag:
2243 case CMD_tselect:
2244 case CMD_stselect:
2245 case CMD_ptselect:
2246 case CMD_tjump:
2247 case CMD_stjump:
2248 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002249 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002250 xp->xp_context = EXPAND_TAGS_LISTFILES;
2251 else
2252 xp->xp_context = EXPAND_TAGS;
2253 xp->xp_pattern = arg;
2254 break;
2255 case CMD_augroup:
2256 xp->xp_context = EXPAND_AUGROUP;
2257 xp->xp_pattern = arg;
2258 break;
2259#ifdef FEAT_SYN_HL
2260 case CMD_syntax:
2261 set_context_in_syntax_cmd(xp, arg);
2262 break;
2263#endif
2264#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002265 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002266 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002267 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002268 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002269 case CMD_if:
2270 case CMD_elseif:
2271 case CMD_while:
2272 case CMD_for:
2273 case CMD_echo:
2274 case CMD_echon:
2275 case CMD_execute:
2276 case CMD_echomsg:
2277 case CMD_echoerr:
2278 case CMD_call:
2279 case CMD_return:
2280 case CMD_cexpr:
2281 case CMD_caddexpr:
2282 case CMD_cgetexpr:
2283 case CMD_lexpr:
2284 case CMD_laddexpr:
2285 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002286 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002287 break;
2288
2289 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002290 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002291 case CMD_function:
2292 case CMD_delfunction:
2293 xp->xp_context = EXPAND_USER_FUNC;
2294 xp->xp_pattern = arg;
2295 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002296 case CMD_disassemble:
2297 set_context_in_disassemble_cmd(xp, arg);
2298 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002299
2300 case CMD_echohl:
2301 set_context_in_echohl_cmd(xp, arg);
2302 break;
2303#endif
2304 case CMD_highlight:
2305 set_context_in_highlight_cmd(xp, arg);
2306 break;
2307#ifdef FEAT_CSCOPE
2308 case CMD_cscope:
2309 case CMD_lcscope:
2310 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002311 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002312 break;
2313#endif
2314#ifdef FEAT_SIGNS
2315 case CMD_sign:
2316 set_context_in_sign_cmd(xp, arg);
2317 break;
2318#endif
2319 case CMD_bdelete:
2320 case CMD_bwipeout:
2321 case CMD_bunload:
2322 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2323 arg = xp->xp_pattern + 1;
2324 // FALLTHROUGH
2325 case CMD_buffer:
2326 case CMD_sbuffer:
2327 case CMD_checktime:
2328 xp->xp_context = EXPAND_BUFFERS;
2329 xp->xp_pattern = arg;
2330 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002331#ifdef FEAT_DIFF
2332 case CMD_diffget:
2333 case CMD_diffput:
2334 // If current buffer is in diff mode, complete buffer names
2335 // which are in diff mode, and different than current buffer.
2336 xp->xp_context = EXPAND_DIFF_BUFFERS;
2337 xp->xp_pattern = arg;
2338 break;
2339#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002340 case CMD_USER:
2341 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002342 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2343 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002344
2345 case CMD_map: case CMD_noremap:
2346 case CMD_nmap: case CMD_nnoremap:
2347 case CMD_vmap: case CMD_vnoremap:
2348 case CMD_omap: case CMD_onoremap:
2349 case CMD_imap: case CMD_inoremap:
2350 case CMD_cmap: case CMD_cnoremap:
2351 case CMD_lmap: case CMD_lnoremap:
2352 case CMD_smap: case CMD_snoremap:
2353 case CMD_tmap: case CMD_tnoremap:
2354 case CMD_xmap: case CMD_xnoremap:
2355 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002356 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002357 case CMD_unmap:
2358 case CMD_nunmap:
2359 case CMD_vunmap:
2360 case CMD_ounmap:
2361 case CMD_iunmap:
2362 case CMD_cunmap:
2363 case CMD_lunmap:
2364 case CMD_sunmap:
2365 case CMD_tunmap:
2366 case CMD_xunmap:
2367 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002368 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002369 case CMD_mapclear:
2370 case CMD_nmapclear:
2371 case CMD_vmapclear:
2372 case CMD_omapclear:
2373 case CMD_imapclear:
2374 case CMD_cmapclear:
2375 case CMD_lmapclear:
2376 case CMD_smapclear:
2377 case CMD_tmapclear:
2378 case CMD_xmapclear:
2379 xp->xp_context = EXPAND_MAPCLEAR;
2380 xp->xp_pattern = arg;
2381 break;
2382
2383 case CMD_abbreviate: case CMD_noreabbrev:
2384 case CMD_cabbrev: case CMD_cnoreabbrev:
2385 case CMD_iabbrev: case CMD_inoreabbrev:
2386 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002387 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002388 case CMD_unabbreviate:
2389 case CMD_cunabbrev:
2390 case CMD_iunabbrev:
2391 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002392 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002393#ifdef FEAT_MENU
2394 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2395 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2396 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2397 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2398 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2399 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2400 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2401 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2402 case CMD_tmenu: case CMD_tunmenu:
2403 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2404 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2405#endif
2406
2407 case CMD_colorscheme:
2408 xp->xp_context = EXPAND_COLORS;
2409 xp->xp_pattern = arg;
2410 break;
2411
2412 case CMD_compiler:
2413 xp->xp_context = EXPAND_COMPILER;
2414 xp->xp_pattern = arg;
2415 break;
2416
2417 case CMD_ownsyntax:
2418 xp->xp_context = EXPAND_OWNSYNTAX;
2419 xp->xp_pattern = arg;
2420 break;
2421
2422 case CMD_setfiletype:
2423 xp->xp_context = EXPAND_FILETYPE;
2424 xp->xp_pattern = arg;
2425 break;
2426
2427 case CMD_packadd:
2428 xp->xp_context = EXPAND_PACKADD;
2429 xp->xp_pattern = arg;
2430 break;
2431
zeertzjqb0d45ec2023-01-25 15:04:22 +00002432 case CMD_runtime:
2433 set_context_in_runtime_cmd(xp, arg);
2434 break;
2435
Bram Moolenaard0190392019-08-23 21:17:35 +02002436#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2437 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002438 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002439#endif
2440#if defined(FEAT_PROFILE)
2441 case CMD_profile:
2442 set_context_in_profile_cmd(xp, arg);
2443 break;
2444#endif
2445 case CMD_behave:
2446 xp->xp_context = EXPAND_BEHAVE;
2447 xp->xp_pattern = arg;
2448 break;
2449
2450 case CMD_messages:
2451 xp->xp_context = EXPAND_MESSAGES;
2452 xp->xp_pattern = arg;
2453 break;
2454
2455 case CMD_history:
2456 xp->xp_context = EXPAND_HISTORY;
2457 xp->xp_pattern = arg;
2458 break;
2459#if defined(FEAT_PROFILE)
2460 case CMD_syntime:
2461 xp->xp_context = EXPAND_SYNTIME;
2462 xp->xp_pattern = arg;
2463 break;
2464#endif
2465
2466 case CMD_argdelete:
2467 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2468 arg = xp->xp_pattern + 1;
2469 xp->xp_context = EXPAND_ARGLIST;
2470 xp->xp_pattern = arg;
2471 break;
2472
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002473#ifdef FEAT_EVAL
2474 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002475 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002476 case CMD_breakdel:
2477 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002478
2479 case CMD_scriptnames:
2480 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002481#endif
2482
Bram Moolenaard0190392019-08-23 21:17:35 +02002483 default:
2484 break;
2485 }
2486 return NULL;
2487}
2488
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002489/*
2490 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2491 * we don't need/want deleted. Maybe this could be done better if we didn't
2492 * repeat all this stuff. The only problem is that they may not stay
2493 * perfectly compatible with each other, but then the command line syntax
2494 * probably won't change that much -- webb.
2495 */
2496 static char_u *
2497set_one_cmd_context(
2498 expand_T *xp,
2499 char_u *buff) // buffer for command string
2500{
2501 char_u *p;
2502 char_u *cmd, *arg;
2503 int len = 0;
2504 exarg_T ea;
2505 int compl = EXPAND_NOTHING;
2506 int forceit = FALSE;
2507 int usefilter = FALSE; // filter instead of file name
2508
2509 ExpandInit(xp);
2510 xp->xp_pattern = buff;
2511 xp->xp_line = buff;
2512 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2513 ea.argt = 0;
2514
2515 // 1. skip comment lines and leading space, colons or bars
2516 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2517 ;
2518 xp->xp_pattern = cmd;
2519
2520 if (*cmd == NUL)
2521 return NULL;
2522 if (*cmd == '"') // ignore comment lines
2523 {
2524 xp->xp_context = EXPAND_NOTHING;
2525 return NULL;
2526 }
2527
2528 // 3. Skip over the range to find the command.
2529 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2530 xp->xp_pattern = cmd;
2531 if (*cmd == NUL)
2532 return NULL;
2533 if (*cmd == '"')
2534 {
2535 xp->xp_context = EXPAND_NOTHING;
2536 return NULL;
2537 }
2538
2539 if (*cmd == '|' || *cmd == '\n')
2540 return cmd + 1; // There's another command
2541
2542 // Get the command index.
2543 p = set_cmd_index(cmd, &ea, xp, &compl);
2544 if (p == NULL)
2545 return NULL;
2546
2547 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2548
2549 if (*p == '!') // forced commands
2550 {
2551 forceit = TRUE;
2552 ++p;
2553 }
2554
2555 // 6. parse arguments
2556 if (!IS_USER_CMDIDX(ea.cmdidx))
2557 ea.argt = excmd_get_argt(ea.cmdidx);
2558
2559 arg = skipwhite(p);
2560
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002561 // Does command allow "++argopt" argument?
2562 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002563 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002564 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2565 {
2566 p = arg + 2;
2567 while (*p && !vim_isspace(*p))
2568 MB_PTR_ADV(p);
2569
2570 // Still touching the command after "++"?
2571 if (*p == NUL)
2572 {
2573 if (ea.argt & EX_ARGOPT)
2574 return set_context_in_argopt(xp, arg + 2);
2575#ifdef FEAT_TERMINAL
2576 if (ea.cmdidx == CMD_terminal)
2577 return set_context_in_terminalopt(xp, arg + 2);
2578#endif
2579 }
2580
2581 arg = skipwhite(p);
2582 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002583 }
2584
2585 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2586 {
2587 if (*arg == '>') // append
2588 {
2589 if (*++arg == '>')
2590 ++arg;
2591 arg = skipwhite(arg);
2592 }
2593 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2594 {
2595 ++arg;
2596 usefilter = TRUE;
2597 }
2598 }
2599
2600 if (ea.cmdidx == CMD_read)
2601 {
2602 usefilter = forceit; // :r! filter if forced
2603 if (*arg == '!') // :r !filter
2604 {
2605 ++arg;
2606 usefilter = TRUE;
2607 }
2608 }
2609
2610 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2611 {
2612 while (*arg == *cmd) // allow any number of '>' or '<'
2613 ++arg;
2614 arg = skipwhite(arg);
2615 }
2616
2617 // Does command allow "+command"?
2618 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2619 {
2620 // Check if we're in the +command
2621 p = arg + 1;
2622 arg = skip_cmd_arg(arg, FALSE);
2623
2624 // Still touching the command after '+'?
2625 if (*arg == NUL)
2626 return p;
2627
2628 // Skip space(s) after +command to get to the real argument
2629 arg = skipwhite(arg);
2630 }
2631
2632
2633 // Check for '|' to separate commands and '"' to start comments.
2634 // Don't do this for ":read !cmd" and ":write !cmd".
2635 if ((ea.argt & EX_TRLBAR) && !usefilter)
2636 {
2637 p = arg;
2638 // ":redir @" is not the start of a comment
2639 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2640 p += 2;
2641 while (*p)
2642 {
2643 if (*p == Ctrl_V)
2644 {
2645 if (p[1] != NUL)
2646 ++p;
2647 }
2648 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2649 || *p == '|' || *p == '\n')
2650 {
2651 if (*(p - 1) != '\\')
2652 {
2653 if (*p == '|' || *p == '\n')
2654 return p + 1;
2655 return NULL; // It's a comment
2656 }
2657 }
2658 MB_PTR_ADV(p);
2659 }
2660 }
2661
2662 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2663 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2664 // no arguments allowed but there is something
2665 return NULL;
2666
2667 // Find start of last argument (argument just before cursor):
2668 p = buff;
2669 xp->xp_pattern = p;
2670 len = (int)STRLEN(buff);
2671 while (*p && p < buff + len)
2672 {
2673 if (*p == ' ' || *p == TAB)
2674 {
2675 // argument starts after a space
2676 xp->xp_pattern = ++p;
2677 }
2678 else
2679 {
2680 if (*p == '\\' && *(p + 1) != NUL)
2681 ++p; // skip over escaped character
2682 MB_PTR_ADV(p);
2683 }
2684 }
2685
2686 if (ea.argt & EX_XFILE)
2687 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2688
2689 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002690 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002691 forceit);
2692}
2693
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002694/*
2695 * Set the completion context in 'xp' for command 'str'
2696 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002697 void
2698set_cmd_context(
2699 expand_T *xp,
2700 char_u *str, // start of command line
2701 int len, // length of command line (excl. NUL)
2702 int col, // position of cursor
2703 int use_ccline UNUSED) // use ccline for info
2704{
2705#ifdef FEAT_EVAL
2706 cmdline_info_T *ccline = get_cmdline_info();
2707#endif
2708 int old_char = NUL;
2709 char_u *nextcomm;
2710
2711 // Avoid a UMR warning from Purify, only save the character if it has been
2712 // written before.
2713 if (col < len)
2714 old_char = str[col];
2715 str[col] = NUL;
2716 nextcomm = str;
2717
2718#ifdef FEAT_EVAL
2719 if (use_ccline && ccline->cmdfirstc == '=')
2720 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002721 // pass CMD_SIZE because there is no real command
2722 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002723 }
2724 else if (use_ccline && ccline->input_fn)
2725 {
2726 xp->xp_context = ccline->xp_context;
2727 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002728 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002729 }
2730 else
2731#endif
2732 while (nextcomm != NULL)
2733 nextcomm = set_one_cmd_context(xp, nextcomm);
2734
2735 // Store the string here so that call_user_expand_func() can get to them
2736 // easily.
2737 xp->xp_line = str;
2738 xp->xp_col = col;
2739
2740 str[col] = old_char;
2741}
2742
2743/*
2744 * Expand the command line "str" from context "xp".
2745 * "xp" must have been set by set_cmd_context().
2746 * xp->xp_pattern points into "str", to where the text that is to be expanded
2747 * starts.
2748 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2749 * cursor.
2750 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2751 * key that triggered expansion literally.
2752 * Returns EXPAND_OK otherwise.
2753 */
2754 int
2755expand_cmdline(
2756 expand_T *xp,
2757 char_u *str, // start of command line
2758 int col, // position of cursor
2759 int *matchcount, // return: nr of matches
2760 char_u ***matches) // return: array of pointers to matches
2761{
2762 char_u *file_str = NULL;
2763 int options = WILD_ADD_SLASH|WILD_SILENT;
2764
2765 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2766 {
2767 beep_flush();
2768 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2769 }
2770 if (xp->xp_context == EXPAND_NOTHING)
2771 {
2772 // Caller can use the character as a normal char instead
2773 return EXPAND_NOTHING;
2774 }
2775
2776 // add star to file name, or convert to regexp if not exp. files.
2777 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002778 if (cmdline_fuzzy_completion_supported(xp))
2779 // If fuzzy matching, don't modify the search string
2780 file_str = vim_strsave(xp->xp_pattern);
2781 else
2782 {
2783 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2784 if (file_str == NULL)
2785 return EXPAND_UNSUCCESSFUL;
2786 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002787
2788 if (p_wic)
2789 options += WILD_ICASE;
2790
2791 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002792 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002793 {
2794 *matchcount = 0;
2795 *matches = NULL;
2796 }
2797 vim_free(file_str);
2798
2799 return EXPAND_OK;
2800}
2801
Bram Moolenaar66b51422019-08-18 21:44:12 +02002802/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002803 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002804 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002805 */
2806 static int
2807expand_files_and_dirs(
2808 expand_T *xp,
2809 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002810 char_u ***matches,
2811 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002812 int flags,
2813 int options)
2814{
2815 int free_pat = FALSE;
2816 int i;
2817 int ret;
2818
2819 // for ":set path=" and ":set tags=" halve backslashes for escaped
2820 // space
2821 if (xp->xp_backslash != XP_BS_NONE)
2822 {
2823 free_pat = TRUE;
2824 pat = vim_strsave(pat);
2825 for (i = 0; pat[i]; ++i)
2826 if (pat[i] == '\\')
2827 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002828 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002829 && pat[i + 1] == '\\'
2830 && pat[i + 2] == '\\'
2831 && pat[i + 3] == ' ')
2832 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002833 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002834 && pat[i + 1] == ' ')
2835 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002836 else if ((xp->xp_backslash & XP_BS_COMMA)
2837 && pat[i + 1] == '\\'
2838 && pat[i + 2] == ',')
2839 STRMOVE(pat + i, pat + i + 2);
2840#ifdef BACKSLASH_IN_FILENAME
2841 else if ((xp->xp_backslash & XP_BS_COMMA)
2842 && pat[i + 1] == ',')
2843 STRMOVE(pat + i, pat + i + 1);
2844#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002845 }
2846 }
2847
2848 if (xp->xp_context == EXPAND_FILES)
2849 flags |= EW_FILE;
2850 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2851 flags |= (EW_FILE | EW_PATH);
LemonBoya20bf692024-07-11 22:35:53 +02002852 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2853 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002854 else
2855 flags = (flags | EW_DIR) & ~EW_FILE;
2856 if (options & WILD_ICASE)
2857 flags |= EW_ICASE;
2858
2859 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002860 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002861 if (free_pat)
2862 vim_free(pat);
2863#ifdef BACKSLASH_IN_FILENAME
2864 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2865 {
2866 int j;
2867
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002868 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002869 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002870 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002871
2872 while (*ptr != NUL)
2873 {
2874 if (p_csl[0] == 's' && *ptr == '\\')
2875 *ptr = '/';
2876 else if (p_csl[0] == 'b' && *ptr == '/')
2877 *ptr = '\\';
2878 ptr += (*mb_ptr2len)(ptr);
2879 }
2880 }
2881 }
2882#endif
2883 return ret;
2884}
2885
2886/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002887 * Function given to ExpandGeneric() to obtain the possible arguments of the
2888 * ":behave {mswin,xterm}" command.
2889 */
2890 static char_u *
2891get_behave_arg(expand_T *xp UNUSED, int idx)
2892{
2893 if (idx == 0)
2894 return (char_u *)"mswin";
2895 if (idx == 1)
2896 return (char_u *)"xterm";
2897 return NULL;
2898}
2899
K.Takata161b6ac2022-11-14 15:31:07 +00002900#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002901/*
2902 * Function given to ExpandGeneric() to obtain the possible arguments of the
2903 * ":breakadd {expr, file, func, here}" command.
2904 * ":breakdel {func, file, here}" command.
2905 */
2906 static char_u *
2907get_breakadd_arg(expand_T *xp UNUSED, int idx)
2908{
2909 char *opts[] = {"expr", "file", "func", "here"};
2910
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002911 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002912 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002913 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002914 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2915 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002916 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2917 {
2918 // breakdel {func, file, here}
2919 if (idx <= 2)
2920 return (char_u *)opts[idx + 1];
2921 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002922 else
2923 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002924 // profdel {func, file}
2925 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002926 return (char_u *)opts[idx + 1];
2927 }
2928 }
2929 return NULL;
2930}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002931
2932/*
2933 * Function given to ExpandGeneric() to obtain the possible arguments for the
2934 * ":scriptnames" command.
2935 */
2936 static char_u *
2937get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2938{
2939 scriptitem_T *si;
2940
2941 if (!SCRIPT_ID_VALID(idx + 1))
2942 return NULL;
2943
2944 si = SCRIPT_ITEM(idx + 1);
2945 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2946 return NameBuff;
2947}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002948#endif
2949
Bram Moolenaard0190392019-08-23 21:17:35 +02002950/*
2951 * Function given to ExpandGeneric() to obtain the possible arguments of the
2952 * ":messages {clear}" command.
2953 */
2954 static char_u *
2955get_messages_arg(expand_T *xp UNUSED, int idx)
2956{
2957 if (idx == 0)
2958 return (char_u *)"clear";
2959 return NULL;
2960}
2961
2962 static char_u *
2963get_mapclear_arg(expand_T *xp UNUSED, int idx)
2964{
2965 if (idx == 0)
2966 return (char_u *)"<buffer>";
2967 return NULL;
2968}
2969
2970/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002971 * Do the expansion based on xp->xp_context and 'rmp'.
2972 */
2973 static int
2974ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002975 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002976 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002977 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002978 char_u ***matches,
2979 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002980{
2981 static struct expgen
2982 {
2983 int context;
2984 char_u *((*func)(expand_T *, int));
2985 int ic;
2986 int escaped;
2987 } tab[] =
2988 {
2989 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2990 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2991 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2992 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2993 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2994 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2995 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2996 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2997 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2998 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002999#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003000 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3001 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3002 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3003 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3004 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003005#endif
3006#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003007 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3008 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003009#endif
3010#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003011 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003012#endif
3013#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003014 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003015#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003016 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3017 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3018 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003019#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003020 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003021#endif
3022#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003023 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003024#endif
3025#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003026 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003027#endif
3028#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003029 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3030 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003031#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003032 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3033 {EXPAND_USER, get_users, TRUE, FALSE},
3034 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003035#ifdef FEAT_EVAL
3036 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003037 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003038#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003039 };
3040 int i;
3041 int ret = FAIL;
3042
3043 // Find a context in the table and call the ExpandGeneric() with the
3044 // right function to do the expansion.
3045 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3046 {
3047 if (xp->xp_context == tab[i].context)
3048 {
3049 if (tab[i].ic)
3050 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003051 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3052 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003053 break;
3054 }
3055 }
3056
3057 return ret;
3058}
3059
3060/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003061 * Map wild expand options to flags for expand_wildcards()
3062 */
3063 static int
3064map_wildopts_to_ewflags(int options)
3065{
3066 int flags;
3067
3068 flags = EW_DIR; // include directories
3069 if (options & WILD_LIST_NOTFOUND)
3070 flags |= EW_NOTFOUND;
3071 if (options & WILD_ADD_SLASH)
3072 flags |= EW_ADDSLASH;
3073 if (options & WILD_KEEP_ALL)
3074 flags |= EW_KEEPALL;
3075 if (options & WILD_SILENT)
3076 flags |= EW_SILENT;
3077 if (options & WILD_NOERROR)
3078 flags |= EW_NOERROR;
3079 if (options & WILD_ALLLINKS)
3080 flags |= EW_ALLLINKS;
3081
3082 return flags;
3083}
3084
3085/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003086 * Do the expansion based on xp->xp_context and "pat".
3087 */
3088 static int
3089ExpandFromContext(
3090 expand_T *xp,
3091 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003092 char_u ***matches,
3093 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003094 int options) // WILD_ flags
3095{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003096 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003097 int ret;
3098 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003099 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003100 int fuzzy = cmdline_fuzzy_complete(pat)
3101 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003103 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104
3105 if (xp->xp_context == EXPAND_FILES
3106 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003107 || xp->xp_context == EXPAND_FILES_IN_PATH
3108 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003109 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3110 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003112 *matches = (char_u **)"";
3113 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003114 if (xp->xp_context == EXPAND_HELP)
3115 {
3116 // With an empty argument we would get all the help tags, which is
3117 // very slow. Get matches for "help" instead.
3118 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003119 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120 {
3121#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003122 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003123#endif
3124 return OK;
3125 }
3126 return FAIL;
3127 }
3128
Bram Moolenaar66b51422019-08-18 21:44:12 +02003129 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003130 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003131 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003132 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003134 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003135#ifdef FEAT_DIFF
3136 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003137 return ExpandBufnames(pat, numMatches, matches,
3138 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003139#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140 if (xp->xp_context == EXPAND_TAGS
3141 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003142 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3143 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003144 if (xp->xp_context == EXPAND_COLORS)
3145 {
3146 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003147 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003148 directories);
3149 }
3150 if (xp->xp_context == EXPAND_COMPILER)
3151 {
3152 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003153 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003154 }
3155 if (xp->xp_context == EXPAND_OWNSYNTAX)
3156 {
3157 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003158 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003159 }
3160 if (xp->xp_context == EXPAND_FILETYPE)
3161 {
3162 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003163 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003164 }
Doug Kearns81642d92024-01-04 22:37:44 +01003165#ifdef FEAT_KEYMAP
3166 if (xp->xp_context == EXPAND_KEYMAP)
3167 {
3168 char *directories[] = {"keymap", NULL};
3169 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3170 }
3171#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003172#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003173 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003174 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003175#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003176 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003177 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003178 if (xp->xp_context == EXPAND_RUNTIME)
3179 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003180
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003181 // When expanding a function name starting with s:, match the <SNR>nr_
3182 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003183 if ((xp->xp_context == EXPAND_USER_FUNC
3184 || xp->xp_context == EXPAND_DISASSEMBLE)
3185 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003186 {
3187 int len = (int)STRLEN(pat) + 20;
3188
3189 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003190 if (tofree == NULL)
3191 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003192 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003193 pat = tofree;
3194 }
3195
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003196 if (!fuzzy)
3197 {
3198 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3199 if (regmatch.regprog == NULL)
3200 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003201
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003202 // set ignore-case according to p_ic, p_scs and pat
3203 regmatch.rm_ic = ignorecase(pat);
3204 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003205
3206 if (xp->xp_context == EXPAND_SETTINGS
3207 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003208 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003209 else if (xp->xp_context == EXPAND_STRING_SETTING)
3210 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3211 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3212 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003213 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003214 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003215 else if (xp->xp_context == EXPAND_ARGOPT)
3216 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3217#if defined(FEAT_TERMINAL)
3218 else if (xp->xp_context == EXPAND_TERMINALOPT)
3219 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3220#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003221#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003222 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003223 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003224#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003225 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003226 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003227
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003228 if (!fuzzy)
3229 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003230 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003231
3232 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003233}
3234
Bram Moolenaar66b51422019-08-18 21:44:12 +02003235/*
3236 * Expand a list of names.
3237 *
3238 * Generic function for command line completion. It calls a function to
3239 * obtain strings, one by one. The strings are matched against a regexp
3240 * program. Matching strings are copied into an array, which is returned.
3241 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003242 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3243 * is used.
3244 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003245 * Returns OK when no problems encountered, FAIL for error (out of memory).
3246 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003247 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003248ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003249 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003250 expand_T *xp,
3251 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003252 char_u ***matches,
3253 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003254 char_u *((*func)(expand_T *, int)),
3255 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003256 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003257{
3258 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003259 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003260 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003261 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003262 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003263 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003264 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003265 int sort_matches = FALSE;
3266 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003267
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003268 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003269 *matches = NULL;
3270 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003271
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003272 if (!fuzzy)
3273 ga_init2(&ga, sizeof(char *), 30);
3274 else
3275 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3276
3277 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003278 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003279 str = (*func)(xp, i);
3280 if (str == NULL) // end of list
3281 break;
3282 if (*str == NUL) // skip empty strings
3283 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003284
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003285 if (xp->xp_pattern[0] != NUL)
3286 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003287 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003288 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003289 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003290 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003291 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003292 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003293 }
3294 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003295 else
3296 match = TRUE;
3297
3298 if (!match)
3299 continue;
3300
3301 if (escaped)
3302 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3303 else
3304 str = vim_strsave(str);
3305 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003306 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003307 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003308 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003309 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003310 return FAIL;
3311 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003312 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003313 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003314 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003315
3316 if (ga_grow(&ga, 1) == FAIL)
3317 {
3318 vim_free(str);
3319 break;
3320 }
3321
3322 if (fuzzy)
3323 {
3324 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3325 fuzmatch->idx = ga.ga_len;
3326 fuzmatch->str = str;
3327 fuzmatch->score = score;
3328 }
3329 else
3330 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3331
K.Takata161b6ac2022-11-14 15:31:07 +00003332#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003333 if (func == get_menu_names)
3334 {
3335 // test for separator added by get_menu_names()
3336 str += STRLEN(str) - 1;
3337 if (*str == '\001')
3338 *str = '.';
3339 }
K.Takata161b6ac2022-11-14 15:31:07 +00003340#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003341
3342 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003343 }
3344
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003345 if (ga.ga_len == 0)
3346 return OK;
3347
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003348 // sort the matches when using regular expression matching and sorting
3349 // applies to the completion context. Menus and scriptnames should be kept
3350 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003351 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003352 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003353 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003354 && xp->xp_context != EXPAND_SCRIPTNAMES
3355 && xp->xp_context != EXPAND_ARGOPT
3356 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003357 sort_matches = TRUE;
3358
3359 // <SNR> functions should be sorted to the end.
3360 if (xp->xp_context == EXPAND_EXPRESSION
3361 || xp->xp_context == EXPAND_FUNCTIONS
3362 || xp->xp_context == EXPAND_USER_FUNC
3363 || xp->xp_context == EXPAND_DISASSEMBLE)
3364 funcsort = TRUE;
3365
3366 // Sort the matches.
3367 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003368 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003369 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003370 // <SNR> functions should be sorted to the end.
3371 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3372 sort_func_compare);
3373 else
3374 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3375 }
3376
3377 if (!fuzzy)
3378 {
3379 *matches = ga.ga_data;
3380 *numMatches = ga.ga_len;
3381 }
3382 else
3383 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003384 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3385 funcsort) == FAIL)
3386 return FAIL;
3387 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003388 }
3389
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003390#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003391 // Reset the variables used for special highlight names expansion, so that
3392 // they don't show up when getting normal highlight names by ID.
3393 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003394#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003395
Bram Moolenaar66b51422019-08-18 21:44:12 +02003396 return OK;
3397}
3398
3399/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003400 * Expand shell command matches in one directory of $PATH.
3401 */
3402 static void
3403expand_shellcmd_onedir(
3404 char_u *buf,
3405 char_u *s,
3406 size_t l,
3407 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003408 char_u ***matches,
3409 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003410 int flags,
3411 hashtab_T *ht,
3412 garray_T *gap)
3413{
3414 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003415 hash_T hash;
3416 hashitem_T *hi;
3417
3418 vim_strncpy(buf, s, l);
3419 add_pathsep(buf);
3420 l = STRLEN(buf);
3421 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3422
3423 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003424 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003425 if (ret != OK)
3426 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003427
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003428 if (ga_grow(gap, *numMatches) == FAIL)
3429 {
3430 FreeWild(*numMatches, *matches);
3431 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003432 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003433
3434 for (int i = 0; i < *numMatches; ++i)
3435 {
3436 char_u *name = (*matches)[i];
3437
3438 if (STRLEN(name) > l)
3439 {
3440 // Check if this name was already found.
3441 hash = hash_hash(name + l);
3442 hi = hash_lookup(ht, name + l, hash);
3443 if (HASHITEM_EMPTY(hi))
3444 {
3445 // Remove the path that was prepended.
3446 STRMOVE(name, name + l);
3447 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3448 hash_add_item(ht, hi, name, hash);
3449 name = NULL;
3450 }
3451 }
3452 vim_free(name);
3453 }
3454 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003455}
3456
3457/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003458 * Complete a shell command.
3459 * Returns FAIL or OK;
3460 */
3461 static int
3462expand_shellcmd(
3463 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003464 char_u ***matches, // return: array with matches
3465 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003466 int flagsarg) // EW_ flags
3467{
3468 char_u *pat;
3469 int i;
3470 char_u *path = NULL;
3471 int mustfree = FALSE;
3472 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003473 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003474 size_t l;
3475 char_u *s, *e;
3476 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003477 int did_curdir = FALSE;
3478 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003479
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003480 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003481 if (buf == NULL)
3482 return FAIL;
3483
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003484 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003485 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003486 if (pat == NULL)
3487 {
3488 vim_free(buf);
3489 return FAIL;
3490 }
3491
Bram Moolenaar66b51422019-08-18 21:44:12 +02003492 for (i = 0; pat[i]; ++i)
3493 if (pat[i] == '\\' && pat[i + 1] == ' ')
3494 STRMOVE(pat + i, pat + i + 1);
3495
3496 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3497
3498 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3499 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3500 path = (char_u *)".";
3501 else
3502 {
3503 // For an absolute name we don't use $PATH.
3504 if (!mch_isFullName(pat))
3505 path = vim_getenv((char_u *)"PATH", &mustfree);
3506 if (path == NULL)
3507 path = (char_u *)"";
3508 }
3509
3510 // Go over all directories in $PATH. Expand matches in that directory and
3511 // collect them in "ga". When "." is not in $PATH also expand for the
3512 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003513 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003514 hash_init(&found_ht);
3515 for (s = path; ; s = e)
3516 {
K.Takata161b6ac2022-11-14 15:31:07 +00003517#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003518 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003519#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003520 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003521#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003522 if (e == NULL)
3523 e = s + STRLEN(s);
3524
3525 if (*s == NUL)
3526 {
3527 if (did_curdir)
3528 break;
3529 // Find directories in the current directory, path is empty.
3530 did_curdir = TRUE;
3531 flags |= EW_DIR;
3532 }
3533 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3534 {
3535 did_curdir = TRUE;
3536 flags |= EW_DIR;
3537 }
3538 else
3539 // Do not match directories inside a $PATH item.
3540 flags &= ~EW_DIR;
3541
3542 l = e - s;
3543 if (l > MAXPATHL - 5)
3544 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003545
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003546 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003547 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003548
Bram Moolenaar66b51422019-08-18 21:44:12 +02003549 if (*e != NUL)
3550 ++e;
3551 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003552 *matches = ga.ga_data;
3553 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003554
3555 vim_free(buf);
3556 vim_free(pat);
3557 if (mustfree)
3558 vim_free(path);
3559 hash_clear(&found_ht);
3560 return OK;
3561}
3562
K.Takata161b6ac2022-11-14 15:31:07 +00003563#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003564/*
3565 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003566 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003567 */
3568 static void *
3569call_user_expand_func(
3570 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003571 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003572{
3573 cmdline_info_T *ccline = get_cmdline_info();
3574 int keep = 0;
3575 typval_T args[4];
3576 sctx_T save_current_sctx = current_sctx;
3577 char_u *pat = NULL;
3578 void *ret;
3579
3580 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3581 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003582
3583 if (ccline->cmdbuff != NULL)
3584 {
3585 keep = ccline->cmdbuff[ccline->cmdlen];
3586 ccline->cmdbuff[ccline->cmdlen] = 0;
3587 }
3588
3589 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3590
3591 args[0].v_type = VAR_STRING;
3592 args[0].vval.v_string = pat;
3593 args[1].v_type = VAR_STRING;
3594 args[1].vval.v_string = xp->xp_line;
3595 args[2].v_type = VAR_NUMBER;
3596 args[2].vval.v_number = xp->xp_col;
3597 args[3].v_type = VAR_UNKNOWN;
3598
3599 current_sctx = xp->xp_script_ctx;
3600
3601 ret = user_expand_func(xp->xp_arg, 3, args);
3602
3603 current_sctx = save_current_sctx;
3604 if (ccline->cmdbuff != NULL)
3605 ccline->cmdbuff[ccline->cmdlen] = keep;
3606
3607 vim_free(pat);
3608 return ret;
3609}
3610
3611/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003612 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3613 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003614 */
3615 static int
3616ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003617 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003618 expand_T *xp,
3619 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003620 char_u ***matches,
3621 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003622{
3623 char_u *retstr;
3624 char_u *s;
3625 char_u *e;
3626 int keep;
3627 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003628 int fuzzy;
3629 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003630 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003631
3632 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003633 *matches = NULL;
3634 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003635
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003636 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003637 if (retstr == NULL)
3638 return FAIL;
3639
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003640 if (!fuzzy)
3641 ga_init2(&ga, sizeof(char *), 3);
3642 else
3643 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3644
Bram Moolenaar66b51422019-08-18 21:44:12 +02003645 for (s = retstr; *s != NUL; s = e)
3646 {
3647 e = vim_strchr(s, '\n');
3648 if (e == NULL)
3649 e = s + STRLEN(s);
3650 keep = *e;
3651 *e = NUL;
3652
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003653 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003654 {
3655 if (!fuzzy)
3656 match = vim_regexec(regmatch, s, (colnr_T)0);
3657 else
3658 {
3659 score = fuzzy_match_str(s, pat);
3660 match = (score != 0);
3661 }
3662 }
3663 else
3664 match = TRUE; // match everything
3665
Bram Moolenaar66b51422019-08-18 21:44:12 +02003666 *e = keep;
3667
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003668 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003669 {
3670 if (ga_grow(&ga, 1) == FAIL)
3671 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003672 if (!fuzzy)
3673 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3674 else
3675 {
3676 fuzmatch_str_T *fuzmatch =
3677 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003678 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003679 fuzmatch->str = vim_strnsave(s, e - s);
3680 fuzmatch->score = score;
3681 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003682 ++ga.ga_len;
3683 }
3684
3685 if (*e != NUL)
3686 ++e;
3687 }
3688 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003689
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003690 if (ga.ga_len == 0)
3691 return OK;
3692
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003693 if (!fuzzy)
3694 {
3695 *matches = ga.ga_data;
3696 *numMatches = ga.ga_len;
3697 }
3698 else
3699 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003700 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3701 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003702 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003703 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003704 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003705 return OK;
3706}
3707
3708/*
3709 * Expand names with a list returned by a function defined by the user.
3710 */
3711 static int
3712ExpandUserList(
3713 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003714 char_u ***matches,
3715 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003716{
3717 list_T *retlist;
3718 listitem_T *li;
3719 garray_T ga;
3720
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003721 *matches = NULL;
3722 *numMatches = 0;
3723 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003724 if (retlist == NULL)
3725 return FAIL;
3726
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003727 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003728 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003729 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003730 {
3731 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3732 continue; // Skip non-string items and empty strings
3733
3734 if (ga_grow(&ga, 1) == FAIL)
3735 break;
3736
3737 ((char_u **)ga.ga_data)[ga.ga_len] =
3738 vim_strsave(li->li_tv.vval.v_string);
3739 ++ga.ga_len;
3740 }
3741 list_unref(retlist);
3742
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003743 *matches = ga.ga_data;
3744 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003745 return OK;
3746}
K.Takata161b6ac2022-11-14 15:31:07 +00003747#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003748
3749/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003750 * Expand "file" for all comma-separated directories in "path".
3751 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003752 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003753 */
3754 void
3755globpath(
3756 char_u *path,
3757 char_u *file,
3758 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003759 int expand_options,
3760 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003761{
3762 expand_T xpc;
3763 char_u *buf;
3764 int i;
3765 int num_p;
3766 char_u **p;
3767
3768 buf = alloc(MAXPATHL);
3769 if (buf == NULL)
3770 return;
3771
3772 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003773 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003774
3775 // Loop over all entries in {path}.
3776 while (*path != NUL)
3777 {
3778 // Copy one item of the path to buf[] and concatenate the file name.
3779 copy_option_part(&path, buf, MAXPATHL, ",");
3780 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3781 {
K.Takata161b6ac2022-11-14 15:31:07 +00003782#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003783 // Using the platform's path separator (\) makes vim incorrectly
3784 // treat it as an escape character, use '/' instead.
3785 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3786 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003787#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003788 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003789#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003790 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003791 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003792 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3793 {
3794 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3795
3796 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003797 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003798 for (i = 0; i < num_p; ++i)
3799 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003800 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003801 ++ga->ga_len;
3802 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003803 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003804 }
3805 }
3806 }
3807
3808 vim_free(buf);
3809}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003810
Bram Moolenaareadee482020-09-04 15:37:31 +02003811/*
3812 * Translate some keys pressed when 'wildmenu' is used.
3813 */
3814 int
3815wildmenu_translate_key(
3816 cmdline_info_T *cclp,
3817 int key,
3818 expand_T *xp,
3819 int did_wild_list)
3820{
3821 int c = key;
3822
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003823 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003824 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003825 // When the popup menu is used for cmdline completion:
3826 // Up : go to the previous item in the menu
3827 // Down : go to the next item in the menu
3828 // Left : go to the parent directory
3829 // Right: list the files in the selected directory
3830 switch (c)
3831 {
3832 case K_UP: c = K_LEFT; break;
3833 case K_DOWN: c = K_RIGHT; break;
3834 case K_LEFT: c = K_UP; break;
3835 case K_RIGHT: c = K_DOWN; break;
3836 default: break;
3837 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003838 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003839
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003840 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003841 {
3842 if (c == K_LEFT)
3843 c = Ctrl_P;
3844 else if (c == K_RIGHT)
3845 c = Ctrl_N;
3846 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003847
Bram Moolenaareadee482020-09-04 15:37:31 +02003848 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003849 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003850 && cclp->cmdpos > 1
3851 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3852 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3853 && (c == '\n' || c == '\r' || c == K_KENTER))
3854 c = K_DOWN;
3855
3856 return c;
3857}
3858
3859/*
3860 * Delete characters on the command line, from "from" to the current
3861 * position.
3862 */
3863 static void
3864cmdline_del(cmdline_info_T *cclp, int from)
3865{
3866 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3867 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3868 cclp->cmdlen -= cclp->cmdpos - from;
3869 cclp->cmdpos = from;
3870}
3871
3872/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003873 * Handle a key pressed when the wild menu for the menu names
3874 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003875 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003876 static int
3877wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003878{
Bram Moolenaareadee482020-09-04 15:37:31 +02003879 int i;
3880 int j;
3881
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003882 // Hitting <Down> after "emenu Name.": complete submenu
3883 if (key == K_DOWN && cclp->cmdpos > 0
3884 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003885 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003886 key = p_wc;
3887 KeyTyped = TRUE; // in case the key was mapped
3888 }
3889 else if (key == K_UP)
3890 {
3891 // Hitting <Up>: Remove one submenu name in front of the
3892 // cursor
3893 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003894
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003895 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3896 i = 0;
3897 while (--j > 0)
3898 {
3899 // check for start of menu name
3900 if (cclp->cmdbuff[j] == ' '
3901 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003902 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003903 i = j + 1;
3904 break;
3905 }
3906 // check for start of submenu name
3907 if (cclp->cmdbuff[j] == '.'
3908 && cclp->cmdbuff[j - 1] != '\\')
3909 {
3910 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003911 {
3912 i = j + 1;
3913 break;
3914 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003915 else
3916 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003917 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003918 }
3919 if (i > 0)
3920 cmdline_del(cclp, i);
3921 key = p_wc;
3922 KeyTyped = TRUE; // in case the key was mapped
3923 xp->xp_context = EXPAND_NOTHING;
3924 }
3925
3926 return key;
3927}
3928
3929/*
3930 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3931 * directory names (EXPAND_DIRECTORIES) or shell command names
3932 * (EXPAND_SHELLCMD) is displayed.
3933 */
3934 static int
3935wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3936{
3937 int i;
3938 int j;
3939 char_u upseg[5];
3940
3941 upseg[0] = PATHSEP;
3942 upseg[1] = '.';
3943 upseg[2] = '.';
3944 upseg[3] = PATHSEP;
3945 upseg[4] = NUL;
3946
3947 if (key == K_DOWN
3948 && cclp->cmdpos > 0
3949 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3950 && (cclp->cmdpos < 3
3951 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3952 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3953 {
3954 // go down a directory
3955 key = p_wc;
3956 KeyTyped = TRUE; // in case the key was mapped
3957 }
3958 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3959 {
3960 // If in a direct ancestor, strip off one ../ to go down
3961 int found = FALSE;
3962
3963 j = cclp->cmdpos;
3964 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3965 while (--j > i)
3966 {
3967 if (has_mbyte)
3968 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3969 if (vim_ispathsep(cclp->cmdbuff[j]))
3970 {
3971 found = TRUE;
3972 break;
3973 }
3974 }
3975 if (found
3976 && cclp->cmdbuff[j - 1] == '.'
3977 && cclp->cmdbuff[j - 2] == '.'
3978 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3979 {
3980 cmdline_del(cclp, j - 2);
3981 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003982 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003983 }
3984 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003985 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003986 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003987 // go up a directory
3988 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003989
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003990 j = cclp->cmdpos - 1;
3991 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3992 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003993 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003994 if (has_mbyte)
3995 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3996 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003997#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003998 && vim_strchr((char_u *)" *?[{`$%#",
3999 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004000#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004001 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004002 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004003 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004004 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004005 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004006 break;
4007 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004008 else
4009 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004010 }
4011 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004012
4013 if (!found)
4014 j = i;
4015 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4016 j += 4;
4017 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4018 && j == i)
4019 j += 3;
4020 else
4021 j = 0;
4022 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004023 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004024 // TODO this is only for DOS/UNIX systems - need to put in
4025 // machine-specific stuff here and in upseg init
4026 cmdline_del(cclp, j);
4027 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004028 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004029 else if (cclp->cmdpos > i)
4030 cmdline_del(cclp, i);
4031
4032 // Now complete in the new directory. Set KeyTyped in case the
4033 // Up key came from a mapping.
4034 key = p_wc;
4035 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004036 }
4037
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004038 return key;
4039}
4040
4041/*
4042 * Handle a key pressed when the wild menu is displayed
4043 */
4044 int
4045wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4046{
4047 if (xp->xp_context == EXPAND_MENUNAMES)
4048 return wildmenu_process_key_menunames(cclp, key, xp);
4049 else if ((xp->xp_context == EXPAND_FILES
4050 || xp->xp_context == EXPAND_DIRECTORIES
4051 || xp->xp_context == EXPAND_SHELLCMD))
4052 return wildmenu_process_key_filenames(cclp, key, xp);
4053
4054 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004055}
4056
4057/*
4058 * Free expanded names when finished walking through the matches
4059 */
4060 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004061wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004062{
4063 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004064
4065 if (!p_wmnu || wild_menu_showing == 0)
4066 return;
4067
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004068#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004069 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004070 if (cclp->input_fn)
4071 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004072#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004073
4074 if (wild_menu_showing == WM_SCROLLED)
4075 {
4076 // Entered command line, move it up
4077 cmdline_row--;
4078 redrawcmd();
4079 }
4080 else if (save_p_ls != -1)
4081 {
4082 // restore 'laststatus' and 'winminheight'
4083 p_ls = save_p_ls;
4084 p_wmh = save_p_wmh;
4085 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004086 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004087 redrawcmd();
4088 save_p_ls = -1;
4089 }
4090 else
4091 {
4092 win_redraw_last_status(topframe);
4093 redraw_statuslines();
4094 }
4095 KeyTyped = skt;
4096 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004097#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004098 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004099 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004100#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004101}
Bram Moolenaareadee482020-09-04 15:37:31 +02004102
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004103#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004104/*
4105 * "getcompletion()" function
4106 */
4107 void
4108f_getcompletion(typval_T *argvars, typval_T *rettv)
4109{
4110 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004111 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004112 expand_T xpc;
4113 int filtered = FALSE;
4114 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004115 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004116
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004117 if (in_vim9script()
4118 && (check_for_string_arg(argvars, 0) == FAIL
4119 || check_for_string_arg(argvars, 1) == FAIL
4120 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4121 return;
4122
ii144785fe02021-11-21 12:13:56 +00004123 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004124 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004125 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004126 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004127
4128 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004129 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004130
4131 if (p_wic)
4132 options |= WILD_ICASE;
4133
4134 // For filtered results, 'wildignore' is used
4135 if (!filtered)
4136 options |= WILD_KEEP_ALL;
4137
4138 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004139 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004140 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004141 int cmdline_len = (int)STRLEN(pat);
4142 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004143 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004144 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004145 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004146 else
4147 {
ii144785fe02021-11-21 12:13:56 +00004148 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004149 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004150 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004151
4152 xpc.xp_context = cmdcomplete_str_to_type(type);
4153 if (xpc.xp_context == EXPAND_NOTHING)
4154 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004155 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004156 return;
4157 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004158
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004159 if (xpc.xp_context == EXPAND_USER_DEFINED)
4160 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004161 // Must be "custom,funcname" pattern
4162 if (STRNCMP(type, "custom,", 7) != 0)
4163 {
4164 semsg(_(e_invalid_argument_str), type);
4165 return;
4166 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004167
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004168 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004169 }
4170
4171 if (xpc.xp_context == EXPAND_USER_LIST)
4172 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004173 // Must be "customlist,funcname" pattern
4174 if (STRNCMP(type, "customlist,", 11) != 0)
4175 {
4176 semsg(_(e_invalid_argument_str), type);
4177 return;
4178 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004179
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004180 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004181 }
4182
Bram Moolenaar66b51422019-08-18 21:44:12 +02004183# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004184 if (xpc.xp_context == EXPAND_MENUS)
4185 {
4186 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4187 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4188 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004189# endif
4190# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004191 if (xpc.xp_context == EXPAND_CSCOPE)
4192 {
4193 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4194 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4195 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004196# endif
4197# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004198 if (xpc.xp_context == EXPAND_SIGN)
4199 {
4200 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4201 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4202 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004203# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004204 if (xpc.xp_context == EXPAND_RUNTIME)
4205 {
4206 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4207 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4208 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004209 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004210
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004211 if (cmdline_fuzzy_completion_supported(&xpc))
4212 // when fuzzy matching, don't modify the search string
4213 pat = vim_strsave(xpc.xp_pattern);
4214 else
4215 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4216
Bram Moolenaar93a10962022-06-16 11:42:09 +01004217 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004218 {
4219 int i;
4220
4221 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4222
4223 for (i = 0; i < xpc.xp_numfiles; i++)
4224 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4225 }
4226 vim_free(pat);
4227 ExpandCleanup(&xpc);
4228}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004229#endif // FEAT_EVAL