blob: 4615aafac9ffdf39477871ecf6d93a4b68368218 [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 ?
Girish Palya6b49fba2025-06-28 19:47:34 +020017static int may_expand_pattern = FALSE;
18static pos_T pre_incsearch_pos; // Cursor position when incsearch started
Bram Moolenaar66b51422019-08-18 21:44:12 +020019
zeertzjq7a5115c2025-03-19 20:29:58 +010020static void set_context_for_wildcard_arg(exarg_T *eap, char_u *arg, int usefilter, expand_T *xp, int *complp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000022static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020025#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000026static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000027static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020028#endif
Girish Palya6b49fba2025-06-28 19:47:34 +020029static int expand_pattern_in_buf(char_u *pat, int dir, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020030
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000031// "compl_match_array" points the currently displayed list of entries in the
32// popup menu. It is NULL when there is no popup menu.
33static pumitem_T *compl_match_array = NULL;
34static int compl_match_arraysize;
35// First column in cmdline of the matched item for completion.
36static int compl_startcol;
37static int compl_selected;
Girish Palya92f68e22025-04-21 11:12:41 +020038// cmdline before expansion
39static char_u *cmdline_orig = NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000040
zeertzjqc51a3762022-12-10 10:22:29 +000041#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000042
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000043/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000044 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
45 * context.
46 */
47 static int
48cmdline_fuzzy_completion_supported(expand_T *xp)
49{
50 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
51 && xp->xp_context != EXPAND_BOOL_SETTINGS
52 && xp->xp_context != EXPAND_COLORS
53 && xp->xp_context != EXPAND_COMPILER
54 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020055 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_FILES
57 && xp->xp_context != EXPAND_FILES_IN_PATH
58 && xp->xp_context != EXPAND_FILETYPE
Christian Brabandta3422aa2025-04-23 21:04:24 +020059 && xp->xp_context != EXPAND_FILETYPECMD
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010060 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000061 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010062 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020064 && xp->xp_context != EXPAND_STRING_SETTING
65 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000066 && xp->xp_context != EXPAND_OWNSYNTAX
67 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000068 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000069 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020070 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000071 && xp->xp_context != EXPAND_TAGS
72 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000073 && xp->xp_context != EXPAND_USER_LIST);
74}
75
76/*
77 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000078 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
79 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000080 */
81 int
82cmdline_fuzzy_complete(char_u *fuzzystr)
83{
84 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
85}
86
87/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000088 * sort function for the completion matches.
89 * <SNR> functions should be sorted to the end.
90 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020091 static int
92sort_func_compare(const void *s1, const void *s2)
93{
94 char_u *p1 = *(char_u **)s1;
95 char_u *p2 = *(char_u **)s2;
96
97 if (*p1 != '<' && *p2 == '<') return -1;
98 if (*p1 == '<' && *p2 != '<') return 1;
99 return STRCMP(p1, p2);
100}
Bram Moolenaar66b51422019-08-18 21:44:12 +0200101
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +0000102/*
103 * Escape special characters in the cmdline completion matches.
104 */
Bram Moolenaar66b51422019-08-18 21:44:12 +0200105 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000106wildescape(
107 expand_T *xp,
108 char_u *str,
109 int numfiles,
110 char_u **files)
111{
112 char_u *p;
113 int vse_what = xp->xp_context == EXPAND_BUFFERS
114 ? VSE_BUFFER : VSE_NONE;
115
116 if (xp->xp_context == EXPAND_FILES
117 || xp->xp_context == EXPAND_FILES_IN_PATH
118 || xp->xp_context == EXPAND_SHELLCMD
119 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200120 || xp->xp_context == EXPAND_DIRECTORIES
121 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000122 {
123 // Insert a backslash into a file name before a space, \, %, #
124 // and wildmatch characters, except '~'.
125 for (int i = 0; i < numfiles; ++i)
126 {
127 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200128 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000129 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200130 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
131 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000132 if (p != NULL)
133 {
134 vim_free(files[i]);
135 files[i] = p;
136#if defined(BACKSLASH_IN_FILENAME)
137 p = vim_strsave_escaped(files[i], (char_u *)" ");
138 if (p != NULL)
139 {
140 vim_free(files[i]);
141 files[i] = p;
142 }
143#endif
144 }
145 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200146 else if (xp->xp_backslash & XP_BS_COMMA)
147 {
148 if (vim_strchr(files[i], ',') != NULL)
149 {
150 p = vim_strsave_escaped(files[i], (char_u *)",");
151 if (p != NULL)
152 {
153 vim_free(files[i]);
154 files[i] = p;
155 }
156 }
157 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000158#ifdef BACKSLASH_IN_FILENAME
159 p = vim_strsave_fnameescape(files[i], vse_what);
160#else
161 p = vim_strsave_fnameescape(files[i],
162 xp->xp_shell ? VSE_SHELL : vse_what);
163#endif
164 if (p != NULL)
165 {
166 vim_free(files[i]);
167 files[i] = p;
168 }
169
170 // If 'str' starts with "\~", replace "~" at start of
171 // files[i] with "\~".
172 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
173 escape_fname(&files[i]);
174 }
175 xp->xp_backslash = XP_BS_NONE;
176
177 // If the first file starts with a '+' escape it. Otherwise it
178 // could be seen as "+cmd".
179 if (*files[0] == '+')
180 escape_fname(&files[0]);
181 }
182 else if (xp->xp_context == EXPAND_TAGS)
183 {
184 // Insert a backslash before characters in a tag name that
185 // would terminate the ":tag" command.
186 for (int i = 0; i < numfiles; ++i)
187 {
188 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
189 if (p != NULL)
190 {
191 vim_free(files[i]);
192 files[i] = p;
193 }
194 }
195 }
196}
197
198/*
199 * Escape special characters in the cmdline completion matches.
200 */
201 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200202ExpandEscape(
203 expand_T *xp,
204 char_u *str,
205 int numfiles,
206 char_u **files,
207 int options)
208{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200209 // May change home directory back to "~"
210 if (options & WILD_HOME_REPLACE)
211 tilde_replace(str, numfiles, files);
212
213 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000214 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200215}
216
217/*
218 * Return FAIL if this is not an appropriate context in which to do
219 * completion of anything, return OK if it is (even if there are no matches).
220 * For the caller, this means that the character is just passed through like a
221 * normal character (instead of being expanded). This allows :s/^I^D etc.
222 */
223 int
224nextwild(
225 expand_T *xp,
226 int type,
227 int options, // extra options for ExpandOne()
228 int escape) // if TRUE, escape the returned matches
229{
230 cmdline_info_T *ccline = get_cmdline_info();
231 int i, j;
232 char_u *p1;
233 char_u *p2;
234 int difflen;
235 int v;
236
237 if (xp->xp_numfiles == -1)
238 {
Girish Palya6b49fba2025-06-28 19:47:34 +0200239 may_expand_pattern = options & WILD_MAY_EXPAND_PATTERN;
240 pre_incsearch_pos = xp->xp_pre_incsearch_pos;
Jim Zhou3255af82025-02-27 19:29:50 +0100241#ifdef FEAT_EVAL
zeertzjq7a5115c2025-03-19 20:29:58 +0100242 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
Jim Zhou3255af82025-02-27 19:29:50 +0100243 {
244 // Expand commands typed in input() function
245 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
zeertzjq7a5115c2025-03-19 20:29:58 +0100246 }
247 else
Jim Zhou3255af82025-02-27 19:29:50 +0100248#endif
zeertzjq7a5115c2025-03-19 20:29:58 +0100249 {
Jim Zhou3255af82025-02-27 19:29:50 +0100250 set_expand_context(xp);
zeertzjq7a5115c2025-03-19 20:29:58 +0100251 }
252 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200253 }
254
255 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
256 {
257 beep_flush();
258 return OK; // Something illegal on command line
259 }
260 if (xp->xp_context == EXPAND_NOTHING)
261 {
262 // Caller can use the character as a normal char instead
263 return FAIL;
264 }
265
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000266 // If cmd_silent is set then don't show the dots, because redrawcmd() below
267 // won't remove them.
268 if (!cmd_silent)
269 {
270 msg_puts("..."); // show that we are busy
271 out_flush();
272 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200273
274 i = (int)(xp->xp_pattern - ccline->cmdbuff);
275 xp->xp_pattern_len = ccline->cmdpos - i;
276
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000277 if (type == WILD_NEXT || type == WILD_PREV
278 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200279 {
280 // Get next/previous match for a previous expanded pattern.
281 p2 = ExpandOne(xp, NULL, NULL, 0, type);
282 }
283 else
284 {
Girish Palya6b49fba2025-06-28 19:47:34 +0200285 if (cmdline_fuzzy_completion_supported(xp)
286 || xp->xp_context == EXPAND_PATTERN_IN_BUF)
287 // Don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200288 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000289 else
290 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
291
Bram Moolenaar66b51422019-08-18 21:44:12 +0200292 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000293 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200294 p2 = NULL;
295 else
296 {
297 int use_options = options |
298 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100299 if (use_options & WILD_KEEP_SOLE_ITEM)
300 use_options &= ~WILD_KEEP_SOLE_ITEM;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200301 if (escape)
302 use_options |= WILD_ESCAPE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200303 if (p_wic)
304 use_options += WILD_ICASE;
Girish Palya6b49fba2025-06-28 19:47:34 +0200305
Bram Moolenaar66b51422019-08-18 21:44:12 +0200306 p2 = ExpandOne(xp, p1,
307 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
308 use_options, type);
309 vim_free(p1);
310 // longest match: make sure it is not shorter, happens with :help
311 if (p2 != NULL && type == WILD_LONGEST)
312 {
313 for (j = 0; j < xp->xp_pattern_len; ++j)
314 if (ccline->cmdbuff[i + j] == '*'
315 || ccline->cmdbuff[i + j] == '?')
316 break;
317 if ((int)STRLEN(p2) < j)
318 VIM_CLEAR(p2);
319 }
320 }
321 }
322
323 if (p2 != NULL && !got_int)
324 {
325 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
326 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
327 {
328 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
329 xp->xp_pattern = ccline->cmdbuff + i;
330 }
331 else
332 v = OK;
333 if (v == OK)
334 {
335 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
336 &ccline->cmdbuff[ccline->cmdpos],
337 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
338 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
339 ccline->cmdlen += difflen;
340 ccline->cmdpos += difflen;
341 }
342 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200343
344 redrawcmd();
345 cursorcmd();
346
347 // When expanding a ":map" command and no matches are found, assume that
348 // the key is supposed to be inserted literally
349 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
350 return FAIL;
351
352 if (xp->xp_numfiles <= 0 && p2 == NULL)
353 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100354 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200355 // free expanded pattern
356 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
357
John Marriott1be5b372025-06-23 19:57:29 +0200358 vim_free(p2);
359
Bram Moolenaar66b51422019-08-18 21:44:12 +0200360 return OK;
361}
362
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000363/*
364 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000365 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000366 */
367 static int
368cmdline_pum_create(
369 cmdline_info_T *ccline,
370 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000371 char_u **matches,
372 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000373 int showtail)
374{
375 int i;
376 int columns;
377
378 // Add all the completion matches
John Marriott1be5b372025-06-23 19:57:29 +0200379 compl_match_array = ALLOC_MULT(pumitem_T, numMatches);
380 if (compl_match_array == NULL)
381 return EXPAND_UNSUCCESSFUL;
382
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000383 compl_match_arraysize = numMatches;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000384 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000385 {
zeertzjqc51a3762022-12-10 10:22:29 +0000386 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000387 compl_match_array[i].pum_info = NULL;
388 compl_match_array[i].pum_extra = NULL;
389 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200390 compl_match_array[i].pum_user_abbr_hlattr = -1;
391 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000392 }
393
394 // Compute the popup menu starting column
Christian Brabandt362be6b2025-04-22 20:06:53 +0200395 compl_startcol = ccline == NULL ? 0 : vim_strsize(ccline->cmdbuff) + 1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000396 columns = vim_strsize(xp->xp_pattern);
397 if (showtail)
398 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000399 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000400 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000401 }
glepnir977561a2025-02-13 20:48:56 +0100402 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000403
404 // no default selection
405 compl_selected = -1;
406
407 cmdline_pum_display();
408
409 return EXPAND_OK;
410}
411
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000412/*
413 * Display the cmdline completion matches in a popup menu
414 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000415 void
416cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000417{
418 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
419}
420
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000421/*
422 * Returns TRUE if the cmdline completion popup menu is being displayed.
423 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000424 int
425cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100427 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000428}
429
430/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000431 * Remove the cmdline completion popup menu (if present), free the list of
432 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000433 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000434 void
zeertzjq1830e782025-03-13 20:29:13 +0100435cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000436{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000437 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100438 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100439#ifdef FEAT_EVAL
440 int save_RedrawingDisabled = RedrawingDisabled;
441 if (cclp->input_fn)
442 RedrawingDisabled = 0;
443#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000444
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000445 pum_undisplay();
446 VIM_CLEAR(compl_match_array);
Girish Palya92f68e22025-04-21 11:12:41 +0200447 compl_match_arraysize = 0;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000448 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000449 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000450 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000451 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100452
453 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
454 // as a side effect.
455 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100456#ifdef FEAT_EVAL
457 if (cclp->input_fn)
458 RedrawingDisabled = save_RedrawingDisabled;
459#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000460}
461
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000462 void
463cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000464{
zeertzjq1830e782025-03-13 20:29:13 +0100465 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000466 wildmenu_cleanup(cclp);
467}
468
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000469/*
470 * Returns the starting column number to use for the cmdline completion popup
471 * menu.
472 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000473 int
474cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000475{
476 return compl_startcol;
477}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000478
Bram Moolenaar66b51422019-08-18 21:44:12 +0200479/*
zeertzjqd8c93402024-06-17 18:25:32 +0200480 * Returns the current cmdline completion pattern.
481 */
482 char_u *
483cmdline_compl_pattern(void)
484{
485 expand_T *xp = get_cmdline_info()->xpc;
486
487 return xp == NULL ? NULL : xp->xp_orig;
488}
489
490/*
491 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
492 */
493 int
494cmdline_compl_is_fuzzy(void)
495{
496 expand_T *xp = get_cmdline_info()->xpc;
497
498 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
499}
500
501/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000502 * Return the number of characters that should be skipped in a status match.
Girish Palya6b49fba2025-06-28 19:47:34 +0200503 * These are backslashes used for escaping. Do show backslashes in help tags
504 * and in search pattern completion matches.
Bram Moolenaard6e91382022-11-12 17:44:13 +0000505 */
506 static int
507skip_status_match_char(expand_T *xp, char_u *s)
508{
Girish Palya6b49fba2025-06-28 19:47:34 +0200509 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP
510 && xp->xp_context != EXPAND_PATTERN_IN_BUF)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000511#ifdef FEAT_MENU
512 || ((xp->xp_context == EXPAND_MENUS
513 || xp->xp_context == EXPAND_MENUNAMES)
514 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
515#endif
516 )
517 {
518#ifndef BACKSLASH_IN_FILENAME
519 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
520 return 2;
521#endif
522 return 1;
523 }
524 return 0;
525}
526
527/*
528 * Get the length of an item as it will be shown in the status line.
529 */
530 static int
531status_match_len(expand_T *xp, char_u *s)
532{
533 int len = 0;
534
535#ifdef FEAT_MENU
536 int emenu = xp->xp_context == EXPAND_MENUS
537 || xp->xp_context == EXPAND_MENUNAMES;
538
539 // Check for menu separators - replace with '|'.
540 if (emenu && menu_is_separator(s))
541 return 1;
542#endif
543
544 while (*s != NUL)
545 {
546 s += skip_status_match_char(xp, s);
547 len += ptr2cells(s);
548 MB_PTR_ADV(s);
549 }
550
551 return len;
552}
553
554/*
555 * Show wildchar matches in the status line.
556 * Show at least the "match" item.
557 * We start at item 'first_match' in the list and show all matches that fit.
558 *
559 * If inversion is possible we use it. Else '=' characters are used.
560 */
561 static void
562win_redr_status_matches(
563 expand_T *xp,
564 int num_matches,
565 char_u **matches, // list of matches
566 int match,
567 int showtail)
568{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000569 int row;
570 char_u *buf;
571 int len;
572 int clen; // length in screen cells
573 int fillchar;
574 int attr;
575 int i;
576 int highlight = TRUE;
577 char_u *selstart = NULL;
578 int selstart_col = 0;
579 char_u *selend = NULL;
580 static int first_match = 0;
581 int add_left = FALSE;
582 char_u *s;
583#ifdef FEAT_MENU
584 int emenu;
585#endif
586 int l;
587
588 if (matches == NULL) // interrupted completion?
589 return;
590
591 if (has_mbyte)
592 buf = alloc(Columns * MB_MAXBYTES + 1);
593 else
594 buf = alloc(Columns + 1);
595 if (buf == NULL)
596 return;
597
598 if (match == -1) // don't show match but original text
599 {
600 match = 0;
601 highlight = FALSE;
602 }
603 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000604 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000605 if (match == 0)
606 first_match = 0;
607 else if (match < first_match)
608 {
609 // jumping left, as far as we can go
610 first_match = match;
611 add_left = TRUE;
612 }
613 else
614 {
615 // check if match fits on the screen
616 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000617 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000618 if (first_match > 0)
619 clen += 2;
620 // jumping right, put match at the left
621 if ((long)clen > Columns)
622 {
623 first_match = match;
624 // if showing the last match, we can add some on the left
625 clen = 2;
626 for (i = match; i < num_matches; ++i)
627 {
zeertzjqc51a3762022-12-10 10:22:29 +0000628 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000629 if ((long)clen >= Columns)
630 break;
631 }
632 if (i == num_matches)
633 add_left = TRUE;
634 }
635 }
636 if (add_left)
637 while (first_match > 0)
638 {
zeertzjqc51a3762022-12-10 10:22:29 +0000639 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000640 if ((long)clen >= Columns)
641 break;
642 --first_match;
643 }
644
645 fillchar = fillchar_status(&attr, curwin);
646
647 if (first_match == 0)
648 {
649 *buf = NUL;
650 len = 0;
651 }
652 else
653 {
654 STRCPY(buf, "< ");
655 len = 2;
656 }
657 clen = len;
658
659 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000660 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000661 {
662 if (i == match)
663 {
664 selstart = buf + len;
665 selstart_col = clen;
666 }
667
zeertzjqc51a3762022-12-10 10:22:29 +0000668 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000669 // Check for menu separators - replace with '|'
670#ifdef FEAT_MENU
671 emenu = (xp->xp_context == EXPAND_MENUS
672 || xp->xp_context == EXPAND_MENUNAMES);
673 if (emenu && menu_is_separator(s))
674 {
675 STRCPY(buf + len, transchar('|'));
676 l = (int)STRLEN(buf + len);
677 len += l;
678 clen += l;
679 }
680 else
681#endif
682 for ( ; *s != NUL; ++s)
683 {
684 s += skip_status_match_char(xp, s);
685 clen += ptr2cells(s);
686 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
687 {
688 STRNCPY(buf + len, s, l);
689 s += l - 1;
690 len += l;
691 }
692 else
693 {
694 STRCPY(buf + len, transchar_byte(*s));
695 len += (int)STRLEN(buf + len);
696 }
697 }
698 if (i == match)
699 selend = buf + len;
700
701 *(buf + len++) = ' ';
702 *(buf + len++) = ' ';
703 clen += 2;
704 if (++i == num_matches)
705 break;
706 }
707
708 if (i != num_matches)
709 {
710 *(buf + len++) = '>';
711 ++clen;
712 }
713
714 buf[len] = NUL;
715
716 row = cmdline_row - 1;
717 if (row >= 0)
718 {
719 if (wild_menu_showing == 0)
720 {
721 if (msg_scrolled > 0)
722 {
723 // Put the wildmenu just above the command line. If there is
724 // no room, scroll the screen one line up.
725 if (cmdline_row == Rows - 1)
726 {
727 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
728 ++msg_scrolled;
729 }
730 else
731 {
732 ++cmdline_row;
733 ++row;
734 }
735 wild_menu_showing = WM_SCROLLED;
736 }
737 else
738 {
739 // Create status line if needed by setting 'laststatus' to 2.
740 // Set 'winminheight' to zero to avoid that the window is
741 // resized.
742 if (lastwin->w_status_height == 0)
743 {
744 save_p_ls = p_ls;
745 save_p_wmh = p_wmh;
746 p_ls = 2;
747 p_wmh = 0;
748 last_status(FALSE);
749 }
750 wild_menu_showing = WM_SHOWN;
751 }
752 }
753
754 screen_puts(buf, row, 0, attr);
755 if (selstart != NULL && highlight)
756 {
757 *selend = NUL;
758 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
759 }
760
761 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
762 }
763
764 win_redraw_last_status(topframe);
765 vim_free(buf);
766}
767
768/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000769 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200770 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000771 */
772 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100773get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000774{
glepnir977561a2025-02-13 20:48:56 +0100775 int findex = xp->xp_selected;
776 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000777
zeertzjqb6c900b2025-02-14 17:59:31 +0100778 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000779 if (xp->xp_numfiles <= 0)
780 return NULL;
781
782 if (mode == WILD_PREV)
783 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100784 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000785 if (findex == -1)
786 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100787 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000788 --findex;
789 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000790 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000791 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100792 // Select the next entry
793 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000794 }
glepnir977561a2025-02-13 20:48:56 +0100795 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000796 {
glepnir977561a2025-02-13 20:48:56 +0100797 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
798 ht = pum_get_height();
799 if (ht > 3)
800 ht -= 2;
801
802 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000803 {
glepnir977561a2025-02-13 20:48:56 +0100804 if (findex == 0)
805 // at the first entry, don't select any entries
806 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100807 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100808 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000809 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100810 else
811 // go up by the pum height
812 findex = MAX(findex - ht, 0);
813 }
814 else // mode == WILD_PAGEDOWN
815 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100816 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100817 // at the last entry, don't select any entries
818 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100819 else if (findex < 0)
820 // no entry is selected, select the first entry
821 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100822 else
823 // go down by the pum height
824 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000825 }
826 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000827
glepnir977561a2025-02-13 20:48:56 +0100828 // Handle wrapping around
829 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000830 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100831 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100832 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000833 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000834 else
glepnir977561a2025-02-13 20:48:56 +0100835 // Wrap around to opposite end
836 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000837 }
glepnir977561a2025-02-13 20:48:56 +0100838
839 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000840 if (compl_match_array)
841 {
842 compl_selected = findex;
843 cmdline_pum_display();
844 }
845 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100846 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
847 cmd_showtail);
848
zeertzjqe9ef3472023-08-17 23:57:05 +0200849 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100850 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100851 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000852}
853
854/*
855 * Start the command-line expansion and get the matches.
856 */
857 static char_u *
858ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
859{
860 int non_suf_match; // number without matching suffix
861 int i;
862 char_u *ss = NULL;
863
864 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000865 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000866 options) == FAIL)
867 {
868#ifdef FNAME_ILLEGAL
869 // Illegal file name has been silently skipped. But when there
870 // are wildcards, the real problem is that there was no match,
871 // causing the pattern to be added, which has illegal characters.
872 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
873 semsg(_(e_no_match_str_2), str);
874#endif
875 }
876 else if (xp->xp_numfiles == 0)
877 {
878 if (!(options & WILD_SILENT))
879 semsg(_(e_no_match_str_2), str);
880 }
881 else
882 {
883 // Escape the matches for use on the command line.
884 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
885
886 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000887 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000888 {
889 if (xp->xp_numfiles)
890 non_suf_match = xp->xp_numfiles;
891 else
892 non_suf_match = 1;
893 if ((xp->xp_context == EXPAND_FILES
894 || xp->xp_context == EXPAND_DIRECTORIES)
895 && xp->xp_numfiles > 1)
896 {
897 // More than one match; check suffix.
898 // The files will have been sorted on matching suffix in
899 // expand_wildcards, only need to check the first two.
900 non_suf_match = 0;
901 for (i = 0; i < 2; ++i)
902 if (match_suffix(xp->xp_files[i]))
903 ++non_suf_match;
904 }
905 if (non_suf_match != 1)
906 {
907 // Can we ever get here unless it's while expanding
908 // interactively? If not, we can get rid of this all
909 // together. Don't really want to wait for this message
910 // (and possibly have to hit return to continue!).
911 if (!(options & WILD_SILENT))
912 emsg(_(e_too_many_file_names));
913 else if (!(options & WILD_NO_BEEP))
914 beep_flush();
915 }
916 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
917 ss = vim_strsave(xp->xp_files[0]);
918 }
919 }
920
921 return ss;
922}
923
924/*
925 * Return the longest common part in the list of cmdline completion matches.
926 */
927 static char_u *
928find_longest_match(expand_T *xp, int options)
929{
930 long_u len;
931 int mb_len = 1;
932 int c0, ci;
933 int i;
934 char_u *ss;
935
936 for (len = 0; xp->xp_files[0][len]; len += mb_len)
937 {
938 if (has_mbyte)
939 {
940 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
941 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
942 }
943 else
944 c0 = xp->xp_files[0][len];
945 for (i = 1; i < xp->xp_numfiles; ++i)
946 {
947 if (has_mbyte)
948 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
949 else
950 ci = xp->xp_files[i][len];
951 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
952 || xp->xp_context == EXPAND_FILES
953 || xp->xp_context == EXPAND_SHELLCMD
954 || xp->xp_context == EXPAND_BUFFERS))
955 {
956 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
957 break;
958 }
959 else if (c0 != ci)
960 break;
961 }
962 if (i < xp->xp_numfiles)
963 {
964 if (!(options & WILD_NO_BEEP))
965 vim_beep(BO_WILD);
966 break;
967 }
968 }
969
970 ss = alloc(len + 1);
971 if (ss)
972 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
973
974 return ss;
975}
976
977/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000978 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200979 * Chars that should not be expanded must be preceded with a backslash.
980 * Return a pointer to allocated memory containing the new string.
981 * Return NULL for failure.
982 *
983 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200984 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
985 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986 *
987 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
988 * is WILD_EXPAND_FREE or WILD_ALL.
989 *
990 * mode = WILD_FREE: just free previously expanded matches
991 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
992 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
993 * mode = WILD_NEXT: use next match in multiple match, wrap to first
994 * mode = WILD_PREV: use previous match in multiple match, wrap to first
995 * mode = WILD_ALL: return all matches concatenated
996 * mode = WILD_LONGEST: return longest matched part
997 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000998 * mode = WILD_APPLY: apply the item selected in the cmdline completion
999 * popup menu and close the menu.
1000 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
1001 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +02001002 *
1003 * options = WILD_LIST_NOTFOUND: list entries without a match
1004 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
1005 * options = WILD_USE_NL: Use '\n' for WILD_ALL
1006 * options = WILD_NO_BEEP: Don't beep for multiple matches
1007 * options = WILD_ADD_SLASH: add a slash after directory names
1008 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
1009 * options = WILD_SILENT: don't print warning messages
1010 * options = WILD_ESCAPE: put backslash before special chars
1011 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +02001012 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +02001013 *
1014 * The variables xp->xp_context and xp->xp_backslash must have been set!
1015 */
1016 char_u *
1017ExpandOne(
1018 expand_T *xp,
1019 char_u *str,
1020 char_u *orig, // allocated copy of original of expanded string
1021 int options,
1022 int mode)
1023{
1024 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001025 int orig_saved = FALSE;
1026 int i;
1027 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001028
1029 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001030 if (mode == WILD_NEXT || mode == WILD_PREV
1031 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001032 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001034 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001035 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001036 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001037 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001038 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001039 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001040
Bram Moolenaar66b51422019-08-18 21:44:12 +02001041 // free old names
1042 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1043 {
1044 FreeWild(xp->xp_numfiles, xp->xp_files);
1045 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001046 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001047
1048 // The entries from xp_files may be used in the PUM, remove it.
1049 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001050 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001051 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001052 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001053
1054 if (mode == WILD_FREE) // only release file name
1055 return NULL;
1056
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001057 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001058 {
zeertzjq28a23602023-09-29 19:58:35 +02001059 vim_free(xp->xp_orig);
1060 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001061 orig_saved = TRUE;
1062
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001063 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001064 }
1065
1066 // Find longest common part
1067 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1068 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001069 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001070 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001071 }
1072
Bram Moolenaar57e95172022-08-20 19:26:14 +01001073 // Concatenate all matching names. Unless interrupted, this can be slow
1074 // and the result probably won't be used.
1075 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076 {
1077 len = 0;
1078 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001079 {
1080 if (i > 0)
1081 {
1082 if (xp->xp_prefix == XP_PREFIX_NO)
1083 len += 2; // prefix "no"
1084 else if (xp->xp_prefix == XP_PREFIX_INV)
1085 len += 3; // prefix "inv"
1086 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001087 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001088 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001089 ss = alloc(len);
1090 if (ss != NULL)
1091 {
1092 *ss = NUL;
1093 for (i = 0; i < xp->xp_numfiles; ++i)
1094 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001095 if (i > 0)
1096 {
1097 if (xp->xp_prefix == XP_PREFIX_NO)
1098 STRCAT(ss, "no");
1099 else if (xp->xp_prefix == XP_PREFIX_INV)
1100 STRCAT(ss, "inv");
1101 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001102 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001103
Bram Moolenaar66b51422019-08-18 21:44:12 +02001104 if (i != xp->xp_numfiles - 1)
1105 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1106 }
1107 }
1108 }
1109
1110 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1111 ExpandCleanup(xp);
1112
zeertzjq28a23602023-09-29 19:58:35 +02001113 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001114 if (!orig_saved)
1115 vim_free(orig);
1116
1117 return ss;
1118}
1119
1120/*
1121 * Prepare an expand structure for use.
1122 */
1123 void
1124ExpandInit(expand_T *xp)
1125{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001126 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001127 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001128 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001129 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001130}
1131
1132/*
1133 * Cleanup an expand structure after use.
1134 */
1135 void
1136ExpandCleanup(expand_T *xp)
1137{
1138 if (xp->xp_numfiles >= 0)
1139 {
1140 FreeWild(xp->xp_numfiles, xp->xp_files);
1141 xp->xp_numfiles = -1;
1142 }
zeertzjq28a23602023-09-29 19:58:35 +02001143 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001144}
1145
zeertzjqec270a52025-04-23 20:50:23 +02001146 void
1147clear_cmdline_orig(void)
1148{
1149 VIM_CLEAR(cmdline_orig);
1150}
1151
Bram Moolenaar66b51422019-08-18 21:44:12 +02001152/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001153 * Display one line of completion matches. Multiple matches are displayed in
1154 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001155 * matches - list of completion match names
1156 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001157 * lines - number of output lines
1158 * linenr - line number of matches to display
1159 * maxlen - maximum number of characters in each line
1160 * showtail - display only the tail of the full path of a file name
1161 * dir_attr - highlight attribute to use for directory names
1162 */
1163 static void
1164showmatches_oneline(
1165 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001166 char_u **matches,
1167 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001168 int lines,
1169 int linenr,
1170 int maxlen,
1171 int showtail,
1172 int dir_attr)
1173{
1174 int i, j;
1175 int isdir;
1176 int lastlen;
1177 char_u *p;
1178
1179 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001180 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001181 {
1182 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1183 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001184 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1185 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001186 msg_advance(maxlen + 1);
1187 msg_puts((char *)p);
1188 msg_advance(maxlen + 3);
1189 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1190 break;
1191 }
1192 for (i = maxlen - lastlen; --i >= 0; )
1193 msg_putchar(' ');
1194 if (xp->xp_context == EXPAND_FILES
1195 || xp->xp_context == EXPAND_SHELLCMD
1196 || xp->xp_context == EXPAND_BUFFERS)
1197 {
1198 // highlight directories
1199 if (xp->xp_numfiles != -1)
1200 {
1201 char_u *halved_slash;
1202 char_u *exp_path;
1203 char_u *path;
1204
1205 // Expansion was done before and special characters
1206 // were escaped, need to halve backslashes. Also
1207 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001208 exp_path = expand_env_save_opt(matches[j], TRUE);
1209 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001210 halved_slash = backslash_halve_save(path);
1211 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001212 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001213 vim_free(exp_path);
1214 if (halved_slash != path)
1215 vim_free(halved_slash);
1216 }
1217 else
1218 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001219 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001220 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001221 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001222 else
1223 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001224 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001225 TRUE);
1226 p = NameBuff;
1227 }
1228 }
1229 else
1230 {
1231 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001232 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001233 }
1234 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1235 }
1236 if (msg_col > 0) // when not wrapped around
1237 {
1238 msg_clr_eos();
1239 msg_putchar('\n');
1240 }
1241 out_flush(); // show one line at a time
1242}
1243
1244/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001245 * Show all matches for completion on the command line.
1246 * Returns EXPAND_NOTHING when the character that triggered expansion should
1247 * be inserted like a normal character.
1248 */
1249 int
1250showmatches(expand_T *xp, int wildmenu UNUSED)
1251{
1252 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001253 int numMatches;
1254 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001255 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001256 int maxlen;
1257 int lines;
1258 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001259 int attr;
1260 int showtail;
1261
Girish Palya92f68e22025-04-21 11:12:41 +02001262 // Save cmdline before expansion
1263 if (ccline->cmdbuff != NULL)
Girish Palya5c3d1e32025-04-22 19:52:16 +02001264 {
1265 vim_free(cmdline_orig);
Girish Palya92f68e22025-04-21 11:12:41 +02001266 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
Girish Palya5c3d1e32025-04-22 19:52:16 +02001267 }
Girish Palya92f68e22025-04-21 11:12:41 +02001268
Bram Moolenaar66b51422019-08-18 21:44:12 +02001269 if (xp->xp_numfiles == -1)
1270 {
1271 set_expand_context(xp);
1272 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001273 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001274 showtail = expand_showtail(xp);
1275 if (i != EXPAND_OK)
1276 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001277 }
1278 else
1279 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001280 numMatches = xp->xp_numfiles;
1281 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001282 showtail = cmd_showtail;
1283 }
1284
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001285 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001286 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001287 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001288
Bram Moolenaar66b51422019-08-18 21:44:12 +02001289 if (!wildmenu)
1290 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001291 msg_didany = FALSE; // lines_left will be set
1292 msg_start(); // prepare for paging
1293 msg_putchar('\n');
1294 out_flush();
1295 cmdline_row = msg_row;
1296 msg_didany = FALSE; // lines_left will be set again
1297 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001298 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001299
1300 if (got_int)
1301 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001302 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001303 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001304 else
1305 {
1306 // find the length of the longest file name
1307 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001308 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001309 {
1310 if (!showtail && (xp->xp_context == EXPAND_FILES
1311 || xp->xp_context == EXPAND_SHELLCMD
1312 || xp->xp_context == EXPAND_BUFFERS))
1313 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001314 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001315 j = vim_strsize(NameBuff);
1316 }
1317 else
zeertzjqc51a3762022-12-10 10:22:29 +00001318 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001319 if (j > maxlen)
1320 maxlen = j;
1321 }
1322
1323 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001324 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001325 else
1326 {
1327 // compute the number of columns and lines for the listing
1328 maxlen += 2; // two spaces between file names
1329 columns = ((int)Columns + 2) / maxlen;
1330 if (columns < 1)
1331 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001332 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001333 }
1334
1335 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1336
1337 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1338 {
1339 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1340 msg_clr_eos();
1341 msg_advance(maxlen - 3);
1342 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1343 }
1344
1345 // list the files line by line
1346 for (i = 0; i < lines; ++i)
1347 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001348 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001349 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001350 if (got_int)
1351 {
1352 got_int = FALSE;
1353 break;
1354 }
1355 }
1356
1357 // we redraw the command below the lines that we have just listed
1358 // This is a bit tricky, but it saves a lot of screen updating.
1359 cmdline_row = msg_row; // will put it back later
1360 }
1361
1362 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001363 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001364
1365 return EXPAND_OK;
1366}
1367
1368/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001369 * gettail() version for showmatches() and win_redr_status_matches():
1370 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001371 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001372 static char_u *
1373showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001374{
1375 char_u *p;
1376 char_u *t = s;
1377 int had_sep = FALSE;
1378
1379 for (p = s; *p != NUL; )
1380 {
1381 if (vim_ispathsep(*p)
1382#ifdef BACKSLASH_IN_FILENAME
1383 && !rem_backslash(p)
1384#endif
1385 )
1386 had_sep = TRUE;
1387 else if (had_sep)
1388 {
1389 t = p;
1390 had_sep = FALSE;
1391 }
1392 MB_PTR_ADV(p);
1393 }
1394 return t;
1395}
1396
1397/*
1398 * Return TRUE if we only need to show the tail of completion matches.
1399 * When not completing file names or there is a wildcard in the path FALSE is
1400 * returned.
1401 */
1402 static int
1403expand_showtail(expand_T *xp)
1404{
1405 char_u *s;
1406 char_u *end;
1407
1408 // When not completing file names a "/" may mean something different.
1409 if (xp->xp_context != EXPAND_FILES
1410 && xp->xp_context != EXPAND_SHELLCMD
1411 && xp->xp_context != EXPAND_DIRECTORIES)
1412 return FALSE;
1413
1414 end = gettail(xp->xp_pattern);
1415 if (end == xp->xp_pattern) // there is no path separator
1416 return FALSE;
1417
1418 for (s = xp->xp_pattern; s < end; s++)
1419 {
1420 // Skip escaped wildcards. Only when the backslash is not a path
1421 // separator, on DOS the '*' "path\*\file" must not be skipped.
1422 if (rem_backslash(s))
1423 ++s;
1424 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1425 return FALSE;
1426 }
1427 return TRUE;
1428}
1429
1430/*
1431 * Prepare a string for expansion.
1432 * When expanding file names: The string will be used with expand_wildcards().
1433 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1434 * When expanding other names: The string will be used with regcomp(). Copy
1435 * the name into allocated memory and prepend "^".
1436 */
1437 char_u *
1438addstar(
1439 char_u *fname,
1440 int len,
1441 int context) // EXPAND_FILES etc.
1442{
1443 char_u *retval;
1444 int i, j;
1445 int new_len;
1446 char_u *tail;
1447 int ends_in_star;
1448
1449 if (context != EXPAND_FILES
1450 && context != EXPAND_FILES_IN_PATH
1451 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001452 && context != EXPAND_DIRECTORIES
1453 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001454 {
1455 // Matching will be done internally (on something other than files).
1456 // So we convert the file-matching-type wildcards into our kind for
1457 // use with vim_regcomp(). First work out how long it will be:
1458
1459 // For help tags the translation is done in find_help_tags().
1460 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001461 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001462 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001463 || context == EXPAND_COLORS
1464 || context == EXPAND_COMPILER
1465 || context == EXPAND_OWNSYNTAX
1466 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001467 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001468 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001469 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001470 || ((context == EXPAND_TAGS_LISTFILES
1471 || context == EXPAND_TAGS)
1472 && fname[0] == '/'))
1473 retval = vim_strnsave(fname, len);
1474 else
1475 {
1476 new_len = len + 2; // +2 for '^' at start, NUL at end
1477 for (i = 0; i < len; i++)
1478 {
1479 if (fname[i] == '*' || fname[i] == '~')
1480 new_len++; // '*' needs to be replaced by ".*"
1481 // '~' needs to be replaced by "\~"
1482
1483 // Buffer names are like file names. "." should be literal
1484 if (context == EXPAND_BUFFERS && fname[i] == '.')
1485 new_len++; // "." becomes "\."
1486
1487 // Custom expansion takes care of special things, match
1488 // backslashes literally (perhaps also for other types?)
1489 if ((context == EXPAND_USER_DEFINED
1490 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1491 new_len++; // '\' becomes "\\"
1492 }
1493 retval = alloc(new_len);
1494 if (retval != NULL)
1495 {
1496 retval[0] = '^';
1497 j = 1;
1498 for (i = 0; i < len; i++, j++)
1499 {
1500 // Skip backslash. But why? At least keep it for custom
1501 // expansion.
1502 if (context != EXPAND_USER_DEFINED
1503 && context != EXPAND_USER_LIST
1504 && fname[i] == '\\'
1505 && ++i == len)
1506 break;
1507
1508 switch (fname[i])
1509 {
1510 case '*': retval[j++] = '.';
1511 break;
1512 case '~': retval[j++] = '\\';
1513 break;
1514 case '?': retval[j] = '.';
1515 continue;
1516 case '.': if (context == EXPAND_BUFFERS)
1517 retval[j++] = '\\';
1518 break;
1519 case '\\': if (context == EXPAND_USER_DEFINED
1520 || context == EXPAND_USER_LIST)
1521 retval[j++] = '\\';
1522 break;
1523 }
1524 retval[j] = fname[i];
1525 }
1526 retval[j] = NUL;
1527 }
1528 }
1529 }
1530 else
1531 {
1532 retval = alloc(len + 4);
1533 if (retval != NULL)
1534 {
1535 vim_strncpy(retval, fname, len);
1536
1537 // Don't add a star to *, ~, ~user, $var or `cmd`.
1538 // * would become **, which walks the whole tree.
1539 // ~ would be at the start of the file name, but not the tail.
1540 // $ could be anywhere in the tail.
1541 // ` could be anywhere in the file name.
1542 // When the name ends in '$' don't add a star, remove the '$'.
1543 tail = gettail(retval);
1544 ends_in_star = (len > 0 && retval[len - 1] == '*');
1545#ifndef BACKSLASH_IN_FILENAME
1546 for (i = len - 2; i >= 0; --i)
1547 {
1548 if (retval[i] != '\\')
1549 break;
1550 ends_in_star = !ends_in_star;
1551 }
1552#endif
1553 if ((*retval != '~' || tail != retval)
1554 && !ends_in_star
1555 && vim_strchr(tail, '$') == NULL
1556 && vim_strchr(retval, '`') == NULL)
1557 retval[len++] = '*';
1558 else if (len > 0 && retval[len - 1] == '$')
1559 --len;
1560 retval[len] = NUL;
1561 }
1562 }
1563 return retval;
1564}
1565
1566/*
1567 * Must parse the command line so far to work out what context we are in.
1568 * Completion can then be done based on that context.
1569 * This routine sets the variables:
1570 * xp->xp_pattern The start of the pattern to be expanded within
1571 * the command line (ends at the cursor).
1572 * xp->xp_context The type of thing to expand. Will be one of:
1573 *
1574 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1575 * the command line, like an unknown command. Caller
1576 * should beep.
1577 * EXPAND_NOTHING Unrecognised context for completion, use char like
1578 * a normal char, rather than for completion. eg
1579 * :s/^I/
1580 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1581 * it.
1582 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1583 * EXPAND_FILES After command with EX_XFILE set, or after setting
1584 * with P_EXPAND set. eg :e ^I, :w>>^I
1585 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001586 * when we know only directories are of interest.
1587 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001588 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1589 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1590 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1591 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1592 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1593 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1594 * EXPAND_EVENTS Complete event names
1595 * EXPAND_SYNTAX Complete :syntax command arguments
1596 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1597 * EXPAND_AUGROUP Complete autocommand group names
1598 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1599 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1600 * eg :unmap a^I , :cunab x^I
1601 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1602 * eg :call sub^I
1603 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1604 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1605 * names in expressions, eg :while s^I
1606 * EXPAND_ENV_VARS Complete environment variable names
1607 * EXPAND_USER Complete user names
Girish Palya6b49fba2025-06-28 19:47:34 +02001608 * EXPAND_PATTERN_IN_BUF Complete pattern in '/', '?', ':s', ':g', etc.
Bram Moolenaar66b51422019-08-18 21:44:12 +02001609 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001610 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001611set_expand_context(expand_T *xp)
1612{
Girish Palya6b49fba2025-06-28 19:47:34 +02001613 cmdline_info_T *ccline = get_cmdline_info();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001614
Girish Palya6b49fba2025-06-28 19:47:34 +02001615 // Handle search commands: '/' or '?'
1616 if ((ccline->cmdfirstc == '/' || ccline->cmdfirstc == '?')
1617 && may_expand_pattern)
1618 {
1619 xp->xp_context = EXPAND_PATTERN_IN_BUF;
1620 xp->xp_search_dir = (ccline->cmdfirstc == '/') ? FORWARD : BACKWARD;
1621 xp->xp_pattern = ccline->cmdbuff;
1622 xp->xp_pattern_len = ccline->cmdpos;
1623#ifdef FEAT_SEARCH_EXTRA
1624 search_first_line = 0; // Search entire buffer
1625#endif
1626 return;
1627 }
1628
1629 // Only handle ':', '>', or '=' command-lines, or expression input
Bram Moolenaar66b51422019-08-18 21:44:12 +02001630 if (ccline->cmdfirstc != ':'
1631#ifdef FEAT_EVAL
1632 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1633 && !ccline->input_fn
1634#endif
Girish Palya6b49fba2025-06-28 19:47:34 +02001635 )
Bram Moolenaar66b51422019-08-18 21:44:12 +02001636 {
1637 xp->xp_context = EXPAND_NOTHING;
1638 return;
1639 }
Girish Palya6b49fba2025-06-28 19:47:34 +02001640
1641 // Fallback to command-line expansion
Bram Moolenaar66b51422019-08-18 21:44:12 +02001642 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1643}
1644
Bram Moolenaard0190392019-08-23 21:17:35 +02001645/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001646 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1647 * For user defined commands, the completion context is set in 'xp' and the
1648 * completion flags in 'complp'.
1649 *
1650 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001651 */
1652 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001653set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001654{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001655 char_u *p = NULL;
1656 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001657 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001658
1659 // Isolate the command and search for it in the command table.
1660 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001661 // - the 'k' command can directly be followed by any character, but do
1662 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1663 // find matches anywhere in the command name, do this only for command
1664 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001665 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001666 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001667 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001668 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001669 p = cmd + 1;
1670 }
1671 else
1672 {
1673 p = cmd;
1674 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1675 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001676 // A user command may contain digits.
1677 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1678 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001679 while (ASCII_ISALNUM(*p) || *p == '*')
1680 ++p;
1681 // for python 3.x: ":py3*" commands completion
1682 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1683 {
1684 ++p;
1685 while (ASCII_ISALPHA(*p) || *p == '*')
1686 ++p;
1687 }
1688 // check for non-alpha command
1689 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1690 ++p;
1691 len = (int)(p - cmd);
1692
1693 if (len == 0)
1694 {
1695 xp->xp_context = EXPAND_UNSUCCESSFUL;
1696 return NULL;
1697 }
1698
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001699 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001700
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001701 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001702 // Also when doing fuzzy expansion for non-shell commands, support
1703 // alphanumeric characters.
1704 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1705 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001706 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1707 ++p;
1708 }
1709
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001710 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001711 // character, complete the command name.
1712 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1713 return NULL;
1714
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001715 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001716 {
1717 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1718 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001719 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001720 p = cmd + 1;
1721 }
1722 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1723 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001724 eap->cmd = cmd;
1725 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001726 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001727 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001728 }
1729 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001730 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001731 {
1732 // Not still touching the command and it was an illegal one
1733 xp->xp_context = EXPAND_UNSUCCESSFUL;
1734 return NULL;
1735 }
1736
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001737 return p;
1738}
Bram Moolenaard0190392019-08-23 21:17:35 +02001739
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001740/*
1741 * Set the completion context for a command argument with wild card characters.
1742 */
1743 static void
1744set_context_for_wildcard_arg(
1745 exarg_T *eap,
1746 char_u *arg,
1747 int usefilter,
1748 expand_T *xp,
1749 int *complp)
1750{
1751 char_u *p;
1752 int c;
1753 int in_quote = FALSE;
1754 char_u *bow = NULL; // Beginning of word
1755 int len = 0;
1756
1757 // Allow spaces within back-quotes to count as part of the argument
1758 // being expanded.
1759 xp->xp_pattern = skipwhite(arg);
1760 p = xp->xp_pattern;
1761 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001762 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001763 if (has_mbyte)
1764 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001765 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001766 c = *p;
1767 if (c == '\\' && p[1] != NUL)
1768 ++p;
1769 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001770 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001771 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001772 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001773 xp->xp_pattern = p;
1774 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001775 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001776 in_quote = !in_quote;
1777 }
1778 // An argument can contain just about everything, except
1779 // characters that end the command and white space.
1780 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001781#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001782 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001783#endif
1784 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001785 {
1786 len = 0; // avoid getting stuck when space is in 'isfname'
1787 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001788 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001789 if (has_mbyte)
1790 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001791 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001792 c = *p;
1793 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001794 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001795 if (has_mbyte)
1796 len = (*mb_ptr2len)(p);
1797 else
1798 len = 1;
1799 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001800 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001801 if (in_quote)
1802 bow = p;
1803 else
1804 xp->xp_pattern = p;
1805 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001806 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001807 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001808 }
1809
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001810 // If we are still inside the quotes, and we passed a space, just
1811 // expand from there.
1812 if (bow != NULL && in_quote)
1813 xp->xp_pattern = bow;
1814 xp->xp_context = EXPAND_FILES;
1815
1816 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001817 if (usefilter
1818 || (eap != NULL
1819 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1820 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001821 {
1822#ifndef BACKSLASH_IN_FILENAME
1823 xp->xp_shell = TRUE;
1824#endif
1825 // When still after the command name expand executables.
1826 if (xp->xp_pattern == skipwhite(arg))
1827 xp->xp_context = EXPAND_SHELLCMD;
1828 }
1829
1830 // Check for environment variable.
1831 if (*xp->xp_pattern == '$')
1832 {
1833 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1834 if (!vim_isIDc(*p))
1835 break;
1836 if (*p == NUL)
1837 {
1838 xp->xp_context = EXPAND_ENV_VARS;
1839 ++xp->xp_pattern;
1840 // Avoid that the assignment uses EXPAND_FILES again.
1841 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1842 *complp = EXPAND_ENV_VARS;
1843 }
1844 }
1845 // Check for user names.
1846 if (*xp->xp_pattern == '~')
1847 {
1848 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1849 ;
1850 // Complete ~user only if it partially matches a user name.
1851 // A full match ~user<Tab> will be replaced by user's home
1852 // directory i.e. something like ~user<Tab> -> /home/user/
1853 if (*p == NUL && p > xp->xp_pattern + 1
1854 && match_user(xp->xp_pattern + 1) >= 1)
1855 {
1856 xp->xp_context = EXPAND_USER;
1857 ++xp->xp_pattern;
1858 }
1859 }
1860}
1861
1862/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001863 * Set the completion context for the "++opt=arg" argument. Always returns
1864 * NULL.
1865 */
1866 static char_u *
1867set_context_in_argopt(expand_T *xp, char_u *arg)
1868{
1869 char_u *p;
1870
1871 p = vim_strchr(arg, '=');
1872 if (p == NULL)
1873 xp->xp_pattern = arg;
1874 else
1875 xp->xp_pattern = p + 1;
1876
1877 xp->xp_context = EXPAND_ARGOPT;
1878 return NULL;
1879}
1880
1881#ifdef FEAT_TERMINAL
1882/*
1883 * Set the completion context for :terminal's [options]. Always returns NULL.
1884 */
1885 static char_u *
1886set_context_in_terminalopt(expand_T *xp, char_u *arg)
1887{
1888 char_u *p;
1889
1890 p = vim_strchr(arg, '=');
1891 if (p == NULL)
1892 xp->xp_pattern = arg;
1893 else
1894 xp->xp_pattern = p + 1;
1895
1896 xp->xp_context = EXPAND_TERMINALOPT;
1897 return NULL;
1898}
1899#endif
1900
1901/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001902 * Set the completion context for the :filter command. Returns a pointer to the
1903 * next command after the :filter command.
1904 */
1905 static char_u *
1906set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1907{
1908 if (*arg != NUL)
1909 arg = skip_vimgrep_pat(arg, NULL, NULL);
1910 if (arg == NULL || *arg == NUL)
1911 {
1912 xp->xp_context = EXPAND_NOTHING;
1913 return NULL;
1914 }
1915 return skipwhite(arg);
1916}
1917
1918#ifdef FEAT_SEARCH_EXTRA
1919/*
1920 * Set the completion context for the :match command. Returns a pointer to the
1921 * next command after the :match command.
1922 */
1923 static char_u *
1924set_context_in_match_cmd(expand_T *xp, char_u *arg)
1925{
1926 if (*arg == NUL || !ends_excmd(*arg))
1927 {
1928 // also complete "None"
1929 set_context_in_echohl_cmd(xp, arg);
1930 arg = skipwhite(skiptowhite(arg));
1931 if (*arg != NUL)
1932 {
1933 xp->xp_context = EXPAND_NOTHING;
1934 arg = skip_regexp(arg + 1, *arg, magic_isset());
1935 }
1936 }
1937 return find_nextcmd(arg);
1938}
1939#endif
1940
1941/*
1942 * Returns a pointer to the next command after a :global or a :v command.
1943 * Returns NULL if there is no next command.
1944 */
1945 static char_u *
1946find_cmd_after_global_cmd(char_u *arg)
1947{
1948 int delim;
1949
1950 delim = *arg; // get the delimiter
1951 if (delim)
1952 ++arg; // skip delimiter if there is one
1953
1954 while (arg[0] != NUL && arg[0] != delim)
1955 {
1956 if (arg[0] == '\\' && arg[1] != NUL)
1957 ++arg;
1958 ++arg;
1959 }
1960 if (arg[0] != NUL)
1961 return arg + 1;
1962
1963 return NULL;
1964}
1965
1966/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001967 * Returns a pointer to the next command after a :substitute or a :& command.
1968 * Returns NULL if there is no next command.
1969 */
1970 static char_u *
1971find_cmd_after_substitute_cmd(char_u *arg)
1972{
1973 int delim;
1974
1975 delim = *arg;
1976 if (delim)
1977 {
1978 // skip "from" part
1979 ++arg;
1980 arg = skip_regexp(arg, delim, magic_isset());
1981
1982 if (arg[0] != NUL && arg[0] == delim)
1983 {
1984 // skip "to" part
1985 ++arg;
1986 while (arg[0] != NUL && arg[0] != delim)
1987 {
1988 if (arg[0] == '\\' && arg[1] != NUL)
1989 ++arg;
1990 ++arg;
1991 }
1992 if (arg[0] != NUL) // skip delimiter
1993 ++arg;
1994 }
1995 }
1996 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1997 ++arg;
1998 if (arg[0] != NUL)
1999 return arg;
2000
2001 return NULL;
2002}
2003
2004/*
2005 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
2006 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
2007 * Returns NULL if there is no next command.
2008 */
2009 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002010find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002011{
2012 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002013 if (*arg != '/')
2014 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002015
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002016 // Match regexp, not just whole words
2017 for (++arg; *arg && *arg != '/'; arg++)
2018 if (*arg == '\\' && arg[1] != NUL)
2019 arg++;
2020 if (*arg)
2021 {
2022 arg = skipwhite(arg + 1);
2023
2024 // Check for trailing illegal characters
2025 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
2026 xp->xp_context = EXPAND_NOTHING;
2027 else
2028 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002029 }
2030
2031 return NULL;
2032}
2033
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002034#ifdef FEAT_EVAL
2035/*
2036 * Set the completion context for the :unlet command. Always returns NULL.
2037 */
2038 static char_u *
2039set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2040{
2041 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2042 arg = xp->xp_pattern + 1;
2043
2044 xp->xp_context = EXPAND_USER_VARS;
2045 xp->xp_pattern = arg;
2046
2047 if (*xp->xp_pattern == '$')
2048 {
2049 xp->xp_context = EXPAND_ENV_VARS;
2050 ++xp->xp_pattern;
2051 }
2052
2053 return NULL;
2054}
2055#endif
2056
2057#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2058/*
2059 * Set the completion context for the :language command. Always returns NULL.
2060 */
2061 static char_u *
2062set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2063{
2064 char_u *p;
2065
2066 p = skiptowhite(arg);
2067 if (*p == NUL)
2068 {
2069 xp->xp_context = EXPAND_LANGUAGE;
2070 xp->xp_pattern = arg;
2071 }
2072 else
2073 {
2074 if ( STRNCMP(arg, "messages", p - arg) == 0
2075 || STRNCMP(arg, "ctype", p - arg) == 0
2076 || STRNCMP(arg, "time", p - arg) == 0
2077 || STRNCMP(arg, "collate", p - arg) == 0)
2078 {
2079 xp->xp_context = EXPAND_LOCALES;
2080 xp->xp_pattern = skipwhite(p);
2081 }
2082 else
2083 xp->xp_context = EXPAND_NOTHING;
2084 }
2085
2086 return NULL;
2087}
2088#endif
2089
Christian Brabandta3422aa2025-04-23 21:04:24 +02002090static enum
2091{
2092 EXP_FILETYPECMD_ALL, // expand all :filetype values
2093 EXP_FILETYPECMD_PLUGIN, // expand plugin on off
2094 EXP_FILETYPECMD_INDENT, // expand indent on off
2095 EXP_FILETYPECMD_ONOFF, // expand on off
2096} filetype_expand_what;
2097
2098#define EXPAND_FILETYPECMD_PLUGIN 0x01
2099#define EXPAND_FILETYPECMD_INDENT 0x02
2100#define EXPAND_FILETYPECMD_ONOFF 0x04
2101
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002102#ifdef FEAT_EVAL
2103static enum
2104{
2105 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002106 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2107 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002108} breakpt_expand_what;
2109
2110/*
2111 * Set the completion context for the :breakadd command. Always returns NULL.
2112 */
2113 static char_u *
2114set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2115{
2116 char_u *p;
2117 char_u *subcmd_start;
2118
2119 xp->xp_context = EXPAND_BREAKPOINT;
2120 xp->xp_pattern = arg;
2121
2122 if (cmdidx == CMD_breakadd)
2123 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002124 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002125 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002126 else
2127 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002128
2129 p = skipwhite(arg);
2130 if (*p == NUL)
2131 return NULL;
2132 subcmd_start = p;
2133
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002134 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002135 {
2136 // :breakadd file [lnum] <filename>
2137 // :breakadd func [lnum] <funcname>
2138 p += 4;
2139 p = skipwhite(p);
2140
2141 // skip line number (if specified)
2142 if (VIM_ISDIGIT(*p))
2143 {
2144 p = skipdigits(p);
2145 if (*p != ' ')
2146 {
2147 xp->xp_context = EXPAND_NOTHING;
2148 return NULL;
2149 }
2150 p = skipwhite(p);
2151 }
2152 if (STRNCMP("file", subcmd_start, 4) == 0)
2153 xp->xp_context = EXPAND_FILES;
2154 else
2155 xp->xp_context = EXPAND_USER_FUNC;
2156 xp->xp_pattern = p;
2157 }
2158 else if (STRNCMP("expr ", p, 5) == 0)
2159 {
2160 // :breakadd expr <expression>
2161 xp->xp_context = EXPAND_EXPRESSION;
2162 xp->xp_pattern = skipwhite(p + 5);
2163 }
2164
2165 return NULL;
2166}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002167
2168 static char_u *
2169set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2170{
2171 char_u *p;
2172
2173 xp->xp_context = EXPAND_NOTHING;
2174 xp->xp_pattern = NULL;
2175
2176 p = skipwhite(arg);
2177 if (VIM_ISDIGIT(*p))
2178 return NULL;
2179
2180 xp->xp_context = EXPAND_SCRIPTNAMES;
2181 xp->xp_pattern = p;
2182
2183 return NULL;
2184}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002185#endif
2186
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002187/*
Christian Brabandta3422aa2025-04-23 21:04:24 +02002188 * Set the completion context for the :filetype command. Always returns NULL.
2189 */
2190 static char_u *
2191set_context_in_filetype_cmd(expand_T *xp, char_u *arg)
2192{
2193 char_u *p;
2194 int val = 0;
2195
2196 xp->xp_context = EXPAND_FILETYPECMD;
2197 xp->xp_pattern = arg;
2198 filetype_expand_what = EXP_FILETYPECMD_ALL;
2199
2200 p = skipwhite(arg);
2201 if (*p == NUL)
2202 return NULL;
2203
2204 for (;;)
2205 {
2206 if (STRNCMP(p, "plugin", 6) == 0)
2207 {
2208 val |= EXPAND_FILETYPECMD_PLUGIN;
2209 p = skipwhite(p + 6);
2210 continue;
2211 }
2212 if (STRNCMP(p, "indent", 6) == 0)
2213 {
2214 val |= EXPAND_FILETYPECMD_INDENT;
2215 p = skipwhite(p + 6);
2216 continue;
2217 }
2218 break;
2219 }
2220
2221 if ((val & EXPAND_FILETYPECMD_PLUGIN) && (val & EXPAND_FILETYPECMD_INDENT))
2222 filetype_expand_what = EXP_FILETYPECMD_ONOFF;
2223 else if ((val & EXPAND_FILETYPECMD_PLUGIN))
2224 filetype_expand_what = EXP_FILETYPECMD_INDENT;
2225 else if ((val & EXPAND_FILETYPECMD_INDENT))
2226 filetype_expand_what = EXP_FILETYPECMD_PLUGIN;
2227
2228 xp->xp_pattern = p;
2229
2230 return NULL;
2231}
2232
Girish Palya6b49fba2025-06-28 19:47:34 +02002233/*
2234 * Sets the completion context for commands that involve a search pattern
2235 * and a line range (e.g., :s, :g, :v).
2236 */
2237 static void
2238set_context_with_pattern(expand_T *xp)
2239{
2240 int skiplen = 0;
2241 cmdline_info_T *ccline = get_cmdline_info();
2242#ifdef FEAT_SEARCH_EXTRA
2243 int dummy, patlen, retval;
2244
2245 ++emsg_off;
2246 retval = parse_pattern_and_range(&pre_incsearch_pos, &dummy, &skiplen,
2247 &patlen);
2248 --emsg_off;
2249
2250 // Check if cursor is within search pattern
2251 if (!retval || ccline->cmdpos <= skiplen
2252 || ccline->cmdpos > skiplen + patlen)
2253 return;
2254#endif
2255
2256 xp->xp_pattern = ccline->cmdbuff + skiplen;
2257 xp->xp_pattern_len = ccline->cmdpos - skiplen;
2258 xp->xp_context = EXPAND_PATTERN_IN_BUF;
2259 xp->xp_search_dir = FORWARD;
2260}
Christian Brabandta3422aa2025-04-23 21:04:24 +02002261
2262/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002263 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2264 * The argument to the command is 'arg' and the argument flags is 'argt'.
2265 * For user-defined commands and for environment variables, 'compl' has the
2266 * completion type.
2267 * Returns a pointer to the next command. Returns NULL if there is no next
2268 * command.
2269 */
2270 static char_u *
2271set_context_by_cmdname(
2272 char_u *cmd,
2273 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002274 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002275 char_u *arg,
2276 long argt,
2277 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002278 int forceit)
2279{
Girish Palya6b49fba2025-06-28 19:47:34 +02002280 char_u *nextcmd;
2281
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002282 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002283 {
2284 case CMD_find:
2285 case CMD_sfind:
2286 case CMD_tabfind:
2287 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002288 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002289 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002290 break;
2291 case CMD_cd:
2292 case CMD_chdir:
2293 case CMD_tcd:
2294 case CMD_tchdir:
2295 case CMD_lcd:
2296 case CMD_lchdir:
2297 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002298 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002299 break;
2300 case CMD_help:
2301 xp->xp_context = EXPAND_HELP;
2302 xp->xp_pattern = arg;
2303 break;
2304
2305 // Command modifiers: return the argument.
2306 // Also for commands with an argument that is a command.
2307 case CMD_aboveleft:
2308 case CMD_argdo:
2309 case CMD_belowright:
2310 case CMD_botright:
2311 case CMD_browse:
2312 case CMD_bufdo:
2313 case CMD_cdo:
2314 case CMD_cfdo:
2315 case CMD_confirm:
2316 case CMD_debug:
2317 case CMD_folddoclosed:
2318 case CMD_folddoopen:
2319 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002320 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002321 case CMD_keepalt:
2322 case CMD_keepjumps:
2323 case CMD_keepmarks:
2324 case CMD_keeppatterns:
2325 case CMD_ldo:
2326 case CMD_leftabove:
2327 case CMD_lfdo:
2328 case CMD_lockmarks:
2329 case CMD_noautocmd:
2330 case CMD_noswapfile:
2331 case CMD_rightbelow:
2332 case CMD_sandbox:
2333 case CMD_silent:
2334 case CMD_tab:
2335 case CMD_tabdo:
2336 case CMD_topleft:
2337 case CMD_verbose:
2338 case CMD_vertical:
2339 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002340 case CMD_vim9cmd:
2341 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002342 return arg;
2343
2344 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002345 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002346
2347#ifdef FEAT_SEARCH_EXTRA
2348 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002349 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002350#endif
2351
2352 // All completion for the +cmdline_compl feature goes here.
2353
2354 case CMD_command:
2355 return set_context_in_user_cmd(xp, arg);
2356
2357 case CMD_delcommand:
2358 xp->xp_context = EXPAND_USER_COMMANDS;
2359 xp->xp_pattern = arg;
2360 break;
2361
2362 case CMD_global:
2363 case CMD_vglobal:
Girish Palya6b49fba2025-06-28 19:47:34 +02002364 nextcmd = find_cmd_after_global_cmd(arg);
2365 if (!nextcmd && may_expand_pattern)
2366 set_context_with_pattern(xp);
2367 return nextcmd;
2368
Bram Moolenaard0190392019-08-23 21:17:35 +02002369 case CMD_and:
2370 case CMD_substitute:
Girish Palya6b49fba2025-06-28 19:47:34 +02002371 nextcmd = find_cmd_after_substitute_cmd(arg);
2372 if (!nextcmd && may_expand_pattern)
2373 set_context_with_pattern(xp);
2374 return nextcmd;
2375
Bram Moolenaard0190392019-08-23 21:17:35 +02002376 case CMD_isearch:
2377 case CMD_dsearch:
2378 case CMD_ilist:
2379 case CMD_dlist:
2380 case CMD_ijump:
2381 case CMD_psearch:
2382 case CMD_djump:
2383 case CMD_isplit:
2384 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002385 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002386 case CMD_autocmd:
2387 return set_context_in_autocmd(xp, arg, FALSE);
2388 case CMD_doautocmd:
2389 case CMD_doautoall:
2390 return set_context_in_autocmd(xp, arg, TRUE);
2391 case CMD_set:
2392 set_context_in_set_cmd(xp, arg, 0);
2393 break;
2394 case CMD_setglobal:
2395 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2396 break;
2397 case CMD_setlocal:
2398 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2399 break;
2400 case CMD_tag:
2401 case CMD_stag:
2402 case CMD_ptag:
2403 case CMD_ltag:
2404 case CMD_tselect:
2405 case CMD_stselect:
2406 case CMD_ptselect:
2407 case CMD_tjump:
2408 case CMD_stjump:
2409 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002410 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002411 xp->xp_context = EXPAND_TAGS_LISTFILES;
2412 else
2413 xp->xp_context = EXPAND_TAGS;
2414 xp->xp_pattern = arg;
2415 break;
2416 case CMD_augroup:
2417 xp->xp_context = EXPAND_AUGROUP;
2418 xp->xp_pattern = arg;
2419 break;
2420#ifdef FEAT_SYN_HL
2421 case CMD_syntax:
2422 set_context_in_syntax_cmd(xp, arg);
2423 break;
2424#endif
2425#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002426 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002427 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002428 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002429 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002430 case CMD_if:
2431 case CMD_elseif:
2432 case CMD_while:
2433 case CMD_for:
2434 case CMD_echo:
2435 case CMD_echon:
2436 case CMD_execute:
2437 case CMD_echomsg:
2438 case CMD_echoerr:
2439 case CMD_call:
2440 case CMD_return:
2441 case CMD_cexpr:
2442 case CMD_caddexpr:
2443 case CMD_cgetexpr:
2444 case CMD_lexpr:
2445 case CMD_laddexpr:
2446 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002447 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002448 break;
2449
2450 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002451 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002452 case CMD_function:
2453 case CMD_delfunction:
2454 xp->xp_context = EXPAND_USER_FUNC;
2455 xp->xp_pattern = arg;
2456 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002457 case CMD_disassemble:
2458 set_context_in_disassemble_cmd(xp, arg);
2459 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002460
2461 case CMD_echohl:
2462 set_context_in_echohl_cmd(xp, arg);
2463 break;
2464#endif
2465 case CMD_highlight:
2466 set_context_in_highlight_cmd(xp, arg);
2467 break;
2468#ifdef FEAT_CSCOPE
2469 case CMD_cscope:
2470 case CMD_lcscope:
2471 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002472 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002473 break;
2474#endif
2475#ifdef FEAT_SIGNS
2476 case CMD_sign:
2477 set_context_in_sign_cmd(xp, arg);
2478 break;
2479#endif
2480 case CMD_bdelete:
2481 case CMD_bwipeout:
2482 case CMD_bunload:
2483 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2484 arg = xp->xp_pattern + 1;
2485 // FALLTHROUGH
2486 case CMD_buffer:
2487 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002488 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002489 case CMD_checktime:
2490 xp->xp_context = EXPAND_BUFFERS;
2491 xp->xp_pattern = arg;
2492 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002493#ifdef FEAT_DIFF
2494 case CMD_diffget:
2495 case CMD_diffput:
2496 // If current buffer is in diff mode, complete buffer names
2497 // which are in diff mode, and different than current buffer.
2498 xp->xp_context = EXPAND_DIFF_BUFFERS;
2499 xp->xp_pattern = arg;
2500 break;
2501#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002502 case CMD_USER:
2503 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002504 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2505 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002506
2507 case CMD_map: case CMD_noremap:
2508 case CMD_nmap: case CMD_nnoremap:
2509 case CMD_vmap: case CMD_vnoremap:
2510 case CMD_omap: case CMD_onoremap:
2511 case CMD_imap: case CMD_inoremap:
2512 case CMD_cmap: case CMD_cnoremap:
2513 case CMD_lmap: case CMD_lnoremap:
2514 case CMD_smap: case CMD_snoremap:
2515 case CMD_tmap: case CMD_tnoremap:
2516 case CMD_xmap: case CMD_xnoremap:
2517 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002518 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002519 case CMD_unmap:
2520 case CMD_nunmap:
2521 case CMD_vunmap:
2522 case CMD_ounmap:
2523 case CMD_iunmap:
2524 case CMD_cunmap:
2525 case CMD_lunmap:
2526 case CMD_sunmap:
2527 case CMD_tunmap:
2528 case CMD_xunmap:
2529 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002530 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002531 case CMD_mapclear:
2532 case CMD_nmapclear:
2533 case CMD_vmapclear:
2534 case CMD_omapclear:
2535 case CMD_imapclear:
2536 case CMD_cmapclear:
2537 case CMD_lmapclear:
2538 case CMD_smapclear:
2539 case CMD_tmapclear:
2540 case CMD_xmapclear:
2541 xp->xp_context = EXPAND_MAPCLEAR;
2542 xp->xp_pattern = arg;
2543 break;
2544
2545 case CMD_abbreviate: case CMD_noreabbrev:
2546 case CMD_cabbrev: case CMD_cnoreabbrev:
2547 case CMD_iabbrev: case CMD_inoreabbrev:
2548 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002549 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002550 case CMD_unabbreviate:
2551 case CMD_cunabbrev:
2552 case CMD_iunabbrev:
2553 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002554 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002555#ifdef FEAT_MENU
2556 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2557 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2558 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2559 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2560 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2561 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2562 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2563 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2564 case CMD_tmenu: case CMD_tunmenu:
2565 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2566 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2567#endif
2568
2569 case CMD_colorscheme:
2570 xp->xp_context = EXPAND_COLORS;
2571 xp->xp_pattern = arg;
2572 break;
2573
2574 case CMD_compiler:
2575 xp->xp_context = EXPAND_COMPILER;
2576 xp->xp_pattern = arg;
2577 break;
2578
2579 case CMD_ownsyntax:
2580 xp->xp_context = EXPAND_OWNSYNTAX;
2581 xp->xp_pattern = arg;
2582 break;
2583
2584 case CMD_setfiletype:
2585 xp->xp_context = EXPAND_FILETYPE;
2586 xp->xp_pattern = arg;
2587 break;
2588
2589 case CMD_packadd:
2590 xp->xp_context = EXPAND_PACKADD;
2591 xp->xp_pattern = arg;
2592 break;
2593
zeertzjqb0d45ec2023-01-25 15:04:22 +00002594 case CMD_runtime:
2595 set_context_in_runtime_cmd(xp, arg);
2596 break;
2597
Bram Moolenaard0190392019-08-23 21:17:35 +02002598#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2599 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002600 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002601#endif
2602#if defined(FEAT_PROFILE)
2603 case CMD_profile:
2604 set_context_in_profile_cmd(xp, arg);
2605 break;
2606#endif
2607 case CMD_behave:
2608 xp->xp_context = EXPAND_BEHAVE;
2609 xp->xp_pattern = arg;
2610 break;
2611
2612 case CMD_messages:
2613 xp->xp_context = EXPAND_MESSAGES;
2614 xp->xp_pattern = arg;
2615 break;
2616
2617 case CMD_history:
2618 xp->xp_context = EXPAND_HISTORY;
2619 xp->xp_pattern = arg;
2620 break;
2621#if defined(FEAT_PROFILE)
2622 case CMD_syntime:
2623 xp->xp_context = EXPAND_SYNTIME;
2624 xp->xp_pattern = arg;
2625 break;
2626#endif
2627
2628 case CMD_argdelete:
2629 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2630 arg = xp->xp_pattern + 1;
2631 xp->xp_context = EXPAND_ARGLIST;
2632 xp->xp_pattern = arg;
2633 break;
2634
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002635#ifdef FEAT_EVAL
2636 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002637 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002638 case CMD_breakdel:
2639 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002640
2641 case CMD_scriptnames:
2642 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002643#endif
Christian Brabandta3422aa2025-04-23 21:04:24 +02002644 case CMD_filetype:
2645 return set_context_in_filetype_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002646
Bram Moolenaard0190392019-08-23 21:17:35 +02002647 default:
2648 break;
2649 }
2650 return NULL;
2651}
2652
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002653/*
2654 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2655 * we don't need/want deleted. Maybe this could be done better if we didn't
2656 * repeat all this stuff. The only problem is that they may not stay
2657 * perfectly compatible with each other, but then the command line syntax
2658 * probably won't change that much -- webb.
2659 */
2660 static char_u *
2661set_one_cmd_context(
2662 expand_T *xp,
2663 char_u *buff) // buffer for command string
2664{
2665 char_u *p;
2666 char_u *cmd, *arg;
2667 int len = 0;
2668 exarg_T ea;
2669 int compl = EXPAND_NOTHING;
2670 int forceit = FALSE;
2671 int usefilter = FALSE; // filter instead of file name
2672
2673 ExpandInit(xp);
2674 xp->xp_pattern = buff;
2675 xp->xp_line = buff;
2676 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2677 ea.argt = 0;
2678
2679 // 1. skip comment lines and leading space, colons or bars
2680 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2681 ;
2682 xp->xp_pattern = cmd;
2683
2684 if (*cmd == NUL)
2685 return NULL;
2686 if (*cmd == '"') // ignore comment lines
2687 {
2688 xp->xp_context = EXPAND_NOTHING;
2689 return NULL;
2690 }
2691
2692 // 3. Skip over the range to find the command.
2693 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2694 xp->xp_pattern = cmd;
2695 if (*cmd == NUL)
2696 return NULL;
2697 if (*cmd == '"')
2698 {
2699 xp->xp_context = EXPAND_NOTHING;
2700 return NULL;
2701 }
2702
2703 if (*cmd == '|' || *cmd == '\n')
2704 return cmd + 1; // There's another command
2705
2706 // Get the command index.
2707 p = set_cmd_index(cmd, &ea, xp, &compl);
2708 if (p == NULL)
2709 return NULL;
2710
2711 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2712
2713 if (*p == '!') // forced commands
2714 {
2715 forceit = TRUE;
2716 ++p;
2717 }
2718
2719 // 6. parse arguments
2720 if (!IS_USER_CMDIDX(ea.cmdidx))
2721 ea.argt = excmd_get_argt(ea.cmdidx);
2722
2723 arg = skipwhite(p);
2724
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002725 // Does command allow "++argopt" argument?
2726 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002727 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002728 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2729 {
2730 p = arg + 2;
2731 while (*p && !vim_isspace(*p))
2732 MB_PTR_ADV(p);
2733
2734 // Still touching the command after "++"?
2735 if (*p == NUL)
2736 {
2737 if (ea.argt & EX_ARGOPT)
2738 return set_context_in_argopt(xp, arg + 2);
2739#ifdef FEAT_TERMINAL
2740 if (ea.cmdidx == CMD_terminal)
2741 return set_context_in_terminalopt(xp, arg + 2);
2742#endif
2743 }
2744
2745 arg = skipwhite(p);
2746 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002747 }
2748
2749 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2750 {
2751 if (*arg == '>') // append
2752 {
2753 if (*++arg == '>')
2754 ++arg;
2755 arg = skipwhite(arg);
2756 }
2757 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2758 {
2759 ++arg;
2760 usefilter = TRUE;
2761 }
2762 }
2763
2764 if (ea.cmdidx == CMD_read)
2765 {
2766 usefilter = forceit; // :r! filter if forced
2767 if (*arg == '!') // :r !filter
2768 {
2769 ++arg;
2770 usefilter = TRUE;
2771 }
2772 }
2773
2774 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2775 {
2776 while (*arg == *cmd) // allow any number of '>' or '<'
2777 ++arg;
2778 arg = skipwhite(arg);
2779 }
2780
2781 // Does command allow "+command"?
2782 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2783 {
2784 // Check if we're in the +command
2785 p = arg + 1;
2786 arg = skip_cmd_arg(arg, FALSE);
2787
2788 // Still touching the command after '+'?
2789 if (*arg == NUL)
2790 return p;
2791
2792 // Skip space(s) after +command to get to the real argument
2793 arg = skipwhite(arg);
2794 }
2795
2796
2797 // Check for '|' to separate commands and '"' to start comments.
2798 // Don't do this for ":read !cmd" and ":write !cmd".
2799 if ((ea.argt & EX_TRLBAR) && !usefilter)
2800 {
2801 p = arg;
2802 // ":redir @" is not the start of a comment
2803 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2804 p += 2;
2805 while (*p)
2806 {
2807 if (*p == Ctrl_V)
2808 {
2809 if (p[1] != NUL)
2810 ++p;
2811 }
2812 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2813 || *p == '|' || *p == '\n')
2814 {
2815 if (*(p - 1) != '\\')
2816 {
2817 if (*p == '|' || *p == '\n')
2818 return p + 1;
2819 return NULL; // It's a comment
2820 }
2821 }
2822 MB_PTR_ADV(p);
2823 }
2824 }
2825
2826 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2827 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2828 // no arguments allowed but there is something
2829 return NULL;
2830
2831 // Find start of last argument (argument just before cursor):
2832 p = buff;
2833 xp->xp_pattern = p;
2834 len = (int)STRLEN(buff);
2835 while (*p && p < buff + len)
2836 {
2837 if (*p == ' ' || *p == TAB)
2838 {
2839 // argument starts after a space
2840 xp->xp_pattern = ++p;
2841 }
2842 else
2843 {
2844 if (*p == '\\' && *(p + 1) != NUL)
2845 ++p; // skip over escaped character
2846 MB_PTR_ADV(p);
2847 }
2848 }
2849
2850 if (ea.argt & EX_XFILE)
2851 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2852
2853 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002854 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002855 forceit);
2856}
2857
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002858/*
2859 * Set the completion context in 'xp' for command 'str'
2860 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002861 void
2862set_cmd_context(
2863 expand_T *xp,
2864 char_u *str, // start of command line
2865 int len, // length of command line (excl. NUL)
2866 int col, // position of cursor
2867 int use_ccline UNUSED) // use ccline for info
2868{
2869#ifdef FEAT_EVAL
2870 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002871 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002872#endif
2873 int old_char = NUL;
2874 char_u *nextcomm;
2875
2876 // Avoid a UMR warning from Purify, only save the character if it has been
2877 // written before.
2878 if (col < len)
2879 old_char = str[col];
2880 str[col] = NUL;
2881 nextcomm = str;
2882
2883#ifdef FEAT_EVAL
2884 if (use_ccline && ccline->cmdfirstc == '=')
2885 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002886 // pass CMD_SIZE because there is no real command
2887 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002888 }
2889 else if (use_ccline && ccline->input_fn)
2890 {
2891 xp->xp_context = ccline->xp_context;
2892 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002893 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002894 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2895 {
2896 context = xp->xp_context;
2897 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2898 &context);
2899 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002900 }
2901 else
2902#endif
2903 while (nextcomm != NULL)
2904 nextcomm = set_one_cmd_context(xp, nextcomm);
2905
2906 // Store the string here so that call_user_expand_func() can get to them
2907 // easily.
2908 xp->xp_line = str;
2909 xp->xp_col = col;
2910
2911 str[col] = old_char;
2912}
2913
2914/*
2915 * Expand the command line "str" from context "xp".
2916 * "xp" must have been set by set_cmd_context().
2917 * xp->xp_pattern points into "str", to where the text that is to be expanded
2918 * starts.
2919 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2920 * cursor.
2921 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2922 * key that triggered expansion literally.
2923 * Returns EXPAND_OK otherwise.
2924 */
2925 int
2926expand_cmdline(
2927 expand_T *xp,
2928 char_u *str, // start of command line
2929 int col, // position of cursor
2930 int *matchcount, // return: nr of matches
2931 char_u ***matches) // return: array of pointers to matches
2932{
2933 char_u *file_str = NULL;
2934 int options = WILD_ADD_SLASH|WILD_SILENT;
2935
2936 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2937 {
2938 beep_flush();
2939 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2940 }
2941 if (xp->xp_context == EXPAND_NOTHING)
2942 {
2943 // Caller can use the character as a normal char instead
2944 return EXPAND_NOTHING;
2945 }
2946
2947 // add star to file name, or convert to regexp if not exp. files.
2948 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002949 if (cmdline_fuzzy_completion_supported(xp))
2950 // If fuzzy matching, don't modify the search string
2951 file_str = vim_strsave(xp->xp_pattern);
2952 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002953 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
John Marriott1be5b372025-06-23 19:57:29 +02002954 if (file_str == NULL)
2955 return EXPAND_UNSUCCESSFUL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002956
2957 if (p_wic)
2958 options += WILD_ICASE;
2959
2960 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002961 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002962 {
2963 *matchcount = 0;
2964 *matches = NULL;
2965 }
2966 vim_free(file_str);
2967
2968 return EXPAND_OK;
2969}
2970
Bram Moolenaar66b51422019-08-18 21:44:12 +02002971/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002972 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002973 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002974 */
2975 static int
2976expand_files_and_dirs(
2977 expand_T *xp,
2978 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002979 char_u ***matches,
2980 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002981 int flags,
2982 int options)
2983{
2984 int free_pat = FALSE;
2985 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002986 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002987
2988 // for ":set path=" and ":set tags=" halve backslashes for escaped
2989 // space
2990 if (xp->xp_backslash != XP_BS_NONE)
2991 {
2992 free_pat = TRUE;
2993 pat = vim_strsave(pat);
John Marriott3b03b432025-06-28 20:41:54 +02002994 if (pat == NULL)
2995 return ret;
2996
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002997 for (i = 0; pat[i]; ++i)
2998 if (pat[i] == '\\')
2999 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02003000 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003001 && pat[i + 1] == '\\'
3002 && pat[i + 2] == '\\'
3003 && pat[i + 3] == ' ')
3004 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02003005 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003006 && pat[i + 1] == ' ')
3007 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02003008 else if ((xp->xp_backslash & XP_BS_COMMA)
3009 && pat[i + 1] == '\\'
3010 && pat[i + 2] == ',')
3011 STRMOVE(pat + i, pat + i + 2);
3012#ifdef BACKSLASH_IN_FILENAME
3013 else if ((xp->xp_backslash & XP_BS_COMMA)
3014 && pat[i + 1] == ',')
3015 STRMOVE(pat + i, pat + i + 1);
3016#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003017 }
3018 }
3019
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003020 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003021 {
3022#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003023 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003024#endif
3025 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003026 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003027 {
3028 if (xp->xp_context == EXPAND_FILES)
3029 flags |= EW_FILE;
3030 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
3031 flags |= (EW_FILE | EW_PATH);
3032 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
3033 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
3034 else
3035 flags = (flags | EW_DIR) & ~EW_FILE;
3036 if (options & WILD_ICASE)
3037 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003038
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003039 // Expand wildcards, supporting %:h and the like.
3040 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
3041 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003042 if (free_pat)
3043 vim_free(pat);
3044#ifdef BACKSLASH_IN_FILENAME
3045 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
3046 {
3047 int j;
3048
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003049 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003050 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003051 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003052
3053 while (*ptr != NUL)
3054 {
3055 if (p_csl[0] == 's' && *ptr == '\\')
3056 *ptr = '/';
3057 else if (p_csl[0] == 'b' && *ptr == '/')
3058 *ptr = '\\';
3059 ptr += (*mb_ptr2len)(ptr);
3060 }
3061 }
3062 }
3063#endif
3064 return ret;
3065}
3066
3067/*
Bram Moolenaard0190392019-08-23 21:17:35 +02003068 * Function given to ExpandGeneric() to obtain the possible arguments of the
3069 * ":behave {mswin,xterm}" command.
3070 */
3071 static char_u *
3072get_behave_arg(expand_T *xp UNUSED, int idx)
3073{
3074 if (idx == 0)
3075 return (char_u *)"mswin";
3076 if (idx == 1)
3077 return (char_u *)"xterm";
3078 return NULL;
3079}
3080
Christian Brabandta3422aa2025-04-23 21:04:24 +02003081/*
3082 * Function given to ExpandGeneric() to obtain the possible arguments of the
3083 * ":filetype {plugin,indent}" command.
3084 */
3085 static char_u *
3086get_filetypecmd_arg(expand_T *xp UNUSED, int idx)
3087{
3088 char *opts_all[] = {"indent", "plugin", "on", "off"};
3089 char *opts_plugin[] = {"plugin", "on", "off"};
3090 char *opts_indent[] = {"indent", "on", "off"};
3091 char *opts_onoff[] = {"on", "off"};
3092
3093 if (filetype_expand_what == EXP_FILETYPECMD_ALL && idx < 4)
3094 return (char_u *)opts_all[idx];
3095 if (filetype_expand_what == EXP_FILETYPECMD_PLUGIN && idx < 3)
3096 return (char_u *)opts_plugin[idx];
3097 if (filetype_expand_what == EXP_FILETYPECMD_INDENT && idx < 3)
3098 return (char_u *)opts_indent[idx];
3099 if (filetype_expand_what == EXP_FILETYPECMD_ONOFF && idx < 2)
3100 return (char_u *)opts_onoff[idx];
3101 return NULL;
3102}
3103
K.Takata161b6ac2022-11-14 15:31:07 +00003104#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003105/*
3106 * Function given to ExpandGeneric() to obtain the possible arguments of the
3107 * ":breakadd {expr, file, func, here}" command.
3108 * ":breakdel {func, file, here}" command.
3109 */
3110 static char_u *
3111get_breakadd_arg(expand_T *xp UNUSED, int idx)
3112{
3113 char *opts[] = {"expr", "file", "func", "here"};
3114
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003115 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003116 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003117 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003118 if (breakpt_expand_what == EXP_BREAKPT_ADD)
3119 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003120 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
3121 {
3122 // breakdel {func, file, here}
3123 if (idx <= 2)
3124 return (char_u *)opts[idx + 1];
3125 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003126 else
3127 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003128 // profdel {func, file}
3129 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003130 return (char_u *)opts[idx + 1];
3131 }
3132 }
3133 return NULL;
3134}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003135
3136/*
3137 * Function given to ExpandGeneric() to obtain the possible arguments for the
3138 * ":scriptnames" command.
3139 */
3140 static char_u *
3141get_scriptnames_arg(expand_T *xp UNUSED, int idx)
3142{
3143 scriptitem_T *si;
3144
3145 if (!SCRIPT_ID_VALID(idx + 1))
3146 return NULL;
3147
3148 si = SCRIPT_ITEM(idx + 1);
3149 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
3150 return NameBuff;
3151}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003152#endif
3153
Bram Moolenaard0190392019-08-23 21:17:35 +02003154/*
3155 * Function given to ExpandGeneric() to obtain the possible arguments of the
3156 * ":messages {clear}" command.
3157 */
3158 static char_u *
3159get_messages_arg(expand_T *xp UNUSED, int idx)
3160{
3161 if (idx == 0)
3162 return (char_u *)"clear";
3163 return NULL;
3164}
3165
3166 static char_u *
3167get_mapclear_arg(expand_T *xp UNUSED, int idx)
3168{
3169 if (idx == 0)
3170 return (char_u *)"<buffer>";
3171 return NULL;
3172}
3173
3174/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003175 * Do the expansion based on xp->xp_context and 'rmp'.
3176 */
3177 static int
3178ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003179 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003180 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003181 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003182 char_u ***matches,
3183 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003184{
3185 static struct expgen
3186 {
3187 int context;
3188 char_u *((*func)(expand_T *, int));
3189 int ic;
3190 int escaped;
3191 } tab[] =
3192 {
3193 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3194 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Christian Brabandta3422aa2025-04-23 21:04:24 +02003195 {EXPAND_FILETYPECMD, get_filetypecmd_arg, TRUE, TRUE},
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003196 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3197 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3198 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3199 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3200 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3201 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3202 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3203 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003204#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003205 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3206 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3207 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3208 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3209 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003210#endif
3211#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003212 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3213 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003214#endif
3215#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003216 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003217#endif
3218#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003219 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003220#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003221 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3222 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3223 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003224#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003225 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003226#endif
3227#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003228 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003229#endif
3230#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003231 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003232#endif
3233#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003234 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3235 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003236#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003237 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3238 {EXPAND_USER, get_users, TRUE, FALSE},
3239 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003240#ifdef FEAT_EVAL
3241 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003242 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003243#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003244 };
3245 int i;
3246 int ret = FAIL;
3247
3248 // Find a context in the table and call the ExpandGeneric() with the
3249 // right function to do the expansion.
3250 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3251 {
3252 if (xp->xp_context == tab[i].context)
3253 {
3254 if (tab[i].ic)
3255 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003256 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3257 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003258 break;
3259 }
3260 }
3261
3262 return ret;
3263}
3264
3265/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003266 * Map wild expand options to flags for expand_wildcards()
3267 */
3268 static int
3269map_wildopts_to_ewflags(int options)
3270{
3271 int flags;
3272
3273 flags = EW_DIR; // include directories
3274 if (options & WILD_LIST_NOTFOUND)
3275 flags |= EW_NOTFOUND;
3276 if (options & WILD_ADD_SLASH)
3277 flags |= EW_ADDSLASH;
3278 if (options & WILD_KEEP_ALL)
3279 flags |= EW_KEEPALL;
3280 if (options & WILD_SILENT)
3281 flags |= EW_SILENT;
3282 if (options & WILD_NOERROR)
3283 flags |= EW_NOERROR;
3284 if (options & WILD_ALLLINKS)
3285 flags |= EW_ALLLINKS;
3286
3287 return flags;
3288}
3289
3290/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003291 * Do the expansion based on xp->xp_context and "pat".
3292 */
3293 static int
3294ExpandFromContext(
3295 expand_T *xp,
3296 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003297 char_u ***matches,
3298 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003299 int options) // WILD_ flags
3300{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003301 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003302 int ret;
3303 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003304 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003305 int fuzzy = cmdline_fuzzy_complete(pat)
3306 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003308 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003309
3310 if (xp->xp_context == EXPAND_FILES
3311 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003312 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003313 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003314 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003315 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3316 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003317
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003318 *matches = (char_u **)"";
3319 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003320 if (xp->xp_context == EXPAND_HELP)
3321 {
3322 // With an empty argument we would get all the help tags, which is
3323 // very slow. Get matches for "help" instead.
3324 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003325 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003326 {
3327#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003328 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003329#endif
3330 return OK;
3331 }
3332 return FAIL;
3333 }
3334
Bram Moolenaar66b51422019-08-18 21:44:12 +02003335 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003336 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003337 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003338 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003339 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003340 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003341#ifdef FEAT_DIFF
3342 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003343 return ExpandBufnames(pat, numMatches, matches,
3344 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003345#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003346 if (xp->xp_context == EXPAND_TAGS
3347 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003348 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3349 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 if (xp->xp_context == EXPAND_COLORS)
3351 {
3352 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003353 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354 directories);
3355 }
3356 if (xp->xp_context == EXPAND_COMPILER)
3357 {
3358 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003359 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360 }
3361 if (xp->xp_context == EXPAND_OWNSYNTAX)
3362 {
3363 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003364 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003365 }
3366 if (xp->xp_context == EXPAND_FILETYPE)
3367 {
3368 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003369 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003370 }
Doug Kearns81642d92024-01-04 22:37:44 +01003371#ifdef FEAT_KEYMAP
3372 if (xp->xp_context == EXPAND_KEYMAP)
3373 {
3374 char *directories[] = {"keymap", NULL};
3375 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3376 }
3377#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003378#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003379 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003380 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003381#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003382 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003383 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003384 if (xp->xp_context == EXPAND_RUNTIME)
3385 return expand_runtime_cmd(pat, numMatches, matches);
Girish Palya6b49fba2025-06-28 19:47:34 +02003386 if (xp->xp_context == EXPAND_PATTERN_IN_BUF)
3387 return expand_pattern_in_buf(pat, xp->xp_search_dir,
3388 matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003389
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003390 // When expanding a function name starting with s:, match the <SNR>nr_
3391 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003392 if ((xp->xp_context == EXPAND_USER_FUNC
3393 || xp->xp_context == EXPAND_DISASSEMBLE)
3394 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003395 {
3396 int len = (int)STRLEN(pat) + 20;
3397
3398 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003399 if (tofree == NULL)
3400 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003401 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003402 pat = tofree;
3403 }
3404
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003405 if (!fuzzy)
3406 {
3407 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3408 if (regmatch.regprog == NULL)
3409 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003410
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003411 // set ignore-case according to p_ic, p_scs and pat
3412 regmatch.rm_ic = ignorecase(pat);
3413 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003414
3415 if (xp->xp_context == EXPAND_SETTINGS
3416 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003417 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003418 else if (xp->xp_context == EXPAND_STRING_SETTING)
3419 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3420 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3421 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003422 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003423 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003424 else if (xp->xp_context == EXPAND_ARGOPT)
3425 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003426 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3427 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003428#if defined(FEAT_TERMINAL)
3429 else if (xp->xp_context == EXPAND_TERMINALOPT)
3430 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3431#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003432#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003433 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003434 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003435#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003436 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003437 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003438
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003439 if (!fuzzy)
3440 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003441 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003442
3443 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003444}
3445
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003446 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003447ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003448 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003449 expand_T *xp,
3450 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003451 char_u ***matches,
3452 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003453 char_u *((*func)(expand_T *, int)),
3454 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003455 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003456{
Yee Cheng China7b81202025-02-23 09:32:47 +01003457 return ExpandGenericExt(
3458 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3459}
3460
3461/*
3462 * Expand a list of names.
3463 *
3464 * Generic function for command line completion. It calls a function to
3465 * obtain strings, one by one. The strings are matched against a regexp
3466 * program. Matching strings are copied into an array, which is returned.
3467 *
3468 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3469 * is used.
3470 *
3471 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3472 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3473 * sorting.
3474 *
3475 * Returns OK when no problems encountered, FAIL for error (out of memory).
3476 */
3477 int
3478ExpandGenericExt(
3479 char_u *pat,
3480 expand_T *xp,
3481 regmatch_T *regmatch,
3482 char_u ***matches,
3483 int *numMatches,
3484 char_u *((*func)(expand_T *, int)),
3485 // returns a string from the list
3486 int escaped,
3487 int sortStartIdx)
3488{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003489 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003490 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003491 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003492 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003493 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003494 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003495 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003496 int sort_matches = FALSE;
3497 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003498 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003499
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003500 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003501 *matches = NULL;
3502 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003503
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003504 if (!fuzzy)
3505 ga_init2(&ga, sizeof(char *), 30);
3506 else
3507 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3508
3509 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003510 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003511 str = (*func)(xp, i);
3512 if (str == NULL) // end of list
3513 break;
3514 if (*str == NUL) // skip empty strings
3515 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003516
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003517 if (xp->xp_pattern[0] != NUL)
3518 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003519 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003520 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003521 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003522 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003523 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003524 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003525 }
3526 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003527 else
3528 match = TRUE;
3529
3530 if (!match)
3531 continue;
3532
3533 if (escaped)
3534 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3535 else
3536 str = vim_strsave(str);
3537 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003538 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003539 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003541 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003542 return FAIL;
3543 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003544 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003545 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003546 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003547
3548 if (ga_grow(&ga, 1) == FAIL)
3549 {
3550 vim_free(str);
3551 break;
3552 }
3553
3554 if (fuzzy)
3555 {
3556 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3557 fuzmatch->idx = ga.ga_len;
3558 fuzmatch->str = str;
3559 fuzmatch->score = score;
3560 }
3561 else
3562 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3563
K.Takata161b6ac2022-11-14 15:31:07 +00003564#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003565 if (func == get_menu_names)
3566 {
3567 // test for separator added by get_menu_names()
3568 str += STRLEN(str) - 1;
3569 if (*str == '\001')
3570 *str = '.';
3571 }
K.Takata161b6ac2022-11-14 15:31:07 +00003572#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003573
Yee Cheng China7b81202025-02-23 09:32:47 +01003574 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3575 {
3576 // Found first item to start sorting from. This is usually 0.
3577 sortStartMatchIdx = ga.ga_len;
3578 }
3579
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003580 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003581 }
3582
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003583 if (ga.ga_len == 0)
3584 return OK;
3585
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003586 // sort the matches when using regular expression matching and sorting
3587 // applies to the completion context. Menus and scriptnames should be kept
3588 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003589 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003590 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003591 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003592 && xp->xp_context != EXPAND_SCRIPTNAMES
3593 && xp->xp_context != EXPAND_ARGOPT
3594 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003595 sort_matches = TRUE;
3596
3597 // <SNR> functions should be sorted to the end.
3598 if (xp->xp_context == EXPAND_EXPRESSION
3599 || xp->xp_context == EXPAND_FUNCTIONS
3600 || xp->xp_context == EXPAND_USER_FUNC
3601 || xp->xp_context == EXPAND_DISASSEMBLE)
3602 funcsort = TRUE;
3603
3604 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003605 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003606 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003607 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003608 // <SNR> functions should be sorted to the end.
3609 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3610 sort_func_compare);
3611 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003612 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003613 }
3614
3615 if (!fuzzy)
3616 {
3617 *matches = ga.ga_data;
3618 *numMatches = ga.ga_len;
3619 }
3620 else
3621 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003622 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3623 funcsort) == FAIL)
3624 return FAIL;
3625 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003626 }
3627
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003628#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003629 // Reset the variables used for special highlight names expansion, so that
3630 // they don't show up when getting normal highlight names by ID.
3631 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003632#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003633
Bram Moolenaar66b51422019-08-18 21:44:12 +02003634 return OK;
3635}
3636
3637/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003638 * Expand shell command matches in one directory of $PATH.
3639 */
3640 static void
3641expand_shellcmd_onedir(
3642 char_u *buf,
3643 char_u *s,
3644 size_t l,
3645 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003646 char_u ***matches,
3647 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003648 int flags,
3649 hashtab_T *ht,
3650 garray_T *gap)
3651{
3652 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003653 hash_T hash;
3654 hashitem_T *hi;
3655
3656 vim_strncpy(buf, s, l);
3657 add_pathsep(buf);
3658 l = STRLEN(buf);
3659 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3660
3661 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003662 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003663 if (ret != OK)
3664 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003665
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003666 if (ga_grow(gap, *numMatches) == FAIL)
3667 {
3668 FreeWild(*numMatches, *matches);
3669 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003670 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003671
3672 for (int i = 0; i < *numMatches; ++i)
3673 {
3674 char_u *name = (*matches)[i];
3675
3676 if (STRLEN(name) > l)
3677 {
3678 // Check if this name was already found.
3679 hash = hash_hash(name + l);
3680 hi = hash_lookup(ht, name + l, hash);
3681 if (HASHITEM_EMPTY(hi))
3682 {
3683 // Remove the path that was prepended.
3684 STRMOVE(name, name + l);
3685 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3686 hash_add_item(ht, hi, name, hash);
3687 name = NULL;
3688 }
3689 }
3690 vim_free(name);
3691 }
3692 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003693}
3694
3695/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003696 * Complete a shell command.
3697 * Returns FAIL or OK;
3698 */
3699 static int
3700expand_shellcmd(
3701 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003702 char_u ***matches, // return: array with matches
3703 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003704 int flagsarg) // EW_ flags
3705{
3706 char_u *pat;
3707 int i;
3708 char_u *path = NULL;
3709 int mustfree = FALSE;
3710 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003711 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003712 size_t l;
3713 char_u *s, *e;
3714 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003715 int did_curdir = FALSE;
3716 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003717
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003718 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003719 if (buf == NULL)
3720 return FAIL;
3721
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003722 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003723 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003724 if (pat == NULL)
3725 {
3726 vim_free(buf);
3727 return FAIL;
3728 }
3729
Bram Moolenaar66b51422019-08-18 21:44:12 +02003730 for (i = 0; pat[i]; ++i)
3731 if (pat[i] == '\\' && pat[i + 1] == ' ')
3732 STRMOVE(pat + i, pat + i + 1);
3733
3734 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3735
3736 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3737 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3738 path = (char_u *)".";
3739 else
3740 {
3741 // For an absolute name we don't use $PATH.
3742 if (!mch_isFullName(pat))
3743 path = vim_getenv((char_u *)"PATH", &mustfree);
3744 if (path == NULL)
3745 path = (char_u *)"";
3746 }
3747
3748 // Go over all directories in $PATH. Expand matches in that directory and
3749 // collect them in "ga". When "." is not in $PATH also expand for the
3750 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003751 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003752 hash_init(&found_ht);
3753 for (s = path; ; s = e)
3754 {
K.Takata161b6ac2022-11-14 15:31:07 +00003755#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003756 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003757#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003758 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003759#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003760 if (e == NULL)
3761 e = s + STRLEN(s);
3762
3763 if (*s == NUL)
3764 {
3765 if (did_curdir)
3766 break;
3767 // Find directories in the current directory, path is empty.
3768 did_curdir = TRUE;
3769 flags |= EW_DIR;
3770 }
3771 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3772 {
3773 did_curdir = TRUE;
3774 flags |= EW_DIR;
3775 }
3776 else
3777 // Do not match directories inside a $PATH item.
3778 flags &= ~EW_DIR;
3779
3780 l = e - s;
3781 if (l > MAXPATHL - 5)
3782 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003783
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003784 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003785 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003786
Bram Moolenaar66b51422019-08-18 21:44:12 +02003787 if (*e != NUL)
3788 ++e;
3789 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003790 *matches = ga.ga_data;
3791 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003792
3793 vim_free(buf);
3794 vim_free(pat);
3795 if (mustfree)
3796 vim_free(path);
3797 hash_clear(&found_ht);
3798 return OK;
3799}
3800
K.Takata161b6ac2022-11-14 15:31:07 +00003801#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003802/*
3803 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003804 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003805 */
3806 static void *
3807call_user_expand_func(
3808 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003809 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003810{
3811 cmdline_info_T *ccline = get_cmdline_info();
3812 int keep = 0;
3813 typval_T args[4];
3814 sctx_T save_current_sctx = current_sctx;
3815 char_u *pat = NULL;
3816 void *ret;
3817
3818 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3819 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003820
3821 if (ccline->cmdbuff != NULL)
3822 {
3823 keep = ccline->cmdbuff[ccline->cmdlen];
3824 ccline->cmdbuff[ccline->cmdlen] = 0;
3825 }
3826
3827 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3828
3829 args[0].v_type = VAR_STRING;
3830 args[0].vval.v_string = pat;
3831 args[1].v_type = VAR_STRING;
3832 args[1].vval.v_string = xp->xp_line;
3833 args[2].v_type = VAR_NUMBER;
3834 args[2].vval.v_number = xp->xp_col;
3835 args[3].v_type = VAR_UNKNOWN;
3836
3837 current_sctx = xp->xp_script_ctx;
3838
3839 ret = user_expand_func(xp->xp_arg, 3, args);
3840
3841 current_sctx = save_current_sctx;
3842 if (ccline->cmdbuff != NULL)
3843 ccline->cmdbuff[ccline->cmdlen] = keep;
3844
3845 vim_free(pat);
3846 return ret;
3847}
3848
3849/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003850 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3851 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003852 */
3853 static int
3854ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003855 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003856 expand_T *xp,
3857 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003858 char_u ***matches,
3859 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003860{
3861 char_u *retstr;
3862 char_u *s;
3863 char_u *e;
3864 int keep;
3865 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003866 int fuzzy;
3867 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003868 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003869
3870 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003871 *matches = NULL;
3872 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003873
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003874 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003875 if (retstr == NULL)
3876 return FAIL;
3877
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003878 if (!fuzzy)
3879 ga_init2(&ga, sizeof(char *), 3);
3880 else
3881 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3882
Bram Moolenaar66b51422019-08-18 21:44:12 +02003883 for (s = retstr; *s != NUL; s = e)
3884 {
3885 e = vim_strchr(s, '\n');
3886 if (e == NULL)
3887 e = s + STRLEN(s);
3888 keep = *e;
3889 *e = NUL;
3890
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003891 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003892 {
3893 if (!fuzzy)
3894 match = vim_regexec(regmatch, s, (colnr_T)0);
3895 else
3896 {
3897 score = fuzzy_match_str(s, pat);
3898 match = (score != 0);
3899 }
3900 }
3901 else
3902 match = TRUE; // match everything
3903
Bram Moolenaar66b51422019-08-18 21:44:12 +02003904 *e = keep;
3905
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003906 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003907 {
John Marriott3b03b432025-06-28 20:41:54 +02003908 char_u *p = vim_strnsave(s, (size_t)(e - s));
3909 if (p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003910 break;
John Marriott3b03b432025-06-28 20:41:54 +02003911
3912 if (ga_grow(&ga, 1) == FAIL)
3913 {
3914 vim_free(p);
3915 break;
3916 }
3917
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003918 if (!fuzzy)
John Marriott3b03b432025-06-28 20:41:54 +02003919 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003920 else
3921 {
3922 fuzmatch_str_T *fuzmatch =
3923 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003924 fuzmatch->idx = ga.ga_len;
John Marriott3b03b432025-06-28 20:41:54 +02003925 fuzmatch->str = p;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003926 fuzmatch->score = score;
3927 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003928 ++ga.ga_len;
3929 }
3930
3931 if (*e != NUL)
3932 ++e;
3933 }
3934 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003935
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003936 if (ga.ga_len == 0)
3937 return OK;
3938
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003939 if (!fuzzy)
3940 {
3941 *matches = ga.ga_data;
3942 *numMatches = ga.ga_len;
3943 }
3944 else
3945 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003946 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3947 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003948 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003949 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003950 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003951 return OK;
3952}
3953
3954/*
3955 * Expand names with a list returned by a function defined by the user.
3956 */
3957 static int
3958ExpandUserList(
3959 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003960 char_u ***matches,
3961 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003962{
3963 list_T *retlist;
3964 listitem_T *li;
3965 garray_T ga;
3966
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003967 *matches = NULL;
3968 *numMatches = 0;
3969 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003970 if (retlist == NULL)
3971 return FAIL;
3972
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003973 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003974 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003975 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003976 {
John Marriott3b03b432025-06-28 20:41:54 +02003977 char_u *p;
3978
Bram Moolenaar66b51422019-08-18 21:44:12 +02003979 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3980 continue; // Skip non-string items and empty strings
3981
John Marriott3b03b432025-06-28 20:41:54 +02003982 p = vim_strsave(li->li_tv.vval.v_string);
3983 if (p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003984 break;
3985
John Marriott3b03b432025-06-28 20:41:54 +02003986 if (ga_grow(&ga, 1) == FAIL)
3987 {
3988 vim_free(p);
3989 break;
3990 }
3991
3992 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003993 ++ga.ga_len;
3994 }
3995 list_unref(retlist);
3996
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003997 *matches = ga.ga_data;
3998 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003999 return OK;
4000}
K.Takata161b6ac2022-11-14 15:31:07 +00004001#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02004002
4003/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02004004 * Expand "file" for all comma-separated directories in "path".
4005 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00004006 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02004007 */
4008 void
4009globpath(
4010 char_u *path,
4011 char_u *file,
4012 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00004013 int expand_options,
4014 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004015{
4016 expand_T xpc;
4017 char_u *buf;
4018 int i;
4019 int num_p;
4020 char_u **p;
4021
4022 buf = alloc(MAXPATHL);
4023 if (buf == NULL)
4024 return;
4025
4026 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00004027 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004028
4029 // Loop over all entries in {path}.
4030 while (*path != NUL)
4031 {
4032 // Copy one item of the path to buf[] and concatenate the file name.
4033 copy_option_part(&path, buf, MAXPATHL, ",");
4034 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4035 {
K.Takata161b6ac2022-11-14 15:31:07 +00004036#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004037 // Using the platform's path separator (\) makes vim incorrectly
4038 // treat it as an escape character, use '/' instead.
4039 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
4040 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00004041#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02004042 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00004043#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02004044 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004045 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02004046 WILD_SILENT|expand_options) != FAIL && num_p > 0)
4047 {
4048 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
4049
4050 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004051 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02004052 for (i = 0; i < num_p; ++i)
4053 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004054 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02004055 ++ga->ga_len;
4056 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004057 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004058 }
4059 }
4060 }
4061
4062 vim_free(buf);
4063}
Bram Moolenaar66b51422019-08-18 21:44:12 +02004064
Bram Moolenaareadee482020-09-04 15:37:31 +02004065/*
4066 * Translate some keys pressed when 'wildmenu' is used.
4067 */
4068 int
4069wildmenu_translate_key(
4070 cmdline_info_T *cclp,
4071 int key,
4072 expand_T *xp,
4073 int did_wild_list)
4074{
4075 int c = key;
4076
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00004077 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004078 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00004079 // When the popup menu is used for cmdline completion:
4080 // Up : go to the previous item in the menu
4081 // Down : go to the next item in the menu
4082 // Left : go to the parent directory
4083 // Right: list the files in the selected directory
4084 switch (c)
4085 {
4086 case K_UP: c = K_LEFT; break;
4087 case K_DOWN: c = K_RIGHT; break;
4088 case K_LEFT: c = K_UP; break;
4089 case K_RIGHT: c = K_DOWN; break;
4090 default: break;
4091 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004092 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004093
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004094 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02004095 {
4096 if (c == K_LEFT)
4097 c = Ctrl_P;
4098 else if (c == K_RIGHT)
4099 c = Ctrl_N;
4100 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004101
Bram Moolenaareadee482020-09-04 15:37:31 +02004102 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004103 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02004104 && cclp->cmdpos > 1
4105 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
4106 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
4107 && (c == '\n' || c == '\r' || c == K_KENTER))
4108 c = K_DOWN;
4109
4110 return c;
4111}
4112
4113/*
4114 * Delete characters on the command line, from "from" to the current
4115 * position.
4116 */
4117 static void
4118cmdline_del(cmdline_info_T *cclp, int from)
4119{
4120 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
4121 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
4122 cclp->cmdlen -= cclp->cmdpos - from;
4123 cclp->cmdpos = from;
4124}
4125
4126/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004127 * Handle a key pressed when the wild menu for the menu names
4128 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02004129 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004130 static int
4131wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02004132{
Bram Moolenaareadee482020-09-04 15:37:31 +02004133 int i;
4134 int j;
4135
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004136 // Hitting <Down> after "emenu Name.": complete submenu
4137 if (key == K_DOWN && cclp->cmdpos > 0
4138 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02004139 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004140 key = p_wc;
4141 KeyTyped = TRUE; // in case the key was mapped
4142 }
4143 else if (key == K_UP)
4144 {
4145 // Hitting <Up>: Remove one submenu name in front of the
4146 // cursor
4147 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004148
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004149 j = (int)(xp->xp_pattern - cclp->cmdbuff);
4150 i = 0;
4151 while (--j > 0)
4152 {
4153 // check for start of menu name
4154 if (cclp->cmdbuff[j] == ' '
4155 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02004156 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004157 i = j + 1;
4158 break;
4159 }
4160 // check for start of submenu name
4161 if (cclp->cmdbuff[j] == '.'
4162 && cclp->cmdbuff[j - 1] != '\\')
4163 {
4164 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004165 {
4166 i = j + 1;
4167 break;
4168 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004169 else
4170 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004171 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004172 }
4173 if (i > 0)
4174 cmdline_del(cclp, i);
4175 key = p_wc;
4176 KeyTyped = TRUE; // in case the key was mapped
4177 xp->xp_context = EXPAND_NOTHING;
4178 }
4179
4180 return key;
4181}
4182
4183/*
4184 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4185 * directory names (EXPAND_DIRECTORIES) or shell command names
4186 * (EXPAND_SHELLCMD) is displayed.
4187 */
4188 static int
4189wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4190{
4191 int i;
4192 int j;
4193 char_u upseg[5];
4194
4195 upseg[0] = PATHSEP;
4196 upseg[1] = '.';
4197 upseg[2] = '.';
4198 upseg[3] = PATHSEP;
4199 upseg[4] = NUL;
4200
4201 if (key == K_DOWN
4202 && cclp->cmdpos > 0
4203 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4204 && (cclp->cmdpos < 3
4205 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4206 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4207 {
4208 // go down a directory
4209 key = p_wc;
4210 KeyTyped = TRUE; // in case the key was mapped
4211 }
4212 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4213 {
4214 // If in a direct ancestor, strip off one ../ to go down
4215 int found = FALSE;
4216
4217 j = cclp->cmdpos;
4218 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4219 while (--j > i)
4220 {
4221 if (has_mbyte)
4222 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4223 if (vim_ispathsep(cclp->cmdbuff[j]))
4224 {
4225 found = TRUE;
4226 break;
4227 }
4228 }
4229 if (found
4230 && cclp->cmdbuff[j - 1] == '.'
4231 && cclp->cmdbuff[j - 2] == '.'
4232 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4233 {
4234 cmdline_del(cclp, j - 2);
4235 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004236 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004237 }
4238 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004239 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004240 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004241 // go up a directory
4242 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004243
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004244 j = cclp->cmdpos - 1;
4245 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4246 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004247 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004248 if (has_mbyte)
4249 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4250 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004251#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004252 && vim_strchr((char_u *)" *?[{`$%#",
4253 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004254#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004255 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004256 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004257 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004258 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004259 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004260 break;
4261 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004262 else
4263 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004264 }
4265 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004266
4267 if (!found)
4268 j = i;
4269 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4270 j += 4;
4271 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4272 && j == i)
4273 j += 3;
4274 else
4275 j = 0;
4276 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004277 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004278 // TODO this is only for DOS/UNIX systems - need to put in
4279 // machine-specific stuff here and in upseg init
4280 cmdline_del(cclp, j);
4281 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004282 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004283 else if (cclp->cmdpos > i)
4284 cmdline_del(cclp, i);
4285
4286 // Now complete in the new directory. Set KeyTyped in case the
4287 // Up key came from a mapping.
4288 key = p_wc;
4289 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004290 }
4291
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004292 return key;
4293}
4294
4295/*
4296 * Handle a key pressed when the wild menu is displayed
4297 */
4298 int
4299wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4300{
4301 if (xp->xp_context == EXPAND_MENUNAMES)
4302 return wildmenu_process_key_menunames(cclp, key, xp);
4303 else if ((xp->xp_context == EXPAND_FILES
4304 || xp->xp_context == EXPAND_DIRECTORIES
4305 || xp->xp_context == EXPAND_SHELLCMD))
4306 return wildmenu_process_key_filenames(cclp, key, xp);
4307
4308 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004309}
4310
4311/*
4312 * Free expanded names when finished walking through the matches
4313 */
4314 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004315wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004316{
4317 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004318
4319 if (!p_wmnu || wild_menu_showing == 0)
4320 return;
4321
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004322#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004323 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004324 if (cclp->input_fn)
4325 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004326#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004327
Girish Palya6b49fba2025-06-28 19:47:34 +02004328#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
4329 // Clear highlighting applied during wildmenu activity
4330 set_no_hlsearch(TRUE);
4331#endif
4332
Bram Moolenaareadee482020-09-04 15:37:31 +02004333 if (wild_menu_showing == WM_SCROLLED)
4334 {
4335 // Entered command line, move it up
4336 cmdline_row--;
4337 redrawcmd();
4338 }
4339 else if (save_p_ls != -1)
4340 {
4341 // restore 'laststatus' and 'winminheight'
4342 p_ls = save_p_ls;
4343 p_wmh = save_p_wmh;
4344 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004345 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004346 redrawcmd();
4347 save_p_ls = -1;
4348 }
4349 else
4350 {
4351 win_redraw_last_status(topframe);
4352 redraw_statuslines();
4353 }
4354 KeyTyped = skt;
4355 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004356#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004357 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004358 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004359#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004360}
Bram Moolenaareadee482020-09-04 15:37:31 +02004361
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004362#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004363/*
4364 * "getcompletion()" function
4365 */
4366 void
4367f_getcompletion(typval_T *argvars, typval_T *rettv)
4368{
4369 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004370 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004371 expand_T xpc;
4372 int filtered = FALSE;
4373 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004374 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004375
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004376 if (in_vim9script()
4377 && (check_for_string_arg(argvars, 0) == FAIL
4378 || check_for_string_arg(argvars, 1) == FAIL
4379 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4380 return;
4381
ii144785fe02021-11-21 12:13:56 +00004382 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004383 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004384 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004385 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004386
4387 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004388 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004389
4390 if (p_wic)
4391 options |= WILD_ICASE;
4392
4393 // For filtered results, 'wildignore' is used
4394 if (!filtered)
4395 options |= WILD_KEEP_ALL;
4396
4397 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004398 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004399 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004400 int cmdline_len = (int)STRLEN(pat);
4401 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004402 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004403 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004404 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004405 else
4406 {
ii144785fe02021-11-21 12:13:56 +00004407 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004408 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004409 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004410
4411 xpc.xp_context = cmdcomplete_str_to_type(type);
4412 if (xpc.xp_context == EXPAND_NOTHING)
4413 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004414 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004415 return;
4416 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004417
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004418 if (xpc.xp_context == EXPAND_USER_DEFINED)
4419 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004420 // Must be "custom,funcname" pattern
4421 if (STRNCMP(type, "custom,", 7) != 0)
4422 {
4423 semsg(_(e_invalid_argument_str), type);
4424 return;
4425 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004426
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004427 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004428 }
4429
4430 if (xpc.xp_context == EXPAND_USER_LIST)
4431 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004432 // Must be "customlist,funcname" pattern
4433 if (STRNCMP(type, "customlist,", 11) != 0)
4434 {
4435 semsg(_(e_invalid_argument_str), type);
4436 return;
4437 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004438
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004439 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004440 }
4441
Bram Moolenaar66b51422019-08-18 21:44:12 +02004442# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004443 if (xpc.xp_context == EXPAND_MENUS)
4444 {
4445 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4446 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4447 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004448# endif
4449# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004450 if (xpc.xp_context == EXPAND_CSCOPE)
4451 {
4452 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4453 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4454 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004455# endif
4456# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004457 if (xpc.xp_context == EXPAND_SIGN)
4458 {
4459 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4460 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4461 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004462# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004463 if (xpc.xp_context == EXPAND_RUNTIME)
4464 {
4465 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4466 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4467 }
zeertzjq85f36d62024-10-10 19:14:13 +02004468 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4469 {
4470 int context = EXPAND_SHELLCMDLINE;
4471 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4472 &context);
4473 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4474 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02004475 if (xpc.xp_context == EXPAND_FILETYPECMD)
4476 filetype_expand_what = EXP_FILETYPECMD_ALL;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004477 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004478
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004479 if (cmdline_fuzzy_completion_supported(&xpc))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004480 // when fuzzy matching, don't modify the search string
4481 pat = vim_strsave(xpc.xp_pattern);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004482 else
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004483 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004484
Bram Moolenaar93a10962022-06-16 11:42:09 +01004485 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004486 {
4487 int i;
4488
4489 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4490
4491 for (i = 0; i < xpc.xp_numfiles; i++)
4492 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4493 }
4494 vim_free(pat);
4495 ExpandCleanup(&xpc);
4496}
Girish Palya92f68e22025-04-21 11:12:41 +02004497
4498/*
4499 * "cmdcomplete_info()" function
4500 */
4501 void
4502f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4503{
4504 cmdline_info_T *ccline = get_cmdline_info();
4505 dict_T *retdict;
4506 list_T *li;
4507 int idx;
4508 int ret = OK;
4509
4510 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4511 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4512 return;
4513 retdict = rettv->vval.v_dict;
4514 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4515 if (ret == OK)
4516 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4517 if (ret == OK)
4518 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4519 if (ret == OK)
4520 {
4521 li = list_alloc();
4522 if (li == NULL)
4523 return;
4524 ret = dict_add_list(retdict, "matches", li);
4525 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4526 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4527 }
4528}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004529#endif // FEAT_EVAL
Girish Palya6b49fba2025-06-28 19:47:34 +02004530
4531/*
4532 * Copy a substring from the current buffer (curbuf), spanning from the given
4533 * 'start' position to the word boundary after 'end' position.
4534 * The copied string is stored in '*match', and the actual end position of the
4535 * matched text is returned in '*match_end'.
4536 */
4537 static int
4538copy_substring_from_pos(pos_T *start, pos_T *end, char_u **match,
4539 pos_T *match_end)
4540{
4541 char_u *word_end;
4542 char_u *line, *start_line, *end_line;
4543 int segment_len;
4544 linenr_T lnum;
4545 garray_T ga;
4546
4547 if (start->lnum > end->lnum
4548 || (start->lnum == end->lnum && start->col >= end->col))
4549 return FAIL; // invalid range
4550
4551 // Get line pointers
4552 start_line = ml_get(start->lnum);
4553 end_line = ml_get(end->lnum);
4554
4555 // Use a growable string (ga)
4556 ga_init2(&ga, 1, 128);
4557
4558 // Append start line from start->col to end
4559 char_u *start_ptr = start_line + start->col;
4560 int is_single_line = start->lnum == end->lnum;
4561
4562 segment_len = is_single_line ? (end->col - start->col)
4563 : (int)STRLEN(start_ptr);
4564 if (ga_grow(&ga, segment_len + 1) != OK)
4565 return FAIL;
4566 ga_concat_len(&ga, start_ptr, segment_len);
4567 if (!is_single_line)
4568 ga_append(&ga, '\n');
4569
4570 // Append full lines between start and end
4571 if (!is_single_line)
4572 for (lnum = start->lnum + 1; lnum < end->lnum; lnum++)
4573 {
4574 line = ml_get(lnum);
4575 if (ga_grow(&ga, ml_get_len(lnum) + 1) != OK)
4576 return FAIL;
4577 ga_concat(&ga, line);
4578 ga_append(&ga, '\n');
4579 }
4580
4581 // Append partial end line (up to word end)
4582 word_end = find_word_end(end_line + end->col);
4583 segment_len = (int)(word_end - end_line);
4584 if (ga_grow(&ga, segment_len) != OK)
4585 return FAIL;
4586 ga_concat_len(&ga, end_line + (is_single_line ? end->col : 0),
4587 segment_len - (is_single_line ? end->col : 0));
4588
4589 // Null-terminate
4590 if (ga_grow(&ga, 1) != OK)
4591 return FAIL;
4592 ga_append(&ga, NUL);
4593
4594 *match = (char_u *)ga.ga_data;
4595 match_end->lnum = end->lnum;
4596 match_end->col = segment_len;
4597
4598 return OK;
4599}
4600
4601/*
4602 * Search for strings matching "pat" in the specified range and return them.
4603 * Returns OK on success, FAIL otherwise.
4604 */
4605 static int
4606expand_pattern_in_buf(
4607 char_u *pat, // pattern to match
4608 int dir, // direction: FORWARD or BACKWARD
4609 char_u ***matches, // return: array with matched strings
4610 int *numMatches) // return: number of matches
4611{
4612 pos_T cur_match_pos, prev_match_pos, end_match_pos, word_end_pos;
4613 garray_T ga;
4614 int found_new_match;
4615 int looped_around = FALSE;
4616 int pat_len, match_len;
4617 int has_range = FALSE;
4618 int compl_started = FALSE;
4619 int search_flags, i;
4620 char_u *match, *line, *word_end;
4621 regmatch_T regmatch;
4622
4623#ifdef FEAT_SEARCH_EXTRA
4624 has_range = search_first_line != 0;
4625#endif
4626
4627 *matches = NULL;
4628 *numMatches = 0;
4629
4630 if (pat == NULL || *pat == NUL)
4631 return FAIL;
4632
4633 pat_len = (int)STRLEN(pat);
4634 CLEAR_FIELD(cur_match_pos);
4635 CLEAR_FIELD(prev_match_pos);
4636#ifdef FEAT_SEARCH_EXTRA
4637 if (has_range)
4638 cur_match_pos.lnum = search_first_line;
4639 else
4640#endif
4641 cur_match_pos = pre_incsearch_pos;
4642
4643 search_flags = SEARCH_OPT | SEARCH_NOOF | SEARCH_PEEK | SEARCH_NFMSG
4644 | (has_range ? SEARCH_START : 0);
4645
4646 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4647 if (regmatch.regprog == NULL)
4648 return FAIL;
4649 regmatch.rm_ic = p_ic;
4650
4651 ga_init2(&ga, sizeof(char_u *), 10); // Use growable array of char_u*
4652
4653 for (;;)
4654 {
4655 ++emsg_off;
4656 ++msg_silent;
4657 found_new_match = searchit(NULL, curbuf, &cur_match_pos,
4658 &end_match_pos, dir, pat, pat_len, 1L,
4659 search_flags, RE_LAST, NULL);
4660 --msg_silent;
4661 --emsg_off;
4662
4663 if (found_new_match == FAIL)
4664 break;
4665
4666#ifdef FEAT_SEARCH_EXTRA
4667 // If in range mode, check if match is within the range
4668 if (has_range && (cur_match_pos.lnum < search_first_line
4669 || cur_match_pos.lnum > search_last_line))
4670 break;
4671#endif
4672
4673 if (compl_started)
4674 {
4675 // If we've looped back to an earlier match, stop
4676 if ((dir == FORWARD
4677 && (cur_match_pos.lnum < prev_match_pos.lnum
4678 || (cur_match_pos.lnum == prev_match_pos.lnum
4679 && cur_match_pos.col <= prev_match_pos.col)))
4680 || (dir == BACKWARD
4681 && (cur_match_pos.lnum > prev_match_pos.lnum
4682 || (cur_match_pos.lnum == prev_match_pos.lnum
4683 && cur_match_pos.col >= prev_match_pos.col))))
4684 {
4685 if (looped_around)
4686 break;
4687 else
4688 looped_around = TRUE;
4689 }
4690 }
4691
4692 compl_started = TRUE;
4693 prev_match_pos = cur_match_pos;
4694
4695 // Abort if user typed a character or interrupted
4696 if (char_avail() || got_int)
4697 {
4698 if (got_int)
4699 {
4700 (void)vpeekc(); // Remove <C-C> from input stream
4701 got_int = FALSE; // Don't abandon the command line
4702 }
4703 goto cleanup;
4704 }
4705
4706 // searchit() can return line number +1 past the last line when
4707 // searching for "foo\n" if "foo" is at end of buffer.
4708 if (end_match_pos.lnum > curbuf->b_ml.ml_line_count)
4709 {
4710 cur_match_pos.lnum = 1;
4711 cur_match_pos.col = 0;
4712 cur_match_pos.coladd = 0;
4713 continue;
4714 }
4715
4716 // Extract the matching text prepended to completed word
4717 if (!copy_substring_from_pos(&cur_match_pos, &end_match_pos, &match,
4718 &word_end_pos))
4719 break;
4720
4721 // Verify that the constructed match actually matches the pattern with
4722 // correct case sensitivity
4723 if (!vim_regexec_nl(&regmatch, match, (colnr_T)0))
4724 {
4725 vim_free(match);
4726 continue;
4727 }
4728 vim_free(match);
4729
4730 // Construct a new match from completed word appended to pattern itself
4731 line = ml_get(end_match_pos.lnum);
4732 word_end = find_word_end(line + end_match_pos.col); // col starts from 0
4733 match_len = (int)(word_end - (line + end_match_pos.col));
4734 match = alloc(match_len + pat_len + 1); // +1 for NUL
4735 if (match == NULL)
4736 goto cleanup;
4737 mch_memmove(match, pat, pat_len);
4738 if (match_len > 0)
4739 mch_memmove(match + pat_len, line + end_match_pos.col, match_len);
4740 match[pat_len + match_len] = NUL;
4741
4742 // Include this match if it is not a duplicate
4743 for (i = 0; i < ga.ga_len; ++i)
4744 {
4745 if (STRCMP(match, ((char_u **)ga.ga_data)[i]) == 0)
4746 {
4747 VIM_CLEAR(match);
4748 break;
4749 }
4750 }
4751 if (match != NULL)
4752 {
4753 if (ga_grow(&ga, 1) == FAIL)
4754 goto cleanup;
4755 ((char_u **)ga.ga_data)[ga.ga_len++] = match;
4756 if (ga.ga_len > TAG_MANY)
4757 break;
4758 }
4759 if (has_range)
4760 cur_match_pos = word_end_pos;
4761 }
4762
4763 vim_regfree(regmatch.regprog);
4764
4765 *matches = (char_u **)ga.ga_data;
4766 *numMatches = ga.ga_len;
4767 return OK;
4768
4769cleanup:
4770 vim_regfree(regmatch.regprog);
4771 ga_clear_strings(&ga);
4772 return FAIL;
4773}